blob: b4b6eb1324df94cc3f084705c09326274a41e405 [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>
Aaron Durbin0b418bd2020-10-08 14:56:03 -06009#include <commonlib/endian.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080010#include <console/console.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080011#include <fmap.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050012#include <lib.h>
Raul E Rangel4cfb8622021-11-01 13:40:14 -060013#include <list.h>
Julius Wernerfdabf3f2020-05-06 17:06:35 -070014#include <metadata_hash.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080015#include <security/tpm/tspi/crtm.h>
16#include <security/vboot/vboot_common.h>
17#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
Raul E Rangel3d7b9842021-10-08 13:10:38 -060023#if ENV_STAGE_HAS_DATA_SECTION
Raul E Rangel5ac82dc2021-07-23 16:43:18 -060024struct mem_pool cbfs_cache =
Raul E Rangel6938f352021-07-23 16:43:18 -060025 MEM_POOL_INIT(_cbfs_cache, REGION_SIZE(cbfs_cache), CONFIG_CBFS_CACHE_ALIGN);
Raul E Rangel3d7b9842021-10-08 13:10:38 -060026#else
Raul E Rangel5ac82dc2021-07-23 16:43:18 -060027struct mem_pool cbfs_cache = MEM_POOL_INIT(NULL, 0, 0);
Raul E Rangel3d7b9842021-10-08 13:10:38 -060028#endif
Julius Werner9b1f3cc2020-12-30 17:30:12 -080029
30static void switch_to_postram_cache(int unused)
31{
32 if (_preram_cbfs_cache != _postram_cbfs_cache)
Raul E Rangel5ac82dc2021-07-23 16:43:18 -060033 mem_pool_init(&cbfs_cache, _postram_cbfs_cache, REGION_SIZE(postram_cbfs_cache),
Raul E Rangel6938f352021-07-23 16:43:18 -060034 CONFIG_CBFS_CACHE_ALIGN);
Julius Werner9b1f3cc2020-12-30 17:30:12 -080035}
36ROMSTAGE_CBMEM_INIT_HOOK(switch_to_postram_cache);
Julius Werner9b1f3cc2020-12-30 17:30:12 -080037
Julius Werner57d4bc62021-11-15 10:13:37 -080038cb_err_t _cbfs_boot_lookup(const char *name, bool force_ro,
39 union cbfs_mdata *mdata, struct region_device *rdev)
Julius Werner1e37c9c2019-12-11 17:09:39 -080040{
Julius Werner0d9072b2020-03-05 12:51:08 -080041 const struct cbfs_boot_device *cbd = cbfs_get_boot_device(force_ro);
42 if (!cbd)
43 return CB_ERR;
44
45 size_t data_offset;
Julius Werner1e37c9c2019-12-11 17:09:39 -080046 cb_err_t err = CB_CBFS_CACHE_FULL;
Julius Werner34cf0732020-12-08 14:21:43 -080047 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size)
Julius Werner1e37c9c2019-12-11 17:09:39 -080048 err = cbfs_mcache_lookup(cbd->mcache, cbd->mcache_size,
Julius Werner723e3b12020-12-29 17:33:30 -080049 name, mdata, &data_offset);
Julius Wernerfdabf3f2020-05-06 17:06:35 -070050 if (err == CB_CBFS_CACHE_FULL) {
51 struct vb2_hash *metadata_hash = NULL;
52 if (CONFIG(TOCTOU_SAFETY)) {
53 if (ENV_SMM) /* Cannot provide TOCTOU safety for SMM */
54 dead_code();
Julius Werner34cf0732020-12-08 14:21:43 -080055 if (!cbd->mcache_size)
56 die("Cannot access CBFS TOCTOU-safely in " ENV_STRING " before CBMEM init!\n");
Julius Werner723e3b12020-12-29 17:33:30 -080057 /* We can only reach this for the RW CBFS -- an mcache overflow in the
58 RO CBFS would have been caught when building the mcache in cbfs_get
59 boot_device(). (Note that TOCTOU_SAFETY implies !NO_CBFS_MCACHE.) */
Julius Wernerfdabf3f2020-05-06 17:06:35 -070060 assert(cbd == vboot_get_cbfs_boot_device());
61 /* TODO: set metadata_hash to RW metadata hash here. */
62 }
Julius Werner723e3b12020-12-29 17:33:30 -080063 err = cbfs_lookup(&cbd->rdev, name, mdata, &data_offset, metadata_hash);
Julius Wernerfdabf3f2020-05-06 17:06:35 -070064 }
Julius Werner0d9072b2020-03-05 12:51:08 -080065
Julius Werner723e3b12020-12-29 17:33:30 -080066 if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && !force_ro && err == CB_CBFS_NOT_FOUND) {
67 printk(BIOS_INFO, "CBFS: Fall back to RO region for %s\n", name);
Julius Werner57d4bc62021-11-15 10:13:37 -080068 return _cbfs_boot_lookup(name, true, mdata, rdev);
Julius Werner0d9072b2020-03-05 12:51:08 -080069 }
Julius Werner0247fcf2020-12-03 12:41:00 -080070 if (err) {
71 if (err == CB_CBFS_NOT_FOUND)
72 printk(BIOS_WARNING, "CBFS: '%s' not found.\n", name);
73 else if (err == CB_CBFS_HASH_MISMATCH)
74 printk(BIOS_ERR, "CBFS ERROR: metadata hash mismatch!\n");
75 else
Julius Werner723e3b12020-12-29 17:33:30 -080076 printk(BIOS_ERR, "CBFS ERROR: error %d when looking up '%s'\n",
Julius Werner0247fcf2020-12-03 12:41:00 -080077 err, name);
Julius Werner0d9072b2020-03-05 12:51:08 -080078 return err;
Julius Werner0247fcf2020-12-03 12:41:00 -080079 }
Julius Werner0d9072b2020-03-05 12:51:08 -080080
81 if (rdev_chain(rdev, &cbd->rdev, data_offset, be32toh(mdata->h.len)))
82 return CB_ERR;
83
Arthur Heymans27545df2021-02-26 14:22:47 +010084 if (tspi_measure_cbfs_hook(rdev, name, be32toh(mdata->h.type))) {
85 printk(BIOS_ERR, "CBFS ERROR: error when measuring '%s'\n", name);
86 }
Julius Werner0d9072b2020-03-05 12:51:08 -080087
88 return CB_SUCCESS;
Julius Werner1e37c9c2019-12-11 17:09:39 -080089}
90
Aaron Durbin37a5d152015-09-17 16:09:30 -050091int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
Aaron Durbin899d13d2015-05-15 23:39:23 -050092{
Julius Werner57d4bc62021-11-15 10:13:37 -080093 if (_cbfs_boot_lookup(name, false, &fh->mdata, &fh->data))
Julius Werner1cd013b2019-12-11 16:50:02 -080094 return -1;
95
96 size_t msize = be32toh(fh->mdata.h.offset);
Julius Wernerc8931972021-04-16 16:48:32 -070097 if (rdev_chain_mem(&fh->metadata, &fh->mdata, msize))
Julius Werner1cd013b2019-12-11 16:50:02 -080098 return -1;
Julius Werner0d9072b2020-03-05 12:51:08 -080099
Julius Werner1cd013b2019-12-11 16:50:02 -0800100 if (type) {
101 if (!*type)
102 *type = be32toh(fh->mdata.h.type);
103 else if (*type != be32toh(fh->mdata.h.type))
104 return -1;
Wim Vervoorn114e2e82019-11-05 14:09:16 +0100105 }
106
Julius Werner1cd013b2019-12-11 16:50:02 -0800107 return 0;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500108}
109
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800110void cbfs_unmap(void *mapping)
Julius Werner834b3ec2020-03-04 16:52:08 -0800111{
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800112 /*
113 * This is save to call with mappings that weren't allocated in the cache (e.g. x86
114 * direct mappings) -- mem_pool_free() just does nothing for addresses it doesn't
115 * recognize. This hardcodes the assumption that if platforms implement an rdev_mmap()
116 * that requires a free() for the boot_device, they need to implement it via the
117 * cbfs_cache mem_pool.
118 */
Raul E Rangel3d7b9842021-10-08 13:10:38 -0600119 mem_pool_free(&cbfs_cache, mapping);
Julius Werner834b3ec2020-03-04 16:52:08 -0800120}
121
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800122int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100123 const char *name, uint32_t *type)
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800124{
125 struct region_device rdev;
Bill XIEbad08c22020-02-13 11:11:35 +0800126 int ret = 0;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800127 if (fmap_locate_area_as_rdev(region_name, &rdev)) {
Julius Werner723e3b12020-12-29 17:33:30 -0800128 LOG("%s region not found while looking for %s\n", region_name, name);
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800129 return -1;
130 }
131
Julius Werner0d9072b2020-03-05 12:51:08 -0800132 uint32_t dummy_type = 0;
133 if (!type)
134 type = &dummy_type;
135
Bill XIEbad08c22020-02-13 11:11:35 +0800136 ret = cbfs_locate(fh, &rdev, name, type);
137 if (!ret)
Julius Werner0d9072b2020-03-05 12:51:08 -0800138 if (tspi_measure_cbfs_hook(&rdev, name, *type))
Arthur Heymans27545df2021-02-26 14:22:47 +0100139 LOG("error measuring %s in region %s\n", name, region_name);
Bill XIEbad08c22020-02-13 11:11:35 +0800140 return ret;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800141}
142
Aaron Durbina85febc2020-05-15 15:09:10 -0600143static inline bool fsps_env(void)
144{
145 /* FSP-S is assumed to be loaded in ramstage. */
146 if (ENV_RAMSTAGE)
147 return true;
148 return false;
149}
150
Aaron Durbinecbfa992020-05-15 17:01:58 -0600151static inline bool fspm_env(void)
152{
153 /* FSP-M is assumed to be loaded in romstage. */
154 if (ENV_ROMSTAGE)
155 return true;
156 return false;
157}
158
Aaron Durbina121f952020-05-26 15:48:10 -0600159static inline bool cbfs_lz4_enabled(void)
160{
Aaron Durbina85febc2020-05-15 15:09:10 -0600161 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZ4))
162 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600163 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZ4))
164 return true;
Aaron Durbina85febc2020-05-15 15:09:10 -0600165
Aaron Durbina121f952020-05-26 15:48:10 -0600166 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES))
167 return false;
168
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800169 if (ENV_SMM)
170 return false;
171
Aaron Durbina121f952020-05-26 15:48:10 -0600172 return true;
173}
174
175static inline bool cbfs_lzma_enabled(void)
176{
Aaron Durbina85febc2020-05-15 15:09:10 -0600177 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZMA))
178 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600179 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA))
180 return true;
Aaron Durbina121f952020-05-26 15:48:10 -0600181 /* We assume here romstage and postcar are never compressed. */
182 if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE)
183 return false;
184 if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE))
185 return false;
Julius Werner723e3b12020-12-29 17:33:30 -0800186 if ((ENV_ROMSTAGE || ENV_POSTCAR) && !CONFIG(COMPRESS_RAMSTAGE))
Aaron Durbina121f952020-05-26 15:48:10 -0600187 return false;
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800188 if (ENV_SMM)
189 return false;
Aaron Durbina121f952020-05-26 15:48:10 -0600190 return true;
191}
192
Julius Werner05714cc2021-04-15 23:25:44 -0700193static bool cbfs_file_hash_mismatch(const void *buffer, size_t size,
194 const union cbfs_mdata *mdata, bool skip_verification)
Julius Wernerfccf1222021-04-02 15:58:05 -0700195{
196 /* Avoid linking hash functions when verification is disabled. */
Julius Werner05714cc2021-04-15 23:25:44 -0700197 if (!CONFIG(CBFS_VERIFICATION) || skip_verification)
Julius Wernerfccf1222021-04-02 15:58:05 -0700198 return false;
199
Julius Werner05714cc2021-04-15 23:25:44 -0700200 const struct vb2_hash *hash = cbfs_file_hash(mdata);
201 if (!hash) {
202 ERROR("'%s' does not have a file hash!\n", mdata->h.filename);
203 return true;
204 }
Julius Wernerfccf1222021-04-02 15:58:05 -0700205
Julius Werner05714cc2021-04-15 23:25:44 -0700206 if (vb2_hash_verify(buffer, size, hash) != VB2_SUCCESS) {
207 ERROR("'%s' file hash mismatch!\n", mdata->h.filename);
208 return true;
209 }
210
211 return false;
Julius Wernerfccf1222021-04-02 15:58:05 -0700212}
213
214static size_t cbfs_load_and_decompress(const struct region_device *rdev, void *buffer,
215 size_t buffer_size, uint32_t compression,
Julius Werner05714cc2021-04-15 23:25:44 -0700216 const union cbfs_mdata *mdata, bool skip_verification)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500217{
Julius Wernereca99af2021-03-10 18:49:37 -0800218 size_t in_size = region_device_sz(rdev);
Julius Wernerfccf1222021-04-02 15:58:05 -0700219 size_t out_size = 0;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600220 void *map;
Julius Werner09f29212015-09-29 13:51:35 -0700221
Julius Werner05714cc2021-04-15 23:25:44 -0700222 DEBUG("Decompressing %zu bytes from '%s' to %p with algo %d\n",
223 in_size, mdata->h.filename, buffer, compression);
Julius Werner7778cf22021-01-05 18:54:19 -0800224
Julius Werner09f29212015-09-29 13:51:35 -0700225 switch (compression) {
226 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700227 if (buffer_size < in_size)
228 return 0;
Julius Wernereca99af2021-03-10 18:49:37 -0800229 if (rdev_readat(rdev, buffer, 0, in_size) != in_size)
Julius Werner09f29212015-09-29 13:51:35 -0700230 return 0;
Julius Werner05714cc2021-04-15 23:25:44 -0700231 if (cbfs_file_hash_mismatch(buffer, in_size, mdata, skip_verification))
Julius Wernerfccf1222021-04-02 15:58:05 -0700232 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700233 return in_size;
234
235 case CBFS_COMPRESS_LZ4:
Aaron Durbina121f952020-05-26 15:48:10 -0600236 if (!cbfs_lz4_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700237 return 0;
238
Julius Wernereca99af2021-03-10 18:49:37 -0800239 /* cbfs_prog_stage_load() takes care of in-place LZ4 decompression by
Julius Werner723e3b12020-12-29 17:33:30 -0800240 setting up the rdev to be in memory. */
Julius Wernereca99af2021-03-10 18:49:37 -0800241 map = rdev_mmap_full(rdev);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600242 if (map == NULL)
Julius Werner09f29212015-09-29 13:51:35 -0700243 return 0;
244
Julius Werner05714cc2021-04-15 23:25:44 -0700245 if (!cbfs_file_hash_mismatch(map, in_size, mdata, skip_verification)) {
Julius Wernerfccf1222021-04-02 15:58:05 -0700246 timestamp_add_now(TS_START_ULZ4F);
247 out_size = ulz4fn(map, in_size, buffer, buffer_size);
248 timestamp_add_now(TS_END_ULZ4F);
249 }
Aaron Durbin84f394e2020-05-26 16:16:42 -0600250
251 rdev_munmap(rdev, map);
252
Julius Werner09f29212015-09-29 13:51:35 -0700253 return out_size;
254
255 case CBFS_COMPRESS_LZMA:
Aaron Durbina121f952020-05-26 15:48:10 -0600256 if (!cbfs_lzma_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700257 return 0;
Julius Wernereca99af2021-03-10 18:49:37 -0800258 map = rdev_mmap_full(rdev);
Julius Werner09f29212015-09-29 13:51:35 -0700259 if (map == NULL)
260 return 0;
261
Julius Werner05714cc2021-04-15 23:25:44 -0700262 if (!cbfs_file_hash_mismatch(map, in_size, mdata, skip_verification)) {
Julius Wernerfccf1222021-04-02 15:58:05 -0700263 /* Note: timestamp not useful for memory-mapped media (x86) */
264 timestamp_add_now(TS_START_ULZMA);
265 out_size = ulzman(map, in_size, buffer, buffer_size);
266 timestamp_add_now(TS_END_ULZMA);
267 }
Julius Werner09f29212015-09-29 13:51:35 -0700268
269 rdev_munmap(rdev, map);
270
271 return out_size;
272
273 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500274 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700275 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500276}
277
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600278struct cbfs_preload_context {
279 struct region_device rdev;
280 struct thread_handle handle;
281 struct list_node list_node;
282 void *buffer;
283 char name[];
284};
285
286static struct list_node cbfs_preload_context_list;
287
288static struct cbfs_preload_context *alloc_cbfs_preload_context(size_t additional)
289{
290 struct cbfs_preload_context *context;
291 size_t size = sizeof(*context) + additional;
292
293 context = mem_pool_alloc(&cbfs_cache, size);
294
295 if (!context)
296 return NULL;
297
298 memset(context, 0, size);
299
300 return context;
301}
302
303static void append_cbfs_preload_context(struct cbfs_preload_context *context)
304{
305 list_append(&context->list_node, &cbfs_preload_context_list);
306}
307
308static void free_cbfs_preload_context(struct cbfs_preload_context *context)
309{
310 list_remove(&context->list_node);
311
312 mem_pool_free(&cbfs_cache, context);
313}
314
315static enum cb_err cbfs_preload_thread_entry(void *arg)
316{
317 struct cbfs_preload_context *context = arg;
318
Julius Wernerf364eb92021-12-01 14:48:59 -0800319 if (rdev_read_full(&context->rdev, context->buffer) < 0) {
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600320 ERROR("%s(name='%s') readat failed\n", __func__, context->name);
321 return CB_ERR;
322 }
323
324 return CB_SUCCESS;
325}
326
327void cbfs_preload(const char *name)
328{
329 struct region_device rdev;
330 union cbfs_mdata mdata;
331 struct cbfs_preload_context *context;
332 bool force_ro = false;
333 size_t size;
334
335 if (!CONFIG(CBFS_PRELOAD))
336 dead_code();
337
338 DEBUG("%s(name='%s')\n", __func__, name);
339
Julius Werner57d4bc62021-11-15 10:13:37 -0800340 if (_cbfs_boot_lookup(name, force_ro, &mdata, &rdev))
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600341 return;
342
343 size = region_device_sz(&rdev);
344
345 context = alloc_cbfs_preload_context(strlen(name) + 1);
346 if (!context) {
347 ERROR("%s(name='%s') failed to allocate preload context\n", __func__, name);
348 return;
349 }
350
351 context->buffer = mem_pool_alloc(&cbfs_cache, size);
352 if (context->buffer == NULL) {
353 ERROR("%s(name='%s') failed to allocate %zu bytes for preload buffer\n",
354 __func__, name, size);
355 goto out;
356 }
357
358 context->rdev = rdev;
359 strcpy(context->name, name);
360
361 append_cbfs_preload_context(context);
362
363 if (thread_run(&context->handle, cbfs_preload_thread_entry, context) == 0)
364 return;
365
366 ERROR("%s(name='%s') failed to start preload thread\n", __func__, name);
367 mem_pool_free(&cbfs_cache, context->buffer);
368
369out:
370 free_cbfs_preload_context(context);
371}
372
373static struct cbfs_preload_context *find_cbfs_preload_context(const char *name)
374{
375 struct cbfs_preload_context *context;
376
377 list_for_each(context, cbfs_preload_context_list, list_node) {
378 if (strcmp(context->name, name) == 0)
379 return context;
380 }
381
382 return NULL;
383}
384
385static enum cb_err get_preload_rdev(struct region_device *rdev, const char *name)
386{
387 enum cb_err err;
388 struct cbfs_preload_context *context;
389
390 if (!CONFIG(CBFS_PRELOAD) || (!ENV_RAMSTAGE && !ENV_ROMSTAGE))
391 return CB_ERR_ARG;
392
393 context = find_cbfs_preload_context(name);
394 if (!context)
395 return CB_ERR_ARG;
396
397 err = thread_join(&context->handle);
398 if (err != CB_SUCCESS) {
399 ERROR("%s(name='%s') Preload thread failed: %u\n", __func__, name, err);
400
401 goto out;
402 }
403
404 if (rdev_chain_mem(rdev, context->buffer, region_device_sz(&context->rdev)) != 0) {
405 ERROR("%s(name='%s') chaining failed\n", __func__, name);
406
407 err = CB_ERR;
408 goto out;
409 }
410
411 err = CB_SUCCESS;
412
413 DEBUG("%s(name='%s') preload successful\n", __func__, name);
414
415out:
416 free_cbfs_preload_context(context);
417
418 return err;
419}
420
Julius Werner05714cc2021-04-15 23:25:44 -0700421static void *do_alloc(union cbfs_mdata *mdata, struct region_device *rdev,
422 cbfs_allocator_t allocator, void *arg, size_t *size_out,
423 bool skip_verification)
424{
425 size_t size = region_device_sz(rdev);
426 void *loc = NULL;
427
428 uint32_t compression = CBFS_COMPRESS_NONE;
429 const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(mdata,
430 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr));
431 if (cattr) {
432 compression = be32toh(cattr->compression);
433 size = be32toh(cattr->decompressed_size);
434 }
435
436 if (size_out)
437 *size_out = size;
438
439 /* allocator == NULL means do a cbfs_map() */
440 if (allocator) {
441 loc = allocator(arg, size, mdata);
442 } else if (compression == CBFS_COMPRESS_NONE) {
443 void *mapping = rdev_mmap_full(rdev);
444 if (!mapping)
445 return NULL;
446 if (cbfs_file_hash_mismatch(mapping, size, mdata, skip_verification)) {
447 rdev_munmap(rdev, mapping);
448 return NULL;
449 }
450 return mapping;
451 } else if (!cbfs_cache.size) {
452 /* In order to use the cbfs_cache you need to add a CBFS_CACHE to your
453 * memlayout. For stages that don't have .data sections (x86 pre-RAM),
454 * it is not possible to add a CBFS_CACHE. */
455 ERROR("Cannot map compressed file %s without cbfs_cache\n", mdata->h.filename);
456 return NULL;
457 } else {
458 loc = mem_pool_alloc(&cbfs_cache, size);
459 }
460
461 if (!loc) {
462 ERROR("'%s' allocation failure\n", mdata->h.filename);
463 return NULL;
464 }
465
466 size = cbfs_load_and_decompress(rdev, loc, size, compression, mdata, skip_verification);
467 if (!size)
468 return NULL;
469
470 return loc;
471}
472
Julius Werner7778cf22021-01-05 18:54:19 -0800473void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg,
474 size_t *size_out, bool force_ro, enum cbfs_type *type)
Julius Wernerf975e552016-08-19 15:43:06 -0700475{
Julius Wernerd17ce412020-10-01 18:25:49 -0700476 struct region_device rdev;
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600477 bool preload_successful = false;
Julius Wernerd17ce412020-10-01 18:25:49 -0700478 union cbfs_mdata mdata;
Julius Werner7778cf22021-01-05 18:54:19 -0800479
480 DEBUG("%s(name='%s', alloc=%p(%p), force_ro=%s, type=%d)\n", __func__, name, allocator,
481 arg, force_ro ? "true" : "false", type ? *type : -1);
Julius Wernerf975e552016-08-19 15:43:06 -0700482
Julius Werner57d4bc62021-11-15 10:13:37 -0800483 if (_cbfs_boot_lookup(name, force_ro, &mdata, &rdev))
Julius Werner7778cf22021-01-05 18:54:19 -0800484 return NULL;
Julius Wernerf975e552016-08-19 15:43:06 -0700485
Julius Werner7778cf22021-01-05 18:54:19 -0800486 if (type) {
487 const enum cbfs_type real_type = be32toh(mdata.h.type);
488 if (*type == CBFS_TYPE_QUERY)
489 *type = real_type;
490 else if (*type != real_type) {
491 ERROR("'%s' type mismatch (is %u, expected %u)\n",
492 mdata.h.filename, real_type, *type);
493 return NULL;
494 }
Julius Wernerd17ce412020-10-01 18:25:49 -0700495 }
Julius Wernerf975e552016-08-19 15:43:06 -0700496
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600497 /* Update the rdev with the preload content */
498 if (!force_ro && get_preload_rdev(&rdev, name) == CB_SUCCESS)
499 preload_successful = true;
500
Julius Werner05714cc2021-04-15 23:25:44 -0700501 void *ret = do_alloc(&mdata, &rdev, allocator, arg, size_out, false);
Raul E Rangel3f41d322021-07-23 15:23:12 -0600502
Julius Werner05714cc2021-04-15 23:25:44 -0700503 /* When using cbfs_preload we need to free the preload buffer after populating the
504 * destination buffer. We know we must have a mem_rdev here, so extra mmap is fine. */
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600505 if (preload_successful)
506 cbfs_unmap(rdev_mmap_full(&rdev));
Julius Werner7778cf22021-01-05 18:54:19 -0800507
Julius Werner05714cc2021-04-15 23:25:44 -0700508 return ret;
509}
510
511void *_cbfs_unverified_area_alloc(const char *area, const char *name,
512 cbfs_allocator_t allocator, void *arg, size_t *size_out)
513{
514 struct region_device area_rdev, file_rdev;
515 union cbfs_mdata mdata;
516 size_t data_offset;
517
518 DEBUG("%s(area='%s', name='%s', alloc=%p(%p))\n", __func__, area, name, allocator, arg);
519
520 if (fmap_locate_area_as_rdev(area, &area_rdev))
521 return NULL;
522
523 if (cbfs_lookup(&area_rdev, name, &mdata, &data_offset, NULL)) {
524 ERROR("'%s' not found in '%s'\n", name, area);
525 return NULL;
526 }
527
528 if (rdev_chain(&file_rdev, &area_rdev, data_offset, be32toh(mdata.h.len)))
529 return NULL;
530
531 if (tspi_measure_cbfs_hook(&file_rdev, name, be32toh(mdata.h.type)))
532 ERROR("error measuring '%s' in '%s'\n", name, area);
533
534 return do_alloc(&mdata, &file_rdev, allocator, arg, size_out, true);
Julius Werner7778cf22021-01-05 18:54:19 -0800535}
536
Julius Werner4676ec52021-03-10 16:52:14 -0800537void *_cbfs_default_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
Julius Werner7778cf22021-01-05 18:54:19 -0800538{
539 struct _cbfs_default_allocator_arg *darg = arg;
540 if (size > darg->buf_size)
541 return NULL;
542 return darg->buf;
543}
544
Julius Werner4676ec52021-03-10 16:52:14 -0800545void *_cbfs_cbmem_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
Julius Werner7778cf22021-01-05 18:54:19 -0800546{
547 return cbmem_add((uintptr_t)arg, size);
Julius Wernerf975e552016-08-19 15:43:06 -0700548}
549
Julius Werner1de87082020-12-23 17:38:11 -0800550cb_err_t cbfs_prog_stage_load(struct prog *pstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500551{
Julius Werner1de87082020-12-23 17:38:11 -0800552 union cbfs_mdata mdata;
553 struct region_device rdev;
Julius Werner1de87082020-12-23 17:38:11 -0800554 cb_err_t err;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800555
Julius Werner1de87082020-12-23 17:38:11 -0800556 prog_locate_hook(pstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500557
Julius Werner57d4bc62021-11-15 10:13:37 -0800558 if ((err = _cbfs_boot_lookup(prog_name(pstage), false, &mdata, &rdev)))
Julius Werner1de87082020-12-23 17:38:11 -0800559 return err;
560
561 assert(be32toh(mdata.h.type) == CBFS_TYPE_STAGE);
562 pstage->cbfs_type = CBFS_TYPE_STAGE;
563
Julius Werner81dc20e2020-10-15 17:37:57 -0700564 enum cbfs_compression compression = CBFS_COMPRESS_NONE;
565 const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(&mdata,
566 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr));
567 if (cattr)
568 compression = be32toh(cattr->compression);
Julius Werner1de87082020-12-23 17:38:11 -0800569
Julius Werner81dc20e2020-10-15 17:37:57 -0700570 const struct cbfs_file_attr_stageheader *sattr = cbfs_find_attr(&mdata,
571 CBFS_FILE_ATTR_TAG_STAGEHEADER, sizeof(*sattr));
572 if (!sattr)
573 return CB_ERR;
574 prog_set_area(pstage, (void *)(uintptr_t)be64toh(sattr->loadaddr),
575 be32toh(sattr->memlen));
576 prog_set_entry(pstage, prog_start(pstage) +
577 be32toh(sattr->entry_offset), NULL);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500578
Aaron Durbined253c82015-10-07 17:22:42 -0500579 /* Hacky way to not load programs over read only media. The stages
580 * that would hit this path initialize themselves. */
Julius Werner21a40532020-04-21 16:03:53 -0700581 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) &&
582 !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
Julius Werner81dc20e2020-10-15 17:37:57 -0700583 void *mapping = rdev_mmap_full(&rdev);
Julius Werner1de87082020-12-23 17:38:11 -0800584 rdev_munmap(&rdev, mapping);
Julius Werner05714cc2021-04-15 23:25:44 -0700585 if (cbfs_file_hash_mismatch(mapping, region_device_sz(&rdev), &mdata, false))
Julius Wernerfccf1222021-04-02 15:58:05 -0700586 return CB_CBFS_HASH_MISMATCH;
Julius Werner81dc20e2020-10-15 17:37:57 -0700587 if (mapping == prog_start(pstage))
588 return CB_SUCCESS;
Aaron Durbined253c82015-10-07 17:22:42 -0500589 }
590
Julius Wernereca99af2021-03-10 18:49:37 -0800591 /* LZ4 stages can be decompressed in-place to save mapping scratch space. Load the
592 compressed data to the end of the buffer and point &rdev to that memory location. */
593 if (cbfs_lz4_enabled() && compression == CBFS_COMPRESS_LZ4) {
594 size_t in_size = region_device_sz(&rdev);
595 void *compr_start = prog_start(pstage) + prog_size(pstage) - in_size;
596 if (rdev_readat(&rdev, compr_start, 0, in_size) != in_size)
597 return CB_ERR;
Julius Wernerc8931972021-04-16 16:48:32 -0700598 rdev_chain_mem(&rdev, compr_start, in_size);
Julius Wernereca99af2021-03-10 18:49:37 -0800599 }
600
601 size_t fsize = cbfs_load_and_decompress(&rdev, prog_start(pstage), prog_size(pstage),
Julius Werner05714cc2021-04-15 23:25:44 -0700602 compression, &mdata, false);
Julius Werner09f29212015-09-29 13:51:35 -0700603 if (!fsize)
Julius Werner1de87082020-12-23 17:38:11 -0800604 return CB_ERR;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500605
606 /* Clear area not covered by file. */
Julius Werner81dc20e2020-10-15 17:37:57 -0700607 memset(prog_start(pstage) + fsize, 0, prog_size(pstage) - fsize);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500608
Julius Werner81dc20e2020-10-15 17:37:57 -0700609 prog_segment_loaded((uintptr_t)prog_start(pstage), prog_size(pstage),
610 SEG_FINAL);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500611
Julius Werner1de87082020-12-23 17:38:11 -0800612 return CB_SUCCESS;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800613}
Aaron Durbin6d720f32015-12-08 17:00:23 -0600614
Julius Werner1e37c9c2019-12-11 17:09:39 -0800615void cbfs_boot_device_find_mcache(struct cbfs_boot_device *cbd, uint32_t id)
Aaron Durbin6d720f32015-12-08 17:00:23 -0600616{
Julius Werner1e37c9c2019-12-11 17:09:39 -0800617 if (CONFIG(NO_CBFS_MCACHE) || ENV_SMM)
618 return;
619
Julius Werner34cf0732020-12-08 14:21:43 -0800620 if (cbd->mcache_size)
621 return;
622
Julius Werner1e37c9c2019-12-11 17:09:39 -0800623 const struct cbmem_entry *entry;
624 if (cbmem_possibly_online() &&
625 (entry = cbmem_entry_find(id))) {
626 cbd->mcache = cbmem_entry_start(entry);
627 cbd->mcache_size = cbmem_entry_size(entry);
628 } else if (ENV_ROMSTAGE_OR_BEFORE) {
629 u8 *boundary = _ecbfs_mcache - REGION_SIZE(cbfs_mcache) *
630 CONFIG_CBFS_MCACHE_RW_PERCENTAGE / 100;
Julius Werner723e3b12020-12-29 17:33:30 -0800631 boundary = (u8 *)ALIGN_DOWN((uintptr_t)boundary, CBFS_MCACHE_ALIGNMENT);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800632 if (id == CBMEM_ID_CBFS_RO_MCACHE) {
633 cbd->mcache = _cbfs_mcache;
634 cbd->mcache_size = boundary - _cbfs_mcache;
635 } else if (id == CBMEM_ID_CBFS_RW_MCACHE) {
636 cbd->mcache = boundary;
637 cbd->mcache_size = _ecbfs_mcache - boundary;
638 }
639 }
Aaron Durbin6d720f32015-12-08 17:00:23 -0600640}
Julius Werner1e37c9c2019-12-11 17:09:39 -0800641
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700642cb_err_t cbfs_init_boot_device(const struct cbfs_boot_device *cbd,
Julius Werner723e3b12020-12-29 17:33:30 -0800643 struct vb2_hash *mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700644{
645 /* If we have an mcache, mcache_build() will also check mdata hash. */
646 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size > 0)
Julius Werner723e3b12020-12-29 17:33:30 -0800647 return cbfs_mcache_build(&cbd->rdev, cbd->mcache, cbd->mcache_size, mdata_hash);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700648
649 /* No mcache and no verification means we have nothing special to do. */
Julius Werner723e3b12020-12-29 17:33:30 -0800650 if (!CONFIG(CBFS_VERIFICATION) || !mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700651 return CB_SUCCESS;
652
Julius Werner723e3b12020-12-29 17:33:30 -0800653 /* Verification only: use cbfs_walk() without a walker() function to just run through
654 the CBFS once, will return NOT_FOUND by default. */
655 cb_err_t err = cbfs_walk(&cbd->rdev, NULL, NULL, mdata_hash, 0);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700656 if (err == CB_CBFS_NOT_FOUND)
657 err = CB_SUCCESS;
658 return err;
659}
660
Julius Werner1e37c9c2019-12-11 17:09:39 -0800661const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro)
662{
663 static struct cbfs_boot_device ro;
664
Julius Werner723e3b12020-12-29 17:33:30 -0800665 /* Ensure we always init RO mcache, even if the first file is from the RW CBFS.
Julius Werner1e37c9c2019-12-11 17:09:39 -0800666 Otherwise it may not be available when needed in later stages. */
667 if (ENV_INITIAL_STAGE && !force_ro && !region_device_sz(&ro.rdev))
668 cbfs_get_boot_device(true);
669
670 if (!force_ro) {
671 const struct cbfs_boot_device *rw = vboot_get_cbfs_boot_device();
Julius Werner723e3b12020-12-29 17:33:30 -0800672 /* This will return NULL if vboot isn't enabled, didn't run yet or decided to
673 boot into recovery mode. */
Julius Werner1e37c9c2019-12-11 17:09:39 -0800674 if (rw)
675 return rw;
676 }
677
Julius Werner723e3b12020-12-29 17:33:30 -0800678 /* In rare cases post-RAM stages may run this before cbmem_initialize(), so we can't
679 lock in the result of find_mcache() on the first try and should keep trying every
680 time until an mcache is found. */
Julius Werner34cf0732020-12-08 14:21:43 -0800681 cbfs_boot_device_find_mcache(&ro, CBMEM_ID_CBFS_RO_MCACHE);
682
Julius Werner1e37c9c2019-12-11 17:09:39 -0800683 if (region_device_sz(&ro.rdev))
684 return &ro;
685
686 if (fmap_locate_area_as_rdev("COREBOOT", &ro.rdev))
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700687 die("Cannot locate primary CBFS");
Julius Werner1e37c9c2019-12-11 17:09:39 -0800688
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700689 if (ENV_INITIAL_STAGE) {
690 cb_err_t err = cbfs_init_boot_device(&ro, metadata_hash_get());
691 if (err == CB_CBFS_HASH_MISMATCH)
692 die("RO CBFS metadata hash verification failure");
693 else if (CONFIG(TOCTOU_SAFETY) && err == CB_CBFS_CACHE_FULL)
694 die("RO mcache overflow breaks TOCTOU safety!\n");
695 else if (err && err != CB_CBFS_CACHE_FULL)
696 die("RO CBFS initialization error: %d", err);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800697 }
698
699 return &ro;
700}
701
702#if !CONFIG(NO_CBFS_MCACHE)
703static void mcache_to_cbmem(const struct cbfs_boot_device *cbd, u32 cbmem_id)
704{
705 if (!cbd)
706 return;
707
708 size_t real_size = cbfs_mcache_real_size(cbd->mcache, cbd->mcache_size);
709 void *cbmem_mcache = cbmem_add(cbmem_id, real_size);
710 if (!cbmem_mcache) {
711 printk(BIOS_ERR, "ERROR: Cannot allocate CBMEM mcache %#x (%#zx bytes)!\n",
712 cbmem_id, real_size);
713 return;
714 }
715 memcpy(cbmem_mcache, cbd->mcache, real_size);
716}
717
718static void cbfs_mcache_migrate(int unused)
719{
720 mcache_to_cbmem(vboot_get_cbfs_boot_device(), CBMEM_ID_CBFS_RW_MCACHE);
721 mcache_to_cbmem(cbfs_get_boot_device(true), CBMEM_ID_CBFS_RO_MCACHE);
722}
723ROMSTAGE_CBMEM_INIT_HOOK(cbfs_mcache_migrate)
724#endif