From: Philippe Mathieu-Daudé Date: Wed, 28 Jul 2021 17:38:05 +0000 (+0200) Subject: hw/sd/sdcard: Document out-of-range addresses for SEND_WRITE_PROT X-Git-Tag: v6.0.1~25 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=21611dd0a5634946367137a30f52a7677ff75928;p=thirdparty%2Fqemu.git hw/sd/sdcard: Document out-of-range addresses for SEND_WRITE_PROT Per the 'Physical Layer Simplified Specification Version 3.01', Table 4-22: 'Block Oriented Write Protection Commands' SEND_WRITE_PROT (CMD30) If the card provides write protection features, this command asks the card to send the status of the write protection bits [1]. [1] 32 write protection bits (representing 32 write protect groups starting at the specified address) [...] The last (least significant) bit of the protection bits corresponds to the first addressed group. If the addresses of the last groups are outside the valid range, then the corresponding write protection bits shall be set to 0. Split the if() statement (without changing the behaviour of the code) to better position the description comment. Reviewed-by: Alexander Bulekov Signed-off-by: Philippe Mathieu-Daudé Message-Id: <20210802235524.3417739-2-f4bug@amsat.org> Reviewed-by: Peter Maydell Tested-by: Alexander Bulekov (cherry picked from commit 2a0396285daa9483459ec1d3791951300b595e85) *context dependency for 4ac0b72bae ("hw/sd/sdcard: Fix assertion accessing out-of-range addresses with CMD30") Signed-off-by: Michael Roth --- diff --git a/hw/sd/sd.c b/hw/sd/sd.c index 282d39a7042..fa6bb79b150 100644 --- a/hw/sd/sd.c +++ b/hw/sd/sd.c @@ -822,7 +822,14 @@ static uint32_t sd_wpbits(SDState *sd, uint64_t addr) for (i = 0; i < 32; i++, wpnum++, addr += WPGROUP_SIZE) { assert(wpnum < sd->wpgrps_size); - if (addr < sd->size && test_bit(wpnum, sd->wp_groups)) { + if (addr >= sd->size) { + /* + * If the addresses of the last groups are outside the valid range, + * then the corresponding write protection bits shall be set to 0. + */ + continue; + } + if (test_bit(wpnum, sd->wp_groups)) { ret |= (1 << i); } }