Skip to content

Commit 5d0b224

Browse files
committed
Fix SDHCI response bit span check
F/1471
1 parent 44db8b3 commit 5d0b224

3 files changed

Lines changed: 107 additions & 2 deletions

File tree

src/sdhci.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ static uint32_t sdhci_get_response_bits(int from, int count)
565565
resp[3] = SDHCI_REG(SDHCI_SRS07);
566566

567567
ret = resp[off] >> shft;
568-
if ((from + shft) > 32) {
568+
if ((shft + count) > 32) {
569569
ret |= resp[off + 1] << ((32 - shft) % 32);
570570
}
571571
return ret & mask;

tools/unit-tests/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
4949
unit-update-flash-enc unit-update-ram unit-pkcs11_store unit-psa_store unit-disk \
5050
unit-update-disk unit-multiboot unit-boot-x86-fsp unit-qspi-flash unit-tpm-rsa-exp \
5151
unit-image-nopart unit-image-sha384 unit-image-sha3-384 unit-store-sbrk \
52-
unit-tpm-blob unit-policy-sign
52+
unit-tpm-blob unit-policy-sign unit-sdhci-response-bits
5353

5454
all: $(TESTS)
5555

@@ -145,6 +145,10 @@ unit-store-sbrk: unit-store-sbrk.c ../../src/store_sbrk.c
145145
unit-string: ../../include/target.h unit-string.c
146146
gcc -o $@ $^ $(CFLAGS) -DDEBUG_UART -DPRINTF_ENABLED $(LDFLAGS)
147147

148+
unit-sdhci-response-bits: ../../include/target.h unit-sdhci-response-bits.c
149+
gcc -o $@ $^ $(CFLAGS) -ffunction-sections -fdata-sections $(LDFLAGS) \
150+
-Wl,--gc-sections
151+
148152
unit-aes128: ../../include/target.h unit-extflash.c
149153
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
150154

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* unit-sdhci-response-bits.c
2+
*
3+
* Unit tests for sdhci response bit extraction.
4+
*
5+
* Copyright (C) 2026 wolfSSL Inc.
6+
*
7+
* This file is part of wolfBoot.
8+
*
9+
* wolfBoot is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* wolfBoot is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program; if not, write to the Free Software
21+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
22+
*/
23+
24+
#define DISK_SDCARD 1
25+
26+
#include <check.h>
27+
#include <stdint.h>
28+
#include <string.h>
29+
30+
static uint32_t mock_regs[0x260 / sizeof(uint32_t)];
31+
32+
uint64_t hal_get_timer_us(void)
33+
{
34+
return 0;
35+
}
36+
37+
uint32_t sdhci_reg_read(uint32_t offset)
38+
{
39+
return mock_regs[offset / sizeof(uint32_t)];
40+
}
41+
42+
void sdhci_reg_write(uint32_t offset, uint32_t val)
43+
{
44+
mock_regs[offset / sizeof(uint32_t)] = val;
45+
}
46+
47+
void sdhci_platform_init(void)
48+
{
49+
}
50+
51+
void sdhci_platform_irq_init(void)
52+
{
53+
}
54+
55+
void sdhci_platform_set_bus_mode(int is_emmc)
56+
{
57+
(void)is_emmc;
58+
}
59+
60+
#include "../../src/sdhci.c"
61+
62+
static void set_response(uint32_t r0, uint32_t r1, uint32_t r2, uint32_t r3)
63+
{
64+
memset(mock_regs, 0, sizeof(mock_regs));
65+
mock_regs[SDHCI_SRS04 / sizeof(uint32_t)] = r0;
66+
mock_regs[SDHCI_SRS05 / sizeof(uint32_t)] = r1;
67+
mock_regs[SDHCI_SRS06 / sizeof(uint32_t)] = r2;
68+
mock_regs[SDHCI_SRS07 / sizeof(uint32_t)] = r3;
69+
}
70+
71+
START_TEST(test_csd_struct_does_not_cross_registers)
72+
{
73+
set_response(0, 0, 0, 2U << 22);
74+
75+
ck_assert_uint_eq(sdhci_get_response_bits(126, 2), 2);
76+
}
77+
END_TEST
78+
79+
Suite *sdhci_suite(void)
80+
{
81+
Suite *s = suite_create("sdhci");
82+
TCase *tc = tcase_create("response_bits");
83+
84+
tcase_add_test(tc, test_csd_struct_does_not_cross_registers);
85+
suite_add_tcase(s, tc);
86+
87+
return s;
88+
}
89+
90+
int main(void)
91+
{
92+
int fails;
93+
Suite *s = sdhci_suite();
94+
SRunner *sr = srunner_create(s);
95+
96+
srunner_run_all(sr, CK_NORMAL);
97+
fails = srunner_ntests_failed(sr);
98+
srunner_free(sr);
99+
100+
return fails;
101+
}

0 commit comments

Comments
 (0)