Skip to content

Commit ae28f5b

Browse files
authored
Merge pull request #52 from henrygab/patch-1
Fix media type mismatch
2 parents 4ea74d4 + 0f216b8 commit ae28f5b

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/usb/uf2/ghostfat.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ static FAT_BootBlock const BootBlock = {
110110
.SectorsPerFAT = SECTORS_PER_FAT,
111111
.SectorsPerTrack = 1,
112112
.Heads = 1,
113+
.PhysicalDriveNum = 0x80, // to match MediaDescriptor of 0xF8
113114
.ExtendedBootSig = 0x29,
114115
.VolumeSerialNumber = 0x00420042,
115116
.VolumeLabel = VOLUME_LABEL,
@@ -176,30 +177,30 @@ void read_block(uint32_t block_no, uint8_t *data) {
176177
memset(data, 0, 512);
177178
uint32_t sectionIdx = block_no;
178179

179-
if (block_no == 0) {
180+
if (block_no == 0) { // Requested boot block
180181
memcpy(data, &BootBlock, sizeof(BootBlock));
181182
data[510] = 0x55;
182183
data[511] = 0xaa;
183184
// logval("data[0]", data[0]);
184-
} else if (block_no < START_ROOTDIR) {
185+
} else if (block_no < START_ROOTDIR) { // Requested FAT table sector
185186
sectionIdx -= START_FAT0;
186187
// logval("sidx", sectionIdx);
187188
if (sectionIdx >= SECTORS_PER_FAT)
188-
sectionIdx -= SECTORS_PER_FAT;
189+
sectionIdx -= SECTORS_PER_FAT; // second FAT is same as the first...
189190
if (sectionIdx == 0) {
190-
data[0] = 0xf0;
191+
data[0] = 0xf8; // first FAT entry must match BPB MediaDescriptor
191192
for (int i = 1; i < NUM_INFO * 2 + 4; ++i) {
192193
data[i] = 0xff;
193194
}
194195
}
195-
for (int i = 0; i < 256; ++i) {
196+
for (int i = 0; i < 256; ++i) { // Generate the FAT chain for the firmware "file"
196197
uint32_t v = sectionIdx * 256 + i;
197198
if (UF2_FIRST_SECTOR <= v && v <= UF2_LAST_SECTOR)
198199
((uint16_t *)(void *)data)[i] = v == UF2_LAST_SECTOR ? 0xffff : v + 1;
199200
}
200-
} else if (block_no < START_CLUSTERS) {
201+
} else if (block_no < START_CLUSTERS) { // Requested root directory sector
201202
sectionIdx -= START_ROOTDIR;
202-
if (sectionIdx == 0) {
203+
if (sectionIdx == 0) { // only one sector of directory entries generated
203204
DirEntry *d = (void *)data;
204205
padded_memcpy(d->name, (char const *) BootBlock.VolumeLabel, 11);
205206
d->attrs = 0x28;
@@ -209,9 +210,9 @@ void read_block(uint32_t block_no, uint8_t *data) {
209210
d->size = inf->content ? strlen(inf->content) : UF2_SIZE;
210211
d->startCluster = i + 2;
211212
padded_memcpy(d->name, inf->name, 11);
212-
}
213-
}
214-
} else {
213+
}
214+
}
215+
} else { // else Generate the UF2 file data on-the-fly
215216
sectionIdx -= START_CLUSTERS;
216217
if (sectionIdx < NUM_INFO - 1) {
217218
memcpy(data, info[sectionIdx].content, strlen(info[sectionIdx].content));

0 commit comments

Comments
 (0)