1 /* Copyright 2014 The ChromiumOS Authors
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 "2api.h"
14 #include "2common.h"
15 #include "2misc.h"
16 #include "2nvstorage_fields.h"
17 #include "2nvstorage.h"
18 #include "2sysincludes.h"
19 #include "common/tests.h"
20
21 /* Single NV storage field to test */
22 struct 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 const char *desc; /* Field description */
28 };
29
30 /* Array of fields to test, terminated with a field with desc==NULL. */
31 static 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, 0x1234, 0xFEDC, "kernel field"},
43 {VB2_NV_DEV_BOOT_EXTERNAL, 0, 1, 0, "dev boot usb"},
44 {VB2_NV_DEV_BOOT_ALTFW, 0, 1, 0, "dev boot altfw"},
45 {VB2_NV_DEV_BOOT_SIGNED_ONLY, 0, 1, 0, "dev boot custom"},
46 {VB2_NV_DEV_DEFAULT_BOOT, 0, 1, 2, "dev default boot"},
47 {VB2_NV_DIAG_REQUEST, 0, 1, 0, "diagnostic rom request"},
48 {VB2_NV_DISABLE_DEV_REQUEST, 0, 1, 0, "disable dev request"},
49 {VB2_NV_CLEAR_TPM_OWNER_REQUEST, 0, 1, 0, "clear tpm owner request"},
50 {VB2_NV_CLEAR_TPM_OWNER_DONE, 0, 1, 0, "clear tpm owner done"},
51 {VB2_NV_TPM_REQUESTED_REBOOT, 0, 1, 0, "tpm requested reboot"},
52 {VB2_NV_REQ_WIPEOUT, 0, 1, 0, "request wipeout"},
53 {VB2_NV_DISPLAY_REQUEST, 0, 1, 0, "oprom needed"},
54 {VB2_NV_BACKUP_NVRAM_REQUEST, 0, 1, 0, "backup nvram request"},
55 {VB2_NV_BOOT_ON_AC_DETECT, 0, 1, 0, "boot on ac detect"},
56 {VB2_NV_TRY_RO_SYNC, 0, 1, 0, "try read only software sync"},
57 {VB2_NV_BATTERY_CUTOFF_REQUEST, 0, 1, 0, "battery cutoff request"},
58 {VB2_NV_KERNEL_MAX_ROLLFORWARD, 0, 0x12345678, 0xFEDCBA98,
59 "kernel max rollforward"},
60 {VB2_NV_POST_EC_SYNC_DELAY, 0, 1, 0, "enable post-ec sync delay"},
61 {0, 0, 0, 0, NULL}
62 };
63
64 /* Fields added in v2. The test_value field is the default returned for V1. */
65 static struct nv_field nv2fields[] = {
66 {VB2_NV_FW_MAX_ROLLFORWARD, 0, VB2_FW_MAX_ROLLFORWARD_V1_DEFAULT,
67 0x87654321, "firmware max rollforward"},
68 {0, 0, 0, 0, NULL}
69 };
70
test_changed(struct vb2_context * c,int changed,const char * why)71 static void test_changed(struct vb2_context *c, int changed, const char *why)
72 {
73 if (changed)
74 TEST_NEQ(c->flags & VB2_CONTEXT_NVDATA_CHANGED, 0, why);
75 else
76 TEST_EQ(c->flags & VB2_CONTEXT_NVDATA_CHANGED, 0, why);
77 };
78
nv_storage_test(uint32_t ctxflags)79 static void nv_storage_test(uint32_t ctxflags)
80 {
81 struct nv_field *vnf;
82 uint8_t goodcrc;
83 uint8_t workbuf[VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE]
84 __attribute__((aligned(VB2_WORKBUF_ALIGN)));
85 struct vb2_context *ctx;
86
87 TEST_SUCC(vb2api_init(workbuf, sizeof(workbuf), &ctx),
88 "vb2api_init failed");
89 ctx->flags = ctxflags;
90
91 struct vb2_shared_data *sd = vb2_get_sd(ctx);
92
93 /* Things that change between V1 and V2 */
94 int expect_header = 0x30 | (ctxflags ? VB2_NV_HEADER_SIGNATURE_V2 :
95 VB2_NV_HEADER_SIGNATURE_V1);
96 int crc_offs = ctxflags ? VB2_NV_OFFS_CRC_V2 : VB2_NV_OFFS_CRC_V1;
97
98 TEST_EQ(vb2_nv_get_size(ctx), ctxflags ? VB2_NVDATA_SIZE_V2 :
99 VB2_NVDATA_SIZE, "vb2_nv_get_size()");
100
101 memset(ctx->nvdata, 0xA6, sizeof(ctx->nvdata));
102
103 /* Init with invalid data should set defaults and regenerate CRC */
104 vb2_nv_init(ctx);
105 TEST_EQ(ctx->nvdata[VB2_NV_OFFS_HEADER], expect_header,
106 "vb2_nv_init() reset header byte");
107 TEST_NEQ(ctx->nvdata[crc_offs], 0, "vb2_nv_init() CRC");
108 TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT | VB2_SD_STATUS_NV_REINIT,
109 "vb2_nv_init() status changed");
110 test_changed(ctx, 1, "vb2_nv_init() reset changed");
111 goodcrc = ctx->nvdata[crc_offs];
112 TEST_SUCC(vb2_nv_check_crc(ctx), "vb2_nv_check_crc() good");
113
114 /* Another init should not cause further changes */
115 ctx->flags = ctxflags;
116 sd->status = 0;
117 vb2_nv_init(ctx);
118 test_changed(ctx, 0, "vb2_nv_init() didn't re-reset");
119 TEST_EQ(ctx->nvdata[crc_offs], goodcrc,
120 "vb2_nv_init() CRC same");
121 TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT,
122 "vb2_nv_init() status same");
123
124 /* Perturbing signature bits in the header should force defaults */
125 ctx->nvdata[VB2_NV_OFFS_HEADER] ^= 0x40;
126 TEST_EQ(vb2_nv_check_crc(ctx),
127 VB2_ERROR_NV_HEADER, "vb2_nv_check_crc() bad header");
128 sd->status = 0;
129 vb2_nv_init(ctx);
130 TEST_EQ(ctx->nvdata[VB2_NV_OFFS_HEADER], expect_header,
131 "vb2_nv_init() reset header byte again");
132 test_changed(ctx, 1, "vb2_nv_init() corrupt changed");
133 TEST_EQ(ctx->nvdata[crc_offs], goodcrc,
134 "vb2_nv_init() CRC same again");
135
136 /* So should perturbing some other byte */
137 TEST_EQ(ctx->nvdata[VB2_NV_OFFS_KERNEL1], 0, "Kernel byte starts at 0");
138 ctx->nvdata[VB2_NV_OFFS_KERNEL1] = 12;
139 TEST_EQ(vb2_nv_check_crc(ctx),
140 VB2_ERROR_NV_CRC, "vb2_nv_check_crc() bad CRC");
141 sd->status = 0;
142 vb2_nv_init(ctx);
143 TEST_EQ(ctx->nvdata[VB2_NV_OFFS_KERNEL1], 0,
144 "vb2_nv_init() reset kernel byte");
145 test_changed(ctx, 1, "vb2_nv_init() corrupt elsewhere changed");
146 TEST_EQ(ctx->nvdata[crc_offs], goodcrc,
147 "vb2_nv_init() CRC same again");
148
149 /* Clear the kernel and firmware flags */
150 sd->status = 0;
151 vb2_nv_init(ctx);
152 TEST_EQ(vb2_nv_get(ctx, VB2_NV_FIRMWARE_SETTINGS_RESET),
153 1, "Firmware settings are reset");
154 vb2_nv_set(ctx, VB2_NV_FIRMWARE_SETTINGS_RESET, 0);
155 TEST_EQ(vb2_nv_get(ctx, VB2_NV_FIRMWARE_SETTINGS_RESET),
156 0, "Firmware settings are clear");
157
158 TEST_EQ(vb2_nv_get(ctx, VB2_NV_KERNEL_SETTINGS_RESET),
159 1, "Kernel settings are reset");
160 vb2_nv_set(ctx, VB2_NV_KERNEL_SETTINGS_RESET, 0);
161 TEST_EQ(vb2_nv_get(ctx, VB2_NV_KERNEL_SETTINGS_RESET),
162 0, "Kernel settings are clear");
163
164 TEST_EQ(ctx->nvdata[VB2_NV_OFFS_HEADER],
165 expect_header & VB2_NV_HEADER_SIGNATURE_MASK,
166 "Header byte now just has the signature");
167 /* That should have changed the CRC */
168 TEST_NEQ(ctx->nvdata[crc_offs], goodcrc,
169 "vb2_nv_init() CRC changed due to flags clear");
170
171 /* Test explicitly setting the reset flags again */
172 sd->status = 0;
173 vb2_nv_init(ctx);
174 vb2_nv_set(ctx, VB2_NV_FIRMWARE_SETTINGS_RESET, 1);
175 TEST_EQ(vb2_nv_get(ctx, VB2_NV_FIRMWARE_SETTINGS_RESET),
176 1, "Firmware settings forced reset");
177 vb2_nv_set(ctx, VB2_NV_FIRMWARE_SETTINGS_RESET, 0);
178
179 vb2_nv_set(ctx, VB2_NV_KERNEL_SETTINGS_RESET, 1);
180 TEST_EQ(vb2_nv_get(ctx, VB2_NV_KERNEL_SETTINGS_RESET),
181 1, "Kernel settings forced reset");
182 vb2_nv_set(ctx, VB2_NV_KERNEL_SETTINGS_RESET, 0);
183
184 /* Get/set an invalid field */
185 sd->status = 0;
186 vb2_nv_init(ctx);
187 vb2_nv_set(ctx, -1, 1);
188 TEST_EQ(vb2_nv_get(ctx, -1), 0, "Get invalid setting");
189
190 /* Test other fields */
191 sd->status = 0;
192 vb2_nv_init(ctx);
193 for (vnf = nvfields; vnf->desc; vnf++) {
194 TEST_EQ(vb2_nv_get(ctx, vnf->param), vnf->default_value,
195 vnf->desc);
196 vb2_nv_set(ctx, vnf->param, vnf->test_value);
197 TEST_EQ(vb2_nv_get(ctx, vnf->param), vnf->test_value, vnf->desc);
198 vb2_nv_set(ctx, vnf->param, vnf->test_value2);
199 TEST_EQ(vb2_nv_get(ctx, vnf->param), vnf->test_value2,
200 vnf->desc);
201 }
202
203 for (vnf = nv2fields; vnf->desc; vnf++) {
204 if (ctxflags) {
205 TEST_EQ(vb2_nv_get(ctx, vnf->param), vnf->default_value,
206 vnf->desc);
207 vb2_nv_set(ctx, vnf->param, vnf->test_value);
208 TEST_EQ(vb2_nv_get(ctx, vnf->param), vnf->test_value,
209 vnf->desc);
210 vb2_nv_set(ctx, vnf->param, vnf->test_value2);
211 TEST_EQ(vb2_nv_get(ctx, vnf->param), vnf->test_value2,
212 vnf->desc);
213 } else {
214 /*
215 * V2 fields always return defaults and can't be set if
216 * a V1 struct is present.
217 */
218 TEST_EQ(vb2_nv_get(ctx, vnf->param), vnf->test_value,
219 vnf->desc);
220 vb2_nv_set(ctx, vnf->param, vnf->test_value2);
221 TEST_EQ(vb2_nv_get(ctx, vnf->param), vnf->test_value,
222 vnf->desc);
223 }
224 }
225
226 /* None of those changes should have caused a reset to defaults */
227 sd->status = 0;
228 vb2_nv_init(ctx);
229 TEST_EQ(vb2_nv_get(ctx, VB2_NV_FIRMWARE_SETTINGS_RESET),
230 0, "Firmware settings are still clear");
231 TEST_EQ(vb2_nv_get(ctx, VB2_NV_KERNEL_SETTINGS_RESET),
232 0, "Kernel settings are still clear");
233
234 /* Writing identical settings doesn't cause the CRC to regenerate */
235 ctx->flags = ctxflags;
236 sd->status = 0;
237 vb2_nv_init(ctx);
238 test_changed(ctx, 0, "No regen CRC on open");
239 for (vnf = nvfields; vnf->desc; vnf++)
240 vb2_nv_set(ctx, vnf->param, vnf->test_value2);
241 test_changed(ctx, 0, "No regen CRC if data not changed");
242 /*
243 * If struct is V2, this is the same test. If struct is V1, this
244 * verifies that the field couldn't be changed anyway.
245 */
246 for (vnf = nv2fields; vnf->desc; vnf++)
247 vb2_nv_set(ctx, vnf->param, vnf->test_value2);
248 test_changed(ctx, 0, "No regen CRC if V2 data not changed");
249
250 /* Test out-of-range fields mapping to defaults or failing */
251 sd->status = 0;
252 vb2_nv_init(ctx);
253 vb2_nv_set(ctx, VB2_NV_TRY_COUNT, 16);
254 TEST_EQ(vb2_nv_get(ctx, VB2_NV_TRY_COUNT),
255 15, "Try b count out of range");
256 vb2_nv_set(ctx, VB2_NV_RECOVERY_REQUEST, 0x101);
257 TEST_EQ(vb2_nv_get(ctx, VB2_NV_RECOVERY_REQUEST),
258 VB2_RECOVERY_LEGACY, "Recovery request out of range");
259 vb2_nv_set(ctx, VB2_NV_LOCALIZATION_INDEX, 0x102);
260 TEST_EQ(vb2_nv_get(ctx, VB2_NV_LOCALIZATION_INDEX),
261 0, "Localization index out of range");
262
263 vb2_nv_set(ctx, VB2_NV_FW_RESULT, 100);
264 TEST_EQ(vb2_nv_get(ctx, VB2_NV_FW_RESULT),
265 VB2_FW_RESULT_UNKNOWN, "Firmware result out of range");
266
267 vb2_nv_set(ctx, VB2_NV_FW_PREV_RESULT, 100);
268 TEST_EQ(vb2_nv_get(ctx, VB2_NV_FW_PREV_RESULT),
269 VB2_FW_RESULT_UNKNOWN, "Fw prev result out of range");
270
271 vb2_nv_set(ctx, VB2_NV_DEV_DEFAULT_BOOT,
272 VB2_DEV_DEFAULT_BOOT_TARGET_INTERNAL + 100);
273 TEST_EQ(vb2_nv_get(ctx, VB2_NV_DEV_DEFAULT_BOOT),
274 VB2_DEV_DEFAULT_BOOT_TARGET_INTERNAL,
275 "default to booting from disk");
276 }
277
main(int argc,char * argv[])278 int main(int argc, char* argv[])
279 {
280 printf("Testing V1\n");
281 nv_storage_test(0);
282 printf("Testing V2\n");
283 nv_storage_test(VB2_CONTEXT_NVDATA_V2);
284
285 return gTestSuccess ? 0 : 255;
286 }
287