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