2 Device Path services. The thing to remember is device paths are built out of
3 nodes. The device path is terminated by an end node that is length
4 sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)
7 The only place where multi-instance device paths are supported is in
8 environment varibles. Multi-instance device paths should never be placed
11 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
12 SPDX-License-Identifier: BSD-2-Clause-Patent
16 #include "UefiDevicePathLib.h"
19 // Template for an end-of-device path node.
21 GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath
= {
23 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
25 END_DEVICE_PATH_LENGTH
,
31 Determine whether a given device path is valid.
33 @param DevicePath A pointer to a device path data structure.
34 @param MaxSize The maximum size of the device path data structure.
36 @retval TRUE DevicePath is valid.
37 @retval FALSE DevicePath is NULL.
38 @retval FALSE Maxsize is less than sizeof(EFI_DEVICE_PATH_PROTOCOL).
39 @retval FALSE The length of any node node in the DevicePath is less
40 than sizeof (EFI_DEVICE_PATH_PROTOCOL).
41 @retval FALSE If MaxSize is not zero, the size of the DevicePath
43 @retval FALSE If PcdMaximumDevicePathNodeCount is not zero, the node
44 count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.
49 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
58 //Validate the input whether exists and its size big enough to touch the first node
60 if (DevicePath
== NULL
|| (MaxSize
> 0 && MaxSize
< END_DEVICE_PATH_LENGTH
)) {
68 for (Count
= 0, Size
= 0; !IsDevicePathEnd (DevicePath
); DevicePath
= NextDevicePathNode (DevicePath
)) {
69 NodeLength
= DevicePathNodeLength (DevicePath
);
70 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
74 if (NodeLength
> MAX_UINTN
- Size
) {
80 // Validate next node before touch it.
82 if (Size
> MaxSize
- END_DEVICE_PATH_LENGTH
) {
86 if (PcdGet32 (PcdMaximumDevicePathNodeCount
) > 0) {
88 if (Count
>= PcdGet32 (PcdMaximumDevicePathNodeCount
)) {
94 // FilePath must be a NULL-terminated string.
96 if (DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
&&
97 DevicePathSubType (DevicePath
) == MEDIA_FILEPATH_DP
&&
98 *(CHAR16
*)((UINT8
*)DevicePath
+ NodeLength
- 2) != 0) {
104 // Only return TRUE when the End Device Path node is valid.
106 return (BOOLEAN
) (DevicePathNodeLength (DevicePath
) == END_DEVICE_PATH_LENGTH
);
111 Returns the Type field of a device path node.
113 Returns the Type field of the device path node specified by Node.
115 If Node is NULL, then ASSERT().
117 @param Node A pointer to a device path node data structure.
119 @return The Type field of the device path node specified by Node.
128 ASSERT (Node
!= NULL
);
129 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Type
;
133 Returns the SubType field of a device path node.
135 Returns the SubType field of the device path node specified by Node.
137 If Node is NULL, then ASSERT().
139 @param Node A pointer to a device path node data structure.
141 @return The SubType field of the device path node specified by Node.
150 ASSERT (Node
!= NULL
);
151 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->SubType
;
155 Returns the 16-bit Length field of a device path node.
157 Returns the 16-bit Length field of the device path node specified by Node.
158 Node is not required to be aligned on a 16-bit boundary, so it is recommended
159 that a function such as ReadUnaligned16() be used to extract the contents of
162 If Node is NULL, then ASSERT().
164 @param Node A pointer to a device path node data structure.
166 @return The 16-bit Length field of the device path node specified by Node.
171 DevicePathNodeLength (
175 ASSERT (Node
!= NULL
);
176 return ReadUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0]);
180 Returns a pointer to the next node in a device path.
182 Returns a pointer to the device path node that follows the device path node
185 If Node is NULL, then ASSERT().
187 @param Node A pointer to a device path node data structure.
189 @return a pointer to the device path node that follows the device path node
193 EFI_DEVICE_PATH_PROTOCOL
*
199 ASSERT (Node
!= NULL
);
200 return (EFI_DEVICE_PATH_PROTOCOL
*)((UINT8
*)(Node
) + DevicePathNodeLength(Node
));
204 Determines if a device path node is an end node of a device path.
205 This includes nodes that are the end of a device path instance and nodes that
206 are the end of an entire device path.
208 Determines if the device path node specified by Node is an end node of a device path.
209 This includes nodes that are the end of a device path instance and nodes that are the
210 end of an entire device path. If Node represents an end node of a device path,
211 then TRUE is returned. Otherwise, FALSE is returned.
213 If Node is NULL, then ASSERT().
215 @param Node A pointer to a device path node data structure.
217 @retval TRUE The device path node specified by Node is an end node of a
219 @retval FALSE The device path node specified by Node is not an end node of
225 IsDevicePathEndType (
229 ASSERT (Node
!= NULL
);
230 return (BOOLEAN
) (DevicePathType (Node
) == END_DEVICE_PATH_TYPE
);
234 Determines if a device path node is an end node of an entire device path.
236 Determines if a device path node specified by Node is an end node of an entire
237 device path. If Node represents the end of an entire device path, then TRUE is
238 returned. Otherwise, FALSE is returned.
240 If Node is NULL, then ASSERT().
242 @param Node A pointer to a device path node data structure.
244 @retval TRUE The device path node specified by Node is the end of an entire
246 @retval FALSE The device path node specified by Node is not the end of an
256 ASSERT (Node
!= NULL
);
257 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
);
261 Determines if a device path node is an end node of a device path instance.
263 Determines if a device path node specified by Node is an end node of a device
264 path instance. If Node represents the end of a device path instance, then TRUE
265 is returned. Otherwise, FALSE is returned.
267 If Node is NULL, then ASSERT().
269 @param Node A pointer to a device path node data structure.
271 @retval TRUE The device path node specified by Node is the end of a device
273 @retval FALSE The device path node specified by Node is not the end of a
274 device path instance.
279 IsDevicePathEndInstance (
283 ASSERT (Node
!= NULL
);
284 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_INSTANCE_DEVICE_PATH_SUBTYPE
);
288 Sets the length, in bytes, of a device path node.
290 Sets the length of the device path node specified by Node to the value specified
291 by NodeLength. NodeLength is returned. Node is not required to be aligned on
292 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()
293 be used to set the contents of the Length field.
295 If Node is NULL, then ASSERT().
296 If NodeLength >= SIZE_64KB, then ASSERT().
297 If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().
299 @param Node A pointer to a device path node data structure.
300 @param Length The length, in bytes, of the device path node.
307 SetDevicePathNodeLength (
312 ASSERT (Node
!= NULL
);
313 ASSERT ((Length
>= sizeof (EFI_DEVICE_PATH_PROTOCOL
)) && (Length
< SIZE_64KB
));
314 return WriteUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0], (UINT16
)(Length
));
318 Fills in all the fields of a device path node that is the end of an entire device path.
320 Fills in all the fields of a device path node specified by Node so Node represents
321 the end of an entire device path. The Type field of Node is set to
322 END_DEVICE_PATH_TYPE, the SubType field of Node is set to
323 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to
324 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary,
325 so it is recommended that a function such as WriteUnaligned16() be used to set
326 the contents of the Length field.
328 If Node is NULL, then ASSERT().
330 @param Node A pointer to a device path node data structure.
335 SetDevicePathEndNode (
339 ASSERT (Node
!= NULL
);
340 CopyMem (Node
, &mUefiDevicePathLibEndDevicePath
, sizeof (mUefiDevicePathLibEndDevicePath
));
344 Returns the size of a device path in bytes.
346 This function returns the size, in bytes, of the device path data structure
347 specified by DevicePath including the end of device path node.
348 If DevicePath is NULL or invalid, then 0 is returned.
350 @param DevicePath A pointer to a device path data structure.
352 @retval 0 If DevicePath is NULL or invalid.
353 @retval Others The size of a device path in bytes.
358 UefiDevicePathLibGetDevicePathSize (
359 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
362 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
364 if (DevicePath
== NULL
) {
368 if (!IsDevicePathValid (DevicePath
, 0)) {
373 // Search for the end of the device path structure
376 while (!IsDevicePathEnd (DevicePath
)) {
377 DevicePath
= NextDevicePathNode (DevicePath
);
381 // Compute the size and add back in the size of the end device path structure
383 return ((UINTN
) DevicePath
- (UINTN
) Start
) + DevicePathNodeLength (DevicePath
);
387 Creates a new copy of an existing device path.
389 This function allocates space for a new copy of the device path specified by DevicePath.
390 If DevicePath is NULL, then NULL is returned. If the memory is successfully
391 allocated, then the contents of DevicePath are copied to the newly allocated
392 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.
393 The memory for the new device path is allocated from EFI boot services memory.
394 It is the responsibility of the caller to free the memory allocated.
396 @param DevicePath A pointer to a device path data structure.
398 @retval NULL DevicePath is NULL or invalid.
399 @retval Others A pointer to the duplicated device path.
402 EFI_DEVICE_PATH_PROTOCOL
*
404 UefiDevicePathLibDuplicateDevicePath (
405 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
413 Size
= GetDevicePathSize (DevicePath
);
419 // Allocate space for duplicate device path
422 return AllocateCopyPool (Size
, DevicePath
);
426 Creates a new device path by appending a second device path to a first device path.
428 This function creates a new device path by appending a copy of SecondDevicePath
429 to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path
430 device node from SecondDevicePath is retained. The newly created device path is
431 returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
432 SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,
433 and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
434 SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.
436 If there is not enough memory for the newly allocated buffer, then NULL is returned.
437 The memory for the new device path is allocated from EFI boot services memory.
438 It is the responsibility of the caller to free the memory allocated.
440 @param FirstDevicePath A pointer to a device path data structure.
441 @param SecondDevicePath A pointer to a device path data structure.
443 @retval NULL If there is not enough memory for the newly allocated buffer.
444 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.
445 @retval Others A pointer to the new device path if success.
446 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
449 EFI_DEVICE_PATH_PROTOCOL
*
451 UefiDevicePathLibAppendDevicePath (
452 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
453 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
459 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
460 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
463 // If there's only 1 path, just duplicate it.
465 if (FirstDevicePath
== NULL
) {
466 return DuplicateDevicePath ((SecondDevicePath
!= NULL
) ? SecondDevicePath
: &mUefiDevicePathLibEndDevicePath
);
469 if (SecondDevicePath
== NULL
) {
470 return DuplicateDevicePath (FirstDevicePath
);
473 if (!IsDevicePathValid (FirstDevicePath
, 0) || !IsDevicePathValid (SecondDevicePath
, 0)) {
478 // Allocate space for the combined device path. It only has one end node of
479 // length EFI_DEVICE_PATH_PROTOCOL.
481 Size1
= GetDevicePathSize (FirstDevicePath
);
482 Size2
= GetDevicePathSize (SecondDevicePath
);
483 Size
= Size1
+ Size2
- END_DEVICE_PATH_LENGTH
;
485 NewDevicePath
= AllocatePool (Size
);
487 if (NewDevicePath
!= NULL
) {
488 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
490 // Over write FirstDevicePath EndNode and do the copy
492 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
493 (Size1
- END_DEVICE_PATH_LENGTH
));
494 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
497 return NewDevicePath
;
501 Creates a new path by appending the device node to the device path.
503 This function creates a new device path by appending a copy of the device node
504 specified by DevicePathNode to a copy of the device path specified by DevicePath
505 in an allocated buffer. The end-of-device-path device node is moved after the
506 end of the appended device node.
507 If DevicePathNode is NULL then a copy of DevicePath is returned.
508 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
509 path device node is returned.
510 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
511 device node is returned.
512 If there is not enough memory to allocate space for the new device path, then
514 The memory is allocated from EFI boot services memory. It is the responsibility
515 of the caller to free the memory allocated.
517 @param DevicePath A pointer to a device path data structure.
518 @param DevicePathNode A pointer to a single device path node.
520 @retval NULL If there is not enough memory for the new device path.
521 @retval Others A pointer to the new device path if success.
522 A copy of DevicePathNode followed by an end-of-device-path node
523 if both FirstDevicePath and SecondDevicePath are NULL.
524 A copy of an end-of-device-path node if both FirstDevicePath
525 and SecondDevicePath are NULL.
528 EFI_DEVICE_PATH_PROTOCOL
*
530 UefiDevicePathLibAppendDevicePathNode (
531 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
532 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
535 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
536 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
537 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
540 if (DevicePathNode
== NULL
) {
541 return DuplicateDevicePath ((DevicePath
!= NULL
) ? DevicePath
: &mUefiDevicePathLibEndDevicePath
);
544 // Build a Node that has a terminator on it
546 NodeLength
= DevicePathNodeLength (DevicePathNode
);
548 TempDevicePath
= AllocatePool (NodeLength
+ END_DEVICE_PATH_LENGTH
);
549 if (TempDevicePath
== NULL
) {
552 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
554 // Add and end device path node to convert Node to device path
556 NextNode
= NextDevicePathNode (TempDevicePath
);
557 SetDevicePathEndNode (NextNode
);
559 // Append device paths
561 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
563 FreePool (TempDevicePath
);
565 return NewDevicePath
;
569 Creates a new device path by appending the specified device path instance to the specified device
572 This function creates a new device path by appending a copy of the device path
573 instance specified by DevicePathInstance to a copy of the device path specified
574 by DevicePath in a allocated buffer.
575 The end-of-device-path device node is moved after the end of the appended device
576 path instance and a new end-of-device-path-instance node is inserted between.
577 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
578 If DevicePathInstance is NULL, then NULL is returned.
579 If DevicePath or DevicePathInstance is invalid, then NULL is returned.
580 If there is not enough memory to allocate space for the new device path, then
582 The memory is allocated from EFI boot services memory. It is the responsibility
583 of the caller to free the memory allocated.
585 @param DevicePath A pointer to a device path data structure.
586 @param DevicePathInstance A pointer to a device path instance.
588 @return A pointer to the new device path.
591 EFI_DEVICE_PATH_PROTOCOL
*
593 UefiDevicePathLibAppendDevicePathInstance (
594 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
595 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
598 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
599 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
603 if (DevicePath
== NULL
) {
604 return DuplicateDevicePath (DevicePathInstance
);
607 if (DevicePathInstance
== NULL
) {
611 if (!IsDevicePathValid (DevicePath
, 0) || !IsDevicePathValid (DevicePathInstance
, 0)) {
615 SrcSize
= GetDevicePathSize (DevicePath
);
616 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
618 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
619 if (NewDevicePath
!= NULL
) {
621 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
623 while (!IsDevicePathEnd (TempDevicePath
)) {
624 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
627 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
628 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
629 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
632 return NewDevicePath
;
636 Creates a copy of the current device path instance and returns a pointer to the next device path
639 This function creates a copy of the current device path instance. It also updates
640 DevicePath to point to the next device path instance in the device path (or NULL
641 if no more) and updates Size to hold the size of the device path instance copy.
642 If DevicePath is NULL, then NULL is returned.
643 If DevicePath points to a invalid device path, then NULL is returned.
644 If there is not enough memory to allocate space for the new device path, then
646 The memory is allocated from EFI boot services memory. It is the responsibility
647 of the caller to free the memory allocated.
648 If Size is NULL, then ASSERT().
650 @param DevicePath On input, this holds the pointer to the current
651 device path instance. On output, this holds
652 the pointer to the next device path instance
653 or NULL if there are no more device path
654 instances in the device path pointer to a
655 device path data structure.
656 @param Size On output, this holds the size of the device
657 path instance, in bytes or zero, if DevicePath
660 @return A pointer to the current device path instance.
663 EFI_DEVICE_PATH_PROTOCOL
*
665 UefiDevicePathLibGetNextDevicePathInstance (
666 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
670 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
671 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
674 ASSERT (Size
!= NULL
);
676 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
681 if (!IsDevicePathValid (*DevicePath
, 0)) {
686 // Find the end of the device path instance
688 DevPath
= *DevicePath
;
689 while (!IsDevicePathEndType (DevPath
)) {
690 DevPath
= NextDevicePathNode (DevPath
);
694 // Compute the size of the device path instance
696 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
699 // Make a copy and return the device path instance
701 Temp
= DevPath
->SubType
;
702 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
703 ReturnValue
= DuplicateDevicePath (*DevicePath
);
704 DevPath
->SubType
= Temp
;
707 // If DevPath is the end of an entire device path, then another instance
708 // does not follow, so *DevicePath is set to NULL.
710 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
713 *DevicePath
= NextDevicePathNode (DevPath
);
720 Creates a device node.
722 This function creates a new device node in a newly allocated buffer of size
723 NodeLength and initializes the device path node header with NodeType and NodeSubType.
724 The new device path node is returned.
725 If NodeLength is smaller than a device path header, then NULL is returned.
726 If there is not enough memory to allocate space for the new device path, then
728 The memory is allocated from EFI boot services memory. It is the responsibility
729 of the caller to free the memory allocated.
731 @param NodeType The device node type for the new device node.
732 @param NodeSubType The device node sub-type for the new device node.
733 @param NodeLength The length of the new device node.
735 @return The new device path.
738 EFI_DEVICE_PATH_PROTOCOL
*
740 UefiDevicePathLibCreateDeviceNode (
742 IN UINT8 NodeSubType
,
746 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
748 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
750 // NodeLength is less than the size of the header.
755 DevicePath
= AllocateZeroPool (NodeLength
);
756 if (DevicePath
!= NULL
) {
757 DevicePath
->Type
= NodeType
;
758 DevicePath
->SubType
= NodeSubType
;
759 SetDevicePathNodeLength (DevicePath
, NodeLength
);
766 Determines if a device path is single or multi-instance.
768 This function returns TRUE if the device path specified by DevicePath is
770 Otherwise, FALSE is returned.
771 If DevicePath is NULL or invalid, then FALSE is returned.
773 @param DevicePath A pointer to a device path data structure.
775 @retval TRUE DevicePath is multi-instance.
776 @retval FALSE DevicePath is not multi-instance, or DevicePath
782 UefiDevicePathLibIsDevicePathMultiInstance (
783 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
786 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
788 if (DevicePath
== NULL
) {
792 if (!IsDevicePathValid (DevicePath
, 0)) {
797 while (!IsDevicePathEnd (Node
)) {
798 if (IsDevicePathEndInstance (Node
)) {
802 Node
= NextDevicePathNode (Node
);
810 Retrieves the device path protocol from a handle.
812 This function returns the device path protocol from the handle specified by Handle.
813 If Handle is NULL or Handle does not contain a device path protocol, then NULL
816 @param Handle The handle from which to retrieve the device
819 @return The device path protocol from the handle specified by Handle.
822 EFI_DEVICE_PATH_PROTOCOL
*
824 DevicePathFromHandle (
828 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
831 Status
= gBS
->HandleProtocol (
833 &gEfiDevicePathProtocolGuid
,
836 if (EFI_ERROR (Status
)) {
843 Allocates a device path for a file and appends it to an existing device path.
845 If Device is a valid device handle that contains a device path protocol, then a device path for
846 the file specified by FileName is allocated and appended to the device path associated with the
847 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
848 that does not support the device path protocol, then a device path containing a single device
849 path node for the file specified by FileName is allocated and returned.
850 The memory for the new device path is allocated from EFI boot services memory. It is the responsibility
851 of the caller to free the memory allocated.
853 If FileName is NULL, then ASSERT().
854 If FileName is not aligned on a 16-bit boundary, then ASSERT().
856 @param Device A pointer to a device handle. This parameter
857 is optional and may be NULL.
858 @param FileName A pointer to a Null-terminated Unicode string.
860 @return The allocated device path.
863 EFI_DEVICE_PATH_PROTOCOL
*
866 IN EFI_HANDLE Device
, OPTIONAL
867 IN CONST CHAR16
*FileName
871 FILEPATH_DEVICE_PATH
*FilePath
;
872 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
873 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
877 Size
= StrSize (FileName
);
878 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ END_DEVICE_PATH_LENGTH
);
879 if (FileDevicePath
!= NULL
) {
880 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
881 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
882 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
883 CopyMem (&FilePath
->PathName
, FileName
, Size
);
884 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
885 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
887 if (Device
!= NULL
) {
888 DevicePath
= DevicePathFromHandle (Device
);
891 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
892 FreePool (FileDevicePath
);