Nico Huber submitted this change.
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>
---
M tests/chip.c
1 file changed, 60 insertions(+), 117 deletions(-)
diff --git a/tests/chip.c b/tests/chip.c
index 2f6581f..54e14d2 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 @@
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 @@
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 @@
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 @@
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 @@
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) 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) 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) 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";
To view, visit change 57437. To unsubscribe, or for help writing mail filters, visit settings.