diff options
author | edisonhello <edisonhello@google.com> | 2021-07-20 18:37:59 +0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-07-30 17:37:18 +0000 |
commit | 6cebe52a33eba8bb82430a7ce5560c590aee99a0 (patch) | |
tree | 7aa12fa2263b7b533490526f5518853b07a48ab3 /tests | |
parent | f975ab651e5364f2c59bca496779754b081e44ce (diff) | |
download | vboot-6cebe52a33eba8bb82430a7ce5560c590aee99a0.tar.gz |
vboot: Add DEV_BOOT_*ALLOWED in ctx->flags
Add DEV_BOOT_ALLOWED, DEV_BOOT_ALTFW_ALLOWED,
DEV_BOOT_EXTERNAL_ALLOWED in ctx->flags, which can be used by
external functions. These flags will be filled in
VbSelectAndLoadKernel, before calling any menu function.
In 2ui.c and 2ui_screen.c, all function calls to vb2_dev_boot_*allowed
are replaced with corresponding flags in ctx->flags.
Remove the parts of mocking these functions in unit tests, and set
ctx->flags instead in reset_common_data.
BUG=b:172339016
TEST=export CC=x86_64-pc-linux-gnu-clang DEBUG=1 DETACHABLE=0; \
make -j32 test_setup && make -j32 runtests;
TEST=export CC=x86_64-pc-linux-gnu-clang DEBUG=1 DETACHABLE=1; \
make -j32 test_setup && make -j32 runtests;
TEST=Hayato booted into developer mode
BRANCH=none
Signed-off-by: edisonhello <edisonhello@google.com>
Change-Id: Ida9f03557a92a1522d631fc04b281ce85f5049c7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/3041498
Reviewed-by: Yu-Ping Wu <yupingso@chromium.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/vb2_misc_tests.c | 116 | ||||
-rw-r--r-- | tests/vb2_ui_action_tests.c | 30 | ||||
-rw-r--r-- | tests/vb2_ui_tests.c | 45 |
3 files changed, 88 insertions, 103 deletions
diff --git a/tests/vb2_misc_tests.c b/tests/vb2_misc_tests.c index a7cc1155..11f4ea90 100644 --- a/tests/vb2_misc_tests.c +++ b/tests/vb2_misc_tests.c @@ -911,7 +911,7 @@ static void dev_default_boot_tests(void) /* Boot from external disk */ reset_common_data(); - vb2_nv_set(ctx, VB2_NV_DEV_BOOT_EXTERNAL, 1); + ctx->flags |= VB2_CONTEXT_DEV_BOOT_EXTERNAL_ALLOWED; vb2_nv_set(ctx, VB2_NV_DEV_DEFAULT_BOOT, VB2_DEV_DEFAULT_BOOT_TARGET_EXTERNAL); TEST_EQ(vb2api_get_dev_default_boot_target(ctx), @@ -926,7 +926,7 @@ static void dev_default_boot_tests(void) VB2_DEV_DEFAULT_BOOT_TARGET_INTERNAL, "default boot external not allowed"); reset_common_data(); - vb2_nv_set(ctx, VB2_NV_DEV_BOOT_ALTFW, 1); + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; vb2_nv_set(ctx, VB2_NV_DEV_DEFAULT_BOOT, VB2_DEV_DEFAULT_BOOT_TARGET_EXTERNAL); TEST_EQ(vb2api_get_dev_default_boot_target(ctx), @@ -935,7 +935,7 @@ static void dev_default_boot_tests(void) /* Boot altfw */ reset_common_data(); - vb2_nv_set(ctx, VB2_NV_DEV_BOOT_ALTFW, 1); + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; vb2_nv_set(ctx, VB2_NV_DEV_DEFAULT_BOOT, VB2_DEV_DEFAULT_BOOT_TARGET_ALTFW); TEST_EQ(vb2api_get_dev_default_boot_target(ctx), @@ -950,7 +950,7 @@ static void dev_default_boot_tests(void) VB2_DEV_DEFAULT_BOOT_TARGET_INTERNAL, "default boot altfw not allowed"); reset_common_data(); - vb2_nv_set(ctx, VB2_NV_DEV_BOOT_EXTERNAL, 1); + ctx->flags |= VB2_CONTEXT_DEV_BOOT_EXTERNAL_ALLOWED; vb2_nv_set(ctx, VB2_NV_DEV_DEFAULT_BOOT, VB2_DEV_DEFAULT_BOOT_TARGET_ALTFW); TEST_EQ(vb2api_get_dev_default_boot_target(ctx), @@ -958,84 +958,100 @@ static void dev_default_boot_tests(void) "default boot altfw not allowed"); } -static void dev_boot_allowed_tests(void) +static void fill_dev_boot_flags_tests(void) { /* Dev boot - allowed by default */ reset_common_data(); - TEST_EQ(vb2_dev_boot_allowed(ctx), 1, "dev boot - allowed by default"); + vb2_fill_dev_boot_flags(ctx); + TEST_TRUE(ctx->flags & VB2_CONTEXT_DEV_BOOT_ALLOWED, + "dev boot - allowed by default"); /* Dev boot - disabled by FWMP */ reset_common_data(); fwmp->flags |= VB2_SECDATA_FWMP_DEV_DISABLE_BOOT; - TEST_EQ(vb2_dev_boot_allowed(ctx), 0, "dev boot - FWMP disabled"); + vb2_fill_dev_boot_flags(ctx); + TEST_FALSE(ctx->flags & VB2_CONTEXT_DEV_BOOT_ALLOWED, + "dev boot - FWMP disabled"); /* Dev boot - force enabled by GBB */ reset_common_data(); fwmp->flags |= VB2_SECDATA_FWMP_DEV_DISABLE_BOOT; gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_SWITCH_ON; - TEST_EQ(vb2_dev_boot_allowed(ctx), 1, "dev boot - GBB force dev on"); - - /* Legacy boot - not allowed by default */ - reset_common_data(); - TEST_EQ(vb2_dev_boot_altfw_allowed(ctx), 0, - "dev boot altfw - not allowed by default"); - - /* Legacy boot - enabled by nvdata */ - reset_common_data(); - vb2_nv_set(ctx, VB2_NV_DEV_BOOT_ALTFW, 1); - TEST_EQ(vb2_dev_boot_altfw_allowed(ctx), 1, - "dev boot altfw - nvdata enabled"); - - /* Legacy boot - enabled by FWMP */ - reset_common_data(); - fwmp->flags |= VB2_SECDATA_FWMP_DEV_ENABLE_ALTFW; - TEST_EQ(vb2_dev_boot_altfw_allowed(ctx), 1, - "dev boot altfw - secdata enabled"); - - /* Legacy boot - force enabled by GBB */ - reset_common_data(); - gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_BOOT_ALTFW; - TEST_EQ(vb2_dev_boot_altfw_allowed(ctx), 1, - "dev boot altfw - GBB force enabled"); - - /* Legacy boot - set all flags */ - reset_common_data(); - vb2_nv_set(ctx, VB2_NV_DEV_BOOT_ALTFW, 1); - fwmp->flags |= VB2_SECDATA_FWMP_DEV_ENABLE_ALTFW; - gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_BOOT_ALTFW; - TEST_EQ(vb2_dev_boot_altfw_allowed(ctx), 1, - "dev boot altfw - all flags set"); + vb2_fill_dev_boot_flags(ctx); + TEST_TRUE(ctx->flags & VB2_CONTEXT_DEV_BOOT_ALLOWED, + "dev boot - GBB force dev on"); /* External boot - not allowed by default */ reset_common_data(); - TEST_EQ(vb2_dev_boot_external_allowed(ctx), 0, - "dev boot external - not allowed by default"); + vb2_fill_dev_boot_flags(ctx); + TEST_FALSE(ctx->flags & VB2_CONTEXT_DEV_BOOT_EXTERNAL_ALLOWED, + "dev boot external - not allowed by default"); /* External boot - enabled by nvdata */ reset_common_data(); vb2_nv_set(ctx, VB2_NV_DEV_BOOT_EXTERNAL, 1); - TEST_EQ(vb2_dev_boot_external_allowed(ctx), 1, - "dev boot external - nvdata enabled"); + vb2_fill_dev_boot_flags(ctx); + TEST_TRUE(ctx->flags & VB2_CONTEXT_DEV_BOOT_EXTERNAL_ALLOWED, + "dev boot external - nvdata enabled"); /* External boot - enabled by FWMP */ reset_common_data(); fwmp->flags |= VB2_SECDATA_FWMP_DEV_ENABLE_EXTERNAL; - TEST_EQ(vb2_dev_boot_external_allowed(ctx), 1, - "dev boot external - secdata enabled"); + vb2_fill_dev_boot_flags(ctx); + TEST_TRUE(ctx->flags & VB2_CONTEXT_DEV_BOOT_EXTERNAL_ALLOWED, + "dev boot external - secdata enabled"); /* External boot - force enabled by GBB */ reset_common_data(); gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_BOOT_USB; - TEST_EQ(vb2_dev_boot_external_allowed(ctx), 1, - "dev boot external - GBB force enabled"); + vb2_fill_dev_boot_flags(ctx); + TEST_TRUE(ctx->flags & VB2_CONTEXT_DEV_BOOT_EXTERNAL_ALLOWED, + "dev boot external - GBB force enabled"); /* External boot - set all flags */ reset_common_data(); vb2_nv_set(ctx, VB2_NV_DEV_BOOT_EXTERNAL, 1); fwmp->flags |= VB2_SECDATA_FWMP_DEV_ENABLE_EXTERNAL; gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_BOOT_USB; - TEST_EQ(vb2_dev_boot_external_allowed(ctx), 1, - "dev boot external - all flags set"); + vb2_fill_dev_boot_flags(ctx); + TEST_TRUE(ctx->flags & VB2_CONTEXT_DEV_BOOT_EXTERNAL_ALLOWED, + "dev boot external - all flags set"); + + /* Alternate boot - not allowed by default */ + reset_common_data(); + vb2_fill_dev_boot_flags(ctx); + TEST_FALSE(ctx->flags & VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED, + "dev boot altfw - not allowed by default"); + + /* Alternate boot - enabled by nvdata */ + reset_common_data(); + vb2_nv_set(ctx, VB2_NV_DEV_BOOT_ALTFW, 1); + vb2_fill_dev_boot_flags(ctx); + TEST_TRUE(ctx->flags & VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED, + "dev boot altfw - nvdata enabled"); + + /* Alternate boot - enabled by FWMP */ + reset_common_data(); + fwmp->flags |= VB2_SECDATA_FWMP_DEV_ENABLE_ALTFW; + vb2_fill_dev_boot_flags(ctx); + TEST_TRUE(ctx->flags & VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED, + "dev boot altfw - secdata enabled"); + + /* Alternate boot - force enabled by GBB */ + reset_common_data(); + gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_BOOT_ALTFW; + vb2_fill_dev_boot_flags(ctx); + TEST_TRUE(ctx->flags & VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED, + "dev boot altfw - GBB force enabled"); + + /* Alternate boot - set all flags */ + reset_common_data(); + vb2_nv_set(ctx, VB2_NV_DEV_BOOT_ALTFW, 1); + fwmp->flags |= VB2_SECDATA_FWMP_DEV_ENABLE_ALTFW; + gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_BOOT_ALTFW; + vb2_fill_dev_boot_flags(ctx); + TEST_TRUE(ctx->flags & VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED, + "dev boot altfw - all flags set"); } static void use_dev_screen_short_delay_tests(void) @@ -1068,7 +1084,7 @@ int main(int argc, char* argv[]) phone_recovery_enabled_tests(); diagnostic_ui_enabled_tests(); dev_default_boot_tests(); - dev_boot_allowed_tests(); + fill_dev_boot_flags_tests(); use_dev_screen_short_delay_tests(); return gTestSuccess ? 0 : 255; diff --git a/tests/vb2_ui_action_tests.c b/tests/vb2_ui_action_tests.c index 6db75bfd..44649ac6 100644 --- a/tests/vb2_ui_action_tests.c +++ b/tests/vb2_ui_action_tests.c @@ -65,9 +65,6 @@ static int mock_get_screen_info_called; static vb2_error_t mock_vbtlk_retval; static uint32_t mock_vbtlk_expected_flag; -static int mock_dev_boot_allowed; -static int mock_dev_boot_altfw_allowed; - static int mock_run_altfw_called; static uint32_t mock_altfw_last; static uint32_t mock_altfw_count; @@ -317,6 +314,9 @@ static void reset_common_data(void) sd = vb2_get_sd(ctx); + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALLOWED; + ctx->flags &= ~(uint64_t)VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; + /* For check_shutdown_request */ mock_calls_until_shutdown = 10; @@ -359,10 +359,6 @@ static void reset_common_data(void) mock_vbtlk_retval = VB2_ERROR_MOCK; mock_vbtlk_expected_flag = MOCK_IGNORE; - /* For dev_boot* in 2misc.h */ - mock_dev_boot_allowed = 1; - mock_dev_boot_altfw_allowed = 0; - /* For vb2ex_run_altfw */ mock_run_altfw_called = 0; mock_altfw_last = -100; @@ -488,16 +484,6 @@ vb2_error_t VbTryLoadKernel(struct vb2_context *c, uint32_t disk_flags) return mock_vbtlk_retval; } -int vb2_dev_boot_allowed(struct vb2_context *c) -{ - return mock_dev_boot_allowed; -} - -int vb2_dev_boot_altfw_allowed(struct vb2_context *c) -{ - return mock_dev_boot_altfw_allowed; -} - vb2_error_t vb2ex_run_altfw(uint32_t altfw_id) { mock_run_altfw_called++; @@ -755,7 +741,7 @@ static void vb2_ui_developer_mode_boot_altfw_action_tests(void) /* Not allowed: not in dev mode */ reset_common_data(); - mock_dev_boot_altfw_allowed = 1; + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; TEST_EQ(vb2_ui_developer_mode_boot_altfw_action(&mock_ui_context), VB2_REQUEST_UI_CONTINUE, "not allowed: not in dev mode"); TEST_EQ(mock_ui_context.error_code, VB2_UI_ERROR_ALTFW_DISABLED, @@ -765,8 +751,8 @@ static void vb2_ui_developer_mode_boot_altfw_action_tests(void) /* Not allowed: dev boot not allowed */ reset_common_data(); ctx->flags |= VB2_CONTEXT_DEVELOPER_MODE; - mock_dev_boot_allowed = 0; - mock_dev_boot_altfw_allowed = 1; + ctx->flags &= ~(uint64_t)VB2_CONTEXT_DEV_BOOT_ALLOWED; + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; TEST_EQ(vb2_ui_developer_mode_boot_altfw_action(&mock_ui_context), VB2_REQUEST_UI_CONTINUE, "not allowed: dev boot not allowed"); TEST_EQ(mock_ui_context.error_code, VB2_UI_ERROR_ALTFW_DISABLED, @@ -785,7 +771,7 @@ static void vb2_ui_developer_mode_boot_altfw_action_tests(void) /* Allowed */ reset_common_data(); ctx->flags |= VB2_CONTEXT_DEVELOPER_MODE; - mock_dev_boot_altfw_allowed = 1; + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; mock_ui_context.state->selected_item = 2; TEST_EQ(vb2_ui_developer_mode_boot_altfw_action(&mock_ui_context), VB2_REQUEST_UI_CONTINUE, "allowed"); @@ -797,7 +783,7 @@ static void vb2_ui_developer_mode_boot_altfw_action_tests(void) /* CTRL+L = default bootloader */ reset_common_data(); ctx->flags |= VB2_CONTEXT_DEVELOPER_MODE; - mock_dev_boot_altfw_allowed = 1; + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; mock_ui_context.key = VB_KEY_CTRL('L'); mock_ui_context.state->selected_item = 4; /* Ignored */ TEST_EQ(vb2_ui_developer_mode_boot_altfw_action(&mock_ui_context), diff --git a/tests/vb2_ui_tests.c b/tests/vb2_ui_tests.c index 657ae5f8..51a8853d 100644 --- a/tests/vb2_ui_tests.c +++ b/tests/vb2_ui_tests.c @@ -73,9 +73,6 @@ static int mock_beep_count; static int mock_beep_total; static enum vb2_dev_default_boot_target mock_default_boot; -static int mock_dev_boot_allowed; -static int mock_dev_boot_altfw_allowed; -static int mock_dev_boot_external_allowed; static int mock_run_altfw_called; static uint32_t mock_altfw_last; @@ -281,6 +278,10 @@ static void reset_common_data(enum reset_type t) sd->flags |= VB2_SD_FLAG_DEV_MODE_ENABLED; } + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALLOWED; + ctx->flags &= ~(uint64_t)VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; + ctx->flags |= VB2_CONTEXT_DEV_BOOT_EXTERNAL_ALLOWED; + /* Mock ui_context based on real screens */ memset(&mock_ui_context, 0, sizeof(mock_ui_context)); mock_ui_context.ctx = ctx; @@ -318,9 +319,6 @@ static void reset_common_data(enum reset_type t) /* For dev_boot* in 2misc.h */ mock_default_boot = VB2_DEV_DEFAULT_BOOT_TARGET_INTERNAL; - mock_dev_boot_allowed = 1; - mock_dev_boot_altfw_allowed = 0; - mock_dev_boot_external_allowed = 1; /* For vb2ex_run_altfw */ mock_run_altfw_called = 0; @@ -489,21 +487,6 @@ enum vb2_dev_default_boot_target vb2api_get_dev_default_boot_target( return mock_default_boot; } -int vb2_dev_boot_allowed(struct vb2_context *c) -{ - return mock_dev_boot_allowed; -} - -int vb2_dev_boot_altfw_allowed(struct vb2_context *c) -{ - return mock_dev_boot_altfw_allowed; -} - -int vb2_dev_boot_external_allowed(struct vb2_context *c) -{ - return mock_dev_boot_external_allowed; -} - vb2_error_t vb2ex_run_altfw(uint32_t altfw_id) { mock_run_altfw_called++; @@ -610,7 +593,7 @@ static void developer_tests(void) /* Don't proceed to internal disk after timeout (dev mode disallowed) */ reset_common_data(FOR_DEVELOPER); - mock_dev_boot_allowed = 0; + ctx->flags &= ~(uint64_t)VB2_CONTEXT_DEV_BOOT_ALLOWED; TEST_EQ(ui_loop(ctx, VB2_SCREEN_DEVELOPER_MODE, NULL), VB2_REQUEST_SHUTDOWN, "do not proceed to internal disk after timeout " @@ -701,13 +684,13 @@ static void developer_tests(void) /* Default boot from external not allowed, don't boot */ reset_common_data(FOR_DEVELOPER); mock_default_boot = VB2_DEV_DEFAULT_BOOT_TARGET_EXTERNAL; - mock_dev_boot_external_allowed = 0; + ctx->flags &= ~(uint64_t)VB2_CONTEXT_DEV_BOOT_EXTERNAL_ALLOWED; TEST_EQ(vb2_developer_menu(ctx), VB2_REQUEST_SHUTDOWN, "default boot from external disk not allowed, don't boot"); /* Don't proceed to external disk after timeout (dev mode disallowed) */ reset_common_data(FOR_DEVELOPER); - mock_dev_boot_allowed = 0; + ctx->flags &= ~(uint64_t)VB2_CONTEXT_DEV_BOOT_ALLOWED; mock_default_boot = VB2_DEV_DEFAULT_BOOT_TARGET_EXTERNAL; TEST_EQ(ui_loop(ctx, VB2_SCREEN_DEVELOPER_MODE, NULL), VB2_REQUEST_SHUTDOWN, @@ -731,7 +714,7 @@ static void developer_tests(void) /* Ctrl+L = boot altfw (allowed) */ reset_common_data(FOR_DEVELOPER); - mock_dev_boot_altfw_allowed = 1; + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; add_mock_keypress(VB_KEY_CTRL('L')); TEST_EQ(vb2_developer_menu(ctx), VB2_REQUEST_SHUTDOWN, "ctrl+l = boot altfw"); @@ -766,7 +749,7 @@ static void developer_tests(void) /* Select to_norm in dev menu and confirm (dev mode disallowed) */ reset_common_data(FOR_DEVELOPER); - mock_dev_boot_allowed = 0; + ctx->flags &= ~(uint64_t)VB2_CONTEXT_DEV_BOOT_ALLOWED; add_mock_keypress(VB_KEY_UP); add_mock_keypress(VB_KEY_ENTER); add_mock_keypress(VB_KEY_ENTER); @@ -1307,7 +1290,7 @@ static void developer_screen_tests(void) /* Dev mode: disabled and hidden item mask */ reset_common_data(FOR_DEVELOPER); - mock_dev_boot_altfw_allowed = 1; + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; add_mock_vbtlk(VB2_SUCCESS, VB_DISK_FLAG_FIXED); TEST_EQ(vb2_developer_menu(ctx), VB2_SUCCESS, "dev mode screen: no disabled or hidden item"); @@ -1315,7 +1298,7 @@ static void developer_screen_tests(void) MOCK_IGNORE, MOCK_IGNORE, 0x0, 0x0, MOCK_IGNORE); reset_common_data(FOR_DEVELOPER); - mock_dev_boot_altfw_allowed = 1; + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_SWITCH_ON; add_mock_vbtlk(VB2_SUCCESS, VB_DISK_FLAG_FIXED); TEST_EQ(vb2_developer_menu(ctx), VB2_SUCCESS, @@ -1325,8 +1308,8 @@ static void developer_screen_tests(void) reset_common_data(FOR_DEVELOPER); add_mock_vbtlk(VB2_SUCCESS, VB_DISK_FLAG_FIXED); - mock_dev_boot_external_allowed = 0; - mock_dev_boot_altfw_allowed = 1; + ctx->flags &= ~(uint64_t)VB2_CONTEXT_DEV_BOOT_EXTERNAL_ALLOWED; + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; TEST_EQ(vb2_developer_menu(ctx), VB2_SUCCESS, "dev mode screen: hide boot external"); DISPLAYED_EQ("dev mode screen", VB2_SCREEN_DEVELOPER_MODE, @@ -1385,7 +1368,7 @@ static void developer_screen_tests(void) DISPLAYED_NO_EXTRA(); reset_common_data(FOR_DEVELOPER); /* Select #2 by default */ - mock_dev_boot_altfw_allowed = 1; + ctx->flags |= VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED; /* #4: Alternate boot */ add_mock_keypress(VB_KEY_DOWN); add_mock_keypress(VB_KEY_DOWN); |