Skip to contentSkip to conversation

Add EFI Debug Support Table feature

PATCH Superseded Ying-Chun Liu (PaulLiu) 3 participants 11 messages Jun 11, 2025 – Jun 16, 2025

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v5 cover 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.

V2: add Kconfig options to turn on/off this feature.
V3: Fix the way of removing debug_image_info.
V4: Fix efi_realloc return failure code and efi_alloc_aligned_pages() for
system_table_pointer.
V5: Fix function comments and move the code into a separate module.

Ying-Chun Liu (PaulLiu) (4):
efi: add EFI_SYSTEM_TABLE_POINTER for debug
efi: add EFI_DEBUG_IMAGE_INFO_TABLE for debug
lib: efi_loader: efi_memory.c: add efi_realloc() for realloc memory
efi: add EFI_DEBUG_IMAGE_INFO for debug

 include/efi_api.h                  |  71 +++++++++++
 include/efi_loader.h               |  19 +++
 lib/efi_loader/Kconfig             |   8 ++
 lib/efi_loader/Makefile            |   1 +
 lib/efi_loader/efi_boottime.c      |  11 ++
 lib/efi_loader/efi_debug_support.c | 181 +++++++++++++++++++++++++++++
 lib/efi_loader/efi_memory.c        |  56 +++++++++
 lib/efi_loader/efi_setup.c         |  20 ++++
 8 files changed, 367 insertions(+)
 create mode 100644 lib/efi_loader/efi_debug_support.c
Signature · 1 line
2.39.5
#1 lore raw

PATCH v5 1/4 efi: add EFI_SYSTEM_TABLE_POINTER for debug

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

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

This feature is described in UEFI SPEC version 2.10. Section 18.4.2. The implementation ensures support for hardware-assisted debugging and provides a standardized mechanism for debuggers to discover the EFI system table.

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Peter Robinson <[email protected]>
Cc Simon Glass <[email protected]>

---
V2: add Kconfig options to turn on/off this feature.
V3: make efi_initialize_system_table_pointer() do the job as described.
V4: use efi_alloc_aligned_pages() for system_table_pointer.
V5: move the code into a separate module.

---
 include/efi_api.h                  | 16 +++++++++++
 include/efi_loader.h               |  2 ++
 lib/efi_loader/Kconfig             |  8 ++++++
 lib/efi_loader/Makefile            |  1 +
 lib/efi_loader/efi_debug_support.c | 46 ++++++++++++++++++++++++++++++
 lib/efi_loader/efi_setup.c         |  8 ++++++
 6 files changed, 81 insertions(+)
 create mode 100644 lib/efi_loader/efi_debug_support.c
include/efi_api.h +16 -0
index eb61eafa028..6c4c1a0cc7b 100644
@@ -259,6 +259,22 @@ struct efi_capsule_result_variable_header {
 	efi_status_t capsule_status;
 } __packed;
 
+/**
+ * struct efi_system_table_pointer - struct to store the pointer of system
+ * table.
+ * @signature: The signature of this struct.
+ * @efi_system_table_base: The physical address of System Table.
+ * @crc32: CRC32 checksum
+ *
+ * This struct is design for hardware debugger to search through memory to
+ * get the address of EFI System Table.
+ */
+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 8f9f2bcf1cb..8f065244d8e 100644
@@ -646,6 +646,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/Kconfig +8 -0
index 7f02a83e2a2..f9d75cbc3fd 100644
@@ -71,6 +71,14 @@ config EFI_SECURE_BOOT
 config EFI_SIGNATURE_SUPPORT
 	bool
 
+config EFI_DEBUG_SUPPORT
+	bool "EFI Debug Support"
+	help
+	  Select this option if you want to setup the EFI Debug Support
+	  Table and the EFI_SYSTEM_TABLE_POINTER which is used by the debug
+	  agent or an external debugger to determine loaded image information
+	  in a quiescent manner.
+
 menu "UEFI services"
 
 config EFI_GET_TIME
lib/efi_loader/Makefile +1 -0
index cf050e5385d..51ccf1cd87e 100644
@@ -70,6 +70,7 @@ obj-$(CONFIG_EFI_RISCV_BOOT_PROTOCOL) += efi_riscv.o
 obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
 obj-$(CONFIG_EFI_SIGNATURE_SUPPORT) += efi_signature.o
 obj-$(CONFIG_EFI_ECPT) += efi_conformance.o
