andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
CCS SD/MMC driver MBR support |
Posted: Fri Aug 27, 2010 7:15 am |
|
|
(See also CCS FAT driver bugfix!)
(Copied from original post in General forum)
I know there's quite a few people using this driver because it's bundled with the compiler. It may not be the best, but the price is hard to beat!
One of its shortcomings is that it doesn't handle cards with a Master Boot Record. Naturally, every single SD card I had on hand had an MBR! Support for the MBR is relatively trivial to implement, so I thought I'd post how. This involves the "mmcsd.c" CCS driver file with "Copyright 2007" in the header comments. Make a backup copy of it before starting to edit it, just in case.
Search for: Code: | uint32_t g_mmcsdBufferAddress; | and add under it: Code: | uint32_t g_mmcsdPartitionOffset; |
Next, just before the "mmcsd_init" function add a new function: Code: | void mmcsd_check_part(uint16_t off)
{
if (g_mmcsd_buffer[off + 0] == 0x80)
{
// active partition
uint8_t t;
t = g_mmcsd_buffer[off + 4];
if (t == 0x04 || t == 0x06 || t == 0x0B)
{
// FAT16 or FAT32 partition
g_mmcsdPartitionOffset = make32(
g_mmcsd_buffer[off + 11], g_mmcsd_buffer[off + 10],
g_mmcsd_buffer[off + 9], g_mmcsd_buffer[off + 8]) * MMCSD_MAX_BLOCK_SIZE;
}
}
} |
In the "mmcsd_init" function, right after the line: Code: | r1 = mmcsd_load_buffer(); | Add the lines: Code: | g_mmcsdPartitionOffset = 0;
mmcsd_check_part(0x1EE);
mmcsd_check_part(0x1DE);
mmcsd_check_part(0x1CE);
mmcsd_check_part(0x1BE); |
Finally, in the function "mmcsd_move_buffer", after the line: Code: | new_block = new_addr - (new_addr % MMCSD_MAX_BLOCK_SIZE); | Add the line: Code: | new_block += g_mmcsdPartitionOffset; |
This doesn't do anything about FAT16 vs FAT32 or SD vs SDHC. If only dynamic runtime support for all those was as easy! _________________ Andrew |
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Mon Oct 15, 2018 11:29 am |
|
|
Thanks for this.
I needed to make a couple of modifications for this to work. Please let me know if those changes break something. Otherwise, it seems to work for me.
The changes are made to the function:
Code: | void mmcsd_check_part(uint16_t off) |
Line 3 checks a byte in the MBR for whether or not the partition is bootable. We should apply an offset whether or not it is bootable.
I changed line 3 from:
Code: | if (g_mmcsd_buffer[off + 0] == 0x80) |
to:
Code: | if (g_mmcsd_buffer[off + 0] == 0x80 || g_mmcsd_buffer[off + 0] == 0) |
Line 8 checks the file system descriptor. 0x0C is also a FAT32 descriptor.
I changed line 8 from:
Code: | if (t == 0x04 || t == 0x06 || t == 0x0B) |
to:
Code: | if (t == 0x04 || t == 0x06 || t == 0x0B || t == 0x0C) |
|
|