Uzebox Bootloader V5

Topics related to the API, programming discussions & questions, coding tips, bugs, etc. should go here.
User avatar
Jubatian
Posts: 1560
Joined: Thu Oct 01, 2015 9:44 pm
Location: Hungary
Contact:

Re: New bootloader

Post by Jubatian »

By what you had sent I suspect it wasn't the empty space which ticked the bootloader off.

I analyzed the hex (sent reply by e-mail), and in that one, at the 0x40000E - 0x40000F location the (Little Endian) value 0x18A0 occurs. This pair of bytes stores the count of reserve (logical) sectors before the FAT by the specification I know about, see https://en.wikipedia.org/wiki/BIOS_parameter_block .

The value is incorrect for that role as in that image the FAT comes just after the boot sector (and not like some 6 MBytes later like the value indicates). I don't know what to do about that one, there the obvious effect is that the bootloader will try to interpret whatever data is 6 MBytes later as the FAT.

In the image you posted here the area although looks more reasonable (not that far off), but is still incorrect in regards the actual location of the FAT.

The value stored there is normally nonzero, can vary, so as far as I see the specs, is essential to decode the filesystem properly. I don't know what sort of algorithm could decode these properly, as I would require two things:
  • How to determine that the partition contains an invalid count of reserved sectors.
  • Where to fetch the actual count of reserved sectors in case the designated area was invalid.
Needs research, obviously. At the moment no idea.
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: New bootloader

Post by Artcfox »

The value 0x18a0 is correct. I just hand-parsed the hexdump from my 4GB card that freezes the bootloader and arrived at the directory entries.

Code: Select all

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 02  |................|
000001c0  03 01 0b 30 f0 c0 00 20  00 00 00 0c 76 00 00 00  |...0... ....v...|
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400000  eb 00 90 20 20 20 20 20  20 20 20 00 02 40 a0 18  |...        ..@..|
00400010  02 00 00 00 00 f8 00 00  3f 00 80 00 00 20 00 00  |........?.... ..|
00400020  00 0c 76 00 b0 03 00 00  00 00 00 00 02 00 00 00  |..v.............|
00400030  01 00 06 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00400040  80 00 29 38 35 63 61 4e  4f 20 4e 41 4d 45 20 20  |..)85caNO NAME  |
00400050  20 20 46 41 54 33 32 20  20 20 00 00 00 00 00 00  |  FAT32   ......|
00400060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400200  52 52 61 41 00 00 00 00  00 00 00 00 00 00 00 00  |RRaA............|
00400210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004003e0  00 00 00 00 72 72 41 61  9c 6a 01 00 15 6d 00 00  |....rrAa.j...m..|
004003f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400400  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004005f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400600  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400c00  eb 00 90 20 20 20 20 20  20 20 20 00 02 40 a0 18  |...        ..@..|
00400c10  02 00 00 00 00 f8 00 00  3f 00 80 00 00 20 00 00  |........?.... ..|
00400c20  00 0c 76 00 b0 03 00 00  00 00 00 00 02 00 00 00  |..v.............|
00400c30  01 00 06 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00400c40  80 00 29 38 35 63 61 4e  4f 20 4e 41 4d 45 20 20  |..)85caNO NAME  |
00400c50  20 20 46 41 54 33 32 20  20 20 00 00 00 00 00 00  |  FAT32   ......|
00400c60  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400df0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400e00  52 52 61 41 00 00 00 00  00 00 00 00 00 00 00 00  |RRaA............|
00400e10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400fe0  00 00 00 00 72 72 41 61  ff ff ff ff 02 00 00 00  |....rrAa........|
00400ff0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00401000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004011f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00401200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
First we check for a valid MBR signature, by calling:

Code: Select all

SD_ReadSubBlock(0x00000000, 0x1FE, (uint8_t*)&sig, sizeof(sig));
and we find 0xaa55, so we're good there.

Next we start reading the partition tables:

Code: Select all

SD_ReadSubBlock(0x00000000, 0x1BE + (i_part << 4), (uint8_t*)&part, sizeof(part));
with i_part set to 0 for the first partition.

That will fill out this struct:

Code: Select all

typedef struct partition_s {
  uint8_t  status = 0x00;
  uint8_t  firstCHS[3] = { 0x02, 0x03, 0x01 };
  uint8_t  type = 0x0b;
  uint8_t  lastCHS[3] = { 0x30, 0xf0, 0xc0 };
  uint32_t firstLBA = 0x00002000;
  uint32_t sizeLBA = 0x00760c00;
} __attribute__ ((packed)) partition_t;
The part.status is correct, and so is the part.type, so we read the sig at the end of the 512 byte sector of the FAT BPB by calling:

Code: Select all

SD_ReadSubBlock(CCS ? part.firstLBA : (part.firstLBA << 9), 0x1FE, (uint8_t*)&sig, sizeof(sig));
= SD_ReadSubBlock((0x2000 << 9), 0x1FE, (uint8_t*)&sig, sizeof(sig));
= SD_ReadSubBlock(0x400000, 0x1FE, (uint8_t*)&sig, sizeof(sig));
which finds 0xaa55, so we're good. (CCS is something we needed to query the SD card for, and it is true if using block addressing, false if byte addressing.)

Next we read the BPB by calling:

Code: Select all

SD_ReadSubBlock(CCS ? part.firstLBA : (part.firstLBA << 9), 0xB, (uint8_t*)&BPB, sizeof(BPB));
= SD_ReadSubBlock(0x400000, 0xB, (uint8_t*)&BPB, sizeof(BPB));
That will fill out this struct:

Code: Select all

typedef struct BPB_s {
  uint16_t BytsPerSec = 0x0200;
  uint8_t  SecPerClus = 0x40;
  uint16_t RsvdSecCnt = 0x18a0;
  uint8_t  NumFATs = 0x02;
  uint16_t RootEntCnt = 0x0000;
  uint16_t TotSec16 = 0x0000;
  uint8_t  Media = 0xf8;      // not used
  uint16_t FATSz16 = 0x0000;
  uint16_t SecPerTrk = 0x003f;  // not used
  uint16_t NumHeads = 0x0080;   // not used
  uint32_t HiddSec = 0x00002000;    // not used
  uint32_t TotSec32 = 0x00760c00;
  // Below are FAT32 specific
  uint32_t FATSz32 = 0x000003b0;
  uint16_t ExtFlags = 0x0000;
  uint16_t FSVer = 0x0000;
  uint32_t RootClus = 0x00000002;
} __attribute__ ((packed)) BPB_t;
The parameters are valid, so let's define some variables to avoid unnecessary muls/divs later:

Code: Select all

