@@ -126,8 +126,18 @@ STATIC_ASSERT(ARRAY_SIZE(indexFile) < BPB_SECTOR_SIZE);
126126#define NUM_FILES (ARRAY_SIZE(info))
127127#define NUM_DIRENTRIES (NUM_FILES + 1) // Code adds volume label as first root directory entry
128128
129- #define UF2_SIZE ((USER_FLASH_END-USER_FLASH_START) * 2)
130- #define UF2_SECTORS (UF2_SIZE / 512)
129+ #define UF2_FIRMWARE_BYTES_PER_SECTOR 256
130+ #define TRUE_USER_FLASH_SIZE (USER_FLASH_END-USER_FLASH_START)
131+ STATIC_ASSERT (TRUE_USER_FLASH_SIZE % UF2_FIRMWARE_BYTES_PER_SECTOR == 0 );
132+
133+ #define UF2_SECTORS ( (TRUE_USER_FLASH_SIZE / UF2_FIRMWARE_BYTES_PER_SECTOR) + \
134+ ((TRUE_USER_FLASH_SIZE % UF2_FIRMWARE_BYTES_PER_SECTOR) ? 1 : 0))
135+ #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 );
140+
131141#define UF2_FIRST_SECTOR (NUM_FILES + 1) // WARNING -- code presumes each non-UF2 file content fits in single sector
132142#define UF2_LAST_SECTOR (UF2_FIRST_SECTOR + UF2_SECTORS - 1)
133143
@@ -144,10 +154,10 @@ STATIC_ASSERT(NUM_DIRENTRIES < DIRENTRIES_PER_SECTOR);
144154static FAT_BootBlock const BootBlock = {
145155 .JumpInstruction = {0xeb , 0x3c , 0x90 },
146156 .OEMInfo = "UF2 UF2 " ,
147- .SectorSize = 512 ,
148- .SectorsPerCluster = 1 ,
157+ .SectorSize = BPB_SECTOR_SIZE ,
158+ .SectorsPerCluster = BPB_SECTORS_PER_CLUSTER ,
149159 .ReservedSectors = BPB_RESERVED_SECTORS ,
150- .FATCopies = 2 ,
160+ .FATCopies = BPB_NUMBER_OF_FATS ,
151161 .RootDirectoryEntries = (ROOT_DIR_SECTORS * DIRENTRIES_PER_SECTOR ),
152162 .TotalSectors16 = NUM_FAT_BLOCKS - 2 ,
153163 .MediaDescriptor = 0xF8 ,
@@ -236,19 +246,20 @@ void padded_memcpy (char *dst, char const *src, int len)
236246}
237247
238248void read_block (uint32_t block_no , uint8_t * data ) {
239- memset (data , 0 , 512 );
249+ memset (data , 0 , BPB_SECTOR_SIZE );
240250 uint32_t sectionIdx = block_no ;
241251
242252 if (block_no == 0 ) { // Requested boot block
243253 memcpy (data , & BootBlock , sizeof (BootBlock ));
244- data [510 ] = 0x55 ;
245- data [511 ] = 0xaa ;
254+ data [510 ] = 0x55 ; // Always at offsets 510/511, even when BPB_SECTOR_SIZE is larger
255+ data [511 ] = 0xaa ; // Always at offsets 510/511, even when BPB_SECTOR_SIZE is larger
246256 // logval("data[0]", data[0]);
247257 } else if (block_no < START_ROOTDIR ) { // Requested FAT table sector
248258 sectionIdx -= START_FAT0 ;
249259 // logval("sidx", sectionIdx);
250- if (sectionIdx >= BPB_SECTORS_PER_FAT )
260+ if (sectionIdx >= BPB_SECTORS_PER_FAT ) {
251261 sectionIdx -= BPB_SECTORS_PER_FAT ; // second FAT is same as the first...
262+ }
252263 if (sectionIdx == 0 ) {
253264 data [0 ] = 0xf8 ; // first FAT entry must match BPB MediaDescriptor
254265 // WARNING -- code presumes only one NULL .content for .UF2 file
@@ -538,5 +549,6 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state)
538549 }
539550 }
540551
541- return 512 ;
552+ STATIC_ASSERT (BPB_SECTOR_SIZE == 512 ); // if sector size changes, may need to re-validate this code
553+ return BPB_SECTOR_SIZE ;
542554}
0 commit comments