Skip to contentSkip to conversation

efi: Add EFI Debug Support Table feature

PATCH Superseded Ying-Chun Liu (PaulLiu) 4 participants 8 messages Apr 23, 2025 – Apr 28, 2025

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

cover efi: Add EFI Debug Support Table feature

From: "Ying-Chun Liu (PaulLiu)" <[email protected]>

I implemented the UEFI Specification version 2.10, specifically focusing on the functionality described in Section 18.4, which details the EFI Debug Support Table feature. This implementation ensures support for hardware-assisted debugging and provides a standardized mechanism for debuggers to discover and interact with system-level debug resources.

Ying-Chun Liu (PaulLiu) (3):
efi: add EFI_SYSTEM_TABLE_POINTER for debug
efi: add EFI_DEBUG_IMAGE_INFO_TABLE for debug
efi: add EFI_DEBUG_IMAGE_INFO for debug

 include/efi_api.h             |  32 ++++++
 include/efi_loader.h          |   7 ++
 lib/efi_loader/efi_boottime.c | 196 ++++++++++++++++++++++++++++++++++
 lib/efi_loader/efi_setup.c    |   7 ++
 4 files changed, 242 insertions(+)
Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/3 efi: add EFI_SYSTEM_TABLE_POINTER for debug

From: "Ying-Chun Liu (PaulLiu)" <[email protected]>

Add EFI_SYSTEM_TABLE_POINTER structure for debugger to locate the address of EFI_SYSTEM_TABLE.

This feature is described in UEFI SPEC version 2.10. Section 18.4.2.

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
---
 include/efi_api.h             |  6 +++++
 include/efi_loader.h          |  2 ++
 lib/efi_loader/efi_boottime.c | 44 +++++++++++++++++++++++++++++++++++
 lib/efi_loader/efi_setup.c    |  7 ++++++
 4 files changed, 59 insertions(+)
include/efi_api.h +6 -0
index eb61eafa028..5017d9037cd 100644
@@ -259,6 +259,12 @@ struct efi_capsule_result_variable_header {
 	efi_status_t capsule_status;
 } __packed;
 
