vb2_nvstorage_tests.c revision 3333e578497aafc4eb8c6e1e359f6e2b1dee633a
1/* Copyright (c) 2014 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 * Tests for firmware NV storage library.
6 */
7
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#include "test_common.h"
14#include "vboot_common.h"
15
16#include "2api.h"
17#include "2common.h"
18#include "2misc.h"
19#include "2nvstorage.h"
20
21/* Single NV storage field to test */
22struct nv_field {
23	enum vb2_nv_param param;  /* Parameter index */
24	uint32_t default_value;   /* Expected default value */
25	uint32_t test_value;      /* Value to test writing */
26	uint32_t test_value2;     /* Second value to test writing */
27	char *desc;               /* Field description */
28};
29
30/* Array of fields to test, terminated with a field with desc==NULL. */
31static struct nv_field nvfields[] = {
32	{VB2_NV_DEBUG_RESET_MODE, 0, 1, 0, "debug reset mode"},
33	{VB2_NV_TRY_NEXT, 0, 1, 0, "try next"},
34	{VB2_NV_TRY_COUNT, 0, 6, 15, "try B count"},
35	{VB2_NV_FW_TRIED, 0, 1, 0, "firmware tried"},
36	{VB2_NV_FW_RESULT, 0, 1, 2, "firmware result"},
37	{VB2_NV_RECOVERY_REQUEST, 0, 0x42, 0xED, "recovery request"},
38	{VB2_NV_RECOVERY_SUBCODE, 0, 0x56, 0xAC, "recovery subcode"},
39	{VB2_NV_LOCALIZATION_INDEX, 0, 0x69, 0xB0, "localization index"},
40	{VB2_NV_KERNEL_FIELD, 0, 0x12345678, 0xFEDCBA98, "kernel field"},
41	{VB2_NV_DEV_BOOT_USB, 0, 1, 0, "dev boot usb"},
42	{VB2_NV_DEV_BOOT_LEGACY, 0, 1, 0, "dev boot legacy"},
43	{VB2_NV_DEV_BOOT_SIGNED_ONLY, 0, 1, 0, "dev boot custom"},
44	{VB2_NV_DISABLE_DEV_REQUEST, 0, 1, 0, "disable dev request"},
45	{VB2_NV_CLEAR_TPM_OWNER_REQUEST, 0, 1, 0, "clear tpm owner request"},
46	{VB2_NV_CLEAR_TPM_OWNER_DONE, 0, 1, 0, "clear tpm owner done"},
47	{VB2_NV_OPROM_NEEDED, 0, 1, 0, "oprom needed"},
48	{0, 0, 0, 0, NULL}
49};
50
51static void test_changed(struct vb2_context *ctx, int changed, const char *why)
52{
53	if (changed)
54		TEST_NEQ(ctx->flags & VB2_CONTEXT_NVDATA_CHANGED, 0, why);
55	else
56		TEST_EQ(ctx->flags & VB2_CONTEXT_NVDATA_CHANGED, 0, why);
57};
58
59static void nv_storage_test(void)
60{
61	struct nv_field *vnf;
62	uint8_t goodcrc;
63	uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE];
64	struct vb2_context c = {
65		.flags = 0,
66		.workbuf = workbuf,
67		.workbuf_size = sizeof(workbuf),
68	};
69	struct vb2_shared_data *sd = vb2_get_sd(&c);
70
71	memset(c.nvdata, 0xA6, sizeof(c.nvdata));
72	vb2_init_context(&c);
73
74	/* Init with invalid data should set defaults and regenerate CRC */
75	vb2_nv_init(&c);
76	TEST_EQ(c.nvdata[0], 0x70, "vb2_nv_init() reset header byte");
77	TEST_NEQ(c.nvdata[15], 0, "vb2_nv_init() CRC");
78	TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT | VB2_SD_STATUS_NV_REINIT,
79		"vb2_nv_init() status changed");
80	test_changed(&c, 1, "vb2_nv_init() reset changed");
81	goodcrc = c.nvdata[15];
82
83	/* Another init should not cause further changes */
84	c.flags = 0;
85	sd->status = 0;
86	vb2_nv_init(&c);
87	test_changed(&c, 0, "vb2_nv_init() didn't re-reset");
88	TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same");
89	TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT, "vb2_nv_init() status same");
90
91	/* Perturbing the header should force defaults */
92	c.nvdata[0] ^= 0x40;
93	vb2_nv_init(&c);
94	TEST_EQ(c.nvdata[0], 0x70, "vb2_nv_init() reset header byte again");
95	test_changed(&c, 1, "vb2_nv_init() corrupt changed");
96	TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same again");
97
98	/* So should perturbing some other byte */
99	TEST_EQ(c.nvdata[11], 0, "Kernel byte starts at 0");
100	c.nvdata[11] = 12;
101	vb2_nv_init(&c);
102	TEST_EQ(c.nvdata[11], 0, "vb2_nv_init() reset kernel byte");
103	test_changed(&c, 1, "vb2_nv_init() corrupt elsewhere changed");
104	TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same again");
105
106	/* Clear the kernel and firmware flags */
107	vb2_nv_init(&c);
108	TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
109		1, "Firmware settings are reset");
110	vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 0);
111	TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
112		0, "Firmware settings are clear");
113
114	TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
115		1, "Kernel settings are reset");
116	vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 0);
117	TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
118		0, "Kernel settings are clear");
119
120	TEST_EQ(c.nvdata[0], 0x40, "Header byte now just has the header bit");
121	/* That should have changed the CRC */
122	TEST_NEQ(c.nvdata[15], goodcrc,
123		 "vb2_nv_init() CRC changed due to flags clear");
124
125	/* Test explicitly setting the reset flags again */
126	vb2_nv_init(&c);
127	vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 1);
128	TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
129		1, "Firmware settings forced reset");
130	vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 0);
131
132	vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 1);
133	TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
134		1, "Kernel settings forced reset");
135	vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 0);
136
137	/* Get/set an invalid field */
138	vb2_nv_init(&c);
139	vb2_nv_set(&c, -1, 1);
140	TEST_EQ(vb2_nv_get(&c, -1), 0, "Get invalid setting");
141
142	/* Test other fields */
143	vb2_nv_init(&c);
144	for (vnf = nvfields; vnf->desc; vnf++) {
145		TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->default_value,
146			vnf->desc);
147		vb2_nv_set(&c, vnf->param, vnf->test_value);
148		TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value, vnf->desc);
149		vb2_nv_set(&c, vnf->param, vnf->test_value2);
150		TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value2,
151			vnf->desc);
152	}
153
154	/* None of those changes should have caused a reset to defaults */
155	vb2_nv_init(&c);
156	TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
157		0, "Firmware settings are still clear");
158	TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
159		0, "Kernel settings are still clear");
160
161	/* Writing identical settings doesn't cause the CRC to regenerate */
162	c.flags = 0;
163	vb2_nv_init(&c);
164	test_changed(&c, 0, "No regen CRC on open");
165	for (vnf = nvfields; vnf->desc; vnf++)
166		vb2_nv_set(&c, vnf->param, vnf->test_value2);
167	test_changed(&c, 0, "No regen CRC if data not changed");
168
169	/* Test out-of-range fields mapping to defaults or failing */
170	vb2_nv_init(&c);
171	vb2_nv_set(&c, VB2_NV_TRY_COUNT, 16);
172	TEST_EQ(vb2_nv_get(&c, VB2_NV_TRY_COUNT),
173		15, "Try b count out of range");
174	vb2_nv_set(&c, VB2_NV_RECOVERY_REQUEST, 0x101);
175	TEST_EQ(vb2_nv_get(&c, VB2_NV_RECOVERY_REQUEST),
176		VB2_RECOVERY_LEGACY, "Recovery request out of range");
177	vb2_nv_set(&c, VB2_NV_LOCALIZATION_INDEX, 0x102);
178	TEST_EQ(vb2_nv_get(&c, VB2_NV_LOCALIZATION_INDEX),
179		0, "Localization index out of range");
180
181	vb2_nv_set(&c, VB2_NV_FW_RESULT, VB2_FW_RESULT_UNKNOWN + 1);
182	vb2_nv_set(&c, VB2_NV_FW_RESULT, VB2_FW_RESULT_UNKNOWN + 100);
183	TEST_EQ(vb2_nv_get(&c, VB2_NV_FW_RESULT),
184		VB2_FW_RESULT_UNKNOWN, "Firmware result out of range");
185}
186
187int main(int argc, char* argv[])
188{
189	nv_storage_test();
190
191	return gTestSuccess ? 0 : 255;
192}
193