|
11 | 11 |
|
12 | 12 |
|
13 | 13 | // Converts LBA to MBR CHS format. |
14 | | -// TODO: SDFormatter uses end head 254 for a 16 GB card. Bug? |
15 | 14 | static u32 lba2chs(u64 lba, const u32 heads, const u32 secPerTrk) |
16 | 15 | { |
17 | | - // We can't encode this even with 255 heads and 63 sectors per track. |
18 | | - // Returning the maximum in this case is ok. |
19 | | - if(lba >= 16450560) |
20 | | - return 0x00FFFFFF; // Cylinder 1023, head 255, sector 63. |
21 | | - |
22 | 16 | const u32 spc = heads * secPerTrk; |
23 | | - const u32 c = lba / spc; |
| 17 | + u32 c = lba / spc; |
24 | 18 | lba %= spc; |
25 | | - const u32 h = lba / secPerTrk; |
26 | | - const u32 s = lba % secPerTrk + 1; |
| 19 | + u32 h = lba / secPerTrk; |
| 20 | + u32 s = lba % secPerTrk + 1; |
27 | 21 |
|
28 | | - // Return the maximum if we can't encode this |
29 | | - // and also set the uppermost byte to maximum to indicate an error. |
30 | | - if(c > 1023 || h > 255 || s > 63) |
31 | | - return 0xFFFFFFFF; |
| 22 | + // Return the maximum if we can't encode this. |
| 23 | + if(c >= 1024 || h >= heads || s > secPerTrk) |
| 24 | + { |
| 25 | + c = 1023; |
| 26 | + h = 254; |
| 27 | + s = 63; |
| 28 | + } |
32 | 29 |
|
33 | | - //printf("lba2chs(%" PRIu64 ", %" PRIu32 ", %" PRIu32 "): c: %" PRIu32 ", h: %" PRIu32 ", s: %" PRIu32 "\n", |
34 | | - // lba, heads, secPerTrk, c, h, s); |
35 | | - return (c & 0xFF)<<16 | (c & 0x300)<<6 | s<<8 | h; |
| 30 | + return (c & 0xFFu)<<16 | (c & 0x300u)<<6 | s<<8 | h; |
36 | 31 | } |
37 | 32 |
|
38 | 33 | // TODO: Rewrite this function to use partSize to determine the fs type. |
@@ -62,18 +57,18 @@ int createMbrAndPartition(const FormatParams ¶ms, BufferedFsWriter &dev) |
62 | 57 | const u64 totSec = params.totSec * (bytesPerSec>>9); // Convert back to physical sectors. |
63 | 58 | const u64 partSize = totSec - partStart; // TODO: Is this correct or should we align the end? |
64 | 59 | u8 type; |
65 | | - if(fatBits == 12) type = 0x01; // FAT12 (16/32 MiB). |
| 60 | + if(fatBits == 12) type = 0x01; // FAT12 (16/32 MiB). |
66 | 61 | else if(fatBits == 16) |
67 | 62 | { |
68 | | - if(partSize < 65536) type = 0x04; // FAT16 (<32 MiB). TODO: Is this actually "<" or "<="? |
69 | | - else type = 0x06; // FAT16 (>=32 MiB). |
| 63 | + if(partSize < 65536) type = 0x04; // FAT16 (<32 MiB). |
| 64 | + else type = 0x06; // FAT16 (>=32 MiB). |
70 | 65 | } |
71 | 66 | else if(fatBits == 32) |
72 | 67 | { |
73 | | - if((totSec - 1) < 16450560) type = 0x0B; // FAT32 CHS. |
74 | | - else type = 0x0C; // FAT32 LBA. |
| 68 | + if(totSec <= 16450560) type = 0x0B; // FAT32 CHS. |
| 69 | + else type = 0x0C; // FAT32 LBA. |
75 | 70 | } |
76 | | - else type = 0x07; // exFAT. |
| 71 | + else type = 0x07; // exFAT. |
77 | 72 | entry.type = type; |
78 | 73 | verbosePrintf("Partition type: 0x%02" PRIX8 "\n", type); |
79 | 74 |
|
|
0 commit comments