+struct efi_system_table_pointer {
+	u64 signature;
+	efi_physical_addr_t efi_system_table_base;
+	u32 crc32;
+};
+
 struct efi_memory_range {
 	efi_physical_addr_t	address;
 	u64			length;
include/efi_loader.h +2 -0
index 1d75d97ebbc..370bc042f70 100644
@@ -615,6 +615,8 @@ efi_status_t efi_tcg2_measure_dtb(void *dtb);
 efi_status_t efi_root_node_register(void);
 /* Called by bootefi to initialize runtime */
 efi_status_t efi_initialize_system_table(void);
+/* Called by bootefi to initialize debug */
+efi_status_t efi_initialize_system_table_pointer(void);
 /* efi_runtime_detach() - detach unimplemented runtime functions */
 void efi_runtime_detach(void);
 /* efi_convert_pointer() - convert pointer to virtual address */
lib/efi_loader/efi_boottime.c +44 -0

53 lines not on this page · raw message

lib/efi_loader/efi_setup.c +7 -0
index aa59bc7779d..073a08b1127 100644
@@ -259,6 +259,13 @@ efi_status_t efi_init_obj_list(void)
 	if (ret != EFI_SUCCESS)
 		goto out;
 
+	/* Initialize system table */
+	ret = efi_initialize_system_table_pointer();
+	if (ret != EFI_SUCCESS) {
+		printf("EFI init system table pointer fail\n");
+		goto out;
+	}
+
 	if (IS_ENABLED(CONFIG_EFI_ECPT)) {
 		ret = efi_ecpt_register();
 		if (ret != EFI_SUCCESS)
Signature · 1 line
2.39.5
#2 lore raw

PATCH 2/3 efi: add EFI_DEBUG_IMAGE_INFO_TABLE for debug

In reply to Ying-Chun Liu (PaulLiu)

From: "Ying-Chun Liu (PaulLiu)" <[email protected]>

EFI_DEBUG_IMAGE_INFO_TABLE is used to store EFI_LOADED_IMAGE for debug purpose. This commit adds the table to the EFI_CONFIGURATION_TABLE.

This feature is described in UEFI Spec version 2.10. Section 18.4.

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
---
 include/efi_api.h             | 24 ++++++++++++++++++++++++
 lib/efi_loader/efi_boottime.c | 16 ++++++++++++++++
 2 files changed, 40 insertions(+)
include/efi_api.h +24 -0
index 5017d9037cd..0f4245091f1 100644
@@ -238,6 +238,10 @@ enum efi_reset_type {
 	EFI_GUID(0xcce33c35, 0x74ac, 0x4087, 0xbc, 0xe7, \
 		 0x8b, 0x29, 0xb0, 0x2e, 0xeb, 0x27)
 
+#define EFI_DEBUG_IMAGE_INFO_TABLE_GUID \
+	EFI_GUID(0x49152e77, 0x1ada, 0x4764, 0xb7, 0xa2, \
+		 0x7a, 0xfe, 0xfe, 0xd9, 0x5e, 0x8b)
+
 struct efi_conformance_profiles_table {
 	u16 version;
 	u16 number_of_profiles;
@@ -564,6 +568,26 @@ struct efi_loaded_image {
 	efi_status_t (EFIAPI *unload)(efi_handle_t image_handle);
 };
 
+#define EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS 0x01
+#define EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED     0x02
+
+struct efi_debug_image_info_normal {
+	u32 image_info_type;
+	struct efi_loaded_image *loaded_image_protocol_instance;
+	efi_handle_t image_handle;
+};
+
+union efi_debug_image_info {
+	u32 *image_info_type;
+	struct efi_debug_image_info_normal *normal_image;
+};
+
+struct efi_debug_image_info_table_header {
+	volatile u32 update_status;
+	u32 table_size;
+	union efi_debug_image_info *efi_debug_image_info_table;
+};
+
 #define EFI_DEVICE_PATH_PROTOCOL_GUID \
 	EFI_GUID(0x09576e91, 0x6d3f, 0x11d2, \
 		 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)
lib/efi_loader/efi_boottime.c +16 -0
index 366bc0d2b1d..e2665459e44 100644
@@ -4038,6 +4038,16 @@ efi_status_t efi_initialize_system_table(void)
 	return ret;
 }
 
+struct efi_debug_image_info_table_header __efi_runtime_data
+efi_m_debug_info_table_header = {
+	0,
+	0,
+	NULL
+};
+
+const efi_guid_t efi_debug_image_info_table_guid =
+	EFI_DEBUG_IMAGE_INFO_TABLE_GUID;
+
 /**
  * efi_initialize_system_table() - Initialize system table
  *
@@ -4053,6 +4063,8 @@ efi_status_t efi_initialize_system_table_pointer(void)
 	u64 addr;
 	u64 aligned_addr;
 	u32 crc32_value;
+	efi_guid_t debug_image_info_table_guid =
+		efi_debug_image_info_table_guid;
 
 	/* Allocate configuration table array */
 	ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
@@ -4077,5 +4089,9 @@ efi_status_t efi_initialize_system_table_pointer(void)
 			    (const unsigned char *)systab_pointer,
 			    sizeof(struct efi_system_table_pointer));
 	systab_pointer->crc32 = crc32_value;
+
+	ret = efi_install_configuration_table(&debug_image_info_table_guid,
+					      &efi_m_debug_info_table_header);
+
 	return ret;
 }
Signature · 1 line
2.39.5
#3 lore raw

PATCH 3/3 efi: add EFI_DEBUG_IMAGE_INFO for debug

In reply to Ying-Chun Liu (PaulLiu)

From: "Ying-Chun Liu (PaulLiu)" <[email protected]>

This commit adds the functionality of generate EFI_DEBUG_IMAGE_INFO while loading the image.

This feature is described in UEFI Spec 2.10. Section 18.4.3.

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
---
 include/efi_api.h             |   2 +
 include/efi_loader.h          |   5 ++
 lib/efi_loader/efi_boottime.c | 136 ++++++++++++++++++++++++++++++++++
 3 files changed, 143 insertions(+)
include/efi_api.h +2 -0
index 0f4245091f1..ba87af17c1f 100644
@@ -571,6 +571,8 @@ struct efi_loaded_image {
 #define EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS 0x01
 #define EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED     0x02
 
+#define EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL  0x01
+
 struct efi_debug_image_info_normal {
 	u32 image_info_type;
 	struct efi_loaded_image *loaded_image_protocol_instance;
include/efi_loader.h +5 -0
index 370bc042f70..babe23a8a83 100644
@@ -617,6 +617,11 @@ efi_status_t efi_root_node_register(void);
 efi_status_t efi_initialize_system_table(void);
 /* Called by bootefi to initialize debug */
 efi_status_t efi_initialize_system_table_pointer(void);
+/* Called by efi_load_image for register debug info */
+void efi_core_new_debug_image_info_entry(u32 image_info_type,
+					 struct efi_loaded_image *loaded_image,
+					 efi_handle_t image_handle);
+void efi_core_remove_debug_image_info_entry(efi_handle_t image_handle);
 /* efi_runtime_detach() - detach unimplemented runtime functions */
 void efi_runtime_detach(void);
 /* efi_convert_pointer() - convert pointer to virtual address */
lib/efi_loader/efi_boottime.c +136 -0

157 lines not on this page · raw message

Signature · 1 line
2.39.5
#4 lore raw

Re: [PATCH 3/3] efi: add EFI_DEBUG_IMAGE_INFO for debug

Quoted 185 lines · Ying-Chun Liu (PaulLiu)

From: "Ying-Chun Liu (PaulLiu)" <[email protected]>

This commit adds the functionality of generate EFI_DEBUG_IMAGE_INFO while loading the image.

This feature is described in UEFI Spec 2.10. Section 18.4.3.

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
---
  include/efi_api.h             |   2 +
  include/efi_loader.h          |   5 ++
  lib/efi_loader/efi_boottime.c | 136 ++++++++++++++++++++++++++++++++++
  3 files changed, 143 insertions(+)
include/efi_api.h +2 -0
index 0f4245091f1..ba87af17c1f 100644
@@ -571,6 +571,8 @@ struct efi_loaded_image {
  #define EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS 0x01
  #define EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED     0x02
 
+#define EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL  0x01
+
  struct efi_debug_image_info_normal {
  	u32 image_info_type;
  	struct efi_loaded_image *loaded_image_protocol_instance;
include/efi_loader.h +5 -0
index 370bc042f70..babe23a8a83 100644
@@ -617,6 +617,11 @@ efi_status_t efi_root_node_register(void);
  efi_status_t efi_initialize_system_table(void);
  /* Called by bootefi to initialize debug */
  efi_status_t efi_initialize_system_table_pointer(void);
+/* Called by efi_load_image for register debug info */
+void efi_core_new_debug_image_info_entry(u32 image_info_type,
+					 struct efi_loaded_image *loaded_image,
+					 efi_handle_t image_handle);
+void efi_core_remove_debug_image_info_entry(efi_handle_t image_handle);
  /* efi_runtime_detach() - detach unimplemented runtime functions */
  void efi_runtime_detach(void);
  /* efi_convert_pointer() - convert pointer to virtual address */
lib/efi_loader/efi_boottime.c +111 -0
index e2665459e44..bf37580c06b 100644
@@ -2130,6 +2130,12 @@ efi_status_t EFIAPI efi_load_image(bool boot_policy,
  		*image_handle = NULL;
  		free(info);
  	}
+
+	if (*image_handle) {
+		efi_core_new_debug_image_info_entry(EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL,
+						    info,
+						    *image_handle);
+	}
  error:
  	return EFI_EXIT(ret);
  }
@@ -3359,6 +3365,7 @@ efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
  		ret = EFI_INVALID_PARAMETER;
  		goto out;
  	}
+	efi_core_remove_debug_image_info_entry(image_handle);
  	switch (efiobj->type) {
  	case EFI_OBJECT_TYPE_STARTED_IMAGE:
  		/* Call the unload function */
@@ -4045,6 +4052,10 @@ efi_m_debug_info_table_header = {
  	NULL
  };
 
+static u32 efi_m_max_table_entries;
+
+#define EFI_DEBUG_TABLE_ENTRY_SIZE  (sizeof(void *))
+
  const efi_guid_t efi_debug_image_info_table_guid =
  	EFI_DEBUG_IMAGE_INFO_TABLE_GUID;
 
@@ -4095,3 +4106,128 @@ efi_status_t efi_initialize_system_table_pointer(void)
 
  	return ret;
  }
+
+/**
+ * Add a new efi_loaded_image structure to the efi_debug_image_info Table.
+ * Re-Allocates the table if it's not large enough to accomidate another
+ * entry.
+ *
+ * @param image_info_type  type of debug image information
+ * @param loaded_image     pointer to the loaded image protocol for the image
+ *                         being loaded
+ * @param image_handle     image handle for the image being loaded
+ **/
+void efi_core_new_debug_image_info_entry(u32 image_info_type,
+					 struct efi_loaded_image *loaded_image,
+					 efi_handle_t image_handle)
+{
+	union efi_debug_image_info *table;
+	union efi_debug_image_info *new_table;
+	u32 index;
+	u32 table_size;
+
+	/* Set the flag indicating that we're in the process of updating
+	 * the table.
+	 */
+	efi_m_debug_info_table_header.update_status |=
+		EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+
+	table = efi_m_debug_info_table_header.efi_debug_image_info_table;
+
+	if (efi_m_debug_info_table_header.table_size < efi_m_max_table_entries) {
+		/* We still have empty entries in the Table, find the first
+		 * empty entry.
+		 */
+		index = 0;
+		while (table[index].normal_image)
+			index++;
+	} else {
+		/* table is full, re-allocate another page for a larger
+		 * table.
+		 */
+		table_size = efi_m_max_table_entries * EFI_DEBUG_TABLE_ENTRY_SIZE;
+		efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
+				  table_size + EFI_PAGE_SIZE,
+				  (void **)&new_table);
+
+		if (!new_table) {
+			efi_m_debug_info_table_header.update_status &=
+				~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+			return;
+		}
+
+		memset(new_table, 0, table_size + EFI_PAGE_SIZE);
+
+		/* Copy the old table into the new one. */
+		memcpy(new_table, table, table_size);
+		/* Free the old table. */
+		efi_free_pool(table);
+		/* Update the table header. */
+		table = new_table;
+		efi_m_debug_info_table_header.efi_debug_image_info_table =
+			new_table;
+
+		/* Enlarge the max table entries and set the first empty
+		 * entry index to be the original max table entries.
+		 */
+		index = efi_m_max_table_entries;
+		efi_m_max_table_entries +=
+			EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;
+	}
+
+	/* Allocate data for new entry. */
+	efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
+			  sizeof(struct efi_debug_image_info_normal),
+			  (void **)(&table[index].normal_image));
+	if (table[index].normal_image) {
+		/* Update the entry. */
+		table[index].normal_image->image_info_type = image_info_type;
+		table[index].normal_image->loaded_image_protocol_instance =
+			loaded_image;
+		table[index].normal_image->image_handle = image_handle;
+
+		/* Increase the number of EFI_DEBUG_IMAGE_INFO elements and
+		 * set the efi_m_debug_info_table_header in modified status.
+		 */
+		efi_m_debug_info_table_header.table_size++;
+		efi_m_debug_info_table_header.update_status |=
+			EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
+	}
+
+	efi_m_debug_info_table_header.update_status &=
+		~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+}
+
+void efi_core_remove_debug_image_info_entry(efi_handle_t image_handle)
+{
+	union efi_debug_image_info *table;
+	u32 index;
+
+	efi_m_debug_info_table_header.update_status |=
+		EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void) · line 4218

Quoted 10 lines
lib/efi_loader/efi_boottime.c +10 -0
+	table = efi_m_debug_info_table_header.efi_debug_image_info_table;
+
+	for (index = 0; index < efi_m_max_table_entries; index++) {
+		if (table[index].normal_image &&
+		    table[index].normal_image->image_handle == image_handle) {
+			/* Found a match. Free up the record, then NULL the
+			 * pointer to indicate the slot is free.
+			 */
+			efi_free_pool(table[index].normal_image);
+			table[index].normal_image = NULL;

It seems that you just copied the logic from EDK II instead of programming according to the UEFI specification.

I cannot see that the UEFI specification allows NULL pointers inside the array.

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void) · line 4224

Quoted 6 lines
lib/efi_loader/efi_boottime.c +6 -0
+
+			/* Decrease the number of EFI_DEBUG_IMAGE_INFO
+			 * elements and set the efi_m_debug_info_table_header
+			 * in modified status.
+			 */
+			efi_m_debug_info_table_header.table_size--;

According to the UEFI specification the table_size must match the array size and not the number of non-NULL elements. If there is a NULL pointer in the middle of the table and you decrease the table_size value, the last non-NULL element is not accessible anymore.

A correct implementation would move all elements after the deleted one one slot to the front.

Best regards

Heinrich

Quoted 9 lines
lib/efi_loader/efi_boottime.c +9 -0
+			efi_m_debug_info_table_header.update_status |=
+				EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
+			break;
+		}
+	}
+
+	efi_m_debug_info_table_header.update_status &=
+		~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+}
#5 lore raw

