Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 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 | |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame] | 8 | #include "sysincludes.h" |
| 9 | |
Simon Glass | 527ba81 | 2013-07-25 08:48:47 -0600 | [diff] [blame] | 10 | #include "gbb_access.h" |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 11 | #include "gbb_header.h" |
| 12 | #include "load_kernel_fw.h" |
Simon Glass | 527ba81 | 2013-07-25 08:48:47 -0600 | [diff] [blame] | 13 | #include "region.h" |
Randall Spangler | 22e7bb2 | 2011-07-22 14:06:51 -0700 | [diff] [blame] | 14 | #include "rollback_index.h" |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 15 | #include "utility.h" |
| 16 | #include "vboot_api.h" |
Bill Richardson | 253a58e | 2011-10-03 14:00:58 -0700 | [diff] [blame] | 17 | #include "vboot_audio.h" |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 18 | #include "vboot_common.h" |
Bill Richardson | 822eca6 | 2011-08-22 14:06:38 -0700 | [diff] [blame] | 19 | #include "vboot_display.h" |
Aaron Durbin | 612797e | 2013-02-26 09:40:55 -0600 | [diff] [blame] | 20 | #include "vboot_kernel.h" |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 21 | #include "vboot_nvstorage.h" |
| 22 | |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 23 | /* Global variables */ |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 24 | static VbNvContext vnc; |
Randall Spangler | 946abf1 | 2016-04-15 14:49:40 -0700 | [diff] [blame] | 25 | static struct RollbackSpaceFwmp fwmp; |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 26 | |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 27 | #ifdef CHROMEOS_ENVIRONMENT |
Randall Spangler | 946abf1 | 2016-04-15 14:49:40 -0700 | [diff] [blame] | 28 | /* Global variable accessors for unit tests */ |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 29 | |
| 30 | VbNvContext *VbApiKernelGetVnc(void) |
| 31 | { |
| 32 | return &vnc; |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 33 | } |
Randall Spangler | 946abf1 | 2016-04-15 14:49:40 -0700 | [diff] [blame] | 34 | |
| 35 | struct RollbackSpaceFwmp *VbApiKernelGetFwmp(void) |
| 36 | { |
| 37 | return &fwmp; |
| 38 | } |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 39 | #endif |
| 40 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 41 | /** |
Randall Spangler | 7f43669 | 2013-02-05 12:42:36 -0800 | [diff] [blame] | 42 | * Set recovery request (called from vboot_api_kernel.c functions only) |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 43 | */ |
| 44 | static void VbSetRecoveryRequest(uint32_t recovery_request) |
| 45 | { |
| 46 | VBDEBUG(("VbSetRecoveryRequest(%d)\n", (int)recovery_request)); |
| 47 | VbNvSet(&vnc, VBNV_RECOVERY_REQUEST, recovery_request); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Daisuke Nojiri | 3a63148 | 2015-10-20 18:31:10 -0700 | [diff] [blame] | 50 | static void VbSetRecoverySubcode(uint32_t recovery_request) |
| 51 | { |
| 52 | VBDEBUG(("VbSetRecoverySubcode(%d)\n", (int)recovery_request)); |
| 53 | VbNvSet(&vnc, VBNV_RECOVERY_SUBCODE, recovery_request); |
| 54 | } |
| 55 | |
| 56 | static void VbNvCommit(void) |
| 57 | { |
| 58 | VbNvTeardown(&vnc); |
| 59 | if (vnc.raw_changed) |
| 60 | VbExNvStorageWrite(vnc.raw); |
| 61 | } |
| 62 | |
Vadim Bendebury | ccca666 | 2015-04-06 18:04:44 -0700 | [diff] [blame] | 63 | static void VbAllowUsbBoot(void) |
| 64 | { |
| 65 | VBDEBUG(("%s\n", __func__)); |
| 66 | VbNvSet(&vnc, VBNV_DEV_BOOT_USB, 1); |
| 67 | } |
| 68 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 69 | /** |
Shawn Nematbakhsh | 5d652cd | 2014-12-12 09:40:42 -0800 | [diff] [blame] | 70 | * Checks GBB flags against VbExIsShutdownRequested() shutdown request to |
| 71 | * determine if a shutdown is required. |
| 72 | * |
| 73 | * Returns true if a shutdown is required and false if no shutdown is required. |
| 74 | */ |
| 75 | static int VbWantShutdown(uint32_t gbb_flags) |
| 76 | { |
| 77 | uint32_t shutdown_request = VbExIsShutdownRequested(); |
| 78 | |
| 79 | /* If desired, ignore shutdown request due to lid closure. */ |
| 80 | if (gbb_flags & GBB_FLAG_DISABLE_LID_SHUTDOWN) |
| 81 | shutdown_request &= ~VB_SHUTDOWN_REQUEST_LID_CLOSED; |
| 82 | |
| 83 | return !!shutdown_request; |
| 84 | } |
| 85 | |
Julius Werner | 957b424 | 2015-05-08 22:54:14 -0700 | [diff] [blame] | 86 | static void VbTryLegacy(int allowed) |
| 87 | { |
| 88 | if (!allowed) |
| 89 | VBDEBUG(("VbBootDeveloper() - Legacy boot is disabled\n")); |
| 90 | else if (0 != RollbackKernelLock(0)) |
| 91 | VBDEBUG(("Error locking kernel versions on legacy boot.\n")); |
| 92 | else |
| 93 | VbExLegacy(); /* will not return if successful */ |
| 94 | |
| 95 | /* If legacy boot fails, beep and return to calling UI loop. */ |
| 96 | VbExBeep(120, 400); |
| 97 | VbExSleepMs(120); |
| 98 | VbExBeep(120, 400); |
| 99 | } |
| 100 | |
Shawn Nematbakhsh | 5d652cd | 2014-12-12 09:40:42 -0800 | [diff] [blame] | 101 | /** |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 102 | * Attempt loading a kernel from the specified type(s) of disks. |
| 103 | * |
| 104 | * If successful, sets p->disk_handle to the disk for the kernel and returns |
Randall Spangler | 1cf77cd | 2011-07-25 09:19:58 -0700 | [diff] [blame] | 105 | * VBERROR_SUCCESS. |
| 106 | * |
| 107 | * Returns VBERROR_NO_DISK_FOUND if no disks of the specified type were found. |
| 108 | * |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 109 | * May return other VBERROR_ codes for other failures. |
| 110 | */ |
| 111 | uint32_t VbTryLoadKernel(VbCommonParams *cparams, LoadKernelParams *p, |
| 112 | uint32_t get_info_flags) |
| 113 | { |
| 114 | VbError_t retval = VBERROR_UNKNOWN; |
| 115 | VbDiskInfo* disk_info = NULL; |
| 116 | uint32_t disk_count = 0; |
| 117 | uint32_t i; |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 118 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 119 | VBDEBUG(("VbTryLoadKernel() start, get_info_flags=0x%x\n", |
| 120 | (unsigned)get_info_flags)); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 121 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 122 | p->disk_handle = NULL; |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 123 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 124 | /* Find disks */ |
| 125 | if (VBERROR_SUCCESS != VbExDiskGetInfo(&disk_info, &disk_count, |
| 126 | get_info_flags)) |
| 127 | disk_count = 0; |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 128 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 129 | VBDEBUG(("VbTryLoadKernel() found %d disks\n", (int)disk_count)); |
| 130 | if (0 == disk_count) { |
| 131 | VbSetRecoveryRequest(VBNV_RECOVERY_RW_NO_DISK); |
| 132 | return VBERROR_NO_DISK_FOUND; |
| 133 | } |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 134 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 135 | /* Loop over disks */ |
| 136 | for (i = 0; i < disk_count; i++) { |
| 137 | VBDEBUG(("VbTryLoadKernel() trying disk %d\n", (int)i)); |
| 138 | /* |
| 139 | * Sanity-check what we can. FWIW, VbTryLoadKernel() is always |
| 140 | * called with only a single bit set in get_info_flags. |
| 141 | * |
| 142 | * Ensure 512-byte sectors and non-trivially sized disk (for |
| 143 | * cgptlib) and that we got a partition with only the flags we |
| 144 | * asked for. |
| 145 | */ |
| 146 | if (512 != disk_info[i].bytes_per_lba || |
Dan Ehrenberg | f47eccf | 2015-01-05 13:05:58 -0800 | [diff] [blame] | 147 | 16 > disk_info[i].lba_count || |
Dan Ehrenberg | 3f4d8d0 | 2014-12-02 08:21:57 -0800 | [diff] [blame] | 148 | get_info_flags != (disk_info[i].flags & ~VB_DISK_FLAG_EXTERNAL_GPT)) { |
Simon Glass | 981cb2a | 2013-08-12 15:12:47 -0600 | [diff] [blame] | 149 | VBDEBUG((" skipping: bytes_per_lba=%" PRIu64 |
| 150 | " lba_count=%" PRIu64 " flags=0x%x\n", |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 151 | disk_info[i].bytes_per_lba, |
| 152 | disk_info[i].lba_count, |
| 153 | disk_info[i].flags)); |
| 154 | continue; |
| 155 | } |
| 156 | p->disk_handle = disk_info[i].handle; |
| 157 | p->bytes_per_lba = disk_info[i].bytes_per_lba; |
Dan Ehrenberg | 3f4d8d0 | 2014-12-02 08:21:57 -0800 | [diff] [blame] | 158 | p->gpt_lba_count = disk_info[i].lba_count; |
| 159 | p->streaming_lba_count = disk_info[i].streaming_lba_count |
| 160 | ?: p->gpt_lba_count; |
| 161 | p->boot_flags |= disk_info[i].flags & VB_DISK_FLAG_EXTERNAL_GPT |
| 162 | ? BOOT_FLAG_EXTERNAL_GPT : 0; |
Simon Glass | 527ba81 | 2013-07-25 08:48:47 -0600 | [diff] [blame] | 163 | retval = LoadKernel(p, cparams); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 164 | VBDEBUG(("VbTryLoadKernel() LoadKernel() = %d\n", retval)); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 165 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 166 | /* |
| 167 | * Stop now if we found a kernel. |
| 168 | * |
| 169 | * TODO: If recovery requested, should track the farthest we |
| 170 | * get, instead of just returning the value from the last disk |
| 171 | * attempted. |
| 172 | */ |
| 173 | if (VBERROR_SUCCESS == retval) |
| 174 | break; |
| 175 | } |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 176 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 177 | /* If we didn't find any good kernels, don't return a disk handle. */ |
| 178 | if (VBERROR_SUCCESS != retval) { |
| 179 | VbSetRecoveryRequest(VBNV_RECOVERY_RW_NO_KERNEL); |
| 180 | p->disk_handle = NULL; |
| 181 | } |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 182 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 183 | VbExDiskFreeInfo(disk_info, p->disk_handle); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 184 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 185 | /* |
| 186 | * Pass through return code. Recovery reason (if any) has already been |
| 187 | * set by LoadKernel(). |
| 188 | */ |
| 189 | return retval; |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Mary Ruthven | 12a55f2 | 2015-10-06 10:42:31 -0700 | [diff] [blame] | 192 | uint32_t VbTryUsb(VbCommonParams *cparams, LoadKernelParams *p) |
| 193 | { |
| 194 | uint32_t retval = VbTryLoadKernel(cparams, p, VB_DISK_FLAG_REMOVABLE); |
| 195 | if (VBERROR_SUCCESS == retval) { |
| 196 | VBDEBUG(("VbBootDeveloper() - booting USB\n")); |
| 197 | } else { |
| 198 | VBDEBUG(("VbBootDeveloper() - no kernel found on USB\n")); |
| 199 | VbExBeep(250, 200); |
| 200 | VbExSleepMs(120); |
| 201 | /* |
| 202 | * Clear recovery requests from failed |
| 203 | * kernel loading, so that powering off |
| 204 | * at this point doesn't put us into |
| 205 | * recovery mode. |
| 206 | */ |
| 207 | VbSetRecoveryRequest( |
| 208 | VBNV_RECOVERY_NOT_REQUESTED); |
| 209 | } |
| 210 | return retval; |
| 211 | } |
| 212 | |
Shawn Nematbakhsh | ba7fd8d | 2012-10-30 15:17:33 -0700 | [diff] [blame] | 213 | #define CONFIRM_KEY_DELAY 20 /* Check confirm screen keys every 20ms */ |
| 214 | |
Luigi Semenzato | a53a0b0 | 2014-01-10 16:26:08 -0800 | [diff] [blame] | 215 | int VbUserConfirms(VbCommonParams *cparams, uint32_t confirm_flags) |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 216 | { |
Luigi Semenzato | a53a0b0 | 2014-01-10 16:26:08 -0800 | [diff] [blame] | 217 | VbSharedDataHeader *shared = |
| 218 | (VbSharedDataHeader *)cparams->shared_data_blob; |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 219 | uint32_t key; |
Luigi Semenzato | a53a0b0 | 2014-01-10 16:26:08 -0800 | [diff] [blame] | 220 | uint32_t key_flags; |
| 221 | uint32_t button; |
| 222 | int rec_button_was_pressed = 0; |
Bill Richardson | 2934475 | 2012-08-01 15:02:15 -0700 | [diff] [blame] | 223 | |
Luigi Semenzato | a53a0b0 | 2014-01-10 16:26:08 -0800 | [diff] [blame] | 224 | VBDEBUG(("Entering %s(0x%x)\n", __func__, confirm_flags)); |
Bill Richardson | 2934475 | 2012-08-01 15:02:15 -0700 | [diff] [blame] | 225 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 226 | /* Await further instructions */ |
| 227 | while (1) { |
Shawn Nematbakhsh | 5d652cd | 2014-12-12 09:40:42 -0800 | [diff] [blame] | 228 | if (VbWantShutdown(cparams->gbb->flags)) |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 229 | return -1; |
Luigi Semenzato | a53a0b0 | 2014-01-10 16:26:08 -0800 | [diff] [blame] | 230 | key = VbExKeyboardReadWithFlags(&key_flags); |
| 231 | button = VbExGetSwitches(VB_INIT_FLAG_REC_BUTTON_PRESSED); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 232 | switch (key) { |
| 233 | case '\r': |
Luigi Semenzato | a53a0b0 | 2014-01-10 16:26:08 -0800 | [diff] [blame] | 234 | /* If we require a trusted keyboard for confirmation, |
| 235 | * but the keyboard may be faked (for instance, a USB |
| 236 | * device), beep and keep waiting. |
| 237 | */ |
| 238 | if (confirm_flags & VB_CONFIRM_MUST_TRUST_KEYBOARD && |
| 239 | !(key_flags & VB_KEY_FLAG_TRUSTED_KEYBOARD)) { |
| 240 | VbExBeep(120, 400); |
| 241 | break; |
| 242 | } |
| 243 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 244 | VBDEBUG(("%s() - Yes (1)\n", __func__)); |
| 245 | return 1; |
| 246 | break; |
| 247 | case ' ': |
Vadim Bendebury | 7aa250f | 2013-08-09 16:02:28 -0700 | [diff] [blame] | 248 | VBDEBUG(("%s() - Space (%d)\n", __func__, |
Luigi Semenzato | a53a0b0 | 2014-01-10 16:26:08 -0800 | [diff] [blame] | 249 | confirm_flags & VB_CONFIRM_SPACE_MEANS_NO)); |
| 250 | if (confirm_flags & VB_CONFIRM_SPACE_MEANS_NO) |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 251 | return 0; |
| 252 | break; |
| 253 | case 0x1b: |
| 254 | VBDEBUG(("%s() - No (0)\n", __func__)); |
| 255 | return 0; |
| 256 | break; |
| 257 | default: |
Luigi Semenzato | a53a0b0 | 2014-01-10 16:26:08 -0800 | [diff] [blame] | 258 | /* If the recovery button is physical, and is pressed, |
| 259 | * this is also a YES, but must wait for release. |
| 260 | */ |
| 261 | if (!(shared->flags & VBSD_BOOT_REC_SWITCH_VIRTUAL)) { |
| 262 | if (button) { |
| 263 | VBDEBUG(("%s() - Rec button pressed\n", |
| 264 | __func__)); |
| 265 | rec_button_was_pressed = 1; |
| 266 | } else if (rec_button_was_pressed) { |
| 267 | VBDEBUG(("%s() - Rec button (1)\n", |
| 268 | __func__)); |
| 269 | return 1; |
| 270 | } |
| 271 | } |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 272 | VbCheckDisplayKey(cparams, key, &vnc); |
| 273 | } |
| 274 | VbExSleepMs(CONFIRM_KEY_DELAY); |
| 275 | } |
| 276 | |
| 277 | /* Not reached, but compiler will complain without it */ |
| 278 | return -1; |
Bill Richardson | 2934475 | 2012-08-01 15:02:15 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Yunlian Jiang | 637ff03 | 2015-06-09 16:21:56 -0700 | [diff] [blame] | 281 | VbError_t test_mockable |
| 282 | VbBootNormal(VbCommonParams *cparams, LoadKernelParams *p) |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 283 | { |
| 284 | /* Boot from fixed disk only */ |
| 285 | VBDEBUG(("Entering %s()\n", __func__)); |
| 286 | return VbTryLoadKernel(cparams, p, VB_DISK_FLAG_FIXED); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Randall Spangler | 946abf1 | 2016-04-15 14:49:40 -0700 | [diff] [blame] | 289 | static const char dev_disable_msg[] = |
| 290 | "Developer mode is disabled on this device by system policy.\n" |
| 291 | "For more information, see http://dev.chromium.org/chromium-os/fwmp\n" |
| 292 | "\n"; |
| 293 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 294 | VbError_t VbBootDeveloper(VbCommonParams *cparams, LoadKernelParams *p) |
| 295 | { |
Simon Glass | 527ba81 | 2013-07-25 08:48:47 -0600 | [diff] [blame] | 296 | GoogleBinaryBlockHeader *gbb = cparams->gbb; |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 297 | VbSharedDataHeader *shared = |
| 298 | (VbSharedDataHeader *)cparams->shared_data_blob; |
Mary Ruthven | 12a55f2 | 2015-10-06 10:42:31 -0700 | [diff] [blame] | 299 | |
| 300 | uint32_t allow_usb = 0; |
| 301 | uint32_t allow_legacy = 0; |
Randall Spangler | 946abf1 | 2016-04-15 14:49:40 -0700 | [diff] [blame] | 302 | uint32_t disable_dev_boot = 0; |
Mary Ruthven | 12a55f2 | 2015-10-06 10:42:31 -0700 | [diff] [blame] | 303 | uint32_t use_usb = 0; |
| 304 | uint32_t use_legacy = 0; |
| 305 | uint32_t default_boot = 0; |
| 306 | uint32_t ctrl_d_pressed = 0; |
| 307 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 308 | VbAudioContext *audio = 0; |
Randall Spangler | daa807c | 2011-07-11 10:55:18 -0700 | [diff] [blame] | 309 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 310 | VBDEBUG(("Entering %s()\n", __func__)); |
Bill Richardson | ec8df16 | 2012-06-07 04:21:14 -0700 | [diff] [blame] | 311 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 312 | /* Check if USB booting is allowed */ |
| 313 | VbNvGet(&vnc, VBNV_DEV_BOOT_USB, &allow_usb); |
| 314 | VbNvGet(&vnc, VBNV_DEV_BOOT_LEGACY, &allow_legacy); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 315 | |
Mary Ruthven | 12a55f2 | 2015-10-06 10:42:31 -0700 | [diff] [blame] | 316 | /* Check if the default is to boot using disk, usb, or legacy */ |
| 317 | VbNvGet(&vnc, VBNV_DEV_DEFAULT_BOOT, &default_boot); |
| 318 | |
| 319 | if(default_boot == VBNV_DEV_DEFAULT_BOOT_USB) |
| 320 | use_usb = 1; |
| 321 | if(default_boot == VBNV_DEV_DEFAULT_BOOT_LEGACY) |
| 322 | use_legacy = 1; |
| 323 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 324 | /* Handle GBB flag override */ |
| 325 | if (gbb->flags & GBB_FLAG_FORCE_DEV_BOOT_USB) |
| 326 | allow_usb = 1; |
| 327 | if (gbb->flags & GBB_FLAG_FORCE_DEV_BOOT_LEGACY) |
| 328 | allow_legacy = 1; |
Mary Ruthven | 12a55f2 | 2015-10-06 10:42:31 -0700 | [diff] [blame] | 329 | if (gbb->flags & GBB_FLAG_DEFAULT_DEV_BOOT_LEGACY) { |
| 330 | use_legacy = 1; |
| 331 | use_usb = 0; |
| 332 | } |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 333 | |
Randall Spangler | 946abf1 | 2016-04-15 14:49:40 -0700 | [diff] [blame] | 334 | /* Handle FWMP override */ |
| 335 | if (fwmp.flags & FWMP_DEV_ENABLE_USB) |
| 336 | allow_usb = 1; |
| 337 | if (fwmp.flags & FWMP_DEV_ENABLE_LEGACY) |
| 338 | allow_legacy = 1; |
| 339 | if (fwmp.flags & FWMP_DEV_DISABLE_BOOT) { |
| 340 | if (gbb->flags & GBB_FLAG_FORCE_DEV_SWITCH_ON) { |
| 341 | VBDEBUG(("%s() - FWMP_DEV_DISABLE_BOOT rejected by " |
| 342 | "FORCE_DEV_SWITCH_ON\n", |
| 343 | __func__)); |
| 344 | } else { |
| 345 | disable_dev_boot = 1; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /* If dev mode is disabled, only allow TONORM */ |
| 350 | while (disable_dev_boot) { |
| 351 | VBDEBUG(("%s() - dev_disable_boot is set.\n", __func__)); |
| 352 | VbDisplayScreen(cparams, VB_SCREEN_DEVELOPER_TO_NORM, 0, &vnc); |
| 353 | VbExDisplayDebugInfo(dev_disable_msg); |
| 354 | |
| 355 | /* Ignore space in VbUserConfirms()... */ |
| 356 | switch (VbUserConfirms(cparams, 0)) { |
| 357 | case 1: |
| 358 | VBDEBUG(("%s() - leaving dev-mode.\n", __func__)); |
| 359 | VbNvSet(&vnc, VBNV_DISABLE_DEV_REQUEST, 1); |
| 360 | VbDisplayScreen(cparams, |
| 361 | VB_SCREEN_TO_NORM_CONFIRMED, |
| 362 | 0, &vnc); |
| 363 | VbExSleepMs(5000); |
| 364 | return VBERROR_TPM_REBOOT_REQUIRED; |
| 365 | case -1: |
| 366 | VBDEBUG(("%s() - shutdown requested\n", __func__)); |
| 367 | return VBERROR_SHUTDOWN_REQUESTED; |
| 368 | default: |
| 369 | /* Ignore user attempt to cancel */ |
| 370 | VBDEBUG(("%s() - ignore cancel TONORM\n", __func__)); |
| 371 | } |
| 372 | } |
| 373 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 374 | /* Show the dev mode warning screen */ |
| 375 | VbDisplayScreen(cparams, VB_SCREEN_DEVELOPER_WARNING, 0, &vnc); |
Bill Richardson | 25a3dbc | 2011-09-23 15:14:49 -0700 | [diff] [blame] | 376 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 377 | /* Get audio/delay context */ |
| 378 | audio = VbAudioOpen(cparams); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 379 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 380 | /* We'll loop until we finish the delay or are interrupted */ |
| 381 | do { |
| 382 | uint32_t key; |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 383 | |
Shawn Nematbakhsh | 5d652cd | 2014-12-12 09:40:42 -0800 | [diff] [blame] | 384 | if (VbWantShutdown(gbb->flags)) { |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 385 | VBDEBUG(("VbBootDeveloper() - shutdown requested!\n")); |
| 386 | VbAudioClose(audio); |
| 387 | return VBERROR_SHUTDOWN_REQUESTED; |
| 388 | } |
Stefan Reinauer | a2326ee | 2012-08-23 15:06:45 -0700 | [diff] [blame] | 389 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 390 | key = VbExKeyboardRead(); |
| 391 | switch (key) { |
| 392 | case 0: |
| 393 | /* nothing pressed */ |
| 394 | break; |
| 395 | case '\r': |
| 396 | /* Only disable virtual dev switch if allowed by GBB */ |
| 397 | if (!(gbb->flags & GBB_FLAG_ENTER_TRIGGERS_TONORM)) |
| 398 | break; |
| 399 | case ' ': |
| 400 | /* See if we should disable virtual dev-mode switch. */ |
| 401 | VBDEBUG(("%s shared->flags=0x%x\n", |
| 402 | __func__, shared->flags)); |
| 403 | if (shared->flags & VBSD_HONOR_VIRT_DEV_SWITCH && |
| 404 | shared->flags & VBSD_BOOT_DEV_SWITCH_ON) { |
| 405 | /* Stop the countdown while we go ask... */ |
| 406 | VbAudioClose(audio); |
| 407 | if (gbb->flags & GBB_FLAG_FORCE_DEV_SWITCH_ON) { |
| 408 | /* |
| 409 | * TONORM won't work (only for |
| 410 | * non-shipping devices). |
| 411 | */ |
| 412 | VBDEBUG(("%s() - TONORM rejected by " |
| 413 | "FORCE_DEV_SWITCH_ON\n", |
| 414 | __func__)); |
| 415 | VbExDisplayDebugInfo( |
| 416 | "WARNING: TONORM prohibited by " |
| 417 | "GBB FORCE_DEV_SWITCH_ON.\n\n"); |
| 418 | VbExBeep(120, 400); |
| 419 | break; |
| 420 | } |
| 421 | VbDisplayScreen(cparams, |
| 422 | VB_SCREEN_DEVELOPER_TO_NORM, |
| 423 | 0, &vnc); |
| 424 | /* Ignore space in VbUserConfirms()... */ |
| 425 | switch (VbUserConfirms(cparams, 0)) { |
| 426 | case 1: |
| 427 | VBDEBUG(("%s() - leaving dev-mode.\n", |
| 428 | __func__)); |
| 429 | VbNvSet(&vnc, VBNV_DISABLE_DEV_REQUEST, |
| 430 | 1); |
| 431 | VbDisplayScreen( |
| 432 | cparams, |
| 433 | VB_SCREEN_TO_NORM_CONFIRMED, |
| 434 | 0, &vnc); |
| 435 | VbExSleepMs(5000); |
| 436 | return VBERROR_TPM_REBOOT_REQUIRED; |
| 437 | case -1: |
| 438 | VBDEBUG(("%s() - shutdown requested\n", |
| 439 | __func__)); |
| 440 | return VBERROR_SHUTDOWN_REQUESTED; |
| 441 | default: |
| 442 | /* Stay in dev-mode */ |
| 443 | VBDEBUG(("%s() - stay in dev-mode\n", |
| 444 | __func__)); |
| 445 | VbDisplayScreen( |
| 446 | cparams, |
| 447 | VB_SCREEN_DEVELOPER_WARNING, |
| 448 | 0, &vnc); |
| 449 | /* Start new countdown */ |
| 450 | audio = VbAudioOpen(cparams); |
| 451 | } |
| 452 | } else { |
| 453 | /* |
| 454 | * No virtual dev-mode switch, so go directly |
| 455 | * to recovery mode. |
| 456 | */ |
| 457 | VBDEBUG(("%s() - going to recovery\n", |
| 458 | __func__)); |
| 459 | VbSetRecoveryRequest( |
| 460 | VBNV_RECOVERY_RW_DEV_SCREEN); |
| 461 | VbAudioClose(audio); |
| 462 | return VBERROR_LOAD_KERNEL_RECOVERY; |
| 463 | } |
| 464 | break; |
| 465 | case 0x04: |
| 466 | /* Ctrl+D = dismiss warning; advance to timeout */ |
| 467 | VBDEBUG(("VbBootDeveloper() - " |
| 468 | "user pressed Ctrl+D; skip delay\n")); |
Randall Spangler | f2a1dc0 | 2013-06-11 16:53:07 -0700 | [diff] [blame] | 469 | ctrl_d_pressed = 1; |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 470 | goto fallout; |
| 471 | break; |
| 472 | case 0x0c: |
| 473 | VBDEBUG(("VbBootDeveloper() - " |
| 474 | "user pressed Ctrl+L; Try legacy boot\n")); |
Julius Werner | 957b424 | 2015-05-08 22:54:14 -0700 | [diff] [blame] | 475 | VbTryLegacy(allow_legacy); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 476 | break; |
| 477 | |
Randall Spangler | 7f43669 | 2013-02-05 12:42:36 -0800 | [diff] [blame] | 478 | case VB_KEY_CTRL_ENTER: |
| 479 | /* |
| 480 | * The Ctrl-Enter is special for Lumpy test purpose; |
| 481 | * fall through to Ctrl+U handler. |
| 482 | */ |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 483 | case 0x15: |
| 484 | /* Ctrl+U = try USB boot, or beep if failure */ |
| 485 | VBDEBUG(("VbBootDeveloper() - " |
| 486 | "user pressed Ctrl+U; try USB\n")); |
| 487 | if (!allow_usb) { |
| 488 | VBDEBUG(("VbBootDeveloper() - " |
| 489 | "USB booting is disabled\n")); |
| 490 | VbExDisplayDebugInfo( |
| 491 | "WARNING: Booting from external media " |
| 492 | "(USB/SD) has not been enabled. Refer " |
| 493 | "to the developer-mode documentation " |
| 494 | "for details.\n"); |
| 495 | VbExBeep(120, 400); |
| 496 | VbExSleepMs(120); |
| 497 | VbExBeep(120, 400); |
| 498 | } else { |
| 499 | /* |
| 500 | * Clear the screen to show we get the Ctrl+U |
| 501 | * key press. |
| 502 | */ |
| 503 | VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0, |
| 504 | &vnc); |
Mary Ruthven | 12a55f2 | 2015-10-06 10:42:31 -0700 | [diff] [blame] | 505 | if (VBERROR_SUCCESS == VbTryUsb(cparams, p)) { |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 506 | VbAudioClose(audio); |
| 507 | return VBERROR_SUCCESS; |
| 508 | } else { |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 509 | /* Show dev mode warning screen again */ |
| 510 | VbDisplayScreen( |
| 511 | cparams, |
| 512 | VB_SCREEN_DEVELOPER_WARNING, |
| 513 | 0, &vnc); |
| 514 | } |
| 515 | } |
| 516 | break; |
| 517 | default: |
| 518 | VBDEBUG(("VbBootDeveloper() - pressed key %d\n", key)); |
| 519 | VbCheckDisplayKey(cparams, key, &vnc); |
| 520 | break; |
| 521 | } |
| 522 | } while(VbAudioLooping(audio)); |
Bill Richardson | 25a3dbc | 2011-09-23 15:14:49 -0700 | [diff] [blame] | 523 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 524 | fallout: |
Randall Spangler | f2a1dc0 | 2013-06-11 16:53:07 -0700 | [diff] [blame] | 525 | |
| 526 | /* If defaulting to legacy boot, try that unless Ctrl+D was pressed */ |
Mary Ruthven | 12a55f2 | 2015-10-06 10:42:31 -0700 | [diff] [blame] | 527 | if (use_legacy && !ctrl_d_pressed) { |
Randall Spangler | f2a1dc0 | 2013-06-11 16:53:07 -0700 | [diff] [blame] | 528 | VBDEBUG(("VbBootDeveloper() - defaulting to legacy\n")); |
Julius Werner | 0140cd2 | 2015-05-08 23:00:16 -0700 | [diff] [blame] | 529 | VbTryLegacy(allow_legacy); |
Randall Spangler | f2a1dc0 | 2013-06-11 16:53:07 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Mary Ruthven | 12a55f2 | 2015-10-06 10:42:31 -0700 | [diff] [blame] | 532 | if ((use_usb && !ctrl_d_pressed) && allow_usb) { |
| 533 | if (VBERROR_SUCCESS == VbTryUsb(cparams, p)) { |
| 534 | VbAudioClose(audio); |
| 535 | return VBERROR_SUCCESS; |
| 536 | } |
| 537 | } |
| 538 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 539 | /* Timeout or Ctrl+D; attempt loading from fixed disk */ |
| 540 | VBDEBUG(("VbBootDeveloper() - trying fixed disk\n")); |
| 541 | VbAudioClose(audio); |
| 542 | return VbTryLoadKernel(cparams, p, VB_DISK_FLAG_FIXED); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Simon Glass | 74fa207 | 2012-09-06 14:55:31 -0700 | [diff] [blame] | 545 | /* Delay in recovery mode */ |
Shawn Nematbakhsh | 0417153 | 2013-12-13 14:41:47 -0800 | [diff] [blame] | 546 | #define REC_DISK_DELAY 1000 /* Check disks every 1s */ |
| 547 | #define REC_KEY_DELAY 20 /* Check keys every 20ms */ |
| 548 | #define REC_MEDIA_INIT_DELAY 500 /* Check removable media every 500ms */ |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 549 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 550 | VbError_t VbBootRecovery(VbCommonParams *cparams, LoadKernelParams *p) |
| 551 | { |
| 552 | VbSharedDataHeader *shared = |
| 553 | (VbSharedDataHeader *)cparams->shared_data_blob; |
| 554 | uint32_t retval; |
| 555 | uint32_t key; |
| 556 | int i; |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 557 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 558 | VBDEBUG(("VbBootRecovery() start\n")); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 559 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 560 | /* |
| 561 | * If the dev-mode switch is off and the user didn't press the recovery |
Daisuke Nojiri | 73a6372 | 2015-04-30 12:41:24 -0700 | [diff] [blame] | 562 | * button (recovery was triggerred automatically), show 'broken' screen. |
| 563 | * The user can either only shutdown to abort or hit esc+refresh+power |
| 564 | * to initiate recovery as instructed on the screen. |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 565 | */ |
| 566 | if (!(shared->flags & VBSD_BOOT_DEV_SWITCH_ON) && |
| 567 | !(shared->flags & VBSD_BOOT_REC_SWITCH_ON)) { |
Daisuke Nojiri | 3a63148 | 2015-10-20 18:31:10 -0700 | [diff] [blame] | 568 | /* |
| 569 | * We have to save the reason here so that it will survive |
| 570 | * coming up three-finger-salute. We're saving it in |
| 571 | * VBNV_RECOVERY_SUBCODE to avoid a recovery loop. |
| 572 | * If we save the reason in VBNV_RECOVERY_REQUEST, we will come |
| 573 | * back here, thus, we won't be able to give a user a chance to |
| 574 | * reboot to workaround boot hicups. |
| 575 | */ |
| 576 | VBDEBUG(("VbBootRecovery() saving recovery reason (%#x)\n", |
| 577 | shared->recovery_reason)); |
| 578 | VbSetRecoverySubcode(shared->recovery_reason); |
| 579 | VbNvCommit(); |
Daisuke Nojiri | 73a6372 | 2015-04-30 12:41:24 -0700 | [diff] [blame] | 580 | VbDisplayScreen(cparams, VB_SCREEN_OS_BROKEN, 0, &vnc); |
Daisuke Nojiri | 3a63148 | 2015-10-20 18:31:10 -0700 | [diff] [blame] | 581 | VBDEBUG(("VbBootRecovery() waiting for manual recovery\n")); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 582 | while (1) { |
Daisuke Nojiri | 73a6372 | 2015-04-30 12:41:24 -0700 | [diff] [blame] | 583 | VbCheckDisplayKey(cparams, VbExKeyboardRead(), &vnc); |
| 584 | if (VbWantShutdown(cparams->gbb->flags)) |
| 585 | return VBERROR_SHUTDOWN_REQUESTED; |
| 586 | VbExSleepMs(REC_KEY_DELAY); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 587 | } |
| 588 | } |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 589 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 590 | /* Loop and wait for a recovery image */ |
Daisuke Nojiri | 73a6372 | 2015-04-30 12:41:24 -0700 | [diff] [blame] | 591 | VBDEBUG(("VbBootRecovery() waiting for a recovery image\n")); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 592 | while (1) { |
| 593 | VBDEBUG(("VbBootRecovery() attempting to load kernel2\n")); |
| 594 | retval = VbTryLoadKernel(cparams, p, VB_DISK_FLAG_REMOVABLE); |
Randall Spangler | ad33485 | 2011-07-19 15:52:43 -0700 | [diff] [blame] | 595 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 596 | /* |
| 597 | * Clear recovery requests from failed kernel loading, since |
| 598 | * we're already in recovery mode. Do this now, so that |
| 599 | * powering off after inserting an invalid disk doesn't leave |
| 600 | * us stuck in recovery mode. |
| 601 | */ |
| 602 | VbSetRecoveryRequest(VBNV_RECOVERY_NOT_REQUESTED); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 603 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 604 | if (VBERROR_SUCCESS == retval) |
| 605 | break; /* Found a recovery kernel */ |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 606 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 607 | VbDisplayScreen(cparams, VBERROR_NO_DISK_FOUND == retval ? |
| 608 | VB_SCREEN_RECOVERY_INSERT : |
| 609 | VB_SCREEN_RECOVERY_NO_GOOD, |
| 610 | 0, &vnc); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 611 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 612 | /* |
| 613 | * Scan keyboard more frequently than media, since x86 |
| 614 | * platforms don't like to scan USB too rapidly. |
| 615 | */ |
| 616 | for (i = 0; i < REC_DISK_DELAY; i += REC_KEY_DELAY) { |
| 617 | key = VbExKeyboardRead(); |
| 618 | /* |
| 619 | * We might want to enter dev-mode from the Insert |
| 620 | * screen if all of the following are true: |
| 621 | * - user pressed Ctrl-D |
| 622 | * - we can honor the virtual dev switch |
| 623 | * - not already in dev mode |
| 624 | * - user forced recovery mode |
| 625 | * - EC isn't pwned |
| 626 | */ |
| 627 | if (key == 0x04 && |
| 628 | shared->flags & VBSD_HONOR_VIRT_DEV_SWITCH && |
| 629 | !(shared->flags & VBSD_BOOT_DEV_SWITCH_ON) && |
| 630 | (shared->flags & VBSD_BOOT_REC_SWITCH_ON) && |
Randall Spangler | e778ada | 2014-07-15 16:14:21 -0700 | [diff] [blame] | 631 | VbExTrustEC(0)) { |
Luigi Semenzato | a53a0b0 | 2014-01-10 16:26:08 -0800 | [diff] [blame] | 632 | if (!(shared->flags & |
| 633 | VBSD_BOOT_REC_SWITCH_VIRTUAL) && |
| 634 | VbExGetSwitches( |
| 635 | VB_INIT_FLAG_REC_BUTTON_PRESSED)) { |
| 636 | /* |
| 637 | * Is the recovery button stuck? In |
| 638 | * any case we don't like this. Beep |
| 639 | * and ignore. |
| 640 | */ |
| 641 | VBDEBUG(("%s() - ^D but rec switch " |
| 642 | "is pressed\n", __func__)); |
| 643 | VbExBeep(120, 400); |
| 644 | continue; |
| 645 | } |
| 646 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 647 | /* Ask the user to confirm entering dev-mode */ |
| 648 | VbDisplayScreen(cparams, |
| 649 | VB_SCREEN_RECOVERY_TO_DEV, |
| 650 | 0, &vnc); |
| 651 | /* SPACE means no... */ |
Luigi Semenzato | a53a0b0 | 2014-01-10 16:26:08 -0800 | [diff] [blame] | 652 | uint32_t vbc_flags = |
| 653 | VB_CONFIRM_SPACE_MEANS_NO | |
| 654 | VB_CONFIRM_MUST_TRUST_KEYBOARD; |
| 655 | switch (VbUserConfirms(cparams, vbc_flags)) { |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 656 | case 1: |
| 657 | VBDEBUG(("%s() Enabling dev-mode...\n", |
| 658 | __func__)); |
| 659 | if (TPM_SUCCESS != SetVirtualDevMode(1)) |
| 660 | return VBERROR_TPM_SET_BOOT_MODE_STATE; |
| 661 | VBDEBUG(("%s() Reboot so it will take " |
| 662 | "effect\n", __func__)); |
Vadim Bendebury | ccca666 | 2015-04-06 18:04:44 -0700 | [diff] [blame] | 663 | if (VbExGetSwitches |
| 664 | (VB_INIT_FLAG_ALLOW_USB_BOOT)) |
| 665 | VbAllowUsbBoot(); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 666 | return VBERROR_TPM_REBOOT_REQUIRED; |
| 667 | case -1: |
| 668 | VBDEBUG(("%s() - Shutdown requested\n", |
| 669 | __func__)); |
| 670 | return VBERROR_SHUTDOWN_REQUESTED; |
| 671 | default: /* zero, actually */ |
| 672 | VBDEBUG(("%s() - Not enabling " |
| 673 | "dev-mode\n", __func__)); |
| 674 | /* |
| 675 | * Jump out of the outer loop to |
| 676 | * refresh the display quickly. |
| 677 | */ |
| 678 | i = 4; |
| 679 | break; |
| 680 | } |
| 681 | } else { |
| 682 | VbCheckDisplayKey(cparams, key, &vnc); |
| 683 | } |
Shawn Nematbakhsh | 5d652cd | 2014-12-12 09:40:42 -0800 | [diff] [blame] | 684 | if (VbWantShutdown(cparams->gbb->flags)) |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 685 | return VBERROR_SHUTDOWN_REQUESTED; |
| 686 | VbExSleepMs(REC_KEY_DELAY); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | return VBERROR_SUCCESS; |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 693 | /** |
Mary Ruthven | 7e0728d | 2015-12-18 11:16:02 -0800 | [diff] [blame] | 694 | * Wrapper around VbExEcProtect() which sets recovery reason on error. |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 695 | */ |
Mary Ruthven | 7e0728d | 2015-12-18 11:16:02 -0800 | [diff] [blame] | 696 | static VbError_t EcProtect(int devidx, enum VbSelectFirmware_t select) |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 697 | { |
Mary Ruthven | 7e0728d | 2015-12-18 11:16:02 -0800 | [diff] [blame] | 698 | int rv = VbExEcProtect(devidx, select); |
Randall Spangler | 09d0c2e | 2012-07-25 15:52:51 -0700 | [diff] [blame] | 699 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 700 | if (rv == VBERROR_EC_REBOOT_TO_RO_REQUIRED) { |
Mary Ruthven | 7e0728d | 2015-12-18 11:16:02 -0800 | [diff] [blame] | 701 | VBDEBUG(("VbExEcProtect() needs reboot\n")); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 702 | } else if (rv != VBERROR_SUCCESS) { |
Mary Ruthven | 7e0728d | 2015-12-18 11:16:02 -0800 | [diff] [blame] | 703 | VBDEBUG(("VbExEcProtect() returned %d\n", rv)); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 704 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_PROTECT); |
| 705 | } |
| 706 | return rv; |
Randall Spangler | 09d0c2e | 2012-07-25 15:52:51 -0700 | [diff] [blame] | 707 | } |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 708 | |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 709 | static VbError_t EcUpdateImage(int devidx, VbCommonParams *cparams, |
| 710 | enum VbSelectFirmware_t select, |
| 711 | int *need_update, int in_rw) |
| 712 | { |
| 713 | VbSharedDataHeader *shared = |
| 714 | (VbSharedDataHeader *)cparams->shared_data_blob; |
| 715 | int rv; |
| 716 | int hash_size; |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 717 | int ec_hash_size; |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 718 | const uint8_t *hash = NULL; |
| 719 | const uint8_t *expected = NULL; |
| 720 | const uint8_t *ec_hash = NULL; |
| 721 | int expected_size; |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 722 | int i; |
| 723 | int rw_request = select != VB_SELECT_FIRMWARE_READONLY; |
| 724 | |
| 725 | *need_update = 0; |
| 726 | VBDEBUG(("EcUpdateImage() - " |
| 727 | "Check for %s update\n", rw_request ? "RW" : "RO")); |
| 728 | |
| 729 | /* Get current EC hash. */ |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 730 | rv = VbExEcHashImage(devidx, select, &ec_hash, &ec_hash_size); |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 731 | if (rv) { |
| 732 | VBDEBUG(("EcUpdateImage() - " |
| 733 | "VbExEcHashImage() returned %d\n", rv)); |
| 734 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_HASH_FAILED); |
| 735 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
| 736 | } |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 737 | VBDEBUG(("EC-%s hash: ", rw_request ? "RW" : "RO")); |
| 738 | for (i = 0; i < ec_hash_size; i++) |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 739 | VBDEBUG(("%02x",ec_hash[i])); |
| 740 | VBDEBUG(("\n")); |
| 741 | |
| 742 | /* Get expected EC hash. */ |
| 743 | rv = VbExEcGetExpectedImageHash(devidx, select, &hash, &hash_size); |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 744 | if (rv) { |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 745 | VBDEBUG(("EcUpdateImage() - " |
| 746 | "VbExEcGetExpectedImageHash() returned %d\n", rv)); |
| 747 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_EXPECTED_HASH); |
| 748 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 749 | } |
| 750 | if (ec_hash_size != hash_size) { |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 751 | VBDEBUG(("EcUpdateImage() - " |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 752 | "EC uses %d-byte hash, but AP-RW contains %d bytes\n", |
| 753 | ec_hash_size, hash_size)); |
| 754 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_HASH_SIZE); |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 755 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 758 | VBDEBUG(("Expected hash: ")); |
| 759 | for (i = 0; i < hash_size; i++) |
| 760 | VBDEBUG(("%02x", hash[i])); |
| 761 | VBDEBUG(("\n")); |
| 762 | *need_update = SafeMemcmp(ec_hash, hash, hash_size); |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 763 | |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 764 | if (!*need_update) |
| 765 | return VBERROR_SUCCESS; |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 766 | |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 767 | /* Get expected EC image */ |
| 768 | rv = VbExEcGetExpectedImage(devidx, select, &expected, &expected_size); |
| 769 | if (rv) { |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 770 | VBDEBUG(("EcUpdateImage() - " |
| 771 | "VbExEcGetExpectedImage() returned %d\n", rv)); |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 772 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_EXPECTED_IMAGE); |
| 773 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
| 774 | } |
| 775 | VBDEBUG(("EcUpdateImage() - image len = %d\n", expected_size)); |
| 776 | |
| 777 | if (in_rw && rw_request) { |
| 778 | /* |
| 779 | * Check if BIOS should also load VGA Option ROM when |
| 780 | * rebooting to save another reboot if possible. |
| 781 | */ |
| 782 | if ((shared->flags & VBSD_EC_SLOW_UPDATE) && |
| 783 | (shared->flags & VBSD_OPROM_MATTERS) && |
| 784 | !(shared->flags & VBSD_OPROM_LOADED)) { |
| 785 | VBDEBUG(("EcUpdateImage() - Reboot to " |
| 786 | "load VGA Option ROM\n")); |
| 787 | VbNvSet(&vnc, VBNV_OPROM_NEEDED, 1); |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * EC is running the wrong RW image. Reboot the EC to |
| 792 | * RO so we can update it on the next boot. |
| 793 | */ |
| 794 | VBDEBUG(("EcUpdateImage() - " |
| 795 | "in RW, need to update RW, so reboot\n")); |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 796 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
| 797 | } |
| 798 | |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 799 | VBDEBUG(("EcUpdateImage() updating EC-%s...\n", |
| 800 | rw_request ? "RW" : "RO")); |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 801 | |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 802 | if (shared->flags & VBSD_EC_SLOW_UPDATE) { |
| 803 | VBDEBUG(("EcUpdateImage() - EC is slow. Show WAIT screen.\n")); |
| 804 | |
| 805 | /* Ensure the VGA Option ROM is loaded */ |
| 806 | if ((shared->flags & VBSD_OPROM_MATTERS) && |
| 807 | !(shared->flags & VBSD_OPROM_LOADED)) { |
| 808 | VBDEBUG(("EcUpdateImage() - Reboot to " |
| 809 | "load VGA Option ROM\n")); |
| 810 | VbNvSet(&vnc, VBNV_OPROM_NEEDED, 1); |
| 811 | return VBERROR_VGA_OPROM_MISMATCH; |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 812 | } |
| 813 | |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 814 | VbDisplayScreen(cparams, VB_SCREEN_WAIT, 0, &vnc); |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 815 | } |
| 816 | |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 817 | rv = VbExEcUpdateImage(devidx, select, expected, expected_size); |
| 818 | if (rv != VBERROR_SUCCESS) { |
| 819 | VBDEBUG(("EcUpdateImage() - " |
| 820 | "VbExEcUpdateImage() returned %d\n", rv)); |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 821 | |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 822 | /* |
| 823 | * The EC may know it needs a reboot. It may need to |
| 824 | * unprotect the region before updating, or may need to |
| 825 | * reboot after updating. Either way, it's not an error |
| 826 | * requiring recovery mode. |
| 827 | * |
| 828 | * If we fail for any other reason, trigger recovery |
| 829 | * mode. |
| 830 | */ |
| 831 | if (rv != VBERROR_EC_REBOOT_TO_RO_REQUIRED) |
| 832 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_UPDATE); |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 833 | |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 834 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /* Verify the EC was updated properly */ |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 838 | rv = VbExEcHashImage(devidx, select, &ec_hash, &ec_hash_size); |
| 839 | if (rv) { |
| 840 | VBDEBUG(("EcUpdateImage() - " |
| 841 | "VbExEcHashImage() returned %d\n", rv)); |
| 842 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_HASH_FAILED); |
| 843 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 844 | } |
Julius Werner | e1867d2 | 2016-05-27 13:27:18 -0700 | [diff] [blame] | 845 | if (hash_size != ec_hash_size) { |
| 846 | VBDEBUG(("EcUpdateImage() - " |
| 847 | "VbExEcHashImage() says size %d, not %d\n", |
| 848 | ec_hash_size, hash_size)); |
| 849 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_HASH_SIZE); |
| 850 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
| 851 | } |
| 852 | VBDEBUG(("Updated EC-%s hash: ", rw_request ? "RW" : "RO")); |
| 853 | for (i = 0; i < ec_hash_size; i++) |
| 854 | VBDEBUG(("%02x",ec_hash[i])); |
| 855 | VBDEBUG(("\n")); |
| 856 | |
| 857 | if (SafeMemcmp(ec_hash, hash, hash_size)){ |
| 858 | VBDEBUG(("EcUpdateImage() - " |
| 859 | "Failed to update EC-%s\n", rw_request ? |
| 860 | "RW" : "RO")); |
| 861 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_UPDATE); |
| 862 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
| 863 | } |
| 864 | |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 865 | return VBERROR_SUCCESS; |
| 866 | } |
| 867 | |
Randall Spangler | e778ada | 2014-07-15 16:14:21 -0700 | [diff] [blame] | 868 | VbError_t VbEcSoftwareSync(int devidx, VbCommonParams *cparams) |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 869 | { |
| 870 | VbSharedDataHeader *shared = |
| 871 | (VbSharedDataHeader *)cparams->shared_data_blob; |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 872 | enum VbSelectFirmware_t select_rw = |
| 873 | shared->firmware_index ? VB_SELECT_FIRMWARE_B : |
| 874 | VB_SELECT_FIRMWARE_A; |
| 875 | enum VbSelectFirmware_t select_ro = VB_SELECT_FIRMWARE_READONLY; |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 876 | int in_rw = 0; |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 877 | int ro_try_count = 2; |
| 878 | int num_tries = 0; |
| 879 | uint32_t try_ro_sync, recovery_request; |
| 880 | int rv, updated_rw, updated_ro; |
Randall Spangler | d4faa06 | 2012-07-16 16:42:40 -0700 | [diff] [blame] | 881 | |
Randall Spangler | 6014c04 | 2014-07-22 14:42:58 -0700 | [diff] [blame] | 882 | VBDEBUG(("VbEcSoftwareSync(devidx=%d)\n", devidx)); |
| 883 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 884 | /* Determine whether the EC is in RO or RW */ |
Randall Spangler | e778ada | 2014-07-15 16:14:21 -0700 | [diff] [blame] | 885 | rv = VbExEcRunningRW(devidx, &in_rw); |
Randall Spangler | 584e0d2 | 2012-07-26 10:29:48 -0700 | [diff] [blame] | 886 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 887 | if (shared->recovery_reason) { |
| 888 | /* Recovery mode; just verify the EC is in RO code */ |
| 889 | if (rv == VBERROR_SUCCESS && in_rw == 1) { |
| 890 | /* |
| 891 | * EC is definitely in RW firmware. We want it in |
| 892 | * read-only code, so preserve the current recovery |
| 893 | * reason and reboot. |
| 894 | * |
| 895 | * We don't reboot on error or unknown EC code, because |
| 896 | * we could end up in an endless reboot loop. If we |
| 897 | * had some way to track that we'd already rebooted for |
| 898 | * this reason, we could retry only once. |
| 899 | */ |
| 900 | VBDEBUG(("VbEcSoftwareSync() - " |
| 901 | "want recovery but got EC-RW\n")); |
| 902 | VbSetRecoveryRequest(shared->recovery_reason); |
| 903 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
| 904 | } |
Randall Spangler | d4faa06 | 2012-07-16 16:42:40 -0700 | [diff] [blame] | 905 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 906 | VBDEBUG(("VbEcSoftwareSync() in recovery; EC-RO\n")); |
| 907 | return VBERROR_SUCCESS; |
| 908 | } |
Randall Spangler | d4faa06 | 2012-07-16 16:42:40 -0700 | [diff] [blame] | 909 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 910 | /* |
| 911 | * Not in recovery. If we couldn't determine where the EC was, |
| 912 | * reboot to recovery. |
| 913 | */ |
| 914 | if (rv != VBERROR_SUCCESS) { |
| 915 | VBDEBUG(("VbEcSoftwareSync() - " |
Vadim Bendebury | 48b26df | 2013-06-21 16:28:28 -0700 | [diff] [blame] | 916 | "VbExEcRunningRW() returned %d\n", rv)); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 917 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_UNKNOWN_IMAGE); |
| 918 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
| 919 | } |
Randall Spangler | d4faa06 | 2012-07-16 16:42:40 -0700 | [diff] [blame] | 920 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 921 | /* If AP is read-only normal, EC should be in its RO code also. */ |
| 922 | if (shared->flags & VBSD_LF_USE_RO_NORMAL) { |
| 923 | /* If EC is in RW code, request reboot back to RO */ |
| 924 | if (in_rw == 1) { |
| 925 | VBDEBUG(("VbEcSoftwareSync() - " |
| 926 | "want RO-normal but got EC-RW\n")); |
| 927 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
| 928 | } |
Randall Spangler | d4faa06 | 2012-07-16 16:42:40 -0700 | [diff] [blame] | 929 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 930 | /* Protect the RW flash and stay in EC-RO */ |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 931 | rv = EcProtect(devidx, select_rw); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 932 | if (rv != VBERROR_SUCCESS) |
| 933 | return rv; |
Randall Spangler | d4faa06 | 2012-07-16 16:42:40 -0700 | [diff] [blame] | 934 | |
Randall Spangler | e778ada | 2014-07-15 16:14:21 -0700 | [diff] [blame] | 935 | rv = VbExEcDisableJump(devidx); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 936 | if (rv != VBERROR_SUCCESS) { |
| 937 | VBDEBUG(("VbEcSoftwareSync() - " |
Daisuke Nojiri | 8912169 | 2013-10-10 11:51:45 -0700 | [diff] [blame] | 938 | "VbExEcDisableJump() returned %d\n", rv)); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 939 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_SOFTWARE_SYNC); |
| 940 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
| 941 | } |
Randall Spangler | d4faa06 | 2012-07-16 16:42:40 -0700 | [diff] [blame] | 942 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 943 | VBDEBUG(("VbEcSoftwareSync() in RO-Normal; EC-RO\n")); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 944 | return VBERROR_SUCCESS; |
| 945 | } |
Randall Spangler | d4faa06 | 2012-07-16 16:42:40 -0700 | [diff] [blame] | 946 | |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 947 | VBDEBUG(("VbEcSoftwareSync() check for RW update.\n")); |
| 948 | |
| 949 | /* Update the RW Image. */ |
| 950 | rv = EcUpdateImage(devidx, cparams, select_rw, &updated_rw, in_rw); |
| 951 | |
| 952 | if (rv != VBERROR_SUCCESS) { |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 953 | VBDEBUG(("VbEcSoftwareSync() - " |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 954 | "EcUpdateImage() returned %d\n", rv)); |
| 955 | return rv; |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 956 | } |
Randall Spangler | 241de33 | 2012-07-23 16:24:43 -0700 | [diff] [blame] | 957 | |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 958 | /* Tell EC to jump to its RW image */ |
| 959 | if (!in_rw) { |
| 960 | VBDEBUG(("VbEcSoftwareSync() jumping to EC-RW\n")); |
| 961 | rv = VbExEcJumpToRW(devidx); |
Randall Spangler | bbc7606 | 2013-09-20 11:18:08 -0700 | [diff] [blame] | 962 | if (rv != VBERROR_SUCCESS) { |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 963 | VBDEBUG(("VbEcSoftwareSync() - " |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 964 | "VbExEcJumpToRW() returned %x\n", rv)); |
Randall Spangler | bbc7606 | 2013-09-20 11:18:08 -0700 | [diff] [blame] | 965 | |
| 966 | /* |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 967 | * If the EC booted RO-normal and a previous AP boot |
| 968 | * has called VbExEcStayInRO(), we need to reboot the EC |
| 969 | * to unlock the ability to jump to the RW firmware. |
Randall Spangler | bbc7606 | 2013-09-20 11:18:08 -0700 | [diff] [blame] | 970 | * |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 971 | * All other errors trigger recovery mode. |
Randall Spangler | bbc7606 | 2013-09-20 11:18:08 -0700 | [diff] [blame] | 972 | */ |
| 973 | if (rv != VBERROR_EC_REBOOT_TO_RO_REQUIRED) |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 974 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_JUMP_RW); |
Randall Spangler | bbc7606 | 2013-09-20 11:18:08 -0700 | [diff] [blame] | 975 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 976 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
| 977 | } |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 978 | } |
Randall Spangler | 241de33 | 2012-07-23 16:24:43 -0700 | [diff] [blame] | 979 | |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 980 | VbNvGet(&vnc, VBNV_TRY_RO_SYNC, &try_ro_sync); |
| 981 | |
| 982 | if (!devidx && try_ro_sync && |
| 983 | !(shared->flags & VBSD_BOOT_FIRMWARE_WP_ENABLED)) { |
| 984 | /* Reset RO Software Sync NV flag */ |
| 985 | VbNvSet(&vnc, VBNV_TRY_RO_SYNC, 0); |
| 986 | |
| 987 | VbNvGet(&vnc, VBNV_RECOVERY_REQUEST, &recovery_request); |
| 988 | |
| 989 | /* Update the RO Image. */ |
| 990 | while (num_tries < ro_try_count) { |
| 991 | VBDEBUG(("VbEcSoftwareSync() RO Software Sync\n")); |
| 992 | |
| 993 | /* Get expected EC-RO Image. */ |
| 994 | rv = EcUpdateImage(devidx, cparams, select_ro, |
| 995 | &updated_ro, in_rw); |
| 996 | if (rv == VBERROR_SUCCESS) { |
| 997 | /* |
| 998 | * If the RO update had failed, reset the |
| 999 | * recovery request. |
| 1000 | */ |
| 1001 | if (num_tries) |
| 1002 | VbSetRecoveryRequest(recovery_request); |
| 1003 | break; |
| 1004 | } else |
| 1005 | VBDEBUG(("VbEcSoftwareSync() - " |
| 1006 | "EcUpdateImage() returned %d\n", rv)); |
| 1007 | |
| 1008 | num_tries++; |
| 1009 | } |
| 1010 | } |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1011 | if (rv != VBERROR_SUCCESS) |
| 1012 | return rv; |
Randall Spangler | 241de33 | 2012-07-23 16:24:43 -0700 | [diff] [blame] | 1013 | |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 1014 | /* Protect RO flash */ |
| 1015 | rv = EcProtect(devidx, select_ro); |
| 1016 | if (rv != VBERROR_SUCCESS) |
| 1017 | return rv; |
Randall Spangler | bbc7606 | 2013-09-20 11:18:08 -0700 | [diff] [blame] | 1018 | |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 1019 | /* Protect RW flash */ |
| 1020 | rv = EcProtect(devidx, select_rw); |
| 1021 | if (rv != VBERROR_SUCCESS) |
| 1022 | return rv; |
Daisuke Nojiri | 8912169 | 2013-10-10 11:51:45 -0700 | [diff] [blame] | 1023 | |
Randall Spangler | e778ada | 2014-07-15 16:14:21 -0700 | [diff] [blame] | 1024 | rv = VbExEcDisableJump(devidx); |
Daisuke Nojiri | 8912169 | 2013-10-10 11:51:45 -0700 | [diff] [blame] | 1025 | if (rv != VBERROR_SUCCESS) { |
| 1026 | VBDEBUG(("VbEcSoftwareSync() - " |
| 1027 | "VbExEcDisableJump() returned %d\n", rv)); |
| 1028 | VbSetRecoveryRequest(VBNV_RECOVERY_EC_SOFTWARE_SYNC); |
| 1029 | return VBERROR_EC_REBOOT_TO_RO_REQUIRED; |
| 1030 | } |
Randall Spangler | 241de33 | 2012-07-23 16:24:43 -0700 | [diff] [blame] | 1031 | |
Duncan Laurie | 731f8e8 | 2014-10-16 11:05:13 -0700 | [diff] [blame] | 1032 | /* |
| 1033 | * Reboot to unload VGA Option ROM if: |
| 1034 | * - RW update was done |
| 1035 | * - the system is NOT in developer mode |
| 1036 | * - the system has slow EC update flag set |
| 1037 | * - the VGA Option ROM was needed and loaded |
| 1038 | */ |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 1039 | if (updated_rw && |
Duncan Laurie | 731f8e8 | 2014-10-16 11:05:13 -0700 | [diff] [blame] | 1040 | !(shared->flags & VBSD_BOOT_DEV_SWITCH_ON) && |
| 1041 | (shared->flags & VBSD_EC_SLOW_UPDATE) && |
| 1042 | (shared->flags & VBSD_OPROM_MATTERS) && |
| 1043 | (shared->flags & VBSD_OPROM_LOADED)) { |
| 1044 | VBDEBUG(("VbEcSoftwareSync() - Reboot to " |
| 1045 | "unload VGA Option ROM\n")); |
Duncan Laurie | eea0116 | 2016-01-20 12:53:15 -0800 | [diff] [blame] | 1046 | VbNvSet(&vnc, VBNV_OPROM_NEEDED, 0); |
Duncan Laurie | 731f8e8 | 2014-10-16 11:05:13 -0700 | [diff] [blame] | 1047 | return VBERROR_VGA_OPROM_MISMATCH; |
| 1048 | } |
| 1049 | |
Mary Ruthven | bcf8389 | 2016-01-06 11:30:55 -0800 | [diff] [blame] | 1050 | |
| 1051 | return rv; |
Randall Spangler | d4faa06 | 2012-07-16 16:42:40 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
Simon Glass | 527ba81 | 2013-07-25 08:48:47 -0600 | [diff] [blame] | 1054 | /* This function is also used by tests */ |
| 1055 | void VbApiKernelFree(VbCommonParams *cparams) |
| 1056 | { |
| 1057 | /* VbSelectAndLoadKernel() always allocates this, tests don't */ |
| 1058 | if (cparams->gbb) { |
| 1059 | VbExFree(cparams->gbb); |
| 1060 | cparams->gbb = NULL; |
| 1061 | } |
| 1062 | if (cparams->bmp) { |
| 1063 | VbExFree(cparams->bmp); |
| 1064 | cparams->bmp = NULL; |
| 1065 | } |
| 1066 | } |
| 1067 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1068 | VbError_t VbSelectAndLoadKernel(VbCommonParams *cparams, |
| 1069 | VbSelectAndLoadKernelParams *kparams) |
| 1070 | { |
| 1071 | VbSharedDataHeader *shared = |
| 1072 | (VbSharedDataHeader *)cparams->shared_data_blob; |
| 1073 | VbError_t retval = VBERROR_SUCCESS; |
| 1074 | LoadKernelParams p; |
| 1075 | uint32_t tpm_status = 0; |
Hung-Te Lin | aee6bd6 | 2016-04-07 12:21:03 +0800 | [diff] [blame] | 1076 | uint32_t battery_cutoff = 0; |
Randall Spangler | d4faa06 | 2012-07-16 16:42:40 -0700 | [diff] [blame] | 1077 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1078 | /* Start timer */ |
| 1079 | shared->timer_vb_select_and_load_kernel_enter = VbExGetTimer(); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 1080 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1081 | VbExNvStorageRead(vnc.raw); |
| 1082 | VbNvSetup(&vnc); |
Randall Spangler | 9619112 | 2011-07-08 14:01:54 -0700 | [diff] [blame] | 1083 | |
Randall Spangler | 1589f94 | 2016-09-02 12:10:31 -0700 | [diff] [blame] | 1084 | /* Fill in params for calls to LoadKernel() */ |
Randall Spangler | 664096b | 2016-10-13 16:16:41 -0700 | [diff] [blame] | 1085 | memset(&p, 0, sizeof(p)); |
Randall Spangler | 1589f94 | 2016-09-02 12:10:31 -0700 | [diff] [blame] | 1086 | p.shared_data_blob = cparams->shared_data_blob; |
| 1087 | p.shared_data_size = cparams->shared_data_size; |
| 1088 | p.gbb_data = cparams->gbb_data; |
| 1089 | p.gbb_size = cparams->gbb_size; |
| 1090 | p.fwmp = &fwmp; |
| 1091 | p.nv_context = &vnc; |
| 1092 | |
| 1093 | /* |
| 1094 | * This could be set to NULL, in which case the vboot header |
| 1095 | * information about the load address and size will be used. |
| 1096 | */ |
| 1097 | p.kernel_buffer = kparams->kernel_buffer; |
| 1098 | p.kernel_buffer_size = kparams->kernel_buffer_size; |
| 1099 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1100 | /* Clear output params in case we fail */ |
| 1101 | kparams->disk_handle = NULL; |
| 1102 | kparams->partition_number = 0; |
| 1103 | kparams->bootloader_address = 0; |
| 1104 | kparams->bootloader_size = 0; |
Furquan Shaikh | b7d1f03 | 2015-02-03 16:28:31 -0800 | [diff] [blame] | 1105 | kparams->flags = 0; |
Randall Spangler | 664096b | 2016-10-13 16:16:41 -0700 | [diff] [blame] | 1106 | memset(kparams->partition_guid, 0, sizeof(kparams->partition_guid)); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 1107 | |
Simon Glass | 527ba81 | 2013-07-25 08:48:47 -0600 | [diff] [blame] | 1108 | cparams->bmp = NULL; |
| 1109 | cparams->gbb = VbExMalloc(sizeof(*cparams->gbb)); |
| 1110 | retval = VbGbbReadHeader_static(cparams, cparams->gbb); |
| 1111 | if (VBERROR_SUCCESS != retval) |
| 1112 | goto VbSelectAndLoadKernel_exit; |
| 1113 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1114 | /* Do EC software sync if necessary */ |
Randall Spangler | f2a1dc0 | 2013-06-11 16:53:07 -0700 | [diff] [blame] | 1115 | if ((shared->flags & VBSD_EC_SOFTWARE_SYNC) && |
Simon Glass | 527ba81 | 2013-07-25 08:48:47 -0600 | [diff] [blame] | 1116 | !(cparams->gbb->flags & GBB_FLAG_DISABLE_EC_SOFTWARE_SYNC)) { |
Duncan Laurie | 731f8e8 | 2014-10-16 11:05:13 -0700 | [diff] [blame] | 1117 | int oprom_mismatch = 0; |
| 1118 | |
Randall Spangler | e778ada | 2014-07-15 16:14:21 -0700 | [diff] [blame] | 1119 | retval = VbEcSoftwareSync(0, cparams); |
Duncan Laurie | 731f8e8 | 2014-10-16 11:05:13 -0700 | [diff] [blame] | 1120 | /* Save reboot requested until after possible PD sync */ |
| 1121 | if (retval == VBERROR_VGA_OPROM_MISMATCH) |
| 1122 | oprom_mismatch = 1; |
| 1123 | else if (retval != VBERROR_SUCCESS) |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1124 | goto VbSelectAndLoadKernel_exit; |
Randall Spangler | 6014c04 | 2014-07-22 14:42:58 -0700 | [diff] [blame] | 1125 | |
| 1126 | #ifdef PD_SYNC |
Duncan Laurie | 277dc52 | 2014-08-11 12:30:04 -0700 | [diff] [blame] | 1127 | if (!(cparams->gbb->flags & |
| 1128 | GBB_FLAG_DISABLE_PD_SOFTWARE_SYNC)) { |
| 1129 | retval = VbEcSoftwareSync(1, cparams); |
Duncan Laurie | 731f8e8 | 2014-10-16 11:05:13 -0700 | [diff] [blame] | 1130 | if (retval == VBERROR_VGA_OPROM_MISMATCH) |
| 1131 | oprom_mismatch = 1; |
| 1132 | else if (retval != VBERROR_SUCCESS) |
Duncan Laurie | 277dc52 | 2014-08-11 12:30:04 -0700 | [diff] [blame] | 1133 | goto VbSelectAndLoadKernel_exit; |
| 1134 | } |
Randall Spangler | 6014c04 | 2014-07-22 14:42:58 -0700 | [diff] [blame] | 1135 | #endif |
Duncan Laurie | 731f8e8 | 2014-10-16 11:05:13 -0700 | [diff] [blame] | 1136 | |
| 1137 | /* Request reboot to unload VGA Option ROM */ |
| 1138 | if (oprom_mismatch) { |
| 1139 | retval = VBERROR_VGA_OPROM_MISMATCH; |
| 1140 | goto VbSelectAndLoadKernel_exit; |
| 1141 | } |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1142 | } |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 1143 | |
Shawn Nematbakhsh | b491bc8 | 2015-10-21 14:31:02 -0700 | [diff] [blame] | 1144 | /* EC verification (and possibily updating / jumping) is done */ |
| 1145 | retval = VbExEcVbootDone(!!shared->recovery_reason); |
| 1146 | if (retval != VBERROR_SUCCESS) |
| 1147 | goto VbSelectAndLoadKernel_exit; |
| 1148 | |
Hung-Te Lin | aee6bd6 | 2016-04-07 12:21:03 +0800 | [diff] [blame] | 1149 | /* Check if we need to cut-off battery. This must be done after EC |
| 1150 | * firmware updating and before kernel started. */ |
| 1151 | VbNvGet(&vnc, VBNV_BATTERY_CUTOFF_REQUEST, &battery_cutoff); |
| 1152 | if (battery_cutoff) { |
| 1153 | VBDEBUG(("Request to cut-off battery\n")); |
| 1154 | VbNvSet(&vnc, VBNV_BATTERY_CUTOFF_REQUEST, 0); |
| 1155 | VbExEcBatteryCutOff(); |
| 1156 | retval = VBERROR_SHUTDOWN_REQUESTED; |
| 1157 | goto VbSelectAndLoadKernel_exit; |
| 1158 | } |
| 1159 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1160 | /* Read kernel version from the TPM. Ignore errors in recovery mode. */ |
| 1161 | tpm_status = RollbackKernelRead(&shared->kernel_version_tpm); |
| 1162 | if (0 != tpm_status) { |
| 1163 | VBDEBUG(("Unable to get kernel versions from TPM\n")); |
| 1164 | if (!shared->recovery_reason) { |
| 1165 | VbSetRecoveryRequest(VBNV_RECOVERY_RW_TPM_R_ERROR); |
| 1166 | retval = VBERROR_TPM_READ_KERNEL; |
| 1167 | goto VbSelectAndLoadKernel_exit; |
| 1168 | } |
| 1169 | } |
| 1170 | shared->kernel_version_tpm_start = shared->kernel_version_tpm; |
Randall Spangler | d4faa06 | 2012-07-16 16:42:40 -0700 | [diff] [blame] | 1171 | |
Randall Spangler | 946abf1 | 2016-04-15 14:49:40 -0700 | [diff] [blame] | 1172 | /* Read FWMP. Ignore errors in recovery mode. */ |
| 1173 | if (cparams->gbb->flags & GBB_FLAG_DISABLE_FWMP) { |
Randall Spangler | 664096b | 2016-10-13 16:16:41 -0700 | [diff] [blame] | 1174 | memset(&fwmp, 0, sizeof(fwmp)); |
Randall Spangler | 946abf1 | 2016-04-15 14:49:40 -0700 | [diff] [blame] | 1175 | tpm_status = 0; |
| 1176 | } else { |
| 1177 | tpm_status = RollbackFwmpRead(&fwmp); |
| 1178 | } |
| 1179 | if (0 != tpm_status) { |
| 1180 | VBDEBUG(("Unable to get FWMP from TPM\n")); |
| 1181 | if (!shared->recovery_reason) { |
| 1182 | VbSetRecoveryRequest(VBNV_RECOVERY_RW_TPM_R_ERROR); |
| 1183 | retval = VBERROR_TPM_READ_FWMP; |
| 1184 | goto VbSelectAndLoadKernel_exit; |
| 1185 | } |
| 1186 | } |
| 1187 | |
Randall Spangler | 1589f94 | 2016-09-02 12:10:31 -0700 | [diff] [blame] | 1188 | /* Set up boot flags */ |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1189 | p.boot_flags = 0; |
| 1190 | if (shared->flags & VBSD_BOOT_DEV_SWITCH_ON) |
| 1191 | p.boot_flags |= BOOT_FLAG_DEVELOPER; |
Vadim Bendebury | b321dbb | 2012-08-01 18:44:05 -0700 | [diff] [blame] | 1192 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1193 | /* Handle separate normal and developer firmware builds. */ |
Randall Spangler | 64ca788 | 2011-07-11 15:46:19 -0700 | [diff] [blame] | 1194 | #if defined(VBOOT_FIRMWARE_TYPE_NORMAL) |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1195 | /* Normal-type firmware always acts like the dev switch is off. */ |
| 1196 | p.boot_flags &= ~BOOT_FLAG_DEVELOPER; |
Randall Spangler | 64ca788 | 2011-07-11 15:46:19 -0700 | [diff] [blame] | 1197 | #elif defined(VBOOT_FIRMWARE_TYPE_DEVELOPER) |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1198 | /* Developer-type firmware fails if the dev switch is off. */ |
| 1199 | if (!(p.boot_flags & BOOT_FLAG_DEVELOPER)) { |
| 1200 | /* |
| 1201 | * Dev firmware should be signed with a key that only verifies |
| 1202 | * when the dev switch is on, so we should never get here. |
| 1203 | */ |
| 1204 | VBDEBUG(("Developer firmware called with dev switch off!\n")); |
| 1205 | VbSetRecoveryRequest(VBNV_RECOVERY_RW_DEV_MISMATCH); |
| 1206 | retval = VBERROR_DEV_FIRMWARE_SWITCH_MISMATCH; |
| 1207 | goto VbSelectAndLoadKernel_exit; |
| 1208 | } |
Randall Spangler | 64ca788 | 2011-07-11 15:46:19 -0700 | [diff] [blame] | 1209 | #else |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1210 | /* |
| 1211 | * Recovery firmware, or merged normal+developer firmware. No need to |
| 1212 | * override flags. |
| 1213 | */ |
Randall Spangler | 64ca788 | 2011-07-11 15:46:19 -0700 | [diff] [blame] | 1214 | #endif |
| 1215 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1216 | /* Select boot path */ |
| 1217 | if (shared->recovery_reason) { |
| 1218 | /* Recovery boot */ |
| 1219 | p.boot_flags |= BOOT_FLAG_RECOVERY; |
| 1220 | retval = VbBootRecovery(cparams, &p); |
Sheng-Liang Song | 487a54b | 2014-07-26 21:34:28 -0700 | [diff] [blame] | 1221 | VbExEcEnteringMode(0, VB_EC_RECOVERY); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1222 | VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0, &vnc); |
Randall Spangler | 22e7bb2 | 2011-07-22 14:06:51 -0700 | [diff] [blame] | 1223 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1224 | } else if (p.boot_flags & BOOT_FLAG_DEVELOPER) { |
| 1225 | /* Developer boot */ |
| 1226 | retval = VbBootDeveloper(cparams, &p); |
Sheng-Liang Song | 487a54b | 2014-07-26 21:34:28 -0700 | [diff] [blame] | 1227 | VbExEcEnteringMode(0, VB_EC_DEVELOPER); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1228 | VbDisplayScreen(cparams, VB_SCREEN_BLANK, 0, &vnc); |
Randall Spangler | 22e7bb2 | 2011-07-22 14:06:51 -0700 | [diff] [blame] | 1229 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1230 | } else { |
| 1231 | /* Normal boot */ |
Sheng-Liang Song | 487a54b | 2014-07-26 21:34:28 -0700 | [diff] [blame] | 1232 | VbExEcEnteringMode(0, VB_EC_NORMAL); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1233 | retval = VbBootNormal(cparams, &p); |
Randall Spangler | 22e7bb2 | 2011-07-22 14:06:51 -0700 | [diff] [blame] | 1234 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1235 | if ((1 == shared->firmware_index) && |
| 1236 | (shared->flags & VBSD_FWB_TRIED)) { |
| 1237 | /* |
| 1238 | * Special cases for when we're trying a new firmware |
| 1239 | * B. These are needed because firmware updates also |
| 1240 | * usually change the kernel key, which means that the |
| 1241 | * B firmware can only boot a new kernel, and the old |
| 1242 | * firmware in A can only boot the previous kernel. |
| 1243 | * |
| 1244 | * Don't advance the TPM if we're trying a new firmware |
| 1245 | * B, because we don't yet know if the new kernel will |
| 1246 | * successfully boot. We still want to be able to fall |
| 1247 | * back to the previous firmware+kernel if the new |
| 1248 | * firmware+kernel fails. |
| 1249 | * |
| 1250 | * If we found only invalid kernels, reboot and try |
| 1251 | * again. This allows us to fall back to the previous |
| 1252 | * firmware+kernel instead of giving up and going to |
| 1253 | * recovery mode right away. We'll still go to |
| 1254 | * recovery mode if we run out of tries and the old |
| 1255 | * firmware can't find a kernel it likes. |
| 1256 | */ |
| 1257 | if (VBERROR_INVALID_KERNEL_FOUND == retval) { |
| 1258 | VBDEBUG(("Trying firmware B, " |
| 1259 | "and only found invalid kernels.\n")); |
| 1260 | VbSetRecoveryRequest(VBNV_RECOVERY_NOT_REQUESTED); |
| 1261 | goto VbSelectAndLoadKernel_exit; |
| 1262 | } |
| 1263 | } else { |
| 1264 | /* Not trying a new firmware B. */ |
Randall Spangler | 9927512 | 2011-07-26 16:26:59 -0700 | [diff] [blame] | 1265 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1266 | /* See if we need to update the TPM. */ |
| 1267 | VBDEBUG(("Checking if TPM kernel version needs " |
| 1268 | "advancing\n")); |
| 1269 | if (shared->kernel_version_tpm > |
| 1270 | shared->kernel_version_tpm_start) { |
| 1271 | tpm_status = RollbackKernelWrite( |
| 1272 | shared->kernel_version_tpm); |
| 1273 | if (0 != tpm_status) { |
| 1274 | VBDEBUG(("Error writing kernel " |
| 1275 | "versions to TPM.\n")); |
| 1276 | VbSetRecoveryRequest(VBNV_RECOVERY_RW_TPM_W_ERROR); |
| 1277 | retval = VBERROR_TPM_WRITE_KERNEL; |
| 1278 | goto VbSelectAndLoadKernel_exit; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | } |
Randall Spangler | 9927512 | 2011-07-26 16:26:59 -0700 | [diff] [blame] | 1283 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1284 | if (VBERROR_SUCCESS != retval) |
| 1285 | goto VbSelectAndLoadKernel_exit; |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 1286 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1287 | /* Save disk parameters */ |
| 1288 | kparams->disk_handle = p.disk_handle; |
| 1289 | kparams->partition_number = (uint32_t)p.partition_number; |
| 1290 | kparams->bootloader_address = p.bootloader_address; |
| 1291 | kparams->bootloader_size = (uint32_t)p.bootloader_size; |
Furquan Shaikh | b7d1f03 | 2015-02-03 16:28:31 -0800 | [diff] [blame] | 1292 | kparams->flags = p.flags; |
Randall Spangler | 664096b | 2016-10-13 16:16:41 -0700 | [diff] [blame] | 1293 | memcpy(kparams->partition_guid, p.partition_guid, |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1294 | sizeof(kparams->partition_guid)); |
Randall Spangler | 22e7bb2 | 2011-07-22 14:06:51 -0700 | [diff] [blame] | 1295 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1296 | /* Lock the kernel versions. Ignore errors in recovery mode. */ |
Shawn Nematbakhsh | 964144b | 2013-07-22 13:33:46 -0700 | [diff] [blame] | 1297 | tpm_status = RollbackKernelLock(shared->recovery_reason); |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1298 | if (0 != tpm_status) { |
| 1299 | VBDEBUG(("Error locking kernel versions.\n")); |
| 1300 | if (!shared->recovery_reason) { |
| 1301 | VbSetRecoveryRequest(VBNV_RECOVERY_RW_TPM_L_ERROR); |
| 1302 | retval = VBERROR_TPM_LOCK_KERNEL; |
| 1303 | goto VbSelectAndLoadKernel_exit; |
| 1304 | } |
| 1305 | } |
Randall Spangler | 22e7bb2 | 2011-07-22 14:06:51 -0700 | [diff] [blame] | 1306 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1307 | VbSelectAndLoadKernel_exit: |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 1308 | |
Simon Glass | 527ba81 | 2013-07-25 08:48:47 -0600 | [diff] [blame] | 1309 | VbApiKernelFree(cparams); |
| 1310 | |
Daisuke Nojiri | 3a63148 | 2015-10-20 18:31:10 -0700 | [diff] [blame] | 1311 | VbNvCommit(); |
Randall Spangler | 22e7bb2 | 2011-07-22 14:06:51 -0700 | [diff] [blame] | 1312 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1313 | /* Stop timer */ |
| 1314 | shared->timer_vb_select_and_load_kernel_exit = VbExGetTimer(); |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 1315 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1316 | kparams->kernel_buffer = p.kernel_buffer; |
| 1317 | kparams->kernel_buffer_size = p.kernel_buffer_size; |
Randall Spangler | 9619112 | 2011-07-08 14:01:54 -0700 | [diff] [blame] | 1318 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1319 | VBDEBUG(("VbSelectAndLoadKernel() returning %d\n", (int)retval)); |
Vadim Bendebury | b321dbb | 2012-08-01 18:44:05 -0700 | [diff] [blame] | 1320 | |
Randall Spangler | 29accd9 | 2013-02-04 14:01:04 -0800 | [diff] [blame] | 1321 | /* Pass through return value from boot path */ |
| 1322 | return retval; |
Randall Spangler | 1b1998d | 2011-07-01 16:12:47 -0700 | [diff] [blame] | 1323 | } |
Furquan Shaikh | f274360 | 2015-05-21 14:39:11 -0700 | [diff] [blame] | 1324 | |
| 1325 | VbError_t VbVerifyMemoryBootImage(VbCommonParams *cparams, |
| 1326 | VbSelectAndLoadKernelParams *kparams, |
| 1327 | void *boot_image, |
| 1328 | size_t image_size) |
| 1329 | { |
| 1330 | VbError_t retval; |
| 1331 | VbPublicKey* kernel_subkey = NULL; |
| 1332 | uint8_t *kbuf; |
| 1333 | VbKeyBlockHeader *key_block; |
| 1334 | VbSharedDataHeader *shared = |
| 1335 | (VbSharedDataHeader *)cparams->shared_data_blob; |
| 1336 | RSAPublicKey *data_key = NULL; |
| 1337 | VbKernelPreambleHeader *preamble; |
| 1338 | uint64_t body_offset; |
| 1339 | int hash_only = 0; |
| 1340 | int dev_switch; |
Furquan Shaikh | 5548455 | 2015-10-28 13:01:27 -0700 | [diff] [blame] | 1341 | uint32_t allow_fastboot_full_cap = 0; |
Furquan Shaikh | f274360 | 2015-05-21 14:39:11 -0700 | [diff] [blame] | 1342 | |
| 1343 | if ((boot_image == NULL) || (image_size == 0)) |
| 1344 | return VBERROR_INVALID_PARAMETER; |
| 1345 | |
| 1346 | /* Clear output params in case we fail. */ |
| 1347 | kparams->disk_handle = NULL; |
| 1348 | kparams->partition_number = 0; |
| 1349 | kparams->bootloader_address = 0; |
| 1350 | kparams->bootloader_size = 0; |
| 1351 | kparams->flags = 0; |
Randall Spangler | 664096b | 2016-10-13 16:16:41 -0700 | [diff] [blame] | 1352 | memset(kparams->partition_guid, 0, sizeof(kparams->partition_guid)); |
Furquan Shaikh | f274360 | 2015-05-21 14:39:11 -0700 | [diff] [blame] | 1353 | |
Furquan Shaikh | f274360 | 2015-05-21 14:39:11 -0700 | [diff] [blame] | 1354 | kbuf = boot_image; |
Furquan Shaikh | f274360 | 2015-05-21 14:39:11 -0700 | [diff] [blame] | 1355 | |
| 1356 | /* Read GBB Header */ |
| 1357 | cparams->bmp = NULL; |
| 1358 | cparams->gbb = VbExMalloc(sizeof(*cparams->gbb)); |
| 1359 | retval = VbGbbReadHeader_static(cparams, cparams->gbb); |
| 1360 | if (VBERROR_SUCCESS != retval) { |
| 1361 | VBDEBUG(("Gbb read header failed.\n")); |
| 1362 | return retval; |
| 1363 | } |
| 1364 | |
| 1365 | /* |
| 1366 | * We don't care verifying the image if: |
| 1367 | * 1. dev-mode switch is on and |
Furquan Shaikh | 5548455 | 2015-10-28 13:01:27 -0700 | [diff] [blame] | 1368 | * 2a. GBB_FLAG_FORCE_DEV_BOOT_FASTBOOT_FULL_CAP is set, or |
| 1369 | * 2b. DEV_BOOT_FASTBOOT_FULL_CAP flag is set in NvStorage |
Furquan Shaikh | f274360 | 2015-05-21 14:39:11 -0700 | [diff] [blame] | 1370 | * |
| 1371 | * Check only the integrity of the image. |
| 1372 | */ |
| 1373 | dev_switch = shared->flags & VBSD_BOOT_DEV_SWITCH_ON; |
Furquan Shaikh | 5548455 | 2015-10-28 13:01:27 -0700 | [diff] [blame] | 1374 | |
| 1375 | VbExNvStorageRead(vnc.raw); |
| 1376 | VbNvSetup(&vnc); |
| 1377 | VbNvGet(&vnc, VBNV_DEV_BOOT_FASTBOOT_FULL_CAP, |
| 1378 | &allow_fastboot_full_cap); |
| 1379 | |
| 1380 | if (0 == allow_fastboot_full_cap) { |
| 1381 | allow_fastboot_full_cap = !!(cparams->gbb->flags & |
| 1382 | GBB_FLAG_FORCE_DEV_BOOT_FASTBOOT_FULL_CAP); |
| 1383 | } |
| 1384 | |
| 1385 | if (dev_switch && allow_fastboot_full_cap) { |
Furquan Shaikh | f274360 | 2015-05-21 14:39:11 -0700 | [diff] [blame] | 1386 | VBDEBUG(("Only performing integrity-check.\n")); |
| 1387 | hash_only = 1; |
| 1388 | } else { |
| 1389 | /* Get recovery key. */ |
| 1390 | retval = VbGbbReadRecoveryKey(cparams, &kernel_subkey); |
| 1391 | if (VBERROR_SUCCESS != retval) { |
| 1392 | VBDEBUG(("Gbb Read Recovery key failed.\n")); |
| 1393 | return retval; |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | /* If we fail at any step, retval returned would be invalid kernel. */ |
| 1398 | retval = VBERROR_INVALID_KERNEL_FOUND; |
| 1399 | |
| 1400 | /* Verify the key block. */ |
Furquan Shaikh | 04e2338 | 2015-05-29 18:00:46 -0700 | [diff] [blame] | 1401 | key_block = (VbKeyBlockHeader *)kbuf; |
Furquan Shaikh | f274360 | 2015-05-21 14:39:11 -0700 | [diff] [blame] | 1402 | if (0 != KeyBlockVerify(key_block, image_size, kernel_subkey, |
| 1403 | hash_only)) { |
| 1404 | VBDEBUG(("Verifying key block signature/hash failed.\n")); |
| 1405 | goto fail; |
| 1406 | } |
| 1407 | |
| 1408 | /* Check the key block flags against the current boot mode. */ |
| 1409 | if (!(key_block->key_block_flags & |
| 1410 | (dev_switch ? KEY_BLOCK_FLAG_DEVELOPER_1 : |
| 1411 | KEY_BLOCK_FLAG_DEVELOPER_0))) { |
| 1412 | VBDEBUG(("Key block developer flag mismatch.\n")); |
| 1413 | if (hash_only == 0) |
| 1414 | goto fail; |
| 1415 | } |
| 1416 | |
| 1417 | if (!(key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_1)) { |
| 1418 | VBDEBUG(("Key block recovery flag mismatch.\n")); |
| 1419 | if (hash_only == 0) |
| 1420 | goto fail; |
| 1421 | } |
| 1422 | |
| 1423 | /* Get key for preamble/data verification from the key block. */ |
| 1424 | data_key = PublicKeyToRSA(&key_block->data_key); |
| 1425 | if (!data_key) { |
| 1426 | VBDEBUG(("Data key bad.\n")); |
| 1427 | goto fail; |
| 1428 | } |
| 1429 | |
| 1430 | /* Verify the preamble, which follows the key block */ |
Furquan Shaikh | 04e2338 | 2015-05-29 18:00:46 -0700 | [diff] [blame] | 1431 | preamble = (VbKernelPreambleHeader *)(kbuf + key_block->key_block_size); |
Furquan Shaikh | f274360 | 2015-05-21 14:39:11 -0700 | [diff] [blame] | 1432 | if ((0 != VerifyKernelPreamble(preamble, |
| 1433 | image_size - |
| 1434 | key_block->key_block_size, |
| 1435 | data_key))) { |
| 1436 | VBDEBUG(("Preamble verification failed.\n")); |
| 1437 | goto fail; |
| 1438 | } |
| 1439 | |
| 1440 | VBDEBUG(("Kernel preamble is good.\n")); |
| 1441 | |
| 1442 | /* Verify kernel data */ |
Furquan Shaikh | 04e2338 | 2015-05-29 18:00:46 -0700 | [diff] [blame] | 1443 | body_offset = key_block->key_block_size + preamble->preamble_size; |
Furquan Shaikh | f274360 | 2015-05-21 14:39:11 -0700 | [diff] [blame] | 1444 | if (0 != VerifyData((const uint8_t *)(kbuf + body_offset), |
| 1445 | image_size - body_offset, |
| 1446 | &preamble->body_signature, data_key)) { |
| 1447 | VBDEBUG(("Kernel data verification failed.\n")); |
| 1448 | goto fail; |
| 1449 | } |
| 1450 | |
| 1451 | VBDEBUG(("Kernel is good.\n")); |
| 1452 | |
| 1453 | /* Fill in output parameters. */ |
| 1454 | kparams->kernel_buffer = kbuf + body_offset; |
| 1455 | kparams->kernel_buffer_size = image_size - body_offset; |
| 1456 | kparams->bootloader_address = preamble->bootloader_address; |
| 1457 | kparams->bootloader_size = preamble->bootloader_size; |
| 1458 | if (VbKernelHasFlags(preamble) == VBOOT_SUCCESS) |
| 1459 | kparams->flags = preamble->flags; |
| 1460 | |
| 1461 | retval = VBERROR_SUCCESS; |
| 1462 | |
| 1463 | fail: |
| 1464 | VbApiKernelFree(cparams); |
| 1465 | if (NULL != data_key) |
| 1466 | RSAPublicKeyFree(data_key); |
| 1467 | if (NULL != kernel_subkey) |
| 1468 | VbExFree(kernel_subkey); |
| 1469 | return retval; |
| 1470 | } |
Furquan Shaikh | 773b5ac | 2015-05-25 21:49:11 -0700 | [diff] [blame] | 1471 | |
| 1472 | VbError_t VbUnlockDevice(void) |
| 1473 | { |
| 1474 | VBDEBUG(("%s() Enabling dev-mode...\n", __func__)); |
| 1475 | if (TPM_SUCCESS != SetVirtualDevMode(1)) |
| 1476 | return VBERROR_TPM_SET_BOOT_MODE_STATE; |
| 1477 | |
| 1478 | VBDEBUG(("%s() Mode change will take effect on next reboot.\n", |
| 1479 | __func__)); |
| 1480 | return VBERROR_SUCCESS; |
| 1481 | } |
| 1482 | |
| 1483 | VbError_t VbLockDevice(void) |
| 1484 | { |
| 1485 | VbExNvStorageRead(vnc.raw); |
| 1486 | VbNvSetup(&vnc); |
| 1487 | |
| 1488 | VBDEBUG(("%s() - Storing request to leave dev-mode.\n", |
| 1489 | __func__)); |
| 1490 | VbNvSet(&vnc, VBNV_DISABLE_DEV_REQUEST, |
| 1491 | 1); |
| 1492 | |
Daisuke Nojiri | 3a63148 | 2015-10-20 18:31:10 -0700 | [diff] [blame] | 1493 | VbNvCommit(); |
Furquan Shaikh | 773b5ac | 2015-05-25 21:49:11 -0700 | [diff] [blame] | 1494 | |
| 1495 | VBDEBUG(("%s() Mode change will take effect on next reboot.\n", |
| 1496 | __func__)); |
| 1497 | |
| 1498 | return VBERROR_SUCCESS; |
| 1499 | } |