From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Cc: afa@pengutronix.de
Subject: [PATCH] mci: Fix erase_grp_size extraction for SD with csd_struct v0
Date: Tue, 1 Oct 2024 11:57:41 +0200 [thread overview]
Message-ID: <20241001095741.920605-1-s.hauer@pengutronix.de> (raw)
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
next reply other threads:[~2024-10-01 9:58 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-01 9:57 Sascha Hauer [this message]
2024-10-01 10:09 ` Ahmad Fatoum
2024-10-01 10:33 ` Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241001095741.920605-1-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=afa@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox