1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025, Heinrich Schuchardt <xypron.glpk@gmx.de>
*
* dbginfodump.efi prints out the content of the EFI_DEBUG_IMAGE_INFO_TABLE.
*/
#include <efi_api.h>
/**
* BUFFER_SIZE - size of the command line input buffer
*/
#define BUFFER_SIZE 64
static struct efi_simple_text_output_protocol *cerr;
static struct efi_simple_text_output_protocol *cout;
static struct efi_simple_text_input_protocol *cin;
static efi_handle_t handle;
static struct efi_system_table *systable;
static struct efi_boot_services *bs;
static efi_guid_t guid_device_path_to_text_protocol =
EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
static struct efi_device_path_to_text_protocol *device_path_to_text;
/* EFI_DEBUG_IMAGE_INFO_TABLE_GUID */
static const efi_guid_t dbg_info_guid =
EFI_GUID(0x49152E77, 0x1ADA, 0x4764, 0xB7, 0xA2,
0x7A, 0xFE, 0xFE, 0xD9, 0x5E, 0x8B);
/* EFI_DEBUG_IMAGE_INFO_NORMAL */
struct dbg_info {
u32 type;
struct efi_loaded_image *info;
efi_handle_t handle;
};
/* FI_DEBUG_IMAGE_INFO_TABLE_HEADER */
struct dbg_info_header {
u32 status;
u32 size;
struct dbg_info **info;
};
/**
* print() - print string
*
* @string: text
*/
static void print(u16 *string)
{
cout->output_string(cout, string);
}
/**
* error() - print error string
*
* @string: error text
*/
static void error(u16 *string)
{
cout->set_attribute(cout, EFI_LIGHTRED | EFI_BACKGROUND_BLACK);
print(string);
cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
}
/**
* printu() - print unsigned
*
* @val: value to print
*/
static void printu(u32 val)
{
u16 str[19] = u"0x";
u16 *ptr = &str[2];
u16 ch;
for (ssize_t i = 8 * sizeof(u32) - 4; i >= 0; i -= 4) {
ch = (val >> i & 0xf) + '0';
if (ch > '9')
ch += 'a' - '9' - 1;
*ptr++ = ch;
}
*ptr = 0;
print(str);
}
/**
* printp() - print pointer
*
* @p: pointer
*/
static void printp(void *p)
{
u16 str[19] = u"0x";
u16 *ptr = &str[2];
u16 ch;
for (ssize_t i = 8 * sizeof(void *) - 4; i >= 0; i -= 4) {
ch = ((uintptr_t)p >> i & 0xf) + '0';
if (ch > '9')
ch += 'a' - '9' - 1;
*ptr++ = ch;
}
*ptr = 0;
print(str);
}
/**
* efi_input() - read string from console
*
* @buffer: input buffer
* @buffer_size: buffer size
* Return: status code
*/
static efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size)
{
struct efi_input_key key = {0};
efi_uintn_t index;
efi_uintn_t pos = 0;
u16 outbuf[2] = u" ";
efi_status_t ret;
/* Drain the console input */
ret = cin->reset(cin, true);
*buffer = 0;
for (;;) {
ret = bs->wait_for_event(1, &cin->wait_for_key, &index);
if (ret != EFI_SUCCESS)
continue;
ret = cin->read_key_stroke(cin, &key);
if (ret != EFI_SUCCESS)
continue;
switch (key.scan_code) {
case 0x17: /* Escape */
print(u"\r\nAborted\r\n");
return EFI_ABORTED;
default:
break;
}
switch (key.unicode_char) {
case 0x08: /* Backspace */
if (pos) {
buffer[pos--] = 0;
print(u"\b \b");
}
break;
case 0x0a: /* Linefeed */
case 0x0d: /* Carriage return */
print(u"\r\n");
return EFI_SUCCESS;
default:
break;
}
/* Ignore surrogate codes */
if (key.unicode_char >= 0xD800 && key.unicode_char <= 0xDBFF)
continue;
if (key.unicode_char >= 0x20 &&
pos < buffer_size - 1) {
*outbuf = key.unicode_char;
buffer[pos++] = key.unicode_char;
buffer[pos] = 0;
print(outbuf);
}
}
}
/**
* skip_whitespace() - skip over leading whitespace
*
* @pos: UTF-16 string
* Return: pointer to first non-whitespace
*/
static u16 *skip_whitespace(u16 *pos)
{
for (; *pos && *pos <= 0x20; ++pos)
;
return pos;
}
/**
* starts_with() - check if @string starts with @keyword
*
* @string: string to search for keyword
* @keyword: keyword to be searched
* Return: true fi @string starts with the keyword
*/
static bool starts_with(u16 *string, u16 *keyword)
{
for (; *keyword; ++string, ++keyword) {
if (*string != *keyword)
return false;
}
return true;
}
/**
* do_help() - print help
*/
static void do_help(void)
{
error(u"dump - print debug info table\r\n");
error(u"exit - exit the shell\r\n");
}
/**
* get_dbg_info_table() - get debug info table
*
* Return: debug info table or NULL
*/
static void *get_dbg_info(void)
{
void *dbg = NULL;
efi_uintn_t i;
for (i = 0; i < systable->nr_tables; ++i) {
if (!memcmp(&systable->tables[i].guid, &dbg_info_guid,
sizeof(efi_guid_t))) {
dbg = systable->tables[i].table;
break;
}
}
return dbg;
}
/**
* print_info() - print loaded image protocol
*/
static void print_info(struct efi_loaded_image *info)
{
print(u" Address: [");
printp(info->image_base);
print(u", ");
printp(info->image_base + info->image_size - 1);
print(u"]\r\n");
if (device_path_to_text && info->file_path) {
u16 *string;
string = device_path_to_text->convert_device_path_to_text(
info->file_path, true, false);
if (!string) {
error(u"ConvertDevicePathToText failed");
} else {
print(u" File: ");
print(string);
}
print(u"\r\n");
}
}
/**
* do_dump() - print debug info table
*/
static efi_status_t do_dump(void)
{
struct dbg_info_header *dbg;
u32 count;
dbg = get_dbg_info();
if (!dbg) {
error(u"Debug info table not found\r\n");
return EFI_NOT_FOUND;
}
if (dbg->status & 0x01) {
error(u"Update in progress\r\n");
return EFI_LOAD_ERROR;
}
if (dbg->status & 0x02)
print(u"Modified\r\n");
print(u"Number of entries: ");
printu(dbg->size);
print(u"\r\n");
count = dbg->size;
for (u32 i = 0; count; ++i) {
struct dbg_info *info = dbg->info[i];
/*
* The EDK II implementation decreases the size field and
* writes a NULL value when deleting an entry which is not
* backed by the UEFI specification.
*/
if (!info) {
print(u"Deleted entry\r\n");
continue;
}
--count;
print(u"Info type ");
printu(info->type);
print(u"\r\n");
if (info->type != 1)
continue;
print_info(info->info);
print(u" Handle: ");
printp(info->handle);
print(u"\r\n");
}
return EFI_SUCCESS;
}
/**
* efi_main() - entry point of the EFI application.
*
* @handle: handle of the loaded image
* @systab: system table
* Return: status code
*/
efi_status_t EFIAPI efi_main(efi_handle_t image_handle,
struct efi_system_table *systab)
{
efi_status_t ret;
handle = image_handle;
systable = systab;
cerr = systable->std_err;
cout = systable->con_out;
cin = systable->con_in;
bs = systable->boottime;
cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
cout->clear_screen(cout);
cout->set_attribute(cout, EFI_WHITE | EFI_BACKGROUND_BLACK);
print(u"Debug Info Table Dump\r\n=====================\r\n\r\n");
cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
ret = bs->locate_protocol(&guid_device_path_to_text_protocol,
NULL, (void **)&device_path_to_text);
if (ret != EFI_SUCCESS) {
error(u"No device path to text protocol\r\n");
device_path_to_text = NULL;
}
for (;;) {
u16 command[BUFFER_SIZE];
u16 *pos;
efi_uintn_t ret;
print(u"=> ");
ret = efi_input(command, sizeof(command));
if (ret == EFI_ABORTED)
break;
pos = skip_whitespace(command);
if (starts_with(pos, u"exit"))
break;
else if (starts_with(pos, u"dump"))
do_dump();
else
do_help();
}
cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK);
cout->clear_screen(cout);
return EFI_SUCCESS;
}
|