Skip to content

fs/fat: fix incorrect representation of SFN files created on Linux#18697

Open
michallenc wants to merge 1 commit intoapache:masterfrom
michallenc:fat-fixes
Open

fs/fat: fix incorrect representation of SFN files created on Linux#18697
michallenc wants to merge 1 commit intoapache:masterfrom
michallenc:fat-fixes

Conversation

@michallenc
Copy link
Copy Markdown
Contributor

@michallenc michallenc commented Apr 9, 2026

Summary

Linux creates all files as long file name entries even if they fit short 8.3 format. This caused issues when deleting or renaming files as the long file name entry was not recognized and only deleted it's short file name entry. This basically kept breaking the file system as subsequent long file name files were not correctly stored. The typical representation of this issue was long file name being represented as it's short name alias.

This commit adjusts the LFN/SFN logic a bit - the code now always fills in long file name and then checks if this could possibly be a short file entry (we still have to do that because Windows stores 8.3 files as short file entry). This ensures compatibility with files created both on Linux and Windows.

The following log is a FAT entry on Linux. You can see short file name config.ini is still written as a long file entry.

touch config.ini on Linux
00042000: 4163 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  Ac.o.n.f.i....g...i.n.i.........
00042020: 434f 4e46 4947 2020 494e 4920 004a 8041 885c 885c 0000 8041 885c 0000 0000 0000  CONFIG  INI .J.A.\.\...A.\......

first rename on Linux
00042000: e563 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  .c.o.n.f.i....g...i.n.i.........
00042020: e54f 4e46 4947 2020 494e 4920 004a 8041 885c 885c 0000 8041 885c 0000 0000 0000  .ONFIG  INI .J.A.\.\...A.\......
00042040: 4237 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  B7.8...i.n....i.................
00042060: 0163 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
00042080: 434f 4e46 4947 7e31 494e 4920 0080 8d41 885c 885c 0000 8d41 885c 0000 0000 0000  CONFIG~1INI ...A.\.\...A.\......

rename back on Linux
00042000: 4163 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  Ac.o.n.f.i....g...i.n.i.........
00042020: 434f 4e46 4947 2020 494e 4920 003c 9741 885c 885c 0000 9741 885c 0000 0000 0000  CONFIG  INI .<.A.\.\...A.\......
00042040: e537 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  .7.8...i.n....i.................
00042060: e563 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
00042080: e54f 4e46 4947 7e31 494e 4920 0080 8d41 885c 885c 0000 8d41 885c 0000 0000 0000  .ONFIG~1INI ...A.\.\...A.\......

second rename on Linux
00042000: e563 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  .c.o.n.f.i....g...i.n.i.........
00042020: e54f 4e46 4947 2020 494e 4920 004a 8041 885c 885c 0000 8041 885c 0000 0000 0000  .ONFIG  INI .J.A.\.\...A.\......
00042040: 4237 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  B7.8...i.n....i.................
00042060: 0163 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
00042080: 434f 4e46 4947 7e31 494e 4920 0049 a541 885c 885c 0000 a541 885c 0000 0000 0000  CONFIG~1INI .I.A.\.\...A.\......

The issue on NuttX is easily reproducible by renaming config.ini file to long name config_12345678.ini. The first rename is ok (though the erase of the old entry already not correct), but subsequent rename back to config.ini on Linux and another rename to long name breaks the system - ls command now doesn't show config_12345678.ini as it should, but it's short file alias CONFIG~1.INI - and it's not just ls, calling open on config_12345678.ini doesn't work either.

touch config.ini on Linux
00042000: 4163 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  Ac.o.n.f.i....g...i.n.i.........
00042020: 434f 4e46 4947 2020 494e 4920 00c1 6f3e 885c 885c 0000 6f3e 885c 0000 0000 0000  CONFIG  INI ..o>.\.\..o>.\......

