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 | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 6 | #include <cbmem.h> |
Julius Werner | 57d4bc6 | 2021-11-15 10:13:37 -0800 | [diff] [blame] | 7 | #include <commonlib/bsd/cbfs_private.h> |
Julius Werner | 98eeb96 | 2019-12-11 15:47:42 -0800 | [diff] [blame] | 8 | #include <commonlib/bsd/compression.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 9 | #include <console/console.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> |
Raul E Rangel | 4cfb862 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 12 | #include <list.h> |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 13 | #include <metadata_hash.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 14 | #include <security/tpm/tspi/crtm.h> |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 15 | #include <security/vboot/vboot_common.h> |
Julius Werner | d96ca24 | 2022-08-08 18:08:35 -0700 | [diff] [blame] | 16 | #include <security/vboot/misc.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 19 | #include <symbols.h> |
Raul E Rangel | 4cfb862 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 20 | #include <thread.h> |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 21 | #include <timestamp.h> |
Patrick Georgi | 58a150a | 2016-05-02 17:22:29 +0800 | [diff] [blame] | 22 | |
Jeremy Compostella | 226f51c | 2023-10-12 09:40:12 -0700 | [diff] [blame] | 23 | #if ENV_X86 && (ENV_POSTCAR || ENV_SMM) |
| 24 | struct mem_pool cbfs_cache = MEM_POOL_INIT(NULL, 0, 0); |
| 25 | #elif CONFIG(POSTRAM_CBFS_CACHE_IN_BSS) && ENV_RAMSTAGE |
| 26 | static u8 cache_buffer[CONFIG_RAMSTAGE_CBFS_CACHE_SIZE]; |
| 27 | struct mem_pool cbfs_cache = |
| 28 | MEM_POOL_INIT(cache_buffer, sizeof(cache_buffer), CONFIG_CBFS_CACHE_ALIGN); |
| 29 | #else |
Raul E Rangel | 5ac82dc | 2021-07-23 16:43:18 -0600 | [diff] [blame] | 30 | struct mem_pool cbfs_cache = |
Raul E Rangel | 6938f35 | 2021-07-23 16:43:18 -0600 | [diff] [blame] | 31 | MEM_POOL_INIT(_cbfs_cache, REGION_SIZE(cbfs_cache), CONFIG_CBFS_CACHE_ALIGN); |
Jeremy Compostella | 226f51c | 2023-10-12 09:40:12 -0700 | [diff] [blame] | 32 | #endif |
Julius Werner | 9b1f3cc | 2020-12-30 17:30:12 -0800 | [diff] [blame] | 33 | |
| 34 | static void switch_to_postram_cache(int unused) |
| 35 | { |
| 36 | if (_preram_cbfs_cache != _postram_cbfs_cache) |
Raul E Rangel | 5ac82dc | 2021-07-23 16:43:18 -0600 | [diff] [blame] | 37 | mem_pool_init(&cbfs_cache, _postram_cbfs_cache, REGION_SIZE(postram_cbfs_cache), |
Raul E Rangel | 6938f35 | 2021-07-23 16:43:18 -0600 | [diff] [blame] | 38 | CONFIG_CBFS_CACHE_ALIGN); |
Julius Werner | 9b1f3cc | 2020-12-30 17:30:12 -0800 | [diff] [blame] | 39 | } |
Kyösti Mälkki | fa3bc04 | 2022-03-31 07:40:10 +0300 | [diff] [blame] | 40 | CBMEM_CREATION_HOOK(switch_to_postram_cache); |
Julius Werner | 9b1f3cc | 2020-12-30 17:30:12 -0800 | [diff] [blame] | 41 | |
Julius Werner | 69cc557 | 2022-03-04 17:49:56 -0800 | [diff] [blame] | 42 | enum cb_err _cbfs_boot_lookup(const char *name, bool force_ro, |
| 43 | union cbfs_mdata *mdata, struct region_device *rdev) |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 44 | { |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame] | 45 | const struct cbfs_boot_device *cbd = cbfs_get_boot_device(force_ro); |
| 46 | if (!cbd) |
| 47 | return CB_ERR; |
| 48 | |
| 49 | size_t data_offset; |
Julius Werner | 69cc557 | 2022-03-04 17:49:56 -0800 | [diff] [blame] | 50 | enum cb_err err = CB_CBFS_CACHE_FULL; |
Julius Werner | 34cf073 | 2020-12-08 14:21:43 -0800 | [diff] [blame] | 51 | if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size) |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 52 | err = cbfs_mcache_lookup(cbd->mcache, cbd->mcache_size, |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 53 | name, mdata, &data_offset); |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 54 | if (err == CB_CBFS_CACHE_FULL) { |
| 55 | struct vb2_hash *metadata_hash = NULL; |
| 56 | if (CONFIG(TOCTOU_SAFETY)) { |
| 57 | if (ENV_SMM) /* Cannot provide TOCTOU safety for SMM */ |
| 58 | dead_code(); |
Julius Werner | 34cf073 | 2020-12-08 14:21:43 -0800 | [diff] [blame] | 59 | if (!cbd->mcache_size) |
| 60 | die("Cannot access CBFS TOCTOU-safely in " ENV_STRING " before CBMEM init!\n"); |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 61 | /* We can only reach this for the RW CBFS -- an mcache overflow in the |
| 62 | RO CBFS would have been caught when building the mcache in cbfs_get |
| 63 | boot_device(). (Note that TOCTOU_SAFETY implies !NO_CBFS_MCACHE.) */ |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 64 | assert(cbd == vboot_get_cbfs_boot_device()); |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 65 | if (!CONFIG(VBOOT) |
| 66 | || vb2api_get_metadata_hash(vboot_get_context(), &metadata_hash) |
| 67 | != VB2_SUCCESS) |
| 68 | die("Failed to get RW metadata hash"); |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 69 | } |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 70 | err = cbfs_lookup(&cbd->rdev, name, mdata, &data_offset, metadata_hash); |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 71 | } |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame] | 72 | |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 73 | if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && !force_ro && err == CB_CBFS_NOT_FOUND) { |
| 74 | printk(BIOS_INFO, "CBFS: Fall back to RO region for %s\n", name); |
Julius Werner | 57d4bc6 | 2021-11-15 10:13:37 -0800 | [diff] [blame] | 75 | return _cbfs_boot_lookup(name, true, mdata, rdev); |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame] | 76 | } |
Julius Werner | 0247fcf | 2020-12-03 12:41:00 -0800 | [diff] [blame] | 77 | if (err) { |
| 78 | if (err == CB_CBFS_NOT_FOUND) |
| 79 | printk(BIOS_WARNING, "CBFS: '%s' not found.\n", name); |
| 80 | else if (err == CB_CBFS_HASH_MISMATCH) |
| 81 | printk(BIOS_ERR, "CBFS ERROR: metadata hash mismatch!\n"); |
| 82 | else |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 83 | printk(BIOS_ERR, "CBFS ERROR: error %d when looking up '%s'\n", |
Julius Werner | 0247fcf | 2020-12-03 12:41:00 -0800 | [diff] [blame] | 84 | err, name); |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame] | 85 | return err; |
Julius Werner | 0247fcf | 2020-12-03 12:41:00 -0800 | [diff] [blame] | 86 | } |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame] | 87 | |
| 88 | if (rdev_chain(rdev, &cbd->rdev, data_offset, be32toh(mdata->h.len))) |
| 89 | return CB_ERR; |
| 90 | |
Julius Werner | 0d9072b | 2020-03-05 12:51:08 -0800 | [diff] [blame] | 91 | return CB_SUCCESS; |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Julius Werner | 9b1f3cc | 2020-12-30 17:30:12 -0800 | [diff] [blame] | 94 | void cbfs_unmap(void *mapping) |
Julius Werner | 834b3ec | 2020-03-04 16:52:08 -0800 | [diff] [blame] | 95 | { |
Julius Werner | 9b1f3cc | 2020-12-30 17:30:12 -0800 | [diff] [blame] | 96 | /* |
| 97 | * This is save to call with mappings that weren't allocated in the cache (e.g. x86 |
| 98 | * direct mappings) -- mem_pool_free() just does nothing for addresses it doesn't |
| 99 | * recognize. This hardcodes the assumption that if platforms implement an rdev_mmap() |
| 100 | * that requires a free() for the boot_device, they need to implement it via the |
| 101 | * cbfs_cache mem_pool. |
| 102 | */ |
Raul E Rangel | 3d7b984 | 2021-10-08 13:10:38 -0600 | [diff] [blame] | 103 | mem_pool_free(&cbfs_cache, mapping); |
Julius Werner | 834b3ec | 2020-03-04 16:52:08 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 106 | static inline bool fsps_env(void) |
| 107 | { |
| 108 | /* FSP-S is assumed to be loaded in ramstage. */ |
| 109 | if (ENV_RAMSTAGE) |
| 110 | return true; |
| 111 | return false; |
| 112 | } |
| 113 | |
Aaron Durbin | ecbfa99 | 2020-05-15 17:01:58 -0600 | [diff] [blame] | 114 | static inline bool fspm_env(void) |
| 115 | { |
| 116 | /* FSP-M is assumed to be loaded in romstage. */ |
Kyösti Mälkki | 11cac78 | 2022-04-07 07:16:48 +0300 | [diff] [blame] | 117 | if (ENV_RAMINIT) |
Aaron Durbin | ecbfa99 | 2020-05-15 17:01:58 -0600 | [diff] [blame] | 118 | return true; |
| 119 | return false; |
| 120 | } |
| 121 | |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 122 | static inline bool cbfs_lz4_enabled(void) |
| 123 | { |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 124 | if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZ4)) |
| 125 | return true; |
Aaron Durbin | ecbfa99 | 2020-05-15 17:01:58 -0600 | [diff] [blame] | 126 | if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZ4)) |
| 127 | return true; |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 128 | |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 129 | if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES)) |
| 130 | return false; |
| 131 | |
Julius Werner | 9b1f3cc | 2020-12-30 17:30:12 -0800 | [diff] [blame] | 132 | if (ENV_SMM) |
| 133 | return false; |
| 134 | |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
| 138 | static inline bool cbfs_lzma_enabled(void) |
| 139 | { |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 140 | if (fsps_env() && CONFIG(FSP_COMPRESS_FSP_S_LZMA)) |
| 141 | return true; |
Aaron Durbin | ecbfa99 | 2020-05-15 17:01:58 -0600 | [diff] [blame] | 142 | if (fspm_env() && CONFIG(FSP_COMPRESS_FSP_M_LZMA)) |
| 143 | return true; |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 144 | /* We assume here romstage and postcar are never compressed. */ |
| 145 | if (ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) |
| 146 | return false; |
| 147 | if (ENV_ROMSTAGE && CONFIG(POSTCAR_STAGE)) |
| 148 | return false; |
Martin Roth | 40729a5 | 2023-01-04 17:26:21 -0700 | [diff] [blame] | 149 | if ((ENV_ROMSTAGE || ENV_POSTCAR) && !CONFIG(COMPRESS_RAMSTAGE_LZMA)) |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 150 | return false; |
Julius Werner | 9b1f3cc | 2020-12-30 17:30:12 -0800 | [diff] [blame] | 151 | if (ENV_SMM) |
| 152 | return false; |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 156 | static bool cbfs_file_hash_mismatch(const void *buffer, size_t size, |
| 157 | const union cbfs_mdata *mdata, bool skip_verification) |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 158 | { |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 159 | /* Avoid linking hash functions when verification and measurement are disabled. */ |
| 160 | if (!CONFIG(CBFS_VERIFICATION) && !CONFIG(TPM_MEASURED_BOOT)) |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 161 | return false; |
| 162 | |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 163 | const struct vb2_hash *hash = NULL; |
| 164 | |
| 165 | if (CONFIG(CBFS_VERIFICATION) && !skip_verification) { |
| 166 | hash = cbfs_file_hash(mdata); |
| 167 | if (!hash) { |
| 168 | ERROR("'%s' does not have a file hash!\n", mdata->h.filename); |
| 169 | return true; |
| 170 | } |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 171 | |
| 172 | vb2_error_t rv = vb2_hash_verify(vboot_hwcrypto_allowed(), buffer, size, hash); |
| 173 | if (rv != VB2_SUCCESS) { |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 174 | ERROR("'%s' file hash mismatch!\n", mdata->h.filename); |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 175 | if (CONFIG(VBOOT_CBFS_INTEGRATION) && !vboot_recovery_mode_enabled() |
| 176 | && vboot_logic_executed()) |
| 177 | vboot_fail_and_reboot(vboot_get_context(), VB2_RECOVERY_FW_BODY, |
| 178 | rv); |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 179 | return true; |
| 180 | } |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 181 | } |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 182 | |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 183 | if (CONFIG(TPM_MEASURED_BOOT) && !ENV_SMM) { |
| 184 | struct vb2_hash calculated_hash; |
| 185 | |
| 186 | /* No need to re-hash file if we already have it from verification. */ |
| 187 | if (!hash || hash->algo != TPM_MEASURE_ALGO) { |
Julius Werner | d96ca24 | 2022-08-08 18:08:35 -0700 | [diff] [blame] | 188 | if (vb2_hash_calculate(vboot_hwcrypto_allowed(), buffer, size, |
| 189 | TPM_MEASURE_ALGO, &calculated_hash)) |
| 190 | hash = NULL; |
| 191 | else |
| 192 | hash = &calculated_hash; |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Julius Werner | d96ca24 | 2022-08-08 18:08:35 -0700 | [diff] [blame] | 195 | if (!hash || |
| 196 | tspi_cbfs_measurement(mdata->h.filename, be32toh(mdata->h.type), hash)) |
Sergii Dmytruk | 2710df7 | 2022-11-10 00:40:51 +0200 | [diff] [blame] | 197 | ERROR("failed to measure '%s' into TPM log\n", mdata->h.filename); |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 198 | /* We intentionally continue to boot on measurement errors. */ |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return false; |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static size_t cbfs_load_and_decompress(const struct region_device *rdev, void *buffer, |
| 205 | size_t buffer_size, uint32_t compression, |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 206 | const union cbfs_mdata *mdata, bool skip_verification) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 207 | { |
Julius Werner | eca99af | 2021-03-10 18:49:37 -0800 | [diff] [blame] | 208 | size_t in_size = region_device_sz(rdev); |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 209 | size_t out_size = 0; |
Aaron Durbin | 84f394e | 2020-05-26 16:16:42 -0600 | [diff] [blame] | 210 | void *map; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 211 | |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 212 | DEBUG("Decompressing %zu bytes from '%s' to %p with algo %d\n", |
| 213 | in_size, mdata->h.filename, buffer, compression); |
Julius Werner | 7778cf2 | 2021-01-05 18:54:19 -0800 | [diff] [blame] | 214 | |
Julius Werner | 6e303aa | 2023-05-25 18:26:32 -0700 | [diff] [blame] | 215 | if (CONFIG(CBFS_VERIFICATION) && !CONFIG(CBFS_ALLOW_UNVERIFIED_DECOMPRESSION) && |
| 216 | skip_verification && compression != CBFS_COMPRESS_NONE) { |
| 217 | ERROR("Refusing to decompress unverified file '%s' with algo %d\n", |
| 218 | mdata->h.filename, compression); |
| 219 | return 0; |
| 220 | } |
| 221 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 222 | switch (compression) { |
| 223 | case CBFS_COMPRESS_NONE: |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 224 | if (buffer_size < in_size) |
| 225 | return 0; |
Julius Werner | eca99af | 2021-03-10 18:49:37 -0800 | [diff] [blame] | 226 | if (rdev_readat(rdev, buffer, 0, in_size) != in_size) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 227 | return 0; |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 228 | if (cbfs_file_hash_mismatch(buffer, in_size, mdata, skip_verification)) |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 229 | return 0; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 230 | return in_size; |
| 231 | |
| 232 | case CBFS_COMPRESS_LZ4: |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 233 | if (!cbfs_lz4_enabled()) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | |
Julius Werner | eca99af | 2021-03-10 18:49:37 -0800 | [diff] [blame] | 236 | /* cbfs_prog_stage_load() takes care of in-place LZ4 decompression by |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 237 | setting up the rdev to be in memory. */ |
Julius Werner | eca99af | 2021-03-10 18:49:37 -0800 | [diff] [blame] | 238 | map = rdev_mmap_full(rdev); |
Aaron Durbin | 84f394e | 2020-05-26 16:16:42 -0600 | [diff] [blame] | 239 | if (map == NULL) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 240 | return 0; |
| 241 | |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 242 | if (!cbfs_file_hash_mismatch(map, in_size, mdata, skip_verification)) { |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 243 | timestamp_add_now(TS_ULZ4F_START); |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 244 | out_size = ulz4fn(map, in_size, buffer, buffer_size); |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 245 | timestamp_add_now(TS_ULZ4F_END); |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 246 | } |
Aaron Durbin | 84f394e | 2020-05-26 16:16:42 -0600 | [diff] [blame] | 247 | |
| 248 | rdev_munmap(rdev, map); |
| 249 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 250 | return out_size; |
| 251 | |
| 252 | case CBFS_COMPRESS_LZMA: |
Aaron Durbin | a121f95 | 2020-05-26 15:48:10 -0600 | [diff] [blame] | 253 | if (!cbfs_lzma_enabled()) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 254 | return 0; |
Julius Werner | eca99af | 2021-03-10 18:49:37 -0800 | [diff] [blame] | 255 | map = rdev_mmap_full(rdev); |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 256 | if (map == NULL) |
| 257 | return 0; |
| 258 | |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 259 | if (!cbfs_file_hash_mismatch(map, in_size, mdata, skip_verification)) { |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 260 | /* Note: timestamp not useful for memory-mapped media (x86) */ |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 261 | timestamp_add_now(TS_ULZMA_START); |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 262 | out_size = ulzman(map, in_size, buffer, buffer_size); |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 263 | timestamp_add_now(TS_ULZMA_END); |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 264 | } |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 265 | |
| 266 | rdev_munmap(rdev, map); |
| 267 | |
| 268 | return out_size; |
| 269 | |
| 270 | default: |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 271 | return 0; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 272 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 273 | } |
| 274 | |
Raul E Rangel | 4cfb862 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 275 | struct cbfs_preload_context { |
| 276 | struct region_device rdev; |
| 277 | struct thread_handle handle; |
| 278 | struct list_node list_node; |
| 279 | void *buffer; |
| 280 | char name[]; |
| 281 | }; |
| 282 | |
| 283 | static struct list_node cbfs_preload_context_list; |
| 284 | |
| 285 | static struct cbfs_preload_context *alloc_cbfs_preload_context(size_t additional) |
| 286 | { |
| 287 | struct cbfs_preload_context *context; |
| 288 | size_t size = sizeof(*context) + additional; |
| 289 | |
| 290 | context = mem_pool_alloc(&cbfs_cache, size); |
| 291 | |
| 292 | if (!context) |
| 293 | return NULL; |
| 294 | |
| 295 | memset(context, 0, size); |
| 296 | |
| 297 | return context; |
| 298 | } |
| 299 | |
| 300 | static void append_cbfs_preload_context(struct cbfs_preload_context *context) |
| 301 | { |
| 302 | list_append(&context->list_node, &cbfs_preload_context_list); |
| 303 | } |
| 304 | |
| 305 | static void free_cbfs_preload_context(struct cbfs_preload_context *context) |
| 306 | { |
| 307 | list_remove(&context->list_node); |
| 308 | |
| 309 | mem_pool_free(&cbfs_cache, context); |
| 310 | } |
| 311 | |
| 312 | static enum cb_err cbfs_preload_thread_entry(void *arg) |
| 313 | { |
| 314 | struct cbfs_preload_context *context = arg; |
| 315 | |
Julius Werner | f364eb9 | 2021-12-01 14:48:59 -0800 | [diff] [blame] | 316 | if (rdev_read_full(&context->rdev, context->buffer) < 0) { |
Raul E Rangel | 4cfb862 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 317 | ERROR("%s(name='%s') readat failed\n", __func__, context->name); |
| 318 | return CB_ERR; |
| 319 | } |
| 320 | |
| 321 | return CB_SUCCESS; |
| 322 | } |
| 323 | |
| 324 | void cbfs_preload(const char *name) |
| 325 | { |
| 326 | struct region_device rdev; |
| 327 | union cbfs_mdata mdata; |
| 328 | struct cbfs_preload_context *context; |
| 329 | bool force_ro = false; |
| 330 | size_t size; |
| 331 | |
| 332 | if (!CONFIG(CBFS_PRELOAD)) |
| 333 | dead_code(); |
| 334 | |
Raul E Rangel | 74d2218 | 2021-12-03 15:09:35 -0700 | [diff] [blame] | 335 | /* We don't want to cross the vboot boundary */ |
| 336 | if (ENV_ROMSTAGE && CONFIG(VBOOT_STARTS_IN_ROMSTAGE)) |
| 337 | return; |
| 338 | |
Raul E Rangel | 4cfb862 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 339 | DEBUG("%s(name='%s')\n", __func__, name); |
| 340 | |
Julius Werner | 57d4bc6 | 2021-11-15 10:13:37 -0800 | [diff] [blame] | 341 | if (_cbfs_boot_lookup(name, force_ro, &mdata, &rdev)) |
Raul E Rangel | 4cfb862 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 342 | return; |
| 343 | |
| 344 | size = region_device_sz(&rdev); |
| 345 | |
| 346 | context = alloc_cbfs_preload_context(strlen(name) + 1); |
| 347 | if (!context) { |
| 348 | ERROR("%s(name='%s') failed to allocate preload context\n", __func__, name); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | context->buffer = mem_pool_alloc(&cbfs_cache, size); |
| 353 | if (context->buffer == NULL) { |
| 354 | ERROR("%s(name='%s') failed to allocate %zu bytes for preload buffer\n", |
| 355 | __func__, name, size); |
| 356 | goto out; |
| 357 | } |
| 358 | |
| 359 | context->rdev = rdev; |
| 360 | strcpy(context->name, name); |
| 361 | |
| 362 | append_cbfs_preload_context(context); |
| 363 | |
| 364 | if (thread_run(&context->handle, cbfs_preload_thread_entry, context) == 0) |
| 365 | return; |
| 366 | |
| 367 | ERROR("%s(name='%s') failed to start preload thread\n", __func__, name); |
| 368 | mem_pool_free(&cbfs_cache, context->buffer); |
| 369 | |
| 370 | out: |
| 371 | free_cbfs_preload_context(context); |
| 372 | } |
| 373 | |
| 374 | static struct cbfs_preload_context *find_cbfs_preload_context(const char *name) |
| 375 | { |
| 376 | struct cbfs_preload_context *context; |
| 377 | |
| 378 | list_for_each(context, cbfs_preload_context_list, list_node) { |
| 379 | if (strcmp(context->name, name) == 0) |
| 380 | return context; |
| 381 | } |
| 382 | |
| 383 | return NULL; |
| 384 | } |
| 385 | |
| 386 | static enum cb_err get_preload_rdev(struct region_device *rdev, const char *name) |
| 387 | { |
| 388 | enum cb_err err; |
| 389 | struct cbfs_preload_context *context; |
| 390 | |
Arthur Heymans | 6acc05e | 2022-05-12 18:01:13 +0200 | [diff] [blame] | 391 | if (!CONFIG(CBFS_PRELOAD) || !ENV_SUPPORTS_COOP) |
Raul E Rangel | 4cfb862 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 392 | return CB_ERR_ARG; |
| 393 | |
| 394 | context = find_cbfs_preload_context(name); |
| 395 | if (!context) |
| 396 | return CB_ERR_ARG; |
| 397 | |
| 398 | err = thread_join(&context->handle); |
| 399 | if (err != CB_SUCCESS) { |
| 400 | ERROR("%s(name='%s') Preload thread failed: %u\n", __func__, name, err); |
| 401 | |
| 402 | goto out; |
| 403 | } |
| 404 | |
| 405 | if (rdev_chain_mem(rdev, context->buffer, region_device_sz(&context->rdev)) != 0) { |
| 406 | ERROR("%s(name='%s') chaining failed\n", __func__, name); |
| 407 | |
| 408 | err = CB_ERR; |
| 409 | goto out; |
| 410 | } |
| 411 | |
| 412 | err = CB_SUCCESS; |
| 413 | |
| 414 | DEBUG("%s(name='%s') preload successful\n", __func__, name); |
| 415 | |
| 416 | out: |
| 417 | free_cbfs_preload_context(context); |
| 418 | |
| 419 | return err; |
| 420 | } |
| 421 | |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 422 | static void *do_alloc(union cbfs_mdata *mdata, struct region_device *rdev, |
| 423 | cbfs_allocator_t allocator, void *arg, size_t *size_out, |
| 424 | bool skip_verification) |
| 425 | { |
| 426 | size_t size = region_device_sz(rdev); |
| 427 | void *loc = NULL; |
| 428 | |
| 429 | uint32_t compression = CBFS_COMPRESS_NONE; |
| 430 | const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(mdata, |
| 431 | CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr)); |
| 432 | if (cattr) { |
| 433 | compression = be32toh(cattr->compression); |
| 434 | size = be32toh(cattr->decompressed_size); |
| 435 | } |
| 436 | |
| 437 | if (size_out) |
| 438 | *size_out = size; |
| 439 | |
| 440 | /* allocator == NULL means do a cbfs_map() */ |
| 441 | if (allocator) { |
| 442 | loc = allocator(arg, size, mdata); |
| 443 | } else if (compression == CBFS_COMPRESS_NONE) { |
| 444 | void *mapping = rdev_mmap_full(rdev); |
| 445 | if (!mapping) |
| 446 | return NULL; |
| 447 | if (cbfs_file_hash_mismatch(mapping, size, mdata, skip_verification)) { |
| 448 | rdev_munmap(rdev, mapping); |
| 449 | return NULL; |
| 450 | } |
| 451 | return mapping; |
| 452 | } else if (!cbfs_cache.size) { |
| 453 | /* In order to use the cbfs_cache you need to add a CBFS_CACHE to your |
Jeremy Compostella | 1c3b6b3 | 2023-09-15 14:19:29 -0700 | [diff] [blame] | 454 | * memlayout. */ |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 455 | ERROR("Cannot map compressed file %s without cbfs_cache\n", mdata->h.filename); |
| 456 | return NULL; |
| 457 | } else { |
| 458 | loc = mem_pool_alloc(&cbfs_cache, size); |
| 459 | } |
| 460 | |
| 461 | if (!loc) { |
| 462 | ERROR("'%s' allocation failure\n", mdata->h.filename); |
| 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | size = cbfs_load_and_decompress(rdev, loc, size, compression, mdata, skip_verification); |
| 467 | if (!size) |
| 468 | return NULL; |
| 469 | |
| 470 | return loc; |
| 471 | } |
| 472 | |
Julius Werner | 7778cf2 | 2021-01-05 18:54:19 -0800 | [diff] [blame] | 473 | void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg, |
| 474 | size_t *size_out, bool force_ro, enum cbfs_type *type) |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 475 | { |
Julius Werner | d17ce41 | 2020-10-01 18:25:49 -0700 | [diff] [blame] | 476 | struct region_device rdev; |
Raul E Rangel | 4cfb862 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 477 | bool preload_successful = false; |
Julius Werner | d17ce41 | 2020-10-01 18:25:49 -0700 | [diff] [blame] | 478 | union cbfs_mdata mdata; |
Julius Werner | 7778cf2 | 2021-01-05 18:54:19 -0800 | [diff] [blame] | 479 | |
| 480 | DEBUG("%s(name='%s', alloc=%p(%p), force_ro=%s, type=%d)\n", __func__, name, allocator, |
| 481 | arg, force_ro ? "true" : "false", type ? *type : -1); |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 482 | |
Julius Werner | 57d4bc6 | 2021-11-15 10:13:37 -0800 | [diff] [blame] | 483 | if (_cbfs_boot_lookup(name, force_ro, &mdata, &rdev)) |
Julius Werner | 7778cf2 | 2021-01-05 18:54:19 -0800 | [diff] [blame] | 484 | return NULL; |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 485 | |
Julius Werner | 7778cf2 | 2021-01-05 18:54:19 -0800 | [diff] [blame] | 486 | if (type) { |
| 487 | const enum cbfs_type real_type = be32toh(mdata.h.type); |
| 488 | if (*type == CBFS_TYPE_QUERY) |
| 489 | *type = real_type; |
| 490 | else if (*type != real_type) { |
| 491 | ERROR("'%s' type mismatch (is %u, expected %u)\n", |
| 492 | mdata.h.filename, real_type, *type); |
| 493 | return NULL; |
| 494 | } |
Julius Werner | d17ce41 | 2020-10-01 18:25:49 -0700 | [diff] [blame] | 495 | } |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 496 | |
Raul E Rangel | 4cfb862 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 497 | /* Update the rdev with the preload content */ |
| 498 | if (!force_ro && get_preload_rdev(&rdev, name) == CB_SUCCESS) |
| 499 | preload_successful = true; |
| 500 | |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 501 | void *ret = do_alloc(&mdata, &rdev, allocator, arg, size_out, false); |
Raul E Rangel | 3f41d32 | 2021-07-23 15:23:12 -0600 | [diff] [blame] | 502 | |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 503 | /* When using cbfs_preload we need to free the preload buffer after populating the |
| 504 | * destination buffer. We know we must have a mem_rdev here, so extra mmap is fine. */ |
Raul E Rangel | 4cfb862 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 505 | if (preload_successful) |
| 506 | cbfs_unmap(rdev_mmap_full(&rdev)); |
Julius Werner | 7778cf2 | 2021-01-05 18:54:19 -0800 | [diff] [blame] | 507 | |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | void *_cbfs_unverified_area_alloc(const char *area, const char *name, |
| 512 | cbfs_allocator_t allocator, void *arg, size_t *size_out) |
| 513 | { |
| 514 | struct region_device area_rdev, file_rdev; |
| 515 | union cbfs_mdata mdata; |
| 516 | size_t data_offset; |
| 517 | |
| 518 | DEBUG("%s(area='%s', name='%s', alloc=%p(%p))\n", __func__, area, name, allocator, arg); |
| 519 | |
| 520 | if (fmap_locate_area_as_rdev(area, &area_rdev)) |
| 521 | return NULL; |
| 522 | |
| 523 | if (cbfs_lookup(&area_rdev, name, &mdata, &data_offset, NULL)) { |
| 524 | ERROR("'%s' not found in '%s'\n", name, area); |
| 525 | return NULL; |
| 526 | } |
| 527 | |
| 528 | if (rdev_chain(&file_rdev, &area_rdev, data_offset, be32toh(mdata.h.len))) |
| 529 | return NULL; |
| 530 | |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 531 | return do_alloc(&mdata, &file_rdev, allocator, arg, size_out, true); |
Julius Werner | 7778cf2 | 2021-01-05 18:54:19 -0800 | [diff] [blame] | 532 | } |
| 533 | |
Julius Werner | 4676ec5 | 2021-03-10 16:52:14 -0800 | [diff] [blame] | 534 | void *_cbfs_default_allocator(void *arg, size_t size, const union cbfs_mdata *unused) |
Julius Werner | 7778cf2 | 2021-01-05 18:54:19 -0800 | [diff] [blame] | 535 | { |
| 536 | struct _cbfs_default_allocator_arg *darg = arg; |
| 537 | if (size > darg->buf_size) |
| 538 | return NULL; |
| 539 | return darg->buf; |
| 540 | } |
| 541 | |
Julius Werner | 4676ec5 | 2021-03-10 16:52:14 -0800 | [diff] [blame] | 542 | void *_cbfs_cbmem_allocator(void *arg, size_t size, const union cbfs_mdata *unused) |
Julius Werner | 7778cf2 | 2021-01-05 18:54:19 -0800 | [diff] [blame] | 543 | { |
| 544 | return cbmem_add((uintptr_t)arg, size); |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Julius Werner | 69cc557 | 2022-03-04 17:49:56 -0800 | [diff] [blame] | 547 | enum cb_err cbfs_prog_stage_load(struct prog *pstage) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 548 | { |
Julius Werner | 1de8708 | 2020-12-23 17:38:11 -0800 | [diff] [blame] | 549 | union cbfs_mdata mdata; |
| 550 | struct region_device rdev; |
Julius Werner | 69cc557 | 2022-03-04 17:49:56 -0800 | [diff] [blame] | 551 | enum cb_err err; |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 552 | |
Julius Werner | 1de8708 | 2020-12-23 17:38:11 -0800 | [diff] [blame] | 553 | prog_locate_hook(pstage); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 554 | |
Julius Werner | 57d4bc6 | 2021-11-15 10:13:37 -0800 | [diff] [blame] | 555 | if ((err = _cbfs_boot_lookup(prog_name(pstage), false, &mdata, &rdev))) |
Julius Werner | 1de8708 | 2020-12-23 17:38:11 -0800 | [diff] [blame] | 556 | return err; |
| 557 | |
| 558 | assert(be32toh(mdata.h.type) == CBFS_TYPE_STAGE); |
| 559 | pstage->cbfs_type = CBFS_TYPE_STAGE; |
| 560 | |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 561 | enum cbfs_compression compression = CBFS_COMPRESS_NONE; |
| 562 | const struct cbfs_file_attr_compression *cattr = cbfs_find_attr(&mdata, |
| 563 | CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr)); |
| 564 | if (cattr) |
| 565 | compression = be32toh(cattr->compression); |
Julius Werner | 1de8708 | 2020-12-23 17:38:11 -0800 | [diff] [blame] | 566 | |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 567 | const struct cbfs_file_attr_stageheader *sattr = cbfs_find_attr(&mdata, |
| 568 | CBFS_FILE_ATTR_TAG_STAGEHEADER, sizeof(*sattr)); |
| 569 | if (!sattr) |
| 570 | return CB_ERR; |
| 571 | prog_set_area(pstage, (void *)(uintptr_t)be64toh(sattr->loadaddr), |
| 572 | be32toh(sattr->memlen)); |
| 573 | prog_set_entry(pstage, prog_start(pstage) + |
| 574 | be32toh(sattr->entry_offset), NULL); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 575 | |
Aaron Durbin | ed253c8 | 2015-10-07 17:22:42 -0500 | [diff] [blame] | 576 | /* Hacky way to not load programs over read only media. The stages |
| 577 | * that would hit this path initialize themselves. */ |
Julius Werner | 21a4053 | 2020-04-21 16:03:53 -0700 | [diff] [blame] | 578 | if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && |
| 579 | !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) { |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 580 | void *mapping = rdev_mmap_full(&rdev); |
Julius Werner | 1de8708 | 2020-12-23 17:38:11 -0800 | [diff] [blame] | 581 | rdev_munmap(&rdev, mapping); |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 582 | if (cbfs_file_hash_mismatch(mapping, region_device_sz(&rdev), &mdata, false)) |
Julius Werner | fccf122 | 2021-04-02 15:58:05 -0700 | [diff] [blame] | 583 | return CB_CBFS_HASH_MISMATCH; |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 584 | if (mapping == prog_start(pstage)) |
| 585 | return CB_SUCCESS; |
Aaron Durbin | ed253c8 | 2015-10-07 17:22:42 -0500 | [diff] [blame] | 586 | } |
| 587 | |
Julius Werner | eca99af | 2021-03-10 18:49:37 -0800 | [diff] [blame] | 588 | /* LZ4 stages can be decompressed in-place to save mapping scratch space. Load the |
| 589 | compressed data to the end of the buffer and point &rdev to that memory location. */ |
| 590 | if (cbfs_lz4_enabled() && compression == CBFS_COMPRESS_LZ4) { |
| 591 | size_t in_size = region_device_sz(&rdev); |
| 592 | void *compr_start = prog_start(pstage) + prog_size(pstage) - in_size; |
| 593 | if (rdev_readat(&rdev, compr_start, 0, in_size) != in_size) |
| 594 | return CB_ERR; |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 595 | rdev_chain_mem(&rdev, compr_start, in_size); |
Julius Werner | eca99af | 2021-03-10 18:49:37 -0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | size_t fsize = cbfs_load_and_decompress(&rdev, prog_start(pstage), prog_size(pstage), |
Julius Werner | 05714cc | 2021-04-15 23:25:44 -0700 | [diff] [blame] | 599 | compression, &mdata, false); |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 600 | if (!fsize) |
Julius Werner | 1de8708 | 2020-12-23 17:38:11 -0800 | [diff] [blame] | 601 | return CB_ERR; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 602 | |
| 603 | /* Clear area not covered by file. */ |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 604 | memset(prog_start(pstage) + fsize, 0, prog_size(pstage) - fsize); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 605 | |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 606 | prog_segment_loaded((uintptr_t)prog_start(pstage), prog_size(pstage), |
| 607 | SEG_FINAL); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 608 | |
Julius Werner | 1de8708 | 2020-12-23 17:38:11 -0800 | [diff] [blame] | 609 | return CB_SUCCESS; |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 610 | } |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 611 | |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 612 | 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] | 613 | { |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 614 | if (CONFIG(NO_CBFS_MCACHE) || ENV_SMM) |
| 615 | return; |
| 616 | |
Julius Werner | 34cf073 | 2020-12-08 14:21:43 -0800 | [diff] [blame] | 617 | if (cbd->mcache_size) |
| 618 | return; |
| 619 | |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 620 | const struct cbmem_entry *entry; |
Arthur Heymans | b7cbb7c | 2023-08-11 11:31:05 +0200 | [diff] [blame] | 621 | if (ENV_HAS_CBMEM && |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 622 | (entry = cbmem_entry_find(id))) { |
| 623 | cbd->mcache = cbmem_entry_start(entry); |
| 624 | cbd->mcache_size = cbmem_entry_size(entry); |
| 625 | } else if (ENV_ROMSTAGE_OR_BEFORE) { |
| 626 | u8 *boundary = _ecbfs_mcache - REGION_SIZE(cbfs_mcache) * |
| 627 | CONFIG_CBFS_MCACHE_RW_PERCENTAGE / 100; |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 628 | boundary = (u8 *)ALIGN_DOWN((uintptr_t)boundary, CBFS_MCACHE_ALIGNMENT); |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 629 | if (id == CBMEM_ID_CBFS_RO_MCACHE) { |
| 630 | cbd->mcache = _cbfs_mcache; |
| 631 | cbd->mcache_size = boundary - _cbfs_mcache; |
| 632 | } else if (id == CBMEM_ID_CBFS_RW_MCACHE) { |
| 633 | cbd->mcache = boundary; |
| 634 | cbd->mcache_size = _ecbfs_mcache - boundary; |
| 635 | } |
| 636 | } |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 637 | } |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 638 | |
Julius Werner | 69cc557 | 2022-03-04 17:49:56 -0800 | [diff] [blame] | 639 | enum cb_err cbfs_init_boot_device(const struct cbfs_boot_device *cbd, |
| 640 | struct vb2_hash *mdata_hash) |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 641 | { |
| 642 | /* If we have an mcache, mcache_build() will also check mdata hash. */ |
| 643 | if (!CONFIG(NO_CBFS_MCACHE) && !ENV_SMM && cbd->mcache_size > 0) |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 644 | return cbfs_mcache_build(&cbd->rdev, cbd->mcache, cbd->mcache_size, mdata_hash); |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 645 | |
| 646 | /* No mcache and no verification means we have nothing special to do. */ |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 647 | if (!CONFIG(CBFS_VERIFICATION) || !mdata_hash) |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 648 | return CB_SUCCESS; |
| 649 | |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 650 | /* Verification only: use cbfs_walk() without a walker() function to just run through |
| 651 | the CBFS once, will return NOT_FOUND by default. */ |
Julius Werner | 69cc557 | 2022-03-04 17:49:56 -0800 | [diff] [blame] | 652 | enum cb_err err = cbfs_walk(&cbd->rdev, NULL, NULL, mdata_hash, 0); |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 653 | if (err == CB_CBFS_NOT_FOUND) |
| 654 | err = CB_SUCCESS; |
| 655 | return err; |
| 656 | } |
| 657 | |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 658 | const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro) |
| 659 | { |
| 660 | static struct cbfs_boot_device ro; |
| 661 | |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 662 | /* Ensure we always init RO mcache, even if the first file is from the RW CBFS. |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 663 | Otherwise it may not be available when needed in later stages. */ |
| 664 | if (ENV_INITIAL_STAGE && !force_ro && !region_device_sz(&ro.rdev)) |
| 665 | cbfs_get_boot_device(true); |
| 666 | |
| 667 | if (!force_ro) { |
| 668 | const struct cbfs_boot_device *rw = vboot_get_cbfs_boot_device(); |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 669 | /* This will return NULL if vboot isn't enabled, didn't run yet or decided to |
| 670 | boot into recovery mode. */ |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 671 | if (rw) |
| 672 | return rw; |
| 673 | } |
| 674 | |
Julius Werner | 723e3b1 | 2020-12-29 17:33:30 -0800 | [diff] [blame] | 675 | /* In rare cases post-RAM stages may run this before cbmem_initialize(), so we can't |
| 676 | lock in the result of find_mcache() on the first try and should keep trying every |
| 677 | time until an mcache is found. */ |
Julius Werner | 34cf073 | 2020-12-08 14:21:43 -0800 | [diff] [blame] | 678 | cbfs_boot_device_find_mcache(&ro, CBMEM_ID_CBFS_RO_MCACHE); |
| 679 | |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 680 | if (region_device_sz(&ro.rdev)) |
| 681 | return &ro; |
| 682 | |
| 683 | if (fmap_locate_area_as_rdev("COREBOOT", &ro.rdev)) |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 684 | die("Cannot locate primary CBFS"); |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 685 | |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 686 | if (ENV_INITIAL_STAGE) { |
Julius Werner | 69cc557 | 2022-03-04 17:49:56 -0800 | [diff] [blame] | 687 | enum cb_err err = cbfs_init_boot_device(&ro, metadata_hash_get()); |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 688 | if (err == CB_CBFS_HASH_MISMATCH) |
| 689 | die("RO CBFS metadata hash verification failure"); |
| 690 | else if (CONFIG(TOCTOU_SAFETY) && err == CB_CBFS_CACHE_FULL) |
| 691 | die("RO mcache overflow breaks TOCTOU safety!\n"); |
| 692 | else if (err && err != CB_CBFS_CACHE_FULL) |
| 693 | die("RO CBFS initialization error: %d", err); |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | return &ro; |
| 697 | } |
| 698 | |
| 699 | #if !CONFIG(NO_CBFS_MCACHE) |
| 700 | static void mcache_to_cbmem(const struct cbfs_boot_device *cbd, u32 cbmem_id) |
| 701 | { |
| 702 | if (!cbd) |
| 703 | return; |
| 704 | |
| 705 | size_t real_size = cbfs_mcache_real_size(cbd->mcache, cbd->mcache_size); |
| 706 | void *cbmem_mcache = cbmem_add(cbmem_id, real_size); |
| 707 | if (!cbmem_mcache) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 708 | printk(BIOS_ERR, "Cannot allocate CBMEM mcache %#x (%#zx bytes)!\n", |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 709 | cbmem_id, real_size); |
| 710 | return; |
| 711 | } |
| 712 | memcpy(cbmem_mcache, cbd->mcache, real_size); |
| 713 | } |
| 714 | |
| 715 | static void cbfs_mcache_migrate(int unused) |
| 716 | { |
| 717 | mcache_to_cbmem(vboot_get_cbfs_boot_device(), CBMEM_ID_CBFS_RW_MCACHE); |
| 718 | mcache_to_cbmem(cbfs_get_boot_device(true), CBMEM_ID_CBFS_RO_MCACHE); |
| 719 | } |
Kyösti Mälkki | fa3bc04 | 2022-03-31 07:40:10 +0300 | [diff] [blame] | 720 | CBMEM_CREATION_HOOK(cbfs_mcache_migrate); |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 721 | #endif |