blob: b55868019f99b90e8802ca0562438b948fc6f68e [file] [log] [blame]
Randall Spangler7c557082013-02-05 09:44:31 -08001/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
Bill Richardsonbf020a02012-01-25 14:06:24 -08002 * 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 VbTryLoadKernel()
6 */
7
Joel Kitching8a4f0be2019-04-25 15:41:35 +08008#include "2common.h"
Joel Kitchingf06f7552019-10-01 16:02:44 +08009#include "2misc.h"
Randall Spanglere4136dc2016-10-27 14:34:59 -070010#include "2nvstorage.h"
Joel Kitchingffd42a82019-08-29 13:58:52 +080011#include "2sysincludes.h"
Bill Richardsonbf020a02012-01-25 14:06:24 -080012#include "load_kernel_fw.h"
Joel Kitching110df5c2019-08-29 13:27:23 +080013#include "secdata_tpm.h"
Bill Richardsonbf020a02012-01-25 14:06:24 -080014#include "test_common.h"
15#include "utility.h"
16#include "vboot_api.h"
Randall Spangler7f436692013-02-05 12:42:36 -080017#include "vboot_kernel.h"
Julius Werner52fa8c12019-05-07 12:59:47 -070018#include "vboot_test.h"
Randall Spanglerad7a7552016-11-22 12:58:22 -080019
Bill Richardsonbf020a02012-01-25 14:06:24 -080020#define MAX_TEST_DISKS 10
21#define DEFAULT_COUNT -1
22
23typedef struct {
Randall Spangler7c557082013-02-05 09:44:31 -080024 uint64_t bytes_per_lba;
25 uint64_t lba_count;
26 uint32_t flags;
27 const char *diskname;
Bill Richardsonbf020a02012-01-25 14:06:24 -080028} disk_desc_t;
29
30typedef struct {
Julius Werner52fa8c12019-05-07 12:59:47 -070031 const char *name;
Bill Richardsonbf020a02012-01-25 14:06:24 -080032
Randall Spangler7c557082013-02-05 09:44:31 -080033 /* inputs for test case */
34 uint32_t want_flags;
Joel Kitching90671fa2019-07-31 13:17:08 +080035 vb2_error_t diskgetinfo_return_val;
Randall Spangler7c557082013-02-05 09:44:31 -080036 disk_desc_t disks_to_provide[MAX_TEST_DISKS];
37 int disk_count_to_return;
Joel Kitching90671fa2019-07-31 13:17:08 +080038 vb2_error_t loadkernel_return_val[MAX_TEST_DISKS];
Dan Ehrenberg3f4d8d02014-12-02 08:21:57 -080039 uint8_t external_expected[MAX_TEST_DISKS];
Bill Richardsonbf020a02012-01-25 14:06:24 -080040
Randall Spangler7c557082013-02-05 09:44:31 -080041 /* outputs from test */
42 uint32_t expected_recovery_request_val;
43 const char *expected_to_find_disk;
44 const char *expected_to_load_disk;
45 uint32_t expected_return_val;
Bill Richardsonbf020a02012-01-25 14:06:24 -080046
47} test_case_t;
48
49/****************************************************************************/
50/* Test cases */
51
52static const char pickme[] = "correct choice";
53#define DONT_CARE ((const char *)42)
54
55test_case_t test[] = {
Randall Spangler7c557082013-02-05 09:44:31 -080056 {
57 .name = "first removable drive",
58 .want_flags = VB_DISK_FLAG_REMOVABLE,
59 .disks_to_provide = {
60 /* too small */
Sam Hurstae602de2018-04-26 12:42:31 -070061 {512, 10, VB_DISK_FLAG_REMOVABLE, 0},
Randall Spangler7c557082013-02-05 09:44:31 -080062 /* wrong LBA */
Sam Hurstae602de2018-04-26 12:42:31 -070063 {511, 100, VB_DISK_FLAG_REMOVABLE, 0},
64 /* not a power of 2 */
65 {2047, 100, VB_DISK_FLAG_REMOVABLE, 0},
66 /* wrong type */
67 {512, 100, VB_DISK_FLAG_FIXED, 0},
68 /* wrong flags */
69 {512, 100, 0, 0},
70 /* still wrong flags */
71 {512, 100, -1, 0},
72 {4096, 100, VB_DISK_FLAG_REMOVABLE, pickme},
73 /* already got one */
74 {512, 100, VB_DISK_FLAG_REMOVABLE, "holygrail"},
75 },
76 .disk_count_to_return = DEFAULT_COUNT,
Joel Kitchingcf49e7b2019-07-29 18:51:00 +080077 .diskgetinfo_return_val = VB2_SUCCESS,
Julius Wernerddcec122019-10-14 16:16:20 -070078 .loadkernel_return_val = {0},
79 .external_expected = {0},
Sam Hurstae602de2018-04-26 12:42:31 -070080
81 .expected_recovery_request_val = VB2_RECOVERY_NOT_REQUESTED,
82 .expected_to_find_disk = pickme,
83 .expected_to_load_disk = pickme,
Joel Kitchingcf49e7b2019-07-29 18:51:00 +080084 .expected_return_val = VB2_SUCCESS
Sam Hurstae602de2018-04-26 12:42:31 -070085 },
86 {
Julius Wernerddcec122019-10-14 16:16:20 -070087 .name = "first removable drive (skip external GPT)",
Sam Hurstae602de2018-04-26 12:42:31 -070088 .want_flags = VB_DISK_FLAG_REMOVABLE,
89 .disks_to_provide = {
90 /* too small */
91 {512, 10, VB_DISK_FLAG_REMOVABLE, 0},
92 /* wrong LBA */
93 {511, 100, VB_DISK_FLAG_REMOVABLE, 0},
94 /* not a power of 2 */
95 {2047, 100, VB_DISK_FLAG_REMOVABLE, 0},
Randall Spangler7c557082013-02-05 09:44:31 -080096 /* wrong type */
97 {512, 100, VB_DISK_FLAG_FIXED, 0},
98 /* wrong flags */
99 {512, 100, 0, 0},
100 /* still wrong flags */
101 {512, 100, -1, 0},
Dan Ehrenberg3f4d8d02014-12-02 08:21:57 -0800102 {512, 100,
103 VB_DISK_FLAG_REMOVABLE | VB_DISK_FLAG_EXTERNAL_GPT,
104 pickme},
Randall Spangler7c557082013-02-05 09:44:31 -0800105 /* already got one */
106 {512, 100, VB_DISK_FLAG_REMOVABLE, "holygrail"},
107 },
108 .disk_count_to_return = DEFAULT_COUNT,
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800109 .diskgetinfo_return_val = VB2_SUCCESS,
Julius Wernerddcec122019-10-14 16:16:20 -0700110 .loadkernel_return_val = {0, 0},
111 .external_expected = {1, 0},
Bill Richardsonbf020a02012-01-25 14:06:24 -0800112
Randall Spanglerdff58522017-11-27 15:37:13 -0800113 .expected_recovery_request_val = VB2_RECOVERY_NOT_REQUESTED,
Randall Spangler7c557082013-02-05 09:44:31 -0800114 .expected_to_find_disk = pickme,
115 .expected_to_load_disk = pickme,
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800116 .expected_return_val = VB2_SUCCESS
Randall Spangler7c557082013-02-05 09:44:31 -0800117 },
118 {
119 .name = "second removable drive",
120 .want_flags = VB_DISK_FLAG_REMOVABLE,
121 .disks_to_provide = {
122 /* wrong flags */
123 {512, 100, 0, 0},
124 {512, 100, VB_DISK_FLAG_REMOVABLE, "not yet"},
125 {512, 100, VB_DISK_FLAG_REMOVABLE, pickme},
126 },
127 .disk_count_to_return = DEFAULT_COUNT,
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800128 .diskgetinfo_return_val = VB2_SUCCESS,
Julius Wernerddcec122019-10-14 16:16:20 -0700129 .loadkernel_return_val = {VBERROR_INVALID_KERNEL_FOUND, 0},
Bill Richardsonbf020a02012-01-25 14:06:24 -0800130
Randall Spanglerdff58522017-11-27 15:37:13 -0800131 .expected_recovery_request_val = VB2_RECOVERY_NOT_REQUESTED,
Randall Spangler7c557082013-02-05 09:44:31 -0800132 .expected_to_find_disk = pickme,
133 .expected_to_load_disk = pickme,
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800134 .expected_return_val = VB2_SUCCESS
Randall Spangler7c557082013-02-05 09:44:31 -0800135 },
136 {
137 .name = "first fixed drive",
138 .want_flags = VB_DISK_FLAG_FIXED,
139 .disks_to_provide = {
140 /* too small */
141 {512, 10, VB_DISK_FLAG_FIXED, 0},
142 /* wrong LBA */
Sam Hurstae602de2018-04-26 12:42:31 -0700143 {511, 100, VB_DISK_FLAG_FIXED, 0},
144 /* not a power of 2 */
145 {2047, 100, VB_DISK_FLAG_REMOVABLE, 0},
Randall Spangler7c557082013-02-05 09:44:31 -0800146 /* wrong type */
147 {512, 100, VB_DISK_FLAG_REMOVABLE, 0},
148 /* wrong flags */
149 {512, 100, 0, 0},
150 /* still wrong flags */
151 {512, 100, -1, 0},
152 /* flags */
153 {512, 100, VB_DISK_FLAG_REMOVABLE|VB_DISK_FLAG_FIXED,
154 0},
155 {512, 100, VB_DISK_FLAG_FIXED, pickme},
156 /* already got one */
157 {512, 100, VB_DISK_FLAG_FIXED, "holygrail"},
158 },
159 .disk_count_to_return = DEFAULT_COUNT,
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800160 .diskgetinfo_return_val = VB2_SUCCESS,
Julius Wernerddcec122019-10-14 16:16:20 -0700161 .loadkernel_return_val = {0},
Bill Richardsonbf020a02012-01-25 14:06:24 -0800162
Randall Spanglerdff58522017-11-27 15:37:13 -0800163 .expected_recovery_request_val = VB2_RECOVERY_NOT_REQUESTED,
Randall Spangler7c557082013-02-05 09:44:31 -0800164 .expected_to_find_disk = pickme,
165 .expected_to_load_disk = pickme,
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800166 .expected_return_val = VB2_SUCCESS
Randall Spangler7c557082013-02-05 09:44:31 -0800167 },
168 {
169 .name = "no drives at all",
170 .want_flags = VB_DISK_FLAG_FIXED,
171 .disks_to_provide = {},
172 .disk_count_to_return = DEFAULT_COUNT,
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800173 .diskgetinfo_return_val = VB2_SUCCESS,
Bill Richardsonbf020a02012-01-25 14:06:24 -0800174
Randall Spanglerdff58522017-11-27 15:37:13 -0800175 .expected_recovery_request_val = VB2_RECOVERY_RW_NO_DISK,
Randall Spangler7c557082013-02-05 09:44:31 -0800176 .expected_to_find_disk = 0,
177 .expected_to_load_disk = 0,
178 .expected_return_val = VBERROR_NO_DISK_FOUND
179 },
180 {
Julius Wernerddcec122019-10-14 16:16:20 -0700181 .name = "VbExDiskGetInfo() error",
182 .want_flags = VB_DISK_FLAG_FIXED,
183 .disks_to_provide = {
184 {512, 10, VB_DISK_FLAG_REMOVABLE, 0},
185 {512, 100, VB_DISK_FLAG_FIXED, 0},
186 },
187 .disk_count_to_return = DEFAULT_COUNT,
188 .diskgetinfo_return_val = VB2_ERROR_UNKNOWN,
189
190 .expected_recovery_request_val = VB2_RECOVERY_RW_NO_DISK,
191 .expected_to_find_disk = 0,
192 .expected_to_load_disk = 0,
193 .expected_return_val = VBERROR_NO_DISK_FOUND,
194 },
195 {
196 .name = "invalid kernel",
Randall Spangler7c557082013-02-05 09:44:31 -0800197 .want_flags = VB_DISK_FLAG_FIXED,
198 .disks_to_provide = {
199 /* too small */
200 {512, 10, VB_DISK_FLAG_FIXED, 0},
201 /* wrong LBA */
Sam Hurstae602de2018-04-26 12:42:31 -0700202 {511, 100, VB_DISK_FLAG_FIXED, 0},
203 /* not a power of 2 */
204 {2047, 100, VB_DISK_FLAG_FIXED, 0},
Randall Spangler7c557082013-02-05 09:44:31 -0800205 /* wrong type */
206 {512, 100, VB_DISK_FLAG_REMOVABLE, 0},
207 /* wrong flags */
208 {512, 100, 0, 0},
209 /* still wrong flags */
210 {512, 100, -1, 0},
211 /* doesn't load */
Julius Wernerddcec122019-10-14 16:16:20 -0700212 {512, 100, VB_DISK_FLAG_FIXED, "corrupted kernel"},
Randall Spangler7c557082013-02-05 09:44:31 -0800213 /* doesn't load */
Julius Wernerddcec122019-10-14 16:16:20 -0700214 {512, 100, VB_DISK_FLAG_FIXED, "stateful partition"},
Randall Spangler7c557082013-02-05 09:44:31 -0800215 },
216 .disk_count_to_return = DEFAULT_COUNT,
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800217 .diskgetinfo_return_val = VB2_SUCCESS,
Julius Wernerddcec122019-10-14 16:16:20 -0700218 .loadkernel_return_val = {VBERROR_INVALID_KERNEL_FOUND,
219 VBERROR_NO_KERNEL_FOUND},
220
221 .expected_recovery_request_val = VB2_RECOVERY_RW_INVALID_OS,
222 .expected_to_find_disk = DONT_CARE,
223 .expected_to_load_disk = 0,
224 .expected_return_val = VBERROR_INVALID_KERNEL_FOUND,
225 },
226 {
227 .name = "invalid kernel, order flipped",
228 .want_flags = VB_DISK_FLAG_FIXED,
229 .disks_to_provide = {
230 {512, 1000, VB_DISK_FLAG_FIXED, "stateful partition"},
231 {512, 1000, VB_DISK_FLAG_FIXED, "corrupted kernel"},
232 },
233 .disk_count_to_return = DEFAULT_COUNT,
234 .diskgetinfo_return_val = VB2_SUCCESS,
235 .loadkernel_return_val = {VBERROR_NO_KERNEL_FOUND,
236 VBERROR_INVALID_KERNEL_FOUND},
237
238 .expected_recovery_request_val = VB2_RECOVERY_RW_INVALID_OS,
239 .expected_to_find_disk = DONT_CARE,
240 .expected_to_load_disk = 0,
241 .expected_return_val = VBERROR_INVALID_KERNEL_FOUND,
242 },
243 {
244 .name = "no Chrome OS partitions",
245 .want_flags = VB_DISK_FLAG_FIXED,
246 .disks_to_provide = {
247 {512, 100, VB_DISK_FLAG_FIXED, "stateful partition"},
248 {512, 1000, VB_DISK_FLAG_FIXED, "Chrubuntu"},
249 },
250 .disk_count_to_return = DEFAULT_COUNT,
251 .diskgetinfo_return_val = VB2_SUCCESS,
252 .loadkernel_return_val = {VBERROR_NO_KERNEL_FOUND,
253 VBERROR_NO_KERNEL_FOUND},
Bill Richardsonbf020a02012-01-25 14:06:24 -0800254
Randall Spanglerdff58522017-11-27 15:37:13 -0800255 .expected_recovery_request_val = VB2_RECOVERY_RW_NO_KERNEL,
Randall Spangler7c557082013-02-05 09:44:31 -0800256 .expected_to_find_disk = DONT_CARE,
257 .expected_to_load_disk = 0,
Julius Wernerddcec122019-10-14 16:16:20 -0700258 .expected_return_val = VBERROR_NO_KERNEL_FOUND,
Randall Spangler7c557082013-02-05 09:44:31 -0800259 },
Julius Werner03206172019-10-14 16:37:46 -0700260 {
261 .name = "invalid kernel (removable)",
262 .want_flags = VB_DISK_FLAG_REMOVABLE,
263 .disks_to_provide = {
264 {512, 100, VB_DISK_FLAG_REMOVABLE, "corrupted"},
265 {512, 100, VB_DISK_FLAG_REMOVABLE, "data"},
266 },
267 .disk_count_to_return = DEFAULT_COUNT,
268 .diskgetinfo_return_val = VB2_SUCCESS,
269 .loadkernel_return_val = {VBERROR_INVALID_KERNEL_FOUND,
270 VBERROR_NO_KERNEL_FOUND},
271
272 .expected_recovery_request_val = VB2_RECOVERY_NOT_REQUESTED,
273 .expected_to_find_disk = DONT_CARE,
274 .expected_to_load_disk = 0,
275 .expected_return_val = VBERROR_INVALID_KERNEL_FOUND,
276 },
277 {
278 .name = "no kernel (removable)",
279 .want_flags = VB_DISK_FLAG_REMOVABLE,
280 .disks_to_provide = {
281 {512, 100, VB_DISK_FLAG_REMOVABLE, "data"},
282 },
283 .disk_count_to_return = DEFAULT_COUNT,
284 .diskgetinfo_return_val = VB2_SUCCESS,
285 .loadkernel_return_val = {VBERROR_NO_KERNEL_FOUND},
286
287 .expected_recovery_request_val = VB2_RECOVERY_NOT_REQUESTED,
288 .expected_to_find_disk = DONT_CARE,
289 .expected_to_load_disk = 0,
290 .expected_return_val = VBERROR_NO_KERNEL_FOUND,
291 },
Bill Richardsonbf020a02012-01-25 14:06:24 -0800292};
293
Bill Richardsonbf020a02012-01-25 14:06:24 -0800294/****************************************************************************/
295
296/* Mock data */
Bill Richardsonbf020a02012-01-25 14:06:24 -0800297static VbDiskInfo mock_disks[MAX_TEST_DISKS];
298static test_case_t *t;
299static int load_kernel_calls;
300static uint32_t got_recovery_request_val;
301static const char *got_find_disk;
302static const char *got_load_disk;
303static uint32_t got_return_val;
Dan Ehrenberg3f4d8d02014-12-02 08:21:57 -0800304static uint32_t got_external_mismatch;
Joel Kitching92ea19a2019-11-05 18:36:42 +0800305static uint8_t workbuf[VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE]
306 __attribute__((aligned(VB2_WORKBUF_ALIGN)));
Joel Kitchingecdca932019-07-25 18:26:18 +0800307static struct vb2_context *ctx;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800308
Randall Spangler7c557082013-02-05 09:44:31 -0800309/**
310 * Reset mock data (for use before each test)
311 */
312static void ResetMocks(int i)
313{
Joel Kitchingecdca932019-07-25 18:26:18 +0800314 TEST_SUCC(vb2api_init(workbuf, sizeof(workbuf), &ctx),
315 "vb2api_init failed");
Randall Spanglerad7a7552016-11-22 12:58:22 -0800316
317 memset(VbApiKernelGetParams(), 0, sizeof(LoadKernelParams));
318
Randall Spangler664096b2016-10-13 16:16:41 -0700319 memset(&mock_disks, 0, sizeof(mock_disks));
Randall Spangler7c557082013-02-05 09:44:31 -0800320 load_kernel_calls = 0;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800321
Randall Spanglerdff58522017-11-27 15:37:13 -0800322 got_recovery_request_val = VB2_RECOVERY_NOT_REQUESTED;
Randall Spangler7c557082013-02-05 09:44:31 -0800323 got_find_disk = 0;
324 got_load_disk = 0;
325 got_return_val = 0xdeadbeef;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800326
Randall Spangler7c557082013-02-05 09:44:31 -0800327 t = test + i;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800328}
329
Julius Werner52fa8c12019-05-07 12:59:47 -0700330static int is_nonzero(const void *vptr, size_t count)
Randall Spangler7c557082013-02-05 09:44:31 -0800331{
332 const char *p = (const char *)vptr;
333 while (count--)
334 if (*p++)
335 return 1;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800336
Randall Spangler7c557082013-02-05 09:44:31 -0800337 return 0;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800338}
339
Bill Richardsonbf020a02012-01-25 14:06:24 -0800340/****************************************************************************/
341/* Mocked verification functions */
342
Joel Kitching90671fa2019-07-31 13:17:08 +0800343vb2_error_t VbExDiskGetInfo(VbDiskInfo **infos_ptr, uint32_t *count,
344 uint32_t disk_flags)
Randall Spangler7c557082013-02-05 09:44:31 -0800345{
346 int i;
347 int num_disks = 0;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800348
Randall Spangler21015892017-01-03 13:49:18 -0800349 VB2_DEBUG("My %s\n", __FUNCTION__);
Bill Richardsonbf020a02012-01-25 14:06:24 -0800350
Randall Spangler7c557082013-02-05 09:44:31 -0800351 *infos_ptr = mock_disks;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800352
Randall Spangler0714d9d2013-02-05 10:23:38 -0800353 for(i = 0; i < MAX_TEST_DISKS; i++) {
Randall Spangler7c557082013-02-05 09:44:31 -0800354 if (is_nonzero(&t->disks_to_provide[i],
355 sizeof(t->disks_to_provide[i]))) {
356 mock_disks[num_disks].bytes_per_lba =
357 t->disks_to_provide[i].bytes_per_lba;
358 mock_disks[num_disks].lba_count =
Dan Ehrenberg3f4d8d02014-12-02 08:21:57 -0800359 mock_disks[num_disks].streaming_lba_count =
Randall Spangler7c557082013-02-05 09:44:31 -0800360 t->disks_to_provide[i].lba_count;
361 mock_disks[num_disks].flags =
362 t->disks_to_provide[i].flags;
363 mock_disks[num_disks].handle = (VbExDiskHandle_t)
364 t->disks_to_provide[i].diskname;
Randall Spangler21015892017-01-03 13:49:18 -0800365 VB2_DEBUG(" mock_disk[%d] %" PRIu64 " %" PRIu64
Joel Kitching3c477452019-10-04 17:46:29 +0800366 " %#x %s\n", i,
Randall Spangler21015892017-01-03 13:49:18 -0800367 mock_disks[num_disks].bytes_per_lba,
368 mock_disks[num_disks].lba_count,
369 mock_disks[num_disks].flags,
370 (mock_disks[num_disks].handle
371 ? (char *)mock_disks[num_disks].handle
372 : "0"));
Randall Spangler7c557082013-02-05 09:44:31 -0800373 num_disks++;
374 } else {
375 mock_disks[num_disks].handle =
376 (VbExDiskHandle_t)"INVALID";
377 }
378 }
Bill Richardsonbf020a02012-01-25 14:06:24 -0800379
Randall Spangler7c557082013-02-05 09:44:31 -0800380 if (t->disk_count_to_return >= 0)
381 *count = t->disk_count_to_return;
382 else
383 *count = num_disks;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800384
Randall Spangler21015892017-01-03 13:49:18 -0800385 VB2_DEBUG(" *count=%" PRIu32 "\n", *count);
Joel Kitching3c477452019-10-04 17:46:29 +0800386 VB2_DEBUG(" return %#x\n", t->diskgetinfo_return_val);
Bill Richardsonbf020a02012-01-25 14:06:24 -0800387
Randall Spangler7c557082013-02-05 09:44:31 -0800388 return t->diskgetinfo_return_val;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800389}
390
Joel Kitching90671fa2019-07-31 13:17:08 +0800391vb2_error_t VbExDiskFreeInfo(VbDiskInfo *infos,
Joel Kitching9ad8a412018-08-02 16:21:17 +0800392 VbExDiskHandle_t preserve_handle)
Randall Spangler7c557082013-02-05 09:44:31 -0800393{
394 got_load_disk = (const char *)preserve_handle;
Randall Spangler21015892017-01-03 13:49:18 -0800395 VB2_DEBUG("%s(): got_load_disk = %s\n", __FUNCTION__,
396 got_load_disk ? got_load_disk : "0");
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800397 return VB2_SUCCESS;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800398}
399
Joel Kitching90671fa2019-07-31 13:17:08 +0800400vb2_error_t LoadKernel(struct vb2_context *c, LoadKernelParams *params)
Randall Spangler7c557082013-02-05 09:44:31 -0800401{
402 got_find_disk = (const char *)params->disk_handle;
Randall Spangler21015892017-01-03 13:49:18 -0800403 VB2_DEBUG("%s(%d): got_find_disk = %s\n", __FUNCTION__,
404 load_kernel_calls,
405 got_find_disk ? got_find_disk : "0");
Dan Ehrenberg3f4d8d02014-12-02 08:21:57 -0800406 if (t->external_expected[load_kernel_calls] !=
407 !!(params->boot_flags & BOOT_FLAG_EXTERNAL_GPT))
408 got_external_mismatch++;
Randall Spangler7c557082013-02-05 09:44:31 -0800409 return t->loadkernel_return_val[load_kernel_calls++];
Bill Richardsonbf020a02012-01-25 14:06:24 -0800410}
411
Julius Werner52fa8c12019-05-07 12:59:47 -0700412void vb2_nv_set(struct vb2_context *c,
Randall Spanglere4136dc2016-10-27 14:34:59 -0700413 enum vb2_nv_param param,
414 uint32_t value)
Randall Spangler7c557082013-02-05 09:44:31 -0800415{
Joel Kitchingf06f7552019-10-01 16:02:44 +0800416 if (param != VB2_NV_RECOVERY_REQUEST)
417 return;
Joel Kitching3c477452019-10-04 17:46:29 +0800418 VB2_DEBUG("%s(): got_recovery_request_val = %d (%#x)\n", __FUNCTION__,
Randall Spangler21015892017-01-03 13:49:18 -0800419 value, value);
Randall Spangler7c557082013-02-05 09:44:31 -0800420 got_recovery_request_val = value;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800421}
422
423/****************************************************************************/
424
Randall Spangler7c557082013-02-05 09:44:31 -0800425static void VbTryLoadKernelTest(void)
426{
427 int i;
428 int num_tests = sizeof(test) / sizeof(test[0]);
Bill Richardsonbf020a02012-01-25 14:06:24 -0800429
Randall Spangler0714d9d2013-02-05 10:23:38 -0800430 for (i = 0; i < num_tests; i++) {
431 printf("Test case: %s ...\n", test[i].name);
Randall Spangler7c557082013-02-05 09:44:31 -0800432 ResetMocks(i);
Joel Kitchingecdca932019-07-25 18:26:18 +0800433 TEST_EQ(VbTryLoadKernel(ctx, test[i].want_flags),
Randall Spangler0714d9d2013-02-05 10:23:38 -0800434 t->expected_return_val, " return value");
435 TEST_EQ(got_recovery_request_val,
436 t->expected_recovery_request_val, " recovery_request");
437 if (t->expected_to_find_disk != DONT_CARE) {
438 TEST_PTR_EQ(got_find_disk, t->expected_to_find_disk,
439 " find disk");
440 TEST_PTR_EQ(got_load_disk, t->expected_to_load_disk,
441 " load disk");
442 }
Dan Ehrenberg3f4d8d02014-12-02 08:21:57 -0800443 TEST_EQ(got_external_mismatch, 0, " external GPT errors");
Randall Spangler7c557082013-02-05 09:44:31 -0800444 }
Bill Richardsonbf020a02012-01-25 14:06:24 -0800445}
446
Randall Spangler7c557082013-02-05 09:44:31 -0800447int main(void)
448{
449 VbTryLoadKernelTest();
Bill Richardsonbf020a02012-01-25 14:06:24 -0800450
Randall Spangler7c557082013-02-05 09:44:31 -0800451 return gTestSuccess ? 0 : 255;
Bill Richardsonbf020a02012-01-25 14:06:24 -0800452}