+obj-$(CONFIG_EFI_DEBUG_SUPPORT) += efi_debug_support.o
 
 EFI_VAR_SEED_FILE := $(subst $\",,$(CONFIG_EFI_VAR_SEED_FILE))
 $(obj)/efi_var_seed.o: $(srctree)/$(EFI_VAR_SEED_FILE)
lib/efi_loader/efi_debug_support.c new file +46 -0

46 lines not on this page · raw message

lib/efi_loader/efi_setup.c +8 -0
index 48f91da5df7..6193b9f6d0c 100644
@@ -278,6 +278,14 @@ efi_status_t efi_init_obj_list(void)
 	if (ret != EFI_SUCCESS)
 		goto out;
 
+	/* Initialize system table pointer */
+	if (IS_ENABLED(CONFIG_EFI_DEBUG_SUPPORT)) {
+		ret = efi_initialize_system_table_pointer();
+		if (ret != EFI_SUCCESS) {
+			goto out;
+		}
+	}
+
 	if (IS_ENABLED(CONFIG_EFI_ECPT)) {
 		ret = efi_ecpt_register();
 		if (ret != EFI_SUCCESS)
Signature · 1 line
2.39.5
Reviewed-by on v5 1/4
#2 lore raw

Re: [PATCH v5 1/4] efi: add EFI_SYSTEM_TABLE_POINTER for debug

Thanks Paul

Quoted 11 lines · Ying-Chun Liu (PaulLiu)

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

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

This feature is described in UEFI SPEC version 2.10. Section 18.4.2. The implementation ensures support for hardware-assisted debugging and provides a standardized mechanism for debuggers to discover the EFI system table.

Quoted 6 lines
Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Peter Robinson <[email protected]>
Cc Simon Glass <[email protected]>

---

A nit below

[...]

lib/efi_loader/efi_debug_support.c line 20

Quoted 2 lines
lib/efi_loader/efi_debug_support.c new file +2 -0
+{
+       const int size_4MB = 0x00400000;

We already have macros for these

Thanks
/Ilias

Quoted 48 lines
lib/efi_loader/efi_debug_support.c new file +26 -0
+       uintptr_t addr;
+       u32 crc32_value;
+
+       /* Allocate configuration table array */
+       addr = (uintptr_t)efi_alloc_aligned_pages(sizeof(struct efi_system_table_pointer),
+                                                 EFI_RUNTIME_SERVICES_DATA,
+                                                 size_4MB);
+
+       if (!addr) {
+               log_err("Installing EFI system table pointer failed\n");
+               return EFI_OUT_OF_RESOURCES;
+       }
+
+       systab_pointer = (struct efi_system_table_pointer *)addr;
+       memset(systab_pointer, 0, sizeof(struct efi_system_table_pointer));
+
+       systab_pointer->signature = EFI_SYSTEM_TABLE_SIGNATURE;
+       systab_pointer->efi_system_table_base = (efi_physical_addr_t)&systab;
+
+       crc32_value = crc32(0,
+                           (const unsigned char *)systab_pointer,
+                           sizeof(struct efi_system_table_pointer));
+       systab_pointer->crc32 = crc32_value;
+
+       return EFI_SUCCESS;
+}
lib/efi_loader/efi_setup.c +8 -1
index 48f91da5df7..6193b9f6d0c 100644
@@ -278,6 +278,14 @@ efi_status_t efi_init_obj_list(void)
        if (ret != EFI_SUCCESS)
                goto out;
 
+       /* Initialize system table pointer */
+       if (IS_ENABLED(CONFIG_EFI_DEBUG_SUPPORT)) {
+               ret = efi_initialize_system_table_pointer();
+               if (ret != EFI_SUCCESS) {
+                       goto out;
+               }
+       }
+
        if (IS_ENABLED(CONFIG_EFI_ECPT)) {
                ret = efi_ecpt_register();
                if (ret != EFI_SUCCESS)
--

2.39.5

#3 lore raw

PATCH v5 2/4 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. The implementation ensures support for hardware-assisted debugging and provides a standardized mechanism for debuggers to discover and interact with system-level debug resources.

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Peter Robinson <[email protected]>
Cc Simon Glass <[email protected]>

---
V3: Fix the way of installation of configuration table.
V5: move the code into a separate module.

---
 include/efi_api.h                  | 53 ++++++++++++++++++++++++++++++
 include/efi_loader.h               |  2 ++
 lib/efi_loader/efi_debug_support.c |  6 ++++
 lib/efi_loader/efi_setup.c         | 12 +++++++
 4 files changed, 73 insertions(+)
include/efi_api.h +53 -0
index 6c4c1a0cc7b..8da0a350ce3 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;
@@ -574,6 +578,55 @@ 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 - Store Debug Information for normal
+ * image.
+ * @image_info_type: the type of image info.
+ * @loaded_image_protocol_instance: the pointer to struct efi_loaded_image.
+ * @image_handle: the EFI handle of the image.
+ *
+ * This struct is created by efi_load_image() and store the information
+ * for debugging an normal image.
+ */
+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 - The union to store a pointer for EFI
+ * DEBUG IMAGE INFO.
+ * @image_info_type: the type of the image_info if it is not a normal image.
+ * @normal_image: The pointer to a normal image.
+ *
+ * This union is for a pointer that can point to the struct of normal_image.
+ * Or it points to an image_info_type.
+ */
+union efi_debug_image_info {
+	u32 *image_info_type;
+	struct efi_debug_image_info_normal *normal_image;
+};
+
+/**
+ * struct efi_debug_image_info_table_header - store the array of
+ * struct efi_debug_image_info.
+ * @update_status: Status to notify this struct is ready to use or not.
+ * @table_size: The number of elements of efi_debug_image_info_table.
+ * @efi_debug_image_info_table: The array of efi_debug_image_info.
+ *
+ * This struct stores the array of efi_debug_image_info. The
+ * number of elements is table_size.
+ */
+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)
include/efi_loader.h +2 -0
index 8f065244d8e..d0c72d0bc58 100644
@@ -315,6 +315,8 @@ extern const struct efi_hii_config_routing_protocol efi_hii_config_routing;
 extern const struct efi_hii_config_access_protocol efi_hii_config_access;
 extern const struct efi_hii_database_protocol efi_hii_database;
 extern const struct efi_hii_string_protocol efi_hii_string;
+/* structure for EFI_DEBUG_SUPPORT_PROTOCOL */
+extern struct efi_debug_image_info_table_header efi_m_debug_info_table_header;
 
 uint16_t *efi_dp_str(struct efi_device_path *dp);
 
lib/efi_loader/efi_debug_support.c +6 -0
index a0f7506779d..a6a672bc0e4 100644
@@ -10,6 +10,12 @@
 
 struct efi_system_table_pointer __efi_runtime_data * systab_pointer = NULL;
 
+struct efi_debug_image_info_table_header efi_m_debug_info_table_header = {
+	0,
+	0,
+	NULL
+};
+
 /**
  * efi_initialize_system_table_pointer() - Initialize system table pointer
  *
lib/efi_loader/efi_setup.c +12 -0
index 6193b9f6d0c..60f3a7e5fbf 100644
@@ -18,6 +18,9 @@
 
 efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
 
+const efi_guid_t efi_debug_image_info_table_guid =
+	EFI_DEBUG_IMAGE_INFO_TABLE_GUID;
+
 /*
  * Allow unaligned memory access.
  *
@@ -280,10 +283,19 @@ efi_status_t efi_init_obj_list(void)
 
 	/* Initialize system table pointer */
 	if (IS_ENABLED(CONFIG_EFI_DEBUG_SUPPORT)) {
+		efi_guid_t debug_image_info_table_guid =
+			efi_debug_image_info_table_guid;
+
 		ret = efi_initialize_system_table_pointer();
 		if (ret != EFI_SUCCESS) {
 			goto out;
 		}
+
+		ret = efi_install_configuration_table(&debug_image_info_table_guid,
+						      &efi_m_debug_info_table_header);
+		if (ret != EFI_SUCCESS) {
+			goto out;
+		}
 	}
 
 	if (IS_ENABLED(CONFIG_EFI_ECPT)) {
Signature · 1 line
2.39.5
Reviewed-by on v5 2/4
#4 lore raw

Re: [PATCH v5 2/4] efi: add EFI_DEBUG_IMAGE_INFO_TABLE for debug

Quoted 11 lines · 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. The implementation ensures support for hardware-assisted debugging and provides a standardized mechanism for debuggers to discover and interact with system-level debug resources.

Quoted 6 lines
Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Peter Robinson <[email protected]>
Cc Simon Glass <[email protected]>

---

Quoted 147 lines

V3: Fix the way of installation of configuration table.
V5: move the code into a separate module.

---
 include/efi_api.h                  | 53 ++++++++++++++++++++++++++++++
 include/efi_loader.h               |  2 ++
 lib/efi_loader/efi_debug_support.c |  6 ++++
 lib/efi_loader/efi_setup.c         | 12 +++++++
 4 files changed, 73 insertions(+)
include/efi_api.h +53 -0
index 6c4c1a0cc7b..8da0a350ce3 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;
@@ -574,6 +578,55 @@ 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 - Store Debug Information for normal
+ * image.
+ * @image_info_type: the type of image info.
+ * @loaded_image_protocol_instance: the pointer to struct efi_loaded_image.
+ * @image_handle: the EFI handle of the image.
+ *
+ * This struct is created by efi_load_image() and store the information
+ * for debugging an normal image.
+ */
+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 - The union to store a pointer for EFI
+ * DEBUG IMAGE INFO.
+ * @image_info_type: the type of the image_info if it is not a normal image.
+ * @normal_image: The pointer to a normal image.
+ *
+ * This union is for a pointer that can point to the struct of normal_image.
+ * Or it points to an image_info_type.
+ */
+union efi_debug_image_info {
+       u32 *image_info_type;
+       struct efi_debug_image_info_normal *normal_image;
+};
+
+/**
+ * struct efi_debug_image_info_table_header - store the array of
+ * struct efi_debug_image_info.
+ * @update_status: Status to notify this struct is ready to use or not.
+ * @table_size: The number of elements of efi_debug_image_info_table.
+ * @efi_debug_image_info_table: The array of efi_debug_image_info.
+ *
+ * This struct stores the array of efi_debug_image_info. The
+ * number of elements is table_size.
+ */
+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)
include/efi_loader.h +2 -0
index 8f065244d8e..d0c72d0bc58 100644
@@ -315,6 +315,8 @@ extern const struct efi_hii_config_routing_protocol efi_hii_config_routing;
 extern const struct efi_hii_config_access_protocol efi_hii_config_access;
 extern const struct efi_hii_database_protocol efi_hii_database;
 extern const struct efi_hii_string_protocol efi_hii_string;
+/* structure for EFI_DEBUG_SUPPORT_PROTOCOL */
+extern struct efi_debug_image_info_table_header efi_m_debug_info_table_header;
 
 uint16_t *efi_dp_str(struct efi_device_path *dp);
 
lib/efi_loader/efi_debug_support.c +6 -0
index a0f7506779d..a6a672bc0e4 100644
@@ -10,6 +10,12 @@
 
 struct efi_system_table_pointer __efi_runtime_data * systab_pointer = NULL;
 
+struct efi_debug_image_info_table_header efi_m_debug_info_table_header = {
+       0,
+       0,
+       NULL
+};
+
 /**
  * efi_initialize_system_table_pointer() - Initialize system table pointer
  *
lib/efi_loader/efi_setup.c +12 -1
index 6193b9f6d0c..60f3a7e5fbf 100644
@@ -18,6 +18,9 @@
 
 efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
 
+const efi_guid_t efi_debug_image_info_table_guid =
+       EFI_DEBUG_IMAGE_INFO_TABLE_GUID;
+
 /*
  * Allow unaligned memory access.
  *
@@ -280,10 +283,19 @@ efi_status_t efi_init_obj_list(void)
 
        /* Initialize system table pointer */
        if (IS_ENABLED(CONFIG_EFI_DEBUG_SUPPORT)) {
+               efi_guid_t debug_image_info_table_guid =
+                       efi_debug_image_info_table_guid;
+
                ret = efi_initialize_system_table_pointer();
                if (ret != EFI_SUCCESS) {
                        goto out;
                }
+
+               ret = efi_install_configuration_table(&debug_image_info_table_guid,
+                                                     &efi_m_debug_info_table_header);
+               if (ret != EFI_SUCCESS) {
+                       goto out;
+               }
        }
 
        if (IS_ENABLED(CONFIG_EFI_ECPT)) {
--

2.39.5

#5 lore raw

PATCH v5 3/4 lib: efi_loader: efi_memory.c: add efi_realloc() for realloc memory

In reply to Ying-Chun Liu (PaulLiu)

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

Add efi_realloc() for realloc memory that previously alloc by efi_alloc().

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Peter Robinson <[email protected]>
Cc Simon Glass <[email protected]>

---
V4: Fix efi_realloc return failure code.

---
 include/efi_loader.h        | 10 +++++++
 lib/efi_loader/efi_memory.c | 56 +++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
include/efi_loader.h +10 -0
index d0c72d0bc58..13ca2ec9a4e 100644
@@ -878,6 +878,16 @@ efi_status_t efi_next_variable_name(efi_uintn_t *size, u16 **buf,
 #define efi_size_in_pages(size) (((size) + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT)
 /* Allocate boot service data pool memory */
 void *efi_alloc(size_t len);
+/**
+ * efi_realloc() - reallocate boot services data pool memory
+ *
+ * Reallocate memory from pool for a new size and copy the data from old one.
+ *
+ * @ptr:	pointer to the buffer
+ * @size:	number of bytes to allocate
+ * Return:	EFI status to indicate success or not
+ */
+efi_status_t efi_realloc(void **ptr, size_t len);
 /* Allocate pages on the specified alignment */
 void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align);
 /* More specific EFI memory allocator, called by EFI payloads */
lib/efi_loader/efi_memory.c +56 -0

62 lines not on this page · raw message

Signature · 1 line
2.39.5
#6 lore raw

Re: [PATCH v5 3/4] lib: efi_loader: efi_memory.c: add efi_realloc() for realloc memory

Hi Paul

[...]

Quoted 10 lines
lib/efi_loader/efi_memory.c +10 -0
+/**
+ * efi_realloc() - reallocate boot services data pool memory
+ *
+ * Reallocate memory from pool for a new size and copy the data from old one.
+ *
+ * @ptr:       pointer to old buffer
+ * @size:      number of bytes to allocate
+ * Return:     EFI status to indicate success or not
+ */
+efi_status_t efi_realloc(void **ptr, size_t size)

lib/efi_loader/efi_memory.c void *efi_alloc(size_t size) · line 688

Quoted 10 lines
lib/efi_loader/efi_memory.c +10 -0
+{
+       efi_status_t ret;
+       void *new_ptr;
+       struct efi_pool_allocation *alloc;
+       u64 num_pages = efi_size_in_pages(size +
+                                         sizeof(struct efi_pool_allocation));
+       size_t old_size;
+
+       if (!*ptr) {
+               *ptr = efi_alloc(size);

Shouldn't you check that *ptr != NULL before returning success here?

Quoted 40 lines
lib/efi_loader/efi_memory.c +36 -1
+               return EFI_SUCCESS;
+       }
+
+       ret = efi_check_allocated((uintptr_t)*ptr, true);
+       if (ret != EFI_SUCCESS)
+               return ret;
+
+       alloc = container_of(*ptr, struct efi_pool_allocation, data);
+
+       /* Check that this memory was allocated by efi_allocate_pool() */
+       if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
+           alloc->checksum != checksum(alloc)) {
+               printf("%s: illegal realloc 0x%p\n", __func__, *ptr);
+               return EFI_INVALID_PARAMETER;
+       }
+
+       /* Don't realloc. The actual size in pages is the same. */
+       if (alloc->num_pages == num_pages)
+               return EFI_SUCCESS;
+
+       old_size = alloc->num_pages * EFI_PAGE_SIZE -
+               sizeof(struct efi_pool_allocation);
+
+       new_ptr = efi_alloc(size);
+
+       /* copy old data to new alloced buffer */
+       memcpy(new_ptr, *ptr, min(size, old_size));
+
+       /* free the old buffer */
+       efi_free_pool(*ptr);
+
+       *ptr = new_ptr;
+
+       return EFI_SUCCESS;
+}
+
 /**
  * efi_free_pool() - free memory from pool
  *
--
Quoted 2 lines

2.39.5

Thanks
/Ilias

#7 lore raw

Re: [PATCH v5 3/4] lib: efi_loader: efi_memory.c: add efi_realloc() for realloc memory

Quoted 18 lines · Ilias Apalodimas

Hi Paul

[...]

Quoted 14 lines
lib/efi_loader/efi_memory.c +14 -0
+/**
+ * efi_realloc() - reallocate boot services data pool memory
+ *
+ * Reallocate memory from pool for a new size and copy the data from old one.
+ *
+ * @ptr:       pointer to old buffer
+ * @size:      number of bytes to allocate
+ * Return:     EFI status to indicate success or not
+ */
+efi_status_t efi_realloc(void **ptr, size_t size)
+{
+       efi_status_t ret;
+       void *new_ptr;
+       struct efi_pool_allocation *alloc;
Quoted 8 lines

lib/efi_loader/efi_memory.c void *efi_alloc(size_t size) · line 688

Quoted 6 lines
lib/efi_loader/efi_memory.c +6 -0
+       u64 num_pages = efi_size_in_pages(size +
+                                         sizeof(struct efi_pool_allocation));
+       size_t old_size;
+
+       if (!*ptr) {
+               *ptr = efi_alloc(size);

Shouldn't you check that *ptr != NULL before returning success here?

Hello Ilias,

Do you mean "check that ptr != NULL"?

Best regards

Heinrich

Quoted 46 lines
Quoted 41 lines
lib/efi_loader/efi_memory.c +36 -1
+               return EFI_SUCCESS;
+       }
+
+       ret = efi_check_allocated((uintptr_t)*ptr, true);
+       if (ret != EFI_SUCCESS)
+               return ret;
+
+       alloc = container_of(*ptr, struct efi_pool_allocation, data);
+
+       /* Check that this memory was allocated by efi_allocate_pool() */
+       if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
+           alloc->checksum != checksum(alloc)) {
+               printf("%s: illegal realloc 0x%p\n", __func__, *ptr);
+               return EFI_INVALID_PARAMETER;
+       }
+
+       /* Don't realloc. The actual size in pages is the same. */
+       if (alloc->num_pages == num_pages)
+               return EFI_SUCCESS;
+
+       old_size = alloc->num_pages * EFI_PAGE_SIZE -
+               sizeof(struct efi_pool_allocation);
+
+       new_ptr = efi_alloc(size);
+
+       /* copy old data to new alloced buffer */
+       memcpy(new_ptr, *ptr, min(size, old_size));
+
+       /* free the old buffer */
+       efi_free_pool(*ptr);
+
+       *ptr = new_ptr;
+
+       return EFI_SUCCESS;
+}
+
  /**
   * efi_free_pool() - free memory from pool
   *
--
Quoted 2 lines

2.39.5

Thanks
/Ilias

#8 lore raw

Re: [PATCH v5 3/4] lib: efi_loader: efi_memory.c: add efi_realloc() for realloc memory

Quoted 29 lines · Heinrich Schuchardt
Quoted 18 lines · Ilias Apalodimas

Hi Paul

[...]

Quoted 14 lines
lib/efi_loader/efi_memory.c +14 -0
+/**
+ * efi_realloc() - reallocate boot services data pool memory
+ *
+ * Reallocate memory from pool for a new size and copy the data from old one.
+ *
+ * @ptr:       pointer to old buffer
+ * @size:      number of bytes to allocate
+ * Return:     EFI status to indicate success or not
+ */
+efi_status_t efi_realloc(void **ptr, size_t size)
+{
+       efi_status_t ret;
+       void *new_ptr;
+       struct efi_pool_allocation *alloc;
Quoted 8 lines

lib/efi_loader/efi_memory.c void *efi_alloc(size_t size) · line 688

Quoted 6 lines
lib/efi_loader/efi_memory.c +6 -0
+       u64 num_pages = efi_size_in_pages(size +
+                                         sizeof(struct efi_pool_allocation));
+       size_t old_size;
+
+       if (!*ptr) {
+               *ptr = efi_alloc(size);

Shouldn't you check that *ptr != NULL before returning success here?

Quoted 3 lines

Hello Ilias,

Do you mean "check that ptr != NULL"?

No i mean that the efi_alloc() call can fail as well

Thanks
/Ilias

Quoted 52 lines

Best regards

Heinrich

Quoted 46 lines
Quoted 41 lines
lib/efi_loader/efi_memory.c +36 -1
+               return EFI_SUCCESS;
+       }
+
+       ret = efi_check_allocated((uintptr_t)*ptr, true);
+       if (ret != EFI_SUCCESS)
+               return ret;
+
+       alloc = container_of(*ptr, struct efi_pool_allocation, data);
+
+       /* Check that this memory was allocated by efi_allocate_pool() */
+       if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
+           alloc->checksum != checksum(alloc)) {
+               printf("%s: illegal realloc 0x%p\n", __func__, *ptr);
+               return EFI_INVALID_PARAMETER;
+       }
+
+       /* Don't realloc. The actual size in pages is the same. */
+       if (alloc->num_pages == num_pages)
+               return EFI_SUCCESS;
+
+       old_size = alloc->num_pages * EFI_PAGE_SIZE -
+               sizeof(struct efi_pool_allocation);
+
+       new_ptr = efi_alloc(size);
+
+       /* copy old data to new alloced buffer */
+       memcpy(new_ptr, *ptr, min(size, old_size));
+
+       /* free the old buffer */
+       efi_free_pool(*ptr);
+
+       *ptr = new_ptr;
+
+       return EFI_SUCCESS;
+}
+
  /**
   * efi_free_pool() - free memory from pool
   *
--
Quoted 2 lines

2.39.5

Thanks
/Ilias

#9 lore raw

PATCH v5 4/4 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. The implementation ensures support for hardware-assisted debugging and provides a standardized mechanism for debuggers to discover the load address of an EFI application.

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Peter Robinson <[email protected]>
Cc Simon Glass <[email protected]>

---
V2: use Kconfig options to turn on/off this feature.
V3: Use efi_realloc to realloc the tables. Move tables to boot time.
V4: Use new efi_realloc().
V5: Fix function comments and move the code into a separate module.

---
 include/efi_api.h                  |   2 +
 include/efi_loader.h               |   5 ++
 lib/efi_loader/efi_boottime.c      |  11 +++
 lib/efi_loader/efi_debug_support.c | 129 +++++++++++++++++++++++++++++
 4 files changed, 147 insertions(+)
include/efi_api.h +2 -0
index 8da0a350ce3..77a05f752e5 100644
@@ -581,6 +581,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 - Store Debug Information for normal
  * image.
include/efi_loader.h +5 -0
index 13ca2ec9a4e..22440b842e3 100644
@@ -650,6 +650,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 */
+efi_status_t 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 +11 -0
index dbebb37dc04..40495c98c18 100644
@@ -2129,6 +2129,14 @@ efi_status_t EFIAPI efi_load_image(bool boot_policy,
 		*image_handle = NULL;
 		free(info);
 	}
