-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathShellProtocol.c
4103 lines (3517 loc) · 133 KB
/
ShellProtocol.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
/** @file
Member functions of EFI_SHELL_PROTOCOL and functions for creation,
manipulation, and initialization of EFI_SHELL_PROTOCOL.
(C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "Shell.h"
#define INIT_NAME_BUFFER_SIZE 128
/**
Close an open file handle.
This function closes a specified file handle. All "dirty" cached file data is
flushed to the device, and the file is closed. In all cases the handle is
closed.
@param[in] FileHandle The file handle to close.
@retval EFI_SUCCESS The file handle was closed successfully.
**/
EFI_STATUS
EFIAPI
EfiShellClose (
IN SHELL_FILE_HANDLE FileHandle
)
{
ShellFileHandleRemove (FileHandle);
return (FileHandleClose (ConvertShellHandleToEfiFileProtocol (FileHandle)));
}
/**
Internal worker to determine whether there is a BlockIo somewhere
upon the device path specified.
@param[in] DevicePath The device path to test.
@retval TRUE gEfiBlockIoProtocolGuid was installed on a handle with this device path
@retval FALSE gEfiBlockIoProtocolGuid was not found.
**/
BOOLEAN
InternalShellProtocolIsBlockIoPresent (
IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
)
{
EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
EFI_STATUS Status;
EFI_HANDLE Handle;
Handle = NULL;
DevicePathCopy = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;
Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevicePathCopy, &Handle);
if ((Handle != NULL) && (!EFI_ERROR (Status))) {
return (TRUE);
}
return (FALSE);
}
/**
Internal worker to determine whether there is a file system somewhere
upon the device path specified.
@param[in] DevicePath The device path to test.
@retval TRUE gEfiSimpleFileSystemProtocolGuid was installed on a handle with this device path
@retval FALSE gEfiSimpleFileSystemProtocolGuid was not found.
**/
BOOLEAN
InternalShellProtocolIsSimpleFileSystemPresent (
IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
)
{
EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
EFI_STATUS Status;
EFI_HANDLE Handle;
Handle = NULL;
DevicePathCopy = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;
Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &Handle);
if ((Handle != NULL) && (!EFI_ERROR (Status))) {
return (TRUE);
}
return (FALSE);
}
/**
This function creates a mapping for a device path.
If both DevicePath and Mapping are NULL, this will reset the mapping to default values.
@param DevicePath Points to the device path. If this is NULL and Mapping points to a valid mapping,
then the mapping will be deleted.
@param Mapping Points to the NULL-terminated mapping for the device path. Must end with a ':'
@retval EFI_SUCCESS Mapping created or deleted successfully.
@retval EFI_NO_MAPPING There is no handle that corresponds exactly to DevicePath. See the
boot service function LocateDevicePath().
@retval EFI_ACCESS_DENIED The mapping is a built-in alias.
@retval EFI_INVALID_PARAMETER Mapping was NULL
@retval EFI_INVALID_PARAMETER Mapping did not end with a ':'
@retval EFI_INVALID_PARAMETER DevicePath was not pointing at a device that had a SIMPLE_FILE_SYSTEM_PROTOCOL installed.
@retval EFI_NOT_FOUND There was no mapping found to delete
@retval EFI_OUT_OF_RESOURCES Memory allocation failed
**/
EFI_STATUS
EFIAPI
EfiShellSetMap (
IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,
IN CONST CHAR16 *Mapping
)
{
EFI_STATUS Status;
SHELL_MAP_LIST *MapListNode;
if (Mapping == NULL) {
return (EFI_INVALID_PARAMETER);
}
if (Mapping[StrLen (Mapping)-1] != ':') {
return (EFI_INVALID_PARAMETER);
}
//
// Delete the mapping
//
if (DevicePath == NULL) {
if (IsListEmpty (&gShellMapList.Link)) {
return (EFI_NOT_FOUND);
}
for ( MapListNode = (SHELL_MAP_LIST *)GetFirstNode (&gShellMapList.Link)
; !IsNull (&gShellMapList.Link, &MapListNode->Link)
; MapListNode = (SHELL_MAP_LIST *)GetNextNode (&gShellMapList.Link, &MapListNode->Link)
)
{
if (StringNoCaseCompare (&MapListNode->MapName, &Mapping) == 0) {
RemoveEntryList (&MapListNode->Link);
SHELL_FREE_NON_NULL (MapListNode->DevicePath);
SHELL_FREE_NON_NULL (MapListNode->MapName);
SHELL_FREE_NON_NULL (MapListNode->CurrentDirectoryPath);
FreePool (MapListNode);
return (EFI_SUCCESS);
}
} // for loop
//
// We didn't find one to delete
//
return (EFI_NOT_FOUND);
}
//
// make sure this is a valid to add device path
//
/// @todo add BlockIo to this test...
if ( !InternalShellProtocolIsSimpleFileSystemPresent (DevicePath)
&& !InternalShellProtocolIsBlockIoPresent (DevicePath))
{
return (EFI_INVALID_PARAMETER);
}
//
// First make sure there is no old mapping
//
Status = EfiShellSetMap (NULL, Mapping);
if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_FOUND)) {
return (Status);
}
//
// now add the new one.
//
Status = ShellCommandAddMapItemAndUpdatePath (Mapping, DevicePath, 0, FALSE);
return (Status);
}
/**
Gets the device path from the mapping.
This function gets the device path associated with a mapping.
@param Mapping A pointer to the mapping
@retval !=NULL Pointer to the device path that corresponds to the
device mapping. The returned pointer does not need
to be freed.
@retval NULL There is no device path associated with the
specified mapping.
**/
CONST EFI_DEVICE_PATH_PROTOCOL *
EFIAPI
EfiShellGetDevicePathFromMap (
IN CONST CHAR16 *Mapping
)
{
SHELL_MAP_LIST *MapListItem;
CHAR16 *NewName;
UINTN Size;
NewName = NULL;
Size = 0;
StrnCatGrow (&NewName, &Size, Mapping, 0);
if (Mapping[StrLen (Mapping)-1] != L':') {
StrnCatGrow (&NewName, &Size, L":", 0);
}
MapListItem = ShellCommandFindMapItem (NewName);
FreePool (NewName);
if (MapListItem != NULL) {
return (MapListItem->DevicePath);
}
return (NULL);
}
/**
Gets the mapping(s) that most closely matches the device path.
This function gets the mapping which corresponds to the device path *DevicePath. If
there is no exact match, then the mapping which most closely matches *DevicePath
is returned, and *DevicePath is updated to point to the remaining portion of the
device path. If there is an exact match, the mapping is returned and *DevicePath
points to the end-of-device-path node.
If there are multiple map names they will be semi-colon separated in the
NULL-terminated string.
@param DevicePath On entry, points to a device path pointer. On
exit, updates the pointer to point to the
portion of the device path after the mapping.
@retval NULL No mapping was found.
@return !=NULL Pointer to NULL-terminated mapping. The buffer
is callee allocated and should be freed by the caller.
**/
CONST CHAR16 *
EFIAPI
EfiShellGetMapFromDevicePath (
IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
)
{
SHELL_MAP_LIST *Node;
CHAR16 *PathForReturn;
UINTN PathSize;
// EFI_HANDLE PathHandle;
// EFI_HANDLE MapHandle;
// EFI_STATUS Status;
// EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
// EFI_DEVICE_PATH_PROTOCOL *MapPathCopy;
if ((DevicePath == NULL) || (*DevicePath == NULL)) {
return (NULL);
}
PathForReturn = NULL;
PathSize = 0;
for ( Node = (SHELL_MAP_LIST *)GetFirstNode (&gShellMapList.Link)
; !IsNull (&gShellMapList.Link, &Node->Link)
; Node = (SHELL_MAP_LIST *)GetNextNode (&gShellMapList.Link, &Node->Link)
)
{
//
// check for exact match
//
if (DevicePathCompare (DevicePath, &Node->DevicePath) == 0) {
ASSERT ((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));
if (PathSize != 0) {
PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, L";", 0);
}
PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, Node->MapName, 0);
}
}
if (PathForReturn != NULL) {
while (!IsDevicePathEndType (*DevicePath)) {
*DevicePath = NextDevicePathNode (*DevicePath);
}
//
// Do not call SetDevicePathEndNode() if the device path node is already the
// end of an entire device path.
//
if (!IsDevicePathEnd (*DevicePath)) {
SetDevicePathEndNode (*DevicePath);
}
}
/*
///@todo finish code for inexact matches.
if (PathForReturn == NULL) {
PathSize = 0;
DevicePathCopy = DuplicateDevicePath(*DevicePath);
ASSERT(DevicePathCopy != NULL);
Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &PathHandle);
ASSERT_EFI_ERROR(Status);
//
// check each of the device paths we have to get the root of the path for consist mappings
//
for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
; !IsNull(&gShellMapList.Link, &Node->Link)
; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
){
if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) == 0) {
continue;
}
MapPathCopy = DuplicateDevicePath(Node->DevicePath);
ASSERT(MapPathCopy != NULL);
Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
if (MapHandle == PathHandle) {
*DevicePath = DevicePathCopy;
MapPathCopy = NULL;
DevicePathCopy = NULL;
PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
break;
}
}
//
// now add on the non-consistent mappings
//
for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
; !IsNull(&gShellMapList.Link, &Node->Link)
; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
){
if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) != 0) {
continue;
}
MapPathCopy = Node->DevicePath;
ASSERT(MapPathCopy != NULL);
Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
if (MapHandle == PathHandle) {
PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
break;
}
}
}
*/
return (AddBufferToFreeList (PathForReturn));
}
/**
Converts a device path to a file system-style path.
This function converts a device path to a file system path by replacing part, or all, of
the device path with the file-system mapping. If there are more than one application
file system mappings, the one that most closely matches Path will be used.
@param Path The pointer to the device path
@retval NULL the device path could not be found.
@return all The pointer of the NULL-terminated file path. The path
is callee-allocated and should be freed by the caller.
**/
CHAR16 *
EFIAPI
EfiShellGetFilePathFromDevicePath (
IN CONST EFI_DEVICE_PATH_PROTOCOL *Path
)
{
EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
EFI_DEVICE_PATH_PROTOCOL *MapPathCopy;
SHELL_MAP_LIST *MapListItem;
CHAR16 *PathForReturn;
UINTN PathSize;
EFI_HANDLE PathHandle;
EFI_HANDLE MapHandle;
EFI_STATUS Status;
FILEPATH_DEVICE_PATH *FilePath;
FILEPATH_DEVICE_PATH *AlignedNode;
PathForReturn = NULL;
PathSize = 0;
DevicePathCopy = (EFI_DEVICE_PATH_PROTOCOL *)Path;
ASSERT (DevicePathCopy != NULL);
if (DevicePathCopy == NULL) {
return (NULL);
}
/// @todo BlockIo?
Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &PathHandle);
if (EFI_ERROR (Status)) {
return (NULL);
}
//
// check each of the device paths we have to get the root of the path
//
for ( MapListItem = (SHELL_MAP_LIST *)GetFirstNode (&gShellMapList.Link)
; !IsNull (&gShellMapList.Link, &MapListItem->Link)
; MapListItem = (SHELL_MAP_LIST *)GetNextNode (&gShellMapList.Link, &MapListItem->Link)
)
{
MapPathCopy = (EFI_DEVICE_PATH_PROTOCOL *)MapListItem->DevicePath;
ASSERT (MapPathCopy != NULL);
/// @todo BlockIo?
Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
if (MapHandle == PathHandle) {
ASSERT ((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));
PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, MapListItem->MapName, 0);
//
// go through all the remaining nodes in the device path
//
for ( FilePath = (FILEPATH_DEVICE_PATH *)DevicePathCopy
; !IsDevicePathEnd (&FilePath->Header)
; FilePath = (FILEPATH_DEVICE_PATH *)NextDevicePathNode (&FilePath->Header)
)
{
//
// If any node is not a file path node, then the conversion can not be completed
//
if ((DevicePathType (&FilePath->Header) != MEDIA_DEVICE_PATH) ||
(DevicePathSubType (&FilePath->Header) != MEDIA_FILEPATH_DP))
{
if (PathForReturn != NULL) {
FreePool (PathForReturn);
}
return NULL;
}
//
// append the path part onto the filepath.
//
ASSERT ((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));
AlignedNode = AllocateCopyPool (DevicePathNodeLength (FilePath), FilePath);
if (AlignedNode == NULL) {
if (PathForReturn != NULL) {
FreePool (PathForReturn);
}
return NULL;
}
// File Path Device Path Nodes 'can optionally add a "\" separator to
// the beginning and/or the end of the Path Name string.'
// (UEFI Spec 2.4 section 9.3.6.4).
// If necessary, add a "\", but otherwise don't
// (This is specified in the above section, and also implied by the
// UEFI Shell spec section 3.7)
if ((PathSize != 0) &&
(PathForReturn != NULL) &&
(PathForReturn[PathSize / sizeof (CHAR16) - 1] != L'\\') &&
(AlignedNode->PathName[0] != L'\\'))
{
PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, L"\\", 1);
}
PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, AlignedNode->PathName, 0);
FreePool (AlignedNode);
} // for loop of remaining nodes
}
if (PathForReturn != NULL) {
break;
}
} // for loop of paths to check
return (PathForReturn);
}
/**
Converts a file system style name to a device path.
This function converts a file system style name to a device path, by replacing any
mapping references to the associated device path.
@param[in] Path The pointer to the path.
@return The pointer of the file path. The file path is callee
allocated and should be freed by the caller.
@retval NULL The path could not be found.
@retval NULL There was not enough available memory.
**/
EFI_DEVICE_PATH_PROTOCOL *
EFIAPI
EfiShellGetDevicePathFromFilePath (
IN CONST CHAR16 *Path
)
{
CHAR16 *MapName;
CHAR16 *NewPath;
CONST CHAR16 *Cwd;
UINTN Size;
CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath;
EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
EFI_DEVICE_PATH_PROTOCOL *DevicePathCopyForFree;
EFI_DEVICE_PATH_PROTOCOL *DevicePathForReturn;
EFI_HANDLE Handle;
EFI_STATUS Status;
if (Path == NULL) {
return (NULL);
}
MapName = NULL;
NewPath = NULL;
if (StrStr (Path, L":") == NULL) {
Cwd = EfiShellGetCurDir (NULL);
if (Cwd == NULL) {
return (NULL);
}
Size = StrSize (Cwd) + StrSize (Path);
NewPath = AllocateZeroPool (Size);
if (NewPath == NULL) {
return (NULL);
}
StrCpyS (NewPath, Size/sizeof (CHAR16), Cwd);
StrCatS (NewPath, Size/sizeof (CHAR16), L"\\");
if (*Path == L'\\') {
Path++;
while (PathRemoveLastItem (NewPath)) {
}
}
StrCatS (NewPath, Size/sizeof (CHAR16), Path);
DevicePathForReturn = EfiShellGetDevicePathFromFilePath (NewPath);
FreePool (NewPath);
return (DevicePathForReturn);
}
Size = 0;
//
// find the part before (but including) the : for the map name
//
ASSERT ((MapName == NULL && Size == 0) || (MapName != NULL));
MapName = StrnCatGrow (&MapName, &Size, Path, (StrStr (Path, L":")-Path+1));
if ((MapName == NULL) || (MapName[StrLen (MapName)-1] != L':')) {
return (NULL);
}
//
// look up the device path in the map
//
DevicePath = EfiShellGetDevicePathFromMap (MapName);
if (DevicePath == NULL) {
//
// Must have been a bad Mapname
//
return (NULL);
}
//
// make a copy for LocateDevicePath to modify (also save a pointer to call FreePool with)
//
DevicePathCopyForFree = DevicePathCopy = DuplicateDevicePath (DevicePath);
if (DevicePathCopy == NULL) {
FreePool (MapName);
return (NULL);
}
//
// get the handle
//
/// @todo BlockIo?
Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &Handle);
if (EFI_ERROR (Status)) {
if (DevicePathCopyForFree != NULL) {
FreePool (DevicePathCopyForFree);
}
FreePool (MapName);
return (NULL);
}
//
// build the full device path
//
if ((*(Path+StrLen (MapName)) != CHAR_NULL) &&
(*(Path+StrLen (MapName)+1) == CHAR_NULL))
{
DevicePathForReturn = FileDevicePath (Handle, L"\\");
} else {
DevicePathForReturn = FileDevicePath (Handle, Path+StrLen (MapName));
}
FreePool (MapName);
if (DevicePathCopyForFree != NULL) {
FreePool (DevicePathCopyForFree);
}
return (DevicePathForReturn);
}
/**
Gets the name of the device specified by the device handle.
This function gets the user-readable name of the device specified by the device
handle. If no user-readable name could be generated, then *BestDeviceName will be
NULL and EFI_NOT_FOUND will be returned.
If EFI_DEVICE_NAME_USE_COMPONENT_NAME is set, then the function will return the
device's name using the EFI_COMPONENT_NAME2_PROTOCOL, if present on
DeviceHandle.
If EFI_DEVICE_NAME_USE_DEVICE_PATH is set, then the function will return the
device's name using the EFI_DEVICE_PATH_PROTOCOL, if present on DeviceHandle.
If both EFI_DEVICE_NAME_USE_COMPONENT_NAME and
EFI_DEVICE_NAME_USE_DEVICE_PATH are set, then
EFI_DEVICE_NAME_USE_COMPONENT_NAME will have higher priority.
@param DeviceHandle The handle of the device.
@param Flags Determines the possible sources of component names.
Valid bits are:
EFI_DEVICE_NAME_USE_COMPONENT_NAME
EFI_DEVICE_NAME_USE_DEVICE_PATH
@param Language A pointer to the language specified for the device
name, in the same format as described in the UEFI
specification, Appendix M
@param BestDeviceName On return, points to the callee-allocated NULL-
terminated name of the device. If no device name
could be found, points to NULL. The name must be
freed by the caller...
@retval EFI_SUCCESS Get the name successfully.
@retval EFI_NOT_FOUND Fail to get the device name.
@retval EFI_INVALID_PARAMETER Flags did not have a valid bit set.
@retval EFI_INVALID_PARAMETER BestDeviceName was NULL
@retval EFI_INVALID_PARAMETER DeviceHandle was NULL
**/
EFI_STATUS
EFIAPI
EfiShellGetDeviceName (
IN EFI_HANDLE DeviceHandle,
IN EFI_SHELL_DEVICE_NAME_FLAGS Flags,
IN CHAR8 *Language,
OUT CHAR16 **BestDeviceName
)
{
EFI_STATUS Status;
EFI_COMPONENT_NAME2_PROTOCOL *CompName2;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
EFI_HANDLE *HandleList;
UINTN HandleCount;
UINTN LoopVar;
CHAR16 *DeviceNameToReturn;
CHAR8 *Lang;
UINTN ParentControllerCount;
EFI_HANDLE *ParentControllerBuffer;
UINTN ParentDriverCount;
EFI_HANDLE *ParentDriverBuffer;
if ((BestDeviceName == NULL) ||
(DeviceHandle == NULL)
)
{
return (EFI_INVALID_PARAMETER);
}
//
// make sure one of the 2 supported bits is on
//
if (((Flags & EFI_DEVICE_NAME_USE_COMPONENT_NAME) == 0) &&
((Flags & EFI_DEVICE_NAME_USE_DEVICE_PATH) == 0))
{
return (EFI_INVALID_PARAMETER);
}
DeviceNameToReturn = NULL;
*BestDeviceName = NULL;
HandleList = NULL;
HandleCount = 0;
Lang = NULL;
if ((Flags & EFI_DEVICE_NAME_USE_COMPONENT_NAME) != 0) {
Status = ParseHandleDatabaseByRelationship (
NULL,
DeviceHandle,
HR_DRIVER_BINDING_HANDLE|HR_DEVICE_DRIVER,
&HandleCount,
&HandleList
);
for (LoopVar = 0; LoopVar < HandleCount; LoopVar++) {
//
// Go through those handles until we get one that passes for GetComponentName
//
Status = gBS->OpenProtocol (
HandleList[LoopVar],
&gEfiComponentName2ProtocolGuid,
(VOID **)&CompName2,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
Status = gBS->OpenProtocol (
HandleList[LoopVar],
&gEfiComponentNameProtocolGuid,
(VOID **)&CompName2,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
}
if (EFI_ERROR (Status)) {
continue;
}
Lang = GetBestLanguageForDriver (CompName2->SupportedLanguages, Language, FALSE);
if (Lang == NULL) {
continue;
}
Status = CompName2->GetControllerName (CompName2, DeviceHandle, NULL, Lang, &DeviceNameToReturn);
FreePool (Lang);
Lang = NULL;
if (!EFI_ERROR (Status) && (DeviceNameToReturn != NULL)) {
break;
}
}
if (HandleList != NULL) {
FreePool (HandleList);
}
//
// Now check the parent controller using this as the child.
//
Status = PARSE_HANDLE_DATABASE_PARENTS (DeviceHandle, &ParentControllerCount, &ParentControllerBuffer);
if ((DeviceNameToReturn == NULL) && !EFI_ERROR (Status)) {
for (LoopVar = 0; LoopVar < ParentControllerCount; LoopVar++) {
Status = PARSE_HANDLE_DATABASE_UEFI_DRIVERS (ParentControllerBuffer[LoopVar], &ParentDriverCount, &ParentDriverBuffer);
if (!EFI_ERROR (Status)) {
for (HandleCount = 0; HandleCount < ParentDriverCount; HandleCount++) {
//
// try using that driver's component name with controller and our driver as the child.
//
Status = gBS->OpenProtocol (
ParentDriverBuffer[HandleCount],
&gEfiComponentName2ProtocolGuid,
(VOID **)&CompName2,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
Status = gBS->OpenProtocol (
ParentDriverBuffer[HandleCount],
&gEfiComponentNameProtocolGuid,
(VOID **)&CompName2,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
}
if (EFI_ERROR (Status)) {
continue;
}
Lang = GetBestLanguageForDriver (CompName2->SupportedLanguages, Language, FALSE);
if (Lang == NULL) {
continue;
}
Status = CompName2->GetControllerName (CompName2, ParentControllerBuffer[LoopVar], DeviceHandle, Lang, &DeviceNameToReturn);
FreePool (Lang);
Lang = NULL;
if (!EFI_ERROR (Status) && (DeviceNameToReturn != NULL)) {
break;
}
}
SHELL_FREE_NON_NULL (ParentDriverBuffer);
if (!EFI_ERROR (Status) && (DeviceNameToReturn != NULL)) {
break;
}
}
}
SHELL_FREE_NON_NULL (ParentControllerBuffer);
}
//
// dont return on fail since we will try device path if that bit is on
//
if (DeviceNameToReturn != NULL) {
ASSERT (BestDeviceName != NULL);
StrnCatGrow (BestDeviceName, NULL, DeviceNameToReturn, 0);
return (EFI_SUCCESS);
}
}
if ((Flags & EFI_DEVICE_NAME_USE_DEVICE_PATH) != 0) {
Status = gBS->OpenProtocol (
DeviceHandle,
&gEfiDevicePathProtocolGuid,
(VOID **)&DevicePath,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (!EFI_ERROR (Status)) {
//
// use device path to text on the device path
//
*BestDeviceName = ConvertDevicePathToText (DevicePath, TRUE, TRUE);
return (EFI_SUCCESS);
}
}
//
// none of the selected bits worked.
//
return (EFI_NOT_FOUND);
}
/**
Opens the root directory of a device on a handle
This function opens the root directory of a device and returns a file handle to it.
@param DeviceHandle The handle of the device that contains the volume.
@param FileHandle On exit, points to the file handle corresponding to the root directory on the
device.
@retval EFI_SUCCESS Root opened successfully.
@retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
could not be opened.
@retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
@retval EFI_DEVICE_ERROR The device had an error.
@retval Others Error status returned from EFI_SIMPLE_FILE_SYSTEM_PROTOCOL->OpenVolume().
**/
EFI_STATUS
EFIAPI
EfiShellOpenRootByHandle (
IN EFI_HANDLE DeviceHandle,
OUT SHELL_FILE_HANDLE *FileHandle
)
{
EFI_STATUS Status;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem;
EFI_FILE_PROTOCOL *RealFileHandle;
EFI_DEVICE_PATH_PROTOCOL *DevPath;
//
// get the simple file system interface
//
Status = gBS->OpenProtocol (
DeviceHandle,
&gEfiSimpleFileSystemProtocolGuid,
(VOID **)&SimpleFileSystem,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
return (EFI_NOT_FOUND);
}
Status = gBS->OpenProtocol (
DeviceHandle,
&gEfiDevicePathProtocolGuid,
(VOID **)&DevPath,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
return (EFI_NOT_FOUND);
}
//
// Open the root volume now...
//
Status = SimpleFileSystem->OpenVolume (SimpleFileSystem, &RealFileHandle);
if (EFI_ERROR (Status)) {
return Status;
}
*FileHandle = ConvertEfiFileProtocolToShellHandle (RealFileHandle, EfiShellGetMapFromDevicePath (&DevPath));
return (EFI_SUCCESS);
}
/**
Opens the root directory of a device.
This function opens the root directory of a device and returns a file handle to it.
@param DevicePath Points to the device path corresponding to the device where the
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL is installed.
@param FileHandle On exit, points to the file handle corresponding to the root directory on the
device.
@retval EFI_SUCCESS Root opened successfully.
@retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
could not be opened.
@retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
@retval EFI_DEVICE_ERROR The device had an error
@retval EFI_INVALID_PARAMETER FileHandle is NULL.
**/
EFI_STATUS
EFIAPI
EfiShellOpenRoot (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
OUT SHELL_FILE_HANDLE *FileHandle
)
{
EFI_STATUS Status;
EFI_HANDLE Handle;
if (FileHandle == NULL) {
return (EFI_INVALID_PARAMETER);
}
//
// find the handle of the device with that device handle and the file system
//
/// @todo BlockIo?
Status = gBS->LocateDevicePath (
&gEfiSimpleFileSystemProtocolGuid,
&DevicePath,
&Handle
);
if (EFI_ERROR (Status)) {
return (EFI_NOT_FOUND);
}
return (EfiShellOpenRootByHandle (Handle, FileHandle));
}
/**
Returns whether any script files are currently being processed.
@retval TRUE There is at least one script file active.
@retval FALSE No script files are active now.
**/
BOOLEAN
EFIAPI
EfiShellBatchIsActive (
VOID
)
{
if (ShellCommandGetCurrentScriptFile () == NULL) {
return (FALSE);
}
return (TRUE);
}
/**
Worker function to open a file based on a device path. this will open the root
of the volume and then traverse down to the file itself.
@param DevicePath Device Path of the file.
@param FileHandle Pointer to the file upon a successful return.
@param OpenMode mode to open file in.
@param Attributes the File Attributes to use when creating a new file.
@retval EFI_SUCCESS the file is open and FileHandle is valid
@retval EFI_UNSUPPORTED the device path contained non-path elements
@retval other an error occurred.
**/
EFI_STATUS
InternalOpenFileDevicePath (
IN OUT EFI_DEVICE_PATH_PROTOCOL *DevicePath,
OUT SHELL_FILE_HANDLE *FileHandle,
IN UINT64 OpenMode,
IN UINT64 Attributes OPTIONAL
)
{
EFI_STATUS Status;
FILEPATH_DEVICE_PATH *FilePathNode;
EFI_HANDLE Handle;
SHELL_FILE_HANDLE ShellHandle;
EFI_FILE_PROTOCOL *Handle1;
EFI_FILE_PROTOCOL *Handle2;
FILEPATH_DEVICE_PATH *AlignedNode;
if (FileHandle == NULL) {