summaryrefslogtreecommitdiff
path: root/tests/vb2_misc2_tests.c
blob: 403e2f6fe58b755f53f83a714d2ea180471016ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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 "common/tests.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;
}