coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
hob.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <arch/hlt.h>
4 #include <console/console.h>
5 #include <fsp/util.h>
6 #include <ip_checksum.h>
7 #include <string.h>
8 
9 /* Compares two EFI GUIDs. Returns true of the GUIDs match, false otherwise. */
10 static bool compare_guid(const EFI_GUID *guid1, const EFI_GUID *guid2)
11 {
12  return !memcmp(guid1, guid2, sizeof(EFI_GUID));
13 }
14 
15 /* Returns the pointer to the HOB list. */
16 void *get_hob_list(void)
17 {
18  void *hob_list;
19 
20  hob_list = fsp_get_hob_list();
21  if (hob_list == NULL)
22  die("Call fsp_set_runtime() before this call!\n");
23  return hob_list;
24 }
25 
26 /* Returns the next instance of a HOB type from the starting HOB. */
27 static void *get_next_hob(uint16_t type, const void *hob_start)
28 {
29  EFI_PEI_HOB_POINTERS hob;
30 
31  if (!hob_start)
32  return NULL;
33 
34  hob.Raw = (UINT8 *)hob_start;
35 
36  /* Parse the HOB list until end of list or matching type is found. */
37  while (!END_OF_HOB_LIST(hob.Raw)) {
38  if (hob.Header->HobType == type)
39  return hob.Raw;
40  if (GET_HOB_LENGTH(hob.Raw) < sizeof(*hob.Header))
41  break;
42  hob.Raw = GET_NEXT_HOB(hob.Raw);
43  }
44  return NULL;
45 }
46 
47 /* Returns the next instance of the matched GUID HOB from the starting HOB. */
48 void *get_guid_hob(const EFI_GUID *guid, const void *hob_start)
49 {
50  EFI_PEI_HOB_POINTERS hob;
51 
52  hob.Raw = (uint8_t *)hob_start;
53  while ((hob.Raw = get_next_hob(EFI_HOB_TYPE_GUID_EXTENSION, hob.Raw))
54  != NULL) {
55  if (compare_guid(guid, &hob.Guid->Name))
56  break;
57  hob.Raw = GET_NEXT_HOB(hob.Raw);
58  }
59  return hob.Raw;
60 }
61 
62 /*
63  * Returns the next instance of the matching resource HOB from the starting HOB.
64  */
65 void *get_resource_hob(const EFI_GUID *guid, const void *hob_start)
66 {
67  EFI_PEI_HOB_POINTERS hob;
68 
69  hob.Raw = (UINT8 *)hob_start;
70  while ((hob.Raw = get_next_hob(EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,
71  hob.Raw)) != NULL) {
72  if (compare_guid(guid, &hob.ResourceDescriptor->Owner))
73  break;
74  hob.Raw = GET_NEXT_HOB(hob.Raw);
75  }
76  return hob.Raw;
77 }
78 
79 static void print_hob_mem_attributes(void *hob_ptr)
80 {
81  EFI_MEMORY_TYPE hob_mem_type;
82  EFI_HOB_MEMORY_ALLOCATION *hob_memory_ptr = hob_ptr;
83  u64 hob_mem_addr = hob_memory_ptr->AllocDescriptor.MemoryBaseAddress;
84  u64 hob_mem_length = hob_memory_ptr->AllocDescriptor.MemoryLength;
85 
86  hob_mem_type = hob_memory_ptr->AllocDescriptor.MemoryType;
87 
88  static const char *hob_mem_type_names[15] = {
89  [EfiReservedMemoryType] = "EfiReservedMemoryType",
90  [EfiLoaderCode] = "EfiLoaderCode",
91  [EfiLoaderData] = "EfiLoaderData",
92  [EfiBootServicesCode] = "EfiBootServicesCode",
93  [EfiBootServicesData] = "EfiBootServicesData",
94  [EfiRuntimeServicesCode] = "EfiRuntimeServicesCode",
95  [EfiRuntimeServicesData] = "EfiRuntimeServicesData",
96  [EfiConventionalMemory] = "EfiConventionalMemory",
97  [EfiUnusableMemory] = "EfiUnusableMemory",
98  [EfiACPIReclaimMemory] = "EfiACPIReclaimMemory",
99  [EfiACPIMemoryNVS] = "EfiACPIMemoryNVS",
100  [EfiMemoryMappedIO] = "EfiMemoryMappedIO",
101  [EfiMemoryMappedIOPortSpace] = "EfiMemoryMappedIOPortSpace",
102  [EfiPalCode] = "EfiPalCode",
103  [EfiMaxMemoryType] = "EfiMaxMemoryType",
104  };
105 
106  if (hob_mem_type >= ARRAY_SIZE(hob_mem_type_names))
107  hob_mem_type = EfiReservedMemoryType;
108 
109  printk(BIOS_SPEW, " Memory type %s (0x%x)\n",
110  hob_mem_type_names[hob_mem_type],
111  (u32)hob_mem_type);
112  printk(BIOS_SPEW, " at location 0x%0lx with length 0x%0lx\n",
113  (unsigned long)hob_mem_addr,
114  (unsigned long)hob_mem_length);
115 }
116 
117 static void print_hob_resource_attributes(void *hob_ptr)
118 {
119  EFI_HOB_RESOURCE_DESCRIPTOR *hob_resource_ptr =
120  (EFI_HOB_RESOURCE_DESCRIPTOR *)hob_ptr;
121  u32 hob_res_type = hob_resource_ptr->ResourceType;
122  u32 hob_res_attr = hob_resource_ptr->ResourceAttribute;
123  u64 hob_res_addr = hob_resource_ptr->PhysicalStart;
124  u64 hob_res_length = hob_resource_ptr->ResourceLength;
125  const char *hob_res_type_str = NULL;
126 
127  /* HOB Resource Types */
128  switch (hob_res_type) {
129  case EFI_RESOURCE_SYSTEM_MEMORY:
130  hob_res_type_str = "EFI_RESOURCE_SYSTEM_MEMORY";
131  break;
132  case EFI_RESOURCE_MEMORY_MAPPED_IO:
133  hob_res_type_str = "EFI_RESOURCE_MEMORY_MAPPED_IO";
134  break;
135  case EFI_RESOURCE_IO:
136  hob_res_type_str = "EFI_RESOURCE_IO";
137  break;
138  case EFI_RESOURCE_FIRMWARE_DEVICE:
139  hob_res_type_str = "EFI_RESOURCE_FIRMWARE_DEVICE";
140  break;
141  case EFI_RESOURCE_MEMORY_MAPPED_IO_PORT:
142  hob_res_type_str = "EFI_RESOURCE_MEMORY_MAPPED_IO_PORT";
143  break;
144  case EFI_RESOURCE_MEMORY_RESERVED:
145  hob_res_type_str = "EFI_RESOURCE_MEMORY_RESERVED";
146  break;
147  case EFI_RESOURCE_IO_RESERVED:
148  hob_res_type_str = "EFI_RESOURCE_IO_RESERVED";
149  break;
150  case EFI_RESOURCE_MAX_MEMORY_TYPE:
151  hob_res_type_str = "EFI_RESOURCE_MAX_MEMORY_TYPE";
152  break;
153  default:
154  hob_res_type_str = "EFI_RESOURCE_UNKNOWN";
155  break;
156  }
157 
158  printk(BIOS_SPEW, " Resource %s (0x%0x) has attributes 0x%0x\n",
159  hob_res_type_str, hob_res_type, hob_res_attr);
160  printk(BIOS_SPEW, " at location 0x%0lx with length 0x%0lx\n",
161  (unsigned long)hob_res_addr,
162  (unsigned long)hob_res_length);
163 }
164 
165 static const char *get_hob_type_string(void *hob_ptr)
166 {
167  EFI_PEI_HOB_POINTERS hob;
168  const char *hob_type_string;
169  const EFI_GUID fsp_reserved_guid =
170  FSP_RESERVED_MEMORY_RESOURCE_HOB_GUID;
171  const EFI_GUID mrc_guid = FSP_NON_VOLATILE_STORAGE_HOB_GUID;
172  const EFI_GUID bootldr_tmp_mem_guid =
173  FSP_BOOTLOADER_TEMP_MEMORY_HOB_GUID;
174  const EFI_GUID bootldr_tolum_guid = FSP_BOOTLOADER_TOLUM_HOB_GUID;
175  const EFI_GUID graphics_info_guid = EFI_PEI_GRAPHICS_INFO_HOB_GUID;
176  const EFI_GUID memory_info_hob_guid = FSP_SMBIOS_MEMORY_INFO_GUID;
177 
178  hob.Header = (EFI_HOB_GENERIC_HEADER *)hob_ptr;
179  switch (hob.Header->HobType) {
181  hob_type_string = "EFI_HOB_TYPE_HANDOFF";
182  break;
183  case EFI_HOB_TYPE_MEMORY_ALLOCATION:
184  hob_type_string = "EFI_HOB_TYPE_MEMORY_ALLOCATION";
185  break;
186  case EFI_HOB_TYPE_RESOURCE_DESCRIPTOR:
187  if (compare_guid(&fsp_reserved_guid, &hob.Guid->Name))
188  hob_type_string = "FSP_RESERVED_MEMORY_RESOURCE_HOB";
189  else if (compare_guid(&bootldr_tolum_guid, &hob.Guid->Name))
190  hob_type_string = "FSP_BOOTLOADER_TOLUM_HOB_GUID";
191  else
192  hob_type_string = "EFI_HOB_TYPE_RESOURCE_DESCRIPTOR";
193  break;
194  case EFI_HOB_TYPE_GUID_EXTENSION:
195  if (compare_guid(&bootldr_tmp_mem_guid, &hob.Guid->Name))
196  hob_type_string = "FSP_BOOTLOADER_TEMP_MEMORY_HOB";
197  else if (compare_guid(&mrc_guid, &hob.Guid->Name))
198  hob_type_string = "FSP_NON_VOLATILE_STORAGE_HOB";
199  else if (compare_guid(&graphics_info_guid, &hob.Guid->Name))
200  hob_type_string = "EFI_PEI_GRAPHICS_INFO_HOB_GUID";
201  else if (compare_guid(&memory_info_hob_guid, &hob.Guid->Name))
202  hob_type_string = "FSP_SMBIOS_MEMORY_INFO_GUID";
203  else
204  hob_type_string = "EFI_HOB_TYPE_GUID_EXTENSION";
205  break;
207  hob_type_string = "EFI_HOB_TYPE_MEMORY_POOL";
208  break;
209  case EFI_HOB_TYPE_UNUSED:
210  hob_type_string = "EFI_HOB_TYPE_UNUSED";
211  break;
212  case EFI_HOB_TYPE_END_OF_HOB_LIST:
213  hob_type_string = "EFI_HOB_TYPE_END_OF_HOB_LIST";
214  break;
215  default:
216  hob_type_string = "EFI_HOB_TYPE_UNRECOGNIZED";
217  break;
218  }
219 
220  return hob_type_string;
221 }
222 
223 /*
224  * Print out a structure of all the HOBs
225  * that match a certain type:
226  * Print all types (0x0000)
227  * EFI_HOB_TYPE_HANDOFF (0x0001)
228  * EFI_HOB_TYPE_MEMORY_ALLOCATION (0x0002)
229  * EFI_HOB_TYPE_RESOURCE_DESCRIPTOR (0x0003)
230  * EFI_HOB_TYPE_GUID_EXTENSION (0x0004)
231  * EFI_HOB_TYPE_MEMORY_POOL (0x0007)
232  * EFI_HOB_TYPE_UNUSED (0xFFFE)
233  * EFI_HOB_TYPE_END_OF_HOB_LIST (0xFFFF)
234  */
235 void print_hob_type_structure(u16 hob_type, void *hob_list_ptr)
236 {
237  void *current_hob;
238  u32 current_type;
239  const char *current_type_str;
240 
241  /*
242  * Print out HOBs of our desired type until
243  * the end of the HOB list
244  */
245  printk(BIOS_DEBUG, "\n=== FSP HOB Data Structure ===\n");
246  printk(BIOS_DEBUG, "%p: hob_list_ptr\n", hob_list_ptr);
247  for (current_hob = hob_list_ptr; !END_OF_HOB_LIST(current_hob);
248  current_hob = GET_NEXT_HOB(current_hob)) {
249 
250  EFI_HOB_GENERIC_HEADER *current_header_ptr =
251  (EFI_HOB_GENERIC_HEADER *)current_hob;
252 
253  /* Get the type of this HOB */
254  current_type = current_header_ptr->HobType;
255  current_type_str = get_hob_type_string(current_hob);
256 
257  if (current_type == hob_type || hob_type == 0x0000) {
258  printk(BIOS_DEBUG, "HOB %p is an %s (type 0x%0x)\n",
259  current_hob, current_type_str,
260  current_type);
261  switch (current_type) {
262  case EFI_HOB_TYPE_MEMORY_ALLOCATION:
263  print_hob_mem_attributes(current_hob);
264  break;
265  case EFI_HOB_TYPE_RESOURCE_DESCRIPTOR:
266  print_hob_resource_attributes(current_hob);
267  break;
268  }
269  }
270  }
271  printk(BIOS_DEBUG, "=== End of FSP HOB Data Structure ===\n\n");
272 }
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define printk(level,...)
Definition: stdlib.h:16
void __noreturn die(const char *fmt,...)
Definition: die.c:17
#define EFI_HOB_TYPE_HANDOFF
Definition: util.h:57
#define EFI_HOB_TYPE_MEMORY_POOL
Definition: util.h:58
hob_type
Definition: util.h:90
void * fsp_get_hob_list(void)
Definition: fsp_util.c:202
void * get_hob_list(void)
Definition: hob.c:16
void print_hob_type_structure(u16 hob_type, void *hob_list_ptr)
Definition: hob.c:235
void * get_resource_hob(const EFI_GUID *guid, const void *hob_start)
Definition: hob.c:65
static const char * get_hob_type_string(void *hob_ptr)
Definition: hob.c:165
static void print_hob_mem_attributes(void *hob_ptr)
Definition: hob.c:79
void * get_guid_hob(const EFI_GUID *guid, const void *hob_start)
Definition: hob.c:48
static void * get_next_hob(uint16_t type, const void *hob_start)
Definition: hob.c:27
static void print_hob_resource_attributes(void *hob_ptr)
Definition: hob.c:117
static bool compare_guid(const EFI_GUID *guid1, const EFI_GUID *guid2)
Definition: hob.c:10
unsigned int type
Definition: edid.c:57
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_SPEW
BIOS_SPEW - Excessively verbose output.
Definition: loglevel.h:142
#define FSP_SMBIOS_MEMORY_INFO_GUID
Definition: romstage.c:23
#define NULL
Definition: stddef.h:19
unsigned short uint16_t
Definition: stdint.h:11
uint64_t u64
Definition: stdint.h:54
uint32_t u32
Definition: stdint.h:51
uint16_t u16
Definition: stdint.h:48
unsigned char uint8_t
Definition: stdint.h:8
int memcmp(const void *s1, const void *s2, size_t n)
Definition: memcmp.c:3