1 /* 2 * Copyright (C) 2013, Intel Corporation 3 * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com> 4 * 5 * SPDX-License-Identifier: Intel 6 */ 7 8 #include <common.h> 9 #include <asm/fsp/fsp_support.h> 10 #include <asm/post.h> 11 12 /** 13 * Compares two GUIDs 14 * 15 * If the GUIDs are identical then true is returned. 16 * If there are any bit differences in the two GUIDs, then false is returned. 17 * 18 * @guid1: A pointer to a 128 bit GUID. 19 * @guid2: A pointer to a 128 bit GUID. 20 * 21 * @retval true: guid1 and guid2 are identical. 22 * @retval false: guid1 and guid2 are not identical. 23 */ 24 static bool compare_guid(const struct efi_guid *guid1, 25 const struct efi_guid *guid2) 26 { 27 if (memcmp(guid1, guid2, sizeof(struct efi_guid)) == 0) 28 return true; 29 else 30 return false; 31 } 32 33 struct fsp_header *__attribute__((optimize("O0"))) find_fsp_header(void) 34 { 35 /* 36 * This function may be called before the a stack is established, 37 * so special care must be taken. First, it cannot declare any local 38 * variable using stack. Only register variable can be used here. 39 * Secondly, some compiler version will add prolog or epilog code 40 * for the C function. If so the function call may not work before 41 * stack is ready. 42 * 43 * GCC 4.8.1 has been verified to be working for the following codes. 44 */ 45 volatile register u8 *fsp asm("eax"); 46 47 /* Initalize the FSP base */ 48 fsp = (u8 *)CONFIG_FSP_ADDR; 49 50 /* Check the FV signature, _FVH */ 51 if (((struct fv_header *)fsp)->sign == EFI_FVH_SIGNATURE) { 52 /* Go to the end of the FV header and align the address */ 53 fsp += ((struct fv_header *)fsp)->ext_hdr_off; 54 fsp += ((struct fv_ext_header *)fsp)->ext_hdr_size; 55 fsp = (u8 *)(((u32)fsp + 7) & 0xFFFFFFF8); 56 } else { 57 fsp = 0; 58 } 59 60 /* Check the FFS GUID */ 61 if (fsp && 62 ((struct ffs_file_header *)fsp)->name.data1 == FSP_GUID_DATA1 && 63 ((struct ffs_file_header *)fsp)->name.data2 == FSP_GUID_DATA2 && 64 ((struct ffs_file_header *)fsp)->name.data3 == FSP_GUID_DATA3 && 65 ((struct ffs_file_header *)fsp)->name.data4[0] == FSP_GUID_DATA4_0 && 66 ((struct ffs_file_header *)fsp)->name.data4[1] == FSP_GUID_DATA4_1 && 67 ((struct ffs_file_header *)fsp)->name.data4[2] == FSP_GUID_DATA4_2 && 68 ((struct ffs_file_header *)fsp)->name.data4[3] == FSP_GUID_DATA4_3 && 69 ((struct ffs_file_header *)fsp)->name.data4[4] == FSP_GUID_DATA4_4 && 70 ((struct ffs_file_header *)fsp)->name.data4[5] == FSP_GUID_DATA4_5 && 71 ((struct ffs_file_header *)fsp)->name.data4[6] == FSP_GUID_DATA4_6 && 72 ((struct ffs_file_header *)fsp)->name.data4[7] == FSP_GUID_DATA4_7) { 73 /* Add the FFS header size to find the raw section header */ 74 fsp += sizeof(struct ffs_file_header); 75 } else { 76 fsp = 0; 77 } 78 79 if (fsp && 80 ((struct raw_section *)fsp)->type == EFI_SECTION_RAW) { 81 /* Add the raw section header size to find the FSP header */ 82 fsp += sizeof(struct raw_section); 83 } else { 84 fsp = 0; 85 } 86 87 return (struct fsp_header *)fsp; 88 } 89 90 void fsp_continue(u32 status, void *hob_list) 91 { 92 post_code(POST_MRC); 93 94 assert(status == 0); 95 96 /* The boot loader main function entry */ 97 fsp_init_done(hob_list); 98 } 99 100 void fsp_init(u32 stack_top, u32 boot_mode, void *nvs_buf) 101 { 102 struct fsp_config_data config_data; 103 fsp_init_f init; 104 struct fsp_init_params params; 105 struct fspinit_rtbuf rt_buf; 106 struct fsp_header *fsp_hdr; 107 struct fsp_init_params *params_ptr; 108 #ifdef CONFIG_FSP_USE_UPD 109 struct vpd_region *fsp_vpd; 110 struct upd_region *fsp_upd; 111 #endif 112 113 #ifdef CONFIG_DEBUG_UART 114 setup_early_uart(); 115 #endif 116 117 fsp_hdr = find_fsp_header(); 118 if (fsp_hdr == NULL) { 119 /* No valid FSP info header was found */ 120 panic("Invalid FSP header"); 121 } 122 123 config_data.common.fsp_hdr = fsp_hdr; 124 config_data.common.stack_top = stack_top; 125 config_data.common.boot_mode = boot_mode; 126 127 #ifdef CONFIG_FSP_USE_UPD 128 /* Get VPD region start */ 129 fsp_vpd = (struct vpd_region *)(fsp_hdr->img_base + 130 fsp_hdr->cfg_region_off); 131 132 /* Verify the VPD data region is valid */ 133 assert(fsp_vpd->sign == VPD_IMAGE_ID); 134 135 fsp_upd = &config_data.fsp_upd; 136 137 /* Copy default data from Flash */ 138 memcpy(fsp_upd, (void *)(fsp_hdr->img_base + fsp_vpd->upd_offset), 139 sizeof(struct upd_region)); 140 141 /* Verify the UPD data region is valid */ 142 assert(fsp_upd->terminator == UPD_TERMINATOR); 143 #endif 144 145 memset(&rt_buf, 0, sizeof(struct fspinit_rtbuf)); 146 147 /* Initialize runtime buffer for fsp_init() */ 148 rt_buf.common.stack_top = stack_top - 32; 149 rt_buf.common.boot_mode = boot_mode; 150 rt_buf.common.upd_data = fsp_upd; 151 152 /* Override any configuration if required */ 153 update_fsp_configs(&config_data); 154 155 memset(¶ms, 0, sizeof(struct fsp_init_params)); 156 params.nvs_buf = nvs_buf; 157 params.rt_buf = (struct fspinit_rtbuf *)&rt_buf; 158 params.continuation = (fsp_continuation_f)asm_continuation; 159 160 init = (fsp_init_f)(fsp_hdr->img_base + fsp_hdr->fsp_init); 161 params_ptr = ¶ms; 162 163 post_code(POST_PRE_MRC); 164 165 /* Load GDT for FSP */ 166 setup_fsp_gdt(); 167 168 /* 169 * Use ASM code to ensure the register value in EAX & EDX 170 * will be passed into fsp_continue 171 */ 172 asm volatile ( 173 "pushl %0;" 174 "call *%%eax;" 175 ".global asm_continuation;" 176 "asm_continuation:;" 177 "movl 4(%%esp), %%eax;" /* status */ 178 "movl 8(%%esp), %%edx;" /* hob_list */ 179 "jmp fsp_continue;" 180 : : "m"(params_ptr), "a"(init) 181 ); 182 183 /* 184 * Should never get here. 185 * Control will continue from fsp_continue. 186 * This line below is to prevent the compiler from optimizing 187 * structure intialization. 188 * 189 * DO NOT REMOVE! 190 */ 191 init(¶ms); 192 } 193 194 u32 fsp_notify(struct fsp_header *fsp_hdr, u32 phase) 195 { 196 fsp_notify_f notify; 197 struct fsp_notify_params params; 198 struct fsp_notify_params *params_ptr; 199 u32 status; 200 201 if (!fsp_hdr) 202 fsp_hdr = (struct fsp_header *)find_fsp_header(); 203 204 if (fsp_hdr == NULL) { 205 /* No valid FSP info header */ 206 panic("Invalid FSP header"); 207 } 208 209 notify = (fsp_notify_f)(fsp_hdr->img_base + fsp_hdr->fsp_notify); 210 params.phase = phase; 211 params_ptr = ¶ms; 212 213 /* 214 * Use ASM code to ensure correct parameter is on the stack for 215 * FspNotify as U-Boot is using different ABI from FSP 216 */ 217 asm volatile ( 218 "pushl %1;" /* push notify phase */ 219 "call *%%eax;" /* call FspNotify */ 220 "addl $4, %%esp;" /* clean up the stack */ 221 : "=a"(status) : "m"(params_ptr), "a"(notify), "m"(*params_ptr) 222 ); 223 224 return status; 225 } 226 227 u32 fsp_get_usable_lowmem_top(const void *hob_list) 228 { 229 const struct hob_header *hdr; 230 struct hob_res_desc *res_desc; 231 phys_addr_t phys_start; 232 u32 top; 233 234 /* Get the HOB list for processing */ 235 hdr = hob_list; 236 237 /* * Collect memory ranges */ 238 top = FSP_LOWMEM_BASE; 239 while (!end_of_hob(hdr)) { 240 if (hdr->type == HOB_TYPE_RES_DESC) { 241 res_desc = (struct hob_res_desc *)hdr; 242 if (res_desc->type == RES_SYS_MEM) { 243 phys_start = res_desc->phys_start; 244 /* Need memory above 1MB to be collected here */ 245 if (phys_start >= FSP_LOWMEM_BASE && 246 phys_start < (phys_addr_t)FSP_HIGHMEM_BASE) 247 top += (u32)(res_desc->len); 248 } 249 } 250 hdr = get_next_hob(hdr); 251 } 252 253 return top; 254 } 255 256 u64 fsp_get_usable_highmem_top(const void *hob_list) 257 { 258 const struct hob_header *hdr; 259 struct hob_res_desc *res_desc; 260 phys_addr_t phys_start; 261 u64 top; 262 263 /* Get the HOB list for processing */ 264 hdr = hob_list; 265 266 /* Collect memory ranges */ 267 top = FSP_HIGHMEM_BASE; 268 while (!end_of_hob(hdr)) { 269 if (hdr->type == HOB_TYPE_RES_DESC) { 270 res_desc = (struct hob_res_desc *)hdr; 271 if (res_desc->type == RES_SYS_MEM) { 272 phys_start = res_desc->phys_start; 273 /* Need memory above 4GB to be collected here */ 274 if (phys_start >= (phys_addr_t)FSP_HIGHMEM_BASE) 275 top += (u32)(res_desc->len); 276 } 277 } 278 hdr = get_next_hob(hdr); 279 } 280 281 return top; 282 } 283 284 u64 fsp_get_reserved_mem_from_guid(const void *hob_list, u64 *len, 285 struct efi_guid *guid) 286 { 287 const struct hob_header *hdr; 288 struct hob_res_desc *res_desc; 289 290 /* Get the HOB list for processing */ 291 hdr = hob_list; 292 293 /* Collect memory ranges */ 294 while (!end_of_hob(hdr)) { 295 if (hdr->type == HOB_TYPE_RES_DESC) { 296 res_desc = (struct hob_res_desc *)hdr; 297 if (res_desc->type == RES_MEM_RESERVED) { 298 if (compare_guid(&res_desc->owner, guid)) { 299 if (len) 300 *len = (u32)(res_desc->len); 301 302 return (u64)(res_desc->phys_start); 303 } 304 } 305 } 306 hdr = get_next_hob(hdr); 307 } 308 309 return 0; 310 } 311 312 u32 fsp_get_fsp_reserved_mem(const void *hob_list, u32 *len) 313 { 314 const struct efi_guid guid = FSP_HOB_RESOURCE_OWNER_FSP_GUID; 315 u64 length; 316 u32 base; 317 318 base = (u32)fsp_get_reserved_mem_from_guid(hob_list, 319 &length, (struct efi_guid *)&guid); 320 if ((len != 0) && (base != 0)) 321 *len = (u32)length; 322 323 return base; 324 } 325 326 u32 fsp_get_tseg_reserved_mem(const void *hob_list, u32 *len) 327 { 328 const struct efi_guid guid = FSP_HOB_RESOURCE_OWNER_TSEG_GUID; 329 u64 length; 330 u32 base; 331 332 base = (u32)fsp_get_reserved_mem_from_guid(hob_list, 333 &length, (struct efi_guid *)&guid); 334 if ((len != 0) && (base != 0)) 335 *len = (u32)length; 336 337 return base; 338 } 339 340 const struct hob_header *fsp_get_next_hob(uint type, const void *hob_list) 341 { 342 const struct hob_header *hdr; 343 344 hdr = hob_list; 345 346 /* Parse the HOB list until end of list or matching type is found */ 347 while (!end_of_hob(hdr)) { 348 if (hdr->type == type) 349 return hdr; 350 351 hdr = get_next_hob(hdr); 352 } 353 354 return NULL; 355 } 356 357 const struct hob_header *fsp_get_next_guid_hob(const struct efi_guid *guid, 358 const void *hob_list) 359 { 360 const struct hob_header *hdr; 361 struct hob_guid *guid_hob; 362 363 hdr = hob_list; 364 while ((hdr = fsp_get_next_hob(HOB_TYPE_GUID_EXT, 365 hdr)) != NULL) { 366 guid_hob = (struct hob_guid *)hdr; 367 if (compare_guid(guid, &(guid_hob->name))) 368 break; 369 hdr = get_next_hob(hdr); 370 } 371 372 return hdr; 373 } 374 375 void *fsp_get_guid_hob_data(const void *hob_list, u32 *len, 376 struct efi_guid *guid) 377 { 378 const struct hob_header *guid_hob; 379 380 guid_hob = fsp_get_next_guid_hob(guid, hob_list); 381 if (guid_hob == NULL) { 382 return NULL; 383 } else { 384 if (len) 385 *len = get_guid_hob_data_size(guid_hob); 386 387 return get_guid_hob_data(guid_hob); 388 } 389 } 390 391 void *fsp_get_nvs_data(const void *hob_list, u32 *len) 392 { 393 const struct efi_guid guid = FSP_NON_VOLATILE_STORAGE_HOB_GUID; 394 395 return fsp_get_guid_hob_data(hob_list, len, (struct efi_guid *)&guid); 396 } 397 398 void *fsp_get_bootloader_tmp_mem(const void *hob_list, u32 *len) 399 { 400 const struct efi_guid guid = FSP_BOOTLOADER_TEMP_MEM_HOB_GUID; 401 402 return fsp_get_guid_hob_data(hob_list, len, (struct efi_guid *)&guid); 403 } 404