| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include "2api.h" |
| 10 |
#include "2common.h" |
| 11 |
#include "2misc.h" |
| 12 |
#include "2nvstorage.h" |
| 13 |
#include "2packed_key.h" |
| 14 |
#include "2secdata.h" |
| 15 |
#include "2sysincludes.h" |
| 16 |
#include "cgptlib.h" |
| 17 |
#include "cgptlib_internal.h" |
| 18 |
#include "gpt_misc.h" |
| 19 |
#include "vboot_api.h" |
| 20 |
|
| 21 |
enum vb2_load_partition_flags { |
| 22 |
VB2_LOAD_PARTITION_FLAG_VBLOCK_ONLY = (1 << 0), |
| 23 |
VB2_LOAD_PARTITION_FLAG_MINIOS = (1 << 1), |
| 24 |
}; |
| 25 |
|
| 26 |
#define KBUF_SIZE 65536 |
| 27 |
|
| 28 |
#define LOWEST_TPM_VERSION 0xffffffff |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
static int need_valid_keyblock(struct vb2_context *ctx) |
| 38 |
{ |
| 39 |
|
| 40 |
if (ctx->boot_mode != VB2_BOOT_MODE_DEVELOPER) |
| 41 |
return 1; |
| 42 |
|
| 43 |
|
| 44 |
if (vb2_secdata_fwmp_get_flag( |
| 45 |
ctx, VB2_SECDATA_FWMP_DEV_ENABLE_OFFICIAL_ONLY)) |
| 46 |
return 1; |
| 47 |
|
| 48 |
|
| 49 |
if (vb2_nv_get(ctx, VB2_NV_DEV_BOOT_SIGNED_ONLY)) |
| 50 |
return 1; |
| 51 |
|
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
static struct vb2_keyblock *get_keyblock(uint8_t *kbuf) |
| 64 |
{ |
| 65 |
return (struct vb2_keyblock *)kbuf; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static struct vb2_kernel_preamble *get_preamble(uint8_t *kbuf) |
| 77 |
{ |
| 78 |
return (struct vb2_kernel_preamble *) |
| 79 |
(kbuf + get_keyblock(kbuf)->keyblock_size); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static uint32_t get_body_offset(uint8_t *kbuf) |
| 91 |
{ |
| 92 |
return (get_keyblock(kbuf)->keyblock_size + |
| 93 |
get_preamble(kbuf)->preamble_size); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static vb2_error_t vb2_verify_kernel_dev_key_hash( |
| 104 |
struct vb2_context *ctx, struct vb2_keyblock *keyblock) |
| 105 |
{ |
| 106 |
struct vb2_packed_key *key = &keyblock->data_key; |
| 107 |
uint8_t *buf = ((uint8_t *)key) + key->key_offset; |
| 108 |
uint32_t buflen = key->key_size; |
| 109 |
struct vb2_hash hash; |
| 110 |
|
| 111 |
VB2_DEBUG("Checking developer key hash.\n"); |
| 112 |
VB2_TRY(vb2_hash_calculate(vb2api_hwcrypto_allowed(ctx), buf, buflen, |
| 113 |
VB2_HASH_SHA256, &hash)); |
| 114 |
|
| 115 |
uint8_t *fwmp_dev_key_hash = |
| 116 |
vb2_secdata_fwmp_get_dev_key_hash(ctx); |
| 117 |
if (fwmp_dev_key_hash == NULL) { |
| 118 |
VB2_DEBUG("Couldn't retrieve developer key hash.\n"); |
| 119 |
return VB2_ERROR_KERNEL_KEYBLOCK_DEV_KEY_HASH; |
| 120 |
} |
| 121 |
|
| 122 |
if (vb2_safe_memcmp(hash.sha256, fwmp_dev_key_hash, |
| 123 |
sizeof(hash.sha256))) { |
| 124 |
int i; |
| 125 |
|
| 126 |
VB2_DEBUG("Wrong developer key hash.\n"); |
| 127 |
VB2_DEBUG("Want: "); |
| 128 |
for (i = 0; i < VB2_SHA256_DIGEST_SIZE; i++) |
| 129 |
VB2_DEBUG_RAW("%02x ", fwmp_dev_key_hash[i]); |
| 130 |
VB2_DEBUG_RAW("\n"); |
| 131 |
VB2_DEBUG("Got: "); |
| 132 |
for (i = 0; i < VB2_SHA256_DIGEST_SIZE; i++) |
| 133 |
VB2_DEBUG_RAW("%02x ", hash.sha256[i]); |
| 134 |
VB2_DEBUG_RAW("\n"); |
| 135 |
|
| 136 |
return VB2_ERROR_KERNEL_KEYBLOCK_DEV_KEY_HASH; |
| 137 |
} |
| 138 |
|
| 139 |
return VB2_SUCCESS; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
static vb2_error_t vb2_verify_kernel_vblock(struct vb2_context *ctx, |
| 155 |
uint8_t *kbuf, uint32_t kbuf_size, |
| 156 |
uint32_t lpflags, |
| 157 |
struct vb2_workbuf *wb, |
| 158 |
uint32_t *kernel_version) |
| 159 |
{ |
| 160 |
struct vb2_shared_data *sd = vb2_get_sd(ctx); |
| 161 |
|
| 162 |
uint8_t *key_data; |
| 163 |
uint32_t key_size; |
| 164 |
struct vb2_public_key kernel_key; |
| 165 |
|
| 166 |
int need_keyblock_valid = need_valid_keyblock(ctx); |
| 167 |
int keyblock_valid = 1; |
| 168 |
|
| 169 |
vb2_error_t rv; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
key_data = vb2_member_of(sd, sd->kernel_key_offset); |
| 174 |
key_size = sd->kernel_key_size; |
| 175 |
VB2_TRY(vb2_unpack_key_buffer(&kernel_key, key_data, key_size)); |
| 176 |
|
| 177 |
kernel_key.allow_hwcrypto = vb2api_hwcrypto_allowed(ctx); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
sd->flags &= ~VB2_SD_FLAG_KERNEL_SIGNED; |
| 185 |
|
| 186 |
|
| 187 |
struct vb2_keyblock *keyblock = get_keyblock(kbuf); |
| 188 |
rv = vb2_verify_keyblock(keyblock, kbuf_size, &kernel_key, wb); |
| 189 |
if (rv) { |
| 190 |
VB2_DEBUG("Verifying keyblock signature failed.\n"); |
| 191 |
keyblock_valid = 0; |
| 192 |
|
| 193 |
|
| 194 |
if (need_keyblock_valid) { |
| 195 |
VB2_DEBUG("Self-signed kernels not enabled.\n"); |
| 196 |
return rv; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
rv = vb2_verify_keyblock_hash(keyblock, kbuf_size, wb); |
| 201 |
if (rv) { |
| 202 |
VB2_DEBUG("Verifying keyblock hash failed.\n"); |
| 203 |
return rv; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
if (!(keyblock->keyblock_flags & |
| 209 |
((ctx->flags & VB2_CONTEXT_DEVELOPER_MODE) ? |
| 210 |
VB2_KEYBLOCK_FLAG_DEVELOPER_1 : |
| 211 |
VB2_KEYBLOCK_FLAG_DEVELOPER_0))) { |
| 212 |
VB2_DEBUG("Keyblock developer flag mismatch.\n"); |
| 213 |
keyblock_valid = 0; |
| 214 |
if (need_keyblock_valid) |
| 215 |
return VB2_ERROR_KERNEL_KEYBLOCK_DEV_FLAG; |
| 216 |
} |
| 217 |
if (!(keyblock->keyblock_flags & |
| 218 |
((ctx->flags & VB2_CONTEXT_RECOVERY_MODE) ? |
| 219 |
VB2_KEYBLOCK_FLAG_RECOVERY_1 : |
| 220 |
VB2_KEYBLOCK_FLAG_RECOVERY_0))) { |
| 221 |
VB2_DEBUG("Keyblock recovery flag mismatch.\n"); |
| 222 |
keyblock_valid = 0; |
| 223 |
if (need_keyblock_valid) |
| 224 |
return VB2_ERROR_KERNEL_KEYBLOCK_REC_FLAG; |
| 225 |
} |
| 226 |
if (!(keyblock->keyblock_flags & |
| 227 |
((lpflags & VB2_LOAD_PARTITION_FLAG_MINIOS) ? |
| 228 |
VB2_KEYBLOCK_FLAG_MINIOS_1 : |
| 229 |
VB2_KEYBLOCK_FLAG_MINIOS_0))) { |
| 230 |
VB2_DEBUG("Keyblock miniOS flag mismatch.\n"); |
| 231 |
keyblock_valid = 0; |
| 232 |
if (need_keyblock_valid) |
| 233 |
return VB2_ERROR_KERNEL_KEYBLOCK_MINIOS_FLAG; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
uint32_t key_version = keyblock->data_key.key_version; |
| 238 |
if (ctx->boot_mode != VB2_BOOT_MODE_MANUAL_RECOVERY) { |
| 239 |
if (key_version < (sd->kernel_version_secdata >> 16)) { |
| 240 |
keyblock_valid = 0; |
| 241 |
if (need_keyblock_valid) { |
| 242 |
VB2_DEBUG("Key version too old.\n"); |
| 243 |
return VB2_ERROR_KERNEL_KEYBLOCK_VERSION_ROLLBACK; |
| 244 |
} |
| 245 |
} |
| 246 |
if (key_version > VB2_MAX_KEY_VERSION) { |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
VB2_DEBUG("Key version > 0xFFFF.\n"); |
| 253 |
keyblock_valid = 0; |
| 254 |
if (need_keyblock_valid) |
| 255 |
return VB2_ERROR_KERNEL_KEYBLOCK_VERSION_RANGE; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
if (ctx->boot_mode == VB2_BOOT_MODE_DEVELOPER && |
| 261 |
vb2_secdata_fwmp_get_flag(ctx, VB2_SECDATA_FWMP_DEV_USE_KEY_HASH)) { |
| 262 |
VB2_TRY(vb2_verify_kernel_dev_key_hash(ctx, keyblock)); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if (keyblock_valid) |
| 272 |
sd->flags |= VB2_SD_FLAG_KERNEL_SIGNED; |
| 273 |
|
| 274 |
|
| 275 |
struct vb2_public_key data_key; |
| 276 |
rv = vb2_unpack_key(&data_key, &keyblock->data_key); |
| 277 |
if (rv) { |
| 278 |
VB2_DEBUG("Unable to unpack kernel data key\n"); |
| 279 |
return rv; |
| 280 |
} |
| 281 |
|
| 282 |
data_key.allow_hwcrypto = kernel_key.allow_hwcrypto; |
| 283 |
|
| 284 |
|
| 285 |
struct vb2_kernel_preamble *preamble = get_preamble(kbuf); |
| 286 |
rv = vb2_verify_kernel_preamble(preamble, |
| 287 |
kbuf_size - keyblock->keyblock_size, |
| 288 |
&data_key, |
| 289 |
wb); |
| 290 |
if (rv) { |
| 291 |
VB2_DEBUG("Preamble verification failed.\n"); |
| 292 |
return rv; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
if (need_keyblock_valid && (lpflags & VB2_LOAD_PARTITION_FLAG_MINIOS)) { |
| 297 |
if (preamble->kernel_version < |
| 298 |
(sd->kernel_version_secdata >> 24)) { |
| 299 |
keyblock_valid = 0; |
| 300 |
if (need_keyblock_valid) { |
| 301 |
VB2_DEBUG("miniOS kernel version too old.\n"); |
| 302 |
return VB2_ERROR_KERNEL_PREAMBLE_VERSION_ROLLBACK; |
| 303 |
} |
| 304 |
} |
| 305 |
if (preamble->kernel_version > 0xff) { |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
VB2_DEBUG("Key version > 0xFF.\n"); |
| 312 |
keyblock_valid = 0; |
| 313 |
if (need_keyblock_valid) |
| 314 |
return VB2_ERROR_KERNEL_PREAMBLE_VERSION_RANGE; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
if (preamble->kernel_version > VB2_MAX_PREAMBLE_VERSION) |
| 323 |
return VB2_ERROR_KERNEL_PREAMBLE_VERSION_RANGE; |
| 324 |
|
| 325 |
|
| 326 |
*kernel_version = key_version << 16 | preamble->kernel_version; |
| 327 |
|
| 328 |
|
| 329 |
if (need_keyblock_valid && |
| 330 |
ctx->boot_mode != VB2_BOOT_MODE_MANUAL_RECOVERY && |
| 331 |
*kernel_version < sd->kernel_version_secdata) { |
| 332 |
VB2_DEBUG("Kernel version too low.\n"); |
| 333 |
return VB2_ERROR_KERNEL_PREAMBLE_VERSION_ROLLBACK; |
| 334 |
} |
| 335 |
|
| 336 |
VB2_DEBUG("Kernel preamble is good.\n"); |
| 337 |
return VB2_SUCCESS; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
static vb2_error_t vb2_load_partition(struct vb2_context *ctx, |
| 351 |
struct vb2_kernel_params *params, |
| 352 |
VbExStream_t stream, uint32_t lpflags, |
| 353 |
uint32_t *kernel_version) |
| 354 |
{ |
| 355 |
uint32_t read_ms = 0, start_ts; |
| 356 |
struct vb2_workbuf wb; |
| 357 |
|
| 358 |
vb2_workbuf_from_ctx(ctx, &wb); |
| 359 |
|
| 360 |
|
| 361 |
uint8_t *kbuf = vb2_workbuf_alloc(&wb, KBUF_SIZE); |
| 362 |
if (!kbuf) |
| 363 |
return VB2_ERROR_LOAD_PARTITION_WORKBUF; |
| 364 |
|
| 365 |
start_ts = vb2ex_mtime(); |
| 366 |
if (VbExStreamRead(stream, KBUF_SIZE, kbuf)) { |
| 367 |
VB2_DEBUG("Unable to read start of partition.\n"); |
| 368 |
return VB2_ERROR_LOAD_PARTITION_READ_VBLOCK; |
| 369 |
} |
| 370 |
read_ms += vb2ex_mtime() - start_ts; |
| 371 |
|
| 372 |
if (vb2_verify_kernel_vblock(ctx, kbuf, KBUF_SIZE, lpflags, &wb, |
| 373 |
kernel_version)) |
| 374 |
return VB2_ERROR_LOAD_PARTITION_VERIFY_VBLOCK; |
| 375 |
|
| 376 |
if (lpflags & VB2_LOAD_PARTITION_FLAG_VBLOCK_ONLY) |
| 377 |
return VB2_SUCCESS; |
| 378 |
|
| 379 |
struct vb2_keyblock *keyblock = get_keyblock(kbuf); |
| 380 |
struct vb2_kernel_preamble *preamble = get_preamble(kbuf); |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
uint32_t body_offset = get_body_offset(kbuf); |
| 390 |
if (body_offset > KBUF_SIZE) { |
| 391 |
VB2_DEBUG("Kernel body offset is %u > 64KB.\n", body_offset); |
| 392 |
return VB2_ERROR_LOAD_PARTITION_BODY_OFFSET; |
| 393 |
} |
| 394 |
|
| 395 |
uint8_t *kernbuf = params->kernel_buffer; |
| 396 |
uint32_t kernbuf_size = params->kernel_buffer_size; |
| 397 |
if (!kernbuf) { |
| 398 |
|
| 399 |
kernbuf = (uint8_t *)((long)preamble->body_load_address); |
| 400 |
kernbuf_size = preamble->body_signature.data_size; |
| 401 |
} else if (preamble->body_signature.data_size > kernbuf_size) { |
| 402 |
VB2_DEBUG("Kernel body doesn't fit in memory.\n"); |
| 403 |
return VB2_ERROR_LOAD_PARTITION_BODY_SIZE; |
| 404 |
} |
| 405 |
|
| 406 |
uint32_t body_toread = preamble->body_signature.data_size; |
| 407 |
uint8_t *body_readptr = kernbuf; |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
uint32_t body_copied = KBUF_SIZE - body_offset; |
| 414 |
if (body_copied > body_toread) |
| 415 |
body_copied = body_toread; |
| 416 |
memcpy(body_readptr, kbuf + body_offset, body_copied); |
| 417 |
body_toread -= body_copied; |
| 418 |
body_readptr += body_copied; |
| 419 |
|
| 420 |
|
| 421 |
start_ts = vb2ex_mtime(); |
| 422 |
if (body_toread && VbExStreamRead(stream, body_toread, body_readptr)) { |
| 423 |
VB2_DEBUG("Unable to read kernel data.\n"); |
| 424 |
return VB2_ERROR_LOAD_PARTITION_READ_BODY; |
| 425 |
} |
| 426 |
read_ms += vb2ex_mtime() - start_ts; |
| 427 |
if (read_ms == 0) |
| 428 |
read_ms = 1; |
| 429 |
VB2_DEBUG("read %u KB in %u ms at %u KB/s.\n", |
| 430 |
(body_toread + KBUF_SIZE) / 1024, read_ms, |
| 431 |
(uint32_t)(((body_toread + KBUF_SIZE) * VB2_MSEC_PER_SEC) / |
| 432 |
(read_ms * 1024))); |
| 433 |
|
| 434 |
|
| 435 |
struct vb2_public_key data_key; |
| 436 |
if (vb2_unpack_key(&data_key, &keyblock->data_key)) { |
| 437 |
VB2_DEBUG("Unable to unpack kernel data key\n"); |
| 438 |
return VB2_ERROR_LOAD_PARTITION_DATA_KEY; |
| 439 |
} |
| 440 |
|
| 441 |
data_key.allow_hwcrypto = vb2api_hwcrypto_allowed(ctx); |
| 442 |
|
| 443 |
|
| 444 |
if (vb2_verify_data(kernbuf, kernbuf_size, &preamble->body_signature, |
| 445 |
&data_key, &wb)) { |
| 446 |
VB2_DEBUG("Kernel data verification failed.\n"); |
| 447 |
return VB2_ERROR_LOAD_PARTITION_VERIFY_BODY; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
VB2_DEBUG("Partition is good.\n"); |
| 452 |
|
| 453 |
|
| 454 |
params->bootloader_offset = preamble->bootloader_address - |
| 455 |
preamble->body_load_address; |
| 456 |
params->bootloader_size = preamble->bootloader_size; |
| 457 |
params->flags = vb2_kernel_get_flags(preamble); |
| 458 |
if (!params->kernel_buffer) { |
| 459 |
params->kernel_buffer = kernbuf; |
| 460 |
params->kernel_buffer_size = kernbuf_size; |
| 461 |
} |
| 462 |
|
| 463 |
return VB2_SUCCESS; |
| 464 |
} |
| 465 |
|
| 466 |
static vb2_error_t try_minios_kernel(struct vb2_context *ctx, |
| 467 |
struct vb2_kernel_params *params, |
| 468 |
struct vb2_disk_info *disk_info, |
| 469 |
uint64_t sector) { |
| 470 |
struct vb2_shared_data *sd = vb2_get_sd(ctx); |
| 471 |
VbExStream_t stream; |
| 472 |
uint64_t sectors_left = disk_info->lba_count - sector; |
| 473 |
const uint32_t lpflags = VB2_LOAD_PARTITION_FLAG_MINIOS; |
| 474 |
uint32_t kernel_version = 0; |
| 475 |
vb2_error_t rv = VB2_ERROR_LK_NO_KERNEL_FOUND; |
| 476 |
|
| 477 |
|
| 478 |
if (VbExStreamOpen(disk_info->handle, sector, sectors_left, |
| 479 |
&stream)) { |
| 480 |
VB2_DEBUG("Unable to open disk handle.\n"); |
| 481 |
return rv; |
| 482 |
} |
| 483 |
|
| 484 |
rv = vb2_load_partition(ctx, params, stream, lpflags, &kernel_version); |
| 485 |
VB2_DEBUG("vb2_load_partition returned: %d\n", rv); |
| 486 |
|
| 487 |
VbExStreamClose(stream); |
| 488 |
|
| 489 |
if (rv) |
| 490 |
return VB2_ERROR_LK_NO_KERNEL_FOUND; |
| 491 |
|
| 492 |
sd->kernel_version = kernel_version; |
| 493 |
|
| 494 |
return rv; |
| 495 |
} |
| 496 |
|
| 497 |
static vb2_error_t try_minios_sectors(struct vb2_context *ctx, |
| 498 |
struct vb2_kernel_params *params, |
| 499 |
struct vb2_disk_info *disk_info, |
| 500 |
uint64_t start, uint64_t count) |
| 501 |
{ |
| 502 |
const uint32_t buf_size = count * disk_info->bytes_per_lba; |
| 503 |
char *buf; |
| 504 |
VbExStream_t stream; |
| 505 |
uint64_t isector; |
| 506 |
vb2_error_t rv = VB2_ERROR_LK_NO_KERNEL_FOUND; |
| 507 |
|
| 508 |
buf = malloc(buf_size); |
| 509 |
if (buf == NULL) { |
| 510 |
VB2_DEBUG("Unable to allocate disk read buffer.\n"); |
| 511 |
return rv; |
| 512 |
} |
| 513 |
|
| 514 |
if (VbExStreamOpen(disk_info->handle, start, count, &stream)) { |
| 515 |
VB2_DEBUG("Unable to open disk handle.\n"); |
| 516 |
free(buf); |
| 517 |
return rv; |
| 518 |
} |
| 519 |
if (VbExStreamRead(stream, buf_size, buf)) { |
| 520 |
VB2_DEBUG("Unable to read disk.\n"); |
| 521 |
free(buf); |
| 522 |
VbExStreamClose(stream); |
| 523 |
return rv; |
| 524 |
} |
| 525 |
VbExStreamClose(stream); |
| 526 |
|
| 527 |
for (isector = 0; isector < count; isector++) { |
| 528 |
if (memcmp(buf + isector * disk_info->bytes_per_lba, |
| 529 |
VB2_KEYBLOCK_MAGIC, VB2_KEYBLOCK_MAGIC_SIZE)) |
| 530 |
continue; |
| 531 |
VB2_DEBUG("Match on sector %" PRIu64 " / %" PRIu64 "\n", |
| 532 |
start + isector, |
| 533 |
disk_info->lba_count - 1); |
| 534 |
rv = try_minios_kernel(ctx, params, disk_info, start + isector); |
| 535 |
if (rv == VB2_SUCCESS) |
| 536 |
break; |
| 537 |
} |
| 538 |
|
| 539 |
free(buf); |
| 540 |
return rv; |
| 541 |
} |
| 542 |
|
| 543 |
static vb2_error_t try_minios_sector_region(struct vb2_context *ctx, |
| 544 |
struct vb2_kernel_params *params, |
| 545 |
struct vb2_disk_info *disk_info, |
| 546 |
int end_region) |
| 547 |
{ |
| 548 |
const uint64_t disk_count_half = (disk_info->lba_count + 1) / 2; |
| 549 |
const uint64_t check_count_256 = 256 * 1024 |
| 550 |
* 1024 / disk_info->bytes_per_lba; |
| 551 |
const uint64_t batch_count_1 = 1024 |
| 552 |
* 1024 / disk_info->bytes_per_lba; |
| 553 |
const uint64_t check_count = VB2_MIN(disk_count_half, check_count_256); |
| 554 |
const uint64_t batch_count = VB2_MIN(disk_count_half, batch_count_1); |
| 555 |
uint64_t sector; |
| 556 |
uint64_t start; |
| 557 |
uint64_t end; |
| 558 |
const char *region_name; |
| 559 |
vb2_error_t rv = VB2_ERROR_LK_NO_KERNEL_FOUND; |
| 560 |
|
| 561 |
if (!end_region) { |
| 562 |
start = 0; |
| 563 |
end = check_count; |
| 564 |
region_name = "start"; |
| 565 |
} else { |
| 566 |
start = disk_info->lba_count - check_count; |
| 567 |
end = disk_info->lba_count; |
| 568 |
region_name = "end"; |
| 569 |
} |
| 570 |
|
| 571 |
VB2_DEBUG("Checking %s of disk for kernels...\n", region_name); |
| 572 |
for (sector = start; sector < end; sector += batch_count) { |
| 573 |
rv = try_minios_sectors(ctx, params, disk_info, sector, |
| 574 |
batch_count); |
| 575 |
if (rv == VB2_SUCCESS) |
| 576 |
return rv; |
| 577 |
} |
| 578 |
|
| 579 |
return rv; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
vb2_error_t vb2api_load_minios_kernel(struct vb2_context *ctx, |
| 588 |
struct vb2_kernel_params *params, |
| 589 |
struct vb2_disk_info *disk_info, |
| 590 |
uint32_t minios_flags) |
| 591 |
{ |
| 592 |
vb2_error_t rv; |
| 593 |
int end_region_first = vb2_nv_get(ctx, VB2_NV_MINIOS_PRIORITY); |
| 594 |
|
| 595 |
if (minios_flags & VB2_MINIOS_FLAG_NON_ACTIVE) |
| 596 |
rv = VB2_ERROR_UNKNOWN; |
| 597 |
else |
| 598 |
rv = try_minios_sector_region(ctx, params, disk_info, |
| 599 |
end_region_first); |
| 600 |
|
| 601 |
if (rv) |
| 602 |
rv = try_minios_sector_region(ctx, params, disk_info, |
| 603 |
!end_region_first); |
| 604 |
|
| 605 |
if (rv == VB2_SUCCESS) |
| 606 |
params->disk_handle = disk_info->handle; |
| 607 |
|
| 608 |
return rv; |
| 609 |
} |
| 610 |
|
| 611 |
vb2_error_t vb2api_load_kernel(struct vb2_context *ctx, |
| 612 |
struct vb2_kernel_params *params, |
| 613 |
struct vb2_disk_info *disk_info) |
| 614 |
{ |
| 615 |
struct vb2_shared_data *sd = vb2_get_sd(ctx); |
| 616 |
int found_partitions = 0; |
| 617 |
uint32_t lowest_version = LOWEST_TPM_VERSION; |
| 618 |
vb2_error_t rv; |
| 619 |
|
| 620 |
|
| 621 |
params->partition_number = 0; |
| 622 |
|
| 623 |
|
| 624 |
GptData gpt; |
| 625 |
gpt.sector_bytes = (uint32_t)disk_info->bytes_per_lba; |
| 626 |
gpt.streaming_drive_sectors = disk_info->streaming_lba_count |
| 627 |
?: disk_info->lba_count; |
| 628 |
gpt.gpt_drive_sectors = disk_info->lba_count; |
| 629 |
gpt.flags = disk_info->flags & VB2_DISK_FLAG_EXTERNAL_GPT |
| 630 |
? GPT_FLAG_EXTERNAL : 0; |
| 631 |
if (AllocAndReadGptData(disk_info->handle, &gpt)) { |
| 632 |
VB2_DEBUG("Unable to read GPT data\n"); |
| 633 |
goto gpt_done; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
if (GptInit(&gpt)) { |
| 638 |
VB2_DEBUG("Error parsing GPT\n"); |
| 639 |
goto gpt_done; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
uint64_t part_start, part_size; |
| 644 |
while (GptNextKernelEntry(&gpt, &part_start, &part_size) == |
| 645 |
GPT_SUCCESS) { |
| 646 |
|
| 647 |
VB2_DEBUG("Found kernel entry at %" |
| 648 |
PRIu64 " size %" PRIu64 "\n", |
| 649 |
part_start, part_size); |
| 650 |
|
| 651 |
|
| 652 |
found_partitions++; |
| 653 |
|
| 654 |
|
| 655 |
VbExStream_t stream = NULL; |
| 656 |
if (VbExStreamOpen(disk_info->handle, |
| 657 |
part_start, part_size, &stream)) { |
| 658 |
VB2_DEBUG("Partition error getting stream.\n"); |
| 659 |
VB2_DEBUG("Marking kernel as invalid.\n"); |
| 660 |
GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD); |
| 661 |
continue; |
| 662 |
} |
| 663 |
|
| 664 |
uint32_t lpflags = 0; |
| 665 |
if (params->partition_number > 0) { |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
lpflags |= VB2_LOAD_PARTITION_FLAG_VBLOCK_ONLY; |
| 671 |
} |
| 672 |
|
| 673 |
uint32_t kernel_version = 0; |
| 674 |
rv = vb2_load_partition(ctx, params, stream, lpflags, |
| 675 |
&kernel_version); |
| 676 |
VbExStreamClose(stream); |
| 677 |
|
| 678 |
if (rv) { |
| 679 |
VB2_DEBUG("Marking kernel as invalid (err=%x).\n", rv); |
| 680 |
GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD); |
| 681 |
continue; |
| 682 |
} |
| 683 |
|
| 684 |
int keyblock_valid = sd->flags & VB2_SD_FLAG_KERNEL_SIGNED; |
| 685 |
|
| 686 |
if (keyblock_valid && lowest_version > kernel_version) |
| 687 |
lowest_version = kernel_version; |
| 688 |
|
| 689 |
VB2_DEBUG("Keyblock valid: %d\n", keyblock_valid); |
| 690 |
VB2_DEBUG("Combined version: %u\n", kernel_version); |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
if (lpflags & VB2_LOAD_PARTITION_FLAG_VBLOCK_ONLY) |
| 697 |
continue; |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
params->partition_number = gpt.current_kernel + 1; |
| 706 |
|
| 707 |
sd->kernel_version = kernel_version; |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
GetCurrentKernelUniqueGuid(&gpt, ¶ms->partition_guid); |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
if (!(ctx->flags & VB2_CONTEXT_NOFAIL_BOOT)) |
| 721 |
GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY); |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
if (ctx->boot_mode == VB2_BOOT_MODE_MANUAL_RECOVERY || |
| 729 |
!keyblock_valid) { |
| 730 |
VB2_DEBUG("In recovery mode or dev-signed kernel\n"); |
| 731 |
break; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
if (sd->kernel_version == sd->kernel_version_secdata) { |
| 742 |
VB2_DEBUG("Same kernel version\n"); |
| 743 |
break; |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
gpt_done: |
| 748 |
|
| 749 |
WriteAndFreeGptData(disk_info->handle, &gpt); |
| 750 |
|
| 751 |
|
| 752 |
if (params->partition_number > 0) { |
| 753 |
VB2_DEBUG("Good partition %d\n", params->partition_number); |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
if (lowest_version != LOWEST_TPM_VERSION && |
| 761 |
lowest_version > sd->kernel_version_secdata) |
| 762 |
sd->kernel_version_secdata = lowest_version; |
| 763 |
|
| 764 |
|
| 765 |
rv = VB2_SUCCESS; |
| 766 |
params->disk_handle = disk_info->handle; |
| 767 |
} else if (found_partitions > 0) { |
| 768 |
rv = VB2_ERROR_LK_INVALID_KERNEL_FOUND; |
| 769 |
} else { |
| 770 |
rv = VB2_ERROR_LK_NO_KERNEL_FOUND; |
| 771 |
} |
| 772 |
|
| 773 |
return rv; |
| 774 |
} |