BytsPerSecExp = 9;  // since BPB.BytsPerSec = 0x200
SecPerClusExp = 6; // since BPB.SecPerClus = 0x40
It passes the extra validation check, since BytsPerSecExp + SecPerClusExp is not > 15 (Bytes per cluster should not be > 2^15).

Then we calculate the FirstDataSector:

Code: Select all

RootDirSectors = ((BPB.RootEntCnt << 5) + (BPB.BytsPerSec - 1)) >> BytsPerSecExp;
RootDirSectors = (0x200 - 1) >> 9
RootDirSectors = 0
FATSz = BPB.FATSz32 = 0x3b0
FirstDataSector = BPB.RsvdSecCnt + BPB.NumFATs * FATSz + RootDirSectors
FirstDataSector = 0x18a0 + (0x02 * 0x3b0) + 0
FirstDataSector = 0x2000
We then count the clusters to determine whether the card is FAT12/16/32

Code: Select all

CountofClusters = (BPB.TotSec32 - FirstDataSector) >> SecPerClusExp
CountofClusters = (0x760c00 - 0x2000) >> 6
CountofClusters = 0x1d7b0
Since that is greater than or equal to 65525, we know it is FAT32.

BPB.FSVer == 0x0000, so the FAT version is also correct.

Now we will start reading from the cluster the root directory starts on

Code: Select all

FAT32ClusEntryVal = BPB.RootClus = 0x02

logicalSector = ((FAT32ClusEntryVal - 2) << SecPerClusExp) + FirstDataSector
logicalSector = ((0x02 - 2) << 6) + 0x2000
logicalSector = 0x2000

physicalSector = (logicalSector << (BytsPerSecExp - 9)) + part.firstLBA
physicalSector = 0x2000 + 0x2000
physicalSector = 0x4000
Now we issue a streaming read for the current cluster of the root directory by calling:

Code: Select all

SD_ReadContiguous(CCS ? physicalSector : (physicalSector << 9), (uint16_t)1 << (BytsPerSecExp + SecPerClusExp), (uint8_t*)&info.DIR, 32, FindDirectoryEntry, (void*)&info);
= SD_ReadContiguous(0x4000 << 9, (uint16_t)1 << (9 + 6), (uint8_t*)&info.DIR, 32, FindDirectoryEntry, (void*)&info);
= SD_ReadContiguous(0x800000, 0x8000, (uint8_t*)&info.DIR, 32, FindDirectoryEntry, (void*)&info);
And if you look at the hexdump at that address, you'll find the first cluster of directory entries:

Code: Select all

