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
)) {
100 // FilePath must be a NULL-terminated string.
102 if (DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
&&
103 DevicePathSubType (DevicePath
) == MEDIA_FILEPATH_DP
&&
104 *(CHAR16
*)((UINT8
*)DevicePath
+ NodeLength
- 2) != 0) {
110 // Only return TRUE when the End Device Path node is valid.
112 return (BOOLEAN
) (DevicePathNodeLength (DevicePath
) == END_DEVICE_PATH_LENGTH
);
117 Returns the Type field of a device path node.
119 Returns the Type field of the device path node specified by Node.
121 If Node is NULL, then ASSERT().
123 @param Node A pointer to a device path node data structure.
125 @return The Type field of the device path node specified by Node.
134 ASSERT (Node
!= NULL
);
135 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Type
;
139 Returns the SubType field of a device path node.
141 Returns the SubType field of the device path node specified by Node.
143 If Node is NULL, then ASSERT().
145 @param Node A pointer to a device path node data structure.
147 @return The SubType field of the device path node specified by Node.
156 ASSERT (Node
!= NULL
);
157 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->SubType
;
161 Returns the 16-bit Length field of a device path node.
163 Returns the 16-bit Length field of the device path node specified by Node.
164 Node is not required to be aligned on a 16-bit boundary, so it is recommended
165 that a function such as ReadUnaligned16() be used to extract the contents of
168 If Node is NULL, then ASSERT().
170 @param Node A pointer to a device path node data structure.
172 @return The 16-bit Length field of the device path node specified by Node.
177 DevicePathNodeLength (
181 ASSERT (Node
!= NULL
);
182 return ReadUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0]);
186 Returns a pointer to the next node in a device path.
188 Returns a pointer to the device path node that follows the device path node
191 If Node is NULL, then ASSERT().
193 @param Node A pointer to a device path node data structure.
195 @return a pointer to the device path node that follows the device path node
199 EFI_DEVICE_PATH_PROTOCOL
*
205 ASSERT (Node
!= NULL
);
206 return (EFI_DEVICE_PATH_PROTOCOL
*)((UINT8
*)(Node
) + DevicePathNodeLength(Node
));
210 Determines if a device path node is an end node of a device path.
211 This includes nodes that are the end of a device path instance and nodes that
212 are the end of an entire device path.
214 Determines if the device path node specified by Node is an end node of a device path.
215 This includes nodes that are the end of a device path instance and nodes that are the
216 end of an entire device path. If Node represents an end node of a device path,
217 then TRUE is returned. Otherwise, FALSE is returned.
219 If Node is NULL, then ASSERT().
221 @param Node A pointer to a device path node data structure.
223 @retval TRUE The device path node specified by Node is an end node of a
225 @retval FALSE The device path node specified by Node is not an end node of
231 IsDevicePathEndType (
235 ASSERT (Node
!= NULL
);
236 return (BOOLEAN
) (DevicePathType (Node
) == END_DEVICE_PATH_TYPE
);
240 Determines if a device path node is an end node of an entire device path.
242 Determines if a device path node specified by Node is an end node of an entire
243 device path. If Node represents the end of an entire device path, then TRUE is
244 returned. Otherwise, FALSE is returned.
246 If Node is NULL, then ASSERT().
248 @param Node A pointer to a device path node data structure.
250 @retval TRUE The device path node specified by Node is the end of an entire
252 @retval FALSE The device path node specified by Node is not the end of an
262 ASSERT (Node
!= NULL
);
263 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
);
267 Determines if a device path node is an end node of a device path instance.
269 Determines if a device path node specified by Node is an end node of a device
270 path instance. If Node represents the end of a device path instance, then TRUE
271 is returned. Otherwise, FALSE is returned.
273 If Node is NULL, then ASSERT().
275 @param Node A pointer to a device path node data structure.
277 @retval TRUE The device path node specified by Node is the end of a device
279 @retval FALSE The device path node specified by Node is not the end of a
280 device path instance.
285 IsDevicePathEndInstance (
289 ASSERT (Node
!= NULL
);
290 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_INSTANCE_DEVICE_PATH_SUBTYPE
);
294 Sets the length, in bytes, of a device path node.
296 Sets the length of the device path node specified by Node to the value specified
297 by NodeLength. NodeLength is returned. Node is not required to be aligned on
298 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()
299 be used to set the contents of the Length field.
301 If Node is NULL, then ASSERT().
302 If NodeLength >= SIZE_64KB, then ASSERT().
303 If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().
305 @param Node A pointer to a device path node data structure.
306 @param Length The length, in bytes, of the device path node.
313 SetDevicePathNodeLength (
318 ASSERT (Node
!= NULL
);
319 ASSERT ((Length
>= sizeof (EFI_DEVICE_PATH_PROTOCOL
)) && (Length
< SIZE_64KB
));
320 return WriteUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0], (UINT16
)(Length
));
324 Fills in all the fields of a device path node that is the end of an entire device path.
326 Fills in all the fields of a device path node specified by Node so Node represents
327 the end of an entire device path. The Type field of Node is set to
328 END_DEVICE_PATH_TYPE, the SubType field of Node is set to
329 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to
330 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary,
331 so it is recommended that a function such as WriteUnaligned16() be used to set
332 the contents of the Length field.
334 If Node is NULL, then ASSERT().
336 @param Node A pointer to a device path node data structure.
341 SetDevicePathEndNode (
345 ASSERT (Node
!= NULL
);
346 CopyMem (Node
, &mUefiDevicePathLibEndDevicePath
, sizeof (mUefiDevicePathLibEndDevicePath
));
350 Returns the size of a device path in bytes.
352 This function returns the size, in bytes, of the device path data structure
353 specified by DevicePath including the end of device path node.
354 If DevicePath is NULL or invalid, then 0 is returned.
356 @param DevicePath A pointer to a device path data structure.
358 @retval 0 If DevicePath is NULL or invalid.
359 @retval Others The size of a device path in bytes.
364 UefiDevicePathLibGetDevicePathSize (
365 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
368 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
370 if (DevicePath
== NULL
) {
374 if (!IsDevicePathValid (DevicePath
, 0)) {
379 // Search for the end of the device path structure
382 while (!IsDevicePathEnd (DevicePath
)) {
383 DevicePath
= NextDevicePathNode (DevicePath
);
387 // Compute the size and add back in the size of the end device path structure
389 return ((UINTN
) DevicePath
- (UINTN
) Start
) + DevicePathNodeLength (DevicePath
);
393 Creates a new copy of an existing device path.
395 This function allocates space for a new copy of the device path specified by DevicePath.
396 If DevicePath is NULL, then NULL is returned. If the memory is successfully
397 allocated, then the contents of DevicePath are copied to the newly allocated
398 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.
399 The memory for the new device path is allocated from EFI boot services memory.
400 It is the responsibility of the caller to free the memory allocated.
402 @param DevicePath A pointer to a device path data structure.
404 @retval NULL DevicePath is NULL or invalid.
405 @retval Others A pointer to the duplicated device path.
408 EFI_DEVICE_PATH_PROTOCOL
*
410 UefiDevicePathLibDuplicateDevicePath (
411 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
419 Size
= GetDevicePathSize (DevicePath
);
425 // Allocate space for duplicate device path
428 return AllocateCopyPool (Size
, DevicePath
);
432 Creates a new device path by appending a second device path to a first device path.
434 This function creates a new device path by appending a copy of SecondDevicePath
435 to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path
436 device node from SecondDevicePath is retained. The newly created device path is
437 returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
438 SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,
439 and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
440 SecondDevicePath are NULL, then a copy of an end-of-device-path is 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.
444 It is the responsibility of the caller to free the memory allocated.
446 @param FirstDevicePath A pointer to a device path data structure.
447 @param SecondDevicePath A pointer to a device path data structure.
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 SecondDevicePath are NULL.
455 EFI_DEVICE_PATH_PROTOCOL
*
457 UefiDevicePathLibAppendDevicePath (
458 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
459 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
465 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
466 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
469 // If there's only 1 path, just duplicate it.
471 if (FirstDevicePath
== NULL
) {
472 return DuplicateDevicePath ((SecondDevicePath
!= NULL
) ? SecondDevicePath
: &mUefiDevicePathLibEndDevicePath
);
475 if (SecondDevicePath
== NULL
) {
476 return DuplicateDevicePath (FirstDevicePath
);
479 if (!IsDevicePathValid (FirstDevicePath
, 0) || !IsDevicePathValid (SecondDevicePath
, 0)) {
484 // Allocate space for the combined device path. It only has one end node of
485 // length EFI_DEVICE_PATH_PROTOCOL.
487 Size1
= GetDevicePathSize (FirstDevicePath
);
488 Size2
= GetDevicePathSize (SecondDevicePath
);
489 Size
= Size1
+ Size2
- END_DEVICE_PATH_LENGTH
;
491 NewDevicePath
= AllocatePool (Size
);
493 if (NewDevicePath
!= NULL
) {
494 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
496 // Over write FirstDevicePath EndNode and do the copy
498 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
499 (Size1
- END_DEVICE_PATH_LENGTH
));
500 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
503 return NewDevicePath
;
507 Creates a new path by appending the device node to the device path.
509 This function creates a new device path by appending a copy of the device node
510 specified by DevicePathNode to a copy of the device path specified by DevicePath
511 in an allocated buffer. The end-of-device-path device node is moved after the
512 end of the appended device node.
513 If DevicePathNode is NULL then a copy of DevicePath is returned.
514 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
515 path device node is returned.
516 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
517 device node is returned.
518 If there is not enough memory to allocate space for the new device path, then
520 The memory is allocated from EFI boot services memory. It is the responsibility
521 of the caller to free the memory allocated.
523 @param DevicePath A pointer to a device path data structure.
524 @param DevicePathNode A pointer to a single device path node.
526 @retval NULL If there is not enough memory for the new device path.
527 @retval Others A pointer to the new device path if success.
528 A copy of DevicePathNode followed by an end-of-device-path node
529 if both FirstDevicePath and SecondDevicePath are NULL.
530 A copy of an end-of-device-path node if both FirstDevicePath
531 and SecondDevicePath are NULL.
534 EFI_DEVICE_PATH_PROTOCOL
*
536 UefiDevicePathLibAppendDevicePathNode (
537 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
538 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
541 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
542 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
543 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
546 if (DevicePathNode
== NULL
) {
547 return DuplicateDevicePath ((DevicePath
!= NULL
) ? DevicePath
: &mUefiDevicePathLibEndDevicePath
);
550 // Build a Node that has a terminator on it
552 NodeLength
= DevicePathNodeLength (DevicePathNode
);
554 TempDevicePath
= AllocatePool (NodeLength
+ END_DEVICE_PATH_LENGTH
);
555 if (TempDevicePath
== NULL
) {
558 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
560 // Add and end device path node to convert Node to device path
562 NextNode
= NextDevicePathNode (TempDevicePath
);
563 SetDevicePathEndNode (NextNode
);
565 // Append device paths
567 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
569 FreePool (TempDevicePath
);
571 return NewDevicePath
;
575 Creates a new device path by appending the specified device path instance to the specified device
578 This function creates a new device path by appending a copy of the device path
579 instance specified by DevicePathInstance to a copy of the device path specified
580 by DevicePath in a allocated buffer.
581 The end-of-device-path device node is moved after the end of the appended device
582 path instance and a new end-of-device-path-instance node is inserted between.
583 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
584 If DevicePathInstance is NULL, then NULL is returned.
585 If DevicePath or DevicePathInstance is invalid, then NULL is returned.
586 If there is not enough memory to allocate space for the new device path, then
588 The memory is allocated from EFI boot services memory. It is the responsibility
589 of the caller to free the memory allocated.
591 @param DevicePath A pointer to a device path data structure.
592 @param DevicePathInstance A pointer to a device path instance.
594 @return A pointer to the new device path.
597 EFI_DEVICE_PATH_PROTOCOL
*
599 UefiDevicePathLibAppendDevicePathInstance (
600 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
601 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
604 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
605 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
609 if (DevicePath
== NULL
) {
610 return DuplicateDevicePath (DevicePathInstance
);
613 if (DevicePathInstance
== NULL
) {
617 if (!IsDevicePathValid (DevicePath
, 0) || !IsDevicePathValid (DevicePathInstance
, 0)) {
621 SrcSize
= GetDevicePathSize (DevicePath
);
622 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
624 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
625 if (NewDevicePath
!= NULL
) {
627 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
629 while (!IsDevicePathEnd (TempDevicePath
)) {
630 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
633 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
634 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
635 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
638 return NewDevicePath
;
642 Creates a copy of the current device path instance and returns a pointer to the next device path
645 This function creates a copy of the current device path instance. It also updates
646 DevicePath to point to the next device path instance in the device path (or NULL
647 if no more) and updates Size to hold the size of the device path instance copy.
648 If DevicePath is NULL, then NULL is returned.
649 If DevicePath points to a invalid device path, then NULL is returned.
650 If there is not enough memory to allocate space for the new device path, then
652 The memory is allocated from EFI boot services memory. It is the responsibility
653 of the caller to free the memory allocated.
654 If Size is NULL, then ASSERT().
656 @param DevicePath On input, this holds the pointer to the current
657 device path instance. On output, this holds
658 the pointer to the next device path instance
659 or NULL if there are no more device path
660 instances in the device path pointer to a
661 device path data structure.
662 @param Size On output, this holds the size of the device
663 path instance, in bytes or zero, if DevicePath
666 @return A pointer to the current device path instance.
669 EFI_DEVICE_PATH_PROTOCOL
*
671 UefiDevicePathLibGetNextDevicePathInstance (
672 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
676 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
677 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
680 ASSERT (Size
!= NULL
);
682 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
687 if (!IsDevicePathValid (*DevicePath
, 0)) {
692 // Find the end of the device path instance
694 DevPath
= *DevicePath
;
695 while (!IsDevicePathEndType (DevPath
)) {
696 DevPath
= NextDevicePathNode (DevPath
);
700 // Compute the size of the device path instance
702 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
705 // Make a copy and return the device path instance
707 Temp
= DevPath
->SubType
;
708 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
709 ReturnValue
= DuplicateDevicePath (*DevicePath
);
710 DevPath
->SubType
= Temp
;
713 // If DevPath is the end of an entire device path, then another instance
714 // does not follow, so *DevicePath is set to NULL.
716 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
719 *DevicePath
= NextDevicePathNode (DevPath
);
726 Creates a device node.
728 This function creates a new device node in a newly allocated buffer of size
729 NodeLength and initializes the device path node header with NodeType and NodeSubType.
730 The new device path node is returned.
731 If NodeLength is smaller than a device path header, then NULL is returned.
732 If there is not enough memory to allocate space for the new device path, then
734 The memory is allocated from EFI boot services memory. It is the responsibility
735 of the caller to free the memory allocated.
737 @param NodeType The device node type for the new device node.
738 @param NodeSubType The device node sub-type for the new device node.
739 @param NodeLength The length of the new device node.
741 @return The new device path.
744 EFI_DEVICE_PATH_PROTOCOL
*
746 UefiDevicePathLibCreateDeviceNode (
748 IN UINT8 NodeSubType
,
752 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
754 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
756 // NodeLength is less than the size of the header.
761 DevicePath
= AllocateZeroPool (NodeLength
);
762 if (DevicePath
!= NULL
) {
763 DevicePath
->Type
= NodeType
;
764 DevicePath
->SubType
= NodeSubType
;
765 SetDevicePathNodeLength (DevicePath
, NodeLength
);
772 Determines if a device path is single or multi-instance.
774 This function returns TRUE if the device path specified by DevicePath is
776 Otherwise, FALSE is returned.
777 If DevicePath is NULL or invalid, then FALSE is returned.
779 @param DevicePath A pointer to a device path data structure.
781 @retval TRUE DevicePath is multi-instance.
782 @retval FALSE DevicePath is not multi-instance, or DevicePath
788 UefiDevicePathLibIsDevicePathMultiInstance (
789 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
792 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
794 if (DevicePath
== NULL
) {
798 if (!IsDevicePathValid (DevicePath
, 0)) {
803 while (!IsDevicePathEnd (Node
)) {
804 if (IsDevicePathEndInstance (Node
)) {
808 Node
= NextDevicePathNode (Node
);
816 Retrieves the device path protocol from a handle.
818 This function returns the device path protocol from the handle specified by Handle.
819 If Handle is NULL or Handle does not contain a device path protocol, then NULL
822 @param Handle The handle from which to retrieve the device
825 @return The device path protocol from the handle specified by Handle.
828 EFI_DEVICE_PATH_PROTOCOL
*
830 DevicePathFromHandle (
834 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
837 Status
= gBS
->HandleProtocol (
839 &gEfiDevicePathProtocolGuid
,
842 if (EFI_ERROR (Status
)) {
849 Allocates a device path for a file and appends it to an existing device path.
851 If Device is a valid device handle that contains a device path protocol, then a device path for
852 the file specified by FileName is allocated and appended to the device path associated with the
853 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
854 that does not support the device path protocol, then a device path containing a single device
855 path node for the file specified by FileName is allocated and returned.
856 The memory for the new device path is allocated from EFI boot services memory. It is the responsibility
857 of the caller to free the memory allocated.
859 If FileName is NULL, then ASSERT().
860 If FileName is not aligned on a 16-bit boundary, then ASSERT().
862 @param Device A pointer to a device handle. This parameter
863 is optional and may be NULL.
864 @param FileName A pointer to a Null-terminated Unicode string.
866 @return The allocated device path.
869 EFI_DEVICE_PATH_PROTOCOL
*
872 IN EFI_HANDLE Device
, OPTIONAL
873 IN CONST CHAR16
*FileName
877 FILEPATH_DEVICE_PATH
*FilePath
;
878 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
879 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
883 Size
= StrSize (FileName
);
884 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ END_DEVICE_PATH_LENGTH
);
885 if (FileDevicePath
!= NULL
) {
886 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
887 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
888 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
889 CopyMem (&FilePath
->PathName
, FileName
, Size
);
890 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
891 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
893 if (Device
!= NULL
) {
894 DevicePath
= DevicePathFromHandle (Device
);
897 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
898 FreePool (FileDevicePath
);