From 7417a111b5fb4078ec17835960d4263813505bd5 Mon Sep 17 00:00:00 2001 From: Tristan Honscheid Date: Tue, 30 Aug 2022 19:35:20 -0600 Subject: zephyr: tests: Test common/flash.c crec_flash_is_erased() Test the crec_flash_is_erased() function. Add a union of zero-length arrays to `struct ec_params_flash_write` for convenience BRANCH=None BUG=b:236074365 TEST=./twister Signed-off-by: Tristan Honscheid Change-Id: Ica8fd3ce19fca3c87c0c0a2be8732dda6691cce4 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3865658 Reviewed-by: Simon Glass --- include/ec_commands.h | 9 ++++- zephyr/test/drivers/default/src/flash.c | 60 +++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/include/ec_commands.h b/include/ec_commands.h index 4f30a0f308..f971387e00 100644 --- a/include/ec_commands.h +++ b/include/ec_commands.h @@ -1744,7 +1744,14 @@ struct ec_params_flash_read { struct ec_params_flash_write { uint32_t offset; uint32_t size; - /* Followed by data to write */ + /* Followed by data to write. This union allows accessing an + * underlying buffer as uint32s or uint8s for convenience. This does not + * increase the size of the struct. + */ + union { + uint32_t words32[0]; + uint8_t bytes[0]; + } data; } __ec_align4; /* Erase flash */ diff --git a/zephyr/test/drivers/default/src/flash.c b/zephyr/test/drivers/default/src/flash.c index 17c4f65172..53c6fd1806 100644 --- a/zephyr/test/drivers/default/src/flash.c +++ b/zephyr/test/drivers/default/src/flash.c @@ -356,6 +356,66 @@ ZTEST_USER(flash, test_console_cmd_flashwp__bad_param) zassert_ok(!shell_execute_cmd(get_ec_shell(), "flashwp xyz"), NULL); } +/** + * @brief Prepare a region of flash for the test_crec_flash_is_erased* tests + * + * @param offset Offset to write bytes at. + * @param size Number of bytes to erase. + * @param make_write If true, write an arbitrary byte after erase so the region + * is no longer fully erased. + */ +static void setup_flash_region_helper(uint32_t offset, uint32_t size, + bool make_write) +{ + struct ec_params_flash_erase erase_params = { + .offset = offset, + .size = size, + }; + struct host_cmd_handler_args erase_args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_FLASH_ERASE, 0, erase_params); + + zassume_ok(host_command_process(&erase_args), NULL); + + if (make_write) { + /* Sized for flash_write header plus one byte of data */ + uint8_t out_buf[sizeof(struct ec_params_flash_write) + + sizeof(uint8_t)]; + + struct ec_params_flash_write *write_params = + (struct ec_params_flash_write *)out_buf; + struct host_cmd_handler_args write_args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_FLASH_WRITE, 0); + + write_params->offset = offset; + write_params->size = 1; + write_args.params = write_params; + write_args.params_size = sizeof(out_buf); + + /* Write one byte at start of region */ + out_buf[sizeof(*write_params)] = 0xec; + + zassume_ok(host_command_process(&write_args), NULL); + } +} + +ZTEST_USER(flash, test_crec_flash_is_erased__happy) +{ + uint32_t offset = 0x10000; + + setup_flash_region_helper(offset, TEST_BUF_SIZE, false); + + zassert_true(crec_flash_is_erased(offset, TEST_BUF_SIZE), NULL); +} + +ZTEST_USER(flash, test_crec_flash_is_erased__not_erased) +{ + uint32_t offset = 0x10000; + + setup_flash_region_helper(offset, TEST_BUF_SIZE, true); + + zassert_true(!crec_flash_is_erased(offset, TEST_BUF_SIZE), NULL); +} + static void flash_reset(void) { /* Set the GPIO WP_L to default */ -- cgit v1.2.1