00800000  31 20 20 20 20 20 20 20  57 41 56 20 00 64 f0 19  |1       WAV .d..|
00800010  92 48 ba 50 00 00 02 6e  90 44 03 00 2c fc 64 00  |.H.P...n.D..,.d.|
00800020  32 20 20 20 20 20 20 20  57 41 56 20 00 64 f0 19  |2       WAV .d..|
00800030  92 48 ba 50 00 00 a8 8c  90 44 cd 00 2c bd 46 00  |.H.P.....D..,.F.|
00800040  33 20 20 20 20 20 20 20  57 41 56 20 00 00 f1 19  |3       WAV ....|
00800050  92 48 ba 50 00 00 25 05  91 44 5b 01 ac 24 23 00  |.H.P..%..D[..$#.|
00800060  34 20 20 20 20 20 20 20  57 41 56 20 00 00 f1 19  |4       WAV ....|
00800070  92 48 ba 50 00 00 48 0d  91 44 a2 01 2c 34 18 00  |.H.P..H..D..,4..|
00800080  35 20 20 20 20 20 20 20  57 41 56 20 00 00 f1 19  |5       WAV ....|
00800090  92 48 ba 50 00 00 00 b5  91 44 d3 01 e4 7e 1d 00  |.H.P.....D...~..|
008000a0  36 20 20 20 20 20 20 20  57 41 56 20 00 00 f1 19  |6       WAV ....|
008000b0  92 48 ba 50 00 00 d7 0e  91 44 0e 02 2c 3c 1f 00  |.H.P.....D..,<..|
008000c0  37 20 20 20 20 20 20 20  57 41 56 20 00 00 f3 19  |7       WAV ....|
008000d0  92 48 ba 50 00 00 b4 10  91 44 4d 02 2c 9c 09 01  |.H.P.....DM.,...|
008000e0  38 20 20 20 20 20 20 20  57 41 56 20 00 00 f3 19  |8       WAV ....|
008000f0  92 48 ba 50 00 00 5b 11  91 44 61 04 2c 50 1f 00  |.H.P..[..Da.,P..|
00800100  39 20 20 20 20 20 20 20  57 41 56 20 00 64 f4 19  |9       WAV .d..|
00800110  92 48 ba 50 00 00 21 12  91 44 a0 04 2c 05 8b 00  |.H.P..!..D..,...|
00800120  31 30 20 20 20 20 20 20  57 41 56 20 00 00 f6 19  |10      WAV ....|
00800130  92 48 ba 50 00 00 0e 13  91 44 b7 05 2c 56 c9 00  |.H.P.....D..,V..|
00800140  31 31 20 20 20 20 20 20  57 41 56 20 00 00 f6 19  |11      WAV ....|
00800150  92 48 ba 50 00 00 a2 16  91 44 4a 07 f4 a6 10 00  |.H.P.....DJ.....|
00800160  31 32 20 20 20 20 20 20  57 41 56 20 00 64 f6 19  |12      WAV .d..|
00800170  92 48 ba 50 00 00 e5 16  91 44 6c 07 2c 66 2c 00  |.H.P.....Dl.,f,.|
00800180  31 33 20 20 20 20 20 20  57 41 56 20 00 00 f8 19  |13      WAV ....|
00800190  92 48 ba 50 00 00 5d 18  91 44 c5 07 2c b4 bf 00  |.H.P..]..D..,...|
008001a0  31 34 20 20 20 20 20 20  57 41 56 20 00 00 f9 19  |14      WAV ....|
008001b0  92 48 ba 50 00 00 a2 19  91 44 45 09 2c 47 5f 00  |.H.P.....DE.,G_.|
008001c0  31 35 20 20 20 20 20 20  57 41 56 20 00 64 f9 19  |15      WAV .d..|
008001d0  92 48 ba 50 00 00 dc 1a  91 44 04 0a ac 8a 40 00  |.H.P.....D....@.|
008001e0  31 36 20 20 20 20 20 20  57 41 56 20 00 00 fa 19  |16      WAV ....|
008001f0  92 48 ba 50 00 00 db 1b  91 44 86 0a 2c c0 33 00  |.H.P.....D..,.3.|
00800200  31 37 20 20 20 20 20 20  57 41 56 20 00 64 fc 19  |17      WAV .d..|
00800210  92 48 ba 50 00 00 04 1d  91 44 ee 0a d6 c4 09 01  |.H.P.....D......|
00800220  31 38 20 20 20 20 20 20  57 41 56 20 00 00 fd 19  |18      WAV ....|
00800230  92 48 ba 50 00 00 83 1d  91 44 02 0d ac 65 23 00  |.H.P.....D...e#.|
00800240  31 39 20 20 20 20 20 20  57 41 56 20 00 64 fd 19  |19      WAV .d..|
00800250  92 48 ba 50 00 00 2a 1e  91 44 49 0d ac de 74 00  |.H.P..*..DI...t.|
00800260  32 30 20 20 20 20 20 20  57 41 56 20 00 00 02 1a  |20      WAV ....|
00800270  92 48 ba 50 00 00 c4 b6  91 44 33 0e 2c ef 28 01  |.H.P.....D3.,.(.|
00800280  32 31 20 20 20 20 20 20  57 41 56 20 00 64 03 1a  |21      WAV .d..|
00800290  92 48 ba 50 00 00 4e b8  91 44 85 10 ec 4b 94 00  |.H.P..N..D...K..|
008002a0  32 32 20 20 20 20 20 20  57 41 56 20 00 00 04 1a  |22      WAV ....|
008002b0  92 48 ba 50 00 00 06 b9  91 44 ae 11 2c 99 34 00  |.H.P.....D..,.4.|
008002c0  32 33 20 20 20 20 20 20  57 41 56 20 00 00 04 1a  |23      WAV ....|
008002d0  92 48 ba 50 00 00 86 b9  91 44 18 12 6c fc 10 00  |.H.P.....D..l...|
008002e0  32 34 20 20 20 20 20 20  57 41 56 20 00 64 04 1a  |24      WAV .d..|
008002f0  92 48 ba 50 00 00 49 ba  91 44 3a 12 2c d8 21 00  |.H.P..I..D:.,.!.|
00800300  32 35 20 20 20 20 20 20  57 41 56 20 00 64 07 1a  |25      WAV .d..|
00800310  92 48 ba 50 00 00 eb bb  91 44 7e 12 68 6b 36 01  |.H.P.....D~.hk6.|
00800320  32 36 20 20 20 20 20 20  57 41 56 20 00 64 08 1a  |26      WAV .d..|
00800330  92 48 ba 50 00 00 40 bd  91 44 eb 14 42 ed 7c 00  |.H.P..@..D..B.|.|
00800340  32 37 20 20 20 20 20 20  57 41 56 20 00 00 0b 1a  |27      WAV ....|
00800350  92 48 ba 50 00 00 10 bf  91 44 e5 15 7e ac 10 01  |.H.P.....D..~...|
00800360  32 38 20 20 20 20 20 20  57 41 56 20 00 00 0b 1a  |28      WAV ....|
00800370  92 48 ba 50 00 00 10 00  92 44 07 18 ac f1 0f 00  |.H.P.....D......|
00800380  32 39 20 20 20 20 20 20  57 41 56 20 00 64 0b 1a  |29      WAV .d..|
00800390  92 48 ba 50 00 00 d6 00  92 44 27 18 6c a2 20 00  |.H.P.....D'.l. .|
008003a0  33 30 20 20 20 20 20 20  57 41 56 20 00 64 0b 1a  |30      WAV .d..|
008003b0  92 48 ba 50 00 00 ca 01  92 44 69 18 6c bc 2d 00  |.H.P.....Di.l.-.|
008003c0  33 31 20 20 20 20 20 20  57 41 56 20 00 00 0d 1a  |31      WAV ....|
008003d0  92 48 ba 50 00 00 3a 04  92 44 c5 18 c4 da 7f 00  |.H.P..:..D......|
008003e0  33 32 20 20 20 20 20 20  57 41 56 20 00 00 0d 1a  |32      WAV ....|
008003f0  92 48 ba 50 00 00 37 05  92 44 c5 19 2c 87 1d 00  |.H.P..7..D..,...|
00800400  33 33 20 20 20 20 20 20  57 41 56 20 00 64 0e 1a  |33      WAV .d..|
00800410  92 48 ba 50 00 00 82 06  92 44 01 1a 2c 68 b1 00  |.H.P.....D..,h..|
00800420  33 34 20 20 20 20 20 20  57 41 56 20 00 00 0f 1a  |34      WAV ....|
00800430  92 48 ba 50 00 00 f4 06  92 44 64 1b ac e6 21 00  |.H.P.....Dd...!.|
00800440  33 35 20 20 20 20 20 20  57 41 56 20 00 00 10 1a  |35      WAV ....|
00800450  92 48 ba 50 00 00 0a 08  92 44 a8 1b 2c b7 78 00  |.H.P.....D..,.x.|
00800460  33 36 20 20 20 20 20 20  57 41 56 20 00 00 11 1a  |36      WAV ....|
00800470  92 48 ba 50 00 00 79 0b  92 44 9a 1c 24 b7 8a 00  |.H.P..y..D..$...|
00800480  33 37 20 20 20 20 20 20  57 41 56 20 00 00 12 1a  |37      WAV ....|
00800490  92 48 ba 50 00 00 d9 0c  92 44 b0 1d 2c 1b 7a 00  |.H.P.....D..,.z.|
008004a0  33 38 20 20 20 20 20 20  57 41 56 20 00 64 12 1a  |38      WAV .d..|
008004b0  92 48 ba 50 00 00 4b 0d  92 44 a5 1e ac d1 57 00  |.H.P..K..D....W.|
008004c0  33 39 20 20 20 20 20 20  57 41 56 20 00 00 14 1a  |39      WAV ....|
008004d0  92 48 ba 50 00 00 2f 0e  92 44 55 1f 6a 15 94 00  |.H.P../..DU.j...|
008004e0  34 30 20 20 20 20 20 20  57 41 56 20 00 64 15 1a  |40      WAV .d..|
008004f0  92 48 ba 50 00 00 0d 0f  92 44 7e 20 9a 45 b2 00  |.H.P.....D~ .E..|
00800500  34 31 20 20 20 20 20 20  57 41 56 20 00 00 16 1a  |41      WAV ....|
00800510  92 48 ba 50 00 00 8e 10  92 44 e3 21 2c 4f 5b 00  |.H.P.....D.!,O[.|
00800520  34 32 20 20 20 20 20 20  57 41 56 20 00 00 17 1a  |42      WAV ....|
00800530  92 48 ba 50 00 00 2a 12  92 44 9a 22 2c 6f 9a 00  |.H.P..*..D.",o..|
00800540  34 33 20 20 20 20 20 20  57 41 56 20 00 00 18 1a  |43      WAV ....|
00800550  92 48 ba 50 00 00 ed 13  92 44 cf 23 6c 67 59 00  |.H.P.....D.#lgY.|
00800560  34 34 20 20 20 20 20 20  57 41 56 20 00 64 1a 1a  |44      WAV .d..|
00800570  92 48 ba 50 00 00 01 15  92 44 82 24 2c 0d 17 01  |.H.P.....D.$,...|
00800580  34 35 20 20 20 20 20 20  57 41 56 20 00 64 1b 1a  |45      WAV .d..|
00800590  92 48 ba 50 00 00 47 17  92 44 b1 26 2c ff 7e 00  |.H.P..G..D.&,.~.|
008005a0  34 36 20 20 20 20 20 20  57 41 56 20 00 00 1c 1a  |46      WAV ....|
008005b0  92 48 ba 50 00 00 bc 18  92 44 af 27 6c ae 3c 00  |.H.P.....D.'l.<.|
008005c0  34 37 20 20 20 20 20 20  57 41 56 20 00 64 1c 1a  |47      WAV .d..|
008005d0  92 48 ba 50 00 00 8a 19  92 44 29 28 2c 29 3a 00  |.H.P.....D)(,):.|
008005e0  36 36 36 36 36 36 36 36  57 41 56 20 00 00 22 1a  |66666666WAV ..".|
008005f0  92 48 ba 50 00 00 d7 15  85 44 9e 28 e4 33 3d 01  |.H.P.....D.(.3=.|
00800600  37 37 37 37 37 37 37 37  57 41 56 20 00 00 25 1a  |77777777WAV ..%.|
00800610  92 48 ba 50 00 00 2c 1e  87 44 19 2b 74 a3 bb 01  |.H.P..,..D.+t...|
00800620  38 38 38 38 38 38 38 38  57 41 56 20 00 00 28 1a  |88888888WAV ..(.|
00800630  92 48 ba 50 00 00 1a 26  87 44 91 2e 88 9a 41 01  |.H.P...&.D....A.|
00800640  39 39 39 39 39 39 39 39  57 41 56 20 00 64 2d 1a  |99999999WAV .d-.|
00800650  92 48 ba 50 00 00 f1 1e  87 44 15 31 2a 65 48 02  |.H.P.....D.1*eH.|
00800660  45 52 52 4f 52 20 20 20  57 41 56 20 00 64 2d 1a  |ERROR   WAV .d-.|
00800670  92 48 ba 50 00 00 b2 b9  8f 44 a6 35 f0 8c 05 00  |.H.P.....D.5....|
00800680  41 4b 00 45 00 59 00 50  00 41 00 0f 00 f7 44 00  |AK.E.Y.P.A....D.|
00800690  2d 00 30 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.0...w.a...v...|
008006a0  4b 45 59 50 41 44 2d 30  57 41 56 20 00 64 2d 1a  |KEYPAD-0WAV .d-.|
008006b0  92 48 ba 50 00 00 6f 34  55 33 b2 35 c8 cc 00 00  |.H.P..o4U3.5....|
008006c0  41 4b 00 45 00 59 00 50  00 41 00 0f 00 d7 44 00  |AK.E.Y.P.A....D.|
008006d0  2d 00 31 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.1...w.a...v...|
008006e0  4b 45 59 50 41 44 2d 31  57 41 56 20 00 64 2d 1a  |KEYPAD-1WAV .d-.|
008006f0  92 48 ba 50 00 00 6f 34  55 33 b4 35 14 ad 00 00  |.H.P..o4U3.5....|
00800700  41 4b 00 45 00 59 00 50  00 41 00 0f 00 37 44 00  |AK.E.Y.P.A...7D.|
00800710  2d 00 32 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.2...w.a...v...|
00800720  4b 45 59 50 41 44 2d 32  57 41 56 20 00 64 2d 1a  |KEYPAD-2WAV .d-.|
00800730  92 48 ba 50 00 00 6f 34  55 33 b6 35 a4 9c 00 00  |.H.P..o4U3.5....|
00800740  41 4b 00 45 00 59 00 50  00 41 00 0f 00 98 44 00  |AK.E.Y.P.A....D.|
00800750  2d 00 33 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.3...w.a...v...|
00800760  4b 45 59 50 41 44 2d 33  57 41 56 20 00 64 2d 1a  |KEYPAD-3WAV .d-.|
00800770  92 48 ba 50 00 00 6f 34  55 33 b8 35 6e ab 00 00  |.H.P..o4U3.5n...|
00800780  41 4b 00 45 00 59 00 50  00 41 00 0f 00 78 44 00  |AK.E.Y.P.A...xD.|
00800790  2d 00 34 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.4...w.a...v...|
008007a0  4b 45 59 50 41 44 2d 34  57 41 56 20 00 64 2d 1a  |KEYPAD-4WAV .d-.|
008007b0  92 48 ba 50 00 00 6f 34  55 33 ba 35 4a ad 00 00  |.H.P..o4U3.5J...|
008007c0  41 4b 00 45 00 59 00 50  00 41 00 0f 00 58 44 00  |AK.E.Y.P.A...XD.|
008007d0  2d 00 35 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.5...w.a...v...|
008007e0  4b 45 59 50 41 44 2d 35  57 41 56 20 00 64 2d 1a  |KEYPAD-5WAV .d-.|
008007f0  92 48 ba 50 00 00 6f 34  55 33 bc 35 d6 cf 00 00  |.H.P..o4U3.5....|
00800800  41 4b 00 45 00 59 00 50  00 41 00 0f 00 b8 44 00  |AK.E.Y.P.A....D.|
00800810  2d 00 36 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.6...w.a...v...|
00800820  4b 45 59 50 41 44 2d 36  57 41 56 20 00 64 2d 1a  |KEYPAD-6WAV .d-.|
00800830  92 48 ba 50 00 00 6f 34  55 33 be 35 20 af 00 00  |.H.P..o4U3.5 ...|
00800840  41 4b 00 45 00 59 00 50  00 41 00 0f 00 18 44 00  |AK.E.Y.P.A....D.|
00800850  2d 00 37 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.7...w.a...v...|
00800860  4b 45 59 50 41 44 2d 37  57 41 56 20 00 64 2d 1a  |KEYPAD-7WAV .d-.|
00800870  92 48 ba 50 00 00 70 34  55 33 c0 35 c4 a9 00 00  |.H.P..p4U3.5....|
00800880  41 4b 00 45 00 59 00 50  00 41 00 0f 00 f8 44 00  |AK.E.Y.P.A....D.|
00800890  2d 00 38 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.8...w.a...v...|
008008a0  4b 45 59 50 41 44 2d 38  57 41 56 20 00 64 2d 1a  |KEYPAD-8WAV .d-.|
008008b0  92 48 ba 50 00 00 70 34  55 33 c2 35 44 7c 00 00  |.H.P..p4U3.5D|..|
008008c0  41 4b 00 45 00 59 00 50  00 41 00 0f 00 d8 44 00  |AK.E.Y.P.A....D.|
008008d0  2d 00 39 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.9...w.a...v...|
008008e0  4b 45 59 50 41 44 2d 39  57 41 56 20 00 64 2d 1a  |KEYPAD-9WAV .d-.|
008008f0  92 48 ba 50 00 00 70 34  55 33 c3 35 c0 d8 00 00  |.H.P..p4U3.5....|
00800900  41 4d 00 45 00 4c 00 4f  00 44 00 0f 00 3c 59 00  |AM.E.L.O.D...<Y.|
00800910  2d 00 31 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.1...w.a...v...|
00800920  4d 45 4c 4f 44 59 2d 31  57 41 56 20 00 64 2d 1a  |MELODY-1WAV .d-.|
00800930  92 48 ba 50 00 00 6c 34  55 33 c5 35 c4 f1 00 00  |.H.P..l4U3.5....|
00800940  41 4d 00 45 00 4c 00 4f  00 44 00 0f 00 9d 59 00  |AM.E.L.O.D....Y.|
00800950  2d 00 32 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.2...w.a...v...|
00800960  4d 45 4c 4f 44 59 2d 32  57 41 56 20 00 64 2d 1a  |MELODY-2WAV .d-.|
00800970  92 48 ba 50 00 00 6c 34  55 33 c7 35 f8 f1 00 00  |.H.P..l4U3.5....|
00800980  41 4d 00 45 00 4c 00 4f  00 44 00 0f 00 7d 59 00  |AM.E.L.O.D...}Y.|
00800990  2d 00 33 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.3...w.a...v...|
008009a0  4d 45 4c 4f 44 59 2d 33  57 41 56 20 00 64 2d 1a  |MELODY-3WAV .d-.|
008009b0  92 48 ba 50 00 00 6c 34  55 33 c9 35 ba f2 00 00  |.H.P..l4U3.5....|
008009c0  41 4d 00 45 00 4c 00 4f  00 44 00 0f 00 5d 59 00  |AM.E.L.O.D...]Y.|
008009d0  2d 00 34 00 2e 00 77 00  61 00 00 00 76 00 00 00  |-.4...w.a...v...|
008009e0  4d 45 4c 4f 44 59 2d 34  57 41 56 20 00 64 2d 1a  |MELODY-4WAV .d-.|
008009f0  92 48 ba 50 00 00 6c 34  55 33 cb 35 74 f3 00 00  |.H.P..l4U3.5t...|
00800a00  32 33 31 20 20 20 20 20  57 41 56 20 00 64 63 87  |231     WAV .dc.|
00800a10  3a 49 ba 50 00 00 63 87  3a 49 cd 35 ac 15 3e 01  |:I.P..c.:I.5..>.|
00800a20  54 32 4b 20 20 20 20 20  55 5a 45 20 00 38 bc a5  |T2K     UZE .8..|
00800a30  ba 50 ba 50 00 00 bc a5  ba 50 4a 38 00 f2 00 00  |.P.P.....PJ8....|
00800a40  55 54 45 4d 50 45 53 54  4c 56 4c 20 00 07 c6 a5  |UTEMPESTLVL ....|
00800a50  ba 50 ba 50 00 00 c6 a5  ba 50 4c 38 00 90 61 1a  |.P.P.....PL8..a.|
00800a60  42 55 47 5a 20 20 20 20  55 5a 45 20 00 4c 1c a6  |BUGZ    UZE .L..|
00800a70  ba 50 ba 50 00 00 1c a6  ba 50 10 6d ee e2 00 00  |.P.P.....P.m....|
00800a80  4c 41 53 45 52 20 20 20  55 5a 45 20 00 34 3a a6  |LASER   UZE .4:.|
00800a90  ba 50 ba 50 00 00 3a a6  ba 50 12 6d e8 ef 00 00  |.P.P..:..P.m....|
00800aa0  4c 41 53 45 52 32 20 20  55 5a 45 20 00 07 40 a6  |LASER2  UZE ..@.|
00800ab0  ba 50 ba 50 00 00 40 a6  ba 50 14 6d 26 ee 00 00  |.P.P..@..P.m&...|
00800ac0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: New bootloader

Post by Artcfox »

Here is a hand-parsing of my blank 16 GB card which uses a different value for the RsvdSecCnt (0x0256), but the directory entries still work out to be at the same location in the end, because the BPB.FATSz32 is also different (0xed5):

Code: Select all

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 82  |................|
000001c0  03 00 0c fe ff ff 00 20  00 00 00 ac da 01 00 00  |....... ........|
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400000  eb 00 90 20 20 20 20 20  20 20 20 00 02 40 56 02  |...        ..@V.|
00400010  02 00 00 00 00 f8 00 00  3f 00 ff 00 00 20 00 00  |........?.... ..|
00400020  00 ac da 01 d5 0e 00 00  00 00 00 00 02 00 00 00  |................|
00400030  01 00 06 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00400040  80 00 29 62 35 33 66 4e  4f 20 4e 41 4d 45 20 20  |..)b53fNO NAME  |
00400050  20 20 46 41 54 33 32 20  20 20 00 00 00 00 00 00  |  FAT32   ......|
00400060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400200  52 52 61 41 00 00 00 00  00 00 00 00 00 00 00 00  |RRaA............|
00400210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004003e0  00 00 00 00 72 72 41 61  ff ff ff ff 02 00 00 00  |....rrAa........|
004003f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400400  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004005f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400600  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400c00  eb 00 90 20 20 20 20 20  20 20 20 00 02 40 56 02  |...        ..@V.|
00400c10  02 00 00 00 00 f8 00 00  3f 00 ff 00 00 20 00 00  |........?.... ..|
00400c20  00 ac da 01 d5 0e 00 00  00 00 00 00 02 00 00 00  |................|
00400c30  01 00 06 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00400c40  80 00 29 62 35 33 66 4e  4f 20 4e 41 4d 45 20 20  |..)b53fNO NAME  |
00400c50  20 20 46 41 54 33 32 20  20 20 00 00 00 00 00 00  |  FAT32   ......|
00400c60  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400df0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400e00  52 52 61 41 00 00 00 00  00 00 00 00 00 00 00 00  |RRaA............|
00400e10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400fe0  00 00 00 00 72 72 41 61  ff ff ff ff 02 00 00 00  |....rrAa........|
00400ff0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00401000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004011f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00401200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
0044ac00  f8 ff ff 0f ff ff ff 0f  ff ff ff 0f 00 00 00 00  |................|
0044ac10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00625600  f8 ff ff 0f ff ff ff 0f  ff ff ff 0f 00 00 00 00  |................|
00625610  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
First we check for a valid MBR signature, by calling:

