MdePkg UefiDevicePathLib: Validate before touch input buffer.
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLib / UefiDevicePathLib.h
CommitLineData
13d40edd 1/** @file\r
4d0a30a4 2 Definition for Device Path library.\r
95276127 3\r
7795c8f9 4Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 5This program and the accompanying materials\r
13d40edd 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
95276127 9\r
13d40edd 10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
95276127 12\r
13d40edd 13**/\r
95276127 14\r
4d0a30a4
RN
15#ifndef _UEFI_DEVICE_PATH_LIB_H_\r
16#define _UEFI_DEVICE_PATH_LIB_H_\r
60c93673 17#include <Uefi.h>\r
859b72fa
A
18#include <Protocol/DevicePathUtilities.h>\r
19#include <Protocol/DebugPort.h>\r
20#include <Protocol/DevicePathToText.h>\r
21#include <Protocol/DevicePathFromText.h>\r
22#include <Guid/PcAnsi.h>\r
23#include <Library/DebugLib.h>\r
24#include <Library/PrintLib.h>\r
859b72fa
A
25#include <Library/BaseLib.h>\r
26#include <Library/BaseMemoryLib.h>\r
27#include <Library/MemoryAllocationLib.h>\r
28#include <Library/UefiBootServicesTableLib.h>\r
29#include <Library/DevicePathLib.h>\r
30#include <Library/PcdLib.h>\r
7795c8f9 31#include <IndustryStandard/Bluetooth.h>\r
95276127 32\r
95276127 33#define IS_COMMA(a) ((a) == L',')\r
34#define IS_HYPHEN(a) ((a) == L'-')\r
35#define IS_DOT(a) ((a) == L'.')\r
36#define IS_LEFT_PARENTH(a) ((a) == L'(')\r
37#define IS_RIGHT_PARENTH(a) ((a) == L')')\r
38#define IS_SLASH(a) ((a) == L'/')\r
39#define IS_NULL(a) ((a) == L'\0')\r
40\r
95276127 41\r
95276127 42//\r
43// Private Data structure\r
44//\r
45typedef struct {\r
46 CHAR16 *Str;\r
4d0a30a4 47 UINTN Count;\r
e9b3cd55 48 UINTN Capacity;\r
95276127 49} POOL_PRINT;\r
50\r
572f5d8a 51typedef\r
52EFI_DEVICE_PATH_PROTOCOL *\r
4d0a30a4
RN
53(*DEVICE_PATH_FROM_TEXT) (\r
54 IN CHAR16 *Str\r
572f5d8a 55 );\r
56\r
1ccdbf2a 57typedef\r
58VOID\r
4d0a30a4 59(*DEVICE_PATH_TO_TEXT) (\r
1ccdbf2a 60 IN OUT POOL_PRINT *Str,\r
4d0a30a4 61 IN VOID *DevicePath,\r
1ccdbf2a 62 IN BOOLEAN DisplayOnly,\r
63 IN BOOLEAN AllowShortcuts\r
64 );\r
65\r
95276127 66typedef struct {\r
4d0a30a4
RN
67 UINT8 Type;\r
68 UINT8 SubType;\r
69 DEVICE_PATH_TO_TEXT Function;\r
95276127 70} DEVICE_PATH_TO_TEXT_TABLE;\r
71\r
5d6a5aee
RN
72typedef struct {\r
73 UINT8 Type;\r
74 CHAR16 *Text;\r
75} DEVICE_PATH_TO_TEXT_GENERIC_TABLE;\r
76\r
95276127 77typedef struct {\r
78 CHAR16 *DevicePathNodeText;\r
4d0a30a4 79 DEVICE_PATH_FROM_TEXT Function;\r
95276127 80} DEVICE_PATH_FROM_TEXT_TABLE;\r
81\r
82typedef struct {\r
83 BOOLEAN ClassExist;\r
84 UINT8 Class;\r
85 BOOLEAN SubClassExist;\r
86 UINT8 SubClass;\r
87} USB_CLASS_TEXT;\r
88\r
89#define USB_CLASS_AUDIO 1\r
90#define USB_CLASS_CDCCONTROL 2\r
91#define USB_CLASS_HID 3\r
92#define USB_CLASS_IMAGE 6\r
93#define USB_CLASS_PRINTER 7\r
94#define USB_CLASS_MASS_STORAGE 8\r
95#define USB_CLASS_HUB 9\r
96#define USB_CLASS_CDCDATA 10\r
97#define USB_CLASS_SMART_CARD 11\r
98#define USB_CLASS_VIDEO 14\r
99#define USB_CLASS_DIAGNOSTIC 220\r
100#define USB_CLASS_WIRELESS 224\r
101\r
102#define USB_CLASS_RESERVE 254\r
103#define USB_SUBCLASS_FW_UPDATE 1\r
104#define USB_SUBCLASS_IRDA_BRIDGE 2\r
105#define USB_SUBCLASS_TEST 3\r
106\r
052019e1 107#define RFC_1700_UDP_PROTOCOL 17\r
108#define RFC_1700_TCP_PROTOCOL 6\r
109\r
cf40f28a 110#pragma pack(1)\r
111\r
95276127 112typedef struct {\r
113 EFI_DEVICE_PATH_PROTOCOL Header;\r
114 EFI_GUID Guid;\r
115 UINT8 VendorDefinedData[1];\r
116} VENDOR_DEFINED_HARDWARE_DEVICE_PATH;\r
117\r
118typedef struct {\r
119 EFI_DEVICE_PATH_PROTOCOL Header;\r
120 EFI_GUID Guid;\r
121 UINT8 VendorDefinedData[1];\r
122} VENDOR_DEFINED_MESSAGING_DEVICE_PATH;\r
123\r
124typedef struct {\r
125 EFI_DEVICE_PATH_PROTOCOL Header;\r
126 EFI_GUID Guid;\r
127 UINT8 VendorDefinedData[1];\r
128} VENDOR_DEFINED_MEDIA_DEVICE_PATH;\r
129\r
130typedef struct {\r
131 EFI_DEVICE_PATH_PROTOCOL Header;\r
1ccdbf2a 132 UINT32 Hid;\r
133 UINT32 Uid;\r
134 UINT32 Cid;\r
95276127 135 CHAR8 HidUidCidStr[3];\r
136} ACPI_EXTENDED_HID_DEVICE_PATH_WITH_STR;\r
137\r
138typedef struct {\r
139 EFI_DEVICE_PATH_PROTOCOL Header;\r
140 UINT16 NetworkProtocol;\r
141 UINT16 LoginOption;\r
95276127 142 UINT64 Lun;\r
cf40f28a 143 UINT16 TargetPortalGroupTag;\r
184f7d83 144 CHAR8 TargetName[1];\r
95276127 145} ISCSI_DEVICE_PATH_WITH_NAME;\r
146\r
147typedef struct {\r
148 EFI_DEVICE_PATH_PROTOCOL Header;\r
149 EFI_GUID Guid;\r
150 UINT8 VendorDefinedData[1];\r
151} VENDOR_DEVICE_PATH_WITH_DATA;\r
152\r
cf40f28a 153#pragma pack()\r
154\r
572f5d8a 155/**\r
4d0a30a4 156 Returns the size of a device path in bytes.\r
572f5d8a 157\r
4d0a30a4
RN
158 This function returns the size, in bytes, of the device path data structure \r
159 specified by DevicePath including the end of device path node.\r
160 If DevicePath is NULL or invalid, then 0 is returned.\r
572f5d8a 161\r
4d0a30a4
RN
162 @param DevicePath A pointer to a device path data structure.\r
163\r
164 @retval 0 If DevicePath is NULL or invalid.\r
165 @retval Others The size of a device path in bytes.\r
572f5d8a 166\r
167**/\r
4d0a30a4 168UINTN\r
572f5d8a 169EFIAPI\r
4d0a30a4
RN
170UefiDevicePathLibGetDevicePathSize (\r
171 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
572f5d8a 172 );\r
95276127 173\r
572f5d8a 174/**\r
4d0a30a4
RN
175 Creates a new copy of an existing device path.\r
176\r
177 This function allocates space for a new copy of the device path specified by DevicePath. \r
178 If DevicePath is NULL, then NULL is returned. If the memory is successfully \r
179 allocated, then the contents of DevicePath are copied to the newly allocated \r
180 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned. \r
181 The memory for the new device path is allocated from EFI boot services memory. \r
182 It is the responsibility of the caller to free the memory allocated. \r
183 \r
184 @param DevicePath A pointer to a device path data structure.\r
185\r
186 @retval NULL DevicePath is NULL or invalid.\r
187 @retval Others A pointer to the duplicated device path.\r
188 \r
572f5d8a 189**/\r
4d0a30a4 190EFI_DEVICE_PATH_PROTOCOL *\r
572f5d8a 191EFIAPI\r
4d0a30a4
RN
192UefiDevicePathLibDuplicateDevicePath (\r
193 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
572f5d8a 194 );\r
95276127 195\r
572f5d8a 196/**\r
4d0a30a4 197 Creates a new device path by appending a second device path to a first device path.\r
95276127 198\r
4d0a30a4
RN
199 This function creates a new device path by appending a copy of SecondDevicePath \r
200 to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path \r
201 device node from SecondDevicePath is retained. The newly created device path is \r
202 returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of \r
203 SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored, \r
204 and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and \r
205 SecondDevicePath are NULL, then a copy of an end-of-device-path is returned. \r
206 \r
207 If there is not enough memory for the newly allocated buffer, then NULL is returned.\r
208 The memory for the new device path is allocated from EFI boot services memory. \r
209 It is the responsibility of the caller to free the memory allocated.\r
95276127 210\r
4d0a30a4
RN
211 @param FirstDevicePath A pointer to a device path data structure.\r
212 @param SecondDevicePath A pointer to a device path data structure.\r
213 \r
214 @retval NULL If there is not enough memory for the newly allocated buffer.\r
215 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.\r
216 @retval Others A pointer to the new device path if success.\r
217 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.\r
95276127 218\r
572f5d8a 219**/\r
95276127 220EFI_DEVICE_PATH_PROTOCOL *\r
572f5d8a 221EFIAPI\r
4d0a30a4
RN
222UefiDevicePathLibAppendDevicePath (\r
223 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL\r
224 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r
572f5d8a 225 );\r
95276127 226\r
572f5d8a 227/**\r
4d0a30a4 228 Creates a new path by appending the device node to the device path.\r
95276127 229\r
4d0a30a4
RN
230 This function creates a new device path by appending a copy of the device node \r
231 specified by DevicePathNode to a copy of the device path specified by DevicePath \r
232 in an allocated buffer. The end-of-device-path device node is moved after the \r
233 end of the appended device node.\r
234 If DevicePathNode is NULL then a copy of DevicePath is returned.\r
235 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device \r
236 path device node is returned.\r
237 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path \r
238 device node is returned.\r
239 If there is not enough memory to allocate space for the new device path, then \r
240 NULL is returned. \r
241 The memory is allocated from EFI boot services memory. It is the responsibility \r
242 of the caller to free the memory allocated.\r
95276127 243\r
4d0a30a4
RN
244 @param DevicePath A pointer to a device path data structure.\r
245 @param DevicePathNode A pointer to a single device path node.\r
95276127 246\r
4d0a30a4
RN
247 @retval NULL If there is not enough memory for the new device path.\r
248 @retval Others A pointer to the new device path if success.\r
249 A copy of DevicePathNode followed by an end-of-device-path node \r
250 if both FirstDevicePath and SecondDevicePath are NULL.\r
251 A copy of an end-of-device-path node if both FirstDevicePath \r
252 and SecondDevicePath are NULL.\r
95276127 253\r
572f5d8a 254**/\r
95276127 255EFI_DEVICE_PATH_PROTOCOL *\r
572f5d8a 256EFIAPI\r
4d0a30a4
RN
257UefiDevicePathLibAppendDevicePathNode (\r
258 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
259 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
572f5d8a 260 );\r
95276127 261\r
572f5d8a 262/**\r
4d0a30a4
RN
263 Creates a new device path by appending the specified device path instance to the specified device\r
264 path.\r
265 \r
266 This function creates a new device path by appending a copy of the device path \r
267 instance specified by DevicePathInstance to a copy of the device path specified \r
268 by DevicePath in a allocated buffer.\r
269 The end-of-device-path device node is moved after the end of the appended device \r
270 path instance and a new end-of-device-path-instance node is inserted between. \r
271 If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
272 If DevicePathInstance is NULL, then NULL is returned.\r
273 If DevicePath or DevicePathInstance is invalid, then NULL is returned.\r
274 If there is not enough memory to allocate space for the new device path, then \r
275 NULL is returned. \r
276 The memory is allocated from EFI boot services memory. It is the responsibility \r
277 of the caller to free the memory allocated.\r
278 \r
572f5d8a 279 @param DevicePath A pointer to a device path data structure.\r
4d0a30a4 280 @param DevicePathInstance A pointer to a device path instance.\r
95276127 281\r
4d0a30a4 282 @return A pointer to the new device path.\r
95276127 283\r
572f5d8a 284**/\r
4d0a30a4 285EFI_DEVICE_PATH_PROTOCOL *\r
572f5d8a 286EFIAPI\r
4d0a30a4
RN
287UefiDevicePathLibAppendDevicePathInstance (\r
288 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
289 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
572f5d8a 290 );\r
95276127 291\r
572f5d8a 292/**\r
4d0a30a4
RN
293 Creates a copy of the current device path instance and returns a pointer to the next device path\r
294 instance.\r
95276127 295\r
4d0a30a4
RN
296 This function creates a copy of the current device path instance. It also updates \r
297 DevicePath to point to the next device path instance in the device path (or NULL \r
298 if no more) and updates Size to hold the size of the device path instance copy.\r
299 If DevicePath is NULL, then NULL is returned.\r
300 If DevicePath points to a invalid device path, then NULL is returned.\r
301 If there is not enough memory to allocate space for the new device path, then \r
302 NULL is returned. \r
303 The memory is allocated from EFI boot services memory. It is the responsibility \r
304 of the caller to free the memory allocated.\r
305 If Size is NULL, then ASSERT().\r
306 \r
307 @param DevicePath On input, this holds the pointer to the current \r
308 device path instance. On output, this holds \r
309 the pointer to the next device path instance \r
310 or NULL if there are no more device path\r
311 instances in the device path pointer to a \r
312 device path data structure.\r
313 @param Size On output, this holds the size of the device \r
314 path instance, in bytes or zero, if DevicePath \r
315 is NULL.\r
95276127 316\r
4d0a30a4 317 @return A pointer to the current device path instance.\r
95276127 318\r
572f5d8a 319**/\r
95276127 320EFI_DEVICE_PATH_PROTOCOL *\r
572f5d8a 321EFIAPI\r
4d0a30a4
RN
322UefiDevicePathLibGetNextDevicePathInstance (\r
323 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
324 OUT UINTN *Size\r
572f5d8a 325 );\r
95276127 326\r
572f5d8a 327/**\r
4d0a30a4 328 Creates a device node.\r
95276127 329\r
4d0a30a4
RN
330 This function creates a new device node in a newly allocated buffer of size \r
331 NodeLength and initializes the device path node header with NodeType and NodeSubType. \r
332 The new device path node is returned.\r
333 If NodeLength is smaller than a device path header, then NULL is returned. \r
334 If there is not enough memory to allocate space for the new device path, then \r
335 NULL is returned. \r
336 The memory is allocated from EFI boot services memory. It is the responsibility \r
337 of the caller to free the memory allocated.\r
95276127 338\r
4d0a30a4
RN
339 @param NodeType The device node type for the new device node.\r
340 @param NodeSubType The device node sub-type for the new device node.\r
341 @param NodeLength The length of the new device node.\r
95276127 342\r
4d0a30a4 343 @return The new device path.\r
95276127 344\r
572f5d8a 345**/\r
95276127 346EFI_DEVICE_PATH_PROTOCOL *\r
572f5d8a 347EFIAPI\r
4d0a30a4
RN
348UefiDevicePathLibCreateDeviceNode (\r
349 IN UINT8 NodeType,\r
350 IN UINT8 NodeSubType,\r
351 IN UINT16 NodeLength\r
572f5d8a 352 );\r
353\r
354/**\r
4d0a30a4 355 Determines if a device path is single or multi-instance.\r
572f5d8a 356\r
4d0a30a4
RN
357 This function returns TRUE if the device path specified by DevicePath is\r
358 multi-instance.\r
359 Otherwise, FALSE is returned.\r
360 If DevicePath is NULL or invalid, then FALSE is returned.\r
572f5d8a 361\r
362 @param DevicePath A pointer to a device path data structure.\r
572f5d8a 363\r
4d0a30a4
RN
364 @retval TRUE DevicePath is multi-instance.\r
365 @retval FALSE DevicePath is not multi-instance, or DevicePath \r
366 is NULL or invalid.\r
95276127 367\r
572f5d8a 368**/\r
4d0a30a4 369BOOLEAN\r
572f5d8a 370EFIAPI\r
4d0a30a4
RN
371UefiDevicePathLibIsDevicePathMultiInstance (\r
372 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
572f5d8a 373 );\r
95276127 374\r
95276127 375\r
4d0a30a4
RN
376/**\r
377 Converts a device path to its text representation.\r
95276127 378\r
4d0a30a4
RN
379 @param DevicePath A Pointer to the device to be converted.\r
380 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation\r
381 of the display node is used, where applicable. If DisplayOnly\r
382 is FALSE, then the longer text representation of the display node\r
383 is used.\r
384 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text\r
385 representation for a device node can be used, where applicable.\r
95276127 386\r
4d0a30a4
RN
387 @return A pointer to the allocated text representation of the device path or\r
388 NULL if DeviceNode is NULL or there was insufficient memory.\r
95276127 389\r
572f5d8a 390**/\r
4d0a30a4 391CHAR16 *\r
572f5d8a 392EFIAPI\r
4d0a30a4
RN
393UefiDevicePathLibConvertDevicePathToText (\r
394 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
395 IN BOOLEAN DisplayOnly,\r
396 IN BOOLEAN AllowShortcuts\r
572f5d8a 397 );\r
398\r
399/**\r
4d0a30a4 400 Converts a device node to its string representation.\r
572f5d8a 401\r
4d0a30a4
RN
402 @param DeviceNode A Pointer to the device node to be converted.\r
403 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation\r
404 of the display node is used, where applicable. If DisplayOnly\r
405 is FALSE, then the longer text representation of the display node\r
406 is used.\r
407 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text\r
408 representation for a device node can be used, where applicable.\r
572f5d8a 409\r
4d0a30a4
RN
410 @return A pointer to the allocated text representation of the device node or NULL if DeviceNode\r
411 is NULL or there was insufficient memory.\r
95276127 412\r
572f5d8a 413**/\r
4d0a30a4 414CHAR16 *\r
572f5d8a 415EFIAPI\r
4d0a30a4
RN
416UefiDevicePathLibConvertDeviceNodeToText (\r
417 IN CONST EFI_DEVICE_PATH_PROTOCOL *DeviceNode,\r
418 IN BOOLEAN DisplayOnly,\r
419 IN BOOLEAN AllowShortcuts\r
572f5d8a 420 );\r
95276127 421\r
572f5d8a 422/**\r
4d0a30a4 423 Convert text to the binary representation of a device node.\r
95276127 424\r
4d0a30a4
RN
425 @param TextDeviceNode TextDeviceNode points to the text representation of a device\r
426 node. Conversion starts with the first character and continues\r
427 until the first non-device node character.\r
95276127 428\r
4d0a30a4
RN
429 @return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was\r
430 insufficient memory or text unsupported.\r
95276127 431\r
572f5d8a 432**/\r
4d0a30a4 433EFI_DEVICE_PATH_PROTOCOL *\r
572f5d8a 434EFIAPI\r
4d0a30a4
RN
435UefiDevicePathLibConvertTextToDeviceNode (\r
436 IN CONST CHAR16 *TextDeviceNode\r
572f5d8a 437 );\r
95276127 438\r
572f5d8a 439/**\r
4d0a30a4 440 Convert text to the binary representation of a device path.\r
95276127 441\r
95276127 442\r
4d0a30a4
RN
443 @param TextDevicePath TextDevicePath points to the text representation of a device\r
444 path. Conversion starts with the first character and continues\r
445 until the first non-device node character.\r
95276127 446\r
4d0a30a4
RN
447 @return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or\r
448 there was insufficient memory.\r
95276127 449\r
572f5d8a 450**/\r
95276127 451EFI_DEVICE_PATH_PROTOCOL *\r
572f5d8a 452EFIAPI\r
4d0a30a4
RN
453UefiDevicePathLibConvertTextToDevicePath (\r
454 IN CONST CHAR16 *TextDevicePath\r
572f5d8a 455 );\r
95276127 456\r
457#endif\r