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 - 2016, 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.
38 If DevicePath is NULL, then ASSERT().
40 @param DevicePath A pointer to a device path data structure.
41 @param MaxSize The maximum size of the device path data structure.
43 @retval TRUE DevicePath is valid.
44 @retval FALSE The length of any node node in the DevicePath is less
45 than sizeof (EFI_DEVICE_PATH_PROTOCOL).
46 @retval FALSE If MaxSize is not zero, the size of the DevicePath
48 @retval FALSE If PcdMaximumDevicePathNodeCount is not zero, the node
49 count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.
54 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
62 ASSERT (DevicePath
!= NULL
);
71 while (MaxSize
>= sizeof (EFI_DEVICE_PATH_PROTOCOL
) &&
72 (MaxSize
- sizeof (EFI_DEVICE_PATH_PROTOCOL
) >= Size
) &&
73 !IsDevicePathEnd (DevicePath
)) {
74 NodeLength
= DevicePathNodeLength (DevicePath
);
75 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
79 if (NodeLength
> MAX_UINTN
- Size
) {
85 if (PcdGet32 (PcdMaximumDevicePathNodeCount
) > 0) {
87 if (Count
>= PcdGet32 (PcdMaximumDevicePathNodeCount
)) {
92 DevicePath
= NextDevicePathNode (DevicePath
);
96 // Only return TRUE when the End Device Path node is valid.
98 return (BOOLEAN
) (DevicePathNodeLength (DevicePath
) == END_DEVICE_PATH_LENGTH
);
103 Returns the Type field of a device path node.
105 Returns the Type field of the device path node specified by Node.
107 If Node is NULL, then ASSERT().
109 @param Node A pointer to a device path node data structure.
111 @return The Type field of the device path node specified by Node.
120 ASSERT (Node
!= NULL
);
121 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Type
;
125 Returns the SubType field of a device path node.
127 Returns the SubType field of the device path node specified by Node.
129 If Node is NULL, then ASSERT().
131 @param Node A pointer to a device path node data structure.
133 @return The SubType field of the device path node specified by Node.
142 ASSERT (Node
!= NULL
);
143 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->SubType
;
147 Returns the 16-bit Length field of a device path node.
149 Returns the 16-bit Length field of the device path node specified by Node.
150 Node is not required to be aligned on a 16-bit boundary, so it is recommended
151 that a function such as ReadUnaligned16() be used to extract the contents of
154 If Node is NULL, then ASSERT().
156 @param Node A pointer to a device path node data structure.
158 @return The 16-bit Length field of the device path node specified by Node.
163 DevicePathNodeLength (
167 ASSERT (Node
!= NULL
);
168 return ReadUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0]);
172 Returns a pointer to the next node in a device path.
174 Returns a pointer to the device path node that follows the device path node
177 If Node is NULL, then ASSERT().
179 @param Node A pointer to a device path node data structure.
181 @return a pointer to the device path node that follows the device path node
185 EFI_DEVICE_PATH_PROTOCOL
*
191 ASSERT (Node
!= NULL
);
192 return (EFI_DEVICE_PATH_PROTOCOL
*)((UINT8
*)(Node
) + DevicePathNodeLength(Node
));
196 Determines if a device path node is an end node of a device path.
197 This includes nodes that are the end of a device path instance and nodes that
198 are the end of an entire device path.
200 Determines if the device path node specified by Node is an end node of a device path.
201 This includes nodes that are the end of a device path instance and nodes that are the
202 end of an entire device path. If Node represents an end node of a device path,
203 then TRUE is returned. Otherwise, FALSE is returned.
205 If Node is NULL, then ASSERT().
207 @param Node A pointer to a device path node data structure.
209 @retval TRUE The device path node specified by Node is an end node of a
211 @retval FALSE The device path node specified by Node is not an end node of
217 IsDevicePathEndType (
221 ASSERT (Node
!= NULL
);
222 return (BOOLEAN
) (DevicePathType (Node
) == END_DEVICE_PATH_TYPE
);
226 Determines if a device path node is an end node of an entire device path.
228 Determines if a device path node specified by Node is an end node of an entire
229 device path. If Node represents the end of an entire device path, then TRUE is
230 returned. Otherwise, FALSE is returned.
232 If Node is NULL, then ASSERT().
234 @param Node A pointer to a device path node data structure.
236 @retval TRUE The device path node specified by Node is the end of an entire
238 @retval FALSE The device path node specified by Node is not the end of an
248 ASSERT (Node
!= NULL
);
249 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
);
253 Determines if a device path node is an end node of a device path instance.
255 Determines if a device path node specified by Node is an end node of a device
256 path instance. If Node represents the end of a device path instance, then TRUE
257 is returned. Otherwise, FALSE is returned.
259 If Node is NULL, then ASSERT().
261 @param Node A pointer to a device path node data structure.
263 @retval TRUE The device path node specified by Node is the end of a device
265 @retval FALSE The device path node specified by Node is not the end of a
266 device path instance.
271 IsDevicePathEndInstance (
275 ASSERT (Node
!= NULL
);
276 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_INSTANCE_DEVICE_PATH_SUBTYPE
);
280 Sets the length, in bytes, of a device path node.
282 Sets the length of the device path node specified by Node to the value specified
283 by NodeLength. NodeLength is returned. Node is not required to be aligned on
284 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()
285 be used to set the contents of the Length field.
287 If Node is NULL, then ASSERT().
288 If NodeLength >= SIZE_64KB, then ASSERT().
289 If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().
291 @param Node A pointer to a device path node data structure.
292 @param Length The length, in bytes, of the device path node.
299 SetDevicePathNodeLength (
304 ASSERT (Node
!= NULL
);
305 ASSERT ((Length
>= sizeof (EFI_DEVICE_PATH_PROTOCOL
)) && (Length
< SIZE_64KB
));
306 return WriteUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0], (UINT16
)(Length
));
310 Fills in all the fields of a device path node that is the end of an entire device path.
312 Fills in all the fields of a device path node specified by Node so Node represents
313 the end of an entire device path. The Type field of Node is set to
314 END_DEVICE_PATH_TYPE, the SubType field of Node is set to
315 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to
316 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary,
317 so it is recommended that a function such as WriteUnaligned16() be used to set
318 the contents of the Length field.
320 If Node is NULL, then ASSERT().
322 @param Node A pointer to a device path node data structure.
327 SetDevicePathEndNode (
331 ASSERT (Node
!= NULL
);
332 CopyMem (Node
, &mUefiDevicePathLibEndDevicePath
, sizeof (mUefiDevicePathLibEndDevicePath
));
336 Returns the size of a device path in bytes.
338 This function returns the size, in bytes, of the device path data structure
339 specified by DevicePath including the end of device path node.
340 If DevicePath is NULL or invalid, then 0 is returned.
342 @param DevicePath A pointer to a device path data structure.
344 @retval 0 If DevicePath is NULL or invalid.
345 @retval Others The size of a device path in bytes.
350 UefiDevicePathLibGetDevicePathSize (
351 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
354 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
356 if (DevicePath
== NULL
) {
360 if (!IsDevicePathValid (DevicePath
, 0)) {
365 // Search for the end of the device path structure
368 while (!IsDevicePathEnd (DevicePath
)) {
369 DevicePath
= NextDevicePathNode (DevicePath
);
373 // Compute the size and add back in the size of the end device path structure
375 return ((UINTN
) DevicePath
- (UINTN
) Start
) + DevicePathNodeLength (DevicePath
);
379 Creates a new copy of an existing device path.
381 This function allocates space for a new copy of the device path specified by DevicePath.
382 If DevicePath is NULL, then NULL is returned. If the memory is successfully
383 allocated, then the contents of DevicePath are copied to the newly allocated
384 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.
385 The memory for the new device path is allocated from EFI boot services memory.
386 It is the responsibility of the caller to free the memory allocated.
388 @param DevicePath A pointer to a device path data structure.
390 @retval NULL DevicePath is NULL or invalid.
391 @retval Others A pointer to the duplicated device path.
394 EFI_DEVICE_PATH_PROTOCOL
*
396 UefiDevicePathLibDuplicateDevicePath (
397 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
405 Size
= GetDevicePathSize (DevicePath
);
411 // Allocate space for duplicate device path
414 return AllocateCopyPool (Size
, DevicePath
);
418 Creates a new device path by appending a second device path to a first device path.
420 This function creates a new device path by appending a copy of SecondDevicePath
421 to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path
422 device node from SecondDevicePath is retained. The newly created device path is
423 returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
424 SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,
425 and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
426 SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.
428 If there is not enough memory for the newly allocated buffer, then NULL is returned.
429 The memory for the new device path is allocated from EFI boot services memory.
430 It is the responsibility of the caller to free the memory allocated.
432 @param FirstDevicePath A pointer to a device path data structure.
433 @param SecondDevicePath A pointer to a device path data structure.
435 @retval NULL If there is not enough memory for the newly allocated buffer.
436 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.
437 @retval Others A pointer to the new device path if success.
438 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
441 EFI_DEVICE_PATH_PROTOCOL
*
443 UefiDevicePathLibAppendDevicePath (
444 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
445 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
451 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
452 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
455 // If there's only 1 path, just duplicate it.
457 if (FirstDevicePath
== NULL
) {
458 return DuplicateDevicePath ((SecondDevicePath
!= NULL
) ? SecondDevicePath
: &mUefiDevicePathLibEndDevicePath
);
461 if (SecondDevicePath
== NULL
) {
462 return DuplicateDevicePath (FirstDevicePath
);
465 if (!IsDevicePathValid (FirstDevicePath
, 0) || !IsDevicePathValid (SecondDevicePath
, 0)) {
470 // Allocate space for the combined device path. It only has one end node of
471 // length EFI_DEVICE_PATH_PROTOCOL.
473 Size1
= GetDevicePathSize (FirstDevicePath
);
474 Size2
= GetDevicePathSize (SecondDevicePath
);
475 Size
= Size1
+ Size2
- END_DEVICE_PATH_LENGTH
;
477 NewDevicePath
= AllocatePool (Size
);
479 if (NewDevicePath
!= NULL
) {
480 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
482 // Over write FirstDevicePath EndNode and do the copy
484 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
485 (Size1
- END_DEVICE_PATH_LENGTH
));
486 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
489 return NewDevicePath
;
493 Creates a new path by appending the device node to the device path.
495 This function creates a new device path by appending a copy of the device node
496 specified by DevicePathNode to a copy of the device path specified by DevicePath
497 in an allocated buffer. The end-of-device-path device node is moved after the
498 end of the appended device node.
499 If DevicePathNode is NULL then a copy of DevicePath is returned.
500 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
501 path device node is returned.
502 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
503 device node is returned.
504 If there is not enough memory to allocate space for the new device path, then
506 The memory is allocated from EFI boot services memory. It is the responsibility
507 of the caller to free the memory allocated.
509 @param DevicePath A pointer to a device path data structure.
510 @param DevicePathNode A pointer to a single device path node.
512 @retval NULL If there is not enough memory for the new device path.
513 @retval Others A pointer to the new device path if success.
514 A copy of DevicePathNode followed by an end-of-device-path node
515 if both FirstDevicePath and SecondDevicePath are NULL.
516 A copy of an end-of-device-path node if both FirstDevicePath
517 and SecondDevicePath are NULL.
520 EFI_DEVICE_PATH_PROTOCOL
*
522 UefiDevicePathLibAppendDevicePathNode (
523 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
524 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
527 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
528 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
529 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
532 if (DevicePathNode
== NULL
) {
533 return DuplicateDevicePath ((DevicePath
!= NULL
) ? DevicePath
: &mUefiDevicePathLibEndDevicePath
);
536 // Build a Node that has a terminator on it
538 NodeLength
= DevicePathNodeLength (DevicePathNode
);
540 TempDevicePath
= AllocatePool (NodeLength
+ END_DEVICE_PATH_LENGTH
);
541 if (TempDevicePath
== NULL
) {
544 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
546 // Add and end device path node to convert Node to device path
548 NextNode
= NextDevicePathNode (TempDevicePath
);
549 SetDevicePathEndNode (NextNode
);
551 // Append device paths
553 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
555 FreePool (TempDevicePath
);
557 return NewDevicePath
;
561 Creates a new device path by appending the specified device path instance to the specified device
564 This function creates a new device path by appending a copy of the device path
565 instance specified by DevicePathInstance to a copy of the device path specified
566 by DevicePath in a allocated buffer.
567 The end-of-device-path device node is moved after the end of the appended device
568 path instance and a new end-of-device-path-instance node is inserted between.
569 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
570 If DevicePathInstance is NULL, then NULL is returned.
571 If DevicePath or DevicePathInstance is invalid, then NULL is returned.
572 If there is not enough memory to allocate space for the new device path, then
574 The memory is allocated from EFI boot services memory. It is the responsibility
575 of the caller to free the memory allocated.
577 @param DevicePath A pointer to a device path data structure.
578 @param DevicePathInstance A pointer to a device path instance.
580 @return A pointer to the new device path.
583 EFI_DEVICE_PATH_PROTOCOL
*
585 UefiDevicePathLibAppendDevicePathInstance (
586 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
587 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
590 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
591 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
595 if (DevicePath
== NULL
) {
596 return DuplicateDevicePath (DevicePathInstance
);
599 if (DevicePathInstance
== NULL
) {
603 if (!IsDevicePathValid (DevicePath
, 0) || !IsDevicePathValid (DevicePathInstance
, 0)) {
607 SrcSize
= GetDevicePathSize (DevicePath
);
608 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
610 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
611 if (NewDevicePath
!= NULL
) {
613 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
615 while (!IsDevicePathEnd (TempDevicePath
)) {
616 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
619 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
620 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
621 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
624 return NewDevicePath
;
628 Creates a copy of the current device path instance and returns a pointer to the next device path
631 This function creates a copy of the current device path instance. It also updates
632 DevicePath to point to the next device path instance in the device path (or NULL
633 if no more) and updates Size to hold the size of the device path instance copy.
634 If DevicePath is NULL, then NULL is returned.
635 If DevicePath points to a invalid device path, then NULL is returned.
636 If there is not enough memory to allocate space for the new device path, then
638 The memory is allocated from EFI boot services memory. It is the responsibility
639 of the caller to free the memory allocated.
640 If Size is NULL, then ASSERT().
642 @param DevicePath On input, this holds the pointer to the current
643 device path instance. On output, this holds
644 the pointer to the next device path instance
645 or NULL if there are no more device path
646 instances in the device path pointer to a
647 device path data structure.
648 @param Size On output, this holds the size of the device
649 path instance, in bytes or zero, if DevicePath
652 @return A pointer to the current device path instance.
655 EFI_DEVICE_PATH_PROTOCOL
*
657 UefiDevicePathLibGetNextDevicePathInstance (
658 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
662 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
663 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
666 ASSERT (Size
!= NULL
);
668 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
673 if (!IsDevicePathValid (*DevicePath
, 0)) {
678 // Find the end of the device path instance
680 DevPath
= *DevicePath
;
681 while (!IsDevicePathEndType (DevPath
)) {
682 DevPath
= NextDevicePathNode (DevPath
);
686 // Compute the size of the device path instance
688 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
691 // Make a copy and return the device path instance
693 Temp
= DevPath
->SubType
;
694 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
695 ReturnValue
= DuplicateDevicePath (*DevicePath
);
696 DevPath
->SubType
= Temp
;
699 // If DevPath is the end of an entire device path, then another instance
700 // does not follow, so *DevicePath is set to NULL.
702 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
705 *DevicePath
= NextDevicePathNode (DevPath
);
712 Creates a device node.
714 This function creates a new device node in a newly allocated buffer of size
715 NodeLength and initializes the device path node header with NodeType and NodeSubType.
716 The new device path node is returned.
717 If NodeLength is smaller than a device path header, then NULL is returned.
718 If there is not enough memory to allocate space for the new device path, then
720 The memory is allocated from EFI boot services memory. It is the responsibility
721 of the caller to free the memory allocated.
723 @param NodeType The device node type for the new device node.
724 @param NodeSubType The device node sub-type for the new device node.
725 @param NodeLength The length of the new device node.
727 @return The new device path.
730 EFI_DEVICE_PATH_PROTOCOL
*
732 UefiDevicePathLibCreateDeviceNode (
734 IN UINT8 NodeSubType
,
738 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
740 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
742 // NodeLength is less than the size of the header.
747 DevicePath
= AllocateZeroPool (NodeLength
);
748 if (DevicePath
!= NULL
) {
749 DevicePath
->Type
= NodeType
;
750 DevicePath
->SubType
= NodeSubType
;
751 SetDevicePathNodeLength (DevicePath
, NodeLength
);
758 Determines if a device path is single or multi-instance.
760 This function returns TRUE if the device path specified by DevicePath is
762 Otherwise, FALSE is returned.
763 If DevicePath is NULL or invalid, then FALSE is returned.
765 @param DevicePath A pointer to a device path data structure.
767 @retval TRUE DevicePath is multi-instance.
768 @retval FALSE DevicePath is not multi-instance, or DevicePath
774 UefiDevicePathLibIsDevicePathMultiInstance (
775 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
778 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
780 if (DevicePath
== NULL
) {
784 if (!IsDevicePathValid (DevicePath
, 0)) {
789 while (!IsDevicePathEnd (Node
)) {
790 if (IsDevicePathEndInstance (Node
)) {
794 Node
= NextDevicePathNode (Node
);
802 Retrieves the device path protocol from a handle.
804 This function returns the device path protocol from the handle specified by Handle.
805 If Handle is NULL or Handle does not contain a device path protocol, then NULL
808 @param Handle The handle from which to retrieve the device
811 @return The device path protocol from the handle specified by Handle.
814 EFI_DEVICE_PATH_PROTOCOL
*
816 DevicePathFromHandle (
820 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
823 Status
= gBS
->HandleProtocol (
825 &gEfiDevicePathProtocolGuid
,
828 if (EFI_ERROR (Status
)) {
835 Allocates a device path for a file and appends it to an existing device path.
837 If Device is a valid device handle that contains a device path protocol, then a device path for
838 the file specified by FileName is allocated and appended to the device path associated with the
839 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
840 that does not support the device path protocol, then a device path containing a single device
841 path node for the file specified by FileName is allocated and returned.
842 The memory for the new device path is allocated from EFI boot services memory. It is the responsibility
843 of the caller to free the memory allocated.
845 If FileName is NULL, then ASSERT().
846 If FileName is not aligned on a 16-bit boundary, then ASSERT().
848 @param Device A pointer to a device handle. This parameter
849 is optional and may be NULL.
850 @param FileName A pointer to a Null-terminated Unicode string.
852 @return The allocated device path.
855 EFI_DEVICE_PATH_PROTOCOL
*
858 IN EFI_HANDLE Device
, OPTIONAL
859 IN CONST CHAR16
*FileName
863 FILEPATH_DEVICE_PATH
*FilePath
;
864 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
865 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
869 Size
= StrSize (FileName
);
870 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ END_DEVICE_PATH_LENGTH
);
871 if (FileDevicePath
!= NULL
) {
872 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
873 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
874 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
875 CopyMem (&FilePath
->PathName
, FileName
, Size
);
876 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
877 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
879 if (Device
!= NULL
) {
880 DevicePath
= DevicePathFromHandle (Device
);
883 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
884 FreePool (FileDevicePath
);