first rename from config.ini to config_12345678.ini on NuttX - you can see the first line is not erased (first byte should be e5)
00042000: 4163 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  Ac.o.n.f.i....g...i.n.i.........
00042020: e54f 4e46 4947 2020 494e 4920 00c1 6f3e 885c 885c 0000 6f3e 885c 0000 0000 0000  .ONFIG  INI ..o>.\.\..o>.\......
00042040: 4237 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  B7.8...i.n....i.................
00042060: 0163 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
00042080: 434f 4e46 4947 7e31 494e 4920 00c1 6f3e 885c 885c 0000 6f3e 885c 0000 0000 0000  CONFIG~1INI ..o>.\.\..o>.\......

rename back to config.ini on Linux
00042000: 4163 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  Ac.o.n.f.i....g...i.n.i.........
00042020: e54f 4e46 4947 2020 494e 4920 00c1 6f3e 885c 885c 0000 6f3e 885c 0000 0000 0000  .ONFIG  INI ..o>.\.\..o>.\......
00042040: e537 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  .7.8...i.n....i.................
00042060: e563 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
00042080: e54f 4e46 4947 7e31 494e 4920 00c1 6f3e 885c 885c 0000 6f3e 885c 0000 0000 0000  .ONFIG~1INI ..o>.\.\..o>.\......
000420a0: 4163 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  Ac.o.n.f.i....g...i.n.i.........
000420c0: 434f 4e46 4947 2020 494e 4920 0020 ac3e 885c 885c 0000 ac3e 885c 0000 0000 0000  CONFIG  INI . .>.\.\...>.\......

second rename on NuttX -> ls shows CONFIG~1.INI
00042000: 4163 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  Ac.o.n.f.i....g...i.n.i.........
00042020: 4237 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  B7.8...i.n....i.................
00042040: 0163 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
00042060: 434f 4e46 4947 7e31 494e 4920 00c1 6f3e 885c 885c 0000 6f3e 885c 0000 0000 0000  CONFIG~1INI ..o>.\.\..o>.\......
00042080: e54f 4e46 4947 7e31 494e 4920 00c1 6f3e 885c 885c 0000 6f3e 885c 0000 0000 0000  .ONFIG~1INI ..o>.\.\..o>.\......
000420a0: 4163 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  Ac.o.n.f.i....g...i.n.i.........
000420c0: e54f 4e46 4947 2020 494e 4920 00c1 6f3e 885c 885c 0000 6f3e 885c 0000 0000 0000  .ONFIG  INI ..o>.\.\..o>.\......

Impact

fs/fat: Fix long/short file (re)name issues caused by incorrect representation of SFN files created on Linux.

Testing

Here are the logs after the fix - both Linux and Windows created files are correctly renamed now.

Linux

touch config.ini
00042000: 4163 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  Ac.o.n.f.i....g...i.n.i.........
00042020: 434f 4e46 4947 2020 494e 4920 00a2 d67d 895c 895c 0000 d67d 895c 0000 0000 0000  CONFIG  INI ...}.\.\...}.\......

first rename from config.ini to config_12345678.ini on NuttX - first entry is correctly erased
00042000: e563 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  .c.o.n.f.i....g...i.n.i.........
00042020: e54f 4e46 4947 2020 494e 4920 00a2 d67d 895c 895c 0000 d67d 895c 0000 0000 0000  .ONFIG  INI ...}.\.\...}.\......
00042040: 4237 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  B7.8...i.n....i.................
00042060: 0163 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
00042080: 434f 4e46 4947 7e31 494e 4920 00a2 d67d 895c 895c 0000 d67d 895c 0000 0000 0000  CONFIG~1INI ...}.\.\...}.\......

rename back to config.ini on Linux
00042000: 4163 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  Ac.o.n.f.i....g...i.n.i.........
00042020: 434f 4e46 4947 2020 494e 4920 00b3 f87d 895c 895c 0000 f87d 895c 0000 0000 0000  CONFIG  INI ...}.\.\...}.\......
00042040: e537 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  .7.8...i.n....i.................
00042060: e563 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
00042080: e54f 4e46 4947 7e31 494e 4920 00a2 d67d 895c 895c 0000 d67d 895c 0000 0000 0000  .ONFIG~1INI ...}.\.\...}.\......

