2 UEFI Device Path Library.
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Protocol/DevicePathUtilities.h>
19 #include <Protocol/DevicePath.h>
21 #include <Library/DevicePathLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/BaseLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/UefiBootServicesTableLib.h>
28 #include "UefiDevicePathLibInternal.h"
30 STATIC EFI_DEVICE_PATH_UTILITIES_PROTOCOL
*mDevicePathUtilities
= NULL
;
33 The constructor function caches the pointer to DevicePathUtilites protocol.
35 The constructor function locates DevicePathUtilities protocol from protocol database.
36 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
38 @param ImageHandle The firmware allocated handle for the EFI image.
39 @param SystemTable A pointer to the EFI System Table.
41 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
46 DevicePathLibConstructor (
47 IN EFI_HANDLE ImageHandle
,
48 IN EFI_SYSTEM_TABLE
*SystemTable
53 Status
= gBS
->LocateProtocol (
54 &gEfiDevicePathUtilitiesProtocolGuid
,
56 (VOID
**) &mDevicePathUtilities
58 ASSERT_EFI_ERROR (Status
);
59 ASSERT (mDevicePathUtilities
!= NULL
);
65 Returns the size of a device path in bytes.
67 This function returns the size, in bytes, of the device path data structure specified by
68 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
70 @param DevicePath A pointer to a device path data structure.
72 @return The size of a device path in bytes.
78 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
81 return mDevicePathUtilities
->GetDevicePathSize (DevicePath
);
85 Creates a new device path by appending a second device path to a first device path.
87 This function allocates space for a new copy of the device path specified by DevicePath. If
88 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
89 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
90 is returned. Otherwise, NULL is returned.
92 @param DevicePath A pointer to a device path data structure.
94 @return A pointer to the duplicated device path.
97 EFI_DEVICE_PATH_PROTOCOL
*
100 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
103 return mDevicePathUtilities
->DuplicateDevicePath (DevicePath
);
107 Creates a new device path by appending a second device path to a first device path.
109 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
110 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
111 SecondDevicePath is retained. The newly created device path is returned.
112 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
113 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
114 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is
116 If there is not enough memory for the newly allocated buffer, then NULL is returned.
117 The memory for the new device path is allocated from EFI boot services memory. It is the
118 responsibility of the caller to free the memory allocated.
120 @param FirstDevicePath A pointer to a device path data structure.
121 @param SecondDevicePath A pointer to a device path data structure.
123 @return A pointer to the new device path.
126 EFI_DEVICE_PATH_PROTOCOL
*
129 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
130 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
133 return mDevicePathUtilities
->AppendDevicePath (FirstDevicePath
, SecondDevicePath
);
137 Creates a new path by appending the device node to the device path.
139 This function creates a new device path by appending a copy of the device node specified by
140 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
141 The end-of-device-path device node is moved after the end of the appended device node.
142 If DevicePathNode is NULL then a copy of DevicePath is returned.
143 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device path device
145 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node
147 If there is not enough memory to allocate space for the new device path, then NULL is returned.
148 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
149 free the memory allocated.
151 @param DevicePath A pointer to a device path data structure.
152 @param DevicePathNode A pointer to a single device path node.
154 @return A pointer to the new device path.
157 EFI_DEVICE_PATH_PROTOCOL
*
159 AppendDevicePathNode (
160 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
161 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
164 return mDevicePathUtilities
->AppendDeviceNode (DevicePath
, DevicePathNode
);
168 Creates a new device path by appending the specified device path instance to the specified device
171 This function creates a new device path by appending a copy of the device path instance specified
172 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
173 The end-of-device-path device node is moved after the end of the appended device path instance
174 and a new end-of-device-path-instance node is inserted between.
175 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
176 If DevicePathInstance is NULL, then NULL is returned.
177 If there is not enough memory to allocate space for the new device path, then NULL is returned.
178 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
179 free the memory allocated.
181 @param DevicePath A pointer to a device path data structure.
182 @param DevicePathInstance A pointer to a device path instance.
184 @return A pointer to the new device path.
187 EFI_DEVICE_PATH_PROTOCOL
*
189 AppendDevicePathInstance (
190 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
191 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
194 return mDevicePathUtilities
->AppendDevicePathInstance (DevicePath
, DevicePathInstance
);
198 Creates a copy of the current device path instance and returns a pointer to the next device path
201 This function creates a copy of the current device path instance. It also updates DevicePath to
202 point to the next device path instance in the device path (or NULL if no more) and updates Size
203 to hold the size of the device path instance copy.
204 If DevicePath is NULL, then NULL is returned.
205 If there is not enough memory to allocate space for the new device path, then NULL is returned.
206 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
207 free the memory allocated.
208 If Size is NULL, then ASSERT().
210 @param DevicePath On input, this holds the pointer to the current device path
211 instance. On output, this holds the pointer to the next device
212 path instance or NULL if there are no more device path
213 instances in the device path pointer to a device path data
215 @param Size On output, this holds the size of the device path instance, in
216 bytes or zero, if DevicePath is NULL.
218 @return A pointer to the current device path instance.
221 EFI_DEVICE_PATH_PROTOCOL
*
223 GetNextDevicePathInstance (
224 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
228 ASSERT (Size
!= NULL
);
229 return mDevicePathUtilities
->GetNextDevicePathInstance (DevicePath
, Size
);
233 Creates a copy of the current device path instance and returns a pointer to the next device path
236 This function creates a new device node in a newly allocated buffer of size NodeLength and
237 initializes the device path node header with NodeType and NodeSubType. The new device path node
239 If NodeLength is smaller than a device path header, then NULL is returned.
240 If there is not enough memory to allocate space for the new device path, then NULL is returned.
241 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
242 free the memory allocated.
244 @param NodeType The device node type for the new device node.
245 @param NodeSubType The device node sub-type for the new device node.
246 @param NodeLength The length of the new device node.
248 @return The new device path.
251 EFI_DEVICE_PATH_PROTOCOL
*
255 IN UINT8 NodeSubType
,
259 return mDevicePathUtilities
->CreateDeviceNode (NodeType
, NodeSubType
, NodeLength
);
263 Determines if a device path is single or multi-instance.
265 This function returns TRUE if the device path specified by DevicePath is multi-instance.
266 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
268 @param DevicePath A pointer to a device path data structure.
270 @retval TRUE DevicePath is multi-instance.
271 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
276 IsDevicePathMultiInstance (
277 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
280 return mDevicePathUtilities
->IsDevicePathMultiInstance (DevicePath
);
284 Retrieves the device path protocol from a handle.
286 This function returns the device path protocol from the handle specified by Handle. If Handle is
287 NULL or Handle does not contain a device path protocol, then NULL is returned.
289 @param Handle The handle from which to retrieve the device path protocol.
291 @return The device path protocol from the handle specified by Handle.
294 EFI_DEVICE_PATH_PROTOCOL
*
296 DevicePathFromHandle (
300 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
303 Status
= gBS
->HandleProtocol (
305 &gEfiDevicePathProtocolGuid
,
308 if (EFI_ERROR (Status
)) {
315 Allocates a device path for a file and appends it to an existing device path.
317 If Device is a valid device handle that contains a device path protocol, then a device path for
318 the file specified by FileName is allocated and appended to the device path associated with the
319 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
320 that does not support the device path protocol, then a device path containing a single device
321 path node for the file specified by FileName is allocated and returned.
322 If FileName is NULL, then ASSERT().
324 @param Device A pointer to a device handle. This parameter is optional and
326 @param FileName A pointer to a Null-terminated Unicode string.
328 @return The allocated device path.
331 EFI_DEVICE_PATH_PROTOCOL
*
334 IN EFI_HANDLE Device
, OPTIONAL
335 IN CONST CHAR16
*FileName
339 FILEPATH_DEVICE_PATH
*FilePath
;
340 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
341 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
345 Size
= StrSize (FileName
);
346 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
347 if (FileDevicePath
!= NULL
) {
348 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
349 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
350 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
351 CopyMem (&FilePath
->PathName
, FileName
, Size
);
352 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
353 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
355 if (Device
!= NULL
) {
356 DevicePath
= DevicePathFromHandle (Device
);
359 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
360 FreePool (FileDevicePath
);