]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c
Update DevicePathLib to reflect the update of UEFI device utility protocol
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLibDevicePathProtocol / UefiDevicePathLib.c
1 /** @file
2 UEFI Device Path Library.
3
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
9
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.
12
13 **/
14
15
16 #include <Uefi.h>
17
18 #include <Protocol/DevicePathUtilities.h>
19 #include <Protocol/DevicePath.h>
20
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>
27
28 #include "UefiDevicePathLibInternal.h"
29
30 STATIC EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mDevicePathUtilities = NULL;
31
32 /**
33 The constructor function caches the pointer to DevicePathUtilites protocol.
34
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.
37
38 @param ImageHandle The firmware allocated handle for the EFI image.
39 @param SystemTable A pointer to the EFI System Table.
40
41 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
42
43 **/
44 EFI_STATUS
45 EFIAPI
46 DevicePathLibConstructor (
47 IN EFI_HANDLE ImageHandle,
48 IN EFI_SYSTEM_TABLE *SystemTable
49 )
50 {
51 EFI_STATUS Status;
52
53 Status = gBS->LocateProtocol (
54 &gEfiDevicePathUtilitiesProtocolGuid,
55 NULL,
56 (VOID**) &mDevicePathUtilities
57 );
58 ASSERT_EFI_ERROR (Status);
59 ASSERT (mDevicePathUtilities != NULL);
60
61 return Status;
62 }
63
64 /**
65 Returns the size of a device path in bytes.
66
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.
69
70 @param DevicePath A pointer to a device path data structure.
71
72 @return The size of a device path in bytes.
73
74 **/
75 UINTN
76 EFIAPI
77 GetDevicePathSize (
78 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
79 )
80 {
81 return mDevicePathUtilities->GetDevicePathSize (DevicePath);
82 }
83
84 /**
85 Creates a new device path by appending a second device path to a first device path.
86
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.
91
92 @param DevicePath A pointer to a device path data structure.
93
94 @return A pointer to the duplicated device path.
95
96 **/
97 EFI_DEVICE_PATH_PROTOCOL *
98 EFIAPI
99 DuplicateDevicePath (
100 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
101 )
102 {
103 return mDevicePathUtilities->DuplicateDevicePath (DevicePath);
104 }
105
106 /**
107 Creates a new device path by appending a second device path to a first device path.
108
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
115 returned.
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.
119
120 @param FirstDevicePath A pointer to a device path data structure.
121 @param SecondDevicePath A pointer to a device path data structure.
122
123 @return A pointer to the new device path.
124
125 **/
126 EFI_DEVICE_PATH_PROTOCOL *
127 EFIAPI
128 AppendDevicePath (
129 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL
130 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL
131 )
132 {
133 return mDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);
134 }
135
136 /**
137 Creates a new path by appending the device node to the device path.
138
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 DeviceNode is NULL then a copy of DevicePath is returned.
143 If DevicePathNode is NULL then a copy of DevicePath is returned.
144 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node
145 is returned.
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.
149
150 @param DevicePath A pointer to a device path data structure.
151 @param DevicePathNode A pointer to a single device path node.
152
153 @return A pointer to the new device path.
154
155 **/
156 EFI_DEVICE_PATH_PROTOCOL *
157 EFIAPI
158 AppendDevicePathNode (
159 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL
160 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL
161 )
162 {
163 return mDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);
164 }
165
166 /**
167 Creates a new device path by appending the specified device path instance to the specified device
168 path.
169
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.
179
180 @param DevicePath A pointer to a device path data structure.
181 @param DevicePathInstance A pointer to a device path instance.
182
183 @return A pointer to the new device path.
184
185 **/
186 EFI_DEVICE_PATH_PROTOCOL *
187 EFIAPI
188 AppendDevicePathInstance (
189 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL
190 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL
191 )
192 {
193 return mDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);
194 }
195
196 /**
197 Creates a copy of the current device path instance and returns a pointer to the next device path
198 instance.
199
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().
208
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
213 structure.
214 @param Size On output, this holds the size of the device path instance, in
215 bytes or zero, if DevicePath is NULL.
216
217 @return A pointer to the current device path instance.
218
219 **/
220 EFI_DEVICE_PATH_PROTOCOL *
221 EFIAPI
222 GetNextDevicePathInstance (
223 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,
224 OUT UINTN *Size
225 )
226 {
227 ASSERT (Size != NULL);
228 return mDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);
229 }
230
231 /**
232 Creates a copy of the current device path instance and returns a pointer to the next device path
233 instance.
234
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
237 is returned.
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.
242
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.
246
247 @return The new device path.
248
249 **/
250 EFI_DEVICE_PATH_PROTOCOL *
251 EFIAPI
252 CreateDeviceNode (
253 IN UINT8 NodeType,
254 IN UINT8 NodeSubType,
255 IN UINT16 NodeLength
256 )
257 {
258 return mDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);
259 }
260
261 /**
262 Determines if a device path is single or multi-instance.
263
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.
266
267 @param DevicePath A pointer to a device path data structure.
268
269 @retval TRUE DevicePath is multi-instance.
270 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
271
272 **/
273 BOOLEAN
274 EFIAPI
275 IsDevicePathMultiInstance (
276 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
277 )
278 {
279 return mDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);
280 }
281
282 /**
283 Retrieves the device path protocol from a handle.
284
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.
287
288 @param Handle The handle from which to retrieve the device path protocol.
289
290 @return The device path protocol from the handle specified by Handle.
291
292 **/
293 EFI_DEVICE_PATH_PROTOCOL *
294 EFIAPI
295 DevicePathFromHandle (
296 IN EFI_HANDLE Handle
297 )
298 {
299 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
300 EFI_STATUS Status;
301
302 Status = gBS->HandleProtocol (
303 Handle,
304 &gEfiDevicePathProtocolGuid,
305 (VOID *) &DevicePath
306 );
307 if (EFI_ERROR (Status)) {
308 DevicePath = NULL;
309 }
310 return DevicePath;
311 }
312
313 /**
314 Allocates a device path for a file and appends it to an existing device path.
315
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().
322
323 @param Device A pointer to a device handle. This parameter is optional and
324 may be NULL.
325 @param FileName A pointer to a Null-terminated Unicode string.
326
327 @return The allocated device path.
328
329 **/
330 EFI_DEVICE_PATH_PROTOCOL *
331 EFIAPI
332 FileDevicePath (
333 IN EFI_HANDLE Device, OPTIONAL
334 IN CONST CHAR16 *FileName
335 )
336 {
337 UINTN Size;
338 FILEPATH_DEVICE_PATH *FilePath;
339 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
340 EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;
341
342 DevicePath = NULL;
343
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));
353
354 if (Device != NULL) {
355 DevicePath = DevicePathFromHandle (Device);
356 }
357
358 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);
359 FreePool (FileDevicePath);
360 }
361
362 return DevicePath;
363 }
364