Skip to content

Commit 66c4507

Browse files
duda-patrykCommit Bot
authored andcommitted
test: Introduce system_is_locked unit test
The test is intended to check system_is_locked() behaviour in the following conditions: - Hardware write protect is off - Hardware write protect is on but software write protect is off - Hardware write protect is on but software write protect is on BUG=b:217946520 BRANCH=none TEST=run_device_test.py --board dartmonkey \ --tests system_is_locked_wp_on TEST=run_device_test.py --board dartmonkey \ --tests system_is_locked_wp_off TEST=run_device_test.py --board bloonchipper \ --tests system_is_locked_wp_on TEST=run_device_test.py --board bloonchipper \ --tests system_is_locked_wp_off Signed-off-by: Patryk Duda <[email protected]> Change-Id: Ibf90829c4a36fb5e5c8e790c598c26297fbba5b0 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3528399 Reviewed-by: Andrea Grandi <[email protected]> Reviewed-by: Tom Hughes <[email protected]> Tested-by: Patryk Duda <[email protected]> Commit-Queue: Patryk Duda <[email protected]>
1 parent 4844236 commit 66c4507

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

board/hatch_fp/build.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ test-list-y=\
4949
sha256_unrolled \
5050
static_if \
5151
stm32f_rtc \
52+
system_is_locked \
5253
timer_dos \
5354
utils \
5455
utils_str \

board/nocturne_fp/build.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ test-list-y=\
4848
sha256 \
4949
sha256_unrolled \
5050
static_if \
51+
system_is_locked \
5152
timer_dos \
5253
utils \
5354
utils_str \

test/build.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ static_if-y=static_if.o
218218
stm32f_rtc-y=stm32f_rtc.o
219219
stress-y=stress.o
220220
system-y=system.o
221+
system_is_locked-y=system_is_locked.o
221222
thermal-y=thermal.o
222223
timer_calib-y=timer_calib.o
223224
timer_dos-y=timer_dos.o

test/run_device_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ def get(board_config: BoardConfig):
178178
TestConfig(name='sha256_unrolled'),
179179
'static_if':
180180
TestConfig(name='static_if'),
181+
'system_is_locked_wp_on':
182+
TestConfig(name='system_is_locked', test_args=['wp_on'],
183+
toggle_power=True, enable_hw_write_protect=True),
184+
'system_is_locked_wp_off':
185+
TestConfig(name='system_is_locked', test_args=['wp_off'],
186+
toggle_power=True, enable_hw_write_protect=False),
181187
'timer_dos':
182188
TestConfig(name='timer_dos'),
183189
'utils':

