Skip to content

Commit 12c95ee

Browse files
committed
oofatfs: Allow fat32 mkfs to be compiled out
Saves 508 bytes
1 parent b9ecb0f commit 12c95ee

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

lib/oofatfs/ff.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5392,7 +5392,9 @@ FRESULT f_mkfs (
53925392
const UINT n_fats = 1; /* Number of FATs for FAT/FAT32 volume (1 or 2) */
53935393
const UINT n_rootdir = 512; /* Number of root directory entries for FAT volume */
53945394
static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0}; /* Cluster size boundary for FAT volume (4Ks unit) */
5395+
#if FF_MKFS_FAT32
53955396
static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0}; /* Cluster size boundary for FAT32 volume (128Ks unit) */
5397+
#endif
53965398
BYTE fmt, sys, *buf, *pte, part; void *pdrv;
53975399
WORD ss; /* Sector size */
53985400
DWORD szb_buf, sz_buf, sz_blk, n_clst, pau, sect, nsect, n;
@@ -5464,7 +5466,7 @@ FRESULT f_mkfs (
54645466
}
54655467
}
54665468
if (au > 128) LEAVE_MKFS(FR_INVALID_PARAMETER); /* Too large au for FAT/FAT32 */
5467-
if (opt & FM_FAT32) { /* FAT32 possible? */
5469+
if (FF_MKFS_FAT32 && (opt & FM_FAT32)) { /* FAT32 possible? */
54685470
if ((opt & FM_ANY) == FM_FAT32 || !(opt & FM_FAT)) { /* FAT32 only or no-FAT? */
54695471
fmt = FS_FAT32; break;
54705472
}
@@ -5641,6 +5643,7 @@ FRESULT f_mkfs (
56415643
do {
56425644
pau = au;
56435645
/* Pre-determine number of clusters and FAT sub-type */
5646+
#if FF_MKFS_FAT32
56445647
if (fmt == FS_FAT32) { /* FAT32 volume */
56455648
if (pau == 0) { /* au auto-selection */
56465649
n = sz_vol / 0x20000; /* Volume size in unit of 128KS */
@@ -5651,7 +5654,9 @@ FRESULT f_mkfs (
56515654
sz_rsv = 32; /* Number of reserved sectors */
56525655
sz_dir = 0; /* No static directory */
56535656
if (n_clst <= MAX_FAT16 || n_clst > MAX_FAT32) LEAVE_MKFS(FR_MKFS_ABORTED);
5654-
} else { /* FAT volume */
5657+
} else
5658+
#endif
5659+
{ /* FAT volume */
56555660
if (pau == 0) { /* au auto-selection */
56565661
n = sz_vol / 0x1000; /* Volume size in unit of 4KS */
56575662
for (i = 0, pau = 1; cst[i] && cst[i] <= n; i++, pau <<= 1) ; /* Get from table */
@@ -5681,12 +5686,14 @@ FRESULT f_mkfs (
56815686
/* Determine number of clusters and final check of validity of the FAT sub-type */
56825687
if (sz_vol < b_data + pau * 16 - b_vol) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume */
56835688
n_clst = (sz_vol - sz_rsv - sz_fat * n_fats - sz_dir) / pau;
5689+
#if FF_MKFS_FAT32
56845690
if (fmt == FS_FAT32) {
56855691
if (n_clst <= MAX_FAT16) { /* Too few clusters for FAT32 */
56865692
if (au == 0 && (au = pau / 2) != 0) continue; /* Adjust cluster size and retry */
56875693
LEAVE_MKFS(FR_MKFS_ABORTED);
56885694
}
56895695
}
5696+
#endif
56905697
if (fmt == FS_FAT16) {
56915698
if (n_clst > MAX_FAT16) { /* Too many clusters for FAT16 */
56925699
if (au == 0 && (pau * 2) <= 64) {
@@ -5720,7 +5727,11 @@ FRESULT f_mkfs (
57205727
buf[BPB_SecPerClus] = (BYTE)pau; /* Cluster size [sector] */
57215728
st_word(buf + BPB_RsvdSecCnt, (WORD)sz_rsv); /* Size of reserved area */
57225729
buf[BPB_NumFATs] = (BYTE)n_fats; /* Number of FATs */
5730+
#if FF_MKFS_FAT32
57235731
st_word(buf + BPB_RootEntCnt, (WORD)((fmt == FS_FAT32) ? 0 : n_rootdir)); /* Number of root directory entries */
5732+
#else
5733+
st_word(buf + BPB_RootEntCnt, (WORD) n_rootdir); /* Number of root directory entries */
5734+
#endif
57245735
if (sz_vol < 0x10000) {
57255736
st_word(buf + BPB_TotSec16, (WORD)sz_vol); /* Volume size in 16-bit LBA */
57265737
} else {
@@ -5730,6 +5741,7 @@ FRESULT f_mkfs (
57305741
st_word(buf + BPB_SecPerTrk, 63); /* Number of sectors per track (for int13) */
57315742
st_word(buf + BPB_NumHeads, 255); /* Number of heads (for int13) */
57325743
st_dword(buf + BPB_HiddSec, b_vol); /* Volume offset in the physical drive [sector] */
5744+
#if FF_MKFS_FAT32
57335745
if (fmt == FS_FAT32) {
57345746
st_dword(buf + BS_VolID32, GET_FATTIME()); /* VSN */
57355747
st_dword(buf + BPB_FATSz32, sz_fat); /* FAT size [sector] */
@@ -5739,7 +5751,9 @@ FRESULT f_mkfs (
57395751
buf[BS_DrvNum32] = 0x80; /* Drive number (for int13) */
57405752
buf[BS_BootSig32] = 0x29; /* Extended boot signature */
57415753
mem_cpy(buf + BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */
5742-
} else {
5754+
} else
5755+
#endif
5756+
{
57435757
st_dword(buf + BS_VolID, GET_FATTIME()); /* VSN */
57445758
st_word(buf + BPB_FATSz16, (WORD)sz_fat); /* FAT size [sector] */
57455759
buf[BS_DrvNum] = 0x80; /* Drive number (for int13) */
@@ -5750,6 +5764,7 @@ FRESULT f_mkfs (
57505764
if (disk_write(pdrv, buf, b_vol, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it to the VBR sector */
57515765

57525766
/* Create FSINFO record if needed */
5767+
#if FF_MKFS_FAT32
57535768
if (fmt == FS_FAT32) {
57545769
disk_write(pdrv, buf, b_vol + 6, 1); /* Write backup VBR (VBR + 6) */
57555770
mem_set(buf, 0, ss);
@@ -5761,16 +5776,20 @@ FRESULT f_mkfs (
57615776
disk_write(pdrv, buf, b_vol + 7, 1); /* Write backup FSINFO (VBR + 7) */
57625777
disk_write(pdrv, buf, b_vol + 1, 1); /* Write original FSINFO (VBR + 1) */
57635778
}
5779+
#endif
57645780

57655781
/* Initialize FAT area */
57665782
mem_set(buf, 0, (UINT)szb_buf);
57675783
sect = b_fat; /* FAT start sector */
57685784
for (i = 0; i < n_fats; i++) { /* Initialize FATs each */
5785+
#if FF_MKFS_FAT32
57695786
if (fmt == FS_FAT32) {
57705787
st_dword(buf + 0, 0xFFFFFFF8); /* Entry 0 */
57715788
st_dword(buf + 4, 0xFFFFFFFF); /* Entry 1 */
57725789
st_dword(buf + 8, 0x0FFFFFFF); /* Entry 2 (root directory) */
5773-
} else {
5790+
} else
5791+
#endif
5792+
{
57745793
st_dword(buf + 0, (fmt == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* Entry 0 and 1 */
57755794
}
57765795
nsect = sz_fat; /* Number of FAT sectors */
@@ -5783,7 +5802,11 @@ FRESULT f_mkfs (
57835802
}
57845803

57855804
/* Initialize root directory (fill with zero) */
5805+
#if FF_MKFS_FAT32
57865806
nsect = (fmt == FS_FAT32) ? pau : sz_dir; /* Number of root directory sectors */
5807+
#else
5808+
nsect = sz_dir; /* Number of root directory sectors */
5809+
#endif
57875810
do {
57885811
n = (nsect > sz_buf) ? sz_buf : nsect;
57895812
if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
@@ -5795,7 +5818,7 @@ FRESULT f_mkfs (
57955818
if (FF_FS_EXFAT && fmt == FS_EXFAT) {
57965819
sys = 0x07; /* HPFS/NTFS/exFAT */
57975820
} else {
5798-
if (fmt == FS_FAT32) {
5821+
if (FF_MKFS_FAT32 && fmt == FS_FAT32) {
57995822
sys = 0x0C; /* FAT32X */
58005823
} else {
58015824
if (sz_vol >= 0x10000) {

lib/oofatfs/ffconf.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@
7272
#define FF_USE_MKFS 1
7373
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
7474

75+
#ifdef MICROPY_FF_MKFS_FAT32
76+
#define FF_MKFS_FAT32 MICROPY_FF_MKFS_FAT32
77+
#else
78+
#define FF_MKFS_FAT32 0
79+
#endif
80+
/* This option switches off FAT32 support in f_mkfs() */
7581

7682
#define FF_USE_FASTSEEK 1
7783
/* This option switches fast seek function. (0:Disable or 1:Enable) */

ports/unix/variants/coverage/mpconfigvariant.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#define MICROPY_PY_URE_SUB (1)
6060
#define MICROPY_VFS_POSIX (1)
6161
#define MICROPY_FATFS_USE_LABEL (1)
62+
#define MICROPY_FF_MKFS_FAT32 (1)
6263
#define MICROPY_PY_FRAMEBUF (1)
6364
#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (1)
6465
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1)

0 commit comments

Comments
 (0)