blob: 03ea188697109f7a344170a88b63a41728cdc64f [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 Spangler43101b42011-07-19 09:45:00 -0700222
Randall Spangler96191122011-07-08 14:01:54 -0700223#define DEBUG_INFO_SIZE 512
Randall Spangler1b1998d2011-07-01 16:12:47 -0700224
Randall Spangler43101b42011-07-19 09:45:00 -0700225/* Display debug info to the screen */
226static VbError_t VbDisplayDebugInfo(VbCommonParams* cparams) {
Randall Spangler96191122011-07-08 14:01:54 -0700227 VbSharedDataHeader* shared = (VbSharedDataHeader*)cparams->shared_data_blob;
228 GoogleBinaryBlockHeader* gbb = (GoogleBinaryBlockHeader*)cparams->gbb_data;
Randall Spangler43101b42011-07-19 09:45:00 -0700229 char buf[DEBUG_INFO_SIZE] = "";
230 uint32_t used = 0;
231 uint32_t i;
232
233 /* Redisplay the current screen, to overwrite any previous debug output */
234 VbDisplayScreen(cparams, disp_current_screen, 1);
235
236 /* Add hardware ID */
237 used += Strncat(buf + used, "HWID: ", DEBUG_INFO_SIZE - used);
238 if (0 == gbb->hwid_size ||
239 gbb->hwid_offset > cparams->gbb_size ||
240 gbb->hwid_offset + gbb->hwid_size > cparams->gbb_size) {
241 VBDEBUG(("VbCheckDisplayKey(): invalid hwid offset/size\n"));
242 used += Strncat(buf + used, "(INVALID)", DEBUG_INFO_SIZE - used);
243 } else {
244 used += Strncat(buf + used, (char*)((uint8_t*)gbb + gbb->hwid_offset),
245 DEBUG_INFO_SIZE - used);
246 }
247
248 /* Add recovery reason */
249 used += Strncat(buf + used, "\nrecovery_reason: 0x", DEBUG_INFO_SIZE - used);
250 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
251 shared->recovery_reason, 16, 2);
252
253 /* Add VbSharedData flags */
254 used += Strncat(buf + used, "\nVbSD.flags: 0x", DEBUG_INFO_SIZE - used);
255 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
256 shared->flags, 16, 8);
257
258 /* Add raw contents of VbNvStorage */
259 used += Strncat(buf + used, "\nVbNv.raw:", DEBUG_INFO_SIZE - used);
260 for (i = 0; i < VBNV_BLOCK_SIZE; i++) {
261 used += Strncat(buf + used, " ", DEBUG_INFO_SIZE - used);
262 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
263 vnc.raw[i], 16, 2);
264 }
265
266 /* Add dev_boot_usb flag */
267 VbNvGet(&vnc, VBNV_DEV_BOOT_USB, &i);
268 used += Strncat(buf + used, "\ndev_boot_usb: ", DEBUG_INFO_SIZE - used);
269 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0);
270
271 /* Make sure we finish with a newline */
272 used += Strncat(buf + used, "\n", DEBUG_INFO_SIZE - used);
273
274 /* TODO: add more interesting data:
275 * - TPM firmware and kernel versions. In the current code, they're
276 * only filled into VbSharedData by LoadFirmware() and LoadKernel(), and
277 * since neither of those is called in the recovery path this isn't
278 * feasible yet.
279 * - SHA1 of kernel subkey (assuming we always set it in VbSelectFirmware,
280 * even in recovery mode, where we just copy it from the root key)
281 * - Information on current disks
282 * - Anything else interesting from VbNvStorage */
283
284 buf[DEBUG_INFO_SIZE - 1] = '\0';
285 VBDEBUG(("VbCheckDisplayKey() wants to show '%s'\n", buf));
286 return VbExDisplayDebugInfo(buf);
287}
288
289
290static VbError_t VbCheckDisplayKey(VbCommonParams* cparams, uint32_t key) {
Randall Spangler1b1998d2011-07-01 16:12:47 -0700291
292 if ('\t' == key) {
293 /* Tab = display debug info */
Randall Spangler43101b42011-07-19 09:45:00 -0700294 return VbDisplayDebugInfo(cparams);
Randall Spangler1b1998d2011-07-01 16:12:47 -0700295
296 } else if (VB_KEY_LEFT == key || VB_KEY_RIGHT == key) {
297 /* Arrow keys = change localization */
298 uint32_t loc = 0;
299 uint32_t count = 0;
300
301 /* Get localization count */
302 VbGetLocalizationCount(cparams, &count);
303
304 /* Change localization */
305 VbNvSetup(&vnc);
306 VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &loc);
307 if (VB_KEY_RIGHT == key)
308 loc = (loc < count - 1 ? loc + 1 : 0);
309 else
310 loc = (loc > 0 ? loc - 1 : count - 1);
311 VBDEBUG(("VbCheckDisplayKey() - change localization to %d\n", (int)loc));
312 VbNvSet(&vnc, VBNV_LOCALIZATION_INDEX, loc);
313 VbNvTeardown(&vnc);
314 if (vnc.raw_changed)
315 VbExNvStorageWrite(vnc.raw);
316
317 /* Force redraw of current screen */
318 return VbDisplayScreen(cparams, disp_current_screen, 1);
319 }
320
321 return VBERROR_SUCCESS;
322}
323
324
325/* Return codes fof VbTryLoadKernel, in addition to VBERROR_SUCCESS */
326enum VbTryLoadKernelError_t {
327 /* No disks found */
328 VBERROR_TRY_LOAD_NO_DISKS = 1,
329 /* Need to reboot to same mode/recovery reason as this boot */
330 VBERROR_TRY_LOAD_REBOOT = 2,
331 /* Some other error; go to recovery mode if this was the only hope to boot */
332 VBERROR_TRY_LOAD_RECOVERY = 3,
333};
334
335
336/* Attempt loading a kernel from the specified type(s) of disks. If
337 * successful, sets p->disk_handle to the disk for the kernel. See
338 * VBERROR_TRY_LOAD_* for additional return codes. */
339uint32_t VbTryLoadKernel(VbCommonParams* cparams, LoadKernelParams* p,
340 uint32_t get_info_flags) {
341 VbSharedDataHeader* shared = (VbSharedDataHeader*)cparams->shared_data_blob;
342 int retval = VBERROR_TRY_LOAD_NO_DISKS;
343 VbDiskInfo* disk_info = NULL;
344 uint32_t disk_count = 0;
345 uint32_t i;
346
347 VBDEBUG(("VbTryLoadKernel() start, get_info_flags=0x%x\n",
348 (int)get_info_flags));
349
350 p->disk_handle = NULL;
351
352 /* Find disks */
353 if (VBERROR_SUCCESS != VbExDiskGetInfo(&disk_info, &disk_count,
354 get_info_flags))
355 disk_count = 0;
356
357 VBDEBUG(("VbTryLoadKernel() found %d disks\n", (int)disk_count));
358 if (0 == disk_count) {
359 VbSetRecoveryRequest(VBNV_RECOVERY_RW_NO_DISK);
360 return VBERROR_TRY_LOAD_NO_DISKS;
361 }
362
363 /* Loop over disks */
364 for (i = 0; i < disk_count; i++) {
365 VBDEBUG(("VbTryLoadKernel() trying disk %d\n", (int)i));
366 p->disk_handle = disk_info[i].handle;
367 p->bytes_per_lba = disk_info[i].bytes_per_lba;
368 p->ending_lba = disk_info[i].lba_count - 1;
369 retval = LoadKernel(p);
370 VBDEBUG(("VbTryLoadKernel() LoadKernel() returned %d\n", retval));
371
372 /* Stop now if we found a kernel or we need to reboot */
373 /* TODO: If recovery requested, should track the farthest we get, instead
374 * of just returning the value from the last disk attempted. */
375 if (LOAD_KERNEL_SUCCESS == retval || LOAD_KERNEL_REBOOT == retval)
376 break;
377 }
378
379 /* If we didn't succeed, don't return a disk handle */
380 if (LOAD_KERNEL_SUCCESS != retval)
381 p->disk_handle = NULL;
382
383 VbExDiskFreeInfo(disk_info, p->disk_handle);
384
385 /* Translate return codes */
386 switch (retval) {
387 case LOAD_KERNEL_SUCCESS:
388 return VBERROR_SUCCESS;
389 case LOAD_KERNEL_REBOOT:
390 /* Reboot to same mode, so reuse the current recovery reason */
391 VbSetRecoveryRequest(shared->recovery_reason);
392 return VBERROR_TRY_LOAD_REBOOT;
393 case LOAD_KERNEL_NOT_FOUND:
394 VbSetRecoveryRequest(VBNV_RECOVERY_RW_NO_OS);
395 return VBERROR_TRY_LOAD_RECOVERY;
396 case LOAD_KERNEL_INVALID:
397 VbSetRecoveryRequest(VBNV_RECOVERY_RW_INVALID_OS);
398 return VBERROR_TRY_LOAD_RECOVERY;
399 case LOAD_KERNEL_RECOVERY:
400 return VBERROR_TRY_LOAD_RECOVERY;
401 default:
402 VbSetRecoveryRequest(VBNV_RECOVERY_RW_UNSPECIFIED);
403 return VBERROR_TRY_LOAD_RECOVERY;
404 }
405}
406
407
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700408/* Handle a normal boot. */
Randall Spangler1b1998d2011-07-01 16:12:47 -0700409VbError_t VbBootNormal(VbCommonParams* cparams, LoadKernelParams* p) {
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700410
411 /* Force dev_boot_usb flag disabled. This ensures the flag will be
412 * initially disabled if the user later transitions back into
413 * developer mode. */
414 VbNvSet(&vnc, VBNV_DEV_BOOT_USB, 0);
415
416 /* Boot from fixed disk only */
Randall Spangler1b1998d2011-07-01 16:12:47 -0700417 return VbTryLoadKernel(cparams, p, VB_DISK_FLAG_FIXED);
418}
419
420
Randall Spangler1b1998d2011-07-01 16:12:47 -0700421/* Developer mode delays. All must be multiples of DEV_DELAY_INCREMENT */
422#define DEV_DELAY_INCREMENT 250 /* Delay each loop, in msec */
423#define DEV_DELAY_BEEP1 20000 /* Beep for first time at this time */
424#define DEV_DELAY_BEEP2 21000 /* Beep for second time at this time */
425#define DEV_DELAY_TIMEOUT 30000 /* Give up at this time */
426
427/* Handle a developer-mode boot */
428VbError_t VbBootDeveloper(VbCommonParams* cparams, LoadKernelParams* p) {
429 uint32_t delay_time = 0;
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700430 uint32_t allow_usb = 0;
431
432 /* Check if USB booting is allowed */
433 VbNvGet(&vnc, VBNV_DEV_BOOT_USB, &allow_usb);
Randall Spangler1b1998d2011-07-01 16:12:47 -0700434
435 /* Show the dev mode warning screen */
436 VbDisplayScreen(cparams, VB_SCREEN_DEVELOPER_WARNING, 0);
437
438 /* Loop for dev mode warning delay */
439 for (delay_time = 0; delay_time < DEV_DELAY_TIMEOUT;
440 delay_time += DEV_DELAY_INCREMENT) {
441 uint32_t key;
442
443 if (VbExIsShutdownRequested())
444 return 1;
445
446 if (DEV_DELAY_BEEP1 == delay_time || DEV_DELAY_BEEP2 == delay_time)
447 VbExBeep(DEV_DELAY_INCREMENT, 400);
448 else
449 VbExSleepMs(DEV_DELAY_INCREMENT);
450
451 /* Handle keypress */
452 key = VbExKeyboardRead();
453 switch (key) {
454 case '\r':
455 case ' ':
456 case 0x1B:
457 /* Enter, space, or ESC = reboot to recovery */
458 VBDEBUG(("VbBootDeveloper() - user pressed ENTER/SPACE/ESC"));
459 VbSetRecoveryRequest(VBNV_RECOVERY_RW_DEV_SCREEN);
460 return 1;
461 case 0x04:
462 /* Ctrl+D = dismiss warning; advance to timeout */
463 VBDEBUG(("VbBootDeveloper() - user pressed Ctrl+D; skip delay\n"));
464 delay_time = DEV_DELAY_TIMEOUT;
465 break;
466 case 0x15:
467 /* Ctrl+U = try USB boot, or beep if failure */
468 VBDEBUG(("VbBootDeveloper() - user pressed Ctrl+U; try USB\n"));
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700469 if (!allow_usb) {
470 VBDEBUG(("VbBootDeveloper() - USB booting is disabled\n"));
Randall Spangler43101b42011-07-19 09:45:00 -0700471 VbExBeep(DEV_DELAY_INCREMENT / 2, 400);
472 VbExSleepMs(DEV_DELAY_INCREMENT / 2);
473 VbExBeep(DEV_DELAY_INCREMENT / 2, 400);
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700474 } else if (VBERROR_SUCCESS ==
475 VbTryLoadKernel(cparams, p, VB_DISK_FLAG_REMOVABLE)) {
Randall Spangler1b1998d2011-07-01 16:12:47 -0700476 VBDEBUG(("VbBootDeveloper() - booting USB\n"));
477 return VBERROR_SUCCESS;
478 } else {
479 VBDEBUG(("VbBootDeveloper() - no kernel found on USB\n"));
480 VbExBeep(DEV_DELAY_INCREMENT, 400);
Randall Spanglerad334852011-07-19 15:52:43 -0700481 /* Clear recovery requests from failed kernel loading, so
482 * that powering off at this point doesn't put us into
483 * recovery mode. */
484 VbSetRecoveryRequest(VBNV_RECOVERY_NOT_REQUESTED);
Randall Spangler1b1998d2011-07-01 16:12:47 -0700485 }
486 break;
487 default:
488 VbCheckDisplayKey(cparams, key);
489 break;
490 /* TODO: xyzzy easter egg check */
491 }
492 }
493
494 /* Timeout or Ctrl+D; attempt loading from fixed disk */
495 VBDEBUG(("VbBootDeveloper() - trying fixed disk\n"));
496 return VbTryLoadKernel(cparams, p, VB_DISK_FLAG_FIXED);
497}
498
Randall Spangler1b1998d2011-07-01 16:12:47 -0700499
500/* Delay between disk checks in recovery mode */
501#define REC_DELAY_INCREMENT 250
502
503/* Handle a recovery-mode boot */
504VbError_t VbBootRecovery(VbCommonParams* cparams, LoadKernelParams* p) {
505 VbSharedDataHeader* shared = (VbSharedDataHeader*)cparams->shared_data_blob;
506 uint32_t retval;
507 int i;
508
509 VBDEBUG(("VbBootRecovery() start\n"));
510
511 /* If dev mode switch is off, require removal of all external media. */
512 if (!(shared->flags & VBSD_BOOT_DEV_SWITCH_ON)) {
513 VbDiskInfo* disk_info = NULL;
514 uint32_t disk_count = 0;
515
516 VBDEBUG(("VbBootRecovery() forcing device removal\n"));
517
518 while (1) {
519 if (VBERROR_SUCCESS != VbExDiskGetInfo(&disk_info, &disk_count,
520 VB_DISK_FLAG_REMOVABLE))
521 disk_count = 0;
522 VbExDiskFreeInfo(disk_info, NULL);
523
524 if (0 == disk_count) {
525 VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0);
526 break;
527 }
528
529 VBDEBUG(("VbBootRecovery() waiting for %d disks to be removed\n",
530 (int)disk_count));
531
532 VbDisplayScreen(cparams, VB_SCREEN_RECOVERY_REMOVE, 0);
533
534 /* Scan keyboard more frequently than media, since x86 platforms
535 * don't like to scan USB too rapidly. */
536 for (i = 0; i < 4; i++) {
537 VbCheckDisplayKey(cparams, VbExKeyboardRead());
538 if (VbExIsShutdownRequested())
539 return 1;
540 VbExSleepMs(REC_DELAY_INCREMENT);
541 }
542 }
543 }
544
545 /* Loop and wait for a recovery image */
546 while (1) {
547 VBDEBUG(("VbBootRecovery() attempting to load kernel\n"));
548 retval = VbTryLoadKernel(cparams, p, VB_DISK_FLAG_REMOVABLE);
549
Randall Spanglerad334852011-07-19 15:52:43 -0700550 /* Clear recovery requests from failed kernel loading, since we're
551 * already in recovery mode. Do this now, so that powering off after
552 * inserting an invalid disk doesn't leave us stuck in recovery mode. */
553 VbSetRecoveryRequest(VBNV_RECOVERY_NOT_REQUESTED);
554
Randall Spangler1b1998d2011-07-01 16:12:47 -0700555 if (VBERROR_SUCCESS == retval)
556 break; /* Found a recovery kernel */
557 else if (VBERROR_TRY_LOAD_REBOOT == retval)
558 return 1; /* Must reboot (back into recovery mode) */
559
560 VbDisplayScreen(cparams, VBERROR_TRY_LOAD_NO_DISKS == retval ?
561 VB_SCREEN_RECOVERY_INSERT : VB_SCREEN_RECOVERY_NO_GOOD, 0);
562
563 /* Scan keyboard more frequently than media, since x86 platforms don't like
564 * to scan USB too rapidly. */
565 for (i = 0; i < 4; i++) {
566 VbCheckDisplayKey(cparams, VbExKeyboardRead());
567 if (VbExIsShutdownRequested())
568 return 1;
569 VbExSleepMs(REC_DELAY_INCREMENT);
570 }
571 }
572
573 return VBERROR_SUCCESS;
574}
575
576
577VbError_t VbSelectAndLoadKernel(VbCommonParams* cparams,
578 VbSelectAndLoadKernelParams* kparams) {
579 VbSharedDataHeader* shared = (VbSharedDataHeader*)cparams->shared_data_blob;
Randall Spangler64ca7882011-07-11 15:46:19 -0700580 VbError_t retval = VBERROR_SUCCESS;
Randall Spangler1b1998d2011-07-01 16:12:47 -0700581 LoadKernelParams p;
582
583 VBDEBUG(("VbSelectAndLoadKernel() start\n"));
584
Randall Spangler96191122011-07-08 14:01:54 -0700585 /* Start timer */
586 shared->timer_vb_select_and_load_kernel_enter = VbExGetTimer();
587
Randall Spangler1b1998d2011-07-01 16:12:47 -0700588 VbExNvStorageRead(vnc.raw);
589 vnc.raw_changed = 0;
590
591 /* Clear output params in case we fail */
592 kparams->disk_handle = NULL;
593 kparams->partition_number = 0;
594 kparams->bootloader_address = 0;
595 kparams->bootloader_size = 0;
596 Memset(kparams->partition_guid, 0, sizeof(kparams->partition_guid));
597
598 /* Fill in params for calls to LoadKernel() */
Randall Spangler43101b42011-07-19 09:45:00 -0700599 Memset(&p, 0, sizeof(p));
Randall Spangler1b1998d2011-07-01 16:12:47 -0700600 p.shared_data_blob = cparams->shared_data_blob;
601 p.shared_data_size = cparams->shared_data_size;
602 p.gbb_data = cparams->gbb_data;
603 p.gbb_size = cparams->gbb_size;
604 p.kernel_buffer = kparams->kernel_buffer;
605 p.kernel_buffer_size = kparams->kernel_buffer_size;
606 p.nv_context = &vnc;
607 p.boot_flags = 0;
608 if (shared->flags & VBSD_BOOT_DEV_SWITCH_ON)
609 p.boot_flags |= BOOT_FLAG_DEVELOPER;
610
Randall Spangler64ca7882011-07-11 15:46:19 -0700611 /* Handle separate normal and developer firmware builds. */
612#if defined(VBOOT_FIRMWARE_TYPE_NORMAL)
613 /* Normal-type firmware always acts like the dev switch is off. */
614 p.boot_flags &= ~BOOT_FLAG_DEVELOPER;
615#elif defined(VBOOT_FIRMWARE_TYPE_DEVELOPER)
616 /* Developer-type firmware fails if the dev switch is off. */
617 if (!(p.boot_flags & BOOT_FLAG_DEVELOPER)) {
618 /* Dev firmware should be signed with a key that only verifies
619 * when the dev switch is on, so we should never get here. */
620 VBDEBUG(("Developer firmware called with dev switch off!\n"));
621 VbSetRecoveryRequest(VBNV_RECOVERY_RW_DEV_MISMATCH);
622 retval = 1;
623 }
624#else
625 /* Recovery firmware, or merged normal+developer firmware. No
626 * need to override flags. */
627#endif
628
Randall Spangler1b1998d2011-07-01 16:12:47 -0700629 /* Select boot path */
Randall Spangler64ca7882011-07-11 15:46:19 -0700630 if (VBERROR_SUCCESS != retval) {
631 /* Failure during setup; don't attempt booting a kernel */
632 } else if (shared->recovery_reason) {
Randall Spangler1b1998d2011-07-01 16:12:47 -0700633 /* Recovery boot */
634 p.boot_flags |= BOOT_FLAG_RECOVERY;
635 retval = VbBootRecovery(cparams, &p);
636 VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0);
Randall Spangler64ca7882011-07-11 15:46:19 -0700637 } else if (p.boot_flags & BOOT_FLAG_DEVELOPER) {
Randall Spangler1b1998d2011-07-01 16:12:47 -0700638 /* Developer boot */
Randall Spangler1b1998d2011-07-01 16:12:47 -0700639 retval = VbBootDeveloper(cparams, &p);
640 VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0);
Randall Spangler64ca7882011-07-11 15:46:19 -0700641 } else {
Randall Spangler1b1998d2011-07-01 16:12:47 -0700642 /* Normal boot */
643 retval = VbBootNormal(cparams, &p);
Randall Spangler1b1998d2011-07-01 16:12:47 -0700644 }
645
646 if (VBERROR_SUCCESS == retval) {
647 /* Save disk parameters */
648 kparams->disk_handle = p.disk_handle;
649 kparams->partition_number = (uint32_t)p.partition_number;
650 kparams->bootloader_address = p.bootloader_address;
651 kparams->bootloader_size = (uint32_t)p.bootloader_size;
652 Memcpy(kparams->partition_guid, p.partition_guid,
653 sizeof(kparams->partition_guid));
Randall Spangler1b1998d2011-07-01 16:12:47 -0700654 }
655
656 if (vnc.raw_changed)
657 VbExNvStorageWrite(vnc.raw);
658
Randall Spangler96191122011-07-08 14:01:54 -0700659 /* Stop timer */
660 shared->timer_vb_select_and_load_kernel_exit = VbExGetTimer();
661
Randall Spangler1b1998d2011-07-01 16:12:47 -0700662 VBDEBUG(("VbSelectAndLoadKernel() returning %d\n", (int)retval));
663
664 /* Pass through return value from boot path */
665 return retval;
666}