Skip to content

Don't stop SD listing at an orphaned long name - #98

Merged
luc-github merged 1 commit into
luc-github:3.0.beta1from
ellensp:fix-sd-orphan-long-name
Oct 24, 2026
Merged

luc-github merged 1 commit into
luc-github:3.0.beta1from
ellensp:fix-sd-orphan-long-name

Conversation

@ellensp

@ellensp ellensp commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Don't stop SD listing at an orphaned long name

Description

With an SD card that was previously used to update another printer board, the ESP3D SD file list is empty: [ESP740] reports Files: 0, Dirs :0, and the web file list returns "files":[], although the card has files on it.

SD bootloaders on many printer boards flash firmware.bin and then rename it to FIRMWARE.CUR, changing only the 8.3 name. The long name entry for firmware.bin stays in front of it. These are the first two root directory entries of the test card, read on the board:

entry 0: 41 66 00 69 00 72 00 6D 00 77 00 0F 00 57 ...   long name "firmware.bin", checksum 0x57
entry 1: 46 49 52 4D 57 41 52 45 43 55 52 20 ...         short name FIRMWARE.CUR

The long name's checksum, 0x57, is the checksum of FIRMWARE.BIN. FIRMWARE.CUR has a checksum of 0x21. SdFat's FatFile::openNext() treats that mismatch as a failure (if (lfnOrd && checksum != lfnChecksum(dir->name)) goto fail;, the same in SdFat 2.2.3, 2.3.0 and 2.3.1). So ESP_SDFile::openNextFile() in sd_sdfat2_esp32.cpp returns no file at the first entry, and every listing of that directory ends there. Linux, and Marlin's own SD code, ignore the orphaned long name and list the rest of the directory.

When SdFat's openNextFile() fails, ESP_SDFile::openNextFile() now looks at the last 32-byte entry it read:

  • If that entry is the end of the directory (first byte 0x00), or the card reports an error, it stops as before.
  • Otherwise it calls openNextFile() again, which continues from the next entry.

The entry that failed (FIRMWARE.CUR here) is skipped, because SdFat won't open it.

Testing

MKS TinyBee, Marlin bugfix-2.1.x with arduino-esp32 3.0.4 (pioarduino 51.03.04) and SdFat 2.2.3. The card has FIRMWARE.CUR (as above), a .Trash-1000 directory and webtest.gcode:

[ESP740] (telnet) /sdfiles?path=/
Before Files: 0, Dirs :0 "files":[]
After [DIR] .Trash-1000, webtest.gcode 53 B, Files: 1, Dirs :1 .Trash-1000 and webtest.gcode

Total and used space are reported the same before and after (7.42 GB, 340 KB).

@luc-github

Copy link
Copy Markdown
Owner

@ellensp can you clarify the context ?
Currently ESP32 MKS TinyBee (esp32) flashed with Marlin do not have custom boot loader (or I missed the code?) and so no mechanism to flash and rename firmware.bin to FIRMWARE.CUR is present in bootloader, as far I know this feature in only present on STM32 models of MKS Boards.
FYR: ESP3DLib do it at firmware level, not bootloader : esp3dfw.bin -> esp3dbin.ok if success or esp3dbin.bad if any issue
and that need 2 OTA partitions because the active one flash the new one and switch the boot partition if ok

I do not denie the issue you raised and fixed but I do not understand how you could meet it

SdFat's openNext() fails when a long name entry's checksum doesn't match
the short name entry that follows it. SD bootloaders on many printer
boards leave exactly that when they rename firmware.bin to FIRMWARE.CUR,
so the SD file list came back empty with a card that had been used to
update such a board.

Keep reading after such a failure, and stop only at the end of the
directory or on a card error.
@ellensp
ellensp force-pushed the fix-sd-orphan-long-name branch from 5f1c5ca to 686fe05 Compare October 24, 2026 05:13
@ellensp

ellensp commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Sorry for the confusion in my first description. I wrote that the TinyBee's bootloader had renamed firmware.bin to FIRMWARE.CUR, but the TinyBee has no SD bootloader. The card had been used to update another printer board before, and that board's bootloader left the orphaned long name behind.

I've corrected the description and the commit message. The code change is the same: any card that has been through one of those bootloaders hits this, whichever board reads it afterwards.

@luc-github

Copy link
Copy Markdown
Owner

Ok that make more sense - thank you for the clarification

@luc-github
luc-github merged commit 63a5ea2 into luc-github:3.0.beta1 Oct 24, 2026
2 checks passed
@luc-github

Copy link
Copy Markdown
Owner

Merged thank you

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants