2 * Copyright (C) 2013, Intel Corporation
3 * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
5 * SPDX-License-Identifier: Intel
9 #include <asm/arch/fsp/fsp_support.h>
13 * Reads a 64-bit value from memory that may be unaligned.
15 * This function returns the 64-bit value pointed to by buf. The function
16 * guarantees that the read operation does not produce an alignment fault.
18 * If the buf is NULL, then ASSERT().
20 * @buf: Pointer to a 64-bit value that may be unaligned.
22 * @return: The 64-bit value read from buf.
24 static u64 read_unaligned64(const u64 *buf)
34 * If the GUIDs are identical then TRUE is returned.
35 * If there are any bit differences in the two GUIDs, then FALSE is returned.
37 * If guid1 is NULL, then ASSERT().
38 * If guid2 is NULL, then ASSERT().
40 * @guid1: A pointer to a 128 bit GUID.
41 * @guid2: A pointer to a 128 bit GUID.
43 * @retval TRUE: guid1 and guid2 are identical.
44 * @retval FALSE: guid1 and guid2 are not identical.
46 static unsigned char compare_guid(const struct efi_guid_t *guid1,
47 const struct efi_guid_t *guid2)
54 guid1_low = read_unaligned64((const u64 *)guid1);
55 guid2_low = read_unaligned64((const u64 *)guid2);
56 guid1_high = read_unaligned64((const u64 *)guid1 + 1);
57 guid2_high = read_unaligned64((const u64 *)guid2 + 1);
59 return (unsigned char)(guid1_low == guid2_low && guid1_high == guid2_high);
62 u32 __attribute__((optimize("O0"))) find_fsp_header(void)
64 volatile register u8 *fsp asm("eax");
66 /* Initalize the FSP base */
67 fsp = (u8 *)CONFIG_FSP_LOCATION;
69 /* Check the FV signature, _FVH */
70 if (((struct fv_header_t *)fsp)->sign == 0x4856465F) {
71 /* Go to the end of the FV header and align the address */
72 fsp += ((struct fv_header_t *)fsp)->ext_hdr_off;
73 fsp += ((struct fv_ext_header_t *)fsp)->ext_hdr_size;
74 fsp = (u8 *)(((u32)fsp + 7) & 0xFFFFFFF8);
79 /* Check the FFS GUID */
81 (((u32 *)&(((struct ffs_file_header_t *)fsp)->name))[0] == 0x912740BE) &&
82 (((u32 *)&(((struct ffs_file_header_t *)fsp)->name))[1] == 0x47342284) &&
83 (((u32 *)&(((struct ffs_file_header_t *)fsp)->name))[2] == 0xB08471B9) &&
84 (((u32 *)&(((struct ffs_file_header_t *)fsp)->name))[3] == 0x0C3F3527)) {
85 /* Add the FFS header size to find the raw section header */
86 fsp += sizeof(struct ffs_file_header_t);
92 ((struct raw_section_t *)fsp)->type == EFI_SECTION_RAW) {
93 /* Add the raw section header size to find the FSP header */
94 fsp += sizeof(struct raw_section_t);
102 void fsp_continue(struct shared_data_t *shared_data, u32 status, void *hob_list)
112 /* Get the migrated stack in normal memory */
113 stack_base = (u32)get_bootloader_tmp_mem(hob_list, &stack_len);
114 ASSERT(stack_base != 0);
115 stack_top = stack_base + stack_len - sizeof(u32);
118 * Old stack base is stored at the very end of the stack top,
119 * use it to calculate the migrated shared data base
121 shared_data = (struct shared_data_t *)(stack_base +
122 ((u32)shared_data - *(u32 *)stack_top));
124 /* The boot loader main function entry */
125 fsp_init_done(hob_list);
128 void fsp_init(u32 stack_top, u32 boot_mode, void *nvs_buf)
130 struct shared_data_t shared_data;
132 struct fsp_init_params_t params;
133 struct fspinit_rtbuf_t rt_buf;
134 struct vpd_region_t *fsp_vpd;
135 struct fsp_header_t *fsp_hdr;
136 struct fsp_init_params_t *params_ptr;
137 struct upd_region_t *fsp_upd;
139 fsp_hdr = (struct fsp_header_t *)find_fsp_header();
140 if (fsp_hdr == NULL) {
141 /* No valid FSP info header was found */
145 fsp_upd = (struct upd_region_t *)&shared_data.fsp_upd;
146 memset((void *)&rt_buf, 0, sizeof(struct fspinit_rtbuf_t));
148 /* Reserve a gap in stack top */
149 rt_buf.common.stack_top = (u32 *)stack_top - 32;
150 rt_buf.common.boot_mode = boot_mode;
151 rt_buf.common.upd_data = (struct upd_region_t *)fsp_upd;
153 /* Get VPD region start */
154 fsp_vpd = (struct vpd_region_t *)(fsp_hdr->img_base +
155 fsp_hdr->cfg_region_off);
157 /* Verifify the VPD data region is valid */
158 ASSERT((fsp_vpd->img_rev == VPD_IMAGE_REV) &&
159 (fsp_vpd->sign == VPD_IMAGE_ID));
161 /* Copy default data from Flash */
162 memcpy(fsp_upd, (void *)(fsp_hdr->img_base + fsp_vpd->upd_offset),
163 sizeof(struct upd_region_t));
165 /* Verifify the UPD data region is valid */
166 ASSERT(fsp_upd->terminator == 0x55AA);
168 /* Override any UPD setting if required */
169 update_fsp_upd(fsp_upd);
171 memset((void *)¶ms, 0, sizeof(struct fsp_init_params_t));
172 params.nvs_buf = nvs_buf;
173 params.rt_buf = (struct fspinit_rtbuf_t *)&rt_buf;
174 params.continuation = (fsp_continuation_f)asm_continuation;
176 init = (fsp_init_f)(fsp_hdr->img_base + fsp_hdr->fsp_init);
177 params_ptr = ¶ms;
179 shared_data.fsp_hdr = fsp_hdr;
180 shared_data.stack_top = (u32 *)stack_top;
182 post_code(POST_PRE_MRC);
185 * Use ASM code to ensure the register value in EAX & ECX
186 * will be passed into BlContinuationFunc
191 ".global asm_continuation;"
193 "movl %%ebx, %%eax;" /* shared_data */
194 "movl 4(%%esp), %%edx;" /* status */
195 "movl 8(%%esp), %%ecx;" /* hob_list */
197 : : "m"(params_ptr), "a"(init), "b"(&shared_data)
201 * Should never get here.
202 * Control will continue from romstage_main_continue_asm.
203 * This line below is to prevent the compiler from optimizing
204 * structure intialization.
209 * Should never return.
210 * Control will continue from ContinuationFunc
215 u32 fsp_notify(struct fsp_header_t *fsp_hdr, u32 phase)
218 struct fsp_notify_params_t params;
219 struct fsp_notify_params_t *params_ptr;
223 fsp_hdr = (struct fsp_header_t *)find_fsp_header();
225 if (fsp_hdr == NULL) {
226 /* No valid FSP info header */
230 notify = (fsp_notify_f)(fsp_hdr->img_base + fsp_hdr->fsp_notify);
231 params.phase = phase;
232 params_ptr = ¶ms;
235 * Use ASM code to ensure correct parameter is on the stack for
236 * FspNotify as U-Boot is using different ABI from FSP
239 "pushl %1;" /* push notify phase */
240 "call *%%eax;" /* call FspNotify */
241 "addl $4, %%esp;" /* clean up the stack */
242 : "=a"(status) : "m"(params_ptr), "a"(notify), "m"(*params_ptr)
248 u32 get_usable_lowmem_top(const void *hob_list)
250 union hob_pointers_t hob;
251 phys_addr_t phys_start;
254 /* Get the HOB list for processing */
255 hob.raw = (void *)hob_list;
257 /* * Collect memory ranges */
259 while (!END_OF_HOB(hob)) {
260 if (hob.hdr->type == HOB_TYPE_RES_DESC) {
261 if (hob.res_desc->type == RES_SYS_MEM) {
262 phys_start = hob.res_desc->phys_start;
263 /* Need memory above 1MB to be collected here */
264 if (phys_start >= 0x100000 &&
265 phys_start < (phys_addr_t)0x100000000)
266 top += (u32)(hob.res_desc->len);
269 hob.raw = GET_NEXT_HOB(hob);
275 u64 get_usable_highmem_top(const void *hob_list)
277 union hob_pointers_t hob;
278 phys_addr_t phys_start;
281 /* Get the HOB list for processing */
282 hob.raw = (void *)hob_list;
284 /* Collect memory ranges */
286 while (!END_OF_HOB(hob)) {
287 if (hob.hdr->type == HOB_TYPE_RES_DESC) {
288 if (hob.res_desc->type == RES_SYS_MEM) {
289 phys_start = hob.res_desc->phys_start;
290 /* Need memory above 1MB to be collected here */
291 if (phys_start >= (phys_addr_t)0x100000000)
292 top += (u32)(hob.res_desc->len);
295 hob.raw = GET_NEXT_HOB(hob);
301 u64 get_fsp_reserved_mem_from_guid(const void *hob_list, u64 *len,
302 struct efi_guid_t *guid)
304 union hob_pointers_t hob;
306 /* Get the HOB list for processing */
307 hob.raw = (void *)hob_list;
309 /* Collect memory ranges */
310 while (!END_OF_HOB(hob)) {
311 if (hob.hdr->type == HOB_TYPE_RES_DESC) {
312 if (hob.res_desc->type == RES_MEM_RESERVED) {
313 if (compare_guid(&hob.res_desc->owner, guid)) {
315 *len = (u32)(hob.res_desc->len);
317 return (u64)(hob.res_desc->phys_start);
321 hob.raw = GET_NEXT_HOB(hob);
327 u32 get_fsp_reserved_mem(const void *hob_list, u32 *len)
329 const struct efi_guid_t guid = FSP_HOB_RESOURCE_OWNER_FSP_GUID;
333 base = (u32)get_fsp_reserved_mem_from_guid(hob_list,
334 &length, (struct efi_guid_t *)&guid);
335 if ((len != 0) && (base != 0))
341 u32 get_tseg_reserved_mem(const void *hob_list, u32 *len)
343 const struct efi_guid_t guid = FSP_HOB_RESOURCE_OWNER_TSEG_GUID;
347 base = (u32)get_fsp_reserved_mem_from_guid(hob_list,
348 &length, (struct efi_guid_t *)&guid);
349 if ((len != 0) && (base != 0))
355 void *get_next_hob(u16 type, const void *hob_list)
357 union hob_pointers_t hob;
359 ASSERT(hob_list != NULL);
361 hob.raw = (u8 *)hob_list;
363 /* Parse the HOB list until end of list or matching type is found */
364 while (!END_OF_HOB(hob)) {
365 if (hob.hdr->type == type)
368 hob.raw = GET_NEXT_HOB(hob);
374 void *get_next_guid_hob(const struct efi_guid_t *guid, const void *hob_list)
376 union hob_pointers_t hob;
378 hob.raw = (u8 *)hob_list;
379 while ((hob.raw = get_next_hob(HOB_TYPE_GUID_EXT,
381 if (compare_guid(guid, &hob.guid->name))
383 hob.raw = GET_NEXT_HOB(hob);
389 void *get_guid_hob_data(const void *hob_list, u32 *len, struct efi_guid_t *guid)
393 guid_hob = get_next_guid_hob(guid, hob_list);
394 if (guid_hob == NULL) {
398 *len = GET_GUID_HOB_DATA_SIZE(guid_hob);
400 return GET_GUID_HOB_DATA(guid_hob);
404 void *get_fsp_nvs_data(const void *hob_list, u32 *len)
406 const struct efi_guid_t guid = FSP_NON_VOLATILE_STORAGE_HOB_GUID;
408 return get_guid_hob_data(hob_list, len, (struct efi_guid_t *)&guid);
411 void *get_bootloader_tmp_mem(const void *hob_list, u32 *len)
413 const struct efi_guid_t guid = FSP_BOOTLOADER_TEMP_MEM_HOB_GUID;
415 return get_guid_hob_data(hob_list, len, (struct efi_guid_t *)&guid);