blob: 493093ebb9e2f9398a9625bc222cda29dbc5fa61 [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{
Aaron Durbin37a5d152015-09-17 16:09:30 -050075 struct cbfsf fh;
Aaron Durbin899d13d2015-05-15 23:39:23 -050076 size_t fsize;
77
Julius Werner834b3ec2020-03-04 16:52:08 -080078 if (cbfs_boot_locate(&fh, name, NULL))
Aaron Durbin899d13d2015-05-15 23:39:23 -050079 return NULL;
80
Aaron Durbin37a5d152015-09-17 16:09:30 -050081 fsize = region_device_sz(&fh.data);
Aaron Durbin899d13d2015-05-15 23:39:23 -050082
Julius Werner834b3ec2020-03-04 16:52:08 -080083 if (size_out != NULL)
84 *size_out = fsize;
Aaron Durbin899d13d2015-05-15 23:39:23 -050085
Aaron Durbin37a5d152015-09-17 16:09:30 -050086 return rdev_mmap(&fh.data, 0, fsize);
Aaron Durbin899d13d2015-05-15 23:39:23 -050087}
88
Julius Werner834b3ec2020-03-04 16:52:08 -080089int cbfs_unmap(void *mapping)
90{
91 /* This works because munmap() only works on the root rdev and never
92 cares about which chained subregion something was mapped from. */
93 return rdev_munmap(boot_device_ro(), mapping);
94}
95
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080096int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010097 const char *name, uint32_t *type)
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080098{
99 struct region_device rdev;
Bill XIEbad08c22020-02-13 11:11:35 +0800100 int ret = 0;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800101 if (fmap_locate_area_as_rdev(region_name, &rdev)) {
102 LOG("%s region not found while looking for %s\n",
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100103 region_name, name);
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800104 return -1;
105 }
106
Julius Werner0d9072b2020-03-05 12:51:08 -0800107 uint32_t dummy_type = 0;
108 if (!type)
109 type = &dummy_type;
110
Bill XIEbad08c22020-02-13 11:11:35 +0800111 ret = cbfs_locate(fh, &rdev, name, type);
112 if (!ret)
Julius Werner0d9072b2020-03-05 12:51:08 -0800113 if (tspi_measure_cbfs_hook(&rdev, name, *type))
Bill XIEbad08c22020-02-13 11:11:35 +0800114 return -1;
115 return ret;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -0800116}
117
Aaron Durbina85febc2020-05-15 15:09:10 -0600118static inline bool fsps_env(void)
119{
120 /* FSP-S is assumed to be loaded in ramstage. */
121 if (ENV_RAMSTAGE)
122 return true;
123 return false;
124}
125
Aaron Durbinecbfa992020-05-15 17:01:58 -0600126static inline bool fspm_env(void)
127{
128 /* FSP-M is assumed to be loaded in romstage. */
129 if (ENV_ROMSTAGE)
130 return true;
131 return false;
132}
133
Aaron Durbina121f952020-05-26 15:48:10 -0600134static inline bool cbfs_lz4_enabled(void)
135{
Aaron Durbina85febc2020-05-15 15:09:10 -0600136 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZ4))
137 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600138 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZ4))
139 return true;
Aaron Durbina85febc2020-05-15 15:09:10 -0600140
Aaron Durbina121f952020-05-26 15:48:10 -0600141 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES))
142 return false;
143
144 return true;
145}
146
147static inline bool cbfs_lzma_enabled(void)
148{
Aaron Durbina85febc2020-05-15 15:09:10 -0600149 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZMA))
150 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600151 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA))
152 return true;
Aaron Durbina121f952020-05-26 15:48:10 -0600153 /* We assume here romstage and postcar are never compressed. */
154 if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE)
155 return false;
156 if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE))
157 return false;
158 if ((ENV_ROMSTAGE || ENV_POSTCAR)
159 && !CONFIG(COMPRESS_RAMSTAGE))
160 return false;
161 return true;
162}
163
Julius Werner09f29212015-09-29 13:51:35 -0700164size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset,
165 size_t in_size, void *buffer, size_t buffer_size, uint32_t compression)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500166{
Julius Werner09f29212015-09-29 13:51:35 -0700167 size_t out_size;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600168 void *map;
Julius Werner09f29212015-09-29 13:51:35 -0700169
170 switch (compression) {
171 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700172 if (buffer_size < in_size)
173 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700174 if (rdev_readat(rdev, buffer, offset, in_size) != in_size)
175 return 0;
176 return in_size;
177
178 case CBFS_COMPRESS_LZ4:
Aaron Durbina121f952020-05-26 15:48:10 -0600179 if (!cbfs_lz4_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700180 return 0;
181
Aaron Durbin84f394e2020-05-26 16:16:42 -0600182 /* cbfs_stage_load_and_decompress() takes care of in-place
183 lz4 decompression by setting up the rdev to be in memory. */
184 map = rdev_mmap(rdev, offset, in_size);
185 if (map == NULL)
Julius Werner09f29212015-09-29 13:51:35 -0700186 return 0;
187
188 timestamp_add_now(TS_START_ULZ4F);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600189 out_size = ulz4fn(map, in_size, buffer, buffer_size);
Julius Werner09f29212015-09-29 13:51:35 -0700190 timestamp_add_now(TS_END_ULZ4F);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600191
192 rdev_munmap(rdev, map);
193
Julius Werner09f29212015-09-29 13:51:35 -0700194 return out_size;
195
196 case CBFS_COMPRESS_LZMA:
Aaron Durbina121f952020-05-26 15:48:10 -0600197 if (!cbfs_lzma_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700198 return 0;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600199 map = rdev_mmap(rdev, offset, in_size);
Julius Werner09f29212015-09-29 13:51:35 -0700200 if (map == NULL)
201 return 0;
202
203 /* Note: timestamp not useful for memory-mapped media (x86) */
204 timestamp_add_now(TS_START_ULZMA);
205 out_size = ulzman(map, in_size, buffer, buffer_size);
206 timestamp_add_now(TS_END_ULZMA);
207
208 rdev_munmap(rdev, map);
209
210 return out_size;
211
212 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500213 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700214 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500215}
216
Aaron Durbin84f394e2020-05-26 16:16:42 -0600217static size_t cbfs_stage_load_and_decompress(const struct region_device *rdev,
218 size_t offset, size_t in_size, void *buffer, size_t buffer_size,
219 uint32_t compression)
220{
221 struct region_device rdev_src;
222
223 if (compression == CBFS_COMPRESS_LZ4) {
224 if (!cbfs_lz4_enabled())
225 return 0;
226 /* Load the compressed image to the end of the available memory
227 * area for in-place decompression. It is the responsibility of
228 * the caller to ensure that buffer_size is large enough
229 * (see compression.h, guaranteed by cbfstool for stages). */
230 void *compr_start = buffer + buffer_size - in_size;
231 if (rdev_readat(rdev, compr_start, offset, in_size) != in_size)
232 return 0;
233 /* Create a region device backed by memory. */
234 rdev_chain(&rdev_src, &addrspace_32bit.rdev,
235 (uintptr_t)compr_start, in_size);
236
237 return cbfs_load_and_decompress(&rdev_src, 0, in_size, buffer,
238 buffer_size, compression);
239 }
240
241 /* All other algorithms can use the generic implementation. */
242 return cbfs_load_and_decompress(rdev, offset, in_size, buffer,
243 buffer_size, compression);
244}
245
Stefan Reinauer800379f2010-03-01 08:34:19 +0000246static inline int tohex4(unsigned int c)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000247{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800248 return (c <= 9) ? (c + '0') : (c - 10 + 'a');
Patrick Georgib203c2f2009-08-20 14:48:03 +0000249}
250
Martin Rotha616a4b2020-01-21 09:28:40 -0700251static void tohex8(unsigned int val, char *dest)
252{
253 dest[0] = tohex4((val >> 4) & 0xf);
254 dest[1] = tohex4(val & 0xf);
255}
256
Lee Leahyb2d834a2017-03-08 16:52:22 -0800257static void tohex16(unsigned int val, char *dest)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000258{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100259 dest[0] = tohex4(val >> 12);
260 dest[1] = tohex4((val >> 8) & 0xf);
261 dest[2] = tohex4((val >> 4) & 0xf);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800262 dest[3] = tohex4(val & 0xf);
Patrick Georgib203c2f2009-08-20 14:48:03 +0000263}
264
Aaron Durbin899d13d2015-05-15 23:39:23 -0500265void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
Peter Stuge483b7bb2009-04-14 07:40:01 +0000266{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800267 char name[17] = "pciXXXX,XXXX.rom";
Peter Stuge483b7bb2009-04-14 07:40:01 +0000268
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100269 tohex16(vendor, name + 3);
270 tohex16(device, name + 8);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000271
Julius Werner834b3ec2020-03-04 16:52:08 -0800272 return cbfs_map(name, NULL);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000273}
274
Martin Rotha616a4b2020-01-21 09:28:40 -0700275void *cbfs_boot_map_optionrom_revision(uint16_t vendor, uint16_t device, uint8_t rev)
276{
277 char name[20] = "pciXXXX,XXXX,XX.rom";
278
279 tohex16(vendor, name + 3);
280 tohex16(device, name + 8);
281 tohex8(rev, name + 13);
282
Julius Werner834b3ec2020-03-04 16:52:08 -0800283 return cbfs_map(name, NULL);
Martin Rotha616a4b2020-01-21 09:28:40 -0700284}
285
Julius Werner834b3ec2020-03-04 16:52:08 -0800286size_t cbfs_load(const char *name, void *buf, size_t buf_size)
Julius Wernerf975e552016-08-19 15:43:06 -0700287{
288 struct cbfsf fh;
289 uint32_t compression_algo;
290 size_t decompressed_size;
Julius Wernerf975e552016-08-19 15:43:06 -0700291
Julius Werner834b3ec2020-03-04 16:52:08 -0800292 if (cbfs_boot_locate(&fh, name, NULL) < 0)
Julius Wernerf975e552016-08-19 15:43:06 -0700293 return 0;
294
295 if (cbfsf_decompression_info(&fh, &compression_algo,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100296 &decompressed_size)
297 < 0
298 || decompressed_size > buf_size)
Julius Wernerf975e552016-08-19 15:43:06 -0700299 return 0;
300
301 return cbfs_load_and_decompress(&fh.data, 0, region_device_sz(&fh.data),
302 buf, buf_size, compression_algo);
303}
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