TianoCore EDK2 master
Loading...
Searching...
No Matches
UefiDevicePathLib.c
Go to the documentation of this file.
1
10#include <Uefi.h>
11
15
17#include <Library/DebugLib.h>
18#include <Library/BaseLib.h>
22#include <Library/PcdLib.h>
23
27
28//
29// Template for an end-of-device path node.
30//
31GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath = {
32 END_DEVICE_PATH_TYPE,
33 END_ENTIRE_DEVICE_PATH_SUBTYPE,
34 {
35 END_DEVICE_PATH_LENGTH,
36 0
37 }
38};
39
53EFIAPI
55 IN EFI_HANDLE ImageHandle,
56 IN EFI_SYSTEM_TABLE *SystemTable
57 )
58{
59 EFI_STATUS Status;
60
61 Status = gBS->LocateProtocol (
62 &gEfiDevicePathUtilitiesProtocolGuid,
63 NULL,
64 (VOID **)&mDevicePathLibDevicePathUtilities
65 );
66 ASSERT_EFI_ERROR (Status);
67 ASSERT (mDevicePathLibDevicePathUtilities != NULL);
68 return Status;
69}
70
86BOOLEAN
87EFIAPI
90 IN UINTN MaxSize
91 )
92{
93 UINTN Count;
94 UINTN Size;
95 UINTN NodeLength;
96
97 ASSERT (DevicePath != NULL);
98
99 if (MaxSize == 0) {
100 MaxSize = MAX_UINTN;
101 }
102
103 //
104 // Validate the input size big enough to touch the first node.
105 //
106 if (MaxSize < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
107 return FALSE;
108 }
109
110 for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {
111 NodeLength = DevicePathNodeLength (DevicePath);
112 if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
113 return FALSE;
114 }
115
116 if (NodeLength > MAX_UINTN - Size) {
117 return FALSE;
118 }
119
120 Size += NodeLength;
121
122 //
123 // Validate next node before touch it.
124 //
125 if (Size > MaxSize - END_DEVICE_PATH_LENGTH ) {
126 return FALSE;
127 }
128
129 if (PcdGet32 (PcdMaximumDevicePathNodeCount) > 0) {
130 Count++;
131 if (Count >= PcdGet32 (PcdMaximumDevicePathNodeCount)) {
132 return FALSE;
133 }
134 }
135
136 //
137 // FilePath must be a NULL-terminated string.
138 //
139 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&
140 (DevicePathSubType (DevicePath) == MEDIA_FILEPATH_DP) &&
141 (*(CHAR16 *)((UINT8 *)DevicePath + NodeLength - 2) != 0))
142 {
143 return FALSE;
144 }
145 }
146
147 //
148 // Only return TRUE when the End Device Path node is valid.
149 //
150 return (BOOLEAN)(DevicePathNodeLength (DevicePath) == END_DEVICE_PATH_LENGTH);
151}
152
165UINT8
166EFIAPI
168 IN CONST VOID *Node
169 )
170{
171 ASSERT (Node != NULL);
172 return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Type;
173}
174
187UINT8
188EFIAPI
190 IN CONST VOID *Node
191 )
192{
193 ASSERT (Node != NULL);
194 return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->SubType;
195}
196
212UINTN
213EFIAPI
215 IN CONST VOID *Node
216 )
217{
218 ASSERT (Node != NULL);
219 return ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);
220}
221
237EFIAPI
239 IN CONST VOID *Node
240 )
241{
242 ASSERT (Node != NULL);
243 return (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)(Node) + DevicePathNodeLength (Node));
244}
245
265BOOLEAN
266EFIAPI
268 IN CONST VOID *Node
269 )
270{
271 ASSERT (Node != NULL);
272 return (BOOLEAN)(DevicePathType (Node) == END_DEVICE_PATH_TYPE);
273}
274
291BOOLEAN
292EFIAPI
294 IN CONST VOID *Node
295 )
296{
297 ASSERT (Node != NULL);
298 return (BOOLEAN)(IsDevicePathEndType (Node) && DevicePathSubType (Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);
299}
300
319BOOLEAN
320EFIAPI
322 IN CONST VOID *Node
323 )
324{
325 ASSERT (Node != NULL);
326 return (BOOLEAN)(IsDevicePathEndType (Node) && DevicePathSubType (Node) == END_INSTANCE_DEVICE_PATH_SUBTYPE);
327}
328
347UINT16
348EFIAPI
350 IN OUT VOID *Node,
351 IN UINTN Length
352 )
353{
354 ASSERT (Node != NULL);
355 ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB));
356 return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));
357}
358
375VOID
376EFIAPI
378 OUT VOID *Node
379 )
380{
381 ASSERT (Node != NULL);
382 CopyMem (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));
383}
384
398UINTN
399EFIAPI
402 )
403{
404 return mDevicePathLibDevicePathUtilities->GetDevicePathSize (DevicePath);
405}
406
425EFIAPI
428 )
429{
430 return mDevicePathLibDevicePathUtilities->DuplicateDevicePath (DevicePath);
431}
432
458EFIAPI
460 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath OPTIONAL,
461 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL
462 )
463{
464 return mDevicePathLibDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);
465}
466
496EFIAPI
498 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,
499 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL
500 )
501{
502 return mDevicePathLibDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);
503}
504
529EFIAPI
531 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,
532 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL
533 )
534{
535 return mDevicePathLibDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);
536}
537
566EFIAPI
568 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,
569 OUT UINTN *Size
570 )
571{
572 ASSERT (Size != NULL);
573 return mDevicePathLibDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);
574}
575
596EFIAPI
598 IN UINT8 NodeType,
599 IN UINT8 NodeSubType,
600 IN UINT16 NodeLength
601 )
602{
603 return mDevicePathLibDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);
604}
605
621BOOLEAN
622EFIAPI
625 )
626{
627 return mDevicePathLibDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);
628}
629
644EFIAPI
646 IN EFI_HANDLE Handle
647 )
648{
649 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
650 EFI_STATUS Status;
651
652 Status = gBS->HandleProtocol (
653 Handle,
654 &gEfiDevicePathProtocolGuid,
655 (VOID *)&DevicePath
656 );
657 if (EFI_ERROR (Status)) {
658 DevicePath = NULL;
659 }
660
661 return DevicePath;
662}
663
687EFIAPI
689 IN EFI_HANDLE Device OPTIONAL,
690 IN CONST CHAR16 *FileName
691 )
692{
693 UINTN Size;
694 FILEPATH_DEVICE_PATH *FilePath;
695 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
697
698 DevicePath = NULL;
699
700 Size = StrSize (FileName);
701 FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + END_DEVICE_PATH_LENGTH);
702 if (FileDevicePath != NULL) {
704 FilePath->Header.Type = MEDIA_DEVICE_PATH;
705 FilePath->Header.SubType = MEDIA_FILEPATH_DP;
706 CopyMem (&FilePath->PathName, FileName, Size);
707 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);
708 SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));
709
710 if (Device != NULL) {
711 DevicePath = DevicePathFromHandle (Device);
712 }
713
714 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);
716 }
717
718 return DevicePath;
719}
720
728VOID *
730 EFI_GUID *ProtocolGuid
731 )
732{
733 EFI_STATUS Status;
734 VOID *Protocol;
735
736 Status = gBS->LocateProtocol (
737 ProtocolGuid,
738 NULL,
739 (VOID **)&Protocol
740 );
741 if (EFI_ERROR (Status)) {
742 return NULL;
743 } else {
744 return Protocol;
745 }
746}
747
763CHAR16 *
764EFIAPI
767 IN BOOLEAN DisplayOnly,
768 IN BOOLEAN AllowShortcuts
769 )
770{
771 if (mDevicePathLibDevicePathToText == NULL) {
772 mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);
773 }
774
775 if (mDevicePathLibDevicePathToText != NULL) {
776 return mDevicePathLibDevicePathToText->ConvertDeviceNodeToText (DeviceNode, DisplayOnly, AllowShortcuts);
777 } else {
778 return NULL;
779 }
780}
781
797CHAR16 *
798EFIAPI
801 IN BOOLEAN DisplayOnly,
802 IN BOOLEAN AllowShortcuts
803 )
804{
805 if (mDevicePathLibDevicePathToText == NULL) {
806 mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);
807 }
808
809 if (mDevicePathLibDevicePathToText != NULL) {
810 return mDevicePathLibDevicePathToText->ConvertDevicePathToText (DevicePath, DisplayOnly, AllowShortcuts);
811 } else {
812 return NULL;
813 }
814}
815
828EFIAPI
830 IN CONST CHAR16 *TextDeviceNode
831 )
832{
833 if (mDevicePathLibDevicePathFromText == NULL) {
834 mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);
835 }
836
837 if (mDevicePathLibDevicePathFromText != NULL) {
838 return mDevicePathLibDevicePathFromText->ConvertTextToDeviceNode (TextDeviceNode);
839 } else {
840 return NULL;
841 }
842}
843
857EFIAPI
859 IN CONST CHAR16 *TextDevicePath
860 )
861{
862 if (mDevicePathLibDevicePathFromText == NULL) {
863 mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);
864 }
865
866 if (mDevicePathLibDevicePathFromText != NULL) {
867 return mDevicePathLibDevicePathFromText->ConvertTextToDevicePath (TextDevicePath);
868 } else {
869 return NULL;
870 }
871}
UINT64 UINTN
UINTN EFIAPI StrSize(IN CONST CHAR16 *String)
Definition: String.c:72
UINT16 EFIAPI ReadUnaligned16(IN CONST UINT16 *Buffer)
Definition: Unaligned.c:29
UINT16 EFIAPI WriteUnaligned16(OUT UINT16 *Buffer, IN UINT16 Value)
Definition: Unaligned.c:61
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define MEDIA_FILEPATH_DP
Definition: DevicePath.h:1098
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define GLOBAL_REMOVE_IF_UNREFERENCED
Definition: Base.h:48
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
EFI_BOOT_SERVICES * gBS
EFI_DEVICE_PATH_PROTOCOL *EFIAPI AppendDevicePathNode(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL, IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL)
CHAR16 *EFIAPI ConvertDeviceNodeToText(IN CONST EFI_DEVICE_PATH_PROTOCOL *DeviceNode, IN BOOLEAN DisplayOnly, IN BOOLEAN AllowShortcuts)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI AppendDevicePathInstance(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL, IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI CreateDeviceNode(IN UINT8 NodeType, IN UINT8 NodeSubType, IN UINT16 NodeLength)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI AppendDevicePath(IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath OPTIONAL, IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI GetNextDevicePathInstance(IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath, OUT UINTN *Size)
CHAR16 *EFIAPI ConvertDevicePathToText(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN BOOLEAN DisplayOnly, IN BOOLEAN AllowShortcuts)
UINTN EFIAPI GetDevicePathSize(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI DuplicateDevicePath(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI ConvertTextToDevicePath(IN CONST CHAR16 *TextDevicePath)
BOOLEAN EFIAPI IsDevicePathMultiInstance(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI ConvertTextToDeviceNode(IN CONST CHAR16 *TextDeviceNode)
UINT8 EFIAPI DevicePathType(IN CONST VOID *Node)
UINT16 EFIAPI SetDevicePathNodeLength(IN OUT VOID *Node, IN UINTN Length)
UINTN EFIAPI DevicePathNodeLength(IN CONST VOID *Node)
UINT8 EFIAPI DevicePathSubType(IN CONST VOID *Node)
EFI_STATUS EFIAPI DevicePathLibConstructor(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI FileDevicePath(IN EFI_HANDLE Device OPTIONAL, IN CONST CHAR16 *FileName)
BOOLEAN EFIAPI IsDevicePathEnd(IN CONST VOID *Node)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI NextDevicePathNode(IN CONST VOID *Node)
BOOLEAN EFIAPI IsDevicePathValid(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN UINTN MaxSize)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI DevicePathFromHandle(IN EFI_HANDLE Handle)
VOID * UefiDevicePathLibLocateProtocol(EFI_GUID *ProtocolGuid)
BOOLEAN EFIAPI IsDevicePathEndType(IN CONST VOID *Node)
VOID EFIAPI SetDevicePathEndNode(OUT VOID *Node)
BOOLEAN EFIAPI IsDevicePathEndInstance(IN CONST VOID *Node)
Definition: Base.h:213