second rename on NuttX -> ls shows correctly config_12345678.ini
00042000: e563 006f 006e 0066 0069 000f 00e6 6700 2e00 6900 6e00 6900 0000 0000 ffff ffff  .c.o.n.f.i....g...i.n.i.........
00042020: e54f 4e46 4947 2020 494e 4920 00a2 d67d 895c 895c 0000 d67d 895c 0000 0000 0000  .ONFIG  INI ...}.\.\...}.\......
00042040: 4237 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  B7.8...i.n....i.................
00042060: 0163 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
00042080: 434f 4e46 4947 7e31 494e 4920 00a2 d67d 895c 895c 0000 d67d 895c 0000 0000 0000  CONFIG~1INI ...}.\.\...}.\......

echo "Hello" > /sdcard/test.txt - file with short name still takes only one entry
00042000: 5445 5354 2020 2020 5458 5420 1800 0000 0000 0000 0000 0000 0000 0200 0600 0000  TEST    TXT ....................
00042020: e54f 4e46 4947 2020 494e 4920 00a2 d67d 895c 895c 0000 d67d 895c 0000 0000 0000  .ONFIG  INI ...}.\.\...}.\......
00042040: 4237 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  B7.8...i.n....i.................
00042060: 0163 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
00042080: 434f 4e46 4947 7e31 494e 4920 00a2 d67d 895c 895c 0000 d67d 895c 0000 0000 0000  CONFIG~1INI ...}.\.\...}.\......

Windows

created config.ini -> short file name on Windows 10
00042000: 4220 0049 006e 0066 006f 000f 0072 7200 6d00 6100 7400 6900 6f00 0000 6e00 0000  B .I.n.f.o...rr.m.a.t.i.o...n...
00042020: 0153 0079 0073 0074 0065 000f 0072 6d00 2000 5600 6f00 6c00 7500 0000 6d00 6500  .S.y.s.t.e...rm. .V.o.l.u...m.e.
00042040: 5359 5354 454d 7e31 2020 2016 0088 ec8d 895c 895c 0000 ed8d 895c 0200 0000 0000  SYSTEM~1   ......\.\.....\......
00042060: 434f 4e46 4947 2020 494e 4920 1807 f58d 895c 895c 0000 3582 885c 0000 0000 0000  CONFIG  INI .....\.\..5..\......

first rename from config.ini to config_12345678.ini on NuttX
00042000: 4220 0049 006e 0066 006f 000f 0072 7200 6d00 6100 7400 6900 6f00 0000 6e00 0000  B .I.n.f.o...rr.m.a.t.i.o...n...
00042020: 0153 0079 0073 0074 0065 000f 0072 6d00 2000 5600 6f00 6c00 7500 0000 6d00 6500  .S.y.s.t.e...rm. .V.o.l.u...m.e.
00042040: 5359 5354 454d 7e31 2020 2016 0088 ec8d 895c 895c 0000 ed8d 895c 0200 0000 0000  SYSTEM~1   ......\.\.....\......
00042060: e54f 4e46 4947 2020 494e 4920 1807 f58d 895c 895c 0000 3582 885c 0000 0000 0000  .ONFIG  INI .....\.\..5..\......
00042080: 4237 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  B7.8...i.n....i.................
000420a0: 0163 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
000420c0: 434f 4e46 4947 7e31 494e 4920 1807 f58d 895c 895c 0000 3582 885c 0000 0000 0000  CONFIG~1INI .....\.\..5..\......

rename back to config.ini on Windows
00042000: 4220 0049 006e 0066 006f 000f 0072 7200 6d00 6100 7400 6900 6f00 0000 6e00 0000  B .I.n.f.o...rr.m.a.t.i.o...n...
00042020: 0153 0079 0073 0074 0065 000f 0072 6d00 2000 5600 6f00 6c00 7500 0000 6d00 6500  .S.y.s.t.e...rm. .V.o.l.u...m.e.
00042040: 5359 5354 454d 7e31 2020 2016 0088 ec8d 895c 895c 0000 ed8d 895c 0200 0000 0000  SYSTEM~1   ......\.\.....\......
00042060: e54f 4e46 4947 2020 494e 4920 1807 f58d 895c 895c 0000 3582 885c 0000 0000 0000  .ONFIG  INI .....\.\..5..\......
00042080: e537 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  .7.8...i.n....i.................
000420a0: e563 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
000420c0: e54f 4e46 4947 7e31 494e 4920 1807 f58d 895c 895c 0000 3582 885c 0000 0000 0000  .ONFIG~1INI .....\.\..5..\......
000420e0: 434f 4e46 4947 2020 494e 4920 1807 f58d 895c 895c 0000 3582 885c 0000 0000 0000  CONFIG  INI .....\.\..5..\......

