blob: dbb3b1a9133e17c44e9e5c0b57b3c8965435cb8d [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 Werner0d9072b2020-03-05 12:51:08 -08006#include <cbfs_private.h>
Julius Werner1e37c9c2019-12-11 17:09:39 -08007#include <cbmem.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 Werner0d9072b2020-03-05 12:51:08 -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 Werner0d9072b2020-03-05 12:51:08 -080068 return cbfs_boot_lookup(name, true, mdata, rdev);
69 }
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 Werner0d9072b2020-03-05 12:51:08 -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 Wernerfccf1222021-04-02 15:58:05 -0700193static inline bool cbfs_file_hash_mismatch(const void *buffer, size_t size,
194 const struct vb2_hash *file_hash)
195{
196 /* Avoid linking hash functions when verification is disabled. */
197 if (!CONFIG(CBFS_VERIFICATION))
198 return false;
199
200 /* If there is no file hash, always count that as a mismatch. */
201 if (file_hash && vb2_hash_verify(buffer, size, file_hash) == VB2_SUCCESS)
202 return false;
203
204 printk(BIOS_CRIT, "CBFS file hash mismatch!\n");
205 return true;
206}
207
208static size_t cbfs_load_and_decompress(const struct region_device *rdev, void *buffer,
209 size_t buffer_size, uint32_t compression,
210 const struct vb2_hash *file_hash)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500211{
Julius Wernereca99af2021-03-10 18:49:37 -0800212 size_t in_size = region_device_sz(rdev);
Julius Wernerfccf1222021-04-02 15:58:05 -0700213 size_t out_size = 0;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600214 void *map;
Julius Werner09f29212015-09-29 13:51:35 -0700215
Julius Wernereca99af2021-03-10 18:49:37 -0800216 DEBUG("Decompressing %zu bytes to %p with algo %d\n", in_size, buffer, compression);
Julius Werner7778cf22021-01-05 18:54:19 -0800217
Julius Werner09f29212015-09-29 13:51:35 -0700218 switch (compression) {
219 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700220 if (buffer_size < in_size)
221 return 0;
Julius Wernereca99af2021-03-10 18:49:37 -0800222 if (rdev_readat(rdev, buffer, 0, in_size) != in_size)
Julius Werner09f29212015-09-29 13:51:35 -0700223 return 0;
Julius Wernerfccf1222021-04-02 15:58:05 -0700224 if (cbfs_file_hash_mismatch(buffer, in_size, file_hash))
225 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700226 return in_size;
227
228 case CBFS_COMPRESS_LZ4:
Aaron Durbina121f952020-05-26 15:48:10 -0600229 if (!cbfs_lz4_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700230 return 0;
231
Julius Wernereca99af2021-03-10 18:49:37 -0800232 /* cbfs_prog_stage_load() takes care of in-place LZ4 decompression by
Julius Werner723e3b12020-12-29 17:33:30 -0800233 setting up the rdev to be in memory. */
Julius Wernereca99af2021-03-10 18:49:37 -0800234 map = rdev_mmap_full(rdev);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600235 if (map == NULL)
Julius Werner09f29212015-09-29 13:51:35 -0700236 return 0;
237
Julius Wernerfccf1222021-04-02 15:58:05 -0700238 if (!cbfs_file_hash_mismatch(map, in_size, file_hash)) {
239 timestamp_add_now(TS_START_ULZ4F);
240 out_size = ulz4fn(map, in_size, buffer, buffer_size);
241 timestamp_add_now(TS_END_ULZ4F);
242 }
Aaron Durbin84f394e2020-05-26 16:16:42 -0600243
244 rdev_munmap(rdev, map);
245
Julius Werner09f29212015-09-29 13:51:35 -0700246 return out_size;
247
248 case CBFS_COMPRESS_LZMA:
Aaron Durbina121f952020-05-26 15:48:10 -0600249 if (!cbfs_lzma_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700250 return 0;
Julius Wernereca99af2021-03-10 18:49:37 -0800251 map = rdev_mmap_full(rdev);
Julius Werner09f29212015-09-29 13:51:35 -0700252 if (map == NULL)
253 return 0;
254
Julius Wernerfccf1222021-04-02 15:58:05 -0700255 if (!cbfs_file_hash_mismatch(map, in_size, file_hash)) {
256 /* Note: timestamp not useful for memory-mapped media (x86) */
257 timestamp_add_now(TS_START_ULZMA);
258 out_size = ulzman(map, in_size, buffer, buffer_size);
259 timestamp_add_now(TS_END_ULZMA);
260 }
Julius Werner09f29212015-09-29 13:51:35 -0700261
262 rdev_munmap(rdev, map);
263
264 return out_size;
265
266 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500267 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700268 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500269}
270
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600271struct cbfs_preload_context {
272 struct region_device rdev;
273 struct thread_handle handle;
274 struct list_node list_node;
275 void *buffer;
276 char name[];
277};
278
279static struct list_node cbfs_preload_context_list;
280
281static struct cbfs_preload_context *alloc_cbfs_preload_context(size_t additional)
282{
283 struct cbfs_preload_context *context;
284 size_t size = sizeof(*context) + additional;
285
286 context = mem_pool_alloc(&cbfs_cache, size);
287
288 if (!context)
289 return NULL;
290
291 memset(context, 0, size);
292
293 return context;
294}
295
296static void append_cbfs_preload_context(struct cbfs_preload_context *context)
297{
298 list_append(&context->list_node, &cbfs_preload_context_list);
299}
300
301static void free_cbfs_preload_context(struct cbfs_preload_context *context)
302{
303 list_remove(&context->list_node);
304
305 mem_pool_free(&cbfs_cache, context);
306}
307
308static enum cb_err cbfs_preload_thread_entry(void *arg)
309{
310 struct cbfs_preload_context *context = arg;
311
312 if (rdev_readat_full(&context->rdev, context->buffer) < 0) {
313 ERROR("%s(name='%s') readat failed\n", __func__, context->name);
314 return CB_ERR;
315 }
316
317 return CB_SUCCESS;
318}
319
320void cbfs_preload(const char *name)
321{
322 struct region_device rdev;
323 union cbfs_mdata mdata;
324 struct cbfs_preload_context *context;
325 bool force_ro = false;
326 size_t size;
327
328 if (!CONFIG(CBFS_PRELOAD))
329 dead_code();
330
331 DEBUG("%s(name='%s')\n", __func__, name);
332
333 if (cbfs_boot_lookup(name, force_ro, &mdata, &rdev))
334 return;
335
336 size = region_device_sz(&rdev);
337
338 context = alloc_cbfs_preload_context(strlen(name) + 1);
339 if (!context) {
340 ERROR("%s(name='%s') failed to allocate preload context\n", __func__, name);
341 return;
342 }
343
344 context->buffer = mem_pool_alloc(&cbfs_cache, size);
345 if (context->buffer == NULL) {
346 ERROR("%s(name='%s') failed to allocate %zu bytes for preload buffer\n",
347 __func__, name, size);
348 goto out;
349 }
350
351 context->rdev = rdev;
352 strcpy(context->name, name);
353
354 append_cbfs_preload_context(context);
355
356 if (thread_run(&context->handle, cbfs_preload_thread_entry, context) == 0)
357 return;
358
359 ERROR("%s(name='%s') failed to start preload thread\n", __func__, name);
360 mem_pool_free(&cbfs_cache, context->buffer);
361
362out:
363 free_cbfs_preload_context(context);
364}
365
366static struct cbfs_preload_context *find_cbfs_preload_context(const char *name)
367{
368 struct cbfs_preload_context *context;
369
370 list_for_each(context, cbfs_preload_context_list, list_node) {
371 if (strcmp(context->name, name) == 0)
372 return context;
373 }
374
375 return NULL;
376}
377
378static enum cb_err get_preload_rdev(struct region_device *rdev, const char *name)
379{
380 enum cb_err err;
381 struct cbfs_preload_context *context;
382
383 if (!CONFIG(CBFS_PRELOAD) || (!ENV_RAMSTAGE && !ENV_ROMSTAGE))
384 return CB_ERR_ARG;
385
386 context = find_cbfs_preload_context(name);
387 if (!context)
388 return CB_ERR_ARG;
389
390 err = thread_join(&context->handle);
391 if (err != CB_SUCCESS) {
392 ERROR("%s(name='%s') Preload thread failed: %u\n", __func__, name, err);
393
394 goto out;
395 }
396
397 if (rdev_chain_mem(rdev, context->buffer, region_device_sz(&context->rdev)) != 0) {
398 ERROR("%s(name='%s') chaining failed\n", __func__, name);
399
400 err = CB_ERR;
401 goto out;
402 }
403
404 err = CB_SUCCESS;
405
406 DEBUG("%s(name='%s') preload successful\n", __func__, name);
407
408out:
409 free_cbfs_preload_context(context);
410
411 return err;
412}
413
Julius Werner7778cf22021-01-05 18:54:19 -0800414void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg,
415 size_t *size_out, bool force_ro, enum cbfs_type *type)
Julius Wernerf975e552016-08-19 15:43:06 -0700416{
Julius Wernerd17ce412020-10-01 18:25:49 -0700417 struct region_device rdev;
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600418 bool preload_successful = false;
Julius Wernerd17ce412020-10-01 18:25:49 -0700419 union cbfs_mdata mdata;
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600420 void *loc = NULL;
Julius Werner7778cf22021-01-05 18:54:19 -0800421
422 DEBUG("%s(name='%s', alloc=%p(%p), force_ro=%s, type=%d)\n", __func__, name, allocator,
423 arg, force_ro ? "true" : "false", type ? *type : -1);
Julius Wernerf975e552016-08-19 15:43:06 -0700424
Julius Werner9d0cc2a2020-01-22 18:00:18 -0800425 if (cbfs_boot_lookup(name, force_ro, &mdata, &rdev))
Julius Werner7778cf22021-01-05 18:54:19 -0800426 return NULL;
Julius Wernerf975e552016-08-19 15:43:06 -0700427
Julius Werner7778cf22021-01-05 18:54:19 -0800428 if (type) {
429 const enum cbfs_type real_type = be32toh(mdata.h.type);
430 if (*type == CBFS_TYPE_QUERY)
431 *type = real_type;
432 else if (*type != real_type) {
433 ERROR("'%s' type mismatch (is %u, expected %u)\n",
434 mdata.h.filename, real_type, *type);
435 return NULL;
436 }
Julius Wernerd17ce412020-10-01 18:25:49 -0700437 }
Julius Wernerf975e552016-08-19 15:43:06 -0700438
Julius Werner7778cf22021-01-05 18:54:19 -0800439 size_t size = region_device_sz(&rdev);
440 uint32_t compression = CBFS_COMPRESS_NONE;
441 const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(&mdata,
442 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr));
443 if (cattr) {
444 compression = be32toh(cattr->compression);
445 size = be32toh(cattr->decompressed_size);
446 }
447
448 if (size_out)
449 *size_out = size;
450
Julius Wernerfccf1222021-04-02 15:58:05 -0700451 const struct vb2_hash *file_hash = NULL;
452 if (CONFIG(CBFS_VERIFICATION))
453 file_hash = cbfs_file_hash(&mdata);
454
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600455 /* Update the rdev with the preload content */
456 if (!force_ro && get_preload_rdev(&rdev, name) == CB_SUCCESS)
457 preload_successful = true;
458
Julius Werner7778cf22021-01-05 18:54:19 -0800459 /* allocator == NULL means do a cbfs_map() */
460 if (allocator) {
461 loc = allocator(arg, size, &mdata);
462 } else if (compression == CBFS_COMPRESS_NONE) {
Julius Wernerfccf1222021-04-02 15:58:05 -0700463 void *mapping = rdev_mmap_full(&rdev);
Raul E Rangel3f41d322021-07-23 15:23:12 -0600464
465 if (!mapping)
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600466 goto out;
Raul E Rangel3f41d322021-07-23 15:23:12 -0600467
468 if (cbfs_file_hash_mismatch(mapping, size, file_hash)) {
469 rdev_munmap(&rdev, mapping);
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600470 goto out;
Raul E Rangel3f41d322021-07-23 15:23:12 -0600471 }
472
Julius Wernerfccf1222021-04-02 15:58:05 -0700473 return mapping;
Raul E Rangel3d7b9842021-10-08 13:10:38 -0600474 } else if (!cbfs_cache.size) {
475 /*
476 * In order to use the cbfs_cache you need to add a CBFS_CACHE to your
477 * memlayout. For stages that don't have .data sections (x86 pre-RAM),
478 * it is not possible to add a CBFS_CACHE.
479 */
480 ERROR("Cannot map compressed file %s without cbfs_cache\n", mdata.h.filename);
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600481 goto out;
Julius Werner7778cf22021-01-05 18:54:19 -0800482 } else {
483 loc = mem_pool_alloc(&cbfs_cache, size);
484 }
485
486 if (!loc) {
487 ERROR("'%s' allocation failure\n", mdata.h.filename);
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600488 goto out;
Julius Werner7778cf22021-01-05 18:54:19 -0800489 }
490
Julius Wernerfccf1222021-04-02 15:58:05 -0700491 size = cbfs_load_and_decompress(&rdev, loc, size, compression, file_hash);
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600492
Julius Werner7778cf22021-01-05 18:54:19 -0800493 if (!size)
Raul E Rangel4cfb8622021-11-01 13:40:14 -0600494 loc = NULL;
495
496out:
497 /*
498 * When using cbfs_preload we need to free the preload buffer after populating the
499 * destination buffer.
500 */
501 if (preload_successful)
502 cbfs_unmap(rdev_mmap_full(&rdev));
Julius Werner7778cf22021-01-05 18:54:19 -0800503
504 return loc;
505}
506
Julius Werner4676ec52021-03-10 16:52:14 -0800507void *_cbfs_default_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
Julius Werner7778cf22021-01-05 18:54:19 -0800508{
509 struct _cbfs_default_allocator_arg *darg = arg;
510 if (size > darg->buf_size)
511 return NULL;
512 return darg->buf;
513}
514
Julius Werner4676ec52021-03-10 16:52:14 -0800515void *_cbfs_cbmem_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
Julius Werner7778cf22021-01-05 18:54:19 -0800516{
517 return cbmem_add((uintptr_t)arg, size);
Julius Wernerf975e552016-08-19 15:43:06 -0700518}
519
Julius Werner1de87082020-12-23 17:38:11 -0800520cb_err_t cbfs_prog_stage_load(struct prog *pstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500521{
Julius Werner1de87082020-12-23 17:38:11 -0800522 union cbfs_mdata mdata;
523 struct region_device rdev;
Julius Werner1de87082020-12-23 17:38:11 -0800524 cb_err_t err;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800525
Julius Werner1de87082020-12-23 17:38:11 -0800526 prog_locate_hook(pstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500527
Julius Werner1de87082020-12-23 17:38:11 -0800528 if ((err = cbfs_boot_lookup(prog_name(pstage), false, &mdata, &rdev)))
529 return err;
530
531 assert(be32toh(mdata.h.type) == CBFS_TYPE_STAGE);
532 pstage->cbfs_type = CBFS_TYPE_STAGE;
533
Julius Werner81dc20e2020-10-15 17:37:57 -0700534 enum cbfs_compression compression = CBFS_COMPRESS_NONE;
535 const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(&mdata,
536 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr));
537 if (cattr)
538 compression = be32toh(cattr->compression);
Julius Werner1de87082020-12-23 17:38:11 -0800539
Julius Werner81dc20e2020-10-15 17:37:57 -0700540 const struct cbfs_file_attr_stageheader *sattr = cbfs_find_attr(&mdata,
541 CBFS_FILE_ATTR_TAG_STAGEHEADER, sizeof(*sattr));
542 if (!sattr)
543 return CB_ERR;
544 prog_set_area(pstage, (void *)(uintptr_t)be64toh(sattr->loadaddr),
545 be32toh(sattr->memlen));
546 prog_set_entry(pstage, prog_start(pstage) +
547 be32toh(sattr->entry_offset), NULL);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500548
Julius Wernerfccf1222021-04-02 15:58:05 -0700549 const struct vb2_hash *file_hash = NULL;
550 if (CONFIG(CBFS_VERIFICATION))
551 file_hash = cbfs_file_hash(&mdata);
552
Aaron Durbined253c82015-10-07 17:22:42 -0500553 /* Hacky way to not load programs over read only media. The stages
554 * that would hit this path initialize themselves. */
Julius Werner21a40532020-04-21 16:03:53 -0700555 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) &&
556 !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
Julius Werner81dc20e2020-10-15 17:37:57 -0700557 void *mapping = rdev_mmap_full(&rdev);
Julius Werner1de87082020-12-23 17:38:11 -0800558 rdev_munmap(&rdev, mapping);
Julius Wernerfccf1222021-04-02 15:58:05 -0700559 if (cbfs_file_hash_mismatch(mapping, region_device_sz(&rdev), file_hash))
560 return CB_CBFS_HASH_MISMATCH;
Julius Werner81dc20e2020-10-15 17:37:57 -0700561 if (mapping == prog_start(pstage))
562 return CB_SUCCESS;
Aaron Durbined253c82015-10-07 17:22:42 -0500563 }
564
Julius Wernereca99af2021-03-10 18:49:37 -0800565 /* LZ4 stages can be decompressed in-place to save mapping scratch space. Load the
566 compressed data to the end of the buffer and point &rdev to that memory location. */
567 if (cbfs_lz4_enabled() && compression == CBFS_COMPRESS_LZ4) {
568 size_t in_size = region_device_sz(&rdev);
569 void *compr_start = prog_start(pstage) + prog_size(pstage) - in_size;
570 if (rdev_readat(&rdev, compr_start, 0, in_size) != in_size)
571 return CB_ERR;
Julius Wernerc8931972021-04-16 16:48:32 -0700572 rdev_chain_mem(&rdev, compr_start, in_size);
Julius Wernereca99af2021-03-10 18:49:37 -0800573 }
574
575 size_t fsize = cbfs_load_and_decompress(&rdev, prog_start(pstage), prog_size(pstage),
Julius Wernerfccf1222021-04-02 15:58:05 -0700576 compression, file_hash);
Julius Werner09f29212015-09-29 13:51:35 -0700577 if (!fsize)
Julius Werner1de87082020-12-23 17:38:11 -0800578 return CB_ERR;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500579
580 /* Clear area not covered by file. */
Julius Werner81dc20e2020-10-15 17:37:57 -0700581 memset(prog_start(pstage) + fsize, 0, prog_size(pstage) - fsize);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500582
Julius Werner81dc20e2020-10-15 17:37:57 -0700583 prog_segment_loaded((uintptr_t)prog_start(pstage), prog_size(pstage),
584 SEG_FINAL);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500585
Julius Werner1de87082020-12-23 17:38:11 -0800586 return CB_SUCCESS;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800587}
Aaron Durbin6d720f32015-12-08 17:00:23 -0600588
Julius Werner1e37c9c2019-12-11 17:09:39 -0800589void cbfs_boot_device_find_mcache(struct cbfs_boot_device *cbd, uint32_t id)
Aaron Durbin6d720f32015-12-08 17:00:23 -0600590{
Julius Werner1e37c9c2019-12-11 17:09:39 -0800591 if (CONFIG(NO_CBFS_MCACHE) || ENV_SMM)
592 return;
593
Julius Werner34cf0732020-12-08 14:21:43 -0800594 if (cbd->mcache_size)
595 return;
596
Julius Werner1e37c9c2019-12-11 17:09:39 -0800597 const struct cbmem_entry *entry;
598 if (cbmem_possibly_online() &&
599 (entry = cbmem_entry_find(id))) {
600 cbd->mcache = cbmem_entry_start(entry);
601 cbd->mcache_size = cbmem_entry_size(entry);
602 } else if (ENV_ROMSTAGE_OR_BEFORE) {
603 u8 *boundary = _ecbfs_mcache - REGION_SIZE(cbfs_mcache) *
604 CONFIG_CBFS_MCACHE_RW_PERCENTAGE / 100;
Julius Werner723e3b12020-12-29 17:33:30 -0800605 boundary = (u8 *)ALIGN_DOWN((uintptr_t)boundary, CBFS_MCACHE_ALIGNMENT);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800606 if (id == CBMEM_ID_CBFS_RO_MCACHE) {
607 cbd->mcache = _cbfs_mcache;
608 cbd->mcache_size = boundary - _cbfs_mcache;
609 } else if (id == CBMEM_ID_CBFS_RW_MCACHE) {
610 cbd->mcache = boundary;
611 cbd->mcache_size = _ecbfs_mcache - boundary;
612 }
613 }
Aaron Durbin6d720f32015-12-08 17:00:23 -0600614}
Julius Werner1e37c9c2019-12-11 17:09:39 -0800615
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700616cb_err_t cbfs_init_boot_device(const struct cbfs_boot_device *cbd,
Julius Werner723e3b12020-12-29 17:33:30 -0800617 struct vb2_hash *mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700618{
619 /* If we have an mcache, mcache_build() will also check mdata hash. */
620 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size > 0)
Julius Werner723e3b12020-12-29 17:33:30 -0800621 return cbfs_mcache_build(&cbd->rdev, cbd->mcache, cbd->mcache_size, mdata_hash);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700622
623 /* No mcache and no verification means we have nothing special to do. */
Julius Werner723e3b12020-12-29 17:33:30 -0800624 if (!CONFIG(CBFS_VERIFICATION) || !mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700625 return CB_SUCCESS;
626
Julius Werner723e3b12020-12-29 17:33:30 -0800627 /* Verification only: use cbfs_walk() without a walker() function to just run through
628 the CBFS once, will return NOT_FOUND by default. */
629 cb_err_t err = cbfs_walk(&cbd->rdev, NULL, NULL, mdata_hash, 0);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700630 if (err == CB_CBFS_NOT_FOUND)
631 err = CB_SUCCESS;
632 return err;
633}
634
Julius Werner1e37c9c2019-12-11 17:09:39 -0800635const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro)
636{
637 static struct cbfs_boot_device ro;
638
Julius Werner723e3b12020-12-29 17:33:30 -0800639 /* Ensure we always init RO mcache, even if the first file is from the RW CBFS.
Julius Werner1e37c9c2019-12-11 17:09:39 -0800640 Otherwise it may not be available when needed in later stages. */
641 if (ENV_INITIAL_STAGE && !force_ro && !region_device_sz(&ro.rdev))
642 cbfs_get_boot_device(true);
643
644 if (!force_ro) {
645 const struct cbfs_boot_device *rw = vboot_get_cbfs_boot_device();
Julius Werner723e3b12020-12-29 17:33:30 -0800646 /* This will return NULL if vboot isn't enabled, didn't run yet or decided to
647 boot into recovery mode. */
Julius Werner1e37c9c2019-12-11 17:09:39 -0800648 if (rw)
649 return rw;
650 }
651
Julius Werner723e3b12020-12-29 17:33:30 -0800652 /* In rare cases post-RAM stages may run this before cbmem_initialize(), so we can't
653 lock in the result of find_mcache() on the first try and should keep trying every
654 time until an mcache is found. */
Julius Werner34cf0732020-12-08 14:21:43 -0800655 cbfs_boot_device_find_mcache(&ro, CBMEM_ID_CBFS_RO_MCACHE);
656
Julius Werner1e37c9c2019-12-11 17:09:39 -0800657 if (region_device_sz(&ro.rdev))
658 return &ro;
659
660 if (fmap_locate_area_as_rdev("COREBOOT", &ro.rdev))
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700661 die("Cannot locate primary CBFS");
Julius Werner1e37c9c2019-12-11 17:09:39 -0800662
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700663 if (ENV_INITIAL_STAGE) {
664 cb_err_t err = cbfs_init_boot_device(&ro, metadata_hash_get());
665 if (err == CB_CBFS_HASH_MISMATCH)
666 die("RO CBFS metadata hash verification failure");
667 else if (CONFIG(TOCTOU_SAFETY) && err == CB_CBFS_CACHE_FULL)
668 die("RO mcache overflow breaks TOCTOU safety!\n");
669 else if (err && err != CB_CBFS_CACHE_FULL)
670 die("RO CBFS initialization error: %d", err);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800671 }
672
673 return &ro;
674}
675
676#if !CONFIG(NO_CBFS_MCACHE)
677static void mcache_to_cbmem(const struct cbfs_boot_device *cbd, u32 cbmem_id)
678{
679 if (!cbd)
680 return;
681
682 size_t real_size = cbfs_mcache_real_size(cbd->mcache, cbd->mcache_size);
683 void *cbmem_mcache = cbmem_add(cbmem_id, real_size);
684 if (!cbmem_mcache) {
685 printk(BIOS_ERR, "ERROR: Cannot allocate CBMEM mcache %#x (%#zx bytes)!\n",
686 cbmem_id, real_size);
687 return;
688 }
689 memcpy(cbmem_mcache, cbd->mcache, real_size);
690}
691
692static void cbfs_mcache_migrate(int unused)
693{
694 mcache_to_cbmem(vboot_get_cbfs_boot_device(), CBMEM_ID_CBFS_RW_MCACHE);
695 mcache_to_cbmem(cbfs_get_boot_device(true), CBMEM_ID_CBFS_RO_MCACHE);
696}
697ROMSTAGE_CBMEM_INIT_HOOK(cbfs_mcache_migrate)
698#endif