+
+	if (IS_ENABLED(CONFIG_EFI_DEBUG_SUPPORT)) {
+		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 +3367,9 @@ efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
 		ret = EFI_INVALID_PARAMETER;
 		goto out;
 	}
+	if (IS_ENABLED(CONFIG_EFI_DEBUG_SUPPORT)) {
+		efi_core_remove_debug_image_info_entry(image_handle);
+	}
 	switch (efiobj->type) {
 	case EFI_OBJECT_TYPE_STARTED_IMAGE:
 		/* Call the unload function */
lib/efi_loader/efi_debug_support.c +129 -0

138 lines not on this page · raw message

Signature · 1 line
2.39.5
#10 lore raw

Re: [PATCH v5 4/4] efi: add EFI_DEBUG_IMAGE_INFO for debug

[...]

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

Quoted 4 lines
        }
lib/efi_loader/efi_boottime.c +3 -0
+
+       if (IS_ENABLED(CONFIG_EFI_DEBUG_SUPPORT)) {
+               if (*image_handle) {

Can you fold this in a single line if?

Quoted 9 lines
lib/efi_loader/efi_boottime.c +5 -0
+                       efi_core_new_debug_image_info_entry(EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL,
+                                                           info,
+                                                           *image_handle);
+               }
+       }
 error:
        return EFI_EXIT(ret);
 }

