Sergii Dmytruk has uploaded this change for review.

View Change

[RFC] tests: add test of write protection

Tests both WP implementation and its emulation in dummy programmer.

Change-Id: I49af7f6d173eb4c56c22d80b01a473b8c499c0f8
Signed-off-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
---
M tests/meson.build
M tests/tests.c
M tests/tests.h
A tests/write_protection.c
4 files changed, 254 insertions(+), 0 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/75/59075/1
diff --git a/tests/meson.build b/tests/meson.build
index 30b1e78..bb08f5a 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -23,6 +23,7 @@
'init_shutdown.c',
'layout.c',
'chip.c',
+ 'write_protection.c',
]

mocks = [
diff --git a/tests/tests.c b/tests/tests.c
index d3df356..457bf26 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -346,5 +346,13 @@
};
ret |= cmocka_run_group_tests_name("chip.c tests", chip_tests, NULL, NULL);

+ const struct CMUnitTest write_protection_tests[] = {
+ cmocka_unit_test(setup_wp_dummyflasher_test_failure),
+ cmocka_unit_test(setup_wp_dummyflasher_test_success),
+ cmocka_unit_test(full_chip_erase_with_wp_dummyflasher_test_failure),
+ cmocka_unit_test(partial_chip_erase_with_wp_dummyflasher_test_success),
+ };
+ ret |= cmocka_run_group_tests_name("write_protection.c tests", write_protection_tests, NULL, NULL);
+
return ret;
}
diff --git a/tests/tests.h b/tests/tests.h
index c142b45..8d08a09 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -62,4 +62,10 @@
void read_chip_test_success(void **state);
void read_chip_with_dummyflasher_test_success(void **state);