second rename on NuttX -> ls shows correctly config_12345678.ini
00042000: 4220 0049 006e 0066 006f 000f 0072 7200 6d00 6100 7400 6900 6f00 0000 6e00 0000  B .I.n.f.o...rr.m.a.t.i.o...n...
00042020: 0153 0079 0073 0074 0065 000f 0072 6d00 2000 5600 6f00 6c00 7500 0000 6d00 6500  .S.y.s.t.e...rm. .V.o.l.u...m.e.
00042040: 5359 5354 454d 7e31 2020 2016 0088 ec8d 895c 895c 0000 ed8d 895c 0200 0000 0000  SYSTEM~1   ......\.\.....\......
00042060: 4237 0038 002e 0069 006e 000f 00ee 6900 0000 ffff ffff ffff ffff 0000 ffff ffff  B7.8...i.n....i.................
00042080: 0163 006f 006e 0066 0069 000f 00ee 6700 5f00 3100 3200 3300 3400 0000 3500 3600  .c.o.n.f.i....g._.1.2.3.4...5.6.
000420a0: 434f 4e46 4947 7e31 494e 4920 1807 f58d 895c 895c 0000 3582 885c 0000 0000 0000  CONFIG~1INI .....\.\..5..\......
000420c0: e54f 4e46 4947 7e31 494e 4920 1807 f58d 895c 895c 0000 3582 885c 0000 0000 0000  .ONFIG~1INI .....\.\..5..\......
000420e0: e54f 4e46 4947 2020 494e 4920 1807 f58d 895c 895c 0000 3582 885c 0000 0000 0000  .ONFIG  INI .....\.\..5..\......

I kept the original design that NuttX creates a file with short name as a short file entry - it seems like a better strategy for us since we are embedded and one would expect we wouldn't waste memory unless really have to.

Linux creates all files as long file name entries even if they
fit short 8.3 format. This caused issues when deleting or renaming
files as the long file name entry was not recognized and only deleted
it's short file name entry. This basically kept breaking the file
system as subsequent long file name files were not correctly stored.
The typical representation of this issue was long file name being
represented as it's short name alias.

This commit adjusts the LFN/SFN logic a bit - the code now always
fills in long file name and then checks if this could possibly be
a short file entry (we still have to do that because Windows stores
8.3 files as short file entry). This ensures compatibility with
files created both on Linux and Windows.

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
@michallenc
Copy link
Copy Markdown
Contributor Author

michallenc commented Apr 9, 2026

It would be lovely if you could test this in your environment as well to ensure I didn't break anything.

@acassis
Copy link
Copy Markdown
Contributor

acassis commented Apr 9, 2026

@michallenc nice fix! Strange nobody faced it before. Do you know if the same issue happens if using Windows/MacOS ?

@michallenc
Copy link
Copy Markdown
Contributor Author

@michallenc nice fix! Strange nobody faced it before. Do you know if the same issue happens if using Windows/MacOS ?

It doesn't happen when using Windows because they store file with short name as a short entry so NuttX correctly removes it. Not sure about MacOS, don't have one to try.

@github-actions github-actions bot added Area: File System File System issues Size: S The size of the change in this PR is small labels Apr 9, 2026
@cederom
Copy link
Copy Markdown
Contributor

cederom commented Apr 9, 2026

I can check on FreeBSD.. what are test steps? I assume this is about FAT on the NuttX side? :-)

@michallenc
Copy link
Copy Markdown
Contributor Author

I can check on FreeBSD.. what are test steps? I assume this is about FAT on the NuttX side? :-)

