]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c
Fix one minor comment issue in last check-in
[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 DevicePathNode is NULL then a copy of DevicePath is returned.
143 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device path device
144 node is returned.
145 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node
146 is returned.
147 If there is not enough memory to allocate space for the new device path, then NULL is returned.
148 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
149 free the memory allocated.
150
151 @param DevicePath A pointer to a device path data structure.
152 @param DevicePathNode A pointer to a single device path node.
153
154 @return A pointer to the new device path.
155
156 **/
157 EFI_DEVICE_PATH_PROTOCOL *
158 EFIAPI
159 AppendDevicePathNode (
160 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL
161 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL
162 )
163 {
164 return mDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);
165 }
166
167 /**
168 Creates a new device path by appending the specified device path instance to the specified device
169 path.
170
171 This function creates a new device path by appending a copy of the device path instance specified
172 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
173 The end-of-device-path device node is moved after the end of the appended device path instance
174 and a new end-of-device-path-instance node is inserted between.
175 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
176 If DevicePathInstance is NULL, then NULL is returned.
177 If there is not enough memory to allocate space for the new device path, then NULL is returned.
178 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
179 free the memory allocated.
180
181 @param DevicePath A pointer to a device path data structure.
182 @param DevicePathInstance A pointer to a device path instance.
183
184 @return A pointer to the new device path.
185
186 **/
187 EFI_DEVICE_PATH_PROTOCOL *
188 EFIAPI
189 AppendDevicePathInstance (
190 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL
191 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL
192 )
193 {
194 return mDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);
195 }
196
197 /**
198 Creates a copy of the current device path instance and returns a pointer to the next device path
199 instance.
200
201 This function creates a copy of the current device path instance. It also updates DevicePath to
202 point to the next device path instance in the device path (or NULL if no more) and updates Size
203 to hold the size of the device path instance copy.
204 If DevicePath is NULL, then NULL is returned.
205 If there is not enough memory to allocate space for the new device path, then NULL is returned.
206 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
207 free the memory allocated.
208 If Size is NULL, then ASSERT().
209
210 @param DevicePath On input, this holds the pointer to the current device path
211 instance. On output, this holds the pointer to the next device
212 path instance or NULL if there are no more device path
213 instances in the device path pointer to a device path data
214 structure.
215 @param Size On output, this holds the size of the device path instance, in
216 bytes or zero, if DevicePath is NULL.
217
218 @return A pointer to the current device path instance.
219
220 **/
221 EFI_DEVICE_PATH_PROTOCOL *
222 EFIAPI
223 GetNextDevicePathInstance (
224 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,
225 OUT UINTN *Size
226 )
227 {
228 ASSERT (Size != NULL);
229 return mDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);
230 }
231
232 /**
233 Creates a copy of the current device path instance and returns a pointer to the next device path
234 instance.
235
236 This function creates a new device node in a newly allocated buffer of size NodeLength and
237 initializes the device path node header with NodeType and NodeSubType. The new device path node
238 is returned.
239 If NodeLength is smaller than a device path header, then NULL is returned.
240 If there is not enough memory to allocate space for the new device path, then NULL is returned.
241 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
242 free the memory allocated.
243
244 @param NodeType The device node type for the new device node.
245 @param NodeSubType The device node sub-type for the new device node.
246 @param NodeLength The length of the new device node.
247
248 @return The new device path.
249
250 **/
251 EFI_DEVICE_PATH_PROTOCOL *
252 EFIAPI
253 CreateDeviceNode (
254 IN UINT8 NodeType,
255 IN UINT8 NodeSubType,
256 IN UINT16 NodeLength
257 )
258 {
259 return mDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);
260 }
261
262 /**
263 Determines if a device path is single or multi-instance.
264
265 This function returns TRUE if the device path specified by DevicePath is multi-instance.
266 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
267
268 @param DevicePath A pointer to a device path data structure.
269
270 @retval TRUE DevicePath is multi-instance.
271 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
272
273 **/
274 BOOLEAN
275 EFIAPI
276 IsDevicePathMultiInstance (
277 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
278 )
279 {
280 return mDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);
281 }
282
283 /**
284 Retrieves the device path protocol from a handle.
285
286 This function returns the device path protocol from the handle specified by Handle. If Handle is
287 NULL or Handle does not contain a device path protocol, then NULL is returned.
288
289 @param Handle The handle from which to retrieve the device path protocol.
290
291 @return The device path protocol from the handle specified by Handle.
292
293 **/
294 EFI_DEVICE_PATH_PROTOCOL *
295 EFIAPI
296 DevicePathFromHandle (
297 IN EFI_HANDLE Handle
298 )
299 {
300 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
301 EFI_STATUS Status;
302
303 Status = gBS->HandleProtocol (
304 Handle,
305 &gEfiDevicePathProtocolGuid,
306 (VOID *) &DevicePath
307 );
308 if (EFI_ERROR (Status)) {
309 DevicePath = NULL;
310 }
311 return DevicePath;
312 }
313
314 /**
315 Allocates a device path for a file and appends it to an existing device path.
316
317 If Device is a valid device handle that contains a device path protocol, then a device path for
318 the file specified by FileName is allocated and appended to the device path associated with the
319 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
320 that does not support the device path protocol, then a device path containing a single device
321 path node for the file specified by FileName is allocated and returned.
322 If FileName is NULL, then ASSERT().
323
324 @param Device A pointer to a device handle. This parameter is optional and
325 may be NULL.
326 @param FileName A pointer to a Null-terminated Unicode string.
327
328 @return The allocated device path.
329
330 **/
331 EFI_DEVICE_PATH_PROTOCOL *
332 EFIAPI
333 FileDevicePath (
334 IN EFI_HANDLE Device, OPTIONAL
335 IN CONST CHAR16 *FileName
336 )
337 {
338 UINTN Size;
339 FILEPATH_DEVICE_PATH *FilePath;
340 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
341 EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;
342
343 DevicePath = NULL;
344
345 Size = StrSize (FileName);
346 FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + EFI_END_DEVICE_PATH_LENGTH);
347 if (FileDevicePath != NULL) {
348 FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;
349 FilePath->Header.Type = MEDIA_DEVICE_PATH;
350 FilePath->Header.SubType = MEDIA_FILEPATH_DP;
351 CopyMem (&FilePath->PathName, FileName, Size);
352 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);
353 SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));
354
355 if (Device != NULL) {
356 DevicePath = DevicePathFromHandle (Device);
357 }
358
359 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);
360 FreePool (FileDevicePath);
361 }
362
363 return DevicePath;
364 }
365