]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLib / UefiDevicePathLibOptionalDevicePathProtocol.c
CommitLineData
4d0a30a4
RN
1/** @file\r
2 Device Path services. The thing to remember is device paths are built out of\r
3 nodes. The device path is terminated by an end node that is length\r
4 sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)\r
5 all over this file.\r
6\r
7 The only place where multi-instance device paths are supported is in\r
8 environment varibles. Multi-instance device paths should never be placed\r
9 on a Handle.\r
10\r
9095d37b 11 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>\r
9344f092 12 SPDX-License-Identifier: BSD-2-Clause-Patent\r
4d0a30a4
RN
13\r
14**/\r
15\r
4d0a30a4
RN
16#include "UefiDevicePathLib.h"\r
17\r
2f88bd3a
MK
18GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mDevicePathLibDevicePathUtilities = NULL;\r
19GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *mDevicePathLibDevicePathToText = NULL;\r
20GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *mDevicePathLibDevicePathFromText = NULL;\r
4d0a30a4
RN
21\r
22/**\r
23 The constructor function caches the pointer to DevicePathUtilites protocol,\r
24 DevicePathToText protocol and DevicePathFromText protocol.\r
9095d37b 25\r
4d0a30a4
RN
26 The constructor function locates these three protocols from protocol database.\r
27 It will caches the pointer to local protocol instance if that operation fails\r
9095d37b 28 and it will always return EFI_SUCCESS.\r
4d0a30a4
RN
29\r
30 @param ImageHandle The firmware allocated handle for the EFI image.\r
31 @param SystemTable A pointer to the EFI System Table.\r
9095d37b 32\r
4d0a30a4
RN
33 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
34\r
35**/\r
36EFI_STATUS\r
37EFIAPI\r
38UefiDevicePathLibOptionalDevicePathProtocolConstructor (\r
2f88bd3a
MK
39 IN EFI_HANDLE ImageHandle,\r
40 IN EFI_SYSTEM_TABLE *SystemTable\r
4d0a30a4
RN
41 )\r
42{\r
2f88bd3a 43 EFI_STATUS Status;\r
4d0a30a4
RN
44\r
45 Status = gBS->LocateProtocol (\r
46 &gEfiDevicePathUtilitiesProtocolGuid,\r
47 NULL,\r
2f88bd3a 48 (VOID **)&mDevicePathLibDevicePathUtilities\r
4d0a30a4
RN
49 );\r
50 ASSERT_EFI_ERROR (Status);\r
51 ASSERT (mDevicePathLibDevicePathUtilities != NULL);\r
52 return Status;\r
53}\r
54\r
55/**\r
56 Returns the size of a device path in bytes.\r
57\r
9095d37b 58 This function returns the size, in bytes, of the device path data structure\r
4d0a30a4
RN
59 specified by DevicePath including the end of device path node.\r
60 If DevicePath is NULL or invalid, then 0 is returned.\r
61\r
62 @param DevicePath A pointer to a device path data structure.\r
63\r
64 @retval 0 If DevicePath is NULL or invalid.\r
65 @retval Others The size of a device path in bytes.\r
66\r
67**/\r
68UINTN\r
69EFIAPI\r
70GetDevicePathSize (\r
71 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
72 )\r
73{\r
74 if (mDevicePathLibDevicePathUtilities != NULL) {\r
75 return mDevicePathLibDevicePathUtilities->GetDevicePathSize (DevicePath);\r
76 } else {\r
77 return UefiDevicePathLibGetDevicePathSize (DevicePath);\r
78 }\r
79}\r
80\r
81/**\r
82 Creates a new copy of an existing device path.\r
83\r
9095d37b
LG
84 This function allocates space for a new copy of the device path specified by DevicePath.\r
85 If DevicePath is NULL, then NULL is returned. If the memory is successfully\r
86 allocated, then the contents of DevicePath are copied to the newly allocated\r
87 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.\r
88 The memory for the new device path is allocated from EFI boot services memory.\r
89 It is the responsibility of the caller to free the memory allocated.\r
90\r
4d0a30a4
RN
91 @param DevicePath A pointer to a device path data structure.\r
92\r
93 @retval NULL DevicePath is NULL or invalid.\r
94 @retval Others A pointer to the duplicated device path.\r
9095d37b 95\r
4d0a30a4
RN
96**/\r
97EFI_DEVICE_PATH_PROTOCOL *\r
98EFIAPI\r
99DuplicateDevicePath (\r
100 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
101 )\r
102{\r
103 if (mDevicePathLibDevicePathUtilities != NULL) {\r
104 return mDevicePathLibDevicePathUtilities->DuplicateDevicePath (DevicePath);\r
105 } else {\r
106 return UefiDevicePathLibDuplicateDevicePath (DevicePath);\r
107 }\r
108}\r
109\r
110/**\r
111 Creates a new device path by appending a second device path to a first device path.\r
112\r
9095d37b
LG
113 This function creates a new device path by appending a copy of SecondDevicePath\r
114 to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path\r
115 device node from SecondDevicePath is retained. The newly created device path is\r
116 returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of\r
117 SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,\r
118 and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and\r
119 SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.\r
120\r
4d0a30a4 121 If there is not enough memory for the newly allocated buffer, then NULL is returned.\r
9095d37b 122 The memory for the new device path is allocated from EFI boot services memory.\r
4d0a30a4
RN
123 It is the responsibility of the caller to free the memory allocated.\r
124\r
125 @param FirstDevicePath A pointer to a device path data structure.\r
126 @param SecondDevicePath A pointer to a device path data structure.\r
9095d37b 127\r
4d0a30a4
RN
128 @retval NULL If there is not enough memory for the newly allocated buffer.\r
129 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.\r
130 @retval Others A pointer to the new device path if success.\r
131 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.\r
132\r
133**/\r
134EFI_DEVICE_PATH_PROTOCOL *\r
135EFIAPI\r
136AppendDevicePath (\r
d0e2f823 137 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath OPTIONAL,\r
4d0a30a4
RN
138 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r
139 )\r
140{\r
141 if (mDevicePathLibDevicePathUtilities != NULL) {\r
142 return mDevicePathLibDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);\r
143 } else {\r
144 return UefiDevicePathLibAppendDevicePath (FirstDevicePath, SecondDevicePath);\r
145 }\r
146}\r
147\r
148/**\r
149 Creates a new path by appending the device node to the device path.\r
150\r
9095d37b
LG
151 This function creates a new device path by appending a copy of the device node\r
152 specified by DevicePathNode to a copy of the device path specified by DevicePath\r
153 in an allocated buffer. The end-of-device-path device node is moved after the\r
4d0a30a4
RN
154 end of the appended device node.\r
155 If DevicePathNode is NULL then a copy of DevicePath is returned.\r
9095d37b 156 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device\r
4d0a30a4 157 path device node is returned.\r
9095d37b 158 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path\r
4d0a30a4 159 device node is returned.\r
9095d37b
LG
160 If there is not enough memory to allocate space for the new device path, then\r
161 NULL is returned.\r
162 The memory is allocated from EFI boot services memory. It is the responsibility\r
4d0a30a4
RN
163 of the caller to free the memory allocated.\r
164\r
165 @param DevicePath A pointer to a device path data structure.\r
166 @param DevicePathNode A pointer to a single device path node.\r
167\r
168 @retval NULL If there is not enough memory for the new device path.\r
169 @retval Others A pointer to the new device path if success.\r
9095d37b 170 A copy of DevicePathNode followed by an end-of-device-path node\r
4d0a30a4 171 if both FirstDevicePath and SecondDevicePath are NULL.\r
9095d37b 172 A copy of an end-of-device-path node if both FirstDevicePath\r
4d0a30a4
RN
173 and SecondDevicePath are NULL.\r
174\r
175**/\r
176EFI_DEVICE_PATH_PROTOCOL *\r
177EFIAPI\r
178AppendDevicePathNode (\r
d0e2f823 179 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,\r
4d0a30a4
RN
180 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
181 )\r
182{\r
183 if (mDevicePathLibDevicePathUtilities != NULL) {\r
184 return mDevicePathLibDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);\r
185 } else {\r
186 return UefiDevicePathLibAppendDevicePathNode (DevicePath, DevicePathNode);\r
187 }\r
188}\r
189\r
190/**\r
191 Creates a new device path by appending the specified device path instance to the specified device\r
192 path.\r
9095d37b
LG
193\r
194 This function creates a new device path by appending a copy of the device path\r
195 instance specified by DevicePathInstance to a copy of the device path specified\r
4d0a30a4 196 by DevicePath in a allocated buffer.\r
9095d37b
LG
197 The end-of-device-path device node is moved after the end of the appended device\r
198 path instance and a new end-of-device-path-instance node is inserted between.\r
4d0a30a4
RN
199 If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
200 If DevicePathInstance is NULL, then NULL is returned.\r
201 If DevicePath or DevicePathInstance is invalid, then NULL is returned.\r
9095d37b
LG
202 If there is not enough memory to allocate space for the new device path, then\r
203 NULL is returned.\r
204 The memory is allocated from EFI boot services memory. It is the responsibility\r
4d0a30a4 205 of the caller to free the memory allocated.\r
9095d37b 206\r
4d0a30a4
RN
207 @param DevicePath A pointer to a device path data structure.\r
208 @param DevicePathInstance A pointer to a device path instance.\r
209\r
210 @return A pointer to the new device path.\r
211\r
212**/\r
213EFI_DEVICE_PATH_PROTOCOL *\r
214EFIAPI\r
215AppendDevicePathInstance (\r
d0e2f823 216 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,\r
4d0a30a4
RN
217 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
218 )\r
219{\r
220 if (mDevicePathLibDevicePathUtilities != NULL) {\r
221 return mDevicePathLibDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);\r
222 } else {\r
223 return UefiDevicePathLibAppendDevicePathInstance (DevicePath, DevicePathInstance);\r
224 }\r
225}\r
226\r
227/**\r
228 Creates a copy of the current device path instance and returns a pointer to the next device path\r
229 instance.\r
230\r
9095d37b
LG
231 This function creates a copy of the current device path instance. It also updates\r
232 DevicePath to point to the next device path instance in the device path (or NULL\r
4d0a30a4
RN
233 if no more) and updates Size to hold the size of the device path instance copy.\r
234 If DevicePath is NULL, then NULL is returned.\r
235 If DevicePath points to a invalid device path, then NULL is returned.\r
9095d37b
LG
236 If there is not enough memory to allocate space for the new device path, then\r
237 NULL is returned.\r
238 The memory is allocated from EFI boot services memory. It is the responsibility\r
4d0a30a4
RN
239 of the caller to free the memory allocated.\r
240 If Size is NULL, then ASSERT().\r
9095d37b
LG
241\r
242 @param DevicePath On input, this holds the pointer to the current\r
243 device path instance. On output, this holds\r
244 the pointer to the next device path instance\r
4d0a30a4 245 or NULL if there are no more device path\r
9095d37b 246 instances in the device path pointer to a\r
4d0a30a4 247 device path data structure.\r
9095d37b
LG
248 @param Size On output, this holds the size of the device\r
249 path instance, in bytes or zero, if DevicePath\r
4d0a30a4
RN
250 is NULL.\r
251\r
252 @return A pointer to the current device path instance.\r
253\r
254**/\r
255EFI_DEVICE_PATH_PROTOCOL *\r
256EFIAPI\r
257GetNextDevicePathInstance (\r
2f88bd3a
MK
258 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
259 OUT UINTN *Size\r
4d0a30a4
RN
260 )\r
261{\r
262 if (mDevicePathLibDevicePathUtilities != NULL) {\r
263 return mDevicePathLibDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);\r
264 } else {\r
265 return UefiDevicePathLibGetNextDevicePathInstance (DevicePath, Size);\r
266 }\r
267}\r
268\r
269/**\r
270 Creates a device node.\r
271\r
9095d37b
LG
272 This function creates a new device node in a newly allocated buffer of size\r
273 NodeLength and initializes the device path node header with NodeType and NodeSubType.\r
4d0a30a4 274 The new device path node is returned.\r
9095d37b
LG
275 If NodeLength is smaller than a device path header, then NULL is returned.\r
276 If there is not enough memory to allocate space for the new device path, then\r
277 NULL is returned.\r
278 The memory is allocated from EFI boot services memory. It is the responsibility\r
4d0a30a4
RN
279 of the caller to free the memory allocated.\r
280\r
281 @param NodeType The device node type for the new device node.\r
282 @param NodeSubType The device node sub-type for the new device node.\r
283 @param NodeLength The length of the new device node.\r
284\r
285 @return The new device path.\r
286\r
287**/\r
288EFI_DEVICE_PATH_PROTOCOL *\r
289EFIAPI\r
290CreateDeviceNode (\r
2f88bd3a
MK
291 IN UINT8 NodeType,\r
292 IN UINT8 NodeSubType,\r
293 IN UINT16 NodeLength\r
4d0a30a4
RN
294 )\r
295{\r
296 if (mDevicePathLibDevicePathUtilities != NULL) {\r
297 return mDevicePathLibDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
298 } else {\r
299 return UefiDevicePathLibCreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
300 }\r
301}\r
302\r
303/**\r
304 Determines if a device path is single or multi-instance.\r
305\r
306 This function returns TRUE if the device path specified by DevicePath is\r
307 multi-instance.\r
308 Otherwise, FALSE is returned.\r
309 If DevicePath is NULL or invalid, then FALSE is returned.\r
310\r
311 @param DevicePath A pointer to a device path data structure.\r
312\r
313 @retval TRUE DevicePath is multi-instance.\r
9095d37b 314 @retval FALSE DevicePath is not multi-instance, or DevicePath\r
4d0a30a4
RN
315 is NULL or invalid.\r
316\r
317**/\r
318BOOLEAN\r
319EFIAPI\r
320IsDevicePathMultiInstance (\r
321 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
322 )\r
323{\r
324 if (mDevicePathLibDevicePathUtilities != NULL) {\r
325 return mDevicePathLibDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);\r
326 } else {\r
327 return UefiDevicePathLibIsDevicePathMultiInstance (DevicePath);\r
328 }\r
329}\r
330\r
331/**\r
332 Locate and return the protocol instance identified by the ProtocolGuid.\r
333\r
334 @param ProtocolGuid The GUID of the protocol.\r
335\r
336 @return A pointer to the protocol instance or NULL when absent.\r
337**/\r
338VOID *\r
339UefiDevicePathLibLocateProtocol (\r
2f88bd3a 340 EFI_GUID *ProtocolGuid\r
4d0a30a4
RN
341 )\r
342{\r
2f88bd3a
MK
343 EFI_STATUS Status;\r
344 VOID *Protocol;\r
345\r
4d0a30a4
RN
346 Status = gBS->LocateProtocol (\r
347 ProtocolGuid,\r
348 NULL,\r
2f88bd3a 349 (VOID **)&Protocol\r
4d0a30a4
RN
350 );\r
351 if (EFI_ERROR (Status)) {\r
352 return NULL;\r
353 } else {\r
354 return Protocol;\r
355 }\r
356}\r
357\r
358/**\r
359 Converts a device node to its string representation.\r
360\r
361 @param DeviceNode A Pointer to the device node to be converted.\r
362 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation\r
363 of the display node is used, where applicable. If DisplayOnly\r
364 is FALSE, then the longer text representation of the display node\r
365 is used.\r
366 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text\r
367 representation for a device node can be used, where applicable.\r
368\r
369 @return A pointer to the allocated text representation of the device node or NULL if DeviceNode\r
370 is NULL or there was insufficient memory.\r
371\r
372**/\r
373CHAR16 *\r
374EFIAPI\r
375ConvertDeviceNodeToText (\r
376 IN CONST EFI_DEVICE_PATH_PROTOCOL *DeviceNode,\r
377 IN BOOLEAN DisplayOnly,\r
378 IN BOOLEAN AllowShortcuts\r
379 )\r
380{\r
381 if (mDevicePathLibDevicePathToText == NULL) {\r
382 mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);\r
383 }\r
2f88bd3a 384\r
4d0a30a4
RN
385 if (mDevicePathLibDevicePathToText != NULL) {\r
386 return mDevicePathLibDevicePathToText->ConvertDeviceNodeToText (DeviceNode, DisplayOnly, AllowShortcuts);\r
387 }\r
388\r
389 return UefiDevicePathLibConvertDeviceNodeToText (DeviceNode, DisplayOnly, AllowShortcuts);\r
390}\r
391\r
392/**\r
393 Converts a device path to its text representation.\r
394\r
395 @param DevicePath A Pointer to the device to be converted.\r
396 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation\r
397 of the display node is used, where applicable. If DisplayOnly\r
398 is FALSE, then the longer text representation of the display node\r
399 is used.\r
400 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text\r
401 representation for a device node can be used, where applicable.\r
402\r
403 @return A pointer to the allocated text representation of the device path or\r
404 NULL if DeviceNode is NULL or there was insufficient memory.\r
405\r
406**/\r
407CHAR16 *\r
408EFIAPI\r
409ConvertDevicePathToText (\r
2f88bd3a
MK
410 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
411 IN BOOLEAN DisplayOnly,\r
412 IN BOOLEAN AllowShortcuts\r
4d0a30a4
RN
413 )\r
414{\r
415 if (mDevicePathLibDevicePathToText == NULL) {\r
416 mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);\r
417 }\r
2f88bd3a 418\r
4d0a30a4
RN
419 if (mDevicePathLibDevicePathToText != NULL) {\r
420 return mDevicePathLibDevicePathToText->ConvertDevicePathToText (DevicePath, DisplayOnly, AllowShortcuts);\r
421 }\r
422\r
423 return UefiDevicePathLibConvertDevicePathToText (DevicePath, DisplayOnly, AllowShortcuts);\r
424}\r
425\r
426/**\r
427 Convert text to the binary representation of a device node.\r
428\r
429 @param TextDeviceNode TextDeviceNode points to the text representation of a device\r
430 node. Conversion starts with the first character and continues\r
431 until the first non-device node character.\r
432\r
433 @return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was\r
434 insufficient memory or text unsupported.\r
435\r
436**/\r
437EFI_DEVICE_PATH_PROTOCOL *\r
438EFIAPI\r
439ConvertTextToDeviceNode (\r
2f88bd3a 440 IN CONST CHAR16 *TextDeviceNode\r
4d0a30a4
RN
441 )\r
442{\r
443 if (mDevicePathLibDevicePathFromText == NULL) {\r
444 mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);\r
445 }\r
2f88bd3a 446\r
4d0a30a4
RN
447 if (mDevicePathLibDevicePathFromText != NULL) {\r
448 return mDevicePathLibDevicePathFromText->ConvertTextToDeviceNode (TextDeviceNode);\r
449 }\r
450\r
451 return UefiDevicePathLibConvertTextToDeviceNode (TextDeviceNode);\r
452}\r
453\r
454/**\r
455 Convert text to the binary representation of a device path.\r
456\r
457\r
458 @param TextDevicePath TextDevicePath points to the text representation of a device\r
459 path. Conversion starts with the first character and continues\r
460 until the first non-device node character.\r
461\r
462 @return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or\r
463 there was insufficient memory.\r
464\r
465**/\r
466EFI_DEVICE_PATH_PROTOCOL *\r
467EFIAPI\r
468ConvertTextToDevicePath (\r
2f88bd3a 469 IN CONST CHAR16 *TextDevicePath\r
4d0a30a4
RN
470 )\r
471{\r
472 if (mDevicePathLibDevicePathFromText == NULL) {\r
473 mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);\r
474 }\r
2f88bd3a 475\r
4d0a30a4
RN
476 if (mDevicePathLibDevicePathFromText != NULL) {\r
477 return mDevicePathLibDevicePathFromText->ConvertTextToDevicePath (TextDevicePath);\r
478 }\r
479\r
480 return UefiDevicePathLibConvertTextToDevicePath (TextDevicePath);\r
481}\r