blob: 3ce2a07f9eb0da4d382e16cbc78c92071ca2f18e [file] [log] [blame]
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +01001// 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
9#include <common.h>
10#include <efi_api.h>
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +010011#include <efi_dt_fixup.h>
Heinrich Schuchardt6bca36c2021-02-02 18:02:19 +010012#include <part.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <linux/libfdt.h>
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +010014
15#define BUFFER_SIZE 64
16#define ESC 0x17
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +010017
18#define efi_size_in_pages(size) ((size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT)
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +010019
20static struct efi_simple_text_output_protocol *cerr;
21static struct efi_simple_text_output_protocol *cout;
22static struct efi_simple_text_input_protocol *cin;
23static struct efi_boot_services *bs;
24static const efi_guid_t fdt_guid = EFI_FDT_GUID;
25static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
26static const efi_guid_t guid_simple_file_system_protocol =
27 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +010028static efi_handle_t handle;
29static struct efi_system_table *systable;
30static const efi_guid_t efi_dt_fixup_protocol_guid = EFI_DT_FIXUP_PROTOCOL_GUID;
31static const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
Heinrich Schuchardt6bca36c2021-02-02 18:02:19 +010032static const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +010033
34/**
Heinrich Schuchardtd56013d2021-01-19 10:06:00 +010035 * print() - print string
36 *
37 * @string: text
38 */
39static void print(u16 *string)
40{
41 cout->output_string(cout, string);
42}
43
44/**
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +010045 * error() - print error string
46 *
47 * @string: error text
48 */
49static void error(u16 *string)
50{
51 cout->set_attribute(cout, EFI_LIGHTRED | EFI_BACKGROUND_BLACK);
Heinrich Schuchardtd56013d2021-01-19 10:06:00 +010052 print(string);
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +010053 cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
54}
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +010055
56/**
Heinrich Schuchardtb6f60802021-01-17 05:13:21 +010057 * efi_input_yn() - get answer to yes/no question
58 *
59 * Return:
60 * y or Y
61 * EFI_SUCCESS
62 * n or N
63 * EFI_ACCESS_DENIED
64 * ESC
65 * EFI_ABORTED
66 */
67static efi_status_t efi_input_yn(void)
68{
69 struct efi_input_key key = {0};
70 efi_uintn_t index;
71 efi_status_t ret;
72
73 /* Drain the console input */
74 ret = cin->reset(cin, true);
75 for (;;) {
76 ret = bs->wait_for_event(1, &cin->wait_for_key, &index);
77 if (ret != EFI_SUCCESS)
78 continue;
79 ret = cin->read_key_stroke(cin, &key);
80 if (ret != EFI_SUCCESS)
81 continue;
82 switch (key.scan_code) {
83 case 0x17: /* Escape */
84 return EFI_ABORTED;
85 default:
86 break;
87 }
88 /* Convert to lower case */
89 switch (key.unicode_char | 0x20) {
90 case 'y':
91 return EFI_SUCCESS;
92 case 'n':
93 return EFI_ACCESS_DENIED;
94 default:
95 break;
96 }
97 }
98}
99
100/**
101 * efi_input() - read string from console
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100102 *
103 * @buffer: input buffer
104 * @buffer_size: buffer size
105 * Return: status code
106 */
107static efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size)
108{
109 struct efi_input_key key = {0};
110 efi_uintn_t index;
111 efi_uintn_t pos = 0;
Simon Glass156ccbc2022-01-23 12:55:12 -0700112 u16 outbuf[2] = u" ";
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100113 efi_status_t ret;
114
115 /* Drain the console input */
116 ret = cin->reset(cin, true);
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100117 *buffer = 0;
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100118 for (;;) {
119 ret = bs->wait_for_event(1, &cin->wait_for_key, &index);
120 if (ret != EFI_SUCCESS)
121 continue;
122 ret = cin->read_key_stroke(cin, &key);
123 if (ret != EFI_SUCCESS)
124 continue;
125 switch (key.scan_code) {
126 case 0x17: /* Escape */
Simon Glass156ccbc2022-01-23 12:55:12 -0700127 print(u"\r\nAborted\r\n");
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100128 return EFI_ABORTED;
129 default:
130 break;
131 }
132 switch (key.unicode_char) {
133 case 0x08: /* Backspace */
134 if (pos) {
135 buffer[pos--] = 0;
Simon Glass156ccbc2022-01-23 12:55:12 -0700136 print(u"\b \b");
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100137 }
138 break;
139 case 0x0a: /* Linefeed */
140 case 0x0d: /* Carriage return */
Simon Glass156ccbc2022-01-23 12:55:12 -0700141 print(u"\r\n");
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100142 return EFI_SUCCESS;
143 default:
144 break;
145 }
146 /* Ignore surrogate codes */
147 if (key.unicode_char >= 0xD800 && key.unicode_char <= 0xDBFF)
148 continue;
149 if (key.unicode_char >= 0x20 &&
150 pos < buffer_size - 1) {
151 *outbuf = key.unicode_char;
152 buffer[pos++] = key.unicode_char;
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100153 buffer[pos] = 0;
Heinrich Schuchardtd56013d2021-01-19 10:06:00 +0100154 print(outbuf);
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100155 }
156 }
157}
158
159/*
160 * Convert FDT value to host endianness.
161 *
162 * @val FDT value
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100163 * Return: converted value
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100164 */
165static u32 f2h(fdt32_t val)
166{
167 char *buf = (char *)&val;
168 char i;
169
170 /* Swap the bytes */
171 i = buf[0]; buf[0] = buf[3]; buf[3] = i;
172 i = buf[1]; buf[1] = buf[2]; buf[2] = i;
173 return *(u32 *)buf;
174}
175
176/**
177 * get_dtb() - get device tree
178 *
179 * @systable: system table
180 * Return: device tree or NULL
181 */
182void *get_dtb(struct efi_system_table *systable)
183{
184 void *dtb = NULL;
185 efi_uintn_t i;
186
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;
191 break;
192 }
193 }
194 return dtb;
195}
196
197/**
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100198 * skip_whitespace() - skip over leading whitespace
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100199 *
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100200 * @pos: UTF-16 string
201 * Return: pointer to first non-whitespace
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100202 */
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100203u16 *skip_whitespace(u16 *pos)
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100204{
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100205 for (; *pos && *pos <= 0x20; ++pos)
206 ;
207 return pos;
208}
209
210/**
211 * starts_with() - check if @string starts with @keyword
212 *
213 * @string: string to search for keyword
214 * @keyword: keyword to be searched
215 * Return: true fi @string starts with the keyword
216 */
217bool starts_with(u16 *string, u16 *keyword)
218{
219 for (; *keyword; ++string, ++keyword) {
220 if (*string != *keyword)
221 return false;
222 }
223 return true;
224}
225
226/**
227 * do_help() - print help
228 */
229void do_help(void)
230{
Simon Glass156ccbc2022-01-23 12:55:12 -0700231 error(u"load <dtb> - load device-tree from file\r\n");
232 error(u"save <dtb> - save device-tree to file\r\n");
233 error(u"exit - exit the shell\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100234}
235
236/**
Heinrich Schuchardt6bca36c2021-02-02 18:02:19 +0100237 * open_file_system() - open simple file system protocol
238 *
239 * file_system: interface of the simple file system protocol
240 * Return: status code
241 */
242static efi_status_t
243open_file_system(struct efi_simple_file_system_protocol **file_system)
244{
245 struct efi_loaded_image *loaded_image;
246 efi_status_t ret;
247 efi_handle_t *handle_buffer = NULL;
248 efi_uintn_t count;
249
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) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700254 error(u"Loaded image protocol not found\r\n");
Heinrich Schuchardt6bca36c2021-02-02 18:02:19 +0100255 return ret;
256 }
257
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)
264 return ret;
265
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)
Simon Glass156ccbc2022-01-23 12:55:12 -0700275 error(u"Failed to open simple file system protocol\r\n");
Heinrich Schuchardt6bca36c2021-02-02 18:02:19 +0100276 if (handle)
277 bs->free_pool(handle_buffer);
278
279 return ret;
280}
281
282/**
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100283 * do_load() - load and install device-tree
284 *
285 * @filename: file name
286 * Return: status code
287 */
288efi_status_t do_load(u16 *filename)
289{
290 struct efi_dt_fixup_protocol *dt_fixup_prot;
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100291 struct efi_simple_file_system_protocol *file_system;
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100292 struct efi_file_handle *root = NULL, *file = NULL;
293 u64 addr = 0;
294 struct efi_file_info *info;
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100295 struct fdt_header *dtb;
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100296 efi_uintn_t buffer_size;
297 efi_uintn_t pages;
298 efi_status_t ret, ret2;
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100299
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100300 ret = bs->locate_protocol(&efi_dt_fixup_protocol_guid, NULL,
301 (void **)&dt_fixup_prot);
302 if (ret != EFI_SUCCESS) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700303 error(u"Device-tree fix-up protocol not found\r\n");
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100304 return ret;
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100305 }
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100306
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100307 filename = skip_whitespace(filename);
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100308
Heinrich Schuchardt6bca36c2021-02-02 18:02:19 +0100309 ret = open_file_system(&file_system);
310 if (ret != EFI_SUCCESS)
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100311 goto out;
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100312
313 /* Open volume */
314 ret = file_system->open_volume(file_system, &root);
315 if (ret != EFI_SUCCESS) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700316 error(u"Failed to open volume\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100317 goto out;
318 }
319
320 /* Open file */
321 ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0);
322 if (ret != EFI_SUCCESS) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700323 error(u"File not found\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100324 goto out;
325 }
326 /* Get file size */
327 buffer_size = 0;
328 ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, NULL);
329 if (ret != EFI_BUFFER_TOO_SMALL) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700330 error(u"Can't get file info size\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100331 goto out;
332 }
333 ret = bs->allocate_pool(EFI_LOADER_DATA, buffer_size, (void **)&info);
334 if (ret != EFI_SUCCESS) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700335 error(u"Out of memory\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100336 goto out;
337 }
338 ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, info);
339 if (ret != EFI_SUCCESS) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700340 error(u"Can't get file info\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100341 goto out;
342 }
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)
Simon Glass156ccbc2022-01-23 12:55:12 -0700347 error(u"Can't free memory pool\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100348 /* Read file */
349 ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
350 EFI_ACPI_RECLAIM_MEMORY,
351 pages, &addr);
352 if (ret != EFI_SUCCESS) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700353 error(u"Out of memory\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100354 goto out;
355 }
356 dtb = (struct fdt_header *)(uintptr_t)addr;
357 ret = file->read(file, &buffer_size, dtb);
358 if (ret != EFI_SUCCESS) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700359 error(u"Can't read file\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100360 goto out;
361 }
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)
Simon Glass156ccbc2022-01-23 12:55:12 -0700370 error(u"Can't free memory pages\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100371 pages = efi_size_in_pages(buffer_size);
372 ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
373 EFI_ACPI_RECLAIM_MEMORY,
374 pages, &addr);
375 if (ret != EFI_SUCCESS) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700376 error(u"Out of memory\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100377 goto out;
378 }
379 dtb = (struct fdt_header *)(uintptr_t)addr;
380 ret = file->setpos(file, 0);
381 if (ret != EFI_SUCCESS) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700382 error(u"Can't position file\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100383 goto out;
384 }
385 ret = file->read(file, &buffer_size, dtb);
386 if (ret != EFI_SUCCESS) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700387 error(u"Can't read file\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100388 goto out;
389 }
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);
395 }
396 if (ret == EFI_SUCCESS)
Simon Glass156ccbc2022-01-23 12:55:12 -0700397 print(u"device-tree installed\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100398 else
Simon Glass156ccbc2022-01-23 12:55:12 -0700399 error(u"Device-tree fix-up failed\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100400out:
401 if (addr) {
402 ret2 = bs->free_pages(addr, pages);
403 if (ret2 != EFI_SUCCESS)
Simon Glass156ccbc2022-01-23 12:55:12 -0700404 error(u"Can't free memory pages\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100405 }
406 if (file) {
407 ret2 = file->close(file);
408 if (ret2 != EFI_SUCCESS)
Simon Glass156ccbc2022-01-23 12:55:12 -0700409 error(u"Can't close file\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100410 }
411 if (root) {
412 ret2 = root->close(root);
413 if (ret2 != EFI_SUCCESS)
Simon Glass156ccbc2022-01-23 12:55:12 -0700414 error(u"Can't close volume\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100415 }
416 return ret;
417}
418
419/**
420 * do_save() - save current device-tree
421 *
422 * @filename: file name
423 * Return: status code
424 */
425efi_status_t do_save(u16 *filename)
426{
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100427 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;
431 efi_uintn_t ret;
432
433 dtb = get_dtb(systable);
434 if (!dtb) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700435 error(u"DTB not found\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100436 return EFI_NOT_FOUND;
437 }
438 if (f2h(dtb->magic) != FDT_MAGIC) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700439 error(u"Wrong device tree magic\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100440 return EFI_NOT_FOUND;
441 }
442 dtb_size = f2h(dtb->totalsize);
443
444 filename = skip_whitespace(filename);
445
Heinrich Schuchardt6bca36c2021-02-02 18:02:19 +0100446 ret = open_file_system(&file_system);
447 if (ret != EFI_SUCCESS)
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100448 return ret;
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100449
450 /* Open volume */
451 ret = file_system->open_volume(file_system, &root);
452 if (ret != EFI_SUCCESS) {
Simon Glass156ccbc2022-01-23 12:55:12 -0700453 error(u"Failed to open volume\r\n");
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100454 return ret;
455 }
Heinrich Schuchardtb6f60802021-01-17 05:13:21 +0100456 /* Check if file already exists */
457 ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0);
458 if (ret == EFI_SUCCESS) {
459 file->close(file);
Simon Glass156ccbc2022-01-23 12:55:12 -0700460 print(u"Overwrite existing file (y/n)? ");
Heinrich Schuchardtb6f60802021-01-17 05:13:21 +0100461 ret = efi_input_yn();
Simon Glass156ccbc2022-01-23 12:55:12 -0700462 print(u"\r\n");
Heinrich Schuchardtb6f60802021-01-17 05:13:21 +0100463 if (ret != EFI_SUCCESS) {
464 root->close(root);
Simon Glass156ccbc2022-01-23 12:55:12 -0700465 error(u"Aborted by user\r\n");
Heinrich Schuchardtb6f60802021-01-17 05:13:21 +0100466 return ret;
467 }
468 }
469
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100470 /* Create file */
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) {
475 /* Write file */
476 ret = file->write(file, &dtb_size, dtb);
477 if (ret != EFI_SUCCESS)
Simon Glass156ccbc2022-01-23 12:55:12 -0700478 error(u"Failed to write file\r\n");
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100479 file->close(file);
480 } else {
Simon Glass156ccbc2022-01-23 12:55:12 -0700481 error(u"Failed to open file\r\n");
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100482 }
483 root->close(root);
484
485 if (ret == EFI_SUCCESS) {
Heinrich Schuchardtd56013d2021-01-19 10:06:00 +0100486 print(filename);
Simon Glass156ccbc2022-01-23 12:55:12 -0700487 print(u" written\r\n");
Heinrich Schuchardtb1aa6fc2020-11-04 22:00:48 +0100488 }
489
490 return ret;
491}
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100492
493/**
494 * efi_main() - entry point of the EFI application.
495 *
496 * @handle: handle of the loaded image
497 * @systab: system table
Heinrich Schuchardt3dd719d2022-01-20 19:48:20 +0100498 * Return: status code
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100499 */
500efi_status_t EFIAPI efi_main(efi_handle_t image_handle,
501 struct efi_system_table *systab)
502{
503 handle = image_handle;
504 systable = systab;
505 cerr = systable->std_err;
506 cout = systable->con_out;
507 cin = systable->con_in;
508 bs = systable->boottime;
509
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);
Simon Glass156ccbc2022-01-23 12:55:12 -0700513 print(u"DTB Dump\r\n========\r\n\r\n");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100514 cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
515
516 for (;;) {
517 u16 command[BUFFER_SIZE];
518 u16 *pos;
519 efi_uintn_t ret;
520
Simon Glass156ccbc2022-01-23 12:55:12 -0700521 print(u"=> ");
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100522 ret = efi_input(command, sizeof(command));
523 if (ret == EFI_ABORTED)
524 break;
525 pos = skip_whitespace(command);
Simon Glass156ccbc2022-01-23 12:55:12 -0700526 if (starts_with(pos, u"exit"))
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100527 break;
Simon Glass156ccbc2022-01-23 12:55:12 -0700528 else if (starts_with(pos, u"load "))
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100529 do_load(pos + 5);
Simon Glass156ccbc2022-01-23 12:55:12 -0700530 else if (starts_with(pos, u"save "))
Heinrich Schuchardt8e70f1c2020-12-13 19:13:57 +0100531 do_save(pos + 5);
532 else
533 do_help();
534 }
535
536 cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK);
537 cout->clear_screen(cout);
538 return EFI_SUCCESS;
539}