Code: Select all

SD_ReadSubBlock(0x00000000, 0x1FE, (uint8_t*)&sig, sizeof(sig));
and we find 0xaa55, so we're good there.

Next we start reading the partition tables:

Code: Select all

SD_ReadSubBlock(0x00000000, 0x1BE + (i_part << 4), (uint8_t*)&part, sizeof(part));
with i_part set to 0 for the first partition.

That will fill out this struct:

Code: Select all

typedef struct partition_s {
  uint8_t  status = 0x00;
  uint8_t  firstCHS[3] = { 0x82, 0x03, 0x00 };
  uint8_t  type = 0x0c;
  uint8_t  lastCHS[3] = { 0xfe, 0xff, 0xff };
  uint32_t firstLBA = 0x00002000;
  uint32_t sizeLBA = 0x01daac00;
} __attribute__ ((packed)) partition_t;
The part.status is correct, and so is the part.type, so we read the sig at the end of the 512 byte sector of the FAT BPB by calling:

Code: Select all

SD_ReadSubBlock(CCS ? part.firstLBA : (part.firstLBA << 9), 0x1FE, (uint8_t*)&sig, sizeof(sig));
= SD_ReadSubBlock((0x2000 << 9), 0x1FE, (uint8_t*)&sig, sizeof(sig));
= SD_ReadSubBlock(0x400000, 0x1FE, (uint8_t*)&sig, sizeof(sig));
which finds 0xaa55, so we're good. (CCS is something we needed to query the SD card for, and it is true if using block addressing, false if byte addressing.)

