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 - 2008, Intel Corporation
12 All rights reserved. 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>
33 // Template for an end-of-device path node.
35 GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath
= {
37 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
39 END_DEVICE_PATH_LENGTH
,
45 Returns the Type field of a device path node.
47 Returns the Type field of the device path node specified by Node.
49 If Node is NULL, then ASSERT().
51 @param Node A pointer to a device path node data structure.
53 @return The Type field of the device path node specified by Node.
61 ASSERT (Node
!= NULL
);
62 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Type
;
66 Returns the SubType field of a device path node.
68 Returns the SubType field of the device path node specified by Node.
70 If Node is NULL, then ASSERT().
72 @param Node A pointer to a device path node data structure.
74 @return The SubType field of the device path node specified by Node.
82 ASSERT (Node
!= NULL
);
83 return ((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->SubType
;
87 Returns the 16-bit Length field of a device path node.
89 Returns the 16-bit Length field of the device path node specified by Node.
90 Node is not required to be aligned on a 16-bit boundary, so it is recommended
91 that a function such as ReadUnaligned16() be used to extract the contents of
94 If Node is NULL, then ASSERT().
96 @param Node A pointer to a device path node data structure.
98 @return The 16-bit Length field of the device path node specified by Node.
102 DevicePathNodeLength (
106 ASSERT (Node
!= NULL
);
107 return ReadUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0]);
111 Returns a pointer to the next node in a device path.
113 Returns a pointer to the device path node that follows the device path node specified by Node.
115 If Node is NULL, then ASSERT().
117 @param Node A pointer to a device path node data structure.
119 @return a pointer to the device path node that follows the device path node specified by Node.
122 EFI_DEVICE_PATH_PROTOCOL
*
127 ASSERT (Node
!= NULL
);
128 return (EFI_DEVICE_PATH_PROTOCOL
*)((UINT8
*)(Node
) + DevicePathNodeLength(Node
));
132 Determines if a device path node is an end node of a device path.
133 This includes nodes that are the end of a device path instance and nodes that are the end of an entire device path.
135 Determines if the device path node specified by Node is an end node of a device path.
136 This includes nodes that are the end of a device path instance and nodes that are the
137 end of an entire device path. If Node represents an end node of a device path,
138 then TRUE is returned. Otherwise, FALSE is returned.
140 If Node is NULL, then ASSERT().
142 @param Node A pointer to a device path node data structure.
144 @retval TRUE The device path node specified by Node is an end node of a device path.
145 @retval FALSE The device path node specified by Node is not an end node of a device path.
149 IsDevicePathEndType (
153 ASSERT (Node
!= NULL
);
154 return (BOOLEAN
) (DevicePathType (Node
) == END_DEVICE_PATH_TYPE
);
158 Determines if a device path node is an end node of an entire device path.
160 Determines if a device path node specified by Node is an end node of an entire device path.
161 If Node represents the end of an entire device path, then TRUE is returned. Otherwise, FALSE is returned.
163 If Node is NULL, then ASSERT().
165 @param Node A pointer to a device path node data structure.
167 @retval TRUE The device path node specified by Node is the end of an entire device path.
168 @retval FALSE The device path node specified by Node is not the end of an entire device path.
176 ASSERT (Node
!= NULL
);
177 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
);
181 Determines if a device path node is an end node of a device path instance.
183 Determines if a device path node specified by Node is an end node of a device path instance.
184 If Node represents the end of a device path instance, then TRUE is returned. Otherwise, FALSE is returned.
186 If Node is NULL, then ASSERT().
188 @param Node A pointer to a device path node data structure.
190 @retval TRUE The device path node specified by Node is the end of a device path instance.
191 @retval FALSE The device path node specified by Node is not the end of a device path instance.
195 IsDevicePathEndInstance (
199 ASSERT (Node
!= NULL
);
200 return (BOOLEAN
) (IsDevicePathEndType (Node
) && DevicePathSubType(Node
) == END_INSTANCE_DEVICE_PATH_SUBTYPE
);
204 Sets the length, in bytes, of a device path node.
206 Sets the length of the device path node specified by Node to the value specified
207 by NodeLength. NodeLength is returned. Node is not required to be aligned on
208 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()
209 be used to set the contents of the Length field.
211 If Node is NULL, then ASSERT().
212 If NodeLength >= 0x10000, then ASSERT().
214 @param Node A pointer to a device path node data structure.
215 @param Length The length, in bytes, of the device path node.
221 SetDevicePathNodeLength (
226 ASSERT (Node
!= NULL
);
227 ASSERT (Length
< 0x10000);
228 return WriteUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0], (UINT16
)(Length
));
232 Fills in all the fields of a device path node that is the end of an entire device path.
234 Fills in all the fields of a device path node specified by Node so Node represents
235 the end of an entire device path. The Type field of Node is set to
236 END_DEVICE_PATH_TYPE, the SubType field of Node is set to
237 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to
238 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary,
239 so it is recommended that a function such as WriteUnaligned16() be used to set
240 the contents of the Length field.
242 If Node is NULL, then ASSERT().
244 @param Node A pointer to a device path node data structure.
248 SetDevicePathEndNode (
252 ASSERT (Node
!= NULL
);
253 CopyMem (Node
, &mUefiDevicePathLibEndDevicePath
, sizeof (mUefiDevicePathLibEndDevicePath
));
257 Returns the size of a device path in bytes.
259 This function returns the size, in bytes, of the device path data structure specified by
260 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
262 @param DevicePath A pointer to a device path data structure.
264 @retval 0 If DevicePath is NULL.
265 @retval Others The size of a device path in bytes.
271 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
274 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
276 if (DevicePath
== NULL
) {
281 // Search for the end of the device path structure
284 while (!IsDevicePathEnd (DevicePath
)) {
285 DevicePath
= NextDevicePathNode (DevicePath
);
289 // Compute the size and add back in the size of the end device path structure
291 return ((UINTN
) DevicePath
- (UINTN
) Start
) + DevicePathNodeLength (DevicePath
);
295 Creates a new copy of an existing device path.
297 This function allocates space for a new copy of the device path specified by DevicePath. If
298 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
299 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
300 is returned. Otherwise, NULL is returned.
301 The memory for the new device path is allocated from EFI boot services memory.
302 It is the responsibility of the caller to free the memory allocated.
304 @param DevicePath A pointer to a device path data structure.
306 @retval NULL If DevicePath is NULL.
307 @retval Others A pointer to the duplicated device path.
310 EFI_DEVICE_PATH_PROTOCOL
*
312 DuplicateDevicePath (
313 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
321 Size
= GetDevicePathSize (DevicePath
);
327 // Allocate space for duplicate device path
330 return AllocateCopyPool (Size
, DevicePath
);
334 Creates a new device path by appending a second device path to a first device path.
336 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
337 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
338 SecondDevicePath is retained. The newly created device path is returned.
339 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
340 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
341 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is
343 If there is not enough memory for the newly allocated buffer, then NULL is returned.
344 The memory for the new device path is allocated from EFI boot services memory. It is the
345 responsibility of the caller to free the memory allocated.
347 @param FirstDevicePath A pointer to a device path data structure.
348 @param SecondDevicePath A pointer to a device path data structure.
350 @retval NULL If there is not enough memory for the newly allocated buffer.
351 @retval Others A pointer to the new device path if success.
352 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
355 EFI_DEVICE_PATH_PROTOCOL
*
358 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
359 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
365 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
366 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
369 // If there's only 1 path, just duplicate it.
371 if (FirstDevicePath
== NULL
) {
372 return DuplicateDevicePath ((SecondDevicePath
!= NULL
) ? SecondDevicePath
: &mUefiDevicePathLibEndDevicePath
);
375 if (SecondDevicePath
== NULL
) {
376 return DuplicateDevicePath (FirstDevicePath
);
380 // Allocate space for the combined device path. It only has one end node of
381 // length EFI_DEVICE_PATH_PROTOCOL.
383 Size1
= GetDevicePathSize (FirstDevicePath
);
384 Size2
= GetDevicePathSize (SecondDevicePath
);
385 Size
= Size1
+ Size2
- END_DEVICE_PATH_LENGTH
;
387 NewDevicePath
= AllocatePool (Size
);
389 if (NewDevicePath
!= NULL
) {
390 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
392 // Over write FirstDevicePath EndNode and do the copy
394 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
395 (Size1
- END_DEVICE_PATH_LENGTH
));
396 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
399 return NewDevicePath
;
403 Creates a new path by appending the device node to the device path.
405 This function creates a new device path by appending a copy of the device node specified by
406 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
407 The end-of-device-path device node is moved after the end of the appended device node.
408 If DevicePathNode is NULL then a copy of DevicePath is returned.
409 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device path device
411 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node
413 If there is not enough memory to allocate space for the new device path, then NULL is returned.
414 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
415 free the memory allocated.
417 @param DevicePath A pointer to a device path data structure.
418 @param DevicePathNode A pointer to a single device path node.
420 @retval NULL If there is not enough memory for the new device path.
421 @retval Others A pointer to the new device path if success.
422 A copy of DevicePathNode followed by an end-of-device-path node
423 if both FirstDevicePath and SecondDevicePath are NULL.
424 A copy of an end-of-device-path node if both FirstDevicePath and SecondDevicePath are NULL.
427 EFI_DEVICE_PATH_PROTOCOL
*
429 AppendDevicePathNode (
430 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
431 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
434 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
435 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
436 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
439 if (DevicePathNode
== NULL
) {
440 return DuplicateDevicePath ((DevicePath
!= NULL
) ? DevicePath
: &mUefiDevicePathLibEndDevicePath
);
443 // Build a Node that has a terminator on it
445 NodeLength
= DevicePathNodeLength (DevicePathNode
);
447 TempDevicePath
= AllocatePool (NodeLength
+ END_DEVICE_PATH_LENGTH
);
448 if (TempDevicePath
== NULL
) {
451 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
453 // Add and end device path node to convert Node to device path
455 NextNode
= NextDevicePathNode (TempDevicePath
);
456 SetDevicePathEndNode (NextNode
);
458 // Append device paths
460 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
462 FreePool (TempDevicePath
);
464 return NewDevicePath
;
468 Creates a new device path by appending the specified device path instance to the specified device
471 This function creates a new device path by appending a copy of the device path instance specified
472 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
473 The end-of-device-path device node is moved after the end of the appended device path instance
474 and a new end-of-device-path-instance node is inserted between.
475 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
476 If DevicePathInstance is NULL, then NULL is returned.
477 If there is not enough memory to allocate space for the new device path, then NULL is returned.
478 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
479 free the memory allocated.
481 @param DevicePath A pointer to a device path data structure.
482 @param DevicePathInstance A pointer to a device path instance.
484 @return A pointer to the new device path.
487 EFI_DEVICE_PATH_PROTOCOL
*
489 AppendDevicePathInstance (
490 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
491 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
494 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
495 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
499 if (DevicePath
== NULL
) {
500 return DuplicateDevicePath (DevicePathInstance
);
503 if (DevicePathInstance
== NULL
) {
507 SrcSize
= GetDevicePathSize (DevicePath
);
508 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
510 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
511 if (NewDevicePath
!= NULL
) {
513 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
515 while (!IsDevicePathEnd (TempDevicePath
)) {
516 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
519 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
520 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
521 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
524 return NewDevicePath
;
528 Creates a copy of the current device path instance and returns a pointer to the next device path
531 This function creates a copy of the current device path instance. It also updates DevicePath to
532 point to the next device path instance in the device path (or NULL if no more) and updates Size
533 to hold the size of the device path instance copy.
534 If DevicePath is NULL, then NULL is returned.
535 If there is not enough memory to allocate space for the new device path, then NULL is returned.
536 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
537 free the memory allocated.
538 If Size is NULL, then ASSERT().
540 @param DevicePath On input, this holds the pointer to the current device path
541 instance. On output, this holds the pointer to the next device
542 path instance or NULL if there are no more device path
543 instances in the device path pointer to a device path data
545 @param Size On output, this holds the size of the device path instance, in
546 bytes or zero, if DevicePath is NULL.
548 @return A pointer to the current device path instance.
551 EFI_DEVICE_PATH_PROTOCOL
*
553 GetNextDevicePathInstance (
554 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
558 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
559 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
562 ASSERT (Size
!= NULL
);
564 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
570 // Find the end of the device path instance
572 DevPath
= *DevicePath
;
573 while (!IsDevicePathEndType (DevPath
)) {
574 DevPath
= NextDevicePathNode (DevPath
);
578 // Compute the size of the device path instance
580 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
583 // Make a copy and return the device path instance
585 Temp
= DevPath
->SubType
;
586 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
587 ReturnValue
= DuplicateDevicePath (*DevicePath
);
588 DevPath
->SubType
= Temp
;
591 // If DevPath is the end of an entire device path, then another instance
592 // does not follow, so *DevicePath is set to NULL.
594 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
597 *DevicePath
= NextDevicePathNode (DevPath
);
604 Creates a device node.
606 This function creates a new device node in a newly allocated buffer of size NodeLength and
607 initializes the device path node header with NodeType and NodeSubType. The new device path node
609 If NodeLength is smaller than a device path header, then NULL is returned.
610 If there is not enough memory to allocate space for the new device path, then NULL is returned.
611 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
612 free the memory allocated.
614 @param NodeType The device node type for the new device node.
615 @param NodeSubType The device node sub-type for the new device node.
616 @param NodeLength The length of the new device node.
618 @return The new device path.
621 EFI_DEVICE_PATH_PROTOCOL
*
625 IN UINT8 NodeSubType
,
629 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
631 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
633 // NodeLength is less than the size of the header.
638 DevicePath
= AllocateZeroPool (NodeLength
);
639 if (DevicePath
!= NULL
) {
640 DevicePath
->Type
= NodeType
;
641 DevicePath
->SubType
= NodeSubType
;
642 SetDevicePathNodeLength (DevicePath
, NodeLength
);
649 Determines if a device path is single or multi-instance.
651 This function returns TRUE if the device path specified by DevicePath is multi-instance.
652 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
654 @param DevicePath A pointer to a device path data structure.
656 @retval TRUE DevicePath is multi-instance.
657 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
662 IsDevicePathMultiInstance (
663 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
666 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
668 if (DevicePath
== NULL
) {
673 while (!IsDevicePathEnd (Node
)) {
674 if (IsDevicePathEndInstance (Node
)) {
678 Node
= NextDevicePathNode (Node
);
686 Retrieves the device path protocol from a handle.
688 This function returns the device path protocol from the handle specified by Handle. If Handle is
689 NULL or Handle does not contain a device path protocol, then NULL is returned.
691 @param Handle The handle from which to retrieve the device path protocol.
693 @return The device path protocol from the handle specified by Handle.
696 EFI_DEVICE_PATH_PROTOCOL
*
698 DevicePathFromHandle (
702 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
705 Status
= gBS
->HandleProtocol (
707 &gEfiDevicePathProtocolGuid
,
710 if (EFI_ERROR (Status
)) {
717 Allocates a device path for a file and appends it to an existing device path.
719 If Device is a valid device handle that contains a device path protocol, then a device path for
720 the file specified by FileName is allocated and appended to the device path associated with the
721 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
722 that does not support the device path protocol, then a device path containing a single device
723 path node for the file specified by FileName is allocated and returned.
724 The memory for the new device path is allocated from EFI boot services memory. It is the responsibility
725 of the caller to free the memory allocated.
727 If FileName is NULL, then ASSERT().
728 If FileName is not aligned on a 16-bit boundary, then ASSERT().
730 @param Device A pointer to a device handle. This parameter is optional and
732 @param FileName A pointer to a Null-terminated Unicode string.
734 @return The allocated device path.
737 EFI_DEVICE_PATH_PROTOCOL
*
740 IN EFI_HANDLE Device
, OPTIONAL
741 IN CONST CHAR16
*FileName
745 FILEPATH_DEVICE_PATH
*FilePath
;
746 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
747 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
751 Size
= (UINT16
) StrSize (FileName
);
753 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ END_DEVICE_PATH_LENGTH
);
754 if (FileDevicePath
!= NULL
) {
755 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
756 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
757 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
758 CopyMem (&FilePath
->PathName
, FileName
, Size
);
759 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
760 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
762 if (Device
!= NULL
) {
763 DevicePath
= DevicePathFromHandle (Device
);
766 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
767 FreePool (FileDevicePath
);