blob: 8d868a6be3fd1fc712101a181810f8728b98ca10 [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>
Bill XIEc79e96b2019-08-22 20:28:36 +080013#include <security/tpm/tspi/crtm.h>
14#include <security/vboot/vboot_common.h>
15#include <stdlib.h>
16#include <string.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050017#include <symbols.h>
Julius Werner09f29212015-09-29 13:51:35 -070018#include <timestamp.h>
Patrick Georgi58a150a2016-05-02 17:22:29 +080019
Julius Werner0d9072b2020-03-05 12:51:08 -080020cb_err_t cbfs_boot_lookup(const char *name, bool force_ro,
21 union cbfs_mdata *mdata, struct region_device *rdev)
Julius Werner1e37c9c2019-12-11 17:09:39 -080022{
Julius Werner0d9072b2020-03-05 12:51:08 -080023 const struct cbfs_boot_device *cbd = cbfs_get_boot_device(force_ro);
24 if (!cbd)
25 return CB_ERR;
26
27 size_t data_offset;
Julius Werner1e37c9c2019-12-11 17:09:39 -080028 cb_err_t err = CB_CBFS_CACHE_FULL;
29 if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM)
30 err = cbfs_mcache_lookup(cbd->mcache, cbd->mcache_size,
Julius Werner0d9072b2020-03-05 12:51:08 -080031 name, mdata, &data_offset);
Julius Werner1e37c9c2019-12-11 17:09:39 -080032 if (err == CB_CBFS_CACHE_FULL)
Julius Werner0d9072b2020-03-05 12:51:08 -080033 err = cbfs_lookup(&cbd->rdev, name, mdata, &data_offset, NULL);
34
35 if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && !force_ro &&
36 err == CB_CBFS_NOT_FOUND) {
37 printk(BIOS_INFO, "CBFS: Fall back to RO region for %s\n",
38 name);
39 return cbfs_boot_lookup(name, true, mdata, rdev);
40 }
41 if (err)
42 return err;
43
44 if (rdev_chain(rdev, &cbd->rdev, data_offset, be32toh(mdata->h.len)))
45 return CB_ERR;
46
47 if (tspi_measure_cbfs_hook(rdev, name, be32toh(mdata->h.type)))
48 return CB_ERR;
49
50 return CB_SUCCESS;
Julius Werner1e37c9c2019-12-11 17:09:39 -080051}
52
Aaron Durbin37a5d152015-09-17 16:09:30 -050053int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
Aaron Durbin899d13d2015-05-15 23:39:23 -050054{
Julius Werner0d9072b2020-03-05 12:51:08 -080055 if (cbfs_boot_lookup(name, false, &fh->mdata, &fh->data))
Julius Werner1cd013b2019-12-11 16:50:02 -080056 return -1;
57
58 size_t msize = be32toh(fh->mdata.h.offset);
59 if (rdev_chain(&fh->metadata, &addrspace_32bit.rdev,
Julius Werner0d9072b2020-03-05 12:51:08 -080060 (uintptr_t)&fh->mdata, msize))
Julius Werner1cd013b2019-12-11 16:50:02 -080061 return -1;
Julius Werner0d9072b2020-03-05 12:51:08 -080062
Julius Werner1cd013b2019-12-11 16:50:02 -080063 if (type) {
64 if (!*type)
65 *type = be32toh(fh->mdata.h.type);
66 else if (*type != be32toh(fh->mdata.h.type))
67 return -1;
Wim Vervoorn114e2e82019-11-05 14:09:16 +010068 }
69
Julius Werner1cd013b2019-12-11 16:50:02 -080070 return 0;
Aaron Durbin899d13d2015-05-15 23:39:23 -050071}
72
Julius Werner834b3ec2020-03-04 16:52:08 -080073void *cbfs_map(const char *name, size_t *size_out)
Aaron Durbin899d13d2015-05-15 23:39:23 -050074{
Julius Wernerd17ce412020-10-01 18:25:49 -070075 struct region_device rdev;
76 union cbfs_mdata mdata;
Aaron Durbin899d13d2015-05-15 23:39:23 -050077
Julius Wernerd17ce412020-10-01 18:25:49 -070078 if (cbfs_boot_lookup(name, false, &mdata, &rdev))
Aaron Durbin899d13d2015-05-15 23:39:23 -050079 return NULL;
80
Julius Werner834b3ec2020-03-04 16:52:08 -080081 if (size_out != NULL)
Julius Wernerd17ce412020-10-01 18:25:49 -070082 *size_out = region_device_sz(&rdev);
Aaron Durbin899d13d2015-05-15 23:39:23 -050083
Julius Wernerd17ce412020-10-01 18:25:49 -070084 return rdev_mmap_full(&rdev);
Aaron Durbin899d13d2015-05-15 23:39:23 -050085}
86
Julius Werner834b3ec2020-03-04 16:52:08 -080087int cbfs_unmap(void *mapping)
88{
89 /* This works because munmap() only works on the root rdev and never
90 cares about which chained subregion something was mapped from. */
91 return rdev_munmap(boot_device_ro(), mapping);
92}
93
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080094int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010095 const char *name, uint32_t *type)
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080096{
97 struct region_device rdev;
Bill XIEbad08c22020-02-13 11:11:35 +080098 int ret = 0;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080099 if (fmap_locate_area_as_rdev(region_name, &rdev)) {
100 LOG("%s region not found while looking for %s\n",
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100101 region_name, name);
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800102 return -1;
103 }
104
Julius Werner0d9072b2020-03-05 12:51:08 -0800105 uint32_t dummy_type = 0;
106 if (!type)
107 type = &dummy_type;
108
Bill XIEbad08c22020-02-13 11:11:35 +0800109 ret = cbfs_locate(fh, &rdev, name, type);
110 if (!ret)
Julius Werner0d9072b2020-03-05 12:51:08 -0800111 if (tspi_measure_cbfs_hook(&rdev, name, *type))
Bill XIEbad08c22020-02-13 11:11:35 +0800112 return -1;
113 return ret;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800114}
115
Aaron Durbina85febc2020-05-15 15:09:10 -0600116static inline bool fsps_env(void)
117{
118 /* FSP-S is assumed to be loaded in ramstage. */
119 if (ENV_RAMSTAGE)
120 return true;
121 return false;
122}
123
Aaron Durbinecbfa992020-05-15 17:01:58 -0600124static inline bool fspm_env(void)
125{
126 /* FSP-M is assumed to be loaded in romstage. */
127 if (ENV_ROMSTAGE)
128 return true;
129 return false;
130}
131
Aaron Durbina121f952020-05-26 15:48:10 -0600132static inline bool cbfs_lz4_enabled(void)
133{
Aaron Durbina85febc2020-05-15 15:09:10 -0600134 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZ4))
135 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600136 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZ4))
137 return true;
Aaron Durbina85febc2020-05-15 15:09:10 -0600138
Aaron Durbina121f952020-05-26 15:48:10 -0600139 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES))
140 return false;
141
142 return true;
143}
144
145static inline bool cbfs_lzma_enabled(void)
146{
Aaron Durbina85febc2020-05-15 15:09:10 -0600147 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZMA))
148 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600149 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA))
150 return true;
Aaron Durbina121f952020-05-26 15:48:10 -0600151 /* We assume here romstage and postcar are never compressed. */
152 if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE)
153 return false;
154 if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE))
155 return false;
156 if ((ENV_ROMSTAGE || ENV_POSTCAR)
157 && !CONFIG(COMPRESS_RAMSTAGE))
158 return false;
159 return true;
160}
161
Julius Werner09f29212015-09-29 13:51:35 -0700162size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset,
163 size_t in_size, void *buffer, size_t buffer_size, uint32_t compression)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500164{
Julius Werner09f29212015-09-29 13:51:35 -0700165 size_t out_size;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600166 void *map;
Julius Werner09f29212015-09-29 13:51:35 -0700167
168 switch (compression) {
169 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700170 if (buffer_size < in_size)
171 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700172 if (rdev_readat(rdev, buffer, offset, in_size) != in_size)
173 return 0;
174 return in_size;
175
176 case CBFS_COMPRESS_LZ4:
Aaron Durbina121f952020-05-26 15:48:10 -0600177 if (!cbfs_lz4_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700178 return 0;
179
Aaron Durbin84f394e2020-05-26 16:16:42 -0600180 /* cbfs_stage_load_and_decompress() takes care of in-place
181 lz4 decompression by setting up the rdev to be in memory. */
182 map = rdev_mmap(rdev, offset, in_size);
183 if (map == NULL)
Julius Werner09f29212015-09-29 13:51:35 -0700184 return 0;
185
186 timestamp_add_now(TS_START_ULZ4F);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600187 out_size = ulz4fn(map, in_size, buffer, buffer_size);
Julius Werner09f29212015-09-29 13:51:35 -0700188 timestamp_add_now(TS_END_ULZ4F);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600189
190 rdev_munmap(rdev, map);
191
Julius Werner09f29212015-09-29 13:51:35 -0700192 return out_size;
193
194 case CBFS_COMPRESS_LZMA:
Aaron Durbina121f952020-05-26 15:48:10 -0600195 if (!cbfs_lzma_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700196 return 0;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600197 map = rdev_mmap(rdev, offset, in_size);
Julius Werner09f29212015-09-29 13:51:35 -0700198 if (map == NULL)
199 return 0;
200
201 /* Note: timestamp not useful for memory-mapped media (x86) */
202 timestamp_add_now(TS_START_ULZMA);
203 out_size = ulzman(map, in_size, buffer, buffer_size);
204 timestamp_add_now(TS_END_ULZMA);
205
206 rdev_munmap(rdev, map);
207
208 return out_size;
209
210 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500211 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700212 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500213}
214
Aaron Durbin84f394e2020-05-26 16:16:42 -0600215static size_t cbfs_stage_load_and_decompress(const struct region_device *rdev,
216 size_t offset, size_t in_size, void *buffer, size_t buffer_size,
217 uint32_t compression)
218{
219 struct region_device rdev_src;
220
221 if (compression == CBFS_COMPRESS_LZ4) {
222 if (!cbfs_lz4_enabled())
223 return 0;
224 /* Load the compressed image to the end of the available memory
225 * area for in-place decompression. It is the responsibility of
226 * the caller to ensure that buffer_size is large enough
227 * (see compression.h, guaranteed by cbfstool for stages). */
228 void *compr_start = buffer + buffer_size - in_size;
229 if (rdev_readat(rdev, compr_start, offset, in_size) != in_size)
230 return 0;
231 /* Create a region device backed by memory. */
232 rdev_chain(&rdev_src, &addrspace_32bit.rdev,
233 (uintptr_t)compr_start, in_size);
234
235 return cbfs_load_and_decompress(&rdev_src, 0, in_size, buffer,
236 buffer_size, compression);
237 }
238
239 /* All other algorithms can use the generic implementation. */
240 return cbfs_load_and_decompress(rdev, offset, in_size, buffer,
241 buffer_size, compression);
242}
243
Stefan Reinauer800379f2010-03-01 08:34:19 +0000244static inline int tohex4(unsigned int c)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000245{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800246 return (c <= 9) ? (c + '0') : (c - 10 + 'a');
Patrick Georgib203c2f2009-08-20 14:48:03 +0000247}
248
Martin Rotha616a4b2020-01-21 09:28:40 -0700249static void tohex8(unsigned int val, char *dest)
250{
251 dest[0] = tohex4((val >> 4) & 0xf);
252 dest[1] = tohex4(val & 0xf);
253}
254
Lee Leahyb2d834a2017-03-08 16:52:22 -0800255static void tohex16(unsigned int val, char *dest)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000256{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100257 dest[0] = tohex4(val >> 12);
258 dest[1] = tohex4((val >> 8) & 0xf);
259 dest[2] = tohex4((val >> 4) & 0xf);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800260 dest[3] = tohex4(val & 0xf);
Patrick Georgib203c2f2009-08-20 14:48:03 +0000261}
262
Aaron Durbin899d13d2015-05-15 23:39:23 -0500263void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
Peter Stuge483b7bb2009-04-14 07:40:01 +0000264{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800265 char name[17] = "pciXXXX,XXXX.rom";
Peter Stuge483b7bb2009-04-14 07:40:01 +0000266
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100267 tohex16(vendor, name + 3);
268 tohex16(device, name + 8);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000269
Julius Werner834b3ec2020-03-04 16:52:08 -0800270 return cbfs_map(name, NULL);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000271}
272
Martin Rotha616a4b2020-01-21 09:28:40 -0700273void *cbfs_boot_map_optionrom_revision(uint16_t vendor, uint16_t device, uint8_t rev)
274{
275 char name[20] = "pciXXXX,XXXX,XX.rom";
276
277 tohex16(vendor, name + 3);
278 tohex16(device, name + 8);
279 tohex8(rev, name + 13);
280
Julius Werner834b3ec2020-03-04 16:52:08 -0800281 return cbfs_map(name, NULL);
Martin Rotha616a4b2020-01-21 09:28:40 -0700282}
283
Julius Werner834b3ec2020-03-04 16:52:08 -0800284size_t cbfs_load(const char *name, void *buf, size_t buf_size)
Julius Wernerf975e552016-08-19 15:43:06 -0700285{
Julius Wernerd17ce412020-10-01 18:25:49 -0700286 struct region_device rdev;
287 union cbfs_mdata mdata;
Julius Wernerf975e552016-08-19 15:43:06 -0700288
Julius Wernerd17ce412020-10-01 18:25:49 -0700289 if (cbfs_boot_lookup(name, false, &mdata, &rdev))
Julius Wernerf975e552016-08-19 15:43:06 -0700290 return 0;
291
Julius Wernerd17ce412020-10-01 18:25:49 -0700292 uint32_t compression = CBFS_COMPRESS_NONE;
293 const struct cbfs_file_attr_compression *attr = cbfs_find_attr(&mdata,
294 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*attr));
295 if (attr) {
296 compression = be32toh(attr->compression);
297 if (buf_size < be32toh(attr->decompressed_size))
298 return 0;
299 }
Julius Wernerf975e552016-08-19 15:43:06 -0700300
Julius Wernerd17ce412020-10-01 18:25:49 -0700301 return cbfs_load_and_decompress(&rdev, 0, region_device_sz(&rdev),
302 buf, buf_size, compression);
Julius Wernerf975e552016-08-19 15:43:06 -0700303}
304
Aaron Durbin899d13d2015-05-15 23:39:23 -0500305int cbfs_prog_stage_load(struct prog *pstage)
306{
307 struct cbfs_stage stage;
308 uint8_t *load;
309 void *entry;
310 size_t fsize;
311 size_t foffset;
Aaron Durbin37a5d152015-09-17 16:09:30 -0500312 const struct region_device *fh = prog_rdev(pstage);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800313
Aaron Durbin899d13d2015-05-15 23:39:23 -0500314 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
Julius Wernerb29bd27b2015-12-03 11:29:12 -0800315 return -1;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500316
317 fsize = region_device_sz(fh);
318 fsize -= sizeof(stage);
319 foffset = 0;
320 foffset += sizeof(stage);
321
Aaron Durbin0b418bd2020-10-08 14:56:03 -0600322 /* cbfs_stage fields are written in little endian despite the other
323 cbfs data types being encoded in big endian. */
324 stage.compression = read_le32(&stage.compression);
325 stage.entry = read_le64(&stage.entry);
326 stage.load = read_le64(&stage.load);
327 stage.len = read_le32(&stage.len);
328 stage.memlen = read_le32(&stage.memlen);
329
Aaron Durbin899d13d2015-05-15 23:39:23 -0500330 assert(fsize == stage.len);
331
Aaron Durbin899d13d2015-05-15 23:39:23 -0500332 load = (void *)(uintptr_t)stage.load;
333 entry = (void *)(uintptr_t)stage.entry;
334
Aaron Durbined253c82015-10-07 17:22:42 -0500335 /* Hacky way to not load programs over read only media. The stages
336 * that would hit this path initialize themselves. */
Julius Werner21a40532020-04-21 16:03:53 -0700337 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) &&
338 !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
Aaron Durbined253c82015-10-07 17:22:42 -0500339 void *mapping = rdev_mmap(fh, foffset, fsize);
340 rdev_munmap(fh, mapping);
341 if (mapping == load)
342 goto out;
343 }
344
Aaron Durbin84f394e2020-05-26 16:16:42 -0600345 fsize = cbfs_stage_load_and_decompress(fh, foffset, fsize, load,
Julius Werner09f29212015-09-29 13:51:35 -0700346 stage.memlen, stage.compression);
347 if (!fsize)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500348 return -1;
349
350 /* Clear area not covered by file. */
351 memset(&load[fsize], 0, stage.memlen - fsize);
352
Aaron Durbin096f4572016-03-31 13:49:00 -0500353 prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL);
Aaron Durbined253c82015-10-07 17:22:42 -0500354
355out:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500356 prog_set_area(pstage, load, stage.memlen);
357 prog_set_entry(pstage, entry, NULL);
358
359 return 0;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800360}
Aaron Durbin6d720f32015-12-08 17:00:23 -0600361
Julius Werner1e37c9c2019-12-11 17:09:39 -0800362void cbfs_boot_device_find_mcache(struct cbfs_boot_device *cbd, uint32_t id)
Aaron Durbin6d720f32015-12-08 17:00:23 -0600363{
Julius Werner1e37c9c2019-12-11 17:09:39 -0800364 if (CONFIG(NO_CBFS_MCACHE) || ENV_SMM)
365 return;
366
367 const struct cbmem_entry *entry;
368 if (cbmem_possibly_online() &&
369 (entry = cbmem_entry_find(id))) {
370 cbd->mcache = cbmem_entry_start(entry);
371 cbd->mcache_size = cbmem_entry_size(entry);
372 } else if (ENV_ROMSTAGE_OR_BEFORE) {
373 u8 *boundary = _ecbfs_mcache - REGION_SIZE(cbfs_mcache) *
374 CONFIG_CBFS_MCACHE_RW_PERCENTAGE / 100;
375 boundary = (u8 *)ALIGN_DOWN((uintptr_t)boundary,
376 CBFS_MCACHE_ALIGNMENT);
377 if (id == CBMEM_ID_CBFS_RO_MCACHE) {
378 cbd->mcache = _cbfs_mcache;
379 cbd->mcache_size = boundary - _cbfs_mcache;
380 } else if (id == CBMEM_ID_CBFS_RW_MCACHE) {
381 cbd->mcache = boundary;
382 cbd->mcache_size = _ecbfs_mcache - boundary;
383 }
384 }
Aaron Durbin6d720f32015-12-08 17:00:23 -0600385}
Julius Werner1e37c9c2019-12-11 17:09:39 -0800386
387const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro)
388{
389 static struct cbfs_boot_device ro;
390
391 /* Ensure we always init RO mcache, even if first file is from RW.
392 Otherwise it may not be available when needed in later stages. */
393 if (ENV_INITIAL_STAGE && !force_ro && !region_device_sz(&ro.rdev))
394 cbfs_get_boot_device(true);
395
396 if (!force_ro) {
397 const struct cbfs_boot_device *rw = vboot_get_cbfs_boot_device();
398 /* This will return NULL if vboot isn't enabled, didn't run yet
399 or decided to boot into recovery mode. */
400 if (rw)
401 return rw;
402 }
403
404 if (region_device_sz(&ro.rdev))
405 return &ro;
406
407 if (fmap_locate_area_as_rdev("COREBOOT", &ro.rdev))
408 return NULL;
409
410 cbfs_boot_device_find_mcache(&ro, CBMEM_ID_CBFS_RO_MCACHE);
411
412 if (ENV_INITIAL_STAGE && !CONFIG(NO_CBFS_MCACHE)) {
413 cb_err_t err = cbfs_mcache_build(&ro.rdev, ro.mcache,
414 ro.mcache_size, NULL);
415 if (err && err != CB_CBFS_CACHE_FULL)
416 die("Failed to build RO mcache");
417 }
418
419 return &ro;
420}
421
422#if !CONFIG(NO_CBFS_MCACHE)
423static void mcache_to_cbmem(const struct cbfs_boot_device *cbd, u32 cbmem_id)
424{
425 if (!cbd)
426 return;
427
428 size_t real_size = cbfs_mcache_real_size(cbd->mcache, cbd->mcache_size);
429 void *cbmem_mcache = cbmem_add(cbmem_id, real_size);
430 if (!cbmem_mcache) {
431 printk(BIOS_ERR, "ERROR: Cannot allocate CBMEM mcache %#x (%#zx bytes)!\n",
432 cbmem_id, real_size);
433 return;
434 }
435 memcpy(cbmem_mcache, cbd->mcache, real_size);
436}
437
438static void cbfs_mcache_migrate(int unused)
439{
440 mcache_to_cbmem(vboot_get_cbfs_boot_device(), CBMEM_ID_CBFS_RW_MCACHE);
441 mcache_to_cbmem(cbfs_get_boot_device(true), CBMEM_ID_CBFS_RO_MCACHE);
442}
443ROMSTAGE_CBMEM_INIT_HOOK(cbfs_mcache_migrate)
444#endif