hw/sd/sdcard: Fix CVE-2020-13253 & cleanups
v5 cover hw/sd/sdcard: Fix CVE-2020-13253 & cleanups
Patches 5 & 6 fix CVE-2020-13253.
The rest are (accumulated) cleanups.
Missing review:
[PATCH 01/15] MAINTAINERS: Cc qemu-block mailing list [PATCH 03/15] hw/sd/sdcard: Move some definitions to use them [PATCH 04/15] hw/sd/sdcard: Use the HWBLOCK_SIZE definition [PATCH 05/15] hw/sd/sdcard: Do not switch to ReceivingData if [PATCH 07/15] hw/sd/sdcard: Initialize constant values first [PATCH 08/15] hw/sd/sdcard: Check address is in range [PATCH 12/15] hw/sd/sdcard: Make iolen unsigned
[PATCH 13/15] hw/sd/sdcard: Correctly display the command name in
$ git backport-diff -u v3 Key: [----] : patches are identical [####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively 001/15:[----] [--] 'MAINTAINERS: Cc qemu-block mailing list' 002/15:[----] [--] 'hw/sd/sdcard: Update coding style to make checkpatch.pl happy' 003/15:[down] 'hw/sd/sdcard: Move some definitions to use them earlier' 004/15:[down] 'hw/sd/sdcard: Use the HWBLOCK_SIZE definition' 005/15:[0030] [FC] 'hw/sd/sdcard: Do not switch to ReceivingData if address is invalid' 006/15:[----] [--] 'hw/sd/sdcard: Restrict Class 6 commands to SCSD cards' 007/15:[down] 'hw/sd/sdcard: Initialize constant values first' 008/15:[down] 'hw/sd/sdcard: Check address is in range' 009/15:[----] [--] 'hw/sd/sdcard: Update the SDState documentation' 010/15:[----] [--] 'hw/sd/sdcard: Simplify cmd_valid_while_locked()' 011/15:[----] [--] 'hw/sd/sdcard: Constify sd_crc*()'s message argument' 012/15:[0004] [FC] 'hw/sd/sdcard: Make iolen unsigned' 013/15:[----] [-C] 'hw/sd/sdcard: Correctly display the command name in trace events' 014/15:[0004] [FC] 'hw/sd/sdcard: Display offset in read/write_data() trace events' 015/15:[----] [--] 'hw/sd/sdcard: Simplify realize() a bit'
Philippe Mathieu-Daudé (15):
MAINTAINERS: Cc qemu-block mailing list
hw/sd/sdcard: Update coding style to make checkpatch.pl happy hw/sd/sdcard: Move some definitions to use them earlier hw/sd/sdcard: Use the HWBLOCK_SIZE definition hw/sd/sdcard: Do not switch to ReceivingData if address is invalid hw/sd/sdcard: Restrict Class 6 commands to SCSD cards hw/sd/sdcard: Initialize constant values first hw/sd/sdcard: Check address is in range
hw/sd/sdcard: Update the SDState documentation hw/sd/sdcard: Simplify cmd_valid_while_locked() hw/sd/sdcard: Constify sd_crc*()'s message argument hw/sd/sdcard: Make iolen unsigned
hw/sd/sdcard: Correctly display the command name in trace events hw/sd/sdcard: Display offset in read/write_data() trace events hw/sd/sdcard: Simplify realize() a bit
hw/sd/sd.c | 173 +++++++++++++++++++++++++++------------------ MAINTAINERS | 1 + hw/sd/trace-events | 4 +- 3 files changed, 109 insertions(+), 69 deletions(-)
Signature · 1 line
2.21.3
PATCH v5 1/15 MAINTAINERS: Cc qemu-block mailing list
From: Philippe Mathieu-Daudé <[email protected]>
We forgot to include the qemu-block mailing list while adding this section in commit 076a0fc32a7. Fix this.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -1622,6 +1622,7 @@ F: hw/ssi/xilinx_* SD (Secure Card) M: Philippe Mathieu-Daudé <[email protected]> +L: [email protected] S: Odd Fixes F: include/hw/sd/sd* F: hw/sd/core.c
Signature · 1 line
2.21.3
PATCH v5 2/15 hw/sd/sdcard: Update coding style to make checkpatch.pl happy
In reply to Philippe Mathieu-DaudéFrom: Philippe Mathieu-Daudé <[email protected]>
To make the next commit easier to review, clean this code first.
--- hw/sd/sd.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
hw/sd/sd.c +16 -8
@@ -1170,8 +1170,9 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) sd->data_start = addr; sd->data_offset =0 ; -if (sd->data_start + sd->blk_len > sd->size) +if (sd->data_start + sd->blk_len > sd->size) { sd->card_status |=ADDRESS_ERROR ; + }return sd_r1;default :
@@ -1186,8 +1187,9 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) sd->data_start = addr; sd->data_offset =0 ; -if (sd->data_start + sd->blk_len > sd->size) +if (sd->data_start + sd->blk_len > sd->size) { sd->card_status |=ADDRESS_ERROR ; + }return sd_r1;default :
@@ -1232,12 +1234,15 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) sd->data_offset =0 ; sd->blk_written =0 ; -if (sd->data_start + sd->blk_len > sd->size) +if (sd->data_start + sd->blk_len > sd->size) { sd->card_status |=ADDRESS_ERROR ; -if (sd_wp_addr (sd, sd->data_start)) + } +if (sd_wp_addr (sd, sd->data_start)) { sd->card_status |=WP_VIOLATION ; -if (sd->csd[14 ] &0x30 ) + } +if (sd->csd[14 ] &0x30 ) { sd->card_status |=WP_VIOLATION ; + }return sd_r1;default :
@@ -1256,12 +1261,15 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) sd->data_offset =0 ; sd->blk_written =0 ; -if (sd->data_start + sd->blk_len > sd->size) +if (sd->data_start + sd->blk_len > sd->size) { sd->card_status |=ADDRESS_ERROR ; -if (sd_wp_addr (sd, sd->data_start)) + } +if (sd_wp_addr (sd, sd->data_start)) { sd->card_status |=WP_VIOLATION ; -if (sd->csd[14 ] &0x30 ) + } +if (sd->csd[14 ] &0x30 ) { sd->card_status |=WP_VIOLATION ; + }return sd_r1;default :
Signature · 1 line
2.21.3
PATCH v5 3/15 hw/sd/sdcard: Move some definitions to use them earlier
In reply to Philippe Mathieu-DaudéMove some definitions to use them earlier.
--- hw/sd/sd.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
hw/sd/sd.c +6 -6
@@ -80,6 +80,12 @@ enum SDCardStates { sd_disconnect_state, }; +#define HWBLOCK_SHIFT 9/* 512 bytes */ +#define SECTOR_SHIFT 5/* 16 kilobytes */ +#define WPGROUP_SHIFT 7/* 2 megs */ +#define CMULT_SHIFT 9/* 512 times HWBLOCK_SIZE */ +#define WPGROUP_SIZE (1 << (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)) +struct SDState {DeviceState parent_obj;
@@ -367,12 +373,6 @@ static void sd_set_cid(SDState *sd) sd->cid[15 ] = (sd_crc7 (sd->cid,15 ) <<1 ) |1 ; } -#define HWBLOCK_SHIFT 9/* 512 bytes */ -#define SECTOR_SHIFT 5/* 16 kilobytes */ -#define WPGROUP_SHIFT 7/* 2 megs */ -#define CMULT_SHIFT 9/* 512 times HWBLOCK_SIZE */ -#define WPGROUP_SIZE (1 << (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)) -static const uint8_t sd_csd_rw_mask[16 ] = {0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0xfc ,0xfe ,
Signature · 1 line
2.21.3
PATCH v5 4/15 hw/sd/sdcard: Use the HWBLOCK_SIZE definition
In reply to Philippe Mathieu-DaudéReplace the following different uses of the same value by the same HWBLOCK_SIZE definition:
- 512 (magic value)
- 0x200 (magic value)
- 1 << HWBLOCK_SHIFT
--- hw/sd/sd.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
hw/sd/sd.c +9 -8
@@ -81,6 +81,7 @@ enum SDCardStates { };#define HWBLOCK_SHIFT 9/* 512 bytes */ +#define HWBLOCK_SIZE (1 << HWBLOCK_SHIFT)#define SECTOR_SHIFT 5/* 16 kilobytes */ #define WPGROUP_SHIFT 7/* 2 megs */ #define CMULT_SHIFT 9/* 512 times HWBLOCK_SIZE */
@@ -129,7 +130,7 @@ struct SDState {uint32_t blk_written;uint64_t data_start;uint32_t data_offset; -uint8_t data[512 ]; +uint8_t data[HWBLOCK_SIZE ];qemu_irq readonly_cb;qemu_irq inserted_cb;QEMUTimer *ocr_power_timer;
@@ -410,7 +411,7 @@ static void sd_set_csd(SDState *sd, uint64_t size) ((HWBLOCK_SHIFT <<6 ) &0xc0 ); sd->csd[14 ] =0x00 ;/* File format group */ }else {/* SDHC */ - size /=512 * KiB; + size /=HWBLOCK_SIZE * KiB; size -=1 ; sd->csd[0 ] =0x40 ; sd->csd[1 ] =0x0e ;
@@ -574,7 +575,7 @@ static void sd_reset(DeviceState *dev) sd->erase_start =0 ; sd->erase_end =0 ; sd->size = size; - sd->blk_len =0x200 ; + sd->blk_len =HWBLOCK_SIZE ; sd->pwd_len =0 ; sd->expecting_acmd = false; sd->dat_lines =0xf ;
@@ -685,7 +686,7 @@ static const VMStateDescription sd_vmstate = {VMSTATE_UINT32 (blk_written, SDState),VMSTATE_UINT64 (data_start, SDState),VMSTATE_UINT32 (data_offset, SDState), -VMSTATE_UINT8_ARRAY (data, SDState,512 ), +VMSTATE_UINT8_ARRAY (data, SDState,HWBLOCK_SIZE ),VMSTATE_UNUSED_V (1 ,512 ),VMSTATE_BOOL (enable, SDState),VMSTATE_END_OF_LIST ()
@@ -754,8 +755,8 @@ static void sd_erase(SDState *sd)if (FIELD_EX32 (sd->ocr,OCR ,CARD_CAPACITY )) {/* High capacity memory card: erase units are 512 byte blocks */ - erase_start *=512 ; - erase_end *=512 ; + erase_start *=HWBLOCK_SIZE ; + erase_end *=HWBLOCK_SIZE ; } erase_start =sd_addr_to_wpnum (erase_start);
@@ -1149,7 +1150,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)case 16 :/* CMD16: SET_BLOCKLEN */ switch (sd->state) {case sd_transfer_state: -if (req.arg > (1 <<HWBLOCK_SHIFT )) { +if (req.arg >HWBLOCK_SIZE ) { sd->card_status |=BLOCK_LEN_ERROR ; }else {trace_sdcard_set_blocklen (req.arg);
@@ -1961,7 +1962,7 @@ uint8_t sd_read_data(SDState *sd)if (sd->card_status & (ADDRESS_ERROR |WP_VIOLATION ))return 0x00 ; - io_len = (sd->ocr & (1 <<30 )) ?512 : sd->blk_len; + io_len = (sd->ocr & (1 <<30 )) ?HWBLOCK_SIZE : sd->blk_len; trace_sdcard_read_data(sd->proto_name, sd_acmd_name(sd->current_cmd),
Signature · 1 line
2.21.3
PATCH v5 5/15 hw/sd/sdcard: Do not switch to ReceivingData if address is invalid
In reply to Philippe Mathieu-DaudéOnly move the state machine to ReceivingData if there is no pending error. This avoids later OOB access while processing commands queued.
"SD Specifications Part 1 Physical Layer Simplified Spec. v3.01"
4.3.3 Data Read
Read command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR occurred and no data transfer is performed.
4.3.4 Data Write
Write command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR occurred and no data transfer is performed.
WP_VIOLATION errors are not modified: the error bit is set, we stay in receive-data state, wait for a stop command. All further data transfer is ignored. See the check on sd->card_status at the beginning of sd_read_data() and sd_write_data().
---
v4: Only modify ADDRESS_ERROR, not WP_VIOLATION (pm215)
--- hw/sd/sd.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-)
hw/sd/sd.c +22 -12
@@ -1167,13 +1167,15 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)case 17 :/* CMD17: READ_SINGLE_BLOCK */ switch (sd->state) {case sd_transfer_state: - sd->state = sd_sendingdata_state; - sd->data_start = addr; - sd->data_offset =0 ;if (sd->data_start + sd->blk_len > sd->size) { sd->card_status |=ADDRESS_ERROR ; +return sd_r1; } + + sd->state = sd_sendingdata_state; + sd->data_start = addr; + sd->data_offset =0 ;return sd_r1;default :
@@ -1184,13 +1186,15 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)case 18 :/* CMD18: READ_MULTIPLE_BLOCK */ switch (sd->state) {case sd_transfer_state: - sd->state = sd_sendingdata_state; - sd->data_start = addr; - sd->data_offset =0 ;if (sd->data_start + sd->blk_len > sd->size) { sd->card_status |=ADDRESS_ERROR ; +return sd_r1; } + + sd->state = sd_sendingdata_state; + sd->data_start = addr; + sd->data_offset =0 ;return sd_r1;default :
@@ -1230,14 +1234,17 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)/* Writing in SPI mode not implemented. */ if (sd->spi)break ; + +if (sd->data_start + sd->blk_len > sd->size) { + sd->card_status |=ADDRESS_ERROR ; +return sd_r1; + } + sd->state = sd_receivingdata_state; sd->data_start = addr; sd->data_offset =0 ; sd->blk_written =0 ; -if (sd->data_start + sd->blk_len > sd->size) { - sd->card_status |=ADDRESS_ERROR ; - }if (sd_wp_addr (sd, sd->data_start)) { sd->card_status |=WP_VIOLATION ; }
@@ -1257,14 +1264,17 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)/* Writing in SPI mode not implemented. */ if (sd->spi)break ; + +if (sd->data_start + sd->blk_len > sd->size) { + sd->card_status |=ADDRESS_ERROR ; +return sd_r1; + } + sd->state = sd_receivingdata_state; sd->data_start = addr; sd->data_offset =0 ; sd->blk_written =0 ; -if (sd->data_start + sd->blk_len > sd->size) { - sd->card_status |=ADDRESS_ERROR ; - }if (sd_wp_addr (sd, sd->data_start)) { sd->card_status |=WP_VIOLATION ; }
Signature · 1 line
2.21.3
PATCH v5 6/15 hw/sd/sdcard: Restrict Class 6 commands to SCSD cards
In reply to Philippe Mathieu-DaudéOnly SCSD cards support Class 6 (Block Oriented Write Protection) commands.
"SD Specifications Part 1 Physical Layer Simplified Spec. v3.01"
4.3.14 Command Functional Difference in Card Capacity Types
* Write Protected Group
SDHC and SDXC do not support write-protected groups. Issuing CMD28, CMD29 and CMD30 generates the ILLEGAL_COMMAND error.
--- hw/sd/sd.c | 5 +++++ 1 file changed, 5 insertions(+)
hw/sd/sd.c +5 -0
@@ -922,6 +922,11 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) sd->multi_blk_cnt =0 ; } +if (sd_cmd_class[req.cmd] ==6 &&FIELD_EX32 (sd->ocr,OCR ,CARD_CAPACITY )) { +/* Only Standard Capacity cards support class 6 commands */ +return sd_illegal; + } +switch (req.cmd) {/* Basic commands (Class 0 and Class 1) */ case 0 :/* CMD0: GO_IDLE_STATE */
Signature · 1 line
2.21.3
PATCH v5 7/15 hw/sd/sdcard: Initialize constant values first
In reply to Philippe Mathieu-DaudéReorder initialization code, constant values first.
--- hw/sd/sd.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)
hw/sd/sd.c +16 -16
@@ -556,22 +556,6 @@ static void sd_reset(DeviceState *dev) } size = sect <<9 ; - sect =sd_addr_to_wpnum (size) +1 ; - - sd->state = sd_idle_state; - sd->rca =0x0000 ; -sd_set_ocr (sd); -sd_set_scr (sd); -sd_set_cid (sd); -sd_set_csd (sd, size); -sd_set_cardstatus (sd); -sd_set_sdstatus (sd); - -g_free (sd->wp_groups); - sd->wp_switch = sd->blk ?blk_is_read_only (sd->blk) : false; - sd->wpgrps_size = sect; - sd->wp_groups =bitmap_new (sd->wpgrps_size); -memset (sd->function_group,0 ,sizeof (sd->function_group)); sd->erase_start =0 ; sd->erase_end =0 ; sd->size = size;
@@ -581,6 +565,22 @@ static void sd_reset(DeviceState *dev) sd->dat_lines =0xf ; sd->cmd_line = true; sd->multi_blk_cnt =0 ; + sd->state = sd_idle_state; + sd->rca =0x0000 ; + +sd_set_ocr (sd); +sd_set_scr (sd); +sd_set_cid (sd); +sd_set_csd (sd, size); +sd_set_cardstatus (sd); +sd_set_sdstatus (sd); + + sect =sd_addr_to_wpnum (size) +1 ; +g_free (sd->wp_groups); + sd->wp_switch = sd->blk ?blk_is_read_only (sd->blk) : false; + sd->wpgrps_size = sect; + sd->wp_groups =bitmap_new (sd->wpgrps_size); +memset (sd->function_group,0 ,sizeof (sd->function_group)); }static bool sd_get_inserted (SDState *sd)
Signature · 1 line
2.21.3
PATCH v5 8/15 hw/sd/sdcard: Check address is in range
In reply to Philippe Mathieu-DaudéAs a defense, assert if the requested address is out of the card area.
--- hw/sd/sd.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
hw/sd/sd.c +10 -8
@@ -537,8 +537,10 @@ static void sd_response_r7_make(SDState *sd, uint8_t *response)stl_be_p (response, sd->vhs); } -static inline uint64_t sd_addr_to_wpnum (uint64_t addr) +static uint64_t sd_addr_to_wpnum (SDState *sd,uint64_t addr) { +assert (addr < sd->size); +return addr >> (HWBLOCK_SHIFT +SECTOR_SHIFT +WPGROUP_SHIFT ); }
@@ -575,7 +577,7 @@ static void sd_reset(DeviceState *dev)sd_set_cardstatus (sd);sd_set_sdstatus (sd); - sect =sd_addr_to_wpnum (size) +1 ; + sect =sd_addr_to_wpnum (sd, size) +1 ;g_free (sd->wp_groups); sd->wp_switch = sd->blk ?blk_is_read_only (sd->blk) : false; sd->wpgrps_size = sect;
@@ -759,8 +761,8 @@ static void sd_erase(SDState *sd) erase_end *=HWBLOCK_SIZE ; } - erase_start =sd_addr_to_wpnum (erase_start); - erase_end =sd_addr_to_wpnum (erase_end); + erase_start =sd_addr_to_wpnum (sd, erase_start); + erase_end =sd_addr_to_wpnum (sd, erase_end); sd->erase_start =0 ; sd->erase_end =0 ; sd->csd[14 ] |=0x40 ;
@@ -777,7 +779,7 @@ static uint32_t sd_wpbits(SDState *sd, uint64_t addr)uint32_t i, wpnum;uint32_t ret =0 ; - wpnum =sd_addr_to_wpnum (addr); + wpnum =sd_addr_to_wpnum (sd, addr);for (i =0 ; i <32 ; i++, wpnum++, addr +=WPGROUP_SIZE ) {if (addr < sd->size &&test_bit (wpnum, sd->wp_groups)) {
@@ -819,7 +821,7 @@ static void sd_function_switch(SDState *sd, uint32_t arg)static inline bool sd_wp_addr (SDState *sd,uint64_t addr) { -return test_bit (sd_addr_to_wpnum (addr), sd->wp_groups); +return test_bit (sd_addr_to_wpnum (sd, addr), sd->wp_groups); }static void sd_lock_command (SDState *sd)
@@ -1331,7 +1333,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) } sd->state = sd_programming_state; -set_bit (sd_addr_to_wpnum (addr), sd->wp_groups); +set_bit (sd_addr_to_wpnum (sd, addr), sd->wp_groups);/* Bzzzzzzztt .... Operation complete. */ sd->state = sd_transfer_state;return sd_r1b;
@@ -1350,7 +1352,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) } sd->state = sd_programming_state; -clear_bit (sd_addr_to_wpnum (addr), sd->wp_groups); +clear_bit (sd_addr_to_wpnum (sd, addr), sd->wp_groups);/* Bzzzzzzztt .... Operation complete. */ sd->state = sd_transfer_state;return sd_r1b;
Signature · 1 line
2.21.3
Re: [PATCH v5 08/15] hw/sd/sdcard: Check address is in range
Quoted 13 lines · Philippe Mathieu-Daudé
As a defense, assert if the requested address is out of the card area.
Suggested-by Peter Maydell <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/sd/sd.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)hw/sd/sd.c +0
-0
hw/sd/sd.c static void sd_response_r7_make(SDState *sd, uint8_t *response) · line 542
Quoted 7 lines
hw/sd/sd.c +2
-1@@ -537,8 +537,10 @@ static void sd_response_r7_make(SDState *sd, uint8_t *response)stl_be_p (response, sd->vhs); } -static inline uint64_t sd_addr_to_wpnum (uint64_t addr) +static uint64_t sd_addr_to_wpnum (SDState *sd,uint64_t addr) { +assert (addr < sd->size);
This should be:
assert(addr <= sd->size);
Quoted 61 lines
hw/sd/sd.c +8
-7+return addr >> (HWBLOCK_SHIFT +SECTOR_SHIFT +WPGROUP_SHIFT ); }@@ -575,7 +577,7 @@ static void sd_reset(DeviceState *dev)sd_set_cardstatus (sd);sd_set_sdstatus (sd); - sect =sd_addr_to_wpnum (size) +1 ; + sect =sd_addr_to_wpnum (sd, size) +1 ;g_free (sd->wp_groups); sd->wp_switch = sd->blk ?blk_is_read_only (sd->blk) : false; sd->wpgrps_size = sect;@@ -759,8 +761,8 @@ static void sd_erase(SDState *sd) erase_end *=HWBLOCK_SIZE ; } - erase_start =sd_addr_to_wpnum (erase_start); - erase_end =sd_addr_to_wpnum (erase_end); + erase_start =sd_addr_to_wpnum (sd, erase_start); + erase_end =sd_addr_to_wpnum (sd, erase_end); sd->erase_start =0 ; sd->erase_end =0 ; sd->csd[14 ] |=0x40 ;@@ -777,7 +779,7 @@ static uint32_t sd_wpbits(SDState *sd, uint64_t addr)uint32_t i, wpnum;uint32_t ret =0 ; - wpnum =sd_addr_to_wpnum (addr); + wpnum =sd_addr_to_wpnum (sd, addr);for (i =0 ; i <32 ; i++, wpnum++, addr +=WPGROUP_SIZE ) {if (addr < sd->size &&test_bit (wpnum, sd->wp_groups)) {@@ -819,7 +821,7 @@ static void sd_function_switch(SDState *sd, uint32_t arg)static inline bool sd_wp_addr (SDState *sd,uint64_t addr) { -return test_bit (sd_addr_to_wpnum (addr), sd->wp_groups); +return test_bit (sd_addr_to_wpnum (sd, addr), sd->wp_groups); }static void sd_lock_command (SDState *sd)@@ -1331,7 +1333,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) } sd->state = sd_programming_state; -set_bit (sd_addr_to_wpnum (addr), sd->wp_groups); +set_bit (sd_addr_to_wpnum (sd, addr), sd->wp_groups);/* Bzzzzzzztt .... Operation complete. */ sd->state = sd_transfer_state;return sd_r1b;@@ -1350,7 +1352,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) } sd->state = sd_programming_state; -clear_bit (sd_addr_to_wpnum (addr), sd->wp_groups); +clear_bit (sd_addr_to_wpnum (sd, addr), sd->wp_groups);/* Bzzzzzzztt .... Operation complete. */ sd->state = sd_transfer_state;return sd_r1b;
Re: [PATCH v5 08/15] hw/sd/sdcard: Check address is in range
Quoted 17 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Philippe Mathieu-Daudé
As a defense, assert if the requested address is out of the card area.
Suggested-by Peter Maydell <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/sd/sd.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)hw/sd/sd.c +0
-0@@ -537,8 +537,10 @@ static void sd_response_r7_make(SDState *sd, uint8_t *response)stl_be_p (response, sd->vhs); }
Quoted 8 lines
hw/sd/sd.c static void sd_response_r7_make(SDState *sd, uint8_t *response) · line 542
Quoted 4 lines
hw/sd/sd.c +2
-1-static inline uint64_t sd_addr_to_wpnum (uint64_t addr) +static uint64_t sd_addr_to_wpnum (SDState *sd,uint64_t addr) { +assert (addr < sd->size);This should be:
assert(addr <= sd->size);
No, the current code is correct...
Quoted 11 lines
hw/sd/sd.c static void sd_reset(DeviceState *dev) · line 580
Quoted 11 lines
hw/sd/sd.c +2
-1+return addr >> (HWBLOCK_SHIFT +SECTOR_SHIFT +WPGROUP_SHIFT ); }@@ -575,7 +577,7 @@ static void sd_reset(DeviceState *dev)sd_set_cardstatus (sd);sd_set_sdstatus (sd); - sect =sd_addr_to_wpnum (size) +1 ; + sect =sd_addr_to_wpnum (sd, size) +1 ;
... but here this should be:
sect = sd_addr_to_wpnum(sd, size - 1) + 1;
Quoted 52 lines
Quoted 51 lines
hw/sd/sd.c +6
-6g_free (sd->wp_groups); sd->wp_switch = sd->blk ?blk_is_read_only (sd->blk) : false; sd->wpgrps_size = sect;@@ -759,8 +761,8 @@ static void sd_erase(SDState *sd) erase_end *=HWBLOCK_SIZE ; } - erase_start =sd_addr_to_wpnum (erase_start); - erase_end =sd_addr_to_wpnum (erase_end); + erase_start =sd_addr_to_wpnum (sd, erase_start); + erase_end =sd_addr_to_wpnum (sd, erase_end); sd->erase_start =0 ; sd->erase_end =0 ; sd->csd[14 ] |=0x40 ;@@ -777,7 +779,7 @@ static uint32_t sd_wpbits(SDState *sd, uint64_t addr)uint32_t i, wpnum;uint32_t ret =0 ; - wpnum =sd_addr_to_wpnum (addr); + wpnum =sd_addr_to_wpnum (sd, addr);for (i =0 ; i <32 ; i++, wpnum++, addr +=WPGROUP_SIZE ) {if (addr < sd->size &&test_bit (wpnum, sd->wp_groups)) {@@ -819,7 +821,7 @@ static void sd_function_switch(SDState *sd, uint32_t arg)static inline bool sd_wp_addr (SDState *sd,uint64_t addr) { -return test_bit (sd_addr_to_wpnum (addr), sd->wp_groups); +return test_bit (sd_addr_to_wpnum (sd, addr), sd->wp_groups); }static void sd_lock_command (SDState *sd)@@ -1331,7 +1333,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) } sd->state = sd_programming_state; -set_bit (sd_addr_to_wpnum (addr), sd->wp_groups); +set_bit (sd_addr_to_wpnum (sd, addr), sd->wp_groups);/* Bzzzzzzztt .... Operation complete. */ sd->state = sd_transfer_state;return sd_r1b;@@ -1350,7 +1352,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) } sd->state = sd_programming_state; -clear_bit (sd_addr_to_wpnum (addr), sd->wp_groups); +clear_bit (sd_addr_to_wpnum (sd, addr), sd->wp_groups);/* Bzzzzzzztt .... Operation complete. */ sd->state = sd_transfer_state;return sd_r1b;
PATCH v5 9/15 hw/sd/sdcard: Update the SDState documentation
In reply to Philippe Mathieu-DaudéAdd more descriptive comments to keep a clear separation between static property vs runtime changeable.
--- hw/sd/sd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
hw/sd/sd.c +4 -1
@@ -103,11 +103,14 @@ struct SDState {uint32_t card_status;uint8_t sd_status[64 ]; -/* Configurable properties */ +/* Static properties */ +uint8_t spec_version;BlockBackend *blk;bool spi; +/* Runtime changeables */ +uint32_t mode;/* current card mode, one of SDCardModes */ int32_t state;/* current card state, one of SDCardStates */ uint32_t vhs;
Signature · 1 line
2.21.3
PATCH v5 10/15 hw/sd/sdcard: Simplify cmd_valid_while_locked()
In reply to Philippe Mathieu-Daudécmd_valid_while_locked() only needs to read SDRequest->cmd, pass it directly and make it const.
--- hw/sd/sd.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
hw/sd/sd.c +5 -6
@@ -1664,7 +1664,7 @@ static sd_rsp_type_t sd_app_command(SDState *sd,return sd_illegal; } -static int cmd_valid_while_locked (SDState *sd,SDRequest *req) +static int cmd_valid_while_locked (SDState *sd,const uint8_t cmd) {/* Valid commands in locked state: * basic class (0)
@@ -1675,13 +1675,12 @@ static int cmd_valid_while_locked(SDState *sd, SDRequest *req)* Anything else provokes an "illegal command" response. */ if (sd->expecting_acmd) { -return req->cmd ==41 || req->cmd ==42 ; +return cmd ==41 || cmd ==42 ; } -if (req->cmd ==16 || req->cmd ==55 ) { +if (cmd ==16 || cmd ==55 ) {return 1 ; } -return sd_cmd_class[req->cmd] ==0 - || sd_cmd_class[req->cmd] ==7 ; +return sd_cmd_class[cmd] ==0 || sd_cmd_class[cmd] ==7 ; } int sd_do_command(SDState *sd, SDRequest *req,
@@ -1707,7 +1706,7 @@ int sd_do_command(SDState *sd, SDRequest *req, }if (sd->card_status &CARD_IS_LOCKED ) { -if (!cmd_valid_while_locked (sd, req)) { +if (!cmd_valid_while_locked (sd, req->cmd)) { sd->card_status |=ILLEGAL_COMMAND ; sd->expecting_acmd = false;qemu_log_mask (LOG_GUEST_ERROR ,"SD: Card is locked\n" );
Signature · 1 line
2.21.3
PATCH v5 11/15 hw/sd/sdcard: Constify sd_crc*()'s message argument
In reply to Philippe Mathieu-DaudéCRC functions don't modify the buffer argument,
make it const.
--- hw/sd/sd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
hw/sd/sd.c +4 -4
@@ -255,11 +255,11 @@ static const int sd_cmd_class[SDMMC_CMD_MAX] = { 7, 7, 10, 7, 9, 9, 9, 8, 8, 10, 8, 8, 8, 8, 8, 8, }; -static uint8_t sd_crc7 (void *message,size_t width) +static uint8_t sd_crc7 (const void *message,size_t width) {int i, bit;uint8_t shift_reg =0x00 ; -uint8_t *msg = (uint8_t *) message; +const uint8_t *msg = (const uint8_t *)message;for (i =0 ; i < width; i ++, msg ++)for (bit =7 ; bit >=0 ; bit --) {
@@ -271,11 +271,11 @@ static uint8_t sd_crc7(void *message, size_t width)return shift_reg; } -static uint16_t sd_crc16 (void *message,size_t width) +static uint16_t sd_crc16 (const void *message,size_t width) {int i, bit;uint16_t shift_reg =0x0000 ; -uint16_t *msg = (uint16_t *) message; +const uint16_t *msg = (const uint16_t *)message; width <<=1 ;for (i =0 ; i < width; i ++, msg ++)
Signature · 1 line
2.21.3
PATCH v5 12/15 hw/sd/sdcard: Make iolen unsigned
In reply to Philippe Mathieu-DaudéFrom: Philippe Mathieu-Daudé <[email protected]>
I/O request length can not be negative.
---
v4: Use uint32_t (pm215)
--- hw/sd/sd.c | 2 +- hw/sd/trace-events | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
hw/sd/sd.c +1 -1
@@ -1967,7 +1967,7 @@ uint8_t sd_read_data(SDState *sd) {/* TODO: Append CRCs */ uint8_t ret; -int io_len; +uint32_t io_len;if (!sd->blk || !blk_is_inserted (sd->blk) || !sd->enable)return 0x00 ;
hw/sd/trace-events +1 -1
@@ -52,7 +52,7 @@ sdcard_unlock(void) "" sdcard_read_block(uint64_t addr, uint32_t len) "addr 0x%" PRIx64 " size 0x%x" sdcard_write_block(uint64_t addr, uint32_t len) "addr 0x%" PRIx64 " size 0x%x" sdcard_write_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint8_t value) "%s %20s/ CMD%02d value 0x%02x" -sdcard_read_data(const char *proto, const char *cmd_desc, uint8_t cmd, int length) "%s %20s/ CMD%02d len %d" +sdcard_read_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint32_t length) "%s %20s/ CMD%02d len %" PRIu32 sdcard_set_voltage(uint16_t millivolts) "%u mV" # milkymist-memcard.c
Signature · 1 line
2.21.3
PATCH v5 13/15 hw/sd/sdcard: Correctly display the command name in trace events
In reply to Philippe Mathieu-DaudéSome ACMD were incorrectly displayed. Fix by remembering if we are processing a ACMD (with current_cmd_is_acmd) and add the sd_current_cmd_name() helper, which display to correct name regardless it is a CMD or ACMD.
--- hw/sd/sd.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
hw/sd/sd.c +14 -3
@@ -125,6 +125,7 @@ struct SDState {uint8_t pwd[16 ];uint32_t pwd_len;uint8_t function_group[6 ]; +bool current_cmd_is_acmd;uint8_t current_cmd;/* True if we will handle the next command as an ACMD. Note that this does * *not* track the APP_CMD status bit!
@@ -1704,6 +1705,8 @@ int sd_do_command(SDState *sd, SDRequest *req, req->cmd); req->cmd &=0x3f ; } + sd->current_cmd = req->cmd; + sd->current_cmd_is_acmd = sd->expecting_acmd;if (sd->card_status &CARD_IS_LOCKED ) {if (!cmd_valid_while_locked (sd, req->cmd)) {
@@ -1731,7 +1734,6 @@ int sd_do_command(SDState *sd, SDRequest *req,/* Valid command, we can update the 'state before command' bits. * (Do this now so they appear in r1 responses.) */ - sd->current_cmd = req->cmd; sd->card_status &= ~CURRENT_STATE ; sd->card_status |= (last_state <<9 ); }
@@ -1792,6 +1794,15 @@ send_response:return rsplen; } +static const char *sd_current_cmd_name (SDState *sd) +{ +if (sd->current_cmd_is_acmd) { +return sd_acmd_name (sd->current_cmd); + }else { +return sd_cmd_name (sd->current_cmd); + } +} +static void sd_blk_read (SDState *sd,uint64_t addr,uint32_t len) {trace_sdcard_read_block (addr, len);
@@ -1830,7 +1841,7 @@ void sd_write_data(SDState *sd, uint8_t value)return ;trace_sdcard_write_data (sd->proto_name, -sd_acmd_name (sd->current_cmd), +sd_current_cmd_name (sd), sd->current_cmd, value);switch (sd->current_cmd) {case 24 :/* CMD24: WRITE_SINGLE_BLOCK */
@@ -1984,7 +1995,7 @@ uint8_t sd_read_data(SDState *sd) io_len = (sd->ocr & (1 <<30 )) ?HWBLOCK_SIZE : sd->blk_len;trace_sdcard_read_data (sd->proto_name, -sd_acmd_name (sd->current_cmd), +sd_current_cmd_name (sd), sd->current_cmd, io_len);switch (sd->current_cmd) {case 6 :/* CMD6: SWITCH_FUNCTION */
Signature · 1 line
2.21.3
PATCH v5 14/15 hw/sd/sdcard: Display offset in read/write_data() trace events
In reply to Philippe Mathieu-DaudéHaving 'base address' and 'relative offset' displayed separately is more helpful than the absolute address.
--- hw/sd/sd.c | 8 ++++---- hw/sd/trace-events | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-)
hw/sd/sd.c +4 -4
@@ -1841,8 +1841,8 @@ void sd_write_data(SDState *sd, uint8_t value)return ;trace_sdcard_write_data (sd->proto_name, -sd_current_cmd_name (sd), - sd->current_cmd, value); +sd_current_cmd_name (sd), sd->current_cmd, + sd->data_start, sd->data_offset, value);switch (sd->current_cmd) {case 24 :/* CMD24: WRITE_SINGLE_BLOCK */ sd->data[sd->data_offset ++] = value;
@@ -1995,8 +1995,8 @@ uint8_t sd_read_data(SDState *sd) io_len = (sd->ocr & (1 <<30 )) ?HWBLOCK_SIZE : sd->blk_len;trace_sdcard_read_data (sd->proto_name, -sd_current_cmd_name (sd), - sd->current_cmd, io_len); +sd_current_cmd_name (sd), sd->current_cmd, + sd->data_start, sd->data_offset, io_len);switch (sd->current_cmd) {case 6 :/* CMD6: SWITCH_FUNCTION */ ret = sd->data[sd->data_offset ++];
hw/sd/trace-events +2 -2
@@ -51,8 +51,8 @@ sdcard_lock(void) "" sdcard_unlock(void) "" sdcard_read_block(uint64_t addr, uint32_t len) "addr 0x%" PRIx64 " size 0x%x" sdcard_write_block(uint64_t addr, uint32_t len) "addr 0x%" PRIx64 " size 0x%x" -sdcard_write_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint8_t value) "%s %20s/ CMD%02d value 0x%02x" -sdcard_read_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint32_t length) "%s %20s/ CMD%02d len %" PRIu32 +sdcard_write_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint64_t address, uint32_t offset, uint8_t value) "%s %20s/ CMD%02d addr 0x%" PRIx64 " ofs 0x%" PRIx32 " val 0x%02x" +sdcard_read_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint64_t address, uint32_t offset, uint32_t length) "%s %20s/ CMD%02d addr 0x%" PRIx64 " ofs 0x%" PRIx32 " len %" PRIu32 sdcard_set_voltage(uint16_t millivolts) "%u mV" # milkymist-memcard.c
Signature · 1 line
2.21.3
PATCH v5 15/15 hw/sd/sdcard: Simplify realize() a bit
In reply to Philippe Mathieu-DaudéWe don't need to check if sd->blk is set twice.
--- hw/sd/sd.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
hw/sd/sd.c +5 -5
@@ -2140,12 +2140,12 @@ static void sd_realize(DeviceState *dev, Error **errp)return ; } -if (sd->blk &&blk_is_read_only (sd->blk)) { -error_setg (errp,"Cannot use read-only drive as SD card" ); -return ; - } -if (sd->blk) { +if (blk_is_read_only (sd->blk)) { +error_setg (errp,"Cannot use read-only drive as SD card" ); +return ; + } + ret =blk_set_perm (sd->blk,BLK_PERM_CONSISTENT_READ |BLK_PERM_WRITE ,BLK_PERM_ALL , errp);if (ret <0 ) {
Signature · 1 line
2.21.3
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series failed the docker-quick@centos7 build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.
=== TEST SCRIPT BEGIN ===
#!/bin/bash
make docker-image-centos7 V=1 NETWORK=1
time make docker-test-quick@centos7 SHOW_ENV=1 J=14 NETWORK=1 === TEST SCRIPT END ===
qemu-system-aarch64: /tmp/qemu-test/src/hw/sd/sd.c:546: sd_addr_to_wpnum: Assertion `addr < sd->size' failed.
Broken pipe/tmp/qemu-test/src/tests/qtest/libqtest.c:175: kill_qemu() detected QEMU death from signal 6 (Aborted) (core dumped)
ERROR - too few tests run (expected 66, got 0)
make: *** [check-qtest-aarch64] Error 1
make: *** Waiting for unfinished jobs....
TEST iotest-qcow2: 080
TEST iotest-qcow2: 086
---
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['sudo', '-n', 'docker', 'run', '--label', 'com.qemu.instance.uuid=4348ef7075d64894b4a7294da7a0762c', '-u', '1001', '--security-opt', 'seccomp=unconfined', '--rm', '-e', 'TARGET_LIST=', '-e', 'EXTRA_CONFIGURE_OPTS=', '-e', 'V=', '-e', 'J=14', '-e', 'DEBUG=', '-e', 'SHOW_ENV=1', '-e', 'CCACHE_DIR=/var/tmp/ccache', '-v', '/home/patchew/.cache/qemu-docker-ccache:/var/tmp/ccache:z', '-v', '/var/tmp/patchew-tester-tmp-f5et805n/src/docker-src.2020-06-26-12.59.19.17334:/var/tmp/qemu:z,ro', 'qemu:centos7', '/var/tmp/qemu/run', 'test-quick']' returned non-zero exit status 2. filter=--filter=label=com.qemu.instance.uuid=4348ef7075d64894b4a7294da7a0762c
make[1]: *** [docker-run] Error 1
make[1]: Leaving directory `/var/tmp/patchew-tester-tmp-f5et805n/src'
make: *** [docker-run-test-quick@centos7] Error 2
real 14m46.134s
user 0m9.080s
The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-quick@centos7/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]