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 - 2012, 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.
25 #include <Library/DevicePathLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/BaseLib.h>
31 #include <Library/PcdLib.h>
34 // Template for an end-of-device path node.
36 GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath
= {
38 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
40 END_DEVICE_PATH_LENGTH
,
46 Determine whether a given device path is valid.
47 If DevicePath is NULL, then ASSERT().
49 @param DevicePath A pointer to a device path data structure.
50 @param MaxSize The maximum size of the device path data structure.
52 @retval TRUE DevicePath is valid.
53 @retval FALSE The length of any node node in the DevicePath is less
54 than sizeof (EFI_DEVICE_PATH_PROTOCOL).
55 @retval FALSE If MaxSize is not zero, the size of the DevicePath
57 @retval FALSE If PcdMaximumDevicePathNodeCount is not zero, the node
58 count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.
63 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
71 ASSERT (DevicePath
!= NULL
);
73 for (Count
= 0, Size
= 0; !IsDevicePathEnd (DevicePath
); DevicePath
= NextDevicePathNode (DevicePath
)) {
74 NodeLength
= DevicePathNodeLength (DevicePath
);
75 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
81 if (Size
+ END_DEVICE_PATH_LENGTH
> MaxSize
) {
86 if (PcdGet32 (PcdMaximumDevicePathNodeCount
) > 0) {
88 if (Count
>= PcdGet32 (PcdMaximumDevicePathNodeCount
)) {
95 // Only return TRUE when the End Device Path node is valid.
97 return (BOOLEAN
) (DevicePathNodeLength (DevicePath
) == END_DEVICE_PATH_LENGTH
);
101 Returns the Type field of a device path node.
103 Returns the Type field of the device path node specified by Node.
105 If Node is NULL, then ASSERT().
107 @param Node A pointer to a device path node data structure.
109 @return The Type field of the device path node specified by Node.
118 ASSERT (Node
!= NULL
);
119 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Type
;
123 Returns the SubType field of a device path node.
125 Returns the SubType field of the device path node specified by Node.
127 If Node is NULL, then ASSERT().
129 @param Node A pointer to a device path node data structure.
131 @return The SubType field of the device path node specified by Node.
140 ASSERT (Node
!= NULL
);
141 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->SubType
;
145 Returns the 16-bit Length field of a device path node.
147 Returns the 16-bit Length field of the device path node specified by Node.
148 Node is not required to be aligned on a 16-bit boundary, so it is recommended
149 that a function such as ReadUnaligned16() be used to extract the contents of
152 If Node is NULL, then ASSERT().
154 @param Node A pointer to a device path node data structure.
156 @return The 16-bit Length field of the device path node specified by Node.
161 DevicePathNodeLength (
167 ASSERT (Node
!= NULL
);
168 Length
= ReadUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0]);
169 ASSERT (Length
>= sizeof (EFI_DEVICE_PATH_PROTOCOL
));
174 Returns a pointer to the next node in a device path.
176 Returns a pointer to the device path node that follows the device path node
179 If Node is NULL, then ASSERT().
181 @param Node A pointer to a device path node data structure.
183 @return a pointer to the device path node that follows the device path node
187 EFI_DEVICE_PATH_PROTOCOL
*
193 ASSERT (Node
!= NULL
);
194 return (EFI_DEVICE_PATH_PROTOCOL
*)((UINT8
*)(Node
) + DevicePathNodeLength(Node
));
198 Determines if a device path node is an end node of a device path.
199 This includes nodes that are the end of a device path instance and nodes that
200 are the end of an entire device path.
202 Determines if the device path node specified by Node is an end node of a device path.
203 This includes nodes that are the end of a device path instance and nodes that are the
204 end of an entire device path. If Node represents an end node of a device path,
205 then TRUE is returned. Otherwise, FALSE is returned.
207 If Node is NULL, then ASSERT().
209 @param Node A pointer to a device path node data structure.
211 @retval TRUE The device path node specified by Node is an end node of a
213 @retval FALSE The device path node specified by Node is not an end node of
219 IsDevicePathEndType (
223 ASSERT (Node
!= NULL
);
224 return (BOOLEAN
) (DevicePathType (Node
) == END_DEVICE_PATH_TYPE
);
228 Determines if a device path node is an end node of an entire device path.
230 Determines if a device path node specified by Node is an end node of an entire
231 device path. If Node represents the end of an entire device path, then TRUE is
232 returned. Otherwise, FALSE is returned.
234 If Node is NULL, then ASSERT().
236 @param Node A pointer to a device path node data structure.
238 @retval TRUE The device path node specified by Node is the end of an entire
240 @retval FALSE The device path node specified by Node is not the end of an
250 ASSERT (Node
!= NULL
);
251 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
);
255 Determines if a device path node is an end node of a device path instance.
257 Determines if a device path node specified by Node is an end node of a device
258 path instance. If Node represents the end of a device path instance, then TRUE
259 is returned. Otherwise, FALSE is returned.
261 If Node is NULL, then ASSERT().
263 @param Node A pointer to a device path node data structure.
265 @retval TRUE The device path node specified by Node is the end of a device
267 @retval FALSE The device path node specified by Node is not the end of a
268 device path instance.
273 IsDevicePathEndInstance (
277 ASSERT (Node
!= NULL
);
278 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_INSTANCE_DEVICE_PATH_SUBTYPE
);
282 Sets the length, in bytes, of a device path node.
284 Sets the length of the device path node specified by Node to the value specified
285 by NodeLength. NodeLength is returned. Node is not required to be aligned on
286 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()
287 be used to set the contents of the Length field.
289 If Node is NULL, then ASSERT().
290 If NodeLength >= SIZE_64KB, then ASSERT().
291 If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().
293 @param Node A pointer to a device path node data structure.
294 @param Length The length, in bytes, of the device path node.
301 SetDevicePathNodeLength (
306 ASSERT (Node
!= NULL
);
307 ASSERT ((Length
>= sizeof (EFI_DEVICE_PATH_PROTOCOL
)) && (Length
< SIZE_64KB
));
308 return WriteUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0], (UINT16
)(Length
));
312 Fills in all the fields of a device path node that is the end of an entire device path.
314 Fills in all the fields of a device path node specified by Node so Node represents
315 the end of an entire device path. The Type field of Node is set to
316 END_DEVICE_PATH_TYPE, the SubType field of Node is set to
317 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to
318 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary,
319 so it is recommended that a function such as WriteUnaligned16() be used to set
320 the contents of the Length field.
322 If Node is NULL, then ASSERT().
324 @param Node A pointer to a device path node data structure.
329 SetDevicePathEndNode (
333 ASSERT (Node
!= NULL
);
334 CopyMem (Node
, &mUefiDevicePathLibEndDevicePath
, sizeof (mUefiDevicePathLibEndDevicePath
));
338 Returns the size of a device path in bytes.
340 This function returns the size, in bytes, of the device path data structure
341 specified by DevicePath including the end of device path node.
342 If DevicePath is NULL or invalid, then 0 is returned.
344 @param DevicePath A pointer to a device path data structure.
346 @retval 0 If DevicePath is NULL or invalid.
347 @retval Others The size of a device path in bytes.
353 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
356 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
358 if (DevicePath
== NULL
) {
362 if (!IsDevicePathValid (DevicePath
, 0)) {
367 // Search for the end of the device path structure
370 while (!IsDevicePathEnd (DevicePath
)) {
371 DevicePath
= NextDevicePathNode (DevicePath
);
375 // Compute the size and add back in the size of the end device path structure
377 return ((UINTN
) DevicePath
- (UINTN
) Start
) + DevicePathNodeLength (DevicePath
);
381 Creates a new copy of an existing device path.
383 This function allocates space for a new copy of the device path specified by DevicePath.
384 If DevicePath is NULL, then NULL is returned. If the memory is successfully
385 allocated, then the contents of DevicePath are copied to the newly allocated
386 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.
387 The memory for the new device path is allocated from EFI boot services memory.
388 It is the responsibility of the caller to free the memory allocated.
390 @param DevicePath A pointer to a device path data structure.
392 @retval NULL DevicePath is NULL or invalid.
393 @retval Others A pointer to the duplicated device path.
396 EFI_DEVICE_PATH_PROTOCOL
*
398 DuplicateDevicePath (
399 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
407 Size
= GetDevicePathSize (DevicePath
);
413 // Allocate space for duplicate device path
416 return AllocateCopyPool (Size
, DevicePath
);
420 Creates a new device path by appending a second device path to a first device path.
422 This function creates a new device path by appending a copy of SecondDevicePath
423 to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path
424 device node from SecondDevicePath is retained. The newly created device path is
425 returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
426 SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,
427 and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
428 SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.
430 If there is not enough memory for the newly allocated buffer, then NULL is returned.
431 The memory for the new device path is allocated from EFI boot services memory.
432 It is the responsibility of the caller to free the memory allocated.
434 @param FirstDevicePath A pointer to a device path data structure.
435 @param SecondDevicePath A pointer to a device path data structure.
437 @retval NULL If there is not enough memory for the newly allocated buffer.
438 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.
439 @retval Others A pointer to the new device path if success.
440 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
443 EFI_DEVICE_PATH_PROTOCOL
*
446 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
447 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
453 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
454 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
457 // If there's only 1 path, just duplicate it.
459 if (FirstDevicePath
== NULL
) {
460 return DuplicateDevicePath ((SecondDevicePath
!= NULL
) ? SecondDevicePath
: &mUefiDevicePathLibEndDevicePath
);
463 if (SecondDevicePath
== NULL
) {
464 return DuplicateDevicePath (FirstDevicePath
);
467 if (!IsDevicePathValid (FirstDevicePath
, 0) || !IsDevicePathValid (SecondDevicePath
, 0)) {
472 // Allocate space for the combined device path. It only has one end node of
473 // length EFI_DEVICE_PATH_PROTOCOL.
475 Size1
= GetDevicePathSize (FirstDevicePath
);
476 Size2
= GetDevicePathSize (SecondDevicePath
);
477 Size
= Size1
+ Size2
- END_DEVICE_PATH_LENGTH
;
479 NewDevicePath
= AllocatePool (Size
);
481 if (NewDevicePath
!= NULL
) {
482 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
484 // Over write FirstDevicePath EndNode and do the copy
486 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
487 (Size1
- END_DEVICE_PATH_LENGTH
));
488 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
491 return NewDevicePath
;
495 Creates a new path by appending the device node to the device path.
497 This function creates a new device path by appending a copy of the device node
498 specified by DevicePathNode to a copy of the device path specified by DevicePath
499 in an allocated buffer. The end-of-device-path device node is moved after the
500 end of the appended device node.
501 If DevicePathNode is NULL then a copy of DevicePath is returned.
502 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
503 path device node is returned.
504 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
505 device node is returned.
506 If there is not enough memory to allocate space for the new device path, then
508 The memory is allocated from EFI boot services memory. It is the responsibility
509 of the caller to free the memory allocated.
511 @param DevicePath A pointer to a device path data structure.
512 @param DevicePathNode A pointer to a single device path node.
514 @retval NULL If there is not enough memory for the new device path.
515 @retval Others A pointer to the new device path if success.
516 A copy of DevicePathNode followed by an end-of-device-path node
517 if both FirstDevicePath and SecondDevicePath are NULL.
518 A copy of an end-of-device-path node if both FirstDevicePath
519 and SecondDevicePath are NULL.
522 EFI_DEVICE_PATH_PROTOCOL
*
524 AppendDevicePathNode (
525 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
526 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
529 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
530 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
531 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
534 if (DevicePathNode
== NULL
) {
535 return DuplicateDevicePath ((DevicePath
!= NULL
) ? DevicePath
: &mUefiDevicePathLibEndDevicePath
);
538 // Build a Node that has a terminator on it
540 NodeLength
= DevicePathNodeLength (DevicePathNode
);
542 TempDevicePath
= AllocatePool (NodeLength
+ END_DEVICE_PATH_LENGTH
);
543 if (TempDevicePath
== NULL
) {
546 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
548 // Add and end device path node to convert Node to device path
550 NextNode
= NextDevicePathNode (TempDevicePath
);
551 SetDevicePathEndNode (NextNode
);
553 // Append device paths
555 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
557 FreePool (TempDevicePath
);
559 return NewDevicePath
;
563 Creates a new device path by appending the specified device path instance to the specified device
566 This function creates a new device path by appending a copy of the device path
567 instance specified by DevicePathInstance to a copy of the device path specified
568 by DevicePath in a allocated buffer.
569 The end-of-device-path device node is moved after the end of the appended device
570 path instance and a new end-of-device-path-instance node is inserted between.
571 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
572 If DevicePathInstance is NULL, then NULL is returned.
573 If DevicePath or DevicePathInstance is invalid, then NULL is returned.
574 If there is not enough memory to allocate space for the new device path, then
576 The memory is allocated from EFI boot services memory. It is the responsibility
577 of the caller to free the memory allocated.
579 @param DevicePath A pointer to a device path data structure.
580 @param DevicePathInstance A pointer to a device path instance.
582 @return A pointer to the new device path.
585 EFI_DEVICE_PATH_PROTOCOL
*
587 AppendDevicePathInstance (
588 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
589 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
592 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
593 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
597 if (DevicePath
== NULL
) {
598 return DuplicateDevicePath (DevicePathInstance
);
601 if (DevicePathInstance
== NULL
) {
605 if (!IsDevicePathValid (DevicePath
, 0) || !IsDevicePathValid (DevicePathInstance
, 0)) {
609 SrcSize
= GetDevicePathSize (DevicePath
);
610 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
612 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
613 if (NewDevicePath
!= NULL
) {
615 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
617 while (!IsDevicePathEnd (TempDevicePath
)) {
618 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
621 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
622 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
623 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
626 return NewDevicePath
;
630 Creates a copy of the current device path instance and returns a pointer to the next device path
633 This function creates a copy of the current device path instance. It also updates
634 DevicePath to point to the next device path instance in the device path (or NULL
635 if no more) and updates Size to hold the size of the device path instance copy.
636 If DevicePath is NULL, then NULL is returned.
637 If DevicePath points to a invalid device path, then NULL is returned.
638 If there is not enough memory to allocate space for the new device path, then
640 The memory is allocated from EFI boot services memory. It is the responsibility
641 of the caller to free the memory allocated.
642 If Size is NULL, then ASSERT().
644 @param DevicePath On input, this holds the pointer to the current
645 device path instance. On output, this holds
646 the pointer to the next device path instance
647 or NULL if there are no more device path
648 instances in the device path pointer to a
649 device path data structure.
650 @param Size On output, this holds the size of the device
651 path instance, in bytes or zero, if DevicePath
654 @return A pointer to the current device path instance.
657 EFI_DEVICE_PATH_PROTOCOL
*
659 GetNextDevicePathInstance (
660 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
664 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
665 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
668 ASSERT (Size
!= NULL
);
670 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
675 if (!IsDevicePathValid (*DevicePath
, 0)) {
680 // Find the end of the device path instance
682 DevPath
= *DevicePath
;
683 while (!IsDevicePathEndType (DevPath
)) {
684 DevPath
= NextDevicePathNode (DevPath
);
688 // Compute the size of the device path instance
690 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
693 // Make a copy and return the device path instance
695 Temp
= DevPath
->SubType
;
696 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
697 ReturnValue
= DuplicateDevicePath (*DevicePath
);
698 DevPath
->SubType
= Temp
;
701 // If DevPath is the end of an entire device path, then another instance
702 // does not follow, so *DevicePath is set to NULL.
704 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
707 *DevicePath
= NextDevicePathNode (DevPath
);
714 Creates a device node.
716 This function creates a new device node in a newly allocated buffer of size
717 NodeLength and initializes the device path node header with NodeType and NodeSubType.
718 The new device path node is returned.
719 If NodeLength is smaller than a device path header, then NULL is returned.
720 If there is not enough memory to allocate space for the new device path, then
722 The memory is allocated from EFI boot services memory. It is the responsibility
723 of the caller to free the memory allocated.
725 @param NodeType The device node type for the new device node.
726 @param NodeSubType The device node sub-type for the new device node.
727 @param NodeLength The length of the new device node.
729 @return The new device path.
732 EFI_DEVICE_PATH_PROTOCOL
*
736 IN UINT8 NodeSubType
,
740 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
742 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
744 // NodeLength is less than the size of the header.
749 DevicePath
= AllocateZeroPool (NodeLength
);
750 if (DevicePath
!= NULL
) {
751 DevicePath
->Type
= NodeType
;
752 DevicePath
->SubType
= NodeSubType
;
753 SetDevicePathNodeLength (DevicePath
, NodeLength
);
760 Determines if a device path is single or multi-instance.
762 This function returns TRUE if the device path specified by DevicePath is
764 Otherwise, FALSE is returned.
765 If DevicePath is NULL or invalid, then FALSE is returned.
767 @param DevicePath A pointer to a device path data structure.
769 @retval TRUE DevicePath is multi-instance.
770 @retval FALSE DevicePath is not multi-instance, or DevicePath
776 IsDevicePathMultiInstance (
777 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
780 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
782 if (DevicePath
== NULL
) {
786 if (!IsDevicePathValid (DevicePath
, 0)) {
791 while (!IsDevicePathEnd (Node
)) {
792 if (IsDevicePathEndInstance (Node
)) {
796 Node
= NextDevicePathNode (Node
);
804 Retrieves the device path protocol from a handle.
806 This function returns the device path protocol from the handle specified by Handle.
807 If Handle is NULL or Handle does not contain a device path protocol, then NULL
810 @param Handle The handle from which to retrieve the device
813 @return The device path protocol from the handle specified by Handle.
816 EFI_DEVICE_PATH_PROTOCOL
*
818 DevicePathFromHandle (
822 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
825 Status
= gBS
->HandleProtocol (
827 &gEfiDevicePathProtocolGuid
,
830 if (EFI_ERROR (Status
)) {
837 Allocates a device path for a file and appends it to an existing device path.
839 If Device is a valid device handle that contains a device path protocol, then a device path for
840 the file specified by FileName is allocated and appended to the device path associated with the
841 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
842 that does not support the device path protocol, then a device path containing a single device
843 path node for the file specified by FileName is allocated and returned.
844 The memory for the new device path is allocated from EFI boot services memory. It is the responsibility
845 of the caller to free the memory allocated.
847 If FileName is NULL, then ASSERT().
848 If FileName is not aligned on a 16-bit boundary, then ASSERT().
850 @param Device A pointer to a device handle. This parameter
851 is optional and may be NULL.
852 @param FileName A pointer to a Null-terminated Unicode string.
854 @return The allocated device path.
857 EFI_DEVICE_PATH_PROTOCOL
*
860 IN EFI_HANDLE Device
, OPTIONAL
861 IN CONST CHAR16
*FileName
865 FILEPATH_DEVICE_PATH
*FilePath
;
866 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
867 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
871 Size
= StrSize (FileName
);
872 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ END_DEVICE_PATH_LENGTH
);
873 if (FileDevicePath
!= NULL
) {
874 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
875 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
876 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
877 CopyMem (&FilePath
->PathName
, FileName
, Size
);
878 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
879 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
881 if (Device
!= NULL
) {
882 DevicePath
= DevicePathFromHandle (Device
);
885 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
886 FreePool (FileDevicePath
);