Next we read the BPB by calling:

Code: Select all

SD_ReadSubBlock(CCS ? part.firstLBA : (part.firstLBA << 9), 0xB, (uint8_t*)&BPB, sizeof(BPB));
= SD_ReadSubBlock(0x400000, 0xB, (uint8_t*)&BPB, sizeof(BPB));
That will fill out this struct:

Code: Select all

typedef struct BPB_s {
  uint16_t BytsPerSec = 0x0200;
  uint8_t  SecPerClus = 0x40;
  uint16_t RsvdSecCnt = 0x0256;
  uint8_t  NumFATs = 0x02;
  uint16_t RootEntCnt = 0x0000;
  uint16_t TotSec16 = 0x0000;
  uint8_t  Media = 0xf8;      // not used
  uint16_t FATSz16 = 0x0000;
  uint16_t SecPerTrk = 0x003f;  // not used
  uint16_t NumHeads = 0x00ff;   // not used
  uint32_t HiddSec = 0x00002000;    // not used
  uint32_t TotSec32 = 0x01daac00;
  // Below are FAT32 specific
  uint32_t FATSz32 = 0x00000ed5;
  uint16_t ExtFlags = 0x0000;
  uint16_t FSVer = 0x0000;
  uint32_t RootClus = 0x00000002;
} __attribute__ ((packed)) BPB_t;
The parameters are valid, so let's define some variables to avoid unnecessary muls/divs later:

