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> |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 13 | |
| 14 | static bool looks_like_fsp_header(const uint8_t *raw_hdr) |
| 15 | { |
| 16 | if (memcmp(raw_hdr, FSP_HDR_SIGNATURE, 4)) { |
| 17 | printk(BIOS_ALERT, "Did not find a valid FSP signature\n"); |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | if (read32(raw_hdr + 4) != FSP_HDR_LEN) { |
| 22 | printk(BIOS_ALERT, "FSP header has invalid length\n"); |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | enum cb_err fsp_identify(struct fsp_header *hdr, const void *fsp_blob) |
| 30 | { |
| 31 | const uint8_t *raw_hdr = fsp_blob; |
| 32 | |
| 33 | if (!looks_like_fsp_header(raw_hdr)) |
| 34 | return CB_ERR; |
| 35 | |
Andrey Petrov | d5a6eb4 | 2016-05-04 17:30:16 -0700 | [diff] [blame] | 36 | hdr->spec_version = read8(raw_hdr + 10); |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 37 | hdr->revision = read8(raw_hdr + 11); |
| 38 | hdr->fsp_revision = read32(raw_hdr + 12); |
| 39 | memcpy(hdr->image_id, raw_hdr + 16, ARRAY_SIZE(hdr->image_id)); |
| 40 | hdr->image_id[ARRAY_SIZE(hdr->image_id) - 1] = '\0'; |
| 41 | hdr->image_size = read32(raw_hdr + 24); |
| 42 | hdr->image_base = read32(raw_hdr + 28); |
Andrey Petrov | d5a6eb4 | 2016-05-04 17:30:16 -0700 | [diff] [blame] | 43 | hdr->image_attribute = read16(raw_hdr + 32); |
| 44 | hdr->component_attribute = read16(raw_hdr + 34); |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 45 | hdr->cfg_region_offset = read32(raw_hdr + 36); |
| 46 | hdr->cfg_region_size = read32(raw_hdr + 40); |
Brenton Dong | 0a5971c | 2016-10-18 11:35:15 -0700 | [diff] [blame] | 47 | hdr->temp_ram_init_entry = read32(raw_hdr + 48); |
| 48 | hdr->temp_ram_exit_entry = read32(raw_hdr + 64); |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 49 | hdr->notify_phase_entry_offset = read32(raw_hdr + 56); |
| 50 | hdr->memory_init_entry_offset = read32(raw_hdr + 60); |
| 51 | hdr->silicon_init_entry_offset = read32(raw_hdr + 68); |
Subrata Banik | 33d9c4a | 2020-05-26 18:26:54 +0530 | [diff] [blame] | 52 | if (CONFIG(PLATFORM_USES_FSP2_2)) |
| 53 | hdr->multi_phase_si_init_entry_offset = read32(raw_hdr + 72); |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 54 | |
| 55 | return CB_SUCCESS; |
| 56 | } |
| 57 | |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 58 | enum cb_err fsp_validate_component(struct fsp_header *hdr, |
| 59 | const struct region_device *rdev) |
| 60 | { |
| 61 | void *membase; |
| 62 | |
| 63 | /* Map just enough of the file to be able to parse the header. */ |
| 64 | membase = rdev_mmap(rdev, FSP_HDR_OFFSET, FSP_HDR_LEN); |
| 65 | |
| 66 | if (membase == NULL) { |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 67 | printk(BIOS_CRIT, "Could not mmap() FSP header.\n"); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 68 | return CB_ERR; |
| 69 | } |
| 70 | |
| 71 | if (fsp_identify(hdr, membase) != CB_SUCCESS) { |
| 72 | rdev_munmap(rdev, membase); |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 73 | printk(BIOS_CRIT, "No valid FSP header\n"); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 74 | return CB_ERR; |
| 75 | } |
| 76 | |
| 77 | rdev_munmap(rdev, membase); |
| 78 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 79 | if (CONFIG(DISPLAY_FSP_HEADER)) |
Lee Leahy | 37b5ef2 | 2016-07-31 14:15:49 -0700 | [diff] [blame] | 80 | fsp_print_header_info(hdr); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 81 | |
| 82 | /* Check if size specified in the header matches the cbfs file size */ |
| 83 | if (region_device_sz(rdev) < hdr->image_size) { |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 84 | printk(BIOS_CRIT, "Component size bigger than cbfs file.\n"); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 85 | return CB_ERR; |
| 86 | } |
| 87 | |
| 88 | return CB_SUCCESS; |
| 89 | } |
| 90 | |
Brandon Breitenstein | c31ba0e | 2016-07-27 17:34:45 -0700 | [diff] [blame] | 91 | static bool fsp_reset_requested(uint32_t status) |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 92 | { |
| 93 | return (status >= FSP_STATUS_RESET_REQUIRED_COLD && |
| 94 | status <= FSP_STATUS_RESET_REQUIRED_8); |
| 95 | } |
| 96 | |
Brandon Breitenstein | c31ba0e | 2016-07-27 17:34:45 -0700 | [diff] [blame] | 97 | void fsp_handle_reset(uint32_t status) |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 98 | { |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 99 | if (!fsp_reset_requested(status)) |
| 100 | return; |
| 101 | |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 102 | printk(BIOS_SPEW, "FSP: handling reset type %x\n", status); |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 103 | |
Lee Leahy | b2b97a5 | 2017-03-10 08:40:18 -0800 | [diff] [blame] | 104 | switch (status) { |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 105 | case FSP_STATUS_RESET_REQUIRED_COLD: |
Patrick Rudolph | f677d17 | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 106 | full_reset(); |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 107 | break; |
| 108 | case FSP_STATUS_RESET_REQUIRED_WARM: |
Patrick Rudolph | f677d17 | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 109 | system_reset(); |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 110 | break; |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 111 | case FSP_STATUS_RESET_REQUIRED_3: |
| 112 | case FSP_STATUS_RESET_REQUIRED_4: |
| 113 | case FSP_STATUS_RESET_REQUIRED_5: |
| 114 | case FSP_STATUS_RESET_REQUIRED_6: |
| 115 | case FSP_STATUS_RESET_REQUIRED_7: |
| 116 | case FSP_STATUS_RESET_REQUIRED_8: |
| 117 | chipset_handle_reset(status); |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 118 | break; |
| 119 | default: |
| 120 | break; |
| 121 | } |
| 122 | } |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 123 | |
Aaron Durbin | ecbfa99 | 2020-05-15 17:01:58 -0600 | [diff] [blame] | 124 | static inline bool fspm_env(void) |
| 125 | { |
| 126 | if (ENV_ROMSTAGE) |
| 127 | return true; |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | static inline bool fspm_xip(void) |
| 132 | { |
| 133 | /* FSP-M is assumed to be loaded in romstage. */ |
| 134 | if (fspm_env() && CONFIG(FSP_M_XIP)) |
| 135 | return true; |
| 136 | return false; |
| 137 | } |
| 138 | |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 139 | static void *fsp_get_dest_and_load(struct fsp_load_descriptor *fspld, size_t size, |
| 140 | const struct region_device *source_rdev, |
| 141 | uint32_t compression_algo) |
| 142 | { |
| 143 | void *dest; |
| 144 | |
| 145 | if (fspld->get_destination(fspld, &dest, size, source_rdev) < 0) { |
| 146 | printk(BIOS_ERR, "FSP Destination not obtained.\n"); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
Aaron Durbin | ecbfa99 | 2020-05-15 17:01:58 -0600 | [diff] [blame] | 150 | /* Don't load when executing in place. */ |
| 151 | if (fspm_xip()) |
| 152 | return dest; |
| 153 | |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 154 | if (cbfs_load_and_decompress(source_rdev, 0, region_device_sz(source_rdev), |
| 155 | dest, size, compression_algo) != size) { |
| 156 | printk(BIOS_ERR, "Failed to load FSP component.\n"); |
| 157 | return NULL; |
| 158 | } |
| 159 | |
Aaron Durbin | ecbfa99 | 2020-05-15 17:01:58 -0600 | [diff] [blame] | 160 | /* Don't allow FSP-M relocation. */ |
| 161 | if (fspm_env()) |
| 162 | return dest; |
| 163 | |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 164 | if (fsp_component_relocate((uintptr_t)dest, dest, size) < 0) { |
| 165 | printk(BIOS_ERR, "Unable to relocate FSP component!\n"); |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | return dest; |
| 170 | } |
| 171 | |
| 172 | /* Load the FSP component described by fsp_load_descriptor from cbfs. The FSP |
| 173 | * header object will be validated and filled in on successful load. */ |
| 174 | enum cb_err fsp_load_component(struct fsp_load_descriptor *fspld, struct fsp_header *hdr) |
| 175 | { |
| 176 | struct cbfsf file_desc; |
| 177 | uint32_t compression_algo; |
| 178 | size_t output_size; |
| 179 | void *dest; |
| 180 | struct region_device source_rdev; |
| 181 | struct prog *fsp_prog = &fspld->fsp_prog; |
| 182 | |
| 183 | if (fspld->get_destination == NULL) |
| 184 | return CB_ERR; |
| 185 | |
| 186 | if (cbfs_boot_locate(&file_desc, prog_name(fsp_prog), &fsp_prog->cbfs_type) < 0) |
| 187 | return CB_ERR; |
| 188 | |
| 189 | if (cbfsf_decompression_info(&file_desc, &compression_algo, &output_size) < 0) |
| 190 | return CB_ERR; |
| 191 | |
| 192 | cbfs_file_data(&source_rdev, &file_desc); |
| 193 | |
| 194 | dest = fsp_get_dest_and_load(fspld, output_size, &source_rdev, compression_algo); |
| 195 | |
| 196 | if (dest == NULL) |
| 197 | return CB_ERR; |
| 198 | |
| 199 | prog_set_area(fsp_prog, dest, output_size); |
| 200 | |
| 201 | if (fsp_validate_component(hdr, prog_rdev(fsp_prog)) != CB_SUCCESS) { |
| 202 | printk(BIOS_ERR, "Invalid FSP header after load!\n"); |
| 203 | return CB_ERR; |
| 204 | } |
| 205 | |
| 206 | /* Signal that FSP component has been loaded. */ |
| 207 | prog_segment_loaded(hdr->image_base, hdr->image_size, SEG_FINAL); |
| 208 | |
| 209 | return CB_SUCCESS; |
| 210 | } |
Johnny Lin | b8899ef | 2020-05-28 14:04:58 +0800 | [diff] [blame] | 211 | |
| 212 | /* Only call this function when FSP header has been read and validated */ |
| 213 | void fsp_get_version(char *buf) |
| 214 | { |
| 215 | struct fsp_header *hdr = &fsps_hdr; |
| 216 | union { |
| 217 | uint32_t val; |
| 218 | struct { |
| 219 | uint8_t bld_num; |
| 220 | uint8_t revision; |
| 221 | uint8_t minor; |
| 222 | uint8_t major; |
| 223 | } rev; |
| 224 | } revision; |
| 225 | |
| 226 | revision.val = hdr->fsp_revision; |
| 227 | snprintf(buf, FSP_VER_LEN, "%u.%u-%u.%u.%u.%u", (hdr->spec_version >> 4), |
| 228 | hdr->spec_version & 0xf, revision.rev.major, |
| 229 | revision.rev.minor, revision.rev.revision, revision.rev.bld_num); |
| 230 | } |
| 231 | |
| 232 | /* Add FSP version to coreboot table LB_TAG_PLATFORM_BLOB_VERSION */ |
| 233 | void lb_string_platform_blob_version(struct lb_header *header) |
| 234 | { |
| 235 | struct lb_string *rec; |
| 236 | size_t len; |
| 237 | char fsp_version[FSP_VER_LEN] = {0}; |
| 238 | |
| 239 | fsp_get_version(fsp_version); |
| 240 | rec = (struct lb_string *)lb_new_record(header); |
| 241 | rec->tag = LB_TAG_PLATFORM_BLOB_VERSION; |
| 242 | len = strlen(fsp_version); |
| 243 | rec->size = ALIGN_UP(sizeof(*rec) + len + 1, 8); |
| 244 | memcpy(rec->string, fsp_version, len+1); |
| 245 | } |