Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 2 | |
Johnny Lin | b8899ef | 2020-05-28 14:04:58 +0800 | [diff] [blame] | 3 | #include <boot/coreboot_tables.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 4 | #include <device/mmio.h> |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 5 | #include <cbfs.h> |
Patrick Rudolph | f677d17 | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 6 | #include <cf9_reset.h> |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 7 | #include <commonlib/bsd/compression.h> |
| 8 | #include <commonlib/fsp.h> |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 9 | #include <console/console.h> |
| 10 | #include <fsp/util.h> |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 11 | #include <string.h> |
Elyes HAOUAS | bd1683d | 2019-05-15 21:05:37 +0200 | [diff] [blame] | 12 | #include <types.h> |
Nikolai Vyssotski | 3c3d2cf | 2021-07-09 18:36:27 -0500 | [diff] [blame] | 13 | #include <assert.h> |
| 14 | |
| 15 | static uint32_t fsp_hdr_get_expected_min_length(void) |
| 16 | { |
Anil Kumar | 57309d3 | 2021-11-11 18:56:21 -0800 | [diff] [blame] | 17 | if (CONFIG(PLATFORM_USES_FSP2_3)) |
| 18 | return 80; |
| 19 | else if (CONFIG(PLATFORM_USES_FSP2_2)) |
Nikolai Vyssotski | 3c3d2cf | 2021-07-09 18:36:27 -0500 | [diff] [blame] | 20 | return 76; |
| 21 | else if (CONFIG(PLATFORM_USES_FSP2_1)) |
| 22 | return 72; |
| 23 | else if (CONFIG(PLATFORM_USES_FSP2_0)) |
| 24 | return 72; |
| 25 | else |
| 26 | return dead_code_t(uint32_t); |
| 27 | } |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 28 | |
| 29 | static bool looks_like_fsp_header(const uint8_t *raw_hdr) |
| 30 | { |
Nikolai Vyssotski | 3c3d2cf | 2021-07-09 18:36:27 -0500 | [diff] [blame] | 31 | uint32_t fsp_header_length = read32(raw_hdr + 4); |
| 32 | |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 33 | if (memcmp(raw_hdr, FSP_HDR_SIGNATURE, 4)) { |
| 34 | printk(BIOS_ALERT, "Did not find a valid FSP signature\n"); |
| 35 | return false; |
| 36 | } |
| 37 | |
Nikolai Vyssotski | 3c3d2cf | 2021-07-09 18:36:27 -0500 | [diff] [blame] | 38 | /* It is possible to build FSP with any version of EDK2 which could have introduced new |
| 39 | fields in FSP_INFO_HEADER. The new fields will be ignored based on the reported FSP |
| 40 | version. This check ensures that the reported header length is at least what the |
| 41 | reported FSP version requires so that we do not access any out-of-bound bytes. */ |
| 42 | if (fsp_header_length < fsp_hdr_get_expected_min_length()) { |
| 43 | printk(BIOS_ALERT, "FSP header has invalid length: %d\n", fsp_header_length); |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | enum cb_err fsp_identify(struct fsp_header *hdr, const void *fsp_blob) |
| 51 | { |
| 52 | const uint8_t *raw_hdr = fsp_blob; |
| 53 | |
| 54 | if (!looks_like_fsp_header(raw_hdr)) |
| 55 | return CB_ERR; |
| 56 | |
Andrey Petrov | d5a6eb4 | 2016-05-04 17:30:16 -0700 | [diff] [blame] | 57 | hdr->spec_version = read8(raw_hdr + 10); |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 58 | hdr->revision = read8(raw_hdr + 11); |
| 59 | hdr->fsp_revision = read32(raw_hdr + 12); |
| 60 | memcpy(hdr->image_id, raw_hdr + 16, ARRAY_SIZE(hdr->image_id)); |
| 61 | hdr->image_id[ARRAY_SIZE(hdr->image_id) - 1] = '\0'; |
| 62 | hdr->image_size = read32(raw_hdr + 24); |
| 63 | hdr->image_base = read32(raw_hdr + 28); |
Andrey Petrov | d5a6eb4 | 2016-05-04 17:30:16 -0700 | [diff] [blame] | 64 | hdr->image_attribute = read16(raw_hdr + 32); |
| 65 | hdr->component_attribute = read16(raw_hdr + 34); |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 66 | hdr->cfg_region_offset = read32(raw_hdr + 36); |
| 67 | hdr->cfg_region_size = read32(raw_hdr + 40); |
Brenton Dong | 0a5971c | 2016-10-18 11:35:15 -0700 | [diff] [blame] | 68 | hdr->temp_ram_init_entry = read32(raw_hdr + 48); |
| 69 | hdr->temp_ram_exit_entry = read32(raw_hdr + 64); |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 70 | hdr->notify_phase_entry_offset = read32(raw_hdr + 56); |
| 71 | hdr->memory_init_entry_offset = read32(raw_hdr + 60); |
| 72 | hdr->silicon_init_entry_offset = read32(raw_hdr + 68); |
Subrata Banik | 33d9c4a | 2020-05-26 18:26:54 +0530 | [diff] [blame] | 73 | if (CONFIG(PLATFORM_USES_FSP2_2)) |
| 74 | hdr->multi_phase_si_init_entry_offset = read32(raw_hdr + 72); |
Anil Kumar | 57309d3 | 2021-11-11 18:56:21 -0800 | [diff] [blame] | 75 | if (CONFIG(PLATFORM_USES_FSP2_3)) |
| 76 | hdr->extended_fsp_revision = read16(raw_hdr + 76); |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 77 | |
| 78 | return CB_SUCCESS; |
| 79 | } |
| 80 | |
Julius Werner | 43c9d70 | 2021-04-12 17:00:16 -0700 | [diff] [blame] | 81 | enum cb_err fsp_validate_component(struct fsp_header *hdr, void *fsp_file, size_t file_size) |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 82 | { |
Julius Werner | 43c9d70 | 2021-04-12 17:00:16 -0700 | [diff] [blame] | 83 | void *raw_hdr = fsp_file + FSP_HDR_OFFSET; |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 84 | |
Nikolai Vyssotski | 3c3d2cf | 2021-07-09 18:36:27 -0500 | [diff] [blame] | 85 | if (file_size < FSP_HDR_OFFSET + fsp_hdr_get_expected_min_length()) { |
Julius Werner | 43c9d70 | 2021-04-12 17:00:16 -0700 | [diff] [blame] | 86 | printk(BIOS_CRIT, "FSP blob too small.\n"); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 87 | return CB_ERR; |
| 88 | } |
| 89 | |
Julius Werner | 43c9d70 | 2021-04-12 17:00:16 -0700 | [diff] [blame] | 90 | if (fsp_identify(hdr, raw_hdr) != CB_SUCCESS) { |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 91 | printk(BIOS_CRIT, "No valid FSP header\n"); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 92 | return CB_ERR; |
| 93 | } |
| 94 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 95 | if (CONFIG(DISPLAY_FSP_HEADER)) |
Lee Leahy | 37b5ef2 | 2016-07-31 14:15:49 -0700 | [diff] [blame] | 96 | fsp_print_header_info(hdr); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 97 | |
| 98 | /* Check if size specified in the header matches the cbfs file size */ |
Julius Werner | 43c9d70 | 2021-04-12 17:00:16 -0700 | [diff] [blame] | 99 | if (file_size < hdr->image_size) { |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 100 | printk(BIOS_CRIT, "Component size bigger than cbfs file.\n"); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 101 | return CB_ERR; |
| 102 | } |
| 103 | |
Johnny Lin | 5b47d77 | 2020-11-13 17:21:25 +0800 | [diff] [blame] | 104 | if (ENV_ROMSTAGE) |
Felix Held | 42df9af | 2021-08-24 19:26:56 +0200 | [diff] [blame] | 105 | soc_validate_fspm_header(hdr); |
Johnny Lin | 5b47d77 | 2020-11-13 17:21:25 +0800 | [diff] [blame] | 106 | |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 107 | return CB_SUCCESS; |
| 108 | } |
| 109 | |
Brandon Breitenstein | c31ba0e | 2016-07-27 17:34:45 -0700 | [diff] [blame] | 110 | static bool fsp_reset_requested(uint32_t status) |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 111 | { |
| 112 | return (status >= FSP_STATUS_RESET_REQUIRED_COLD && |
| 113 | status <= FSP_STATUS_RESET_REQUIRED_8); |
| 114 | } |
| 115 | |
Brandon Breitenstein | c31ba0e | 2016-07-27 17:34:45 -0700 | [diff] [blame] | 116 | void fsp_handle_reset(uint32_t status) |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 117 | { |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 118 | if (!fsp_reset_requested(status)) |
| 119 | return; |
| 120 | |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 121 | printk(BIOS_SPEW, "FSP: handling reset type %x\n", status); |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 122 | |
Lee Leahy | b2b97a5 | 2017-03-10 08:40:18 -0800 | [diff] [blame] | 123 | switch (status) { |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 124 | case FSP_STATUS_RESET_REQUIRED_COLD: |
Patrick Rudolph | f677d17 | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 125 | full_reset(); |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 126 | break; |
| 127 | case FSP_STATUS_RESET_REQUIRED_WARM: |
Patrick Rudolph | f677d17 | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 128 | system_reset(); |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 129 | break; |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 130 | case FSP_STATUS_RESET_REQUIRED_3: |
| 131 | case FSP_STATUS_RESET_REQUIRED_4: |
| 132 | case FSP_STATUS_RESET_REQUIRED_5: |
| 133 | case FSP_STATUS_RESET_REQUIRED_6: |
| 134 | case FSP_STATUS_RESET_REQUIRED_7: |
| 135 | case FSP_STATUS_RESET_REQUIRED_8: |
| 136 | chipset_handle_reset(status); |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 137 | break; |
| 138 | default: |
| 139 | break; |
| 140 | } |
| 141 | } |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 142 | |
Aaron Durbin | ecbfa99 | 2020-05-15 17:01:58 -0600 | [diff] [blame] | 143 | static inline bool fspm_env(void) |
| 144 | { |
| 145 | if (ENV_ROMSTAGE) |
| 146 | return true; |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | static inline bool fspm_xip(void) |
| 151 | { |
| 152 | /* FSP-M is assumed to be loaded in romstage. */ |
| 153 | if (fspm_env() && CONFIG(FSP_M_XIP)) |
| 154 | return true; |
| 155 | return false; |
| 156 | } |
| 157 | |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 158 | /* Load the FSP component described by fsp_load_descriptor from cbfs. The FSP |
| 159 | * header object will be validated and filled in on successful load. */ |
| 160 | enum cb_err fsp_load_component(struct fsp_load_descriptor *fspld, struct fsp_header *hdr) |
| 161 | { |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 162 | size_t output_size; |
| 163 | void *dest; |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 164 | struct prog *fsp_prog = &fspld->fsp_prog; |
| 165 | |
Julius Werner | 8205ce6 | 2021-03-10 17:25:01 -0800 | [diff] [blame] | 166 | dest = cbfs_alloc(prog_name(fsp_prog), fspld->alloc, fspld, &output_size); |
| 167 | if (!dest) |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 168 | return CB_ERR; |
| 169 | |
Raul E Rangel | 4911dc7 | 2021-11-05 10:29:24 -0600 | [diff] [blame] | 170 | /* Don't allow FSP-M relocation when XIP. */ |
| 171 | if (!fspm_xip() && fsp_component_relocate((uintptr_t)dest, dest, output_size) < 0) { |
Julius Werner | 8205ce6 | 2021-03-10 17:25:01 -0800 | [diff] [blame] | 172 | printk(BIOS_ERR, "Unable to relocate FSP component!\n"); |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 173 | return CB_ERR; |
Julius Werner | 8205ce6 | 2021-03-10 17:25:01 -0800 | [diff] [blame] | 174 | } |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 175 | |
| 176 | prog_set_area(fsp_prog, dest, output_size); |
| 177 | |
Julius Werner | 43c9d70 | 2021-04-12 17:00:16 -0700 | [diff] [blame] | 178 | if (fsp_validate_component(hdr, dest, output_size) != CB_SUCCESS) { |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 179 | printk(BIOS_ERR, "Invalid FSP header after load!\n"); |
| 180 | return CB_ERR; |
| 181 | } |
| 182 | |
| 183 | /* Signal that FSP component has been loaded. */ |
| 184 | prog_segment_loaded(hdr->image_base, hdr->image_size, SEG_FINAL); |
| 185 | |
| 186 | return CB_SUCCESS; |
| 187 | } |
Johnny Lin | b8899ef | 2020-05-28 14:04:58 +0800 | [diff] [blame] | 188 | |
| 189 | /* Only call this function when FSP header has been read and validated */ |
| 190 | void fsp_get_version(char *buf) |
| 191 | { |
| 192 | struct fsp_header *hdr = &fsps_hdr; |
Johnny Lin | 5b47d77 | 2020-11-13 17:21:25 +0800 | [diff] [blame] | 193 | union fsp_revision revision; |
Johnny Lin | b8899ef | 2020-05-28 14:04:58 +0800 | [diff] [blame] | 194 | |
| 195 | revision.val = hdr->fsp_revision; |
| 196 | snprintf(buf, FSP_VER_LEN, "%u.%u-%u.%u.%u.%u", (hdr->spec_version >> 4), |
| 197 | hdr->spec_version & 0xf, revision.rev.major, |
| 198 | revision.rev.minor, revision.rev.revision, revision.rev.bld_num); |
| 199 | } |
| 200 | |
Felix Held | 8899598 | 2021-01-28 22:43:52 +0100 | [diff] [blame] | 201 | /* Check if the signature in the UPD header matches the expected one. If it doesn't match, the |
| 202 | FSP binaries in CBFS are for a different platform than the platform code trying to use it |
| 203 | in which case the function calls die(). */ |
| 204 | void fsp_verify_upd_header_signature(uint64_t upd_signature, uint64_t expected_signature) |
| 205 | { |
| 206 | if (upd_signature != expected_signature) { |
| 207 | /* The UPD signatures are non-zero-terminated ASCII stored as a little endian |
| 208 | uint64_t, so this needs some casts. */ |
| 209 | die_with_post_code(POST_INVALID_VENDOR_BINARY, |
| 210 | "Invalid UPD signature! FSP provided \"%8s\", expected was \"%8s\".\n", |
| 211 | (char *)&upd_signature, |
| 212 | (char *)&expected_signature); |
| 213 | } |
| 214 | } |
| 215 | |
Johnny Lin | b8899ef | 2020-05-28 14:04:58 +0800 | [diff] [blame] | 216 | /* Add FSP version to coreboot table LB_TAG_PLATFORM_BLOB_VERSION */ |
| 217 | void lb_string_platform_blob_version(struct lb_header *header) |
| 218 | { |
| 219 | struct lb_string *rec; |
| 220 | size_t len; |
| 221 | char fsp_version[FSP_VER_LEN] = {0}; |
| 222 | |
| 223 | fsp_get_version(fsp_version); |
| 224 | rec = (struct lb_string *)lb_new_record(header); |
| 225 | rec->tag = LB_TAG_PLATFORM_BLOB_VERSION; |
| 226 | len = strlen(fsp_version); |
| 227 | rec->size = ALIGN_UP(sizeof(*rec) + len + 1, 8); |
| 228 | memcpy(rec->string, fsp_version, len+1); |
| 229 | } |
Johnny Lin | 5b47d77 | 2020-11-13 17:21:25 +0800 | [diff] [blame] | 230 | |
Felix Held | 42df9af | 2021-08-24 19:26:56 +0200 | [diff] [blame] | 231 | __weak void soc_validate_fspm_header(const struct fsp_header *hdr) |
Johnny Lin | 5b47d77 | 2020-11-13 17:21:25 +0800 | [diff] [blame] | 232 | { |
Johnny Lin | 5b47d77 | 2020-11-13 17:21:25 +0800 | [diff] [blame] | 233 | } |