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 This program and the accompanying materials
13 are licensed and made available under the terms and conditions of the BSD License
14 which accompanies this distribution. The full text of the license may be found at
15 http://opensource.org/licenses/bsd-license.php.
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include "UefiDevicePathLib.h"
25 // Template for an end-of-device path node.
27 GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath
= {
29 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
31 END_DEVICE_PATH_LENGTH
,
37 Determine whether a given device path is valid.
39 @param DevicePath A pointer to a device path data structure.
40 @param MaxSize The maximum size of the device path data structure.
42 @retval TRUE DevicePath is valid.
43 @retval FALSE DevicePath is NULL.
44 @retval FALSE Maxsize is less than sizeof(EFI_DEVICE_PATH_PROTOCOL).
45 @retval FALSE The length of any node node in the DevicePath is less
46 than sizeof (EFI_DEVICE_PATH_PROTOCOL).
47 @retval FALSE If MaxSize is not zero, the size of the DevicePath
49 @retval FALSE If PcdMaximumDevicePathNodeCount is not zero, the node
50 count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.
55 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
64 //Validate the input whether exists and its size big enough to touch the first node
66 if (DevicePath
== NULL
|| (MaxSize
> 0 && MaxSize
< END_DEVICE_PATH_LENGTH
)) {
74 for (Count
= 0, Size
= 0; !IsDevicePathEnd (DevicePath
); DevicePath
= NextDevicePathNode (DevicePath
)) {
75 NodeLength
= DevicePathNodeLength (DevicePath
);
76 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
80 if (NodeLength
> MAX_UINTN
- Size
) {
86 // Validate next node before touch it.
88 if (Size
> MaxSize
- END_DEVICE_PATH_LENGTH
) {
92 if (PcdGet32 (PcdMaximumDevicePathNodeCount
) > 0) {
94 if (Count
>= PcdGet32 (PcdMaximumDevicePathNodeCount
)) {
101 // Only return TRUE when the End Device Path node is valid.
103 return (BOOLEAN
) (DevicePathNodeLength (DevicePath
) == END_DEVICE_PATH_LENGTH
);
108 Returns the Type field of a device path node.
110 Returns the Type field of the device path node specified by Node.
112 If Node is NULL, then ASSERT().
114 @param Node A pointer to a device path node data structure.
116 @return The Type field of the device path node specified by Node.
125 ASSERT (Node
!= NULL
);
126 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Type
;
130 Returns the SubType field of a device path node.
132 Returns the SubType field of the device path node specified by Node.
134 If Node is NULL, then ASSERT().
136 @param Node A pointer to a device path node data structure.
138 @return The SubType field of the device path node specified by Node.
147 ASSERT (Node
!= NULL
);
148 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->SubType
;
152 Returns the 16-bit Length field of a device path node.
154 Returns the 16-bit Length field of the device path node specified by Node.
155 Node is not required to be aligned on a 16-bit boundary, so it is recommended
156 that a function such as ReadUnaligned16() be used to extract the contents of
159 If Node is NULL, then ASSERT().
161 @param Node A pointer to a device path node data structure.
163 @return The 16-bit Length field of the device path node specified by Node.
168 DevicePathNodeLength (
172 ASSERT (Node
!= NULL
);
173 return ReadUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0]);
177 Returns a pointer to the next node in a device path.
179 Returns a pointer to the device path node that follows the device path node
182 If Node is NULL, then ASSERT().
184 @param Node A pointer to a device path node data structure.
186 @return a pointer to the device path node that follows the device path node
190 EFI_DEVICE_PATH_PROTOCOL
*
196 ASSERT (Node
!= NULL
);
197 return (EFI_DEVICE_PATH_PROTOCOL
*)((UINT8
*)(Node
) + DevicePathNodeLength(Node
));
201 Determines if a device path node is an end node of a device path.
202 This includes nodes that are the end of a device path instance and nodes that
203 are the end of an entire device path.
205 Determines if the device path node specified by Node is an end node of a device path.
206 This includes nodes that are the end of a device path instance and nodes that are the
207 end of an entire device path. If Node represents an end node of a device path,
208 then TRUE is returned. Otherwise, FALSE is returned.
210 If Node is NULL, then ASSERT().
212 @param Node A pointer to a device path node data structure.
214 @retval TRUE The device path node specified by Node is an end node of a
216 @retval FALSE The device path node specified by Node is not an end node of
222 IsDevicePathEndType (
226 ASSERT (Node
!= NULL
);
227 return (BOOLEAN
) (DevicePathType (Node
) == END_DEVICE_PATH_TYPE
);
231 Determines if a device path node is an end node of an entire device path.
233 Determines if a device path node specified by Node is an end node of an entire
234 device path. If Node represents the end of an entire device path, then TRUE is
235 returned. Otherwise, FALSE is returned.
237 If Node is NULL, then ASSERT().
239 @param Node A pointer to a device path node data structure.
241 @retval TRUE The device path node specified by Node is the end of an entire
243 @retval FALSE The device path node specified by Node is not the end of an
253 ASSERT (Node
!= NULL
);
254 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
);
258 Determines if a device path node is an end node of a device path instance.
260 Determines if a device path node specified by Node is an end node of a device
261 path instance. If Node represents the end of a device path instance, then TRUE
262 is returned. Otherwise, FALSE is returned.
264 If Node is NULL, then ASSERT().
266 @param Node A pointer to a device path node data structure.
268 @retval TRUE The device path node specified by Node is the end of a device
270 @retval FALSE The device path node specified by Node is not the end of a
271 device path instance.
276 IsDevicePathEndInstance (
280 ASSERT (Node
!= NULL
);
281 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_INSTANCE_DEVICE_PATH_SUBTYPE
);
285 Sets the length, in bytes, of a device path node.
287 Sets the length of the device path node specified by Node to the value specified
288 by NodeLength. NodeLength is returned. Node is not required to be aligned on
289 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()
290 be used to set the contents of the Length field.
292 If Node is NULL, then ASSERT().
293 If NodeLength >= SIZE_64KB, then ASSERT().
294 If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().
296 @param Node A pointer to a device path node data structure.
297 @param Length The length, in bytes, of the device path node.
304 SetDevicePathNodeLength (
309 ASSERT (Node
!= NULL
);
310 ASSERT ((Length
>= sizeof (EFI_DEVICE_PATH_PROTOCOL
)) && (Length
< SIZE_64KB
));
311 return WriteUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0], (UINT16
)(Length
));
315 Fills in all the fields of a device path node that is the end of an entire device path.
317 Fills in all the fields of a device path node specified by Node so Node represents
318 the end of an entire device path. The Type field of Node is set to
319 END_DEVICE_PATH_TYPE, the SubType field of Node is set to
320 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to
321 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary,
322 so it is recommended that a function such as WriteUnaligned16() be used to set
323 the contents of the Length field.
325 If Node is NULL, then ASSERT().
327 @param Node A pointer to a device path node data structure.
332 SetDevicePathEndNode (
336 ASSERT (Node
!= NULL
);
337 CopyMem (Node
, &mUefiDevicePathLibEndDevicePath
, sizeof (mUefiDevicePathLibEndDevicePath
));
341 Returns the size of a device path in bytes.
343 This function returns the size, in bytes, of the device path data structure
344 specified by DevicePath including the end of device path node.
345 If DevicePath is NULL or invalid, then 0 is returned.
347 @param DevicePath A pointer to a device path data structure.
349 @retval 0 If DevicePath is NULL or invalid.
350 @retval Others The size of a device path in bytes.
355 UefiDevicePathLibGetDevicePathSize (
356 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
359 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
361 if (DevicePath
== NULL
) {
365 if (!IsDevicePathValid (DevicePath
, 0)) {
370 // Search for the end of the device path structure
373 while (!IsDevicePathEnd (DevicePath
)) {
374 DevicePath
= NextDevicePathNode (DevicePath
);
378 // Compute the size and add back in the size of the end device path structure
380 return ((UINTN
) DevicePath
- (UINTN
) Start
) + DevicePathNodeLength (DevicePath
);
384 Creates a new copy of an existing device path.
386 This function allocates space for a new copy of the device path specified by DevicePath.
387 If DevicePath is NULL, then NULL is returned. If the memory is successfully
388 allocated, then the contents of DevicePath are copied to the newly allocated
389 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.
390 The memory for the new device path is allocated from EFI boot services memory.
391 It is the responsibility of the caller to free the memory allocated.
393 @param DevicePath A pointer to a device path data structure.
395 @retval NULL DevicePath is NULL or invalid.
396 @retval Others A pointer to the duplicated device path.
399 EFI_DEVICE_PATH_PROTOCOL
*
401 UefiDevicePathLibDuplicateDevicePath (
402 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
410 Size
= GetDevicePathSize (DevicePath
);
416 // Allocate space for duplicate device path
419 return AllocateCopyPool (Size
, DevicePath
);
423 Creates a new device path by appending a second device path to a first device path.
425 This function creates a new device path by appending a copy of SecondDevicePath
426 to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path
427 device node from SecondDevicePath is retained. The newly created device path is
428 returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
429 SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,
430 and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
431 SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.
433 If there is not enough memory for the newly allocated buffer, then NULL is returned.
434 The memory for the new device path is allocated from EFI boot services memory.
435 It is the responsibility of the caller to free the memory allocated.
437 @param FirstDevicePath A pointer to a device path data structure.
438 @param SecondDevicePath A pointer to a device path data structure.
440 @retval NULL If there is not enough memory for the newly allocated buffer.
441 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.
442 @retval Others A pointer to the new device path if success.
443 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
446 EFI_DEVICE_PATH_PROTOCOL
*
448 UefiDevicePathLibAppendDevicePath (
449 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
450 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
456 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
457 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
460 // If there's only 1 path, just duplicate it.
462 if (FirstDevicePath
== NULL
) {
463 return DuplicateDevicePath ((SecondDevicePath
!= NULL
) ? SecondDevicePath
: &mUefiDevicePathLibEndDevicePath
);
466 if (SecondDevicePath
== NULL
) {
467 return DuplicateDevicePath (FirstDevicePath
);
470 if (!IsDevicePathValid (FirstDevicePath
, 0) || !IsDevicePathValid (SecondDevicePath
, 0)) {
475 // Allocate space for the combined device path. It only has one end node of
476 // length EFI_DEVICE_PATH_PROTOCOL.
478 Size1
= GetDevicePathSize (FirstDevicePath
);
479 Size2
= GetDevicePathSize (SecondDevicePath
);
480 Size
= Size1
+ Size2
- END_DEVICE_PATH_LENGTH
;
482 NewDevicePath
= AllocatePool (Size
);
484 if (NewDevicePath
!= NULL
) {
485 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
487 // Over write FirstDevicePath EndNode and do the copy
489 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
490 (Size1
- END_DEVICE_PATH_LENGTH
));
491 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
494 return NewDevicePath
;
498 Creates a new path by appending the device node to the device path.
500 This function creates a new device path by appending a copy of the device node
501 specified by DevicePathNode to a copy of the device path specified by DevicePath
502 in an allocated buffer. The end-of-device-path device node is moved after the
503 end of the appended device node.
504 If DevicePathNode is NULL then a copy of DevicePath is returned.
505 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
506 path device node is returned.
507 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
508 device node is returned.
509 If there is not enough memory to allocate space for the new device path, then
511 The memory is allocated from EFI boot services memory. It is the responsibility
512 of the caller to free the memory allocated.
514 @param DevicePath A pointer to a device path data structure.
515 @param DevicePathNode A pointer to a single device path node.
517 @retval NULL If there is not enough memory for the new device path.
518 @retval Others A pointer to the new device path if success.
519 A copy of DevicePathNode followed by an end-of-device-path node
520 if both FirstDevicePath and SecondDevicePath are NULL.
521 A copy of an end-of-device-path node if both FirstDevicePath
522 and SecondDevicePath are NULL.
525 EFI_DEVICE_PATH_PROTOCOL
*
527 UefiDevicePathLibAppendDevicePathNode (
528 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
529 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
532 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
533 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
534 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
537 if (DevicePathNode
== NULL
) {
538 return DuplicateDevicePath ((DevicePath
!= NULL
) ? DevicePath
: &mUefiDevicePathLibEndDevicePath
);
541 // Build a Node that has a terminator on it
543 NodeLength
= DevicePathNodeLength (DevicePathNode
);
545 TempDevicePath
= AllocatePool (NodeLength
+ END_DEVICE_PATH_LENGTH
);
546 if (TempDevicePath
== NULL
) {
549 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
551 // Add and end device path node to convert Node to device path
553 NextNode
= NextDevicePathNode (TempDevicePath
);
554 SetDevicePathEndNode (NextNode
);
556 // Append device paths
558 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
560 FreePool (TempDevicePath
);
562 return NewDevicePath
;
566 Creates a new device path by appending the specified device path instance to the specified device
569 This function creates a new device path by appending a copy of the device path
570 instance specified by DevicePathInstance to a copy of the device path specified
571 by DevicePath in a allocated buffer.
572 The end-of-device-path device node is moved after the end of the appended device
573 path instance and a new end-of-device-path-instance node is inserted between.
574 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
575 If DevicePathInstance is NULL, then NULL is returned.
576 If DevicePath or DevicePathInstance is invalid, then NULL is returned.
577 If there is not enough memory to allocate space for the new device path, then
579 The memory is allocated from EFI boot services memory. It is the responsibility
580 of the caller to free the memory allocated.
582 @param DevicePath A pointer to a device path data structure.
583 @param DevicePathInstance A pointer to a device path instance.
585 @return A pointer to the new device path.
588 EFI_DEVICE_PATH_PROTOCOL
*
590 UefiDevicePathLibAppendDevicePathInstance (
591 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
592 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
595 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
596 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
600 if (DevicePath
== NULL
) {
601 return DuplicateDevicePath (DevicePathInstance
);
604 if (DevicePathInstance
== NULL
) {
608 if (!IsDevicePathValid (DevicePath
, 0) || !IsDevicePathValid (DevicePathInstance
, 0)) {
612 SrcSize
= GetDevicePathSize (DevicePath
);
613 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
615 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
616 if (NewDevicePath
!= NULL
) {
618 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
620 while (!IsDevicePathEnd (TempDevicePath
)) {
621 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
624 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
625 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
626 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
629 return NewDevicePath
;
633 Creates a copy of the current device path instance and returns a pointer to the next device path
636 This function creates a copy of the current device path instance. It also updates
637 DevicePath to point to the next device path instance in the device path (or NULL
638 if no more) and updates Size to hold the size of the device path instance copy.
639 If DevicePath is NULL, then NULL is returned.
640 If DevicePath points to a invalid device path, then NULL is returned.
641 If there is not enough memory to allocate space for the new device path, then
643 The memory is allocated from EFI boot services memory. It is the responsibility
644 of the caller to free the memory allocated.
645 If Size is NULL, then ASSERT().
647 @param DevicePath On input, this holds the pointer to the current
648 device path instance. On output, this holds
649 the pointer to the next device path instance
650 or NULL if there are no more device path
651 instances in the device path pointer to a
652 device path data structure.
653 @param Size On output, this holds the size of the device
654 path instance, in bytes or zero, if DevicePath
657 @return A pointer to the current device path instance.
660 EFI_DEVICE_PATH_PROTOCOL
*
662 UefiDevicePathLibGetNextDevicePathInstance (
663 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
667 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
668 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
671 ASSERT (Size
!= NULL
);
673 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
678 if (!IsDevicePathValid (*DevicePath
, 0)) {
683 // Find the end of the device path instance
685 DevPath
= *DevicePath
;
686 while (!IsDevicePathEndType (DevPath
)) {
687 DevPath
= NextDevicePathNode (DevPath
);
691 // Compute the size of the device path instance
693 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
696 // Make a copy and return the device path instance
698 Temp
= DevPath
->SubType
;
699 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
700 ReturnValue
= DuplicateDevicePath (*DevicePath
);
701 DevPath
->SubType
= Temp
;
704 // If DevPath is the end of an entire device path, then another instance
705 // does not follow, so *DevicePath is set to NULL.
707 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
710 *DevicePath
= NextDevicePathNode (DevPath
);
717 Creates a device node.
719 This function creates a new device node in a newly allocated buffer of size
720 NodeLength and initializes the device path node header with NodeType and NodeSubType.
721 The new device path node is returned.
722 If NodeLength is smaller than a device path header, then NULL is returned.
723 If there is not enough memory to allocate space for the new device path, then
725 The memory is allocated from EFI boot services memory. It is the responsibility
726 of the caller to free the memory allocated.
728 @param NodeType The device node type for the new device node.
729 @param NodeSubType The device node sub-type for the new device node.
730 @param NodeLength The length of the new device node.
732 @return The new device path.
735 EFI_DEVICE_PATH_PROTOCOL
*
737 UefiDevicePathLibCreateDeviceNode (
739 IN UINT8 NodeSubType
,
743 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
745 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
747 // NodeLength is less than the size of the header.
752 DevicePath
= AllocateZeroPool (NodeLength
);
753 if (DevicePath
!= NULL
) {
754 DevicePath
->Type
= NodeType
;
755 DevicePath
->SubType
= NodeSubType
;
756 SetDevicePathNodeLength (DevicePath
, NodeLength
);
763 Determines if a device path is single or multi-instance.
765 This function returns TRUE if the device path specified by DevicePath is
767 Otherwise, FALSE is returned.
768 If DevicePath is NULL or invalid, then FALSE is returned.
770 @param DevicePath A pointer to a device path data structure.
772 @retval TRUE DevicePath is multi-instance.
773 @retval FALSE DevicePath is not multi-instance, or DevicePath
779 UefiDevicePathLibIsDevicePathMultiInstance (
780 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
783 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
785 if (DevicePath
== NULL
) {
789 if (!IsDevicePathValid (DevicePath
, 0)) {
794 while (!IsDevicePathEnd (Node
)) {
795 if (IsDevicePathEndInstance (Node
)) {
799 Node
= NextDevicePathNode (Node
);
807 Retrieves the device path protocol from a handle.
809 This function returns the device path protocol from the handle specified by Handle.
810 If Handle is NULL or Handle does not contain a device path protocol, then NULL
813 @param Handle The handle from which to retrieve the device
816 @return The device path protocol from the handle specified by Handle.
819 EFI_DEVICE_PATH_PROTOCOL
*
821 DevicePathFromHandle (
825 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
828 Status
= gBS
->HandleProtocol (
830 &gEfiDevicePathProtocolGuid
,
833 if (EFI_ERROR (Status
)) {
840 Allocates a device path for a file and appends it to an existing device path.
842 If Device is a valid device handle that contains a device path protocol, then a device path for
843 the file specified by FileName is allocated and appended to the device path associated with the
844 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
845 that does not support the device path protocol, then a device path containing a single device
846 path node for the file specified by FileName is allocated and returned.
847 The memory for the new device path is allocated from EFI boot services memory. It is the responsibility
848 of the caller to free the memory allocated.
850 If FileName is NULL, then ASSERT().
851 If FileName is not aligned on a 16-bit boundary, then ASSERT().
853 @param Device A pointer to a device handle. This parameter
854 is optional and may be NULL.
855 @param FileName A pointer to a Null-terminated Unicode string.
857 @return The allocated device path.
860 EFI_DEVICE_PATH_PROTOCOL
*
863 IN EFI_HANDLE Device
, OPTIONAL
864 IN CONST CHAR16
*FileName
868 FILEPATH_DEVICE_PATH
*FilePath
;
869 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
870 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
874 Size
= StrSize (FileName
);
875 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ END_DEVICE_PATH_LENGTH
);
876 if (FileDevicePath
!= NULL
) {
877 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
878 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
879 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
880 CopyMem (&FilePath
->PathName
, FileName
, Size
);
881 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
882 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
884 if (Device
!= NULL
) {
885 DevicePath
= DevicePathFromHandle (Device
);
888 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
889 FreePool (FileDevicePath
);