blob: c66af8dfc1bfeb6360889b4bbc4d76e0a9d0cd47 [file] [log] [blame]
Randall Spangler0bf64052013-01-28 15:00:56 -08001/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
Bill Richardson822eca62011-08-22 14:06:38 -07002 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Display functions used in kernel selection.
6 */
7
Bill Richardson0c3ba242013-03-29 11:09:30 -07008#include "sysincludes.h"
9
Bill Richardson0a9977e2011-08-22 16:03:59 -070010#include "bmpblk_font.h"
Simon Glass527ba812013-07-25 08:48:47 -060011#include "gbb_access.h"
Bill Richardson822eca62011-08-22 14:06:38 -070012#include "gbb_header.h"
Simon Glass527ba812013-07-25 08:48:47 -060013#include "region.h"
Bill Richardson822eca62011-08-22 14:06:38 -070014#include "utility.h"
15#include "vboot_api.h"
16#include "vboot_common.h"
17#include "vboot_display.h"
18#include "vboot_nvstorage.h"
19
20static uint32_t disp_current_screen = VB_SCREEN_BLANK;
21static uint32_t disp_width = 0, disp_height = 0;
22
Randall Spangler0bf64052013-01-28 15:00:56 -080023VbError_t VbGetLocalizationCount(VbCommonParams *cparams, uint32_t *count)
24{
Simon Glass527ba812013-07-25 08:48:47 -060025 BmpBlockHeader hdr;
26 VbError_t ret;
Bill Richardson822eca62011-08-22 14:06:38 -070027
Randall Spangler0bf64052013-01-28 15:00:56 -080028 /* Default to 0 on error */
29 *count = 0;
Bill Richardson822eca62011-08-22 14:06:38 -070030
Simon Glass527ba812013-07-25 08:48:47 -060031 ret = VbGbbReadBmpHeader(cparams, &hdr);
32 if (ret)
33 return ret;
Bill Richardson822eca62011-08-22 14:06:38 -070034
Simon Glass527ba812013-07-25 08:48:47 -060035 *count = hdr.number_of_localizations;
Randall Spangler0bf64052013-01-28 15:00:56 -080036 return VBERROR_SUCCESS;
Bill Richardson822eca62011-08-22 14:06:38 -070037}
38
Randall Spangler0bf64052013-01-28 15:00:56 -080039/*
40 * TODO: We could cache the font info to speed things up, by making the
Bill Richardson0a9977e2011-08-22 16:03:59 -070041 * in-memory font structure distinct from the in-flash version. We'll do that
42 * Real Soon Now. Until then, we just repeat the same linear search every time.
43 */
Bill Richardson0a9977e2011-08-22 16:03:59 -070044
Randall Spanglerfe510c02013-02-06 17:10:55 -080045VbFont_t *VbInternalizeFontData(FontArrayHeader *fonthdr)
Randall Spangler0bf64052013-01-28 15:00:56 -080046{
47 /* Just return the raw data pointer for now. */
48 return (VbFont_t *)fonthdr;
Bill Richardson0a9977e2011-08-22 16:03:59 -070049}
50
Randall Spanglerfe510c02013-02-06 17:10:55 -080051void VbDoneWithFontForNow(VbFont_t *ptr)
Randall Spangler0bf64052013-01-28 15:00:56 -080052{
53 /* Nothing. */
Bill Richardson0a9977e2011-08-22 16:03:59 -070054}
55
Randall Spanglerfe510c02013-02-06 17:10:55 -080056ImageInfo *VbFindFontGlyph(VbFont_t *font, uint32_t ascii,
57 void **bufferptr, uint32_t *buffersize)
Randall Spangler0bf64052013-01-28 15:00:56 -080058{
59 uint8_t *ptr, *firstptr;
60 uint32_t max;
61 uint32_t i;
62 FontArrayEntryHeader *entry;
Bill Richardson0a9977e2011-08-22 16:03:59 -070063
Randall Spangler0bf64052013-01-28 15:00:56 -080064 ptr = (uint8_t *)font;
65 max = ((FontArrayHeader *)ptr)->num_entries;
66 ptr += sizeof(FontArrayHeader);
67 firstptr = ptr;
Bill Richardson0a9977e2011-08-22 16:03:59 -070068
Randall Spangler0bf64052013-01-28 15:00:56 -080069 /*
70 * Simple linear search.
71 *
72 * Note: We're assuming glpyhs are uncompressed. That's true because
73 * the bmpblk_font tool doesn't compress anything. The bmpblk_utility
74 * does, but it compresses the entire font blob at once, and we've
75 * already uncompressed that before we got here.
76 */
77 for(i=0; i<max; i++) {
78 entry = (FontArrayEntryHeader *)ptr;
79 if (entry->ascii == ascii) {
80 *bufferptr = ptr + sizeof(FontArrayEntryHeader);
81 *buffersize = entry->info.original_size;
82 return &(entry->info);
83 }
84 ptr += sizeof(FontArrayEntryHeader)+entry->info.compressed_size;
85 }
Bill Richardson0a9977e2011-08-22 16:03:59 -070086
Randall Spangler0bf64052013-01-28 15:00:56 -080087 /*
88 * We must return something valid. We'll just use the first glyph in
89 * the font structure (so it should be something distinct).
90 */
91 entry = (FontArrayEntryHeader *)firstptr;
92 *bufferptr = firstptr + sizeof(FontArrayEntryHeader);
93 *buffersize = entry->info.original_size;
94 return &(entry->info);
Bill Richardson0a9977e2011-08-22 16:03:59 -070095}
96
Aaron Durbin9d7d0cb2013-02-26 09:02:02 -060097void VbRenderTextAtPos(const char *text, int right_to_left,
Randall Spanglerfe510c02013-02-06 17:10:55 -080098 uint32_t x, uint32_t y, VbFont_t *font)
Randall Spangler0bf64052013-01-28 15:00:56 -080099{
100 int i;
101 ImageInfo *image_info = 0;
102 void *buffer;
103 uint32_t buffersize;
104 uint32_t cur_x = x, cur_y = y;
Bill Richardson0a9977e2011-08-22 16:03:59 -0700105
Randall Spangler0bf64052013-01-28 15:00:56 -0800106 if (!text || !font) {
107 VBDEBUG((" VbRenderTextAtPos: invalid args\n"));
108 return;
109 }
Bill Richardson0a9977e2011-08-22 16:03:59 -0700110
Randall Spangler0bf64052013-01-28 15:00:56 -0800111 for (i=0; text[i]; i++) {
Bill Richardson0a9977e2011-08-22 16:03:59 -0700112
Randall Spangler0bf64052013-01-28 15:00:56 -0800113 if (text[i] == '\n') {
114 if (!image_info)
115 image_info = VbFindFontGlyph(font, text[i],
116 &buffer,
117 &buffersize);
118 cur_x = x;
119 cur_y += image_info->height;
120 continue;
121 }
Bill Richardson0a9977e2011-08-22 16:03:59 -0700122
Randall Spangler0bf64052013-01-28 15:00:56 -0800123 image_info = VbFindFontGlyph(font, text[i], &buffer,
124 &buffersize);
Bill Richardson0a9977e2011-08-22 16:03:59 -0700125
Randall Spangler0bf64052013-01-28 15:00:56 -0800126 if (right_to_left)
127 cur_x -= image_info->width;
Bill Richardson0a9977e2011-08-22 16:03:59 -0700128
Randall Spangler0bf64052013-01-28 15:00:56 -0800129 if (VBERROR_SUCCESS != VbExDisplayImage(cur_x, cur_y, buffer,
130 buffersize)) {
131 VBDEBUG((" VbRenderTextAtPos: "
132 "can't display ascii 0x%x\n", text[i]));
133 }
Bill Richardson0a9977e2011-08-22 16:03:59 -0700134
Randall Spangler0bf64052013-01-28 15:00:56 -0800135 if (!right_to_left)
136 cur_x += image_info->width;
137 }
Bill Richardson0a9977e2011-08-22 16:03:59 -0700138}
139
Randall Spangler0bf64052013-01-28 15:00:56 -0800140VbError_t VbDisplayScreenFromGBB(VbCommonParams *cparams, uint32_t screen,
141 VbNvContext *vncptr)
142{
Simon Glass527ba812013-07-25 08:48:47 -0600143 char *fullimage = NULL;
144 BmpBlockHeader hdr;
Randall Spangler0bf64052013-01-28 15:00:56 -0800145 uint32_t screen_index;
146 uint32_t localization = 0;
147 VbError_t retval = VBERROR_UNKNOWN; /* Assume error until proven ok */
148 uint32_t inoutsize;
Randall Spangler0bf64052013-01-28 15:00:56 -0800149 uint32_t i;
150 VbFont_t *font;
Aaron Durbin9d7d0cb2013-02-26 09:02:02 -0600151 const char *text_to_show;
Randall Spangler0bf64052013-01-28 15:00:56 -0800152 int rtol = 0;
Simon Glass527ba812013-07-25 08:48:47 -0600153 VbError_t ret;
Bill Richardson822eca62011-08-22 14:06:38 -0700154
Simon Glass527ba812013-07-25 08:48:47 -0600155 ret = VbGbbReadBmpHeader(cparams, &hdr);
156 if (ret)
157 return ret;
Bill Richardson822eca62011-08-22 14:06:38 -0700158
Randall Spangler0bf64052013-01-28 15:00:56 -0800159 /*
160 * Translate screen ID into index. Note that not all screens are in
161 * the GBB.
162 *
163 * TODO: ensure screen IDs match indices? Having this translation here
164 * is awful.
165 */
166 switch (screen) {
167 case VB_SCREEN_DEVELOPER_WARNING:
168 screen_index = SCREEN_DEVELOPER_WARNING;
169 break;
170 case VB_SCREEN_RECOVERY_REMOVE:
171 screen_index = SCREEN_RECOVERY_REMOVE;
172 break;
173 case VB_SCREEN_RECOVERY_NO_GOOD:
174 screen_index = SCREEN_RECOVERY_NO_GOOD;
175 break;
176 case VB_SCREEN_RECOVERY_INSERT:
177 screen_index = SCREEN_RECOVERY_INSERT;
178 break;
179 case VB_SCREEN_RECOVERY_TO_DEV:
180 screen_index = SCREEN_RECOVERY_TO_DEV;
181 break;
182 case VB_SCREEN_DEVELOPER_TO_NORM:
183 screen_index = SCREEN_DEVELOPER_TO_NORM;
184 break;
185 case VB_SCREEN_WAIT:
186 screen_index = SCREEN_WAIT;
187 break;
188 case VB_SCREEN_TO_NORM_CONFIRMED:
189 screen_index = SCREEN_TO_NORM_CONFIRMED;
190 break;
191 case VB_SCREEN_BLANK:
192 case VB_SCREEN_DEVELOPER_EGG:
193 default:
194 /* Screens which aren't in the GBB */
195 VBDEBUG(("VbDisplayScreenFromGBB(): screen %d not in the GBB\n",
196 (int)screen));
197 retval = VBERROR_INVALID_SCREEN_INDEX;
198 goto VbDisplayScreenFromGBB_exit;
199 }
Bill Richardson822eca62011-08-22 14:06:38 -0700200
Simon Glass527ba812013-07-25 08:48:47 -0600201 if (screen_index >= hdr.number_of_screenlayouts) {
Randall Spangler0bf64052013-01-28 15:00:56 -0800202 VBDEBUG(("VbDisplayScreenFromGBB(): "
203 "screen %d index %d not in the GBB\n",
204 (int)screen, (int)screen_index));
205 retval = VBERROR_INVALID_SCREEN_INDEX;
206 goto VbDisplayScreenFromGBB_exit;
207 }
Bill Richardson822eca62011-08-22 14:06:38 -0700208
Randall Spangler0bf64052013-01-28 15:00:56 -0800209 /* Clip localization to number of localizations present in the GBB */
210 VbNvGet(vncptr, VBNV_LOCALIZATION_INDEX, &localization);
Simon Glass527ba812013-07-25 08:48:47 -0600211 if (localization >= hdr.number_of_localizations) {
Randall Spangler0bf64052013-01-28 15:00:56 -0800212 localization = 0;
213 VbNvSet(vncptr, VBNV_LOCALIZATION_INDEX, localization);
Bill Richardsonb64f0972014-05-28 15:49:23 -0700214 VbNvSet(vncptr, VBNV_BACKUP_NVRAM_REQUEST, 1);
Randall Spangler0bf64052013-01-28 15:00:56 -0800215 }
Bill Richardson822eca62011-08-22 14:06:38 -0700216
Randall Spangler0bf64052013-01-28 15:00:56 -0800217 /* Display all bitmaps for the image */
218 for (i = 0; i < MAX_IMAGE_IN_LAYOUT; i++) {
Simon Glass527ba812013-07-25 08:48:47 -0600219 ScreenLayout layout;
220 ImageInfo image_info;
221 char hwid[256];
Yoshiki Iguchi8fa13ad2013-08-29 05:10:33 +0000222
Simon Glass527ba812013-07-25 08:48:47 -0600223 ret = VbGbbReadImage(cparams, localization, screen_index,
224 i, &layout, &image_info,
225 &fullimage, &inoutsize);
226 if (ret == VBERROR_NO_IMAGE_PRESENT) {
227 continue;
228 } else if (ret) {
229 retval = ret;
230 goto VbDisplayScreenFromGBB_exit;
Randall Spangler0bf64052013-01-28 15:00:56 -0800231 }
Bill Richardson0a9977e2011-08-22 16:03:59 -0700232
Simon Glass527ba812013-07-25 08:48:47 -0600233 switch(image_info.format) {
Randall Spangler0bf64052013-01-28 15:00:56 -0800234 case FORMAT_BMP:
Hung-Te Lin5e8f1db2014-05-09 20:40:15 +0800235 if (i == 0) {
236 /**
237 * In current version GBB bitmaps, first image
238 * is always the background.
239 */
240 ret = VbExDisplaySetDimension(
241 image_info.width,
242 image_info.height);
Hung-Te Lin8f7b7052014-05-21 11:15:57 +0800243 if (ret) {
Hung-Te Lin5e8f1db2014-05-09 20:40:15 +0800244 VBDEBUG(("VbExDisplaySetDimension"
Hung-Te Lin8f7b7052014-05-21 11:15:57 +0800245 "(%d,%d): failed (%#x).\n",
Hung-Te Lin5e8f1db2014-05-09 20:40:15 +0800246 image_info.width,
Hung-Te Lin8f7b7052014-05-21 11:15:57 +0800247 image_info.height, ret));
Hung-Te Lin5e8f1db2014-05-09 20:40:15 +0800248 }
249 }
250
Simon Glass527ba812013-07-25 08:48:47 -0600251 retval = VbExDisplayImage(layout.images[i].x,
252 layout.images[i].y,
Randall Spangler0bf64052013-01-28 15:00:56 -0800253 fullimage, inoutsize);
254 break;
Bill Richardson0a9977e2011-08-22 16:03:59 -0700255
Randall Spangler0bf64052013-01-28 15:00:56 -0800256 case FORMAT_FONT:
257 /*
258 * The uncompressed blob is our font structure. Cache
259 * it as needed.
260 */
Simon Glass527ba812013-07-25 08:48:47 -0600261 font = VbInternalizeFontData(
262 (FontArrayHeader *)fullimage);
Bill Richardson0a9977e2011-08-22 16:03:59 -0700263
Randall Spangler0bf64052013-01-28 15:00:56 -0800264 /* TODO: handle text in general here */
Simon Glass527ba812013-07-25 08:48:47 -0600265 if (TAG_HWID == image_info.tag ||
266 TAG_HWID_RTOL == image_info.tag) {
267 VbRegionReadHWID(cparams, hwid, sizeof(hwid));
268 text_to_show = hwid;
269 rtol = (TAG_HWID_RTOL == image_info.tag);
Randall Spangler0bf64052013-01-28 15:00:56 -0800270 } else {
271 text_to_show = "";
272 rtol = 0;
273 }
Bill Richardson0a9977e2011-08-22 16:03:59 -0700274
Randall Spangler0bf64052013-01-28 15:00:56 -0800275 VbRenderTextAtPos(text_to_show, rtol,
Simon Glass527ba812013-07-25 08:48:47 -0600276 layout.images[i].x,
277 layout.images[i].y, font);
Bill Richardson0a9977e2011-08-22 16:03:59 -0700278
Randall Spangler0bf64052013-01-28 15:00:56 -0800279 VbDoneWithFontForNow(font);
280 break;
Bill Richardson0a9977e2011-08-22 16:03:59 -0700281
Randall Spangler0bf64052013-01-28 15:00:56 -0800282 default:
283 VBDEBUG(("VbDisplayScreenFromGBB(): "
284 "unsupported ImageFormat %d\n",
Simon Glass527ba812013-07-25 08:48:47 -0600285 image_info.format));
Randall Spangler0bf64052013-01-28 15:00:56 -0800286 retval = VBERROR_INVALID_GBB;
287 }
Bill Richardson0a9977e2011-08-22 16:03:59 -0700288
Simon Glass527ba812013-07-25 08:48:47 -0600289 VbExFree(fullimage);
Bill Richardson822eca62011-08-22 14:06:38 -0700290
Randall Spangler0bf64052013-01-28 15:00:56 -0800291 if (VBERROR_SUCCESS != retval)
292 goto VbDisplayScreenFromGBB_exit;
293 }
Bill Richardson822eca62011-08-22 14:06:38 -0700294
Randall Spangler0bf64052013-01-28 15:00:56 -0800295 /* Successful if all bitmaps displayed */
296 retval = VBERROR_SUCCESS;
Bill Richardsonec35beb2011-09-14 10:42:54 -0700297
Simon Glass527ba812013-07-25 08:48:47 -0600298 VbRegionCheckVersion(cparams);
Bill Richardsonec35beb2011-09-14 10:42:54 -0700299
Randall Spangler0bf64052013-01-28 15:00:56 -0800300 VbDisplayScreenFromGBB_exit:
301 VBDEBUG(("leaving VbDisplayScreenFromGBB() with %d\n",retval));
302 return retval;
Bill Richardson822eca62011-08-22 14:06:38 -0700303}
304
Simon Glass527ba812013-07-25 08:48:47 -0600305VbError_t VbDisplayScreen(VbCommonParams *cparams, uint32_t screen,
306 int force, VbNvContext *vncptr)
Randall Spangler0bf64052013-01-28 15:00:56 -0800307{
308 VbError_t retval;
Bill Richardson822eca62011-08-22 14:06:38 -0700309
Randall Spangler0bf64052013-01-28 15:00:56 -0800310 /* Initialize display if necessary */
311 if (!disp_width) {
312 retval = VbExDisplayInit(&disp_width, &disp_height);
313 if (VBERROR_SUCCESS != retval)
314 return retval;
315 }
Bill Richardson822eca62011-08-22 14:06:38 -0700316
Randall Spangler0bf64052013-01-28 15:00:56 -0800317 /* If requested screen is the same as the current one, we're done. */
318 if (disp_current_screen == screen && 0 == force)
319 return VBERROR_SUCCESS;
Bill Richardson822eca62011-08-22 14:06:38 -0700320
Randall Spangler0bf64052013-01-28 15:00:56 -0800321 /* If the screen is blank, turn off the backlight; else turn it on. */
322 VbExDisplayBacklight(VB_SCREEN_BLANK == screen ? 0 : 1);
Bill Richardson822eca62011-08-22 14:06:38 -0700323
Randall Spangler0bf64052013-01-28 15:00:56 -0800324 /* Request the screen */
325 disp_current_screen = screen;
Bill Richardson822eca62011-08-22 14:06:38 -0700326
Randall Spangler0bf64052013-01-28 15:00:56 -0800327 /* Look in the GBB first */
Simon Glass527ba812013-07-25 08:48:47 -0600328 if (VBERROR_SUCCESS == VbDisplayScreenFromGBB(cparams, screen,
329 vncptr))
Randall Spangler0bf64052013-01-28 15:00:56 -0800330 return VBERROR_SUCCESS;
Bill Richardson822eca62011-08-22 14:06:38 -0700331
Randall Spangler0bf64052013-01-28 15:00:56 -0800332 /* If screen wasn't in the GBB bitmaps, fall back to a default */
333 return VbExDisplayScreen(screen);
Bill Richardson822eca62011-08-22 14:06:38 -0700334}
335
Randall Spangler0bf64052013-01-28 15:00:56 -0800336static void Uint8ToString(char *buf, uint8_t val)
337{
338 const char *trans = "0123456789abcdef";
339 *buf++ = trans[val >> 4];
340 *buf = trans[val & 0xF];
Bill Richardson5de6b402011-09-01 14:47:05 -0700341}
342
Randall Spangler0bf64052013-01-28 15:00:56 -0800343static void FillInSha1Sum(char *outbuf, VbPublicKey *key)
344{
345 uint8_t *buf = ((uint8_t *)key) + key->key_offset;
346 uint64_t buflen = key->key_size;
347 uint8_t *digest = DigestBuf(buf, buflen, SHA1_DIGEST_ALGORITHM);
348 int i;
349 for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
350 Uint8ToString(outbuf, digest[i]);
351 outbuf += 2;
352 }
353 *outbuf = '\0';
354 VbExFree(digest);
Bill Richardson5de6b402011-09-01 14:47:05 -0700355}
356
Randall Spangler0bf64052013-01-28 15:00:56 -0800357const char *RecoveryReasonString(uint8_t code)
358{
359 switch(code) {
360 case VBNV_RECOVERY_NOT_REQUESTED:
361 return "Recovery not requested";
362 case VBNV_RECOVERY_LEGACY:
363 return "Recovery requested from legacy utility";
364 case VBNV_RECOVERY_RO_MANUAL:
365 return "recovery button pressed";
366 case VBNV_RECOVERY_RO_INVALID_RW:
367 return "RW firmware failed signature check";
368 case VBNV_RECOVERY_RO_S3_RESUME:
369 return "S3 resume failed";
370 case VBNV_RECOVERY_DEP_RO_TPM_ERROR:
371 return "TPM error in read-only firmware";
372 case VBNV_RECOVERY_RO_SHARED_DATA:
373 return "Shared data error in read-only firmware";
374 case VBNV_RECOVERY_RO_TEST_S3:
375 return "Test error from S3Resume()";
376 case VBNV_RECOVERY_RO_TEST_LFS:
377 return "Test error from LoadFirmwareSetup()";
378 case VBNV_RECOVERY_RO_TEST_LF:
379 return "Test error from LoadFirmware()";
380 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN + VBSD_LF_CHECK_NOT_DONE:
381 return "RW firmware check not done";
382 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN + VBSD_LF_CHECK_DEV_MISMATCH:
383 return "RW firmware developer flag mismatch";
384 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN + VBSD_LF_CHECK_REC_MISMATCH:
385 return "RW firmware recovery flag mismatch";
386 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN +
387 VBSD_LF_CHECK_VERIFY_KEYBLOCK:
388 return "RW firmware unable to verify key block";
389 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN + VBSD_LF_CHECK_KEY_ROLLBACK:
390 return "RW firmware key version rollback detected";
391 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN +
392 VBSD_LF_CHECK_DATA_KEY_PARSE:
393 return "RW firmware unable to parse data key";
394 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN +
395 VBSD_LF_CHECK_VERIFY_PREAMBLE:
396 return "RW firmware unable to verify preamble";
397 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN + VBSD_LF_CHECK_FW_ROLLBACK:
398 return "RW firmware version rollback detected";
399 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN + VBSD_LF_CHECK_GET_FW_BODY:
400 return "RW firmware unable to get firmware body";
401 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN +
402 VBSD_LF_CHECK_HASH_WRONG_SIZE:
403 return "RW firmware hash is wrong size";
404 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN + VBSD_LF_CHECK_VERIFY_BODY:
405 return "RW firmware unable to verify firmware body";
406 case VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN + VBSD_LF_CHECK_NO_RO_NORMAL:
407 return "RW firmware read-only normal path is not supported";
408 case VBNV_RECOVERY_RO_FIRMWARE:
409 return "Firmware problem outside of verified boot";
410 case VBNV_RECOVERY_RO_TPM_REBOOT:
411 return "TPM requires a system reboot (should be transient)";
412 case VBNV_RECOVERY_EC_SOFTWARE_SYNC:
413 return "EC software sync error";
414 case VBNV_RECOVERY_EC_UNKNOWN_IMAGE:
415 return "EC software sync unable to determine active EC image";
416 case VBNV_RECOVERY_DEP_EC_HASH:
417 return "EC software sync error obtaining EC image hash";
418 case VBNV_RECOVERY_EC_EXPECTED_IMAGE:
419 return "EC software sync error "
420 "obtaining expected EC image from BIOS";
Randall Spangler5ca4ea02013-02-04 15:00:09 -0800421 case VBNV_RECOVERY_EC_EXPECTED_HASH:
422 return "EC software sync error "
423 "obtaining expected EC hash from BIOS";
424 case VBNV_RECOVERY_EC_HASH_MISMATCH:
425 return "EC software sync error "
426 "comparing expected EC hash and image";
Randall Spangler0bf64052013-01-28 15:00:56 -0800427 case VBNV_RECOVERY_EC_UPDATE:
428 return "EC software sync error updating EC";
429 case VBNV_RECOVERY_EC_JUMP_RW:
430 return "EC software sync unable to jump to EC-RW";
431 case VBNV_RECOVERY_EC_PROTECT:
432 return "EC software sync protection error";
433 case VBNV_RECOVERY_RO_UNSPECIFIED:
434 return "Unspecified/unknown error in RO firmware";
435 case VBNV_RECOVERY_RW_DEV_SCREEN:
436 return "User requested recovery from dev-mode warning screen";
437 case VBNV_RECOVERY_RW_NO_OS:
438 return "No OS kernel detected (or kernel rollback attempt?)";
439 case VBNV_RECOVERY_RW_INVALID_OS:
440 return "OS kernel failed signature check";
441 case VBNV_RECOVERY_DEP_RW_TPM_ERROR:
442 return "TPM error in rewritable firmware";
443 case VBNV_RECOVERY_RW_DEV_MISMATCH:
444 return "RW firmware in dev mode, but dev switch is off";
445 case VBNV_RECOVERY_RW_SHARED_DATA:
446 return "Shared data error in rewritable firmware";
447 case VBNV_RECOVERY_RW_TEST_LK:
448 return "Test error from LoadKernel()";
449 case VBNV_RECOVERY_DEP_RW_NO_DISK:
450 return "No bootable disk found";
451 case VBNV_RECOVERY_TPM_E_FAIL:
452 return "TPM error that was not fixed by reboot";
453 case VBNV_RECOVERY_RO_TPM_S_ERROR:
454 return "TPM setup error in read-only firmware";
455 case VBNV_RECOVERY_RO_TPM_W_ERROR:
456 return "TPM write error in read-only firmware";
457 case VBNV_RECOVERY_RO_TPM_L_ERROR:
458 return "TPM lock error in read-only firmware";
459 case VBNV_RECOVERY_RO_TPM_U_ERROR:
460 return "TPM update error in read-only firmware";
461 case VBNV_RECOVERY_RW_TPM_R_ERROR:
462 return "TPM read error in rewritable firmware";
463 case VBNV_RECOVERY_RW_TPM_W_ERROR:
464 return "TPM write error in rewritable firmware";
465 case VBNV_RECOVERY_RW_TPM_L_ERROR:
466 return "TPM lock error in rewritable firmware";
467 case VBNV_RECOVERY_EC_HASH_FAILED:
468 return "EC software sync unable to get EC image hash";
469 case VBNV_RECOVERY_EC_HASH_SIZE:
470 return "EC software sync invalid image hash size";
471 case VBNV_RECOVERY_LK_UNSPECIFIED:
472 return "Unspecified error while trying to load kernel";
473 case VBNV_RECOVERY_RW_NO_DISK:
474 return "No bootable storage device in system";
475 case VBNV_RECOVERY_RW_NO_KERNEL:
476 return "No bootable kernel found on disk";
477 case VBNV_RECOVERY_RW_UNSPECIFIED:
478 return "Unspecified/unknown error in RW firmware";
479 case VBNV_RECOVERY_KE_DM_VERITY:
480 return "DM-verity error";
481 case VBNV_RECOVERY_KE_UNSPECIFIED:
482 return "Unspecified/unknown error in kernel";
483 case VBNV_RECOVERY_US_TEST:
484 return "Recovery mode test from user-mode";
485 case VBNV_RECOVERY_US_UNSPECIFIED:
486 return "Unspecified/unknown error in user-mode";
487 }
488 return "We have no idea what this means";
Bill Richardsonf2ad05f2011-09-28 14:42:36 -0700489}
490
Bill Richardson822eca62011-08-22 14:06:38 -0700491#define DEBUG_INFO_SIZE 512
492
Randall Spangler0bf64052013-01-28 15:00:56 -0800493VbError_t VbDisplayDebugInfo(VbCommonParams *cparams, VbNvContext *vncptr)
494{
495 VbSharedDataHeader *shared =
496 (VbSharedDataHeader *)cparams->shared_data_blob;
Simon Glass527ba812013-07-25 08:48:47 -0600497 GoogleBinaryBlockHeader *gbb = cparams->gbb;
Randall Spangler0bf64052013-01-28 15:00:56 -0800498 char buf[DEBUG_INFO_SIZE] = "";
499 char sha1sum[SHA1_DIGEST_SIZE * 2 + 1];
Simon Glass527ba812013-07-25 08:48:47 -0600500 char hwid[256];
Randall Spangler0bf64052013-01-28 15:00:56 -0800501 uint32_t used = 0;
Simon Glass527ba812013-07-25 08:48:47 -0600502 VbPublicKey *key;
503 VbError_t ret;
Randall Spangler0bf64052013-01-28 15:00:56 -0800504 uint32_t i;
Bill Richardson822eca62011-08-22 14:06:38 -0700505
Randall Spangler0bf64052013-01-28 15:00:56 -0800506 /* Redisplay current screen to overwrite any previous debug output */
507 VbDisplayScreen(cparams, disp_current_screen, 1, vncptr);
Bill Richardson822eca62011-08-22 14:06:38 -0700508
Randall Spangler0bf64052013-01-28 15:00:56 -0800509 /* Add hardware ID */
Simon Glass527ba812013-07-25 08:48:47 -0600510 VbRegionReadHWID(cparams, hwid, sizeof(hwid));
Bill Richardson5fed2a62013-03-04 15:11:38 -0800511 used += StrnAppend(buf + used, "HWID: ", DEBUG_INFO_SIZE - used);
Simon Glass527ba812013-07-25 08:48:47 -0600512 used += StrnAppend(buf + used, hwid, DEBUG_INFO_SIZE - used);
Bill Richardson822eca62011-08-22 14:06:38 -0700513
Randall Spangler0bf64052013-01-28 15:00:56 -0800514 /* Add recovery reason */
Bill Richardson5fed2a62013-03-04 15:11:38 -0800515 used += StrnAppend(buf + used,
Randall Spangler0bf64052013-01-28 15:00:56 -0800516 "\nrecovery_reason: 0x", DEBUG_INFO_SIZE - used);
517 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
518 shared->recovery_reason, 16, 2);
Bill Richardson5fed2a62013-03-04 15:11:38 -0800519 used += StrnAppend(buf + used, " ", DEBUG_INFO_SIZE - used);
520 used += StrnAppend(buf + used,
Randall Spangler0bf64052013-01-28 15:00:56 -0800521 RecoveryReasonString(shared->recovery_reason),
522 DEBUG_INFO_SIZE - used);
Bill Richardsonf2ad05f2011-09-28 14:42:36 -0700523
Randall Spangler0bf64052013-01-28 15:00:56 -0800524 /* Add VbSharedData flags */
Bill Richardson5fed2a62013-03-04 15:11:38 -0800525 used += StrnAppend(buf + used, "\nVbSD.flags: 0x", DEBUG_INFO_SIZE - used);
Randall Spangler0bf64052013-01-28 15:00:56 -0800526 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
527 shared->flags, 16, 8);
Bill Richardson822eca62011-08-22 14:06:38 -0700528
Randall Spangler0bf64052013-01-28 15:00:56 -0800529 /* Add raw contents of VbNvStorage */
Bill Richardson5fed2a62013-03-04 15:11:38 -0800530 used += StrnAppend(buf + used, "\nVbNv.raw:", DEBUG_INFO_SIZE - used);
Randall Spangler0bf64052013-01-28 15:00:56 -0800531 for (i = 0; i < VBNV_BLOCK_SIZE; i++) {
Bill Richardson5fed2a62013-03-04 15:11:38 -0800532 used += StrnAppend(buf + used, " ", DEBUG_INFO_SIZE - used);
Randall Spangler0bf64052013-01-28 15:00:56 -0800533 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
534 vncptr->raw[i], 16, 2);
535 }
Bill Richardson822eca62011-08-22 14:06:38 -0700536
Randall Spangler0bf64052013-01-28 15:00:56 -0800537 /* Add dev_boot_usb flag */
538 VbNvGet(vncptr, VBNV_DEV_BOOT_USB, &i);
Bill Richardson5fed2a62013-03-04 15:11:38 -0800539 used += StrnAppend(buf + used, "\ndev_boot_usb: ", DEBUG_INFO_SIZE - used);
Randall Spangler0bf64052013-01-28 15:00:56 -0800540 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0);
Bill Richardson822eca62011-08-22 14:06:38 -0700541
Randall Spangler0bf64052013-01-28 15:00:56 -0800542 /* Add dev_boot_legacy flag */
543 VbNvGet(vncptr, VBNV_DEV_BOOT_LEGACY, &i);
Bill Richardson5fed2a62013-03-04 15:11:38 -0800544 used += StrnAppend(buf + used,
Randall Spangler0bf64052013-01-28 15:00:56 -0800545 "\ndev_boot_legacy: ", DEBUG_INFO_SIZE - used);
546 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0);
Bill Richardson822eca62011-08-22 14:06:38 -0700547
Randall Spangler0bf64052013-01-28 15:00:56 -0800548 /* Add dev_boot_signed_only flag */
549 VbNvGet(vncptr, VBNV_DEV_BOOT_SIGNED_ONLY, &i);
Bill Richardson5fed2a62013-03-04 15:11:38 -0800550 used += StrnAppend(buf + used, "\ndev_boot_signed_only: ",
Randall Spangler0bf64052013-01-28 15:00:56 -0800551 DEBUG_INFO_SIZE - used);
552 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0);
Stefan Reinauera2326ee2012-08-23 15:06:45 -0700553
Randall Spangler0bf64052013-01-28 15:00:56 -0800554 /* Add TPM versions */
Bill Richardson5fed2a62013-03-04 15:11:38 -0800555 used += StrnAppend(buf + used, "\nTPM: fwver=0x", DEBUG_INFO_SIZE - used);
Randall Spangler0bf64052013-01-28 15:00:56 -0800556 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
557 shared->fw_version_tpm, 16, 8);
Bill Richardson5fed2a62013-03-04 15:11:38 -0800558 used += StrnAppend(buf + used, " kernver=0x", DEBUG_INFO_SIZE - used);
Randall Spangler0bf64052013-01-28 15:00:56 -0800559 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
560 shared->kernel_version_tpm, 16, 8);
Bill Richardsonfa9d7782011-11-09 09:11:34 -0800561
Randall Spangler0bf64052013-01-28 15:00:56 -0800562 /* Add GBB flags */
Bill Richardson5fed2a62013-03-04 15:11:38 -0800563 used += StrnAppend(buf + used, "\ngbb.flags: 0x", DEBUG_INFO_SIZE - used);
Randall Spangler0bf64052013-01-28 15:00:56 -0800564 if (gbb->major_version == GBB_MAJOR_VER && gbb->minor_version >= 1) {
565 used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
566 gbb->flags, 16, 8);
567 } else {
Bill Richardson5fed2a62013-03-04 15:11:38 -0800568 used += StrnAppend(buf + used,
Randall Spangler0bf64052013-01-28 15:00:56 -0800569 "0 (default)", DEBUG_INFO_SIZE - used);
570 }
Bill Richardson822eca62011-08-22 14:06:38 -0700571
Randall Spangler0bf64052013-01-28 15:00:56 -0800572 /* Add sha1sum for Root & Recovery keys */
Simon Glass527ba812013-07-25 08:48:47 -0600573 ret = VbGbbReadRootKey(cparams, &key);
574 if (!ret) {
575 FillInSha1Sum(sha1sum, key);
576 VbExFree(key);
577 used += StrnAppend(buf + used, "\ngbb.rootkey: ",
578 DEBUG_INFO_SIZE - used);
579 used += StrnAppend(buf + used, sha1sum,
580 DEBUG_INFO_SIZE - used);
581 }
582
583 ret = VbGbbReadRecoveryKey(cparams, &key);
584 if (!ret) {
585 FillInSha1Sum(sha1sum, key);
586 VbExFree(key);
587 used += StrnAppend(buf + used, "\ngbb.recovery_key: ",
588 DEBUG_INFO_SIZE - used);
589 used += StrnAppend(buf + used, sha1sum,
590 DEBUG_INFO_SIZE - used);
591 }
Bill Richardson822eca62011-08-22 14:06:38 -0700592
Randall Spangler0bf64052013-01-28 15:00:56 -0800593 /* If we're in dev-mode, show the kernel subkey that we expect, too. */
594 if (0 == shared->recovery_reason) {
595 FillInSha1Sum(sha1sum, &shared->kernel_subkey);
Bill Richardson5fed2a62013-03-04 15:11:38 -0800596 used += StrnAppend(buf + used,
Randall Spangler0bf64052013-01-28 15:00:56 -0800597 "\nkernel_subkey: ", DEBUG_INFO_SIZE - used);
Bill Richardson5fed2a62013-03-04 15:11:38 -0800598 used += StrnAppend(buf + used, sha1sum, DEBUG_INFO_SIZE - used);
Randall Spangler0bf64052013-01-28 15:00:56 -0800599 }
Bill Richardson5de6b402011-09-01 14:47:05 -0700600
Randall Spangler0bf64052013-01-28 15:00:56 -0800601 /* Make sure we finish with a newline */
Bill Richardson5fed2a62013-03-04 15:11:38 -0800602 used += StrnAppend(buf + used, "\n", DEBUG_INFO_SIZE - used);
Bill Richardson5de6b402011-09-01 14:47:05 -0700603
Randall Spangler0bf64052013-01-28 15:00:56 -0800604 /* TODO: add more interesting data:
605 * - Information on current disks */
Bill Richardson822eca62011-08-22 14:06:38 -0700606
Randall Spangler0bf64052013-01-28 15:00:56 -0800607 buf[DEBUG_INFO_SIZE - 1] = '\0';
608 return VbExDisplayDebugInfo(buf);
Bill Richardson822eca62011-08-22 14:06:38 -0700609}
610
Bill Richardson518d4f32011-09-13 15:28:55 -0700611#define MAGIC_WORD_LEN 5
Randall Spangler0bf64052013-01-28 15:00:56 -0800612#define MAGIC_WORD "xyzzy"
Bill Richardson518d4f32011-09-13 15:28:55 -0700613static uint8_t MagicBuffer[MAGIC_WORD_LEN];
614
Randall Spangler0bf64052013-01-28 15:00:56 -0800615VbError_t VbCheckDisplayKey(VbCommonParams *cparams, uint32_t key,
616 VbNvContext *vncptr)
617{
618 int i;
Bill Richardson518d4f32011-09-13 15:28:55 -0700619
Randall Spangler0bf64052013-01-28 15:00:56 -0800620 /* Update key buffer */
621 for(i = 1; i < MAGIC_WORD_LEN; i++)
622 MagicBuffer[i - 1] = MagicBuffer[i];
623 /* Save as lower-case ASCII */
624 MagicBuffer[MAGIC_WORD_LEN - 1] = (key | 0x20) & 0xFF;
Bill Richardson822eca62011-08-22 14:06:38 -0700625
Randall Spangler0bf64052013-01-28 15:00:56 -0800626 if ('\t' == key) {
627 /* Tab = display debug info */
628 return VbDisplayDebugInfo(cparams, vncptr);
629 } else if (VB_KEY_LEFT == key || VB_KEY_RIGHT == key ||
630 VB_KEY_DOWN == key || VB_KEY_UP == key) {
631 /* Arrow keys = change localization */
632 uint32_t loc = 0;
633 uint32_t count = 0;
Bill Richardson822eca62011-08-22 14:06:38 -0700634
Randall Spangler0bf64052013-01-28 15:00:56 -0800635 VbNvGet(vncptr, VBNV_LOCALIZATION_INDEX, &loc);
636 if (VBERROR_SUCCESS != VbGetLocalizationCount(cparams, &count))
637 loc = 0; /* No localization count (bad GBB?) */
638 else if (VB_KEY_RIGHT == key || VB_KEY_UP == key)
639 loc = (loc < count - 1 ? loc + 1 : 0);
640 else
641 loc = (loc > 0 ? loc - 1 : count - 1);
642 VBDEBUG(("VbCheckDisplayKey() - change localization to %d\n",
643 (int)loc));
644 VbNvSet(vncptr, VBNV_LOCALIZATION_INDEX, loc);
Bill Richardsonb64f0972014-05-28 15:49:23 -0700645 VbNvSet(vncptr, VBNV_BACKUP_NVRAM_REQUEST, 1);
Simon Glass985e90e2012-09-06 14:57:54 -0700646
Randall Spanglerbe94d552012-09-24 10:16:15 -0700647#ifdef SAVE_LOCALE_IMMEDIATELY
Randall Spangler0bf64052013-01-28 15:00:56 -0800648 VbNvTeardown(vncptr); /* really only computes checksum */
649 if (vncptr->raw_changed)
650 VbExNvStorageWrite(vncptr->raw);
Simon Glass985e90e2012-09-06 14:57:54 -0700651#endif
Bill Richardson822eca62011-08-22 14:06:38 -0700652
Randall Spangler0bf64052013-01-28 15:00:56 -0800653 /* Force redraw of current screen */
654 return VbDisplayScreen(cparams, disp_current_screen, 1, vncptr);
655 }
Bill Richardson822eca62011-08-22 14:06:38 -0700656
Randall Spangler0bf64052013-01-28 15:00:56 -0800657 if (0 == Memcmp(MagicBuffer, MAGIC_WORD, MAGIC_WORD_LEN)) {
658 if (VBEASTEREGG)
659 (void)VbDisplayScreen(cparams, disp_current_screen,
660 1, vncptr);
661 }
Bill Richardson518d4f32011-09-13 15:28:55 -0700662
Bill Richardson822eca62011-08-22 14:06:38 -0700663 return VBERROR_SUCCESS;
664}