#12 mkfs_makevfat misaligned word/halfword writes

open
nobody
None
5
2006-11-11
2006-11-11
Vito
No

My target is LPC2148 (ARM7 from Philips)

I had the problem of having an unreadable card after
invoking mkfs_makevfat in mkfs.c.

The issue is that the following lines make misaligned
halfword/word writes on buf, thus overwriting adjacent
bytes (such misaligned writes are forbidden on ARM7
architecture):

/* Bytes/Sector */
*((unsigned short*)(buf+11)) = 512; //this puts a 0 in
buf+11 too

/* RootEntryCount */
*((unsigned short*)(buf+17)) = 0; //this puts a 0 in
buf+16 too

/* Total Sector Count __16 */
*((unsigned short*)(buf+19)) = 0;

/* VolumeID */
*((unsigned long*)(buf+67)) = 0x13371337;

Replacing them with:

/* Bytes/Sector */
*(buf+11)=0;
*(buf+12)=2;

/* RootEntryCount */
*(buf+17)=0;
*(buf+18)=0;

/* Total Sector Count __16 */
*(buf+19)=0;
*(buf+20)=0;

/* VolumeID */
*(buf+67)=0x37;
*(buf+68)=0x13;
*(buf+69)=0x37;
*(buf+70)=0x13;

After the modifications above, I get a valid MMC card
after invoking mkfs_makevfat.

Regards
Vito

Discussion

  • soliko
    soliko
    2006-11-29

    Logged In: YES
    user_id=1655283
    Originator: NO

    I got the same problem with my port on a MSP430 when I invoke mkfs_makevfat. The SD card was not readable on a PC! I apply this solution and it fix the problem. Thanks.

     
  • jpd
    jpd
    2006-12-11

    Logged In: YES
    user_id=1665715
    Originator: NO

    I made the following modifications : (I used ex_setb16 and ex_setb32)

    /* Bytes/Sector */
    ex_setb16(buf,11,512);

    /* Sectors/Cluster */
    *(buf+13) = c;

    /* Reserved Sectors */
    ex_setb16(buf,14,32);

    /* Number of FAT Tables */
    *(buf+16) = 2;

    /* RootEntryCount */
    ex_setb16(buf,17,0);

    /* Total Sector Count __16 */
    ex_setb16(buf,19,0);

    /* Media (crap) */
    *(buf+21) = 0xF8;

    /* FAT size 16 */
    ex_setb16(buf,22,0);

    /* Total Sector Count __32 */
    ex_setb32(buf,32,ns);

    /* Fat Size 32 */
    ex_setb32(buf,36,fs);

    /* First Cluster Root Dir */
    ex_setb32(buf,44,2);

    /* VolumeID */
    ex_setb32(buf,67,0);

    /* Volume Label */
    memCpy("DISCOSMASH!",buf+71,11);

    /* Filesystemtype */
    memCpy("FAT32 ",buf+82,8);

    /* Magic */
    *(buf+510) = 0x55; *(buf+511) = 0xAA;

    part_writeBuf(part,0,buf);

    memClr(buf,512);
    for(c=32;c<(32+2*fs);c++){
    part_writeBuf(part,c,buf);
    }
    ex_setb32(buf,0,(euint32)0x0FFFFFF8);
    ex_setb32(buf,4,(euint32)0x0FFFFFFF);
    ex_setb32(buf,8,(euint32)0x0FFFFFF8);

     
  • Logged In: YES
    user_id=776134
    Originator: NO

    What a funny thing: Today i "discovered" the same problems. What makes me doubtful that your patch is "enough" is that there are some other places within the source code that contain cast-alignment-problems (try gcc -Wcast-align) to get the warnings. (look at dir.c and ui.c)

    In the development version the problem seems to be fixed by abstract procedures for these spots.

    Any other idea how to fix this? Is the 0.2.8-version more stable than the 0.3.5 which seems to have the fix built in?
    best regards

    cgommel

     
  • akhil
    akhil
    2011-01-18

    I used jipette's changes on ARM9 (AT91SAM9G20 from Atmel) with an SD card. The card was formatted and showed up empty on Windows XP. However, chkdsk reported errors and converted some lost chains into files. Has anyone fixed this / knows how to?