dtbdump.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
  4. *
  5. * dtbdump.efi saves the device tree provided as a configuration table
  6. * to a file.
  7. */
  8. #include <common.h>
  9. #include <efi_api.h>
  10. #define BUFFER_SIZE 64
  11. #define ESC 0x17
  12. #define DEFAULT_FILENAME L"dtb.dtb"
  13. static struct efi_simple_text_output_protocol *cerr;
  14. static struct efi_simple_text_output_protocol *cout;
  15. static struct efi_simple_text_input_protocol *cin;
  16. static struct efi_boot_services *bs;
  17. static const efi_guid_t fdt_guid = EFI_FDT_GUID;
  18. static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
  19. static const efi_guid_t guid_simple_file_system_protocol =
  20. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
  21. /**
  22. * input() - read string from console
  23. *
  24. * @buffer: input buffer
  25. * @buffer_size: buffer size
  26. * Return: status code
  27. */
  28. static efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size)
  29. {
  30. struct efi_input_key key = {0};
  31. efi_uintn_t index;
  32. efi_uintn_t pos = 0;
  33. u16 outbuf[2] = L" ";
  34. efi_status_t ret;
  35. /* Drain the console input */
  36. ret = cin->reset(cin, true);
  37. for (;;) {
  38. ret = bs->wait_for_event(1, &cin->wait_for_key, &index);
  39. if (ret != EFI_SUCCESS)
  40. continue;
  41. ret = cin->read_key_stroke(cin, &key);
  42. if (ret != EFI_SUCCESS)
  43. continue;
  44. switch (key.scan_code) {
  45. case 0x17: /* Escape */
  46. cout->output_string(cout, L"\nAborted\n");
  47. return EFI_ABORTED;
  48. default:
  49. break;
  50. }
  51. switch (key.unicode_char) {
  52. case 0x08: /* Backspace */
  53. if (pos) {
  54. buffer[pos--] = 0;
  55. cout->output_string(cout, L"\b \b");
  56. }
  57. break;
  58. case 0x0a: /* Linefeed */
  59. case 0x0d: /* Carriage return */
  60. return EFI_SUCCESS;
  61. default:
  62. break;
  63. }
  64. /* Ignore surrogate codes */
  65. if (key.unicode_char >= 0xD800 && key.unicode_char <= 0xDBFF)
  66. continue;
  67. if (key.unicode_char >= 0x20 &&
  68. pos < buffer_size - 1) {
  69. *outbuf = key.unicode_char;
  70. buffer[pos++] = key.unicode_char;
  71. cout->output_string(cout, outbuf);
  72. }
  73. }
  74. }
  75. /*
  76. * Convert FDT value to host endianness.
  77. *
  78. * @val FDT value
  79. * @return converted value
  80. */
  81. static u32 f2h(fdt32_t val)
  82. {
  83. char *buf = (char *)&val;
  84. char i;
  85. /* Swap the bytes */
  86. i = buf[0]; buf[0] = buf[3]; buf[3] = i;
  87. i = buf[1]; buf[1] = buf[2]; buf[2] = i;
  88. return *(u32 *)buf;
  89. }
  90. /**
  91. * get_dtb() - get device tree
  92. *
  93. * @systable: system table
  94. * Return: device tree or NULL
  95. */
  96. void *get_dtb(struct efi_system_table *systable)
  97. {
  98. void *dtb = NULL;
  99. efi_uintn_t i;
  100. for (i = 0; i < systable->nr_tables; ++i) {
  101. if (!memcmp(&systable->tables[i].guid, &fdt_guid,
  102. sizeof(efi_guid_t))) {
  103. dtb = systable->tables[i].table;
  104. break;
  105. }
  106. }
  107. return dtb;
  108. }
  109. /**
  110. * efi_main() - entry point of the EFI application.
  111. *
  112. * @handle: handle of the loaded image
  113. * @systable: system table
  114. * @return: status code
  115. */
  116. efi_status_t EFIAPI efi_main(efi_handle_t handle,
  117. struct efi_system_table *systable)
  118. {
  119. efi_uintn_t ret;
  120. u16 filename[BUFFER_SIZE] = {0};
  121. efi_uintn_t dtb_size;
  122. struct efi_loaded_image *loaded_image;
  123. struct efi_simple_file_system_protocol *file_system;
  124. struct efi_file_handle *root, *file;
  125. struct fdt_header *dtb;
  126. cerr = systable->std_err;
  127. cout = systable->con_out;
  128. cin = systable->con_in;
  129. bs = systable->boottime;
  130. cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK);
  131. cout->clear_screen(cout);
  132. cout->set_attribute(cout, EFI_YELLOW | EFI_BACKGROUND_BLACK);
  133. cout->output_string(cout, L"DTB Dump\n\n");
  134. cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK);
  135. dtb = get_dtb(systable);
  136. if (!dtb) {
  137. cerr->output_string(cout, L"DTB not found\n");
  138. return EFI_NOT_FOUND;
  139. }
  140. if (f2h(dtb->magic) != FDT_MAGIC) {
  141. cerr->output_string(cout, L"Wrong device tree magic\n");
  142. return EFI_NOT_FOUND;
  143. }
  144. dtb_size = f2h(dtb->totalsize);
  145. cout->output_string(cout, L"Filename (" DEFAULT_FILENAME ")?\n");
  146. ret = efi_input(filename, sizeof(filename));
  147. if (ret != EFI_SUCCESS)
  148. return ret;
  149. if (!*filename)
  150. memcpy(filename, DEFAULT_FILENAME, sizeof(DEFAULT_FILENAME));
  151. cout->output_string(cout, L"\n");
  152. ret = bs->open_protocol(handle, &loaded_image_guid,
  153. (void **)&loaded_image, NULL, NULL,
  154. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  155. if (ret != EFI_SUCCESS) {
  156. cerr->output_string(cout,
  157. L"Loaded image protocol not found\n");
  158. return ret;
  159. }
  160. /* Open the simple file system protocol */
  161. ret = bs->open_protocol(loaded_image->device_handle,
  162. &guid_simple_file_system_protocol,
  163. (void **)&file_system, NULL, NULL,
  164. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  165. if (ret != EFI_SUCCESS) {
  166. cerr->output_string(
  167. cout, L"Failed to open simple file system protocol\n");
  168. return ret;
  169. }
  170. /* Open volume */
  171. ret = file_system->open_volume(file_system, &root);
  172. if (ret != EFI_SUCCESS) {
  173. cerr->output_string(cerr, L"Failed to open volume\n");
  174. return ret;
  175. }
  176. /* Create file */
  177. ret = root->open(root, &file, filename,
  178. EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
  179. EFI_FILE_MODE_CREATE, EFI_FILE_ARCHIVE);
  180. if (ret == EFI_SUCCESS) {
  181. /* Write file */
  182. ret = file->write(file, &dtb_size, dtb);
  183. if (ret != EFI_SUCCESS)
  184. cerr->output_string(cerr, L"Failed to write file\n");
  185. file->close(file);
  186. } else {
  187. cerr->output_string(cerr, L"Failed to open file\n");
  188. }
  189. root->close(root);
  190. if (ret == EFI_SUCCESS) {
  191. cout->output_string(cout, filename);
  192. cout->output_string(cout, L" written\n");
  193. }
  194. return ret;
  195. }