blob: be2ab726627bd7cdb019be9cdd384ce4570503b4 [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 Werner0d9072b2020-03-05 12:51:08 -080021cb_err_t cbfs_boot_lookup(const char *name, bool force_ro,
22 union cbfs_mdata *mdata, struct region_device *rdev)
Julius Werner1e37c9c2019-12-11 17:09:39 -080023{
Julius Werner0d9072b2020-03-05 12:51:08 -080024 const struct cbfs_boot_device *cbd = cbfs_get_boot_device(force_ro);
25 if (!cbd)
26 return CB_ERR;
27
28 size_t data_offset;
Julius Werner1e37c9c2019-12-11 17:09:39 -080029 cb_err_t err = CB_CBFS_CACHE_FULL;
Julius Werner34cf0732020-12-08 14:21:43 -080030 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size)
Julius Werner1e37c9c2019-12-11 17:09:39 -080031 err = cbfs_mcache_lookup(cbd->mcache, cbd->mcache_size,
Julius Werner723e3b12020-12-29 17:33:30 -080032 name, mdata, &data_offset);
Julius Wernerfdabf3f2020-05-06 17:06:35 -070033 if (err == CB_CBFS_CACHE_FULL) {
34 struct vb2_hash *metadata_hash = NULL;
35 if (CONFIG(TOCTOU_SAFETY)) {
36 if (ENV_SMM) /* Cannot provide TOCTOU safety for SMM */
37 dead_code();
Julius Werner34cf0732020-12-08 14:21:43 -080038 if (!cbd->mcache_size)
39 die("Cannot access CBFS TOCTOU-safely in " ENV_STRING " before CBMEM init!\n");
Julius Werner723e3b12020-12-29 17:33:30 -080040 /* We can only reach this for the RW CBFS -- an mcache overflow in the
41 RO CBFS would have been caught when building the mcache in cbfs_get
42 boot_device(). (Note that TOCTOU_SAFETY implies !NO_CBFS_MCACHE.) */
Julius Wernerfdabf3f2020-05-06 17:06:35 -070043 assert(cbd == vboot_get_cbfs_boot_device());
44 /* TODO: set metadata_hash to RW metadata hash here. */
45 }
Julius Werner723e3b12020-12-29 17:33:30 -080046 err = cbfs_lookup(&cbd->rdev, name, mdata, &data_offset, metadata_hash);
Julius Wernerfdabf3f2020-05-06 17:06:35 -070047 }
Julius Werner0d9072b2020-03-05 12:51:08 -080048
Julius Werner723e3b12020-12-29 17:33:30 -080049 if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && !force_ro && err == CB_CBFS_NOT_FOUND) {
50 printk(BIOS_INFO, "CBFS: Fall back to RO region for %s\n", name);
Julius Werner0d9072b2020-03-05 12:51:08 -080051 return cbfs_boot_lookup(name, true, mdata, rdev);
52 }
Julius Werner0247fcf2020-12-03 12:41:00 -080053 if (err) {
54 if (err == CB_CBFS_NOT_FOUND)
55 printk(BIOS_WARNING, "CBFS: '%s' not found.\n", name);
56 else if (err == CB_CBFS_HASH_MISMATCH)
57 printk(BIOS_ERR, "CBFS ERROR: metadata hash mismatch!\n");
58 else
Julius Werner723e3b12020-12-29 17:33:30 -080059 printk(BIOS_ERR, "CBFS ERROR: error %d when looking up '%s'\n",
Julius Werner0247fcf2020-12-03 12:41:00 -080060 err, name);
Julius Werner0d9072b2020-03-05 12:51:08 -080061 return err;
Julius Werner0247fcf2020-12-03 12:41:00 -080062 }
Julius Werner0d9072b2020-03-05 12:51:08 -080063
64 if (rdev_chain(rdev, &cbd->rdev, data_offset, be32toh(mdata->h.len)))
65 return CB_ERR;
66
Arthur Heymans27545df2021-02-26 14:22:47 +010067 if (tspi_measure_cbfs_hook(rdev, name, be32toh(mdata->h.type))) {
68 printk(BIOS_ERR, "CBFS ERROR: error when measuring '%s'\n", name);
69 }
Julius Werner0d9072b2020-03-05 12:51:08 -080070
71 return CB_SUCCESS;
Julius Werner1e37c9c2019-12-11 17:09:39 -080072}
73
Aaron Durbin37a5d152015-09-17 16:09:30 -050074int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
Aaron Durbin899d13d2015-05-15 23:39:23 -050075{
Julius Werner0d9072b2020-03-05 12:51:08 -080076 if (cbfs_boot_lookup(name, false, &fh->mdata, &fh->data))
Julius Werner1cd013b2019-12-11 16:50:02 -080077 return -1;
78
79 size_t msize = be32toh(fh->mdata.h.offset);
Julius Werner723e3b12020-12-29 17:33:30 -080080 if (rdev_chain(&fh->metadata, &addrspace_32bit.rdev, (uintptr_t)&fh->mdata, msize))
Julius Werner1cd013b2019-12-11 16:50:02 -080081 return -1;
Julius Werner0d9072b2020-03-05 12:51:08 -080082
Julius Werner1cd013b2019-12-11 16:50:02 -080083 if (type) {
84 if (!*type)
85 *type = be32toh(fh->mdata.h.type);
86 else if (*type != be32toh(fh->mdata.h.type))
87 return -1;
Wim Vervoorn114e2e82019-11-05 14:09:16 +010088 }
89
Julius Werner1cd013b2019-12-11 16:50:02 -080090 return 0;
Aaron Durbin899d13d2015-05-15 23:39:23 -050091}
92
Julius Werner9d0cc2a2020-01-22 18:00:18 -080093static void *_cbfs_map(const char *name, size_t *size_out, bool force_ro)
Aaron Durbin899d13d2015-05-15 23:39:23 -050094{
Julius Wernerd17ce412020-10-01 18:25:49 -070095 struct region_device rdev;
96 union cbfs_mdata mdata;
Aaron Durbin899d13d2015-05-15 23:39:23 -050097
Julius Werner9d0cc2a2020-01-22 18:00:18 -080098 if (cbfs_boot_lookup(name, force_ro, &mdata, &rdev))
Aaron Durbin899d13d2015-05-15 23:39:23 -050099 return NULL;
100
Julius Werner834b3ec2020-03-04 16:52:08 -0800101 if (size_out != NULL)
Julius Wernerd17ce412020-10-01 18:25:49 -0700102 *size_out = region_device_sz(&rdev);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500103
Julius Wernerd17ce412020-10-01 18:25:49 -0700104 return rdev_mmap_full(&rdev);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500105}
106
Julius Werner9d0cc2a2020-01-22 18:00:18 -0800107void *cbfs_map(const char *name, size_t *size_out)
108{
109 return _cbfs_map(name, size_out, false);
110}
111
112void *cbfs_ro_map(const char *name, size_t *size_out)
113{
114 return _cbfs_map(name, size_out, true);
115}
116
Julius Werner834b3ec2020-03-04 16:52:08 -0800117int cbfs_unmap(void *mapping)
118{
Julius Werner723e3b12020-12-29 17:33:30 -0800119 /* This works because munmap() only works on the root rdev and never cares about which
120 chained subregion something was mapped from. */
Julius Werner834b3ec2020-03-04 16:52:08 -0800121 return rdev_munmap(boot_device_ro(), mapping);
122}
123
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800124int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100125 const char *name, uint32_t *type)
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800126{
127 struct region_device rdev;
Bill XIEbad08c22020-02-13 11:11:35 +0800128 int ret = 0;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800129 if (fmap_locate_area_as_rdev(region_name, &rdev)) {
Julius Werner723e3b12020-12-29 17:33:30 -0800130 LOG("%s region not found while looking for %s\n", region_name, name);
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800131 return -1;
132 }
133
Julius Werner0d9072b2020-03-05 12:51:08 -0800134 uint32_t dummy_type = 0;
135 if (!type)
136 type = &dummy_type;
137
Bill XIEbad08c22020-02-13 11:11:35 +0800138 ret = cbfs_locate(fh, &rdev, name, type);
139 if (!ret)
Julius Werner0d9072b2020-03-05 12:51:08 -0800140 if (tspi_measure_cbfs_hook(&rdev, name, *type))
Arthur Heymans27545df2021-02-26 14:22:47 +0100141 LOG("error measuring %s in region %s\n", name, region_name);
Bill XIEbad08c22020-02-13 11:11:35 +0800142 return ret;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800143}
144
Aaron Durbina85febc2020-05-15 15:09:10 -0600145static inline bool fsps_env(void)
146{
147 /* FSP-S is assumed to be loaded in ramstage. */
148 if (ENV_RAMSTAGE)
149 return true;
150 return false;
151}
152
Aaron Durbinecbfa992020-05-15 17:01:58 -0600153static inline bool fspm_env(void)
154{
155 /* FSP-M is assumed to be loaded in romstage. */
156 if (ENV_ROMSTAGE)
157 return true;
158 return false;
159}
160
Aaron Durbina121f952020-05-26 15:48:10 -0600161static inline bool cbfs_lz4_enabled(void)
162{
Aaron Durbina85febc2020-05-15 15:09:10 -0600163 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZ4))
164 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600165 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZ4))
166 return true;
Aaron Durbina85febc2020-05-15 15:09:10 -0600167
Aaron Durbina121f952020-05-26 15:48:10 -0600168 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES))
169 return false;
170
171 return true;
172}
173
174static inline bool cbfs_lzma_enabled(void)
175{
Aaron Durbina85febc2020-05-15 15:09:10 -0600176 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZMA))
177 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600178 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA))
179 return true;
Aaron Durbina121f952020-05-26 15:48:10 -0600180 /* We assume here romstage and postcar are never compressed. */
181 if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE)
182 return false;
183 if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE))
184 return false;
Julius Werner723e3b12020-12-29 17:33:30 -0800185 if ((ENV_ROMSTAGE || ENV_POSTCAR) && !CONFIG(COMPRESS_RAMSTAGE))
Aaron Durbina121f952020-05-26 15:48:10 -0600186 return false;
187 return true;
188}
189
Julius Werner723e3b12020-12-29 17:33:30 -0800190size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset, size_t in_size,
191 void *buffer, size_t buffer_size, uint32_t compression)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500192{
Julius Werner09f29212015-09-29 13:51:35 -0700193 size_t out_size;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600194 void *map;
Julius Werner09f29212015-09-29 13:51:35 -0700195
196 switch (compression) {
197 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700198 if (buffer_size < in_size)
199 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700200 if (rdev_readat(rdev, buffer, offset, in_size) != in_size)
201 return 0;
202 return in_size;
203
204 case CBFS_COMPRESS_LZ4:
Aaron Durbina121f952020-05-26 15:48:10 -0600205 if (!cbfs_lz4_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700206 return 0;
207
Julius Werner723e3b12020-12-29 17:33:30 -0800208 /* cbfs_stage_load_and_decompress() takes care of in-place LZ4 decompression by
209 setting up the rdev to be in memory. */
Aaron Durbin84f394e2020-05-26 16:16:42 -0600210 map = rdev_mmap(rdev, offset, in_size);
211 if (map == NULL)
Julius Werner09f29212015-09-29 13:51:35 -0700212 return 0;
213
214 timestamp_add_now(TS_START_ULZ4F);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600215 out_size = ulz4fn(map, in_size, buffer, buffer_size);
Julius Werner09f29212015-09-29 13:51:35 -0700216 timestamp_add_now(TS_END_ULZ4F);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600217
218 rdev_munmap(rdev, map);
219
Julius Werner09f29212015-09-29 13:51:35 -0700220 return out_size;
221
222 case CBFS_COMPRESS_LZMA:
Aaron Durbina121f952020-05-26 15:48:10 -0600223 if (!cbfs_lzma_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700224 return 0;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600225 map = rdev_mmap(rdev, offset, in_size);
Julius Werner09f29212015-09-29 13:51:35 -0700226 if (map == NULL)
227 return 0;
228
229 /* Note: timestamp not useful for memory-mapped media (x86) */
230 timestamp_add_now(TS_START_ULZMA);
231 out_size = ulzman(map, in_size, buffer, buffer_size);
232 timestamp_add_now(TS_END_ULZMA);
233
234 rdev_munmap(rdev, map);
235
236 return out_size;
237
238 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500239 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700240 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500241}
242
Julius Werner723e3b12020-12-29 17:33:30 -0800243static size_t cbfs_stage_load_and_decompress(const struct region_device *rdev, size_t offset,
244 size_t in_size, void *buffer, size_t buffer_size, uint32_t compression)
Aaron Durbin84f394e2020-05-26 16:16:42 -0600245{
246 struct region_device rdev_src;
247
248 if (compression == CBFS_COMPRESS_LZ4) {
249 if (!cbfs_lz4_enabled())
250 return 0;
Julius Werner723e3b12020-12-29 17:33:30 -0800251 /* Load the compressed image to the end of the available memory area for
252 in-place decompression. It is the responsibility of the caller to ensure that
253 buffer_size is large enough (see compression.h, guaranteed by cbfstool for
254 stages). */
Aaron Durbin84f394e2020-05-26 16:16:42 -0600255 void *compr_start = buffer + buffer_size - in_size;
256 if (rdev_readat(rdev, compr_start, offset, in_size) != in_size)
257 return 0;
258 /* Create a region device backed by memory. */
Julius Werner723e3b12020-12-29 17:33:30 -0800259 rdev_chain(&rdev_src, &addrspace_32bit.rdev, (uintptr_t)compr_start, in_size);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600260
Julius Werner723e3b12020-12-29 17:33:30 -0800261 return cbfs_load_and_decompress(&rdev_src, 0, in_size, buffer, buffer_size,
262 compression);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600263 }
264
265 /* All other algorithms can use the generic implementation. */
Julius Werner723e3b12020-12-29 17:33:30 -0800266 return cbfs_load_and_decompress(rdev, offset, in_size, buffer, buffer_size,
267 compression);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600268}
269
Stefan Reinauer800379f2010-03-01 08:34:19 +0000270static inline int tohex4(unsigned int c)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000271{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800272 return (c <= 9) ? (c + '0') : (c - 10 + 'a');
Patrick Georgib203c2f2009-08-20 14:48:03 +0000273}
274
Martin Rotha616a4b2020-01-21 09:28:40 -0700275static void tohex8(unsigned int val, char *dest)
276{
277 dest[0] = tohex4((val >> 4) & 0xf);
278 dest[1] = tohex4(val & 0xf);
279}
280
Lee Leahyb2d834a2017-03-08 16:52:22 -0800281static void tohex16(unsigned int val, char *dest)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000282{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100283 dest[0] = tohex4(val >> 12);
284 dest[1] = tohex4((val >> 8) & 0xf);
285 dest[2] = tohex4((val >> 4) & 0xf);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800286 dest[3] = tohex4(val & 0xf);
Patrick Georgib203c2f2009-08-20 14:48:03 +0000287}
288
Aaron Durbin899d13d2015-05-15 23:39:23 -0500289void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
Peter Stuge483b7bb2009-04-14 07:40:01 +0000290{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800291 char name[17] = "pciXXXX,XXXX.rom";
Peter Stuge483b7bb2009-04-14 07:40:01 +0000292
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100293 tohex16(vendor, name + 3);
294 tohex16(device, name + 8);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000295
Julius Werner834b3ec2020-03-04 16:52:08 -0800296 return cbfs_map(name, NULL);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000297}
298
Martin Rotha616a4b2020-01-21 09:28:40 -0700299void *cbfs_boot_map_optionrom_revision(uint16_t vendor, uint16_t device, uint8_t rev)
300{
301 char name[20] = "pciXXXX,XXXX,XX.rom";
302
303 tohex16(vendor, name + 3);
304 tohex16(device, name + 8);
305 tohex8(rev, name + 13);
306
Julius Werner834b3ec2020-03-04 16:52:08 -0800307 return cbfs_map(name, NULL);
Martin Rotha616a4b2020-01-21 09:28:40 -0700308}
309
Julius Werner723e3b12020-12-29 17:33:30 -0800310static size_t _cbfs_load(const char *name, void *buf, size_t buf_size, bool force_ro)
Julius Wernerf975e552016-08-19 15:43:06 -0700311{
Julius Wernerd17ce412020-10-01 18:25:49 -0700312 struct region_device rdev;
313 union cbfs_mdata mdata;
Julius Wernerf975e552016-08-19 15:43:06 -0700314
Julius Werner9d0cc2a2020-01-22 18:00:18 -0800315 if (cbfs_boot_lookup(name, force_ro, &mdata, &rdev))
Julius Wernerf975e552016-08-19 15:43:06 -0700316 return 0;
317
Julius Wernerd17ce412020-10-01 18:25:49 -0700318 uint32_t compression = CBFS_COMPRESS_NONE;
319 const struct cbfs_file_attr_compression *attr = cbfs_find_attr(&mdata,
320 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*attr));
321 if (attr) {
322 compression = be32toh(attr->compression);
323 if (buf_size < be32toh(attr->decompressed_size))
324 return 0;
325 }
Julius Wernerf975e552016-08-19 15:43:06 -0700326
Julius Wernerd17ce412020-10-01 18:25:49 -0700327 return cbfs_load_and_decompress(&rdev, 0, region_device_sz(&rdev),
328 buf, buf_size, compression);
Julius Wernerf975e552016-08-19 15:43:06 -0700329}
330
Julius Werner9d0cc2a2020-01-22 18:00:18 -0800331size_t cbfs_load(const char *name, void *buf, size_t buf_size)
332{
333 return _cbfs_load(name, buf, buf_size, false);
334}
335
336size_t cbfs_ro_load(const char *name, void *buf, size_t buf_size)
337{
338 return _cbfs_load(name, buf, buf_size, true);
339}
340
Aaron Durbin899d13d2015-05-15 23:39:23 -0500341int cbfs_prog_stage_load(struct prog *pstage)
342{
343 struct cbfs_stage stage;
344 uint8_t *load;
345 void *entry;
346 size_t fsize;
347 size_t foffset;
Aaron Durbin37a5d152015-09-17 16:09:30 -0500348 const struct region_device *fh = prog_rdev(pstage);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800349
Aaron Durbin899d13d2015-05-15 23:39:23 -0500350 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
Julius Wernerb29bd27b2015-12-03 11:29:12 -0800351 return -1;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500352
353 fsize = region_device_sz(fh);
354 fsize -= sizeof(stage);
355 foffset = 0;
356 foffset += sizeof(stage);
357
Aaron Durbin0b418bd2020-10-08 14:56:03 -0600358 /* cbfs_stage fields are written in little endian despite the other
359 cbfs data types being encoded in big endian. */
360 stage.compression = read_le32(&stage.compression);
361 stage.entry = read_le64(&stage.entry);
362 stage.load = read_le64(&stage.load);
363 stage.len = read_le32(&stage.len);
364 stage.memlen = read_le32(&stage.memlen);
365
Aaron Durbin899d13d2015-05-15 23:39:23 -0500366 assert(fsize == stage.len);
367
Aaron Durbin899d13d2015-05-15 23:39:23 -0500368 load = (void *)(uintptr_t)stage.load;
369 entry = (void *)(uintptr_t)stage.entry;
370
Aaron Durbined253c82015-10-07 17:22:42 -0500371 /* Hacky way to not load programs over read only media. The stages
372 * that would hit this path initialize themselves. */
Julius Werner21a40532020-04-21 16:03:53 -0700373 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) &&
374 !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
Aaron Durbined253c82015-10-07 17:22:42 -0500375 void *mapping = rdev_mmap(fh, foffset, fsize);
376 rdev_munmap(fh, mapping);
377 if (mapping == load)
378 goto out;
379 }
380
Aaron Durbin84f394e2020-05-26 16:16:42 -0600381 fsize = cbfs_stage_load_and_decompress(fh, foffset, fsize, load,
Julius Werner723e3b12020-12-29 17:33:30 -0800382 stage.memlen, stage.compression);
Julius Werner09f29212015-09-29 13:51:35 -0700383 if (!fsize)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500384 return -1;
385
386 /* Clear area not covered by file. */
387 memset(&load[fsize], 0, stage.memlen - fsize);
388
Aaron Durbin096f4572016-03-31 13:49:00 -0500389 prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL);
Aaron Durbined253c82015-10-07 17:22:42 -0500390
391out:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500392 prog_set_area(pstage, load, stage.memlen);
393 prog_set_entry(pstage, entry, NULL);
394
395 return 0;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800396}
Aaron Durbin6d720f32015-12-08 17:00:23 -0600397
Julius Werner1e37c9c2019-12-11 17:09:39 -0800398void cbfs_boot_device_find_mcache(struct cbfs_boot_device *cbd, uint32_t id)
Aaron Durbin6d720f32015-12-08 17:00:23 -0600399{
Julius Werner1e37c9c2019-12-11 17:09:39 -0800400 if (CONFIG(NO_CBFS_MCACHE) || ENV_SMM)
401 return;
402
Julius Werner34cf0732020-12-08 14:21:43 -0800403 if (cbd->mcache_size)
404 return;
405
Julius Werner1e37c9c2019-12-11 17:09:39 -0800406 const struct cbmem_entry *entry;
407 if (cbmem_possibly_online() &&
408 (entry = cbmem_entry_find(id))) {
409 cbd->mcache = cbmem_entry_start(entry);
410 cbd->mcache_size = cbmem_entry_size(entry);
411 } else if (ENV_ROMSTAGE_OR_BEFORE) {
412 u8 *boundary = _ecbfs_mcache - REGION_SIZE(cbfs_mcache) *
413 CONFIG_CBFS_MCACHE_RW_PERCENTAGE / 100;
Julius Werner723e3b12020-12-29 17:33:30 -0800414 boundary = (u8 *)ALIGN_DOWN((uintptr_t)boundary, CBFS_MCACHE_ALIGNMENT);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800415 if (id == CBMEM_ID_CBFS_RO_MCACHE) {
416 cbd->mcache = _cbfs_mcache;
417 cbd->mcache_size = boundary - _cbfs_mcache;
418 } else if (id == CBMEM_ID_CBFS_RW_MCACHE) {
419 cbd->mcache = boundary;
420 cbd->mcache_size = _ecbfs_mcache - boundary;
421 }
422 }
Aaron Durbin6d720f32015-12-08 17:00:23 -0600423}
Julius Werner1e37c9c2019-12-11 17:09:39 -0800424
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700425cb_err_t cbfs_init_boot_device(const struct cbfs_boot_device *cbd,
Julius Werner723e3b12020-12-29 17:33:30 -0800426 struct vb2_hash *mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700427{
428 /* If we have an mcache, mcache_build() will also check mdata hash. */
429 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size > 0)
Julius Werner723e3b12020-12-29 17:33:30 -0800430 return cbfs_mcache_build(&cbd->rdev, cbd->mcache, cbd->mcache_size, mdata_hash);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700431
432 /* No mcache and no verification means we have nothing special to do. */
Julius Werner723e3b12020-12-29 17:33:30 -0800433 if (!CONFIG(CBFS_VERIFICATION) || !mdata_hash)
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700434 return CB_SUCCESS;
435
Julius Werner723e3b12020-12-29 17:33:30 -0800436 /* Verification only: use cbfs_walk() without a walker() function to just run through
437 the CBFS once, will return NOT_FOUND by default. */
438 cb_err_t err = cbfs_walk(&cbd->rdev, NULL, NULL, mdata_hash, 0);
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700439 if (err == CB_CBFS_NOT_FOUND)
440 err = CB_SUCCESS;
441 return err;
442}
443
Julius Werner1e37c9c2019-12-11 17:09:39 -0800444const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro)
445{
446 static struct cbfs_boot_device ro;
447
Julius Werner723e3b12020-12-29 17:33:30 -0800448 /* Ensure we always init RO mcache, even if the first file is from the RW CBFS.
Julius Werner1e37c9c2019-12-11 17:09:39 -0800449 Otherwise it may not be available when needed in later stages. */
450 if (ENV_INITIAL_STAGE && !force_ro && !region_device_sz(&ro.rdev))
451 cbfs_get_boot_device(true);
452
453 if (!force_ro) {
454 const struct cbfs_boot_device *rw = vboot_get_cbfs_boot_device();
Julius Werner723e3b12020-12-29 17:33:30 -0800455 /* This will return NULL if vboot isn't enabled, didn't run yet or decided to
456 boot into recovery mode. */
Julius Werner1e37c9c2019-12-11 17:09:39 -0800457 if (rw)
458 return rw;
459 }
460
Julius Werner723e3b12020-12-29 17:33:30 -0800461 /* In rare cases post-RAM stages may run this before cbmem_initialize(), so we can't
462 lock in the result of find_mcache() on the first try and should keep trying every
463 time until an mcache is found. */
Julius Werner34cf0732020-12-08 14:21:43 -0800464 cbfs_boot_device_find_mcache(&ro, CBMEM_ID_CBFS_RO_MCACHE);
465
Julius Werner1e37c9c2019-12-11 17:09:39 -0800466 if (region_device_sz(&ro.rdev))
467 return &ro;
468
469 if (fmap_locate_area_as_rdev("COREBOOT", &ro.rdev))
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700470 die("Cannot locate primary CBFS");
Julius Werner1e37c9c2019-12-11 17:09:39 -0800471
Julius Wernerfdabf3f2020-05-06 17:06:35 -0700472 if (ENV_INITIAL_STAGE) {
473 cb_err_t err = cbfs_init_boot_device(&ro, metadata_hash_get());
474 if (err == CB_CBFS_HASH_MISMATCH)
475 die("RO CBFS metadata hash verification failure");
476 else if (CONFIG(TOCTOU_SAFETY) && err == CB_CBFS_CACHE_FULL)
477 die("RO mcache overflow breaks TOCTOU safety!\n");
478 else if (err && err != CB_CBFS_CACHE_FULL)
479 die("RO CBFS initialization error: %d", err);
Julius Werner1e37c9c2019-12-11 17:09:39 -0800480 }
481
482 return &ro;
483}
484
485#if !CONFIG(NO_CBFS_MCACHE)
486static void mcache_to_cbmem(const struct cbfs_boot_device *cbd, u32 cbmem_id)
487{
488 if (!cbd)
489 return;
490
491 size_t real_size = cbfs_mcache_real_size(cbd->mcache, cbd->mcache_size);
492 void *cbmem_mcache = cbmem_add(cbmem_id, real_size);
493 if (!cbmem_mcache) {
494 printk(BIOS_ERR, "ERROR: Cannot allocate CBMEM mcache %#x (%#zx bytes)!\n",
495 cbmem_id, real_size);
496 return;
497 }
498 memcpy(cbmem_mcache, cbd->mcache, real_size);
499}
500
501static void cbfs_mcache_migrate(int unused)
502{
503 mcache_to_cbmem(vboot_get_cbfs_boot_device(), CBMEM_ID_CBFS_RW_MCACHE);
504 mcache_to_cbmem(cbfs_get_boot_device(true), CBMEM_ID_CBFS_RO_MCACHE);
505}
506ROMSTAGE_CBMEM_INIT_HOOK(cbfs_mcache_migrate)
507#endif