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 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 3 | #include <device/mmio.h> |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 4 | #include <cbfs.h> |
Patrick Rudolph | f677d17 | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 5 | #include <cf9_reset.h> |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 6 | #include <commonlib/bsd/compression.h> |
| 7 | #include <commonlib/fsp.h> |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 8 | #include <console/console.h> |
| 9 | #include <fsp/util.h> |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 10 | #include <lib.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); |
| 52 | |
| 53 | return CB_SUCCESS; |
| 54 | } |
| 55 | |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 56 | enum cb_err fsp_validate_component(struct fsp_header *hdr, |
| 57 | const struct region_device *rdev) |
| 58 | { |
| 59 | void *membase; |
| 60 | |
| 61 | /* Map just enough of the file to be able to parse the header. */ |
| 62 | membase = rdev_mmap(rdev, FSP_HDR_OFFSET, FSP_HDR_LEN); |
| 63 | |
| 64 | if (membase == NULL) { |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 65 | printk(BIOS_CRIT, "Could not mmap() FSP header.\n"); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 66 | return CB_ERR; |
| 67 | } |
| 68 | |
| 69 | if (fsp_identify(hdr, membase) != CB_SUCCESS) { |
| 70 | rdev_munmap(rdev, membase); |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 71 | printk(BIOS_CRIT, "No valid FSP header\n"); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 72 | return CB_ERR; |
| 73 | } |
| 74 | |
| 75 | rdev_munmap(rdev, membase); |
| 76 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 77 | if (CONFIG(DISPLAY_FSP_HEADER)) |
Lee Leahy | 37b5ef2 | 2016-07-31 14:15:49 -0700 | [diff] [blame] | 78 | fsp_print_header_info(hdr); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 79 | |
| 80 | /* Check if size specified in the header matches the cbfs file size */ |
| 81 | if (region_device_sz(rdev) < hdr->image_size) { |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 82 | printk(BIOS_CRIT, "Component size bigger than cbfs file.\n"); |
Aaron Durbin | a413e5e | 2016-07-17 23:06:03 -0500 | [diff] [blame] | 83 | return CB_ERR; |
| 84 | } |
| 85 | |
| 86 | return CB_SUCCESS; |
| 87 | } |
| 88 | |
Brandon Breitenstein | c31ba0e | 2016-07-27 17:34:45 -0700 | [diff] [blame] | 89 | static bool fsp_reset_requested(uint32_t status) |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 90 | { |
| 91 | return (status >= FSP_STATUS_RESET_REQUIRED_COLD && |
| 92 | status <= FSP_STATUS_RESET_REQUIRED_8); |
| 93 | } |
| 94 | |
Brandon Breitenstein | c31ba0e | 2016-07-27 17:34:45 -0700 | [diff] [blame] | 95 | void fsp_handle_reset(uint32_t status) |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 96 | { |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 97 | if (!fsp_reset_requested(status)) |
| 98 | return; |
| 99 | |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 100 | printk(BIOS_SPEW, "FSP: handling reset type %x\n", status); |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 101 | |
Lee Leahy | b2b97a5 | 2017-03-10 08:40:18 -0800 | [diff] [blame] | 102 | switch (status) { |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 103 | case FSP_STATUS_RESET_REQUIRED_COLD: |
Patrick Rudolph | f677d17 | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 104 | full_reset(); |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 105 | break; |
| 106 | case FSP_STATUS_RESET_REQUIRED_WARM: |
Patrick Rudolph | f677d17 | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 107 | system_reset(); |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 108 | break; |
Andrey Petrov | 3a94a3b | 2016-07-18 00:15:41 -0700 | [diff] [blame] | 109 | case FSP_STATUS_RESET_REQUIRED_3: |
| 110 | case FSP_STATUS_RESET_REQUIRED_4: |
| 111 | case FSP_STATUS_RESET_REQUIRED_5: |
| 112 | case FSP_STATUS_RESET_REQUIRED_6: |
| 113 | case FSP_STATUS_RESET_REQUIRED_7: |
| 114 | case FSP_STATUS_RESET_REQUIRED_8: |
| 115 | chipset_handle_reset(status); |
Andrey Petrov | 901e43c | 2016-06-22 19:22:30 -0700 | [diff] [blame] | 116 | break; |
| 117 | default: |
| 118 | break; |
| 119 | } |
| 120 | } |
Aaron Durbin | a85febc | 2020-05-15 15:09:10 -0600 | [diff] [blame] | 121 | |
| 122 | static void *fsp_get_dest_and_load(struct fsp_load_descriptor *fspld, size_t size, |
| 123 | const struct region_device *source_rdev, |
| 124 | uint32_t compression_algo) |
| 125 | { |
| 126 | void *dest; |
| 127 | |
| 128 | if (fspld->get_destination(fspld, &dest, size, source_rdev) < 0) { |
| 129 | printk(BIOS_ERR, "FSP Destination not obtained.\n"); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | if (cbfs_load_and_decompress(source_rdev, 0, region_device_sz(source_rdev), |
| 134 | dest, size, compression_algo) != size) { |
| 135 | printk(BIOS_ERR, "Failed to load FSP component.\n"); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | if (fsp_component_relocate((uintptr_t)dest, dest, size) < 0) { |
| 140 | printk(BIOS_ERR, "Unable to relocate FSP component!\n"); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | return dest; |
| 145 | } |
| 146 | |
| 147 | /* Load the FSP component described by fsp_load_descriptor from cbfs. The FSP |
| 148 | * header object will be validated and filled in on successful load. */ |
| 149 | enum cb_err fsp_load_component(struct fsp_load_descriptor *fspld, struct fsp_header *hdr) |
| 150 | { |
| 151 | struct cbfsf file_desc; |
| 152 | uint32_t compression_algo; |
| 153 | size_t output_size; |
| 154 | void *dest; |
| 155 | struct region_device source_rdev; |
| 156 | struct prog *fsp_prog = &fspld->fsp_prog; |
| 157 | |
| 158 | if (fspld->get_destination == NULL) |
| 159 | return CB_ERR; |
| 160 | |
| 161 | if (cbfs_boot_locate(&file_desc, prog_name(fsp_prog), &fsp_prog->cbfs_type) < 0) |
| 162 | return CB_ERR; |
| 163 | |
| 164 | if (cbfsf_decompression_info(&file_desc, &compression_algo, &output_size) < 0) |
| 165 | return CB_ERR; |
| 166 | |
| 167 | cbfs_file_data(&source_rdev, &file_desc); |
| 168 | |
| 169 | dest = fsp_get_dest_and_load(fspld, output_size, &source_rdev, compression_algo); |
| 170 | |
| 171 | if (dest == NULL) |
| 172 | return CB_ERR; |
| 173 | |
| 174 | prog_set_area(fsp_prog, dest, output_size); |
| 175 | |
| 176 | if (fsp_validate_component(hdr, prog_rdev(fsp_prog)) != CB_SUCCESS) { |
| 177 | printk(BIOS_ERR, "Invalid FSP header after load!\n"); |
| 178 | return CB_ERR; |
| 179 | } |
| 180 | |
| 181 | /* Signal that FSP component has been loaded. */ |
| 182 | prog_segment_loaded(hdr->image_base, hdr->image_size, SEG_FINAL); |
| 183 | |
| 184 | return CB_SUCCESS; |
| 185 | } |