@@ -89,8 +89,6 @@ STATIC_ASSERT(BPB_SECTOR_SIZE % sizeof(DirEntry) == 0); // FAT
8989STATIC_ASSERT (BPB_ROOT_DIR_ENTRIES % DIRENTRIES_PER_SECTOR == 0 ); // FAT requirement
9090STATIC_ASSERT (BPB_SECTOR_SIZE * BPB_SECTORS_PER_CLUSTER <= (32 * 1024 )); // FAT requirement (64k+ has known compatibility problems)
9191
92- STATIC_ASSERT (ROOT_DIR_SECTORS == 4 ); // Not required ... just validating equal to prior code
93-
9492#define STR0 (x ) #x
9593#define STR (x ) STR0(x)
9694
@@ -125,6 +123,14 @@ STATIC_ASSERT(ARRAY_SIZE(indexFile) < BPB_SECTOR_SIZE);
125123
126124#define NUM_FILES (ARRAY_SIZE(info))
127125#define NUM_DIRENTRIES (NUM_FILES + 1) // Code adds volume label as first root directory entry
126+ #define REQUIRED_ROOT_DIRECTORY_SECTORS ( ((NUM_DIRENTRIES+1) / DIRENTRIES_PER_SECTOR) + \
127+ (((NUM_DIRENTRIES+1) % DIRENTRIES_PER_SECTOR) ? 1 : 0))
128+ STATIC_ASSERT (NUM_DIRENTRIES < (DIRENTRIES_PER_SECTOR * ROOT_DIR_SECTORS )); // Need at least one terminating (unused) entry
129+ STATIC_ASSERT (ROOT_DIR_SECTORS >= REQUIRED_ROOT_DIRECTORY_SECTORS ); // Ensures BPB reserves sufficient entries for files
130+ STATIC_ASSERT (NUM_DIRENTRIES < BPB_ROOT_DIR_ENTRIES );
131+ // all directory entries must fit in a single sector
132+ // because otherwise current code overflows buffer
133+ STATIC_ASSERT (NUM_DIRENTRIES < DIRENTRIES_PER_SECTOR );
128134
129135#define UF2_FIRMWARE_BYTES_PER_SECTOR 256
130136#define TRUE_USER_FLASH_SIZE (USER_FLASH_END-USER_FLASH_START)
@@ -133,23 +139,15 @@ STATIC_ASSERT(TRUE_USER_FLASH_SIZE % UF2_FIRMWARE_BYTES_PER_SECTOR == 0);
133139#define UF2_SECTORS ( (TRUE_USER_FLASH_SIZE / UF2_FIRMWARE_BYTES_PER_SECTOR) + \
134140 ((TRUE_USER_FLASH_SIZE % UF2_FIRMWARE_BYTES_PER_SECTOR) ? 1 : 0))
135141#define UF2_SIZE (UF2_SECTORS * BPB_SECTOR_SIZE)
136- #define UF2_SIZE_OLD ((USER_FLASH_END-USER_FLASH_START) * 2)
137- #define UF2_SECTORS_OLD (UF2_SIZE / 512)
138- STATIC_ASSERT (UF2_SECTORS == UF2_SECTORS_OLD );
139- STATIC_ASSERT (UF2_SIZE == UF2_SIZE_OLD );
140142
141- #define UF2_FIRST_SECTOR (NUM_FILES + 1) // WARNING -- code presumes each non-UF2 file content fits in single sector
142- #define UF2_LAST_SECTOR (UF2_FIRST_SECTOR + UF2_SECTORS - 1)
143+ #define UF2_FIRST_SECTOR (( NUM_FILES + 1) * BPB_SECTORS_PER_CLUSTER ) // WARNING -- code presumes each non-UF2 file content fits in single sector
144+ #define UF2_LAST_SECTOR (( UF2_FIRST_SECTOR + UF2_SECTORS - 1) * BPB_SECTORS_PER_CLUSTER )
143145
144146#define START_FAT0 BPB_RESERVED_SECTORS
145147#define START_FAT1 (START_FAT0 + BPB_SECTORS_PER_FAT)
146148#define START_ROOTDIR (START_FAT1 + BPB_SECTORS_PER_FAT)
147149#define START_CLUSTERS (START_ROOTDIR + ROOT_DIR_SECTORS)
148150
149- STATIC_ASSERT (NUM_DIRENTRIES < BPB_ROOT_DIR_ENTRIES );
150- // all directory entries must fit in a single sector
151- // because otherwise current code overflows buffer
152- STATIC_ASSERT (NUM_DIRENTRIES < DIRENTRIES_PER_SECTOR );
153151
154152static FAT_BootBlock const BootBlock = {
155153 .JumpInstruction = {0xeb , 0x3c , 0x90 },
@@ -160,7 +158,7 @@ static FAT_BootBlock const BootBlock = {
160158 .FATCopies = BPB_NUMBER_OF_FATS ,
161159 .RootDirectoryEntries = (ROOT_DIR_SECTORS * DIRENTRIES_PER_SECTOR ),
162160 .TotalSectors16 = NUM_FAT_BLOCKS - 2 ,
163- .MediaDescriptor = 0xF8 ,
161+ .MediaDescriptor = BPB_MEDIA_DESCRIPTOR_BYTE ,
164162 .SectorsPerFAT = BPB_SECTORS_PER_FAT ,
165163 .SectorsPerTrack = 1 ,
166164 .Heads = 1 ,
@@ -261,7 +259,8 @@ void read_block(uint32_t block_no, uint8_t *data) {
261259 sectionIdx -= BPB_SECTORS_PER_FAT ; // second FAT is same as the first...
262260 }
263261 if (sectionIdx == 0 ) {
264- data [0 ] = 0xf8 ; // first FAT entry must match BPB MediaDescriptor
262+ // first FAT entry must match BPB MediaDescriptor
263+ data [0 ] = BPB_MEDIA_DESCRIPTOR_BYTE ;
265264 // WARNING -- code presumes only one NULL .content for .UF2 file
266265 // and all non-NULL .content fit in one sector
267266 // and requires it be the last element of the array
0 commit comments