hob.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2006-2014, Intel Corporation. All rights reserved.
  3. * Copyright (c) 2017-2024, Arm Limited and Contributors. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <inttypes.h>
  10. #include <string.h>
  11. #include <arch.h>
  12. #include <common/debug.h>
  13. #include <lib/hob/hob.h>
  14. #include <lib/hob/hob_guid.h>
  15. #include <lib/hob/mmram.h>
  16. #include <lib/utils_def.h>
  17. #define ALIGN_UP(x, a) ((x + (a - 1)) & ~(a - 1))
  18. static void *_create_hob(struct efi_hob_handoff_info_table *hob_table,
  19. uint16_t hob_type, uint16_t hob_length)
  20. {
  21. size_t free_mem_size;
  22. struct efi_hob_generic_header *new_hob;
  23. struct efi_hob_generic_header *hob_end;
  24. if ((hob_table == NULL) || (hob_length == 0)) {
  25. return NULL;
  26. }
  27. hob_length = ALIGN_UP(hob_length, 8);
  28. free_mem_size = hob_table->efi_free_memory_top - hob_table->efi_free_memory_bottom;
  29. /**
  30. * hob_length already including sizeof(efi_hob_generic_header).
  31. * See the each export interface create_xxx_hob.
  32. */
  33. if ((size_t) hob_length > free_mem_size) {
  34. return NULL;
  35. }
  36. new_hob = (struct efi_hob_generic_header *) hob_table->efi_end_of_hob_list;
  37. new_hob->hob_type = hob_type;
  38. new_hob->hob_length = hob_length;
  39. new_hob->reserved = 0x00;
  40. hob_end = (struct efi_hob_generic_header *) (hob_table->efi_end_of_hob_list + hob_length);
  41. hob_end->hob_type = EFI_HOB_TYPE_END_OF_HOB_LIST;
  42. hob_end->hob_length = sizeof(struct efi_hob_generic_header);
  43. hob_end->reserved = 0x00;
  44. hob_table->efi_end_of_hob_list = (efi_physical_address_t) hob_end;
  45. hob_table->efi_free_memory_bottom = (efi_physical_address_t) (hob_end + 1);
  46. return new_hob;
  47. }
  48. /*
  49. * Create PHIT HOB list.
  50. *
  51. * On success, return the address PHIT HOB list
  52. * On error, return NULL.
  53. *
  54. * efi_memory_begin
  55. * Base address for partition.
  56. * efi_memory_length
  57. * Size of memory for patition.
  58. * efi_free_memory_bottom
  59. * Base address PHIT HOB list can be allocated
  60. * efi_free_memory_length.
  61. * Maximum size of PHIT HOB list can have
  62. */
  63. struct efi_hob_handoff_info_table *create_hob_list(
  64. efi_physical_address_t efi_memory_begin, size_t efi_memory_length,
  65. efi_physical_address_t efi_free_memory_bottom, size_t efi_free_memory_length)
  66. {
  67. struct efi_hob_handoff_info_table *hob_table;
  68. struct efi_hob_generic_header *hob_end;
  69. if ((efi_memory_begin == 0) || (efi_free_memory_bottom == 0) ||
  70. (efi_memory_length == 0) || (efi_free_memory_length == 0)) {
  71. return NULL;
  72. }
  73. hob_table = (struct efi_hob_handoff_info_table *) efi_free_memory_bottom;
  74. hob_end = (struct efi_hob_generic_header *) (hob_table + 1);
  75. hob_table->header.hob_type = EFI_HOB_TYPE_HANDOFF;
  76. hob_table->header.hob_length = sizeof(struct efi_hob_handoff_info_table);
  77. hob_table->header.reserved = 0;
  78. hob_end->hob_type = EFI_HOB_TYPE_END_OF_HOB_LIST;
  79. hob_end->hob_length = sizeof(struct efi_hob_generic_header);
  80. hob_end->reserved = 0;
  81. hob_table->version = EFI_HOB_HANDOFF_TABLE_VERSION;
  82. hob_table->boot_mode = EFI_BOOT_WITH_FULL_CONFIGURATION;
  83. hob_table->efi_memory_top = efi_memory_begin + efi_memory_length;
  84. hob_table->efi_memory_bottom = efi_memory_begin;
  85. hob_table->efi_free_memory_top = efi_memory_begin + efi_free_memory_length;
  86. hob_table->efi_free_memory_bottom = (efi_physical_address_t) (hob_end + 1);
  87. hob_table->efi_end_of_hob_list = (efi_physical_address_t) hob_end;
  88. return hob_table;
  89. }
  90. /*
  91. * Create resource description HOB in PHIT HOB list.
  92. *
  93. * On success, return 0.
  94. * On error, return error code.
  95. *
  96. * hob_table
  97. * Address of PHIT HOB list
  98. * resource_type
  99. * Resource type see EFI_RESOURCE_* in the include/lib/hob/efi_types.h
  100. * resource_attribute
  101. * Resource attribute see EFI_RESOURCE_ATTRIBUTES_*
  102. * in the include/lib/hob/efi_types.h
  103. * phy_addr_start
  104. * Physical base address of resource
  105. * resource_length
  106. * Size of resource
  107. */
  108. int create_resource_descriptor_hob(
  109. struct efi_hob_handoff_info_table *hob_table,
  110. efi_resource_type_t resource_type,
  111. efi_resource_attribute_type_t resource_attribute,
  112. efi_physical_address_t phy_addr_start,
  113. uint64_t resource_length)
  114. {
  115. struct efi_hob_resource_descriptor *rd_hop;
  116. rd_hop = _create_hob(hob_table, EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,
  117. sizeof(struct efi_hob_resource_descriptor));
  118. if (rd_hop == NULL) {
  119. ERROR("No space for creating resource descriptor type hob...\n");
  120. return -ENOMEM;
  121. }
  122. rd_hop->resource_type = resource_type;
  123. rd_hop->resource_attribute = resource_attribute;
  124. rd_hop->physical_start = phy_addr_start;
  125. rd_hop->resource_length = resource_length;
  126. memset(&rd_hop->owner, 0, sizeof(struct efi_guid));
  127. return 0;
  128. }
  129. /*
  130. * Create GUID HOB in PHIT HOB list.
  131. *
  132. * On success, return 0.
  133. * On error, return error code.
  134. *
  135. * hob_table
  136. * Address of PHIT HOB list
  137. * guid
  138. * guid.
  139. * data length
  140. * Size of data
  141. * data
  142. * Data
  143. */
  144. int create_guid_hob(struct efi_hob_handoff_info_table *hob_table,
  145. struct efi_guid *guid, uint16_t data_length, void **data)
  146. {
  147. struct efi_hob_guid_type *guid_hob;
  148. uint16_t hob_length;
  149. hob_length = data_length + sizeof(struct efi_hob_guid_type);
  150. if ((guid == NULL) || (data == NULL) || (hob_length < data_length)) {
  151. return -EINVAL;
  152. }
  153. guid_hob = _create_hob(hob_table, EFI_HOB_TYPE_GUID_EXTENSION, hob_length);
  154. if (guid_hob == NULL) {
  155. ERROR("No space for creating guid type hob...\n");
  156. return -ENOMEM;
  157. }
  158. memcpy(&guid_hob->name, guid, sizeof(struct efi_guid));
  159. *data = (void *) (guid_hob + 1);
  160. return 0;
  161. }
  162. /*
  163. * Create Firmware Volume HOB in PHIT HOB list.
  164. *
  165. * On success, return 0.
  166. * On error, return error code.
  167. *
  168. * hob_table
  169. * Address of PHIT HOB list
  170. * base_addr
  171. * Base address of firmware volume
  172. * size
  173. * Size of Firmware Volume
  174. */
  175. int create_fv_hob(struct efi_hob_handoff_info_table *hob_table,
  176. efi_physical_address_t base_addr, uint64_t size)
  177. {
  178. struct efi_hob_firmware_volume *fv_hob;
  179. fv_hob = _create_hob(hob_table, EFI_HOB_TYPE_FV,
  180. sizeof(struct efi_hob_firmware_volume));
  181. if (fv_hob == NULL) {
  182. ERROR("No space for creating fv type hob...\n");
  183. return -ENOMEM;
  184. }
  185. fv_hob->base_address = base_addr;
  186. fv_hob->length = size;
  187. return 0;
  188. }