Re: [PATCH 3/3] efi: add EFI_DEBUG_IMAGE_INFO for debug

Hi Heinrich,

Yes. I just copied the code from EDK2. Seems this is incorrect. I'll try to ask some help from EDK2 people.
Actually I'm working on Android (AOSP)'s GBL project. It is an EFI application, used as a bootloader for loading and booting Android images. So some people are asking how to debug that EFI application on real hardwares. And since GBL runs on both U-boot and EDK2, the debug method should
follow the standard so that the downstream people can have a unified way to debug the application.

I'll ask EDK2 people for help and then continue working on this patch.

Thanks a lot,
Paul

Quoted 248 lines · Heinrich Schuchardt
Quoted 34 lines · Ying-Chun Liu (PaulLiu)

From: "Ying-Chun Liu (PaulLiu)" <[email protected]>

This commit adds the functionality of generate EFI_DEBUG_IMAGE_INFO while loading the image.

This feature is described in UEFI Spec 2.10. Section 18.4.3.

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
---
  include/efi_api.h             |   2 +
  include/efi_loader.h          |   5 ++
  lib/efi_loader/efi_boottime.c | 136 ++++++++++++++++++++++++++++++++++
  3 files changed, 143 insertions(+)
include/efi_api.h +2 -0
index 0f4245091f1..ba87af17c1f 100644
@@ -571,6 +571,8 @@ struct efi_loaded_image {
  #define EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS 0x01
  #define EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED     0x02
 
+#define EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL  0x01
+
  struct efi_debug_image_info_normal {
      u32 image_info_type;
      struct efi_loaded_image *loaded_image_protocol_instance;
include/efi_loader.h +0 -0
index 370bc042f70..babe23a8a83 100644

include/efi_loader.h efi_status_t efi_root_node_register(void); · line 622

Quoted 6 lines
include/efi_loader.h +3 -0
@@ -617,6 +617,11 @@ efi_status_t efi_root_node_register(void);
  efi_status_t efi_initialize_system_table(void);
  /* Called by bootefi to initialize debug */
  efi_status_t efi_initialize_system_table_pointer(void);
+/* Called by efi_load_image for register debug info */
+void efi_core_new_debug_image_info_entry(u32 image_info_type,
+                                      struct efi_loaded_image

*loaded_image,

Review

Quoted 6 lines
include/efi_loader.h +2 -0
+                                      efi_handle_t image_handle);
+void efi_core_remove_debug_image_info_entry(efi_handle_t image_handle);
  /* efi_runtime_detach() - detach unimplemented runtime functions */
  void efi_runtime_detach(void);
  /* efi_convert_pointer() - convert pointer to virtual address */
(quoted diff) +0 -0

b/lib/efi_loader/efi_boottime.c

lib/efi_loader/efi_boottime.c efi_status_t EFIAPI efi_load_image(bool

Quoted 4 lines

index e2665459e44..bf37580c06b 100644

lib/efi_loader/efi_boottime.c +0 -0
@@ -2130,6 +2130,12 @@ efi_status_t EFIAPI efi_load_image(bool

boot_policy,

lib/efi_loader/efi_boottime.c efi_status_t EFIAPI efi_load_image(bool boot_policy,

Quoted 6 lines
lib/efi_loader/efi_boottime.c +3 -0
              *image_handle = NULL;
              free(info);
      }
+
+     if (*image_handle) {
+

efi_core_new_debug_image_info_entry(EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL,

lib/efi_loader/efi_boottime.c efi_status_t EFIAPI efi_unload_image(efi_handle_t

Quoted 7 lines
lib/efi_loader/efi_boottime.c +3 -0
+                                                 info,
+                                                 *image_handle);
+     }
  error:
      return EFI_EXIT(ret);
  }
@@ -3359,6 +3365,7 @@ efi_status_t EFIAPI efi_unload_image(efi_handle_t

image_handle)

lib/efi_loader/efi_boottime.c efi_status_t

Quoted 19 lines
lib/efi_loader/efi_boottime.c +5 -0
              ret = EFI_INVALID_PARAMETER;
              goto out;
      }
+     efi_core_remove_debug_image_info_entry(image_handle);
      switch (efiobj->type) {
      case EFI_OBJECT_TYPE_STARTED_IMAGE:
              /* Call the unload function */
@@ -4045,6 +4052,10 @@ efi_m_debug_info_table_header = {
      NULL
  };
 
+static u32 efi_m_max_table_entries;
+
+#define EFI_DEBUG_TABLE_ENTRY_SIZE  (sizeof(void *))
+
  const efi_guid_t efi_debug_image_info_table_guid =
      EFI_DEBUG_IMAGE_INFO_TABLE_GUID;
 
@@ -4095,3 +4106,128 @@ efi_status_t

efi_initialize_system_table_pointer(void)

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void)

Quoted 6 lines
lib/efi_loader/efi_boottime.c +3 -0
      return ret;
  }
+
+/**
+ * Add a new efi_loaded_image structure to the efi_debug_image_info

Table.

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void)

Quoted 5 lines
lib/efi_loader/efi_boottime.c +5 -0
+ * Re-Allocates the table if it's not large enough to accomidate another
+ * entry.
+ *
+ * @param image_info_type  type of debug image information
+ * @param loaded_image     pointer to the loaded image protocol for the

image

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void)

Quoted 5 lines
lib/efi_loader/efi_boottime.c +5 -0
+ *                         being loaded
+ * @param image_handle     image handle for the image being loaded
+ **/
+void efi_core_new_debug_image_info_entry(u32 image_info_type,
+                                      struct efi_loaded_image

*loaded_image,

Quoted 6 lines
lib/efi_loader/efi_boottime.c +6 -0
+                                      efi_handle_t image_handle)
+{
+     union efi_debug_image_info *table;
+     union efi_debug_image_info *new_table;
+     u32 index;
+     u32 table_size;

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void)

Quoted 10 lines
lib/efi_loader/efi_boottime.c +10 -0
+
+     /* Set the flag indicating that we're in the process of updating
+      * the table.
+      */
+     efi_m_debug_info_table_header.update_status |=
+             EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+
+     table = efi_m_debug_info_table_header.efi_debug_image_info_table;
+
+     if (efi_m_debug_info_table_header.table_size <

efi_m_max_table_entries) {

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void)

Quoted 11 lines
lib/efi_loader/efi_boottime.c +11 -0
+             /* We still have empty entries in the Table, find the first
+              * empty entry.
+              */
+             index = 0;
+             while (table[index].normal_image)
+                     index++;
+     } else {
+             /* table is full, re-allocate another page for a larger
+              * table.
+              */
+             table_size = efi_m_max_table_entries *

EFI_DEBUG_TABLE_ENTRY_SIZE;

Quoted 26 lines
lib/efi_loader/efi_boottime.c +26 -0
+             efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
+                               table_size + EFI_PAGE_SIZE,
+                               (void **)&new_table);
+
+             if (!new_table) {
+                     efi_m_debug_info_table_header.update_status &=
+                             ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+                     return;
+             }
+
+             memset(new_table, 0, table_size + EFI_PAGE_SIZE);
+
+             /* Copy the old table into the new one. */
+             memcpy(new_table, table, table_size);
+             /* Free the old table. */
+             efi_free_pool(table);
+             /* Update the table header. */
+             table = new_table;
+             efi_m_debug_info_table_header.efi_debug_image_info_table =
+                     new_table;
+
+             /* Enlarge the max table entries and set the first empty
+              * entry index to be the original max table entries.
+              */
+             index = efi_m_max_table_entries;
+             efi_m_max_table_entries +=

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void)

