2 * Copyright (c) 2017 Netflix, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Taken from MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.h
31 * hash a11928f3310518ab1c6fd34e8d0fdbb72de9602c 2017-Mar-01
35 Definition for Device Path library.
37 Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>
38 This program and the accompanying materials
39 are licensed and made available under the terms and conditions of the BSD License
40 which accompanies this distribution. The full text of the license may be found at
41 http://opensource.org/licenses/bsd-license.php
43 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
44 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
48 #ifndef _UEFI_DEVICE_PATH_LIB_H_
49 #define _UEFI_DEVICE_PATH_LIB_H_
51 #include <Protocol/DevicePathUtilities.h>
52 #include <Protocol/DebugPort.h>
53 #include <Protocol/DevicePathToText.h>
54 #include <Protocol/DevicePathFromText.h>
55 #include <Guid/PcAnsi.h>
56 #include <Library/DebugLib.h>
57 #include <Library/PrintLib.h>
58 #include <Library/BaseLib.h>
59 #include <Library/BaseMemoryLib.h>
60 #include <Library/MemoryAllocationLib.h>
61 #include <Library/UefiBootServicesTableLib.h>
62 #include <Library/DevicePathLib.h>
63 #include <Library/PcdLib.h>
64 #include <IndustryStandard/Bluetooth.h>
66 #define IS_COMMA(a) ((a) == ',')
67 #define IS_HYPHEN(a) ((a) == '-')
68 #define IS_DOT(a) ((a) == '.')
69 #define IS_LEFT_PARENTH(a) ((a) == '(')
70 #define IS_RIGHT_PARENTH(a) ((a) == ')')
71 #define IS_SLASH(a) ((a) == '/')
72 #define IS_NULL(a) ((a) == '\0')
76 // Private Data structure
85 EFI_DEVICE_PATH_PROTOCOL *
86 (*DEVICE_PATH_FROM_TEXT) (
92 (*DEVICE_PATH_TO_TEXT) (
93 IN OUT POOL_PRINT *Str,
95 IN BOOLEAN DisplayOnly,
96 IN BOOLEAN AllowShortcuts
102 DEVICE_PATH_TO_TEXT Function;
103 } DEVICE_PATH_TO_TEXT_TABLE;
108 } DEVICE_PATH_TO_TEXT_GENERIC_TABLE;
111 const char *DevicePathNodeText;
112 DEVICE_PATH_FROM_TEXT Function;
113 } DEVICE_PATH_FROM_TEXT_TABLE;
118 BOOLEAN SubClassExist;
122 #define USB_CLASS_AUDIO 1
123 #define USB_CLASS_CDCCONTROL 2
124 #define USB_CLASS_HID 3
125 #define USB_CLASS_IMAGE 6
126 #define USB_CLASS_PRINTER 7
127 #define USB_CLASS_MASS_STORAGE 8
128 #define USB_CLASS_HUB 9
129 #define USB_CLASS_CDCDATA 10
130 #define USB_CLASS_SMART_CARD 11
131 #define USB_CLASS_VIDEO 14
132 #define USB_CLASS_DIAGNOSTIC 220
133 #define USB_CLASS_WIRELESS 224
135 #define USB_CLASS_RESERVE 254
136 #define USB_SUBCLASS_FW_UPDATE 1
137 #define USB_SUBCLASS_IRDA_BRIDGE 2
138 #define USB_SUBCLASS_TEST 3
140 #define RFC_1700_UDP_PROTOCOL 17
141 #define RFC_1700_TCP_PROTOCOL 6
146 EFI_DEVICE_PATH_PROTOCOL Header;
148 UINT8 VendorDefinedData[1];
149 } VENDOR_DEFINED_HARDWARE_DEVICE_PATH;
152 EFI_DEVICE_PATH_PROTOCOL Header;
154 UINT8 VendorDefinedData[1];
155 } VENDOR_DEFINED_MESSAGING_DEVICE_PATH;
158 EFI_DEVICE_PATH_PROTOCOL Header;
160 UINT8 VendorDefinedData[1];
161 } VENDOR_DEFINED_MEDIA_DEVICE_PATH;
164 EFI_DEVICE_PATH_PROTOCOL Header;
168 CHAR8 HidUidCidStr[3];
169 } ACPI_EXTENDED_HID_DEVICE_PATH_WITH_STR;
172 EFI_DEVICE_PATH_PROTOCOL Header;
173 UINT16 NetworkProtocol;
176 UINT16 TargetPortalGroupTag;
178 } ISCSI_DEVICE_PATH_WITH_NAME;
181 EFI_DEVICE_PATH_PROTOCOL Header;
183 UINT8 VendorDefinedData[1];
184 } VENDOR_DEVICE_PATH_WITH_DATA;
188 #ifdef FreeBSD /* Remove these on FreeBSD */
191 Returns the size of a device path in bytes.
193 This function returns the size, in bytes, of the device path data structure
194 specified by DevicePath including the end of device path node.
195 If DevicePath is NULL or invalid, then 0 is returned.
197 @param DevicePath A pointer to a device path data structure.
199 @retval 0 If DevicePath is NULL or invalid.
200 @retval Others The size of a device path in bytes.
205 UefiDevicePathLibGetDevicePathSize (
206 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
210 Creates a new copy of an existing device path.
212 This function allocates space for a new copy of the device path specified by DevicePath.
213 If DevicePath is NULL, then NULL is returned. If the memory is successfully
214 allocated, then the contents of DevicePath are copied to the newly allocated
215 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.
216 The memory for the new device path is allocated from EFI boot services memory.
217 It is the responsibility of the caller to free the memory allocated.
219 @param DevicePath A pointer to a device path data structure.
221 @retval NULL DevicePath is NULL or invalid.
222 @retval Others A pointer to the duplicated device path.
225 EFI_DEVICE_PATH_PROTOCOL *
227 UefiDevicePathLibDuplicateDevicePath (
228 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
232 Creates a new device path by appending a second device path to a first device path.
234 This function creates a new device path by appending a copy of SecondDevicePath
235 to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path
236 device node from SecondDevicePath is retained. The newly created device path is
237 returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
238 SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,
239 and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
240 SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.
242 If there is not enough memory for the newly allocated buffer, then NULL is returned.
243 The memory for the new device path is allocated from EFI boot services memory.
244 It is the responsibility of the caller to free the memory allocated.
246 @param FirstDevicePath A pointer to a device path data structure.
247 @param SecondDevicePath A pointer to a device path data structure.
249 @retval NULL If there is not enough memory for the newly allocated buffer.
250 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.
251 @retval Others A pointer to the new device path if success.
252 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
255 EFI_DEVICE_PATH_PROTOCOL *
257 UefiDevicePathLibAppendDevicePath (
258 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL
259 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL
263 Creates a new path by appending the device node to the device path.
265 This function creates a new device path by appending a copy of the device node
266 specified by DevicePathNode to a copy of the device path specified by DevicePath
267 in an allocated buffer. The end-of-device-path device node is moved after the
268 end of the appended device node.
269 If DevicePathNode is NULL then a copy of DevicePath is returned.
270 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
271 path device node is returned.
272 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
273 device node is returned.
274 If there is not enough memory to allocate space for the new device path, then
276 The memory is allocated from EFI boot services memory. It is the responsibility
277 of the caller to free the memory allocated.
279 @param DevicePath A pointer to a device path data structure.
280 @param DevicePathNode A pointer to a single device path node.
282 @retval NULL If there is not enough memory for the new device path.
283 @retval Others A pointer to the new device path if success.
284 A copy of DevicePathNode followed by an end-of-device-path node
285 if both FirstDevicePath and SecondDevicePath are NULL.
286 A copy of an end-of-device-path node if both FirstDevicePath
287 and SecondDevicePath are NULL.
290 EFI_DEVICE_PATH_PROTOCOL *
292 UefiDevicePathLibAppendDevicePathNode (
293 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL
294 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL
298 Creates a new device path by appending the specified device path instance to the specified device
301 This function creates a new device path by appending a copy of the device path
302 instance specified by DevicePathInstance to a copy of the device path specified
303 by DevicePath in a allocated buffer.
304 The end-of-device-path device node is moved after the end of the appended device
305 path instance and a new end-of-device-path-instance node is inserted between.
306 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
307 If DevicePathInstance is NULL, then NULL is returned.
308 If DevicePath or DevicePathInstance is invalid, then NULL is returned.
309 If there is not enough memory to allocate space for the new device path, then
311 The memory is allocated from EFI boot services memory. It is the responsibility
312 of the caller to free the memory allocated.
314 @param DevicePath A pointer to a device path data structure.
315 @param DevicePathInstance A pointer to a device path instance.
317 @return A pointer to the new device path.
320 EFI_DEVICE_PATH_PROTOCOL *
322 UefiDevicePathLibAppendDevicePathInstance (
323 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL
324 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL
328 Creates a copy of the current device path instance and returns a pointer to the next device path
331 This function creates a copy of the current device path instance. It also updates
332 DevicePath to point to the next device path instance in the device path (or NULL
333 if no more) and updates Size to hold the size of the device path instance copy.
334 If DevicePath is NULL, then NULL is returned.
335 If DevicePath points to a invalid device path, then NULL is returned.
336 If there is not enough memory to allocate space for the new device path, then
338 The memory is allocated from EFI boot services memory. It is the responsibility
339 of the caller to free the memory allocated.
340 If Size is NULL, then ASSERT().
342 @param DevicePath On input, this holds the pointer to the current
343 device path instance. On output, this holds
344 the pointer to the next device path instance
345 or NULL if there are no more device path
346 instances in the device path pointer to a
347 device path data structure.
348 @param Size On output, this holds the size of the device
349 path instance, in bytes or zero, if DevicePath
352 @return A pointer to the current device path instance.
355 EFI_DEVICE_PATH_PROTOCOL *
357 UefiDevicePathLibGetNextDevicePathInstance (
358 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,
363 Creates a device node.
365 This function creates a new device node in a newly allocated buffer of size
366 NodeLength and initializes the device path node header with NodeType and NodeSubType.
367 The new device path node is returned.
368 If NodeLength is smaller than a device path header, then NULL is returned.
369 If there is not enough memory to allocate space for the new device path, then
371 The memory is allocated from EFI boot services memory. It is the responsibility
372 of the caller to free the memory allocated.
374 @param NodeType The device node type for the new device node.
375 @param NodeSubType The device node sub-type for the new device node.
376 @param NodeLength The length of the new device node.
378 @return The new device path.
381 EFI_DEVICE_PATH_PROTOCOL *
383 UefiDevicePathLibCreateDeviceNode (
385 IN UINT8 NodeSubType,
390 Determines if a device path is single or multi-instance.
392 This function returns TRUE if the device path specified by DevicePath is
394 Otherwise, FALSE is returned.
395 If DevicePath is NULL or invalid, then FALSE is returned.
397 @param DevicePath A pointer to a device path data structure.
399 @retval TRUE DevicePath is multi-instance.
400 @retval FALSE DevicePath is not multi-instance, or DevicePath
406 UefiDevicePathLibIsDevicePathMultiInstance (
407 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
412 Converts a device path to its text representation.
414 @param DevicePath A Pointer to the device to be converted.
415 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation
416 of the display node is used, where applicable. If DisplayOnly
417 is FALSE, then the longer text representation of the display node
419 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text
420 representation for a device node can be used, where applicable.
422 @return A pointer to the allocated text representation of the device path or
423 NULL if DeviceNode is NULL or there was insufficient memory.
428 UefiDevicePathLibConvertDevicePathToText (
429 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
430 IN BOOLEAN DisplayOnly,
431 IN BOOLEAN AllowShortcuts
435 Converts a device node to its string representation.
437 @param DeviceNode A Pointer to the device node to be converted.
438 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation
439 of the display node is used, where applicable. If DisplayOnly
440 is FALSE, then the longer text representation of the display node
442 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text
443 representation for a device node can be used, where applicable.
445 @return A pointer to the allocated text representation of the device node or NULL if DeviceNode
446 is NULL or there was insufficient memory.
451 UefiDevicePathLibConvertDeviceNodeToText (
452 IN CONST EFI_DEVICE_PATH_PROTOCOL *DeviceNode,
453 IN BOOLEAN DisplayOnly,
454 IN BOOLEAN AllowShortcuts
458 Convert text to the binary representation of a device node.
460 @param TextDeviceNode TextDeviceNode points to the text representation of a device
461 node. Conversion starts with the first character and continues
462 until the first non-device node character.
464 @return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was
465 insufficient memory or text unsupported.
468 EFI_DEVICE_PATH_PROTOCOL *
470 UefiDevicePathLibConvertTextToDeviceNode (
471 IN CONST CHAR16 *TextDeviceNode
475 Convert text to the binary representation of a device path.
478 @param TextDevicePath TextDevicePath points to the text representation of a device
479 path. Conversion starts with the first character and continues
480 until the first non-device node character.
482 @return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or
483 there was insufficient memory.
486 EFI_DEVICE_PATH_PROTOCOL *
488 UefiDevicePathLibConvertTextToDevicePath (
489 IN CONST CHAR16 *TextDevicePath
494 * Small FreeBSD shim layer. Fast and lose hacks to make this code work with FreeBSD.
499 #define _PCD_GET_MODE_32_PcdMaximumDevicePathNodeCount 1000
500 #define MAX_UINTN UINTPTR_MAX
502 #define AllocatePool(x) malloc(x)
503 #define AllocateZeroPool(x) calloc(1,x)
504 #define AsciiStrLen(s) strlen(s)
505 #define CopyGuid(dst, src) memcpy(dst, src, sizeof(uuid_t))
506 #define CopyMem(d, s, l) memcpy(d, s, l)
507 #define FreePool(x) free(x)
508 #define LShiftU64(x, s) ((x) << s)
509 #define ReadUnaligned64(x) le64dec(x)
510 #define ReallocatePool(old, new, ptr) realloc(ptr, new)
512 * Quirky StrCmp returns 0 if equal, 1 if not. This is what the code
513 * expects, though that expectation is likely a bug (it casts the
514 * return value. EDK2's StrCmp returns values just like C's strcmp,
515 * but the parse code casts this to an UINTN, which is bogus. This
516 * definition papers over that bogusness to do the right thing. If
517 * iSCSI protocol string processing is ever fixed, we can remove this
518 * bletcherous kludge.
520 #define StrCmp(a, b) (strcmp(a, b) != 0)
521 #define StrCpyS(d, l, s) strcpy(d, s)
522 #define StrHexToUint64(x) strtoll(x, NULL, 16)
523 #define StrHexToUintn(x) strtoll(x, NULL, 16)
524 #define StrLen(x) strlen(x)
525 #define StrSize(x) (strlen(x) + 1)
526 #define StrnCatS(d, l, s, len) strncat(d, s, len)
527 #define StrnCmp(a, b, n) strncmp(a, b, n)
528 #define StrnLenS(str, max) strlen(str)
529 #define Strtoi(x) strtol(x, NULL, 0)
530 #define Strtoi64(x, y) *(long long *)y = strtoll(x, NULL, 0)
531 #define SwapBytes64(u64) bswap64(u64)
532 #define UnicodeStrToAsciiStrS(src, dest, len) strlcpy(dest, src, len)
533 #define ZeroMem(p,l) memset(p, 0, l)
539 * Define AllocateCopyPool and others so that we "forget" about the
540 * previous non-static deifnition since we want these to be static
543 #define AllocateCopyPool AllocateCopyPoolFreeBSD
544 #define CompareGuid CompareGuidFreeBSD
545 #define StrHexToBytes StrHexToBytesFreeBSD
546 #define StrToGuid StrToGuidFreeBSD
547 #define WriteUnaligned64 WriteUnaligned64FreeBSD
550 AllocateCopyPool(size_t l, const void *p)
561 static inline BOOLEAN
562 CompareGuid (const GUID *g1, const GUID *g2)
564 uint32_t ignored_status;
566 return (uuid_compare((const uuid_t *)g1, (const uuid_t *)g2,
567 &ignored_status) == 0);
571 StrHexToBytes(const char *str, size_t len, uint8_t *buf, size_t buflen)
577 * Sanity check preconditions.
579 if (buflen != len / 2 || (len % 1) == 1)
581 for (i = 0; i < len; i += 2) {
582 if (!isxdigit(str[i]) || !isxdigit(str[i + 1]))
587 buf[i / 2] = strtol(hex, NULL, 16);
593 StrToGuid(const char *str, GUID *guid)
597 uuid_from_string(str, (uuid_t *)guid, &status);
601 WriteUnaligned64(void *ptr, uint64_t val)
603 memcpy(ptr, &val, sizeof(val));
607 * Hack to allow converting %g to %s in printfs. Hack because
608 * it's single entry, uses a static buffer, etc. Sufficient for
609 * the day for this file though. IF you ever have to convert
610 * two %g's in one format, punt. Did I mention this was super lame.
611 * Not to mention it's name.... Also, the error GUID is horrific.
613 static inline const char *
614 guid_str(const GUID *g)
616 static char buf[36 + 1];
618 int32_t ignored_status;
620 uuid_to_string((const uuid_t *)g, &str, &ignored_status);
622 strlcpy(buf, str, sizeof(buf));
624 strlcpy(buf, "groot-cannot-decode-guid-groot-smash",
625 sizeof(buf)); /* ^^^^^^^ 36 characters ^^^^^^^ */
629 #define G(x) guid_str((const GUID *)(const void *)x)
632 #undef GLOBAL_REMOVE_IF_UNREFERENCED
633 #define GLOBAL_REMOVE_IF_UNREFERENCED static