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