Quoted 10 lines
lib/efi_loader/efi_boottime.c +10 -0
+                     EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;
+     }
+
+     /* Allocate data for new entry. */
+     efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
+                       sizeof(struct efi_debug_image_info_normal),
+                       (void **)(&table[index].normal_image));
+     if (table[index].normal_image) {
+             /* Update the entry. */
+             table[index].normal_image->image_info_type =

image_info_type;

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void)

Quoted 6 lines
lib/efi_loader/efi_boottime.c +6 -0
+             table[index].normal_image->loaded_image_protocol_instance =
+                     loaded_image;
+             table[index].normal_image->image_handle = image_handle;
+
+             /* Increase the number of EFI_DEBUG_IMAGE_INFO elements and
+              * set the efi_m_debug_info_table_header in modified

status.

Quoted 13 lines
lib/efi_loader/efi_boottime.c +13 -0
+              */
+             efi_m_debug_info_table_header.table_size++;
+             efi_m_debug_info_table_header.update_status |=
+                     EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
+     }
+
+     efi_m_debug_info_table_header.update_status &=
+             ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+}
+
+void efi_core_remove_debug_image_info_entry(efi_handle_t image_handle)
+{
+     union efi_debug_image_info *table;

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void)

Quoted 10 lines
lib/efi_loader/efi_boottime.c +10 -0
+     u32 index;
+
+     efi_m_debug_info_table_header.update_status |=
+             EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+
+     table = efi_m_debug_info_table_header.efi_debug_image_info_table;
+
+     for (index = 0; index < efi_m_max_table_entries; index++) {
+             if (table[index].normal_image &&
+                 table[index].normal_image->image_handle ==

image_handle) {

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void) · line 4218

Quoted 5 lines
lib/efi_loader/efi_boottime.c +5 -0
+                     /* Found a match. Free up the record, then NULL the
+                      * pointer to indicate the slot is free.
+                      */
+                     efi_free_pool(table[index].normal_image);
+                     table[index].normal_image = NULL;

It seems that you just copied the logic from EDK II instead of programming according to the UEFI specification.

I cannot see that the UEFI specification allows NULL pointers inside the array.

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void)

