• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Intel
2 /*
3  * Copyright (C) 2013, Intel Corporation
4  * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
5  */
6 
7 #include <common.h>
8 #include <asm/fsp1/fsp_support.h>
9 #include <asm/post.h>
10 
fsp_find_header(void)11 struct fsp_header *__attribute__((optimize("O0"))) fsp_find_header(void)
12 {
13 	/*
14 	 * This function may be called before the a stack is established,
15 	 * so special care must be taken. First, it cannot declare any local
16 	 * variable using stack. Only register variable can be used here.
17 	 * Secondly, some compiler version will add prolog or epilog code
18 	 * for the C function. If so the function call may not work before
19 	 * stack is ready.
20 	 *
21 	 * GCC 4.8.1 has been verified to be working for the following codes.
22 	 */
23 	volatile register u8 *fsp asm("eax");
24 
25 	/* Initalize the FSP base */
26 	fsp = (u8 *)CONFIG_FSP_ADDR;
27 
28 	/* Check the FV signature, _FVH */
29 	if (((struct fv_header *)fsp)->sign == EFI_FVH_SIGNATURE) {
30 		/* Go to the end of the FV header and align the address */
31 		fsp += ((struct fv_header *)fsp)->ext_hdr_off;
32 		fsp += ((struct fv_ext_header *)fsp)->ext_hdr_size;
33 		fsp  = (u8 *)(((u32)fsp + 7) & 0xFFFFFFF8);
34 	} else {
35 		fsp  = 0;
36 	}
37 
38 	/* Check the FFS GUID */
39 	if (fsp &&
40 	    ((struct ffs_file_header *)fsp)->name.b[0] == FSP_GUID_BYTE0 &&
41 	    ((struct ffs_file_header *)fsp)->name.b[1] == FSP_GUID_BYTE1 &&
42 	    ((struct ffs_file_header *)fsp)->name.b[2] == FSP_GUID_BYTE2 &&
43 	    ((struct ffs_file_header *)fsp)->name.b[3] == FSP_GUID_BYTE3 &&
44 	    ((struct ffs_file_header *)fsp)->name.b[4] == FSP_GUID_BYTE4 &&
45 	    ((struct ffs_file_header *)fsp)->name.b[5] == FSP_GUID_BYTE5 &&
46 	    ((struct ffs_file_header *)fsp)->name.b[6] == FSP_GUID_BYTE6 &&
47 	    ((struct ffs_file_header *)fsp)->name.b[7] == FSP_GUID_BYTE7 &&
48 	    ((struct ffs_file_header *)fsp)->name.b[8] == FSP_GUID_BYTE8 &&
49 	    ((struct ffs_file_header *)fsp)->name.b[9] == FSP_GUID_BYTE9 &&
50 	    ((struct ffs_file_header *)fsp)->name.b[10] == FSP_GUID_BYTE10 &&
51 	    ((struct ffs_file_header *)fsp)->name.b[11] == FSP_GUID_BYTE11 &&
52 	    ((struct ffs_file_header *)fsp)->name.b[12] == FSP_GUID_BYTE12 &&
53 	    ((struct ffs_file_header *)fsp)->name.b[13] == FSP_GUID_BYTE13 &&
54 	    ((struct ffs_file_header *)fsp)->name.b[14] == FSP_GUID_BYTE14 &&
55 	    ((struct ffs_file_header *)fsp)->name.b[15] == FSP_GUID_BYTE15) {
56 		/* Add the FFS header size to find the raw section header */
57 		fsp += sizeof(struct ffs_file_header);
58 	} else {
59 		fsp = 0;
60 	}
61 
62 	if (fsp &&
63 	    ((struct raw_section *)fsp)->type == EFI_SECTION_RAW) {
64 		/* Add the raw section header size to find the FSP header */
65 		fsp += sizeof(struct raw_section);
66 	} else {
67 		fsp = 0;
68 	}
69 
70 	return (struct fsp_header *)fsp;
71 }
72 
fsp_continue(u32 status,void * hob_list)73 void fsp_continue(u32 status, void *hob_list)
74 {
75 	post_code(POST_MRC);
76 
77 	assert(status == 0);
78 
79 	/* The boot loader main function entry */
80 	fsp_init_done(hob_list);
81 }
82 
fsp_init(u32 stack_top,u32 boot_mode,void * nvs_buf)83 void fsp_init(u32 stack_top, u32 boot_mode, void *nvs_buf)
84 {
85 	struct fsp_config_data config_data;
86 	fsp_init_f init;
87 	struct fsp_init_params params;
88 	struct fspinit_rtbuf rt_buf;
89 	struct fsp_header *fsp_hdr;
90 	struct fsp_init_params *params_ptr;
91 #ifdef CONFIG_FSP_USE_UPD
92 	struct vpd_region *fsp_vpd;
93 	struct upd_region *fsp_upd;
94 #endif
95 
96 	fsp_hdr = fsp_find_header();
97 	if (fsp_hdr == NULL) {
98 		/* No valid FSP info header was found */
99 		panic("Invalid FSP header");
100 	}
101 
102 	config_data.common.fsp_hdr = fsp_hdr;
103 	config_data.common.stack_top = stack_top;
104 	config_data.common.boot_mode = boot_mode;
105 
106 #ifdef CONFIG_FSP_USE_UPD
107 	/* Get VPD region start */
108 	fsp_vpd = (struct vpd_region *)(fsp_hdr->img_base +
109 			fsp_hdr->cfg_region_off);
110 
111 	/* Verify the VPD data region is valid */
112 	assert(fsp_vpd->sign == VPD_IMAGE_ID);
113 
114 	fsp_upd = &config_data.fsp_upd;
115 
116 	/* Copy default data from Flash */
117 	memcpy(fsp_upd, (void *)(fsp_hdr->img_base + fsp_vpd->upd_offset),
118 	       sizeof(struct upd_region));
119 
120 	/* Verify the UPD data region is valid */
121 	assert(fsp_upd->terminator == UPD_TERMINATOR);
122 #endif
123 
124 	memset(&rt_buf, 0, sizeof(struct fspinit_rtbuf));
125 
126 	/* Override any configuration if required */
127 	fsp_update_configs(&config_data, &rt_buf);
128 
129 	memset(&params, 0, sizeof(struct fsp_init_params));
130 	params.nvs_buf = nvs_buf;
131 	params.rt_buf = (struct fspinit_rtbuf *)&rt_buf;
132 	params.continuation = (fsp_continuation_f)fsp_asm_continuation;
133 
134 	init = (fsp_init_f)(fsp_hdr->img_base + fsp_hdr->fsp_init);
135 	params_ptr = &params;
136 
137 	post_code(POST_PRE_MRC);
138 
139 	/* Load GDT for FSP */
140 	setup_fsp_gdt();
141 
142 	/*
143 	 * Use ASM code to ensure the register value in EAX & EDX
144 	 * will be passed into fsp_continue
145 	 */
146 	asm volatile (
147 		"pushl	%0;"
148 		"call	*%%eax;"
149 		".global fsp_asm_continuation;"
150 		"fsp_asm_continuation:;"
151 		"movl	4(%%esp), %%eax;"	/* status */
152 		"movl	8(%%esp), %%edx;"	/* hob_list */
153 		"jmp	fsp_continue;"
154 		: : "m"(params_ptr), "a"(init)
155 	);
156 
157 	/*
158 	 * Should never get here.
159 	 * Control will continue from fsp_continue.
160 	 * This line below is to prevent the compiler from optimizing
161 	 * structure intialization.
162 	 *
163 	 * DO NOT REMOVE!
164 	 */
165 	init(&params);
166 }
167 
fsp_notify(struct fsp_header * fsp_hdr,u32 phase)168 u32 fsp_notify(struct fsp_header *fsp_hdr, u32 phase)
169 {
170 	fsp_notify_f notify;
171 	struct fsp_notify_params params;
172 	struct fsp_notify_params *params_ptr;
173 	u32 status;
174 
175 	if (!fsp_hdr)
176 		fsp_hdr = (struct fsp_header *)fsp_find_header();
177 
178 	if (fsp_hdr == NULL) {
179 		/* No valid FSP info header */
180 		panic("Invalid FSP header");
181 	}
182 
183 	notify = (fsp_notify_f)(fsp_hdr->img_base + fsp_hdr->fsp_notify);
184 	params.phase = phase;
185 	params_ptr = &params;
186 
187 	/*
188 	 * Use ASM code to ensure correct parameter is on the stack for
189 	 * FspNotify as U-Boot is using different ABI from FSP
190 	 */
191 	asm volatile (
192 		"pushl	%1;"		/* push notify phase */
193 		"call	*%%eax;"	/* call FspNotify */
194 		"addl	$4, %%esp;"	/* clean up the stack */
195 		: "=a"(status) : "m"(params_ptr), "a"(notify), "m"(*params_ptr)
196 	);
197 
198 	return status;
199 }
200