Fatfsgen.py fails to create bin partition

maurizio.scian
Posts: 26
Joined: Mon Jun 26, 2023 8:09 am

Fatfsgen.py fails to create bin partition

Postby maurizio.scian » Sat Jun 07, 2025 3:21 pm

I have a fatfs partition in my controller. If the partition is initialized and written by the controller it works really well.
Now i want write this partition with esptool. Before, i need to create the binary fatfs partition, using fatfsgen: i create a folder with the file i need and launch this command.

Code: Select all

python fatfs\fatfsgen.py --output_file myfatfs.bin --partition_size 0x90000 --sector_size 4096 --use_default_datetime fatfs_temp
It correctly generate a file myfatfs.bin, with size 576KB.
I can also check this file with fatfsparse.py:

Code: Select all

python fatfs\fatfsparse.py myfatfs.bin
It generates a folder "Espressif" with my files inside.
Well, I write the partition inside my controller:

Code: Select all

esptool --chip esp32s3 -b 460800 --before default_reset --after hard_reset write_flash --flash_mode dio --flash_freq 40m 0xEE2000 myfatfs.bin
And it works. I can check the partition with esptool: the partition is correctly written, no difference in binary comparison between the binary written and the binary read.

But microcontroller can't mount this partition: it shows error 13. After that, i wrote the files by wi-fi upload, so it stores the files correctly in the fatfs partition, and all works.

So, i tried to read the partition with esptool, due to compare the binary files. The broken binary generated by fatfsgen and the correct binary dumped from the controller are very similar, with small differences if i compare the files with a hex editor.
From location 0 to 0x1000 there's no difference.
At location 0x1000, in the fatfsgen's generated file i can find:

Code: Select all

f8 ff ff ff ff ff ff 0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
and at the same location, in the working controller's binary i can find:

Code: Select all

eb fe 90 4d 53 44 4f 53 35 2e 30 20 10 01 01 20 01 20 02 8c 20 f8 01 20 3f 20 ff 20 20 20 20 20 20 20 20 20 80 20 29 f7 85 99 58 4e 4f 20 4e 41 4d 45 20 20 20 20 46 41 54 20 20 20 20 20
There's no other difference between the files, but it's enough for the mounting partition error.
What is the problem?

Sprite
Espressif staff
Espressif staff
Posts: 10596
Joined: Thu Nov 26, 2015 4:08 am

Re: Fatfsgen.py fails to create bin partition

Postby Sprite » Sun Jun 08, 2025 8:37 am

Perhaps a wear leveling issue? If you mount FAT read/write, you need a wear leveling enabled image, if you only mount it read-only, you can use a 'raw' image. You cannot mount one with the other and vice-versa.

maurizio.scian
Posts: 26
Joined: Mon Jun 26, 2023 8:09 am

Re: Fatfsgen.py fails to create bin partition

Postby maurizio.scian » Tue Jun 10, 2025 7:43 am

Hi, Sprite, thanks for the answer.

My sdkconfig configuration about wear levelling is that:

Code: Select all

#
# Wear Levelling
#
# CONFIG_WL_SECTOR_SIZE_512 is not set
CONFIG_WL_SECTOR_SIZE_4096=y
CONFIG_WL_SECTOR_SIZE=4096
# end of Wear Levelling
Using the ESP-IDF Menuconfig, i found this:

Code: Select all

WL_SECTOR_MODE(=)
WL_SECTOR_MODE_PERF(=n) "Perfomance"
WL_SECTOR_MODE_SAFE(=n) "Safety"
WL_SECTOR_SIZE(=4096)
WL_SECTOR_SIZE_4096(=y) "4096"
WL_SECTOR_SIZE_512(=n) "512"
Wear Levelling don't seem enabled. If not, I don't know how this can change the image, but i don't need this feature at all: the fat memory is written one only time.

My fat partition initialization is this:

Code: Select all

static const esp_vfs_fat_mount_config_t mount_config = {
	.max_files = 4,
	.format_if_mount_failed = true,
	.allocation_unit_size = CONFIG_WL_SECTOR_SIZE,
};

bool CertificatesMountPartition() {
	return esp_vfs_fat_spiflash_mount_rw_wl("/CRT", "certificates", &mount_config, &s_wl_handle);
}
The fat memory is mounted R/W, so the binary image is a R/W mounted binary image. I can't understand why it should be different from the binary generated from fatfsgen. I don't mount the image in read-only mode because i can access to it by webserver, but it's an exceptional condition.

So, how i can generate a mountable image with fatfsgen?

maurizio.scian
Posts: 26
Joined: Mon Jun 26, 2023 8:09 am

Re: Fatfsgen.py fails to create bin partition

Postby maurizio.scian » Tue Jun 10, 2025 8:38 am

Ok, I generated a mountable image: as Sprite mentioned, fatfsgen is only for readonly images. If you want to generate a R/W mountable image you need to use wl_fatfsgen:

Code: Select all

wl_fatfsgen.py --output_file figa.bin --partition_size 0x90000 --sector_size 4096 --use_default_datetime fatfs_temp
(Where fatfs_temp is the name of the directory).

Also, even if the image is mounted to a certain base path (in my case "/CRT") by the microcontroller, the folder from which the image is taken must not contain the CRT folder as I initially did.

So, if you the microcontroller find 2 files inside the /CRT path, mount with this pattern:

Code: Select all

return esp_vfs_fat_spiflash_mount_rw_wl("/CRT", "partitionlabel", &mount_config, &s_wl_handle);
And the image is generated from a folder like this:

Code: Select all

fatfs_temp
	--->file1.xxx
	--->file2.xxx
And NOT like this:

Code: Select all

fatfs_temp
	--->CRT
		--->file1.xxx
		--->file2.xxx

Sprite
Espressif staff
Espressif staff
Posts: 10596
Joined: Thu Nov 26, 2015 4:08 am

Re: Fatfsgen.py fails to create bin partition

Postby Sprite » Tue Jun 10, 2025 2:26 pm

Glad you managed to get it to work and thanks for posting the solutions and gotchas!

Who is online

Users browsing this forum: Applebot, Baidu [Spider], Bing [Bot], ChatGPT-User, Qwantbot and 4 guests