1 /** @file
2   Library instance that implement UEFI Device Path Library class based on protocol
3   gEfiDevicePathUtilitiesProtocolGuid.
4 
5   Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6   SPDX-License-Identifier: BSD-2-Clause-Patent
7 
8 **/
9 
10 
11 #include <Uefi.h>
12 
13 #include <Protocol/DevicePathUtilities.h>
14 #include <Protocol/DevicePathToText.h>
15 #include <Protocol/DevicePathFromText.h>
16 
17 #include <Library/DevicePathLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/PcdLib.h>
24 
25 GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mDevicePathLibDevicePathUtilities = NULL;
26 GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_TO_TEXT_PROTOCOL   *mDevicePathLibDevicePathToText    = NULL;
27 GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *mDevicePathLibDevicePathFromText  = NULL;
28 
29 //
30 // Template for an end-of-device path node.
31 //
32 GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL  mUefiDevicePathLibEndDevicePath = {
33   END_DEVICE_PATH_TYPE,
34   END_ENTIRE_DEVICE_PATH_SUBTYPE,
35   {
36     END_DEVICE_PATH_LENGTH,
37     0
38   }
39 };
40 
41 /**
42   The constructor function caches the pointer to DevicePathUtilites protocol.
43 
44   The constructor function locates DevicePathUtilities protocol from protocol database.
45   It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
46 
47   @param  ImageHandle   The firmware allocated handle for the EFI image.
48   @param  SystemTable   A pointer to the EFI System Table.
49 
50   @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
51 
52 **/
53 EFI_STATUS
54 EFIAPI
DevicePathLibConstructor(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)55 DevicePathLibConstructor (
56   IN      EFI_HANDLE                ImageHandle,
57   IN      EFI_SYSTEM_TABLE          *SystemTable
58   )
59 {
60   EFI_STATUS                        Status;
61 
62   Status = gBS->LocateProtocol (
63                   &gEfiDevicePathUtilitiesProtocolGuid,
64                   NULL,
65                   (VOID**) &mDevicePathLibDevicePathUtilities
66                   );
67   ASSERT_EFI_ERROR (Status);
68   ASSERT (mDevicePathLibDevicePathUtilities != NULL);
69   return Status;
70 }
71 
72 /**
73   Determine whether a given device path is valid.
74   If DevicePath is NULL, then ASSERT().
75 
76   @param  DevicePath  A pointer to a device path data structure.
77   @param  MaxSize     The maximum size of the device path data structure.
78 
79   @retval TRUE        DevicePath is valid.
80   @retval FALSE       The length of any node node in the DevicePath is less
81                       than sizeof (EFI_DEVICE_PATH_PROTOCOL).
82   @retval FALSE       If MaxSize is not zero, the size of the DevicePath
83                       exceeds MaxSize.
84   @retval FALSE       If PcdMaximumDevicePathNodeCount is not zero, the node
85                       count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.
86 **/
87 BOOLEAN
88 EFIAPI
IsDevicePathValid(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath,IN UINTN MaxSize)89 IsDevicePathValid (
90   IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
91   IN       UINTN                    MaxSize
92   )
93 {
94   UINTN Count;
95   UINTN Size;
96   UINTN NodeLength;
97 
98   ASSERT (DevicePath != NULL);
99 
100   if (MaxSize == 0) {
101     MaxSize = MAX_UINTN;
102   }
103 
104   //
105   // Validate the input size big enough to touch the first node.
106   //
107   if (MaxSize < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
108     return FALSE;
109   }
110 
111   for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {
112     NodeLength = DevicePathNodeLength (DevicePath);
113     if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
114       return FALSE;
115     }
116 
117     if (NodeLength > MAX_UINTN - Size) {
118       return FALSE;
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       return FALSE;
143     }
144   }
145 
146   //
147   // Only return TRUE when the End Device Path node is valid.
148   //
149   return (BOOLEAN) (DevicePathNodeLength (DevicePath) == END_DEVICE_PATH_LENGTH);
150 }
151 
152 /**
153   Returns the Type field of a device path node.
154 
155   Returns the Type field of the device path node specified by Node.
156 
157   If Node is NULL, then ASSERT().
158 
159   @param  Node      A pointer to a device path node data structure.
160 
161   @return The Type field of the device path node specified by Node.
162 
163 **/
164 UINT8
165 EFIAPI
DevicePathType(IN CONST VOID * Node)166 DevicePathType (
167   IN CONST VOID  *Node
168   )
169 {
170   ASSERT (Node != NULL);
171   return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Type;
172 }
173 
174 /**
175   Returns the SubType field of a device path node.
176 
177   Returns the SubType field of the device path node specified by Node.
178 
179   If Node is NULL, then ASSERT().
180 
181   @param  Node      A pointer to a device path node data structure.
182 
183   @return The SubType field of the device path node specified by Node.
184 
185 **/
186 UINT8
187 EFIAPI
DevicePathSubType(IN CONST VOID * Node)188 DevicePathSubType (
189   IN CONST VOID  *Node
190   )
191 {
192   ASSERT (Node != NULL);
193   return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->SubType;
194 }
195 
196 /**
197   Returns the 16-bit Length field of a device path node.
198 
199   Returns the 16-bit Length field of the device path node specified by Node.
200   Node is not required to be aligned on a 16-bit boundary, so it is recommended
201   that a function such as ReadUnaligned16() be used to extract the contents of
202   the Length field.
203 
204   If Node is NULL, then ASSERT().
205 
206   @param  Node      A pointer to a device path node data structure.
207 
208   @return The 16-bit Length field of the device path node specified by Node.
209 
210 **/
211 UINTN
212 EFIAPI
DevicePathNodeLength(IN CONST VOID * Node)213 DevicePathNodeLength (
214   IN CONST VOID  *Node
215   )
216 {
217   ASSERT (Node != NULL);
218   return ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);
219 }
220 
221 /**
222   Returns a pointer to the next node in a device path.
223 
224   Returns a pointer to the device path node that follows the device path node
225   specified by Node.
226 
227   If Node is NULL, then ASSERT().
228 
229   @param  Node      A pointer to a device path node data structure.
230 
231   @return a pointer to the device path node that follows the device path node
232   specified by Node.
233 
234 **/
235 EFI_DEVICE_PATH_PROTOCOL *
236 EFIAPI
NextDevicePathNode(IN CONST VOID * Node)237 NextDevicePathNode (
238   IN CONST VOID  *Node
239   )
240 {
241   ASSERT (Node != NULL);
242   return (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)(Node) + DevicePathNodeLength(Node));
243 }
244 
245 /**
246   Determines if a device path node is an end node of a device path.
247   This includes nodes that are the end of a device path instance and nodes that
248   are the end of an entire device path.
249 
250   Determines if the device path node specified by Node is an end node of a device path.
251   This includes nodes that are the end of a device path instance and nodes that are the
252   end of an entire device path.  If Node represents an end node of a device path,
253   then TRUE is returned.  Otherwise, FALSE is returned.
254 
255   If Node is NULL, then ASSERT().
256 
257   @param  Node      A pointer to a device path node data structure.
258 
259   @retval TRUE      The device path node specified by Node is an end node of a device path.
260   @retval FALSE     The device path node specified by Node is not an end node of
261                     a device path.
262 
263 **/
264 BOOLEAN
265 EFIAPI
IsDevicePathEndType(IN CONST VOID * Node)266 IsDevicePathEndType (
267   IN CONST VOID  *Node
268   )
269 {
270   ASSERT (Node != NULL);
271   return (BOOLEAN) (DevicePathType (Node) == END_DEVICE_PATH_TYPE);
272 }
273 
274 /**
275   Determines if a device path node is an end node of an entire device path.
276 
277   Determines if a device path node specified by Node is an end node of an entire
278   device path.
279   If Node represents the end of an entire device path, then TRUE is returned.
280   Otherwise, FALSE is returned.
281 
282   If Node is NULL, then ASSERT().
283 
284   @param  Node      A pointer to a device path node data structure.
285 
286   @retval TRUE      The device path node specified by Node is the end of an entire device path.
287   @retval FALSE     The device path node specified by Node is not the end of an entire device path.
288 
289 **/
290 BOOLEAN
291 EFIAPI
IsDevicePathEnd(IN CONST VOID * Node)292 IsDevicePathEnd (
293   IN CONST VOID  *Node
294   )
295 {
296   ASSERT (Node != NULL);
297   return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);
298 }
299 
300 /**
301   Determines if a device path node is an end node of a device path instance.
302 
303   Determines if a device path node specified by Node is an end node of a device
304   path instance.
305   If Node represents the end of a device path instance, then TRUE is returned.
306   Otherwise, FALSE is returned.
307 
308   If Node is NULL, then ASSERT().
309 
310   @param  Node      A pointer to a device path node data structure.
311 
312   @retval TRUE      The device path node specified by Node is the end of a device
313   path instance.
314   @retval FALSE     The device path node specified by Node is not the end of a
315   device path instance.
316 
317 **/
318 BOOLEAN
319 EFIAPI
IsDevicePathEndInstance(IN CONST VOID * Node)320 IsDevicePathEndInstance (
321   IN CONST VOID  *Node
322   )
323 {
324   ASSERT (Node != NULL);
325   return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_INSTANCE_DEVICE_PATH_SUBTYPE);
326 }
327 
328 /**
329   Sets the length, in bytes, of a device path node.
330 
331   Sets the length of the device path node specified by Node to the value specified
332   by NodeLength.  NodeLength is returned.  Node is not required to be aligned on
333   a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()
334   be used to set the contents of the Length field.
335 
336   If Node is NULL, then ASSERT().
337   If NodeLength >= SIZE_64KB, then ASSERT().
338   If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().
339 
340   @param  Node      A pointer to a device path node data structure.
341   @param  Length    The length, in bytes, of the device path node.
342 
343   @return Length
344 
345 **/
346 UINT16
347 EFIAPI
SetDevicePathNodeLength(IN OUT VOID * Node,IN UINTN Length)348 SetDevicePathNodeLength (
349   IN OUT VOID  *Node,
350   IN UINTN     Length
351   )
352 {
353   ASSERT (Node != NULL);
354   ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB));
355   return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));
356 }
357 
358 /**
359   Fills in all the fields of a device path node that is the end of an entire device path.
360 
361   Fills in all the fields of a device path node specified by Node so Node represents
362   the end of an entire device path.  The Type field of Node is set to
363   END_DEVICE_PATH_TYPE, the SubType field of Node is set to
364   END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to
365   END_DEVICE_PATH_LENGTH.  Node is not required to be aligned on a 16-bit boundary,
366   so it is recommended that a function such as WriteUnaligned16() be used to set
367   the contents of the Length field.
368 
369   If Node is NULL, then ASSERT().
370 
371   @param  Node      A pointer to a device path node data structure.
372 
373 **/
374 VOID
375 EFIAPI
SetDevicePathEndNode(OUT VOID * Node)376 SetDevicePathEndNode (
377   OUT VOID  *Node
378   )
379 {
380   ASSERT (Node != NULL);
381   CopyMem (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));
382 }
383 
384 /**
385   Returns the size of a device path in bytes.
386 
387   This function returns the size, in bytes, of the device path data structure
388   specified by DevicePath including the end of device path node.
389   If DevicePath is NULL or invalid, then 0 is returned.
390 
391   @param  DevicePath  A pointer to a device path data structure.
392 
393   @retval 0           If DevicePath is NULL or invalid.
394   @retval Others      The size of a device path in bytes.
395 
396 **/
397 UINTN
398 EFIAPI
GetDevicePathSize(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath)399 GetDevicePathSize (
400   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath
401   )
402 {
403   return mDevicePathLibDevicePathUtilities->GetDevicePathSize (DevicePath);
404 }
405 
406 /**
407   Creates a new copy of an existing device path.
408 
409   This function allocates space for a new copy of the device path specified by
410   DevicePath.  If DevicePath is NULL, then NULL is returned.
411   If the memory is successfully allocated, then the
412   contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
413   is returned.  Otherwise, NULL is returned.
414   The memory for the new device path is allocated from EFI boot services memory.
415   It is the responsibility of the caller to free the memory allocated.
416 
417   @param  DevicePath                 A pointer to a device path data structure.
418 
419   @retval NULL    If DevicePath is NULL or invalid.
420   @retval Others  A pointer to the duplicated device path.
421 
422 **/
423 EFI_DEVICE_PATH_PROTOCOL *
424 EFIAPI
DuplicateDevicePath(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath)425 DuplicateDevicePath (
426   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath
427   )
428 {
429   return mDevicePathLibDevicePathUtilities->DuplicateDevicePath (DevicePath);
430 }
431 
432 /**
433   Creates a new device path by appending a second device path to a first device path.
434 
435   This function creates a new device path by appending a copy of SecondDevicePath to a copy of
436   FirstDevicePath in a newly allocated buffer.  Only the end-of-device-path device node from
437   SecondDevicePath is retained. The newly created device path is returned.
438   If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
439   If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
440   If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is
441   returned.
442   If there is not enough memory for the newly allocated buffer, then NULL is returned.
443   The memory for the new device path is allocated from EFI boot services memory. It is the
444   responsibility of the caller to free the memory allocated.
445 
446   @param  FirstDevicePath            A pointer to a device path data structure.
447   @param  SecondDevicePath           A pointer to a device path data structure.
448 
449   @retval NULL      If there is not enough memory for the newly allocated buffer.
450   @retval NULL      If FirstDevicePath or SecondDevicePath is invalid.
451   @retval Others    A pointer to the new device path if success.
452                     Or a copy an end-of-device-path if both FirstDevicePath and
453                     SecondDevicePath are NULL.
454 
455 **/
456 EFI_DEVICE_PATH_PROTOCOL *
457 EFIAPI
AppendDevicePath(IN CONST EFI_DEVICE_PATH_PROTOCOL * FirstDevicePath,OPTIONAL IN CONST EFI_DEVICE_PATH_PROTOCOL * SecondDevicePath OPTIONAL)458 AppendDevicePath (
459   IN CONST EFI_DEVICE_PATH_PROTOCOL  *FirstDevicePath,  OPTIONAL
460   IN CONST EFI_DEVICE_PATH_PROTOCOL  *SecondDevicePath  OPTIONAL
461   )
462 {
463   return mDevicePathLibDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);
464 }
465 
466 /**
467   Creates a new path by appending the device node to the device path.
468 
469   This function creates a new device path by appending a copy of the device node
470   specified by DevicePathNode to a copy of the device path specified by DevicePath
471   in an allocated buffer.
472   The end-of-device-path device node is moved after the end of the appended device node.
473   If DevicePathNode is NULL then a copy of DevicePath is returned.
474   If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
475   path device node is returned.
476   If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
477   device node is returned.
478   If there is not enough memory to allocate space for the new device path, then
479   NULL is returned.
480   The memory is allocated from EFI boot services memory. It is the responsibility
481   of the caller to free the memory allocated.
482 
483   @param  DevicePath                 A pointer to a device path data structure.
484   @param  DevicePathNode             A pointer to a single device path node.
485 
486   @retval NULL      If there is not enough memory for the new device path.
487   @retval Others    A pointer to the new device path if success.
488                     A copy of DevicePathNode followed by an end-of-device-path node
489                     if both FirstDevicePath and SecondDevicePath are NULL.
490                     A copy of an end-of-device-path node if both FirstDevicePath
491                     and SecondDevicePath are NULL.
492 
493 **/
494 EFI_DEVICE_PATH_PROTOCOL *
495 EFIAPI
AppendDevicePathNode(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath,OPTIONAL IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePathNode OPTIONAL)496 AppendDevicePathNode (
497   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath,     OPTIONAL
498   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePathNode  OPTIONAL
499   )
500 {
501   return mDevicePathLibDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);
502 }
503 
504 /**
505   Creates a new device path by appending the specified device path instance to
506   the specified device path.
507 
508   This function creates a new device path by appending a copy of the device path
509   instance specified by DevicePathInstance to a copy of the device path specified
510   by DevicePath in a allocated buffer.
511   The end-of-device-path device node is moved after the end of the appended device
512   path instance and a new end-of-device-path-instance node is inserted between.
513   If DevicePath is NULL, then a copy if DevicePathInstance is returned.
514   If DevicePathInstance is NULL, then NULL is returned.
515   If DevicePath or DevicePathInstance is invalid, then NULL is returned.
516   If there is not enough memory to allocate space for the new device path, then
517   NULL is returned.
518   The memory is allocated from EFI boot services memory. It is the responsibility
519   of the caller to free the memory allocated.
520 
521   @param  DevicePath                 A pointer to a device path data structure.
522   @param  DevicePathInstance         A pointer to a device path instance.
523 
524   @return A pointer to the new device path.
525 
526 **/
527 EFI_DEVICE_PATH_PROTOCOL *
528 EFIAPI
AppendDevicePathInstance(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath,OPTIONAL IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePathInstance OPTIONAL)529 AppendDevicePathInstance (
530   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath,        OPTIONAL
531   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePathInstance OPTIONAL
532   )
533 {
534   return mDevicePathLibDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);
535 }
536 
537 /**
538   Creates a copy of the current device path instance and returns a pointer to the
539   next device path instance.
540 
541   This function creates a copy of the current device path instance. It also updates
542   DevicePath to point to the next device path instance in the device path (or NULL
543   if no more) and updates Size to hold the size of the device path instance copy.
544   If DevicePath is NULL, then NULL is returned.
545   If there is not enough memory to allocate space for the new device path, then
546   NULL is returned.
547   The memory is allocated from EFI boot services memory. It is the responsibility
548   of the caller to free the memory allocated.
549   If Size is NULL, then ASSERT().
550 
551   @param  DevicePath                 On input, this holds the pointer to the current
552                                      device path instance. On output, this holds
553                                      the pointer to the next device path instance
554                                      or NULL if there are no more device path
555                                      instances in the device path pointer to a
556                                      device path data structure.
557   @param  Size                       On output, this holds the size of the device
558                                      path instance, in bytes or zero, if DevicePath
559                                      is NULL.
560 
561   @return A pointer to the current device path instance.
562 
563 **/
564 EFI_DEVICE_PATH_PROTOCOL *
565 EFIAPI
GetNextDevicePathInstance(IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,OUT UINTN * Size)566 GetNextDevicePathInstance (
567   IN OUT EFI_DEVICE_PATH_PROTOCOL    **DevicePath,
568   OUT UINTN                          *Size
569   )
570 {
571   ASSERT (Size != NULL);
572   return mDevicePathLibDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);
573 }
574 
575 /**
576   Creates a device node.
577 
578   This function creates a new device node in a newly allocated buffer of size
579   NodeLength and initializes the device path node header with NodeType and NodeSubType.
580   The new device path node is returned.
581   If NodeLength is smaller than a device path header, then NULL is returned.
582   If there is not enough memory to allocate space for the new device path, then
583   NULL is returned.
584   The memory is allocated from EFI boot services memory. It is the responsibility
585   of the caller to free the memory allocated.
586 
587   @param  NodeType                   The device node type for the new device node.
588   @param  NodeSubType                The device node sub-type for the new device node.
589   @param  NodeLength                 The length of the new device node.
590 
591   @return The new device path.
592 
593 **/
594 EFI_DEVICE_PATH_PROTOCOL *
595 EFIAPI
CreateDeviceNode(IN UINT8 NodeType,IN UINT8 NodeSubType,IN UINT16 NodeLength)596 CreateDeviceNode (
597   IN UINT8                           NodeType,
598   IN UINT8                           NodeSubType,
599   IN UINT16                          NodeLength
600   )
601 {
602   return mDevicePathLibDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);
603 }
604 
605 /**
606   Determines if a device path is single or multi-instance.
607 
608   This function returns TRUE if the device path specified by DevicePath is
609   multi-instance.
610   Otherwise, FALSE is returned.
611   If DevicePath is NULL or invalid, then FALSE is returned.
612 
613   @param  DevicePath                 A pointer to a device path data structure.
614 
615   @retval  TRUE                      DevicePath is multi-instance.
616   @retval  FALSE                     DevicePath is not multi-instance, or DevicePath
617                                      is NULL or invalid.
618 
619 **/
620 BOOLEAN
621 EFIAPI
IsDevicePathMultiInstance(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath)622 IsDevicePathMultiInstance (
623   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath
624   )
625 {
626   return mDevicePathLibDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);
627 }
628 
629 /**
630   Retrieves the device path protocol from a handle.
631 
632   This function returns the device path protocol from the handle specified by Handle.
633   If Handle is NULL or Handle does not contain a device path protocol, then NULL
634   is returned.
635 
636   @param  Handle                     The handle from which to retrieve the device
637                                      path protocol.
638 
639   @return The device path protocol from the handle specified by Handle.
640 
641 **/
642 EFI_DEVICE_PATH_PROTOCOL *
643 EFIAPI
DevicePathFromHandle(IN EFI_HANDLE Handle)644 DevicePathFromHandle (
645   IN EFI_HANDLE                      Handle
646   )
647 {
648   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
649   EFI_STATUS                Status;
650 
651   Status = gBS->HandleProtocol (
652                   Handle,
653                   &gEfiDevicePathProtocolGuid,
654                   (VOID *) &DevicePath
655                   );
656   if (EFI_ERROR (Status)) {
657     DevicePath = NULL;
658   }
659   return DevicePath;
660 }
661 
662 /**
663   Allocates a device path for a file and appends it to an existing device path.
664 
665   If Device is a valid device handle that contains a device path protocol, then
666   a device path for the file specified by FileName  is allocated and appended to
667   the device path associated with the handle Device.  The allocated device path
668   is returned.  If Device is NULL or Device is a handle that does not support the
669   device path protocol, then a device path containing a single device path node
670   for the file specified by FileName is allocated and returned.
671   The memory for the new device path is allocated from EFI boot services memory.
672   It is the responsibility of the caller to free the memory allocated.
673 
674   If FileName is NULL, then ASSERT().
675   If FileName is not aligned on a 16-bit boundary, then ASSERT().
676 
677   @param  Device                     A pointer to a device handle.  This parameter
678                                      is optional and may be NULL.
679   @param  FileName                   A pointer to a Null-terminated Unicode string.
680 
681   @return The allocated device path.
682 
683 **/
684 EFI_DEVICE_PATH_PROTOCOL *
685 EFIAPI
FileDevicePath(IN EFI_HANDLE Device,OPTIONAL IN CONST CHAR16 * FileName)686 FileDevicePath (
687   IN EFI_HANDLE                      Device,     OPTIONAL
688   IN CONST CHAR16                    *FileName
689   )
690 {
691   UINTN                     Size;
692   FILEPATH_DEVICE_PATH      *FilePath;
693   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
694   EFI_DEVICE_PATH_PROTOCOL  *FileDevicePath;
695 
696   DevicePath = NULL;
697 
698   Size = StrSize (FileName);
699   FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + END_DEVICE_PATH_LENGTH);
700   if (FileDevicePath != NULL) {
701     FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;
702     FilePath->Header.Type    = MEDIA_DEVICE_PATH;
703     FilePath->Header.SubType = MEDIA_FILEPATH_DP;
704     CopyMem (&FilePath->PathName, FileName, Size);
705     SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);
706     SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));
707 
708     if (Device != NULL) {
709       DevicePath = DevicePathFromHandle (Device);
710     }
711 
712     DevicePath = AppendDevicePath (DevicePath, FileDevicePath);
713     FreePool (FileDevicePath);
714   }
715 
716   return DevicePath;
717 }
718 
719 /**
720   Locate and return the protocol instance identified by the ProtocolGuid.
721 
722   @param ProtocolGuid     The GUID of the protocol.
723 
724   @return A pointer to the protocol instance or NULL when absent.
725 **/
726 VOID *
UefiDevicePathLibLocateProtocol(EFI_GUID * ProtocolGuid)727 UefiDevicePathLibLocateProtocol (
728   EFI_GUID                         *ProtocolGuid
729   )
730 {
731   EFI_STATUS Status;
732   VOID       *Protocol;
733   Status = gBS->LocateProtocol (
734                   ProtocolGuid,
735                   NULL,
736                   (VOID**) &Protocol
737                   );
738   if (EFI_ERROR (Status)) {
739     return NULL;
740   } else {
741     return Protocol;
742   }
743 }
744 
745 /**
746   Converts a device node to its string representation.
747 
748   @param DeviceNode        A Pointer to the device node to be converted.
749   @param DisplayOnly       If DisplayOnly is TRUE, then the shorter text representation
750                            of the display node is used, where applicable. If DisplayOnly
751                            is FALSE, then the longer text representation of the display node
752                            is used.
753   @param AllowShortcuts    If AllowShortcuts is TRUE, then the shortcut forms of text
754                            representation for a device node can be used, where applicable.
755 
756   @return A pointer to the allocated text representation of the device node or NULL if DeviceNode
757           is NULL or there was insufficient memory.
758 
759 **/
760 CHAR16 *
761 EFIAPI
ConvertDeviceNodeToText(IN CONST EFI_DEVICE_PATH_PROTOCOL * DeviceNode,IN BOOLEAN DisplayOnly,IN BOOLEAN AllowShortcuts)762 ConvertDeviceNodeToText (
763   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DeviceNode,
764   IN BOOLEAN                         DisplayOnly,
765   IN BOOLEAN                         AllowShortcuts
766   )
767 {
768   if (mDevicePathLibDevicePathToText == NULL) {
769     mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);
770   }
771   if (mDevicePathLibDevicePathToText != NULL) {
772     return mDevicePathLibDevicePathToText->ConvertDeviceNodeToText (DeviceNode, DisplayOnly, AllowShortcuts);
773   } else {
774     return NULL;
775   }
776 }
777 
778 /**
779   Converts a device path to its text representation.
780 
781   @param DevicePath      A Pointer to the device to be converted.
782   @param DisplayOnly     If DisplayOnly is TRUE, then the shorter text representation
783                          of the display node is used, where applicable. If DisplayOnly
784                          is FALSE, then the longer text representation of the display node
785                          is used.
786   @param AllowShortcuts  If AllowShortcuts is TRUE, then the shortcut forms of text
787                          representation for a device node can be used, where applicable.
788 
789   @return A pointer to the allocated text representation of the device path or
790           NULL if DeviceNode is NULL or there was insufficient memory.
791 
792 **/
793 CHAR16 *
794 EFIAPI
ConvertDevicePathToText(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath,IN BOOLEAN DisplayOnly,IN BOOLEAN AllowShortcuts)795 ConvertDevicePathToText (
796   IN CONST EFI_DEVICE_PATH_PROTOCOL   *DevicePath,
797   IN BOOLEAN                          DisplayOnly,
798   IN BOOLEAN                          AllowShortcuts
799   )
800 {
801   if (mDevicePathLibDevicePathToText == NULL) {
802     mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);
803   }
804   if (mDevicePathLibDevicePathToText != NULL) {
805     return mDevicePathLibDevicePathToText->ConvertDevicePathToText (DevicePath, DisplayOnly, AllowShortcuts);
806   } else {
807     return NULL;
808   }
809 }
810 
811 /**
812   Convert text to the binary representation of a device node.
813 
814   @param TextDeviceNode  TextDeviceNode points to the text representation of a device
815                          node. Conversion starts with the first character and continues
816                          until the first non-device node character.
817 
818   @return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was
819           insufficient memory or text unsupported.
820 
821 **/
822 EFI_DEVICE_PATH_PROTOCOL *
823 EFIAPI
ConvertTextToDeviceNode(IN CONST CHAR16 * TextDeviceNode)824 ConvertTextToDeviceNode (
825   IN CONST CHAR16 *TextDeviceNode
826   )
827 {
828   if (mDevicePathLibDevicePathFromText == NULL) {
829     mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);
830   }
831   if (mDevicePathLibDevicePathFromText != NULL) {
832     return mDevicePathLibDevicePathFromText->ConvertTextToDeviceNode (TextDeviceNode);
833   } else {
834     return NULL;
835   }
836 }
837 
838 /**
839   Convert text to the binary representation of a device path.
840 
841 
842   @param TextDevicePath  TextDevicePath points to the text representation of a device
843                          path. Conversion starts with the first character and continues
844                          until the first non-device node character.
845 
846   @return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or
847           there was insufficient memory.
848 
849 **/
850 EFI_DEVICE_PATH_PROTOCOL *
851 EFIAPI
ConvertTextToDevicePath(IN CONST CHAR16 * TextDevicePath)852 ConvertTextToDevicePath (
853   IN CONST CHAR16 *TextDevicePath
854   )
855 {
856   if (mDevicePathLibDevicePathFromText == NULL) {
857     mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);
858   }
859   if (mDevicePathLibDevicePathFromText != NULL) {
860     return mDevicePathLibDevicePathFromText->ConvertTextToDevicePath (TextDevicePath);
861   } else {
862     return NULL;
863   }
864 }
865 
866