blob: 1c79b65d6b7ce37903bb3a52e9ee9d32987ed39c [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>
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>
15#include <security/vboot/vboot_common.h>
16#include <stdlib.h>
17#include <string.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050018#include <symbols.h>
Julius Werner09f29212015-09-29 13:51:35 -070019#include <timestamp.h>
Patrick Georgi58a150a2016-05-02 17:22:29 +080020
Raul E Rangel3d7b9842021-10-08 13:10:38 -060021#if ENV_STAGE_HAS_DATA_SECTION
Raul E Rangel5ac82dc2021-07-23 16:43:18 -060022struct mem_pool cbfs_cache =
Raul E Rangel6938f352021-07-23 16:43:18 -060023 MEM_POOL_INIT(_cbfs_cache, REGION_SIZE(cbfs_cache), CONFIG_CBFS_CACHE_ALIGN);
Raul E Rangel3d7b9842021-10-08 13:10:38 -060024#else
Raul E Rangel5ac82dc2021-07-23 16:43:18 -060025struct mem_pool cbfs_cache = MEM_POOL_INIT(NULL, 0, 0);
Raul E Rangel3d7b9842021-10-08 13:10:38 -060026#endif
Julius Werner9b1f3cc2020-12-30 17:30:12 -080027
28static void switch_to_postram_cache(int unused)
29{
30 if (_preram_cbfs_cache != _postram_cbfs_cache)
Raul E Rangel5ac82dc2021-07-23 16:43:18 -060031 mem_pool_init(&cbfs_cache, _postram_cbfs_cache, REGION_SIZE(postram_cbfs_cache),
Raul E Rangel6938f352021-07-23 16:43:18 -060032 CONFIG_CBFS_CACHE_ALIGN);
Julius Werner9b1f3cc2020-12-30 17:30:12 -080033}
34ROMSTAGE_CBMEM_INIT_HOOK(switch_to_postram_cache);
Julius Werner9b1f3cc2020-12-30 17:30:12 -080035
Julius Werner0d9072b2020-03-05 12:51:08 -080036cb_err_t cbfs_boot_lookup(const char *name, bool force_ro,
37 union cbfs_mdata *mdata, struct region_device *rdev)
Julius Werner1e37c9c2019-12-11 17:09:39 -080038{
Julius Werner0d9072b2020-03-05 12:51:08 -080039 const struct cbfs_boot_device *cbd = cbfs_get_boot_device(force_ro);
40 if (!cbd)
41 return CB_ERR;
42
43 size_t data_offset;
Julius Werner1e37c9c2019-12-11 17:09:39 -080044 cb_err_t err = CB_CBFS_CACHE_FULL;
Julius Werner34cf0732020-12-08 14:21:43 -080045 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size)
Julius Werner1e37c9c2019-12-11 17:09:39 -080046 err = cbfs_mcache_lookup(cbd->mcache, cbd->mcache_size,
Julius Werner723e3b12020-12-29 17:33:30 -080047 name, mdata, &data_offset);
Julius Wernerfdabf3f2020-05-06 17:06:35 -070048 if (err == CB_CBFS_CACHE_FULL) {
49 struct vb2_hash *metadata_hash = NULL;
50 if (CONFIG(TOCTOU_SAFETY)) {
51 if (ENV_SMM) /* Cannot provide TOCTOU safety for SMM */
52 dead_code();
Julius Werner34cf0732020-12-08 14:21:43 -080053 if (!cbd->mcache_size)
54 die("Cannot access CBFS TOCTOU-safely in " ENV_STRING " before CBMEM init!\n");
Julius Werner723e3b12020-12-29 17:33:30 -080055 /* We can only reach this for the RW CBFS -- an mcache overflow in the
56 RO CBFS would have been caught when building the mcache in cbfs_get
57 boot_device(). (Note that TOCTOU_SAFETY implies !NO_CBFS_MCACHE.) */
Julius Wernerfdabf3f2020-05-06 17:06:35 -070058 assert(cbd == vboot_get_cbfs_boot_device());
59 /* TODO: set metadata_hash to RW metadata hash here. */
60 }
Julius Werner723e3b12020-12-29 17:33:30 -080061 err = cbfs_lookup(&cbd->rdev, name, mdata, &data_offset, metadata_hash);
Julius Wernerfdabf3f2020-05-06 17:06:35 -070062 }
Julius Werner0d9072b2020-03-05 12:51:08 -080063
Julius Werner723e3b12020-12-29 17:33:30 -080064 if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && !force_ro && err == CB_CBFS_NOT_FOUND) {
65 printk(BIOS_INFO, "CBFS: Fall back to RO region for %s\n", name);
Julius Werner0d9072b2020-03-05 12:51:08 -080066 return cbfs_boot_lookup(name, true, mdata, rdev);
67 }
Julius Werner0247fcf2020-12-03 12:41:00 -080068 if (err) {
69 if (err == CB_CBFS_NOT_FOUND)
70 printk(BIOS_WARNING, "CBFS: '%s' not found.\n", name);
71 else if (err == CB_CBFS_HASH_MISMATCH)
72 printk(BIOS_ERR, "CBFS ERROR: metadata hash mismatch!\n");
73 else
Julius Werner723e3b12020-12-29 17:33:30 -080074 printk(BIOS_ERR, "CBFS ERROR: error %d when looking up '%s'\n",
Julius Werner0247fcf2020-12-03 12:41:00 -080075 err, name);
Julius Werner0d9072b2020-03-05 12:51:08 -080076 return err;
Julius Werner0247fcf2020-12-03 12:41:00 -080077 }
Julius Werner0d9072b2020-03-05 12:51:08 -080078
79 if (rdev_chain(rdev, &cbd->rdev, data_offset, be32toh(mdata->h.len)))
80 return CB_ERR;
81
Arthur Heymans27545df2021-02-26 14:22:47 +010082 if (tspi_measure_cbfs_hook(rdev, name, be32toh(mdata->h.type))) {
83 printk(BIOS_ERR, "CBFS ERROR: error when measuring '%s'\n", name);
84 }
Julius Werner0d9072b2020-03-05 12:51:08 -080085
86 return CB_SUCCESS;
Julius Werner1e37c9c2019-12-11 17:09:39 -080087}
88
Aaron Durbin37a5d152015-09-17 16:09:30 -050089int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
Aaron Durbin899d13d2015-05-15 23:39:23 -050090{
Julius Werner0d9072b2020-03-05 12:51:08 -080091 if (cbfs_boot_lookup(name, false, &fh->mdata, &fh->data))
Julius Werner1cd013b2019-12-11 16:50:02 -080092 return -1;
93
94 size_t msize = be32toh(fh->mdata.h.offset);
Julius Wernerc8931972021-04-16 16:48:32 -070095 if (rdev_chain_mem(&fh->metadata, &fh->mdata, msize))
Julius Werner1cd013b2019-12-11 16:50:02 -080096 return -1;
Julius Werner0d9072b2020-03-05 12:51:08 -080097
Julius Werner1cd013b2019-12-11 16:50:02 -080098 if (type) {
99 if (!*type)
100 *type = be32toh(fh->mdata.h.type);
101 else if (*type != be32toh(fh->mdata.h.type))
102 return -1;
Wim Vervoorn114e2e82019-11-05 14:09:16 +0100103 }
104
Julius Werner1cd013b2019-12-11 16:50:02 -0800105 return 0;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500106}
107
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800108void cbfs_unmap(void *mapping)
Julius Werner834b3ec2020-03-04 16:52:08 -0800109{
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800110 /*
111 * This is save to call with mappings that weren't allocated in the cache (e.g. x86
112 * direct mappings) -- mem_pool_free() just does nothing for addresses it doesn't
113 * recognize. This hardcodes the assumption that if platforms implement an rdev_mmap()
114 * that requires a free() for the boot_device, they need to implement it via the
115 * cbfs_cache mem_pool.
116 */
Raul E Rangel3d7b9842021-10-08 13:10:38 -0600117 mem_pool_free(&cbfs_cache, mapping);
Julius Werner834b3ec2020-03-04 16:52:08 -0800118}
119
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800120int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100121 const char *name, uint32_t *type)
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800122{
123 struct region_device rdev;
Bill XIEbad08c22020-02-13 11:11:35 +0800124 int ret = 0;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800125 if (fmap_locate_area_as_rdev(region_name, &rdev)) {
Julius Werner723e3b12020-12-29 17:33:30 -0800126 LOG("%s region not found while looking for %s\n", region_name, name);
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800127 return -1;
128 }
129
Julius Werner0d9072b2020-03-05 12:51:08 -0800130 uint32_t dummy_type = 0;
131 if (!type)
132 type = &dummy_type;
133
Bill XIEbad08c22020-02-13 11:11:35 +0800134 ret = cbfs_locate(fh, &rdev, name, type);
135 if (!ret)
Julius Werner0d9072b2020-03-05 12:51:08 -0800136 if (tspi_measure_cbfs_hook(&rdev, name, *type))
Arthur Heymans27545df2021-02-26 14:22:47 +0100137 LOG("error measuring %s in region %s\n", name, region_name);
Bill XIEbad08c22020-02-13 11:11:35 +0800138 return ret;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800139}
140
Aaron Durbina85febc2020-05-15 15:09:10 -0600141static inline bool fsps_env(void)
142{
143 /* FSP-S is assumed to be loaded in ramstage. */
144 if (ENV_RAMSTAGE)
145 return true;
146 return false;
147}
148
Aaron Durbinecbfa992020-05-15 17:01:58 -0600149static inline bool fspm_env(void)
150{
151 /* FSP-M is assumed to be loaded in romstage. */
152 if (ENV_ROMSTAGE)
153 return true;
154 return false;
155}
156
Aaron Durbina121f952020-05-26 15:48:10 -0600157static inline bool cbfs_lz4_enabled(void)
158{
Aaron Durbina85febc2020-05-15 15:09:10 -0600159 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZ4))
160 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600161 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZ4))
162 return true;
Aaron Durbina85febc2020-05-15 15:09:10 -0600163
Aaron Durbina121f952020-05-26 15:48:10 -0600164 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES))
165 return false;
166
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800167 if (ENV_SMM)
168 return false;
169
Aaron Durbina121f952020-05-26 15:48:10 -0600170 return true;
171}
172
173static inline bool cbfs_lzma_enabled(void)
174{
Aaron Durbina85febc2020-05-15 15:09:10 -0600175 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZMA))
176 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600177 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA))
178 return true;
Aaron Durbina121f952020-05-26 15:48:10 -0600179 /* We assume here romstage and postcar are never compressed. */
180 if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE)
181 return false;
182 if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE))
183 return false;
Julius Werner723e3b12020-12-29 17:33:30 -0800184 if ((ENV_ROMSTAGE || ENV_POSTCAR) && !CONFIG(COMPRESS_RAMSTAGE))
Aaron Durbina121f952020-05-26 15:48:10 -0600185 return false;
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800186 if (ENV_SMM)
187 return false;
Aaron Durbina121f952020-05-26 15:48:10 -0600188 return true;
189}
190
Julius Wernerfccf1222021-04-02 15:58:05 -0700191static inline bool cbfs_file_hash_mismatch(const void *buffer, size_t size,
192 const struct vb2_hash *file_hash)
193{
194 /* Avoid linking hash functions when verification is disabled. */
195 if (!CONFIG(CBFS_VERIFICATION))
196 return false;
197
198 /* If there is no file hash, always count that as a mismatch. */
199 if (file_hash && vb2_hash_verify(buffer, size, file_hash) == VB2_SUCCESS)
200 return false;
201
202 printk(BIOS_CRIT, "CBFS file hash mismatch!\n");
203 return true;
204}
205
206static size_t cbfs_load_and_decompress(const struct region_device *rdev, void *buffer,
207 size_t buffer_size, uint32_t compression,
208 const struct vb2_hash *file_hash)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500209{
Julius Wernereca99af2021-03-10 18:49:37 -0800210 size_t in_size = region_device_sz(rdev);
Julius Wernerfccf1222021-04-02 15:58:05 -0700211 size_t out_size = 0;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600212 void *map;
Julius Werner09f29212015-09-29 13:51:35 -0700213
Julius Wernereca99af2021-03-10 18:49:37 -0800214 DEBUG("Decompressing %zu bytes to %p with algo %d\n", in_size, buffer, compression);
Julius Werner7778cf22021-01-05 18:54:19 -0800215
Julius Werner09f29212015-09-29 13:51:35 -0700216 switch (compression) {
217 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700218 if (buffer_size < in_size)
219 return 0;
Julius Wernereca99af2021-03-10 18:49:37 -0800220 if (rdev_readat(rdev, buffer, 0, in_size) != in_size)
Julius Werner09f29212015-09-29 13:51:35 -0700221 return 0;
Julius Wernerfccf1222021-04-02 15:58:05 -0700222 if (cbfs_file_hash_mismatch(buffer, in_size, file_hash))
223 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700224 return in_size;
225
226 case CBFS_COMPRESS_LZ4:
Aaron Durbina121f952020-05-26 15:48:10 -0600227 if (!cbfs_lz4_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700228 return 0;
229
Julius Wernereca99af2021-03-10 18:49:37 -0800230 /* cbfs_prog_stage_load() takes care of in-place LZ4 decompression by
Julius Werner723e3b12020-12-29 17:33:30 -0800231 setting up the rdev to be in memory. */
Julius Wernereca99af2021-03-10 18:49:37 -0800232 map = rdev_mmap_full(rdev);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600233 if (map == NULL)
Julius Werner09f29212015-09-29 13:51:35 -0700234 return 0;
235
Julius Wernerfccf1222021-04-02 15:58:05 -0700236 if (!cbfs_file_hash_mismatch(map, in_size, file_hash)) {
237 timestamp_add_now(TS_START_ULZ4F);
238 out_size = ulz4fn(map, in_size, buffer, buffer_size);
239 timestamp_add_now(TS_END_ULZ4F);
240 }
Aaron Durbin84f394e2020-05-26 16:16:42 -0600241
242 rdev_munmap(rdev, map);
243
Julius Werner09f29212015-09-29 13:51:35 -0700244 return out_size;
245
246 case CBFS_COMPRESS_LZMA:
Aaron Durbina121f952020-05-26 15:48:10 -0600247 if (!cbfs_lzma_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700248 return 0;
Julius Wernereca99af2021-03-10 18:49:37 -0800249 map = rdev_mmap_full(rdev);
Julius Werner09f29212015-09-29 13:51:35 -0700250 if (map == NULL)
251 return 0;
252
Julius Wernerfccf1222021-04-02 15:58:05 -0700253 if (!cbfs_file_hash_mismatch(map, in_size, file_hash)) {
254 /* Note: timestamp not useful for memory-mapped media (x86) */
255 timestamp_add_now(TS_START_ULZMA);
256 out_size = ulzman(map, in_size, buffer, buffer_size);
257 timestamp_add_now(TS_END_ULZMA);
258 }
Julius Werner09f29212015-09-29 13:51:35 -0700259
260 rdev_munmap(rdev, map);
261
262 return out_size;
263
264 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500265 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700266 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500267}
268
Julius Werner7778cf22021-01-05 18:54:19 -0800269void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg,
270 size_t *size_out, bool force_ro, enum cbfs_type *type)
Julius Wernerf975e552016-08-19 15:43:06 -0700271{
Julius Wernerd17ce412020-10-01 18:25:49 -0700272 struct region_device rdev;
273 union cbfs_mdata mdata;
Julius Werner7778cf22021-01-05 18:54:19 -0800274 void *loc;
275
276 DEBUG("%s(name='%s', alloc=%p(%p), force_ro=%s, type=%d)\n", __func__, name, allocator,
277 arg, force_ro ? "true" : "false", type ? *type : -1);
Julius Wernerf975e552016-08-19 15:43:06 -0700278
Julius Werner9d0cc2a2020-01-22 18:00:18 -0800279 if (cbfs_boot_lookup(name, force_ro, &mdata, &rdev))
Julius Werner7778cf22021-01-05 18:54:19 -0800280 return NULL;
Julius Wernerf975e552016-08-19 15:43:06 -0700281
Julius Werner7778cf22021-01-05 18:54:19 -0800282 if (type) {
283 const enum cbfs_type real_type = be32toh(mdata.h.type);
284 if (*type == CBFS_TYPE_QUERY)
285 *type = real_type;
286 else if (*type != real_type) {
287 ERROR("'%s' type mismatch (is %u, expected %u)\n",
288 mdata.h.filename, real_type, *type);
289 return NULL;
290 }
Julius Wernerd17ce412020-10-01 18:25:49 -0700291 }
Julius Wernerf975e552016-08-19 15:43:06 -0700292
Julius Werner7778cf22021-01-05 18:54:19 -0800293 size_t size = region_device_sz(&rdev);
294 uint32_t compression = CBFS_COMPRESS_NONE;
295 const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(&mdata,
296 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr));
297 if (cattr) {
298 compression = be32toh(cattr->compression);
299 size = be32toh(cattr->decompressed_size);
300 }
301
302 if (size_out)
303 *size_out = size;
304
Julius Wernerfccf1222021-04-02 15:58:05 -0700305 const struct vb2_hash *file_hash = NULL;
306 if (CONFIG(CBFS_VERIFICATION))
307 file_hash = cbfs_file_hash(&mdata);
308
Julius Werner7778cf22021-01-05 18:54:19 -0800309 /* allocator == NULL means do a cbfs_map() */
310 if (allocator) {
311 loc = allocator(arg, size, &mdata);
312 } else if (compression == CBFS_COMPRESS_NONE) {
Julius Wernerfccf1222021-04-02 15:58:05 -0700313 void *mapping = rdev_mmap_full(&rdev);
Raul E Rangel3f41d322021-07-23 15:23:12 -0600314
315 if (!mapping)
Julius Wernerfccf1222021-04-02 15:58:05 -0700316 return NULL;
Raul E Rangel3f41d322021-07-23 15:23:12 -0600317
318 if (cbfs_file_hash_mismatch(mapping, size, file_hash)) {
319 rdev_munmap(&rdev, mapping);
320 return NULL;
321 }
322
Julius Wernerfccf1222021-04-02 15:58:05 -0700323 return mapping;
Raul E Rangel3d7b9842021-10-08 13:10:38 -0600324 } else if (!cbfs_cache.size) {
325 /*
326 * In order to use the cbfs_cache you need to add a CBFS_CACHE to your
327 * memlayout. For stages that don't have .data sections (x86 pre-RAM),
328 * it is not possible to add a CBFS_CACHE.
329 */
330 ERROR("Cannot map compressed file %s without cbfs_cache\n", mdata.h.filename);
Julius Werner7778cf22021-01-05 18:54:19 -0800331 return NULL;
332 } else {
333 loc = mem_pool_alloc(&cbfs_cache, size);
334 }
335
336 if (!loc) {
337 ERROR("'%s' allocation failure\n", mdata.h.filename);
338 return NULL;
339 }
340
Julius Wernerfccf1222021-04-02 15:58:05 -0700341 size = cbfs_load_and_decompress(&rdev, loc, size, compression, file_hash);
Julius Werner7778cf22021-01-05 18:54:19 -0800342 if (!size)
343 return NULL;
344
345 return loc;
346}
347
Julius Werner4676ec52021-03-10 16:52:14 -0800348void *_cbfs_default_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
Julius Werner7778cf22021-01-05 18:54:19 -0800349{
350 struct _cbfs_default_allocator_arg *darg = arg;
351 if (size > darg->buf_size)
352 return NULL;
353 return darg->buf;
354}
355
Julius Werner4676ec52021-03-10 16:52:14 -0800356void *_cbfs_cbmem_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
Julius Werner7778cf22021-01-05 18:54:19 -0800357{
358 return cbmem_add((uintptr_t)arg, size);
Julius Wernerf975e552016-08-19 15:43:06 -0700359}
360
Julius Werner1de87082020-12-23 17:38:11 -0800361cb_err_t cbfs_prog_stage_load(struct prog *pstage)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500362{
Julius Werner1de87082020-12-23 17:38:11 -0800363 union cbfs_mdata mdata;
364 struct region_device rdev;
Julius Werner1de87082020-12-23 17:38:11 -0800365 cb_err_t err;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800366
Julius Werner1de87082020-12-23 17:38:11 -0800367 prog_locate_hook(pstage);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500368
Julius Werner1de87082020-12-23 17:38:11 -0800369 if ((err = cbfs_boot_lookup(prog_name(pstage), false, &mdata, &rdev)))
370 return err;
371
372 assert(be32toh(mdata.h.type) == CBFS_TYPE_STAGE);
373 pstage->cbfs_type = CBFS_TYPE_STAGE;
374
Julius Werner81dc20e2020-10-15 17:37:57 -0700375 enum cbfs_compression compression = CBFS_COMPRESS_NONE;
376 const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(&mdata,
377 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr));
378 if (cattr)
379 compression = be32toh(cattr->compression);
Julius Werner1de87082020-12-23 17:38:11 -0800380
Julius Werner81dc20e2020-10-15 17:37:57 -0700381 const struct cbfs_file_attr_stageheader *sattr = cbfs_find_attr(&mdata,
382 CBFS_FILE_ATTR_TAG_STAGEHEADER, sizeof(*sattr));
383 if (!sattr)
384 return CB_ERR;
385 prog_set_area(pstage, (void *)(uintptr_t)be64toh(sattr->loadaddr),
386 be32toh(sattr->memlen));
387 prog_set_entry(pstage, prog_start(pstage) +
388 be32toh(sattr->entry_offset), NULL);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500389
Julius Wernerfccf1222021-04-02 15:58:05 -0700390 const struct vb2_hash *file_hash = NULL;
391 if (CONFIG(CBFS_VERIFICATION))
392 file_hash = cbfs_file_hash(&mdata);
393
Aaron Durbined253c82015-10-07 17:22:42 -0500394 /* Hacky way to not load programs over read only media. The stages
395 * that would hit this path initialize themselves. */
Julius Werner21a40532020-04-21 16:03:53 -0700396 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) &&
397 !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
Julius Werner81dc20e2020-10-15 17:37:57 -0700398 void *mapping = rdev_mmap_full(&rdev);
Julius Werner1de87082020-12-23 17:38:11 -0800399 rdev_munmap(&rdev, mapping);
Julius Wernerfccf1222021-04-02 15:58:05 -0700400 if (cbfs_file_hash_mismatch(mapping, region_device_sz(&rdev), file_hash))
401 return CB_CBFS_HASH_MISMATCH;
Julius Werner81dc20e2020-10-15 17:37:57 -0700402 if (mapping == prog_start(pstage))
403 return CB_SUCCESS;
Aaron Durbined253c82015-10-07 17:22:42 -0500404 }
405
Julius Wernereca99af2021-03-10 18:49:37 -0800406 /* LZ4 stages can be decompressed in-place to save mapping scratch space. Load the
407 compressed data to the end of the buffer and point &rdev to that memory location. */
408 if (cbfs_lz4_enabled() && compression == CBFS_COMPRESS_LZ4) {
409 size_t in_size = region_device_sz(&rdev);
410 void *compr_start = prog_start(pstage) + prog_size(pstage) - in_size;
411 if (rdev_readat(&rdev, compr_start, 0, in_size) != in_size)
412 return CB_ERR;
Julius Wernerc8931972021-04-16 16:48:32 -0700413 rdev_chain_mem(&rdev, compr_start, in_size);
Julius Wernereca99af2021-03-10 18:49:37 -0800414 }
415
416 size_t fsize = cbfs_load_and_decompress(&rdev, prog_start(pstage), prog_size(pstage),
Julius Wernerfccf1222021-04-02 15:58:05 -0700417 compression, file_hash);
Julius Werner09f29212015-09-29 13:51:35 -0700418 if (!fsize)
Julius Werner1de87082020-12-23 17:38:11 -0800419 return CB_ERR;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500420
421 /* Clear area not covered by file. */
Julius Werner81dc20e2020-10-15 17:37:57 -0700422 memset(prog_start(pstage) + fsize, 0, prog_size(pstage) - fsize);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500423
Julius Werner81dc20e2020-10-15 17:37:57 -0700424 prog_segment_loaded((uintptr_t)prog_start(pstage), prog_size(pstage),
425 SEG_FINAL);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500426
Julius Werner1de87082020-12-23 17:38:11 -0800427 return CB_SUCCESS;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800428}
Aaron Durbin6d720f32015-12-08 17:00:23 -0600429
Julius Werner1e37c9c2019-12-11 17:09:39 -0800430void cbfs_boot_device_find_mcache(struct cbfs_boot_device *cbd, uint32_t id)
Aaron Durbin6d720f32015-12-08 17:00:23 -0600431{
Julius Werner1e37c9c2019-12-11 17:09:39 -0800432 if (CONFIG(NO_CBFS_MCACHE) || ENV_SMM)
433 return;
434
Julius Werner34cf0732020-12-08 14:21:43 -0800435 if (cbd->mcache_size)
436 return;
437
Julius Werner1e37c9c2019-12-11 17:09:39 -0800438 const struct cbmem_entry *entry;
439 if (cbmem_possibly_online() &&
440 (entry = cbmem_entry_find(id))) {
441 cbd->mcache = cbmem_entry_start(entry);
442 cbd->mcache_size = cbmem_entry_size(entry);
443 } else if (ENV_ROMSTAGE_OR_BEFORE) {
444 u8 *boundary = _ecbfs_mcache - REGION_SIZE(cbfs_mcache) *
445 CONFIG_CBFS_MCACHE_RW_PERCENTAGE / 100;
Julius Werner723e3b12020-12-29 17:33:30 -0800446 boundary = (u8 *)ALIGN_DOWN((uintptr_t)boundary, CBFS_MCACHE_ALIGNMENT);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800447 if (id == CBMEM_ID_CBFS_RO_MCACHE) {
448 cbd->mcache = _cbfs_mcache;
449 cbd->mcache_size = boundary - _cbfs_mcache;
450 } else if (id == CBMEM_ID_CBFS_RW_MCACHE) {
451 cbd->mcache = boundary;
452 cbd->mcache_size = _ecbfs_mcache - boundary;
453 }
454 }
Aaron Durbin6d720f32015-12-08 17:00:23 -0600455}
Julius Werner1e37c9c2019-12-11 17:09:39 -0800456
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700457cb_err_t cbfs_init_boot_device(const struct cbfs_boot_device *cbd,
Julius Werner723e3b12020-12-29 17:33:30 -0800458 struct vb2_hash *mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700459{
460 /* If we have an mcache, mcache_build() will also check mdata hash. */
461 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size > 0)
Julius Werner723e3b12020-12-29 17:33:30 -0800462 return cbfs_mcache_build(&cbd->rdev, cbd->mcache, cbd->mcache_size, mdata_hash);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700463
464 /* No mcache and no verification means we have nothing special to do. */
Julius Werner723e3b12020-12-29 17:33:30 -0800465 if (!CONFIG(CBFS_VERIFICATION) || !mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700466 return CB_SUCCESS;
467
Julius Werner723e3b12020-12-29 17:33:30 -0800468 /* Verification only: use cbfs_walk() without a walker() function to just run through
469 the CBFS once, will return NOT_FOUND by default. */
470 cb_err_t err = cbfs_walk(&cbd->rdev, NULL, NULL, mdata_hash, 0);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700471 if (err == CB_CBFS_NOT_FOUND)
472 err = CB_SUCCESS;
473 return err;
474}
475
Julius Werner1e37c9c2019-12-11 17:09:39 -0800476const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro)
477{
478 static struct cbfs_boot_device ro;
479
Julius Werner723e3b12020-12-29 17:33:30 -0800480 /* Ensure we always init RO mcache, even if the first file is from the RW CBFS.
Julius Werner1e37c9c2019-12-11 17:09:39 -0800481 Otherwise it may not be available when needed in later stages. */
482 if (ENV_INITIAL_STAGE && !force_ro && !region_device_sz(&ro.rdev))
483 cbfs_get_boot_device(true);
484
485 if (!force_ro) {
486 const struct cbfs_boot_device *rw = vboot_get_cbfs_boot_device();
Julius Werner723e3b12020-12-29 17:33:30 -0800487 /* This will return NULL if vboot isn't enabled, didn't run yet or decided to
488 boot into recovery mode. */
Julius Werner1e37c9c2019-12-11 17:09:39 -0800489 if (rw)
490 return rw;
491 }
492
Julius Werner723e3b12020-12-29 17:33:30 -0800493 /* In rare cases post-RAM stages may run this before cbmem_initialize(), so we can't
494 lock in the result of find_mcache() on the first try and should keep trying every
495 time until an mcache is found. */
Julius Werner34cf0732020-12-08 14:21:43 -0800496 cbfs_boot_device_find_mcache(&ro, CBMEM_ID_CBFS_RO_MCACHE);
497
Julius Werner1e37c9c2019-12-11 17:09:39 -0800498 if (region_device_sz(&ro.rdev))
499 return &ro;
500
501 if (fmap_locate_area_as_rdev("COREBOOT", &ro.rdev))
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700502 die("Cannot locate primary CBFS");
Julius Werner1e37c9c2019-12-11 17:09:39 -0800503
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700504 if (ENV_INITIAL_STAGE) {
505 cb_err_t err = cbfs_init_boot_device(&ro, metadata_hash_get());
506 if (err == CB_CBFS_HASH_MISMATCH)
507 die("RO CBFS metadata hash verification failure");
508 else if (CONFIG(TOCTOU_SAFETY) && err == CB_CBFS_CACHE_FULL)
509 die("RO mcache overflow breaks TOCTOU safety!\n");
510 else if (err && err != CB_CBFS_CACHE_FULL)
511 die("RO CBFS initialization error: %d", err);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800512 }
513
514 return &ro;
515}
516
517#if !CONFIG(NO_CBFS_MCACHE)
518static void mcache_to_cbmem(const struct cbfs_boot_device *cbd, u32 cbmem_id)
519{
520 if (!cbd)
521 return;
522
523 size_t real_size = cbfs_mcache_real_size(cbd->mcache, cbd->mcache_size);
524 void *cbmem_mcache = cbmem_add(cbmem_id, real_size);
525 if (!cbmem_mcache) {
526 printk(BIOS_ERR, "ERROR: Cannot allocate CBMEM mcache %#x (%#zx bytes)!\n",
527 cbmem_id, real_size);
528 return;
529 }
530 memcpy(cbmem_mcache, cbd->mcache, real_size);
531}
532
533static void cbfs_mcache_migrate(int unused)
534{
535 mcache_to_cbmem(vboot_get_cbfs_boot_device(), CBMEM_ID_CBFS_RW_MCACHE);
536 mcache_to_cbmem(cbfs_get_boot_device(true), CBMEM_ID_CBFS_RO_MCACHE);
537}
538ROMSTAGE_CBMEM_INIT_HOOK(cbfs_mcache_migrate)
539#endif