Code: Select all

BytsPerSecExp = 9;  // since BPB.BytsPerSec = 0x200
SecPerClusExp = 6; // since BPB.SecPerClus = 0x40
It passes the extra validation check, since BytsPerSecExp + SecPerClusExp is not > 15 (Bytes per cluster should not be > 2^15).

Then we calculate the FirstDataSector:

Code: Select all

RootDirSectors = ((BPB.RootEntCnt << 5) + (BPB.BytsPerSec - 1)) >> BytsPerSecExp;
RootDirSectors = (0x200 - 1) >> 9
RootDirSectors = 0
FATSz = BPB.FATSz32 = 0xed5
FirstDataSector = BPB.RsvdSecCnt + BPB.NumFATs * FATSz + RootDirSectors
FirstDataSector = 0x0256 + (0x02 * 0xed5) + 0
FirstDataSector = 0x2000
We then count the clusters to determine whether the card is FAT12/16/32

Code: Select all

CountofClusters = (BPB.TotSec32 - FirstDataSector) >> SecPerClusExp
CountofClusters = (0x1daac00 - 0x2000) >> 6
CountofClusters = 0x1da8c00
Since that is greater than or equal to 65525, we know it is FAT32.

BPB.FSVer == 0x0000, so the FAT version is also correct.

Now we will start reading from the cluster the root directory starts on

Code: Select all

FAT32ClusEntryVal = BPB.RootClus = 0x02

logicalSector = ((FAT32ClusEntryVal - 2) << SecPerClusExp) + FirstDataSector
logicalSector = ((0x02 - 2) << 6) + 0x2000
logicalSector = 0x2000

physicalSector = (logicalSector << (BytsPerSecExp - 9)) + part.firstLBA
physicalSector = 0x2000 + 0x2000
physicalSector = 0x4000
Now we issue a streaming read for the current cluster of the root directory by calling:

Code: Select all

SD_ReadContiguous(CCS ? physicalSector : (physicalSector << 9), (uint16_t)1 << (BytsPerSecExp + SecPerClusExp), (uint8_t*)&info.DIR, 32, FindDirectoryEntry, (void*)&info);
= SD_ReadContiguous(0x4000 << 9, (uint16_t)1 << (9 + 6), (uint8_t*)&info.DIR, 32, FindDirectoryEntry, (void*)&info);
= SD_ReadContiguous(0x800000, 0x8000, (uint8_t*)&info.DIR, 32, FindDirectoryEntry, (void*)&info);
And if you look at the hexdump at that address, you would find the first cluster of directory entries—except it's all zeroes because this card is still blank!
User avatar
Jubatian
Posts: 1560
Joined: Thu Oct 01, 2015 9:44 pm
Location: Hungary
Contact:

Re: New bootloader

Post by Jubatian »

I am totally at loss at this.

I reproduced the filesystem structure on an SDHC card (first partition at 0x400000, count of reserved sectors 0x18A0, FAT located accordingly, the SD Association Card formatter ended up producing this for my card), and for me the thing doesn't fail, the bootloader reads it just fine with Tempest and its level file copied on it.

