-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathefidebug.c
1673 lines (1485 loc) · 40.9 KB
/
efidebug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0+
/*
* UEFI Shell-like command
*
* Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
*/
#include <charset.h>
#include <command.h>
#include <dm/device.h>
#include <efi_dt_fixup.h>
#include <efi_load_initrd.h>
#include <efi_loader.h>
#include <efi_rng.h>
#include <efi_variable.h>
#include <exports.h>
#include <hexdump.h>
#include <log.h>
#include <malloc.h>
#include <mapmem.h>
#include <net.h>
#include <part.h>
#include <search.h>
#include <linux/ctype.h>
#include <linux/err.h>
#define BS systab.boottime
#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
/**
* do_efi_capsule_update() - process a capsule update
*
* @cmdtp: Command table
* @flag: Command flag
* @argc: Number of arguments
* @argv: Argument array
* Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
*
* Implement efidebug "capsule update" sub-command.
* process a capsule update.
*
* efidebug capsule update [-v] <capsule address>
*/
static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
int argc, char * const argv[])
{
struct efi_capsule_header *capsule;
int verbose = 0;
char *endp;
efi_status_t ret;
if (argc != 2 && argc != 3)
return CMD_RET_USAGE;
if (argc == 3) {
if (strcmp(argv[1], "-v"))
return CMD_RET_USAGE;
verbose = 1;
argc--;
argv++;
}
capsule = (typeof(capsule))hextoul(argv[1], &endp);
if (endp == argv[1]) {
printf("Invalid address: %s", argv[1]);
return CMD_RET_FAILURE;
}
if (verbose) {
printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
printf("Capsule flags: 0x%x\n", capsule->flags);
printf("Capsule header size: 0x%x\n", capsule->header_size);
printf("Capsule image size: 0x%x\n",
capsule->capsule_image_size);
}
ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0));
if (ret) {
printf("Cannot handle a capsule at %p\n", capsule);
return CMD_RET_FAILURE;
}
return CMD_RET_SUCCESS;
}
#ifdef CONFIG_EFI_CAPSULE_ON_DISK
static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
int argc, char * const argv[])
{
efi_status_t ret;
ret = efi_launch_capsules();
return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
}
#endif
/**
* do_efi_capsule_show() - show capsule information
*
* @cmdtp: Command table
* @flag: Command flag
* @argc: Number of arguments
* @argv: Argument array
* Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
*
* Implement efidebug "capsule show" sub-command.
* show capsule information.
*
* efidebug capsule show <capsule address>
*/
static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
int argc, char * const argv[])
{
struct efi_capsule_header *capsule;
char *endp;
if (argc != 2)
return CMD_RET_USAGE;
capsule = (typeof(capsule))hextoul(argv[1], &endp);
if (endp == argv[1]) {
printf("Invalid address: %s", argv[1]);
return CMD_RET_FAILURE;
}
printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
printf("Capsule flags: 0x%x\n", capsule->flags);
printf("Capsule header size: 0x%x\n", capsule->header_size);
printf("Capsule image size: 0x%x\n",
capsule->capsule_image_size);
return CMD_RET_SUCCESS;
}
#ifdef CONFIG_EFI_ESRT
#define EFI_ESRT_FW_TYPE_NUM 4
char *efi_fw_type_str[EFI_ESRT_FW_TYPE_NUM] = {"unknown", "system FW", "device FW",
"UEFI driver"};
#define EFI_ESRT_UPDATE_STATUS_NUM 9
char *efi_update_status_str[EFI_ESRT_UPDATE_STATUS_NUM] = {"success", "unsuccessful",
"insufficient resources", "incorrect version", "invalid format",
"auth error", "power event (AC)", "power event (batt)",
"unsatisfied dependencies"};
#define EFI_FW_TYPE_STR_GET(idx) (\
EFI_ESRT_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\
)
#define EFI_FW_STATUS_STR_GET(idx) (\
EFI_ESRT_UPDATE_STATUS_NUM > (idx) ? efi_update_status_str[(idx)] : "error"\
)
/**
* do_efi_capsule_esrt() - manage UEFI capsules
*
* @cmdtp: Command table
* @flag: Command flag
* @argc: Number of arguments
* @argv: Argument array
* Return: CMD_RET_SUCCESS on success,
* CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
*
* Implement efidebug "capsule esrt" sub-command.
* The prints the current ESRT table.
*
* efidebug capsule esrt
*/
static int do_efi_capsule_esrt(struct cmd_tbl *cmdtp, int flag,
int argc, char * const argv[])
{
struct efi_system_resource_table *esrt;
if (argc != 1)
return CMD_RET_USAGE;
esrt = efi_get_configuration_table(&efi_esrt_guid);
if (!esrt) {
log_info("ESRT: table not present\n");
return CMD_RET_SUCCESS;
}
printf("========================================\n");
printf("ESRT: fw_resource_count=%d\n", esrt->fw_resource_count);
printf("ESRT: fw_resource_count_max=%d\n", esrt->fw_resource_count_max);
printf("ESRT: fw_resource_version=%lld\n", esrt->fw_resource_version);
for (int idx = 0; idx < esrt->fw_resource_count; idx++) {
printf("[entry %d]==============================\n", idx);
printf("ESRT: fw_class=%pUL\n", &esrt->entries[idx].fw_class);
printf("ESRT: fw_type=%s\n", EFI_FW_TYPE_STR_GET(esrt->entries[idx].fw_type));
printf("ESRT: fw_version=%d\n", esrt->entries[idx].fw_version);
printf("ESRT: lowest_supported_fw_version=%d\n",
esrt->entries[idx].lowest_supported_fw_version);
printf("ESRT: capsule_flags=%d\n",
esrt->entries[idx].capsule_flags);
printf("ESRT: last_attempt_version=%d\n",
esrt->entries[idx].last_attempt_version);
printf("ESRT: last_attempt_status=%s\n",
EFI_FW_STATUS_STR_GET(esrt->entries[idx].last_attempt_status));
}
printf("========================================\n");
return CMD_RET_SUCCESS;
}
#endif /* CONFIG_EFI_ESRT */
/**
* do_efi_capsule_res() - show a capsule update result
*
* @cmdtp: Command table
* @flag: Command flag
* @argc: Number of arguments
* @argv: Argument array
* Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
*
* Implement efidebug "capsule result" sub-command.
* show a capsule update result.
* If result number is not specified, CapsuleLast will be shown.
*
* efidebug capsule result [<capsule result number>]
*/
static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
int argc, char * const argv[])
{
int capsule_id;
char *endp;
u16 var_name16[12];
efi_guid_t guid;
struct efi_capsule_result_variable_header *result = NULL;
efi_uintn_t size;
efi_status_t ret;
if (argc != 1 && argc != 2)
return CMD_RET_USAGE;
guid = efi_guid_capsule_report;
if (argc == 1) {
size = sizeof(var_name16);
ret = efi_get_variable_int(u"CapsuleLast", &guid, NULL,
&size, var_name16, NULL);
if (ret != EFI_SUCCESS) {
if (ret == EFI_NOT_FOUND)
printf("CapsuleLast doesn't exist\n");
else
printf("Failed to get CapsuleLast\n");
return CMD_RET_FAILURE;
}
printf("CapsuleLast is %ls\n", var_name16);
} else {
argc--;
argv++;
capsule_id = hextoul(argv[0], &endp);
if (capsule_id < 0 || capsule_id > 0xffff)
return CMD_RET_USAGE;
efi_create_indexed_name(var_name16, sizeof(var_name16),
"Capsule", capsule_id);
}
size = 0;
ret = efi_get_variable_int(var_name16, &guid, NULL, &size, NULL, NULL);
if (ret == EFI_BUFFER_TOO_SMALL) {
result = malloc(size);
if (!result)
return CMD_RET_FAILURE;
ret = efi_get_variable_int(var_name16, &guid, NULL, &size,
result, NULL);
}
if (ret != EFI_SUCCESS) {
free(result);
printf("Failed to get %ls\n", var_name16);
return CMD_RET_FAILURE;
}
printf("Result total size: 0x%x\n", result->variable_total_size);
printf("Capsule guid: %pUl\n", &result->capsule_guid);
printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
result->capsule_processed.year, result->capsule_processed.month,
result->capsule_processed.day, result->capsule_processed.hour,
result->capsule_processed.minute,
result->capsule_processed.second);
printf("Capsule status: 0x%lx\n", result->capsule_status);
free(result);
return CMD_RET_SUCCESS;
}
static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
"", ""),
U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
"", ""),
#ifdef CONFIG_EFI_ESRT
U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt,
"", ""),
#endif
#ifdef CONFIG_EFI_CAPSULE_ON_DISK
U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
"", ""),
#endif
U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
"", ""),
};
/**
* do_efi_capsule() - manage UEFI capsules
*
* @cmdtp: Command table
* @flag: Command flag
* @argc: Number of arguments
* @argv: Argument array
* Return: CMD_RET_SUCCESS on success,
* CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
*
* Implement efidebug "capsule" sub-command.
*/
static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
int argc, char * const argv[])
{
struct cmd_tbl *cp;
if (argc < 2)
return CMD_RET_USAGE;
argc--; argv++;
cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
ARRAY_SIZE(cmd_efidebug_capsule_sub));
if (!cp)
return CMD_RET_USAGE;
return cp->cmd(cmdtp, flag, argc, argv);
}
#endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
#define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
static const char spc[] = " ";
static const char sep[] = "================";
/**
* efi_get_driver_handle_info() - get information of UEFI driver
*
* @handle: Handle of UEFI device
* @driver_name: Driver name
* @image_path: Pointer to text of device path
* Return: 0 on success, -1 on failure
*
* Currently return no useful information as all UEFI drivers are
* built-in..
*/
static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
u16 **image_path)
{
struct efi_handler *handler;
struct efi_loaded_image *image;
efi_status_t ret;
/*
* driver name
* TODO: support EFI_COMPONENT_NAME2_PROTOCOL
*/
*driver_name = NULL;
/* image name */
ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
if (ret != EFI_SUCCESS) {
*image_path = NULL;
return 0;
}
image = handler->protocol_interface;
*image_path = efi_dp_str(image->file_path);
return 0;
}
/**
* do_efi_show_drivers() - show UEFI drivers
*
* @cmdtp: Command table
* @flag: Command flag
* @argc: Number of arguments
* @argv: Argument array
* Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
*
* Implement efidebug "drivers" sub-command.
* Show all UEFI drivers and their information.
*/
static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
int argc, char *const argv[])
{
efi_handle_t *handles;
efi_uintn_t num, i;
u16 *driver_name, *image_path_text;
efi_status_t ret;
ret = EFI_CALL(efi_locate_handle_buffer(
BY_PROTOCOL, &efi_guid_driver_binding_protocol,
NULL, &num, &handles));
if (ret != EFI_SUCCESS)
return CMD_RET_FAILURE;
if (!num)
return CMD_RET_SUCCESS;
printf("Driver%.*s Name Image Path\n",
EFI_HANDLE_WIDTH - 6, spc);
printf("%.*s ==================== ====================\n",
EFI_HANDLE_WIDTH, sep);
for (i = 0; i < num; i++) {
if (!efi_get_driver_handle_info(handles[i], &driver_name,
&image_path_text)) {
if (image_path_text)
printf("%p %-20ls %ls\n", handles[i],
driver_name, image_path_text);
else
printf("%p %-20ls <built-in>\n",
handles[i], driver_name);
efi_free_pool(driver_name);
efi_free_pool(image_path_text);
}
}
efi_free_pool(handles);
return CMD_RET_SUCCESS;
}
/**
* do_efi_show_handles() - show UEFI handles
*
* @cmdtp: Command table
* @flag: Command flag
* @argc: Number of arguments
* @argv: Argument array
* Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
*
* Implement efidebug "dh" sub-command.
* Show all UEFI handles and their information, currently all protocols
* added to handle.
*/
static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
int argc, char *const argv[])
{
efi_handle_t *handles;
efi_guid_t **guid;
efi_uintn_t num, count, i, j;
efi_status_t ret;
ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
&num, &handles));
if (ret != EFI_SUCCESS)
return CMD_RET_FAILURE;
if (!num)
return CMD_RET_SUCCESS;
for (i = 0; i < num; i++) {
struct efi_handler *handler;
printf("\n%p", handles[i]);
if (handles[i]->dev)
printf(" (%s)", handles[i]->dev->name);
printf("\n");
/* Print device path */
ret = efi_search_protocol(handles[i], &efi_guid_device_path,
&handler);
if (ret == EFI_SUCCESS)
printf(" %pD\n", handler->protocol_interface);
ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
&count));
/* Print other protocols */
for (j = 0; j < count; j++) {
if (guidcmp(guid[j], &efi_guid_device_path))
printf(" %pUs\n", guid[j]);
}
efi_free_pool(guid);
}
efi_free_pool(handles);
return CMD_RET_SUCCESS;
}
/**
* do_efi_show_images() - show UEFI images
*
* @cmdtp: Command table
* @flag: Command flag
* @argc: Number of arguments
* @argv: Argument array
* Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
*
* Implement efidebug "images" sub-command.
* Show all UEFI loaded images and their information.
*/
static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
int argc, char *const argv[])
{
efi_print_image_infos(NULL);
return CMD_RET_SUCCESS;
}
static const char * const efi_mem_type_string[] = {
[EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
[EFI_LOADER_CODE] = "LOADER CODE",
[EFI_LOADER_DATA] = "LOADER DATA",
[EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
[EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
[EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
[EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
[EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
[EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
[EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
[EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
[EFI_MMAP_IO] = "IO",
[EFI_MMAP_IO_PORT] = "IO PORT",
[EFI_PAL_CODE] = "PAL",
[EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
};
static const struct efi_mem_attrs {
const u64 bit;
const char *text;
} efi_mem_attrs[] = {
{EFI_MEMORY_UC, "UC"},
{EFI_MEMORY_UC, "UC"},
{EFI_MEMORY_WC, "WC"},
{EFI_MEMORY_WT, "WT"},
{EFI_MEMORY_WB, "WB"},
{EFI_MEMORY_UCE, "UCE"},
{EFI_MEMORY_WP, "WP"},
{EFI_MEMORY_RP, "RP"},
{EFI_MEMORY_XP, "WP"},
{EFI_MEMORY_NV, "NV"},
{EFI_MEMORY_MORE_RELIABLE, "REL"},
{EFI_MEMORY_RO, "RO"},
{EFI_MEMORY_SP, "SP"},
{EFI_MEMORY_RUNTIME, "RT"},
};
/**
* print_memory_attributes() - print memory map attributes
*
* @attributes: Attribute value
*
* Print memory map attributes
*/
static void print_memory_attributes(u64 attributes)
{
int sep, i;
for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
if (attributes & efi_mem_attrs[i].bit) {
if (sep) {
putc('|');
} else {
putc(' ');
sep = 1;
}
puts(efi_mem_attrs[i].text);
}
}
#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
/**
* do_efi_show_memmap() - show UEFI memory map
*
* @cmdtp: Command table
* @flag: Command flag
* @argc: Number of arguments
* @argv: Argument array
* Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
*
* Implement efidebug "memmap" sub-command.
* Show UEFI memory map.
*/
static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
int argc, char *const argv[])
{
struct efi_mem_desc *memmap, *map;
efi_uintn_t map_size;
const char *type;
int i;
efi_status_t ret;
ret = efi_get_memory_map_alloc(&map_size, &memmap);
if (ret != EFI_SUCCESS)
return CMD_RET_FAILURE;
printf("Type Start%.*s End%.*s Attributes\n",
EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
printf("================ %.*s %.*s ==========\n",
EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
/*
* Coverity check: dereferencing null pointer "map."
* This is a false positive as memmap will always be
* populated by allocate_pool() above.
*/
for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
if (map->type < ARRAY_SIZE(efi_mem_type_string))
type = efi_mem_type_string[map->type];
else
type = "(unknown)";
printf("%-16s %.*llx-%.*llx", type,
EFI_PHYS_ADDR_WIDTH,
(u64)map_to_sysmem((void *)(uintptr_t)
map->physical_start),
EFI_PHYS_ADDR_WIDTH,
(u64)map_to_sysmem((void *)(uintptr_t)
(map->physical_start +
map->num_pages * EFI_PAGE_SIZE)));
print_memory_attributes(map->attribute);
putc('\n');
}
efi_free_pool(memmap);
return CMD_RET_SUCCESS;
}
/**
* do_efi_show_tables() - show UEFI configuration tables
*
* @cmdtp: Command table
* @flag: Command flag
* @argc: Number of arguments
* @argv: Argument array
* Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
*
* Implement efidebug "tables" sub-command.
* Show UEFI configuration tables.
*/
static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
int argc, char *const argv[])
{
efi_show_tables(&systab);
return CMD_RET_SUCCESS;
}
/**
* enum efi_lo_dp_part - part of device path in load option
*/
enum efi_lo_dp_part {
/** @EFI_LO_DP_PART_BINARY: binary */
EFI_LO_DP_PART_BINARY,
/** @EFI_LO_DP_PART_INITRD: initial RAM disk */
EFI_LO_DP_PART_INITRD,
/** @EFI_LP_DP_PART_FDT: device-tree */
EFI_LP_DP_PART_FDT,
};
/**
* create_lo_dp() - create a special device path for our Boot### option
*
* @dev: device
* @part: disk partition
* @file: filename
* @shortform: create short form device path
* @type: part of device path to be created
* Return: pointer to the device path or ERR_PTR
*/
static
struct efi_device_path *create_lo_dp_part(const char *dev, const char *part,
const char *file, bool shortform,
enum efi_lo_dp_part type)
{
struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL, *short_fp = NULL;
struct efi_device_path *dp = NULL;
const struct efi_device_path *dp_prefix;
efi_status_t ret;
const struct efi_lo_dp_prefix fdt_dp = {
.vendor = {
{
DEVICE_PATH_TYPE_MEDIA_DEVICE,
DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
sizeof(fdt_dp.vendor),
},
EFI_FDT_GUID,
},
.end = {
DEVICE_PATH_TYPE_END,
DEVICE_PATH_SUB_TYPE_END,
sizeof(fdt_dp.end),
}
};
const struct efi_lo_dp_prefix initrd_dp = {
.vendor = {
{
DEVICE_PATH_TYPE_MEDIA_DEVICE,
DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
sizeof(initrd_dp.vendor),
},
EFI_INITRD_MEDIA_GUID,
},
.end = {
DEVICE_PATH_TYPE_END,
DEVICE_PATH_SUB_TYPE_END,
sizeof(initrd_dp.end),
}
};
switch (type) {
case EFI_LO_DP_PART_INITRD:
dp_prefix = &initrd_dp.vendor.dp;
break;
case EFI_LP_DP_PART_FDT:
dp_prefix = &fdt_dp.vendor.dp;
break;
default:
dp_prefix = NULL;
break;
}
ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp);
if (ret != EFI_SUCCESS) {
printf("Cannot create device path for \"%s %s\"\n", part, file);
goto out;
}
if (shortform)
short_fp = efi_dp_shorten(tmp_fp);
if (!short_fp)
short_fp = tmp_fp;
dp = efi_dp_concat(dp_prefix, short_fp, 0);
out:
efi_free_pool(tmp_dp);
efi_free_pool(tmp_fp);
return dp;
}
/**
* efi_boot_add_uri() - set URI load option
*
* @argc: Number of arguments
* @argv: Argument array
* @var_name16: variable name buffer
* @var_name16_size: variable name buffer size
* @lo: pointer to the load option
* @file_path: buffer to set the generated device path pointer
* @fp_size: file_path size
* Return: CMD_RET_SUCCESS on success,
* CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
*/
static int efi_boot_add_uri(int argc, char *const argv[], u16 *var_name16,
size_t var_name16_size, struct efi_load_option *lo,
struct efi_device_path **file_path,
efi_uintn_t *fp_size)
{
int id;
char *pos;
char *endp;
u16 *label;
efi_uintn_t uridp_len;
struct efi_device_path_uri *uridp;
if (argc < 3 || lo->label)
return CMD_RET_USAGE;
id = (int)hextoul(argv[1], &endp);
if (*endp != '\0' || id > 0xffff)
return CMD_RET_USAGE;
label = efi_convert_string(argv[2]);
if (!label)
return CMD_RET_FAILURE;
if (!wget_validate_uri(argv[3])) {
printf("ERROR: invalid URI\n");
return CMD_RET_FAILURE;
}
efi_create_indexed_name(var_name16, var_name16_size, "Boot", id);
lo->label = label;
uridp_len = sizeof(struct efi_device_path) + strlen(argv[3]) + 1;
uridp = efi_alloc(uridp_len + sizeof(END));
if (!uridp) {
log_err("Out of memory\n");
return CMD_RET_FAILURE;
}
uridp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
uridp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_URI;
uridp->dp.length = uridp_len;
strcpy(uridp->uri, argv[3]);
pos = (char *)uridp + uridp_len;
memcpy(pos, &END, sizeof(END));
*file_path = &uridp->dp;
*fp_size += uridp_len + sizeof(END);
return CMD_RET_SUCCESS;
}
/**
* do_efi_boot_add() - set UEFI load option
*
* @cmdtp: Command table
* @flag: Command flag
* @argc: Number of arguments
* @argv: Argument array
* Return: CMD_RET_SUCCESS on success,
* CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
*
* Implement efidebug "boot add" sub-command. Create or change UEFI load option.
*
* efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file>
* -i <file> <interface2> <devnum2>[:<part>] <initrd>
* -s '<options>'
*/
static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
int argc, char *const argv[])
{
int id;
char *endp;
u16 var_name16[9];
efi_guid_t guid;
u16 *label;
struct efi_device_path *file_path = NULL;
struct efi_device_path *initrd_dp = NULL;
struct efi_device_path *fdt_dp = NULL;
struct efi_load_option lo;
void *data = NULL;
efi_uintn_t size;
efi_uintn_t fp_size = 0;
efi_status_t ret;
int r = CMD_RET_SUCCESS;
guid = efi_global_variable_guid;
/* attributes */
lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
lo.optional_data = NULL;
lo.label = NULL;
argc--;
argv++; /* 'add' */
for (; argc > 0; argc--, argv++) {
int shortform;
if (*argv[0] != '-' || strlen(argv[0]) != 2) {
r = CMD_RET_USAGE;
goto out;
}
shortform = 0;
switch (argv[0][1]) {
case 'b':
shortform = 1;
/* fallthrough */
case 'B':
if (argc < 5 || lo.label) {
r = CMD_RET_USAGE;
goto out;
}
id = (int)hextoul(argv[1], &endp);
if (*endp != '\0' || id > 0xffff)
return CMD_RET_USAGE;
efi_create_indexed_name(var_name16, sizeof(var_name16),
"Boot", id);
/* label */
label = efi_convert_string(argv[2]);
if (!label)
return CMD_RET_FAILURE;
lo.label = label; /* label will be changed below */
/* file path */
file_path = create_lo_dp_part(argv[3], argv[4], argv[5],
shortform,
EFI_LO_DP_PART_BINARY);
argc -= 5;
argv += 5;
break;
case 'd':
shortform = 1;
fallthrough;
case 'D':
if (argc < 3 || fdt_dp) {
r = CMD_RET_USAGE;
goto out;
}
fdt_dp = create_lo_dp_part(argv[1], argv[2], argv[3],
shortform,
EFI_LP_DP_PART_FDT);
if (!fdt_dp) {
printf("Cannot add a device-tree\n");
r = CMD_RET_FAILURE;
goto out;
}
argc -= 3;
argv += 3;
break;
case 'i':
shortform = 1;
/* fallthrough */
case 'I':
if (argc < 3 || initrd_dp) {
r = CMD_RET_USAGE;
goto out;
}
initrd_dp = create_lo_dp_part(argv[1], argv[2], argv[3],
shortform,
EFI_LO_DP_PART_INITRD);
if (!initrd_dp) {
printf("Cannot add an initrd\n");
r = CMD_RET_FAILURE;
goto out;
}
argc -= 3;
argv += 3;
break;
case 's':
if (argc < 1 || lo.optional_data) {
r = CMD_RET_USAGE;
goto out;
}
lo.optional_data = (const u8 *)argv[1];
argc -= 1;
argv += 1;
break;
case 'u':
if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT)) {
r = efi_boot_add_uri(argc, argv, var_name16,
sizeof(var_name16), &lo,
&file_path, &fp_size);
if (r != CMD_RET_SUCCESS)
goto out;
argc -= 3;
argv += 3;
} else{
r = CMD_RET_USAGE;
goto out;
}
break;
default:
r = CMD_RET_USAGE;
goto out;
}
}
if (!file_path) {
printf("Missing binary\n");
r = CMD_RET_USAGE;
goto out;
}
ret = efi_load_option_dp_join(&file_path, &fp_size, initrd_dp, fdt_dp);
if (ret != EFI_SUCCESS) {
printf("Cannot create final device path\n");
r = CMD_RET_FAILURE;
goto out;
}
lo.file_path = file_path;
lo.file_path_length = fp_size;
size = efi_serialize_load_option(&lo, (u8 **)&data);
if (!size) {
r = CMD_RET_FAILURE;
goto out;
}
ret = efi_set_variable_int(var_name16, &guid,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS |
EFI_VARIABLE_RUNTIME_ACCESS,
size, data, false);
if (ret != EFI_SUCCESS) {
printf("Cannot set %ls\n", var_name16);
r = CMD_RET_FAILURE;
}
out:
free(data);
efi_free_pool(initrd_dp);
efi_free_pool(fdt_dp);
efi_free_pool(file_path);
free(lo.label);
return r;
}