summaryrefslogtreecommitdiffstats
path: root/tests/chip.c
diff options
context:
space:
mode:
authorAnastasia Klimchuk <aklm@chromium.org>2021-09-07 15:32:01 +1000
committerNico Huber <nico.h@gmx.de>2021-10-01 11:35:03 +0000
commitcaf6e0dcb2cdac644a73affad9d6fbb4d9d4891e (patch)
tree97ffd38a6f803857d0fff17c8dc14fb55ac4294a /tests/chip.c
parent1f62b8346efdf3a62ab5781c835946bb9c990810 (diff)
downloadflashrom-caf6e0dcb2cdac644a73affad9d6fbb4d9d4891e.tar.gz
flashrom-caf6e0dcb2cdac644a73affad9d6fbb4d9d4891e.tar.bz2
flashrom-caf6e0dcb2cdac644a73affad9d6fbb4d9d4891e.zip
tests: Revise mock chip definition and usage
This patch is doing few things: 1) Makes chip definitions static global so that they can be reused between test functions. 2) Promotes existing mock chip from 8KiB to 8MiB and eraseblocks are expanded accordingly. Old value of 8KiB was very small and it was confusing. Mock chip looks more realistic now. 3) Uses KiB and MiB macros from flash.h for mock chip definition 4) Renames CHIP_TOTAL_SIZE to MOCK_CHIP_SIZE to avoid confusion (there is also a W25Q128.V chip in the tests) 5) Makes chip definitions const so that every test can work on a fresh copy on the stack. BUG=b:181803212 TEST=builds and ninja test Change-Id: Ia9b5fc71e30610684e68e9aca9fb1970da8f840a Signed-off-by: Anastasia Klimchuk <aklm@chromium.org> Reviewed-on: https://review.coreboot.org/c/flashrom/+/57437 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Nico Huber <nico.h@gmx.de>
Diffstat (limited to 'tests/chip.c')
-rw-r--r--tests/chip.c177
1 files changed, 60 insertions, 117 deletions
diff --git a/tests/chip.c b/tests/chip.c
index 2f6581fb0..54e14d22f 100644
--- a/tests/chip.c
+++ b/tests/chip.c
@@ -21,11 +21,11 @@
#include "flash.h"
#include "programmer.h"
-#define CHIP_TOTAL_SIZE 8192
+#define MOCK_CHIP_SIZE (8*MiB)
static struct {
unsigned int unlock_calls; /* how many times unlock function was called */
- uint8_t buf[CHIP_TOTAL_SIZE]; /* buffer of total size of chip, to emulate a chip */
+ uint8_t buf[MOCK_CHIP_SIZE]; /* buffer of total size of chip, to emulate a chip */
} g_chip_state = {
.unlock_calls = 0,
.buf = { 0 },
@@ -39,7 +39,7 @@ int read_chip(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned
return 1;
}
- assert_in_range(start + len, 0, CHIP_TOTAL_SIZE);
+ assert_in_range(start + len, 0, MOCK_CHIP_SIZE);
memcpy(buf, &g_chip_state.buf[start], len);
return 0;
@@ -53,7 +53,7 @@ int write_chip(struct flashctx *flash, const uint8_t *buf, unsigned int start, u
return 1;
}
- assert_in_range(start + len, 0, CHIP_TOTAL_SIZE);
+ assert_in_range(start + len, 0, MOCK_CHIP_SIZE);
memcpy(&g_chip_state.buf[start], buf, len);
return 0;
@@ -80,7 +80,7 @@ int block_erase_chip(struct flashctx *flash, unsigned int blockaddr, unsigned in
return 1;
}
- assert_in_range(blockaddr + blocklen, 0, CHIP_TOTAL_SIZE);
+ assert_in_range(blockaddr + blocklen, 0, MOCK_CHIP_SIZE);
memset(&g_chip_state.buf[blockaddr], 0xff, blocklen);
return 0;
@@ -96,7 +96,7 @@ static void setup_chip(struct flashrom_flashctx *flash, struct flashrom_layout *
printf("Creating layout with one included region... ");
assert_int_equal(0, flashrom_layout_new(layout));
/* One region which covers total size of chip. */
- assert_int_equal(0, flashrom_layout_add_region(*layout, 0, chip->total_size * 1024 - 1, "region"));
+ assert_int_equal(0, flashrom_layout_add_region(*layout, 0, chip->total_size * KiB - 1, "region"));
assert_int_equal(0, flashrom_layout_include_region(*layout, "region"));
flashrom_layout_set(flash, *layout);
@@ -125,33 +125,60 @@ static void teardown(struct flashrom_layout **layout)
printf("done\n");
}
+static const struct flashchip chip_8MiB = {
+ .vendor = "aklm",
+ .total_size = MOCK_CHIP_SIZE / KiB,
+ .tested = TEST_OK_PREW,
+ .read = read_chip,
+ .write = write_chip,
+ .unlock = unlock_chip,
+ .block_erasers =
+ {{
+ /* All blocks within total size of the chip. */
+ .eraseblocks = { {2 * MiB, 4} },
+ .block_erase = block_erase_chip,
+ }},
+};
+
+/* Setup the struct for W25Q128.V, all values come from flashchips.c */
+static const struct flashchip chip_W25Q128_V = {
+ .vendor = "aklm&dummyflasher",
+ .total_size = 16 * 1024,
+ .tested = TEST_OK_PREW,
+ .read = spi_chip_read,
+ .write = spi_chip_write_256,
+ .unlock = spi_disable_blockprotect,
+ .block_erasers =
+ {
+ {
+ .eraseblocks = { {4 * 1024, 4096} },
+ .block_erase = spi_block_erase_20,
+ }, {
+ .eraseblocks = { {32 * 1024, 512} },
+ .block_erase = spi_block_erase_52,
+ }, {
+ .eraseblocks = { {64 * 1024, 256} },
+ .block_erase = spi_block_erase_d8,
+ }, {
+ .eraseblocks = { {16 * 1024 * 1024, 1} },
+ .block_erase = spi_block_erase_60,
+ }, {
+ .eraseblocks = { {16 * 1024 * 1024, 1} },
+ .block_erase = spi_block_erase_c7,
+ }
+ },
+};
+
void erase_chip_test_success(void **state)
{
(void) state; /* unused */
- struct flashchip chip = {
- .vendor = "aklm",
- /*
- * Total size less than 16 to skip some steps
- * in flashrom.c#prepare_flash_access.
- */
- .total_size = 8,
- .tested = TEST_OK_PREW,
- .read = read_chip,
- .write = write_chip,
- .unlock = unlock_chip,
- .block_erasers =
- {{
- /* All blocks within total size of the chip. */
- .eraseblocks = { {2 * 1024, 4} },
- .block_erase = block_erase_chip,
- }},
- };
struct flashrom_flashctx flash = { 0 };
struct flashrom_layout *layout;
+ struct flashchip mock_chip = chip_8MiB;
const char *param = ""; /* Default values for all params. */
- setup_chip(&flash, &layout, &chip, param);
+ setup_chip(&flash, &layout, &mock_chip, param);
printf("Erase chip operation started.\n");
assert_int_equal(0, do_erase(&flash));
@@ -164,49 +191,16 @@ void erase_chip_with_dummyflasher_test_success(void **state)
{
(void) state; /* unused */
- struct flashchip chip = {
- .vendor = "aklm&dummyflasher",
- /*
- * Setup the values for W25Q128.V because we ask dummyflasher
- * to emulate this chip. All operations: read/write/unlock/erase
- * are real, not mocks, and they are expected to be handled by
- * dummyflasher.
- */
- .total_size = 16 * 1024,
- .tested = TEST_OK_PREW,
- .read = spi_chip_read,
- .write = spi_chip_write_256,
- .unlock = spi_disable_blockprotect,
- .block_erasers =
- {
- {
- .eraseblocks = { {4 * 1024, 4096} },
- .block_erase = spi_block_erase_20,
- }, {
- .eraseblocks = { {32 * 1024, 512} },
- .block_erase = spi_block_erase_52,
- }, {
- .eraseblocks = { {64 * 1024, 256} },
- .block_erase = spi_block_erase_d8,
- }, {
- .eraseblocks = { {16 * 1024 * 1024, 1} },
- .block_erase = spi_block_erase_60,
- }, {
- .eraseblocks = { {16 * 1024 * 1024, 1} },
- .block_erase = spi_block_erase_c7,
- }
- },
- };
-
struct flashrom_flashctx flash = { 0 };
struct flashrom_layout *layout;
+ struct flashchip mock_chip = chip_W25Q128_V;
/*
- * Dummyflasher is capable to emulate a chip, so we ask it to do this.
+ * Dummyflasher is capable to emulate W25Q128.V, so we ask it to do this.
* Nothing to mock, dummy is taking care of this already.
*/
char *param_dup = strdup("bus=spi,emulate=W25Q128FV");
- setup_chip(&flash, &layout, &chip, param_dup);
+ setup_chip(&flash, &layout, &mock_chip, param_dup);
printf("Erase chip operation started.\n");
assert_int_equal(0, do_erase(&flash));
@@ -221,30 +215,12 @@ void read_chip_test_success(void **state)
{
(void) state; /* unused */
- struct flashchip chip = {
- .vendor = "aklm",
- /*
- * Total size less than 16 to skip some steps
- * in flashrom.c#prepare_flash_access.
- */
- .total_size = 8,
- .tested = TEST_OK_PREW,
- .read = read_chip,
- .write = write_chip,
- .unlock = unlock_chip,
- .block_erasers =
- {{
- /* All blocks within total size of the chip. */
- .eraseblocks = { {2 * 1024, 4} },
- .block_erase = block_erase_chip,
- }},
- };
-
struct flashrom_flashctx flash = { 0 };
struct flashrom_layout *layout;
+ struct flashchip mock_chip = chip_8MiB;
const char *param = ""; /* Default values for all params. */
- setup_chip(&flash, &layout, &chip, param);
+ setup_chip(&flash, &layout, &mock_chip, param);
const char *const filename = "read_chip.test";
@@ -259,49 +235,16 @@ void read_chip_with_dummyflasher_test_success(void **state)
{
(void) state; /* unused */
- struct flashchip chip = {
- .vendor = "aklm&dummyflasher",
- /*
- * Setup the values for W25Q128.V because we ask dummyflasher
- * to emulate this chip. All operations: read/write/unlock/erase
- * are real, not mocks, and they are expected to be handled by
- * dummyflasher.
- */
- .total_size = 16 * 1024,
- .tested = TEST_OK_PREW,
- .read = spi_chip_read,
- .write = spi_chip_write_256,
- .unlock = spi_disable_blockprotect,
- .block_erasers =
- {
- {
- .eraseblocks = { {4 * 1024, 4096} },
- .block_erase = spi_block_erase_20,
- }, {
- .eraseblocks = { {32 * 1024, 512} },
- .block_erase = spi_block_erase_52,
- }, {
- .eraseblocks = { {64 * 1024, 256} },
- .block_erase = spi_block_erase_d8,
- }, {
- .eraseblocks = { {16 * 1024 * 1024, 1} },
- .block_erase = spi_block_erase_60,
- }, {
- .eraseblocks = { {16 * 1024 * 1024, 1} },
- .block_erase = spi_block_erase_c7,
- }
- },
- };
-
struct flashrom_flashctx flash = { 0 };
struct flashrom_layout *layout;
+ struct flashchip mock_chip = chip_W25Q128_V;
/*
- * Dummyflasher is capable to emulate a chip, so we ask it to do this.
+ * Dummyflasher is capable to emulate W25Q128.V, so we ask it to do this.
* Nothing to mock, dummy is taking care of this already.
*/
char *param_dup = strdup("bus=spi,emulate=W25Q128FV");
- setup_chip(&flash, &layout, &chip, param_dup);
+ setup_chip(&flash, &layout, &mock_chip, param_dup);
const char *const filename = "read_chip.test";