diff options
author | Hsin-Te Yuan <yuanhsinte@google.com> | 2022-06-28 16:38:26 +0800 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-07-13 05:01:22 +0000 |
commit | 4756206b07f2250155c9c4c1d75577281ea111f5 (patch) | |
tree | 90a27dd88ac49fefe6ed2571b8eb0690ad6c12ef /tests/vb2_misc2_tests.c | |
parent | 35f4cb027230e03af30a6206cbea8e638d9dd0d0 (diff) | |
download | vboot-4756206b07f2250155c9c4c1d75577281ea111f5.tar.gz |
vboot_api_kernel.c: Remove kparams_ptr
Pass VbSelectAndLoadKernelParams kparams as a function argument instead
of using global variable kparams_ptr. Remove VbSelectAndLoadKernel and
replace its tests with the unit tests for vb2_set_boot_mode,
vb2api_kernel_phase2, vb2api_kernel_finalize, and vb2api_normal_boot.
BUG=b:172339016
BRANCH=none
TEST=make runtests
Cq-Depend: chromium:3731710
Signed-off-by: Hsin-Te Yuan <yuanhsinte@google.com>
Change-Id: I26895ced5e310b2894b9d42d0ad5514d3b0b930a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/3731412
Reviewed-by: Yu-Ping Wu <yupingso@chromium.org>
Diffstat (limited to 'tests/vb2_misc2_tests.c')
-rw-r--r-- | tests/vb2_misc2_tests.c | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/tests/vb2_misc2_tests.c b/tests/vb2_misc2_tests.c new file mode 100644 index 00000000..a4ed5789 --- /dev/null +++ b/tests/vb2_misc2_tests.c @@ -0,0 +1,128 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Tests for vb2_set_boot_mode. + */ + +#include "2api.h" +#include "2common.h" +#include "2misc.h" +#include "2nvstorage.h" +#include "test_common.h" + +/* Mock data */ + +static uint8_t workbuf[VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE] + __attribute__((aligned(VB2_WORKBUF_ALIGN))); +static struct vb2_context *ctx; +static struct vb2_shared_data *sd; +static struct vb2_gbb_header gbb; + +static int mock_diagnostic_ui_enabled; + +/* Mock functions */ + +struct vb2_gbb_header *vb2_get_gbb(struct vb2_context *c) +{ + return &gbb; +} + +int vb2api_diagnostic_ui_enabled(struct vb2_context *c) +{ + return mock_diagnostic_ui_enabled; +} + +static void reset_common_data(void) +{ + memset(workbuf, 0xaa, sizeof(workbuf)); + + memset(&gbb, 0, sizeof(gbb)); + + TEST_SUCC(vb2api_init(workbuf, sizeof(workbuf), &ctx), + "vb2api_init failed"); + sd = vb2_get_sd(ctx); + + vb2_nv_init(ctx); + + mock_diagnostic_ui_enabled = 0; +} + +static void set_boot_mode_tests(void) +{ + /* Normal boot */ + reset_common_data(); + vb2_set_boot_mode(ctx); + TEST_EQ(ctx->boot_mode, VB2_BOOT_MODE_NORMAL, "Normal boot"); + + /* Check that NV_DIAG_REQUEST triggers diagnostic mode */ + reset_common_data(); + mock_diagnostic_ui_enabled = 1; + vb2_nv_set(ctx, VB2_NV_DIAG_REQUEST, 1); + vb2_set_boot_mode(ctx); + TEST_EQ(ctx->boot_mode, VB2_BOOT_MODE_DIAGNOSTICS, + "Normal boot with diag UI enabled"); + + reset_common_data(); + vb2_nv_set(ctx, VB2_NV_DIAG_REQUEST, 1); + vb2_set_boot_mode(ctx); + TEST_EQ(ctx->boot_mode, VB2_BOOT_MODE_NORMAL, + "Normal boot with diag UI disabled"); + + /* Developer boot */ + reset_common_data(); + ctx->flags |= VB2_CONTEXT_DEVELOPER_MODE; + sd->flags |= VB2_SD_FLAG_DEV_MODE_ENABLED; + vb2_set_boot_mode(ctx); + TEST_EQ(ctx->boot_mode, VB2_BOOT_MODE_DEVELOPER, "Dev boot"); + + /* Recovery boot */ + reset_common_data(); + sd->recovery_reason = 123; + ctx->flags |= VB2_CONTEXT_RECOVERY_MODE; + vb2_set_boot_mode(ctx); + TEST_EQ(ctx->boot_mode, VB2_BOOT_MODE_BROKEN_SCREEN, "Broken screen"); + + reset_common_data(); + sd->recovery_reason = VB2_RECOVERY_RO_MANUAL; + ctx->flags |= VB2_CONTEXT_RECOVERY_MODE; + gbb.flags |= VB2_GBB_FLAG_FORCE_MANUAL_RECOVERY; + vb2_set_boot_mode(ctx); + TEST_EQ(ctx->boot_mode, VB2_BOOT_MODE_MANUAL_RECOVERY, + "Manual recovery: forced by GBB flags"); + + reset_common_data(); + sd->recovery_reason = VB2_RECOVERY_RO_MANUAL; + ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE; + ctx->flags |= VB2_CONTEXT_EC_TRUSTED; + vb2_set_boot_mode(ctx); + TEST_EQ(ctx->boot_mode, VB2_BOOT_MODE_MANUAL_RECOVERY, + "Manual recovery: physical rec switch"); + + reset_common_data(); + ctx->flags |= VB2_CONTEXT_EC_TRUSTED; + vb2_set_boot_mode(ctx); + TEST_NEQ(ctx->boot_mode, VB2_BOOT_MODE_MANUAL_RECOVERY, + "VB2_CONTEXT_FORCE_RECOVERY_MODE is not set"); + + reset_common_data(); + ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE; + ctx->flags |= VB2_CONTEXT_NO_BOOT; + ctx->flags |= VB2_CONTEXT_EC_TRUSTED; + vb2_set_boot_mode(ctx); + TEST_NEQ(ctx->boot_mode, VB2_BOOT_MODE_MANUAL_RECOVERY, + "Block manual recovery if NO_BOOT"); + + reset_common_data(); + ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE; + vb2_set_boot_mode(ctx); + TEST_NEQ(ctx->boot_mode, VB2_BOOT_MODE_MANUAL_RECOVERY, + "Block manual recovery for untrusted EC"); +} + +int main(void) +{ + set_boot_mode_tests(); + + return gTestSuccess ? 0 : 255; +} |