|
| 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 | +} |
0 commit comments