1 // SPDX-License-Identifier: Intel
3 * Copyright 2019 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
10 #include <spi_flash.h>
11 #include <asm/fsp/fsp_support.h>
12 #include <asm/fsp2/fsp_internal.h>
14 /* The amount of the FSP header to probe to obtain what we need */
15 #define PROBE_BUF_SIZE 0x180
17 int fsp_get_header(ulong offset, ulong size, bool use_spi_flash,
18 struct fsp_header **fspp)
20 static efi_guid_t guid = FSP_HEADER_GUID;
21 struct fv_ext_header *exhdr;
22 struct fsp_header *fsp;
23 struct ffs_file_header *file_hdr;
25 struct raw_section *raw;
27 u8 buf[PROBE_BUF_SIZE];
32 * There are quite a very steps to work through all the headers in this
33 * file and the structs have similar names. Turn on debugging if needed
34 * to understand what is going wrong.
36 * You are in a maze of twisty little headers all alike.
38 log_debug("offset=%x buf=%x, use_spi_flash=%d\n", (uint)offset,
39 (uint)buf, use_spi_flash);
41 ret = uclass_first_device_err(UCLASS_SPI_FLASH, &dev);
43 return log_msg_ret("Cannot find flash device", ret);
44 ret = spi_flash_read_dm(dev, offset, PROBE_BUF_SIZE, buf);
46 return log_msg_ret("Cannot read flash", ret);
48 memcpy(buf, (void *)offset, PROBE_BUF_SIZE);
51 /* Initalise the FSP base */
55 /* Check the FV signature, _FVH */
56 log_debug("offset=%x sign=%x\n", (uint)offset, (uint)fv->sign);
57 if (fv->sign != EFI_FVH_SIGNATURE)
58 return log_msg_ret("Base FV signature", -EINVAL);
60 /* Go to the end of the FV header and align the address */
61 log_debug("fv->ext_hdr_off = %x\n", fv->ext_hdr_off);
62 ptr += fv->ext_hdr_off;
64 ptr += ALIGN(exhdr->ext_hdr_size, 8);
65 log_debug("ptr=%x\n", ptr - (void *)buf);
67 /* Check the FFS GUID */
69 if (memcmp(&file_hdr->name, &guid, sizeof(guid)))
70 return log_msg_ret("Base FFS GUID", -ENXIO);
71 /* Add the FFS header size to find the raw section header */
75 log_debug("raw->type = %x\n", raw->type);
76 if (raw->type != EFI_SECTION_RAW)
77 return log_msg_ret("Section type not RAW", -ENOEXEC);
79 /* Add the raw section header size to find the FSP header */
83 /* Check the FSPH header */
84 log_debug("fsp %x, fsp-buf=%x, si=%x\n", (uint)fsp, ptr - (void *)buf,
85 (void *)&fsp->fsp_silicon_init - (void *)buf);
86 if (fsp->sign != EFI_FSPH_SIGNATURE)
87 return log_msg_ret("Base FSPH signature", -EACCES);
89 base = (void *)fsp->img_base;
90 log_debug("image base %x\n", (uint)base);
91 if (fsp->fsp_mem_init)
92 log_debug("mem_init offset %x\n", (uint)fsp->fsp_mem_init);
93 else if (fsp->fsp_silicon_init)
94 log_debug("silicon_init offset %x\n",
95 (uint)fsp->fsp_silicon_init);
97 ret = spi_flash_read_dm(dev, offset, size, base);
99 return log_msg_ret("Could not read FPS-M", ret);
101 memcpy(base, (void *)offset, size);
103 ptr = base + (ptr - (void *)buf);
109 u32 fsp_notify(struct fsp_header *fsp_hdr, u32 phase)
112 struct fsp_notify_params params;
113 struct fsp_notify_params *params_ptr;
117 fsp_hdr = gd->arch.fsp_s_hdr;
120 return log_msg_ret("no FSP", -ENOENT);
122 notify = (fsp_notify_f)(fsp_hdr->img_base + fsp_hdr->fsp_notify);
123 params.phase = phase;
124 params_ptr = ¶ms;
127 * Use ASM code to ensure correct parameter is on the stack for
128 * FspNotify as U-Boot is using different ABI from FSP
131 "pushl %1;" /* push notify phase */
132 "call *%%eax;" /* call FspNotify */
133 "addl $4, %%esp;" /* clean up the stack */
134 : "=a"(status) : "m"(params_ptr), "a"(notify), "m"(*params_ptr)