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 Returns the size of a device path in bytes.
27 This function returns the size, in bytes, of the device path data structure specified by
28 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
30 @param DevicePath A pointer to a device path data structure.
32 @return The size of a device path in bytes.
38 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
41 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
43 if (DevicePath
== NULL
) {
48 // Search for the end of the device path structure
51 while (!EfiIsDevicePathEnd (DevicePath
)) {
52 DevicePath
= EfiNextDevicePathNode (DevicePath
);
56 // Compute the size and add back in the size of the end device path structure
58 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
62 Creates a new device path by appending a second device path to a first device path.
64 This function allocates space for a new copy of the device path specified by DevicePath. If
65 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
66 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
67 is returned. Otherwise, NULL is returned.
69 @param DevicePath A pointer to a device path data structure.
71 @return A pointer to the duplicated device path.
74 EFI_DEVICE_PATH_PROTOCOL
*
77 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
80 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
86 Size
= GetDevicePathSize (DevicePath
);
92 // Allocate space for duplicate device path
94 NewDevicePath
= AllocateCopyPool (Size
, DevicePath
);
100 Creates a new device path by appending a second device path to a first device path.
102 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
103 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
104 SecondDevicePath is retained. The newly created device path is returned.
105 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
106 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
107 If both FirstDevicePath and SecondDevicePath are NULL, then NULL is returned.
108 If there is not enough memory for the newly allocated buffer, then NULL is returned.
109 The memory for the new device path is allocated from EFI boot services memory. It is the
110 responsibility of the caller to free the memory allocated.
112 @param FirstDevicePath A pointer to a device path data structure.
113 @param SecondDevicePath A pointer to a device path data structure.
115 @return A pointer to the new device path.
118 EFI_DEVICE_PATH_PROTOCOL
*
121 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
122 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
128 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
129 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
132 // If there's only 1 path, just duplicate it.
134 if (FirstDevicePath
== NULL
) {
135 return DuplicateDevicePath (SecondDevicePath
);
138 if (SecondDevicePath
== NULL
) {
139 return DuplicateDevicePath (FirstDevicePath
);
143 // Allocate space for the combined device path. It only has one end node of
144 // length EFI_DEVICE_PATH_PROTOCOL.
146 Size1
= GetDevicePathSize (FirstDevicePath
);
147 Size2
= GetDevicePathSize (SecondDevicePath
);
148 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
150 NewDevicePath
= AllocatePool (Size
);
152 if (NewDevicePath
!= NULL
) {
153 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
155 // Over write FirstDevicePath EndNode and do the copy
157 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
158 (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
159 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
162 return NewDevicePath
;
166 Creates a new path by appending the device node to the device path.
168 This function creates a new device path by appending a copy of the device node specified by
169 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
170 The end-of-device-path device node is moved after the end of the appended device node.
171 If DevicePath is NULL, then NULL is returned.
172 If DevicePathNode is NULL, then NULL is returned.
173 If there is not enough memory to allocate space for the new device path, then NULL is returned.
174 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
175 free the memory allocated.
177 @param DevicePath A pointer to a device path data structure.
178 @param DevicePathNode A pointer to a single device path node.
180 @return A pointer to the new device path.
183 EFI_DEVICE_PATH_PROTOCOL
*
185 AppendDevicePathNode (
186 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
187 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
190 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
191 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
192 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
195 if (DevicePath
== NULL
|| DevicePathNode
== NULL
) {
199 // Build a Node that has a terminator on it
201 NodeLength
= DevicePathNodeLength (DevicePathNode
);
203 TempDevicePath
= AllocatePool (NodeLength
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
));
204 if (TempDevicePath
== NULL
) {
207 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
209 // Add and end device path node to convert Node to device path
211 NextNode
= NextDevicePathNode (TempDevicePath
);
212 SetDevicePathEndNode (NextNode
);
214 // Append device paths
216 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
218 FreePool (TempDevicePath
);
220 return NewDevicePath
;
224 Creates a new device path by appending the specified device path instance to the specified device
227 This function creates a new device path by appending a copy of the device path instance specified
228 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
229 The end-of-device-path device node is moved after the end of the appended device path instance
230 and a new end-of-device-path-instance node is inserted between.
231 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
232 If DevicePathInstance is NULL, then NULL is returned.
233 If there is not enough memory to allocate space for the new device path, then NULL is returned.
234 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
235 free the memory allocated.
237 @param DevicePath A pointer to a device path data structure.
238 @param DevicePathInstance A pointer to a device path instance.
240 @return A pointer to the new device path.
243 EFI_DEVICE_PATH_PROTOCOL
*
245 AppendDevicePathInstance (
246 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
247 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
250 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
251 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
255 if (DevicePath
== NULL
) {
256 return DuplicateDevicePath (DevicePathInstance
);
259 if (DevicePathInstance
== NULL
) {
263 SrcSize
= GetDevicePathSize (DevicePath
);
264 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
266 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
267 if (NewDevicePath
!= NULL
) {
269 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
271 while (!IsDevicePathEnd (TempDevicePath
)) {
272 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
275 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
276 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
277 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
280 return NewDevicePath
;
284 Creates a copy of the current device path instance and returns a pointer to the next device path
287 This function creates a copy of the current device path instance. It also updates DevicePath to
288 point to the next device path instance in the device path (or NULL if no more) and updates Size
289 to hold the size of the device path instance copy.
290 If DevicePath is NULL, then NULL is returned.
291 If there is not enough memory to allocate space for the new device path, then NULL is returned.
292 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
293 free the memory allocated.
294 If Size is NULL, then ASSERT().
296 @param DevicePath On input, this holds the pointer to the current device path
297 instance. On output, this holds the pointer to the next device
298 path instance or NULL if there are no more device path
299 instances in the device path pointer to a device path data
301 @param Size On output, this holds the size of the device path instance, in
302 bytes or zero, if DevicePath is NULL.
304 @return A pointer to the current device path instance.
307 EFI_DEVICE_PATH_PROTOCOL
*
309 GetNextDevicePathInstance (
310 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
314 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
315 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
318 ASSERT (Size
!= NULL
);
320 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
326 // Find the end of the device path instance
328 DevPath
= *DevicePath
;
329 while (!IsDevicePathEndType (DevPath
)) {
330 DevPath
= NextDevicePathNode (DevPath
);
334 // Compute the size of the device path instance
336 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
339 // Make a copy and return the device path instance
341 Temp
= DevPath
->SubType
;
342 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
343 ReturnValue
= DuplicateDevicePath (*DevicePath
);
344 DevPath
->SubType
= Temp
;
347 // If DevPath is the end of an entire device path, then another instance
348 // does not follow, so *DevicePath is set to NULL.
350 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
353 *DevicePath
= NextDevicePathNode (DevPath
);
360 Creates a copy of the current device path instance and returns a pointer to the next device path
363 This function creates a new device node in a newly allocated buffer of size NodeLength and
364 initializes the device path node header with NodeType and NodeSubType. The new device path node
366 If NodeLength is smaller than a device path header, then NULL is returned.
367 If there is not enough memory to allocate space for the new device path, then NULL is returned.
368 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
369 free the memory allocated.
371 @param NodeType The device node type for the new device node.
372 @param NodeSubType The device node sub-type for the new device node.
373 @param NodeLength The length of the new device node.
375 @return The new device path.
378 EFI_DEVICE_PATH_PROTOCOL
*
382 IN UINT8 NodeSubType
,
386 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
388 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
390 // NodeLength is less than the size of the header.
395 DevicePath
= AllocatePool (NodeLength
);
396 if (DevicePath
!= NULL
) {
397 DevicePath
->Type
= NodeType
;
398 DevicePath
->SubType
= NodeSubType
;
405 Determines if a device path is single or multi-instance.
407 This function returns TRUE if the device path specified by DevicePath is multi-instance.
408 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
410 @param DevicePath A pointer to a device path data structure.
412 @retval TRUE DevicePath is multi-instance.
413 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
418 IsDevicePathMultiInstance (
419 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
422 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
424 if (DevicePath
== NULL
) {
429 while (!EfiIsDevicePathEnd (Node
)) {
430 if (EfiIsDevicePathEndInstance (Node
)) {
434 Node
= EfiNextDevicePathNode (Node
);
442 Retrieves the device path protocol from a handle.
444 This function returns the device path protocol from the handle specified by Handle. If Handle is
445 NULL or Handle does not contain a device path protocol, then NULL is returned.
447 @param Handle The handle from which to retrieve the device path protocol.
449 @return The device path protocol from the handle specified by Handle.
452 EFI_DEVICE_PATH_PROTOCOL
*
454 DevicePathFromHandle (
458 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
461 Status
= gBS
->HandleProtocol (
463 &gEfiDevicePathProtocolGuid
,
466 if (EFI_ERROR (Status
)) {
473 Allocates a device path for a file and appends it to an existing device path.
475 If Device is a valid device handle that contains a device path protocol, then a device path for
476 the file specified by FileName is allocated and appended to the device path associated with the
477 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
478 that does not support the device path protocol, then a device path containing a single device
479 path node for the file specified by FileName is allocated and returned.
480 If FileName is NULL, then ASSERT().
482 @param Device A pointer to a device handle. This parameter is optional and
484 @param FileName A pointer to a Null-terminated Unicode string.
486 @return The allocated device path.
489 EFI_DEVICE_PATH_PROTOCOL
*
492 IN EFI_HANDLE Device
, OPTIONAL
493 IN CONST CHAR16
*FileName
497 FILEPATH_DEVICE_PATH
*FilePath
;
498 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
499 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
503 Size
= StrSize (FileName
);
504 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
505 if (FileDevicePath
!= NULL
) {
506 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
507 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
508 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
509 CopyMem (&FilePath
->PathName
, FileName
, Size
);
510 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
511 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
513 if (Device
!= NULL
) {
514 DevicePath
= DevicePathFromHandle (Device
);
517 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
518 FreePool (FileDevicePath
);