I don't know where I should even look. Maybe it is somehow a combination of the card's behaviour and the filesystem on it? Like if the card perpetually timed out when reading certain areas making the bootloader failing to read it?

I need a reproduction case since without that I can't even see what actually happens in the bootloader, and there are no obvious things anywhere beyond the known 32MByte limit (which isn't crossed) for Boot Sector & FAT start.

Could you produce full disk dumps for repro?

To minimize size, first wipe the card with "dd if=/dev/zero of=/dev/<sdcard_device> bs=1M", then format, copy the stuff on it, grab it with "dd" and zip it. I need to see what happens if I have exactly the same stuff on the card, if I find the bootloader working, then it is the card which behaves somehow out of ordinary.
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: New bootloader

Post by Artcfox »

Out of curiosity, are you ignoring long filenames?

This is what my directory entry parsing code looks like:

Code: Select all

static uint8_t FindDirectoryEntry(void *arg) {
  findDirectoryEntryInfo_t *info = (findDirectoryEntryInfo_t*)arg;
  if (info == 0 || info->DIR.Name[0] == 0x00)
    return -1; // stop streaming when end of populated entries is reached
  if (info->DIR.Name[0] == 0xE5 || (info->DIR.Attr & (0x08 | 0x10)))
    return 0; // ignore deleted files, directories, and volume ids

  if (memcmp(info->DIR.Name, info->Name, sizeof(info->Name)) == 0) {
    info->foundEntry = 1; // set flag if this directory entry matches
    return -1; // stop streaming because we found a match
  }
  return 0; // keep streaming
}
By ignoring deleted files, directories, and volume ids, it also ignores long filenames.

I see you added a check for an underscore to solve the hidden OS X dot files, but the DIR_Attr bits should be enough to know it is hidden.

I just copied over bugz.uze and laser2.uze to a blank 16 GB card that the bootloader correctly identified as empty, and the directory entries look like this:

Code: Select all

00800000  41 62 00 75 00 67 00 7a  00 2e 00 0f 00 3d 75 00  |Ab.u.g.z.....=u.|
00800010  7a 00 65 00 00 00 ff ff  ff ff 00 00 ff ff ff ff  |z.e.............|
00800020  42 55 47 5a 20 20 20 20  55 5a 45 20 00 00 a4 b9  |BUGZ    UZE ....|
00800030  c6 50 c6 50 00 00 a4 b9  c6 50 03 00 ee e2 00 00  |.P.P.....P......|
00800040  41 6c 00 61 00 73 00 65  00 72 00 0f 00 f8 32 00  |Al.a.s.e.r....2.|
00800050  2e 00 75 00 7a 00 65 00  00 00 00 00 ff ff ff ff  |..u.z.e.........|
00800060  4c 41 53 45 52 32 20 20  55 5a 45 20 00 00 a9 b9  |LASER2  UZE ....|
00800070  c6 50 c6 50 00 00 a9 b9  c6 50 05 00 26 ee 00 00  |.P.P.....P..&...|
00800080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
You can see the long file name entries before the short file name entries, because their DIR_Attr byte is 0x0f.

And now the bootloader freezes when I insert that card. I did take full compressed disk images of the card before and after.

Long filenames have the read only, hidden, system, and volume id bits set:
Screenshot_20200606_195523.png
Screenshot_20200606_195523.png (168.09 KiB) Viewed 15535 times
Edit: I've now emailed you a full compressed image of this card.
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: New bootloader

Post by Artcfox »

The long filenames might be a red herring, because I used a hex editor to get rid of them on my card (I turned them into hard links with a different file name as their short versions), and I still have the same issue.

Is it possible for cuzebox to use an image file instead of a directory? Then I could at least run cuzebox inside a debugger while it is running the bootloader with on that card image to try to discover the problem.

Edit: I guess the check for the extension UZE would also filter out the long filenames.
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: New bootloader

Post by Artcfox »

So I told you that before I wrote anything to this 16 GB card that I took a disk image of it when it was blank.

I wrote the blank image back to the card (and verified that it is the same), and expected the bootloader to again recognize that it is an empty card by printing the message, but now it doesn't even print a message about an empty card. :(

Edit: I found a Windows computer, downloaded the official sdcard.org formatter, and did a full format, and the bootloader still doesn't recognize it as an empty card, and won't list a uze file that I copied to it.

This is what the card looked like after:

Code: Select all

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  ba 4f 84 41 00 00 00 82  |.........O.A....|
000001c0  03 00 0c fe ff ff 00 20  00 00 00 ac da 01 00 00  |....... ........|
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400000  eb 00 90 4d 53 57 49 4e  34 2e 31 00 02 40 56 02  |...MSWIN4.1..@V.|
00400010  02 00 00 00 00 f8 00 00  3f 00 ff 00 00 20 00 00  |........?.... ..|
00400020  00 ac da 01 d5 0e 00 00  00 00 00 00 02 00 00 00  |................|
00400030  01 00 06 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00400040  80 00 29 ae 13 9a 6c 4e  4f 20 4e 41 4d 45 20 20  |..)...lNO NAME  |
00400050  20 20 46 41 54 33 32 20  20 20 f4 f4 f4 f4 f4 f4  |  FAT32   ......|
00400060  f4 f4 f4 f4 f4 f4 f4 f4  f4 f4 f4 f4 f4 f4 f4 f4  |................|
*
004001f0  f4 f4 f4 f4 f4 f4 f4 f4  f4 f4 f4 f4 f4 f4 55 aa  |..............U.|
00400200  52 52 61 41 00 00 00 00  00 00 00 00 00 00 00 00  |RRaA............|
00400210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004003e0  00 00 00 00 72 72 41 61  2f 6a 07 00 03 00 00 00  |....rrAa/j......|
004003f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400400  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004005f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400600  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400c00  eb 00 90 4d 53 57 49 4e  34 2e 31 00 02 40 56 02  |...MSWIN4.1..@V.|
00400c10  02 00 00 00 00 f8 00 00  3f 00 ff 00 00 20 00 00  |........?.... ..|
00400c20  00 ac da 01 d5 0e 00 00  00 00 00 00 02 00 00 00  |................|
00400c30  01 00 06 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00400c40  80 00 29 ae 13 9a 6c 4e  4f 20 4e 41 4d 45 20 20  |..)...lNO NAME  |
00400c50  20 20 46 41 54 33 32 20  20 20 f4 f4 f4 f4 f4 f4  |  FAT32   ......|
00400c60  f4 f4 f4 f4 f4 f4 f4 f4  f4 f4 f4 f4 f4 f4 f4 f4  |................|
*
00400df0  f4 f4 f4 f4 f4 f4 f4 f4  f4 f4 f4 f4 f4 f4 55 aa  |..............U.|
00400e00  52 52 61 41 00 00 00 00  00 00 00 00 00 00 00 00  |RRaA............|
00400e10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400fe0  00 00 00 00 72 72 41 61  ff ff ff ff 03 00 00 00  |....rrAa........|
00400ff0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00401000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004011f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00401200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
0044ac00  f8 ff ff 0f ff ff ff ff  ff ff ff 0f 00 00 00 00  |................|
0044ac10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00625600  f8 ff ff 0f ff ff ff ff  ff ff ff 0f 00 00 00 00  |................|
00625610  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
3b5980000
And then once I copied BUGZ.UZE (keeping it all caps, so it doesn't generate long filenames):

Code: Select all

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  ba 4f 84 41 00 00 00 82  |.........O.A....|
000001c0  03 00 0c fe ff ff 00 20  00 00 00 ac da 01 00 00  |....... ........|
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400000  eb 00 90 4d 53 57 49 4e  34 2e 31 00 02 40 56 02  |...MSWIN4.1..@V.|
00400010  02 00 00 00 00 f8 00 00  3f 00 ff 00 00 20 00 00  |........?.... ..|
00400020  00 ac da 01 d5 0e 00 00  00 00 00 00 02 00 00 00  |................|
00400030  01 00 06 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00400040  80 01 29 ae 13 9a 6c 4e  4f 20 4e 41 4d 45 20 20  |..)...lNO NAME  |
00400050  20 20 46 41 54 33 32 20  20 20 f4 f4 f4 f4 f4 f4  |  FAT32   ......|
00400060  f4 f4 f4 f4 f4 f4 f4 f4  f4 f4 f4 f4 f4 f4 f4 f4  |................|
*
004001f0  f4 f4 f4 f4 f4 f4 f4 f4  f4 f4 f4 f4 f4 f4 55 aa  |..............U.|
00400200  52 52 61 41 00 00 00 00  00 00 00 00 00 00 00 00  |RRaA............|
00400210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004003e0  00 00 00 00 72 72 41 61  2d 6a 07 00 05 00 00 00  |....rrAa-j......|
004003f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400400  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004005f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00400600  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400c00  eb 00 90 4d 53 57 49 4e  34 2e 31 00 02 40 56 02  |...MSWIN4.1..@V.|
00400c10  02 00 00 00 00 f8 00 00  3f 00 ff 00 00 20 00 00  |........?.... ..|
00400c20  00 ac da 01 d5 0e 00 00  00 00 00 00 02 00 00 00  |................|
00400c30  01 00 06 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00400c40  80 00 29 ae 13 9a 6c 4e  4f 20 4e 41 4d 45 20 20  |..)...lNO NAME  |
00400c50  20 20 46 41 54 33 32 20  20 20 f4 f4 f4 f4 f4 f4  |  FAT32   ......|
00400c60  f4 f4 f4 f4 f4 f4 f4 f4  f4 f4 f4 f4 f4 f4 f4 f4  |................|
*
00400df0  f4 f4 f4 f4 f4 f4 f4 f4  f4 f4 f4 f4 f4 f4 55 aa  |..............U.|
00400e00  52 52 61 41 00 00 00 00  00 00 00 00 00 00 00 00  |RRaA............|
00400e10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00400fe0  00 00 00 00 72 72 41 61  ff ff ff ff 03 00 00 00  |....rrAa........|
00400ff0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00401000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
004011f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00401200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
0044ac00  f8 ff ff 0f ff ff ff ff  ff ff ff 0f 00 00 00 00  |................|
0044ac10  05 00 00 00 ff ff ff 0f  00 00 00 00 00 00 00 00  |................|
0044ac20  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00625600  f8 ff ff 0f ff ff ff ff  ff ff ff 0f 00 00 00 00  |................|
00625610  05 00 00 00 ff ff ff 0f  00 00 00 00 00 00 00 00  |................|
00625620  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00800000  42 55 47 5a 20 20 20 20  55 5a 45 20 00 64 13 07  |BUGZ    UZE .d..|
00800010  c7 50 c7 50 00 00 13 07  c7 50 04 00 ee e2 00 00  |.P.P.....P......|
00800020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00810000  55 5a 45 42 4f 58 01 00  ee e0 00 00 df 07 42 75  |UZEBOX........Bu|
00810010  67 7a 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |gz..............|
00810020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 4d 61  |..............Ma|
00810030  74 74 20 50 61 6e 64 69  6e 61 00 00 00 00 00 00  |tt Pandina......|
00810040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
<the rest of the data>
User avatar
Jubatian
Posts: 1560
Joined: Thu Oct 01, 2015 9:44 pm
Location: Hungary
Contact:

Re: New bootloader

Post by Jubatian »

This was quite a mess.

Thank you, with the repro case I could start actually hunting this down and locate it.

It wasn't anywhere near the SD / FS initialization or however parts of the FS are laid out. The root directory listing got stuck due to that I apparently misread how special cluster values are formatted in FAT32. It couldn't ever detect the end of the root directory in some cases.

I committed the fix to Uzebox master (Bootloader version is now V.5.0.12), it took one extra instruction, so there is only 2 bytes (1 AVR instruction) left in the bootloader now. The kernel's bootloader SD library (bootlib.h / bootlib.s) is not affected as it can only use FAT32 / SDHC through the Bootloader.
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: New bootloader

Post by Artcfox »

I just tried it on my Uzebox and Uzebox Portable with all of the cards that previously did (and did not) work, and now everything is working great! I'm so glad that you could repro and fix it!

Tornado 2000 now works on real hardware with all of the SDHC cards that I have. :D

Edit: In the uzebox/demos/Bootloader5/README.txt file, it is missing the instructions for flashing the bootloader. It just says:

Code: Select all

For flashing a bootloader onto the AVR, see:
It would be helpful if it included the instructions for avrdude, along with the recommended fuses:

Code: Select all

avrdude -c avrispmkII -P usb -p m644 -U flash:w:Bootloader5.hex
avrdude -c avrispmkII -P usb -p m644 -U hfuse:w:0xd2:m -U lfuse:w:0xf7:m -U efuse:w:0xfc:m
(Though maybe on the portable it shouldn't set the brownout detection that high?)
User avatar
nicksen782
Posts: 714
Joined: Wed Feb 01, 2012 8:23 pm
Location: Detroit, United States
Contact:

Re: New bootloader

Post by nicksen782 »

I have not been able to play Tornado on real hardware. I have played it on the emulators though. I would really like to play it on a real tv.

So, there is a new bootloader that will support Tornado and a new update to Tornado too?

Uze (or whomever knows for certain) what should the fuses be set to?
Post Reply