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 DevicePath A pointer to a device path data structure.
160 @param DevicePathNode 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
*DevicePath
,
171 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
174 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
175 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
176 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
180 // Build a Node that has a terminator on it
182 NodeLength
= DevicePathNodeLength (DevicePathNode
);
184 TempDevicePath
= AllocatePool (NodeLength
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
));
185 if (TempDevicePath
== NULL
) {
188 TempDevicePath
= CopyMem (TempDevicePath
, DevicePathNode
, NodeLength
);
190 // Add and end device path node to convert Node to device path
192 NextNode
= NextDevicePathNode (TempDevicePath
);
193 SetDevicePathEndNode (NextNode
);
195 // Append device paths
197 NewDevicePath
= AppendDevicePath (DevicePath
, TempDevicePath
);
199 FreePool (TempDevicePath
);
201 return NewDevicePath
;
205 This function appends the device path instance Instance to the device path Source.
206 If Source is NULL, then a new device path with one instance is created.
208 @param Source A pointer to a device path data structure.
209 @param Instance A pointer to a device path instance.
211 @return A pointer to the new device path.
212 If there is not enough temporary pool memory available to complete this function,
213 then NULL is returned.
216 EFI_DEVICE_PATH_PROTOCOL
*
218 AppendDevicePathInstance (
219 IN CONST EFI_DEVICE_PATH_PROTOCOL
*Source
,
220 IN CONST EFI_DEVICE_PATH_PROTOCOL
*Instance
223 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
224 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
228 if (Source
== NULL
) {
229 return DuplicateDevicePath (Instance
);
232 SrcSize
= GetDevicePathSize (Source
);
233 InstanceSize
= GetDevicePathSize (Instance
);
235 NewDevicePath
= AllocatePool (SrcSize
+ InstanceSize
);
236 if (NewDevicePath
!= NULL
) {
238 DevicePath
= CopyMem (NewDevicePath
, Source
, SrcSize
);;
240 while (!IsDevicePathEnd (DevicePath
)) {
241 DevicePath
= NextDevicePathNode (DevicePath
);
244 DevicePath
->SubType
= END_INSTANCE_DEVICE_PATH_SUBTYPE
;
246 DevicePath
= NextDevicePathNode (DevicePath
);
247 CopyMem (DevicePath
, Instance
, InstanceSize
);
250 return NewDevicePath
;
254 Function retrieves the next device path instance from a device path data structure.
256 @param DevicePath A pointer to a device path data structure.
258 @param Size A pointer to the size of a device path instance in bytes.
260 @return This function returns a pointer to the current device path instance.
261 In addition, it returns the size in bytes of the current device path instance in Size,
262 and a pointer to the next device path instance in DevicePath.
263 If there are no more device path instances in DevicePath, then DevicePath will be set to NULL.
266 EFI_DEVICE_PATH_PROTOCOL
*
268 GetNextDevicePathInstance (
269 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
273 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
274 EFI_DEVICE_PATH_PROTOCOL
*ReturnValue
;
277 ASSERT (DevicePath
!= NULL
);
278 ASSERT (Size
!= NULL
);
279 if (*DevicePath
== NULL
) {
285 // Find the end of the device path instance
287 DevPath
= *DevicePath
;
288 while (!IsDevicePathEndType (DevPath
)) {
289 DevPath
= NextDevicePathNode (DevPath
);
293 // Compute the size of the device path instance
295 *Size
= ((UINTN
) DevPath
- (UINTN
) (*DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
298 // Make a copy and return the device path instance
300 Temp
= DevPath
->SubType
;
301 DevPath
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
302 ReturnValue
= DuplicateDevicePath (*DevicePath
);
303 DevPath
->SubType
= Temp
;
306 // If DevPath is the end of an entire device path, then another instance
307 // does not follow, so *DevicePath is set to NULL.
309 if (DevicePathSubType (DevPath
) == END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
312 *DevicePath
= NextDevicePathNode (DevPath
);
319 Return TRUE is this is a multi instance device path.
321 @param DevicePath A pointer to a device path data structure.
323 @retval TRUE If DevicePath is multi-instance.
324 @retval FALSE If DevicePath is not multi-instance or DevicePath is NULL.
329 IsDevicePathMultiInstance (
330 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
333 CONST EFI_DEVICE_PATH_PROTOCOL
*Node
;
335 if (DevicePath
== NULL
) {
340 while (!EfiIsDevicePathEnd (Node
)) {
341 if (EfiIsDevicePathEndInstance (Node
)) {
345 Node
= EfiNextDevicePathNode (Node
);
352 This function retrieves the device path protocol from a handle.
354 @param Handle The handle from which to retrieve the device path protocol.
356 @return This function returns the device path protocol from the handle specified by Handle.
357 If Handle is NULL or Handle does not contain a device path protocol, then NULL is returned.
360 EFI_DEVICE_PATH_PROTOCOL
*
362 DevicePathFromHandle (
366 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
369 Status
= gBS
->HandleProtocol (
371 &gEfiDevicePathProtocolGuid
,
374 if (EFI_ERROR (Status
)) {
381 This function allocates a device path for a file and appends it to an existing device path.
383 @param Device A pointer to a device handle. This parameter is optional and may be NULL.
384 @param FileName A pointer to a Null-terminated Unicode string.
386 @return If Device is a valid device handle that contains a device path protocol,
387 then a device path for the file specified by FileName is allocated
388 and appended to the device path associated with the handle Device. The allocated device path is returned.
389 If Device is NULL or Device is a handle that does not support the device path protocol,
390 then a device path containing a single device path node for the file specified by FileName
391 is allocated and returned.
394 EFI_DEVICE_PATH_PROTOCOL
*
397 IN EFI_HANDLE Device
, OPTIONAL
398 IN CONST CHAR16
*FileName
402 UINTN FilePathNodeSize
;
403 FILEPATH_DEVICE_PATH
*FilePathNode
;
404 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
408 FileNameSize
= StrSize (FileName
);
409 FilePathNodeSize
= FileNameSize
+ SIZE_OF_FILEPATH_DEVICE_PATH
;
410 FilePathNode
= AllocatePool (FilePathNodeSize
);
411 if (FilePathNode
!= NULL
) {
413 // Build a file path node
415 FilePathNode
->Header
.Type
= MEDIA_DEVICE_PATH
;
416 FilePathNode
->Header
.SubType
= MEDIA_FILEPATH_DP
;
417 SetDevicePathNodeLength (&FilePathNode
->Header
, FilePathNodeSize
);
418 CopyMem (FilePathNode
->PathName
, FileName
, FileNameSize
);
421 // Append file path node to device's device path
423 if (Device
!= NULL
) {
424 DevicePath
= DevicePathFromHandle (Device
);
426 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) FilePathNode
);
427 FreePool (FilePathNode
);