Skip to content

Commit

Permalink
sd: sd_ops: take card lock when issuing IOCTL command
Browse files Browse the repository at this point in the history
Take card lock when running IOCTL command, to avoid race conditions that
could occur within the lower SDHC transfer implementations (as these
will be called by sdmmc_wait_ready)

Fixes #72368

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
  • Loading branch information
danieldegrasse authored and nashif committed May 16, 2024
1 parent d44a414 commit 05c84cc
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions subsys/sd/sd_ops.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 NXP
* Copyright 2022,2024 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -773,6 +773,13 @@ int card_write_blocks(struct sd_card *card, const uint8_t *wbuf, uint32_t start_
/* IO Control handler for SD MMC */
int card_ioctl(struct sd_card *card, uint8_t cmd, void *buf)
{
int ret;

ret = k_mutex_lock(&card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT));
if (ret) {
LOG_WRN("Could not get SD card mutex");
return ret;
}
switch (cmd) {
case DISK_IOCTL_GET_SECTOR_COUNT:
(*(uint32_t *)buf) = card->block_count;
Expand All @@ -786,9 +793,10 @@ int card_ioctl(struct sd_card *card, uint8_t cmd, void *buf)
* Note that SD stack does not support enabling caching, so
* cache flush is not required here
*/
return sdmmc_wait_ready(card);
ret = sdmmc_wait_ready(card);
default:
return -ENOTSUP;
ret = -ENOTSUP;
}
return 0;
k_mutex_unlock(&card->lock);
return ret;
}

0 comments on commit 05c84cc

Please sign in to comment.