-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheficonfig.c
2502 lines (2144 loc) · 61.9 KB
/
eficonfig.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+
/*
* Menu-driven UEFI Variable maintenance
*
* Copyright (c) 2022 Masahisa Kojima, Linaro Limited
*/
#include <ansi.h>
#include <common.h>
#include <charset.h>
#include <efi_loader.h>
#include <efi_load_initrd.h>
#include <efi_config.h>
#include <efi_variable.h>
#include <log.h>
#include <malloc.h>
#include <menu.h>
#include <sort.h>
#include <watchdog.h>
#include <asm/unaligned.h>
#include <linux/delay.h>
static struct efi_simple_text_input_protocol *cin;
#define EFICONFIG_DESCRIPTION_MAX 32
#define EFICONFIG_OPTIONAL_DATA_MAX 64
/**
* struct eficonfig_filepath_info - structure to be used to store file path
*
* @name: file or directory name
* @list: list structure
*/
struct eficonfig_filepath_info {
char *name;
struct list_head list;
};
/**
* struct eficonfig_boot_option - structure to be used for updating UEFI boot option
*
* @file_info: user selected file info
* @initrd_info: user selected initrd file info
* @boot_index: index of the boot option
* @description: pointer to the description string
* @optional_data: pointer to the optional_data
* @edit_completed: flag indicates edit complete
*/
struct eficonfig_boot_option {
struct eficonfig_select_file_info file_info;
struct eficonfig_select_file_info initrd_info;
unsigned int boot_index;
u16 *description;
u16 *optional_data;
bool edit_completed;
};
/**
* struct eficonfig_volume_entry_data - structure to be used to store volume info
*
* @file_info: pointer to file info structure
* @v: pointer to the protocol interface
* @dp: pointer to the device path
*/
struct eficonfig_volume_entry_data {
struct eficonfig_select_file_info *file_info;
struct efi_simple_file_system_protocol *v;
struct efi_device_path *dp;
};
/**
* struct eficonfig_file_entry_data - structure to be used to store file info
*
* @file_info: pointer to file info structure
* @is_directory: flag to identify the directory or file
* @file_name: name of directory or file
*/
struct eficonfig_file_entry_data {
struct eficonfig_select_file_info *file_info;
bool is_directory;
char *file_name;
};
/**
* struct eficonfig_boot_selection_data - structure to be used to select the boot option entry
*
* @boot_index: index of the boot option
* @selected: pointer to store the selected index in the BootOrder variable
*/
struct eficonfig_boot_selection_data {
u16 boot_index;
int *selected;
};
/**
* struct eficonfig_boot_order - structure to be used to update BootOrder variable
*
* @num: index in the menu entry
* @description: pointer to the description string
* @boot_index: boot option index
* @active: flag to include the boot option into BootOrder variable
* @list: list structure
*/
struct eficonfig_boot_order {
u32 num;
u16 *description;
u32 boot_index;
bool active;
struct list_head list;
};
/**
* eficonfig_print_msg() - print message
*
* display the message to the user, user proceeds the screen
* with any key press.
*
* @items: pointer to the structure of each menu entry
* @count: the number of menu entry
* @menu_header: pointer to the menu header string
* Return: status code
*/
void eficonfig_print_msg(char *msg)
{
/* Flush input */
while (tstc())
getchar();
printf(ANSI_CURSOR_HIDE
ANSI_CLEAR_CONSOLE
ANSI_CURSOR_POSITION
"%s\n\n Press any key to continue", 3, 4, msg);
getchar();
}
/**
* eficonfig_print_entry() - print each menu entry
*
* @data: pointer to the data associated with each menu entry
*/
static void eficonfig_print_entry(void *data)
{
struct eficonfig_entry *entry = data;
int reverse = (entry->efi_menu->active == entry->num);
/* TODO: support scroll or page for many entries */
/*
* Move cursor to line where the entry will be drawn (entry->num)
* First 3 lines(menu header) + 1 empty line
*/
printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
if (reverse)
puts(ANSI_COLOR_REVERSE);
printf("%s", entry->title);
if (reverse)
puts(ANSI_COLOR_RESET);
}
/**
* eficonfig_display_statusline() - print status line
*
* @m: pointer to the menu structure
*/
static void eficonfig_display_statusline(struct menu *m)
{
struct eficonfig_entry *entry;
if (menu_default_choice(m, (void *)&entry) < 0)
return;
printf(ANSI_CURSOR_POSITION
"\n%s\n"
ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
" Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"
ANSI_CLEAR_LINE_TO_END ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
1, 1, entry->efi_menu->menu_header, entry->efi_menu->count + 5, 1,
entry->efi_menu->count + 6, 1, entry->efi_menu->count + 7, 1);
}
/**
* eficonfig_choice_entry() - user key input handler
*
* @data: pointer to the efimenu structure
* Return: key string to identify the selected entry
*/
static char *eficonfig_choice_entry(void *data)
{
int esc = 0;
struct list_head *pos, *n;
struct eficonfig_entry *entry;
enum bootmenu_key key = KEY_NONE;
struct efimenu *efi_menu = data;
while (1) {
bootmenu_loop((struct bootmenu_data *)efi_menu, &key, &esc);
switch (key) {
case KEY_UP:
if (efi_menu->active > 0)
--efi_menu->active;
/* no menu key selected, regenerate menu */
return NULL;
case KEY_DOWN:
if (efi_menu->active < efi_menu->count - 1)
++efi_menu->active;
/* no menu key selected, regenerate menu */
return NULL;
case KEY_SELECT:
list_for_each_safe(pos, n, &efi_menu->list) {
entry = list_entry(pos, struct eficonfig_entry, list);
if (entry->num == efi_menu->active)
return entry->key;
}
break;
case KEY_QUIT:
/* Quit by choosing the last entry */
entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
return entry->key;
default:
/* Pressed key is not valid, no need to regenerate the menu */
break;
}
}
}
/**
* eficonfig_destroy() - destroy efimenu
*
* @efi_menu: pointer to the efimenu structure
*/
void eficonfig_destroy(struct efimenu *efi_menu)
{
struct list_head *pos, *n;
struct eficonfig_entry *entry;
if (!efi_menu)
return;
list_for_each_safe(pos, n, &efi_menu->list) {
entry = list_entry(pos, struct eficonfig_entry, list);
free(entry->title);
list_del(&entry->list);
free(entry);
}
free(efi_menu->menu_header);
free(efi_menu);
}
/**
* eficonfig_process_quit() - callback function for "Quit" entry
*
* @data: pointer to the data
* Return: status code
*/
efi_status_t eficonfig_process_quit(void *data)
{
return EFI_ABORTED;
}
/**
* append_entry() - append menu item
*
* @efi_menu: pointer to the efimenu structure
* @title: pointer to the entry title
* @func: callback of each entry
* @data: pointer to the data to be passed to each entry callback
* Return: status code
*/
static efi_status_t append_entry(struct efimenu *efi_menu,
char *title, eficonfig_entry_func func, void *data)
{
struct eficonfig_entry *entry;
if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
return EFI_OUT_OF_RESOURCES;
entry = calloc(1, sizeof(struct eficonfig_entry));
if (!entry)
return EFI_OUT_OF_RESOURCES;
entry->title = title;
sprintf(entry->key, "%d", efi_menu->count);
entry->efi_menu = efi_menu;
entry->func = func;
entry->data = data;
entry->num = efi_menu->count++;
list_add_tail(&entry->list, &efi_menu->list);
return EFI_SUCCESS;
}
/**
* append_quit_entry() - append quit entry
*
* @efi_menu: pointer to the efimenu structure
* Return: status code
*/
static efi_status_t append_quit_entry(struct efimenu *efi_menu)
{
char *title;
efi_status_t ret;
title = strdup("Quit");
if (!title)
return EFI_OUT_OF_RESOURCES;
ret = append_entry(efi_menu, title, eficonfig_process_quit, NULL);
if (ret != EFI_SUCCESS)
free(title);
return ret;
}
/**
* eficonfig_create_fixed_menu() - create fixed entry menu structure
*
* @items: pointer to the menu entry item
* @count: the number of menu entry
* Return: pointer to the efimenu structure
*/
void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
{
u32 i;
char *title;
efi_status_t ret;
struct efimenu *efi_menu;
const struct eficonfig_item *iter = items;
efi_menu = calloc(1, sizeof(struct efimenu));
if (!efi_menu)
return NULL;
INIT_LIST_HEAD(&efi_menu->list);
for (i = 0; i < count; i++, iter++) {
title = strdup(iter->title);
if (!title)
goto out;
ret = append_entry(efi_menu, title, iter->func, iter->data);
if (ret != EFI_SUCCESS) {
free(title);
goto out;
}
}
return efi_menu;
out:
eficonfig_destroy(efi_menu);
return NULL;
}
/**
* eficonfig_process_common() - main handler for UEFI menu
*
* Construct the structures required to show the menu, then handle
* the user input interacting with u-boot menu functions.
*
* @efi_menu: pointer to the efimenu structure
* @menu_header: pointer to the menu header string
* Return: status code
*/
efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_header)
{
struct menu *menu;
void *choice = NULL;
struct list_head *pos, *n;
struct eficonfig_entry *entry;
efi_status_t ret = EFI_SUCCESS;
if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
return EFI_OUT_OF_RESOURCES;
efi_menu->delay = -1;
efi_menu->active = 0;
if (menu_header) {
efi_menu->menu_header = strdup(menu_header);
if (!efi_menu->menu_header)
return EFI_OUT_OF_RESOURCES;
}
menu = menu_create(NULL, 0, 1, eficonfig_display_statusline,
eficonfig_print_entry, eficonfig_choice_entry,
efi_menu);
if (!menu)
return EFI_INVALID_PARAMETER;
list_for_each_safe(pos, n, &efi_menu->list) {
entry = list_entry(pos, struct eficonfig_entry, list);
if (!menu_item_add(menu, entry->key, entry)) {
ret = EFI_INVALID_PARAMETER;
goto out;
}
}
entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
if (entry)
menu_default_set(menu, entry->key);
printf(ANSI_CURSOR_HIDE
ANSI_CLEAR_CONSOLE
ANSI_CURSOR_POSITION, 1, 1);
if (menu_get_choice(menu, &choice)) {
entry = choice;
if (entry->func)
ret = entry->func(entry->data);
}
out:
menu_destroy(menu);
printf(ANSI_CLEAR_CONSOLE
ANSI_CURSOR_POSITION
ANSI_CURSOR_SHOW, 1, 1);
return ret;
}
/**
* eficonfig_volume_selected() - handler of volume selection
*
* @data: pointer to the data of selected entry
* Return: status code
*/
static efi_status_t eficonfig_volume_selected(void *data)
{
struct eficonfig_volume_entry_data *info = data;
if (info) {
info->file_info->current_volume = info->v;
info->file_info->dp_volume = info->dp;
}
return EFI_SUCCESS;
}
/**
* create_selected_device_path() - create device path
*
* @file_info: pointer to the selected file information
* Return:
* device path or NULL. Caller must free the returned value
*/
static
struct efi_device_path *create_selected_device_path(struct eficonfig_select_file_info *file_info)
{
char *p;
void *buf;
efi_uintn_t fp_size;
struct efi_device_path *dp;
struct efi_device_path_file_path *fp;
fp_size = sizeof(struct efi_device_path) +
((u16_strlen(file_info->current_path) + 1) * sizeof(u16));
buf = calloc(1, fp_size + sizeof(END));
if (!buf)
return NULL;
fp = buf;
fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
fp->dp.length = (u16)fp_size;
u16_strcpy(fp->str, file_info->current_path);
p = buf;
p += fp_size;
*((struct efi_device_path *)p) = END;
dp = efi_dp_append(file_info->dp_volume, (struct efi_device_path *)buf);
free(buf);
return dp;
}
/**
* eficonfig_file_selected() - handler of file selection
*
* @data: pointer to the data of selected entry
* Return: status code
*/
static efi_status_t eficonfig_file_selected(void *data)
{
u16 *tmp;
struct eficonfig_file_entry_data *info = data;
if (!info)
return EFI_INVALID_PARAMETER;
if (!strcmp(info->file_name, "..")) {
struct eficonfig_filepath_info *iter;
struct list_head *pos, *n;
int is_last;
char *filepath;
tmp = info->file_info->current_path;
memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
if (!filepath)
return EFI_OUT_OF_RESOURCES;
list_for_each_safe(pos, n, &info->file_info->filepath_list) {
iter = list_entry(pos, struct eficonfig_filepath_info, list);
is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
if (is_last) {
list_del(&iter->list);
free(iter->name);
free(iter);
break;
}
strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
}
utf8_utf16_strcpy(&tmp, filepath);
} else {
size_t new_len;
struct eficonfig_filepath_info *filepath_info;
new_len = u16_strlen(info->file_info->current_path) +
strlen(info->file_name);
if (new_len >= EFICONFIG_FILE_PATH_MAX) {
eficonfig_print_msg("File path is too long!");
return EFI_INVALID_PARAMETER;
}
tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
utf8_utf16_strcpy(&tmp, info->file_name);
filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
if (!filepath_info)
return EFI_OUT_OF_RESOURCES;
filepath_info->name = strdup(info->file_name);
if (!filepath_info->name) {
free(filepath_info);
return EFI_OUT_OF_RESOURCES;
}
list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
if (!info->is_directory)
info->file_info->file_selected = true;
}
return EFI_SUCCESS;
}
/**
* eficonfig_select_volume() - construct the volume selection menu
*
* @file_info: pointer to the file selection structure
* Return: status code
*/
static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
{
u32 i;
efi_status_t ret;
efi_uintn_t count;
struct efimenu *efi_menu;
struct list_head *pos, *n;
struct efi_handler *handler;
struct eficonfig_entry *entry;
struct efi_device_path *device_path;
efi_handle_t *volume_handles = NULL;
struct efi_simple_file_system_protocol *v;
ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
NULL, &count, (efi_handle_t **)&volume_handles);
if (ret != EFI_SUCCESS) {
eficonfig_print_msg("No block device found!");
return ret;
}
efi_menu = calloc(1, sizeof(struct efimenu));
if (!efi_menu)
return EFI_OUT_OF_RESOURCES;
INIT_LIST_HEAD(&efi_menu->list);
for (i = 0; i < count; i++) {
char *devname;
struct efi_block_io *block_io;
struct eficonfig_volume_entry_data *info;
if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
break;
ret = efi_search_protocol(volume_handles[i],
&efi_simple_file_system_protocol_guid, &handler);
if (ret != EFI_SUCCESS)
continue;
ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (ret != EFI_SUCCESS)
continue;
ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
if (ret != EFI_SUCCESS)
continue;
ret = efi_protocol_open(handler, (void **)&device_path,
efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (ret != EFI_SUCCESS)
continue;
ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
if (ret != EFI_SUCCESS)
continue;
ret = efi_protocol_open(handler, (void **)&block_io,
efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (ret != EFI_SUCCESS)
continue;
info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
if (!info) {
ret = EFI_OUT_OF_RESOURCES;
goto out;
}
devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
if (!devname) {
free(info);
ret = EFI_OUT_OF_RESOURCES;
goto out;
}
ret = efi_disk_get_device_name(volume_handles[i], devname,
BOOTMENU_DEVICE_NAME_MAX);
if (ret != EFI_SUCCESS) {
free(info);
goto out;
}
info->v = v;
info->dp = device_path;
info->file_info = file_info;
ret = append_entry(efi_menu, devname, eficonfig_volume_selected, info);
if (ret != EFI_SUCCESS) {
free(info);
goto out;
}
}
ret = append_quit_entry(efi_menu);
if (ret != EFI_SUCCESS)
goto out;
ret = eficonfig_process_common(efi_menu, " ** Select Volume **");
out:
efi_free_pool(volume_handles);
list_for_each_safe(pos, n, &efi_menu->list) {
entry = list_entry(pos, struct eficonfig_entry, list);
free(entry->data);
}
eficonfig_destroy(efi_menu);
return ret;
}
/**
* sort_file() - sort the file name in ascii order
*
* @data1: pointer to the file entry data
* @data2: pointer to the file entry data
* Return: -1 if the data1 file name is less than data2 file name,
* 0 if both file name match,
* 1 if the data1 file name is greater thant data2 file name.
*/
static int sort_file(const void *arg1, const void *arg2)
{
const struct eficonfig_file_entry_data *data1, *data2;
data1 = *((const struct eficonfig_file_entry_data **)arg1);
data2 = *((const struct eficonfig_file_entry_data **)arg2);
return strcasecmp(data1->file_name, data2->file_name);
}
/**
* eficonfig_create_file_entry() - construct the file menu entry
*
* @efi_menu: pointer to the efimenu structure
* @count: number of the directory and file
* @tmp_infos: pointer to the entry data array
* @f: pointer to the file handle
* @buf: pointer to the buffer to store the directory information
* @file_info: pointer to the file selection structure
* Return: status code
*/
static efi_status_t
eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
struct eficonfig_file_entry_data **tmp_infos,
struct efi_file_handle *f, struct efi_file_info *buf,
struct eficonfig_select_file_info *file_info)
{
char *name, *p;
efi_uintn_t len;
efi_status_t ret;
u32 i, entry_num = 0;
struct eficonfig_file_entry_data *info;
efi_file_setpos_int(f, 0);
/* Read directory and construct menu structure */
for (i = 0; i < count; i++) {
if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
break;
len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
ret = efi_file_read_int(f, &len, buf);
if (ret != EFI_SUCCESS || len == 0)
break;
info = calloc(1, sizeof(struct eficonfig_file_entry_data));
if (!info) {
ret = EFI_OUT_OF_RESOURCES;
goto out;
}
/* append '\\' at the end of directory name */
name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
if (!name) {
ret = EFI_OUT_OF_RESOURCES;
free(info);
goto out;
}
p = name;
utf16_utf8_strcpy(&p, buf->file_name);
if (buf->attribute & EFI_FILE_DIRECTORY) {
/* filter out u'.' */
if (!u16_strcmp(buf->file_name, u".")) {
free(info);
free(name);
continue;
}
name[u16_strlen(buf->file_name)] = '\\';
info->is_directory = true;
}
info->file_name = name;
info->file_info = file_info;
tmp_infos[entry_num++] = info;
}
qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
(int (*)(const void *, const void *))sort_file);
for (i = 0; i < entry_num; i++) {
ret = append_entry(efi_menu, tmp_infos[i]->file_name,
eficonfig_file_selected, tmp_infos[i]);
if (ret != EFI_SUCCESS)
goto out;
}
out:
return ret;
}
/**
* eficonfig_select_file() - construct the file selection menu
*
* @file_info: pointer to the file selection structure
* @root: pointer to the file handle
* Return: status code
*/
static efi_status_t eficonfig_select_file(struct eficonfig_select_file_info *file_info,
struct efi_file_handle *root)
{
u32 count = 0, i;
efi_uintn_t len;
efi_status_t ret;
struct efimenu *efi_menu;
struct efi_file_handle *f;
struct efi_file_info *buf;
struct eficonfig_file_entry_data **tmp_infos;
buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
if (!buf)
return EFI_OUT_OF_RESOURCES;
while (!file_info->file_selected) {
efi_menu = calloc(1, sizeof(struct efimenu));
if (!efi_menu) {
ret = EFI_OUT_OF_RESOURCES;
goto out;
}
INIT_LIST_HEAD(&efi_menu->list);
ret = efi_file_open_int(root, &f, file_info->current_path, EFI_FILE_MODE_READ, 0);
if (ret != EFI_SUCCESS) {
eficonfig_print_msg("Reading volume failed!");
free(efi_menu);
ret = EFI_ABORTED;
goto out;
}
/* Count the number of directory entries */
for (;;) {
len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
ret = efi_file_read_int(f, &len, buf);
if (ret != EFI_SUCCESS || len == 0)
break;
count++;
}
/* allocate array to sort the entry */
tmp_infos = calloc(count, sizeof(*tmp_infos));
if (!tmp_infos) {
ret = EFI_OUT_OF_RESOURCES;
goto err;
}
ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
f, buf, file_info);
if (ret != EFI_SUCCESS)
goto err;
ret = append_quit_entry(efi_menu);
if (ret != EFI_SUCCESS)
goto err;
ret = eficonfig_process_common(efi_menu, " ** Select File **");
err:
efi_file_close_int(f);
eficonfig_destroy(efi_menu);
if (tmp_infos) {
for (i = 0; i < count; i++)
free(tmp_infos[i]);
}
free(tmp_infos);
if (ret != EFI_SUCCESS)
break;
}
out:
free(buf);
return ret;
}
/**
* handle_user_input() - handle user input
*
* @buf: pointer to the buffer
* @buf_size: size of the buffer
* @cursor_col: cursor column for user input
* @msg: pointer to the string to display
* Return: status code
*/
static efi_status_t handle_user_input(u16 *buf, int buf_size,
int cursor_col, char *msg)
{
u16 *tmp;
efi_status_t ret;
printf(ANSI_CLEAR_CONSOLE
ANSI_CURSOR_POSITION
"%s"
ANSI_CURSOR_POSITION
" Press ENTER to complete, ESC/CTRL+C to quit",
0, 1, msg, 8, 1);
/* tmp is used to accept user cancel */
tmp = calloc(1, buf_size * sizeof(u16));
if (!tmp)
return EFI_OUT_OF_RESOURCES;
ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
if (ret == EFI_SUCCESS)
u16_strcpy(buf, tmp);
free(tmp);
/* to stay the parent menu */
ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
return ret;
}
/**
* eficonfig_boot_add_enter_description() - handle user input for description
*
* @data: pointer to the internal boot option structure
* Return: status code
*/
static efi_status_t eficonfig_boot_add_enter_description(void *data)
{
struct eficonfig_boot_option *bo = data;
return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
"\n ** Edit Description **\n"
"\n"
" enter description: ");
}
/**
* eficonfig_boot_add_optional_data() - handle user input for optional data
*
* @data: pointer to the internal boot option structure
* Return: status code
*/
static efi_status_t eficonfig_boot_add_optional_data(void *data)
{
struct eficonfig_boot_option *bo = data;
return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
"\n ** Edit Optional Data **\n"
"\n"
" enter optional data:");
}
/**
* eficonfig_boot_edit_save() - handler to save the boot option
*
* @data: pointer to the internal boot option structure
* Return: status code
*/
static efi_status_t eficonfig_boot_edit_save(void *data)
{
struct eficonfig_boot_option *bo = data;
if (u16_strlen(bo->description) == 0) {
eficonfig_print_msg("Boot Description is empty!");
bo->edit_completed = false;
return EFI_NOT_READY;
}
if (u16_strlen(bo->file_info.current_path) == 0) {
eficonfig_print_msg("File is not selected!");
bo->edit_completed = false;
return EFI_NOT_READY;
}
bo->edit_completed = true;
return EFI_SUCCESS;
}
/**
* eficonfig_process_select_file() - callback function for "Select File" entry
*
* @data: pointer to the data
* Return: status code
*/
efi_status_t eficonfig_process_select_file(void *data)
{
return EFI_SUCCESS;
}
/**
* eficonfig_process_clear_file_selection() - callback function for "Clear" entry
*
* @data: pointer to the data
* Return: status code
*/
efi_status_t eficonfig_process_clear_file_selection(void *data)
{
struct eficonfig_select_file_info *file_info = data;
/* clear the existing file information */
file_info->current_volume = NULL;
file_info->current_path[0] = u'\0';
file_info->dp_volume = NULL;
return EFI_ABORTED;
}
static struct eficonfig_item select_file_menu_items[] = {
{"Select File", eficonfig_process_select_file},
{"Clear", eficonfig_process_clear_file_selection},
{"Quit", eficonfig_process_quit},
};
/**
* eficonfig_display_select_file_option() - display select file option
*
* @file_info: pointer to the file information structure
* Return: status code
*/
efi_status_t eficonfig_display_select_file_option(struct eficonfig_select_file_info *file_info)
{
efi_status_t ret;
struct efimenu *efi_menu;
select_file_menu_items[1].data = file_info;
efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
ARRAY_SIZE(select_file_menu_items));
if (!efi_menu)
return EFI_OUT_OF_RESOURCES;
ret = eficonfig_process_common(efi_menu, " ** Update File **");
if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
ret = EFI_NOT_READY;
eficonfig_destroy(efi_menu);
return ret;