mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] mci: Fix erase_grp_size extraction for SD with csd_struct v0
@ 2024-10-01  9:57 Sascha Hauer
  2024-10-01 10:09 ` Ahmad Fatoum
  2024-10-01 10:33 ` Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Sascha Hauer @ 2024-10-01  9:57 UTC (permalink / raw)
  To: Barebox List; +Cc: afa

In mci_extract_erase_group_size() we test for (IS_SD() && csd_struct_v1)
which means that for non csd_struct_v1 SD cards we fall into the MMC
path and evaluate ext_csd. This doesn't exist for SD cards and the
code crashes with a NULL pointer deref.

Fix this by always falling into the SD case for SD cards. Add the
missing erase_grp_size extraction for csd_struct v0 cards. The code
for that is taken from the corresponding Linux code.

Fixes: 91a11c7d50 ("mci: add support for discarding write blocks")
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/mci/mci-core.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index ec4a5c0749..48a3df9ec9 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -1228,9 +1228,20 @@ static void mci_extract_erase_group_size(struct mci *mci)
 		return;
 
 
-	if (IS_SD(mci) && UNSTUFF_BITS(mci->csd, 126, 2) != 0) {
-		/* For SD with csd_struct v1, erase group is always one sector */
-		mci->erase_grp_size = 1;
+	if (IS_SD(mci)) {
+		if (UNSTUFF_BITS(mci->csd, 126, 2) == 0) {
+			unsigned int write_blkbits = UNSTUFF_BITS(mci->csd, 22, 4);
+
+			if (UNSTUFF_BITS(mci->csd, 46, 1)) {
+				mci->erase_grp_size = 1;
+			} else if (write_blkbits >= 9) {
+				mci->erase_grp_size = UNSTUFF_BITS(mci->csd, 39, 7) + 1;
+				mci->erase_grp_size <<= write_blkbits - 9;
+			}
+		} else {
+			/* For SD with csd_struct v1, erase group is always one sector */
+			mci->erase_grp_size = 1;
+		}
 	} else {
 		if (mci->ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
 			/* Read out group size from ext_csd */
-- 
2.39.5




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-01 10:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-01  9:57 [PATCH] mci: Fix erase_grp_size extraction for SD with csd_struct v0 Sascha Hauer
2024-10-01 10:09 ` Ahmad Fatoum
2024-10-01 10:33 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox