blob: 5e85bb9bde8d0c22c010fb5a911726d10e75e2c8 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Peter Stuge483b7bb2009-04-14 07:40:01 +00003
Aaron Durbin899d13d2015-05-15 23:39:23 -05004#include <assert.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -05005#include <boot_device.h>
6#include <cbfs.h>
Julius Werner98eeb962019-12-11 15:47:42 -08007#include <commonlib/bsd/compression.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08008#include <console/console.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -05009#include <endian.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080010#include <fmap.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050011#include <lib.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080012#include <security/tpm/tspi/crtm.h>
13#include <security/vboot/vboot_common.h>
14#include <stdlib.h>
15#include <string.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050016#include <symbols.h>
Julius Werner09f29212015-09-29 13:51:35 -070017#include <timestamp.h>
Patrick Georgi58a150a2016-05-02 17:22:29 +080018
Aaron Durbin899d13d2015-05-15 23:39:23 -050019#define ERROR(x...) printk(BIOS_ERR, "CBFS: " x)
20#define LOG(x...) printk(BIOS_INFO, "CBFS: " x)
Julius Wernercd49cce2019-03-05 16:53:33 -080021#if CONFIG(DEBUG_CBFS)
Aaron Durbin899d13d2015-05-15 23:39:23 -050022#define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x)
23#else
24#define DEBUG(x...)
25#endif
Peter Stuge483b7bb2009-04-14 07:40:01 +000026
Aaron Durbin37a5d152015-09-17 16:09:30 -050027int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
Aaron Durbin899d13d2015-05-15 23:39:23 -050028{
Aaron Durbin899d13d2015-05-15 23:39:23 -050029 struct region_device rdev;
Aaron Durbin899d13d2015-05-15 23:39:23 -050030
Aaron Durbinfe338e22019-11-18 12:35:21 -070031 if (cbfs_boot_region_device(&rdev))
Aaron Durbin899d13d2015-05-15 23:39:23 -050032 return -1;
Aaron Durbin899d13d2015-05-15 23:39:23 -050033
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010034 int ret = cbfs_locate(fh, &rdev, name, type);
Wim Vervoorn114e2e82019-11-05 14:09:16 +010035
36 if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && ret) {
37
38 /*
39 * When VBOOT_ENABLE_CBFS_FALLBACK is enabled and a file is not available in the
40 * active RW region, the RO (COREBOOT) region will be used to locate the file.
41 *
42 * This functionality makes it possible to avoid duplicate files in the RO
43 * and RW partitions while maintaining updateability.
44 *
45 * Files can be added to the RO_REGION_ONLY config option to use this feature.
46 */
47 printk(BIOS_DEBUG, "Fall back to RO region for %s\n", name);
Bill XIEbad08c22020-02-13 11:11:35 +080048 if (fmap_locate_area_as_rdev("COREBOOT", &rdev))
49 ERROR("RO region not found\n");
50 else
51 ret = cbfs_locate(fh, &rdev, name, type);
Wim Vervoorn114e2e82019-11-05 14:09:16 +010052 }
53
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010054 if (!ret)
Bill XIEc79e96b2019-08-22 20:28:36 +080055 if (tspi_measure_cbfs_hook(fh, name))
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010056 return -1;
57
58 return ret;
Aaron Durbin899d13d2015-05-15 23:39:23 -050059}
60
61void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size)
62{
Aaron Durbin37a5d152015-09-17 16:09:30 -050063 struct cbfsf fh;
Aaron Durbin899d13d2015-05-15 23:39:23 -050064 size_t fsize;
65
66 if (cbfs_boot_locate(&fh, name, &type))
67 return NULL;
68
Aaron Durbin37a5d152015-09-17 16:09:30 -050069 fsize = region_device_sz(&fh.data);
Aaron Durbin899d13d2015-05-15 23:39:23 -050070
71 if (size != NULL)
72 *size = fsize;
73
Aaron Durbin37a5d152015-09-17 16:09:30 -050074 return rdev_mmap(&fh.data, 0, fsize);
Aaron Durbin899d13d2015-05-15 23:39:23 -050075}
76
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080077int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010078 const char *name, uint32_t *type)
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080079{
80 struct region_device rdev;
Bill XIEbad08c22020-02-13 11:11:35 +080081 int ret = 0;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080082 if (fmap_locate_area_as_rdev(region_name, &rdev)) {
83 LOG("%s region not found while looking for %s\n",
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010084 region_name, name);
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080085 return -1;
86 }
87
Bill XIEbad08c22020-02-13 11:11:35 +080088 ret = cbfs_locate(fh, &rdev, name, type);
89 if (!ret)
90 if (tspi_measure_cbfs_hook(fh, name))
91 return -1;
92 return ret;
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080093}
94
Julius Werner09f29212015-09-29 13:51:35 -070095size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset,
96 size_t in_size, void *buffer, size_t buffer_size, uint32_t compression)
Aaron Durbin899d13d2015-05-15 23:39:23 -050097{
Julius Werner09f29212015-09-29 13:51:35 -070098 size_t out_size;
99
100 switch (compression) {
101 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700102 if (buffer_size < in_size)
103 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700104 if (rdev_readat(rdev, buffer, offset, in_size) != in_size)
105 return 0;
106 return in_size;
107
108 case CBFS_COMPRESS_LZ4:
109 if ((ENV_BOOTBLOCK || ENV_VERSTAGE) &&
Julius Wernercd49cce2019-03-05 16:53:33 -0800110 !CONFIG(COMPRESS_PRERAM_STAGES))
Julius Werner09f29212015-09-29 13:51:35 -0700111 return 0;
112
113 /* Load the compressed image to the end of the available memory
114 * area for in-place decompression. It is the responsibility of
115 * the caller to ensure that buffer_size is large enough
116 * (see compression.h, guaranteed by cbfstool for stages). */
117 void *compr_start = buffer + buffer_size - in_size;
118 if (rdev_readat(rdev, compr_start, offset, in_size) != in_size)
119 return 0;
120
121 timestamp_add_now(TS_START_ULZ4F);
122 out_size = ulz4fn(compr_start, in_size, buffer, buffer_size);
123 timestamp_add_now(TS_END_ULZ4F);
124 return out_size;
125
126 case CBFS_COMPRESS_LZMA:
Kyösti Mälkkib5211ef2018-06-01 07:11:25 +0300127 /* We assume here romstage and postcar are never compressed. */
Julius Werner09f29212015-09-29 13:51:35 -0700128 if (ENV_BOOTBLOCK || ENV_VERSTAGE)
129 return 0;
Julius Wernercd49cce2019-03-05 16:53:33 -0800130 if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE))
Kyösti Mälkkib5211ef2018-06-01 07:11:25 +0300131 return 0;
Lee Leahyd950f512016-07-25 09:53:35 -0700132 if ((ENV_ROMSTAGE || ENV_POSTCAR)
Julius Wernercd49cce2019-03-05 16:53:33 -0800133 && !CONFIG(COMPRESS_RAMSTAGE))
Julius Werner09f29212015-09-29 13:51:35 -0700134 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700135 void *map = rdev_mmap(rdev, offset, in_size);
136 if (map == NULL)
137 return 0;
138
139 /* Note: timestamp not useful for memory-mapped media (x86) */
140 timestamp_add_now(TS_START_ULZMA);
141 out_size = ulzman(map, in_size, buffer, buffer_size);
142 timestamp_add_now(TS_END_ULZMA);
143
144 rdev_munmap(rdev, map);
145
146 return out_size;
147
148 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500149 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700150 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500151}
152
Stefan Reinauer800379f2010-03-01 08:34:19 +0000153static inline int tohex4(unsigned int c)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000154{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800155 return (c <= 9) ? (c + '0') : (c - 10 + 'a');
Patrick Georgib203c2f2009-08-20 14:48:03 +0000156}
157
Martin Rotha616a4b2020-01-21 09:28:40 -0700158static void tohex8(unsigned int val, char *dest)
159{
160 dest[0] = tohex4((val >> 4) & 0xf);
161 dest[1] = tohex4(val & 0xf);
162}
163
Lee Leahyb2d834a2017-03-08 16:52:22 -0800164static void tohex16(unsigned int val, char *dest)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000165{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100166 dest[0] = tohex4(val >> 12);
167 dest[1] = tohex4((val >> 8) & 0xf);
168 dest[2] = tohex4((val >> 4) & 0xf);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800169 dest[3] = tohex4(val & 0xf);
Patrick Georgib203c2f2009-08-20 14:48:03 +0000170}
171
Aaron Durbin899d13d2015-05-15 23:39:23 -0500172void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
Peter Stuge483b7bb2009-04-14 07:40:01 +0000173{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800174 char name[17] = "pciXXXX,XXXX.rom";
Peter Stuge483b7bb2009-04-14 07:40:01 +0000175
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100176 tohex16(vendor, name + 3);
177 tohex16(device, name + 8);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000178
Aaron Durbin899d13d2015-05-15 23:39:23 -0500179 return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000180}
181
Martin Rotha616a4b2020-01-21 09:28:40 -0700182void *cbfs_boot_map_optionrom_revision(uint16_t vendor, uint16_t device, uint8_t rev)
183{
184 char name[20] = "pciXXXX,XXXX,XX.rom";
185
186 tohex16(vendor, name + 3);
187 tohex16(device, name + 8);
188 tohex8(rev, name + 13);
189
190 return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL);
191}
192
T Michael Turney809fa7b2018-04-12 13:36:40 -0700193size_t cbfs_boot_load_file(const char *name, void *buf, size_t buf_size,
194 uint32_t type)
Julius Wernerf975e552016-08-19 15:43:06 -0700195{
196 struct cbfsf fh;
197 uint32_t compression_algo;
198 size_t decompressed_size;
Julius Wernerf975e552016-08-19 15:43:06 -0700199
200 if (cbfs_boot_locate(&fh, name, &type) < 0)
201 return 0;
202
203 if (cbfsf_decompression_info(&fh, &compression_algo,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100204 &decompressed_size)
205 < 0
206 || decompressed_size > buf_size)
Julius Wernerf975e552016-08-19 15:43:06 -0700207 return 0;
208
209 return cbfs_load_and_decompress(&fh.data, 0, region_device_sz(&fh.data),
210 buf, buf_size, compression_algo);
211}
212
Aaron Durbin899d13d2015-05-15 23:39:23 -0500213int cbfs_prog_stage_load(struct prog *pstage)
214{
215 struct cbfs_stage stage;
216 uint8_t *load;
217 void *entry;
218 size_t fsize;
219 size_t foffset;
Aaron Durbin37a5d152015-09-17 16:09:30 -0500220 const struct region_device *fh = prog_rdev(pstage);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800221
Aaron Durbin899d13d2015-05-15 23:39:23 -0500222 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
Julius Wernerb29bd27b2015-12-03 11:29:12 -0800223 return -1;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500224
225 fsize = region_device_sz(fh);
226 fsize -= sizeof(stage);
227 foffset = 0;
228 foffset += sizeof(stage);
229
230 assert(fsize == stage.len);
231
232 /* Note: cbfs_stage fields are currently in the endianness of the
233 * running processor. */
234 load = (void *)(uintptr_t)stage.load;
235 entry = (void *)(uintptr_t)stage.entry;
236
Aaron Durbined253c82015-10-07 17:22:42 -0500237 /* Hacky way to not load programs over read only media. The stages
238 * that would hit this path initialize themselves. */
Arthur Heymans7c24de92019-10-25 16:53:29 +0200239 if ((ENV_BOOTBLOCK || ENV_VERSTAGE) && !CONFIG(NO_XIP_EARLY_STAGES) &&
Julius Wernercd49cce2019-03-05 16:53:33 -0800240 CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
Aaron Durbined253c82015-10-07 17:22:42 -0500241 void *mapping = rdev_mmap(fh, foffset, fsize);
242 rdev_munmap(fh, mapping);
243 if (mapping == load)
244 goto out;
245 }
246
Julius Werner09f29212015-09-29 13:51:35 -0700247 fsize = cbfs_load_and_decompress(fh, foffset, fsize, load,
248 stage.memlen, stage.compression);
249 if (!fsize)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500250 return -1;
251
252 /* Clear area not covered by file. */
253 memset(&load[fsize], 0, stage.memlen - fsize);
254
Aaron Durbin096f4572016-03-31 13:49:00 -0500255 prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL);
Aaron Durbined253c82015-10-07 17:22:42 -0500256
257out:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500258 prog_set_area(pstage, load, stage.memlen);
259 prog_set_entry(pstage, entry, NULL);
260
261 return 0;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800262}
Aaron Durbin6d720f32015-12-08 17:00:23 -0600263
Aaron Durbinfe338e22019-11-18 12:35:21 -0700264int cbfs_boot_region_device(struct region_device *rdev)
Aaron Durbin6d720f32015-12-08 17:00:23 -0600265{
Aaron Durbin6d720f32015-12-08 17:00:23 -0600266 boot_device_init();
Julius Werner815611e2019-12-05 22:29:07 -0800267 return vboot_locate_cbfs(rdev) &&
268 fmap_locate_area_as_rdev("COREBOOT", rdev);
Aaron Durbin6d720f32015-12-08 17:00:23 -0600269}