This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

formatting an uSDcard from ext4 to fat32

Hi, I have a problem that when formatting an uSDcard from ext4 to fat32 it gets with both file systems.

  • In my code I use the FAT FS library [http://elm-chan.org/fsw/ff/00index_e.html] and the nordic 12.2.0 SDK [http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v12.2.0%2Findex.html].

The purpose of my code is to use SD card with any content, I always format the SDCard when starting the code..

I did several tests with the support of the gparted tool:

Test 1:

  1. Ensure with gparted that the sd card is empty (If there is any partition select it and delete it)
  2. Run my code
  3. Insert the sd card into the pc verify that the filesystem is a fat32 and that all the desired files were created.

Test 3: 1.With gparted remove the fat32 partition (select the partition and delete it) and create a new with fat16 filesystem with 4gb (New->Partition) 2. Run my code 3. Insert the sd card into the pc verify that the filesystem is a fat32 and that all the desired files were created.

Test 4: 1.With gparted remove the fat32 partition (select the partition and delete it) and create a new with ext4 filesystem with max size (New->Partition) 2. Run my code 3. Insert the sd card into the pc verify that the filesystem is a fat32 and that all the desired files were created.

Test 5: 1.With gparted remove the fat32 partition (select the partition and delete it) and create a new with ntfs filesystem with max size (New->Partition) 2. Run my code 3. Insert the sd card into the pc verify that the filesystem is a fat32 and that all the desired files were created.

Test 6: 1.With gparted format the current partition to one of type fat 16 (select the partition and choose format to... -> fat16) 2. Run my code 3. Insert the sd card into the pc verify that the filesystem is a fat32 and that all the desired files were created.

Test 7: 1.With gparted format the current partition to one of type ntfs (select the partition and choose format to... -> ntfs) 2. Run my code 3. Insert the sd card into the pc verify that the filesystem is a fat32 and that all the desired files were created.

Test 8: 1.With gparted remove the fat32 partition (select the partition and delete it) and create a new with fat32 filesystem with max size (New->Partition). After created format the partition to one of type ext4 (select the partition and choose format to... -> fat32) 2. Run my code 3. Insert the sd card into the pc verify that the filesystem is a fat32 and that all the desired files were created.

Test 9: 1.With gparted format the current partition to one of type ext4 (select the partition and choose format to... -> ext4) 2. Run my code 3. When I insert the file system is not recognized if I run the command: $mount /dev/{device} I verified the existence of more than one file system. I need to run the command mount -t vfat /dev/{device} to ru the fat32 filesystem and see the files created by my code. -To recover the sdcard I need to delete the two file systems created with the command: sudo dd bs=512 count=100 if=/dev/zero of=/dev/{device}

Steps to format and create the fat32 filesystem:

/* Partition table must be initialized by the user */
PARTITION VolToPart[] = {0}; /* "0:" ==> Physical drive 0, 1st partition */

/* Use a total size of a single partition */
DWORD plist[] = {100};

/* Maximum working area 512 bytes */
BYTE work[_MAX_SS];

/* Creating a partition */
f_fdisk(0, plist, &work);

/* Creating FAT32 file system */
create_fs_result = f_mkfs(DEFAULT_DRIVE, FM_FAT32, DEFAULT_ALLOCATION_UNIT_SIZE, work, sizeof(work));
  • Hi,

    This sounds like a very specific and less common use-case. It could sound like a bug/issue in FatFs, more than a MCU specific issue. Have you tried looking in the documentation of FatFs, to see if it mentions anything about such cases?

    I also see that there is a user forum on the FatFs site, maybe someone there can shed some light on what might be causing this behaviour?

    Also, which device did you try this on (nRF51/nRF52832/nRF52840)? I could try to replicate your issue with one of my devices, if I can get hold of a microSD connector.

    Best regards,

    Jørgen

  • I already searched and I did not find any open issues with this problem. In the forum also I did not find any issue related to this. I tried on the nRF52832 with microSD breakout board from adafruit

  • That behavirour is actually expected, especially when one considers how ext4 and fat32 organize their metatdata.

    FAT32 has all the metadata at the beginning of the partition, except for directory entries. This also means that when formatting only the first part is actually written to, other data would simply stay as it is.

    Ext{2,3,4} keeps a number of "superblock" backups scattered across the entire partition. These won't be overwritten.

    Now mount will find both ext4 superblocks and fat32 boot sector - and thus fail.

    One could erase or overwrite the additional superblocks before formatting fat32 - the mkfs.ext4 command prints out those.

    Another solution would be to use the dedicated SD erase commands (CMD32, CMD33, CMD38) to erase the entire fat32 partition before formatting.

  • Hi, I was unaware of this difference between fat32 and ext4. The explanation makes sense but I do not understand why with gparted if you do new-> Partition-> Ext4 and run my code I can create a partition with only the fat32 file system and if I already have a partition with fat32 file system created by me and select it and make fotmat to-> ext4 and then run my code I get both file systems.

Related