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.
20 Module Name: UefiDevicePathLib.c
25 // Include common header file for this module.
27 #include "CommonHeader.h"
30 Returns the size of a device path in bytes.
32 This function returns the size, in bytes, of the device path data structure specified by
33 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
35 @param DevicePath A pointer to a device path data structure.
37 @return The size of a device path in bytes.
43 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
46 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
48 if (DevicePath
== NULL
) {
53 // Search for the end of the device path structure
56 while (!EfiIsDevicePathEnd (DevicePath
)) {
57 DevicePath
= EfiNextDevicePathNode (DevicePath
);
61 // Compute the size and add back in the size of the end device path structure
63 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
67 Creates a new device path by appending a second device path to a first device path.
69 This function allocates space for a new copy of the device path specified by DevicePath. If
70 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
71 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
72 is returned. Otherwise, NULL is returned.
74 @param DevicePath A pointer to a device path data structure.
76 @return A pointer to the duplicated device path.
79 EFI_DEVICE_PATH_PROTOCOL
*
82 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
85 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
91 Size
= GetDevicePathSize (DevicePath
);
97 // Allocate space for duplicate device path
99 NewDevicePath
= AllocateCopyPool (Size
, DevicePath
);
101 return NewDevicePath
;
105 Creates a new device path by appending a second device path to a first device path.
107 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
108 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
109 SecondDevicePath is retained. The newly created device path is returned.
110 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
111 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
112 If both FirstDevicePath and SecondDevicePath are NULL, then NULL is returned.
113 If there is not enough memory for the newly allocated buffer, then NULL is returned.
114 The memory for the new device path is allocated from EFI boot services memory. It is the
115 responsibility of the caller to free the memory allocated.
117 @param FirstDevicePath A pointer to a device path data structure.
118 @param SecondDevicePath A pointer to a device path data structure.
120 @return A pointer to the new device path.
123 EFI_DEVICE_PATH_PROTOCOL
*
126 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
127 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
133 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
134 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
137 // If there's only 1 path, just duplicate it.
139 if (FirstDevicePath
== NULL
) {
140 return DuplicateDevicePath (SecondDevicePath
);
143 if (SecondDevicePath
== NULL
) {
144 return DuplicateDevicePath (FirstDevicePath
);
148 // Allocate space for the combined device path. It only has one end node of
149 // length EFI_DEVICE_PATH_PROTOCOL.
151 Size1
= GetDevicePathSize (FirstDevicePath
);
152 Size2
= GetDevicePathSize (SecondDevicePath
);
153 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
155 NewDevicePath
= AllocatePool (Size
);
157 if (NewDevicePath
!= NULL
) {
158 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
160 // Over write FirstDevicePath EndNode and do the copy
162 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
163 (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
164 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
167 return NewDevicePath
;
171 Creates a new path by appending the device node to the device path.
173 This function creates a new device path by appending a copy of the device node specified by
174 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
175 The end-of-device-path device node is moved after the end of the appended device node.
176 If DevicePath is NULL, then NULL is returned.
177 If DevicePathNode is NULL, then NULL is returned.
178 If there is not enough memory to allocate space for the new device path, then NULL is returned.
179 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
180 free the memory allocated.
182 @param DevicePath A pointer to a device path data structure.
183 @param DevicePathNode A pointer to a single device path node.
185 @return A pointer to the new device path.
188 EFI_DEVICE_PATH_PROTOCOL
*
190 AppendDevicePathNode (
191 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
192 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
195 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
196 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
197 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
200 if (DevicePath
== NULL
|| DevicePathNode
== NULL
) {
204 // Build a Node that has a terminator on it
206 NodeLength
= DevicePathNodeLength (DevicePathNode
);
208 TempDevicePath
= AllocatePool (NodeLength
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
));
209 if (TempDevicePath
== NULL
) {
212 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
214 // Add and end device path node to convert Node to device path
216 NextNode
= NextDevicePathNode (TempDevicePath
);
217 SetDevicePathEndNode (NextNode
);
219 // Append device paths
221 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
223 FreePool (TempDevicePath
);
225 return NewDevicePath
;
229 Creates a new device path by appending the specified device path instance to the specified device
232 This function creates a new device path by appending a copy of the device path instance specified
233 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
234 The end-of-device-path device node is moved after the end of the appended device path instance
235 and a new end-of-device-path-instance node is inserted between.
236 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
237 If DevicePathInstance is NULL, then NULL is returned.
238 If there is not enough memory to allocate space for the new device path, then NULL is returned.
239 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
240 free the memory allocated.
242 @param DevicePath A pointer to a device path data structure.
243 @param DevicePathInstance A pointer to a device path instance.
245 @return A pointer to the new device path.
248 EFI_DEVICE_PATH_PROTOCOL
*
250 AppendDevicePathInstance (
251 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
252 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
255 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
256 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
260 if (DevicePath
== NULL
) {
261 return DuplicateDevicePath (DevicePathInstance
);
264 if (DevicePathInstance
== NULL
) {
268 SrcSize
= GetDevicePathSize (DevicePath
);
269 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
271 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
272 if (NewDevicePath
!= NULL
) {
274 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
276 while (!IsDevicePathEnd (TempDevicePath
)) {
277 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
280 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
281 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
282 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
285 return NewDevicePath
;
289 Creates a copy of the current device path instance and returns a pointer to the next device path
292 This function creates a copy of the current device path instance. It also updates DevicePath to
293 point to the next device path instance in the device path (or NULL if no more) and updates Size
294 to hold the size of the device path instance copy.
295 If DevicePath is NULL, then NULL is returned.
296 If there is not enough memory to allocate space for the new device path, then NULL is returned.
297 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
298 free the memory allocated.
299 If Size is NULL, then ASSERT().
301 @param DevicePath On input, this holds the pointer to the current device path
302 instance. On output, this holds the pointer to the next device
303 path instance or NULL if there are no more device path
304 instances in the device path pointer to a device path data
306 @param Size On output, this holds the size of the device path instance, in
307 bytes or zero, if DevicePath is NULL.
309 @return A pointer to the current device path instance.
312 EFI_DEVICE_PATH_PROTOCOL
*
314 GetNextDevicePathInstance (
315 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
319 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
320 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
323 ASSERT (Size
!= NULL
);
325 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
331 // Find the end of the device path instance
333 DevPath
= *DevicePath
;
334 while (!IsDevicePathEndType (DevPath
)) {
335 DevPath
= NextDevicePathNode (DevPath
);
339 // Compute the size of the device path instance
341 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
344 // Make a copy and return the device path instance
346 Temp
= DevPath
->SubType
;
347 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
348 ReturnValue
= DuplicateDevicePath (*DevicePath
);
349 DevPath
->SubType
= Temp
;
352 // If DevPath is the end of an entire device path, then another instance
353 // does not follow, so *DevicePath is set to NULL.
355 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
358 *DevicePath
= NextDevicePathNode (DevPath
);
365 Creates a copy of the current device path instance and returns a pointer to the next device path
368 This function creates a new device node in a newly allocated buffer of size NodeLength and
369 initializes the device path node header with NodeType and NodeSubType. The new device path node
371 If NodeLength is smaller than a device path header, then NULL is returned.
372 If there is not enough memory to allocate space for the new device path, then NULL is returned.
373 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
374 free the memory allocated.
376 @param NodeType The device node type for the new device node.
377 @param NodeSubType The device node sub-type for the new device node.
378 @param NodeLength The length of the new device node.
380 @return The new device path.
383 EFI_DEVICE_PATH_PROTOCOL
*
387 IN UINT8 NodeSubType
,
391 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
393 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
395 // NodeLength is less than the size of the header.
400 DevicePath
= AllocatePool (NodeLength
);
401 if (DevicePath
!= NULL
) {
402 DevicePath
->Type
= NodeType
;
403 DevicePath
->SubType
= NodeSubType
;
404 SetDevicePathNodeLength (DevicePath
, NodeLength
);
411 Determines if a device path is single or multi-instance.
413 This function returns TRUE if the device path specified by DevicePath is multi-instance.
414 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
416 @param DevicePath A pointer to a device path data structure.
418 @retval TRUE DevicePath is multi-instance.
419 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
424 IsDevicePathMultiInstance (
425 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
428 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
430 if (DevicePath
== NULL
) {
435 while (!EfiIsDevicePathEnd (Node
)) {
436 if (EfiIsDevicePathEndInstance (Node
)) {
440 Node
= EfiNextDevicePathNode (Node
);
448 Retrieves the device path protocol from a handle.
450 This function returns the device path protocol from the handle specified by Handle. If Handle is
451 NULL or Handle does not contain a device path protocol, then NULL is returned.
453 @param Handle The handle from which to retrieve the device path protocol.
455 @return The device path protocol from the handle specified by Handle.
458 EFI_DEVICE_PATH_PROTOCOL
*
460 DevicePathFromHandle (
464 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
467 Status
= gBS
->HandleProtocol (
469 &gEfiDevicePathProtocolGuid
,
472 if (EFI_ERROR (Status
)) {
479 Allocates a device path for a file and appends it to an existing device path.
481 If Device is a valid device handle that contains a device path protocol, then a device path for
482 the file specified by FileName is allocated and appended to the device path associated with the
483 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
484 that does not support the device path protocol, then a device path containing a single device
485 path node for the file specified by FileName is allocated and returned.
486 If FileName is NULL, then ASSERT().
488 @param Device A pointer to a device handle. This parameter is optional and
490 @param FileName A pointer to a Null-terminated Unicode string.
492 @return The allocated device path.
495 EFI_DEVICE_PATH_PROTOCOL
*
498 IN EFI_HANDLE Device
, OPTIONAL
499 IN CONST CHAR16
*FileName
503 FILEPATH_DEVICE_PATH
*FilePath
;
504 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
505 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
509 Size
= StrSize (FileName
);
510 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
511 if (FileDevicePath
!= NULL
) {
512 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
513 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
514 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
515 CopyMem (&FilePath
->PathName
, FileName
, Size
);
516 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
517 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
519 if (Device
!= NULL
) {
520 DevicePath
= DevicePathFromHandle (Device
);
523 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
524 FreePool (FileDevicePath
);