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