| 1 |
|
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
#include <boot_device.h> |
| 5 |
#include <cbfs.h> |
| 6 |
#include <cbmem.h> |
| 7 |
#include <commonlib/bsd/cbfs_private.h> |
| 8 |
#include <commonlib/bsd/compression.h> |
| 9 |
#include <commonlib/list.h> |
| 10 |
#include <console/console.h> |
| 11 |
#include <fmap.h> |
| 12 |
#include <lib.h> |
| 13 |
#include <metadata_hash.h> |
| 14 |
#include <security/tpm/tspi/crtm.h> |
| 15 |
#include <security/vboot/vboot_common.h> |
| 16 |
#include <security/vboot/misc.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
#include <symbols.h> |
| 20 |
#include <thread.h> |
| 21 |
#include <timestamp.h> |
| 22 |
|
| 23 |
#if ENV_X86 && (ENV_POSTCAR || ENV_SMM) |
| 24 |
struct mem_pool cbfs_cache = MEM_POOL_INIT(NULL, 0, 0); |
| 25 |
#elif CONFIG(POSTRAM_CBFS_CACHE_IN_BSS) && ENV_RAMSTAGE |
| 26 |
static u8 cache_buffer[CONFIG_RAMSTAGE_CBFS_CACHE_SIZE] __aligned(CONFIG_CBFS_CACHE_ALIGN); |
| 27 |
struct mem_pool cbfs_cache = |
| 28 |
MEM_POOL_INIT(cache_buffer, sizeof(cache_buffer), CONFIG_CBFS_CACHE_ALIGN); |
| 29 |
#else |
| 30 |
struct mem_pool cbfs_cache = |
| 31 |
MEM_POOL_INIT(_cbfs_cache, REGION_SIZE(cbfs_cache), CONFIG_CBFS_CACHE_ALIGN); |
| 32 |
#endif |
| 33 |
|
| 34 |
static void switch_to_postram_cache(int unused) |
| 35 |
{ |
| 36 |
if (_preram_cbfs_cache != _postram_cbfs_cache) |
| 37 |
mem_pool_init(&cbfs_cache, _postram_cbfs_cache, REGION_SIZE(postram_cbfs_cache), |
| 38 |
CONFIG_CBFS_CACHE_ALIGN); |
| 39 |
} |
| 40 |
CBMEM_CREATION_HOOK(switch_to_postram_cache); |
| 41 |
|
| 42 |
enum cb_err _cbfs_boot_lookup(const char *name, bool force_ro, |
| 43 |
union cbfs_mdata *mdata, struct region_device *rdev) |
| 44 |
{ |
| 45 |
const struct cbfs_boot_device *cbd = cbfs_get_boot_device(force_ro); |
| 46 |
if (!cbd) |
| 47 |
return CB_ERR; |
| 48 |
|
| 49 |
size_t data_offset; |
| 50 |
enum cb_err err = CB_CBFS_CACHE_FULL; |
| 51 |
if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size) |
| 52 |
err = cbfs_mcache_lookup(cbd->mcache, cbd->mcache_size, |
| 53 |
name, mdata, &data_offset); |
| 54 |
if (err == CB_CBFS_CACHE_FULL) { |
| 55 |
struct vb2_hash *metadata_hash = NULL; |
| 56 |
if (CONFIG(TOCTOU_SAFETY)) { |
| 57 |
if (ENV_SMM) |
| 58 |
dead_code(); |
| 59 |
if (!cbd->mcache_size) |
| 60 |
die("Cannot access CBFS TOCTOU-safely in " ENV_STRING " before CBMEM init!\n"); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
assert(cbd == vboot_get_cbfs_boot_device()); |
| 65 |
if (!CONFIG(VBOOT) |
| 66 |
|| vb2api_get_metadata_hash(vboot_get_context(), &metadata_hash) |
| 67 |
!= VB2_SUCCESS) |
| 68 |
die("Failed to get RW metadata hash"); |
| 69 |
} |
| 70 |
err = cbfs_lookup(&cbd->rdev, name, mdata, &data_offset, metadata_hash); |
| 71 |
} |
| 72 |
|
| 73 |
if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && !force_ro && err == CB_CBFS_NOT_FOUND) { |
| 74 |
printk(BIOS_INFO, "CBFS: Fall back to RO region for %s\n", name); |
| 75 |
return _cbfs_boot_lookup(name, true, mdata, rdev); |
| 76 |
} |
| 77 |
if (err) { |
| 78 |
if (err == CB_CBFS_NOT_FOUND) |
| 79 |
printk(BIOS_WARNING, "CBFS: '%s' not found.\n", name); |
| 80 |
else if (err == CB_CBFS_HASH_MISMATCH) |
| 81 |
printk(BIOS_ERR, "CBFS ERROR: metadata hash mismatch!\n"); |
| 82 |
else |
| 83 |
printk(BIOS_ERR, "CBFS ERROR: error %d when looking up '%s'\n", |
| 84 |
err, name); |
| 85 |
return err; |
| 86 |
} |
| 87 |
|
| 88 |
if (rdev_chain(rdev, &cbd->rdev, data_offset, be32toh(mdata->h.len))) |
| 89 |
return CB_ERR; |
| 90 |
|
| 91 |
return CB_SUCCESS; |
| 92 |
} |
| 93 |
|
| 94 |
void cbfs_unmap(void *mapping) |
| 95 |
{ |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
mem_pool_free(&cbfs_cache, mapping); |
| 104 |
} |
| 105 |
|
| 106 |
static inline bool fsps_env(void) |
| 107 |
{ |
| 108 |
|
| 109 |
if (ENV_RAMSTAGE) |
| 110 |
return true; |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
static inline bool fspm_env(void) |
| 115 |
{ |
| 116 |
|
| 117 |
if (ENV_RAMINIT) |
| 118 |
return true; |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
static inline bool cbfs_lz4_enabled(void) |
| 123 |
{ |
| 124 |
if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZ4)) |
| 125 |
return true; |
| 126 |
if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZ4)) |
| 127 |
return true; |
| 128 |
|
| 129 |
if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES)) |
| 130 |
return false; |
| 131 |
|
| 132 |
if (ENV_SMM) |
| 133 |
return false; |
| 134 |
|
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
static inline bool cbfs_lzma_enabled(void) |
| 139 |
{ |
| 140 |
if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZMA)) |
| 141 |
return true; |
| 142 |
if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA)) |
| 143 |
return true; |
| 144 |
|
| 145 |
|
| 146 |
if (ENV_PAYLOAD_LOADER) |
| 147 |
return true; |
| 148 |
|
| 149 |
if (!CONFIG(COMPRESS_RAMSTAGE_LZMA)) |
| 150 |
return false; |
| 151 |
|
| 152 |
if (CONFIG(POSTCAR_STAGE)) |
| 153 |
return ENV_POSTCAR; |
| 154 |
|
| 155 |
if (CONFIG(SEPARATE_ROMSTAGE)) |
| 156 |
return ENV_SEPARATE_ROMSTAGE; |
| 157 |
|
| 158 |
return ENV_BOOTBLOCK; |
| 159 |
} |
| 160 |
|
| 161 |
static bool cbfs_file_hash_mismatch(const void *buffer, size_t size, |
| 162 |
const union cbfs_mdata *mdata, bool skip_verification) |
| 163 |
{ |
| 164 |
|
| 165 |
if (!CONFIG(CBFS_VERIFICATION) && !CONFIG(TPM_MEASURED_BOOT)) |
| 166 |
return false; |
| 167 |
|
| 168 |
const struct vb2_hash *hash = NULL; |
| 169 |
|
| 170 |
if (CONFIG(CBFS_VERIFICATION) && !skip_verification) { |
| 171 |
hash = cbfs_file_hash(mdata); |
| 172 |
if (!hash) { |
| 173 |
ERROR("'%s' does not have a file hash!\n", mdata->h.filename); |
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
vb2_error_t rv = vb2_hash_verify(vboot_hwcrypto_allowed(), buffer, size, hash); |
| 178 |
if (rv != VB2_SUCCESS) { |
| 179 |
ERROR("'%s' file hash mismatch!\n", mdata->h.filename); |
| 180 |
if (CONFIG(VBOOT_CBFS_INTEGRATION) && !vboot_recovery_mode_enabled() |
| 181 |
&& vboot_logic_executed()) |
| 182 |
vboot_fail_and_reboot(vboot_get_context(), VB2_RECOVERY_FW_BODY, |
| 183 |
rv); |
| 184 |
return true; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if (CONFIG(TPM_MEASURED_BOOT) && !ENV_SMM) { |
| 189 |
struct vb2_hash calculated_hash; |
| 190 |
|
| 191 |
|
| 192 |
if (!hash || hash->algo != TPM_MEASURE_ALGO) { |
| 193 |
if (vb2_hash_calculate(vboot_hwcrypto_allowed(), buffer, size, |
| 194 |
TPM_MEASURE_ALGO, &calculated_hash)) |
| 195 |
hash = NULL; |
| 196 |
else |
| 197 |
hash = &calculated_hash; |
| 198 |
} |
| 199 |
|
| 200 |
if (!hash || |
| 201 |
tspi_cbfs_measurement(mdata->h.filename, be32toh(mdata->h.type), hash)) |
| 202 |
ERROR("failed to measure '%s' into TPM log\n", mdata->h.filename); |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
static size_t cbfs_load_and_decompress(const struct region_device *rdev, void *buffer, |
| 210 |
size_t buffer_size, uint32_t compression, |
| 211 |
const union cbfs_mdata *mdata, bool skip_verification) |
| 212 |
{ |
| 213 |
size_t in_size = region_device_sz(rdev); |
| 214 |
size_t out_size = 0; |
| 215 |
void *map; |
| 216 |
|
| 217 |
DEBUG("Decompressing %zu bytes from '%s' to %p with algo %d\n", |
| 218 |
in_size, mdata->h.filename, buffer, compression); |
| 219 |
|
| 220 |
if (CONFIG(CBFS_VERIFICATION) && !CONFIG(CBFS_ALLOW_UNVERIFIED_DECOMPRESSION) && |
| 221 |
skip_verification && compression != CBFS_COMPRESS_NONE) { |
| 222 |
ERROR("Refusing to decompress unverified file '%s' with algo %d\n", |
| 223 |
mdata->h.filename, compression); |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
switch (compression) { |
| 228 |
case CBFS_COMPRESS_NONE: |
| 229 |
if (buffer_size < in_size) |
| 230 |
return 0; |
| 231 |
if (rdev_readat(rdev, buffer, 0, in_size) != in_size) |
| 232 |
return 0; |
| 233 |
if (cbfs_file_hash_mismatch(buffer, in_size, mdata, skip_verification)) |
| 234 |
return 0; |
| 235 |
return in_size; |
| 236 |
|
| 237 |
case CBFS_COMPRESS_LZ4: |
| 238 |
if (!cbfs_lz4_enabled()) |
| 239 |
return 0; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
map = rdev_mmap_full(rdev); |
| 244 |
if (map == NULL) |
| 245 |
return 0; |
| 246 |
|
| 247 |
if (!cbfs_file_hash_mismatch(map, in_size, mdata, skip_verification)) { |
| 248 |
timestamp_add_now(TS_ULZ4F_START); |
| 249 |
out_size = ulz4fn(map, in_size, buffer, buffer_size); |
| 250 |
timestamp_add_now(TS_ULZ4F_END); |
| 251 |
} |
| 252 |
|
| 253 |
rdev_munmap(rdev, map); |
| 254 |
|
| 255 |
return out_size; |
| 256 |
|
| 257 |
case CBFS_COMPRESS_LZMA: |
| 258 |
if (!cbfs_lzma_enabled()) |
| 259 |
return 0; |
| 260 |
map = rdev_mmap_full(rdev); |
| 261 |
if (map == NULL) |
| 262 |
return 0; |
| 263 |
|
| 264 |
if (!cbfs_file_hash_mismatch(map, in_size, mdata, skip_verification)) { |
| 265 |
|
| 266 |
timestamp_add_now(TS_ULZMA_START); |
| 267 |
out_size = ulzman(map, in_size, buffer, buffer_size); |
| 268 |
timestamp_add_now(TS_ULZMA_END); |
| 269 |
} |
| 270 |
|
| 271 |
rdev_munmap(rdev, map); |
| 272 |
|
| 273 |
return out_size; |
| 274 |
|
| 275 |
default: |
| 276 |
return 0; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
struct cbfs_preload_context { |
| 281 |
struct region_device rdev; |
| 282 |
struct thread_handle handle; |
| 283 |
struct list_node list_node; |
| 284 |
void *buffer; |
| 285 |
char name[]; |
| 286 |
}; |
| 287 |
|
| 288 |
static struct list_node cbfs_preload_context_list; |
| 289 |
|
| 290 |
static struct cbfs_preload_context *alloc_cbfs_preload_context(size_t additional) |
| 291 |
{ |
| 292 |
struct cbfs_preload_context *context; |
| 293 |
size_t size = sizeof(*context) + additional; |
| 294 |
|
| 295 |
context = mem_pool_alloc(&cbfs_cache, size); |
| 296 |
|
| 297 |
if (!context) |
| 298 |
return NULL; |
| 299 |
|
| 300 |
memset(context, 0, size); |
| 301 |
|
| 302 |
return context; |
| 303 |
} |
| 304 |
|
| 305 |
static void append_cbfs_preload_context(struct cbfs_preload_context *context) |
| 306 |
{ |
| 307 |
list_append(&context->list_node, &cbfs_preload_context_list); |
| 308 |
} |
| 309 |
|
| 310 |
static void free_cbfs_preload_context(struct cbfs_preload_context *context) |
| 311 |
{ |
| 312 |
list_remove(&context->list_node); |
| 313 |
|
| 314 |
mem_pool_free(&cbfs_cache, context); |
| 315 |
} |
| 316 |
|
| 317 |
static enum cb_err cbfs_preload_thread_entry(void *arg) |
| 318 |
{ |
| 319 |
struct cbfs_preload_context *context = arg; |
| 320 |
|
| 321 |
if (rdev_read_full(&context->rdev, context->buffer) < 0) { |
| 322 |
ERROR("%s(name='%s') readat failed\n", __func__, context->name); |
| 323 |
return CB_ERR; |
| 324 |
} |
| 325 |
|
| 326 |
return CB_SUCCESS; |
| 327 |
} |
| 328 |
|
| 329 |
void cbfs_preload(const char *name) |
| 330 |
{ |
| 331 |
struct region_device rdev; |
| 332 |
union cbfs_mdata mdata; |
| 333 |
struct cbfs_preload_context *context; |
| 334 |
bool force_ro = false; |
| 335 |
size_t size; |
| 336 |
|
| 337 |
if (!CONFIG(CBFS_PRELOAD)) |
| 338 |
dead_code(); |
| 339 |
|
| 340 |
|
| 341 |
if (ENV_SEPARATE_ROMSTAGE && CONFIG(VBOOT_STARTS_IN_ROMSTAGE)) |
| 342 |
return; |
| 343 |
|
| 344 |
DEBUG("%s(name='%s')\n", __func__, name); |
| 345 |
|
| 346 |
if (_cbfs_boot_lookup(name, force_ro, &mdata, &rdev)) |
| 347 |
return; |
| 348 |
|
| 349 |
size = region_device_sz(&rdev); |
| 350 |
|
| 351 |
context = alloc_cbfs_preload_context(strlen(name) + 1); |
| 352 |
if (!context) { |
| 353 |
ERROR("%s(name='%s') failed to allocate preload context\n", __func__, name); |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
context->buffer = mem_pool_alloc(&cbfs_cache, size); |
| 358 |
if (context->buffer == NULL) { |
| 359 |
ERROR("%s(name='%s') failed to allocate %zu bytes for preload buffer\n", |
| 360 |
__func__, name, size); |
| 361 |
goto out; |
| 362 |
} |
| 363 |
|
| 364 |
context->rdev = rdev; |
| 365 |
strcpy(context->name, name); |
| 366 |
|
| 367 |
append_cbfs_preload_context(context); |
| 368 |
|
| 369 |
if (thread_run(&context->handle, cbfs_preload_thread_entry, context) == 0) |
| 370 |
return; |
| 371 |
|
| 372 |
ERROR("%s(name='%s') failed to start preload thread\n", __func__, name); |
| 373 |
mem_pool_free(&cbfs_cache, context->buffer); |
| 374 |
|
| 375 |
out: |
| 376 |
free_cbfs_preload_context(context); |
| 377 |
} |
| 378 |
|
| 379 |
void cbfs_preload_wait_for_all(void) |
| 380 |
{ |
| 381 |
struct cbfs_preload_context *context; |
| 382 |
|
| 383 |
list_for_each(context, cbfs_preload_context_list, list_node) |
| 384 |
thread_join(&context->handle); |
| 385 |
} |
| 386 |
|
| 387 |
static struct cbfs_preload_context *find_cbfs_preload_context(const char *name) |
| 388 |
{ |
| 389 |
struct cbfs_preload_context *context; |
| 390 |
|
| 391 |
list_for_each(context, cbfs_preload_context_list, list_node) { |
| 392 |
if (strcmp(context->name, name) == 0) |
| 393 |
return context; |
| 394 |
} |
| 395 |
|
| 396 |
return NULL; |
| 397 |
} |
| 398 |
|
| 399 |
static enum cb_err get_preload_rdev(struct region_device *rdev, const char *name) |
| 400 |
{ |
| 401 |
enum cb_err err; |
| 402 |
struct cbfs_preload_context *context; |
| 403 |
|
| 404 |
if (!CONFIG(CBFS_PRELOAD) || !ENV_SUPPORTS_COOP) |
| 405 |
return CB_ERR_ARG; |
| 406 |
|
| 407 |
context = find_cbfs_preload_context(name); |
| 408 |
if (!context) |
| 409 |
return CB_ERR_ARG; |
| 410 |
|
| 411 |
err = thread_join(&context->handle); |
| 412 |
if (err != CB_SUCCESS) { |
| 413 |
ERROR("%s(name='%s') Preload thread failed: %u\n", __func__, name, err); |
| 414 |
|
| 415 |
goto out; |
| 416 |
} |
| 417 |
|
| 418 |
if (rdev_chain_mem(rdev, context->buffer, region_device_sz(&context->rdev)) != 0) { |
| 419 |
ERROR("%s(name='%s') chaining failed\n", __func__, name); |
| 420 |
|
| 421 |
err = CB_ERR; |
| 422 |
goto out; |
| 423 |
} |
| 424 |
|
| 425 |
err = CB_SUCCESS; |
| 426 |
|
| 427 |
DEBUG("%s(name='%s') preload successful\n", __func__, name); |
| 428 |
|
| 429 |
out: |
| 430 |
free_cbfs_preload_context(context); |
| 431 |
|
| 432 |
return err; |
| 433 |
} |
| 434 |
|
| 435 |
static void *do_alloc(union cbfs_mdata *mdata, struct region_device *rdev, |
| 436 |
cbfs_allocator_t allocator, void *arg, size_t *size_out, |
| 437 |
bool skip_verification) |
| 438 |
{ |
| 439 |
size_t size = region_device_sz(rdev); |
| 440 |
void *loc = NULL; |
| 441 |
|
| 442 |
uint32_t compression = CBFS_COMPRESS_NONE; |
| 443 |
const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(mdata, |
| 444 |
CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr)); |
| 445 |
if (cattr) { |
| 446 |
compression = be32toh(cattr->compression); |
| 447 |
size = be32toh(cattr->decompressed_size); |
| 448 |
} |
| 449 |
|
| 450 |
if (size_out) |
| 451 |
*size_out = size; |
| 452 |
|
| 453 |
|
| 454 |
if (allocator) { |
| 455 |
loc = allocator(arg, size, mdata); |
| 456 |
} else if (compression == CBFS_COMPRESS_NONE) { |
| 457 |
void *mapping = rdev_mmap_full(rdev); |
| 458 |
if (!mapping) |
| 459 |
return NULL; |
| 460 |
if (cbfs_file_hash_mismatch(mapping, size, mdata, skip_verification)) { |
| 461 |
rdev_munmap(rdev, mapping); |
| 462 |
return NULL; |
| 463 |
} |
| 464 |
return mapping; |
| 465 |
} else if (!cbfs_cache.size) { |
| 466 |
|
| 467 |
|
| 468 |
ERROR("Cannot map compressed file %s without cbfs_cache\n", mdata->h.filename); |
| 469 |
return NULL; |
| 470 |
} else { |
| 471 |
loc = mem_pool_alloc(&cbfs_cache, size); |
| 472 |
} |
| 473 |
|
| 474 |
if (!loc) { |
| 475 |
ERROR("'%s' allocation failure\n", mdata->h.filename); |
| 476 |
return NULL; |
| 477 |
} |
| 478 |
|
| 479 |
size = cbfs_load_and_decompress(rdev, loc, size, compression, mdata, skip_verification); |
| 480 |
if (!size) |
| 481 |
return NULL; |
| 482 |
|
| 483 |
return loc; |
| 484 |
} |
| 485 |
|
| 486 |
void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg, |
| 487 |
size_t *size_out, bool force_ro, enum cbfs_type *type) |
| 488 |
{ |
| 489 |
struct region_device rdev; |
| 490 |
bool preload_successful = false; |
| 491 |
union cbfs_mdata mdata; |
| 492 |
|
| 493 |
DEBUG("%s(name='%s', alloc=%p(%p), force_ro=%s, type=%d)\n", __func__, name, allocator, |
| 494 |
arg, force_ro ? "true" : "false", type ? *type : -1); |
| 495 |
|
| 496 |
if (_cbfs_boot_lookup(name, force_ro, &mdata, &rdev)) |
| 497 |
return NULL; |
| 498 |
|
| 499 |
if (type) { |
| 500 |
const enum cbfs_type real_type = be32toh(mdata.h.type); |
| 501 |
if (*type == CBFS_TYPE_QUERY) |
| 502 |
*type = real_type; |
| 503 |
else if (*type != real_type) { |
| 504 |
ERROR("'%s' type mismatch (is %u, expected %u)\n", |
| 505 |
mdata.h.filename, real_type, *type); |
| 506 |
return NULL; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
if (!force_ro && get_preload_rdev(&rdev, name) == CB_SUCCESS) |
| 512 |
preload_successful = true; |
| 513 |
|
| 514 |
void *ret = do_alloc(&mdata, &rdev, allocator, arg, size_out, false); |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
if (preload_successful) |
| 519 |
cbfs_unmap(rdev_mmap_full(&rdev)); |
| 520 |
|
| 521 |
return ret; |
| 522 |
} |
| 523 |
|
| 524 |
void *_cbfs_unverified_area_alloc(const char *area, const char *name, |
| 525 |
cbfs_allocator_t allocator, void *arg, size_t *size_out) |
| 526 |
{ |
| 527 |
struct region_device area_rdev, file_rdev; |
| 528 |
union cbfs_mdata mdata; |
| 529 |
size_t data_offset; |
| 530 |
|
| 531 |
DEBUG("%s(area='%s', name='%s', alloc=%p(%p))\n", __func__, area, name, allocator, arg); |
| 532 |
|
| 533 |
if (fmap_locate_area_as_rdev(area, &area_rdev)) |
| 534 |
return NULL; |
| 535 |
|
| 536 |
if (cbfs_lookup(&area_rdev, name, &mdata, &data_offset, NULL)) { |
| 537 |
ERROR("'%s' not found in '%s'\n", name, area); |
| 538 |
return NULL; |
| 539 |
} |
| 540 |
|
| 541 |
if (rdev_chain(&file_rdev, &area_rdev, data_offset, be32toh(mdata.h.len))) |
| 542 |
return NULL; |
| 543 |
|
| 544 |
return do_alloc(&mdata, &file_rdev, allocator, arg, size_out, true); |
| 545 |
} |
| 546 |
|
| 547 |
void *_cbfs_default_allocator(void *arg, size_t size, const union cbfs_mdata *unused) |
| 548 |
{ |
| 549 |
struct _cbfs_default_allocator_arg *darg = arg; |
| 550 |
if (size > darg->buf_size) |
| 551 |
return NULL; |
| 552 |
return darg->buf; |
| 553 |
} |
| 554 |
|
| 555 |
void *_cbfs_cbmem_allocator(void *arg, size_t size, const union cbfs_mdata *unused) |
| 556 |
{ |
| 557 |
return cbmem_add((uintptr_t)arg, size); |
| 558 |
} |
| 559 |
|
| 560 |
enum cb_err cbfs_prog_stage_load(struct prog *pstage) |
| 561 |
{ |
| 562 |
union cbfs_mdata mdata; |
| 563 |
struct region_device rdev; |
| 564 |
enum cb_err err; |
| 565 |
|
| 566 |
prog_locate_hook(pstage); |
| 567 |
|
| 568 |
if ((err = _cbfs_boot_lookup(prog_name(pstage), false, &mdata, &rdev))) |
| 569 |
return err; |
| 570 |
|
| 571 |
assert(be32toh(mdata.h.type) == CBFS_TYPE_STAGE); |
| 572 |
pstage->cbfs_type = CBFS_TYPE_STAGE; |
| 573 |
|
| 574 |
enum cbfs_compression compression = CBFS_COMPRESS_NONE; |
| 575 |
const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(&mdata, |
| 576 |
CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr)); |
| 577 |
if (cattr) |
| 578 |
compression = be32toh(cattr->compression); |
| 579 |
|
| 580 |
const struct cbfs_file_attr_stageheader *sattr = cbfs_find_attr(&mdata, |
| 581 |
CBFS_FILE_ATTR_TAG_STAGEHEADER, sizeof(*sattr)); |
| 582 |
if (!sattr) |
| 583 |
return CB_ERR; |
| 584 |
prog_set_area(pstage, (void *)(uintptr_t)be64toh(sattr->loadaddr), |
| 585 |
be32toh(sattr->memlen)); |
| 586 |
prog_set_entry(pstage, prog_start(pstage) + |
| 587 |
be32toh(sattr->entry_offset), NULL); |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && |
| 592 |
!CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) { |
| 593 |
void *mapping = rdev_mmap_full(&rdev); |
| 594 |
rdev_munmap(&rdev, mapping); |
| 595 |
if (cbfs_file_hash_mismatch(mapping, region_device_sz(&rdev), &mdata, false)) |
| 596 |
return CB_CBFS_HASH_MISMATCH; |
| 597 |
if (mapping == prog_start(pstage)) |
| 598 |
return CB_SUCCESS; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
if (cbfs_lz4_enabled() && compression == CBFS_COMPRESS_LZ4) { |
| 604 |
size_t in_size = region_device_sz(&rdev); |
| 605 |
void *compr_start = prog_start(pstage) + prog_size(pstage) - in_size; |
| 606 |
if (rdev_readat(&rdev, compr_start, 0, in_size) != in_size) |
| 607 |
return CB_ERR; |
| 608 |
rdev_chain_mem(&rdev, compr_start, in_size); |
| 609 |
} |
| 610 |
|
| 611 |
size_t fsize = cbfs_load_and_decompress(&rdev, prog_start(pstage), prog_size(pstage), |
| 612 |
compression, &mdata, false); |
| 613 |
if (!fsize) |
| 614 |
return CB_ERR; |
| 615 |
|
| 616 |
|
| 617 |
memset(prog_start(pstage) + fsize, 0, prog_size(pstage) - fsize); |
| 618 |
|
| 619 |
prog_segment_loaded((uintptr_t)prog_start(pstage), prog_size(pstage), |
| 620 |
SEG_FINAL); |
| 621 |
|
| 622 |
return CB_SUCCESS; |
| 623 |
} |
| 624 |
|
| 625 |
void cbfs_boot_device_find_mcache(struct cbfs_boot_device *cbd, uint32_t id) |
| 626 |
{ |
| 627 |
if (CONFIG(NO_CBFS_MCACHE) || ENV_SMM) |
| 628 |
return; |
| 629 |
|
| 630 |
if (cbd->mcache_size) |
| 631 |
return; |
| 632 |
|
| 633 |
const struct cbmem_entry *entry; |
| 634 |
if (ENV_HAS_CBMEM && |
| 635 |
(entry = cbmem_entry_find(id))) { |
| 636 |
cbd->mcache = cbmem_entry_start(entry); |
| 637 |
cbd->mcache_size = cbmem_entry_size(entry); |
| 638 |
} else if (ENV_ROMSTAGE_OR_BEFORE) { |
| 639 |
u8 *boundary = _ecbfs_mcache - REGION_SIZE(cbfs_mcache) * |
| 640 |
CONFIG_CBFS_MCACHE_RW_PERCENTAGE / 100; |
| 641 |
boundary = (u8 *)ALIGN_DOWN((uintptr_t)boundary, CBFS_MCACHE_ALIGNMENT); |
| 642 |
if (id == CBMEM_ID_CBFS_RO_MCACHE) { |
| 643 |
cbd->mcache = _cbfs_mcache; |
| 644 |
cbd->mcache_size = boundary - _cbfs_mcache; |
| 645 |
} else if (id == CBMEM_ID_CBFS_RW_MCACHE) { |
| 646 |
cbd->mcache = boundary; |
| 647 |
cbd->mcache_size = _ecbfs_mcache - boundary; |
| 648 |
} |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
enum cb_err cbfs_init_boot_device(const struct cbfs_boot_device *cbd, |
| 653 |
struct vb2_hash *mdata_hash) |
| 654 |
{ |
| 655 |
|
| 656 |
if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size > 0) |
| 657 |
return cbfs_mcache_build(&cbd->rdev, cbd->mcache, cbd->mcache_size, mdata_hash); |
| 658 |
|
| 659 |
|
| 660 |
if (!CONFIG(CBFS_VERIFICATION) || !mdata_hash) |
| 661 |
return CB_SUCCESS; |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
enum cb_err err = cbfs_walk(&cbd->rdev, NULL, NULL, mdata_hash, 0); |
| 666 |
if (err == CB_CBFS_NOT_FOUND) |
| 667 |
err = CB_SUCCESS; |
| 668 |
return err; |
| 669 |
} |
| 670 |
|
| 671 |
const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro) |
| 672 |
{ |
| 673 |
static struct cbfs_boot_device ro; |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
if (ENV_INITIAL_STAGE && !force_ro && !region_device_sz(&ro.rdev)) |
| 678 |
cbfs_get_boot_device(true); |
| 679 |
|
| 680 |
if (!force_ro) { |
| 681 |
const struct cbfs_boot_device *rw = vboot_get_cbfs_boot_device(); |
| 682 |
|
| 683 |
|
| 684 |
if (rw) |
| 685 |
return rw; |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
cbfs_boot_device_find_mcache(&ro, CBMEM_ID_CBFS_RO_MCACHE); |
| 692 |
|
| 693 |
if (region_device_sz(&ro.rdev)) |
| 694 |
return &ro; |
| 695 |
|
| 696 |
if (fmap_locate_area_as_rdev("COREBOOT", &ro.rdev)) |
| 697 |
die("Cannot locate primary CBFS"); |
| 698 |
|
| 699 |
if (ENV_INITIAL_STAGE) { |
| 700 |
enum cb_err err = cbfs_init_boot_device(&ro, metadata_hash_get()); |
| 701 |
if (err == CB_CBFS_HASH_MISMATCH) |
| 702 |
die("RO CBFS metadata hash verification failure"); |
| 703 |
else if (CONFIG(TOCTOU_SAFETY) && err == CB_CBFS_CACHE_FULL) |
| 704 |
die("RO mcache overflow breaks TOCTOU safety!\n"); |
| 705 |
else if (err && err != CB_CBFS_CACHE_FULL) |
| 706 |
die("RO CBFS initialization error: %d", err); |
| 707 |
} |
| 708 |
|
| 709 |
return &ro; |
| 710 |
} |
| 711 |
|
| 712 |
#if !CONFIG(NO_CBFS_MCACHE) |
| 713 |
static void mcache_to_cbmem(const struct cbfs_boot_device *cbd, u32 cbmem_id) |
| 714 |
{ |
| 715 |
if (!cbd) |
| 716 |
return; |
| 717 |
|
| 718 |
size_t real_size = cbfs_mcache_real_size(cbd->mcache, cbd->mcache_size); |
| 719 |
void *cbmem_mcache = cbmem_add(cbmem_id, real_size); |
| 720 |
if (!cbmem_mcache) { |
| 721 |
printk(BIOS_ERR, "Cannot allocate CBMEM mcache %#x (%#zx bytes)!\n", |
| 722 |
cbmem_id, real_size); |
| 723 |
return; |
| 724 |
} |
| 725 |
memcpy(cbmem_mcache, cbd->mcache, real_size); |
| 726 |
} |
| 727 |
|
| 728 |
static void cbfs_mcache_migrate(int unused) |
| 729 |
{ |
| 730 |
mcache_to_cbmem(vboot_get_cbfs_boot_device(), CBMEM_ID_CBFS_RW_MCACHE); |
| 731 |
mcache_to_cbmem(cbfs_get_boot_device(true), CBMEM_ID_CBFS_RO_MCACHE); |
| 732 |
} |
| 733 |
CBMEM_CREATION_HOOK(cbfs_mcache_migrate); |
| 734 |
#endif |