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 // Template for an end-of-device path node.
37 STATIC EFI_DEVICE_PATH_PROTOCOL mEndDevicePath
[] = {
39 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
40 END_DEVICE_PATH_LENGTH
,
45 Returns the size of a device path in bytes.
47 This function returns the size, in bytes, of the device path data structure specified by
48 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
50 @param DevicePath A pointer to a device path data structure.
52 @return The size of a device path in bytes.
58 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
61 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
63 if (DevicePath
== NULL
) {
68 // Search for the end of the device path structure
71 while (!EfiIsDevicePathEnd (DevicePath
)) {
72 DevicePath
= EfiNextDevicePathNode (DevicePath
);
76 // Compute the size and add back in the size of the end device path structure
78 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
82 Creates a new device path by appending a second device path to a first device path.
84 This function allocates space for a new copy of the device path specified by DevicePath. If
85 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
86 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
87 is returned. Otherwise, NULL is returned.
89 @param DevicePath A pointer to a device path data structure.
91 @return A pointer to the duplicated device path.
94 EFI_DEVICE_PATH_PROTOCOL
*
97 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
100 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
106 Size
= GetDevicePathSize (DevicePath
);
112 // Allocate space for duplicate device path
114 NewDevicePath
= AllocateCopyPool (Size
, DevicePath
);
116 return NewDevicePath
;
120 Creates a new device path by appending a second device path to a first device path.
122 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
123 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
124 SecondDevicePath is retained. The newly created device path is returned.
125 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
126 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
127 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is
129 If there is not enough memory for the newly allocated buffer, then NULL is returned.
130 The memory for the new device path is allocated from EFI boot services memory. It is the
131 responsibility of the caller to free the memory allocated.
133 @param FirstDevicePath A pointer to a device path data structure.
134 @param SecondDevicePath A pointer to a device path data structure.
136 @return A pointer to the new device path.
139 EFI_DEVICE_PATH_PROTOCOL
*
142 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
143 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
149 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
150 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
153 // If there's only 1 path, just duplicate it.
155 if (FirstDevicePath
== NULL
) {
156 return DuplicateDevicePath ((SecondDevicePath
!= NULL
) ? SecondDevicePath
: mEndDevicePath
);
159 if (SecondDevicePath
== NULL
) {
160 return DuplicateDevicePath (FirstDevicePath
);
164 // Allocate space for the combined device path. It only has one end node of
165 // length EFI_DEVICE_PATH_PROTOCOL.
167 Size1
= GetDevicePathSize (FirstDevicePath
);
168 Size2
= GetDevicePathSize (SecondDevicePath
);
169 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
171 NewDevicePath
= AllocatePool (Size
);
173 if (NewDevicePath
!= NULL
) {
174 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
176 // Over write FirstDevicePath EndNode and do the copy
178 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
179 (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
180 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
183 return NewDevicePath
;
187 Creates a new path by appending the device node to the device path.
189 This function creates a new device path by appending a copy of the device node specified by
190 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
191 The end-of-device-path device node is moved after the end of the appended device node.
192 If DeviceNode is NULL then a copy of DevicePath is returned.
193 If DevicePathNode is NULL then a copy of DevicePath is returned.
194 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node
196 If there is not enough memory to allocate space for the new device path, then NULL is returned.
197 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
198 free the memory allocated.
200 @param DevicePath A pointer to a device path data structure.
201 @param DevicePathNode A pointer to a single device path node.
203 @return A pointer to the new device path.
206 EFI_DEVICE_PATH_PROTOCOL
*
208 AppendDevicePathNode (
209 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
210 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
213 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
214 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
215 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
218 if (DevicePathNode
== NULL
) {
219 return DuplicateDevicePath ((DevicePath
!= NULL
) ? DevicePath
: mEndDevicePath
);
222 // Build a Node that has a terminator on it
224 NodeLength
= DevicePathNodeLength (DevicePathNode
);
226 TempDevicePath
= AllocatePool (NodeLength
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
));
227 if (TempDevicePath
== NULL
) {
230 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
232 // Add and end device path node to convert Node to device path
234 NextNode
= NextDevicePathNode (TempDevicePath
);
235 SetDevicePathEndNode (NextNode
);
237 // Append device paths
239 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
241 FreePool (TempDevicePath
);
243 return NewDevicePath
;
247 Creates a new device path by appending the specified device path instance to the specified device
250 This function creates a new device path by appending a copy of the device path instance specified
251 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
252 The end-of-device-path device node is moved after the end of the appended device path instance
253 and a new end-of-device-path-instance node is inserted between.
254 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
255 If DevicePathInstance is NULL, then NULL is returned.
256 If there is not enough memory to allocate space for the new device path, then NULL is returned.
257 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
258 free the memory allocated.
260 @param DevicePath A pointer to a device path data structure.
261 @param DevicePathInstance A pointer to a device path instance.
263 @return A pointer to the new device path.
266 EFI_DEVICE_PATH_PROTOCOL
*
268 AppendDevicePathInstance (
269 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
270 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
273 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
274 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
278 if (DevicePath
== NULL
) {
279 return DuplicateDevicePath (DevicePathInstance
);
282 if (DevicePathInstance
== NULL
) {
286 SrcSize
= GetDevicePathSize (DevicePath
);
287 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
289 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
290 if (NewDevicePath
!= NULL
) {
292 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
294 while (!IsDevicePathEnd (TempDevicePath
)) {
295 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
298 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
299 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
300 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
303 return NewDevicePath
;
307 Creates a copy of the current device path instance and returns a pointer to the next device path
310 This function creates a copy of the current device path instance. It also updates DevicePath to
311 point to the next device path instance in the device path (or NULL if no more) and updates Size
312 to hold the size of the device path instance copy.
313 If DevicePath is NULL, then NULL is returned.
314 If there is not enough memory to allocate space for the new device path, then NULL is returned.
315 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
316 free the memory allocated.
317 If Size is NULL, then ASSERT().
319 @param DevicePath On input, this holds the pointer to the current device path
320 instance. On output, this holds the pointer to the next device
321 path instance or NULL if there are no more device path
322 instances in the device path pointer to a device path data
324 @param Size On output, this holds the size of the device path instance, in
325 bytes or zero, if DevicePath is NULL.
327 @return A pointer to the current device path instance.
330 EFI_DEVICE_PATH_PROTOCOL
*
332 GetNextDevicePathInstance (
333 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
337 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
338 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
341 ASSERT (Size
!= NULL
);
343 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
349 // Find the end of the device path instance
351 DevPath
= *DevicePath
;
352 while (!IsDevicePathEndType (DevPath
)) {
353 DevPath
= NextDevicePathNode (DevPath
);
357 // Compute the size of the device path instance
359 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
362 // Make a copy and return the device path instance
364 Temp
= DevPath
->SubType
;
365 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
366 ReturnValue
= DuplicateDevicePath (*DevicePath
);
367 DevPath
->SubType
= Temp
;
370 // If DevPath is the end of an entire device path, then another instance
371 // does not follow, so *DevicePath is set to NULL.
373 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
376 *DevicePath
= NextDevicePathNode (DevPath
);
383 Creates a copy of the current device path instance and returns a pointer to the next device path
386 This function creates a new device node in a newly allocated buffer of size NodeLength and
387 initializes the device path node header with NodeType and NodeSubType. The new device path node
389 If NodeLength is smaller than a device path header, then NULL is returned.
390 If there is not enough memory to allocate space for the new device path, then NULL is returned.
391 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
392 free the memory allocated.
394 @param NodeType The device node type for the new device node.
395 @param NodeSubType The device node sub-type for the new device node.
396 @param NodeLength The length of the new device node.
398 @return The new device path.
401 EFI_DEVICE_PATH_PROTOCOL
*
405 IN UINT8 NodeSubType
,
409 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
411 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
413 // NodeLength is less than the size of the header.
418 DevicePath
= AllocatePool (NodeLength
);
419 if (DevicePath
!= NULL
) {
420 DevicePath
->Type
= NodeType
;
421 DevicePath
->SubType
= NodeSubType
;
422 SetDevicePathNodeLength (DevicePath
, NodeLength
);
429 Determines if a device path is single or multi-instance.
431 This function returns TRUE if the device path specified by DevicePath is multi-instance.
432 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
434 @param DevicePath A pointer to a device path data structure.
436 @retval TRUE DevicePath is multi-instance.
437 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
442 IsDevicePathMultiInstance (
443 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
446 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
448 if (DevicePath
== NULL
) {
453 while (!EfiIsDevicePathEnd (Node
)) {
454 if (EfiIsDevicePathEndInstance (Node
)) {
458 Node
= EfiNextDevicePathNode (Node
);
466 Retrieves the device path protocol from a handle.
468 This function returns the device path protocol from the handle specified by Handle. If Handle is
469 NULL or Handle does not contain a device path protocol, then NULL is returned.
471 @param Handle The handle from which to retrieve the device path protocol.
473 @return The device path protocol from the handle specified by Handle.
476 EFI_DEVICE_PATH_PROTOCOL
*
478 DevicePathFromHandle (
482 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
485 Status
= gBS
->HandleProtocol (
487 &gEfiDevicePathProtocolGuid
,
490 if (EFI_ERROR (Status
)) {
497 Allocates a device path for a file and appends it to an existing device path.
499 If Device is a valid device handle that contains a device path protocol, then a device path for
500 the file specified by FileName is allocated and appended to the device path associated with the
501 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
502 that does not support the device path protocol, then a device path containing a single device
503 path node for the file specified by FileName is allocated and returned.
504 If FileName is NULL, then ASSERT().
506 @param Device A pointer to a device handle. This parameter is optional and
508 @param FileName A pointer to a Null-terminated Unicode string.
510 @return The allocated device path.
513 EFI_DEVICE_PATH_PROTOCOL
*
516 IN EFI_HANDLE Device
, OPTIONAL
517 IN CONST CHAR16
*FileName
521 FILEPATH_DEVICE_PATH
*FilePath
;
522 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
523 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
527 Size
= StrSize (FileName
);
528 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
529 if (FileDevicePath
!= NULL
) {
530 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
531 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
532 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
533 CopyMem (&FilePath
->PathName
, FileName
, Size
);
534 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
535 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
537 if (Device
!= NULL
) {
538 DevicePath
= DevicePathFromHandle (Device
);
541 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
542 FreePool (FileDevicePath
);