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 <Protocol/DevicePath.h>
27 #include <Library/DevicePathLib.h>
28 #include <Library/BaseMemoryLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/MemoryAllocationLib.h>
31 #include <Library/UefiBootServicesTableLib.h>
32 #include <Library/BaseLib.h>
35 // Template for an end-of-device path node.
37 GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath
= {
39 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
41 END_DEVICE_PATH_LENGTH
,
47 Returns the size of a device path in bytes.
49 This function returns the size, in bytes, of the device path data structure specified by
50 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
52 @param DevicePath A pointer to a device path data structure.
54 @retval 0 If DevicePath is NULL.
55 @retval Others The size of a device path in bytes.
61 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
64 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
66 if (DevicePath
== NULL
) {
71 // Search for the end of the device path structure
74 while (!IsDevicePathEnd (DevicePath
)) {
75 DevicePath
= NextDevicePathNode (DevicePath
);
79 // Compute the size and add back in the size of the end device path structure
81 return ((UINTN
) DevicePath
- (UINTN
) Start
) + DevicePathNodeLength (DevicePath
);
85 Creates a new copy of an existing device path.
87 This function allocates space for a new copy of the device path specified by DevicePath. If
88 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
89 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
90 is returned. Otherwise, NULL is returned.
91 The memory for the new device path is allocated from EFI boot services memory.
92 It is the responsibility of the caller to free the memory allocated.
94 @param DevicePath A pointer to a device path data structure.
96 @retval NULL If DevicePath is NULL.
97 @retval Others A pointer to the duplicated device path.
100 EFI_DEVICE_PATH_PROTOCOL
*
102 DuplicateDevicePath (
103 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
111 Size
= GetDevicePathSize (DevicePath
);
117 // Allocate space for duplicate device path
120 return AllocateCopyPool (Size
, DevicePath
);
124 Creates a new device path by appending a second device path to a first device path.
126 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
127 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
128 SecondDevicePath is retained. The newly created device path is returned.
129 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
130 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
131 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is
133 If there is not enough memory for the newly allocated buffer, then NULL is returned.
134 The memory for the new device path is allocated from EFI boot services memory. It is the
135 responsibility of the caller to free the memory allocated.
137 @param FirstDevicePath A pointer to a device path data structure.
138 @param SecondDevicePath A pointer to a device path data structure.
140 @retval NULL If there is not enough memory for the newly allocated buffer.
141 @retval Others A pointer to the new device path if success.
142 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
145 EFI_DEVICE_PATH_PROTOCOL
*
148 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
149 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
155 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
156 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
159 // If there's only 1 path, just duplicate it.
161 if (FirstDevicePath
== NULL
) {
162 return DuplicateDevicePath ((SecondDevicePath
!= NULL
) ? SecondDevicePath
: &mUefiDevicePathLibEndDevicePath
);
165 if (SecondDevicePath
== NULL
) {
166 return DuplicateDevicePath (FirstDevicePath
);
170 // Allocate space for the combined device path. It only has one end node of
171 // length EFI_DEVICE_PATH_PROTOCOL.
173 Size1
= GetDevicePathSize (FirstDevicePath
);
174 Size2
= GetDevicePathSize (SecondDevicePath
);
175 Size
= Size1
+ Size2
- END_DEVICE_PATH_LENGTH
;
177 NewDevicePath
= AllocatePool (Size
);
179 if (NewDevicePath
!= NULL
) {
180 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
182 // Over write FirstDevicePath EndNode and do the copy
184 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
185 (Size1
- END_DEVICE_PATH_LENGTH
));
186 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
189 return NewDevicePath
;
193 Creates a new path by appending the device node to the device path.
195 This function creates a new device path by appending a copy of the device node specified by
196 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
197 The end-of-device-path device node is moved after the end of the appended device node.
198 If DevicePathNode is NULL then a copy of DevicePath is returned.
199 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device path device
201 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node
203 If there is not enough memory to allocate space for the new device path, then NULL is returned.
204 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
205 free the memory allocated.
207 @param DevicePath A pointer to a device path data structure.
208 @param DevicePathNode A pointer to a single device path node.
210 @retval NULL If there is not enough memory for the new device path.
211 @retval Others A pointer to the new device path if success.
212 A copy of DevicePathNode followed by an end-of-device-path node
213 if both FirstDevicePath and SecondDevicePath are NULL.
214 A copy of an end-of-device-path node if both FirstDevicePath and SecondDevicePath are NULL.
217 EFI_DEVICE_PATH_PROTOCOL
*
219 AppendDevicePathNode (
220 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
221 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
224 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
225 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
226 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
229 if (DevicePathNode
== NULL
) {
230 return DuplicateDevicePath ((DevicePath
!= NULL
) ? DevicePath
: &mUefiDevicePathLibEndDevicePath
);
233 // Build a Node that has a terminator on it
235 NodeLength
= DevicePathNodeLength (DevicePathNode
);
237 TempDevicePath
= AllocatePool (NodeLength
+ END_DEVICE_PATH_LENGTH
);
238 if (TempDevicePath
== NULL
) {
241 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
243 // Add and end device path node to convert Node to device path
245 NextNode
= NextDevicePathNode (TempDevicePath
);
246 SetDevicePathEndNode (NextNode
);
248 // Append device paths
250 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
252 FreePool (TempDevicePath
);
254 return NewDevicePath
;
258 Creates a new device path by appending the specified device path instance to the specified device
261 This function creates a new device path by appending a copy of the device path instance specified
262 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
263 The end-of-device-path device node is moved after the end of the appended device path instance
264 and a new end-of-device-path-instance node is inserted between.
265 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
266 If DevicePathInstance is NULL, then NULL is returned.
267 If there is not enough memory to allocate space for the new device path, then NULL is returned.
268 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
269 free the memory allocated.
271 @param DevicePath A pointer to a device path data structure.
272 @param DevicePathInstance A pointer to a device path instance.
274 @return A pointer to the new device path.
277 EFI_DEVICE_PATH_PROTOCOL
*
279 AppendDevicePathInstance (
280 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
281 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
284 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
285 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
289 if (DevicePath
== NULL
) {
290 return DuplicateDevicePath (DevicePathInstance
);
293 if (DevicePathInstance
== NULL
) {
297 SrcSize
= GetDevicePathSize (DevicePath
);
298 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
300 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
301 if (NewDevicePath
!= NULL
) {
303 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
305 while (!IsDevicePathEnd (TempDevicePath
)) {
306 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
309 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
310 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
311 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
314 return NewDevicePath
;
318 Creates a copy of the current device path instance and returns a pointer to the next device path
321 This function creates a copy of the current device path instance. It also updates DevicePath to
322 point to the next device path instance in the device path (or NULL if no more) and updates Size
323 to hold the size of the device path instance copy.
324 If DevicePath is NULL, then NULL is returned.
325 If there is not enough memory to allocate space for the new device path, then NULL is returned.
326 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
327 free the memory allocated.
328 If Size is NULL, then ASSERT().
330 @param DevicePath On input, this holds the pointer to the current device path
331 instance. On output, this holds the pointer to the next device
332 path instance or NULL if there are no more device path
333 instances in the device path pointer to a device path data
335 @param Size On output, this holds the size of the device path instance, in
336 bytes or zero, if DevicePath is NULL.
338 @return A pointer to the current device path instance.
341 EFI_DEVICE_PATH_PROTOCOL
*
343 GetNextDevicePathInstance (
344 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
348 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
349 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
352 ASSERT (Size
!= NULL
);
354 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
360 // Find the end of the device path instance
362 DevPath
= *DevicePath
;
363 while (!IsDevicePathEndType (DevPath
)) {
364 DevPath
= NextDevicePathNode (DevPath
);
368 // Compute the size of the device path instance
370 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
373 // Make a copy and return the device path instance
375 Temp
= DevPath
->SubType
;
376 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
377 ReturnValue
= DuplicateDevicePath (*DevicePath
);
378 DevPath
->SubType
= Temp
;
381 // If DevPath is the end of an entire device path, then another instance
382 // does not follow, so *DevicePath is set to NULL.
384 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
387 *DevicePath
= NextDevicePathNode (DevPath
);
394 Creates a device node.
396 This function creates a new device node in a newly allocated buffer of size NodeLength and
397 initializes the device path node header with NodeType and NodeSubType. The new device path node
399 If NodeLength is smaller than a device path header, then NULL is returned.
400 If there is not enough memory to allocate space for the new device path, then NULL is returned.
401 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
402 free the memory allocated.
404 @param NodeType The device node type for the new device node.
405 @param NodeSubType The device node sub-type for the new device node.
406 @param NodeLength The length of the new device node.
408 @return The new device path.
411 EFI_DEVICE_PATH_PROTOCOL
*
415 IN UINT8 NodeSubType
,
419 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
421 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
423 // NodeLength is less than the size of the header.
428 DevicePath
= AllocateZeroPool (NodeLength
);
429 if (DevicePath
!= NULL
) {
430 DevicePath
->Type
= NodeType
;
431 DevicePath
->SubType
= NodeSubType
;
432 SetDevicePathNodeLength (DevicePath
, NodeLength
);
439 Determines if a device path is single or multi-instance.
441 This function returns TRUE if the device path specified by DevicePath is multi-instance.
442 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
444 @param DevicePath A pointer to a device path data structure.
446 @retval TRUE DevicePath is multi-instance.
447 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
452 IsDevicePathMultiInstance (
453 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
456 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
458 if (DevicePath
== NULL
) {
463 while (!IsDevicePathEnd (Node
)) {
464 if (IsDevicePathEndInstance (Node
)) {
468 Node
= NextDevicePathNode (Node
);
476 Retrieves the device path protocol from a handle.
478 This function returns the device path protocol from the handle specified by Handle. If Handle is
479 NULL or Handle does not contain a device path protocol, then NULL is returned.
481 @param Handle The handle from which to retrieve the device path protocol.
483 @return The device path protocol from the handle specified by Handle.
486 EFI_DEVICE_PATH_PROTOCOL
*
488 DevicePathFromHandle (
492 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
495 Status
= gBS
->HandleProtocol (
497 &gEfiDevicePathProtocolGuid
,
500 if (EFI_ERROR (Status
)) {
507 Allocates a device path for a file and appends it to an existing device path.
509 If Device is a valid device handle that contains a device path protocol, then a device path for
510 the file specified by FileName is allocated and appended to the device path associated with the
511 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
512 that does not support the device path protocol, then a device path containing a single device
513 path node for the file specified by FileName is allocated and returned.
514 The memory for the new device path is allocated from EFI boot services memory. It is the responsibility
515 of the caller to free the memory allocated.
517 If FileName is NULL, then ASSERT().
518 If FileName is not aligned on a 16-bit boundary, then ASSERT().
520 @param Device A pointer to a device handle. This parameter is optional and
522 @param FileName A pointer to a Null-terminated Unicode string.
524 @return The allocated device path.
527 EFI_DEVICE_PATH_PROTOCOL
*
530 IN EFI_HANDLE Device
, OPTIONAL
531 IN CONST CHAR16
*FileName
535 FILEPATH_DEVICE_PATH
*FilePath
;
536 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
537 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
541 Size
= (UINT16
) StrSize (FileName
);
543 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ END_DEVICE_PATH_LENGTH
);
544 if (FileDevicePath
!= NULL
) {
545 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
546 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
547 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
548 CopyMem (&FilePath
->PathName
, FileName
, Size
);
549 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
550 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
552 if (Device
!= NULL
) {
553 DevicePath
= DevicePathFromHandle (Device
);
556 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
557 FreePool (FileDevicePath
);