+/* write_protection.c */
+void setup_wp_dummyflasher_test_failure(void **state);
+void setup_wp_dummyflasher_test_success(void **state);
+void full_chip_erase_with_wp_dummyflasher_test_failure(void **state);
+void partial_chip_erase_with_wp_dummyflasher_test_success(void **state);
+
#endif /* TESTS_H */
diff --git a/tests/write_protection.c b/tests/write_protection.c
new file mode 100644
index 0000000..fca9e9f
--- /dev/null
+++ b/tests/write_protection.c
@@ -0,0 +1,239 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright 2021 3mdeb Embedded Systems Consulting
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <include/test.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "chipdrivers.h"
+#include "flash.h"
+#include "programmer.h"
+#include "writeprotect.h"
+
+/*
+ * No mocking. Using WP emulation in dummy programmer.
+ */
+
+static void setup_chip(struct flashrom_flashctx *flash, struct flashrom_layout **layout,
+ struct flashchip *chip, const char *programmer_param)
+{
+ flash->chip = chip;
+
+ printf("Creating layout with one included region... ");
+ assert_int_equal(0, flashrom_layout_new(layout));
+ assert_int_equal(0, flashrom_layout_add_region(*layout, 0, 0xfff, "head"));
+ assert_int_equal(0, flashrom_layout_add_region(*layout, 0x1000, chip->total_size * KiB - 1, "tail"));
+
+ flashrom_layout_set(flash, *layout);
+ printf("done\n");
+
+ printf("Dummyflasher initialising with param=\"%s\"... ", programmer_param);
+ assert_int_equal(0, programmer_init(&programmer_dummy, programmer_param));
+ /* Assignment below normally happens while probing, but this test is not probing. */
+ flash->mst = &registered_masters[0];
+ printf("done\n");
+}
+
+static void teardown(struct flashrom_layout **layout)
+{
+ printf("Dummyflasher shutdown... ");
+ assert_int_equal(0, programmer_shutdown());
+ printf("done\n");
+
+ printf("Releasing layout... ");
+ flashrom_layout_release(*layout);
+ printf("done\n");
+}
+
+/* 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,
+ .feature_bits = FEATURE_WRSR_WREN | FEATURE_OTP |
+ FEATURE_WRSR2 | FEATURE_RDSR2,
+ .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,
+ }
+ },
+ .reg_bits =
+ {
+ .srp = {{STATUS1, 7, RW}, {STATUS2, 0, RW}},
+ .bp = {{STATUS1, 2, RW}, {STATUS1, 3, RW}, {STATUS1, 4, RW}},
+ .tb = {STATUS1, 5, RW},
+ .sec = {STATUS1, 6, RW},
+ .cmp = {STATUS2, 6, RW},
+ },
+ .decode_range = decode_range_w25,
+};
+
+void setup_wp_dummyflasher_test_failure(void **state)
+{
+ (void) state; /* unused */
+
+ struct flashrom_flashctx flash = { 0 };
+ struct flashrom_layout *layout;
+ struct flashchip mock_chip = chip_W25Q128_V;
+
+ struct wp_range range = { .chip_len = mock_chip.total_size * 1024 };
+
+ char *param_dup = strdup("bus=spi,emulate=W25Q128FV,wp=no");
+
+ setup_chip(&flash, &layout, &mock_chip, param_dup);
+ assert_int_equal(0, flashrom_layout_include_region(layout, "head"));
+ assert_int_equal(0, flashrom_layout_include_region(layout, "tail"));
+
+ /* Invalid range. */
+ range.start = 0x1000;
+ range.len = 0x1000;
+ assert_int_equal(1, wp_set_range(&flash, range));
+
+ teardown(&layout);
+
+ free(param_dup);
+}
+
+void setup_wp_dummyflasher_test_success(void **state)
+{
+ (void) state; /* unused */
+
+ struct flashrom_flashctx flash = { 0 };
+ struct flashrom_layout *layout;
+ struct flashchip mock_chip = chip_W25Q128_V;
+
+ enum wp_mode mode;
+ struct wp_range range = { .chip_len = mock_chip.total_size * 1024 };
+
+ char *param_dup = strdup("bus=spi,emulate=W25Q128FV,wp=no");
+
+ setup_chip(&flash, &layout, &mock_chip, param_dup);
+ assert_int_equal(0, flashrom_layout_include_region(layout, "head"));
+ assert_int_equal(0, flashrom_layout_include_region(layout, "tail"));
+
+ assert_true(wp_supported(&flash));
+
+ /* Use first 4 KiB for a range. */
+ range.len = 0x1000;
+ assert_int_equal(0, wp_set_range(&flash, range));
+
+ /* Change range to last 4 KiB. */
+ range.start = 0x00fff000;
+ assert_int_equal(0, wp_set_range(&flash, range));
+
+ /* Check initial mode. */
+ assert_int_equal(0, wp_get_mode(&flash, &mode));
+ assert_int_equal(WP_MODE_DISABLED, mode);
+
+ /* Switch modes. */
+ assert_int_equal(0, wp_set_mode(&flash, WP_MODE_HARDWARE));
+ assert_int_equal(0, wp_set_mode(&flash, WP_MODE_DISABLED));
+ assert_int_equal(0, wp_set_mode(&flash, WP_MODE_POWER_CYCLE));
+ assert_int_equal(1, wp_set_mode(&flash, WP_MODE_PERMANENT));
+
+ /* Check final mode. */
+ assert_int_equal(0, wp_get_mode(&flash, &mode));
+ assert_int_equal(WP_MODE_POWER_CYCLE, mode);
+
+ teardown(&layout);
+
+ free(param_dup);
+}
+
+void full_chip_erase_with_wp_dummyflasher_test_failure(void **state)
+{
+ (void) state; /* unused */
+
+ struct flashrom_flashctx flash = { 0 };
+ struct flashrom_layout *layout;
+ struct flashchip mock_chip = chip_W25Q128_V;
+
+ char *param_dup = strdup("bus=spi,emulate=W25Q128FV,wp=yes");
+
+ setup_chip(&flash, &layout, &mock_chip, param_dup);
+ assert_int_equal(0, flashrom_layout_include_region(layout, "head"));
+ assert_int_equal(0, flashrom_layout_include_region(layout, "tail"));
+
+ /* Write protection takes effect only after changing SRP values. */
+ printf("Erase chip operation started.\n");
+ assert_int_equal(0, do_erase(&flash));
+ printf("Erase chip operation done.\n");
+
+ /* Protect first 4 KiB. */
+ struct wp_range range = {
+ .chip_len = mock_chip.total_size * 1024,
+ .start = 0,
+ .len = 0x1000,
+ };
+ assert_int_equal(0, wp_set_range(&flash, range));
+ assert_int_equal(0, wp_set_mode(&flash, WP_MODE_HARDWARE));
+
+ /* Try erasing the chip again. */
+ printf("Erase chip operation started.\n");
+ assert_int_equal(1, do_erase(&flash));
+ printf("Erase chip operation done.\n");
+
+ teardown(&layout);
+
+ free(param_dup);
+}
+
+void partial_chip_erase_with_wp_dummyflasher_test_success(void **state)
+{
+ (void) state; /* unused */
+
+ struct flashrom_flashctx flash = { 0 };
+ struct flashrom_layout *layout;
+ struct flashchip mock_chip = chip_W25Q128_V;
+
+ char *param_dup = strdup("bus=spi,emulate=W25Q128FV,wp=yes");
+
+ setup_chip(&flash, &layout, &mock_chip, param_dup);
+ assert_int_equal(0, flashrom_layout_include_region(layout, "head"));
+
+ /* Protect everything but first 4 KiB. */
+ struct wp_range range = {
+ .chip_len = mock_chip.total_size * 1024,
+ .start = 0x1000,
+ .len = mock_chip.total_size * 1024 - 0x1000,
+ };
+ assert_int_equal(0, wp_set_range(&flash, range));
+ assert_int_equal(0, wp_set_mode(&flash, WP_MODE_POWER_CYCLE));
+
+ printf("Erase chip operation started.\n");
+ assert_int_equal(0, do_erase(&flash));
+ printf("Erase chip operation done.\n");
+
+ teardown(&layout);
+
+ free(param_dup);
+}

To view, visit change 59075. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I49af7f6d173eb4c56c22d80b01a473b8c499c0f8
Gerrit-Change-Number: 59075
Gerrit-PatchSet: 1
Gerrit-Owner: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Gerrit-MessageType: newchange