1
2
3
4
5
6
7
8
9#include <common.h>
10#include <efi_api.h>
11#include <efi_dt_fixup.h>
12#include <part.h>
13#include <linux/libfdt.h>
14
15#define BUFFER_SIZE 64
16#define ESC 0x17
17
18#define efi_size_in_pages(size) ((size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT)
19
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;
28static 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;
32static const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
33
34
35
36
37
38
39static void print(u16 *string)
40{
41 cout->output_string(cout, string);
42}
43
44
45
46
47
48
49static void error(u16 *string)
50{
51 cout->set_attribute(cout, EFI_LIGHTRED | EFI_BACKGROUND_BLACK);
52 print(string);
53 cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
54}
55
56
57
58
59
60
61
62
63
64
65
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
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:
84 return EFI_ABORTED;
85 default:
86 break;
87 }
88
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
102
103
104
105
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;
112 u16 outbuf[2] = u" ";
113 efi_status_t ret;
114
115
116 ret = cin->reset(cin, true);
117 *buffer = 0;
118 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:
127 print(u"\r\nAborted\r\n");
128 return EFI_ABORTED;
129 default:
130 break;
131 }
132 switch (key.unicode_char) {
133 case 0x08:
134 if (pos) {
135 buffer[pos--] = 0;
136 print(u"\b \b");
137 }
138 break;
139 case 0x0a:
140 case 0x0d:
141 print(u"\r\n");
142 return EFI_SUCCESS;
143 default:
144 break;
145 }
146
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;
153 buffer[pos] = 0;
154 print(outbuf);
155 }
156 }
157}
158
159
160
161
162
163
164
165static u32 f2h(fdt32_t val)
166{
167 char *buf = (char *)&val;
168 char i;
169
170
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
178
179
180
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
198
199
200
201
202
203u16 *skip_whitespace(u16 *pos)
204{
205 for (; *pos && *pos <= 0x20; ++pos)
206 ;
207 return pos;
208}
209
210
211
212
213
214
215
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
228
229void do_help(void)
230{
231 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");
234}
235
236
237
238
239
240
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) {
254 error(u"Loaded image protocol not found\r\n");
255 return ret;
256 }
257
258
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
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(u"Failed to open simple file system protocol\r\n");
276 if (handle)
277 bs->free_pool(handle_buffer);
278
279 return ret;
280}
281
282
283
284
285
286
287
288efi_status_t do_load(u16 *filename)
289{
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;
293 u64 addr = 0;
294 struct efi_file_info *info;
295 struct fdt_header *dtb;
296 efi_uintn_t buffer_size;
297 efi_uintn_t pages;
298 efi_status_t ret, ret2;
299
300 ret = bs->locate_protocol(&efi_dt_fixup_protocol_guid, NULL,
301 (void **)&dt_fixup_prot);
302 if (ret != EFI_SUCCESS) {
303 error(u"Device-tree fix-up protocol not found\r\n");
304 return ret;
305 }
306
307 filename = skip_whitespace(filename);
308
309 ret = open_file_system(&file_system);
310 if (ret != EFI_SUCCESS)
311 goto out;
312
313
314 ret = file_system->open_volume(file_system, &root);
315 if (ret != EFI_SUCCESS) {
316 error(u"Failed to open volume\r\n");
317 goto out;
318 }
319
320
321 ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0);
322 if (ret != EFI_SUCCESS) {
323 error(u"File not found\r\n");
324 goto out;
325 }
326
327 buffer_size = 0;
328 ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, NULL);
329 if (ret != EFI_BUFFER_TOO_SMALL) {
330 error(u"Can't get file info size\r\n");
331 goto out;
332 }
333 ret = bs->allocate_pool(EFI_LOADER_DATA, buffer_size, (void **)&info);
334 if (ret != EFI_SUCCESS) {
335 error(u"Out of memory\r\n");
336 goto out;
337 }
338 ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, info);
339 if (ret != EFI_SUCCESS) {
340 error(u"Can't get file info\r\n");
341 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)
347 error(u"Can't free memory pool\r\n");
348
349 ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
350 EFI_ACPI_RECLAIM_MEMORY,
351 pages, &addr);
352 if (ret != EFI_SUCCESS) {
353 error(u"Out of memory\r\n");
354 goto out;
355 }
356 dtb = (struct fdt_header *)(uintptr_t)addr;
357 ret = file->read(file, &buffer_size, dtb);
358 if (ret != EFI_SUCCESS) {
359 error(u"Can't read file\r\n");
360 goto out;
361 }
362
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
368 ret = bs->free_pages(addr, pages);
369 if (ret != EFI_SUCCESS)
370 error(u"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,
374 pages, &addr);
375 if (ret != EFI_SUCCESS) {
376 error(u"Out of memory\r\n");
377 goto out;
378 }
379 dtb = (struct fdt_header *)(uintptr_t)addr;
380 ret = file->setpos(file, 0);
381 if (ret != EFI_SUCCESS) {
382 error(u"Can't position file\r\n");
383 goto out;
384 }
385 ret = file->read(file, &buffer_size, dtb);
386 if (ret != EFI_SUCCESS) {
387 error(u"Can't read file\r\n");
388 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)
397 print(u"device-tree installed\r\n");
398 else
399 error(u"Device-tree fix-up failed\r\n");
400out:
401 if (addr) {
402 ret2 = bs->free_pages(addr, pages);
403 if (ret2 != EFI_SUCCESS)
404 error(u"Can't free memory pages\r\n");
405 }
406 if (file) {
407 ret2 = file->close(file);
408 if (ret2 != EFI_SUCCESS)
409 error(u"Can't close file\r\n");
410 }
411 if (root) {
412 ret2 = root->close(root);
413 if (ret2 != EFI_SUCCESS)
414 error(u"Can't close volume\r\n");
415 }
416 return ret;
417}
418
419
420
421
422
423
424
425efi_status_t do_save(u16 *filename)
426{
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;
431 efi_uintn_t ret;
432
433 dtb = get_dtb(systable);
434 if (!dtb) {
435 error(u"DTB not found\r\n");
436 return EFI_NOT_FOUND;
437 }
438 if (f2h(dtb->magic) != FDT_MAGIC) {
439 error(u"Wrong device tree magic\r\n");
440 return EFI_NOT_FOUND;
441 }
442 dtb_size = f2h(dtb->totalsize);
443
444 filename = skip_whitespace(filename);
445
446 ret = open_file_system(&file_system);
447 if (ret != EFI_SUCCESS)
448 return ret;
449
450
451 ret = file_system->open_volume(file_system, &root);
452 if (ret != EFI_SUCCESS) {
453 error(u"Failed to open volume\r\n");
454 return ret;
455 }
456
457 ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0);
458 if (ret == EFI_SUCCESS) {
459 file->close(file);
460 print(u"Overwrite existing file (y/n)? ");
461 ret = efi_input_yn();
462 print(u"\r\n");
463 if (ret != EFI_SUCCESS) {
464 root->close(root);
465 error(u"Aborted by user\r\n");
466 return ret;
467 }
468 }
469
470
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
476 ret = file->write(file, &dtb_size, dtb);
477 if (ret != EFI_SUCCESS)
478 error(u"Failed to write file\r\n");
479 file->close(file);
480 } else {
481 error(u"Failed to open file\r\n");
482 }
483 root->close(root);
484
485 if (ret == EFI_SUCCESS) {
486 print(filename);
487 print(u" written\r\n");
488 }
489
490 return ret;
491}
492
493
494
495
496
497
498
499
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);
513 print(u"DTB Dump\r\n========\r\n\r\n");
514 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
521 print(u"=> ");
522 ret = efi_input(command, sizeof(command));
523 if (ret == EFI_ABORTED)
524 break;
525 pos = skip_whitespace(command);
526 if (starts_with(pos, u"exit"))
527 break;
528 else if (starts_with(pos, u"load "))
529 do_load(pos + 5);
530 else if (starts_with(pos, u"save "))
531 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}
540