1 // SPDX-License-Identifier: GPL-2.0+
5 * dtbdump.efi saves the device tree provided as a configuration table
11 #include <efi_dt_fixup.h>
13 #include <linux/libfdt.h>
15 #define BUFFER_SIZE 64
18 #define efi_size_in_pages(size) ((size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT)
20 static struct efi_simple_text_output_protocol *cerr;
21 static struct efi_simple_text_output_protocol *cout;
22 static struct efi_simple_text_input_protocol *cin;
23 static struct efi_boot_services *bs;
24 static const efi_guid_t fdt_guid = EFI_FDT_GUID;
25 static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
26 static const efi_guid_t guid_simple_file_system_protocol =
27 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
28 static efi_handle_t handle;
29 static struct efi_system_table *systable;
30 static const efi_guid_t efi_dt_fixup_protocol_guid = EFI_DT_FIXUP_PROTOCOL_GUID;
31 static const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
32 static const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
35 * print() - print string
39 static void print(u16 *string)
41 cout->output_string(cout, string);
45 * error() - print error string
49 static void error(u16 *string)
51 cout->set_attribute(cout, EFI_LIGHTRED | EFI_BACKGROUND_BLACK);
53 cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
57 * efi_input_yn() - get answer to yes/no question
67 static efi_status_t efi_input_yn(void)
69 struct efi_input_key key = {0};
73 /* Drain the console input */
74 ret = cin->reset(cin, true);
76 ret = bs->wait_for_event(1, &cin->wait_for_key, &index);
77 if (ret != EFI_SUCCESS)
79 ret = cin->read_key_stroke(cin, &key);
80 if (ret != EFI_SUCCESS)
82 switch (key.scan_code) {
83 case 0x17: /* Escape */
88 /* Convert to lower case */
89 switch (key.unicode_char | 0x20) {
93 return EFI_ACCESS_DENIED;
101 * efi_input() - read string from console
103 * @buffer: input buffer
104 * @buffer_size: buffer size
105 * Return: status code
107 static efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size)
109 struct efi_input_key key = {0};
112 u16 outbuf[2] = L" ";
115 /* Drain the console input */
116 ret = cin->reset(cin, true);
119 ret = bs->wait_for_event(1, &cin->wait_for_key, &index);
120 if (ret != EFI_SUCCESS)
122 ret = cin->read_key_stroke(cin, &key);
123 if (ret != EFI_SUCCESS)
125 switch (key.scan_code) {
126 case 0x17: /* Escape */
127 print(L"\r\nAborted\r\n");
132 switch (key.unicode_char) {
133 case 0x08: /* Backspace */
139 case 0x0a: /* Linefeed */
140 case 0x0d: /* Carriage return */
146 /* Ignore surrogate codes */
147 if (key.unicode_char >= 0xD800 && key.unicode_char <= 0xDBFF)
149 if (key.unicode_char >= 0x20 &&
150 pos < buffer_size - 1) {
151 *outbuf = key.unicode_char;
152 buffer[pos++] = key.unicode_char;
160 * Convert FDT value to host endianness.
163 * Return: converted value
165 static u32 f2h(fdt32_t val)
167 char *buf = (char *)&val;
171 i = buf[0]; buf[0] = buf[3]; buf[3] = i;
172 i = buf[1]; buf[1] = buf[2]; buf[2] = i;
177 * get_dtb() - get device tree
179 * @systable: system table
180 * Return: device tree or NULL
182 void *get_dtb(struct efi_system_table *systable)
187 for (i = 0; i < systable->nr_tables; ++i) {
188 if (!memcmp(&systable->tables[i].guid, &fdt_guid,
189 sizeof(efi_guid_t))) {
190 dtb = systable->tables[i].table;
198 * skip_whitespace() - skip over leading whitespace
200 * @pos: UTF-16 string
201 * Return: pointer to first non-whitespace
203 u16 *skip_whitespace(u16 *pos)
205 for (; *pos && *pos <= 0x20; ++pos)
211 * starts_with() - check if @string starts with @keyword
213 * @string: string to search for keyword
214 * @keyword: keyword to be searched
215 * Return: true fi @string starts with the keyword
217 bool starts_with(u16 *string, u16 *keyword)
219 for (; *keyword; ++string, ++keyword) {
220 if (*string != *keyword)
227 * do_help() - print help
231 error(L"load <dtb> - load device-tree from file\r\n");
232 error(L"save <dtb> - save device-tree to file\r\n");
233 error(L"exit - exit the shell\r\n");
237 * open_file_system() - open simple file system protocol
239 * file_system: interface of the simple file system protocol
240 * Return: status code
243 open_file_system(struct efi_simple_file_system_protocol **file_system)
245 struct efi_loaded_image *loaded_image;
247 efi_handle_t *handle_buffer = NULL;
250 ret = bs->open_protocol(handle, &loaded_image_guid,
251 (void **)&loaded_image, NULL, NULL,
252 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
253 if (ret != EFI_SUCCESS) {
254 error(L"Loaded image protocol not found\r\n");
258 /* Open the simple file system protocol on the same partition */
259 ret = bs->open_protocol(loaded_image->device_handle,
260 &guid_simple_file_system_protocol,
261 (void **)file_system, NULL, NULL,
262 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
263 if (ret == EFI_SUCCESS)
266 /* Open the simple file system protocol on the UEFI system partition */
267 ret = bs->locate_handle_buffer(BY_PROTOCOL, &efi_system_partition_guid,
268 NULL, &count, &handle_buffer);
269 if (ret == EFI_SUCCESS && handle_buffer)
270 ret = bs->open_protocol(handle_buffer[0],
271 &guid_simple_file_system_protocol,
272 (void **)file_system, NULL, NULL,
273 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
274 if (ret != EFI_SUCCESS)
275 error(L"Failed to open simple file system protocol\r\n");
277 bs->free_pool(handle_buffer);
283 * do_load() - load and install device-tree
285 * @filename: file name
286 * Return: status code
288 efi_status_t do_load(u16 *filename)
290 struct efi_dt_fixup_protocol *dt_fixup_prot;
291 struct efi_simple_file_system_protocol *file_system;
292 struct efi_file_handle *root = NULL, *file = NULL;
294 struct efi_file_info *info;
295 struct fdt_header *dtb;
296 efi_uintn_t buffer_size;
298 efi_status_t ret, ret2;
300 ret = bs->locate_protocol(&efi_dt_fixup_protocol_guid, NULL,
301 (void **)&dt_fixup_prot);
302 if (ret != EFI_SUCCESS) {
303 error(L"Device-tree fix-up protocol not found\r\n");
307 filename = skip_whitespace(filename);
309 ret = open_file_system(&file_system);
310 if (ret != EFI_SUCCESS)
314 ret = file_system->open_volume(file_system, &root);
315 if (ret != EFI_SUCCESS) {
316 error(L"Failed to open volume\r\n");
321 ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0);
322 if (ret != EFI_SUCCESS) {
323 error(L"File not found\r\n");
328 ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, NULL);
329 if (ret != EFI_BUFFER_TOO_SMALL) {
330 error(L"Can't get file info size\r\n");
333 ret = bs->allocate_pool(EFI_LOADER_DATA, buffer_size, (void **)&info);
334 if (ret != EFI_SUCCESS) {
335 error(L"Out of memory\r\n");
338 ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, info);
339 if (ret != EFI_SUCCESS) {
340 error(L"Can't get file info\r\n");
343 buffer_size = info->file_size;
344 pages = efi_size_in_pages(buffer_size);
345 ret = bs->free_pool(info);
346 if (ret != EFI_SUCCESS)
347 error(L"Can't free memory pool\r\n");
349 ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
350 EFI_ACPI_RECLAIM_MEMORY,
352 if (ret != EFI_SUCCESS) {
353 error(L"Out of memory\r\n");
356 dtb = (struct fdt_header *)(uintptr_t)addr;
357 ret = file->read(file, &buffer_size, dtb);
358 if (ret != EFI_SUCCESS) {
359 error(L"Can't read file\r\n");
362 /* Fixup file, expecting EFI_BUFFER_TOO_SMALL */
363 ret = dt_fixup_prot->fixup(dt_fixup_prot, dtb, &buffer_size,
364 EFI_DT_APPLY_FIXUPS | EFI_DT_RESERVE_MEMORY |
365 EFI_DT_INSTALL_TABLE);
366 if (ret == EFI_BUFFER_TOO_SMALL) {
367 /* Read file into larger buffer */
368 ret = bs->free_pages(addr, pages);
369 if (ret != EFI_SUCCESS)
370 error(L"Can't free memory pages\r\n");
371 pages = efi_size_in_pages(buffer_size);
372 ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
373 EFI_ACPI_RECLAIM_MEMORY,
375 if (ret != EFI_SUCCESS) {
376 error(L"Out of memory\r\n");
379 dtb = (struct fdt_header *)(uintptr_t)addr;
380 ret = file->setpos(file, 0);
381 if (ret != EFI_SUCCESS) {
382 error(L"Can't position file\r\n");
385 ret = file->read(file, &buffer_size, dtb);
386 if (ret != EFI_SUCCESS) {
387 error(L"Can't read file\r\n");
390 buffer_size = pages << EFI_PAGE_SHIFT;
391 ret = dt_fixup_prot->fixup(
392 dt_fixup_prot, dtb, &buffer_size,
393 EFI_DT_APPLY_FIXUPS | EFI_DT_RESERVE_MEMORY |
394 EFI_DT_INSTALL_TABLE);
396 if (ret == EFI_SUCCESS)
397 print(L"device-tree installed\r\n");
399 error(L"Device-tree fix-up failed\r\n");
402 ret2 = bs->free_pages(addr, pages);
403 if (ret2 != EFI_SUCCESS)
404 error(L"Can't free memory pages\r\n");
407 ret2 = file->close(file);
408 if (ret2 != EFI_SUCCESS)
409 error(L"Can't close file\r\n");
412 ret2 = root->close(root);
413 if (ret2 != EFI_SUCCESS)
414 error(L"Can't close volume\r\n");
420 * do_save() - save current device-tree
422 * @filename: file name
423 * Return: status code
425 efi_status_t do_save(u16 *filename)
427 struct efi_simple_file_system_protocol *file_system;
428 efi_uintn_t dtb_size;
429 struct efi_file_handle *root, *file;
430 struct fdt_header *dtb;
433 dtb = get_dtb(systable);
435 error(L"DTB not found\r\n");
436 return EFI_NOT_FOUND;
438 if (f2h(dtb->magic) != FDT_MAGIC) {
439 error(L"Wrong device tree magic\r\n");
440 return EFI_NOT_FOUND;
442 dtb_size = f2h(dtb->totalsize);
444 filename = skip_whitespace(filename);
446 ret = open_file_system(&file_system);
447 if (ret != EFI_SUCCESS)
451 ret = file_system->open_volume(file_system, &root);
452 if (ret != EFI_SUCCESS) {
453 error(L"Failed to open volume\r\n");
456 /* Check if file already exists */
457 ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0);
458 if (ret == EFI_SUCCESS) {
460 print(L"Overwrite existing file (y/n)? ");
461 ret = efi_input_yn();
463 if (ret != EFI_SUCCESS) {
465 error(L"Aborted by user\r\n");
471 ret = root->open(root, &file, filename,
472 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
473 EFI_FILE_MODE_CREATE, EFI_FILE_ARCHIVE);
474 if (ret == EFI_SUCCESS) {
476 ret = file->write(file, &dtb_size, dtb);
477 if (ret != EFI_SUCCESS)
478 error(L"Failed to write file\r\n");
481 error(L"Failed to open file\r\n");
485 if (ret == EFI_SUCCESS) {
487 print(L" written\r\n");
494 * efi_main() - entry point of the EFI application.
496 * @handle: handle of the loaded image
497 * @systab: system table
498 * Return: status code
500 efi_status_t EFIAPI efi_main(efi_handle_t image_handle,
501 struct efi_system_table *systab)
503 handle = image_handle;
505 cerr = systable->std_err;
506 cout = systable->con_out;
507 cin = systable->con_in;
508 bs = systable->boottime;
510 cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
511 cout->clear_screen(cout);
512 cout->set_attribute(cout, EFI_WHITE | EFI_BACKGROUND_BLACK);
513 print(L"DTB Dump\r\n========\r\n\r\n");
514 cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
517 u16 command[BUFFER_SIZE];
522 ret = efi_input(command, sizeof(command));
523 if (ret == EFI_ABORTED)
525 pos = skip_whitespace(command);
526 if (starts_with(pos, L"exit"))
528 else if (starts_with(pos, L"load "))
530 else if (starts_with(pos, L"save "))
536 cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK);
537 cout->clear_screen(cout);