From 686fe053a27849702d05165e4fa0e9ee1930fbd8 Mon Sep 17 00:00:00 2001 From: ellensp <530024+ellensp@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:30:22 +1300 Subject: [PATCH] Don't stop SD listing at an orphaned long name 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. --- src/modules/filesystem/sd/sd_sdfat2_esp32.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/modules/filesystem/sd/sd_sdfat2_esp32.cpp b/src/modules/filesystem/sd/sd_sdfat2_esp32.cpp index 90d877a..93a5b20 100644 --- a/src/modules/filesystem/sd/sd_sdfat2_esp32.cpp +++ b/src/modules/filesystem/sd/sd_sdfat2_esp32.cpp @@ -496,7 +496,21 @@ ESP_SDFile ESP_SDFile::openNextFile() { esp3d_log("openNextFile failed"); return ESP_SDFile(); } - ESP3D_File tmp = tSDFile_handle[_index].openNextFile(); + ESP3D_File& dir = tSDFile_handle[_index]; + ESP3D_File tmp = dir.openNextFile(); + // SdFat stops at a long name entry whose checksum doesn't match the short name + // that follows it, e.g. a bootloader renaming FIRMWARE.BIN to FIRMWARE.CUR. + // Skip such entries and stop only at the end of the directory or on an error. + while (!tmp && !ESP3D_SD_Card.sdErrorCode()) { + // The entry SdFat stopped at is the last one it read + const uint64_t pos = dir.curPosition(); + uint8_t entry[32]; + if (pos < sizeof(entry) || !dir.seekSet(pos - sizeof(entry)) || + dir.read(entry, sizeof(entry)) != sizeof(entry) || entry[0] == 0x00) { + break; // End of directory (free entry) or a read error + } + tmp = dir.openNextFile(); + } if (tmp) { char tmps[255]; tmp.getName(tmps, 254);