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 - 2013, 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
);
64 for (Count
= 0, Size
= 0; !IsDevicePathEnd (DevicePath
); DevicePath
= NextDevicePathNode (DevicePath
)) {
65 NodeLength
= DevicePathNodeLength (DevicePath
);
66 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
72 if (Size
+ END_DEVICE_PATH_LENGTH
> MaxSize
) {
77 if (PcdGet32 (PcdMaximumDevicePathNodeCount
) > 0) {
79 if (Count
>= PcdGet32 (PcdMaximumDevicePathNodeCount
)) {
86 // Only return TRUE when the End Device Path node is valid.
88 return (BOOLEAN
) (DevicePathNodeLength (DevicePath
) == END_DEVICE_PATH_LENGTH
);
92 Returns the Type field of a device path node.
94 Returns the Type field of the device path node specified by Node.
96 If Node is NULL, then ASSERT().
98 @param Node A pointer to a device path node data structure.
100 @return The Type field of the device path node specified by Node.
109 ASSERT (Node
!= NULL
);
110 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Type
;
114 Returns the SubType field of a device path node.
116 Returns the SubType field of the device path node specified by Node.
118 If Node is NULL, then ASSERT().
120 @param Node A pointer to a device path node data structure.
122 @return The SubType field of the device path node specified by Node.
131 ASSERT (Node
!= NULL
);
132 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->SubType
;
136 Returns the 16-bit Length field of a device path node.
138 Returns the 16-bit Length field of the device path node specified by Node.
139 Node is not required to be aligned on a 16-bit boundary, so it is recommended
140 that a function such as ReadUnaligned16() be used to extract the contents of
143 If Node is NULL, then ASSERT().
145 @param Node A pointer to a device path node data structure.
147 @return The 16-bit Length field of the device path node specified by Node.
152 DevicePathNodeLength (
158 ASSERT (Node
!= NULL
);
159 Length
= ReadUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0]);
160 ASSERT (Length
>= sizeof (EFI_DEVICE_PATH_PROTOCOL
));
165 Returns a pointer to the next node in a device path.
167 Returns a pointer to the device path node that follows the device path node
170 If Node is NULL, then ASSERT().
172 @param Node A pointer to a device path node data structure.
174 @return a pointer to the device path node that follows the device path node
178 EFI_DEVICE_PATH_PROTOCOL
*
184 ASSERT (Node
!= NULL
);
185 return (EFI_DEVICE_PATH_PROTOCOL
*)((UINT8
*)(Node
) + DevicePathNodeLength(Node
));
189 Determines if a device path node is an end node of a device path.
190 This includes nodes that are the end of a device path instance and nodes that
191 are the end of an entire device path.
193 Determines if the device path node specified by Node is an end node of a device path.
194 This includes nodes that are the end of a device path instance and nodes that are the
195 end of an entire device path. If Node represents an end node of a device path,
196 then TRUE is returned. Otherwise, FALSE is returned.
198 If Node is NULL, then ASSERT().
200 @param Node A pointer to a device path node data structure.
202 @retval TRUE The device path node specified by Node is an end node of a
204 @retval FALSE The device path node specified by Node is not an end node of
210 IsDevicePathEndType (
214 ASSERT (Node
!= NULL
);
215 return (BOOLEAN
) (DevicePathType (Node
) == END_DEVICE_PATH_TYPE
);
219 Determines if a device path node is an end node of an entire device path.
221 Determines if a device path node specified by Node is an end node of an entire
222 device path. If Node represents the end of an entire device path, then TRUE is
223 returned. Otherwise, FALSE is returned.
225 If Node is NULL, then ASSERT().
227 @param Node A pointer to a device path node data structure.
229 @retval TRUE The device path node specified by Node is the end of an entire
231 @retval FALSE The device path node specified by Node is not the end of an
241 ASSERT (Node
!= NULL
);
242 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
);
246 Determines if a device path node is an end node of a device path instance.
248 Determines if a device path node specified by Node is an end node of a device
249 path instance. If Node represents the end of a device path instance, then TRUE
250 is returned. Otherwise, FALSE is returned.
252 If Node is NULL, then ASSERT().
254 @param Node A pointer to a device path node data structure.
256 @retval TRUE The device path node specified by Node is the end of a device
258 @retval FALSE The device path node specified by Node is not the end of a
259 device path instance.
264 IsDevicePathEndInstance (
268 ASSERT (Node
!= NULL
);
269 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_INSTANCE_DEVICE_PATH_SUBTYPE
);
273 Sets the length, in bytes, of a device path node.
275 Sets the length of the device path node specified by Node to the value specified
276 by NodeLength. NodeLength is returned. Node is not required to be aligned on
277 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()
278 be used to set the contents of the Length field.
280 If Node is NULL, then ASSERT().
281 If NodeLength >= SIZE_64KB, then ASSERT().
282 If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().
284 @param Node A pointer to a device path node data structure.
285 @param Length The length, in bytes, of the device path node.
292 SetDevicePathNodeLength (
297 ASSERT (Node
!= NULL
);
298 ASSERT ((Length
>= sizeof (EFI_DEVICE_PATH_PROTOCOL
)) && (Length
< SIZE_64KB
));
299 return WriteUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0], (UINT16
)(Length
));
303 Fills in all the fields of a device path node that is the end of an entire device path.
305 Fills in all the fields of a device path node specified by Node so Node represents
306 the end of an entire device path. The Type field of Node is set to
307 END_DEVICE_PATH_TYPE, the SubType field of Node is set to
308 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to
309 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary,
310 so it is recommended that a function such as WriteUnaligned16() be used to set
311 the contents of the Length field.
313 If Node is NULL, then ASSERT().
315 @param Node A pointer to a device path node data structure.
320 SetDevicePathEndNode (
324 ASSERT (Node
!= NULL
);
325 CopyMem (Node
, &mUefiDevicePathLibEndDevicePath
, sizeof (mUefiDevicePathLibEndDevicePath
));
329 Returns the size of a device path in bytes.
331 This function returns the size, in bytes, of the device path data structure
332 specified by DevicePath including the end of device path node.
333 If DevicePath is NULL or invalid, then 0 is returned.
335 @param DevicePath A pointer to a device path data structure.
337 @retval 0 If DevicePath is NULL or invalid.
338 @retval Others The size of a device path in bytes.
343 UefiDevicePathLibGetDevicePathSize (
344 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
347 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
349 if (DevicePath
== NULL
) {
353 if (!IsDevicePathValid (DevicePath
, 0)) {
358 // Search for the end of the device path structure
361 while (!IsDevicePathEnd (DevicePath
)) {
362 DevicePath
= NextDevicePathNode (DevicePath
);
366 // Compute the size and add back in the size of the end device path structure
368 return ((UINTN
) DevicePath
- (UINTN
) Start
) + DevicePathNodeLength (DevicePath
);
372 Creates a new copy of an existing device path.
374 This function allocates space for a new copy of the device path specified by DevicePath.
375 If DevicePath is NULL, then NULL is returned. If the memory is successfully
376 allocated, then the contents of DevicePath are copied to the newly allocated
377 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.
378 The memory for the new device path is allocated from EFI boot services memory.
379 It is the responsibility of the caller to free the memory allocated.
381 @param DevicePath A pointer to a device path data structure.
383 @retval NULL DevicePath is NULL or invalid.
384 @retval Others A pointer to the duplicated device path.
387 EFI_DEVICE_PATH_PROTOCOL
*
389 UefiDevicePathLibDuplicateDevicePath (
390 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
398 Size
= GetDevicePathSize (DevicePath
);
404 // Allocate space for duplicate device path
407 return AllocateCopyPool (Size
, DevicePath
);
411 Creates a new device path by appending a second device path to a first device path.
413 This function creates a new device path by appending a copy of SecondDevicePath
414 to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path
415 device node from SecondDevicePath is retained. The newly created device path is
416 returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
417 SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,
418 and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
419 SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.
421 If there is not enough memory for the newly allocated buffer, then NULL is returned.
422 The memory for the new device path is allocated from EFI boot services memory.
423 It is the responsibility of the caller to free the memory allocated.
425 @param FirstDevicePath A pointer to a device path data structure.
426 @param SecondDevicePath A pointer to a device path data structure.
428 @retval NULL If there is not enough memory for the newly allocated buffer.
429 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.
430 @retval Others A pointer to the new device path if success.
431 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
434 EFI_DEVICE_PATH_PROTOCOL
*
436 UefiDevicePathLibAppendDevicePath (
437 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
438 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
444 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
445 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
448 // If there's only 1 path, just duplicate it.
450 if (FirstDevicePath
== NULL
) {
451 return DuplicateDevicePath ((SecondDevicePath
!= NULL
) ? SecondDevicePath
: &mUefiDevicePathLibEndDevicePath
);
454 if (SecondDevicePath
== NULL
) {
455 return DuplicateDevicePath (FirstDevicePath
);
458 if (!IsDevicePathValid (FirstDevicePath
, 0) || !IsDevicePathValid (SecondDevicePath
, 0)) {
463 // Allocate space for the combined device path. It only has one end node of
464 // length EFI_DEVICE_PATH_PROTOCOL.
466 Size1
= GetDevicePathSize (FirstDevicePath
);
467 Size2
= GetDevicePathSize (SecondDevicePath
);
468 Size
= Size1
+ Size2
- END_DEVICE_PATH_LENGTH
;
470 NewDevicePath
= AllocatePool (Size
);
472 if (NewDevicePath
!= NULL
) {
473 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
475 // Over write FirstDevicePath EndNode and do the copy
477 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
478 (Size1
- END_DEVICE_PATH_LENGTH
));
479 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
482 return NewDevicePath
;
486 Creates a new path by appending the device node to the device path.
488 This function creates a new device path by appending a copy of the device node
489 specified by DevicePathNode to a copy of the device path specified by DevicePath
490 in an allocated buffer. The end-of-device-path device node is moved after the
491 end of the appended device node.
492 If DevicePathNode is NULL then a copy of DevicePath is returned.
493 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
494 path device node is returned.
495 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
496 device node is returned.
497 If there is not enough memory to allocate space for the new device path, then
499 The memory is allocated from EFI boot services memory. It is the responsibility
500 of the caller to free the memory allocated.
502 @param DevicePath A pointer to a device path data structure.
503 @param DevicePathNode A pointer to a single device path node.
505 @retval NULL If there is not enough memory for the new device path.
506 @retval Others A pointer to the new device path if success.
507 A copy of DevicePathNode followed by an end-of-device-path node
508 if both FirstDevicePath and SecondDevicePath are NULL.
509 A copy of an end-of-device-path node if both FirstDevicePath
510 and SecondDevicePath are NULL.
513 EFI_DEVICE_PATH_PROTOCOL
*
515 UefiDevicePathLibAppendDevicePathNode (
516 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
517 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
520 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
521 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
522 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
525 if (DevicePathNode
== NULL
) {
526 return DuplicateDevicePath ((DevicePath
!= NULL
) ? DevicePath
: &mUefiDevicePathLibEndDevicePath
);
529 // Build a Node that has a terminator on it
531 NodeLength
= DevicePathNodeLength (DevicePathNode
);
533 TempDevicePath
= AllocatePool (NodeLength
+ END_DEVICE_PATH_LENGTH
);
534 if (TempDevicePath
== NULL
) {
537 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
539 // Add and end device path node to convert Node to device path
541 NextNode
= NextDevicePathNode (TempDevicePath
);
542 SetDevicePathEndNode (NextNode
);
544 // Append device paths
546 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
548 FreePool (TempDevicePath
);
550 return NewDevicePath
;
554 Creates a new device path by appending the specified device path instance to the specified device
557 This function creates a new device path by appending a copy of the device path
558 instance specified by DevicePathInstance to a copy of the device path specified
559 by DevicePath in a allocated buffer.
560 The end-of-device-path device node is moved after the end of the appended device
561 path instance and a new end-of-device-path-instance node is inserted between.
562 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
563 If DevicePathInstance is NULL, then NULL is returned.
564 If DevicePath or DevicePathInstance is invalid, then NULL is returned.
565 If there is not enough memory to allocate space for the new device path, then
567 The memory is allocated from EFI boot services memory. It is the responsibility
568 of the caller to free the memory allocated.
570 @param DevicePath A pointer to a device path data structure.
571 @param DevicePathInstance A pointer to a device path instance.
573 @return A pointer to the new device path.
576 EFI_DEVICE_PATH_PROTOCOL
*
578 UefiDevicePathLibAppendDevicePathInstance (
579 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
580 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
583 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
584 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
588 if (DevicePath
== NULL
) {
589 return DuplicateDevicePath (DevicePathInstance
);
592 if (DevicePathInstance
== NULL
) {
596 if (!IsDevicePathValid (DevicePath
, 0) || !IsDevicePathValid (DevicePathInstance
, 0)) {
600 SrcSize
= GetDevicePathSize (DevicePath
);
601 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
603 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
604 if (NewDevicePath
!= NULL
) {
606 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
608 while (!IsDevicePathEnd (TempDevicePath
)) {
609 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
612 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
613 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
614 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
617 return NewDevicePath
;
621 Creates a copy of the current device path instance and returns a pointer to the next device path
624 This function creates a copy of the current device path instance. It also updates
625 DevicePath to point to the next device path instance in the device path (or NULL
626 if no more) and updates Size to hold the size of the device path instance copy.
627 If DevicePath is NULL, then NULL is returned.
628 If DevicePath points to a invalid device path, then NULL is returned.
629 If there is not enough memory to allocate space for the new device path, then
631 The memory is allocated from EFI boot services memory. It is the responsibility
632 of the caller to free the memory allocated.
633 If Size is NULL, then ASSERT().
635 @param DevicePath On input, this holds the pointer to the current
636 device path instance. On output, this holds
637 the pointer to the next device path instance
638 or NULL if there are no more device path
639 instances in the device path pointer to a
640 device path data structure.
641 @param Size On output, this holds the size of the device
642 path instance, in bytes or zero, if DevicePath
645 @return A pointer to the current device path instance.
648 EFI_DEVICE_PATH_PROTOCOL
*
650 UefiDevicePathLibGetNextDevicePathInstance (
651 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
655 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
656 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
659 ASSERT (Size
!= NULL
);
661 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
666 if (!IsDevicePathValid (*DevicePath
, 0)) {
671 // Find the end of the device path instance
673 DevPath
= *DevicePath
;
674 while (!IsDevicePathEndType (DevPath
)) {
675 DevPath
= NextDevicePathNode (DevPath
);
679 // Compute the size of the device path instance
681 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
684 // Make a copy and return the device path instance
686 Temp
= DevPath
->SubType
;
687 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
688 ReturnValue
= DuplicateDevicePath (*DevicePath
);
689 DevPath
->SubType
= Temp
;
692 // If DevPath is the end of an entire device path, then another instance
693 // does not follow, so *DevicePath is set to NULL.
695 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
698 *DevicePath
= NextDevicePathNode (DevPath
);
705 Creates a device node.
707 This function creates a new device node in a newly allocated buffer of size
708 NodeLength and initializes the device path node header with NodeType and NodeSubType.
709 The new device path node is returned.
710 If NodeLength is smaller than a device path header, then NULL is returned.
711 If there is not enough memory to allocate space for the new device path, then
713 The memory is allocated from EFI boot services memory. It is the responsibility
714 of the caller to free the memory allocated.
716 @param NodeType The device node type for the new device node.
717 @param NodeSubType The device node sub-type for the new device node.
718 @param NodeLength The length of the new device node.
720 @return The new device path.
723 EFI_DEVICE_PATH_PROTOCOL
*
725 UefiDevicePathLibCreateDeviceNode (
727 IN UINT8 NodeSubType
,
731 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
733 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
735 // NodeLength is less than the size of the header.
740 DevicePath
= AllocateZeroPool (NodeLength
);
741 if (DevicePath
!= NULL
) {
742 DevicePath
->Type
= NodeType
;
743 DevicePath
->SubType
= NodeSubType
;
744 SetDevicePathNodeLength (DevicePath
, NodeLength
);
751 Determines if a device path is single or multi-instance.
753 This function returns TRUE if the device path specified by DevicePath is
755 Otherwise, FALSE is returned.
756 If DevicePath is NULL or invalid, then FALSE is returned.
758 @param DevicePath A pointer to a device path data structure.
760 @retval TRUE DevicePath is multi-instance.
761 @retval FALSE DevicePath is not multi-instance, or DevicePath
767 UefiDevicePathLibIsDevicePathMultiInstance (
768 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
771 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
773 if (DevicePath
== NULL
) {
777 if (!IsDevicePathValid (DevicePath
, 0)) {
782 while (!IsDevicePathEnd (Node
)) {
783 if (IsDevicePathEndInstance (Node
)) {
787 Node
= NextDevicePathNode (Node
);
795 Retrieves the device path protocol from a handle.
797 This function returns the device path protocol from the handle specified by Handle.
798 If Handle is NULL or Handle does not contain a device path protocol, then NULL
801 @param Handle The handle from which to retrieve the device
804 @return The device path protocol from the handle specified by Handle.
807 EFI_DEVICE_PATH_PROTOCOL
*
809 DevicePathFromHandle (
813 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
816 Status
= gBS
->HandleProtocol (
818 &gEfiDevicePathProtocolGuid
,
821 if (EFI_ERROR (Status
)) {
828 Allocates a device path for a file and appends it to an existing device path.
830 If Device is a valid device handle that contains a device path protocol, then a device path for
831 the file specified by FileName is allocated and appended to the device path associated with the
832 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
833 that does not support the device path protocol, then a device path containing a single device
834 path node for the file specified by FileName is allocated and returned.
835 The memory for the new device path is allocated from EFI boot services memory. It is the responsibility
836 of the caller to free the memory allocated.
838 If FileName is NULL, then ASSERT().
839 If FileName is not aligned on a 16-bit boundary, then ASSERT().
841 @param Device A pointer to a device handle. This parameter
842 is optional and may be NULL.
843 @param FileName A pointer to a Null-terminated Unicode string.
845 @return The allocated device path.
848 EFI_DEVICE_PATH_PROTOCOL
*
851 IN EFI_HANDLE Device
, OPTIONAL
852 IN CONST CHAR16
*FileName
856 FILEPATH_DEVICE_PATH
*FilePath
;
857 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
858 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
862 Size
= StrSize (FileName
);
863 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ END_DEVICE_PATH_LENGTH
);
864 if (FileDevicePath
!= NULL
) {
865 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
866 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
867 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
868 CopyMem (&FilePath
->PathName
, FileName
, Size
);
869 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
870 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
872 if (Device
!= NULL
) {
873 DevicePath
= DevicePathFromHandle (Device
);
876 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
877 FreePool (FileDevicePath
);