SD card read errors are not reported to the host and send bad data

Aperta
#11,715 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@mikeysklar ci sta già lavorando.

Dal 3/11/2026.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c
Ambito
embedded-iot

Direzione di ricerca

Start with tud_msc_read10_cb() and tud_msc_write10_cb() in supervisor/shared/usb/usb_msc_flash.c, checking the result of disk_read() and disk_write() from extmod/vfs_fat_diskio.c. Then read sdcardio_sdcard_readblocks() in shared-module/sdcardio/SDCard.c, where the CMD12 result overwrites the earlier error. Done means failed reads and writes return an error to TinyUSB, the multi-block path returns the first error, and the misleading comment is corrected. Choosing between TUD_MSC_RET_ERROR and TUD_MSC_RET_BUSY needs a maintainer decision.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug sdcardio storage usb

Claude wrote this:

In https://github.com/adafruit/circuitpython/issues/11692#issuecomment-6526961492, @jedgarpark found that when sdcardio could not get the SPI bus lock, a failed read was not reported, and sent bad data. On their card, a later write built from those stale sectors emptied a file and wiped a directory.

The comment blamed FatFs, but CircuitPython's own FatFs mount does see the error: disk_read() in extmod/vfs_fat_diskio.c turns any nonzero result into RES_ERROR. The stale data more likely went out over USB, because their build presented the SD card to the computer (CIRCUITPY_SDCARD_USB). Checking main at cf41418682 shows two places where a read error is dropped, and one wrong comment.

Problems
  • tud_msc_read10_cb() in supervisor/shared/usb/usb_msc_flash.c ignores the result of disk_read() and always returns the full byte count. When the read fails, the computer receives whatever was already in the buffer as if it were the card's data. If the computer then writes back a sector built from that data, the filesystem is damaged. tud_msc_write10_cb() likewise ignores the result of disk_write(), so a failed write looks successful to the computer.
  • In sdcardio_sdcard_readblocks() in shared-module/sdcardio/SDCard.c, a multi-block read keeps any error from CMD18 or readinto() in r. It then overwrites r with the result of the CMD12 stop command, so a failed multi-block read can return 0.
  • The comment // No caller actually uses this value. at the end of sdcardio_sdcard_readblocks() is wrong. disk_read() uses the value as described above, and SDCard.readblocks() raises OSError on a negative value.
Possible fix
  • Return TUD_MSC_RET_ERROR from tud_msc_read10_cb() and tud_msc_write10_cb() when disk_read() or disk_write() does not return RES_OK. TinyUSB then fails the SCSI command instead of sending the buffer. Returning TUD_MSC_RET_BUSY instead would make TinyUSB retry the callback, which might suit a bus-lock timeout better.
  • In the multi-block path, save the first error before sending CMD12 and return it.
  • Remove or correct the comment.
Lingua principale
C
Stelle
4.6k
Fork
1.4k
Merge medio
1g 8h
PR unite (30g)
183

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di adafruit/circuitpython

Tutte le issue di adafruit/circuitpython

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.