hw/sd/sdcard: Extract address_in_range() helper, log invalid accesses
Multiple commands have to check the address requested is valid. Extract this code pattern as a new address_in_range() helper, and log invalid accesses as guest errors. Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org> Reviewed-by: Bin Meng <bmeng.cn@gmail.com> Message-Id: <20210624142209.1193073-3-f4bug@amsat.org>
This commit is contained in:
parent
c60b292106
commit
66c152d7b4
33
hw/sd/sd.c
33
hw/sd/sd.c
@ -937,6 +937,19 @@ static void sd_lock_command(SDState *sd)
|
||||
sd->card_status &= ~CARD_IS_LOCKED;
|
||||
}
|
||||
|
||||
static bool address_in_range(SDState *sd, const char *desc,
|
||||
uint64_t addr, uint32_t length)
|
||||
{
|
||||
if (addr + length > sd->size) {
|
||||
qemu_log_mask(LOG_GUEST_ERROR,
|
||||
"%s offset %"PRIu64" > card %"PRIu64" [%%%u]\n",
|
||||
desc, addr, sd->size, length);
|
||||
sd->card_status |= ADDRESS_ERROR;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
|
||||
{
|
||||
uint32_t rca = 0x0000;
|
||||
@ -1218,8 +1231,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
|
||||
switch (sd->state) {
|
||||
case sd_transfer_state:
|
||||
|
||||
if (addr + sd->blk_len > sd->size) {
|
||||
sd->card_status |= ADDRESS_ERROR;
|
||||
if (!address_in_range(sd, "READ_BLOCK", addr, sd->blk_len)) {
|
||||
return sd_r1;
|
||||
}
|
||||
|
||||
@ -1264,8 +1276,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
|
||||
switch (sd->state) {
|
||||
case sd_transfer_state:
|
||||
|
||||
if (addr + sd->blk_len > sd->size) {
|
||||
sd->card_status |= ADDRESS_ERROR;
|
||||
if (!address_in_range(sd, "WRITE_BLOCK", addr, sd->blk_len)) {
|
||||
return sd_r1;
|
||||
}
|
||||
|
||||
@ -1325,8 +1336,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
|
||||
|
||||
switch (sd->state) {
|
||||
case sd_transfer_state:
|
||||
if (addr >= sd->size) {
|
||||
sd->card_status |= ADDRESS_ERROR;
|
||||
if (!address_in_range(sd, "SET_WRITE_PROT", addr, 1)) {
|
||||
return sd_r1b;
|
||||
}
|
||||
|
||||
@ -1348,8 +1358,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
|
||||
|
||||
switch (sd->state) {
|
||||
case sd_transfer_state:
|
||||
if (addr >= sd->size) {
|
||||
sd->card_status |= ADDRESS_ERROR;
|
||||
if (!address_in_range(sd, "CLR_WRITE_PROT", addr, 1)) {
|
||||
return sd_r1b;
|
||||
}
|
||||
|
||||
@ -1826,8 +1835,8 @@ void sd_write_byte(SDState *sd, uint8_t value)
|
||||
case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
|
||||
if (sd->data_offset == 0) {
|
||||
/* Start of the block - let's check the address is valid */
|
||||
if (sd->data_start + sd->blk_len > sd->size) {
|
||||
sd->card_status |= ADDRESS_ERROR;
|
||||
if (!address_in_range(sd, "WRITE_MULTIPLE_BLOCK",
|
||||
sd->data_start, sd->blk_len)) {
|
||||
break;
|
||||
}
|
||||
if (sd->size <= SDSC_MAX_CAPACITY) {
|
||||
@ -1999,8 +2008,8 @@ uint8_t sd_read_byte(SDState *sd)
|
||||
|
||||
case 18: /* CMD18: READ_MULTIPLE_BLOCK */
|
||||
if (sd->data_offset == 0) {
|
||||
if (sd->data_start + io_len > sd->size) {
|
||||
sd->card_status |= ADDRESS_ERROR;
|
||||
if (!address_in_range(sd, "READ_MULTIPLE_BLOCK",
|
||||
sd->data_start, io_len)) {
|
||||
return 0x00;
|
||||
}
|
||||
BLK_READ_BLOCK(sd->data_start, io_len);
|
||||
|
Loading…
Reference in New Issue
Block a user