blob: 4392ab7ab0d6ba1446cfa5128bb4d4b02c14dff6 [file] [log] [blame]
Peter Stuge483b7bb2009-04-14 07:40:01 +00001/*
2 * This file is part of the coreboot project.
3 *
Peter Stuge483b7bb2009-04-14 07:40:01 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Peter Stuge483b7bb2009-04-14 07:40:01 +000013 */
14
Aaron Durbin899d13d2015-05-15 23:39:23 -050015#include <assert.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050016#include <boot_device.h>
17#include <cbfs.h>
Julius Werner98eeb962019-12-11 15:47:42 -080018#include <commonlib/bsd/compression.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080019#include <console/console.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050020#include <endian.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080021#include <fmap.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050022#include <lib.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080023#include <security/tpm/tspi/crtm.h>
24#include <security/vboot/vboot_common.h>
25#include <stdlib.h>
26#include <string.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050027#include <symbols.h>
Julius Werner09f29212015-09-29 13:51:35 -070028#include <timestamp.h>
Patrick Georgi58a150a2016-05-02 17:22:29 +080029
Aaron Durbin899d13d2015-05-15 23:39:23 -050030#define ERROR(x...) printk(BIOS_ERR, "CBFS: " x)
31#define LOG(x...) printk(BIOS_INFO, "CBFS: " x)
Julius Wernercd49cce2019-03-05 16:53:33 -080032#if CONFIG(DEBUG_CBFS)
Aaron Durbin899d13d2015-05-15 23:39:23 -050033#define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x)
34#else
35#define DEBUG(x...)
36#endif
Peter Stuge483b7bb2009-04-14 07:40:01 +000037
Aaron Durbin37a5d152015-09-17 16:09:30 -050038int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
Aaron Durbin899d13d2015-05-15 23:39:23 -050039{
Aaron Durbin899d13d2015-05-15 23:39:23 -050040 struct region_device rdev;
Aaron Durbin899d13d2015-05-15 23:39:23 -050041
Aaron Durbinfe338e22019-11-18 12:35:21 -070042 if (cbfs_boot_region_device(&rdev))
Aaron Durbin899d13d2015-05-15 23:39:23 -050043 return -1;
Aaron Durbin899d13d2015-05-15 23:39:23 -050044
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010045 int ret = cbfs_locate(fh, &rdev, name, type);
Wim Vervoorn114e2e82019-11-05 14:09:16 +010046
47 if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && ret) {
48
49 /*
50 * When VBOOT_ENABLE_CBFS_FALLBACK is enabled and a file is not available in the
51 * active RW region, the RO (COREBOOT) region will be used to locate the file.
52 *
53 * This functionality makes it possible to avoid duplicate files in the RO
54 * and RW partitions while maintaining updateability.
55 *
56 * Files can be added to the RO_REGION_ONLY config option to use this feature.
57 */
58 printk(BIOS_DEBUG, "Fall back to RO region for %s\n", name);
59 ret = cbfs_locate_file_in_region(fh, "COREBOOT", name, type);
60 }
61
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010062 if (!ret)
Bill XIEc79e96b2019-08-22 20:28:36 +080063 if (tspi_measure_cbfs_hook(fh, name))
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010064 return -1;
65
66 return ret;
Aaron Durbin899d13d2015-05-15 23:39:23 -050067}
68
69void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size)
70{
Aaron Durbin37a5d152015-09-17 16:09:30 -050071 struct cbfsf fh;
Aaron Durbin899d13d2015-05-15 23:39:23 -050072 size_t fsize;
73
74 if (cbfs_boot_locate(&fh, name, &type))
75 return NULL;
76
Aaron Durbin37a5d152015-09-17 16:09:30 -050077 fsize = region_device_sz(&fh.data);
Aaron Durbin899d13d2015-05-15 23:39:23 -050078
79 if (size != NULL)
80 *size = fsize;
81
Aaron Durbin37a5d152015-09-17 16:09:30 -050082 return rdev_mmap(&fh.data, 0, fsize);
Aaron Durbin899d13d2015-05-15 23:39:23 -050083}
84
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080085int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010086 const char *name, uint32_t *type)
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080087{
88 struct region_device rdev;
89
90 if (fmap_locate_area_as_rdev(region_name, &rdev)) {
91 LOG("%s region not found while looking for %s\n",
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010092 region_name, name);
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080093 return -1;
94 }
95
96 return cbfs_locate(fh, &rdev, name, type);
97}
98
Julius Werner09f29212015-09-29 13:51:35 -070099size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset,
100 size_t in_size, void *buffer, size_t buffer_size, uint32_t compression)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500101{
Julius Werner09f29212015-09-29 13:51:35 -0700102 size_t out_size;
103
104 switch (compression) {
105 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700106 if (buffer_size < in_size)
107 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700108 if (rdev_readat(rdev, buffer, offset, in_size) != in_size)
109 return 0;
110 return in_size;
111
112 case CBFS_COMPRESS_LZ4:
113 if ((ENV_BOOTBLOCK || ENV_VERSTAGE) &&
Julius Wernercd49cce2019-03-05 16:53:33 -0800114 !CONFIG(COMPRESS_PRERAM_STAGES))
Julius Werner09f29212015-09-29 13:51:35 -0700115 return 0;
116
117 /* Load the compressed image to the end of the available memory
118 * area for in-place decompression. It is the responsibility of
119 * the caller to ensure that buffer_size is large enough
120 * (see compression.h, guaranteed by cbfstool for stages). */
121 void *compr_start = buffer + buffer_size - in_size;
122 if (rdev_readat(rdev, compr_start, offset, in_size) != in_size)
123 return 0;
124
125 timestamp_add_now(TS_START_ULZ4F);
126 out_size = ulz4fn(compr_start, in_size, buffer, buffer_size);
127 timestamp_add_now(TS_END_ULZ4F);
128 return out_size;
129
130 case CBFS_COMPRESS_LZMA:
Kyösti Mälkkib5211ef2018-06-01 07:11:25 +0300131 /* We assume here romstage and postcar are never compressed. */
Julius Werner09f29212015-09-29 13:51:35 -0700132 if (ENV_BOOTBLOCK || ENV_VERSTAGE)
133 return 0;
Julius Wernercd49cce2019-03-05 16:53:33 -0800134 if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE))
Kyösti Mälkkib5211ef2018-06-01 07:11:25 +0300135 return 0;
Lee Leahyd950f512016-07-25 09:53:35 -0700136 if ((ENV_ROMSTAGE || ENV_POSTCAR)
Julius Wernercd49cce2019-03-05 16:53:33 -0800137 && !CONFIG(COMPRESS_RAMSTAGE))
Julius Werner09f29212015-09-29 13:51:35 -0700138 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700139 void *map = rdev_mmap(rdev, offset, in_size);
140 if (map == NULL)
141 return 0;
142
143 /* Note: timestamp not useful for memory-mapped media (x86) */
144 timestamp_add_now(TS_START_ULZMA);
145 out_size = ulzman(map, in_size, buffer, buffer_size);
146 timestamp_add_now(TS_END_ULZMA);
147
148 rdev_munmap(rdev, map);
149
150 return out_size;
151
152 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500153 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700154 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500155}
156
Stefan Reinauer800379f2010-03-01 08:34:19 +0000157static inline int tohex4(unsigned int c)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000158{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800159 return (c <= 9) ? (c + '0') : (c - 10 + 'a');
Patrick Georgib203c2f2009-08-20 14:48:03 +0000160}
161
Martin Rotha616a4b2020-01-21 09:28:40 -0700162static void tohex8(unsigned int val, char *dest)
163{
164 dest[0] = tohex4((val >> 4) & 0xf);
165 dest[1] = tohex4(val & 0xf);
166}
167
Lee Leahyb2d834a2017-03-08 16:52:22 -0800168static void tohex16(unsigned int val, char *dest)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000169{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100170 dest[0] = tohex4(val >> 12);
171 dest[1] = tohex4((val >> 8) & 0xf);
172 dest[2] = tohex4((val >> 4) & 0xf);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800173 dest[3] = tohex4(val & 0xf);
Patrick Georgib203c2f2009-08-20 14:48:03 +0000174}
175
Aaron Durbin899d13d2015-05-15 23:39:23 -0500176void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
Peter Stuge483b7bb2009-04-14 07:40:01 +0000177{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800178 char name[17] = "pciXXXX,XXXX.rom";
Peter Stuge483b7bb2009-04-14 07:40:01 +0000179
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100180 tohex16(vendor, name + 3);
181 tohex16(device, name + 8);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000182
Aaron Durbin899d13d2015-05-15 23:39:23 -0500183 return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000184}
185
Martin Rotha616a4b2020-01-21 09:28:40 -0700186void *cbfs_boot_map_optionrom_revision(uint16_t vendor, uint16_t device, uint8_t rev)
187{
188 char name[20] = "pciXXXX,XXXX,XX.rom";
189
190 tohex16(vendor, name + 3);
191 tohex16(device, name + 8);
192 tohex8(rev, name + 13);
193
194 return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL);
195}
196
T Michael Turney809fa7b2018-04-12 13:36:40 -0700197size_t cbfs_boot_load_file(const char *name, void *buf, size_t buf_size,
198 uint32_t type)
Julius Wernerf975e552016-08-19 15:43:06 -0700199{
200 struct cbfsf fh;
201 uint32_t compression_algo;
202 size_t decompressed_size;
Julius Wernerf975e552016-08-19 15:43:06 -0700203
204 if (cbfs_boot_locate(&fh, name, &type) < 0)
205 return 0;
206
207 if (cbfsf_decompression_info(&fh, &compression_algo,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100208 &decompressed_size)
209 < 0
210 || decompressed_size > buf_size)
Julius Wernerf975e552016-08-19 15:43:06 -0700211 return 0;
212
213 return cbfs_load_and_decompress(&fh.data, 0, region_device_sz(&fh.data),
214 buf, buf_size, compression_algo);
215}
216
Aaron Durbin899d13d2015-05-15 23:39:23 -0500217int cbfs_prog_stage_load(struct prog *pstage)
218{
219 struct cbfs_stage stage;
220 uint8_t *load;
221 void *entry;
222 size_t fsize;
223 size_t foffset;
Aaron Durbin37a5d152015-09-17 16:09:30 -0500224 const struct region_device *fh = prog_rdev(pstage);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800225
Aaron Durbin899d13d2015-05-15 23:39:23 -0500226 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
Julius Wernerb29bd27b2015-12-03 11:29:12 -0800227 return -1;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500228
229 fsize = region_device_sz(fh);
230 fsize -= sizeof(stage);
231 foffset = 0;
232 foffset += sizeof(stage);
233
234 assert(fsize == stage.len);
235
236 /* Note: cbfs_stage fields are currently in the endianness of the
237 * running processor. */
238 load = (void *)(uintptr_t)stage.load;
239 entry = (void *)(uintptr_t)stage.entry;
240
Aaron Durbined253c82015-10-07 17:22:42 -0500241 /* Hacky way to not load programs over read only media. The stages
242 * that would hit this path initialize themselves. */
Arthur Heymans7c24de92019-10-25 16:53:29 +0200243 if ((ENV_BOOTBLOCK || ENV_VERSTAGE) && !CONFIG(NO_XIP_EARLY_STAGES) &&
Julius Wernercd49cce2019-03-05 16:53:33 -0800244 CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
Aaron Durbined253c82015-10-07 17:22:42 -0500245 void *mapping = rdev_mmap(fh, foffset, fsize);
246 rdev_munmap(fh, mapping);
247 if (mapping == load)
248 goto out;
249 }
250
Julius Werner09f29212015-09-29 13:51:35 -0700251 fsize = cbfs_load_and_decompress(fh, foffset, fsize, load,
252 stage.memlen, stage.compression);
253 if (!fsize)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500254 return -1;
255
256 /* Clear area not covered by file. */
257 memset(&load[fsize], 0, stage.memlen - fsize);
258
Aaron Durbin096f4572016-03-31 13:49:00 -0500259 prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL);
Aaron Durbined253c82015-10-07 17:22:42 -0500260
261out:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500262 prog_set_area(pstage, load, stage.memlen);
263 prog_set_entry(pstage, entry, NULL);
264
265 return 0;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800266}
Aaron Durbin6d720f32015-12-08 17:00:23 -0600267
Aaron Durbinfe338e22019-11-18 12:35:21 -0700268int cbfs_boot_region_device(struct region_device *rdev)
Aaron Durbin6d720f32015-12-08 17:00:23 -0600269{
Aaron Durbin6d720f32015-12-08 17:00:23 -0600270 boot_device_init();
Julius Werner815611e2019-12-05 22:29:07 -0800271 return vboot_locate_cbfs(rdev) &&
272 fmap_locate_area_as_rdev("COREBOOT", rdev);
Aaron Durbin6d720f32015-12-08 17:00:23 -0600273}