Message ID | 20180504155918.21287-7-f4bug@amsat.org |
---|---|
State | New |
Headers | show |
Series | sdcard: proper implementation of CRC | expand |
On Fri, May 4, 2018 at 9:00 AM Philippe Mathieu-Daudé <f4bug@amsat.org> wrote: > Since not all modelled controllers use the CRC verification (which is > somehow expensive), let the controller have a configurable property > to enable verification. > So far only the Milkymist controller uses it. > This silent the Coverity warning: > "Code block is unreachable because of the syntactic structure of the code (CWE-561)" > and fixes the following issue: > - CID1005332 (hw/sd/sd.c::sd_req_crc_validate) Structurally dead code > Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Alistair > --- > hw/sd/milkymist-memcard.c | 1 + > hw/sd/sd.c | 12 ++++++++---- > 2 files changed, 9 insertions(+), 4 deletions(-) > diff --git a/hw/sd/milkymist-memcard.c b/hw/sd/milkymist-memcard.c > index d8cbb7b681..04babc092f 100644 > --- a/hw/sd/milkymist-memcard.c > +++ b/hw/sd/milkymist-memcard.c > @@ -278,6 +278,7 @@ static void milkymist_memcard_realize(DeviceState *dev, Error **errp) > blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL; > carddev = qdev_create(&s->sdbus.qbus, TYPE_SD_CARD); > qdev_prop_set_drive(carddev, "drive", blk, &err); > + object_property_set_bool(OBJECT(carddev), true, "validate-crc", &err); > object_property_set_bool(OBJECT(carddev), true, "realized", &err); > if (err) { > error_setg(errp, "failed to init SD card: %s", error_get_pretty(err)); > diff --git a/hw/sd/sd.c b/hw/sd/sd.c > index be75e118c0..801ddc2cb5 100644 > --- a/hw/sd/sd.c > +++ b/hw/sd/sd.c > @@ -93,6 +93,7 @@ struct SDState { > /* Configurable properties */ > BlockBackend *blk; > bool spi; > + bool validate_crc; > uint32_t mode; /* current card mode, one of SDCardModes */ > int32_t state; /* current card state, one of SDCardStates */ > @@ -496,10 +497,12 @@ static bool sd_verify_frame48_checksum(SDFrame48 *frame) > return crc == frame->crc; > } > -static int sd_req_crc_validate(SDRequest *req) > +static bool sd_req_crc_validate(SDState *sd, SDRequest *req) > { > - return 0; > - return !sd_verify_frame48_checksum(req); /* TODO */ > + if (sd->validate_crc) { > + return sd_verify_frame48_checksum(req); > + } > + return true; > } > static void sd_update_frame48_checksum(SDFrame48 *frame) > @@ -1685,7 +1688,7 @@ int sd_do_command(SDState *sd, SDRequest *req, > return 0; > } > - if (sd_req_crc_validate(req)) { > + if (!sd_req_crc_validate(sd, req)) { > sd->card_status |= COM_CRC_ERROR; > rtype = sd_illegal; > goto send_response; > @@ -2134,6 +2137,7 @@ static Property sd_properties[] = { > * board to ensure that ssi transfers only occur when the chip select > * is asserted. */ > DEFINE_PROP_BOOL("spi", SDState, spi, false), > + DEFINE_PROP_BOOL("validate-crc", SDState, validate_crc, false), > DEFINE_PROP_END_OF_LIST() > }; > -- > 2.17.0
diff --git a/hw/sd/milkymist-memcard.c b/hw/sd/milkymist-memcard.c index d8cbb7b681..04babc092f 100644 --- a/hw/sd/milkymist-memcard.c +++ b/hw/sd/milkymist-memcard.c @@ -278,6 +278,7 @@ static void milkymist_memcard_realize(DeviceState *dev, Error **errp) blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL; carddev = qdev_create(&s->sdbus.qbus, TYPE_SD_CARD); qdev_prop_set_drive(carddev, "drive", blk, &err); + object_property_set_bool(OBJECT(carddev), true, "validate-crc", &err); object_property_set_bool(OBJECT(carddev), true, "realized", &err); if (err) { error_setg(errp, "failed to init SD card: %s", error_get_pretty(err)); diff --git a/hw/sd/sd.c b/hw/sd/sd.c index be75e118c0..801ddc2cb5 100644 --- a/hw/sd/sd.c +++ b/hw/sd/sd.c @@ -93,6 +93,7 @@ struct SDState { /* Configurable properties */ BlockBackend *blk; bool spi; + bool validate_crc; uint32_t mode; /* current card mode, one of SDCardModes */ int32_t state; /* current card state, one of SDCardStates */ @@ -496,10 +497,12 @@ static bool sd_verify_frame48_checksum(SDFrame48 *frame) return crc == frame->crc; } -static int sd_req_crc_validate(SDRequest *req) +static bool sd_req_crc_validate(SDState *sd, SDRequest *req) { - return 0; - return !sd_verify_frame48_checksum(req); /* TODO */ + if (sd->validate_crc) { + return sd_verify_frame48_checksum(req); + } + return true; } static void sd_update_frame48_checksum(SDFrame48 *frame) @@ -1685,7 +1688,7 @@ int sd_do_command(SDState *sd, SDRequest *req, return 0; } - if (sd_req_crc_validate(req)) { + if (!sd_req_crc_validate(sd, req)) { sd->card_status |= COM_CRC_ERROR; rtype = sd_illegal; goto send_response; @@ -2134,6 +2137,7 @@ static Property sd_properties[] = { * board to ensure that ssi transfers only occur when the chip select * is asserted. */ DEFINE_PROP_BOOL("spi", SDState, spi, false), + DEFINE_PROP_BOOL("validate-crc", SDState, validate_crc, false), DEFINE_PROP_END_OF_LIST() };
Since not all modelled controllers use the CRC verification (which is somehow expensive), let the controller have a configurable property to enable verification. So far only the Milkymist controller uses it. This silent the Coverity warning: "Code block is unreachable because of the syntactic structure of the code (CWE-561)" and fixes the following issue: - CID1005332 (hw/sd/sd.c::sd_req_crc_validate) Structurally dead code Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org> --- hw/sd/milkymist-memcard.c | 1 + hw/sd/sd.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-)