1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * EFI debug support
4  *
5  * Copyright (c) 2025 Ying-Chun Liu, Linaro Ltd. <paul.liu@linaro.org>
6  */
7 
8 #include <efi_loader.h>
9 #include <linux/sizes.h>
10 #include <u-boot/crc.h>
11 
12 struct efi_system_table_pointer __efi_runtime_data * systab_pointer = NULL;
13 
14 struct efi_debug_image_info_table_header efi_m_debug_info_table_header = {
15 	0,
16 	0,
17 	NULL
18 };
19 
20 /* efi_m_max_table_entries is the maximum entries allocated for
21  * the efi_m_debug_info_table_header.efi_debug_image_info_table.
22  */
23 static u32 efi_m_max_table_entries;
24 
25 #define EFI_DEBUG_TABLE_ENTRY_SIZE  (sizeof(union efi_debug_image_info))
26 
27 /**
28  * efi_initialize_system_table_pointer() - Initialize system table pointer
29  *
30  * Return:	status code
31  */
efi_initialize_system_table_pointer(void)32 efi_status_t efi_initialize_system_table_pointer(void)
33 {
34 	/* Allocate efi_system_table_pointer structure with 4MB alignment. */
35 	systab_pointer = efi_alloc_aligned_pages(sizeof(struct efi_system_table_pointer),
36 						 EFI_RUNTIME_SERVICES_DATA,
37 						 SZ_4M);
38 
39 	if (!systab_pointer) {
40 		log_err("Installing EFI system table pointer failed\n");
41 		return EFI_OUT_OF_RESOURCES;
42 	}
43 
44 	systab_pointer->crc32 = 0;
45 
46 	systab_pointer->signature = EFI_SYSTEM_TABLE_SIGNATURE;
47 	systab_pointer->efi_system_table_base = (uintptr_t)&systab;
48 	systab_pointer->crc32 = crc32(0,
49 				      (const unsigned char *)systab_pointer,
50 				      sizeof(struct efi_system_table_pointer));
51 
52 	return EFI_SUCCESS;
53 }
54 
55 /**
56  * efi_core_new_debug_image_info_entry() - Add a new efi_loaded_image structure to the
57  *                                         efi_debug_image_info table.
58  *
59  * @image_info_type: type of debug image information
60  * @loaded_image:    pointer to the loaded image protocol for the image
61  *                   being loaded
62  * @image_handle:    image handle for the image being loaded
63  *
64  * Re-Allocates the table if it's not large enough to accommodate another
65  * entry.
66  *
67  * Return: status code
68  **/
efi_core_new_debug_image_info_entry(u32 image_info_type,struct efi_loaded_image * loaded_image,efi_handle_t image_handle)69 efi_status_t efi_core_new_debug_image_info_entry(u32 image_info_type,
70 						 struct efi_loaded_image *loaded_image,
71 						 efi_handle_t image_handle)
72 {
73 	union efi_debug_image_info **table;
74 	u32 index;
75 	u32 table_size;
76 	efi_status_t ret;
77 
78 	/* Set the flag indicating that we're in the process of updating
79 	 * the table.
80 	 */
81 	efi_m_debug_info_table_header.update_status |=
82 		EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
83 
84 	table = &efi_m_debug_info_table_header.efi_debug_image_info_table;
85 
86 	if (efi_m_debug_info_table_header.table_size >= efi_m_max_table_entries) {
87 		/* table is full, re-allocate the buffer increasing the size
88 		 * by 4 KiB.
89 		 */
90 		table_size = efi_m_max_table_entries * EFI_DEBUG_TABLE_ENTRY_SIZE;
91 
92 		ret = efi_realloc((void **)table, table_size + EFI_PAGE_SIZE);
93 
94 		if (ret != EFI_SUCCESS) {
95 			efi_m_debug_info_table_header.update_status &=
96 				~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
97 			return ret;
98 		}
99 
100 		/* Enlarge the max table entries and set the first empty
101 		 * entry index to be the original max table entries.
102 		 */
103 		efi_m_max_table_entries +=
104 			EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;
105 	}
106 
107 	/* We always put the next entry at the end of the currently consumed
108 	 * table (i.e. first free entry)
109 	 */
110 	index = efi_m_debug_info_table_header.table_size;
111 
112 	/* Allocate data for new entry. */
113 	ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
114 				sizeof(union efi_debug_image_info),
115 				(void **)(&(*table)[index].normal_image));
116 	if (ret == EFI_SUCCESS && (*table)[index].normal_image) {
117 		/* Update the entry. */
118 		(*table)[index].normal_image->image_info_type = image_info_type;
119 		(*table)[index].normal_image->loaded_image_protocol_instance =
120 			loaded_image;
121 		(*table)[index].normal_image->image_handle = image_handle;
122 
123 		/* Increase the number of EFI_DEBUG_IMAGE_INFO elements and
124 		 * set the efi_m_debug_info_table_header in modified status.
125 		 */
126 		efi_m_debug_info_table_header.table_size++;
127 		efi_m_debug_info_table_header.update_status |=
128 			EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
129 	} else {
130 		log_err("Adding new efi_debug_image_info failed\n");
131 		return ret;
132 	}
133 
134 	efi_m_debug_info_table_header.update_status &=
135 		~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
136 
137 	return EFI_SUCCESS;
138 }
139 
140 /**
141  * efi_core_remove_debug_image_info_entry() - Remove an efi_debug_image_info entry.
142  *
143  * @image_handle:    image handle for the image being removed
144  **/
efi_core_remove_debug_image_info_entry(efi_handle_t image_handle)145 void efi_core_remove_debug_image_info_entry(efi_handle_t image_handle)
146 {
147 	union efi_debug_image_info *table;
148 	u32 index;
149 
150 	efi_m_debug_info_table_header.update_status |=
151 		EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
152 
153 	table = efi_m_debug_info_table_header.efi_debug_image_info_table;
154 
155 	for (index = 0; index < efi_m_max_table_entries; index++) {
156 		if (table[index].normal_image &&
157 		    table[index].normal_image->image_handle == image_handle) {
158 			/* Found a match. Free up the table entry.
159 			 * Move the tail of the table one slot to the front.
160 			 */
161 			efi_free_pool(table[index].normal_image);
162 
163 			memmove(&table[index],
164 				&table[index + 1],
165 				(efi_m_debug_info_table_header.table_size -
166 				 index - 1) * EFI_DEBUG_TABLE_ENTRY_SIZE);
167 
168 			/* Decrease the number of EFI_DEBUG_IMAGE_INFO
169 			 * elements and set the efi_m_debug_info_table_header
170 			 * in modified status.
171 			 */
172 			efi_m_debug_info_table_header.table_size--;
173 			table[efi_m_debug_info_table_header.table_size].normal_image =
174 				NULL;
175 			efi_m_debug_info_table_header.update_status |=
176 				EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
177 			break;
178 		}
179 	}
180 
181 	efi_m_debug_info_table_header.update_status &=
182 		~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
183 }
184