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 This function returns the size, in bytes,
26 of the device path data structure specified by DevicePath.
27 If DevicePath is NULL, then 0 is returned.
29 @param DevicePath A pointer to a device path data structure.
31 @return The size of a device path in bytes.
37 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
40 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
42 if (DevicePath
== NULL
) {
47 // Search for the end of the device path structure
50 while (!EfiIsDevicePathEnd (DevicePath
)) {
51 DevicePath
= EfiNextDevicePathNode (DevicePath
);
55 // Compute the size and add back in the size of the end device path structure
57 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
61 This function allocates space for a new copy of the device path
62 specified by DevicePath.
64 @param DevicePath A pointer to a device path data structure.
66 @return The duplicated device path.
69 EFI_DEVICE_PATH_PROTOCOL
*
72 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
75 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
81 Size
= GetDevicePathSize (DevicePath
);
87 // Allocate space for duplicate device path
89 NewDevicePath
= AllocateCopyPool (Size
, DevicePath
);
95 This function appends the device path SecondDevicePath
96 to every device path instance in FirstDevicePath.
98 @param FirstDevicePath A pointer to a device path data structure.
100 @param SecondDevicePath A pointer to a device path data structure.
102 @return A pointer to the new device path is returned.
103 NULL is returned if space for the new device path could not be allocated from pool.
104 It is up to the caller to free the memory used by FirstDevicePath and SecondDevicePath
105 if they are no longer needed.
108 EFI_DEVICE_PATH_PROTOCOL
*
111 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
,
112 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath
118 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
119 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
122 // If there's only 1 path, just duplicate it
124 if (FirstDevicePath
== NULL
) {
125 return DuplicateDevicePath (SecondDevicePath
);
128 if (SecondDevicePath
== NULL
) {
129 return DuplicateDevicePath (FirstDevicePath
);
133 // Allocate space for the combined device path. It only has one end node of
134 // length EFI_DEVICE_PATH_PROTOCOL
136 Size1
= GetDevicePathSize (FirstDevicePath
);
137 Size2
= GetDevicePathSize (SecondDevicePath
);
138 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
140 NewDevicePath
= AllocatePool (Size
);
142 if (NewDevicePath
!= NULL
) {
143 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
145 // Over write Src1 EndNode and do the copy
147 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+ (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
148 CopyMem (DevicePath2
, SecondDevicePath
, Size2
);
151 return NewDevicePath
;
155 This function appends the device path node SecondDevicePath
156 to every device path instance in FirstDevicePath.
158 @param FirstDevicePath A pointer to a device path data structure.
160 @param SecondDevicePath A pointer to a single device path node.
162 @return A pointer to the new device path.
163 If there is not enough temporary pool memory available to complete this function,
164 then NULL is returned.
167 EFI_DEVICE_PATH_PROTOCOL
*
169 AppendDevicePathNode (
170 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
,
171 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath
174 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
175 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
180 // Build a Node that has a terminator on it
182 NodeLength
= DevicePathNodeLength (SecondDevicePath
);
183 Size1
= GetDevicePathSize (FirstDevicePath
);
185 NewDevicePath
= AllocatePool (NodeLength
+ Size1
);
186 if (NewDevicePath
!= NULL
) {
188 // Copy the first device path to the new device path
190 NewDevicePath
= CopyMem (NewDevicePath
, FirstDevicePath
, Size1
);
193 // Copy the device path node to the new device path
195 NextNode
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+ (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
196 NextNode
= CopyMem (NextNode
, SecondDevicePath
, NodeLength
);
199 // Terminate the whole device path
201 NextNode
= NextDevicePathNode (NextNode
);
202 SetDevicePathEndNode (NextNode
);
204 return NewDevicePath
;
208 This function appends the device path instance Instance to the device path Source.
209 If Source is NULL, then a new device path with one instance is created.
211 @param Source A pointer to a device path data structure.
212 @param Instance A pointer to a device path instance.
214 @return A pointer to the new device path.
215 If there is not enough temporary pool memory available to complete this function,
216 then NULL is returned.
219 EFI_DEVICE_PATH_PROTOCOL
*
221 AppendDevicePathInstance (
222 IN CONST EFI_DEVICE_PATH_PROTOCOL
*Source
,
223 IN CONST EFI_DEVICE_PATH_PROTOCOL
*Instance
226 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
227 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
231 if (Source
== NULL
) {
232 return DuplicateDevicePath (Instance
);
235 SrcSize
= GetDevicePathSize (Source
);
236 InstanceSize
= GetDevicePathSize (Instance
);
238 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
239 if (NewDevicePath
!= NULL
) {
241 DevicePath
= CopyMem (NewDevicePath
, Source
, SrcSize
);;
243 while (!IsDevicePathEnd (DevicePath
)) {
244 DevicePath
= NextDevicePathNode (DevicePath
);
247 DevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
249 DevicePath
= NextDevicePathNode (DevicePath
);
250 CopyMem (DevicePath
, Instance
, InstanceSize
);
253 return NewDevicePath
;
257 Function retrieves the next device path instance from a device path data structure.
259 @param DevicePath A pointer to a device path data structure.
261 @param Size A pointer to the size of a device path instance in bytes.
263 @return This function returns a pointer to the current device path instance.
264 In addition, it returns the size in bytes of the current device path instance in Size,
265 and a pointer to the next device path instance in DevicePath.
266 If there are no more device path instances in DevicePath, then DevicePath will be set to NULL.
269 EFI_DEVICE_PATH_PROTOCOL
*
271 GetNextDevicePathInstance (
272 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
276 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
277 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
280 ASSERT (DevicePath
!= NULL
);
281 ASSERT (Size
!= NULL
);
282 if (*DevicePath
== NULL
) {
288 // Find the end of the device path instance
290 DevPath
= *DevicePath
;
291 while (!IsDevicePathEndType (DevPath
)) {
292 DevPath
= NextDevicePathNode (DevPath
);
296 // Compute the size of the device path instance
298 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
301 // Make a copy and return the device path instance
303 Temp
= DevPath
->SubType
;
304 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
305 ReturnValue
= DuplicateDevicePath (*DevicePath
);
306 DevPath
->SubType
= Temp
;
309 // If DevPath is the end of an entire device path, then another instance
310 // does not follow, so *DevicePath is set to NULL.
312 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
315 *DevicePath
= NextDevicePathNode (DevPath
);
322 Return TRUE is this is a multi instance device path.
324 @param DevicePath A pointer to a device path data structure.
326 @retval TRUE If DevicePath is multi-instance.
327 @retval FALSE If DevicePath is not multi-instance or DevicePath is NULL.
332 IsDevicePathMultiInstance (
333 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
336 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
338 if (DevicePath
== NULL
) {
343 while (!EfiIsDevicePathEnd (Node
)) {
344 if (EfiIsDevicePathEndInstance (Node
)) {
348 Node
= EfiNextDevicePathNode (Node
);
355 This function retrieves the device path protocol from a handle.
357 @param Handle The handle from which to retrieve the device path protocol.
359 @return This function returns the device path protocol from the handle specified by Handle.
360 If Handle is NULL or Handle does not contain a device path protocol, then NULL is returned.
363 EFI_DEVICE_PATH_PROTOCOL
*
365 DevicePathFromHandle (
369 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
372 Status
= gBS
->HandleProtocol (
374 &gEfiDevicePathProtocolGuid
,
377 if (EFI_ERROR (Status
)) {
384 This function allocates a device path for a file and appends it to an existing device path.
386 @param Device A pointer to a device handle. This parameter is optional and may be NULL.
387 @param FileName A pointer to a Null-terminated Unicode string.
389 @return If Device is a valid device handle that contains a device path protocol,
390 then a device path for the file specified by FileName is allocated
391 and appended to the device path associated with the handle Device. The allocated device path is returned.
392 If Device is NULL or Device is a handle that does not support the device path protocol,
393 then a device path containing a single device path node for the file specified by FileName
394 is allocated and returned.
397 EFI_DEVICE_PATH_PROTOCOL
*
400 IN EFI_HANDLE Device
, OPTIONAL
401 IN CONST CHAR16
*FileName
405 UINTN FilePathNodeSize
;
406 FILEPATH_DEVICE_PATH
*FilePathNode
;
407 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
411 FileNameSize
= StrSize (FileName
);
412 FilePathNodeSize
= FileNameSize
+ SIZE_OF_FILEPATH_DEVICE_PATH
;
413 FilePathNode
= AllocatePool (FilePathNodeSize
);
414 if (FilePathNode
!= NULL
) {
416 // Build a file path node
418 FilePathNode
->Header
.Type
= MEDIA_DEVICE_PATH
;
419 FilePathNode
->Header
.SubType
= MEDIA_FILEPATH_DP
;
420 SetDevicePathNodeLength (&FilePathNode
->Header
, FilePathNodeSize
);
421 CopyMem (FilePathNode
->PathName
, FileName
, FileNameSize
);
424 // Append file path node to device's device path
426 if (Device
!= NULL
) {
427 DevicePath
= DevicePathFromHandle (Device
);
429 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) FilePathNode
);
430 FreePool (FilePathNode
);