1 /* Copyright (c) 2013 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.
5 * Tests for vboot_api_kernel, part 3 - software sync
12 #include "gbb_header.h"
13 #include "host_common.h"
14 #include "load_kernel_fw.h"
15 #include "rollback_index.h"
16 #include "test_common.h"
17 #include "vboot_audio.h"
18 #include "vboot_common.h"
19 #include "vboot_kernel.h"
20 #include "vboot_nvstorage.h"
21 #include "vboot_struct.h"
24 static VbCommonParams cparams;
25 static uint8_t shared_data[VB_SHARED_DATA_MIN_SIZE];
26 static VbSharedDataHeader *shared = (VbSharedDataHeader *)shared_data;
27 static GoogleBinaryBlockHeader gbb;
30 static int mock_in_rw;
31 static VbError_t in_rw_retval;
32 static int protect_retval;
33 static int ec_protected;
34 static int run_retval;
35 static int ec_run_image;
36 static int update_retval;
37 static int ec_updated;
38 static int get_expected_retval;
39 static int shutdown_request_calls_left;
41 static uint8_t mock_ec_hash[32];
42 static int mock_ec_hash_size;
43 static uint8_t want_ec_hash[32];
44 static int want_ec_hash_size;
45 static uint8_t mock_sha[32];
47 static uint32_t screens_displayed[8];
48 static uint32_t screens_count = 0;
50 /* Reset mock data (for use before each test) */
51 static void ResetMocks(void)
53 Memset(&cparams, 0, sizeof(cparams));
54 cparams.shared_data_size = sizeof(shared_data);
55 cparams.shared_data_blob = shared_data;
56 cparams.gbb_data = &gbb;
58 Memset(&gbb, 0, sizeof(gbb));
59 gbb.major_version = GBB_MAJOR_VER;
60 gbb.minor_version = GBB_MINOR_VER;
64 * Only the outermost vboot_api_kernel call sets vboot_api_kernel's
65 * vnc. So clear it here too.
67 Memset(VbApiKernelGetVnc(), 0, sizeof(VbNvContext));
68 VbNvSetup(VbApiKernelGetVnc());
69 VbNvTeardown(VbApiKernelGetVnc()); /* So CRC gets generated */
71 Memset(&shared_data, 0, sizeof(shared_data));
72 VbSharedDataInit(shared, sizeof(shared_data));
77 ec_run_image = 0; /* 0 = RO, 1 = RW */
79 in_rw_retval = VBERROR_SUCCESS;
80 protect_retval = VBERROR_SUCCESS;
81 update_retval = VBERROR_SUCCESS;
82 run_retval = VBERROR_SUCCESS;
83 get_expected_retval = VBERROR_SUCCESS;
84 shutdown_request_calls_left = -1;
86 Memset(mock_ec_hash, 0, sizeof(mock_ec_hash));
88 mock_ec_hash_size = sizeof(mock_ec_hash);
90 Memset(want_ec_hash, 0, sizeof(want_ec_hash));
92 want_ec_hash_size = sizeof(want_ec_hash);
94 Memset(mock_sha, 0, sizeof(want_ec_hash));
97 // TODO: ensure these are actually needed
99 Memset(screens_displayed, 0, sizeof(screens_displayed));
105 uint32_t VbExIsShutdownRequested(void)
107 if (shutdown_request_calls_left == 0)
109 else if (shutdown_request_calls_left > 0)
110 shutdown_request_calls_left--;
115 int VbExTrustEC(int devidx)
120 VbError_t VbExEcRunningRW(int devidx, int *in_rw)
126 VbError_t VbExEcProtect(int devidx, enum VbSelectFirmware_t select)
129 return protect_retval;
132 VbError_t VbExEcDisableJump(int devidx)
137 VbError_t VbExEcJumpToRW(int devidx)
143 VbError_t VbExEcHashImage(int devidx, enum VbSelectFirmware_t select,
144 const uint8_t **hash, int *hash_size)
146 *hash = mock_ec_hash;
147 *hash_size = mock_ec_hash_size;
148 return mock_ec_hash_size ? VBERROR_SUCCESS : VBERROR_SIMULATED;
151 VbError_t VbExEcGetExpectedImage(int devidx, enum VbSelectFirmware_t select,
152 const uint8_t **image, int *image_size)
154 static uint8_t fake_image[64] = {5, 6, 7, 8};
156 *image_size = sizeof(fake_image);
157 return get_expected_retval;
160 VbError_t VbExEcGetExpectedImageHash(int devidx, enum VbSelectFirmware_t select,
161 const uint8_t **hash, int *hash_size)
163 *hash = want_ec_hash;
164 *hash_size = want_ec_hash_size;
166 if (want_ec_hash_size == -1)
167 return VBERROR_EC_GET_EXPECTED_HASH_FROM_IMAGE;
169 return want_ec_hash_size ? VBERROR_SUCCESS : VBERROR_SIMULATED;
172 uint8_t *internal_SHA256(const uint8_t *data, uint64_t len, uint8_t *digest)
174 Memcpy(digest, mock_sha, sizeof(mock_sha));
178 VbError_t VbExEcUpdateImage(int devidx, enum VbSelectFirmware_t select,
179 const uint8_t *image, int image_size)
182 return update_retval;
185 VbError_t VbDisplayScreen(VbCommonParams *cparams, uint32_t screen, int force,
188 if (screens_count < ARRAY_SIZE(screens_displayed))
189 screens_displayed[screens_count++] = screen;
191 return VBERROR_SUCCESS;
194 static void test_ssync(VbError_t retval, int recovery_reason, const char *desc)
198 TEST_EQ(VbEcSoftwareSync(0, &cparams), retval, desc);
199 VbNvGet(VbApiKernelGetVnc(), VBNV_RECOVERY_REQUEST, &u);
200 TEST_EQ(u, recovery_reason, " recovery reason");
205 static void VbSoftwareSyncTest(void)
209 shared->recovery_reason = 123;
210 test_ssync(0, 0, "In recovery, EC-RO");
211 TEST_EQ(ec_protected, 0, " ec protected");
214 shared->recovery_reason = 123;
216 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
217 123, "Recovery needs EC-RO");
221 in_rw_retval = VBERROR_SIMULATED;
222 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
223 VBNV_RECOVERY_EC_UNKNOWN_IMAGE, "Unknown EC image");
226 shared->flags |= VBSD_LF_USE_RO_NORMAL;
228 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
229 0, "AP-RO needs EC-RO");
232 shared->flags |= VBSD_LF_USE_RO_NORMAL;
233 test_ssync(0, 0, "AP-RO, EC-RO");
234 TEST_EQ(ec_protected, 1, " ec protected");
235 TEST_EQ(ec_run_image, 0, " ec run image");
238 shared->flags |= VBSD_LF_USE_RO_NORMAL;
239 run_retval = VBERROR_SIMULATED;
240 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
241 VBNV_RECOVERY_EC_SOFTWARE_SYNC, "Stay in RO fail");
244 shared->flags |= VBSD_LF_USE_RO_NORMAL;
245 protect_retval = VBERROR_SIMULATED;
246 test_ssync(VBERROR_SIMULATED,
247 VBNV_RECOVERY_EC_PROTECT, "Protect error");
249 /* No longer check for shutdown requested */
251 shared->flags |= VBSD_LF_USE_RO_NORMAL;
252 shutdown_request_calls_left = 0;
253 test_ssync(0, 0, "AP-RO shutdown requested");
255 /* Calculate hashes */
257 mock_ec_hash_size = 0;
258 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
259 VBNV_RECOVERY_EC_HASH_FAILED, "Bad EC hash");
262 mock_ec_hash_size = 16;
263 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
264 VBNV_RECOVERY_EC_HASH_SIZE, "Bad EC hash size");
267 want_ec_hash_size = 0;
268 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
269 VBNV_RECOVERY_EC_EXPECTED_HASH, "Bad precalculated hash");
272 want_ec_hash_size = 16;
273 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
274 VBNV_RECOVERY_EC_EXPECTED_HASH,
275 "Bad precalculated hash size");
279 want_ec_hash_size = -1;
280 test_ssync(0, 0, "No precomputed hash");
283 want_ec_hash_size = -1;
284 get_expected_retval = VBERROR_SIMULATED;
285 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
286 VBNV_RECOVERY_EC_EXPECTED_IMAGE, "Can't fetch image");
288 /* Updates required */
292 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
293 VBNV_RECOVERY_EC_HASH_MISMATCH,
294 "Precalculated hash mismatch");
299 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
300 0, "Pending update needs reboot");
304 test_ssync(0, 0, "Update without reboot");
305 TEST_EQ(ec_protected, 1, " ec protected");
306 TEST_EQ(ec_run_image, 1, " ec run image");
307 TEST_EQ(ec_updated, 1, " ec updated");
311 update_retval = VBERROR_EC_REBOOT_TO_RO_REQUIRED;
312 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
313 0, "Reboot after update");
314 TEST_EQ(ec_updated, 1, " ec updated");
318 update_retval = VBERROR_SIMULATED;
319 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
320 VBNV_RECOVERY_EC_UPDATE, "Update failed");
324 shared->flags |= VBSD_EC_SLOW_UPDATE;
325 test_ssync(0, 0, "Slow update");
326 TEST_EQ(screens_displayed[0], VB_SCREEN_WAIT, " wait screen");
328 /* RW cases, no update */
331 test_ssync(0, 0, "AP-RW, EC-RW");
334 test_ssync(0, 0, "AP-RW, EC-RO -> EC-RW");
335 TEST_EQ(ec_protected, 1, " ec protected");
336 TEST_EQ(ec_run_image, 1, " ec run image");
337 TEST_EQ(ec_updated, 0, " ec updated");
340 run_retval = VBERROR_SIMULATED;
341 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
342 VBNV_RECOVERY_EC_JUMP_RW, "Jump to RW fail");
345 run_retval = VBERROR_EC_REBOOT_TO_RO_REQUIRED;
346 test_ssync(VBERROR_EC_REBOOT_TO_RO_REQUIRED,
347 0, "Jump to RW fail because locked");
350 protect_retval = VBERROR_SIMULATED;
351 test_ssync(VBERROR_SIMULATED,
352 VBNV_RECOVERY_EC_PROTECT, "Protect error");
354 /* No longer check for shutdown requested */
356 shutdown_request_calls_left = 0;
358 "AP-RW, EC-RO -> EC-RW shutdown requested");
362 shutdown_request_calls_left = 0;
363 test_ssync(0, 0, "AP-RW shutdown requested");
368 VbSoftwareSyncTest();
370 if (vboot_api_stub_check_memory())
373 return gTestSuccess ? 0 : 255;