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 DevicePathNode is NULL then a copy of DevicePath is returned.
193 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device path device
195 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node
197 If there is not enough memory to allocate space for the new device path, then NULL is returned.
198 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
199 free the memory allocated.
201 @param DevicePath A pointer to a device path data structure.
202 @param DevicePathNode A pointer to a single device path node.
204 @return A pointer to the new device path.
207 EFI_DEVICE_PATH_PROTOCOL
*
209 AppendDevicePathNode (
210 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
211 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
214 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
215 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
216 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
219 if (DevicePathNode
== NULL
) {
220 return DuplicateDevicePath ((DevicePath
!= NULL
) ? DevicePath
: mEndDevicePath
);
223 // Build a Node that has a terminator on it
225 NodeLength
= DevicePathNodeLength (DevicePathNode
);
227 TempDevicePath
= AllocatePool (NodeLength
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
));
228 if (TempDevicePath
== NULL
) {
231 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
233 // Add and end device path node to convert Node to device path
235 NextNode
= NextDevicePathNode (TempDevicePath
);
236 SetDevicePathEndNode (NextNode
);
238 // Append device paths
240 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
242 FreePool (TempDevicePath
);
244 return NewDevicePath
;
248 Creates a new device path by appending the specified device path instance to the specified device
251 This function creates a new device path by appending a copy of the device path instance specified
252 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
253 The end-of-device-path device node is moved after the end of the appended device path instance
254 and a new end-of-device-path-instance node is inserted between.
255 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
256 If DevicePathInstance is NULL, then NULL is returned.
257 If there is not enough memory to allocate space for the new device path, then NULL is returned.
258 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
259 free the memory allocated.
261 @param DevicePath A pointer to a device path data structure.
262 @param DevicePathInstance A pointer to a device path instance.
264 @return A pointer to the new device path.
267 EFI_DEVICE_PATH_PROTOCOL
*
269 AppendDevicePathInstance (
270 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
271 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
274 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
275 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
279 if (DevicePath
== NULL
) {
280 return DuplicateDevicePath (DevicePathInstance
);
283 if (DevicePathInstance
== NULL
) {
287 SrcSize
= GetDevicePathSize (DevicePath
);
288 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
290 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
291 if (NewDevicePath
!= NULL
) {
293 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
295 while (!IsDevicePathEnd (TempDevicePath
)) {
296 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
299 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
300 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
301 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
304 return NewDevicePath
;
308 Creates a copy of the current device path instance and returns a pointer to the next device path
311 This function creates a copy of the current device path instance. It also updates DevicePath to
312 point to the next device path instance in the device path (or NULL if no more) and updates Size
313 to hold the size of the device path instance copy.
314 If DevicePath is NULL, then NULL is returned.
315 If there is not enough memory to allocate space for the new device path, then NULL is returned.
316 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
317 free the memory allocated.
318 If Size is NULL, then ASSERT().
320 @param DevicePath On input, this holds the pointer to the current device path
321 instance. On output, this holds the pointer to the next device
322 path instance or NULL if there are no more device path
323 instances in the device path pointer to a device path data
325 @param Size On output, this holds the size of the device path instance, in
326 bytes or zero, if DevicePath is NULL.
328 @return A pointer to the current device path instance.
331 EFI_DEVICE_PATH_PROTOCOL
*
333 GetNextDevicePathInstance (
334 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
338 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
339 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
342 ASSERT (Size
!= NULL
);
344 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
350 // Find the end of the device path instance
352 DevPath
= *DevicePath
;
353 while (!IsDevicePathEndType (DevPath
)) {
354 DevPath
= NextDevicePathNode (DevPath
);
358 // Compute the size of the device path instance
360 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
363 // Make a copy and return the device path instance
365 Temp
= DevPath
->SubType
;
366 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
367 ReturnValue
= DuplicateDevicePath (*DevicePath
);
368 DevPath
->SubType
= Temp
;
371 // If DevPath is the end of an entire device path, then another instance
372 // does not follow, so *DevicePath is set to NULL.
374 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
377 *DevicePath
= NextDevicePathNode (DevPath
);
384 Creates a copy of the current device path instance and returns a pointer to the next device path
387 This function creates a new device node in a newly allocated buffer of size NodeLength and
388 initializes the device path node header with NodeType and NodeSubType. The new device path node
390 If NodeLength is smaller than a device path header, then NULL is returned.
391 If there is not enough memory to allocate space for the new device path, then NULL is returned.
392 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
393 free the memory allocated.
395 @param NodeType The device node type for the new device node.
396 @param NodeSubType The device node sub-type for the new device node.
397 @param NodeLength The length of the new device node.
399 @return The new device path.
402 EFI_DEVICE_PATH_PROTOCOL
*
406 IN UINT8 NodeSubType
,
410 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
412 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
414 // NodeLength is less than the size of the header.
419 DevicePath
= AllocatePool (NodeLength
);
420 if (DevicePath
!= NULL
) {
421 DevicePath
->Type
= NodeType
;
422 DevicePath
->SubType
= NodeSubType
;
423 SetDevicePathNodeLength (DevicePath
, NodeLength
);
430 Determines if a device path is single or multi-instance.
432 This function returns TRUE if the device path specified by DevicePath is multi-instance.
433 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
435 @param DevicePath A pointer to a device path data structure.
437 @retval TRUE DevicePath is multi-instance.
438 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
443 IsDevicePathMultiInstance (
444 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
447 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
449 if (DevicePath
== NULL
) {
454 while (!EfiIsDevicePathEnd (Node
)) {
455 if (EfiIsDevicePathEndInstance (Node
)) {
459 Node
= EfiNextDevicePathNode (Node
);
467 Retrieves the device path protocol from a handle.
469 This function returns the device path protocol from the handle specified by Handle. If Handle is
470 NULL or Handle does not contain a device path protocol, then NULL is returned.
472 @param Handle The handle from which to retrieve the device path protocol.
474 @return The device path protocol from the handle specified by Handle.
477 EFI_DEVICE_PATH_PROTOCOL
*
479 DevicePathFromHandle (
483 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
486 Status
= gBS
->HandleProtocol (
488 &gEfiDevicePathProtocolGuid
,
491 if (EFI_ERROR (Status
)) {
498 Allocates a device path for a file and appends it to an existing device path.
500 If Device is a valid device handle that contains a device path protocol, then a device path for
501 the file specified by FileName is allocated and appended to the device path associated with the
502 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
503 that does not support the device path protocol, then a device path containing a single device
504 path node for the file specified by FileName is allocated and returned.
505 If FileName is NULL, then ASSERT().
507 @param Device A pointer to a device handle. This parameter is optional and
509 @param FileName A pointer to a Null-terminated Unicode string.
511 @return The allocated device path.
514 EFI_DEVICE_PATH_PROTOCOL
*
517 IN EFI_HANDLE Device
, OPTIONAL
518 IN CONST CHAR16
*FileName
522 FILEPATH_DEVICE_PATH
*FilePath
;
523 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
524 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
528 Size
= StrSize (FileName
);
529 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
530 if (FileDevicePath
!= NULL
) {
531 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
532 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
533 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
534 CopyMem (&FilePath
->PathName
, FileName
, Size
);
535 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
536 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
538 if (Device
!= NULL
) {
539 DevicePath
= DevicePathFromHandle (Device
);
542 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
543 FreePool (FileDevicePath
);