fsp_support.c 11 KB

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