blob: 24097f812003d46f25a0a82337be888479f02e2f [file] [log] [blame]
Patrick Georgiac959032020-05-05 22:49:26 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrey Petrov97389702016-02-25 14:15:37 -08002
Johnny Linb8899ef2020-05-28 14:04:58 +08003#include <boot/coreboot_tables.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02004#include <device/mmio.h>
Aaron Durbina85febc2020-05-15 15:09:10 -06005#include <cbfs.h>
Patrick Rudolphf677d172018-10-01 19:17:11 +02006#include <cf9_reset.h>
Aaron Durbina85febc2020-05-15 15:09:10 -06007#include <commonlib/bsd/compression.h>
8#include <commonlib/fsp.h>
Andrey Petrov97389702016-02-25 14:15:37 -08009#include <console/console.h>
10#include <fsp/util.h>
Andrey Petrov97389702016-02-25 14:15:37 -080011#include <string.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020012#include <types.h>
Andrey Petrov97389702016-02-25 14:15:37 -080013
14static 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
29enum 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 Petrovd5a6eb42016-05-04 17:30:16 -070036 hdr->spec_version = read8(raw_hdr + 10);
Andrey Petrov97389702016-02-25 14:15:37 -080037 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 Petrovd5a6eb42016-05-04 17:30:16 -070043 hdr->image_attribute = read16(raw_hdr + 32);
44 hdr->component_attribute = read16(raw_hdr + 34);
Andrey Petrov97389702016-02-25 14:15:37 -080045 hdr->cfg_region_offset = read32(raw_hdr + 36);
46 hdr->cfg_region_size = read32(raw_hdr + 40);
Brenton Dong0a5971c2016-10-18 11:35:15 -070047 hdr->temp_ram_init_entry = read32(raw_hdr + 48);
48 hdr->temp_ram_exit_entry = read32(raw_hdr + 64);
Andrey Petrov97389702016-02-25 14:15:37 -080049 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 Banik33d9c4a2020-05-26 18:26:54 +053052 if (CONFIG(PLATFORM_USES_FSP2_2))
53 hdr->multi_phase_si_init_entry_offset = read32(raw_hdr + 72);
Andrey Petrov97389702016-02-25 14:15:37 -080054
55 return CB_SUCCESS;
56}
57
Aaron Durbina413e5e2016-07-17 23:06:03 -050058enum 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 Leahyb20d4ba2016-07-31 16:49:28 -070067 printk(BIOS_CRIT, "Could not mmap() FSP header.\n");
Aaron Durbina413e5e2016-07-17 23:06:03 -050068 return CB_ERR;
69 }
70
71 if (fsp_identify(hdr, membase) != CB_SUCCESS) {
72 rdev_munmap(rdev, membase);
Lee Leahyb20d4ba2016-07-31 16:49:28 -070073 printk(BIOS_CRIT, "No valid FSP header\n");
Aaron Durbina413e5e2016-07-17 23:06:03 -050074 return CB_ERR;
75 }
76
77 rdev_munmap(rdev, membase);
78
Julius Wernercd49cce2019-03-05 16:53:33 -080079 if (CONFIG(DISPLAY_FSP_HEADER))
Lee Leahy37b5ef22016-07-31 14:15:49 -070080 fsp_print_header_info(hdr);
Aaron Durbina413e5e2016-07-17 23:06:03 -050081
82 /* Check if size specified in the header matches the cbfs file size */
83 if (region_device_sz(rdev) < hdr->image_size) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -070084 printk(BIOS_CRIT, "Component size bigger than cbfs file.\n");
Aaron Durbina413e5e2016-07-17 23:06:03 -050085 return CB_ERR;
86 }
87
Johnny Lin5b47d772020-11-13 17:21:25 +080088 if (ENV_ROMSTAGE)
89 soc_validate_fsp_version(hdr);
90
Aaron Durbina413e5e2016-07-17 23:06:03 -050091 return CB_SUCCESS;
92}
93
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -070094static bool fsp_reset_requested(uint32_t status)
Andrey Petrov3a94a3b2016-07-18 00:15:41 -070095{
96 return (status >= FSP_STATUS_RESET_REQUIRED_COLD &&
97 status <= FSP_STATUS_RESET_REQUIRED_8);
98}
99
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700100void fsp_handle_reset(uint32_t status)
Andrey Petrov901e43c2016-06-22 19:22:30 -0700101{
Andrey Petrov3a94a3b2016-07-18 00:15:41 -0700102 if (!fsp_reset_requested(status))
103 return;
104
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700105 printk(BIOS_SPEW, "FSP: handling reset type %x\n", status);
Andrey Petrov3a94a3b2016-07-18 00:15:41 -0700106
Lee Leahyb2b97a52017-03-10 08:40:18 -0800107 switch (status) {
Andrey Petrov901e43c2016-06-22 19:22:30 -0700108 case FSP_STATUS_RESET_REQUIRED_COLD:
Patrick Rudolphf677d172018-10-01 19:17:11 +0200109 full_reset();
Andrey Petrov901e43c2016-06-22 19:22:30 -0700110 break;
111 case FSP_STATUS_RESET_REQUIRED_WARM:
Patrick Rudolphf677d172018-10-01 19:17:11 +0200112 system_reset();
Andrey Petrov901e43c2016-06-22 19:22:30 -0700113 break;
Andrey Petrov3a94a3b2016-07-18 00:15:41 -0700114 case FSP_STATUS_RESET_REQUIRED_3:
115 case FSP_STATUS_RESET_REQUIRED_4:
116 case FSP_STATUS_RESET_REQUIRED_5:
117 case FSP_STATUS_RESET_REQUIRED_6:
118 case FSP_STATUS_RESET_REQUIRED_7:
119 case FSP_STATUS_RESET_REQUIRED_8:
120 chipset_handle_reset(status);
Andrey Petrov901e43c2016-06-22 19:22:30 -0700121 break;
122 default:
123 break;
124 }
125}
Aaron Durbina85febc2020-05-15 15:09:10 -0600126
Aaron Durbinecbfa992020-05-15 17:01:58 -0600127static inline bool fspm_env(void)
128{
129 if (ENV_ROMSTAGE)
130 return true;
131 return false;
132}
133
134static inline bool fspm_xip(void)
135{
136 /* FSP-M is assumed to be loaded in romstage. */
137 if (fspm_env() && CONFIG(FSP_M_XIP))
138 return true;
139 return false;
140}
141
Aaron Durbina85febc2020-05-15 15:09:10 -0600142static void *fsp_get_dest_and_load(struct fsp_load_descriptor *fspld, size_t size,
143 const struct region_device *source_rdev,
144 uint32_t compression_algo)
145{
146 void *dest;
147
148 if (fspld->get_destination(fspld, &dest, size, source_rdev) < 0) {
149 printk(BIOS_ERR, "FSP Destination not obtained.\n");
150 return NULL;
151 }
152
Aaron Durbinecbfa992020-05-15 17:01:58 -0600153 /* Don't load when executing in place. */
154 if (fspm_xip())
155 return dest;
156
Aaron Durbina85febc2020-05-15 15:09:10 -0600157 if (cbfs_load_and_decompress(source_rdev, 0, region_device_sz(source_rdev),
158 dest, size, compression_algo) != size) {
159 printk(BIOS_ERR, "Failed to load FSP component.\n");
160 return NULL;
161 }
162
Aaron Durbinecbfa992020-05-15 17:01:58 -0600163 /* Don't allow FSP-M relocation. */
164 if (fspm_env())
165 return dest;
166
Aaron Durbina85febc2020-05-15 15:09:10 -0600167 if (fsp_component_relocate((uintptr_t)dest, dest, size) < 0) {
168 printk(BIOS_ERR, "Unable to relocate FSP component!\n");
169 return NULL;
170 }
171
172 return dest;
173}
174
175/* Load the FSP component described by fsp_load_descriptor from cbfs. The FSP
176 * header object will be validated and filled in on successful load. */
177enum cb_err fsp_load_component(struct fsp_load_descriptor *fspld, struct fsp_header *hdr)
178{
179 struct cbfsf file_desc;
180 uint32_t compression_algo;
181 size_t output_size;
182 void *dest;
183 struct region_device source_rdev;
184 struct prog *fsp_prog = &fspld->fsp_prog;
185
186 if (fspld->get_destination == NULL)
187 return CB_ERR;
188
189 if (cbfs_boot_locate(&file_desc, prog_name(fsp_prog), &fsp_prog->cbfs_type) < 0)
190 return CB_ERR;
191
192 if (cbfsf_decompression_info(&file_desc, &compression_algo, &output_size) < 0)
193 return CB_ERR;
194
195 cbfs_file_data(&source_rdev, &file_desc);
196
197 dest = fsp_get_dest_and_load(fspld, output_size, &source_rdev, compression_algo);
198
199 if (dest == NULL)
200 return CB_ERR;
201
202 prog_set_area(fsp_prog, dest, output_size);
203
204 if (fsp_validate_component(hdr, prog_rdev(fsp_prog)) != CB_SUCCESS) {
205 printk(BIOS_ERR, "Invalid FSP header after load!\n");
206 return CB_ERR;
207 }
208
209 /* Signal that FSP component has been loaded. */
210 prog_segment_loaded(hdr->image_base, hdr->image_size, SEG_FINAL);
211
212 return CB_SUCCESS;
213}
Johnny Linb8899ef2020-05-28 14:04:58 +0800214
215/* Only call this function when FSP header has been read and validated */
216void fsp_get_version(char *buf)
217{
218 struct fsp_header *hdr = &fsps_hdr;
Johnny Lin5b47d772020-11-13 17:21:25 +0800219 union fsp_revision revision;
Johnny Linb8899ef2020-05-28 14:04:58 +0800220
221 revision.val = hdr->fsp_revision;
222 snprintf(buf, FSP_VER_LEN, "%u.%u-%u.%u.%u.%u", (hdr->spec_version >> 4),
223 hdr->spec_version & 0xf, revision.rev.major,
224 revision.rev.minor, revision.rev.revision, revision.rev.bld_num);
225}
226
Felix Held88995982021-01-28 22:43:52 +0100227/* Check if the signature in the UPD header matches the expected one. If it doesn't match, the
228 FSP binaries in CBFS are for a different platform than the platform code trying to use it
229 in which case the function calls die(). */
230void fsp_verify_upd_header_signature(uint64_t upd_signature, uint64_t expected_signature)
231{
232 if (upd_signature != expected_signature) {
233 /* The UPD signatures are non-zero-terminated ASCII stored as a little endian
234 uint64_t, so this needs some casts. */
235 die_with_post_code(POST_INVALID_VENDOR_BINARY,
236 "Invalid UPD signature! FSP provided \"%8s\", expected was \"%8s\".\n",
237 (char *)&upd_signature,
238 (char *)&expected_signature);
239 }
240}
241
Johnny Linb8899ef2020-05-28 14:04:58 +0800242/* Add FSP version to coreboot table LB_TAG_PLATFORM_BLOB_VERSION */
243void lb_string_platform_blob_version(struct lb_header *header)
244{
245 struct lb_string *rec;
246 size_t len;
247 char fsp_version[FSP_VER_LEN] = {0};
248
249 fsp_get_version(fsp_version);
250 rec = (struct lb_string *)lb_new_record(header);
251 rec->tag = LB_TAG_PLATFORM_BLOB_VERSION;
252 len = strlen(fsp_version);
253 rec->size = ALIGN_UP(sizeof(*rec) + len + 1, 8);
254 memcpy(rec->string, fsp_version, len+1);
255}
Johnny Lin5b47d772020-11-13 17:21:25 +0800256
257__weak void soc_validate_fsp_version(const struct fsp_header *hdr)
258{
Johnny Lin5b47d772020-11-13 17:21:25 +0800259}