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.
13 Module Name: UefiDevicePathLib.c
18 // The package level header files this module uses
22 // The protocols, PPI and GUID defintions for this module
24 #include <Protocol/DevicePathUtilities.h>
25 #include <Protocol/DevicePath.h>
27 // The Library classes this module consumes
29 #include <Library/DevicePathLib.h>
30 #include <Library/DebugLib.h>
31 #include <Library/BaseLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/UefiBootServicesTableLib.h>
36 #include "UefiDevicePathLibInternal.h"
38 STATIC EFI_DEVICE_PATH_UTILITIES_PROTOCOL
*mDevicePathUtilities
= NULL
;
41 The constructor function caches the pointer to DevicePathUtilites protocol.
43 The constructor function locates DevicePathUtilities protocol from protocol database.
44 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
46 @param ImageHandle The firmware allocated handle for the EFI image.
47 @param SystemTable A pointer to the EFI System Table.
49 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
54 DevicePathLibConstructor (
55 IN EFI_HANDLE ImageHandle
,
56 IN EFI_SYSTEM_TABLE
*SystemTable
61 Status
= gBS
->LocateProtocol (
62 &gEfiDevicePathUtilitiesProtocolGuid
,
64 (VOID
**) &mDevicePathUtilities
66 ASSERT_EFI_ERROR (Status
);
67 ASSERT (mDevicePathUtilities
!= NULL
);
73 Returns the size of a device path in bytes.
75 This function returns the size, in bytes, of the device path data structure specified by
76 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
78 @param DevicePath A pointer to a device path data structure.
80 @return The size of a device path in bytes.
86 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
89 return mDevicePathUtilities
->GetDevicePathSize (DevicePath
);
93 Creates a new device path by appending a second device path to a first device path.
95 This function allocates space for a new copy of the device path specified by DevicePath. If
96 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
97 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
98 is returned. Otherwise, NULL is returned.
100 @param DevicePath A pointer to a device path data structure.
102 @return A pointer to the duplicated device path.
105 EFI_DEVICE_PATH_PROTOCOL
*
107 DuplicateDevicePath (
108 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
111 return mDevicePathUtilities
->DuplicateDevicePath (DevicePath
);
115 Creates a new device path by appending a second device path to a first device path.
117 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
118 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
119 SecondDevicePath is retained. The newly created device path is returned.
120 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
121 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
122 If both FirstDevicePath and SecondDevicePath are NULL, then NULL is returned.
123 If there is not enough memory for the newly allocated buffer, then NULL is returned.
124 The memory for the new device path is allocated from EFI boot services memory. It is the
125 responsibility of the caller to free the memory allocated.
127 @param FirstDevicePath A pointer to a device path data structure.
128 @param SecondDevicePath A pointer to a device path data structure.
130 @return A pointer to the new device path.
133 EFI_DEVICE_PATH_PROTOCOL
*
136 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
137 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
140 return mDevicePathUtilities
->AppendDevicePath (FirstDevicePath
, SecondDevicePath
);
144 Creates a new path by appending the device node to the device path.
146 This function creates a new device path by appending a copy of the device node specified by
147 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
148 The end-of-device-path device node is moved after the end of the appended device node.
149 If DevicePath is NULL, then NULL is returned.
150 If DevicePathNode is NULL, then NULL is returned.
151 If there is not enough memory to allocate space for the new device path, then NULL is returned.
152 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
153 free the memory allocated.
155 @param DevicePath A pointer to a device path data structure.
156 @param DevicePathNode A pointer to a single device path node.
158 @return A pointer to the new device path.
161 EFI_DEVICE_PATH_PROTOCOL
*
163 AppendDevicePathNode (
164 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
165 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
168 return mDevicePathUtilities
->AppendDeviceNode (DevicePath
, DevicePathNode
);
172 Creates a new device path by appending the specified device path instance to the specified device
175 This function creates a new device path by appending a copy of the device path instance specified
176 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
177 The end-of-device-path device node is moved after the end of the appended device path instance
178 and a new end-of-device-path-instance node is inserted between.
179 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
180 If DevicePathInstance is NULL, then NULL is returned.
181 If there is not enough memory to allocate space for the new device path, then NULL is returned.
182 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
183 free the memory allocated.
185 @param DevicePath A pointer to a device path data structure.
186 @param DevicePathInstance A pointer to a device path instance.
188 @return A pointer to the new device path.
191 EFI_DEVICE_PATH_PROTOCOL
*
193 AppendDevicePathInstance (
194 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
195 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
198 return mDevicePathUtilities
->AppendDevicePathInstance (DevicePath
, DevicePathInstance
);
202 Creates a copy of the current device path instance and returns a pointer to the next device path
205 This function creates a copy of the current device path instance. It also updates DevicePath to
206 point to the next device path instance in the device path (or NULL if no more) and updates Size
207 to hold the size of the device path instance copy.
208 If DevicePath is NULL, then NULL is returned.
209 If there is not enough memory to allocate space for the new device path, then NULL is returned.
210 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
211 free the memory allocated.
212 If Size is NULL, then ASSERT().
214 @param DevicePath On input, this holds the pointer to the current device path
215 instance. On output, this holds the pointer to the next device
216 path instance or NULL if there are no more device path
217 instances in the device path pointer to a device path data
219 @param Size On output, this holds the size of the device path instance, in
220 bytes or zero, if DevicePath is NULL.
222 @return A pointer to the current device path instance.
225 EFI_DEVICE_PATH_PROTOCOL
*
227 GetNextDevicePathInstance (
228 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
232 ASSERT (Size
!= NULL
);
233 return mDevicePathUtilities
->GetNextDevicePathInstance (DevicePath
, Size
);
237 Creates a copy of the current device path instance and returns a pointer to the next device path
240 This function creates a new device node in a newly allocated buffer of size NodeLength and
241 initializes the device path node header with NodeType and NodeSubType. The new device path node
243 If NodeLength is smaller than a device path header, then NULL is returned.
244 If there is not enough memory to allocate space for the new device path, then NULL is returned.
245 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
246 free the memory allocated.
248 @param NodeType The device node type for the new device node.
249 @param NodeSubType The device node sub-type for the new device node.
250 @param NodeLength The length of the new device node.
252 @return The new device path.
255 EFI_DEVICE_PATH_PROTOCOL
*
259 IN UINT8 NodeSubType
,
263 return mDevicePathUtilities
->CreateDeviceNode (NodeType
, NodeSubType
, NodeLength
);
267 Determines if a device path is single or multi-instance.
269 This function returns TRUE if the device path specified by DevicePath is multi-instance.
270 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
272 @param DevicePath A pointer to a device path data structure.
274 @retval TRUE DevicePath is multi-instance.
275 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
280 IsDevicePathMultiInstance (
281 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
284 return mDevicePathUtilities
->IsDevicePathMultiInstance (DevicePath
);
288 Retrieves the device path protocol from a handle.
290 This function returns the device path protocol from the handle specified by Handle. If Handle is
291 NULL or Handle does not contain a device path protocol, then NULL is returned.
293 @param Handle The handle from which to retrieve the device path protocol.
295 @return The device path protocol from the handle specified by Handle.
298 EFI_DEVICE_PATH_PROTOCOL
*
300 DevicePathFromHandle (
304 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
307 Status
= gBS
->HandleProtocol (
309 &gEfiDevicePathProtocolGuid
,
312 if (EFI_ERROR (Status
)) {
319 Allocates a device path for a file and appends it to an existing device path.
321 If Device is a valid device handle that contains a device path protocol, then a device path for
322 the file specified by FileName is allocated and appended to the device path associated with the
323 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
324 that does not support the device path protocol, then a device path containing a single device
325 path node for the file specified by FileName is allocated and returned.
326 If FileName is NULL, then ASSERT().
328 @param Device A pointer to a device handle. This parameter is optional and
330 @param FileName A pointer to a Null-terminated Unicode string.
332 @return The allocated device path.
335 EFI_DEVICE_PATH_PROTOCOL
*
338 IN EFI_HANDLE Device
, OPTIONAL
339 IN CONST CHAR16
*FileName
343 FILEPATH_DEVICE_PATH
*FilePath
;
344 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
345 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
349 Size
= StrSize (FileName
);
350 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
351 if (FileDevicePath
!= NULL
) {
352 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
353 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
354 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
355 CopyMem (&FilePath
->PathName
, FileName
, Size
);
356 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
357 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
359 if (Device
!= NULL
) {
360 DevicePath
= DevicePathFromHandle (Device
);
363 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
364 FreePool (FileDevicePath
);