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_FW_PREV_TRIED, 0, 1, 0, "firmware prev tried"},
38	{VB2_NV_FW_PREV_RESULT, 0, 1, 3, "firmware prev result"},
39	{VB2_NV_RECOVERY_REQUEST, 0, 0x42, 0xED, "recovery request"},
40	{VB2_NV_RECOVERY_SUBCODE, 0, 0x56, 0xAC, "recovery subcode"},
41	{VB2_NV_LOCALIZATION_INDEX, 0, 0x69, 0xB0, "localization index"},
42	{VB2_NV_KERNEL_FIELD, 0, 0x12345678, 0xFEDCBA98, "kernel field"},
43	{VB2_NV_DEV_BOOT_USB, 0, 1, 0, "dev boot usb"},
44	{VB2_NV_DEV_BOOT_LEGACY, 0, 1, 0, "dev boot legacy"},
45	{VB2_NV_DEV_BOOT_SIGNED_ONLY, 0, 1, 0, "dev boot custom"},
46	{VB2_NV_DISABLE_DEV_REQUEST, 0, 1, 0, "disable dev request"},
47	{VB2_NV_CLEAR_TPM_OWNER_REQUEST, 0, 1, 0, "clear tpm owner request"},
48	{VB2_NV_CLEAR_TPM_OWNER_DONE, 0, 1, 0, "clear tpm owner done"},
49	{VB2_NV_OPROM_NEEDED, 0, 1, 0, "oprom needed"},
50	{VB2_NV_BACKUP_NVRAM_REQUEST, 0, 1, 0, "backup nvram request"},
51	{0, 0, 0, 0, NULL}
52};
53
54static void test_changed(struct vb2_context *ctx, int changed, const char *why)
55{
56	if (changed)
57		TEST_NEQ(ctx->flags & VB2_CONTEXT_NVDATA_CHANGED, 0, why);
58	else
59		TEST_EQ(ctx->flags & VB2_CONTEXT_NVDATA_CHANGED, 0, why);
60};
61
62static void nv_storage_test(void)
63{
64	struct nv_field *vnf;
65	uint8_t goodcrc;
66	uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE]
67		__attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
68	struct vb2_context c = {
69		.flags = 0,
70		.workbuf = workbuf,
71		.workbuf_size = sizeof(workbuf),
72	};
73	struct vb2_shared_data *sd = vb2_get_sd(&c);
74
75	memset(c.nvdata, 0xA6, sizeof(c.nvdata));
76	vb2_init_context(&c);
77
78	/* Init with invalid data should set defaults and regenerate CRC */
79	vb2_nv_init(&c);
80	TEST_EQ(c.nvdata[0], 0x70, "vb2_nv_init() reset header byte");
81	TEST_NEQ(c.nvdata[15], 0, "vb2_nv_init() CRC");
82	TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT | VB2_SD_STATUS_NV_REINIT,
83		"vb2_nv_init() status changed");
84	test_changed(&c, 1, "vb2_nv_init() reset changed");
85	goodcrc = c.nvdata[15];
86	TEST_SUCC(vb2_nv_check_crc(&c), "vb2_nv_check_crc() good");
87
88	/* Another init should not cause further changes */
89	c.flags = 0;
90	sd->status = 0;
91	vb2_nv_init(&c);
92	test_changed(&c, 0, "vb2_nv_init() didn't re-reset");
93	TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same");
94	TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT, "vb2_nv_init() status same");
95
96	/* Perturbing the header should force defaults */
97	c.nvdata[0] ^= 0x40;
98	TEST_EQ(vb2_nv_check_crc(&c),
99		VB2_ERROR_NV_HEADER, "vb2_nv_check_crc() bad header");
100	vb2_nv_init(&c);
101	TEST_EQ(c.nvdata[0], 0x70, "vb2_nv_init() reset header byte again");
102	test_changed(&c, 1, "vb2_nv_init() corrupt changed");
103	TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same again");
104
105	/* So should perturbing some other byte */
106	TEST_EQ(c.nvdata[11], 0, "Kernel byte starts at 0");
107	c.nvdata[11] = 12;
108	TEST_EQ(vb2_nv_check_crc(&c),
109		VB2_ERROR_NV_CRC, "vb2_nv_check_crc() bad CRC");
110	vb2_nv_init(&c);
111	TEST_EQ(c.nvdata[11], 0, "vb2_nv_init() reset kernel byte");
112	test_changed(&c, 1, "vb2_nv_init() corrupt elsewhere changed");
113	TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same again");
114
115	/* Clear the kernel and firmware flags */
116	vb2_nv_init(&c);
117	TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
118		1, "Firmware settings are reset");
119	vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 0);
120	TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
121		0, "Firmware settings are clear");
122
123	TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
124		1, "Kernel settings are reset");
125	vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 0);
126	TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
127		0, "Kernel settings are clear");
128
129	TEST_EQ(c.nvdata[0], 0x40, "Header byte now just has the header bit");
130	/* That should have changed the CRC */
131	TEST_NEQ(c.nvdata[15], goodcrc,
132		 "vb2_nv_init() CRC changed due to flags clear");
133
134	/* Test explicitly setting the reset flags again */
135	vb2_nv_init(&c);
136	vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 1);
137	TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
138		1, "Firmware settings forced reset");
139	vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 0);
140
141	vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 1);
142	TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
143		1, "Kernel settings forced reset");
144	vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 0);
145
146	/* Get/set an invalid field */
147	vb2_nv_init(&c);
148	vb2_nv_set(&c, -1, 1);
149	TEST_EQ(vb2_nv_get(&c, -1), 0, "Get invalid setting");
150
151	/* Test other fields */
152	vb2_nv_init(&c);
153	for (vnf = nvfields; vnf->desc; vnf++) {
154		TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->default_value,
155			vnf->desc);
156		vb2_nv_set(&c, vnf->param, vnf->test_value);
157		TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value, vnf->desc);
158		vb2_nv_set(&c, vnf->param, vnf->test_value2);
159		TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value2,
160			vnf->desc);
161	}
162
163	/* None of those changes should have caused a reset to defaults */
164	vb2_nv_init(&c);
165	TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
166		0, "Firmware settings are still clear");
167	TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
168		0, "Kernel settings are still clear");
169
170	/* Writing identical settings doesn't cause the CRC to regenerate */
171	c.flags = 0;
172	vb2_nv_init(&c);
173	test_changed(&c, 0, "No regen CRC on open");
174	for (vnf = nvfields; vnf->desc; vnf++)
175		vb2_nv_set(&c, vnf->param, vnf->test_value2);
176	test_changed(&c, 0, "No regen CRC if data not changed");
177
178	/* Test out-of-range fields mapping to defaults or failing */
179	vb2_nv_init(&c);
180	vb2_nv_set(&c, VB2_NV_TRY_COUNT, 16);
181	TEST_EQ(vb2_nv_get(&c, VB2_NV_TRY_COUNT),
182		15, "Try b count out of range");
183	vb2_nv_set(&c, VB2_NV_RECOVERY_REQUEST, 0x101);
184	TEST_EQ(vb2_nv_get(&c, VB2_NV_RECOVERY_REQUEST),
185		VB2_RECOVERY_LEGACY, "Recovery request out of range");
186	vb2_nv_set(&c, VB2_NV_LOCALIZATION_INDEX, 0x102);
187	TEST_EQ(vb2_nv_get(&c, VB2_NV_LOCALIZATION_INDEX),
188		0, "Localization index out of range");
189
190	vb2_nv_set(&c, VB2_NV_FW_RESULT, VB2_FW_RESULT_UNKNOWN + 1);
191	vb2_nv_set(&c, VB2_NV_FW_RESULT, VB2_FW_RESULT_UNKNOWN + 100);
192	TEST_EQ(vb2_nv_get(&c, VB2_NV_FW_RESULT),
193		VB2_FW_RESULT_UNKNOWN, "Firmware result out of range");
194}
195
196int main(int argc, char* argv[])
197{
198	nv_storage_test();
199
200	return gTestSuccess ? 0 : 255;
201}
202