blob: cb66f81d99aab5eca168d46098aa3f64222ef398 [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 Werner98eeb962019-12-11 15:47:42 -08006#include <commonlib/bsd/compression.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08007#include <console/console.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -05008#include <endian.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08009#include <fmap.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050010#include <lib.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080011#include <security/tpm/tspi/crtm.h>
12#include <security/vboot/vboot_common.h>
13#include <stdlib.h>
14#include <string.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050015#include <symbols.h>
Julius Werner09f29212015-09-29 13:51:35 -070016#include <timestamp.h>
Patrick Georgi58a150a2016-05-02 17:22:29 +080017
Aaron Durbin899d13d2015-05-15 23:39:23 -050018#define ERROR(x...) printk(BIOS_ERR, "CBFS: " x)
19#define LOG(x...) printk(BIOS_INFO, "CBFS: " x)
Julius Wernercd49cce2019-03-05 16:53:33 -080020#if CONFIG(DEBUG_CBFS)
Aaron Durbin899d13d2015-05-15 23:39:23 -050021#define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x)
22#else
23#define DEBUG(x...)
24#endif
Peter Stuge483b7bb2009-04-14 07:40:01 +000025
Aaron Durbin37a5d152015-09-17 16:09:30 -050026int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
Aaron Durbin899d13d2015-05-15 23:39:23 -050027{
Aaron Durbin899d13d2015-05-15 23:39:23 -050028 struct region_device rdev;
Aaron Durbin899d13d2015-05-15 23:39:23 -050029
Aaron Durbinfe338e22019-11-18 12:35:21 -070030 if (cbfs_boot_region_device(&rdev))
Aaron Durbin899d13d2015-05-15 23:39:23 -050031 return -1;
Aaron Durbin899d13d2015-05-15 23:39:23 -050032
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010033 int ret = cbfs_locate(fh, &rdev, name, type);
Wim Vervoorn114e2e82019-11-05 14:09:16 +010034
35 if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && ret) {
36
37 /*
38 * When VBOOT_ENABLE_CBFS_FALLBACK is enabled and a file is not available in the
39 * active RW region, the RO (COREBOOT) region will be used to locate the file.
40 *
41 * This functionality makes it possible to avoid duplicate files in the RO
42 * and RW partitions while maintaining updateability.
43 *
44 * Files can be added to the RO_REGION_ONLY config option to use this feature.
45 */
46 printk(BIOS_DEBUG, "Fall back to RO region for %s\n", name);
Bill XIEbad08c22020-02-13 11:11:35 +080047 if (fmap_locate_area_as_rdev("COREBOOT", &rdev))
48 ERROR("RO region not found\n");
49 else
50 ret = cbfs_locate(fh, &rdev, name, type);
Wim Vervoorn114e2e82019-11-05 14:09:16 +010051 }
52
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010053 if (!ret)
Bill XIEc79e96b2019-08-22 20:28:36 +080054 if (tspi_measure_cbfs_hook(fh, name))
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010055 return -1;
56
57 return ret;
Aaron Durbin899d13d2015-05-15 23:39:23 -050058}
59
60void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size)
61{
Aaron Durbin37a5d152015-09-17 16:09:30 -050062 struct cbfsf fh;
Aaron Durbin899d13d2015-05-15 23:39:23 -050063 size_t fsize;
64
65 if (cbfs_boot_locate(&fh, name, &type))
66 return NULL;
67
Aaron Durbin37a5d152015-09-17 16:09:30 -050068 fsize = region_device_sz(&fh.data);
Aaron Durbin899d13d2015-05-15 23:39:23 -050069
70 if (size != NULL)
71 *size = fsize;
72
Aaron Durbin37a5d152015-09-17 16:09:30 -050073 return rdev_mmap(&fh.data, 0, fsize);
Aaron Durbin899d13d2015-05-15 23:39:23 -050074}
75
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080076int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010077 const char *name, uint32_t *type)
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080078{
79 struct region_device rdev;
Bill XIEbad08c22020-02-13 11:11:35 +080080 int ret = 0;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080081 if (fmap_locate_area_as_rdev(region_name, &rdev)) {
82 LOG("%s region not found while looking for %s\n",
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010083 region_name, name);
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080084 return -1;
85 }
86
Bill XIEbad08c22020-02-13 11:11:35 +080087 ret = cbfs_locate(fh, &rdev, name, type);
88 if (!ret)
89 if (tspi_measure_cbfs_hook(fh, name))
90 return -1;
91 return ret;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080092}
93
Aaron Durbina85febc2020-05-15 15:09:10 -060094static inline bool fsps_env(void)
95{
96 /* FSP-S is assumed to be loaded in ramstage. */
97 if (ENV_RAMSTAGE)
98 return true;
99 return false;
100}
101
Aaron Durbinecbfa992020-05-15 17:01:58 -0600102static inline bool fspm_env(void)
103{
104 /* FSP-M is assumed to be loaded in romstage. */
105 if (ENV_ROMSTAGE)
106 return true;
107 return false;
108}
109
Aaron Durbina121f952020-05-26 15:48:10 -0600110static inline bool cbfs_lz4_enabled(void)
111{
Aaron Durbina85febc2020-05-15 15:09:10 -0600112 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZ4))
113 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600114 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZ4))
115 return true;
Aaron Durbina85febc2020-05-15 15:09:10 -0600116
Aaron Durbina121f952020-05-26 15:48:10 -0600117 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES))
118 return false;
119
120 return true;
121}
122
123static inline bool cbfs_lzma_enabled(void)
124{
Aaron Durbina85febc2020-05-15 15:09:10 -0600125 if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZMA))
126 return true;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600127 if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA))
128 return true;
Aaron Durbina121f952020-05-26 15:48:10 -0600129 /* We assume here romstage and postcar are never compressed. */
130 if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE)
131 return false;
132 if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE))
133 return false;
134 if ((ENV_ROMSTAGE || ENV_POSTCAR)
135 && !CONFIG(COMPRESS_RAMSTAGE))
136 return false;
137 return true;
138}
139
Julius Werner09f29212015-09-29 13:51:35 -0700140size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset,
141 size_t in_size, void *buffer, size_t buffer_size, uint32_t compression)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500142{
Julius Werner09f29212015-09-29 13:51:35 -0700143 size_t out_size;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600144 void *map;
Julius Werner09f29212015-09-29 13:51:35 -0700145
146 switch (compression) {
147 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700148 if (buffer_size < in_size)
149 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700150 if (rdev_readat(rdev, buffer, offset, in_size) != in_size)
151 return 0;
152 return in_size;
153
154 case CBFS_COMPRESS_LZ4:
Aaron Durbina121f952020-05-26 15:48:10 -0600155 if (!cbfs_lz4_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700156 return 0;
157
Aaron Durbin84f394e2020-05-26 16:16:42 -0600158 /* cbfs_stage_load_and_decompress() takes care of in-place
159 lz4 decompression by setting up the rdev to be in memory. */
160 map = rdev_mmap(rdev, offset, in_size);
161 if (map == NULL)
Julius Werner09f29212015-09-29 13:51:35 -0700162 return 0;
163
164 timestamp_add_now(TS_START_ULZ4F);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600165 out_size = ulz4fn(map, in_size, buffer, buffer_size);
Julius Werner09f29212015-09-29 13:51:35 -0700166 timestamp_add_now(TS_END_ULZ4F);
Aaron Durbin84f394e2020-05-26 16:16:42 -0600167
168 rdev_munmap(rdev, map);
169
Julius Werner09f29212015-09-29 13:51:35 -0700170 return out_size;
171
172 case CBFS_COMPRESS_LZMA:
Aaron Durbina121f952020-05-26 15:48:10 -0600173 if (!cbfs_lzma_enabled())
Julius Werner09f29212015-09-29 13:51:35 -0700174 return 0;
Aaron Durbin84f394e2020-05-26 16:16:42 -0600175 map = rdev_mmap(rdev, offset, in_size);
Julius Werner09f29212015-09-29 13:51:35 -0700176 if (map == NULL)
177 return 0;
178
179 /* Note: timestamp not useful for memory-mapped media (x86) */
180 timestamp_add_now(TS_START_ULZMA);
181 out_size = ulzman(map, in_size, buffer, buffer_size);
182 timestamp_add_now(TS_END_ULZMA);
183
184 rdev_munmap(rdev, map);
185
186 return out_size;
187
188 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500189 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700190 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500191}
192
Aaron Durbin84f394e2020-05-26 16:16:42 -0600193static size_t cbfs_stage_load_and_decompress(const struct region_device *rdev,
194 size_t offset, size_t in_size, void *buffer, size_t buffer_size,
195 uint32_t compression)
196{
197 struct region_device rdev_src;
198
199 if (compression == CBFS_COMPRESS_LZ4) {
200 if (!cbfs_lz4_enabled())
201 return 0;
202 /* Load the compressed image to the end of the available memory
203 * area for in-place decompression. It is the responsibility of
204 * the caller to ensure that buffer_size is large enough
205 * (see compression.h, guaranteed by cbfstool for stages). */
206 void *compr_start = buffer + buffer_size - in_size;
207 if (rdev_readat(rdev, compr_start, offset, in_size) != in_size)
208 return 0;
209 /* Create a region device backed by memory. */
210 rdev_chain(&rdev_src, &addrspace_32bit.rdev,
211 (uintptr_t)compr_start, in_size);
212
213 return cbfs_load_and_decompress(&rdev_src, 0, in_size, buffer,
214 buffer_size, compression);
215 }
216
217 /* All other algorithms can use the generic implementation. */
218 return cbfs_load_and_decompress(rdev, offset, in_size, buffer,
219 buffer_size, compression);
220}
221
Stefan Reinauer800379f2010-03-01 08:34:19 +0000222static inline int tohex4(unsigned int c)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000223{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800224 return (c <= 9) ? (c + '0') : (c - 10 + 'a');
Patrick Georgib203c2f2009-08-20 14:48:03 +0000225}
226
Martin Rotha616a4b2020-01-21 09:28:40 -0700227static void tohex8(unsigned int val, char *dest)
228{
229 dest[0] = tohex4((val >> 4) & 0xf);
230 dest[1] = tohex4(val & 0xf);
231}
232
Lee Leahyb2d834a2017-03-08 16:52:22 -0800233static void tohex16(unsigned int val, char *dest)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000234{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100235 dest[0] = tohex4(val >> 12);
236 dest[1] = tohex4((val >> 8) & 0xf);
237 dest[2] = tohex4((val >> 4) & 0xf);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800238 dest[3] = tohex4(val & 0xf);
Patrick Georgib203c2f2009-08-20 14:48:03 +0000239}
240
Aaron Durbin899d13d2015-05-15 23:39:23 -0500241void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
Peter Stuge483b7bb2009-04-14 07:40:01 +0000242{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800243 char name[17] = "pciXXXX,XXXX.rom";
Peter Stuge483b7bb2009-04-14 07:40:01 +0000244
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100245 tohex16(vendor, name + 3);
246 tohex16(device, name + 8);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000247
Aaron Durbin899d13d2015-05-15 23:39:23 -0500248 return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000249}
250
Martin Rotha616a4b2020-01-21 09:28:40 -0700251void *cbfs_boot_map_optionrom_revision(uint16_t vendor, uint16_t device, uint8_t rev)
252{
253 char name[20] = "pciXXXX,XXXX,XX.rom";
254
255 tohex16(vendor, name + 3);
256 tohex16(device, name + 8);
257 tohex8(rev, name + 13);
258
259 return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL);
260}
261
T Michael Turney809fa7b2018-04-12 13:36:40 -0700262size_t cbfs_boot_load_file(const char *name, void *buf, size_t buf_size,
263 uint32_t type)
Julius Wernerf975e552016-08-19 15:43:06 -0700264{
265 struct cbfsf fh;
266 uint32_t compression_algo;
267 size_t decompressed_size;
Julius Wernerf975e552016-08-19 15:43:06 -0700268
269 if (cbfs_boot_locate(&fh, name, &type) < 0)
270 return 0;
271
272 if (cbfsf_decompression_info(&fh, &compression_algo,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100273 &decompressed_size)
274 < 0
275 || decompressed_size > buf_size)
Julius Wernerf975e552016-08-19 15:43:06 -0700276 return 0;
277
278 return cbfs_load_and_decompress(&fh.data, 0, region_device_sz(&fh.data),
279 buf, buf_size, compression_algo);
280}
281
Aaron Durbin899d13d2015-05-15 23:39:23 -0500282int cbfs_prog_stage_load(struct prog *pstage)
283{
284 struct cbfs_stage stage;
285 uint8_t *load;
286 void *entry;
287 size_t fsize;
288 size_t foffset;
Aaron Durbin37a5d152015-09-17 16:09:30 -0500289 const struct region_device *fh = prog_rdev(pstage);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800290
Aaron Durbin899d13d2015-05-15 23:39:23 -0500291 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
Julius Wernerb29bd27b2015-12-03 11:29:12 -0800292 return -1;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500293
294 fsize = region_device_sz(fh);
295 fsize -= sizeof(stage);
296 foffset = 0;
297 foffset += sizeof(stage);
298
299 assert(fsize == stage.len);
300
301 /* Note: cbfs_stage fields are currently in the endianness of the
302 * running processor. */
303 load = (void *)(uintptr_t)stage.load;
304 entry = (void *)(uintptr_t)stage.entry;
305
Aaron Durbined253c82015-10-07 17:22:42 -0500306 /* Hacky way to not load programs over read only media. The stages
307 * that would hit this path initialize themselves. */
Julius Werner21a40532020-04-21 16:03:53 -0700308 if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) &&
309 !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
Aaron Durbined253c82015-10-07 17:22:42 -0500310 void *mapping = rdev_mmap(fh, foffset, fsize);
311 rdev_munmap(fh, mapping);
312 if (mapping == load)
313 goto out;
314 }
315
Aaron Durbin84f394e2020-05-26 16:16:42 -0600316 fsize = cbfs_stage_load_and_decompress(fh, foffset, fsize, load,
Julius Werner09f29212015-09-29 13:51:35 -0700317 stage.memlen, stage.compression);
318 if (!fsize)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500319 return -1;
320
321 /* Clear area not covered by file. */
322 memset(&load[fsize], 0, stage.memlen - fsize);
323
Aaron Durbin096f4572016-03-31 13:49:00 -0500324 prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL);
Aaron Durbined253c82015-10-07 17:22:42 -0500325
326out:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500327 prog_set_area(pstage, load, stage.memlen);
328 prog_set_entry(pstage, entry, NULL);
329
330 return 0;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800331}
Aaron Durbin6d720f32015-12-08 17:00:23 -0600332
Aaron Durbinfe338e22019-11-18 12:35:21 -0700333int cbfs_boot_region_device(struct region_device *rdev)
Aaron Durbin6d720f32015-12-08 17:00:23 -0600334{
Aaron Durbin6d720f32015-12-08 17:00:23 -0600335 boot_device_init();
Julius Werner815611e2019-12-05 22:29:07 -0800336 return vboot_locate_cbfs(rdev) &&
337 fmap_locate_area_as_rdev("COREBOOT", rdev);
Aaron Durbin6d720f32015-12-08 17:00:23 -0600338}