Yes, it's about FAT on the NuttX side. Testing is pretty simple

  1. create a file with short name on FAT on your computer (I used config.ini)
  2. rename the file to config_12345678.ini on NuttX
  3. rename it back to config.ini on your computer
  4. rename it again to config_12345678.ini on NuttX
  5. you should see CONFIG~1.INI instead of config_12345678.ini

I have this simple C code that does the rename on NuttX for you.

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
	openlog("sdcard", LOG_PERROR, LOG_USER);
	int fd = open("/sdcard/config.ini", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
	if (fd < 0) {
		syslog(LOG_ERR, "Failed to create SD card %d\n", errno);
		return -1;
	}

	close(fd);
	char newpath[64];
	uint32_t rnd = argc > 1 ? atol(argv[1]) : 0x12345678;
	sprintf(newpath, "%s_%08" PRIx32 ".ini", "/sdcard/config", rnd);
	rename("sdcard/config.ini", newpath);
	return 0;
}

This happens ONLY IF your computer creates the initial config.ini as a long file name entry - which is a case of Linux (at least my Debian 12). Windows creates it as short file name entry and there it's fine. The main issue on NuttX is that we expect there is short file name entry only if the name fits into 8+3 format, which is not if the file is created on Linux.

But I am more concerned whether I break anything on NuttX FAT side - hopefully not, I tested file creation, rename, delete and it all seems to work without issues.

@cederom
Copy link
Copy Markdown
Contributor

cederom commented Apr 9, 2026

Okay, my box does not create anything on its own for sure, I even need to mount things by hand :D

What board:config can you recommend? I guess we need usbmsc? Or something with SD card reader? :-P

@cederom
Copy link
Copy Markdown
Contributor

cederom commented Apr 10, 2026

  • Hmm, I was not able to replicate this problem with raspberrypi-4b:sd on FreeBSD 14.4 nor macOS 26 with current nuttx master and your branch @michallenc. Thus it looks like Linux specific problem?
  • I also tried your patch and was not able to repeat the problem and all seemed to work okay. If you make this behavior not possible at all then the fix is good :-)
  • Regarding FAT support from my experience it is very very important on how the memory is formatted:
    • What exact FAT is used, sometimes FAT12, sometimes FAT16.
    • What is the partition table if there is any or none.
    • Fresh pendrives / sd cards tend to have strange initial format and it caused many problems many times over the years. Thus I always format all new memories by hand anyway as a rule :-)
  • You may try to recall if you used fresh card that you did not format, or you are sure you did format it yourself? You may want to dump the first 512 bytes of the memory to verify MBR. If fat is not FAT32 and there are no partitions then Linux may do some magic on its own behind the scenes thus the problem encountered. If so you may report a Linux bug :-)
  • Below is the MBR dump of my memory card:
% xxd -l 512 /dev/da0s1
00000000: eb58 906d 6b66 732e 6661 7400 0204 2000  .X.mkfs.fat... .
00000010: 0200 0000 00f8 0000 3f00 ff00 0040 0000  ........?....@..
00000020: fcff 0f00 f807 0000 0000 0000 0200 0000  ................
00000030: 0100 0600 0000 0000 0000 0000 0000 0000  ................
00000040: 8001 2990 b80b 3a62 6f6f 7466 7320 2020  ..)...:bootfs
00000050: 2020 4641 5433 3220 2020 0e1f be77 7cac    FAT32   ...w|.
00000060: 22c0 740b 56b4 0ebb 0700 cd10 5eeb f032  ".t.V.......^..2
00000070: e4cd 16cd 19eb fe54 6869 7320 6973 206e  .......This is n
00000080: 6f74 2061 2062 6f6f 7461 626c 6520 6469  ot a bootable di
00000090: 736b 2e20 2050 6c65 6173 6520 696e 7365  sk.  Please inse
000000a0: 7274 2061 2062 6f6f 7461 626c 6520 666c  rt a bootable fl
000000b0: 6f70 7079 2061 6e64 0d0a 7072 6573 7320  oppy and..press
000000c0: 616e 7920 6b65 7920 746f 2074 7279 2061  any key to try a
000000d0: 6761 696e 202e 2e2e 200d 0a00 0000 0000  gain ... .......
000000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000110: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000120: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000130: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000140: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000160: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000170: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000180: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000190: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001f0: 0000 0000 0000 0000 0000 0000 0000 55aa  ..............U.

