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
17 STATIC EFI_DEVICE_PATH_UTILITIES_PROTOCOL
*mDevicePathUtilities
= NULL
;
20 The constructor function caches the pointer to DevicePathUtilites protocol.
22 The constructor function locates DevicePathUtilities protocol from protocol database.
23 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
25 @param ImageHandle The firmware allocated handle for the EFI image.
26 @param SystemTable A pointer to the EFI System Table.
28 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
33 DevicePathLibConstructor (
34 IN EFI_HANDLE ImageHandle
,
35 IN EFI_SYSTEM_TABLE
*SystemTable
40 Status
= gBS
->LocateProtocol (
41 &gEfiDevicePathUtilitiesProtocolGuid
,
43 (VOID
**) &mDevicePathUtilities
45 ASSERT_EFI_ERROR (Status
);
46 ASSERT (mDevicePathUtilities
!= NULL
);
52 Returns the size of a device path in bytes.
54 This function returns the size, in bytes, of the device path data structure specified by
55 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
57 @param DevicePath A pointer to a device path data structure.
59 @return The size of a device path in bytes.
65 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
68 return mDevicePathUtilities
->GetDevicePathSize (DevicePath
);
72 Creates a new device path by appending a second device path to a first device path.
74 This function allocates space for a new copy of the device path specified by DevicePath. If
75 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
76 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
77 is returned. Otherwise, NULL is returned.
79 @param DevicePath A pointer to a device path data structure.
81 @return A pointer to the duplicated device path.
84 EFI_DEVICE_PATH_PROTOCOL
*
87 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
90 return mDevicePathUtilities
->DuplicateDevicePath (DevicePath
);
94 Creates a new device path by appending a second device path to a first device path.
96 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
97 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
98 SecondDevicePath is retained. The newly created device path is returned.
99 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
100 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
101 If both FirstDevicePath and SecondDevicePath are NULL, then NULL is returned.
102 If there is not enough memory for the newly allocated buffer, then NULL is returned.
103 The memory for the new device path is allocated from EFI boot services memory. It is the
104 responsibility of the caller to free the memory allocated.
106 @param FirstDevicePath A pointer to a device path data structure.
107 @param SecondDevicePath A pointer to a device path data structure.
109 @return A pointer to the new device path.
112 EFI_DEVICE_PATH_PROTOCOL
*
115 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
116 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
119 return mDevicePathUtilities
->AppendDevicePath (FirstDevicePath
, SecondDevicePath
);
123 Creates a new path by appending the device node to the device path.
125 This function creates a new device path by appending a copy of the device node specified by
126 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
127 The end-of-device-path device node is moved after the end of the appended device node.
128 If DevicePath is NULL, then NULL is returned.
129 If DevicePathNode is NULL, then NULL is returned.
130 If there is not enough memory to allocate space for the new device path, then NULL is returned.
131 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
132 free the memory allocated.
134 @param DevicePath A pointer to a device path data structure.
135 @param DevicePathNode A pointer to a single device path node.
137 @return A pointer to the new device path.
140 EFI_DEVICE_PATH_PROTOCOL
*
142 AppendDevicePathNode (
143 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
144 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
147 return mDevicePathUtilities
->AppendDeviceNode (DevicePath
, DevicePathNode
);
151 Creates a new device path by appending the specified device path instance to the specified device
154 This function creates a new device path by appending a copy of the device path instance specified
155 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
156 The end-of-device-path device node is moved after the end of the appended device path instance
157 and a new end-of-device-path-instance node is inserted between.
158 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
159 If DevicePathInstance is NULL, then NULL is returned.
160 If there is not enough memory to allocate space for the new device path, then NULL is returned.
161 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
162 free the memory allocated.
164 @param DevicePath A pointer to a device path data structure.
165 @param DevicePathInstance A pointer to a device path instance.
167 @return A pointer to the new device path.
170 EFI_DEVICE_PATH_PROTOCOL
*
172 AppendDevicePathInstance (
173 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
174 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
177 return mDevicePathUtilities
->AppendDevicePathInstance (DevicePath
, DevicePathInstance
);
181 Creates a copy of the current device path instance and returns a pointer to the next device path
184 This function creates a copy of the current device path instance. It also updates DevicePath to
185 point to the next device path instance in the device path (or NULL if no more) and updates Size
186 to hold the size of the device path instance copy.
187 If DevicePath is NULL, then NULL is returned.
188 If there is not enough memory to allocate space for the new device path, then NULL is returned.
189 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
190 free the memory allocated.
191 If Size is NULL, then ASSERT().
193 @param DevicePath On input, this holds the pointer to the current device path
194 instance. On output, this holds the pointer to the next device
195 path instance or NULL if there are no more device path
196 instances in the device path pointer to a device path data
198 @param Size On output, this holds the size of the device path instance, in
199 bytes or zero, if DevicePath is NULL.
201 @return A pointer to the current device path instance.
204 EFI_DEVICE_PATH_PROTOCOL
*
206 GetNextDevicePathInstance (
207 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
211 ASSERT (Size
!= NULL
);
212 return mDevicePathUtilities
->GetNextDevicePathInstance (DevicePath
, Size
);
216 Creates a copy of the current device path instance and returns a pointer to the next device path
219 This function creates a new device node in a newly allocated buffer of size NodeLength and
220 initializes the device path node header with NodeType and NodeSubType. The new device path node
222 If NodeLength is smaller than a device path header, then NULL is returned.
223 If there is not enough memory to allocate space for the new device path, then NULL is returned.
224 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
225 free the memory allocated.
227 @param NodeType The device node type for the new device node.
228 @param NodeSubType The device node sub-type for the new device node.
229 @param NodeLength The length of the new device node.
231 @return The new device path.
234 EFI_DEVICE_PATH_PROTOCOL
*
238 IN UINT8 NodeSubType
,
242 return mDevicePathUtilities
->CreateDeviceNode (NodeType
, NodeSubType
, NodeLength
);
246 Determines if a device path is single or multi-instance.
248 This function returns TRUE if the device path specified by DevicePath is multi-instance.
249 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
251 @param DevicePath A pointer to a device path data structure.
253 @retval TRUE DevicePath is multi-instance.
254 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
259 IsDevicePathMultiInstance (
260 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
263 return mDevicePathUtilities
->IsDevicePathMultiInstance (DevicePath
);
267 Retrieves the device path protocol from a handle.
269 This function returns the device path protocol from the handle specified by Handle. If Handle is
270 NULL or Handle does not contain a device path protocol, then NULL is returned.
272 @param Handle The handle from which to retrieve the device path protocol.
274 @return The device path protocol from the handle specified by Handle.
277 EFI_DEVICE_PATH_PROTOCOL
*
279 DevicePathFromHandle (
283 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
286 Status
= gBS
->HandleProtocol (
288 &gEfiDevicePathProtocolGuid
,
291 if (EFI_ERROR (Status
)) {
298 Allocates a device path for a file and appends it to an existing device path.
300 If Device is a valid device handle that contains a device path protocol, then a device path for
301 the file specified by FileName is allocated and appended to the device path associated with the
302 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
303 that does not support the device path protocol, then a device path containing a single device
304 path node for the file specified by FileName is allocated and returned.
305 If FileName is NULL, then ASSERT().
307 @param Device A pointer to a device handle. This parameter is optional and
309 @param FileName A pointer to a Null-terminated Unicode string.
311 @return The allocated device path.
314 EFI_DEVICE_PATH_PROTOCOL
*
317 IN EFI_HANDLE Device
, OPTIONAL
318 IN CONST CHAR16
*FileName
322 FILEPATH_DEVICE_PATH
*FilePath
;
323 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
324 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
328 Size
= StrSize (FileName
);
329 FileDevicePath
= AllocatePool (Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ EFI_END_DEVICE_PATH_LENGTH
);
330 if (FileDevicePath
!= NULL
) {
331 FilePath
= (FILEPATH_DEVICE_PATH
*) FileDevicePath
;
332 FilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
333 FilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
334 CopyMem (&FilePath
->PathName
, FileName
, Size
);
335 SetDevicePathNodeLength (&FilePath
->Header
, Size
+ SIZE_OF_FILEPATH_DEVICE_PATH
);
336 SetDevicePathEndNode (NextDevicePathNode (&FilePath
->Header
));
338 if (Device
!= NULL
) {
339 DevicePath
= DevicePathFromHandle (Device
);
342 DevicePath
= AppendDevicePath (DevicePath
, FileDevicePath
);
343 FreePool (FileDevicePath
);