Quoted 3 lines
lib/efi_loader/efi_boottime.c +3 -0
+
+                     /* Decrease the number of EFI_DEBUG_IMAGE_INFO
+                      * elements and set the

efi_m_debug_info_table_header

lib/efi_loader/efi_boottime.c efi_status_t efi_initialize_system_table_pointer(void) · line 4224

Quoted 3 lines
lib/efi_loader/efi_boottime.c +3 -0
+                      * in modified status.
+                      */
+                     efi_m_debug_info_table_header.table_size--;

According to the UEFI specification the table_size must match the array size and not the number of non-NULL elements. If there is a NULL pointer in the middle of the table and you decrease the table_size value, the last non-NULL element is not accessible anymore.

A correct implementation would move all elements after the deleted one one slot to the front.

Best regards

Heinrich

Quoted 9 lines
lib/efi_loader/efi_boottime.c +9 -0
+                     efi_m_debug_info_table_header.update_status |=
+                             EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
+                     break;
+             }
+     }
+
+     efi_m_debug_info_table_header.update_status &=
+             ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+}
#6 lore raw

Re: [PATCH 3/3] efi: add EFI_DEBUG_IMAGE_INFO for debug

Quoted 8 lines · Paul Liu

Hi Heinrich,

Yes. I just copied the code from EDK2. Seems this is incorrect. I'll try to ask some help from EDK2 people.
Actually I'm working on Android (AOSP)'s GBL project. It is an EFI application, used as a bootloader for loading and booting Android images. So some people are asking how to debug that EFI application on real hardwares. And since GBL runs on both U-boot and EDK2, the debug method should follow the standard so that the downstream people can have a unified way to debug the application.

I'll ask EDK2 people for help and then continue working on this patch.

I have created https://github.com/tianocore/edk2/pull/11019

Best regards

Heinrich

#7 lore raw
In reply to Ying-Chun Liu (PaulLiu)

Hi Ying-Chun,

Quoted 5 lines

I implemented the UEFI Specification version 2.10, specifically focusing on the functionality described in Section 18.4, which details the EFI Debug Support Table feature. This implementation ensures support for hardware-assisted debugging and provides a standardized mechanism for debuggers to discover and interact with system-level debug resources.

I think this should be configurable, I suspect there would be usecases where it would be useful to be able to disable this functionality.

Peter

Quoted 14 lines

Ying-Chun Liu (PaulLiu) (3):
efi: add EFI_SYSTEM_TABLE_POINTER for debug
efi: add EFI_DEBUG_IMAGE_INFO_TABLE for debug
efi: add EFI_DEBUG_IMAGE_INFO for debug

 include/efi_api.h             |  32 ++++++
 include/efi_loader.h          |   7 ++
 lib/efi_loader/efi_boottime.c | 196 ++++++++++++++++++++++++++++++++++
 lib/efi_loader/efi_setup.c    |   7 ++
 4 files changed, 242 insertions(+)

--
2.39.5