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
,
41 END_DEVICE_PATH_LENGTH
,
47 Returns the size of a device path in bytes.
49 This function returns the size, in bytes, of the device path data structure specified by
50 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
52 @param DevicePath A pointer to a device path data structure.
54 @return The size of a device path in bytes.
60 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
63 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
65 if (DevicePath
== NULL
) {
70 // Search for the end of the device path structure
73 while (!EfiIsDevicePathEnd (DevicePath
)) {
74 DevicePath
= EfiNextDevicePathNode (DevicePath
);
78 // Compute the size and add back in the size of the end device path structure
80 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
84 Creates a new device path by appending a second device path to a first device path.
86 This function allocates space for a new copy of the device path specified by DevicePath. If
87 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
88 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
89 is returned. Otherwise, NULL is returned.
91 @param DevicePath A pointer to a device path data structure.
93 @return A pointer to the duplicated device path.
96 EFI_DEVICE_PATH_PROTOCOL
*
99 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
102 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
108 Size
= GetDevicePathSize (DevicePath
);
114 // Allocate space for duplicate device path
116 NewDevicePath
= AllocateCopyPool (Size
, DevicePath
);
118 return NewDevicePath
;
122 Creates a new device path by appending a second device path to a first device path.
124 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
125 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
126 SecondDevicePath is retained. The newly created device path is returned.
127 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
128 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
129 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is
131 If there is not enough memory for the newly allocated buffer, then NULL is returned.
132 The memory for the new device path is allocated from EFI boot services memory. It is the
133 responsibility of the caller to free the memory allocated.
135 @param FirstDevicePath A pointer to a device path data structure.
136 @param SecondDevicePath A pointer to a device path data structure.
138 @return A pointer to the new device path.
141 EFI_DEVICE_PATH_PROTOCOL
*
144 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
145 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
151 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
152 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
155 // If there's only 1 path, just duplicate it.
157 if (FirstDevicePath
== NULL
) {
158 return DuplicateDevicePath ((SecondDevicePath
!= NULL
) ? SecondDevicePath
: &mEndDevicePath
);
161 if (SecondDevicePath
== NULL
) {
162 return DuplicateDevicePath (FirstDevicePath
);
166 // Allocate space for the combined device path. It only has one end node of
167 // length EFI_DEVICE_PATH_PROTOCOL.
169 Size1
= GetDevicePathSize (FirstDevicePath
);
170 Size2
= GetDevicePathSize (SecondDevicePath
);
171 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
173 NewDevicePath
= AllocatePool (Size
);
175 if (NewDevicePath
!= NULL
) {
176 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
178 // Over write FirstDevicePath EndNode and do the copy
180 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+
181 (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
182 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
185 return NewDevicePath
;
189 Creates a new path by appending the device node to the device path.
191 This function creates a new device path by appending a copy of the device node specified by
192 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
193 The end-of-device-path device node is moved after the end of the appended device node.
194 If DevicePathNode is NULL then a copy of DevicePath is returned.
195 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device path device
197 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node
199 If there is not enough memory to allocate space for the new device path, then NULL is returned.
200 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
201 free the memory allocated.
203 @param DevicePath A pointer to a device path data structure.
204 @param DevicePathNode A pointer to a single device path node.
206 @return A pointer to the new device path.
209 EFI_DEVICE_PATH_PROTOCOL
*
211 AppendDevicePathNode (
212 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
213 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
216 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
217 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
218 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
221 if (DevicePathNode
== NULL
) {
222 return DuplicateDevicePath ((DevicePath
!= NULL
) ? DevicePath
: &mEndDevicePath
);
225 // Build a Node that has a terminator on it
227 NodeLength
= DevicePathNodeLength (DevicePathNode
);
229 TempDevicePath
= AllocatePool (NodeLength
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
));
230 if (TempDevicePath
== NULL
) {
233 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
235 // Add and end device path node to convert Node to device path
237 NextNode
= NextDevicePathNode (TempDevicePath
);
238 SetDevicePathEndNode (NextNode
);
240 // Append device paths
242 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
244 FreePool (TempDevicePath
);
246 return NewDevicePath
;
250 Creates a new device path by appending the specified device path instance to the specified device
253 This function creates a new device path by appending a copy of the device path instance specified
254 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
255 The end-of-device-path device node is moved after the end of the appended device path instance
256 and a new end-of-device-path-instance node is inserted between.
257 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
258 If DevicePathInstance is NULL, then NULL is returned.
259 If there is not enough memory to allocate space for the new device path, then NULL is returned.
260 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
261 free the memory allocated.
263 @param DevicePath A pointer to a device path data structure.
264 @param DevicePathInstance A pointer to a device path instance.
266 @return A pointer to the new device path.
269 EFI_DEVICE_PATH_PROTOCOL
*
271 AppendDevicePathInstance (
272 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
273 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
276 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
277 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
281 if (DevicePath
== NULL
) {
282 return DuplicateDevicePath (DevicePathInstance
);
285 if (DevicePathInstance
== NULL
) {
289 SrcSize
= GetDevicePathSize (DevicePath
);
290 InstanceSize
= GetDevicePathSize (DevicePathInstance
);
292 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
293 if (NewDevicePath
!= NULL
) {
295 TempDevicePath
= CopyMem (NewDevicePath
, DevicePath
, SrcSize
);;
297 while (!IsDevicePathEnd (TempDevicePath
)) {
298 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
301 TempDevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
302 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
303 CopyMem (TempDevicePath
, DevicePathInstance
, InstanceSize
);
306 return NewDevicePath
;
310 Creates a copy of the current device path instance and returns a pointer to the next device path
313 This function creates a copy of the current device path instance. It also updates DevicePath to
314 point to the next device path instance in the device path (or NULL if no more) and updates Size
315 to hold the size of the device path instance copy.
316 If DevicePath is NULL, then NULL is returned.
317 If there is not enough memory to allocate space for the new device path, then NULL is returned.
318 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
319 free the memory allocated.
320 If Size is NULL, then ASSERT().
322 @param DevicePath On input, this holds the pointer to the current device path
323 instance. On output, this holds the pointer to the next device
324 path instance or NULL if there are no more device path
325 instances in the device path pointer to a device path data
327 @param Size On output, this holds the size of the device path instance, in
328 bytes or zero, if DevicePath is NULL.
330 @return A pointer to the current device path instance.
333 EFI_DEVICE_PATH_PROTOCOL
*
335 GetNextDevicePathInstance (
336 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
340 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
341 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
344 ASSERT (Size
!= NULL
);
346 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
352 // Find the end of the device path instance
354 DevPath
= *DevicePath
;
355 while (!IsDevicePathEndType (DevPath
)) {
356 DevPath
= NextDevicePathNode (DevPath
);
360 // Compute the size of the device path instance
362 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
365 // Make a copy and return the device path instance
367 Temp
= DevPath
->SubType
;
368 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
369 ReturnValue
= DuplicateDevicePath (*DevicePath
);
370 DevPath
->SubType
= Temp
;
373 // If DevPath is the end of an entire device path, then another instance
374 // does not follow, so *DevicePath is set to NULL.
376 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
379 *DevicePath
= NextDevicePathNode (DevPath
);
386 Creates a copy of the current device path instance and returns a pointer to the next device path
389 This function creates a new device node in a newly allocated buffer of size NodeLength and
390 initializes the device path node header with NodeType and NodeSubType. The new device path node
392 If NodeLength is smaller than a device path header, then NULL is returned.
393 If there is not enough memory to allocate space for the new device path, then NULL is returned.
394 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
395 free the memory allocated.
397 @param NodeType The device node type for the new device node.
398 @param NodeSubType The device node sub-type for the new device node.
399 @param NodeLength The length of the new device node.
401 @return The new device path.
404 EFI_DEVICE_PATH_PROTOCOL
*
408 IN UINT8 NodeSubType
,
412 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
414 if (NodeLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
416 // NodeLength is less than the size of the header.
421 DevicePath
= AllocatePool (NodeLength
);
422 if (DevicePath
!= NULL
) {
423 DevicePath
->Type
= NodeType
;
424 DevicePath
->SubType
= NodeSubType
;
425 SetDevicePathNodeLength (DevicePath
, NodeLength
);
432 Determines if a device path is single or multi-instance.
434 This function returns TRUE if the device path specified by DevicePath is multi-instance.
435 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
437 @param DevicePath A pointer to a device path data structure.
439 @retval TRUE DevicePath is multi-instance.
440 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
445 IsDevicePathMultiInstance (
446 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
449 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
451 if (DevicePath
== NULL
) {
456 while (!EfiIsDevicePathEnd (Node
)) {
457 if (EfiIsDevicePathEndInstance (Node
)) {
461 Node
= EfiNextDevicePathNode (Node
);
469 Retrieves the device path protocol from a handle.
471 This function returns the device path protocol from the handle specified by Handle. If Handle is
472 NULL or Handle does not contain a device path protocol, then NULL is returned.
474 @param Handle The handle from which to retrieve the device path protocol.
476 @return The device path protocol from the handle specified by Handle.
479 EFI_DEVICE_PATH_PROTOCOL
*
481 DevicePathFromHandle (
485 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
488 Status
= gBS
->HandleProtocol (
490 &gEfiDevicePathProtocolGuid
,
493 if (EFI_ERROR (Status
)) {
500 Allocates a device path for a file and appends it to an existing device path.
502 If Device is a valid device handle that contains a device path protocol, then a device path for
503 the file specified by FileName is allocated and appended to the device path associated with the
504 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
505 that does not support the device path protocol, then a device path containing a single device
506 path node for the file specified by FileName is allocated and returned.
507 If FileName is NULL, then ASSERT().
509 @param Device A pointer to a device handle. This parameter is optional and
511 @param FileName A pointer to a Null-terminated Unicode string.
513 @return The allocated device path.
516 EFI_DEVICE_PATH_PROTOCOL
*
519 IN EFI_HANDLE Device
, OPTIONAL
520 IN CONST CHAR16
*FileName
524 FILEPATH_DEVICE_PATH
*FilePath
;
525 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
526 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
530 Size
= StrSize (FileName
);
531 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
532 if (FileDevicePath
!= NULL
) {
533 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
534 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
535 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
536 CopyMem (&FilePath
->PathName
, FileName
, Size
);
537 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
538 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
540 if (Device
!= NULL
) {
541 DevicePath
= DevicePathFromHandle (Device
);
544 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
545 FreePool (FileDevicePath
);