1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
5 * dtbdump.efi saves the device tree provided as a configuration table
12 #define BUFFER_SIZE 64
14 #define DEFAULT_FILENAME L"dtb.dtb"
16 static struct efi_simple_text_output_protocol *cerr;
17 static struct efi_simple_text_output_protocol *cout;
18 static struct efi_simple_text_input_protocol *cin;
19 static struct efi_boot_services *bs;
20 static const efi_guid_t fdt_guid = EFI_FDT_GUID;
21 static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
22 static const efi_guid_t guid_simple_file_system_protocol =
23 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
26 * input() - read string from console
28 * @buffer: input buffer
29 * @buffer_size: buffer size
32 static efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size)
34 struct efi_input_key key = {0};
40 /* Drain the console input */
41 ret = cin->reset(cin, true);
43 ret = bs->wait_for_event(1, &cin->wait_for_key, &index);
44 if (ret != EFI_SUCCESS)
46 ret = cin->read_key_stroke(cin, &key);
47 if (ret != EFI_SUCCESS)
49 switch (key.scan_code) {
50 case 0x17: /* Escape */
51 cout->output_string(cout, L"\nAborted\n");
56 switch (key.unicode_char) {
57 case 0x08: /* Backspace */
60 cout->output_string(cout, L"\b \b");
63 case 0x0a: /* Linefeed */
64 case 0x0d: /* Carriage return */
69 /* Ignore surrogate codes */
70 if (key.unicode_char >= 0xD800 && key.unicode_char <= 0xDBFF)
72 if (key.unicode_char >= 0x20 &&
73 pos < buffer_size - 1) {
74 *outbuf = key.unicode_char;
75 buffer[pos++] = key.unicode_char;
76 cout->output_string(cout, outbuf);
82 * Convert FDT value to host endianness.
85 * @return converted value
87 static u32 f2h(fdt32_t val)
89 char *buf = (char *)&val;
93 i = buf[0]; buf[0] = buf[3]; buf[3] = i;
94 i = buf[1]; buf[1] = buf[2]; buf[2] = i;
99 * get_dtb() - get device tree
101 * @systable: system table
102 * Return: device tree or NULL
104 void *get_dtb(struct efi_system_table *systable)
109 for (i = 0; i < systable->nr_tables; ++i) {
110 if (!memcmp(&systable->tables[i].guid, &fdt_guid,
111 sizeof(efi_guid_t))) {
112 dtb = systable->tables[i].table;
120 * efi_main() - entry point of the EFI application.
122 * @handle: handle of the loaded image
123 * @systable: system table
124 * @return: status code
126 efi_status_t EFIAPI efi_main(efi_handle_t handle,
127 struct efi_system_table *systable)
130 u16 filename[BUFFER_SIZE] = {0};
131 efi_uintn_t dtb_size;
132 struct efi_loaded_image *loaded_image;
133 struct efi_simple_file_system_protocol *file_system;
134 struct efi_file_handle *root, *file;
135 struct fdt_header *dtb;
137 cerr = systable->std_err;
138 cout = systable->con_out;
139 cin = systable->con_in;
140 bs = systable->boottime;
142 cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK);
143 cout->clear_screen(cout);
144 cout->set_attribute(cout, EFI_YELLOW | EFI_BACKGROUND_BLACK);
145 cout->output_string(cout, L"DTB Dump\n\n");
146 cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK);
148 dtb = get_dtb(systable);
150 cerr->output_string(cout, L"DTB not found\n");
151 return EFI_NOT_FOUND;
153 if (f2h(dtb->magic) != FDT_MAGIC) {
154 cerr->output_string(cout, L"Wrong device tree magic\n");
155 return EFI_NOT_FOUND;
157 dtb_size = f2h(dtb->totalsize);
159 cout->output_string(cout, L"Filename (" DEFAULT_FILENAME ")?\n");
160 ret = efi_input(filename, sizeof(filename));
161 if (ret != EFI_SUCCESS)
164 memcpy(filename, DEFAULT_FILENAME, sizeof(DEFAULT_FILENAME));
166 cout->output_string(cout, L"\n");
168 ret = bs->open_protocol(handle, &loaded_image_guid,
169 (void **)&loaded_image, NULL, NULL,
170 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
171 if (ret != EFI_SUCCESS) {
172 cerr->output_string(cout,
173 L"Loaded image protocol not found\n");
177 /* Open the simple file system protocol */
178 ret = bs->open_protocol(loaded_image->device_handle,
179 &guid_simple_file_system_protocol,
180 (void **)&file_system, NULL, NULL,
181 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
182 if (ret != EFI_SUCCESS) {
184 cout, L"Failed to open simple file system protocol\n");
189 ret = file_system->open_volume(file_system, &root);
190 if (ret != EFI_SUCCESS) {
191 cerr->output_string(cerr, L"Failed to open volume\n");
195 ret = root->open(root, &file, filename,
196 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
197 EFI_FILE_MODE_CREATE, EFI_FILE_ARCHIVE);
198 if (ret == EFI_SUCCESS) {
200 ret = file->write(file, &dtb_size, dtb);
201 if (ret != EFI_SUCCESS)
202 cerr->output_string(cerr, L"Failed to write file\n");
205 cerr->output_string(cerr, L"Failed to open file\n");
209 if (ret == EFI_SUCCESS) {
210 cout->output_string(cout, filename);
211 cout->output_string(cout, L" written\n");