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

已关闭
#11,715 0 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@mikeysklar 已经在做这个了。

开始于 2026年11月3日。

  • #11752 来自 @mikeysklar —— 已合并

评估

难度
3/5
预计耗时
1-2 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c
领域
embedded-iot

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.
主要语言
C
星标
4.6k
派生
1.4k
平均合并
1 天 7 小时
30 天内合并 PR
187

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

adafruit/circuitpython 的其他 Issue

查看 adafruit/circuitpython 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。