blob: 1afac378637695f0325e3ebd3f5d586959e4bdac [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
222
223static VbError_t VbCheckDisplayKey(VbCommonParams* cparams, uint32_t key) {
224
225 if ('\t' == key) {
226 /* Tab = display debug info */
227
228 /* Redisplay the current screen, to overwrite any previous debug output */
229 VbDisplayScreen(cparams, disp_current_screen, 1);
230
231 /* TODO: add real data:
232 * - HWID
233 * - Current recovery request
234 * - Boot flags
235 * - Information on current disks
236 * - Anything else interesting from cparams and/or nvram
237 *
238 * TODO: Add a VbExSnprintf() function for this? */
239 return VbExDisplayDebugInfo("Testing 1 2 3\nTesting 4 5 6\n");
240
241 } else if (VB_KEY_LEFT == key || VB_KEY_RIGHT == key) {
242 /* Arrow keys = change localization */
243 uint32_t loc = 0;
244 uint32_t count = 0;
245
246 /* Get localization count */
247 VbGetLocalizationCount(cparams, &count);
248
249 /* Change localization */
250 VbNvSetup(&vnc);
251 VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &loc);
252 if (VB_KEY_RIGHT == key)
253 loc = (loc < count - 1 ? loc + 1 : 0);
254 else
255 loc = (loc > 0 ? loc - 1 : count - 1);
256 VBDEBUG(("VbCheckDisplayKey() - change localization to %d\n", (int)loc));
257 VbNvSet(&vnc, VBNV_LOCALIZATION_INDEX, loc);
258 VbNvTeardown(&vnc);
259 if (vnc.raw_changed)
260 VbExNvStorageWrite(vnc.raw);
261
262 /* Force redraw of current screen */
263 return VbDisplayScreen(cparams, disp_current_screen, 1);
264 }
265
266 return VBERROR_SUCCESS;
267}
268
269
270/* Return codes fof VbTryLoadKernel, in addition to VBERROR_SUCCESS */
271enum VbTryLoadKernelError_t {
272 /* No disks found */
273 VBERROR_TRY_LOAD_NO_DISKS = 1,
274 /* Need to reboot to same mode/recovery reason as this boot */
275 VBERROR_TRY_LOAD_REBOOT = 2,
276 /* Some other error; go to recovery mode if this was the only hope to boot */
277 VBERROR_TRY_LOAD_RECOVERY = 3,
278};
279
280
281/* Attempt loading a kernel from the specified type(s) of disks. If
282 * successful, sets p->disk_handle to the disk for the kernel. See
283 * VBERROR_TRY_LOAD_* for additional return codes. */
284uint32_t VbTryLoadKernel(VbCommonParams* cparams, LoadKernelParams* p,
285 uint32_t get_info_flags) {
286 VbSharedDataHeader* shared = (VbSharedDataHeader*)cparams->shared_data_blob;
287 int retval = VBERROR_TRY_LOAD_NO_DISKS;
288 VbDiskInfo* disk_info = NULL;
289 uint32_t disk_count = 0;
290 uint32_t i;
291
292 VBDEBUG(("VbTryLoadKernel() start, get_info_flags=0x%x\n",
293 (int)get_info_flags));
294
295 p->disk_handle = NULL;
296
297 /* Find disks */
298 if (VBERROR_SUCCESS != VbExDiskGetInfo(&disk_info, &disk_count,
299 get_info_flags))
300 disk_count = 0;
301
302 VBDEBUG(("VbTryLoadKernel() found %d disks\n", (int)disk_count));
303 if (0 == disk_count) {
304 VbSetRecoveryRequest(VBNV_RECOVERY_RW_NO_DISK);
305 return VBERROR_TRY_LOAD_NO_DISKS;
306 }
307
308 /* Loop over disks */
309 for (i = 0; i < disk_count; i++) {
310 VBDEBUG(("VbTryLoadKernel() trying disk %d\n", (int)i));
311 p->disk_handle = disk_info[i].handle;
312 p->bytes_per_lba = disk_info[i].bytes_per_lba;
313 p->ending_lba = disk_info[i].lba_count - 1;
314 retval = LoadKernel(p);
315 VBDEBUG(("VbTryLoadKernel() LoadKernel() returned %d\n", retval));
316
317 /* Stop now if we found a kernel or we need to reboot */
318 /* TODO: If recovery requested, should track the farthest we get, instead
319 * of just returning the value from the last disk attempted. */
320 if (LOAD_KERNEL_SUCCESS == retval || LOAD_KERNEL_REBOOT == retval)
321 break;
322 }
323
324 /* If we didn't succeed, don't return a disk handle */
325 if (LOAD_KERNEL_SUCCESS != retval)
326 p->disk_handle = NULL;
327
328 VbExDiskFreeInfo(disk_info, p->disk_handle);
329
330 /* Translate return codes */
331 switch (retval) {
332 case LOAD_KERNEL_SUCCESS:
333 return VBERROR_SUCCESS;
334 case LOAD_KERNEL_REBOOT:
335 /* Reboot to same mode, so reuse the current recovery reason */
336 VbSetRecoveryRequest(shared->recovery_reason);
337 return VBERROR_TRY_LOAD_REBOOT;
338 case LOAD_KERNEL_NOT_FOUND:
339 VbSetRecoveryRequest(VBNV_RECOVERY_RW_NO_OS);
340 return VBERROR_TRY_LOAD_RECOVERY;
341 case LOAD_KERNEL_INVALID:
342 VbSetRecoveryRequest(VBNV_RECOVERY_RW_INVALID_OS);
343 return VBERROR_TRY_LOAD_RECOVERY;
344 case LOAD_KERNEL_RECOVERY:
345 return VBERROR_TRY_LOAD_RECOVERY;
346 default:
347 VbSetRecoveryRequest(VBNV_RECOVERY_RW_UNSPECIFIED);
348 return VBERROR_TRY_LOAD_RECOVERY;
349 }
350}
351
352
353/* Handle a normal boot from fixed drive only. */
354VbError_t VbBootNormal(VbCommonParams* cparams, LoadKernelParams* p) {
355 return VbTryLoadKernel(cparams, p, VB_DISK_FLAG_FIXED);
356}
357
358
359#ifdef BUILD_FVDEVELOPER
360/* Developer mode delays. All must be multiples of DEV_DELAY_INCREMENT */
361#define DEV_DELAY_INCREMENT 250 /* Delay each loop, in msec */
362#define DEV_DELAY_BEEP1 20000 /* Beep for first time at this time */
363#define DEV_DELAY_BEEP2 21000 /* Beep for second time at this time */
364#define DEV_DELAY_TIMEOUT 30000 /* Give up at this time */
365
366/* Handle a developer-mode boot */
367VbError_t VbBootDeveloper(VbCommonParams* cparams, LoadKernelParams* p) {
368 uint32_t delay_time = 0;
369
370 /* Show the dev mode warning screen */
371 VbDisplayScreen(cparams, VB_SCREEN_DEVELOPER_WARNING, 0);
372
373 /* Loop for dev mode warning delay */
374 for (delay_time = 0; delay_time < DEV_DELAY_TIMEOUT;
375 delay_time += DEV_DELAY_INCREMENT) {
376 uint32_t key;
377
378 if (VbExIsShutdownRequested())
379 return 1;
380
381 if (DEV_DELAY_BEEP1 == delay_time || DEV_DELAY_BEEP2 == delay_time)
382 VbExBeep(DEV_DELAY_INCREMENT, 400);
383 else
384 VbExSleepMs(DEV_DELAY_INCREMENT);
385
386 /* Handle keypress */
387 key = VbExKeyboardRead();
388 switch (key) {
389 case '\r':
390 case ' ':
391 case 0x1B:
392 /* Enter, space, or ESC = reboot to recovery */
393 VBDEBUG(("VbBootDeveloper() - user pressed ENTER/SPACE/ESC"));
394 VbSetRecoveryRequest(VBNV_RECOVERY_RW_DEV_SCREEN);
395 return 1;
396 case 0x04:
397 /* Ctrl+D = dismiss warning; advance to timeout */
398 VBDEBUG(("VbBootDeveloper() - user pressed Ctrl+D; skip delay\n"));
399 delay_time = DEV_DELAY_TIMEOUT;
400 break;
401 case 0x15:
402 /* Ctrl+U = try USB boot, or beep if failure */
403 VBDEBUG(("VbBootDeveloper() - user pressed Ctrl+U; try USB\n"));
404 if (VBERROR_SUCCESS == VbTryLoadKernel(cparams, p,
405 VB_DISK_FLAG_REMOVABLE)) {
406 VBDEBUG(("VbBootDeveloper() - booting USB\n"));
407 return VBERROR_SUCCESS;
408 } else {
409 VBDEBUG(("VbBootDeveloper() - no kernel found on USB\n"));
410 VbExBeep(DEV_DELAY_INCREMENT, 400);
411 }
412 break;
413 default:
414 VbCheckDisplayKey(cparams, key);
415 break;
416 /* TODO: xyzzy easter egg check */
417 }
418 }
419
420 /* Timeout or Ctrl+D; attempt loading from fixed disk */
421 VBDEBUG(("VbBootDeveloper() - trying fixed disk\n"));
422 return VbTryLoadKernel(cparams, p, VB_DISK_FLAG_FIXED);
423}
424
425#endif /* BUILD_FVDEVELOPER */
426
427
428/* Delay between disk checks in recovery mode */
429#define REC_DELAY_INCREMENT 250
430
431/* Handle a recovery-mode boot */
432VbError_t VbBootRecovery(VbCommonParams* cparams, LoadKernelParams* p) {
433 VbSharedDataHeader* shared = (VbSharedDataHeader*)cparams->shared_data_blob;
434 uint32_t retval;
435 int i;
436
437 VBDEBUG(("VbBootRecovery() start\n"));
438
439 /* If dev mode switch is off, require removal of all external media. */
440 if (!(shared->flags & VBSD_BOOT_DEV_SWITCH_ON)) {
441 VbDiskInfo* disk_info = NULL;
442 uint32_t disk_count = 0;
443
444 VBDEBUG(("VbBootRecovery() forcing device removal\n"));
445
446 while (1) {
447 if (VBERROR_SUCCESS != VbExDiskGetInfo(&disk_info, &disk_count,
448 VB_DISK_FLAG_REMOVABLE))
449 disk_count = 0;
450 VbExDiskFreeInfo(disk_info, NULL);
451
452 if (0 == disk_count) {
453 VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0);
454 break;
455 }
456
457 VBDEBUG(("VbBootRecovery() waiting for %d disks to be removed\n",
458 (int)disk_count));
459
460 VbDisplayScreen(cparams, VB_SCREEN_RECOVERY_REMOVE, 0);
461
462 /* Scan keyboard more frequently than media, since x86 platforms
463 * don't like to scan USB too rapidly. */
464 for (i = 0; i < 4; i++) {
465 VbCheckDisplayKey(cparams, VbExKeyboardRead());
466 if (VbExIsShutdownRequested())
467 return 1;
468 VbExSleepMs(REC_DELAY_INCREMENT);
469 }
470 }
471 }
472
473 /* Loop and wait for a recovery image */
474 while (1) {
475 VBDEBUG(("VbBootRecovery() attempting to load kernel\n"));
476 retval = VbTryLoadKernel(cparams, p, VB_DISK_FLAG_REMOVABLE);
477
478 if (VBERROR_SUCCESS == retval)
479 break; /* Found a recovery kernel */
480 else if (VBERROR_TRY_LOAD_REBOOT == retval)
481 return 1; /* Must reboot (back into recovery mode) */
482
483 VbDisplayScreen(cparams, VBERROR_TRY_LOAD_NO_DISKS == retval ?
484 VB_SCREEN_RECOVERY_INSERT : VB_SCREEN_RECOVERY_NO_GOOD, 0);
485
486 /* Scan keyboard more frequently than media, since x86 platforms don't like
487 * to scan USB too rapidly. */
488 for (i = 0; i < 4; i++) {
489 VbCheckDisplayKey(cparams, VbExKeyboardRead());
490 if (VbExIsShutdownRequested())
491 return 1;
492 VbExSleepMs(REC_DELAY_INCREMENT);
493 }
494 }
495
496 return VBERROR_SUCCESS;
497}
498
499
500VbError_t VbSelectAndLoadKernel(VbCommonParams* cparams,
501 VbSelectAndLoadKernelParams* kparams) {
502 VbSharedDataHeader* shared = (VbSharedDataHeader*)cparams->shared_data_blob;
503 VbError_t retval;
504 LoadKernelParams p;
505
506 VBDEBUG(("VbSelectAndLoadKernel() start\n"));
507
508 VbExNvStorageRead(vnc.raw);
509 vnc.raw_changed = 0;
510
511 /* Clear output params in case we fail */
512 kparams->disk_handle = NULL;
513 kparams->partition_number = 0;
514 kparams->bootloader_address = 0;
515 kparams->bootloader_size = 0;
516 Memset(kparams->partition_guid, 0, sizeof(kparams->partition_guid));
517
518 /* Fill in params for calls to LoadKernel() */
519 p.shared_data_blob = cparams->shared_data_blob;
520 p.shared_data_size = cparams->shared_data_size;
521 p.gbb_data = cparams->gbb_data;
522 p.gbb_size = cparams->gbb_size;
523 p.kernel_buffer = kparams->kernel_buffer;
524 p.kernel_buffer_size = kparams->kernel_buffer_size;
525 p.nv_context = &vnc;
526 p.boot_flags = 0;
527 if (shared->flags & VBSD_BOOT_DEV_SWITCH_ON)
528 p.boot_flags |= BOOT_FLAG_DEVELOPER;
529
530 /* Select boot path */
531 if (shared->recovery_reason) {
532 /* Recovery boot */
533 p.boot_flags |= BOOT_FLAG_RECOVERY;
534 retval = VbBootRecovery(cparams, &p);
535 VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0);
536 } else {
537 /* TODO: vboot compiler define for developer mode; this is the H2C one */
538#ifdef BUILD_FVDEVELOPER
539 /* Developer boot */
540 p.boot_flags |= BOOT_FLAG_DEV_FIRMWARE;
541 retval = VbBootDeveloper(cparams, &p);
542 VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0);
543#else
544 /* Normal boot */
545 retval = VbBootNormal(cparams, &p);
546#endif
547 }
548
549 if (VBERROR_SUCCESS == retval) {
550 /* Save disk parameters */
551 kparams->disk_handle = p.disk_handle;
552 kparams->partition_number = (uint32_t)p.partition_number;
553 kparams->bootloader_address = p.bootloader_address;
554 kparams->bootloader_size = (uint32_t)p.bootloader_size;
555 Memcpy(kparams->partition_guid, p.partition_guid,
556 sizeof(kparams->partition_guid));
557
558 /* Since we did find something to boot, clear recovery request, if any,
559 * resulting from disk checks during developer or recovery mode. */
560 VbSetRecoveryRequest(VBNV_RECOVERY_NOT_REQUESTED);
561 }
562
563 if (vnc.raw_changed)
564 VbExNvStorageWrite(vnc.raw);
565
566 VBDEBUG(("VbSelectAndLoadKernel() returning %d\n", (int)retval));
567
568 /* Pass through return value from boot path */
569 return retval;
570}