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