@@ -177,6 +177,7 @@ static FAT_BootBlock const BootBlock = {
177
177
.SectorsPerFAT = SECTORS_PER_FAT ,
178
178
.SectorsPerTrack = 1 ,
179
179
.Heads = 1 ,
180
+ .PhysicalDriveNum = 0x80 , // to match MediaDescriptor of 0xF8
180
181
.ExtendedBootSig = 0x29 ,
181
182
.VolumeSerialNumber = 0x00420042 ,
182
183
.VolumeLabel = VOLUME_LABEL ,
@@ -190,35 +191,37 @@ static FAT_BootBlock const BootBlock = {
190
191
static uint32_t current_flash_size (void )
191
192
{
192
193
static uint32_t flash_sz = 0 ;
194
+ uint32_t result = flash_sz ; // presumes atomic 32-bit read/write and static result
193
195
194
196
// only need to compute once
195
- if ( flash_sz == 0 )
197
+ if ( result == 0 )
196
198
{
197
199
// return 1 block of 256 bytes
198
200
if ( !bootloader_app_is_valid (DFU_BANK_0_REGION_START ) )
199
201
{
200
- flash_sz = 256 ;
202
+ result = 256 ;
201
203
}else
202
204
{
203
205
bootloader_settings_t const * boot_setting ;
204
206
bootloader_util_settings_get (& boot_setting );
205
207
206
- flash_sz = boot_setting -> bank_0_size ;
208
+ result = boot_setting -> bank_0_size ;
207
209
208
210
// Copy size must be multiple of 256 bytes
209
211
// else we will got an issue copying current.uf2
210
- if (flash_sz & 0xff )
212
+ if (result & 0xff )
211
213
{
212
- flash_sz = (flash_sz & ~0xff ) + 256 ;
214
+ result = (result & ~0xff ) + 256 ;
213
215
}
214
216
215
217
// if bank0 size is not valid, happens when flashed with jlink
216
218
// use maximum application size
217
- if ( (flash_sz == 0 ) || (flash_sz == 0xFFFFFFFFUL ) )
219
+ if ( (result == 0 ) || (result == 0xFFFFFFFFUL ) )
218
220
{
219
- flash_sz = FLASH_SIZE ;
221
+ result = FLASH_SIZE ;
220
222
}
221
223
}
224
+ flash_sz = result ; // presumes atomic 32-bit read/write and static result
222
225
}
223
226
224
227
return flash_sz ;
@@ -243,16 +246,16 @@ void read_block(uint32_t block_no, uint8_t *data) {
243
246
memset (data , 0 , 512 );
244
247
uint32_t sectionIdx = block_no ;
245
248
246
- if (block_no == 0 ) {
249
+ if (block_no == 0 ) { // Requested boot block
247
250
memcpy (data , & BootBlock , sizeof (BootBlock ));
248
251
data [510 ] = 0x55 ;
249
252
data [511 ] = 0xaa ;
250
253
// logval("data[0]", data[0]);
251
- } else if (block_no < START_ROOTDIR ) {
254
+ } else if (block_no < START_ROOTDIR ) { // Requested FAT table sector
252
255
sectionIdx -= START_FAT0 ;
253
256
// logval("sidx", sectionIdx);
254
257
if (sectionIdx >= SECTORS_PER_FAT )
255
- sectionIdx -= SECTORS_PER_FAT ;
258
+ sectionIdx -= SECTORS_PER_FAT ; // second FAT is same as the first...
256
259
if (sectionIdx == 0 ) {
257
260
data [0 ] = 0xf0 ;
258
261
// WARNING -- code presumes only one NULL .content for .UF2 file
@@ -262,7 +265,7 @@ void read_block(uint32_t block_no, uint8_t *data) {
262
265
data [i ] = 0xff ;
263
266
}
264
267
}
265
- for (int i = 0 ; i < 256 ; ++ i ) {
268
+ for (int i = 0 ; i < 256 ; ++ i ) { // Generate the FAT chain for the firmware "file"
266
269
uint32_t v = sectionIdx * 256 + i ;
267
270
if (UF2_FIRST_SECTOR <= v && v <= UF2_LAST_SECTOR )
268
271
((uint16_t * )(void * )data )[i ] = v == UF2_LAST_SECTOR ? 0xffff : v + 1 ;
0 commit comments