blob: 92a00e44186e0d31d8231253272f4169ed311912 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Peter Stuge483b7bb2009-04-14 07:40:01 +00002
Aaron Durbin899d13d2015-05-15 23:39:23 -05003#include <assert.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -05004#include <boot_device.h>
5#include <cbfs.h>
Julius Werner1e37c9c2019-12-11 17:09:39 -08006#include <cbmem.h>
Julius Werner57d4bc62021-11-15 10:13:37 -08007#include <commonlib/bsd/cbfs_private.h>
Julius Werner98eeb962019-12-11 15:47:42 -08008#include <commonlib/bsd/compression.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08009#include <console/console.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080010#include <fmap.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050011#include <lib.h>
Raul E Rangel4cfb8622021-11-01 13:40:14 -060012#include <list.h>
Julius Wernerfdabf3f2020-05-06 17:06:35 -070013#include <metadata_hash.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080014#include <security/tpm/tspi/crtm.h>
Jakub Czapiga967a76b2022-08-19 12:25:27 +020015#include <security/vboot/vboot_common.h>
Julius Wernerd96ca242022-08-08 18:08:35 -070016#include <security/vboot/misc.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080017#include <stdlib.h>
18#include <string.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050019#include <symbols.h>
Raul E Rangel4cfb8622021-11-01 13:40:14 -060020#include <thread.h>
Julius Werner09f29212015-09-29 13:51:35 -070021#include <timestamp.h>
Patrick Georgi58a150a2016-05-02 17:22:29 +080022
Jeremy Compostella226f51c2023-10-12 09:40:12 -070023#if ENV_X86 && (ENV_POSTCAR || ENV_SMM)
24struct mem_pool cbfs_cache = MEM_POOL_INIT(NULL, 0, 0);
25#elif CONFIG(POSTRAM_CBFS_CACHE_IN_BSS) && ENV_RAMSTAGE
26static u8 cache_buffer[CONFIG_RAMSTAGE_CBFS_CACHE_SIZE];
27struct mem_pool cbfs_cache =
28 MEM_POOL_INIT(cache_buffer, sizeof(cache_buffer), CONFIG_CBFS_CACHE_ALIGN);
29#else
Raul E Rangel5ac82dc2021-07-23 16:43:18 -060030struct mem_pool cbfs_cache =
Raul E Rangel6938f352021-07-23 16:43:18 -060031 MEM_POOL_INIT(_cbfs_cache, REGION_SIZE(cbfs_cache), CONFIG_CBFS_CACHE_ALIGN);
Jeremy Compostella226f51c2023-10-12 09:40:12 -070032#endif
Julius Werner9b1f3cc2020-12-30 17:30:12 -080033
34static void switch_to_postram_cache(int unused)
35{
36 if (_preram_cbfs_cache != _postram_cbfs_cache)
Raul E Rangel5ac82dc2021-07-23 16:43:18 -060037 mem_pool_init(&cbfs_cache, _postram_cbfs_cache, REGION_SIZE(postram_cbfs_cache),
Raul E Rangel6938f352021-07-23 16:43:18 -060038 CONFIG_CBFS_CACHE_ALIGN);
Julius Werner9b1f3cc2020-12-30 17:30:12 -080039}
Kyösti Mälkkifa3bc042022-03-31 07:40:10 +030040CBMEM_CREATION_HOOK(switch_to_postram_cache);
Julius Werner9b1f3cc2020-12-30 17:30:12 -080041
Julius Werner69cc5572022-03-04 17:49:56 -080042enum cb_err _cbfs_boot_lookup(const char *name, bool force_ro,
43 union cbfs_mdata *mdata, struct region_device *rdev)
Julius Werner1e37c9c2019-12-11 17:09:39 -080044{
Julius Werner0d9072b2020-03-05 12:51:08 -080045 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;
Julius Werner69cc5572022-03-04 17:49:56 -080050 enum cb_err err = CB_CBFS_CACHE_FULL;
Julius Werner34cf0732020-12-08 14:21:43 -080051 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size)
Julius Werner1e37c9c2019-12-11 17:09:39 -080052 err = cbfs_mcache_lookup(cbd->mcache, cbd->mcache_size,
Julius Werner723e3b12020-12-29 17:33:30 -080053 name, mdata, &data_offset);
Julius Wernerfdabf3f2020-05-06 17:06:35 -070054 if (err == CB_CBFS_CACHE_FULL) {
55 struct vb2_hash *metadata_hash = NULL;
56 if (CONFIG(TOCTOU_SAFETY)) {
57 if (ENV_SMM) /* Cannot provide TOCTOU safety for SMM */
58 dead_code();
Julius Werner34cf0732020-12-08 14:21:43 -080059 if (!cbd->mcache_size)
60 die("Cannot access CBFS TOCTOU-safely in " ENV_STRING " before CBMEM init!\n");
Julius Werner723e3b12020-12-29 17:33:30 -080061 /* We can only reach this for the RW CBFS -- an mcache overflow in the
62 RO CBFS would have been caught when building the mcache in cbfs_get
63 boot_device(). (Note that TOCTOU_SAFETY implies !NO_CBFS_MCACHE.) */
Julius Wernerfdabf3f2020-05-06 17:06:35 -070064 assert(cbd == vboot_get_cbfs_boot_device());
Jakub Czapiga967a76b2022-08-19 12:25:27 +020065 if (!CONFIG(VBOOT)
66 || vb2api_get_metadata_hash(vboot_get_context(), &metadata_hash)
67 != VB2_SUCCESS)
68 die("Failed to get RW metadata hash");
Julius Wernerfdabf3f2020-05-06 17:06:35 -070069 }
Julius Werner723e3b12020-12-29 17:33:30 -080070 err = cbfs_lookup(&cbd->rdev, name, mdata, &data_offset, metadata_hash);
Julius Wernerfdabf3f2020-05-06 17:06:35 -070071 }
Julius Werner0d9072b2020-03-05 12:51:08 -080072
Julius Werner723e3b12020-12-29 17:33:30 -080073 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);
Julius Werner57d4bc62021-11-15 10:13:37 -080075 return _cbfs_boot_lookup(name, true, mdata, rdev);
Julius Werner0d9072b2020-03-05 12:51:08 -080076 }
Julius Werner0247fcf2020-12-03 12:41:00 -080077 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
Julius Werner723e3b12020-12-29 17:33:30 -080083 printk(BIOS_ERR, "CBFS ERROR: error %d when looking up '%s'\n",
Julius Werner0247fcf2020-12-03 12:41:00 -080084 err, name);
Julius Werner0d9072b2020-03-05 12:51:08 -080085 return err;
Julius Werner0247fcf2020-12-03 12:41:00 -080086 }
Julius Werner0d9072b2020-03-05 12:51:08 -080087
88 if (rdev_chain(rdev, &cbd->rdev, data_offset, be32toh(mdata->h.len)))
89 return CB_ERR;
90
Julius Werner0d9072b2020-03-05 12:51:08 -080091 return CB_SUCCESS;
Julius Werner1e37c9c2019-12-11 17:09:39 -080092}
93
Julius Werner9b1f3cc2020-12-30 17:30:12 -080094void cbfs_unmap(void *mapping)
Julius Werner834b3ec2020-03-04 16:52:08 -080095{
Julius Werner9b1f3cc2020-12-30 17:30:12 -080096 /*
97 * This is save to call with mappings that weren't allocated in the cache (e.g. x86
98 * direct mappings) -- mem_pool_free() just does nothing for addresses it doesn't
99 * recognize. This hardcodes the assumption that if platforms implement an rdev_mmap()
100 * that requires a free() for the boot_device, they need to implement it via the
101 * cbfs_cache mem_pool.
102 */
Raul E Rangel3d7b9842021-10-08 13:10:38 -0600103 mem_pool_free(&cbfs_cache, mapping);
Julius Werner834b3ec2020-03-04 16:52:08 -0800104}
105
Aaron Durbina85febc2020-05-15 15:09:10 -0600106static inline bool fsps_env(void)
107{
108 /* FSP-S is assumed to be loaded in ramstage. */
109 if (ENV_RAMSTAGE)
110 return true;
111 return false;
112}
113
Aaron Durbinecbfa992020-05-15 17:01:58 -0600114static inline bool fspm_env(void)
115{
116 /* FSP-M is assumed to be loaded in romstage. */
Kyösti Mälkki11cac782022-04-07 07:16:48 +0300117 if (ENV_RAMINIT)
Aaron Durbinecbfa992020-05-15 17:01:58 -0600118 return true;
119 return false;
120}
121
Aaron Durbina121f952020-05-26 15:48:10 -0600122static inline bool cbfs_lz4_enabled(void)
123{
Aaron Durbina85febc2020-05-15 15:09:10 -0600124 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZ4))
125 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600126 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZ4))
127 return true;
Aaron Durbina85febc2020-05-15 15:09:10 -0600128
Aaron Durbina121f952020-05-26 15:48:10 -0600129 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES))
130 return false;
131
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800132 if (ENV_SMM)
133 return false;
134
Aaron Durbina121f952020-05-26 15:48:10 -0600135 return true;
136}
137
138static inline bool cbfs_lzma_enabled(void)
139{
Aaron Durbina85febc2020-05-15 15:09:10 -0600140 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZMA))
141 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600142 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA))
143 return true;
Arthur Heymansa2bc2542021-05-29 08:10:49 +0200144
145 /* Payload loader (ramstage) always needs LZMA. */
146 if (ENV_PAYLOAD_LOADER)
147 return true;
148 /* Only other use of LZMA is ramstage compression. */
149 if (!CONFIG(COMPRESS_RAMSTAGE_LZMA))
Aaron Durbina121f952020-05-26 15:48:10 -0600150 return false;
Arthur Heymansa2bc2542021-05-29 08:10:49 +0200151 /* If there is a postcar, it loads the ramstage. */
152 if (CONFIG(POSTCAR_STAGE))
153 return ENV_POSTCAR;
154 /* If there is no postcar but a separate romstage, it loads the ramstage. */
155 if (CONFIG(SEPARATE_ROMSTAGE))
156 return ENV_SEPARATE_ROMSTAGE;
157 /* Otherwise, the combined bootblock+romstage loads the ramstage. */
158 return ENV_BOOTBLOCK;
Aaron Durbina121f952020-05-26 15:48:10 -0600159}
160
Julius Werner05714cc2021-04-15 23:25:44 -0700161static bool cbfs_file_hash_mismatch(const void *buffer, size_t size,
162 const union cbfs_mdata *mdata, bool skip_verification)
Julius Wernerfccf1222021-04-02 15:58:05 -0700163{
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700164 /* Avoid linking hash functions when verification and measurement are disabled. */
165 if (!CONFIG(CBFS_VERIFICATION) && !CONFIG(TPM_MEASURED_BOOT))
Julius Wernerfccf1222021-04-02 15:58:05 -0700166 return false;
167
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700168 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 }
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200176
177 vb2_error_t rv = vb2_hash_verify(vboot_hwcrypto_allowed(), buffer, size, hash);
178 if (rv != VB2_SUCCESS) {
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700179 ERROR("'%s' file hash mismatch!\n", mdata->h.filename);
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200180 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);
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700184 return true;
185 }
Julius Werner05714cc2021-04-15 23:25:44 -0700186 }
Julius Wernerfccf1222021-04-02 15:58:05 -0700187
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700188 if (CONFIG(TPM_MEASURED_BOOT) && !ENV_SMM) {
189 struct vb2_hash calculated_hash;
190
191 /* No need to re-hash file if we already have it from verification. */
192 if (!hash || hash->algo != TPM_MEASURE_ALGO) {
Julius Wernerd96ca242022-08-08 18:08:35 -0700193 if (vb2_hash_calculate(vboot_hwcrypto_allowed(), buffer, size,
194 TPM_MEASURE_ALGO, &calculated_hash))
195 hash = NULL;
196 else
197 hash = &calculated_hash;
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700198 }
199
Julius Wernerd96ca242022-08-08 18:08:35 -0700200 if (!hash ||
201 tspi_cbfs_measurement(mdata->h.filename, be32toh(mdata->h.type), hash))
Sergii Dmytruk2710df72022-11-10 00:40:51 +0200202 ERROR("failed to measure '%s' into TPM log\n", mdata->h.filename);
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700203 /* We intentionally continue to boot on measurement errors. */
Julius Werner05714cc2021-04-15 23:25:44 -0700204 }
205
206 return false;
Julius Wernerfccf1222021-04-02 15:58:05 -0700207}
208
209static size_t cbfs_load_and_decompress(const struct region_device *rdev, void *buffer,
210 size_t buffer_size, uint32_t compression,
Julius Werner05714cc2021-04-15 23:25:44 -0700211 const union cbfs_mdata *mdata, bool skip_verification)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500212{
Julius Wernereca99af2021-03-10 18:49:37 -0800213 size_t in_size = region_device_sz(rdev);
Julius Wernerfccf1222021-04-02 15:58:05 -0700214 size_t out_size = 0;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600215 void *map;
Julius Werner09f29212015-09-29 13:51:35 -0700216
Julius Werner05714cc2021-04-15 23:25:44 -0700217 DEBUG("Decompressing %zu bytes from '%s' to %p with algo %d\n",
218 in_size, mdata->h.filename, buffer, compression);
Julius Werner7778cf22021-01-05 18:54:19 -0800219
Julius Werner6e303aa2023-05-25 18:26:32 -0700220 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
Julius Werner09f29212015-09-29 13:51:35 -0700227 switch (compression) {
228 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700229 if (buffer_size < in_size)
230 return 0;
Julius Wernereca99af2021-03-10 18:49:37 -0800231 if (rdev_readat(rdev, buffer, 0, in_size) != in_size)
Julius Werner09f29212015-09-29 13:51:35 -0700232 return 0;
Julius Werner05714cc2021-04-15 23:25:44 -0700233 if (cbfs_file_hash_mismatch(buffer, in_size, mdata, skip_verification))
Julius Wernerfccf1222021-04-02 15:58:05 -0700234 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700235 return in_size;
236
237 case CBFS_COMPRESS_LZ4:
Aaron Durbina121f952020-05-26 15:48:10 -0600238 if (!cbfs_lz4_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700239 return 0;
240
Julius Wernereca99af2021-03-10 18:49:37 -0800241 /* cbfs_prog_stage_load() takes care of in-place LZ4 decompression by
Julius Werner723e3b12020-12-29 17:33:30 -0800242 setting up the rdev to be in memory. */
Julius Wernereca99af2021-03-10 18:49:37 -0800243 map = rdev_mmap_full(rdev);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600244 if (map == NULL)
Julius Werner09f29212015-09-29 13:51:35 -0700245 return 0;
246
Julius Werner05714cc2021-04-15 23:25:44 -0700247 if (!cbfs_file_hash_mismatch(map, in_size, mdata, skip_verification)) {
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100248 timestamp_add_now(TS_ULZ4F_START);
Julius Wernerfccf1222021-04-02 15:58:05 -0700249 out_size = ulz4fn(map, in_size, buffer, buffer_size);
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100250 timestamp_add_now(TS_ULZ4F_END);
Julius Wernerfccf1222021-04-02 15:58:05 -0700251 }
Aaron Durbin84f394e2020-05-26 16:16:42 -0600252
253 rdev_munmap(rdev, map);
254
Julius Werner09f29212015-09-29 13:51:35 -0700255 return out_size;
256
257 case CBFS_COMPRESS_LZMA:
Aaron Durbina121f952020-05-26 15:48:10 -0600258 if (!cbfs_lzma_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700259 return 0;
Julius Wernereca99af2021-03-10 18:49:37 -0800260 map = rdev_mmap_full(rdev);
Julius Werner09f29212015-09-29 13:51:35 -0700261 if (map == NULL)
262 return 0;
263
Julius Werner05714cc2021-04-15 23:25:44 -0700264 if (!cbfs_file_hash_mismatch(map, in_size, mdata, skip_verification)) {
Julius Wernerfccf1222021-04-02 15:58:05 -0700265 /* Note: timestamp not useful for memory-mapped media (x86) */
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100266 timestamp_add_now(TS_ULZMA_START);
Julius Wernerfccf1222021-04-02 15:58:05 -0700267 out_size = ulzman(map, in_size, buffer, buffer_size);
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100268 timestamp_add_now(TS_ULZMA_END);
Julius Wernerfccf1222021-04-02 15:58:05 -0700269 }
Julius Werner09f29212015-09-29 13:51:35 -0700270
271 rdev_munmap(rdev, map);
272
273 return out_size;
274
275 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500276 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700277 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500278}
279
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600280struct 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
288static struct list_node cbfs_preload_context_list;
289
290static 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
305static void append_cbfs_preload_context(struct cbfs_preload_context *context)
306{
307 list_append(&context->list_node, &cbfs_preload_context_list);
308}
309
310static 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
317static enum cb_err cbfs_preload_thread_entry(void *arg)
318{
319 struct cbfs_preload_context *context = arg;
320
Julius Wernerf364eb92021-12-01 14:48:59 -0800321 if (rdev_read_full(&context->rdev, context->buffer) < 0) {
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600322 ERROR("%s(name='%s') readat failed\n", __func__, context->name);
323 return CB_ERR;
324 }
325
326 return CB_SUCCESS;
327}
328
329void 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
Raul E Rangel74d22182021-12-03 15:09:35 -0700340 /* We don't want to cross the vboot boundary */
Arthur Heymansa2bc2542021-05-29 08:10:49 +0200341 if (ENV_SEPARATE_ROMSTAGE && CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
Raul E Rangel74d22182021-12-03 15:09:35 -0700342 return;
343
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600344 DEBUG("%s(name='%s')\n", __func__, name);
345
Julius Werner57d4bc62021-11-15 10:13:37 -0800346 if (_cbfs_boot_lookup(name, force_ro, &mdata, &rdev))
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600347 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
375out:
376 free_cbfs_preload_context(context);
377}
378
379static struct cbfs_preload_context *find_cbfs_preload_context(const char *name)
380{
381 struct cbfs_preload_context *context;
382
383 list_for_each(context, cbfs_preload_context_list, list_node) {
384 if (strcmp(context->name, name) == 0)
385 return context;
386 }
387
388 return NULL;
389}
390
391static enum cb_err get_preload_rdev(struct region_device *rdev, const char *name)
392{
393 enum cb_err err;
394 struct cbfs_preload_context *context;
395
Arthur Heymans6acc05e2022-05-12 18:01:13 +0200396 if (!CONFIG(CBFS_PRELOAD) || !ENV_SUPPORTS_COOP)
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600397 return CB_ERR_ARG;
398
399 context = find_cbfs_preload_context(name);
400 if (!context)
401 return CB_ERR_ARG;
402
403 err = thread_join(&context->handle);
404 if (err != CB_SUCCESS) {
405 ERROR("%s(name='%s') Preload thread failed: %u\n", __func__, name, err);
406
407 goto out;
408 }
409
410 if (rdev_chain_mem(rdev, context->buffer, region_device_sz(&context->rdev)) != 0) {
411 ERROR("%s(name='%s') chaining failed\n", __func__, name);
412
413 err = CB_ERR;
414 goto out;
415 }
416
417 err = CB_SUCCESS;
418
419 DEBUG("%s(name='%s') preload successful\n", __func__, name);
420
421out:
422 free_cbfs_preload_context(context);
423
424 return err;
425}
426
Julius Werner05714cc2021-04-15 23:25:44 -0700427static void *do_alloc(union cbfs_mdata *mdata, struct region_device *rdev,
428 cbfs_allocator_t allocator, void *arg, size_t *size_out,
429 bool skip_verification)
430{
431 size_t size = region_device_sz(rdev);
432 void *loc = NULL;
433
434 uint32_t compression = CBFS_COMPRESS_NONE;
435 const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(mdata,
436 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr));
437 if (cattr) {
438 compression = be32toh(cattr->compression);
439 size = be32toh(cattr->decompressed_size);
440 }
441
442 if (size_out)
443 *size_out = size;
444
445 /* allocator == NULL means do a cbfs_map() */
446 if (allocator) {
447 loc = allocator(arg, size, mdata);
448 } else if (compression == CBFS_COMPRESS_NONE) {
449 void *mapping = rdev_mmap_full(rdev);
450 if (!mapping)
451 return NULL;
452 if (cbfs_file_hash_mismatch(mapping, size, mdata, skip_verification)) {
453 rdev_munmap(rdev, mapping);
454 return NULL;
455 }
456 return mapping;
457 } else if (!cbfs_cache.size) {
458 /* In order to use the cbfs_cache you need to add a CBFS_CACHE to your
Jeremy Compostella1c3b6b32023-09-15 14:19:29 -0700459 * memlayout. */
Julius Werner05714cc2021-04-15 23:25:44 -0700460 ERROR("Cannot map compressed file %s without cbfs_cache\n", mdata->h.filename);
461 return NULL;
462 } else {
463 loc = mem_pool_alloc(&cbfs_cache, size);
464 }
465
466 if (!loc) {
467 ERROR("'%s' allocation failure\n", mdata->h.filename);
468 return NULL;
469 }
470
471 size = cbfs_load_and_decompress(rdev, loc, size, compression, mdata, skip_verification);
472 if (!size)
473 return NULL;
474
475 return loc;
476}
477
Julius Werner7778cf22021-01-05 18:54:19 -0800478void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg,
479 size_t *size_out, bool force_ro, enum cbfs_type *type)
Julius Wernerf975e552016-08-19 15:43:06 -0700480{
Julius Wernerd17ce412020-10-01 18:25:49 -0700481 struct region_device rdev;
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600482 bool preload_successful = false;
Julius Wernerd17ce412020-10-01 18:25:49 -0700483 union cbfs_mdata mdata;
Julius Werner7778cf22021-01-05 18:54:19 -0800484
485 DEBUG("%s(name='%s', alloc=%p(%p), force_ro=%s, type=%d)\n", __func__, name, allocator,
486 arg, force_ro ? "true" : "false", type ? *type : -1);
Julius Wernerf975e552016-08-19 15:43:06 -0700487
Julius Werner57d4bc62021-11-15 10:13:37 -0800488 if (_cbfs_boot_lookup(name, force_ro, &mdata, &rdev))
Julius Werner7778cf22021-01-05 18:54:19 -0800489 return NULL;
Julius Wernerf975e552016-08-19 15:43:06 -0700490
Julius Werner7778cf22021-01-05 18:54:19 -0800491 if (type) {
492 const enum cbfs_type real_type = be32toh(mdata.h.type);
493 if (*type == CBFS_TYPE_QUERY)
494 *type = real_type;
495 else if (*type != real_type) {
496 ERROR("'%s' type mismatch (is %u, expected %u)\n",
497 mdata.h.filename, real_type, *type);
498 return NULL;
499 }
Julius Wernerd17ce412020-10-01 18:25:49 -0700500 }
Julius Wernerf975e552016-08-19 15:43:06 -0700501
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600502 /* Update the rdev with the preload content */
503 if (!force_ro && get_preload_rdev(&rdev, name) == CB_SUCCESS)
504 preload_successful = true;
505
Julius Werner05714cc2021-04-15 23:25:44 -0700506 void *ret = do_alloc(&mdata, &rdev, allocator, arg, size_out, false);
Raul E Rangel3f41d322021-07-23 15:23:12 -0600507
Julius Werner05714cc2021-04-15 23:25:44 -0700508 /* When using cbfs_preload we need to free the preload buffer after populating the
509 * destination buffer. We know we must have a mem_rdev here, so extra mmap is fine. */
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600510 if (preload_successful)
511 cbfs_unmap(rdev_mmap_full(&rdev));
Julius Werner7778cf22021-01-05 18:54:19 -0800512
Julius Werner05714cc2021-04-15 23:25:44 -0700513 return ret;
514}
515
516void *_cbfs_unverified_area_alloc(const char *area, const char *name,
517 cbfs_allocator_t allocator, void *arg, size_t *size_out)
518{
519 struct region_device area_rdev, file_rdev;
520 union cbfs_mdata mdata;
521 size_t data_offset;
522
523 DEBUG("%s(area='%s', name='%s', alloc=%p(%p))\n", __func__, area, name, allocator, arg);
524
525 if (fmap_locate_area_as_rdev(area, &area_rdev))
526 return NULL;
527
528 if (cbfs_lookup(&area_rdev, name, &mdata, &data_offset, NULL)) {
529 ERROR("'%s' not found in '%s'\n", name, area);
530 return NULL;
531 }
532
533 if (rdev_chain(&file_rdev, &area_rdev, data_offset, be32toh(mdata.h.len)))
534 return NULL;
535
Julius Werner05714cc2021-04-15 23:25:44 -0700536 return do_alloc(&mdata, &file_rdev, allocator, arg, size_out, true);
Julius Werner7778cf22021-01-05 18:54:19 -0800537}
538
Julius Werner4676ec52021-03-10 16:52:14 -0800539void *_cbfs_default_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
Julius Werner7778cf22021-01-05 18:54:19 -0800540{
541 struct _cbfs_default_allocator_arg *darg = arg;
542 if (size > darg->buf_size)
543 return NULL;
544 return darg->buf;
545}
546
Julius Werner4676ec52021-03-10 16:52:14 -0800547void *_cbfs_cbmem_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
Julius Werner7778cf22021-01-05 18:54:19 -0800548{
549 return cbmem_add((uintptr_t)arg, size);
Julius Wernerf975e552016-08-19 15:43:06 -0700550}
551
Julius Werner69cc5572022-03-04 17:49:56 -0800552enum cb_err cbfs_prog_stage_load(struct prog *pstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500553{
Julius Werner1de87082020-12-23 17:38:11 -0800554 union cbfs_mdata mdata;
555 struct region_device rdev;
Julius Werner69cc5572022-03-04 17:49:56 -0800556 enum cb_err err;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800557
Julius Werner1de87082020-12-23 17:38:11 -0800558 prog_locate_hook(pstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500559
Julius Werner57d4bc62021-11-15 10:13:37 -0800560 if ((err = _cbfs_boot_lookup(prog_name(pstage), false, &mdata, &rdev)))
Julius Werner1de87082020-12-23 17:38:11 -0800561 return err;
562
563 assert(be32toh(mdata.h.type) == CBFS_TYPE_STAGE);
564 pstage->cbfs_type = CBFS_TYPE_STAGE;
565
Julius Werner81dc20e2020-10-15 17:37:57 -0700566 enum cbfs_compression compression = CBFS_COMPRESS_NONE;
567 const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(&mdata,
568 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr));
569 if (cattr)
570 compression = be32toh(cattr->compression);
Julius Werner1de87082020-12-23 17:38:11 -0800571
Julius Werner81dc20e2020-10-15 17:37:57 -0700572 const struct cbfs_file_attr_stageheader *sattr = cbfs_find_attr(&mdata,
573 CBFS_FILE_ATTR_TAG_STAGEHEADER, sizeof(*sattr));
574 if (!sattr)
575 return CB_ERR;
576 prog_set_area(pstage, (void *)(uintptr_t)be64toh(sattr->loadaddr),
577 be32toh(sattr->memlen));
578 prog_set_entry(pstage, prog_start(pstage) +
579 be32toh(sattr->entry_offset), NULL);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500580
Aaron Durbined253c82015-10-07 17:22:42 -0500581 /* Hacky way to not load programs over read only media. The stages
582 * that would hit this path initialize themselves. */
Julius Werner21a40532020-04-21 16:03:53 -0700583 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) &&
584 !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
Julius Werner81dc20e2020-10-15 17:37:57 -0700585 void *mapping = rdev_mmap_full(&rdev);
Julius Werner1de87082020-12-23 17:38:11 -0800586 rdev_munmap(&rdev, mapping);
Julius Werner05714cc2021-04-15 23:25:44 -0700587 if (cbfs_file_hash_mismatch(mapping, region_device_sz(&rdev), &mdata, false))
Julius Wernerfccf1222021-04-02 15:58:05 -0700588 return CB_CBFS_HASH_MISMATCH;
Julius Werner81dc20e2020-10-15 17:37:57 -0700589 if (mapping == prog_start(pstage))
590 return CB_SUCCESS;
Aaron Durbined253c82015-10-07 17:22:42 -0500591 }
592
Julius Wernereca99af2021-03-10 18:49:37 -0800593 /* LZ4 stages can be decompressed in-place to save mapping scratch space. Load the
594 compressed data to the end of the buffer and point &rdev to that memory location. */
595 if (cbfs_lz4_enabled() && compression == CBFS_COMPRESS_LZ4) {
596 size_t in_size = region_device_sz(&rdev);
597 void *compr_start = prog_start(pstage) + prog_size(pstage) - in_size;
598 if (rdev_readat(&rdev, compr_start, 0, in_size) != in_size)
599 return CB_ERR;
Julius Wernerc8931972021-04-16 16:48:32 -0700600 rdev_chain_mem(&rdev, compr_start, in_size);
Julius Wernereca99af2021-03-10 18:49:37 -0800601 }
602
603 size_t fsize = cbfs_load_and_decompress(&rdev, prog_start(pstage), prog_size(pstage),
Julius Werner05714cc2021-04-15 23:25:44 -0700604 compression, &mdata, false);
Julius Werner09f29212015-09-29 13:51:35 -0700605 if (!fsize)
Julius Werner1de87082020-12-23 17:38:11 -0800606 return CB_ERR;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500607
608 /* Clear area not covered by file. */
Julius Werner81dc20e2020-10-15 17:37:57 -0700609 memset(prog_start(pstage) + fsize, 0, prog_size(pstage) - fsize);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500610
Julius Werner81dc20e2020-10-15 17:37:57 -0700611 prog_segment_loaded((uintptr_t)prog_start(pstage), prog_size(pstage),
612 SEG_FINAL);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500613
Julius Werner1de87082020-12-23 17:38:11 -0800614 return CB_SUCCESS;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800615}
Aaron Durbin6d720f32015-12-08 17:00:23 -0600616
Julius Werner1e37c9c2019-12-11 17:09:39 -0800617void cbfs_boot_device_find_mcache(struct cbfs_boot_device *cbd, uint32_t id)
Aaron Durbin6d720f32015-12-08 17:00:23 -0600618{
Julius Werner1e37c9c2019-12-11 17:09:39 -0800619 if (CONFIG(NO_CBFS_MCACHE) || ENV_SMM)
620 return;
621
Julius Werner34cf0732020-12-08 14:21:43 -0800622 if (cbd->mcache_size)
623 return;
624
Julius Werner1e37c9c2019-12-11 17:09:39 -0800625 const struct cbmem_entry *entry;
Arthur Heymansb7cbb7c2023-08-11 11:31:05 +0200626 if (ENV_HAS_CBMEM &&
Julius Werner1e37c9c2019-12-11 17:09:39 -0800627 (entry = cbmem_entry_find(id))) {
628 cbd->mcache = cbmem_entry_start(entry);
629 cbd->mcache_size = cbmem_entry_size(entry);
630 } else if (ENV_ROMSTAGE_OR_BEFORE) {
631 u8 *boundary = _ecbfs_mcache - REGION_SIZE(cbfs_mcache) *
632 CONFIG_CBFS_MCACHE_RW_PERCENTAGE / 100;
Julius Werner723e3b12020-12-29 17:33:30 -0800633 boundary = (u8 *)ALIGN_DOWN((uintptr_t)boundary, CBFS_MCACHE_ALIGNMENT);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800634 if (id == CBMEM_ID_CBFS_RO_MCACHE) {
635 cbd->mcache = _cbfs_mcache;
636 cbd->mcache_size = boundary - _cbfs_mcache;
637 } else if (id == CBMEM_ID_CBFS_RW_MCACHE) {
638 cbd->mcache = boundary;
639 cbd->mcache_size = _ecbfs_mcache - boundary;
640 }
641 }
Aaron Durbin6d720f32015-12-08 17:00:23 -0600642}
Julius Werner1e37c9c2019-12-11 17:09:39 -0800643
Julius Werner69cc5572022-03-04 17:49:56 -0800644enum cb_err cbfs_init_boot_device(const struct cbfs_boot_device *cbd,
645 struct vb2_hash *mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700646{
647 /* If we have an mcache, mcache_build() will also check mdata hash. */
648 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size > 0)
Julius Werner723e3b12020-12-29 17:33:30 -0800649 return cbfs_mcache_build(&cbd->rdev, cbd->mcache, cbd->mcache_size, mdata_hash);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700650
651 /* No mcache and no verification means we have nothing special to do. */
Julius Werner723e3b12020-12-29 17:33:30 -0800652 if (!CONFIG(CBFS_VERIFICATION) || !mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700653 return CB_SUCCESS;
654
Julius Werner723e3b12020-12-29 17:33:30 -0800655 /* Verification only: use cbfs_walk() without a walker() function to just run through
656 the CBFS once, will return NOT_FOUND by default. */
Julius Werner69cc5572022-03-04 17:49:56 -0800657 enum cb_err err = cbfs_walk(&cbd->rdev, NULL, NULL, mdata_hash, 0);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700658 if (err == CB_CBFS_NOT_FOUND)
659 err = CB_SUCCESS;
660 return err;
661}
662
Julius Werner1e37c9c2019-12-11 17:09:39 -0800663const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro)
664{
665 static struct cbfs_boot_device ro;
666
Julius Werner723e3b12020-12-29 17:33:30 -0800667 /* Ensure we always init RO mcache, even if the first file is from the RW CBFS.
Julius Werner1e37c9c2019-12-11 17:09:39 -0800668 Otherwise it may not be available when needed in later stages. */
669 if (ENV_INITIAL_STAGE && !force_ro && !region_device_sz(&ro.rdev))
670 cbfs_get_boot_device(true);
671
672 if (!force_ro) {
673 const struct cbfs_boot_device *rw = vboot_get_cbfs_boot_device();
Julius Werner723e3b12020-12-29 17:33:30 -0800674 /* This will return NULL if vboot isn't enabled, didn't run yet or decided to
675 boot into recovery mode. */
Julius Werner1e37c9c2019-12-11 17:09:39 -0800676 if (rw)
677 return rw;
678 }
679
Julius Werner723e3b12020-12-29 17:33:30 -0800680 /* In rare cases post-RAM stages may run this before cbmem_initialize(), so we can't
681 lock in the result of find_mcache() on the first try and should keep trying every
682 time until an mcache is found. */
Julius Werner34cf0732020-12-08 14:21:43 -0800683 cbfs_boot_device_find_mcache(&ro, CBMEM_ID_CBFS_RO_MCACHE);
684
Julius Werner1e37c9c2019-12-11 17:09:39 -0800685 if (region_device_sz(&ro.rdev))
686 return &ro;
687
688 if (fmap_locate_area_as_rdev("COREBOOT", &ro.rdev))
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700689 die("Cannot locate primary CBFS");
Julius Werner1e37c9c2019-12-11 17:09:39 -0800690
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700691 if (ENV_INITIAL_STAGE) {
Julius Werner69cc5572022-03-04 17:49:56 -0800692 enum cb_err err = cbfs_init_boot_device(&ro, metadata_hash_get());
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700693 if (err == CB_CBFS_HASH_MISMATCH)
694 die("RO CBFS metadata hash verification failure");
695 else if (CONFIG(TOCTOU_SAFETY) && err == CB_CBFS_CACHE_FULL)
696 die("RO mcache overflow breaks TOCTOU safety!\n");
697 else if (err && err != CB_CBFS_CACHE_FULL)
698 die("RO CBFS initialization error: %d", err);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800699 }
700
701 return &ro;
702}
703
704#if !CONFIG(NO_CBFS_MCACHE)
705static void mcache_to_cbmem(const struct cbfs_boot_device *cbd, u32 cbmem_id)
706{
707 if (!cbd)
708 return;
709
710 size_t real_size = cbfs_mcache_real_size(cbd->mcache, cbd->mcache_size);
711 void *cbmem_mcache = cbmem_add(cbmem_id, real_size);
712 if (!cbmem_mcache) {
Julius Wernere9665952022-01-21 17:06:20 -0800713 printk(BIOS_ERR, "Cannot allocate CBMEM mcache %#x (%#zx bytes)!\n",
Julius Werner1e37c9c2019-12-11 17:09:39 -0800714 cbmem_id, real_size);
715 return;
716 }
717 memcpy(cbmem_mcache, cbd->mcache, real_size);
718}
719
720static void cbfs_mcache_migrate(int unused)
721{
722 mcache_to_cbmem(vboot_get_cbfs_boot_device(), CBMEM_ID_CBFS_RW_MCACHE);
723 mcache_to_cbmem(cbfs_get_boot_device(true), CBMEM_ID_CBFS_RO_MCACHE);
724}
Kyösti Mälkkifa3bc042022-03-31 07:40:10 +0300725CBMEM_CREATION_HOOK(cbfs_mcache_migrate);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800726#endif