fsp_support.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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(struct shared_data *shared_data, u32 status, void *hob_list)
  82. {
  83. u32 stack_len;
  84. u32 stack_base;
  85. u32 stack_top;
  86. post_code(POST_MRC);
  87. assert(status == 0);
  88. /* Get the migrated stack in normal memory */
  89. stack_base = (u32)fsp_get_bootloader_tmp_mem(hob_list, &stack_len);
  90. assert(stack_base != 0);
  91. stack_top = stack_base + stack_len - sizeof(u32);
  92. /*
  93. * Old stack base is stored at the very end of the stack top,
  94. * use it to calculate the migrated shared data base
  95. */
  96. shared_data = (struct shared_data *)(stack_base +
  97. ((u32)shared_data - *(u32 *)stack_top));
  98. /* The boot loader main function entry */
  99. fsp_init_done(hob_list);
  100. }
  101. void fsp_init(u32 stack_top, u32 boot_mode, void *nvs_buf)
  102. {
  103. struct shared_data shared_data;
  104. fsp_init_f init;
  105. struct fsp_init_params params;
  106. struct fspinit_rtbuf rt_buf;
  107. struct vpd_region *fsp_vpd;
  108. struct fsp_header *fsp_hdr;
  109. struct fsp_init_params *params_ptr;
  110. struct upd_region *fsp_upd;
  111. #ifdef CONFIG_DEBUG_UART
  112. setup_early_uart();
  113. #endif
  114. fsp_hdr = find_fsp_header();
  115. if (fsp_hdr == NULL) {
  116. /* No valid FSP info header was found */
  117. panic("Invalid FSP header");
  118. }
  119. fsp_upd = &shared_data.fsp_upd;
  120. memset(&rt_buf, 0, sizeof(struct fspinit_rtbuf));
  121. /* Reserve a gap in stack top */
  122. rt_buf.common.stack_top = (u32 *)stack_top - 32;
  123. rt_buf.common.boot_mode = boot_mode;
  124. rt_buf.common.upd_data = fsp_upd;
  125. /* Get VPD region start */
  126. fsp_vpd = (struct vpd_region *)(fsp_hdr->img_base +
  127. fsp_hdr->cfg_region_off);
  128. /* Verify the VPD data region is valid */
  129. assert((fsp_vpd->img_rev == VPD_IMAGE_REV) &&
  130. (fsp_vpd->sign == VPD_IMAGE_ID));
  131. /* Copy default data from Flash */
  132. memcpy(fsp_upd, (void *)(fsp_hdr->img_base + fsp_vpd->upd_offset),
  133. sizeof(struct upd_region));
  134. /* Verify the UPD data region is valid */
  135. assert(fsp_upd->terminator == UPD_TERMINATOR);
  136. /* Override any UPD setting if required */
  137. update_fsp_upd(fsp_upd);
  138. memset(&params, 0, sizeof(struct fsp_init_params));
  139. params.nvs_buf = nvs_buf;
  140. params.rt_buf = (struct fspinit_rtbuf *)&rt_buf;
  141. params.continuation = (fsp_continuation_f)asm_continuation;
  142. init = (fsp_init_f)(fsp_hdr->img_base + fsp_hdr->fsp_init);
  143. params_ptr = &params;
  144. shared_data.fsp_hdr = fsp_hdr;
  145. shared_data.stack_top = (u32 *)stack_top;
  146. post_code(POST_PRE_MRC);
  147. /* Load GDT for FSP */
  148. setup_fsp_gdt();
  149. /*
  150. * Use ASM code to ensure the register value in EAX & ECX
  151. * will be passed into BlContinuationFunc
  152. */
  153. asm volatile (
  154. "pushl %0;"
  155. "call *%%eax;"
  156. ".global asm_continuation;"
  157. "asm_continuation:;"
  158. "movl %%ebx, %%eax;" /* shared_data */
  159. "movl 4(%%esp), %%edx;" /* status */
  160. "movl 8(%%esp), %%ecx;" /* hob_list */
  161. "jmp fsp_continue;"
  162. : : "m"(params_ptr), "a"(init), "b"(&shared_data)
  163. );
  164. /*
  165. * Should never get here.
  166. * Control will continue from fsp_continue.
  167. * This line below is to prevent the compiler from optimizing
  168. * structure intialization.
  169. *
  170. * DO NOT REMOVE!
  171. */
  172. init(&params);
  173. }
  174. u32 fsp_notify(struct fsp_header *fsp_hdr, u32 phase)
  175. {
  176. fsp_notify_f notify;
  177. struct fsp_notify_params params;
  178. struct fsp_notify_params *params_ptr;
  179. u32 status;
  180. if (!fsp_hdr)
  181. fsp_hdr = (struct fsp_header *)find_fsp_header();
  182. if (fsp_hdr == NULL) {
  183. /* No valid FSP info header */
  184. panic("Invalid FSP header");
  185. }
  186. notify = (fsp_notify_f)(fsp_hdr->img_base + fsp_hdr->fsp_notify);
  187. params.phase = phase;
  188. params_ptr = &params;
  189. /*
  190. * Use ASM code to ensure correct parameter is on the stack for
  191. * FspNotify as U-Boot is using different ABI from FSP
  192. */
  193. asm volatile (
  194. "pushl %1;" /* push notify phase */
  195. "call *%%eax;" /* call FspNotify */
  196. "addl $4, %%esp;" /* clean up the stack */
  197. : "=a"(status) : "m"(params_ptr), "a"(notify), "m"(*params_ptr)
  198. );
  199. return status;
  200. }
  201. u32 fsp_get_usable_lowmem_top(const void *hob_list)
  202. {
  203. const struct hob_header *hdr;
  204. struct hob_res_desc *res_desc;
  205. phys_addr_t phys_start;
  206. u32 top;
  207. /* Get the HOB list for processing */
  208. hdr = hob_list;
  209. /* * Collect memory ranges */
  210. top = FSP_LOWMEM_BASE;
  211. while (!end_of_hob(hdr)) {
  212. if (hdr->type == HOB_TYPE_RES_DESC) {
  213. res_desc = (struct hob_res_desc *)hdr;
  214. if (res_desc->type == RES_SYS_MEM) {
  215. phys_start = res_desc->phys_start;
  216. /* Need memory above 1MB to be collected here */
  217. if (phys_start >= FSP_LOWMEM_BASE &&
  218. phys_start < (phys_addr_t)FSP_HIGHMEM_BASE)
  219. top += (u32)(res_desc->len);
  220. }
  221. }
  222. hdr = get_next_hob(hdr);
  223. }
  224. return top;
  225. }
  226. u64 fsp_get_usable_highmem_top(const void *hob_list)
  227. {
  228. const struct hob_header *hdr;
  229. struct hob_res_desc *res_desc;
  230. phys_addr_t phys_start;
  231. u64 top;
  232. /* Get the HOB list for processing */
  233. hdr = hob_list;
  234. /* Collect memory ranges */
  235. top = FSP_HIGHMEM_BASE;
  236. while (!end_of_hob(hdr)) {
  237. if (hdr->type == HOB_TYPE_RES_DESC) {
  238. res_desc = (struct hob_res_desc *)hdr;
  239. if (res_desc->type == RES_SYS_MEM) {
  240. phys_start = res_desc->phys_start;
  241. /* Need memory above 4GB to be collected here */
  242. if (phys_start >= (phys_addr_t)FSP_HIGHMEM_BASE)
  243. top += (u32)(res_desc->len);
  244. }
  245. }
  246. hdr = get_next_hob(hdr);
  247. }
  248. return top;
  249. }
  250. u64 fsp_get_reserved_mem_from_guid(const void *hob_list, u64 *len,
  251. struct efi_guid *guid)
  252. {
  253. const struct hob_header *hdr;
  254. struct hob_res_desc *res_desc;
  255. /* Get the HOB list for processing */
  256. hdr = hob_list;
  257. /* Collect memory ranges */
  258. while (!end_of_hob(hdr)) {
  259. if (hdr->type == HOB_TYPE_RES_DESC) {
  260. res_desc = (struct hob_res_desc *)hdr;
  261. if (res_desc->type == RES_MEM_RESERVED) {
  262. if (compare_guid(&res_desc->owner, guid)) {
  263. if (len)
  264. *len = (u32)(res_desc->len);
  265. return (u64)(res_desc->phys_start);
  266. }
  267. }
  268. }
  269. hdr = get_next_hob(hdr);
  270. }
  271. return 0;
  272. }
  273. u32 fsp_get_fsp_reserved_mem(const void *hob_list, u32 *len)
  274. {
  275. const struct efi_guid guid = FSP_HOB_RESOURCE_OWNER_FSP_GUID;
  276. u64 length;
  277. u32 base;
  278. base = (u32)fsp_get_reserved_mem_from_guid(hob_list,
  279. &length, (struct efi_guid *)&guid);
  280. if ((len != 0) && (base != 0))
  281. *len = (u32)length;
  282. return base;
  283. }
  284. u32 fsp_get_tseg_reserved_mem(const void *hob_list, u32 *len)
  285. {
  286. const struct efi_guid guid = FSP_HOB_RESOURCE_OWNER_TSEG_GUID;
  287. u64 length;
  288. u32 base;
  289. base = (u32)fsp_get_reserved_mem_from_guid(hob_list,
  290. &length, (struct efi_guid *)&guid);
  291. if ((len != 0) && (base != 0))
  292. *len = (u32)length;
  293. return base;
  294. }
  295. const struct hob_header *fsp_get_next_hob(uint type, const void *hob_list)
  296. {
  297. const struct hob_header *hdr;
  298. hdr = hob_list;
  299. /* Parse the HOB list until end of list or matching type is found */
  300. while (!end_of_hob(hdr)) {
  301. if (hdr->type == type)
  302. return hdr;
  303. hdr = get_next_hob(hdr);
  304. }
  305. return NULL;
  306. }
  307. const struct hob_header *fsp_get_next_guid_hob(const struct efi_guid *guid,
  308. const void *hob_list)
  309. {
  310. const struct hob_header *hdr;
  311. struct hob_guid *guid_hob;
  312. hdr = hob_list;
  313. while ((hdr = fsp_get_next_hob(HOB_TYPE_GUID_EXT,
  314. hdr)) != NULL) {
  315. guid_hob = (struct hob_guid *)hdr;
  316. if (compare_guid(guid, &(guid_hob->name)))
  317. break;
  318. hdr = get_next_hob(hdr);
  319. }
  320. return hdr;
  321. }
  322. void *fsp_get_guid_hob_data(const void *hob_list, u32 *len,
  323. struct efi_guid *guid)
  324. {
  325. const struct hob_header *guid_hob;
  326. guid_hob = fsp_get_next_guid_hob(guid, hob_list);
  327. if (guid_hob == NULL) {
  328. return NULL;
  329. } else {
  330. if (len)
  331. *len = get_guid_hob_data_size(guid_hob);
  332. return get_guid_hob_data(guid_hob);
  333. }
  334. }
  335. void *fsp_get_nvs_data(const void *hob_list, u32 *len)
  336. {
  337. const struct efi_guid guid = FSP_NON_VOLATILE_STORAGE_HOB_GUID;
  338. return fsp_get_guid_hob_data(hob_list, len, (struct efi_guid *)&guid);
  339. }
  340. void *fsp_get_bootloader_tmp_mem(const void *hob_list, u32 *len)
  341. {
  342. const struct efi_guid guid = FSP_BOOTLOADER_TEMP_MEM_HOB_GUID;
  343. return fsp_get_guid_hob_data(hob_list, len, (struct efi_guid *)&guid);
  344. }