@@ -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 ,
@@ -176,30 +177,30 @@ void read_block(uint32_t block_no, uint8_t *data) {
176
177
memset (data , 0 , 512 );
177
178
uint32_t sectionIdx = block_no ;
178
179
179
- if (block_no == 0 ) {
180
+ if (block_no == 0 ) { // Requested boot block
180
181
memcpy (data , & BootBlock , sizeof (BootBlock ));
181
182
data [510 ] = 0x55 ;
182
183
data [511 ] = 0xaa ;
183
184
// logval("data[0]", data[0]);
184
- } else if (block_no < START_ROOTDIR ) {
185
+ } else if (block_no < START_ROOTDIR ) { // Requested FAT table sector
185
186
sectionIdx -= START_FAT0 ;
186
187
// logval("sidx", sectionIdx);
187
188
if (sectionIdx >= SECTORS_PER_FAT )
188
- sectionIdx -= SECTORS_PER_FAT ;
189
+ sectionIdx -= SECTORS_PER_FAT ; // second FAT is same as the first...
189
190
if (sectionIdx == 0 ) {
190
- data [0 ] = 0xf0 ;
191
+ data [0 ] = 0xf8 ; // first FAT entry must match BPB MediaDescriptor
191
192
for (int i = 1 ; i < NUM_INFO * 2 + 4 ; ++ i ) {
192
193
data [i ] = 0xff ;
193
194
}
194
195
}
195
- for (int i = 0 ; i < 256 ; ++ i ) {
196
+ for (int i = 0 ; i < 256 ; ++ i ) { // Generate the FAT chain for the firmware "file"
196
197
uint32_t v = sectionIdx * 256 + i ;
197
198
if (UF2_FIRST_SECTOR <= v && v <= UF2_LAST_SECTOR )
198
199
((uint16_t * )(void * )data )[i ] = v == UF2_LAST_SECTOR ? 0xffff : v + 1 ;
199
200
}
200
- } else if (block_no < START_CLUSTERS ) {
201
+ } else if (block_no < START_CLUSTERS ) { // Requested root directory sector
201
202
sectionIdx -= START_ROOTDIR ;
202
- if (sectionIdx == 0 ) {
203
+ if (sectionIdx == 0 ) { // only one sector of directory entries generated
203
204
DirEntry * d = (void * )data ;
204
205
padded_memcpy (d -> name , (char const * ) BootBlock .VolumeLabel , 11 );
205
206
d -> attrs = 0x28 ;
@@ -209,9 +210,9 @@ void read_block(uint32_t block_no, uint8_t *data) {
209
210
d -> size = inf -> content ? strlen (inf -> content ) : UF2_SIZE ;
210
211
d -> startCluster = i + 2 ;
211
212
padded_memcpy (d -> name , inf -> name , 11 );
212
- }
213
- }
214
- } else {
213
+ }
214
+ }
215
+ } else { // else Generate the UF2 file data on-the-fly
215
216
sectionIdx -= START_CLUSTERS ;
216
217
if (sectionIdx < NUM_INFO - 1 ) {
217
218
memcpy (data , info [sectionIdx ].content , strlen (info [sectionIdx ].content ));
0 commit comments