blob: 11ff828e1ad36a7fff86ee0c3c0ed308e0d4d6d0 [file] [log] [blame]
Randall Spangler1b1998d2011-07-01 16:12:47 -07001/* Copyright (c) 2011 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 * High-level firmware wrapper API - entry points for kernel selection
6 */
7
8#include "gbb_header.h"
9#include "load_kernel_fw.h"
10#include "utility.h"
11#include "vboot_api.h"
12#include "vboot_common.h"
13#include "vboot_nvstorage.h"
14
15
16/* Global variables */
17static uint32_t disp_current_screen = VB_SCREEN_BLANK;
18static uint32_t disp_width = 0, disp_height = 0;
19static VbNvContext vnc;
20
21
22#ifdef CHROMEOS_ENVIRONMENT
23/* Global variable accessors for unit tests */
24VbNvContext* VbApiKernelGetVnc(void) {
25 return &vnc;
26}
27#endif
28
29
30/* Set recovery request */
31static void VbSetRecoveryRequest(uint32_t recovery_request) {
32 VBDEBUG(("VbSetRecoveryRequest(%d)\n", (int)recovery_request));
33
34 VbNvSetup(&vnc);
35 VbNvSet(&vnc, VBNV_RECOVERY_REQUEST, recovery_request);
36 VbNvTeardown(&vnc);
37 if (vnc.raw_changed)
38 VbExNvStorageWrite(vnc.raw);
39}
40
41
42/* Get the number of localizations in the GBB bitmap data. */
43static VbError_t VbGetLocalizationCount(VbCommonParams* cparams,
44 uint32_t* count) {
45 GoogleBinaryBlockHeader* gbb = (GoogleBinaryBlockHeader*)cparams->gbb_data;
46 BmpBlockHeader* hdr;
47
48 /* Default to 0 on error */
49 *count = 0;
50
51 /* Make sure the bitmap data is inside the GBB and is non-zero in size */
52 if (0 == gbb->bmpfv_size ||
53 gbb->bmpfv_offset > cparams->gbb_size ||
54 gbb->bmpfv_offset + gbb->bmpfv_size > cparams->gbb_size) {
55 return 1;
56 }
57
58 /* Sanity-check the bitmap block header */
59 hdr = (BmpBlockHeader *)(((uint8_t*)gbb) + gbb->bmpfv_offset);
60 if ((0 != Memcmp(hdr->signature, BMPBLOCK_SIGNATURE,
61 BMPBLOCK_SIGNATURE_SIZE)) ||
62 (hdr->major_version > BMPBLOCK_MAJOR_VERSION) ||
63 ((hdr->major_version == BMPBLOCK_MAJOR_VERSION) &&
64 (hdr->minor_version > BMPBLOCK_MINOR_VERSION))) {
65 return 1;
66 }
67
68 *count = hdr->number_of_localizations;
69 return VBERROR_SUCCESS;
70}
71
72
73/* Display a screen from the GBB. */
74static VbError_t VbDisplayScreenFromGBB(VbCommonParams* cparams,
75 uint32_t screen) {
76 GoogleBinaryBlockHeader* gbb = (GoogleBinaryBlockHeader*)cparams->gbb_data;
77 uint8_t* bmpfv = NULL;
78 BmpBlockHeader* hdr;
79 ScreenLayout* layout;
80 ImageInfo* image_info;
81 uint32_t screen_index;
82 uint32_t localization = 0;
83 VbError_t retval = 1; /* Assume error until proven successful */
84 uint32_t offset;
85 uint32_t i;
86
87 /* Make sure the bitmap data is inside the GBB and is non-zero in size */
88 if (0 == gbb->bmpfv_size ||
89 gbb->bmpfv_offset > cparams->gbb_size ||
90 gbb->bmpfv_offset + gbb->bmpfv_size > cparams->gbb_size) {
91 VBDEBUG(("VbDisplayScreenFromGBB(): invalid bmpfv offset/size\n"));
92 return 1;
93 }
94
95 /* Copy bitmap data from GBB into RAM for speed */
96 bmpfv = (uint8_t*)VbExMalloc(gbb->bmpfv_size);
97 Memcpy(bmpfv, ((uint8_t*)gbb) + gbb->bmpfv_offset, gbb->bmpfv_size);
98
99 /* Sanity-check the bitmap block header */
100 hdr = (BmpBlockHeader *)bmpfv;
101 if ((0 != Memcmp(hdr->signature, BMPBLOCK_SIGNATURE,
102 BMPBLOCK_SIGNATURE_SIZE)) ||
103 (hdr->major_version > BMPBLOCK_MAJOR_VERSION) ||
104 ((hdr->major_version == BMPBLOCK_MAJOR_VERSION) &&
105 (hdr->minor_version > BMPBLOCK_MINOR_VERSION))) {
106 VBDEBUG(("VbDisplayScreenFromGBB(): invalid/too new bitmap header\n"));
107 goto VbDisplayScreenFromGBB_exit;
108 }
109
110 /* Translate screen ID into index. Note that not all screens are in the
111 * GBB. */
112 /* TODO: ensure screen IDs match indices? Having this translation
113 * here is awful. */
114 switch (screen) {
115 case VB_SCREEN_DEVELOPER_WARNING:
116 screen_index = 0;
117 break;
118 case VB_SCREEN_RECOVERY_REMOVE:
119 screen_index = 1;
120 break;
121 case VB_SCREEN_RECOVERY_NO_GOOD:
122 screen_index = 2;
123 break;
124 case VB_SCREEN_RECOVERY_INSERT:
125 screen_index = 3;
126 break;
127 case VB_SCREEN_BLANK:
128 case VB_SCREEN_DEVELOPER_EGG:
129 default:
130 /* Screens which aren't in the GBB */
131 VBDEBUG(("VbDisplayScreenFromGBB(): screen %d not in the GBB\n",
132 (int)screen));
133 goto VbDisplayScreenFromGBB_exit;
134 }
135 if (screen_index >= hdr->number_of_screenlayouts) {
136 VBDEBUG(("VbDisplayScreenFromGBB(): screen %d index %d not in the GBB\n",
137 (int)screen, (int)screen_index));
138 goto VbDisplayScreenFromGBB_exit;
139 }
140
141 /* Clip localization to the number of localizations present in the GBB */
142 VbNvSetup(&vnc);
143 VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &localization);
144 if (localization >= hdr->number_of_localizations) {
145 localization = 0;
146 VbNvSet(&vnc, VBNV_LOCALIZATION_INDEX, localization);
147 }
148 VbNvTeardown(&vnc);
149 if (vnc.raw_changed)
150 VbExNvStorageWrite(vnc.raw);
151
152 /* Calculate offset of screen layout = start of screen stuff +
153 * correct locale + correct screen. */
154 offset = sizeof(BmpBlockHeader) +
155 localization * hdr->number_of_screenlayouts * sizeof(ScreenLayout) +
156 screen_index * sizeof(ScreenLayout);
157 VBDEBUG(("VbDisplayScreenFromGBB(): scr_%d_%d at offset 0x%x\n",
158 localization, screen_index, offset));
159 layout = (ScreenLayout*)(bmpfv + offset);
160
161 /* Display all bitmaps for the image */
162 for (i = 0; i < MAX_IMAGE_IN_LAYOUT; i++) {
163 if (layout->images[i].image_info_offset) {
164 offset = layout->images[i].image_info_offset;
165 image_info = (ImageInfo*)(bmpfv + offset);
166 VBDEBUG(("VbDisplayScreenFromGBB: image %d: %dx%d+%d+%d %d/%d"
167 "tag %d at 0x%x\n",
168 i, image_info->width, image_info->height,
169 layout->images[i].x, layout->images[i].y,
170 image_info->compressed_size, image_info->original_size,
171 image_info->tag, offset));
172
173 retval = VbExDisplayImage(layout->images[i].x, layout->images[i].y,
174 image_info, bmpfv + offset + sizeof(ImageInfo));
175 if (VBERROR_SUCCESS != retval)
176 goto VbDisplayScreenFromGBB_exit;
177 }
178 }
179
180 /* Successful if all bitmaps displayed */
181 retval = VBERROR_SUCCESS;
182
183VbDisplayScreenFromGBB_exit:
184
185 /* Free the bitmap data copy */
186 VbExFree(bmpfv);
187 return retval;
188}
189
190
191/* Display a screen, initializing the display if necessary. If force!=0,
192 * redisplays the screen even if it's the same as the current screen. */
193static VbError_t VbDisplayScreen(VbCommonParams* cparams, uint32_t screen,
194 int force) {
195
196 VBDEBUG(("VbDisplayScreen(%d, %d)\n", (int)screen, force));
197
198 /* Initialize display if necessary */
199 if (!disp_width) {
200 if (VBERROR_SUCCESS != VbExDisplayInit(&disp_width, &disp_height))
201 return 1;
202 }
203
204 /* If the requested screen is the same as the current one, we're done. */
205 if (disp_current_screen == screen && 0 == force)
206 return VBERROR_SUCCESS;
207
208 /* If the screen is blank, turn off the backlight; else turn it on. */
209 VbExDisplayBacklight(VB_SCREEN_BLANK == screen ? 0 : 1);
210
211 /* Request the screen */
212 disp_current_screen = screen;
213
214 /* Look in the GBB first */
215 if (VBERROR_SUCCESS == VbDisplayScreenFromGBB(cparams, screen))
216 return VBERROR_SUCCESS;
217
218 /* If the screen wasn't in the GBB bitmaps, fall back to a default screen. */
219 return VbExDisplayScreen(screen);
220}
221
Randall Spangler96191122011-07-08 14:01:54 -0700222#define DEBUG_INFO_SIZE 512
Randall Spangler1b1998d2011-07-01 16:12:47 -0700223
224static VbError_t VbCheckDisplayKey(VbCommonParams* cparams, uint32_t key) {
Randall Spangler96191122011-07-08 14:01:54 -0700225 VbSharedDataHeader* shared = (VbSharedDataHeader*)cparams->shared_data_blob;
226 GoogleBinaryBlockHeader* gbb = (GoogleBinaryBlockHeader*)cparams->gbb_data;
Randall Spangler1b1998d2011-07-01 16:12:47 -0700227
228 if ('\t' == key) {
229 /* Tab = display debug info */
Randall Spangler96191122011-07-08 14:01:54 -0700230 char buf[DEBUG_INFO_SIZE] = "";
231 uint32_t used = 0;
232 uint32_t i;
Randall Spangler1b1998d2011-07-01 16:12:47 -0700233
234 /* Redisplay the current screen, to overwrite any previous debug output */
235 VbDisplayScreen(cparams, disp_current_screen, 1);
236
Randall Spangler96191122011-07-08 14:01:54 -0700237 /* Add hardware ID */
238 used += Strncat(buf + used, "HWID: ", DEBUG_INFO_SIZE - used);
239 if (0 == gbb->hwid_size ||
240 gbb->hwid_offset > cparams->gbb_size ||
241 gbb->hwid_offset + gbb->hwid_size > cparams->gbb_size) {
242 VBDEBUG(("VbCheckDisplayKey(): invalid hwid offset/size\n"));
243 used += Strncat(buf + used, "(INVALID)", DEBUG_INFO_SIZE - used);
244 } else {
245 used += Strncat(buf + used, (char*)((uint8_t*)gbb + gbb->hwid_offset),
246 DEBUG_INFO_SIZE - used);
247 }
248
249 /* Add recovery request */
250 used += Strncat(buf + used, "\nrecovery_request: 0x",
251 DEBUG_INFO_SIZE - used);
252 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
253 shared->recovery_reason, 16, 2);
254
255 /* Add VbSharedData flags */
256 used += Strncat(buf + used, "\nVbSD.flags: 0x", DEBUG_INFO_SIZE - used);
257 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
258 shared->flags, 16, 8);
259
260 /* Add raw contents of VbNvStorage */
261 used += Strncat(buf + used, "\nVbNv.raw:", DEBUG_INFO_SIZE - used);
262 for (i = 0; i < VBNV_BLOCK_SIZE; i++) {
263 used += Strncat(buf + used, " ", DEBUG_INFO_SIZE - used);
264 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
265 vnc.raw[i], 16, 2);
266 }
267
268 used += Strncat(buf + used, "\n", DEBUG_INFO_SIZE - used);
269
270 /* TODO: add more interesting data:
271 * - TPM firmware and kernel versions. In the current code, they're
272 * only filled into VbSharedData by LoadFirmware() and LoadKernel(), and
273 * since neither of those is called in the recovery path this isn't
274 * feasible yet.
275 * - SHA1 of kernel subkey (assuming we always set it in VbSelectFirmware,
276 * even in recovery mode, where we just copy it from the root key)
Randall Spangler1b1998d2011-07-01 16:12:47 -0700277 * - Information on current disks
Randall Spangler96191122011-07-08 14:01:54 -0700278 * - Anything else interesting from VbNvStorage */
279
280 buf[DEBUG_INFO_SIZE - 1] = '\0';
281 VBDEBUG(("VbCheckDisplayKey() wants to show '%s'\n", buf));
282 return VbExDisplayDebugInfo(buf);
Randall Spangler1b1998d2011-07-01 16:12:47 -0700283
284 } else if (VB_KEY_LEFT == key || VB_KEY_RIGHT == key) {
285 /* Arrow keys = change localization */
286 uint32_t loc = 0;
287 uint32_t count = 0;
288
289 /* Get localization count */
290 VbGetLocalizationCount(cparams, &count);
291
292 /* Change localization */
293 VbNvSetup(&vnc);
294 VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &loc);
295 if (VB_KEY_RIGHT == key)
296 loc = (loc < count - 1 ? loc + 1 : 0);
297 else
298 loc = (loc > 0 ? loc - 1 : count - 1);
299 VBDEBUG(("VbCheckDisplayKey() - change localization to %d\n", (int)loc));
300 VbNvSet(&vnc, VBNV_LOCALIZATION_INDEX, loc);
301 VbNvTeardown(&vnc);
302 if (vnc.raw_changed)
303 VbExNvStorageWrite(vnc.raw);
304
305 /* Force redraw of current screen */
306 return VbDisplayScreen(cparams, disp_current_screen, 1);
307 }
308
309 return VBERROR_SUCCESS;
310}
311
312
313/* Return codes fof VbTryLoadKernel, in addition to VBERROR_SUCCESS */
314enum VbTryLoadKernelError_t {
315 /* No disks found */
316 VBERROR_TRY_LOAD_NO_DISKS = 1,
317 /* Need to reboot to same mode/recovery reason as this boot */
318 VBERROR_TRY_LOAD_REBOOT = 2,
319 /* Some other error; go to recovery mode if this was the only hope to boot */
320 VBERROR_TRY_LOAD_RECOVERY = 3,
321};
322
323
324/* Attempt loading a kernel from the specified type(s) of disks. If
325 * successful, sets p->disk_handle to the disk for the kernel. See
326 * VBERROR_TRY_LOAD_* for additional return codes. */
327uint32_t VbTryLoadKernel(VbCommonParams* cparams, LoadKernelParams* p,
328 uint32_t get_info_flags) {
329 VbSharedDataHeader* shared = (VbSharedDataHeader*)cparams->shared_data_blob;
330 int retval = VBERROR_TRY_LOAD_NO_DISKS;
331 VbDiskInfo* disk_info = NULL;
332 uint32_t disk_count = 0;
333 uint32_t i;
334
335 VBDEBUG(("VbTryLoadKernel() start, get_info_flags=0x%x\n",
336 (int)get_info_flags));
337
338 p->disk_handle = NULL;
339
340 /* Find disks */
341 if (VBERROR_SUCCESS != VbExDiskGetInfo(&disk_info, &disk_count,
342 get_info_flags))
343 disk_count = 0;
344
345 VBDEBUG(("VbTryLoadKernel() found %d disks\n", (int)disk_count));
346 if (0 == disk_count) {
347 VbSetRecoveryRequest(VBNV_RECOVERY_RW_NO_DISK);
348 return VBERROR_TRY_LOAD_NO_DISKS;
349 }
350
351 /* Loop over disks */
352 for (i = 0; i < disk_count; i++) {
353 VBDEBUG(("VbTryLoadKernel() trying disk %d\n", (int)i));
354 p->disk_handle = disk_info[i].handle;
355 p->bytes_per_lba = disk_info[i].bytes_per_lba;
356 p->ending_lba = disk_info[i].lba_count - 1;
357 retval = LoadKernel(p);
358 VBDEBUG(("VbTryLoadKernel() LoadKernel() returned %d\n", retval));
359
360 /* Stop now if we found a kernel or we need to reboot */
361 /* TODO: If recovery requested, should track the farthest we get, instead
362 * of just returning the value from the last disk attempted. */
363 if (LOAD_KERNEL_SUCCESS == retval || LOAD_KERNEL_REBOOT == retval)
364 break;
365 }
366
367 /* If we didn't succeed, don't return a disk handle */
368 if (LOAD_KERNEL_SUCCESS != retval)
369 p->disk_handle = NULL;
370
371 VbExDiskFreeInfo(disk_info, p->disk_handle);
372
373 /* Translate return codes */
374 switch (retval) {
375 case LOAD_KERNEL_SUCCESS:
376 return VBERROR_SUCCESS;
377 case LOAD_KERNEL_REBOOT:
378 /* Reboot to same mode, so reuse the current recovery reason */
379 VbSetRecoveryRequest(shared->recovery_reason);
380 return VBERROR_TRY_LOAD_REBOOT;
381 case LOAD_KERNEL_NOT_FOUND:
382 VbSetRecoveryRequest(VBNV_RECOVERY_RW_NO_OS);
383 return VBERROR_TRY_LOAD_RECOVERY;
384 case LOAD_KERNEL_INVALID:
385 VbSetRecoveryRequest(VBNV_RECOVERY_RW_INVALID_OS);
386 return VBERROR_TRY_LOAD_RECOVERY;
387 case LOAD_KERNEL_RECOVERY:
388 return VBERROR_TRY_LOAD_RECOVERY;
389 default:
390 VbSetRecoveryRequest(VBNV_RECOVERY_RW_UNSPECIFIED);
391 return VBERROR_TRY_LOAD_RECOVERY;
392 }
393}
394
395
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700396/* Handle a normal boot. */
Randall Spangler1b1998d2011-07-01 16:12:47 -0700397VbError_t VbBootNormal(VbCommonParams* cparams, LoadKernelParams* p) {
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700398
399 /* Force dev_boot_usb flag disabled. This ensures the flag will be
400 * initially disabled if the user later transitions back into
401 * developer mode. */
402 VbNvSet(&vnc, VBNV_DEV_BOOT_USB, 0);
403
404 /* Boot from fixed disk only */
Randall Spangler1b1998d2011-07-01 16:12:47 -0700405 return VbTryLoadKernel(cparams, p, VB_DISK_FLAG_FIXED);
406}
407
408
Randall Spangler1b1998d2011-07-01 16:12:47 -0700409/* Developer mode delays. All must be multiples of DEV_DELAY_INCREMENT */
410#define DEV_DELAY_INCREMENT 250 /* Delay each loop, in msec */
411#define DEV_DELAY_BEEP1 20000 /* Beep for first time at this time */
412#define DEV_DELAY_BEEP2 21000 /* Beep for second time at this time */
413#define DEV_DELAY_TIMEOUT 30000 /* Give up at this time */
414
415/* Handle a developer-mode boot */
416VbError_t VbBootDeveloper(VbCommonParams* cparams, LoadKernelParams* p) {
417 uint32_t delay_time = 0;
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700418 uint32_t allow_usb = 0;
419
420 /* Check if USB booting is allowed */
421 VbNvGet(&vnc, VBNV_DEV_BOOT_USB, &allow_usb);
Randall Spangler1b1998d2011-07-01 16:12:47 -0700422
423 /* Show the dev mode warning screen */
424 VbDisplayScreen(cparams, VB_SCREEN_DEVELOPER_WARNING, 0);
425
426 /* Loop for dev mode warning delay */
427 for (delay_time = 0; delay_time < DEV_DELAY_TIMEOUT;
428 delay_time += DEV_DELAY_INCREMENT) {
429 uint32_t key;
430
431 if (VbExIsShutdownRequested())
432 return 1;
433
434 if (DEV_DELAY_BEEP1 == delay_time || DEV_DELAY_BEEP2 == delay_time)
435 VbExBeep(DEV_DELAY_INCREMENT, 400);
436 else
437 VbExSleepMs(DEV_DELAY_INCREMENT);
438
439 /* Handle keypress */
440 key = VbExKeyboardRead();
441 switch (key) {
442 case '\r':
443 case ' ':
444 case 0x1B:
445 /* Enter, space, or ESC = reboot to recovery */
446 VBDEBUG(("VbBootDeveloper() - user pressed ENTER/SPACE/ESC"));
447 VbSetRecoveryRequest(VBNV_RECOVERY_RW_DEV_SCREEN);
448 return 1;
449 case 0x04:
450 /* Ctrl+D = dismiss warning; advance to timeout */
451 VBDEBUG(("VbBootDeveloper() - user pressed Ctrl+D; skip delay\n"));
452 delay_time = DEV_DELAY_TIMEOUT;
453 break;
454 case 0x15:
455 /* Ctrl+U = try USB boot, or beep if failure */
456 VBDEBUG(("VbBootDeveloper() - user pressed Ctrl+U; try USB\n"));
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700457 if (!allow_usb) {
458 VBDEBUG(("VbBootDeveloper() - USB booting is disabled\n"));
459 VbExBeep(DEV_DELAY_INCREMENT, 400);
460 } else if (VBERROR_SUCCESS ==
461 VbTryLoadKernel(cparams, p, VB_DISK_FLAG_REMOVABLE)) {
Randall Spangler1b1998d2011-07-01 16:12:47 -0700462 VBDEBUG(("VbBootDeveloper() - booting USB\n"));
463 return VBERROR_SUCCESS;
464 } else {
465 VBDEBUG(("VbBootDeveloper() - no kernel found on USB\n"));
466 VbExBeep(DEV_DELAY_INCREMENT, 400);
467 }
468 break;
469 default:
470 VbCheckDisplayKey(cparams, key);
471 break;
472 /* TODO: xyzzy easter egg check */
473 }
474 }
475
476 /* Timeout or Ctrl+D; attempt loading from fixed disk */
477 VBDEBUG(("VbBootDeveloper() - trying fixed disk\n"));
478 return VbTryLoadKernel(cparams, p, VB_DISK_FLAG_FIXED);
479}
480
Randall Spangler1b1998d2011-07-01 16:12:47 -0700481
482/* Delay between disk checks in recovery mode */
483#define REC_DELAY_INCREMENT 250
484
485/* Handle a recovery-mode boot */
486VbError_t VbBootRecovery(VbCommonParams* cparams, LoadKernelParams* p) {
487 VbSharedDataHeader* shared = (VbSharedDataHeader*)cparams->shared_data_blob;
488 uint32_t retval;
489 int i;
490
491 VBDEBUG(("VbBootRecovery() start\n"));
492
493 /* If dev mode switch is off, require removal of all external media. */
494 if (!(shared->flags & VBSD_BOOT_DEV_SWITCH_ON)) {
495 VbDiskInfo* disk_info = NULL;
496 uint32_t disk_count = 0;
497
498 VBDEBUG(("VbBootRecovery() forcing device removal\n"));
499
500 while (1) {
501 if (VBERROR_SUCCESS != VbExDiskGetInfo(&disk_info, &disk_count,
502 VB_DISK_FLAG_REMOVABLE))
503 disk_count = 0;
504 VbExDiskFreeInfo(disk_info, NULL);
505
506 if (0 == disk_count) {
507 VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0);
508 break;
509 }
510
511 VBDEBUG(("VbBootRecovery() waiting for %d disks to be removed\n",
512 (int)disk_count));
513
514 VbDisplayScreen(cparams, VB_SCREEN_RECOVERY_REMOVE, 0);
515
516 /* Scan keyboard more frequently than media, since x86 platforms
517 * don't like to scan USB too rapidly. */
518 for (i = 0; i < 4; i++) {
519 VbCheckDisplayKey(cparams, VbExKeyboardRead());
520 if (VbExIsShutdownRequested())
521 return 1;
522 VbExSleepMs(REC_DELAY_INCREMENT);
523 }
524 }
525 }
526
527 /* Loop and wait for a recovery image */
528 while (1) {
529 VBDEBUG(("VbBootRecovery() attempting to load kernel\n"));
530 retval = VbTryLoadKernel(cparams, p, VB_DISK_FLAG_REMOVABLE);
531
532 if (VBERROR_SUCCESS == retval)
533 break; /* Found a recovery kernel */
534 else if (VBERROR_TRY_LOAD_REBOOT == retval)
535 return 1; /* Must reboot (back into recovery mode) */
536
537 VbDisplayScreen(cparams, VBERROR_TRY_LOAD_NO_DISKS == retval ?
538 VB_SCREEN_RECOVERY_INSERT : VB_SCREEN_RECOVERY_NO_GOOD, 0);
539
540 /* Scan keyboard more frequently than media, since x86 platforms don't like
541 * to scan USB too rapidly. */
542 for (i = 0; i < 4; i++) {
543 VbCheckDisplayKey(cparams, VbExKeyboardRead());
544 if (VbExIsShutdownRequested())
545 return 1;
546 VbExSleepMs(REC_DELAY_INCREMENT);
547 }
548 }
549
550 return VBERROR_SUCCESS;
551}
552
553
554VbError_t VbSelectAndLoadKernel(VbCommonParams* cparams,
555 VbSelectAndLoadKernelParams* kparams) {
556 VbSharedDataHeader* shared = (VbSharedDataHeader*)cparams->shared_data_blob;
Randall Spangler64ca7882011-07-11 15:46:19 -0700557 VbError_t retval = VBERROR_SUCCESS;
Randall Spangler1b1998d2011-07-01 16:12:47 -0700558 LoadKernelParams p;
559
560 VBDEBUG(("VbSelectAndLoadKernel() start\n"));
561
Randall Spangler96191122011-07-08 14:01:54 -0700562 /* Start timer */
563 shared->timer_vb_select_and_load_kernel_enter = VbExGetTimer();
564
Randall Spangler1b1998d2011-07-01 16:12:47 -0700565 VbExNvStorageRead(vnc.raw);
566 vnc.raw_changed = 0;
567
568 /* Clear output params in case we fail */
569 kparams->disk_handle = NULL;
570 kparams->partition_number = 0;
571 kparams->bootloader_address = 0;
572 kparams->bootloader_size = 0;
573 Memset(kparams->partition_guid, 0, sizeof(kparams->partition_guid));
574
575 /* Fill in params for calls to LoadKernel() */
576 p.shared_data_blob = cparams->shared_data_blob;
577 p.shared_data_size = cparams->shared_data_size;
578 p.gbb_data = cparams->gbb_data;
579 p.gbb_size = cparams->gbb_size;
580 p.kernel_buffer = kparams->kernel_buffer;
581 p.kernel_buffer_size = kparams->kernel_buffer_size;
582 p.nv_context = &vnc;
583 p.boot_flags = 0;
584 if (shared->flags & VBSD_BOOT_DEV_SWITCH_ON)
585 p.boot_flags |= BOOT_FLAG_DEVELOPER;
586
Randall Spangler64ca7882011-07-11 15:46:19 -0700587 /* Handle separate normal and developer firmware builds. */
588#if defined(VBOOT_FIRMWARE_TYPE_NORMAL)
589 /* Normal-type firmware always acts like the dev switch is off. */
590 p.boot_flags &= ~BOOT_FLAG_DEVELOPER;
591#elif defined(VBOOT_FIRMWARE_TYPE_DEVELOPER)
592 /* Developer-type firmware fails if the dev switch is off. */
593 if (!(p.boot_flags & BOOT_FLAG_DEVELOPER)) {
594 /* Dev firmware should be signed with a key that only verifies
595 * when the dev switch is on, so we should never get here. */
596 VBDEBUG(("Developer firmware called with dev switch off!\n"));
597 VbSetRecoveryRequest(VBNV_RECOVERY_RW_DEV_MISMATCH);
598 retval = 1;
599 }
600#else
601 /* Recovery firmware, or merged normal+developer firmware. No
602 * need to override flags. */
603#endif
604
Randall Spangler1b1998d2011-07-01 16:12:47 -0700605 /* Select boot path */
Randall Spangler64ca7882011-07-11 15:46:19 -0700606 if (VBERROR_SUCCESS != retval) {
607 /* Failure during setup; don't attempt booting a kernel */
608 } else if (shared->recovery_reason) {
Randall Spangler1b1998d2011-07-01 16:12:47 -0700609 /* Recovery boot */
610 p.boot_flags |= BOOT_FLAG_RECOVERY;
611 retval = VbBootRecovery(cparams, &p);
612 VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0);
Randall Spangler64ca7882011-07-11 15:46:19 -0700613 } else if (p.boot_flags & BOOT_FLAG_DEVELOPER) {
Randall Spangler1b1998d2011-07-01 16:12:47 -0700614 /* Developer boot */
Randall Spangler1b1998d2011-07-01 16:12:47 -0700615 retval = VbBootDeveloper(cparams, &p);
616 VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0);
Randall Spangler64ca7882011-07-11 15:46:19 -0700617 } else {
Randall Spangler1b1998d2011-07-01 16:12:47 -0700618 /* Normal boot */
619 retval = VbBootNormal(cparams, &p);
Randall Spangler1b1998d2011-07-01 16:12:47 -0700620 }
621
622 if (VBERROR_SUCCESS == retval) {
623 /* Save disk parameters */
624 kparams->disk_handle = p.disk_handle;
625 kparams->partition_number = (uint32_t)p.partition_number;
626 kparams->bootloader_address = p.bootloader_address;
627 kparams->bootloader_size = (uint32_t)p.bootloader_size;
628 Memcpy(kparams->partition_guid, p.partition_guid,
629 sizeof(kparams->partition_guid));
630
631 /* Since we did find something to boot, clear recovery request, if any,
632 * resulting from disk checks during developer or recovery mode. */
633 VbSetRecoveryRequest(VBNV_RECOVERY_NOT_REQUESTED);
634 }
635
636 if (vnc.raw_changed)
637 VbExNvStorageWrite(vnc.raw);
638
Randall Spangler96191122011-07-08 14:01:54 -0700639 /* Stop timer */
640 shared->timer_vb_select_and_load_kernel_exit = VbExGetTimer();
641
Randall Spangler1b1998d2011-07-01 16:12:47 -0700642 VBDEBUG(("VbSelectAndLoadKernel() returning %d\n", (int)retval));
643
644 /* Pass through return value from boot path */
645 return retval;
646}