Copy link
Copy Markdown
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @michallenc good catch! :-)

If there is anyone using FAT insensively more testing is welcome :-)

I did call for testing on the mailing list :-)

{
/* Get short file name for given path */

char *name = kmm_zalloc(DIR_MAXFNAME);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we avoid malloc here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because of FAR const char *node = *path; line in fat_parsesfname. And we need to keep the possibility to edit path pointer because calling fat_parsesfname when CONFIG_FAT_LFN=n needs to move it after / terminator.

It could be done, but would require larger rewrite of these functions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DIR_MAXFNAME is 11 why not define local array directly

@michallenc
Copy link
Copy Markdown
Contributor Author

  • Hmm, I was not able to replicate this problem with raspberrypi-4b:sd on FreeBSD 14.4 nor macOS 26 with current nuttx master and your branch @michallenc. Thus it looks like Linux specific problem?

    • I also tried your patch and was not able to repeat the problem and all seemed to work okay. If you make this behavior not possible at all then the fix is good :-)

    • Regarding FAT support from my experience it is very very important on how the memory is formatted:

      • What exact FAT is used, sometimes FAT12, sometimes FAT16.
      • What is the partition table if there is any or none.
      • Fresh pendrives / sd cards tend to have strange initial format and it caused many problems many times over the years. Thus I always format all new memories by hand anyway as a rule :-)
    • You may try to recall if you used fresh card that you did not format, or you are sure you did format it yourself? You may want to dump the first 512 bytes of the memory to verify MBR. If fat is not FAT32 and there are no partitions then Linux may do some magic on its own behind the scenes thus the problem encountered. If so you may report a Linux bug :-)

    • Below is the MBR dump of my memory card:

% xxd -l 512 /dev/da0s1
00000000: eb58 906d 6b66 732e 6661 7400 0204 2000  .X.mkfs.fat... .
00000010: 0200 0000 00f8 0000 3f00 ff00 0040 0000  ........?....@..
00000020: fcff 0f00 f807 0000 0000 0000 0200 0000  ................
00000030: 0100 0600 0000 0000 0000 0000 0000 0000  ................
00000040: 8001 2990 b80b 3a62 6f6f 7466 7320 2020  ..)...:bootfs
00000050: 2020 4641 5433 3220 2020 0e1f be77 7cac    FAT32   ...w|.
00000060: 22c0 740b 56b4 0ebb 0700 cd10 5eeb f032  ".t.V.......^..2
00000070: e4cd 16cd 19eb fe54 6869 7320 6973 206e  .......This is n
00000080: 6f74 2061 2062 6f6f 7461 626c 6520 6469  ot a bootable di
00000090: 736b 2e20 2050 6c65 6173 6520 696e 7365  sk.  Please inse
000000a0: 7274 2061 2062 6f6f 7461 626c 6520 666c  rt a bootable fl
000000b0: 6f70 7079 2061 6e64 0d0a 7072 6573 7320  oppy and..press
000000c0: 616e 7920 6b65 7920 746f 2074 7279 2061  any key to try a
000000d0: 6761 696e 202e 2e2e 200d 0a00 0000 0000  gain ... .......
000000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000110: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000120: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000130: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000140: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000160: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000170: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000180: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000190: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001f0: 0000 0000 0000 0000 0000 0000 0000 55aa  ..............U.

I formatted the card many times and tried both FAT16 (that's the default for my SD card since it only has about 400 MBs) and FAT32, there is no difference in how the file are created - it should just affect the size of the allocation table. I think this is more of a implementation simplification on Linux side and our wrong expectation that it has to be short file entry if the file name fits into 8+3 scheme.

Regarding your dump, what it look like if you find the file entry? You should be able to grep 'CONFIG` or whatever name you used - just try it with freshly formatted SD card, there might be left old deleted entries otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: File System File System issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants