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, 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 Returns the size of a device path in bytes.
37 This function returns the size, in bytes, of the device path data structure specified by
38 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
40 @param DevicePath A pointer to a device path data structure.
42 @return The size of a device path in bytes.
48 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
51 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
53 if (DevicePath
== NULL
) {
58 // Search for the end of the device path structure
61 while (!EfiIsDevicePathEnd (DevicePath
)) {
62 DevicePath
= EfiNextDevicePathNode (DevicePath
);
66 // Compute the size and add back in the size of the end device path structure
68 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
72 Creates a new device path by appending a second device path to a first device path.
74 This function allocates space for a new copy of the device path specified by DevicePath. If
75 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
76 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
77 is returned. Otherwise, NULL is returned.
79 @param DevicePath A pointer to a device path data structure.
81 @return A pointer to the duplicated device path.
84 EFI_DEVICE_PATH_PROTOCOL
*
87 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
90 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
96 Size
= GetDevicePathSize (DevicePath
);
102 // Allocate space for duplicate device path
104 NewDevicePath
= AllocateCopyPool (Size
, DevicePath
);
106 return NewDevicePath
;
110 Creates a new device path by appending a second device path to a first device path.
112 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
113 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
114 SecondDevicePath is retained. The newly created device path is returned.
115 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
116 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
117 If both FirstDevicePath and SecondDevicePath are NULL, then NULL is returned.
118 If there is not enough memory for the newly allocated buffer, then NULL is returned.
119 The memory for the new device path is allocated from EFI boot services memory. It is the
120 responsibility of the caller to free the memory allocated.
122 @param FirstDevicePath A pointer to a device path data structure.
123 @param SecondDevicePath A pointer to a device path data structure.
125 @return A pointer to the new device path.
128 EFI_DEVICE_PATH_PROTOCOL
*
131 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
132 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
138 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
139 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
142 // If there's only 1 path, just duplicate it.
144 if (FirstDevicePath
== NULL
) {
145 return DuplicateDevicePath (SecondDevicePath
);
148 if (SecondDevicePath
== NULL
) {
149 return DuplicateDevicePath (FirstDevicePath
);
153 // Allocate space for the combined device path. It only has one end node of
154 // length EFI_DEVICE_PATH_PROTOCOL.
156 Size1
= GetDevicePathSize (FirstDevicePath
);
157 Size2
= GetDevicePathSize (SecondDevicePath
);
158 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
160 NewDevicePath
= AllocatePool (Size
);
162 if (NewDevicePath
!= NULL
) {
163 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
165 // Over write FirstDevicePath EndNode and do the copy
167 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
168 (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
169 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
172 return NewDevicePath
;
176 Creates a new path by appending the device node to the device path.
178 This function creates a new device path by appending a copy of the device node specified by
179 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
180 The end-of-device-path device node is moved after the end of the appended device node.
181 If DevicePath is NULL, then NULL is returned.
182 If DevicePathNode is NULL, then NULL is returned.
183 If there is not enough memory to allocate space for the new device path, then NULL is returned.
184 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
185 free the memory allocated.
187 @param DevicePath A pointer to a device path data structure.
188 @param DevicePathNode A pointer to a single device path node.
190 @return A pointer to the new device path.
193 EFI_DEVICE_PATH_PROTOCOL
*
195 AppendDevicePathNode (
196 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
197 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
200 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
201 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
202 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
205 if (DevicePath
== NULL
|| DevicePathNode
== NULL
) {
209 // Build a Node that has a terminator on it
211 NodeLength
= DevicePathNodeLength (DevicePathNode
);
213 TempDevicePath
= AllocatePool (NodeLength
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
));
214 if (TempDevicePath
== NULL
) {
217 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
219 // Add and end device path node to convert Node to device path
221 NextNode
= NextDevicePathNode (TempDevicePath
);
222 SetDevicePathEndNode (NextNode
);
224 // Append device paths
226 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
228 FreePool (TempDevicePath
);
230 return NewDevicePath
;
234 Creates a new device path by appending the specified device path instance to the specified device
237 This function creates a new device path by appending a copy of the device path instance specified
238 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
239 The end-of-device-path device node is moved after the end of the appended device path instance
240 and a new end-of-device-path-instance node is inserted between.
241 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
242 If DevicePathInstance is NULL, then NULL is returned.
243 If there is not enough memory to allocate space for the new device path, then NULL is returned.
244 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
245 free the memory allocated.
247 @param DevicePath A pointer to a device path data structure.
248 @param DevicePathInstance A pointer to a device path instance.
250 @return A pointer to the new device path.
253 EFI_DEVICE_PATH_PROTOCOL
*
255 AppendDevicePathInstance (
256 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
257 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
260 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
261 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
265 if (DevicePath
== NULL
) {
266 return DuplicateDevicePath (DevicePathInstance
);
269 if (DevicePathInstance
== NULL
) {
273 SrcSize
= GetDevicePathSize (DevicePath
);
274 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
276 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
277 if (NewDevicePath
!= NULL
) {
279 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
281 while (!IsDevicePathEnd (TempDevicePath
)) {
282 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
285 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
286 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
287 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
290 return NewDevicePath
;
294 Creates a copy of the current device path instance and returns a pointer to the next device path
297 This function creates a copy of the current device path instance. It also updates DevicePath to
298 point to the next device path instance in the device path (or NULL if no more) and updates Size
299 to hold the size of the device path instance copy.
300 If DevicePath is NULL, then NULL is returned.
301 If there is not enough memory to allocate space for the new device path, then NULL is returned.
302 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
303 free the memory allocated.
304 If Size is NULL, then ASSERT().
306 @param DevicePath On input, this holds the pointer to the current device path
307 instance. On output, this holds the pointer to the next device
308 path instance or NULL if there are no more device path
309 instances in the device path pointer to a device path data
311 @param Size On output, this holds the size of the device path instance, in
312 bytes or zero, if DevicePath is NULL.
314 @return A pointer to the current device path instance.
317 EFI_DEVICE_PATH_PROTOCOL
*
319 GetNextDevicePathInstance (
320 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
324 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
325 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
328 ASSERT (Size
!= NULL
);
330 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
336 // Find the end of the device path instance
338 DevPath
= *DevicePath
;
339 while (!IsDevicePathEndType (DevPath
)) {
340 DevPath
= NextDevicePathNode (DevPath
);
344 // Compute the size of the device path instance
346 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
349 // Make a copy and return the device path instance
351 Temp
= DevPath
->SubType
;
352 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
353 ReturnValue
= DuplicateDevicePath (*DevicePath
);
354 DevPath
->SubType
= Temp
;
357 // If DevPath is the end of an entire device path, then another instance
358 // does not follow, so *DevicePath is set to NULL.
360 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
363 *DevicePath
= NextDevicePathNode (DevPath
);
370 Creates a copy of the current device path instance and returns a pointer to the next device path
373 This function creates a new device node in a newly allocated buffer of size NodeLength and
374 initializes the device path node header with NodeType and NodeSubType. The new device path node
376 If NodeLength is smaller than a device path header, then NULL is returned.
377 If there is not enough memory to allocate space for the new device path, then NULL is returned.
378 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
379 free the memory allocated.
381 @param NodeType The device node type for the new device node.
382 @param NodeSubType The device node sub-type for the new device node.
383 @param NodeLength The length of the new device node.
385 @return The new device path.
388 EFI_DEVICE_PATH_PROTOCOL
*
392 IN UINT8 NodeSubType
,
396 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
398 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
400 // NodeLength is less than the size of the header.
405 DevicePath
= AllocatePool (NodeLength
);
406 if (DevicePath
!= NULL
) {
407 DevicePath
->Type
= NodeType
;
408 DevicePath
->SubType
= NodeSubType
;
409 SetDevicePathNodeLength (DevicePath
, NodeLength
);
416 Determines if a device path is single or multi-instance.
418 This function returns TRUE if the device path specified by DevicePath is multi-instance.
419 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
421 @param DevicePath A pointer to a device path data structure.
423 @retval TRUE DevicePath is multi-instance.
424 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
429 IsDevicePathMultiInstance (
430 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
433 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
435 if (DevicePath
== NULL
) {
440 while (!EfiIsDevicePathEnd (Node
)) {
441 if (EfiIsDevicePathEndInstance (Node
)) {
445 Node
= EfiNextDevicePathNode (Node
);
453 Retrieves the device path protocol from a handle.
455 This function returns the device path protocol from the handle specified by Handle. If Handle is
456 NULL or Handle does not contain a device path protocol, then NULL is returned.
458 @param Handle The handle from which to retrieve the device path protocol.
460 @return The device path protocol from the handle specified by Handle.
463 EFI_DEVICE_PATH_PROTOCOL
*
465 DevicePathFromHandle (
469 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
472 Status
= gBS
->HandleProtocol (
474 &gEfiDevicePathProtocolGuid
,
477 if (EFI_ERROR (Status
)) {
484 Allocates a device path for a file and appends it to an existing device path.
486 If Device is a valid device handle that contains a device path protocol, then a device path for
487 the file specified by FileName is allocated and appended to the device path associated with the
488 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
489 that does not support the device path protocol, then a device path containing a single device
490 path node for the file specified by FileName is allocated and returned.
491 If FileName is NULL, then ASSERT().
493 @param Device A pointer to a device handle. This parameter is optional and
495 @param FileName A pointer to a Null-terminated Unicode string.
497 @return The allocated device path.
500 EFI_DEVICE_PATH_PROTOCOL
*
503 IN EFI_HANDLE Device
, OPTIONAL
504 IN CONST CHAR16
*FileName
508 FILEPATH_DEVICE_PATH
*FilePath
;
509 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
510 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
514 Size
= StrSize (FileName
);
515 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
516 if (FileDevicePath
!= NULL
) {
517 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
518 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
519 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
520 CopyMem (&FilePath
->PathName
, FileName
, Size
);
521 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
522 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
524 if (Device
!= NULL
) {
525 DevicePath
= DevicePathFromHandle (Device
);
528 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
529 FreePool (FileDevicePath
);