Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 2 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 3 | #include <assert.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 4 | #include <boot_device.h> |
| 5 | #include <cbfs.h> |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame^] | 6 | #include <cbfs_private.h> |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 7 | #include <cbmem.h> |
Julius Werner | 98eeb96 | 2019-12-11 15:47:42 -0800 | [diff] [blame] | 8 | #include <commonlib/bsd/compression.h> |
Aaron Durbin | 0b418bd | 2020-10-08 14:56:03 -0600 | [diff] [blame] | 9 | #include <commonlib/endian.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 10 | #include <console/console.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 11 | #include <fmap.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 12 | #include <lib.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 13 | #include <security/tpm/tspi/crtm.h> |
| 14 | #include <security/vboot/vboot_common.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 17 | #include <symbols.h> |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 18 | #include <timestamp.h> |
Patrick Georgi | 58a150a | 2016-05-02 17:22:29 +0800 | [diff] [blame] | 19 | |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame^] | 20 | cb_err_t cbfs_boot_lookup(const char *name, bool force_ro, |
| 21 | union cbfs_mdata *mdata, struct region_device *rdev) |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 22 | { |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame^] | 23 | const struct cbfs_boot_device *cbd = cbfs_get_boot_device(force_ro); |
| 24 | if (!cbd) |
| 25 | return CB_ERR; |
| 26 | |
| 27 | size_t data_offset; |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 28 | cb_err_t err = CB_CBFS_CACHE_FULL; |
| 29 | if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM) |
| 30 | err = cbfs_mcache_lookup(cbd->mcache, cbd->mcache_size, |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame^] | 31 | name, mdata, &data_offset); |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 32 | if (err == CB_CBFS_CACHE_FULL) |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame^] | 33 | err = cbfs_lookup(&cbd->rdev, name, mdata, &data_offset, NULL); |
| 34 | |
| 35 | if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && !force_ro && |
| 36 | err == CB_CBFS_NOT_FOUND) { |
| 37 | printk(BIOS_INFO, "CBFS: Fall back to RO region for %s\n", |
| 38 | name); |
| 39 | return cbfs_boot_lookup(name, true, mdata, rdev); |
| 40 | } |
| 41 | if (err) |
| 42 | return err; |
| 43 | |
| 44 | if (rdev_chain(rdev, &cbd->rdev, data_offset, be32toh(mdata->h.len))) |
| 45 | return CB_ERR; |
| 46 | |
| 47 | if (tspi_measure_cbfs_hook(rdev, name, be32toh(mdata->h.type))) |
| 48 | return CB_ERR; |
| 49 | |
| 50 | return CB_SUCCESS; |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 53 | 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] | 54 | { |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame^] | 55 | if (cbfs_boot_lookup(name, false, &fh->mdata, &fh->data)) |
Julius Werner | 1cd013b | 2019-12-11 16:50:02 -0800 | [diff] [blame] | 56 | return -1; |
| 57 | |
| 58 | size_t msize = be32toh(fh->mdata.h.offset); |
| 59 | if (rdev_chain(&fh->metadata, &addrspace_32bit.rdev, |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame^] | 60 | (uintptr_t)&fh->mdata, msize)) |
Julius Werner | 1cd013b | 2019-12-11 16:50:02 -0800 | [diff] [blame] | 61 | return -1; |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame^] | 62 | |
Julius Werner | 1cd013b | 2019-12-11 16:50:02 -0800 | [diff] [blame] | 63 | if (type) { |
| 64 | if (!*type) |
| 65 | *type = be32toh(fh->mdata.h.type); |
| 66 | else if (*type != be32toh(fh->mdata.h.type)) |
| 67 | return -1; |
Wim Vervoorn | 114e2e8 | 2019-11-05 14:09:16 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Julius Werner | 1cd013b | 2019-12-11 16:50:02 -0800 | [diff] [blame] | 70 | return 0; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size) |
| 74 | { |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 75 | struct cbfsf fh; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 76 | size_t fsize; |
| 77 | |
| 78 | if (cbfs_boot_locate(&fh, name, &type)) |
| 79 | return NULL; |
| 80 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 81 | fsize = region_device_sz(&fh.data); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 82 | |
| 83 | if (size != NULL) |
| 84 | *size = fsize; |
| 85 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 86 | return rdev_mmap(&fh.data, 0, fsize); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Pratik Prajapati | 2a7708a | 2016-11-30 17:29:10 -0800 | [diff] [blame] | 89 | 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] | 90 | const char *name, uint32_t *type) |
Pratik Prajapati | 2a7708a | 2016-11-30 17:29:10 -0800 | [diff] [blame] | 91 | { |
| 92 | struct region_device rdev; |
Bill XIE | bad08c2 | 2020-02-13 11:11:35 +0800 | [diff] [blame] | 93 | int ret = 0; |
Pratik Prajapati | 2a7708a | 2016-11-30 17:29:10 -0800 | [diff] [blame] | 94 | if (fmap_locate_area_as_rdev(region_name, &rdev)) { |
| 95 | LOG("%s region not found while looking for %s\n", |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 96 | region_name, name); |
Pratik Prajapati | 2a7708a | 2016-11-30 17:29:10 -0800 | [diff] [blame] | 97 | return -1; |
| 98 | } |
| 99 | |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame^] | 100 | uint32_t dummy_type = 0; |
| 101 | if (!type) |
| 102 | type = &dummy_type; |
| 103 | |
Bill XIE | bad08c2 | 2020-02-13 11:11:35 +0800 | [diff] [blame] | 104 | ret = cbfs_locate(fh, &rdev, name, type); |
| 105 | if (!ret) |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame^] | 106 | if (tspi_measure_cbfs_hook(&rdev, name, *type)) |
Bill XIE | bad08c2 | 2020-02-13 11:11:35 +0800 | [diff] [blame] | 107 | return -1; |
| 108 | return ret; |
Pratik Prajapati | 2a7708a | 2016-11-30 17:29:10 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 111 | static inline bool fsps_env(void) |
| 112 | { |
| 113 | /* FSP-S is assumed to be loaded in ramstage. */ |
| 114 | if (ENV_RAMSTAGE) |
| 115 | return true; |
| 116 | return false; |
| 117 | } |
| 118 | |
Aaron Durbin | ecbfa99 | 2020-05-15 17:01:58 -0600 | [diff] [blame] | 119 | static inline bool fspm_env(void) |
| 120 | { |
| 121 | /* FSP-M is assumed to be loaded in romstage. */ |
| 122 | if (ENV_ROMSTAGE) |
| 123 | return true; |
| 124 | return false; |
| 125 | } |
| 126 | |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 127 | static inline bool cbfs_lz4_enabled(void) |
| 128 | { |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 129 | if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZ4)) |
| 130 | return true; |
Aaron Durbin | ecbfa99 | 2020-05-15 17:01:58 -0600 | [diff] [blame] | 131 | if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZ4)) |
| 132 | return true; |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 133 | |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 134 | if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES)) |
| 135 | return false; |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | static inline bool cbfs_lzma_enabled(void) |
| 141 | { |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 142 | if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZMA)) |
| 143 | return true; |
Aaron Durbin | ecbfa99 | 2020-05-15 17:01:58 -0600 | [diff] [blame] | 144 | if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA)) |
| 145 | return true; |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 146 | /* We assume here romstage and postcar are never compressed. */ |
| 147 | if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) |
| 148 | return false; |
| 149 | if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE)) |
| 150 | return false; |
| 151 | if ((ENV_ROMSTAGE || ENV_POSTCAR) |
| 152 | && !CONFIG(COMPRESS_RAMSTAGE)) |
| 153 | return false; |
| 154 | return true; |
| 155 | } |
| 156 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 157 | size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset, |
| 158 | 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] | 159 | { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 160 | size_t out_size; |
Aaron Durbin | 84f394e | 2020-05-26 16:16:42 -0600 | [diff] [blame] | 161 | void *map; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 162 | |
| 163 | switch (compression) { |
| 164 | case CBFS_COMPRESS_NONE: |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 165 | if (buffer_size < in_size) |
| 166 | return 0; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 167 | if (rdev_readat(rdev, buffer, offset, in_size) != in_size) |
| 168 | return 0; |
| 169 | return in_size; |
| 170 | |
| 171 | case CBFS_COMPRESS_LZ4: |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 172 | if (!cbfs_lz4_enabled()) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 173 | return 0; |
| 174 | |
Aaron Durbin | 84f394e | 2020-05-26 16:16:42 -0600 | [diff] [blame] | 175 | /* cbfs_stage_load_and_decompress() takes care of in-place |
| 176 | lz4 decompression by setting up the rdev to be in memory. */ |
| 177 | map = rdev_mmap(rdev, offset, in_size); |
| 178 | if (map == NULL) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 179 | return 0; |
| 180 | |
| 181 | timestamp_add_now(TS_START_ULZ4F); |
Aaron Durbin | 84f394e | 2020-05-26 16:16:42 -0600 | [diff] [blame] | 182 | out_size = ulz4fn(map, in_size, buffer, buffer_size); |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 183 | timestamp_add_now(TS_END_ULZ4F); |
Aaron Durbin | 84f394e | 2020-05-26 16:16:42 -0600 | [diff] [blame] | 184 | |
| 185 | rdev_munmap(rdev, map); |
| 186 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 187 | return out_size; |
| 188 | |
| 189 | case CBFS_COMPRESS_LZMA: |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 190 | if (!cbfs_lzma_enabled()) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 191 | return 0; |
Aaron Durbin | 84f394e | 2020-05-26 16:16:42 -0600 | [diff] [blame] | 192 | map = rdev_mmap(rdev, offset, in_size); |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 193 | if (map == NULL) |
| 194 | return 0; |
| 195 | |
| 196 | /* Note: timestamp not useful for memory-mapped media (x86) */ |
| 197 | timestamp_add_now(TS_START_ULZMA); |
| 198 | out_size = ulzman(map, in_size, buffer, buffer_size); |
| 199 | timestamp_add_now(TS_END_ULZMA); |
| 200 | |
| 201 | rdev_munmap(rdev, map); |
| 202 | |
| 203 | return out_size; |
| 204 | |
| 205 | default: |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 206 | return 0; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 207 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Aaron Durbin | 84f394e | 2020-05-26 16:16:42 -0600 | [diff] [blame] | 210 | static size_t cbfs_stage_load_and_decompress(const struct region_device *rdev, |
| 211 | size_t offset, size_t in_size, void *buffer, size_t buffer_size, |
| 212 | uint32_t compression) |
| 213 | { |
| 214 | struct region_device rdev_src; |
| 215 | |
| 216 | if (compression == CBFS_COMPRESS_LZ4) { |
| 217 | if (!cbfs_lz4_enabled()) |
| 218 | return 0; |
| 219 | /* Load the compressed image to the end of the available memory |
| 220 | * area for in-place decompression. It is the responsibility of |
| 221 | * the caller to ensure that buffer_size is large enough |
| 222 | * (see compression.h, guaranteed by cbfstool for stages). */ |
| 223 | void *compr_start = buffer + buffer_size - in_size; |
| 224 | if (rdev_readat(rdev, compr_start, offset, in_size) != in_size) |
| 225 | return 0; |
| 226 | /* Create a region device backed by memory. */ |
| 227 | rdev_chain(&rdev_src, &addrspace_32bit.rdev, |
| 228 | (uintptr_t)compr_start, in_size); |
| 229 | |
| 230 | return cbfs_load_and_decompress(&rdev_src, 0, in_size, buffer, |
| 231 | buffer_size, compression); |
| 232 | } |
| 233 | |
| 234 | /* All other algorithms can use the generic implementation. */ |
| 235 | return cbfs_load_and_decompress(rdev, offset, in_size, buffer, |
| 236 | buffer_size, compression); |
| 237 | } |
| 238 | |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 239 | static inline int tohex4(unsigned int c) |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 240 | { |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 241 | return (c <= 9) ? (c + '0') : (c - 10 + 'a'); |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Martin Roth | a616a4b | 2020-01-21 09:28:40 -0700 | [diff] [blame] | 244 | static void tohex8(unsigned int val, char *dest) |
| 245 | { |
| 246 | dest[0] = tohex4((val >> 4) & 0xf); |
| 247 | dest[1] = tohex4(val & 0xf); |
| 248 | } |
| 249 | |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 250 | static void tohex16(unsigned int val, char *dest) |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 251 | { |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 252 | dest[0] = tohex4(val >> 12); |
| 253 | dest[1] = tohex4((val >> 8) & 0xf); |
| 254 | dest[2] = tohex4((val >> 4) & 0xf); |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 255 | dest[3] = tohex4(val & 0xf); |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 258 | void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device) |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 259 | { |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 260 | char name[17] = "pciXXXX,XXXX.rom"; |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 261 | |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 262 | tohex16(vendor, name + 3); |
| 263 | tohex16(device, name + 8); |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 264 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 265 | return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL); |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Martin Roth | a616a4b | 2020-01-21 09:28:40 -0700 | [diff] [blame] | 268 | void *cbfs_boot_map_optionrom_revision(uint16_t vendor, uint16_t device, uint8_t rev) |
| 269 | { |
| 270 | char name[20] = "pciXXXX,XXXX,XX.rom"; |
| 271 | |
| 272 | tohex16(vendor, name + 3); |
| 273 | tohex16(device, name + 8); |
| 274 | tohex8(rev, name + 13); |
| 275 | |
| 276 | return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL); |
| 277 | } |
| 278 | |
T Michael Turney | 809fa7b | 2018-04-12 13:36:40 -0700 | [diff] [blame] | 279 | size_t cbfs_boot_load_file(const char *name, void *buf, size_t buf_size, |
| 280 | uint32_t type) |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 281 | { |
| 282 | struct cbfsf fh; |
| 283 | uint32_t compression_algo; |
| 284 | size_t decompressed_size; |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 285 | |
| 286 | if (cbfs_boot_locate(&fh, name, &type) < 0) |
| 287 | return 0; |
| 288 | |
| 289 | if (cbfsf_decompression_info(&fh, &compression_algo, |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 290 | &decompressed_size) |
| 291 | < 0 |
| 292 | || decompressed_size > buf_size) |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 293 | return 0; |
| 294 | |
| 295 | return cbfs_load_and_decompress(&fh.data, 0, region_device_sz(&fh.data), |
| 296 | buf, buf_size, compression_algo); |
| 297 | } |
| 298 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 299 | int cbfs_prog_stage_load(struct prog *pstage) |
| 300 | { |
| 301 | struct cbfs_stage stage; |
| 302 | uint8_t *load; |
| 303 | void *entry; |
| 304 | size_t fsize; |
| 305 | size_t foffset; |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 306 | const struct region_device *fh = prog_rdev(pstage); |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 307 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 308 | if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage)) |
Julius Werner | b29bd27b | 2015-12-03 11:29:12 -0800 | [diff] [blame] | 309 | return -1; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 310 | |
| 311 | fsize = region_device_sz(fh); |
| 312 | fsize -= sizeof(stage); |
| 313 | foffset = 0; |
| 314 | foffset += sizeof(stage); |
| 315 | |
Aaron Durbin | 0b418bd | 2020-10-08 14:56:03 -0600 | [diff] [blame] | 316 | /* cbfs_stage fields are written in little endian despite the other |
| 317 | cbfs data types being encoded in big endian. */ |
| 318 | stage.compression = read_le32(&stage.compression); |
| 319 | stage.entry = read_le64(&stage.entry); |
| 320 | stage.load = read_le64(&stage.load); |
| 321 | stage.len = read_le32(&stage.len); |
| 322 | stage.memlen = read_le32(&stage.memlen); |
| 323 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 324 | assert(fsize == stage.len); |
| 325 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 326 | load = (void *)(uintptr_t)stage.load; |
| 327 | entry = (void *)(uintptr_t)stage.entry; |
| 328 | |
Aaron Durbin | ed253c8 | 2015-10-07 17:22:42 -0500 | [diff] [blame] | 329 | /* Hacky way to not load programs over read only media. The stages |
| 330 | * that would hit this path initialize themselves. */ |
Julius Werner | 21a4053 | 2020-04-21 16:03:53 -0700 | [diff] [blame] | 331 | if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && |
| 332 | !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) { |
Aaron Durbin | ed253c8 | 2015-10-07 17:22:42 -0500 | [diff] [blame] | 333 | void *mapping = rdev_mmap(fh, foffset, fsize); |
| 334 | rdev_munmap(fh, mapping); |
| 335 | if (mapping == load) |
| 336 | goto out; |
| 337 | } |
| 338 | |
Aaron Durbin | 84f394e | 2020-05-26 16:16:42 -0600 | [diff] [blame] | 339 | fsize = cbfs_stage_load_and_decompress(fh, foffset, fsize, load, |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 340 | stage.memlen, stage.compression); |
| 341 | if (!fsize) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 342 | return -1; |
| 343 | |
| 344 | /* Clear area not covered by file. */ |
| 345 | memset(&load[fsize], 0, stage.memlen - fsize); |
| 346 | |
Aaron Durbin | 096f457 | 2016-03-31 13:49:00 -0500 | [diff] [blame] | 347 | prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL); |
Aaron Durbin | ed253c8 | 2015-10-07 17:22:42 -0500 | [diff] [blame] | 348 | |
| 349 | out: |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 350 | prog_set_area(pstage, load, stage.memlen); |
| 351 | prog_set_entry(pstage, entry, NULL); |
| 352 | |
| 353 | return 0; |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 354 | } |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 355 | |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 356 | void cbfs_boot_device_find_mcache(struct cbfs_boot_device *cbd, uint32_t id) |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 357 | { |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 358 | if (CONFIG(NO_CBFS_MCACHE) || ENV_SMM) |
| 359 | return; |
| 360 | |
| 361 | const struct cbmem_entry *entry; |
| 362 | if (cbmem_possibly_online() && |
| 363 | (entry = cbmem_entry_find(id))) { |
| 364 | cbd->mcache = cbmem_entry_start(entry); |
| 365 | cbd->mcache_size = cbmem_entry_size(entry); |
| 366 | } else if (ENV_ROMSTAGE_OR_BEFORE) { |
| 367 | u8 *boundary = _ecbfs_mcache - REGION_SIZE(cbfs_mcache) * |
| 368 | CONFIG_CBFS_MCACHE_RW_PERCENTAGE / 100; |
| 369 | boundary = (u8 *)ALIGN_DOWN((uintptr_t)boundary, |
| 370 | CBFS_MCACHE_ALIGNMENT); |
| 371 | if (id == CBMEM_ID_CBFS_RO_MCACHE) { |
| 372 | cbd->mcache = _cbfs_mcache; |
| 373 | cbd->mcache_size = boundary - _cbfs_mcache; |
| 374 | } else if (id == CBMEM_ID_CBFS_RW_MCACHE) { |
| 375 | cbd->mcache = boundary; |
| 376 | cbd->mcache_size = _ecbfs_mcache - boundary; |
| 377 | } |
| 378 | } |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 379 | } |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 380 | |
| 381 | const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro) |
| 382 | { |
| 383 | static struct cbfs_boot_device ro; |
| 384 | |
| 385 | /* Ensure we always init RO mcache, even if first file is from RW. |
| 386 | Otherwise it may not be available when needed in later stages. */ |
| 387 | if (ENV_INITIAL_STAGE && !force_ro && !region_device_sz(&ro.rdev)) |
| 388 | cbfs_get_boot_device(true); |
| 389 | |
| 390 | if (!force_ro) { |
| 391 | const struct cbfs_boot_device *rw = vboot_get_cbfs_boot_device(); |
| 392 | /* This will return NULL if vboot isn't enabled, didn't run yet |
| 393 | or decided to boot into recovery mode. */ |
| 394 | if (rw) |
| 395 | return rw; |
| 396 | } |
| 397 | |
| 398 | if (region_device_sz(&ro.rdev)) |
| 399 | return &ro; |
| 400 | |
| 401 | if (fmap_locate_area_as_rdev("COREBOOT", &ro.rdev)) |
| 402 | return NULL; |
| 403 | |
| 404 | cbfs_boot_device_find_mcache(&ro, CBMEM_ID_CBFS_RO_MCACHE); |
| 405 | |
| 406 | if (ENV_INITIAL_STAGE && !CONFIG(NO_CBFS_MCACHE)) { |
| 407 | cb_err_t err = cbfs_mcache_build(&ro.rdev, ro.mcache, |
| 408 | ro.mcache_size, NULL); |
| 409 | if (err && err != CB_CBFS_CACHE_FULL) |
| 410 | die("Failed to build RO mcache"); |
| 411 | } |
| 412 | |
| 413 | return &ro; |
| 414 | } |
| 415 | |
| 416 | #if !CONFIG(NO_CBFS_MCACHE) |
| 417 | static void mcache_to_cbmem(const struct cbfs_boot_device *cbd, u32 cbmem_id) |
| 418 | { |
| 419 | if (!cbd) |
| 420 | return; |
| 421 | |
| 422 | size_t real_size = cbfs_mcache_real_size(cbd->mcache, cbd->mcache_size); |
| 423 | void *cbmem_mcache = cbmem_add(cbmem_id, real_size); |
| 424 | if (!cbmem_mcache) { |
| 425 | printk(BIOS_ERR, "ERROR: Cannot allocate CBMEM mcache %#x (%#zx bytes)!\n", |
| 426 | cbmem_id, real_size); |
| 427 | return; |
| 428 | } |
| 429 | memcpy(cbmem_mcache, cbd->mcache, real_size); |
| 430 | } |
| 431 | |
| 432 | static void cbfs_mcache_migrate(int unused) |
| 433 | { |
| 434 | mcache_to_cbmem(vboot_get_cbfs_boot_device(), CBMEM_ID_CBFS_RW_MCACHE); |
| 435 | mcache_to_cbmem(cbfs_get_boot_device(true), CBMEM_ID_CBFS_RO_MCACHE); |
| 436 | } |
| 437 | ROMSTAGE_CBMEM_INIT_HOOK(cbfs_mcache_migrate) |
| 438 | #endif |