From e2afc36daeaeefd0de214d114db071b63e4820bb Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sun, 10 Mar 2019 20:02:56 -0700 Subject: [PATCH 1/4] Fix media type mismatch Fix the boot sector media descriptor to match the first FAT entries. --- src/usb/uf2/ghostfat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/usb/uf2/ghostfat.c b/src/usb/uf2/ghostfat.c index 1f41c10..5ac475d 100644 --- a/src/usb/uf2/ghostfat.c +++ b/src/usb/uf2/ghostfat.c @@ -106,7 +106,7 @@ static FAT_BootBlock const BootBlock = { .FATCopies = 2, .RootDirectoryEntries = (ROOT_DIR_SECTORS * 512 / 32), .TotalSectors16 = NUM_FAT_BLOCKS - 2, - .MediaDescriptor = 0xF8, + .MediaDescriptor = 0xF0, .SectorsPerFAT = SECTORS_PER_FAT, .SectorsPerTrack = 1, .Heads = 1, From 12bb62eaddabf57f50e0f244c81bd18d7adbd30c Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Tue, 12 Mar 2019 12:09:49 -0700 Subject: [PATCH 2/4] Per request, keep hard drive type in boot block. Update the .PhysicalDrive in boot block. Update the first fat entries to match MediaDescriptor of boot block. --- src/usb/uf2/ghostfat.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/usb/uf2/ghostfat.c b/src/usb/uf2/ghostfat.c index 5ac475d..3d4e71f 100644 --- a/src/usb/uf2/ghostfat.c +++ b/src/usb/uf2/ghostfat.c @@ -106,10 +106,11 @@ static FAT_BootBlock const BootBlock = { .FATCopies = 2, .RootDirectoryEntries = (ROOT_DIR_SECTORS * 512 / 32), .TotalSectors16 = NUM_FAT_BLOCKS - 2, - .MediaDescriptor = 0xF0, + .MediaDescriptor = 0xF8, .SectorsPerFAT = SECTORS_PER_FAT, .SectorsPerTrack = 1, .Heads = 1, + .PhysicalDriveNum = 0x80, // to match MediaDescriptor of 0xF8 .ExtendedBootSig = 0x29, .VolumeSerialNumber = 0x00420042, .VolumeLabel = VOLUME_LABEL, @@ -187,7 +188,7 @@ void read_block(uint32_t block_no, uint8_t *data) { if (sectionIdx >= SECTORS_PER_FAT) sectionIdx -= SECTORS_PER_FAT; if (sectionIdx == 0) { - data[0] = 0xf0; + data[0] = 0xf8; for (int i = 1; i < NUM_INFO * 2 + 4; ++i) { data[i] = 0xff; } @@ -210,7 +211,7 @@ void read_block(uint32_t block_no, uint8_t *data) { d->startCluster = i + 2; padded_memcpy(d->name, inf->name, 11); } - } + } } else { sectionIdx -= START_CLUSTERS; if (sectionIdx < NUM_INFO - 1) { From e0251415e76cd6778ad4507aa742f23b47b582d8 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Tue, 12 Mar 2019 12:29:05 -0700 Subject: [PATCH 3/4] Add some additional comments. --- src/usb/uf2/ghostfat.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/usb/uf2/ghostfat.c b/src/usb/uf2/ghostfat.c index 3d4e71f..1f0e60f 100644 --- a/src/usb/uf2/ghostfat.c +++ b/src/usb/uf2/ghostfat.c @@ -177,30 +177,30 @@ void read_block(uint32_t block_no, uint8_t *data) { memset(data, 0, 512); uint32_t sectionIdx = block_no; - if (block_no == 0) { + if (block_no == 0) { // Requested boot block memcpy(data, &BootBlock, sizeof(BootBlock)); data[510] = 0x55; data[511] = 0xaa; // logval("data[0]", data[0]); - } else if (block_no < START_ROOTDIR) { + } else if (block_no < START_ROOTDIR) { // Requested FAT table sector sectionIdx -= START_FAT0; // logval("sidx", sectionIdx); if (sectionIdx >= SECTORS_PER_FAT) - sectionIdx -= SECTORS_PER_FAT; + sectionIdx -= SECTORS_PER_FAT; // second FAT is same as the first... if (sectionIdx == 0) { - data[0] = 0xf8; + data[0] = 0xf8; // first FAT entry must match BPB MediaDescriptor for (int i = 1; i < NUM_INFO * 2 + 4; ++i) { data[i] = 0xff; } } - for (int i = 0; i < 256; ++i) { + for (int i = 0; i < 256; ++i) { // Generate the FAT chain for the firmware "file" uint32_t v = sectionIdx * 256 + i; if (UF2_FIRST_SECTOR <= v && v <= UF2_LAST_SECTOR) ((uint16_t *)(void *)data)[i] = v == UF2_LAST_SECTOR ? 0xffff : v + 1; } - } else if (block_no < START_CLUSTERS) { + } else if (block_no < START_CLUSTERS) { // Requested root directory sector sectionIdx -= START_ROOTDIR; - if (sectionIdx == 0) { + if (sectionIdx == 0) { // only one sector of directory entries generated DirEntry *d = (void *)data; padded_memcpy(d->name, (char const *) BootBlock.VolumeLabel, 11); d->attrs = 0x28; @@ -212,7 +212,7 @@ void read_block(uint32_t block_no, uint8_t *data) { padded_memcpy(d->name, inf->name, 11); } } - } else { + } else { // else Generate the UF2 file data on-the-fly sectionIdx -= START_CLUSTERS; if (sectionIdx < NUM_INFO - 1) { memcpy(data, info[sectionIdx].content, strlen(info[sectionIdx].content)); From 0f216b8618d78c43abf6037b75bbfefe28fea563 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Tue, 12 Mar 2019 13:13:58 -0700 Subject: [PATCH 4/4] Fix spacing --- src/usb/uf2/ghostfat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/usb/uf2/ghostfat.c b/src/usb/uf2/ghostfat.c index 1f0e60f..c5ae379 100644 --- a/src/usb/uf2/ghostfat.c +++ b/src/usb/uf2/ghostfat.c @@ -210,7 +210,7 @@ void read_block(uint32_t block_no, uint8_t *data) { d->size = inf->content ? strlen(inf->content) : UF2_SIZE; d->startCluster = i + 2; padded_memcpy(d->name, inf->name, 11); - } + } } } else { // else Generate the UF2 file data on-the-fly sectionIdx -= START_CLUSTERS;