diff options
author | Lee Leahy <leroy.p.leahy@intel.com> | 2017-05-09 16:37:58 -0700 |
---|---|---|
committer | Lee Leahy <leroy.p.leahy@intel.com> | 2017-05-10 22:31:36 +0200 |
commit | bf5d5093fcc37298b8359237195dc92c978a5536 (patch) | |
tree | 5981b49dcd85cbe6bd51f05addaca6ba461d53e5 /src | |
parent | 72f730e23c6682e2db16cf64a5be432f2a58a88b (diff) | |
download | coreboot-bf5d5093fcc37298b8359237195dc92c978a5536.tar.xz |
drivers/storage: Fix array references
Fix bug detected by coverity to handle the zero capacity case. Specific
changes:
* Reduce loop count by one to handle zero capacity case
* Use structure instead of dual arrays
* Move structures into display_capacity routine
Coverity Issues:
* 1374931
* 1374932
* 1374933
* 1374934
TEST=Build and run on Galileo Gen2
Change-Id: Ie5c96e78417b667438a00ee22c70894a00d13291
Signed-off-by: Lee Leahy <Leroy.P.Leahy@intel.com>
Reviewed-on: https://review.coreboot.org/19643
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/drivers/storage/storage.c | 77 |
1 files changed, 31 insertions, 46 deletions
diff --git a/src/drivers/storage/storage.c b/src/drivers/storage/storage.c index 57477f9a03..8d7692c69b 100644 --- a/src/drivers/storage/storage.c +++ b/src/drivers/storage/storage.c @@ -30,41 +30,9 @@ #define DECIMAL_CAPACITY_MULTIPLIER 1000ULL #define HEX_CAPACITY_MULTIPLIER 1024ULL -static const char * const hex_unit_name[] = { - "TiB", "GiB", "MiB", "KiB", "B" -}; -static const char * const decimal_unit_name[] = { - "TB", "GB", "MB", "KB", "B" -}; - -static const uint64_t decimal_capacity_table[] = { - /* TB */ - DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER - * DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER, - /* GB */ - DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER - * DECIMAL_CAPACITY_MULTIPLIER, - /* MB */ - DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER, - /* KB */ - DECIMAL_CAPACITY_MULTIPLIER, - /* B */ - 1 -}; - -static const uint64_t hex_capacity_table[] = { - /* TiB */ - HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER - * HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER, - /* GiB */ - HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER - * HEX_CAPACITY_MULTIPLIER, - /* MiB */ - HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER, - /* KiB */ - HEX_CAPACITY_MULTIPLIER, - /* B */ - 1 +struct capacity { + const char * const units; + uint64_t bytes; }; static void display_capacity(struct storage_media *media, int partition_number) @@ -77,6 +45,26 @@ static void display_capacity(struct storage_media *media, int partition_number) int index; const char *name; const char *separator; + const struct capacity decimal_list[] = { + {"TB", DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER + * DECIMAL_CAPACITY_MULTIPLIER + * DECIMAL_CAPACITY_MULTIPLIER}, + {"GB", DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER + * DECIMAL_CAPACITY_MULTIPLIER}, + {"MB", DECIMAL_CAPACITY_MULTIPLIER + * DECIMAL_CAPACITY_MULTIPLIER}, + {"KB", DECIMAL_CAPACITY_MULTIPLIER}, + {"B", 1} + }; + const struct capacity hex_list[] = { + {"TiB", HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER + * HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER}, + {"GiB", HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER + * HEX_CAPACITY_MULTIPLIER}, + {"MiB", HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER}, + {"KiB", HEX_CAPACITY_MULTIPLIER}, + {"B", 1} + }; /* Get the partition name */ capacity = media->capacity[partition_number]; @@ -86,23 +74,20 @@ static void display_capacity(struct storage_media *media, int partition_number) separator = ": "; /* Determine the decimal divisor for the capacity */ - ASSERT(ARRAY_SIZE(decimal_capacity_table) - == ARRAY_SIZE(decimal_unit_name)); - for (index = 0; index < ARRAY_SIZE(decimal_capacity_table); index++) { - if (capacity >= decimal_capacity_table[index]) + for (index = 0; index < ARRAY_SIZE(decimal_list) - 1; index++) { + if (capacity >= decimal_list[index].bytes) break; } - decimal_divisor = decimal_capacity_table[index]; - decimal_units = decimal_unit_name[index]; + decimal_divisor = decimal_list[index].bytes; + decimal_units = decimal_list[index].units; /* Determine the hex divisor for the capacity */ - ASSERT(ARRAY_SIZE(hex_capacity_table) == ARRAY_SIZE(hex_unit_name)); - for (index = 0; index < ARRAY_SIZE(hex_capacity_table); index++) { - if (capacity >= hex_capacity_table[index]) + for (index = 0; index < ARRAY_SIZE(hex_list) - 1; index++) { + if (capacity >= hex_list[index].bytes) break; } - hex_divisor = hex_capacity_table[index]; - hex_units = hex_unit_name[index]; + hex_divisor = hex_list[index].bytes; + hex_units = hex_list[index].units; /* Display the capacity */ sdhc_debug("%3lld.%03lld %sytes (%3lld.%03lld %sytes)%s%s\n", |