blob: 93dbb681aa91f19a0e23df3a04880854c5891741 [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
Julius Werner9b1f3cc2020-12-30 17:30:12 -080021#if CBFS_CACHE_AVAILABLE
22struct mem_pool cbfs_cache = MEM_POOL_INIT(_cbfs_cache, REGION_SIZE(cbfs_cache));
23
24static void switch_to_postram_cache(int unused)
25{
26 if (_preram_cbfs_cache != _postram_cbfs_cache)
27 mem_pool_init(&cbfs_cache, _postram_cbfs_cache,
28 REGION_SIZE(postram_cbfs_cache));
29}
30ROMSTAGE_CBMEM_INIT_HOOK(switch_to_postram_cache);
31#endif
32
Julius Werner0d9072b2020-03-05 12:51:08 -080033cb_err_t cbfs_boot_lookup(const char *name, bool force_ro,
34 union cbfs_mdata *mdata, struct region_device *rdev)
Julius Werner1e37c9c2019-12-11 17:09:39 -080035{
Julius Werner0d9072b2020-03-05 12:51:08 -080036 const struct cbfs_boot_device *cbd = cbfs_get_boot_device(force_ro);
37 if (!cbd)
38 return CB_ERR;
39
40 size_t data_offset;
Julius Werner1e37c9c2019-12-11 17:09:39 -080041 cb_err_t err = CB_CBFS_CACHE_FULL;
Julius Werner34cf0732020-12-08 14:21:43 -080042 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size)
Julius Werner1e37c9c2019-12-11 17:09:39 -080043 err = cbfs_mcache_lookup(cbd->mcache, cbd->mcache_size,
Julius Werner723e3b12020-12-29 17:33:30 -080044 name, mdata, &data_offset);
Julius Wernerfdabf3f2020-05-06 17:06:35 -070045 if (err == CB_CBFS_CACHE_FULL) {
46 struct vb2_hash *metadata_hash = NULL;
47 if (CONFIG(TOCTOU_SAFETY)) {
48 if (ENV_SMM) /* Cannot provide TOCTOU safety for SMM */
49 dead_code();
Julius Werner34cf0732020-12-08 14:21:43 -080050 if (!cbd->mcache_size)
51 die("Cannot access CBFS TOCTOU-safely in " ENV_STRING " before CBMEM init!\n");
Julius Werner723e3b12020-12-29 17:33:30 -080052 /* We can only reach this for the RW CBFS -- an mcache overflow in the
53 RO CBFS would have been caught when building the mcache in cbfs_get
54 boot_device(). (Note that TOCTOU_SAFETY implies !NO_CBFS_MCACHE.) */
Julius Wernerfdabf3f2020-05-06 17:06:35 -070055 assert(cbd == vboot_get_cbfs_boot_device());
56 /* TODO: set metadata_hash to RW metadata hash here. */
57 }
Julius Werner723e3b12020-12-29 17:33:30 -080058 err = cbfs_lookup(&cbd->rdev, name, mdata, &data_offset, metadata_hash);
Julius Wernerfdabf3f2020-05-06 17:06:35 -070059 }
Julius Werner0d9072b2020-03-05 12:51:08 -080060
Julius Werner723e3b12020-12-29 17:33:30 -080061 if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && !force_ro && err == CB_CBFS_NOT_FOUND) {
62 printk(BIOS_INFO, "CBFS: Fall back to RO region for %s\n", name);
Julius Werner0d9072b2020-03-05 12:51:08 -080063 return cbfs_boot_lookup(name, true, mdata, rdev);
64 }
Julius Werner0247fcf2020-12-03 12:41:00 -080065 if (err) {
66 if (err == CB_CBFS_NOT_FOUND)
67 printk(BIOS_WARNING, "CBFS: '%s' not found.\n", name);
68 else if (err == CB_CBFS_HASH_MISMATCH)
69 printk(BIOS_ERR, "CBFS ERROR: metadata hash mismatch!\n");
70 else
Julius Werner723e3b12020-12-29 17:33:30 -080071 printk(BIOS_ERR, "CBFS ERROR: error %d when looking up '%s'\n",
Julius Werner0247fcf2020-12-03 12:41:00 -080072 err, name);
Julius Werner0d9072b2020-03-05 12:51:08 -080073 return err;
Julius Werner0247fcf2020-12-03 12:41:00 -080074 }
Julius Werner0d9072b2020-03-05 12:51:08 -080075
76 if (rdev_chain(rdev, &cbd->rdev, data_offset, be32toh(mdata->h.len)))
77 return CB_ERR;
78
Arthur Heymans27545df2021-02-26 14:22:47 +010079 if (tspi_measure_cbfs_hook(rdev, name, be32toh(mdata->h.type))) {
80 printk(BIOS_ERR, "CBFS ERROR: error when measuring '%s'\n", name);
81 }
Julius Werner0d9072b2020-03-05 12:51:08 -080082
83 return CB_SUCCESS;
Julius Werner1e37c9c2019-12-11 17:09:39 -080084}
85
Aaron Durbin37a5d152015-09-17 16:09:30 -050086int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
Aaron Durbin899d13d2015-05-15 23:39:23 -050087{
Julius Werner0d9072b2020-03-05 12:51:08 -080088 if (cbfs_boot_lookup(name, false, &fh->mdata, &fh->data))
Julius Werner1cd013b2019-12-11 16:50:02 -080089 return -1;
90
91 size_t msize = be32toh(fh->mdata.h.offset);
Julius Werner723e3b12020-12-29 17:33:30 -080092 if (rdev_chain(&fh->metadata, &addrspace_32bit.rdev, (uintptr_t)&fh->mdata, msize))
Julius Werner1cd013b2019-12-11 16:50:02 -080093 return -1;
Julius Werner0d9072b2020-03-05 12:51:08 -080094
Julius Werner1cd013b2019-12-11 16:50:02 -080095 if (type) {
96 if (!*type)
97 *type = be32toh(fh->mdata.h.type);
98 else if (*type != be32toh(fh->mdata.h.type))
99 return -1;
Wim Vervoorn114e2e82019-11-05 14:09:16 +0100100 }
101
Julius Werner1cd013b2019-12-11 16:50:02 -0800102 return 0;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500103}
104
Julius Werner11075fc2020-12-29 17:51:04 -0800105void *_cbfs_map(const char *name, size_t *size_out, bool force_ro)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500106{
Julius Wernerd17ce412020-10-01 18:25:49 -0700107 struct region_device rdev;
108 union cbfs_mdata mdata;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500109
Julius Werner9d0cc2a2020-01-22 18:00:18 -0800110 if (cbfs_boot_lookup(name, force_ro, &mdata, &rdev))
Aaron Durbin899d13d2015-05-15 23:39:23 -0500111 return NULL;
112
Julius Werner834b3ec2020-03-04 16:52:08 -0800113 if (size_out != NULL)
Julius Wernerd17ce412020-10-01 18:25:49 -0700114 *size_out = region_device_sz(&rdev);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500115
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800116 uint32_t compression = CBFS_COMPRESS_NONE;
117 const struct cbfs_file_attr_compression *attr = cbfs_find_attr(&mdata,
118 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*attr));
119 if (attr)
120 compression = be32toh(attr->compression);
121
122 if (compression == CBFS_COMPRESS_NONE)
123 return rdev_mmap_full(&rdev);
124
125 if (!CBFS_CACHE_AVAILABLE) {
126 ERROR("Cannot map compressed file %s on x86\n", mdata.h.filename);
127 return NULL;
128 }
129
130 size_t size = be32toh(attr->decompressed_size);
131 void *buf = mem_pool_alloc(&cbfs_cache, size);
132 if (!buf) {
133 ERROR("CBFS cache out of memory when mapping %s\n", mdata.h.filename);
134 return NULL;
135 }
136
137 size = cbfs_load_and_decompress(&rdev, 0, region_device_sz(&rdev), buf, size,
138 compression);
139 if (!size)
140 return NULL;
141
142 if (size_out != NULL)
143 *size_out = size;
144 return buf;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500145}
146
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800147void cbfs_unmap(void *mapping)
Julius Werner834b3ec2020-03-04 16:52:08 -0800148{
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800149 /*
150 * This is save to call with mappings that weren't allocated in the cache (e.g. x86
151 * direct mappings) -- mem_pool_free() just does nothing for addresses it doesn't
152 * recognize. This hardcodes the assumption that if platforms implement an rdev_mmap()
153 * that requires a free() for the boot_device, they need to implement it via the
154 * cbfs_cache mem_pool.
155 */
156 if (CBFS_CACHE_AVAILABLE)
157 mem_pool_free(&cbfs_cache, mapping);
Julius Werner834b3ec2020-03-04 16:52:08 -0800158}
159
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800160int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100161 const char *name, uint32_t *type)
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800162{
163 struct region_device rdev;
Bill XIEbad08c22020-02-13 11:11:35 +0800164 int ret = 0;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800165 if (fmap_locate_area_as_rdev(region_name, &rdev)) {
Julius Werner723e3b12020-12-29 17:33:30 -0800166 LOG("%s region not found while looking for %s\n", region_name, name);
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800167 return -1;
168 }
169
Julius Werner0d9072b2020-03-05 12:51:08 -0800170 uint32_t dummy_type = 0;
171 if (!type)
172 type = &dummy_type;
173
Bill XIEbad08c22020-02-13 11:11:35 +0800174 ret = cbfs_locate(fh, &rdev, name, type);
175 if (!ret)
Julius Werner0d9072b2020-03-05 12:51:08 -0800176 if (tspi_measure_cbfs_hook(&rdev, name, *type))
Arthur Heymans27545df2021-02-26 14:22:47 +0100177 LOG("error measuring %s in region %s\n", name, region_name);
Bill XIEbad08c22020-02-13 11:11:35 +0800178 return ret;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800179}
180
Aaron Durbina85febc2020-05-15 15:09:10 -0600181static inline bool fsps_env(void)
182{
183 /* FSP-S is assumed to be loaded in ramstage. */
184 if (ENV_RAMSTAGE)
185 return true;
186 return false;
187}
188
Aaron Durbinecbfa992020-05-15 17:01:58 -0600189static inline bool fspm_env(void)
190{
191 /* FSP-M is assumed to be loaded in romstage. */
192 if (ENV_ROMSTAGE)
193 return true;
194 return false;
195}
196
Aaron Durbina121f952020-05-26 15:48:10 -0600197static inline bool cbfs_lz4_enabled(void)
198{
Aaron Durbina85febc2020-05-15 15:09:10 -0600199 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZ4))
200 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600201 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZ4))
202 return true;
Aaron Durbina85febc2020-05-15 15:09:10 -0600203
Aaron Durbina121f952020-05-26 15:48:10 -0600204 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES))
205 return false;
206
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800207 if (ENV_SMM)
208 return false;
209
Aaron Durbina121f952020-05-26 15:48:10 -0600210 return true;
211}
212
213static inline bool cbfs_lzma_enabled(void)
214{
Aaron Durbina85febc2020-05-15 15:09:10 -0600215 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZMA))
216 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600217 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA))
218 return true;
Aaron Durbina121f952020-05-26 15:48:10 -0600219 /* We assume here romstage and postcar are never compressed. */
220 if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE)
221 return false;
222 if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE))
223 return false;
Julius Werner723e3b12020-12-29 17:33:30 -0800224 if ((ENV_ROMSTAGE || ENV_POSTCAR) && !CONFIG(COMPRESS_RAMSTAGE))
Aaron Durbina121f952020-05-26 15:48:10 -0600225 return false;
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800226 if (ENV_SMM)
227 return false;
Aaron Durbina121f952020-05-26 15:48:10 -0600228 return true;
229}
230
Julius Werner723e3b12020-12-29 17:33:30 -0800231size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset, size_t in_size,
232 void *buffer, size_t buffer_size, uint32_t compression)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500233{
Julius Werner09f29212015-09-29 13:51:35 -0700234 size_t out_size;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600235 void *map;
Julius Werner09f29212015-09-29 13:51:35 -0700236
237 switch (compression) {
238 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700239 if (buffer_size < in_size)
240 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700241 if (rdev_readat(rdev, buffer, offset, in_size) != in_size)
242 return 0;
243 return in_size;
244
245 case CBFS_COMPRESS_LZ4:
Aaron Durbina121f952020-05-26 15:48:10 -0600246 if (!cbfs_lz4_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700247 return 0;
248
Julius Werner723e3b12020-12-29 17:33:30 -0800249 /* cbfs_stage_load_and_decompress() takes care of in-place LZ4 decompression by
250 setting up the rdev to be in memory. */
Aaron Durbin84f394e2020-05-26 16:16:42 -0600251 map = rdev_mmap(rdev, offset, in_size);
252 if (map == NULL)
Julius Werner09f29212015-09-29 13:51:35 -0700253 return 0;
254
255 timestamp_add_now(TS_START_ULZ4F);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600256 out_size = ulz4fn(map, in_size, buffer, buffer_size);
Julius Werner09f29212015-09-29 13:51:35 -0700257 timestamp_add_now(TS_END_ULZ4F);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600258
259 rdev_munmap(rdev, map);
260
Julius Werner09f29212015-09-29 13:51:35 -0700261 return out_size;
262
263 case CBFS_COMPRESS_LZMA:
Aaron Durbina121f952020-05-26 15:48:10 -0600264 if (!cbfs_lzma_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700265 return 0;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600266 map = rdev_mmap(rdev, offset, in_size);
Julius Werner09f29212015-09-29 13:51:35 -0700267 if (map == NULL)
268 return 0;
269
270 /* Note: timestamp not useful for memory-mapped media (x86) */
271 timestamp_add_now(TS_START_ULZMA);
272 out_size = ulzman(map, in_size, buffer, buffer_size);
273 timestamp_add_now(TS_END_ULZMA);
274
275 rdev_munmap(rdev, map);
276
277 return out_size;
278
279 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500280 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700281 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500282}
283
Julius Werner723e3b12020-12-29 17:33:30 -0800284static size_t cbfs_stage_load_and_decompress(const struct region_device *rdev, size_t offset,
285 size_t in_size, void *buffer, size_t buffer_size, uint32_t compression)
Aaron Durbin84f394e2020-05-26 16:16:42 -0600286{
287 struct region_device rdev_src;
288
289 if (compression == CBFS_COMPRESS_LZ4) {
290 if (!cbfs_lz4_enabled())
291 return 0;
Julius Werner723e3b12020-12-29 17:33:30 -0800292 /* Load the compressed image to the end of the available memory area for
293 in-place decompression. It is the responsibility of the caller to ensure that
294 buffer_size is large enough (see compression.h, guaranteed by cbfstool for
295 stages). */
Aaron Durbin84f394e2020-05-26 16:16:42 -0600296 void *compr_start = buffer + buffer_size - in_size;
297 if (rdev_readat(rdev, compr_start, offset, in_size) != in_size)
298 return 0;
299 /* Create a region device backed by memory. */
Julius Werner723e3b12020-12-29 17:33:30 -0800300 rdev_chain(&rdev_src, &addrspace_32bit.rdev, (uintptr_t)compr_start, in_size);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600301
Julius Werner723e3b12020-12-29 17:33:30 -0800302 return cbfs_load_and_decompress(&rdev_src, 0, in_size, buffer, buffer_size,
303 compression);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600304 }
305
306 /* All other algorithms can use the generic implementation. */
Julius Werner723e3b12020-12-29 17:33:30 -0800307 return cbfs_load_and_decompress(rdev, offset, in_size, buffer, buffer_size,
308 compression);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600309}
310
Stefan Reinauer800379f2010-03-01 08:34:19 +0000311static inline int tohex4(unsigned int c)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000312{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800313 return (c <= 9) ? (c + '0') : (c - 10 + 'a');
Patrick Georgib203c2f2009-08-20 14:48:03 +0000314}
315
Martin Rotha616a4b2020-01-21 09:28:40 -0700316static void tohex8(unsigned int val, char *dest)
317{
318 dest[0] = tohex4((val >> 4) & 0xf);
319 dest[1] = tohex4(val & 0xf);
320}
321
Lee Leahyb2d834a2017-03-08 16:52:22 -0800322static void tohex16(unsigned int val, char *dest)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000323{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100324 dest[0] = tohex4(val >> 12);
325 dest[1] = tohex4((val >> 8) & 0xf);
326 dest[2] = tohex4((val >> 4) & 0xf);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800327 dest[3] = tohex4(val & 0xf);
Patrick Georgib203c2f2009-08-20 14:48:03 +0000328}
329
Aaron Durbin899d13d2015-05-15 23:39:23 -0500330void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
Peter Stuge483b7bb2009-04-14 07:40:01 +0000331{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800332 char name[17] = "pciXXXX,XXXX.rom";
Peter Stuge483b7bb2009-04-14 07:40:01 +0000333
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100334 tohex16(vendor, name + 3);
335 tohex16(device, name + 8);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000336
Julius Werner834b3ec2020-03-04 16:52:08 -0800337 return cbfs_map(name, NULL);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000338}
339
Martin Rotha616a4b2020-01-21 09:28:40 -0700340void *cbfs_boot_map_optionrom_revision(uint16_t vendor, uint16_t device, uint8_t rev)
341{
342 char name[20] = "pciXXXX,XXXX,XX.rom";
343
344 tohex16(vendor, name + 3);
345 tohex16(device, name + 8);
346 tohex8(rev, name + 13);
347
Julius Werner834b3ec2020-03-04 16:52:08 -0800348 return cbfs_map(name, NULL);
Martin Rotha616a4b2020-01-21 09:28:40 -0700349}
350
Julius Werner11075fc2020-12-29 17:51:04 -0800351size_t _cbfs_load(const char *name, void *buf, size_t buf_size, bool force_ro)
Julius Wernerf975e552016-08-19 15:43:06 -0700352{
Julius Wernerd17ce412020-10-01 18:25:49 -0700353 struct region_device rdev;
354 union cbfs_mdata mdata;
Julius Wernerf975e552016-08-19 15:43:06 -0700355
Julius Werner9d0cc2a2020-01-22 18:00:18 -0800356 if (cbfs_boot_lookup(name, force_ro, &mdata, &rdev))
Julius Wernerf975e552016-08-19 15:43:06 -0700357 return 0;
358
Julius Wernerd17ce412020-10-01 18:25:49 -0700359 uint32_t compression = CBFS_COMPRESS_NONE;
360 const struct cbfs_file_attr_compression *attr = cbfs_find_attr(&mdata,
361 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*attr));
362 if (attr) {
363 compression = be32toh(attr->compression);
364 if (buf_size < be32toh(attr->decompressed_size))
365 return 0;
366 }
Julius Wernerf975e552016-08-19 15:43:06 -0700367
Julius Wernerd17ce412020-10-01 18:25:49 -0700368 return cbfs_load_and_decompress(&rdev, 0, region_device_sz(&rdev),
369 buf, buf_size, compression);
Julius Wernerf975e552016-08-19 15:43:06 -0700370}
371
Aaron Durbin899d13d2015-05-15 23:39:23 -0500372int cbfs_prog_stage_load(struct prog *pstage)
373{
374 struct cbfs_stage stage;
375 uint8_t *load;
376 void *entry;
377 size_t fsize;
378 size_t foffset;
Aaron Durbin37a5d152015-09-17 16:09:30 -0500379 const struct region_device *fh = prog_rdev(pstage);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800380
Aaron Durbin899d13d2015-05-15 23:39:23 -0500381 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
Julius Wernerb29bd27b2015-12-03 11:29:12 -0800382 return -1;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500383
384 fsize = region_device_sz(fh);
385 fsize -= sizeof(stage);
386 foffset = 0;
387 foffset += sizeof(stage);
388
Aaron Durbin0b418bd2020-10-08 14:56:03 -0600389 /* cbfs_stage fields are written in little endian despite the other
390 cbfs data types being encoded in big endian. */
391 stage.compression = read_le32(&stage.compression);
392 stage.entry = read_le64(&stage.entry);
393 stage.load = read_le64(&stage.load);
394 stage.len = read_le32(&stage.len);
395 stage.memlen = read_le32(&stage.memlen);
396
Aaron Durbin899d13d2015-05-15 23:39:23 -0500397 assert(fsize == stage.len);
398
Aaron Durbin899d13d2015-05-15 23:39:23 -0500399 load = (void *)(uintptr_t)stage.load;
400 entry = (void *)(uintptr_t)stage.entry;
401
Aaron Durbined253c82015-10-07 17:22:42 -0500402 /* Hacky way to not load programs over read only media. The stages
403 * that would hit this path initialize themselves. */
Julius Werner21a40532020-04-21 16:03:53 -0700404 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) &&
405 !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
Aaron Durbined253c82015-10-07 17:22:42 -0500406 void *mapping = rdev_mmap(fh, foffset, fsize);
407 rdev_munmap(fh, mapping);
408 if (mapping == load)
409 goto out;
410 }
411
Aaron Durbin84f394e2020-05-26 16:16:42 -0600412 fsize = cbfs_stage_load_and_decompress(fh, foffset, fsize, load,
Julius Werner723e3b12020-12-29 17:33:30 -0800413 stage.memlen, stage.compression);
Julius Werner09f29212015-09-29 13:51:35 -0700414 if (!fsize)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500415 return -1;
416
417 /* Clear area not covered by file. */
418 memset(&load[fsize], 0, stage.memlen - fsize);
419
Aaron Durbin096f4572016-03-31 13:49:00 -0500420 prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL);
Aaron Durbined253c82015-10-07 17:22:42 -0500421
422out:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500423 prog_set_area(pstage, load, stage.memlen);
424 prog_set_entry(pstage, entry, NULL);
425
426 return 0;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800427}
Aaron Durbin6d720f32015-12-08 17:00:23 -0600428
Julius Werner1e37c9c2019-12-11 17:09:39 -0800429void cbfs_boot_device_find_mcache(struct cbfs_boot_device *cbd, uint32_t id)
Aaron Durbin6d720f32015-12-08 17:00:23 -0600430{
Julius Werner1e37c9c2019-12-11 17:09:39 -0800431 if (CONFIG(NO_CBFS_MCACHE) || ENV_SMM)
432 return;
433
Julius Werner34cf0732020-12-08 14:21:43 -0800434 if (cbd->mcache_size)
435 return;
436
Julius Werner1e37c9c2019-12-11 17:09:39 -0800437 const struct cbmem_entry *entry;
438 if (cbmem_possibly_online() &&
439 (entry = cbmem_entry_find(id))) {
440 cbd->mcache = cbmem_entry_start(entry);
441 cbd->mcache_size = cbmem_entry_size(entry);
442 } else if (ENV_ROMSTAGE_OR_BEFORE) {
443 u8 *boundary = _ecbfs_mcache - REGION_SIZE(cbfs_mcache) *
444 CONFIG_CBFS_MCACHE_RW_PERCENTAGE / 100;
Julius Werner723e3b12020-12-29 17:33:30 -0800445 boundary = (u8 *)ALIGN_DOWN((uintptr_t)boundary, CBFS_MCACHE_ALIGNMENT);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800446 if (id == CBMEM_ID_CBFS_RO_MCACHE) {
447 cbd->mcache = _cbfs_mcache;
448 cbd->mcache_size = boundary - _cbfs_mcache;
449 } else if (id == CBMEM_ID_CBFS_RW_MCACHE) {
450 cbd->mcache = boundary;
451 cbd->mcache_size = _ecbfs_mcache - boundary;
452 }
453 }
Aaron Durbin6d720f32015-12-08 17:00:23 -0600454}
Julius Werner1e37c9c2019-12-11 17:09:39 -0800455
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700456cb_err_t cbfs_init_boot_device(const struct cbfs_boot_device *cbd,
Julius Werner723e3b12020-12-29 17:33:30 -0800457 struct vb2_hash *mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700458{
459 /* If we have an mcache, mcache_build() will also check mdata hash. */
460 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size > 0)
Julius Werner723e3b12020-12-29 17:33:30 -0800461 return cbfs_mcache_build(&cbd->rdev, cbd->mcache, cbd->mcache_size, mdata_hash);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700462
463 /* No mcache and no verification means we have nothing special to do. */
Julius Werner723e3b12020-12-29 17:33:30 -0800464 if (!CONFIG(CBFS_VERIFICATION) || !mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700465 return CB_SUCCESS;
466
Julius Werner723e3b12020-12-29 17:33:30 -0800467 /* Verification only: use cbfs_walk() without a walker() function to just run through
468 the CBFS once, will return NOT_FOUND by default. */
469 cb_err_t err = cbfs_walk(&cbd->rdev, NULL, NULL, mdata_hash, 0);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700470 if (err == CB_CBFS_NOT_FOUND)
471 err = CB_SUCCESS;
472 return err;
473}
474
Julius Werner1e37c9c2019-12-11 17:09:39 -0800475const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro)
476{
477 static struct cbfs_boot_device ro;
478
Julius Werner723e3b12020-12-29 17:33:30 -0800479 /* Ensure we always init RO mcache, even if the first file is from the RW CBFS.
Julius Werner1e37c9c2019-12-11 17:09:39 -0800480 Otherwise it may not be available when needed in later stages. */
481 if (ENV_INITIAL_STAGE && !force_ro && !region_device_sz(&ro.rdev))
482 cbfs_get_boot_device(true);
483
484 if (!force_ro) {
485 const struct cbfs_boot_device *rw = vboot_get_cbfs_boot_device();
Julius Werner723e3b12020-12-29 17:33:30 -0800486 /* This will return NULL if vboot isn't enabled, didn't run yet or decided to
487 boot into recovery mode. */
Julius Werner1e37c9c2019-12-11 17:09:39 -0800488 if (rw)
489 return rw;
490 }
491
Julius Werner723e3b12020-12-29 17:33:30 -0800492 /* In rare cases post-RAM stages may run this before cbmem_initialize(), so we can't
493 lock in the result of find_mcache() on the first try and should keep trying every
494 time until an mcache is found. */
Julius Werner34cf0732020-12-08 14:21:43 -0800495 cbfs_boot_device_find_mcache(&ro, CBMEM_ID_CBFS_RO_MCACHE);
496
Julius Werner1e37c9c2019-12-11 17:09:39 -0800497 if (region_device_sz(&ro.rdev))
498 return &ro;
499
500 if (fmap_locate_area_as_rdev("COREBOOT", &ro.rdev))
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700501 die("Cannot locate primary CBFS");
Julius Werner1e37c9c2019-12-11 17:09:39 -0800502
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700503 if (ENV_INITIAL_STAGE) {
504 cb_err_t err = cbfs_init_boot_device(&ro, metadata_hash_get());
505 if (err == CB_CBFS_HASH_MISMATCH)
506 die("RO CBFS metadata hash verification failure");
507 else if (CONFIG(TOCTOU_SAFETY) && err == CB_CBFS_CACHE_FULL)
508 die("RO mcache overflow breaks TOCTOU safety!\n");
509 else if (err && err != CB_CBFS_CACHE_FULL)
510 die("RO CBFS initialization error: %d", err);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800511 }
512
513 return &ro;
514}
515
516#if !CONFIG(NO_CBFS_MCACHE)
517static void mcache_to_cbmem(const struct cbfs_boot_device *cbd, u32 cbmem_id)
518{
519 if (!cbd)
520 return;
521
522 size_t real_size = cbfs_mcache_real_size(cbd->mcache, cbd->mcache_size);
523 void *cbmem_mcache = cbmem_add(cbmem_id, real_size);
524 if (!cbmem_mcache) {
525 printk(BIOS_ERR, "ERROR: Cannot allocate CBMEM mcache %#x (%#zx bytes)!\n",
526 cbmem_id, real_size);
527 return;
528 }
529 memcpy(cbmem_mcache, cbd->mcache, real_size);
530}
531
532static void cbfs_mcache_migrate(int unused)
533{
534 mcache_to_cbmem(vboot_get_cbfs_boot_device(), CBMEM_ID_CBFS_RW_MCACHE);
535 mcache_to_cbmem(cbfs_get_boot_device(true), CBMEM_ID_CBFS_RO_MCACHE);
536}
537ROMSTAGE_CBMEM_INIT_HOOK(cbfs_mcache_migrate)
538#endif