Signed-off-by: Philippe Mathieu-Daudé <f4...@amsat.org> --- include/hw/sd/sd.h | 10 ++++++++++ hw/sd/sdmmc-internal.c | 6 ++++++ tests/sdcard-test.c | 12 ++++++++++++ 3 files changed, 28 insertions(+)
diff --git a/include/hw/sd/sd.h b/include/hw/sd/sd.h index 752d8edd6c..83399cd42d 100644 --- a/include/hw/sd/sd.h +++ b/include/hw/sd/sd.h @@ -160,6 +160,16 @@ uint8_t sd_frame136_calc_checksum(const void *content); */ bool sd_frame48_verify_checksum(const void *content); +/** + * sd_frame136_verify_checksum: + * @content: pointer to the frame content + * + * Verify the 7-bit CRC checksum of a 136-bit SD frame. + * + * Returns: A boolean indicating whether the frame 7-bit CRC is correct. + */ +bool sd_frame136_verify_checksum(const void *content); + /* Legacy functions to be used only by non-qdevified callers */ SDState *sd_init(BlockBackend *bs, bool is_spi); int sd_do_command(SDState *sd, SDRequest *req, diff --git a/hw/sd/sdmmc-internal.c b/hw/sd/sdmmc-internal.c index a9d19ce3eb..f709211401 100644 --- a/hw/sd/sdmmc-internal.c +++ b/hw/sd/sdmmc-internal.c @@ -111,3 +111,9 @@ bool sd_frame48_verify_checksum(const void *content) return sd_frame48_calc_checksum(content) == ((const uint8_t *)content)[F48_CONTENT_LENGTH]; } + +bool sd_frame136_verify_checksum(const void *content) +{ + return sd_frame136_calc_checksum(content) + == ((const uint8_t *)content)[F136_CONTENT_LENGTH]; +} diff --git a/tests/sdcard-test.c b/tests/sdcard-test.c index c59af28382..81789d1f88 100644 --- a/tests/sdcard-test.c +++ b/tests/sdcard-test.c @@ -23,6 +23,17 @@ static void test_sd_response_frame136_crc7(void) sizeof(buf)); buf[15] = sd_frame136_calc_checksum(buf); g_assert_cmphex(buf[15], ==, 0xad); + + g_assert_true(sd_frame136_verify_checksum(buf)); +} + +static void test_sd_verify_cksum_frame136(void) +{ + uint8_t buf[16]; + + memset(buf, 69, sizeof(buf)); + buf[15] = sd_frame136_calc_checksum(buf); + g_assert_true(sd_frame136_verify_checksum(buf)); } int main(int argc, char *argv[]) @@ -30,6 +41,7 @@ int main(int argc, char *argv[]) g_test_init(&argc, &argv, NULL); qtest_add_func("sd/prepare_resp136_crc7", test_sd_response_frame136_crc7); + qtest_add_func("sd/verify_cksum_frame136", test_sd_verify_cksum_frame136); return g_test_run(); } -- 2.17.0