dtbdump.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. #include <efi_dt_fixup.h>
  11. #define BUFFER_SIZE 64
  12. #define ESC 0x17
  13. #define efi_size_in_pages(size) ((size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT)
  14. static struct efi_simple_text_output_protocol *cerr;
  15. static struct efi_simple_text_output_protocol *cout;
  16. static struct efi_simple_text_input_protocol *cin;
  17. static struct efi_boot_services *bs;
  18. static const efi_guid_t fdt_guid = EFI_FDT_GUID;
  19. static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
  20. static const efi_guid_t guid_simple_file_system_protocol =
  21. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
  22. static efi_handle_t handle;
  23. static struct efi_system_table *systable;
  24. static const efi_guid_t efi_dt_fixup_protocol_guid = EFI_DT_FIXUP_PROTOCOL_GUID;
  25. static const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
  26. /**
  27. * error() - print error string
  28. *
  29. * @string: error text
  30. */
  31. static void error(u16 *string)
  32. {
  33. cout->set_attribute(cout, EFI_LIGHTRED | EFI_BACKGROUND_BLACK);
  34. cout->output_string(cout, string);
  35. cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
  36. }
  37. /**
  38. * input() - read string from console
  39. *
  40. * @buffer: input buffer
  41. * @buffer_size: buffer size
  42. * Return: status code
  43. */
  44. static efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size)
  45. {
  46. struct efi_input_key key = {0};
  47. efi_uintn_t index;
  48. efi_uintn_t pos = 0;
  49. u16 outbuf[2] = L" ";
  50. efi_status_t ret;
  51. /* Drain the console input */
  52. ret = cin->reset(cin, true);
  53. *buffer = 0;
  54. for (;;) {
  55. ret = bs->wait_for_event(1, &cin->wait_for_key, &index);
  56. if (ret != EFI_SUCCESS)
  57. continue;
  58. ret = cin->read_key_stroke(cin, &key);
  59. if (ret != EFI_SUCCESS)
  60. continue;
  61. switch (key.scan_code) {
  62. case 0x17: /* Escape */
  63. cout->output_string(cout, L"\nAborted\n");
  64. return EFI_ABORTED;
  65. default:
  66. break;
  67. }
  68. switch (key.unicode_char) {
  69. case 0x08: /* Backspace */
  70. if (pos) {
  71. buffer[pos--] = 0;
  72. cout->output_string(cout, L"\b \b");
  73. }
  74. break;
  75. case 0x0a: /* Linefeed */
  76. case 0x0d: /* Carriage return */
  77. cout->output_string(cout, L"\n");
  78. return EFI_SUCCESS;
  79. default:
  80. break;
  81. }
  82. /* Ignore surrogate codes */
  83. if (key.unicode_char >= 0xD800 && key.unicode_char <= 0xDBFF)
  84. continue;
  85. if (key.unicode_char >= 0x20 &&
  86. pos < buffer_size - 1) {
  87. *outbuf = key.unicode_char;
  88. buffer[pos++] = key.unicode_char;
  89. buffer[pos] = 0;
  90. cout->output_string(cout, outbuf);
  91. }
  92. }
  93. }
  94. /*
  95. * Convert FDT value to host endianness.
  96. *
  97. * @val FDT value
  98. * @return converted value
  99. */
  100. static u32 f2h(fdt32_t val)
  101. {
  102. char *buf = (char *)&val;
  103. char i;
  104. /* Swap the bytes */
  105. i = buf[0]; buf[0] = buf[3]; buf[3] = i;
  106. i = buf[1]; buf[1] = buf[2]; buf[2] = i;
  107. return *(u32 *)buf;
  108. }
  109. /**
  110. * get_dtb() - get device tree
  111. *
  112. * @systable: system table
  113. * Return: device tree or NULL
  114. */
  115. void *get_dtb(struct efi_system_table *systable)
  116. {
  117. void *dtb = NULL;
  118. efi_uintn_t i;
  119. for (i = 0; i < systable->nr_tables; ++i) {
  120. if (!memcmp(&systable->tables[i].guid, &fdt_guid,
  121. sizeof(efi_guid_t))) {
  122. dtb = systable->tables[i].table;
  123. break;
  124. }
  125. }
  126. return dtb;
  127. }
  128. /**
  129. * skip_whitespace() - skip over leading whitespace
  130. *
  131. * @pos: UTF-16 string
  132. * Return: pointer to first non-whitespace
  133. */
  134. u16 *skip_whitespace(u16 *pos)
  135. {
  136. for (; *pos && *pos <= 0x20; ++pos)
  137. ;
  138. return pos;
  139. }
  140. /**
  141. * starts_with() - check if @string starts with @keyword
  142. *
  143. * @string: string to search for keyword
  144. * @keyword: keyword to be searched
  145. * Return: true fi @string starts with the keyword
  146. */
  147. bool starts_with(u16 *string, u16 *keyword)
  148. {
  149. for (; *keyword; ++string, ++keyword) {
  150. if (*string != *keyword)
  151. return false;
  152. }
  153. return true;
  154. }
  155. /**
  156. * do_help() - print help
  157. */
  158. void do_help(void)
  159. {
  160. error(L"load <dtb> - load device-tree from file\n");
  161. error(L"save <dtb> - save device-tree to file\n");
  162. error(L"exit - exit the shell\n");
  163. }
  164. /**
  165. * do_load() - load and install device-tree
  166. *
  167. * @filename: file name
  168. * Return: status code
  169. */
  170. efi_status_t do_load(u16 *filename)
  171. {
  172. struct efi_dt_fixup_protocol *dt_fixup_prot;
  173. struct efi_loaded_image *loaded_image;
  174. struct efi_simple_file_system_protocol *file_system;
  175. struct efi_file_handle *root = NULL, *file = NULL;
  176. u64 addr = 0;
  177. struct efi_file_info *info;
  178. struct fdt_header *dtb;
  179. efi_uintn_t buffer_size;
  180. efi_uintn_t pages;
  181. efi_status_t ret, ret2;
  182. ret = bs->locate_protocol(&efi_dt_fixup_protocol_guid, NULL,
  183. (void **)&dt_fixup_prot);
  184. if (ret != EFI_SUCCESS) {
  185. error(L"Device-tree fix-up protocol not found\n");
  186. return ret;
  187. }
  188. filename = skip_whitespace(filename);
  189. ret = bs->open_protocol(handle, &loaded_image_guid,
  190. (void **)&loaded_image, NULL, NULL,
  191. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  192. if (ret != EFI_SUCCESS) {
  193. error(L"Loaded image protocol not found\n");
  194. return ret;
  195. }
  196. /* Open the simple file system protocol */
  197. ret = bs->open_protocol(loaded_image->device_handle,
  198. &guid_simple_file_system_protocol,
  199. (void **)&file_system, NULL, NULL,
  200. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  201. if (ret != EFI_SUCCESS) {
  202. error(L"Failed to open simple file system protocol\n");
  203. goto out;
  204. }
  205. /* Open volume */
  206. ret = file_system->open_volume(file_system, &root);
  207. if (ret != EFI_SUCCESS) {
  208. error(L"Failed to open volume\n");
  209. goto out;
  210. }
  211. /* Open file */
  212. ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0);
  213. if (ret != EFI_SUCCESS) {
  214. error(L"File not found\n");
  215. goto out;
  216. }
  217. /* Get file size */
  218. buffer_size = 0;
  219. ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, NULL);
  220. if (ret != EFI_BUFFER_TOO_SMALL) {
  221. error(L"Can't get file info size\n");
  222. goto out;
  223. }
  224. ret = bs->allocate_pool(EFI_LOADER_DATA, buffer_size, (void **)&info);
  225. if (ret != EFI_SUCCESS) {
  226. error(L"Out of memory\n");
  227. goto out;
  228. }
  229. ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, info);
  230. if (ret != EFI_SUCCESS) {
  231. error(L"Can't get file info\n");
  232. goto out;
  233. }
  234. buffer_size = info->file_size;
  235. pages = efi_size_in_pages(buffer_size);
  236. ret = bs->free_pool(info);
  237. if (ret != EFI_SUCCESS)
  238. error(L"Can't free memory pool\n");
  239. /* Read file */
  240. ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
  241. EFI_ACPI_RECLAIM_MEMORY,
  242. pages, &addr);
  243. if (ret != EFI_SUCCESS) {
  244. error(L"Out of memory\n");
  245. goto out;
  246. }
  247. dtb = (struct fdt_header *)(uintptr_t)addr;
  248. ret = file->read(file, &buffer_size, dtb);
  249. if (ret != EFI_SUCCESS) {
  250. error(L"Can't read file\n");
  251. goto out;
  252. }
  253. /* Fixup file, expecting EFI_BUFFER_TOO_SMALL */
  254. ret = dt_fixup_prot->fixup(dt_fixup_prot, dtb, &buffer_size,
  255. EFI_DT_APPLY_FIXUPS | EFI_DT_RESERVE_MEMORY |
  256. EFI_DT_INSTALL_TABLE);
  257. if (ret == EFI_BUFFER_TOO_SMALL) {
  258. /* Read file into larger buffer */
  259. ret = bs->free_pages(addr, pages);
  260. if (ret != EFI_SUCCESS)
  261. error(L"Can't free memory pages\n");
  262. pages = efi_size_in_pages(buffer_size);
  263. ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
  264. EFI_ACPI_RECLAIM_MEMORY,
  265. pages, &addr);
  266. if (ret != EFI_SUCCESS) {
  267. error(L"Out of memory\n");
  268. goto out;
  269. }
  270. dtb = (struct fdt_header *)(uintptr_t)addr;
  271. ret = file->setpos(file, 0);
  272. if (ret != EFI_SUCCESS) {
  273. error(L"Can't position file\n");
  274. goto out;
  275. }
  276. ret = file->read(file, &buffer_size, dtb);
  277. if (ret != EFI_SUCCESS) {
  278. error(L"Can't read file\n");
  279. goto out;
  280. }
  281. buffer_size = pages << EFI_PAGE_SHIFT;
  282. ret = dt_fixup_prot->fixup(
  283. dt_fixup_prot, dtb, &buffer_size,
  284. EFI_DT_APPLY_FIXUPS | EFI_DT_RESERVE_MEMORY |
  285. EFI_DT_INSTALL_TABLE);
  286. }
  287. if (ret == EFI_SUCCESS)
  288. cout->output_string(cout, L"device-tree installed\n");
  289. else
  290. error(L"Device-tree fix-up failed\n");
  291. out:
  292. if (addr) {
  293. ret2 = bs->free_pages(addr, pages);
  294. if (ret2 != EFI_SUCCESS)
  295. error(L"Can't free memory pages\n");
  296. }
  297. if (file) {
  298. ret2 = file->close(file);
  299. if (ret2 != EFI_SUCCESS)
  300. error(L"Can't close file\n");
  301. }
  302. if (root) {
  303. ret2 = root->close(root);
  304. if (ret2 != EFI_SUCCESS)
  305. error(L"Can't close volume\n");
  306. }
  307. return ret;
  308. }
  309. /**
  310. * do_save() - save current device-tree
  311. *
  312. * @filename: file name
  313. * Return: status code
  314. */
  315. efi_status_t do_save(u16 *filename)
  316. {
  317. struct efi_loaded_image *loaded_image;
  318. struct efi_simple_file_system_protocol *file_system;
  319. efi_uintn_t dtb_size;
  320. struct efi_file_handle *root, *file;
  321. struct fdt_header *dtb;
  322. efi_uintn_t ret;
  323. dtb = get_dtb(systable);
  324. if (!dtb) {
  325. error(L"DTB not found\n");
  326. return EFI_NOT_FOUND;
  327. }
  328. if (f2h(dtb->magic) != FDT_MAGIC) {
  329. error(L"Wrong device tree magic\n");
  330. return EFI_NOT_FOUND;
  331. }
  332. dtb_size = f2h(dtb->totalsize);
  333. filename = skip_whitespace(filename);
  334. ret = bs->open_protocol(handle, &loaded_image_guid,
  335. (void **)&loaded_image, NULL, NULL,
  336. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  337. if (ret != EFI_SUCCESS) {
  338. error(L"Loaded image protocol not found\n");
  339. return ret;
  340. }
  341. /* Open the simple file system protocol */
  342. ret = bs->open_protocol(loaded_image->device_handle,
  343. &guid_simple_file_system_protocol,
  344. (void **)&file_system, NULL, NULL,
  345. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  346. if (ret != EFI_SUCCESS) {
  347. error(L"Failed to open simple file system protocol\n");
  348. return ret;
  349. }
  350. /* Open volume */
  351. ret = file_system->open_volume(file_system, &root);
  352. if (ret != EFI_SUCCESS) {
  353. error(L"Failed to open volume\n");
  354. return ret;
  355. }
  356. /* Create file */
  357. ret = root->open(root, &file, filename,
  358. EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
  359. EFI_FILE_MODE_CREATE, EFI_FILE_ARCHIVE);
  360. if (ret == EFI_SUCCESS) {
  361. /* Write file */
  362. ret = file->write(file, &dtb_size, dtb);
  363. if (ret != EFI_SUCCESS)
  364. error(L"Failed to write file\n");
  365. file->close(file);
  366. } else {
  367. error(L"Failed to open file\n");
  368. }
  369. root->close(root);
  370. if (ret == EFI_SUCCESS) {
  371. cout->output_string(cout, filename);
  372. cout->output_string(cout, L" written\n");
  373. }
  374. return ret;
  375. }
  376. /**
  377. * efi_main() - entry point of the EFI application.
  378. *
  379. * @handle: handle of the loaded image
  380. * @systab: system table
  381. * @return: status code
  382. */
  383. efi_status_t EFIAPI efi_main(efi_handle_t image_handle,
  384. struct efi_system_table *systab)
  385. {
  386. handle = image_handle;
  387. systable = systab;
  388. cerr = systable->std_err;
  389. cout = systable->con_out;
  390. cin = systable->con_in;
  391. bs = systable->boottime;
  392. cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
  393. cout->clear_screen(cout);
  394. cout->set_attribute(cout, EFI_WHITE | EFI_BACKGROUND_BLACK);
  395. cout->output_string(cout, L"DTB Dump\n========\n\n");
  396. cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
  397. for (;;) {
  398. u16 command[BUFFER_SIZE];
  399. u16 *pos;
  400. efi_uintn_t ret;
  401. cout->output_string(cout, L"=> ");
  402. ret = efi_input(command, sizeof(command));
  403. if (ret == EFI_ABORTED)
  404. break;
  405. pos = skip_whitespace(command);
  406. if (starts_with(pos, L"exit"))
  407. break;
  408. else if (starts_with(pos, L"load "))
  409. do_load(pos + 5);
  410. else if (starts_with(pos, L"save "))
  411. do_save(pos + 5);
  412. else
  413. do_help();
  414. }
  415. cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK);
  416. cout->clear_screen(cout);
  417. return EFI_SUCCESS;
  418. }