2 Library instance that implement UEFI Device Path Library class based on protocol
3 gEfiDevicePathUtilitiesProtocolGuid.
5 Copyright (c) 2006, Intel Corporation<BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Protocol/DevicePathUtilities.h>
20 #include <Protocol/DevicePath.h>
22 #include <Library/DevicePathLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/UefiBootServicesTableLib.h>
29 EFI_DEVICE_PATH_UTILITIES_PROTOCOL
*mDevicePathUtilities
= NULL
;
32 The constructor function caches the pointer to DevicePathUtilites protocol.
34 The constructor function locates DevicePathUtilities protocol from protocol database.
35 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
37 @param ImageHandle The firmware allocated handle for the EFI image.
38 @param SystemTable A pointer to the EFI System Table.
40 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
45 DevicePathLibConstructor (
46 IN EFI_HANDLE ImageHandle
,
47 IN EFI_SYSTEM_TABLE
*SystemTable
52 Status
= gBS
->LocateProtocol (
53 &gEfiDevicePathUtilitiesProtocolGuid
,
55 (VOID
**) &mDevicePathUtilities
57 ASSERT_EFI_ERROR (Status
);
58 ASSERT (mDevicePathUtilities
!= NULL
);
64 Returns the size of a device path in bytes.
66 This function returns the size, in bytes, of the device path data structure specified by
67 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
69 @param DevicePath A pointer to a device path data structure.
71 @return The size of a device path in bytes.
77 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
80 return mDevicePathUtilities
->GetDevicePathSize (DevicePath
);
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 return mDevicePathUtilities
->DuplicateDevicePath (DevicePath
);
106 Creates a new device path by appending a second device path to a first device path.
108 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
109 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
110 SecondDevicePath is retained. The newly created device path is returned.
111 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
112 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
113 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is
115 If there is not enough memory for the newly allocated buffer, then NULL is returned.
116 The memory for the new device path is allocated from EFI boot services memory. It is the
117 responsibility of the caller to free the memory allocated.
119 @param FirstDevicePath A pointer to a device path data structure.
120 @param SecondDevicePath A pointer to a device path data structure.
122 @return A pointer to the new device path.
125 EFI_DEVICE_PATH_PROTOCOL
*
128 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
129 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
132 return mDevicePathUtilities
->AppendDevicePath (FirstDevicePath
, SecondDevicePath
);
136 Creates a new path by appending the device node to the device path.
138 This function creates a new device path by appending a copy of the device node specified by
139 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
140 The end-of-device-path device node is moved after the end of the appended device node.
141 If DevicePathNode is NULL then a copy of DevicePath is returned.
142 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device path device
144 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node
146 If there is not enough memory to allocate space for the new device path, then NULL is returned.
147 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
148 free the memory allocated.
150 @param DevicePath A pointer to a device path data structure.
151 @param DevicePathNode A pointer to a single device path node.
153 @return A pointer to the new device path.
156 EFI_DEVICE_PATH_PROTOCOL
*
158 AppendDevicePathNode (
159 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
160 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
163 return mDevicePathUtilities
->AppendDeviceNode (DevicePath
, DevicePathNode
);
167 Creates a new device path by appending the specified device path instance to the specified device
170 This function creates a new device path by appending a copy of the device path instance specified
171 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
172 The end-of-device-path device node is moved after the end of the appended device path instance
173 and a new end-of-device-path-instance node is inserted between.
174 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
175 If DevicePathInstance is NULL, then NULL is returned.
176 If there is not enough memory to allocate space for the new device path, then NULL is returned.
177 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
178 free the memory allocated.
180 @param DevicePath A pointer to a device path data structure.
181 @param DevicePathInstance A pointer to a device path instance.
183 @return A pointer to the new device path.
186 EFI_DEVICE_PATH_PROTOCOL
*
188 AppendDevicePathInstance (
189 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
190 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
193 return mDevicePathUtilities
->AppendDevicePathInstance (DevicePath
, DevicePathInstance
);
197 Creates a copy of the current device path instance and returns a pointer to the next device path
200 This function creates a copy of the current device path instance. It also updates DevicePath to
201 point to the next device path instance in the device path (or NULL if no more) and updates Size
202 to hold the size of the device path instance copy.
203 If DevicePath is NULL, then NULL is returned.
204 If there is not enough memory to allocate space for the new device path, then NULL is returned.
205 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
206 free the memory allocated.
207 If Size is NULL, then ASSERT().
209 @param DevicePath On input, this holds the pointer to the current device path
210 instance. On output, this holds the pointer to the next device
211 path instance or NULL if there are no more device path
212 instances in the device path pointer to a device path data
214 @param Size On output, this holds the size of the device path instance, in
215 bytes or zero, if DevicePath is NULL.
217 @return A pointer to the current device path instance.
220 EFI_DEVICE_PATH_PROTOCOL
*
222 GetNextDevicePathInstance (
223 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
227 ASSERT (Size
!= NULL
);
228 return mDevicePathUtilities
->GetNextDevicePathInstance (DevicePath
, Size
);
232 Creates a copy of the current device path instance and returns a pointer to the next device path
235 This function creates a new device node in a newly allocated buffer of size NodeLength and
236 initializes the device path node header with NodeType and NodeSubType. The new device path node
238 If NodeLength is smaller than a device path header, then NULL is returned.
239 If there is not enough memory to allocate space for the new device path, then NULL is returned.
240 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
241 free the memory allocated.
243 @param NodeType The device node type for the new device node.
244 @param NodeSubType The device node sub-type for the new device node.
245 @param NodeLength The length of the new device node.
247 @return A pointer to the new created file device path
250 EFI_DEVICE_PATH_PROTOCOL
*
254 IN UINT8 NodeSubType
,
258 return mDevicePathUtilities
->CreateDeviceNode (NodeType
, NodeSubType
, NodeLength
);
262 Determines if a device path is single or multi-instance.
264 This function returns TRUE if the device path specified by DevicePath is multi-instance.
265 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
267 @param DevicePath A pointer to a device path data structure.
269 @retval TRUE DevicePath is multi-instance.
270 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
275 IsDevicePathMultiInstance (
276 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
279 return mDevicePathUtilities
->IsDevicePathMultiInstance (DevicePath
);
283 Retrieves the device path protocol from a handle.
285 This function returns the device path protocol from the handle specified by Handle. If Handle is
286 NULL or Handle does not contain a device path protocol, then NULL is returned.
288 @param Handle The handle from which to retrieve the device path protocol.
290 @return The device path protocol from the handle specified by Handle.
293 EFI_DEVICE_PATH_PROTOCOL
*
295 DevicePathFromHandle (
299 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
302 Status
= gBS
->HandleProtocol (
304 &gEfiDevicePathProtocolGuid
,
307 if (EFI_ERROR (Status
)) {
314 Allocates a device path for a file and appends it to an existing device path.
316 If Device is a valid device handle that contains a device path protocol, then a device path for
317 the file specified by FileName is allocated and appended to the device path associated with the
318 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
319 that does not support the device path protocol, then a device path containing a single device
320 path node for the file specified by FileName is allocated and returned.
321 If FileName is NULL, then ASSERT().
323 @param Device A pointer to a device handle. This parameter is optional and
325 @param FileName A pointer to a Null-terminated Unicode string.
327 @return A pointer to the new created file device path
330 EFI_DEVICE_PATH_PROTOCOL
*
333 IN EFI_HANDLE Device
, OPTIONAL
334 IN CONST CHAR16
*FileName
338 FILEPATH_DEVICE_PATH
*FilePath
;
339 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
340 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
344 Size
= StrSize (FileName
);
345 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
346 if (FileDevicePath
!= NULL
) {
347 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
348 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
349 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
350 CopyMem (&FilePath
->PathName
, FileName
, Size
);
351 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
352 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
354 if (Device
!= NULL
) {
355 DevicePath
= DevicePathFromHandle (Device
);
358 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
359 FreePool (FileDevicePath
);