lib/efi_loader/efi_boottime.c efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle) · line 3372

Quoted 6 lines
lib/efi_loader/efi_boottime.c +3 -0
@@ -3359,6 +3367,9 @@ efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
                ret = EFI_INVALID_PARAMETER;
                goto out;
        }
+       if (IS_ENABLED(CONFIG_EFI_DEBUG_SUPPORT)) {
+               efi_core_remove_debug_image_info_entry(image_handle);
+       }

Drop the {}

Quoted 26 lines
lib/efi_loader/efi_boottime.c +0 -0
        switch (efiobj->type) {
        case EFI_OBJECT_TYPE_STARTED_IMAGE:
                /* Call the unload function */
lib/efi_loader/efi_debug_support.c +8 -0
index a6a672bc0e4..468e9e1cc83 100644
@@ -16,6 +16,13 @@ struct efi_debug_image_info_table_header efi_m_debug_info_table_header = {
        NULL
 };
 
+/* efi_m_max_table_entries is the maximum entries allocated for
+ * the efi_m_debug_info_table_header.efi_debug_image_info_table.
+ */
+static u32 efi_m_max_table_entries;
+
+#define EFI_DEBUG_TABLE_ENTRY_SIZE  (sizeof(union efi_debug_image_info *))
+
 /**
  * efi_initialize_system_table_pointer() - Initialize system table pointer
  *
@@ -50,3 +57,125 @@ efi_status_t efi_initialize_system_table_pointer(void)
 
        return EFI_SUCCESS;
 }
+

lib/efi_loader/efi_debug_support.c efi_status_t efi_initialize_system_table_pointer(void) · line 70

Quoted 10 lines
lib/efi_loader/efi_debug_support.c +10 -0
+/**
+ * efi_core_new_debug_image_info_entry() - Add a new efi_loaded_image structure to the
+ *                                         efi_debug_image_info Table.
+ *
+ * @image_info_type: type of debug image information
+ * @loaded_image:    pointer to the loaded image protocol for the image
+ *                   being loaded
+ * @image_handle:    image handle for the image being loaded
+ *
+ * Re-Allocates the table if it's not large enough to accomidate another

s/accomidate/accomodate

Quoted 55 lines
lib/efi_loader/efi_debug_support.c +55 -0
+ * entry.
+ *
+ * Return: status code
+ **/
+efi_status_t 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;
+       u32 index;
+       u32 table_size;
+       efi_status_t ret;
+
+       /* 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) {
+               /* table is full, re-allocate the buffer increasing the size
+                * by 4 KiB.
+                */
+               table_size = efi_m_max_table_entries * EFI_DEBUG_TABLE_ENTRY_SIZE;
+
+               ret = efi_realloc((void **)table, table_size + EFI_PAGE_SIZE);
+
+               if (ret != EFI_SUCCESS) {
+                       efi_m_debug_info_table_header.update_status &=
+                               ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+                       return ret;
+               }
+
+               /* Enlarge the max table entries and set the first empty
+                * entry index to be the original max table entries.
+                */
+               efi_m_max_table_entries +=
+                       EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;
+       }
+
+       /* We always put the next entry at the end of the currently consumed
+        * table (i.e. first free entry)
+        */
+       index = efi_m_debug_info_table_header.table_size;
+
+       /* Allocate data for new entry. */
+       efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
+                         sizeof(union efi_debug_image_info),
+                         (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 =

lib/efi_loader/efi_debug_support.c efi_status_t efi_initialize_system_table_pointer(void) · line 135

Quoted 10 lines
lib/efi_loader/efi_debug_support.c +10 -0
+                       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;
+       }

Since it's a debug protocol I am ok with not unwinding all the reallocations here, but don't you at least have to somehow warn the consumer that updating the entry failed?

lib/efi_loader/efi_debug_support.c efi_status_t efi_initialize_system_table_pointer(void) · line 142

Quoted 7 lines
lib/efi_loader/efi_debug_support.c +7 -0
+
+       efi_m_debug_info_table_header.update_status &=
+               ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
+
+       return EFI_SUCCESS;
+}
+

[...]

Thanks
/Ilias