test/system_is_locked.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* Copyright 2022 The Chromium OS Authors. All rights reserved.
2+
* Use of this source code is governed by a BSD-style license that can be
3+
* found in the LICENSE file.
4+
*/
5+
6+
#include "test_util.h"
7+
8+
#include "flash.h"
9+
#include "string.h"
10+
#include "system.h"
11+
#include "task.h"
12+
#include "write_protect.h"
13+
14+
static bool write_protect_enabled;
15+
16+
test_static int test_write_protect(void)
17+
{
18+
TEST_EQ(write_protect_is_asserted(), write_protect_enabled, "%d");
19+
20+
return EC_SUCCESS;
21+
}
22+
23+
test_static int test_ro_protection_enabled(void)
24+
{
25+
TEST_BITS_SET(crec_flash_get_protect(), EC_FLASH_PROTECT_RO_NOW);
26+
27+
return EC_SUCCESS;
28+
}
29+
30+
test_static int test_system_is_locked(void)
31+
{
32+
if (!write_protect_is_asserted() ||
33+
(~crec_flash_get_protect() & EC_FLASH_PROTECT_RO_NOW))
34+
TEST_EQ(system_is_locked(), 0, "%d");
35+
else
36+
TEST_EQ(system_is_locked(), 1, "%d");
37+
38+
return EC_SUCCESS;
39+
}
40+
41+
static void print_usage(void)
42+
{
43+
ccprintf("usage: runtest [wp_on|wp_off]\n");
44+
}
45+
46+
void test_run_step(uint32_t state)
47+
{
48+
/*
49+
* Step 1: Check if reported write protect and system_is_locked()
50+
* output is correct. Since RO protection is not enabled at this point
51+
* we expect system_is_locked() to return 0. If write protect is
52+
* enabled then attempt to enable RO protection.
53+
*/
54+
if (state & TEST_STATE_MASK(TEST_STATE_STEP_1)) {
55+
RUN_TEST(test_write_protect);
56+
RUN_TEST(test_system_is_locked);
57+
58+
if (test_get_error_count())
59+
test_reboot_to_next_step(TEST_STATE_FAILED);
60+
else if (write_protect_enabled) {
61+
ccprintf("Request RO protection at boot\n");
62+
crec_flash_set_protect(EC_FLASH_PROTECT_RO_AT_BOOT,
63+
EC_FLASH_PROTECT_RO_AT_BOOT);
64+
test_reboot_to_next_step(TEST_STATE_STEP_2);
65+
} else {
66+
/* Write protect is disabled, nothing else to do */
67+
test_reboot_to_next_step(TEST_STATE_PASSED);
68+
}
69+
}
70+
/*
71+
* Step 2: Check if hardware write protect is enabled, RO protection
72+
* is enabled and system_is_locked() returns 1.
73+
*/
74+
else if (state & TEST_STATE_MASK(TEST_STATE_STEP_2)) {
75+
/* Expect hardware write protect to be enabled */
76+
write_protect_enabled = true;
77+
RUN_TEST(test_write_protect);
78+
RUN_TEST(test_ro_protection_enabled);
79+
RUN_TEST(test_system_is_locked);
80+
if (test_get_error_count())
81+
test_reboot_to_next_step(TEST_STATE_FAILED);
82+
else
83+
test_reboot_to_next_step(TEST_STATE_PASSED);
84+
}
85+
}
86+
87+
int task_test(void *unused)
88+
{
89+
test_run_multistep();
90+
return EC_SUCCESS;
91+
}
92+
93+
void run_test(int argc, char **argv)
94+
{
95+
test_reset();
96+
97+
if (IS_ENABLED(CONFIG_SYSTEM_UNLOCKED)) {
98+
ccprintf("Please disable CONFIG_SYSTEM_UNLOCKED before "
99+
"running this test\n");
100+
test_fail();
101+
return;
102+
}
103+
104+
if (argc < 2) {
105+
print_usage();
106+
test_fail();
107+
return;
108+
}
109+
110+
if (strncmp(argv[1], "wp_on", 5) == 0)
111+
write_protect_enabled = true;
112+
else if (strncmp(argv[1], "wp_off", 6) == 0) {
113+
write_protect_enabled = false;
114+
if (IS_ENABLED(CONFIG_WP_ALWAYS)) {
115+
ccprintf("Hardware write protect always enabled. "
116+
"Please disable CONFIG_WP_ALWAYS before "
117+
"running this test\n");
118+
test_fail();
119+
return;
120+
}
121+
} else {
122+
print_usage();
123+
test_fail();
124+
return;
125+
}
126+
127+
msleep(30); /* Wait for TASK_ID_TEST to initialize */
128+
task_wake(TASK_ID_TEST);
129+
}

test/system_is_locked.tasklist

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Copyright 2022 The Chromium OS Authors. All rights reserved.
2+
* Use of this source code is governed by a BSD-style license that can be
3+
* found in the LICENSE file.
4+
*/
5+
6+
/**
7+
* See CONFIG_TASK_LIST in config.h for details.
8+
*/
9+
#define CONFIG_TEST_TASK_LIST \
10+
TASK_TEST(TEST, task_test, NULL, TASK_STACK_SIZE)

0 commit comments

Comments
 (0)