Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 3 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 4 | #include <assert.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 5 | #include <boot_device.h> |
| 6 | #include <cbfs.h> |
Julius Werner | 98eeb96 | 2019-12-11 15:47:42 -0800 | [diff] [blame] | 7 | #include <commonlib/bsd/compression.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 8 | #include <console/console.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 9 | #include <endian.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 10 | #include <fmap.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 11 | #include <lib.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 12 | #include <security/tpm/tspi/crtm.h> |
| 13 | #include <security/vboot/vboot_common.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 16 | #include <symbols.h> |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 17 | #include <timestamp.h> |
Patrick Georgi | 58a150a | 2016-05-02 17:22:29 +0800 | [diff] [blame] | 18 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 19 | #define ERROR(x...) printk(BIOS_ERR, "CBFS: " x) |
| 20 | #define LOG(x...) printk(BIOS_INFO, "CBFS: " x) |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 21 | #if CONFIG(DEBUG_CBFS) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 22 | #define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x) |
| 23 | #else |
| 24 | #define DEBUG(x...) |
| 25 | #endif |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 26 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 27 | int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 28 | { |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 29 | struct region_device rdev; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 30 | |
Aaron Durbin | fe338e2 | 2019-11-18 12:35:21 -0700 | [diff] [blame] | 31 | if (cbfs_boot_region_device(&rdev)) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 32 | return -1; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 33 | |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 34 | int ret = cbfs_locate(fh, &rdev, name, type); |
Wim Vervoorn | 114e2e8 | 2019-11-05 14:09:16 +0100 | [diff] [blame] | 35 | |
| 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 XIE | bad08c2 | 2020-02-13 11:11:35 +0800 | [diff] [blame] | 48 | 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 Vervoorn | 114e2e8 | 2019-11-05 14:09:16 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 54 | if (!ret) |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 55 | if (tspi_measure_cbfs_hook(fh, name)) |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 56 | return -1; |
| 57 | |
| 58 | return ret; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size) |
| 62 | { |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 63 | struct cbfsf fh; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 64 | size_t fsize; |
| 65 | |
| 66 | if (cbfs_boot_locate(&fh, name, &type)) |
| 67 | return NULL; |
| 68 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 69 | fsize = region_device_sz(&fh.data); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 70 | |
| 71 | if (size != NULL) |
| 72 | *size = fsize; |
| 73 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 74 | return rdev_mmap(&fh.data, 0, fsize); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Pratik Prajapati | 2a7708a | 2016-11-30 17:29:10 -0800 | [diff] [blame] | 77 | int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name, |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 78 | const char *name, uint32_t *type) |
Pratik Prajapati | 2a7708a | 2016-11-30 17:29:10 -0800 | [diff] [blame] | 79 | { |
| 80 | struct region_device rdev; |
Bill XIE | bad08c2 | 2020-02-13 11:11:35 +0800 | [diff] [blame] | 81 | int ret = 0; |
Pratik Prajapati | 2a7708a | 2016-11-30 17:29:10 -0800 | [diff] [blame] | 82 | if (fmap_locate_area_as_rdev(region_name, &rdev)) { |
| 83 | LOG("%s region not found while looking for %s\n", |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 84 | region_name, name); |
Pratik Prajapati | 2a7708a | 2016-11-30 17:29:10 -0800 | [diff] [blame] | 85 | return -1; |
| 86 | } |
| 87 | |
Bill XIE | bad08c2 | 2020-02-13 11:11:35 +0800 | [diff] [blame] | 88 | 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 Prajapati | 2a7708a | 2016-11-30 17:29:10 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 95 | size_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 Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 97 | { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 98 | size_t out_size; |
| 99 | |
| 100 | switch (compression) { |
| 101 | case CBFS_COMPRESS_NONE: |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 102 | if (buffer_size < in_size) |
| 103 | return 0; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 104 | if (rdev_readat(rdev, buffer, offset, in_size) != in_size) |
| 105 | return 0; |
| 106 | return in_size; |
| 107 | |
| 108 | case CBFS_COMPRESS_LZ4: |
Julius Werner | 21a4053 | 2020-04-21 16:03:53 -0700 | [diff] [blame^] | 109 | if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 110 | !CONFIG(COMPRESS_PRERAM_STAGES)) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 111 | 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älkki | b5211ef | 2018-06-01 07:11:25 +0300 | [diff] [blame] | 127 | /* We assume here romstage and postcar are never compressed. */ |
Julius Werner | 21a4053 | 2020-04-21 16:03:53 -0700 | [diff] [blame^] | 128 | if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 129 | return 0; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 130 | if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE)) |
Kyösti Mälkki | b5211ef | 2018-06-01 07:11:25 +0300 | [diff] [blame] | 131 | return 0; |
Lee Leahy | d950f51 | 2016-07-25 09:53:35 -0700 | [diff] [blame] | 132 | if ((ENV_ROMSTAGE || ENV_POSTCAR) |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 133 | && !CONFIG(COMPRESS_RAMSTAGE)) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 134 | return 0; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 135 | 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 Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 149 | return 0; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 150 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 153 | static inline int tohex4(unsigned int c) |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 154 | { |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 155 | return (c <= 9) ? (c + '0') : (c - 10 + 'a'); |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Martin Roth | a616a4b | 2020-01-21 09:28:40 -0700 | [diff] [blame] | 158 | static void tohex8(unsigned int val, char *dest) |
| 159 | { |
| 160 | dest[0] = tohex4((val >> 4) & 0xf); |
| 161 | dest[1] = tohex4(val & 0xf); |
| 162 | } |
| 163 | |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 164 | static void tohex16(unsigned int val, char *dest) |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 165 | { |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 166 | dest[0] = tohex4(val >> 12); |
| 167 | dest[1] = tohex4((val >> 8) & 0xf); |
| 168 | dest[2] = tohex4((val >> 4) & 0xf); |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 169 | dest[3] = tohex4(val & 0xf); |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 172 | void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device) |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 173 | { |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 174 | char name[17] = "pciXXXX,XXXX.rom"; |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 175 | |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 176 | tohex16(vendor, name + 3); |
| 177 | tohex16(device, name + 8); |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 178 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 179 | return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL); |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Martin Roth | a616a4b | 2020-01-21 09:28:40 -0700 | [diff] [blame] | 182 | void *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 Turney | 809fa7b | 2018-04-12 13:36:40 -0700 | [diff] [blame] | 193 | size_t cbfs_boot_load_file(const char *name, void *buf, size_t buf_size, |
| 194 | uint32_t type) |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 195 | { |
| 196 | struct cbfsf fh; |
| 197 | uint32_t compression_algo; |
| 198 | size_t decompressed_size; |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 199 | |
| 200 | if (cbfs_boot_locate(&fh, name, &type) < 0) |
| 201 | return 0; |
| 202 | |
| 203 | if (cbfsf_decompression_info(&fh, &compression_algo, |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 204 | &decompressed_size) |
| 205 | < 0 |
| 206 | || decompressed_size > buf_size) |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 207 | 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 Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 213 | int 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 Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 220 | const struct region_device *fh = prog_rdev(pstage); |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 221 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 222 | if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage)) |
Julius Werner | b29bd27b | 2015-12-03 11:29:12 -0800 | [diff] [blame] | 223 | return -1; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 224 | |
| 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 Durbin | ed253c8 | 2015-10-07 17:22:42 -0500 | [diff] [blame] | 237 | /* Hacky way to not load programs over read only media. The stages |
| 238 | * that would hit this path initialize themselves. */ |
Julius Werner | 21a4053 | 2020-04-21 16:03:53 -0700 | [diff] [blame^] | 239 | if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && |
| 240 | !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) { |
Aaron Durbin | ed253c8 | 2015-10-07 17:22:42 -0500 | [diff] [blame] | 241 | void *mapping = rdev_mmap(fh, foffset, fsize); |
| 242 | rdev_munmap(fh, mapping); |
| 243 | if (mapping == load) |
| 244 | goto out; |
| 245 | } |
| 246 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 247 | fsize = cbfs_load_and_decompress(fh, foffset, fsize, load, |
| 248 | stage.memlen, stage.compression); |
| 249 | if (!fsize) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 250 | return -1; |
| 251 | |
| 252 | /* Clear area not covered by file. */ |
| 253 | memset(&load[fsize], 0, stage.memlen - fsize); |
| 254 | |
Aaron Durbin | 096f457 | 2016-03-31 13:49:00 -0500 | [diff] [blame] | 255 | prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL); |
Aaron Durbin | ed253c8 | 2015-10-07 17:22:42 -0500 | [diff] [blame] | 256 | |
| 257 | out: |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 258 | prog_set_area(pstage, load, stage.memlen); |
| 259 | prog_set_entry(pstage, entry, NULL); |
| 260 | |
| 261 | return 0; |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 262 | } |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 263 | |
Aaron Durbin | fe338e2 | 2019-11-18 12:35:21 -0700 | [diff] [blame] | 264 | int cbfs_boot_region_device(struct region_device *rdev) |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 265 | { |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 266 | boot_device_init(); |
Julius Werner | 815611e | 2019-12-05 22:29:07 -0800 | [diff] [blame] | 267 | return vboot_locate_cbfs(rdev) && |
| 268 | fmap_locate_area_as_rdev("COREBOOT", rdev); |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 269 | } |