vboot_api_kernel.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * High-level firmware wrapper API - entry points for kernel selection
  6. */
  7. #include "sysincludes.h"
  8. #include "2sysincludes.h"
  9. #include "2common.h"
  10. #include "2misc.h"
  11. #include "2nvstorage.h"
  12. #include "2rsa.h"
  13. #include "ec_sync.h"
  14. #include "gbb_access.h"
  15. #include "gbb_header.h"
  16. #include "load_kernel_fw.h"
  17. #include "region.h"
  18. #include "rollback_index.h"
  19. #include "utility.h"
  20. #include "vb2_common.h"
  21. #include "vboot_api.h"
  22. #include "vboot_common.h"
  23. #include "vboot_kernel.h"
  24. #include "vboot_nvstorage.h"
  25. /* Global variables */
  26. static VbNvContext vnc;
  27. static struct RollbackSpaceFwmp fwmp;
  28. static LoadKernelParams lkp;
  29. static struct vb2_context ctx;
  30. static uint8_t *unaligned_workbuf;
  31. #ifdef CHROMEOS_ENVIRONMENT
  32. /* Global variable accessors for unit tests */
  33. struct RollbackSpaceFwmp *VbApiKernelGetFwmp(void)
  34. {
  35. return &fwmp;
  36. }
  37. struct LoadKernelParams *VbApiKernelGetParams(void)
  38. {
  39. return &lkp;
  40. }
  41. #endif
  42. /**
  43. * Set recovery request (called from vboot_api_kernel.c functions only)
  44. */
  45. static void VbSetRecoveryRequest(struct vb2_context *ctx,
  46. uint32_t recovery_request)
  47. {
  48. VB2_DEBUG("VbSetRecoveryRequest(%d)\n", (int)recovery_request);
  49. vb2_nv_set(ctx, VB2_NV_RECOVERY_REQUEST, recovery_request);
  50. }
  51. static void VbNvLoad(void)
  52. {
  53. VbExNvStorageRead(vnc.raw);
  54. VbNvSetup(&vnc);
  55. }
  56. static void VbNvCommit(void)
  57. {
  58. VbNvTeardown(&vnc);
  59. if (vnc.raw_changed)
  60. VbExNvStorageWrite(vnc.raw);
  61. }
  62. void vb2_nv_commit(struct vb2_context *ctx)
  63. {
  64. /* Copy nvdata back to old vboot1 nv context if needed */
  65. if (ctx->flags & VB2_CONTEXT_NVDATA_CHANGED) {
  66. memcpy(vnc.raw, ctx->nvdata, VB2_NVDATA_SIZE);
  67. vnc.raw_changed = 1;
  68. ctx->flags &= ~VB2_CONTEXT_NVDATA_CHANGED;
  69. }
  70. VbNvCommit();
  71. }
  72. uint32_t vb2_get_fwmp_flags(void)
  73. {
  74. return fwmp.flags;
  75. }
  76. /**
  77. * Attempt loading a kernel from the specified type(s) of disks.
  78. *
  79. * If successful, sets p->disk_handle to the disk for the kernel and returns
  80. * VBERROR_SUCCESS.
  81. *
  82. * @param ctx Vboot context
  83. * @param cparams Vboot common params
  84. * @param p Parameters for loading kernel
  85. * @param get_info_flags Flags to pass to VbExDiskGetInfo()
  86. * @return VBERROR_SUCCESS, VBERROR_NO_DISK_FOUND if no disks of the specified
  87. * type were found, or other non-zero VBERROR_ codes for other failures.
  88. */
  89. uint32_t VbTryLoadKernel(struct vb2_context *ctx, VbCommonParams *cparams,
  90. uint32_t get_info_flags)
  91. {
  92. VbError_t retval = VBERROR_UNKNOWN;
  93. VbDiskInfo* disk_info = NULL;
  94. uint32_t disk_count = 0;
  95. uint32_t i;
  96. VB2_DEBUG("VbTryLoadKernel() start, get_info_flags=0x%x\n",
  97. (unsigned)get_info_flags);
  98. lkp.fwmp = &fwmp;
  99. lkp.nv_context = &vnc;
  100. lkp.disk_handle = NULL;
  101. /* Find disks */
  102. if (VBERROR_SUCCESS != VbExDiskGetInfo(&disk_info, &disk_count,
  103. get_info_flags))
  104. disk_count = 0;
  105. VB2_DEBUG("VbTryLoadKernel() found %d disks\n", (int)disk_count);
  106. if (0 == disk_count) {
  107. VbSetRecoveryRequest(ctx, VBNV_RECOVERY_RW_NO_DISK);
  108. return VBERROR_NO_DISK_FOUND;
  109. }
  110. /* Loop over disks */
  111. for (i = 0; i < disk_count; i++) {
  112. VB2_DEBUG("VbTryLoadKernel() trying disk %d\n", (int)i);
  113. /*
  114. * Sanity-check what we can. FWIW, VbTryLoadKernel() is always
  115. * called with only a single bit set in get_info_flags.
  116. *
  117. * Ensure 512-byte sectors and non-trivially sized disk (for
  118. * cgptlib) and that we got a partition with only the flags we
  119. * asked for.
  120. */
  121. if (512 != disk_info[i].bytes_per_lba ||
  122. 16 > disk_info[i].lba_count ||
  123. get_info_flags != (disk_info[i].flags &
  124. ~VB_DISK_FLAG_EXTERNAL_GPT)) {
  125. VB2_DEBUG(" skipping: bytes_per_lba=%" PRIu64
  126. " lba_count=%" PRIu64 " flags=0x%x\n",
  127. disk_info[i].bytes_per_lba,
  128. disk_info[i].lba_count,
  129. disk_info[i].flags);
  130. continue;
  131. }
  132. lkp.disk_handle = disk_info[i].handle;
  133. lkp.bytes_per_lba = disk_info[i].bytes_per_lba;
  134. lkp.gpt_lba_count = disk_info[i].lba_count;
  135. lkp.streaming_lba_count = disk_info[i].streaming_lba_count
  136. ?: lkp.gpt_lba_count;
  137. lkp.boot_flags |= disk_info[i].flags & VB_DISK_FLAG_EXTERNAL_GPT
  138. ? BOOT_FLAG_EXTERNAL_GPT : 0;
  139. retval = LoadKernel(ctx, &lkp, cparams);
  140. VB2_DEBUG("VbTryLoadKernel() LoadKernel() = %d\n", retval);
  141. /*
  142. * Stop now if we found a kernel.
  143. *
  144. * TODO: If recovery requested, should track the farthest we
  145. * get, instead of just returning the value from the last disk
  146. * attempted.
  147. */
  148. if (VBERROR_SUCCESS == retval)
  149. break;
  150. }
  151. /* If we didn't find any good kernels, don't return a disk handle. */
  152. if (VBERROR_SUCCESS != retval) {
  153. VbSetRecoveryRequest(ctx, VBNV_RECOVERY_RW_NO_KERNEL);
  154. lkp.disk_handle = NULL;
  155. }
  156. VbExDiskFreeInfo(disk_info, lkp.disk_handle);
  157. /*
  158. * Pass through return code. Recovery reason (if any) has already been
  159. * set by LoadKernel().
  160. */
  161. return retval;
  162. }
  163. VbError_t VbBootNormal(struct vb2_context *ctx, VbCommonParams *cparams)
  164. {
  165. VbSharedDataHeader *shared =
  166. (VbSharedDataHeader *)cparams->shared_data_blob;
  167. /* Boot from fixed disk only */
  168. VB2_DEBUG("Entering\n");
  169. VbError_t rv = VbTryLoadKernel(ctx, cparams, VB_DISK_FLAG_FIXED);
  170. VB2_DEBUG("Checking if TPM kernel version needs advancing\n");
  171. if ((1 == shared->firmware_index) && (shared->flags & VBSD_FWB_TRIED)) {
  172. /*
  173. * Special cases for when we're trying a new firmware B. These
  174. * are needed because firmware updates also usually change the
  175. * kernel key, which means that the B firmware can only boot a
  176. * new kernel, and the old firmware in A can only boot the
  177. * previous kernel.
  178. *
  179. * Don't advance the TPM if we're trying a new firmware B,
  180. * because we don't yet know if the new kernel will
  181. * successfully boot. We still want to be able to fall back to
  182. * the previous firmware+kernel if the new firmware+kernel
  183. * fails.
  184. *
  185. * If we found only invalid kernels, reboot and try again.
  186. * This allows us to fall back to the previous firmware+kernel
  187. * instead of giving up and going to recovery mode right away.
  188. * We'll still go to recovery mode if we run out of tries and
  189. * the old firmware can't find a kernel it likes.
  190. */
  191. if (rv == VBERROR_INVALID_KERNEL_FOUND) {
  192. VB2_DEBUG("Trying FW B; only found invalid kernels.\n");
  193. VbSetRecoveryRequest(ctx, VBNV_RECOVERY_NOT_REQUESTED);
  194. }
  195. return rv;
  196. }
  197. if ((shared->kernel_version_tpm > shared->kernel_version_tpm_start) &&
  198. RollbackKernelWrite(shared->kernel_version_tpm)) {
  199. VB2_DEBUG("Error writing kernel versions to TPM.\n");
  200. VbSetRecoveryRequest(ctx, VBNV_RECOVERY_RW_TPM_W_ERROR);
  201. return VBERROR_TPM_WRITE_KERNEL;
  202. }
  203. return rv;
  204. }
  205. /* This function is also used by tests */
  206. void VbApiKernelFree(VbCommonParams *cparams)
  207. {
  208. /* VbSelectAndLoadKernel() always allocates this, tests don't */
  209. if (cparams->gbb) {
  210. free(cparams->gbb);
  211. cparams->gbb = NULL;
  212. }
  213. if (cparams->bmp) {
  214. free(cparams->bmp);
  215. cparams->bmp = NULL;
  216. }
  217. }
  218. static VbError_t vb2_kernel_setup(VbCommonParams *cparams,
  219. VbSelectAndLoadKernelParams *kparams)
  220. {
  221. VbSharedDataHeader *shared =
  222. (VbSharedDataHeader *)cparams->shared_data_blob;
  223. /* Start timer */
  224. shared->timer_vb_select_and_load_kernel_enter = VbExGetTimer();
  225. /*
  226. * Set up vboot context.
  227. *
  228. * TODO: Propagate this up to higher API levels, and use more of the
  229. * context fields (e.g. secdatak) and flags.
  230. */
  231. memset(&ctx, 0, sizeof(ctx));
  232. VbNvLoad();
  233. memcpy(ctx.nvdata, vnc.raw, VB2_NVDATA_SIZE);
  234. if (shared->recovery_reason)
  235. ctx.flags |= VB2_CONTEXT_RECOVERY_MODE;
  236. if (shared->flags & VBSD_BOOT_DEV_SWITCH_ON)
  237. ctx.flags |= VB2_CONTEXT_DEVELOPER_MODE;
  238. ctx.workbuf_size = VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE +
  239. VB2_WORKBUF_ALIGN;
  240. unaligned_workbuf = ctx.workbuf = malloc(ctx.workbuf_size);
  241. if (!unaligned_workbuf) {
  242. VB2_DEBUG("Can't allocate work buffer\n");
  243. VbSetRecoveryRequest(&ctx, VB2_RECOVERY_RW_SHARED_DATA);
  244. return VBERROR_INIT_SHARED_DATA;
  245. }
  246. if (VB2_SUCCESS != vb2_align(&ctx.workbuf, &ctx.workbuf_size,
  247. VB2_WORKBUF_ALIGN,
  248. VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE)) {
  249. VB2_DEBUG("Can't align work buffer\n");
  250. VbSetRecoveryRequest(&ctx, VB2_RECOVERY_RW_SHARED_DATA);
  251. return VBERROR_INIT_SHARED_DATA;
  252. }
  253. if (VB2_SUCCESS != vb2_init_context(&ctx)) {
  254. VB2_DEBUG("Can't init vb2_context\n");
  255. free(unaligned_workbuf);
  256. VbSetRecoveryRequest(&ctx, VB2_RECOVERY_RW_SHARED_DATA);
  257. return VBERROR_INIT_SHARED_DATA;
  258. }
  259. struct vb2_shared_data *sd = vb2_get_sd(&ctx);
  260. sd->recovery_reason = shared->recovery_reason;
  261. /*
  262. * If we're in recovery mode just to do memory retraining, all we
  263. * need to do is reboot.
  264. */
  265. if (shared->recovery_reason == VBNV_RECOVERY_TRAIN_AND_REBOOT) {
  266. VB2_DEBUG("Reboot after retraining in recovery.\n");
  267. return VBERROR_REBOOT_REQUIRED;
  268. }
  269. /* Fill in params for calls to LoadKernel() */
  270. memset(&lkp, 0, sizeof(lkp));
  271. lkp.kernel_buffer = kparams->kernel_buffer;
  272. lkp.kernel_buffer_size = kparams->kernel_buffer_size;
  273. /* Clear output params in case we fail */
  274. kparams->disk_handle = NULL;
  275. kparams->partition_number = 0;
  276. kparams->bootloader_address = 0;
  277. kparams->bootloader_size = 0;
  278. kparams->flags = 0;
  279. memset(kparams->partition_guid, 0, sizeof(kparams->partition_guid));
  280. /* Read GBB header, since we'll needs flags from it */
  281. cparams->bmp = NULL;
  282. cparams->gbb = malloc(sizeof(*cparams->gbb));
  283. uint32_t retval = VbGbbReadHeader_static(cparams, cparams->gbb);
  284. if (retval)
  285. return retval;
  286. /* Read kernel version from the TPM. Ignore errors in recovery mode. */
  287. if (RollbackKernelRead(&shared->kernel_version_tpm)) {
  288. VB2_DEBUG("Unable to get kernel versions from TPM\n");
  289. if (!shared->recovery_reason) {
  290. VbSetRecoveryRequest(&ctx,
  291. VBNV_RECOVERY_RW_TPM_R_ERROR);
  292. return VBERROR_TPM_READ_KERNEL;
  293. }
  294. }
  295. shared->kernel_version_tpm_start = shared->kernel_version_tpm;
  296. /* Read FWMP. Ignore errors in recovery mode. */
  297. if (cparams->gbb->flags & GBB_FLAG_DISABLE_FWMP) {
  298. memset(&fwmp, 0, sizeof(fwmp));
  299. } else if (RollbackFwmpRead(&fwmp)) {
  300. VB2_DEBUG("Unable to get FWMP from TPM\n");
  301. if (!shared->recovery_reason) {
  302. VbSetRecoveryRequest(&ctx,
  303. VBNV_RECOVERY_RW_TPM_R_ERROR);
  304. return VBERROR_TPM_READ_FWMP;
  305. }
  306. }
  307. return VBERROR_SUCCESS;
  308. }
  309. static VbError_t vb2_kernel_phase4(VbCommonParams *cparams,
  310. VbSelectAndLoadKernelParams *kparams)
  311. {
  312. VbSharedDataHeader *shared =
  313. (VbSharedDataHeader *)cparams->shared_data_blob;
  314. /* Save disk parameters */
  315. kparams->disk_handle = lkp.disk_handle;
  316. kparams->partition_number = lkp.partition_number;
  317. kparams->bootloader_address = lkp.bootloader_address;
  318. kparams->bootloader_size = lkp.bootloader_size;
  319. kparams->flags = lkp.flags;
  320. kparams->kernel_buffer = lkp.kernel_buffer;
  321. kparams->kernel_buffer_size = lkp.kernel_buffer_size;
  322. memcpy(kparams->partition_guid, lkp.partition_guid,
  323. sizeof(kparams->partition_guid));
  324. /* Lock the kernel versions if not in recovery mode */
  325. if (!shared->recovery_reason &&
  326. RollbackKernelLock(shared->recovery_reason)) {
  327. VB2_DEBUG("Error locking kernel versions.\n");
  328. VbSetRecoveryRequest(&ctx, VBNV_RECOVERY_RW_TPM_L_ERROR);
  329. return VBERROR_TPM_LOCK_KERNEL;
  330. }
  331. return VBERROR_SUCCESS;
  332. }
  333. static void vb2_kernel_cleanup(struct vb2_context *ctx, VbCommonParams *cparams)
  334. {
  335. VbSharedDataHeader *shared =
  336. (VbSharedDataHeader *)cparams->shared_data_blob;
  337. /*
  338. * Clean up vboot context.
  339. *
  340. * TODO: This should propagate up to higher levels
  341. */
  342. /* Free buffers */
  343. free(unaligned_workbuf);
  344. VbApiKernelFree(cparams);
  345. vb2_nv_commit(ctx);
  346. /* Stop timer */
  347. shared->timer_vb_select_and_load_kernel_exit = VbExGetTimer();
  348. }
  349. VbError_t VbSelectAndLoadKernel(VbCommonParams *cparams,
  350. VbSelectAndLoadKernelParams *kparams)
  351. {
  352. VbSharedDataHeader *shared =
  353. (VbSharedDataHeader *)cparams->shared_data_blob;
  354. VbError_t retval = vb2_kernel_setup(cparams, kparams);
  355. if (retval)
  356. goto VbSelectAndLoadKernel_exit;
  357. /*
  358. * Do EC software sync if necessary. This has UI, but it's just a
  359. * single non-interactive WAIT screen.
  360. */
  361. retval = ec_sync_all(&ctx, cparams);
  362. if (retval)
  363. goto VbSelectAndLoadKernel_exit;
  364. /* Select boot path */
  365. if (shared->recovery_reason) {
  366. /* Recovery boot. This has UI. */
  367. if (kparams->inflags & VB_SALK_INFLAGS_ENABLE_DETACHABLE_UI)
  368. retval = VbBootRecoveryMenu(&ctx, cparams);
  369. else
  370. retval = VbBootRecovery(&ctx, cparams);
  371. VbExEcEnteringMode(0, VB_EC_RECOVERY);
  372. } else if (shared->flags & VBSD_BOOT_DEV_SWITCH_ON) {
  373. /* Developer boot. This has UI. */
  374. if (kparams->inflags & VB_SALK_INFLAGS_ENABLE_DETACHABLE_UI)
  375. retval = VbBootDeveloperMenu(&ctx, cparams);
  376. else
  377. retval = VbBootDeveloper(&ctx, cparams);
  378. VbExEcEnteringMode(0, VB_EC_DEVELOPER);
  379. } else {
  380. /* Normal boot */
  381. retval = VbBootNormal(&ctx, cparams);
  382. VbExEcEnteringMode(0, VB_EC_NORMAL);
  383. }
  384. VbSelectAndLoadKernel_exit:
  385. if (VBERROR_SUCCESS == retval)
  386. retval = vb2_kernel_phase4(cparams, kparams);
  387. vb2_kernel_cleanup(&ctx, cparams);
  388. /* Pass through return value from boot path */
  389. VB2_DEBUG("Returning %d\n", (int)retval);
  390. return retval;
  391. }
  392. VbError_t VbVerifyMemoryBootImage(VbCommonParams *cparams,
  393. VbSelectAndLoadKernelParams *kparams,
  394. void *boot_image,
  395. size_t image_size)
  396. {
  397. VbError_t retval;
  398. VbPublicKey* kernel_subkey = NULL;
  399. uint8_t *kbuf;
  400. VbKeyBlockHeader *key_block;
  401. VbSharedDataHeader *shared =
  402. (VbSharedDataHeader *)cparams->shared_data_blob;
  403. VbKernelPreambleHeader *preamble;
  404. uint64_t body_offset;
  405. int hash_only = 0;
  406. int dev_switch;
  407. uint32_t allow_fastboot_full_cap = 0;
  408. uint8_t *workbuf = NULL;
  409. struct vb2_workbuf wb;
  410. if ((boot_image == NULL) || (image_size == 0))
  411. return VBERROR_INVALID_PARAMETER;
  412. /* Clear output params in case we fail. */
  413. kparams->disk_handle = NULL;
  414. kparams->partition_number = 0;
  415. kparams->bootloader_address = 0;
  416. kparams->bootloader_size = 0;
  417. kparams->flags = 0;
  418. memset(kparams->partition_guid, 0, sizeof(kparams->partition_guid));
  419. kbuf = boot_image;
  420. /* Read GBB Header */
  421. cparams->bmp = NULL;
  422. cparams->gbb = malloc(sizeof(*cparams->gbb));
  423. retval = VbGbbReadHeader_static(cparams, cparams->gbb);
  424. if (VBERROR_SUCCESS != retval) {
  425. VB2_DEBUG("Gbb read header failed.\n");
  426. return retval;
  427. }
  428. /*
  429. * We don't care verifying the image if:
  430. * 1. dev-mode switch is on and
  431. * 2a. GBB_FLAG_FORCE_DEV_BOOT_FASTBOOT_FULL_CAP is set, or
  432. * 2b. DEV_BOOT_FASTBOOT_FULL_CAP flag is set in NvStorage
  433. *
  434. * Check only the integrity of the image.
  435. */
  436. dev_switch = shared->flags & VBSD_BOOT_DEV_SWITCH_ON;
  437. VbNvLoad();
  438. VbNvGet(&vnc, VB2_NV_DEV_BOOT_FASTBOOT_FULL_CAP,
  439. &allow_fastboot_full_cap);
  440. if (0 == allow_fastboot_full_cap) {
  441. allow_fastboot_full_cap = !!(cparams->gbb->flags &
  442. GBB_FLAG_FORCE_DEV_BOOT_FASTBOOT_FULL_CAP);
  443. }
  444. if (dev_switch && allow_fastboot_full_cap) {
  445. VB2_DEBUG("Only performing integrity-check.\n");
  446. hash_only = 1;
  447. } else {
  448. /* Get recovery key. */
  449. retval = VbGbbReadRecoveryKey(cparams, &kernel_subkey);
  450. if (VBERROR_SUCCESS != retval) {
  451. VB2_DEBUG("Gbb Read Recovery key failed.\n");
  452. return retval;
  453. }
  454. }
  455. /* If we fail at any step, retval returned would be invalid kernel. */
  456. retval = VBERROR_INVALID_KERNEL_FOUND;
  457. /* Allocate work buffer */
  458. workbuf = (uint8_t *)malloc(VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE);
  459. if (!workbuf)
  460. goto fail;
  461. vb2_workbuf_init(&wb, workbuf, VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE);
  462. /* Verify the key block. */
  463. key_block = (VbKeyBlockHeader *)kbuf;
  464. struct vb2_keyblock *keyblock2 = (struct vb2_keyblock *)kbuf;
  465. int rv;
  466. if (hash_only) {
  467. rv = vb2_verify_keyblock_hash(keyblock2, image_size, &wb);
  468. } else {
  469. /* Unpack kernel subkey */
  470. struct vb2_public_key kernel_subkey2;
  471. if (VB2_SUCCESS !=
  472. vb2_unpack_key(&kernel_subkey2,
  473. (struct vb2_packed_key *)kernel_subkey)) {
  474. VB2_DEBUG("Unable to unpack kernel subkey\n");
  475. goto fail;
  476. }
  477. rv = vb2_verify_keyblock(keyblock2, image_size,
  478. &kernel_subkey2, &wb);
  479. }
  480. if (VB2_SUCCESS != rv) {
  481. VB2_DEBUG("Verifying key block signature/hash failed.\n");
  482. goto fail;
  483. }
  484. /* Check the key block flags against the current boot mode. */
  485. if (!(key_block->key_block_flags &
  486. (dev_switch ? KEY_BLOCK_FLAG_DEVELOPER_1 :
  487. KEY_BLOCK_FLAG_DEVELOPER_0))) {
  488. VB2_DEBUG("Key block developer flag mismatch.\n");
  489. if (hash_only == 0)
  490. goto fail;
  491. }
  492. if (!(key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_1)) {
  493. VB2_DEBUG("Key block recovery flag mismatch.\n");
  494. if (hash_only == 0)
  495. goto fail;
  496. }
  497. /* Get key for preamble/data verification from the key block. */
  498. struct vb2_public_key data_key2;
  499. if (VB2_SUCCESS != vb2_unpack_key(&data_key2, &keyblock2->data_key)) {
  500. VB2_DEBUG("Unable to unpack kernel data key\n");
  501. goto fail;
  502. }
  503. /* Verify the preamble, which follows the key block */
  504. preamble = (VbKernelPreambleHeader *)(kbuf + key_block->key_block_size);
  505. struct vb2_kernel_preamble *preamble2 =
  506. (struct vb2_kernel_preamble *)
  507. (kbuf + key_block->key_block_size);
  508. if (VB2_SUCCESS != vb2_verify_kernel_preamble(
  509. preamble2,
  510. image_size - key_block->key_block_size,
  511. &data_key2,
  512. &wb)) {
  513. VB2_DEBUG("Preamble verification failed.\n");
  514. goto fail;
  515. }
  516. VB2_DEBUG("Kernel preamble is good.\n");
  517. /* Verify kernel data */
  518. body_offset = key_block->key_block_size + preamble->preamble_size;
  519. if (VB2_SUCCESS != vb2_verify_data(
  520. (const uint8_t *)(kbuf + body_offset),
  521. image_size - body_offset,
  522. (struct vb2_signature *)&preamble->body_signature,
  523. &data_key2, &wb)) {
  524. VB2_DEBUG("Kernel data verification failed.\n");
  525. goto fail;
  526. }
  527. VB2_DEBUG("Kernel is good.\n");
  528. /* Fill in output parameters. */
  529. kparams->kernel_buffer = kbuf + body_offset;
  530. kparams->kernel_buffer_size = image_size - body_offset;
  531. kparams->bootloader_address = preamble->bootloader_address;
  532. kparams->bootloader_size = preamble->bootloader_size;
  533. if (VbKernelHasFlags(preamble) == VBOOT_SUCCESS)
  534. kparams->flags = preamble->flags;
  535. retval = VBERROR_SUCCESS;
  536. fail:
  537. VbApiKernelFree(cparams);
  538. if (NULL != kernel_subkey)
  539. free(kernel_subkey);
  540. if (NULL != workbuf)
  541. free(workbuf);
  542. return retval;
  543. }
  544. VbError_t VbUnlockDevice(void)
  545. {
  546. VB2_DEBUG("Enabling dev-mode...\n");
  547. if (TPM_SUCCESS != SetVirtualDevMode(1))
  548. return VBERROR_TPM_SET_BOOT_MODE_STATE;
  549. VB2_DEBUG("Mode change will take effect on next reboot.\n");
  550. return VBERROR_SUCCESS;
  551. }
  552. VbError_t VbLockDevice(void)
  553. {
  554. VbNvLoad();
  555. VB2_DEBUG("Storing request to leave dev-mode.\n");
  556. VbNvSet(&vnc, VBNV_DISABLE_DEV_REQUEST, 1);
  557. VbNvCommit();
  558. VB2_DEBUG("Mode change will take effect on next reboot.\n");
  559. return VBERROR_SUCCESS;
  560. }