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.
16 // The package level header files this module uses
20 // The protocols, PPI and GUID defintions for this module
22 #include <Protocol/DevicePathUtilities.h>
23 #include <Protocol/DevicePath.h>
25 // The Library classes this module consumes
27 #include <Library/DevicePathLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/BaseLib.h>
30 #include <Library/MemoryAllocationLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/UefiBootServicesTableLib.h>
34 #include "UefiDevicePathLibInternal.h"
36 STATIC EFI_DEVICE_PATH_UTILITIES_PROTOCOL
*mDevicePathUtilities
= NULL
;
39 The constructor function caches the pointer to DevicePathUtilites protocol.
41 The constructor function locates DevicePathUtilities protocol from protocol database.
42 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
44 @param ImageHandle The firmware allocated handle for the EFI image.
45 @param SystemTable A pointer to the EFI System Table.
47 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
52 DevicePathLibConstructor (
53 IN EFI_HANDLE ImageHandle
,
54 IN EFI_SYSTEM_TABLE
*SystemTable
59 Status
= gBS
->LocateProtocol (
60 &gEfiDevicePathUtilitiesProtocolGuid
,
62 (VOID
**) &mDevicePathUtilities
64 ASSERT_EFI_ERROR (Status
);
65 ASSERT (mDevicePathUtilities
!= NULL
);
71 Returns the size of a device path in bytes.
73 This function returns the size, in bytes, of the device path data structure specified by
74 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
76 @param DevicePath A pointer to a device path data structure.
78 @return The size of a device path in bytes.
84 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
87 return mDevicePathUtilities
->GetDevicePathSize (DevicePath
);
91 Creates a new device path by appending a second device path to a first device path.
93 This function allocates space for a new copy of the device path specified by DevicePath. If
94 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
95 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
96 is returned. Otherwise, NULL is returned.
98 @param DevicePath A pointer to a device path data structure.
100 @return A pointer to the duplicated device path.
103 EFI_DEVICE_PATH_PROTOCOL
*
105 DuplicateDevicePath (
106 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
109 return mDevicePathUtilities
->DuplicateDevicePath (DevicePath
);
113 Creates a new device path by appending a second device path to a first device path.
115 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
116 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
117 SecondDevicePath is retained. The newly created device path is returned.
118 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
119 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
120 If both FirstDevicePath and SecondDevicePath are NULL, then NULL is returned.
121 If there is not enough memory for the newly allocated buffer, then NULL is returned.
122 The memory for the new device path is allocated from EFI boot services memory. It is the
123 responsibility of the caller to free the memory allocated.
125 @param FirstDevicePath A pointer to a device path data structure.
126 @param SecondDevicePath A pointer to a device path data structure.
128 @return A pointer to the new device path.
131 EFI_DEVICE_PATH_PROTOCOL
*
134 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
135 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
138 return mDevicePathUtilities
->AppendDevicePath (FirstDevicePath
, SecondDevicePath
);
142 Creates a new path by appending the device node to the device path.
144 This function creates a new device path by appending a copy of the device node specified by
145 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
146 The end-of-device-path device node is moved after the end of the appended device node.
147 If DevicePath is NULL, then NULL is returned.
148 If DevicePathNode is NULL, then NULL is returned.
149 If there is not enough memory to allocate space for the new device path, then NULL is returned.
150 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
151 free the memory allocated.
153 @param DevicePath A pointer to a device path data structure.
154 @param DevicePathNode A pointer to a single device path node.
156 @return A pointer to the new device path.
159 EFI_DEVICE_PATH_PROTOCOL
*
161 AppendDevicePathNode (
162 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
163 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
166 return mDevicePathUtilities
->AppendDeviceNode (DevicePath
, DevicePathNode
);
170 Creates a new device path by appending the specified device path instance to the specified device
173 This function creates a new device path by appending a copy of the device path instance specified
174 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
175 The end-of-device-path device node is moved after the end of the appended device path instance
176 and a new end-of-device-path-instance node is inserted between.
177 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
178 If DevicePathInstance is NULL, then NULL is returned.
179 If there is not enough memory to allocate space for the new device path, then NULL is returned.
180 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
181 free the memory allocated.
183 @param DevicePath A pointer to a device path data structure.
184 @param DevicePathInstance A pointer to a device path instance.
186 @return A pointer to the new device path.
189 EFI_DEVICE_PATH_PROTOCOL
*
191 AppendDevicePathInstance (
192 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
193 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
196 return mDevicePathUtilities
->AppendDevicePathInstance (DevicePath
, DevicePathInstance
);
200 Creates a copy of the current device path instance and returns a pointer to the next device path
203 This function creates a copy of the current device path instance. It also updates DevicePath to
204 point to the next device path instance in the device path (or NULL if no more) and updates Size
205 to hold the size of the device path instance copy.
206 If DevicePath is NULL, then NULL is returned.
207 If there is not enough memory to allocate space for the new device path, then NULL is returned.
208 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
209 free the memory allocated.
210 If Size is NULL, then ASSERT().
212 @param DevicePath On input, this holds the pointer to the current device path
213 instance. On output, this holds the pointer to the next device
214 path instance or NULL if there are no more device path
215 instances in the device path pointer to a device path data
217 @param Size On output, this holds the size of the device path instance, in
218 bytes or zero, if DevicePath is NULL.
220 @return A pointer to the current device path instance.
223 EFI_DEVICE_PATH_PROTOCOL
*
225 GetNextDevicePathInstance (
226 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
230 ASSERT (Size
!= NULL
);
231 return mDevicePathUtilities
->GetNextDevicePathInstance (DevicePath
, Size
);
235 Creates a copy of the current device path instance and returns a pointer to the next device path
238 This function creates a new device node in a newly allocated buffer of size NodeLength and
239 initializes the device path node header with NodeType and NodeSubType. The new device path node
241 If NodeLength is smaller than a device path header, then NULL is returned.
242 If there is not enough memory to allocate space for the new device path, then NULL is returned.
243 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
244 free the memory allocated.
246 @param NodeType The device node type for the new device node.
247 @param NodeSubType The device node sub-type for the new device node.
248 @param NodeLength The length of the new device node.
250 @return The new device path.
253 EFI_DEVICE_PATH_PROTOCOL
*
257 IN UINT8 NodeSubType
,
261 return mDevicePathUtilities
->CreateDeviceNode (NodeType
, NodeSubType
, NodeLength
);
265 Determines if a device path is single or multi-instance.
267 This function returns TRUE if the device path specified by DevicePath is multi-instance.
268 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
270 @param DevicePath A pointer to a device path data structure.
272 @retval TRUE DevicePath is multi-instance.
273 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
278 IsDevicePathMultiInstance (
279 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
282 return mDevicePathUtilities
->IsDevicePathMultiInstance (DevicePath
);
286 Retrieves the device path protocol from a handle.
288 This function returns the device path protocol from the handle specified by Handle. If Handle is
289 NULL or Handle does not contain a device path protocol, then NULL is returned.
291 @param Handle The handle from which to retrieve the device path protocol.
293 @return The device path protocol from the handle specified by Handle.
296 EFI_DEVICE_PATH_PROTOCOL
*
298 DevicePathFromHandle (
302 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
305 Status
= gBS
->HandleProtocol (
307 &gEfiDevicePathProtocolGuid
,
310 if (EFI_ERROR (Status
)) {
317 Allocates a device path for a file and appends it to an existing device path.
319 If Device is a valid device handle that contains a device path protocol, then a device path for
320 the file specified by FileName is allocated and appended to the device path associated with the
321 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
322 that does not support the device path protocol, then a device path containing a single device
323 path node for the file specified by FileName is allocated and returned.
324 If FileName is NULL, then ASSERT().
326 @param Device A pointer to a device handle. This parameter is optional and
328 @param FileName A pointer to a Null-terminated Unicode string.
330 @return The allocated device path.
333 EFI_DEVICE_PATH_PROTOCOL
*
336 IN EFI_HANDLE Device
, OPTIONAL
337 IN CONST CHAR16
*FileName
341 FILEPATH_DEVICE_PATH
*FilePath
;
342 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
343 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
347 Size
= StrSize (FileName
);
348 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
349 if (FileDevicePath
!= NULL
) {
350 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
351 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
352 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
353 CopyMem (&FilePath
->PathName
, FileName
, Size
);
354 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
355 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
357 if (Device
!= NULL
) {
358 DevicePath
= DevicePathFromHandle (Device
);
361 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
362 FreePool (FileDevicePath
);