@@ -110,6 +110,7 @@ static FAT_BootBlock const BootBlock = {
110
110
.SectorsPerFAT = SECTORS_PER_FAT ,
111
111
.SectorsPerTrack = 1 ,
112
112
.Heads = 1 ,
113
+ .PhysicalDriveNum = 0x80 , // to match MediaDescriptor of 0xF8
113
114
.ExtendedBootSig = 0x29 ,
114
115
.VolumeSerialNumber = 0x00420042 ,
115
116
.VolumeLabel = VOLUME_LABEL ,
@@ -123,35 +124,37 @@ static FAT_BootBlock const BootBlock = {
123
124
static uint32_t current_flash_size (void )
124
125
{
125
126
static uint32_t flash_sz = 0 ;
127
+ uint32_t result = flash_sz ; // presumes atomic 32-bit read/write and static result
126
128
127
129
// only need to compute once
128
- if ( flash_sz == 0 )
130
+ if ( result == 0 )
129
131
{
130
132
// return 1 block of 256 bytes
131
133
if ( !bootloader_app_is_valid (DFU_BANK_0_REGION_START ) )
132
134
{
133
- flash_sz = 256 ;
135
+ result = 256 ;
134
136
}else
135
137
{
136
138
bootloader_settings_t const * boot_setting ;
137
139
bootloader_util_settings_get (& boot_setting );
138
140
139
- flash_sz = boot_setting -> bank_0_size ;
141
+ result = boot_setting -> bank_0_size ;
140
142
141
143
// Copy size must be multiple of 256 bytes
142
144
// else we will got an issue copying current.uf2
143
- if (flash_sz & 0xff )
145
+ if (result & 0xff )
144
146
{
145
- flash_sz = (flash_sz & ~0xff ) + 256 ;
147
+ result = (result & ~0xff ) + 256 ;
146
148
}
147
149
148
150
// if bank0 size is not valid, happens when flashed with jlink
149
151
// use maximum application size
150
- if ( (flash_sz == 0 ) || (flash_sz == 0xFFFFFFFFUL ) )
152
+ if ( (result == 0 ) || (result == 0xFFFFFFFFUL ) )
151
153
{
152
- flash_sz = FLASH_SIZE ;
154
+ result = FLASH_SIZE ;
153
155
}
154
156
}
157
+ flash_sz = result ; // presumes atomic 32-bit read/write and static result
155
158
}
156
159
157
160
return flash_sz ;
@@ -176,30 +179,30 @@ void read_block(uint32_t block_no, uint8_t *data) {
176
179
memset (data , 0 , 512 );
177
180
uint32_t sectionIdx = block_no ;
178
181
179
- if (block_no == 0 ) {
182
+ if (block_no == 0 ) { // Requested boot block
180
183
memcpy (data , & BootBlock , sizeof (BootBlock ));
181
184
data [510 ] = 0x55 ;
182
185
data [511 ] = 0xaa ;
183
186
// logval("data[0]", data[0]);
184
- } else if (block_no < START_ROOTDIR ) {
187
+ } else if (block_no < START_ROOTDIR ) { // Requested FAT table sector
185
188
sectionIdx -= START_FAT0 ;
186
189
// logval("sidx", sectionIdx);
187
190
if (sectionIdx >= SECTORS_PER_FAT )
188
- sectionIdx -= SECTORS_PER_FAT ;
191
+ sectionIdx -= SECTORS_PER_FAT ; // second FAT is same as the first...
189
192
if (sectionIdx == 0 ) {
190
- data [0 ] = 0xf0 ;
193
+ data [0 ] = 0xf8 ; // first FAT entry must match BPB MediaDescriptor
191
194
for (int i = 1 ; i < NUM_INFO * 2 + 4 ; ++ i ) {
192
195
data [i ] = 0xff ;
193
196
}
194
197
}
195
- for (int i = 0 ; i < 256 ; ++ i ) {
198
+ for (int i = 0 ; i < 256 ; ++ i ) { // Generate the FAT chain for the firmware "file"
196
199
uint32_t v = sectionIdx * 256 + i ;
197
200
if (UF2_FIRST_SECTOR <= v && v <= UF2_LAST_SECTOR )
198
201
((uint16_t * )(void * )data )[i ] = v == UF2_LAST_SECTOR ? 0xffff : v + 1 ;
199
202
}
200
- } else if (block_no < START_CLUSTERS ) {
203
+ } else if (block_no < START_CLUSTERS ) { // Requested root directory sector
201
204
sectionIdx -= START_ROOTDIR ;
202
- if (sectionIdx == 0 ) {
205
+ if (sectionIdx == 0 ) { // only one sector of directory entries generated
203
206
DirEntry * d = (void * )data ;
204
207
padded_memcpy (d -> name , (char const * ) BootBlock .VolumeLabel , 11 );
205
208
d -> attrs = 0x28 ;
@@ -209,9 +212,9 @@ void read_block(uint32_t block_no, uint8_t *data) {
209
212
d -> size = inf -> content ? strlen (inf -> content ) : UF2_SIZE ;
210
213
d -> startCluster = i + 2 ;
211
214
padded_memcpy (d -> name , inf -> name , 11 );
212
- }
213
- }
214
- } else {
215
+ }
216
+ }
217
+ } else { // else Generate the UF2 file data on-the-fly
215
218
sectionIdx -= START_CLUSTERS ;
216
219
if (sectionIdx < NUM_INFO - 1 ) {
217
220
memcpy (data , info [sectionIdx ].content , strlen (info [sectionIdx ].content ));
0 commit comments