blob: 1d54ef6ff06197f602d3a697a35863d918ce4d1e [file] [log] [blame]
Randall Spangler7993f252013-01-29 15:01:12 -08001/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
Randall Spanglerd1836442010-06-10 09:59:04 -07002 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Functions for loading a kernel from disk.
6 * (Firmware portion)
7 */
8
Randall Spangler7c3ae422016-05-11 13:50:18 -07009#include "2common.h"
Randall Spanglere4136dc2016-10-27 14:34:59 -070010#include "2misc.h"
11#include "2nvstorage.h"
Randall Spangler13b10972016-10-14 11:04:27 -070012#include "2rsa.h"
Randall Spangler7c3ae422016-05-11 13:50:18 -070013#include "2sha.h"
Joel Kitching9adf2aa2019-08-20 17:43:50 +080014#include "2sysincludes.h"
Randall Spanglerd1836442010-06-10 09:59:04 -070015#include "cgptlib.h"
Bill Richardson5deb67f2010-07-23 17:22:25 -070016#include "cgptlib_internal.h"
Dan Ehrenberg7c2beb02014-10-21 16:15:54 -070017#include "gpt_misc.h"
Randall Spanglerd1836442010-06-10 09:59:04 -070018#include "load_kernel_fw.h"
Randall Spangler946abf12016-04-15 14:49:40 -070019#include "rollback_index.h"
Randall Spanglerd1836442010-06-10 09:59:04 -070020#include "utility.h"
Randall Spangler13b10972016-10-14 11:04:27 -070021#include "vb2_common.h"
Randall Spanglere49e8af2011-07-08 13:03:32 -070022#include "vboot_api.h"
Randall Spangler83c88cf2010-06-11 16:14:18 -070023#include "vboot_common.h"
Randall Spanglere49e8af2011-07-08 13:03:32 -070024#include "vboot_kernel.h"
25
Stefan Reinauer55db6a62011-03-15 16:23:41 -070026#define LOWEST_TPM_VERSION 0xffffffff
Randall Spanglerd1836442010-06-10 09:59:04 -070027
Randall Spanglerf1824012016-10-25 10:00:27 -070028enum vboot_mode {
Randall Spangler7993f252013-01-29 15:01:12 -080029 kBootRecovery = 0, /* Recovery firmware, any dev switch position */
30 kBootNormal = 1, /* Normal boot - kernel must be verified */
31 kBootDev = 2 /* Developer boot - self-signed kernel ok */
Randall Spanglerf1824012016-10-25 10:00:27 -070032};
33
34/**
35 * Return the boot mode based on the parameters.
36 *
37 * @param params Load kernel parameters
38 * @return The current boot mode.
39 */
Randall Spanglere4136dc2016-10-27 14:34:59 -070040static enum vboot_mode get_kernel_boot_mode(struct vb2_context *ctx)
Randall Spanglerf1824012016-10-25 10:00:27 -070041{
Randall Spanglere4136dc2016-10-27 14:34:59 -070042 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE)
Randall Spanglerf1824012016-10-25 10:00:27 -070043 return kBootRecovery;
44
Randall Spanglere4136dc2016-10-27 14:34:59 -070045 if (ctx->flags & VB2_CONTEXT_DEVELOPER_MODE)
Randall Spanglerf1824012016-10-25 10:00:27 -070046 return kBootDev;
47
48 return kBootNormal;
49};
50
51/**
52 * Check if the parameters require an officially signed OS.
53 *
54 * @param params Load kernel parameters
55 * @return 1 if official OS required; 0 if self-signed kernels are ok
56 */
Randall Spanglere4136dc2016-10-27 14:34:59 -070057static int require_official_os(struct vb2_context *ctx,
58 const LoadKernelParams *params)
Randall Spanglerf1824012016-10-25 10:00:27 -070059{
60 /* Normal and recovery modes always require official OS */
Randall Spanglere4136dc2016-10-27 14:34:59 -070061 if (get_kernel_boot_mode(ctx) != kBootDev)
Randall Spanglerf1824012016-10-25 10:00:27 -070062 return 1;
63
64 /* FWMP can require developer mode to use official OS */
65 if (params->fwmp &&
66 (params->fwmp->flags & FWMP_DEV_ENABLE_OFFICIAL_ONLY))
67 return 1;
68
69 /* Developer can request official OS via nvstorage */
Randall Spanglere4136dc2016-10-27 14:34:59 -070070 return vb2_nv_get(ctx, VB2_NV_DEV_BOOT_SIGNED_ONLY);
Randall Spanglerf1824012016-10-25 10:00:27 -070071}
72
73/**
74 * Return a pointer to the keyblock inside a vblock.
75 *
76 * Must only be called during or after vb2_verify_kernel_vblock().
77 *
78 * @param kbuf Buffer containing vblock
79 * @return The keyblock pointer.
80 */
81static struct vb2_keyblock *get_keyblock(uint8_t *kbuf)
82{
83 return (struct vb2_keyblock *)kbuf;
84}
85
86/**
87 * Return a pointer to the kernel preamble inside a vblock.
88 *
89 * Must only be called during or after vb2_verify_kernel_vblock().
90 *
91 * @param kbuf Buffer containing vblock
92 * @return The kernel preamble pointer.
93 */
94static struct vb2_kernel_preamble *get_preamble(uint8_t *kbuf)
95{
96 return (struct vb2_kernel_preamble *)
97 (kbuf + get_keyblock(kbuf)->keyblock_size);
98}
99
100/**
101 * Return the offset of the kernel body from the start of the vblock.
102 *
103 * Must only be called during or after vb2_verify_kernel_vblock().
104 *
105 * @param kbuf Buffer containing vblock
106 * @return The offset of the kernel body from the vblock start, in bytes.
107 */
108static uint32_t get_body_offset(uint8_t *kbuf)
109{
110 return (get_keyblock(kbuf)->keyblock_size +
111 get_preamble(kbuf)->preamble_size);
112}
113
114/**
115 * Verify a kernel vblock.
116 *
117 * @param kbuf Buffer containing the vblock
118 * @param kbuf_size Size of the buffer in bytes
119 * @param kernel_subkey Packed kernel subkey to use in validating keyblock
120 * @param params Load kernel parameters
121 * @param min_version Minimum kernel version
122 * @param shpart Destination for verification results
123 * @param wb Work buffer. Must be at least
124 * VB2_VERIFY_KERNEL_PREAMBLE_WORKBUF_BYTES bytes.
125 * @return VB2_SUCCESS, or non-zero error code.
126 */
Joel Kitchinge6700f42019-07-31 14:12:30 +0800127static vb2_error_t vb2_verify_kernel_vblock(
128 struct vb2_context *ctx, uint8_t *kbuf, uint32_t kbuf_size,
129 const struct vb2_packed_key *kernel_subkey,
130 const LoadKernelParams *params, uint32_t min_version,
131 VbSharedDataKernelPart *shpart, struct vb2_workbuf *wb)
Randall Spanglerf1824012016-10-25 10:00:27 -0700132{
133 /* Unpack kernel subkey */
134 struct vb2_public_key kernel_subkey2;
135 if (VB2_SUCCESS != vb2_unpack_key(&kernel_subkey2, kernel_subkey)) {
136 VB2_DEBUG("Unable to unpack kernel subkey\n");
137 return VB2_ERROR_VBLOCK_KERNEL_SUBKEY;
138 }
139
140 /* Verify the key block. */
141 int keyblock_valid = 1; /* Assume valid */
142 struct vb2_keyblock *keyblock = get_keyblock(kbuf);
143 if (VB2_SUCCESS != vb2_verify_keyblock(keyblock, kbuf_size,
144 &kernel_subkey2, wb)) {
145 VB2_DEBUG("Verifying key block signature failed.\n");
146 shpart->check_result = VBSD_LKP_CHECK_KEY_BLOCK_SIG;
147 keyblock_valid = 0;
148
149 /* Check if we must have an officially signed kernel */
Randall Spanglere4136dc2016-10-27 14:34:59 -0700150 if (require_official_os(ctx, params)) {
Randall Spanglerf1824012016-10-25 10:00:27 -0700151 VB2_DEBUG("Self-signed kernels not enabled.\n");
152 shpart->check_result = VBSD_LKP_CHECK_SELF_SIGNED;
153 return VB2_ERROR_VBLOCK_SELF_SIGNED;
154 }
155
156 /* Otherwise, allow the kernel if the key block hash is valid */
157 if (VB2_SUCCESS !=
158 vb2_verify_keyblock_hash(keyblock, kbuf_size, wb)) {
159 VB2_DEBUG("Verifying key block hash failed.\n");
160 shpart->check_result = VBSD_LKP_CHECK_KEY_BLOCK_HASH;
161 return VB2_ERROR_VBLOCK_KEYBLOCK_HASH;
162 }
163 }
164
165 /* Check the key block flags against boot flags. */
166 if (!(keyblock->keyblock_flags &
Randall Spanglere4136dc2016-10-27 14:34:59 -0700167 ((ctx->flags & VB2_CONTEXT_DEVELOPER_MODE) ?
Randall Spanglerf1824012016-10-25 10:00:27 -0700168 KEY_BLOCK_FLAG_DEVELOPER_1 : KEY_BLOCK_FLAG_DEVELOPER_0))) {
169 VB2_DEBUG("Key block developer flag mismatch.\n");
170 shpart->check_result = VBSD_LKP_CHECK_DEV_MISMATCH;
171 keyblock_valid = 0;
172 }
173 if (!(keyblock->keyblock_flags &
Randall Spanglere4136dc2016-10-27 14:34:59 -0700174 ((ctx->flags & VB2_CONTEXT_RECOVERY_MODE) ?
Randall Spanglerf1824012016-10-25 10:00:27 -0700175 KEY_BLOCK_FLAG_RECOVERY_1 : KEY_BLOCK_FLAG_RECOVERY_0))) {
176 VB2_DEBUG("Key block recovery flag mismatch.\n");
177 shpart->check_result = VBSD_LKP_CHECK_REC_MISMATCH;
178 keyblock_valid = 0;
179 }
180
181 /* Check for rollback of key version except in recovery mode. */
Randall Spanglere4136dc2016-10-27 14:34:59 -0700182 enum vboot_mode boot_mode = get_kernel_boot_mode(ctx);
Randall Spanglerf1824012016-10-25 10:00:27 -0700183 uint32_t key_version = keyblock->data_key.key_version;
184 if (kBootRecovery != boot_mode) {
185 if (key_version < (min_version >> 16)) {
186 VB2_DEBUG("Key version too old.\n");
187 shpart->check_result = VBSD_LKP_CHECK_KEY_ROLLBACK;
188 keyblock_valid = 0;
189 }
190 if (key_version > 0xFFFF) {
191 /*
192 * Key version is stored in 16 bits in the TPM, so key
193 * versions greater than 0xFFFF can't be stored
194 * properly.
195 */
196 VB2_DEBUG("Key version > 0xFFFF.\n");
197 shpart->check_result = VBSD_LKP_CHECK_KEY_ROLLBACK;
198 keyblock_valid = 0;
199 }
200 }
201
202 /* If not in developer mode, key block required to be valid. */
203 if (kBootDev != boot_mode && !keyblock_valid) {
204 VB2_DEBUG("Key block is invalid.\n");
205 return VB2_ERROR_VBLOCK_KEYBLOCK;
206 }
207
208 /* If in developer mode and using key hash, check it */
209 if ((kBootDev == boot_mode) &&
210 params->fwmp && (params->fwmp->flags & FWMP_DEV_USE_KEY_HASH)) {
211 struct vb2_packed_key *key = &keyblock->data_key;
212 uint8_t *buf = ((uint8_t *)key) + key->key_offset;
213 uint32_t buflen = key->key_size;
214 uint8_t digest[VB2_SHA256_DIGEST_SIZE];
215
216 VB2_DEBUG("Checking developer key hash.\n");
217 vb2_digest_buffer(buf, buflen, VB2_HASH_SHA256,
218 digest, sizeof(digest));
219 if (0 != vb2_safe_memcmp(digest, params->fwmp->dev_key_hash,
220 VB2_SHA256_DIGEST_SIZE)) {
221 int i;
222
223 VB2_DEBUG("Wrong developer key hash.\n");
224 VB2_DEBUG("Want: ");
225 for (i = 0; i < VB2_SHA256_DIGEST_SIZE; i++)
226 VB2_DEBUG("%02x",
227 params->fwmp->dev_key_hash[i]);
228 VB2_DEBUG("\nGot: ");
229 for (i = 0; i < VB2_SHA256_DIGEST_SIZE; i++)
230 VB2_DEBUG("%02x", digest[i]);
231 VB2_DEBUG("\n");
232
233 return VB2_ERROR_VBLOCK_DEV_KEY_HASH;
234 }
235 }
236
237 /* Get key for preamble verification from the key block. */
238 struct vb2_public_key data_key;
239 if (VB2_SUCCESS != vb2_unpack_key(&data_key, &keyblock->data_key)) {
240 VB2_DEBUG("Unable to unpack kernel data key\n");
241 shpart->check_result = VBSD_LKP_CHECK_DATA_KEY_PARSE;
242 return VB2_ERROR_UNKNOWN;
243 }
244
245 /* Verify the preamble, which follows the key block */
246 struct vb2_kernel_preamble *preamble = get_preamble(kbuf);
247 if (VB2_SUCCESS !=
248 vb2_verify_kernel_preamble(preamble,
249 kbuf_size - keyblock->keyblock_size,
250 &data_key,
251 wb)) {
252 VB2_DEBUG("Preamble verification failed.\n");
253 shpart->check_result = VBSD_LKP_CHECK_VERIFY_PREAMBLE;
254 return VB2_ERROR_UNKNOWN;
255 }
256
257 /*
258 * If the key block is valid and we're not in recovery mode, check for
259 * rollback of the kernel version.
260 */
261 uint32_t combined_version = (key_version << 16) |
262 (preamble->kernel_version & 0xFFFF);
263 shpart->combined_version = combined_version;
264 if (keyblock_valid && kBootRecovery != boot_mode) {
265 if (combined_version < min_version) {
266 VB2_DEBUG("Kernel version too low.\n");
267 shpart->check_result = VBSD_LKP_CHECK_KERNEL_ROLLBACK;
268 /*
269 * If not in developer mode, kernel version
270 * must be valid.
271 */
272 if (kBootDev != boot_mode)
273 return VB2_ERROR_UNKNOWN;
274 }
275 }
276
277 VB2_DEBUG("Kernel preamble is good.\n");
278 shpart->check_result = VBSD_LKP_CHECK_PREAMBLE_VALID;
279 if (keyblock_valid)
280 shpart->flags |= VBSD_LKP_FLAG_KEY_BLOCK_VALID;
281
282 return VB2_SUCCESS;
283}
284
285enum vb2_load_partition_flags {
286 /* Only check the vblock to */
287 VB2_LOAD_PARTITION_VBLOCK_ONLY = (1 << 0),
288};
289
290#define KBUF_SIZE 65536 /* Bytes to read at start of kernel partition */
Randall Spanglere4136dc2016-10-27 14:34:59 -0700291
292/* Minimum context work buffer size needed for vb2_load_partition() */
Randall Spanglerf1824012016-10-25 10:00:27 -0700293#define VB2_LOAD_PARTITION_WORKBUF_BYTES \
294 (VB2_VERIFY_KERNEL_PREAMBLE_WORKBUF_BYTES + KBUF_SIZE)
295
296/**
297 * Load and verify a partition from the stream.
298 *
Randall Spanglere4136dc2016-10-27 14:34:59 -0700299 * @param ctx Vboot context
Randall Spanglerf1824012016-10-25 10:00:27 -0700300 * @param stream Stream to load kernel from
301 * @param kernel_subkey Key to use to verify vblock
302 * @param flags Flags (one or more of vb2_load_partition_flags)
303 * @param params Load-kernel parameters
304 * @param min_version Minimum kernel version from TPM
305 * @param shpart Destination for verification results
Joel Kitching3eb00ef2019-05-23 15:33:54 +0800306 * @param wb Workbuf for data storage
Randall Spanglerf1824012016-10-25 10:00:27 -0700307 * @return VB2_SUCCESS, or non-zero error code.
308 */
Joel Kitchinge6700f42019-07-31 14:12:30 +0800309static vb2_error_t vb2_load_partition(
310 struct vb2_context *ctx, VbExStream_t stream,
311 const struct vb2_packed_key *kernel_subkey, uint32_t flags,
312 LoadKernelParams *params, uint32_t min_version,
313 VbSharedDataKernelPart *shpart, struct vb2_workbuf *wb)
Randall Spanglerf1824012016-10-25 10:00:27 -0700314{
Raul E Rangel4c1a6f42019-06-17 10:48:55 -0600315 uint64_t read_us = 0, start_ts;
Joel Kitching3eb00ef2019-05-23 15:33:54 +0800316 struct vb2_workbuf wblocal = *wb;
Randall Spanglerf1824012016-10-25 10:00:27 -0700317
318 /* Allocate kernel header buffer in workbuf */
319 uint8_t *kbuf = vb2_workbuf_alloc(&wblocal, KBUF_SIZE);
320 if (!kbuf)
321 return VB2_ERROR_LOAD_PARTITION_WORKBUF;
322
Raul E Rangel4c1a6f42019-06-17 10:48:55 -0600323 start_ts = VbExGetTimer();
Randall Spanglerf1824012016-10-25 10:00:27 -0700324 if (VbExStreamRead(stream, KBUF_SIZE, kbuf)) {
325 VB2_DEBUG("Unable to read start of partition.\n");
326 shpart->check_result = VBSD_LKP_CHECK_READ_START;
327 return VB2_ERROR_LOAD_PARTITION_READ_VBLOCK;
328 }
Raul E Rangel4c1a6f42019-06-17 10:48:55 -0600329 read_us += VbExGetTimer() - start_ts;
Randall Spanglerf1824012016-10-25 10:00:27 -0700330
331 if (VB2_SUCCESS !=
Randall Spanglere4136dc2016-10-27 14:34:59 -0700332 vb2_verify_kernel_vblock(ctx, kbuf, KBUF_SIZE, kernel_subkey,
333 params, min_version, shpart, &wblocal)) {
Randall Spanglerf1824012016-10-25 10:00:27 -0700334 return VB2_ERROR_LOAD_PARTITION_VERIFY_VBLOCK;
335 }
336
337 if (flags & VB2_LOAD_PARTITION_VBLOCK_ONLY)
338 return VB2_SUCCESS;
339
340 struct vb2_keyblock *keyblock = get_keyblock(kbuf);
341 struct vb2_kernel_preamble *preamble = get_preamble(kbuf);
342
343 /*
344 * Make sure the kernel starts at or before what we already read into
345 * kbuf.
346 *
347 * We could deal with a larger offset by reading and discarding the
348 * data in between the vblock and the kernel data.
349 */
350 uint32_t body_offset = get_body_offset(kbuf);
351 if (body_offset > KBUF_SIZE) {
352 shpart->check_result = VBSD_LKP_CHECK_BODY_OFFSET;
353 VB2_DEBUG("Kernel body offset is %u > 64KB.\n", body_offset);
354 return VB2_ERROR_LOAD_PARTITION_BODY_OFFSET;
355 }
356
357 uint8_t *kernbuf = params->kernel_buffer;
358 uint32_t kernbuf_size = params->kernel_buffer_size;
359 if (!kernbuf) {
360 /* Get kernel load address and size from the header. */
361 kernbuf = (uint8_t *)((long)preamble->body_load_address);
362 kernbuf_size = preamble->body_signature.data_size;
363 } else if (preamble->body_signature.data_size > kernbuf_size) {
364 VB2_DEBUG("Kernel body doesn't fit in memory.\n");
365 shpart->check_result = VBSD_LKP_CHECK_BODY_EXCEEDS_MEM;
366 return VB2_ERROR_LOAD_PARTITION_BODY_SIZE;
367 }
368
369 uint32_t body_toread = preamble->body_signature.data_size;
370 uint8_t *body_readptr = kernbuf;
371
372 /*
373 * If we've already read part of the kernel, copy that to the beginning
374 * of the kernel buffer.
375 */
376 uint32_t body_copied = KBUF_SIZE - body_offset;
377 if (body_copied > body_toread)
378 body_copied = body_toread; /* Don't over-copy tiny kernel */
379 memcpy(body_readptr, kbuf + body_offset, body_copied);
380 body_toread -= body_copied;
381 body_readptr += body_copied;
382
383 /* Read the kernel data */
Raul E Rangel4c1a6f42019-06-17 10:48:55 -0600384 start_ts = VbExGetTimer();
Randall Spanglerf1824012016-10-25 10:00:27 -0700385 if (body_toread && VbExStreamRead(stream, body_toread, body_readptr)) {
386 VB2_DEBUG("Unable to read kernel data.\n");
387 shpart->check_result = VBSD_LKP_CHECK_READ_DATA;
388 return VB2_ERROR_LOAD_PARTITION_READ_BODY;
389 }
Raul E Rangel4c1a6f42019-06-17 10:48:55 -0600390 read_us += VbExGetTimer() - start_ts;
391 VB2_DEBUG("read %" PRIu32 " KB in %" PRIu64 " ms at %" PRIu64 " KB/s.\n",
392 (body_toread + KBUF_SIZE) / 1024, read_us / 1000,
393 ((uint64_t)(body_toread + KBUF_SIZE) * 1000 * 1000) /
394 (read_us * 1024));
Randall Spanglerf1824012016-10-25 10:00:27 -0700395
396 /* Get key for preamble/data verification from the key block. */
397 struct vb2_public_key data_key;
398 if (VB2_SUCCESS != vb2_unpack_key(&data_key, &keyblock->data_key)) {
399 VB2_DEBUG("Unable to unpack kernel data key\n");
400 shpart->check_result = VBSD_LKP_CHECK_DATA_KEY_PARSE;
401 return VB2_ERROR_LOAD_PARTITION_DATA_KEY;
402 }
403
404 /* Verify kernel data */
405 if (VB2_SUCCESS != vb2_verify_data(kernbuf, kernbuf_size,
406 &preamble->body_signature,
407 &data_key, &wblocal)) {
408 VB2_DEBUG("Kernel data verification failed.\n");
409 shpart->check_result = VBSD_LKP_CHECK_VERIFY_DATA;
410 return VB2_ERROR_LOAD_PARTITION_VERIFY_BODY;
411 }
412
413 /* If we're still here, the kernel is valid */
414 VB2_DEBUG("Partition is good.\n");
415 shpart->check_result = VBSD_LKP_CHECK_KERNEL_GOOD;
416
417 /* Save kernel data back to parameters */
418 params->bootloader_address = preamble->bootloader_address;
419 params->bootloader_size = preamble->bootloader_size;
420 params->flags = vb2_kernel_get_flags(preamble);
421 if (!params->kernel_buffer) {
422 params->kernel_buffer = kernbuf;
423 params->kernel_buffer_size = kernbuf_size;
424 }
425
426 return VB2_SUCCESS;
427}
Randall Spangler640fb512011-03-03 10:11:17 -0800428
Joel Kitching90671fa2019-07-31 13:17:08 +0800429vb2_error_t LoadKernel(struct vb2_context *ctx, LoadKernelParams *params)
Randall Spangler7993f252013-01-29 15:01:12 -0800430{
Randall Spangler79c1c612018-01-03 13:42:40 -0800431 struct vb2_shared_data *sd = vb2_get_sd(ctx);
Joel Kitching3eb00ef2019-05-23 15:33:54 +0800432 struct vb2_workbuf wb;
Randall Spangler79c1c612018-01-03 13:42:40 -0800433 VbSharedDataHeader *shared = sd->vbsd;
Randall Spangler7993f252013-01-29 15:01:12 -0800434 VbSharedDataKernelCall *shcall = NULL;
Randall Spangler7993f252013-01-29 15:01:12 -0800435 int found_partitions = 0;
Randall Spangler7993f252013-01-29 15:01:12 -0800436 uint32_t lowest_version = LOWEST_TPM_VERSION;
Joel Kitching9908a9a2019-07-29 18:53:38 +0800437 vb2_error_t retval = VB2_ERROR_UNKNOWN;
Randall Spanglerdff58522017-11-27 15:37:13 -0800438 int recovery = VB2_RECOVERY_LK_UNSPECIFIED;
Joel Kitchinge6700f42019-07-31 14:12:30 +0800439 vb2_error_t rv;
Randall Spanglerd1836442010-06-10 09:59:04 -0700440
Joel Kitching3eb00ef2019-05-23 15:33:54 +0800441 vb2_workbuf_from_ctx(ctx, &wb);
442
Randall Spangler7993f252013-01-29 15:01:12 -0800443 /* Clear output params in case we fail */
444 params->partition_number = 0;
445 params->bootloader_address = 0;
446 params->bootloader_size = 0;
Furquan Shaikhb7d1f032015-02-03 16:28:31 -0800447 params->flags = 0;
Bill Richardsone2729402010-07-22 12:23:47 -0700448
Randall Spangler7993f252013-01-29 15:01:12 -0800449 /*
450 * Set up tracking for this call. This wraps around if called many
451 * times, so we need to initialize the call entry each time.
452 */
Randall Spanglerf1824012016-10-25 10:00:27 -0700453 shcall = shared->lk_calls +
454 (shared->lk_call_count & (VBSD_MAX_KERNEL_CALLS - 1));
455 memset(shcall, 0, sizeof(*shcall));
Randall Spangler7993f252013-01-29 15:01:12 -0800456 shcall->boot_flags = (uint32_t)params->boot_flags;
Randall Spanglere4136dc2016-10-27 14:34:59 -0700457 shcall->boot_mode = get_kernel_boot_mode(ctx);
Randall Spangler7993f252013-01-29 15:01:12 -0800458 shcall->sector_size = (uint32_t)params->bytes_per_lba;
Dan Ehrenberg3f4d8d02014-12-02 08:21:57 -0800459 shcall->sector_count = params->streaming_lba_count;
Randall Spangler7993f252013-01-29 15:01:12 -0800460 shared->lk_call_count++;
Randall Spangler17c71262011-03-18 11:24:27 -0700461
Randall Spanglerf1824012016-10-25 10:00:27 -0700462 /* Choose key to verify kernel */
463 struct vb2_packed_key *kernel_subkey;
464 if (kBootRecovery == shcall->boot_mode) {
465 /* Use the recovery key to verify the kernel */
Joel Kitchingb4b45072019-06-09 12:44:32 +0800466 rv = vb2_gbb_read_recovery_key(ctx, &kernel_subkey, NULL, &wb);
467 if (VB2_SUCCESS != rv) {
468 VB2_DEBUG("GBB read recovery key failed.\n");
469 retval = VBERROR_INVALID_GBB;
Randall Spanglerf1824012016-10-25 10:00:27 -0700470 goto load_kernel_exit;
Joel Kitchingb4b45072019-06-09 12:44:32 +0800471 }
Randall Spanglerf1824012016-10-25 10:00:27 -0700472 } else {
473 /* Use the kernel subkey passed from firmware verification */
474 kernel_subkey = (struct vb2_packed_key *)&shared->kernel_subkey;
Randall Spangler7993f252013-01-29 15:01:12 -0800475 }
Randall Spangler17c71262011-03-18 11:24:27 -0700476
Randall Spangler7993f252013-01-29 15:01:12 -0800477 /* Read GPT data */
Randall Spanglerf1824012016-10-25 10:00:27 -0700478 GptData gpt;
479 gpt.sector_bytes = (uint32_t)params->bytes_per_lba;
Dan Ehrenberg3f4d8d02014-12-02 08:21:57 -0800480 gpt.streaming_drive_sectors = params->streaming_lba_count;
481 gpt.gpt_drive_sectors = params->gpt_lba_count;
482 gpt.flags = params->boot_flags & BOOT_FLAG_EXTERNAL_GPT
483 ? GPT_FLAG_EXTERNAL : 0;
Randall Spangler7993f252013-01-29 15:01:12 -0800484 if (0 != AllocAndReadGptData(params->disk_handle, &gpt)) {
Randall Spanglerf1824012016-10-25 10:00:27 -0700485 VB2_DEBUG("Unable to read GPT data\n");
Randall Spangler7993f252013-01-29 15:01:12 -0800486 shcall->check_result = VBSD_LKC_CHECK_GPT_READ_ERROR;
Randall Spanglerf1824012016-10-25 10:00:27 -0700487 goto gpt_done;
Randall Spangler7993f252013-01-29 15:01:12 -0800488 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700489
Randall Spangler7993f252013-01-29 15:01:12 -0800490 /* Initialize GPT library */
491 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglerf1824012016-10-25 10:00:27 -0700492 VB2_DEBUG("Error parsing GPT\n");
Randall Spangler7993f252013-01-29 15:01:12 -0800493 shcall->check_result = VBSD_LKC_CHECK_GPT_PARSE_ERROR;
Randall Spanglerf1824012016-10-25 10:00:27 -0700494 goto gpt_done;
Randall Spangler13b10972016-10-14 11:04:27 -0700495 }
496
Gwendal Grignoue2e14ae2015-04-27 09:46:52 -0700497 /* Loop over candidate kernel partitions */
Randall Spanglerf1824012016-10-25 10:00:27 -0700498 uint64_t part_start, part_size;
Gwendal Grignoue2e14ae2015-04-27 09:46:52 -0700499 while (GPT_SUCCESS ==
Randall Spanglerf1824012016-10-25 10:00:27 -0700500 GptNextKernelEntry(&gpt, &part_start, &part_size)) {
Randall Spanglerd1836442010-06-10 09:59:04 -0700501
Randall Spanglerf1824012016-10-25 10:00:27 -0700502 VB2_DEBUG("Found kernel entry at %"
503 PRIu64 " size %" PRIu64 "\n",
504 part_start, part_size);
Randall Spanglerd1836442010-06-10 09:59:04 -0700505
Randall Spangler7993f252013-01-29 15:01:12 -0800506 /*
507 * Set up tracking for this partition. This wraps around if
508 * called many times, so initialize the partition entry each
509 * time.
510 */
Randall Spanglerf1824012016-10-25 10:00:27 -0700511 VbSharedDataKernelPart *shpart =
512 shcall->parts + (shcall->kernel_parts_found
Gwendal Grignoue2e14ae2015-04-27 09:46:52 -0700513 & (VBSD_MAX_KERNEL_PARTS - 1));
Randall Spangler664096b2016-10-13 16:16:41 -0700514 memset(shpart, 0, sizeof(VbSharedDataKernelPart));
Randall Spangler7993f252013-01-29 15:01:12 -0800515 shpart->sector_start = part_start;
516 shpart->sector_count = part_size;
517 /*
518 * TODO: GPT partitions start at 1, but cgptlib starts them at
519 * 0. Adjust here, until cgptlib is fixed.
520 */
521 shpart->gpt_index = (uint8_t)(gpt.current_kernel + 1);
522 shcall->kernel_parts_found++;
Randall Spangler695cd162010-06-15 23:38:23 -0700523
Randall Spangler7993f252013-01-29 15:01:12 -0800524 /* Found at least one kernel partition. */
525 found_partitions++;
Randall Spangler17c71262011-03-18 11:24:27 -0700526
Randall Spangler4184e622014-10-08 16:41:01 -0700527 /* Set up the stream */
Randall Spanglerf1824012016-10-25 10:00:27 -0700528 VbExStream_t stream = NULL;
Randall Spangler4184e622014-10-08 16:41:01 -0700529 if (VbExStreamOpen(params->disk_handle,
530 part_start, part_size, &stream)) {
Randall Spanglerf1824012016-10-25 10:00:27 -0700531 VB2_DEBUG("Partition error getting stream.\n");
Randall Spangler7993f252013-01-29 15:01:12 -0800532 shpart->check_result = VBSD_LKP_CHECK_TOO_SMALL;
Randall Spanglerf1824012016-10-25 10:00:27 -0700533 VB2_DEBUG("Marking kernel as invalid.\n");
534 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
Randall Spangler7993f252013-01-29 15:01:12 -0800535 continue;
Randall Spangler4184e622014-10-08 16:41:01 -0700536 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700537
Randall Spanglerf1824012016-10-25 10:00:27 -0700538 uint32_t lpflags = 0;
539 if (params->partition_number > 0) {
540 /*
541 * If we already have a good kernel, we only needed to
542 * look at the vblock versions to check for rollback.
543 */
544 lpflags |= VB2_LOAD_PARTITION_VBLOCK_ONLY;
Randall Spangler7993f252013-01-29 15:01:12 -0800545 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700546
Joel Kitchingb4b45072019-06-09 12:44:32 +0800547 rv = vb2_load_partition(ctx,
548 stream,
549 kernel_subkey,
550 lpflags,
551 params,
552 shared->kernel_version_tpm,
553 shpart,
554 &wb);
Randall Spangler4184e622014-10-08 16:41:01 -0700555 VbExStreamClose(stream);
Randall Spangler4184e622014-10-08 16:41:01 -0700556
Randall Spanglerf1824012016-10-25 10:00:27 -0700557 if (rv != VB2_SUCCESS) {
558 VB2_DEBUG("Marking kernel as invalid.\n");
559 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
560 continue;
Randall Spangler7993f252013-01-29 15:01:12 -0800561 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700562
Randall Spanglerf1824012016-10-25 10:00:27 -0700563 int keyblock_valid = (shpart->flags &
564 VBSD_LKP_FLAG_KEY_BLOCK_VALID);
565 if (keyblock_valid) {
566 shared->flags |= VBSD_KERNEL_KEY_VERIFIED;
567 /* Track lowest version from a valid header. */
568 if (lowest_version > shpart->combined_version)
569 lowest_version = shpart->combined_version;
570 }
571 VB2_DEBUG("Key block valid: %d\n", keyblock_valid);
572 VB2_DEBUG("Combined version: %u\n", shpart->combined_version);
Randall Spangler17c71262011-03-18 11:24:27 -0700573
Randall Spangler7993f252013-01-29 15:01:12 -0800574 /*
Randall Spanglerf1824012016-10-25 10:00:27 -0700575 * If we're only looking at headers, we're done with this
576 * partition.
577 */
578 if (lpflags & VB2_LOAD_PARTITION_VBLOCK_ONLY)
579 continue;
580
581 /*
582 * Otherwise, we found a partition we like.
583 *
Randall Spangler7993f252013-01-29 15:01:12 -0800584 * TODO: GPT partitions start at 1, but cgptlib starts them at
585 * 0. Adjust here, until cgptlib is fixed.
586 */
Randall Spangler7993f252013-01-29 15:01:12 -0800587 params->partition_number = gpt.current_kernel + 1;
Randall Spanglerf1824012016-10-25 10:00:27 -0700588
Randall Spangler7993f252013-01-29 15:01:12 -0800589 /*
590 * TODO: GetCurrentKernelUniqueGuid() should take a destination
591 * size, or the dest should be a struct, so we know it's big
592 * enough.
593 */
Randall Spanglerf1824012016-10-25 10:00:27 -0700594 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler741d2b22010-08-20 16:37:12 -0700595
Patrick Georgiebf886b2015-02-10 14:58:28 +0100596 /* Update GPT to note this is the kernel we're trying.
597 * But not when we assume that the boot process may
598 * not complete for valid reasons (eg. early shutdown).
599 */
600 if (!(shared->flags & VBSD_NOFAIL_BOOT))
601 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY);
Randall Spangler741d2b22010-08-20 16:37:12 -0700602
Randall Spangler7993f252013-01-29 15:01:12 -0800603 /*
604 * If we're in recovery mode or we're about to boot a
Randall Spanglerf1824012016-10-25 10:00:27 -0700605 * non-officially-signed kernel, there's no rollback
606 * protection, so we can stop at the first valid kernel.
Randall Spangler7993f252013-01-29 15:01:12 -0800607 */
Randall Spanglerf1824012016-10-25 10:00:27 -0700608 if (kBootRecovery == shcall->boot_mode || !keyblock_valid) {
609 VB2_DEBUG("In recovery mode or dev-signed kernel\n");
Randall Spangler7993f252013-01-29 15:01:12 -0800610 break;
611 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700612
Randall Spangler7993f252013-01-29 15:01:12 -0800613 /*
614 * Otherwise, we do care about the key index in the TPM. If
615 * the good partition's key version is the same as the tpm,
616 * then the TPM doesn't need updating; we can stop now.
617 * Otherwise, we'll check all the other headers to see if they
618 * contain a newer key.
619 */
Randall Spanglerf1824012016-10-25 10:00:27 -0700620 if (shpart->combined_version == shared->kernel_version_tpm) {
621 VB2_DEBUG("Same kernel version\n");
Randall Spangler7993f252013-01-29 15:01:12 -0800622 break;
623 }
Gwendal Grignoue2e14ae2015-04-27 09:46:52 -0700624 } /* while(GptNextKernelEntry) */
Randall Spangler49cb0d32013-01-29 14:28:16 -0800625
Randall Spanglerf1824012016-10-25 10:00:27 -0700626gpt_done:
Randall Spangler7993f252013-01-29 15:01:12 -0800627 /* Write and free GPT data */
628 WriteAndFreeGptData(params->disk_handle, &gpt);
Randall Spanglerd1836442010-06-10 09:59:04 -0700629
Randall Spangler7993f252013-01-29 15:01:12 -0800630 /* Handle finding a good partition */
Randall Spanglerf1824012016-10-25 10:00:27 -0700631 if (params->partition_number > 0) {
632 VB2_DEBUG("Good partition %d\n", params->partition_number);
Randall Spangler7993f252013-01-29 15:01:12 -0800633 shcall->check_result = VBSD_LKC_CHECK_GOOD_PARTITION;
634 shared->kernel_version_lowest = lowest_version;
635 /*
636 * Sanity check - only store a new TPM version if we found one.
637 * If lowest_version is still at its initial value, we didn't
638 * find one; for example, we're in developer mode and just
639 * didn't look.
640 */
641 if (lowest_version != LOWEST_TPM_VERSION &&
642 lowest_version > shared->kernel_version_tpm)
643 shared->kernel_version_tpm = lowest_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700644
Randall Spangler7993f252013-01-29 15:01:12 -0800645 /* Success! */
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800646 retval = VB2_SUCCESS;
Randall Spangler7993f252013-01-29 15:01:12 -0800647 } else if (found_partitions > 0) {
648 shcall->check_result = VBSD_LKC_CHECK_INVALID_PARTITIONS;
Randall Spanglerdff58522017-11-27 15:37:13 -0800649 recovery = VB2_RECOVERY_RW_INVALID_OS;
Randall Spangler7993f252013-01-29 15:01:12 -0800650 retval = VBERROR_INVALID_KERNEL_FOUND;
651 } else {
652 shcall->check_result = VBSD_LKC_CHECK_NO_PARTITIONS;
Randall Spanglerdff58522017-11-27 15:37:13 -0800653 recovery = VB2_RECOVERY_RW_NO_OS;
Randall Spangler7993f252013-01-29 15:01:12 -0800654 retval = VBERROR_NO_KERNEL_FOUND;
655 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700656
Randall Spanglerf1824012016-10-25 10:00:27 -0700657load_kernel_exit:
Randall Spangler7993f252013-01-29 15:01:12 -0800658 /* Store recovery request, if any */
Randall Spanglere4136dc2016-10-27 14:34:59 -0700659 vb2_nv_set(ctx, VB2_NV_RECOVERY_REQUEST,
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800660 VB2_SUCCESS != retval ?
Randall Spanglerdff58522017-11-27 15:37:13 -0800661 recovery : VB2_RECOVERY_NOT_REQUESTED);
Randall Spangler640fb512011-03-03 10:11:17 -0800662
Randall Spanglereedd4292016-11-01 15:29:05 -0700663 shcall->return_code = (uint8_t)retval;
Randall Spangler7993f252013-01-29 15:01:12 -0800664 return retval;
Randall Spanglerd1836442010-06-10 09:59:04 -0700665}