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 // The package level header files this module uses
29 // The protocols, PPI and GUID defintions for this module
31 #include <Protocol/DevicePath.h>
33 // The Library classes this module consumes
35 #include <Library/DevicePathLib.h>
36 #include <Library/BaseMemoryLib.h>
37 #include <Library/DebugLib.h>
38 #include <Library/MemoryAllocationLib.h>
39 #include <Library/UefiBootServicesTableLib.h>
40 #include <Library/BaseLib.h>
43 Returns the size of a device path in bytes.
45 This function returns the size, in bytes, of the device path data structure specified by
46 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
48 @param DevicePath A pointer to a device path data structure.
50 @return The size of a device path in bytes.
56 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
59 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
61 if (DevicePath
== NULL
) {
66 // Search for the end of the device path structure
69 while (!EfiIsDevicePathEnd (DevicePath
)) {
70 DevicePath
= EfiNextDevicePathNode (DevicePath
);
74 // Compute the size and add back in the size of the end device path structure
76 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
80 Creates a new device path by appending a second device path to a first device path.
82 This function allocates space for a new copy of the device path specified by DevicePath. If
83 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
84 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
85 is returned. Otherwise, NULL is returned.
87 @param DevicePath A pointer to a device path data structure.
89 @return A pointer to the duplicated device path.
92 EFI_DEVICE_PATH_PROTOCOL
*
95 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
98 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
104 Size
= GetDevicePathSize (DevicePath
);
110 // Allocate space for duplicate device path
112 NewDevicePath
= AllocateCopyPool (Size
, DevicePath
);
114 return NewDevicePath
;
118 Creates a new device path by appending a second device path to a first device path.
120 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
121 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
122 SecondDevicePath is retained. The newly created device path is returned.
123 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
124 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
125 If both FirstDevicePath and SecondDevicePath are NULL, then NULL is returned.
126 If there is not enough memory for the newly allocated buffer, then NULL is returned.
127 The memory for the new device path is allocated from EFI boot services memory. It is the
128 responsibility of the caller to free the memory allocated.
130 @param FirstDevicePath A pointer to a device path data structure.
131 @param SecondDevicePath A pointer to a device path data structure.
133 @return A pointer to the new device path.
136 EFI_DEVICE_PATH_PROTOCOL
*
139 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
140 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
146 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
147 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
150 // If there's only 1 path, just duplicate it.
152 if (FirstDevicePath
== NULL
) {
153 return DuplicateDevicePath (SecondDevicePath
);
156 if (SecondDevicePath
== NULL
) {
157 return DuplicateDevicePath (FirstDevicePath
);
161 // Allocate space for the combined device path. It only has one end node of
162 // length EFI_DEVICE_PATH_PROTOCOL.
164 Size1
= GetDevicePathSize (FirstDevicePath
);
165 Size2
= GetDevicePathSize (SecondDevicePath
);
166 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
168 NewDevicePath
= AllocatePool (Size
);
170 if (NewDevicePath
!= NULL
) {
171 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
173 // Over write FirstDevicePath EndNode and do the copy
175 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
176 (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
177 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
180 return NewDevicePath
;
184 Creates a new path by appending the device node to the device path.
186 This function creates a new device path by appending a copy of the device node specified by
187 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
188 The end-of-device-path device node is moved after the end of the appended device node.
189 If DevicePath is NULL, then NULL is returned.
190 If DevicePathNode is NULL, then NULL is returned.
191 If there is not enough memory to allocate space for the new device path, then NULL is returned.
192 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
193 free the memory allocated.
195 @param DevicePath A pointer to a device path data structure.
196 @param DevicePathNode A pointer to a single device path node.
198 @return A pointer to the new device path.
201 EFI_DEVICE_PATH_PROTOCOL
*
203 AppendDevicePathNode (
204 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
205 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
208 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
209 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
210 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
213 if (DevicePath
== NULL
|| DevicePathNode
== NULL
) {
217 // Build a Node that has a terminator on it
219 NodeLength
= DevicePathNodeLength (DevicePathNode
);
221 TempDevicePath
= AllocatePool (NodeLength
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
));
222 if (TempDevicePath
== NULL
) {
225 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
227 // Add and end device path node to convert Node to device path
229 NextNode
= NextDevicePathNode (TempDevicePath
);
230 SetDevicePathEndNode (NextNode
);
232 // Append device paths
234 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
236 FreePool (TempDevicePath
);
238 return NewDevicePath
;
242 Creates a new device path by appending the specified device path instance to the specified device
245 This function creates a new device path by appending a copy of the device path instance specified
246 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
247 The end-of-device-path device node is moved after the end of the appended device path instance
248 and a new end-of-device-path-instance node is inserted between.
249 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
250 If DevicePathInstance is NULL, then NULL is returned.
251 If there is not enough memory to allocate space for the new device path, then NULL is returned.
252 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
253 free the memory allocated.
255 @param DevicePath A pointer to a device path data structure.
256 @param DevicePathInstance A pointer to a device path instance.
258 @return A pointer to the new device path.
261 EFI_DEVICE_PATH_PROTOCOL
*
263 AppendDevicePathInstance (
264 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
265 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
268 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
269 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
273 if (DevicePath
== NULL
) {
274 return DuplicateDevicePath (DevicePathInstance
);
277 if (DevicePathInstance
== NULL
) {
281 SrcSize
= GetDevicePathSize (DevicePath
);
282 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
284 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
285 if (NewDevicePath
!= NULL
) {
287 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
289 while (!IsDevicePathEnd (TempDevicePath
)) {
290 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
293 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
294 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
295 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
298 return NewDevicePath
;
302 Creates a copy of the current device path instance and returns a pointer to the next device path
305 This function creates a copy of the current device path instance. It also updates DevicePath to
306 point to the next device path instance in the device path (or NULL if no more) and updates Size
307 to hold the size of the device path instance copy.
308 If DevicePath is NULL, then NULL is returned.
309 If there is not enough memory to allocate space for the new device path, then NULL is returned.
310 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
311 free the memory allocated.
312 If Size is NULL, then ASSERT().
314 @param DevicePath On input, this holds the pointer to the current device path
315 instance. On output, this holds the pointer to the next device
316 path instance or NULL if there are no more device path
317 instances in the device path pointer to a device path data
319 @param Size On output, this holds the size of the device path instance, in
320 bytes or zero, if DevicePath is NULL.
322 @return A pointer to the current device path instance.
325 EFI_DEVICE_PATH_PROTOCOL
*
327 GetNextDevicePathInstance (
328 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
332 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
333 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
336 ASSERT (Size
!= NULL
);
338 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
344 // Find the end of the device path instance
346 DevPath
= *DevicePath
;
347 while (!IsDevicePathEndType (DevPath
)) {
348 DevPath
= NextDevicePathNode (DevPath
);
352 // Compute the size of the device path instance
354 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
357 // Make a copy and return the device path instance
359 Temp
= DevPath
->SubType
;
360 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
361 ReturnValue
= DuplicateDevicePath (*DevicePath
);
362 DevPath
->SubType
= Temp
;
365 // If DevPath is the end of an entire device path, then another instance
366 // does not follow, so *DevicePath is set to NULL.
368 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
371 *DevicePath
= NextDevicePathNode (DevPath
);
378 Creates a copy of the current device path instance and returns a pointer to the next device path
381 This function creates a new device node in a newly allocated buffer of size NodeLength and
382 initializes the device path node header with NodeType and NodeSubType. The new device path node
384 If NodeLength is smaller than a device path header, then NULL is returned.
385 If there is not enough memory to allocate space for the new device path, then NULL is returned.
386 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
387 free the memory allocated.
389 @param NodeType The device node type for the new device node.
390 @param NodeSubType The device node sub-type for the new device node.
391 @param NodeLength The length of the new device node.
393 @return The new device path.
396 EFI_DEVICE_PATH_PROTOCOL
*
400 IN UINT8 NodeSubType
,
404 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
406 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
408 // NodeLength is less than the size of the header.
413 DevicePath
= AllocatePool (NodeLength
);
414 if (DevicePath
!= NULL
) {
415 DevicePath
->Type
= NodeType
;
416 DevicePath
->SubType
= NodeSubType
;
417 SetDevicePathNodeLength (DevicePath
, NodeLength
);
424 Determines if a device path is single or multi-instance.
426 This function returns TRUE if the device path specified by DevicePath is multi-instance.
427 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
429 @param DevicePath A pointer to a device path data structure.
431 @retval TRUE DevicePath is multi-instance.
432 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
437 IsDevicePathMultiInstance (
438 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
441 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
443 if (DevicePath
== NULL
) {
448 while (!EfiIsDevicePathEnd (Node
)) {
449 if (EfiIsDevicePathEndInstance (Node
)) {
453 Node
= EfiNextDevicePathNode (Node
);
461 Retrieves the device path protocol from a handle.
463 This function returns the device path protocol from the handle specified by Handle. If Handle is
464 NULL or Handle does not contain a device path protocol, then NULL is returned.
466 @param Handle The handle from which to retrieve the device path protocol.
468 @return The device path protocol from the handle specified by Handle.
471 EFI_DEVICE_PATH_PROTOCOL
*
473 DevicePathFromHandle (
477 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
480 Status
= gBS
->HandleProtocol (
482 &gEfiDevicePathProtocolGuid
,
485 if (EFI_ERROR (Status
)) {
492 Allocates a device path for a file and appends it to an existing device path.
494 If Device is a valid device handle that contains a device path protocol, then a device path for
495 the file specified by FileName is allocated and appended to the device path associated with the
496 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
497 that does not support the device path protocol, then a device path containing a single device
498 path node for the file specified by FileName is allocated and returned.
499 If FileName is NULL, then ASSERT().
501 @param Device A pointer to a device handle. This parameter is optional and
503 @param FileName A pointer to a Null-terminated Unicode string.
505 @return The allocated device path.
508 EFI_DEVICE_PATH_PROTOCOL
*
511 IN EFI_HANDLE Device
, OPTIONAL
512 IN CONST CHAR16
*FileName
516 FILEPATH_DEVICE_PATH
*FilePath
;
517 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
518 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
522 Size
= StrSize (FileName
);
523 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
524 if (FileDevicePath
!= NULL
) {
525 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
526 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
527 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
528 CopyMem (&FilePath
->PathName
, FileName
, Size
);
529 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
530 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
532 if (Device
!= NULL
) {
533 DevicePath
= DevicePathFromHandle (Device
);
536 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
537 FreePool (FileDevicePath
);