blob: c712f76be8bf70d712e39172d9fbc7ce88fcf332 [file] [log] [blame]
Peter Stuge483b7bb2009-04-14 07:40:01 +00001/*
2 * This file is part of the coreboot project.
3 *
Aaron Durbin899d13d2015-05-15 23:39:23 -05004 * Copyright (C) 2011 secunet Security Networks AG
5 * Copyright 2015 Google Inc.
Peter Stuge483b7bb2009-04-14 07:40:01 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Peter Stuge483b7bb2009-04-14 07:40:01 +000015 */
16
Aaron Durbin899d13d2015-05-15 23:39:23 -050017#include <assert.h>
Elyes HAOUAS351e3e52019-04-05 18:11:19 +020018#include <console/console.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050019#include <string.h>
20#include <stdlib.h>
21#include <boot_device.h>
22#include <cbfs.h>
Julius Werner98eeb962019-12-11 15:47:42 -080023#include <commonlib/bsd/compression.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050024#include <endian.h>
25#include <lib.h>
26#include <symbols.h>
Julius Werner09f29212015-09-29 13:51:35 -070027#include <timestamp.h>
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080028#include <fmap.h>
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010029#include <security/vboot/vboot_crtm.h>
Julius Werner815611e2019-12-05 22:29:07 -080030#include <security/vboot/vboot_common.h>
Patrick Georgi58a150a2016-05-02 17:22:29 +080031
Aaron Durbin899d13d2015-05-15 23:39:23 -050032#define ERROR(x...) printk(BIOS_ERR, "CBFS: " x)
33#define LOG(x...) printk(BIOS_INFO, "CBFS: " x)
Julius Wernercd49cce2019-03-05 16:53:33 -080034#if CONFIG(DEBUG_CBFS)
Aaron Durbin899d13d2015-05-15 23:39:23 -050035#define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x)
36#else
37#define DEBUG(x...)
38#endif
Peter Stuge483b7bb2009-04-14 07:40:01 +000039
Aaron Durbin37a5d152015-09-17 16:09:30 -050040int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
Aaron Durbin899d13d2015-05-15 23:39:23 -050041{
Aaron Durbin899d13d2015-05-15 23:39:23 -050042 struct region_device rdev;
Aaron Durbin899d13d2015-05-15 23:39:23 -050043
Aaron Durbinfe338e22019-11-18 12:35:21 -070044 if (cbfs_boot_region_device(&rdev))
Aaron Durbin899d13d2015-05-15 23:39:23 -050045 return -1;
Aaron Durbin899d13d2015-05-15 23:39:23 -050046
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010047 int ret = cbfs_locate(fh, &rdev, name, type);
Wim Vervoorn114e2e82019-11-05 14:09:16 +010048
49 if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && ret) {
50
51 /*
52 * When VBOOT_ENABLE_CBFS_FALLBACK is enabled and a file is not available in the
53 * active RW region, the RO (COREBOOT) region will be used to locate the file.
54 *
55 * This functionality makes it possible to avoid duplicate files in the RO
56 * and RW partitions while maintaining updateability.
57 *
58 * Files can be added to the RO_REGION_ONLY config option to use this feature.
59 */
60 printk(BIOS_DEBUG, "Fall back to RO region for %s\n", name);
61 ret = cbfs_locate_file_in_region(fh, "COREBOOT", name, type);
62 }
63
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010064 if (!ret)
65 if (vboot_measure_cbfs_hook(fh, name))
66 return -1;
67
68 return ret;
Aaron Durbin899d13d2015-05-15 23:39:23 -050069}
70
71void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size)
72{
Aaron Durbin37a5d152015-09-17 16:09:30 -050073 struct cbfsf fh;
Aaron Durbin899d13d2015-05-15 23:39:23 -050074 size_t fsize;
75
76 if (cbfs_boot_locate(&fh, name, &type))
77 return NULL;
78
Aaron Durbin37a5d152015-09-17 16:09:30 -050079 fsize = region_device_sz(&fh.data);
Aaron Durbin899d13d2015-05-15 23:39:23 -050080
81 if (size != NULL)
82 *size = fsize;
83
Aaron Durbin37a5d152015-09-17 16:09:30 -050084 return rdev_mmap(&fh.data, 0, fsize);
Aaron Durbin899d13d2015-05-15 23:39:23 -050085}
86
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080087int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010088 const char *name, uint32_t *type)
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080089{
90 struct region_device rdev;
91
92 if (fmap_locate_area_as_rdev(region_name, &rdev)) {
93 LOG("%s region not found while looking for %s\n",
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010094 region_name, name);
Pratik Prajapati2a7708a2016-11-30 17:29:10 -080095 return -1;
96 }
97
98 return cbfs_locate(fh, &rdev, name, type);
99}
100
Julius Werner09f29212015-09-29 13:51:35 -0700101size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset,
102 size_t in_size, void *buffer, size_t buffer_size, uint32_t compression)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500103{
Julius Werner09f29212015-09-29 13:51:35 -0700104 size_t out_size;
105
106 switch (compression) {
107 case CBFS_COMPRESS_NONE:
Julius Wernerf975e552016-08-19 15:43:06 -0700108 if (buffer_size < in_size)
109 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700110 if (rdev_readat(rdev, buffer, offset, in_size) != in_size)
111 return 0;
112 return in_size;
113
114 case CBFS_COMPRESS_LZ4:
115 if ((ENV_BOOTBLOCK || ENV_VERSTAGE) &&
Julius Wernercd49cce2019-03-05 16:53:33 -0800116 !CONFIG(COMPRESS_PRERAM_STAGES))
Julius Werner09f29212015-09-29 13:51:35 -0700117 return 0;
118
119 /* Load the compressed image to the end of the available memory
120 * area for in-place decompression. It is the responsibility of
121 * the caller to ensure that buffer_size is large enough
122 * (see compression.h, guaranteed by cbfstool for stages). */
123 void *compr_start = buffer + buffer_size - in_size;
124 if (rdev_readat(rdev, compr_start, offset, in_size) != in_size)
125 return 0;
126
127 timestamp_add_now(TS_START_ULZ4F);
128 out_size = ulz4fn(compr_start, in_size, buffer, buffer_size);
129 timestamp_add_now(TS_END_ULZ4F);
130 return out_size;
131
132 case CBFS_COMPRESS_LZMA:
Kyösti Mälkkib5211ef2018-06-01 07:11:25 +0300133 /* We assume here romstage and postcar are never compressed. */
Julius Werner09f29212015-09-29 13:51:35 -0700134 if (ENV_BOOTBLOCK || ENV_VERSTAGE)
135 return 0;
Julius Wernercd49cce2019-03-05 16:53:33 -0800136 if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE))
Kyösti Mälkkib5211ef2018-06-01 07:11:25 +0300137 return 0;
Lee Leahyd950f512016-07-25 09:53:35 -0700138 if ((ENV_ROMSTAGE || ENV_POSTCAR)
Julius Wernercd49cce2019-03-05 16:53:33 -0800139 && !CONFIG(COMPRESS_RAMSTAGE))
Julius Werner09f29212015-09-29 13:51:35 -0700140 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700141 void *map = rdev_mmap(rdev, offset, in_size);
142 if (map == NULL)
143 return 0;
144
145 /* Note: timestamp not useful for memory-mapped media (x86) */
146 timestamp_add_now(TS_START_ULZMA);
147 out_size = ulzman(map, in_size, buffer, buffer_size);
148 timestamp_add_now(TS_END_ULZMA);
149
150 rdev_munmap(rdev, map);
151
152 return out_size;
153
154 default:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500155 return 0;
Julius Werner09f29212015-09-29 13:51:35 -0700156 }
Aaron Durbin899d13d2015-05-15 23:39:23 -0500157}
158
Stefan Reinauer800379f2010-03-01 08:34:19 +0000159static inline int tohex4(unsigned int c)
Patrick Georgib203c2f2009-08-20 14:48:03 +0000160{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800161 return (c <= 9) ? (c + '0') : (c - 10 + 'a');
Patrick Georgib203c2f2009-08-20 14:48:03 +0000162}
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
Aaron Durbin899d13d2015-05-15 23:39:23 -0500182void *cbfs_boot_load_stage_by_name(const char *name)
Peter Stuge483b7bb2009-04-14 07:40:01 +0000183{
Aaron Durbin37a5d152015-09-17 16:09:30 -0500184 struct cbfsf fh;
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600185 struct prog stage = PROG_INIT(PROG_UNKNOWN, name);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500186 uint32_t type = CBFS_TYPE_STAGE;
Aaron Durbin3948e532015-03-20 13:00:20 -0500187
Aaron Durbin37a5d152015-09-17 16:09:30 -0500188 if (cbfs_boot_locate(&fh, name, &type))
Aaron Durbin899d13d2015-05-15 23:39:23 -0500189 return NULL;
Aaron Durbin3948e532015-03-20 13:00:20 -0500190
Aaron Durbin37a5d152015-09-17 16:09:30 -0500191 /* Chain data portion in the prog. */
192 cbfs_file_data(prog_rdev(&stage), &fh);
193
Aaron Durbin899d13d2015-05-15 23:39:23 -0500194 if (cbfs_prog_stage_load(&stage))
195 return NULL;
196
197 return prog_entry(&stage);
Peter Stuge483b7bb2009-04-14 07:40:01 +0000198}
199
T Michael Turney809fa7b2018-04-12 13:36:40 -0700200size_t cbfs_boot_load_file(const char *name, void *buf, size_t buf_size,
201 uint32_t type)
Julius Wernerf975e552016-08-19 15:43:06 -0700202{
203 struct cbfsf fh;
204 uint32_t compression_algo;
205 size_t decompressed_size;
Julius Wernerf975e552016-08-19 15:43:06 -0700206
207 if (cbfs_boot_locate(&fh, name, &type) < 0)
208 return 0;
209
210 if (cbfsf_decompression_info(&fh, &compression_algo,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100211 &decompressed_size)
212 < 0
213 || decompressed_size > buf_size)
Julius Wernerf975e552016-08-19 15:43:06 -0700214 return 0;
215
216 return cbfs_load_and_decompress(&fh.data, 0, region_device_sz(&fh.data),
217 buf, buf_size, compression_algo);
218}
219
Kyösti Mälkki9d6f3652016-06-28 07:38:46 +0300220size_t cbfs_prog_stage_section(struct prog *pstage, uintptr_t *base)
221{
222 struct cbfs_stage stage;
223 const struct region_device *fh = prog_rdev(pstage);
224
225 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
226 return 0;
227
228 *base = (uintptr_t)stage.load;
229 return stage.memlen;
230}
231
Aaron Durbin899d13d2015-05-15 23:39:23 -0500232int cbfs_prog_stage_load(struct prog *pstage)
233{
234 struct cbfs_stage stage;
235 uint8_t *load;
236 void *entry;
237 size_t fsize;
238 size_t foffset;
Aaron Durbin37a5d152015-09-17 16:09:30 -0500239 const struct region_device *fh = prog_rdev(pstage);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800240
Aaron Durbin899d13d2015-05-15 23:39:23 -0500241 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
Julius Wernerb29bd27b2015-12-03 11:29:12 -0800242 return -1;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500243
244 fsize = region_device_sz(fh);
245 fsize -= sizeof(stage);
246 foffset = 0;
247 foffset += sizeof(stage);
248
249 assert(fsize == stage.len);
250
251 /* Note: cbfs_stage fields are currently in the endianness of the
252 * running processor. */
253 load = (void *)(uintptr_t)stage.load;
254 entry = (void *)(uintptr_t)stage.entry;
255
Aaron Durbined253c82015-10-07 17:22:42 -0500256 /* Hacky way to not load programs over read only media. The stages
257 * that would hit this path initialize themselves. */
Arthur Heymans7c24de92019-10-25 16:53:29 +0200258 if ((ENV_BOOTBLOCK || ENV_VERSTAGE) && !CONFIG(NO_XIP_EARLY_STAGES) &&
Julius Wernercd49cce2019-03-05 16:53:33 -0800259 CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
Aaron Durbined253c82015-10-07 17:22:42 -0500260 void *mapping = rdev_mmap(fh, foffset, fsize);
261 rdev_munmap(fh, mapping);
262 if (mapping == load)
263 goto out;
264 }
265
Julius Werner09f29212015-09-29 13:51:35 -0700266 fsize = cbfs_load_and_decompress(fh, foffset, fsize, load,
267 stage.memlen, stage.compression);
268 if (!fsize)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500269 return -1;
270
271 /* Clear area not covered by file. */
272 memset(&load[fsize], 0, stage.memlen - fsize);
273
Aaron Durbin096f4572016-03-31 13:49:00 -0500274 prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL);
Aaron Durbined253c82015-10-07 17:22:42 -0500275
276out:
Aaron Durbin899d13d2015-05-15 23:39:23 -0500277 prog_set_area(pstage, load, stage.memlen);
278 prog_set_entry(pstage, entry, NULL);
279
280 return 0;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800281}
Aaron Durbin6d720f32015-12-08 17:00:23 -0600282
Aaron Durbinfe338e22019-11-18 12:35:21 -0700283int cbfs_boot_region_device(struct region_device *rdev)
Aaron Durbin6d720f32015-12-08 17:00:23 -0600284{
Aaron Durbin6d720f32015-12-08 17:00:23 -0600285 boot_device_init();
Julius Werner815611e2019-12-05 22:29:07 -0800286 return vboot_locate_cbfs(rdev) &&
287 fmap_locate_area_as_rdev("COREBOOT", rdev);
Aaron Durbin6d720f32015-12-08 17:00:23 -0600288}