17270962aSWarner Losh /*- 27270962aSWarner Losh * Copyright (c) 2017 Netflix, Inc. 37270962aSWarner Losh * 47270962aSWarner Losh * Redistribution and use in source and binary forms, with or without 57270962aSWarner Losh * modification, are permitted provided that the following conditions 67270962aSWarner Losh * are met: 77270962aSWarner Losh * 1. Redistributions of source code must retain the above copyright 86decf2ccSEd Maste * notice, this list of conditions and the following disclaimer. 97270962aSWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 107270962aSWarner Losh * notice, this list of conditions and the following disclaimer in the 117270962aSWarner Losh * documentation and/or other materials provided with the distribution. 127270962aSWarner Losh * 136decf2ccSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 146decf2ccSEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 156decf2ccSEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 166decf2ccSEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 176decf2ccSEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 186decf2ccSEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 196decf2ccSEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 206decf2ccSEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 216decf2ccSEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 226decf2ccSEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 236decf2ccSEd Maste * SUCH DAMAGE. 247270962aSWarner Losh */ 257270962aSWarner Losh 267270962aSWarner Losh /* 277270962aSWarner Losh * Taken from MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.h 287270962aSWarner Losh */ 297270962aSWarner Losh 307270962aSWarner Losh /** @file 317270962aSWarner Losh Definition for Device Path library. 327270962aSWarner Losh 3349951297SJose Luis Duran Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR> 3411a9ff5bSJose Luis Duran SPDX-License-Identifier: BSD-2-Clause-Patent 357270962aSWarner Losh 367270962aSWarner Losh **/ 377270962aSWarner Losh 387270962aSWarner Losh #ifndef _UEFI_DEVICE_PATH_LIB_H_ 397270962aSWarner Losh #define _UEFI_DEVICE_PATH_LIB_H_ 407270962aSWarner Losh #include <Uefi.h> 417270962aSWarner Losh #include <Protocol/DevicePathUtilities.h> 427270962aSWarner Losh #include <Protocol/DebugPort.h> 437270962aSWarner Losh #include <Protocol/DevicePathToText.h> 447270962aSWarner Losh #include <Protocol/DevicePathFromText.h> 457270962aSWarner Losh #include <Guid/PcAnsi.h> 467270962aSWarner Losh #include <Library/DebugLib.h> 477270962aSWarner Losh #include <Library/PrintLib.h> 487270962aSWarner Losh #include <Library/BaseLib.h> 497270962aSWarner Losh #include <Library/BaseMemoryLib.h> 507270962aSWarner Losh #include <Library/MemoryAllocationLib.h> 517270962aSWarner Losh #include <Library/UefiBootServicesTableLib.h> 527270962aSWarner Losh #include <Library/DevicePathLib.h> 537270962aSWarner Losh #include <Library/PcdLib.h> 547270962aSWarner Losh #include <IndustryStandard/Bluetooth.h> 557270962aSWarner Losh 567270962aSWarner Losh #define IS_COMMA(a) ((a) == ',') 577270962aSWarner Losh #define IS_HYPHEN(a) ((a) == '-') 587270962aSWarner Losh #define IS_DOT(a) ((a) == '.') 597270962aSWarner Losh #define IS_LEFT_PARENTH(a) ((a) == '(') 607270962aSWarner Losh #define IS_RIGHT_PARENTH(a) ((a) == ')') 617270962aSWarner Losh #define IS_SLASH(a) ((a) == '/') 627270962aSWarner Losh #define IS_NULL(a) ((a) == '\0') 637270962aSWarner Losh 647270962aSWarner Losh // 657270962aSWarner Losh // Private Data structure 667270962aSWarner Losh // 677270962aSWarner Losh typedef struct { 687270962aSWarner Losh char *Str; 697270962aSWarner Losh UINTN Count; 707270962aSWarner Losh UINTN Capacity; 717270962aSWarner Losh } POOL_PRINT; 727270962aSWarner Losh 737270962aSWarner Losh typedef 747270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL * 757270962aSWarner Losh (*DEVICE_PATH_FROM_TEXT) ( 767270962aSWarner Losh IN char *Str 777270962aSWarner Losh ); 787270962aSWarner Losh 797270962aSWarner Losh typedef 807270962aSWarner Losh VOID 817270962aSWarner Losh (*DEVICE_PATH_TO_TEXT) ( 827270962aSWarner Losh IN OUT POOL_PRINT *Str, 837270962aSWarner Losh IN VOID *DevicePath, 847270962aSWarner Losh IN BOOLEAN DisplayOnly, 857270962aSWarner Losh IN BOOLEAN AllowShortcuts 867270962aSWarner Losh ); 877270962aSWarner Losh 887270962aSWarner Losh typedef struct { 897270962aSWarner Losh UINT8 Type; 907270962aSWarner Losh UINT8 SubType; 917270962aSWarner Losh DEVICE_PATH_TO_TEXT Function; 927270962aSWarner Losh } DEVICE_PATH_TO_TEXT_TABLE; 937270962aSWarner Losh 947270962aSWarner Losh typedef struct { 957270962aSWarner Losh UINT8 Type; 967270962aSWarner Losh const char *Text; 977270962aSWarner Losh } DEVICE_PATH_TO_TEXT_GENERIC_TABLE; 987270962aSWarner Losh 997270962aSWarner Losh typedef struct { 1007270962aSWarner Losh const char *DevicePathNodeText; 1017270962aSWarner Losh DEVICE_PATH_FROM_TEXT Function; 1027270962aSWarner Losh } DEVICE_PATH_FROM_TEXT_TABLE; 1037270962aSWarner Losh 1047270962aSWarner Losh typedef struct { 1057270962aSWarner Losh BOOLEAN ClassExist; 1067270962aSWarner Losh UINT8 Class; 1077270962aSWarner Losh BOOLEAN SubClassExist; 1087270962aSWarner Losh UINT8 SubClass; 1097270962aSWarner Losh } USB_CLASS_TEXT; 1107270962aSWarner Losh 1117270962aSWarner Losh #define USB_CLASS_AUDIO 1 1127270962aSWarner Losh #define USB_CLASS_CDCCONTROL 2 1137270962aSWarner Losh #define USB_CLASS_HID 3 1147270962aSWarner Losh #define USB_CLASS_IMAGE 6 1157270962aSWarner Losh #define USB_CLASS_PRINTER 7 1167270962aSWarner Losh #define USB_CLASS_MASS_STORAGE 8 1177270962aSWarner Losh #define USB_CLASS_HUB 9 1187270962aSWarner Losh #define USB_CLASS_CDCDATA 10 1197270962aSWarner Losh #define USB_CLASS_SMART_CARD 11 1207270962aSWarner Losh #define USB_CLASS_VIDEO 14 1217270962aSWarner Losh #define USB_CLASS_DIAGNOSTIC 220 1227270962aSWarner Losh #define USB_CLASS_WIRELESS 224 1237270962aSWarner Losh 1247270962aSWarner Losh #define USB_CLASS_RESERVE 254 1257270962aSWarner Losh #define USB_SUBCLASS_FW_UPDATE 1 1267270962aSWarner Losh #define USB_SUBCLASS_IRDA_BRIDGE 2 1277270962aSWarner Losh #define USB_SUBCLASS_TEST 3 1287270962aSWarner Losh 1297270962aSWarner Losh #define RFC_1700_UDP_PROTOCOL 17 1307270962aSWarner Losh #define RFC_1700_TCP_PROTOCOL 6 1317270962aSWarner Losh 1327270962aSWarner Losh #pragma pack(1) 1337270962aSWarner Losh 1347270962aSWarner Losh typedef struct { 1357270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL Header; 1367270962aSWarner Losh EFI_GUID Guid; 1377270962aSWarner Losh UINT8 VendorDefinedData[1]; 1387270962aSWarner Losh } VENDOR_DEFINED_HARDWARE_DEVICE_PATH; 1397270962aSWarner Losh 1407270962aSWarner Losh typedef struct { 1417270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL Header; 1427270962aSWarner Losh EFI_GUID Guid; 1437270962aSWarner Losh UINT8 VendorDefinedData[1]; 1447270962aSWarner Losh } VENDOR_DEFINED_MESSAGING_DEVICE_PATH; 1457270962aSWarner Losh 1467270962aSWarner Losh typedef struct { 1477270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL Header; 1487270962aSWarner Losh EFI_GUID Guid; 1497270962aSWarner Losh UINT8 VendorDefinedData[1]; 1507270962aSWarner Losh } VENDOR_DEFINED_MEDIA_DEVICE_PATH; 1517270962aSWarner Losh 1527270962aSWarner Losh typedef struct { 1537270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL Header; 1547270962aSWarner Losh UINT32 Hid; 1557270962aSWarner Losh UINT32 Uid; 1567270962aSWarner Losh UINT32 Cid; 1577270962aSWarner Losh CHAR8 HidUidCidStr[3]; 1587270962aSWarner Losh } ACPI_EXTENDED_HID_DEVICE_PATH_WITH_STR; 1597270962aSWarner Losh 1607270962aSWarner Losh typedef struct { 1617270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL Header; 1627270962aSWarner Losh UINT16 NetworkProtocol; 1637270962aSWarner Losh UINT16 LoginOption; 1647270962aSWarner Losh UINT64 Lun; 1657270962aSWarner Losh UINT16 TargetPortalGroupTag; 1667270962aSWarner Losh CHAR8 TargetName[1]; 1677270962aSWarner Losh } ISCSI_DEVICE_PATH_WITH_NAME; 1687270962aSWarner Losh 1697270962aSWarner Losh typedef struct { 1707270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL Header; 1717270962aSWarner Losh EFI_GUID Guid; 1727270962aSWarner Losh UINT8 VendorDefinedData[1]; 1737270962aSWarner Losh } VENDOR_DEVICE_PATH_WITH_DATA; 1747270962aSWarner Losh 1757270962aSWarner Losh #pragma pack() 1767270962aSWarner Losh 1777270962aSWarner Losh #ifdef FreeBSD /* Remove these on FreeBSD */ 1787270962aSWarner Losh 1797270962aSWarner Losh /** 1807270962aSWarner Losh Returns the size of a device path in bytes. 1817270962aSWarner Losh 1827270962aSWarner Losh This function returns the size, in bytes, of the device path data structure 1837270962aSWarner Losh specified by DevicePath including the end of device path node. 1847270962aSWarner Losh If DevicePath is NULL or invalid, then 0 is returned. 1857270962aSWarner Losh 1867270962aSWarner Losh @param DevicePath A pointer to a device path data structure. 1877270962aSWarner Losh 1887270962aSWarner Losh @retval 0 If DevicePath is NULL or invalid. 1897270962aSWarner Losh @retval Others The size of a device path in bytes. 1907270962aSWarner Losh 1917270962aSWarner Losh **/ 1927270962aSWarner Losh UINTN 1937270962aSWarner Losh EFIAPI 1947270962aSWarner Losh UefiDevicePathLibGetDevicePathSize ( 1957270962aSWarner Losh IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath 1967270962aSWarner Losh ); 1977270962aSWarner Losh 1987270962aSWarner Losh /** 1997270962aSWarner Losh Creates a new copy of an existing device path. 2007270962aSWarner Losh 2017270962aSWarner Losh This function allocates space for a new copy of the device path specified by DevicePath. 2027270962aSWarner Losh If DevicePath is NULL, then NULL is returned. If the memory is successfully 2037270962aSWarner Losh allocated, then the contents of DevicePath are copied to the newly allocated 2047270962aSWarner Losh buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned. 2057270962aSWarner Losh The memory for the new device path is allocated from EFI boot services memory. 2067270962aSWarner Losh It is the responsibility of the caller to free the memory allocated. 2077270962aSWarner Losh 2087270962aSWarner Losh @param DevicePath A pointer to a device path data structure. 2097270962aSWarner Losh 2107270962aSWarner Losh @retval NULL DevicePath is NULL or invalid. 2117270962aSWarner Losh @retval Others A pointer to the duplicated device path. 2127270962aSWarner Losh 2137270962aSWarner Losh **/ 2147270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL * 2157270962aSWarner Losh EFIAPI 2167270962aSWarner Losh UefiDevicePathLibDuplicateDevicePath ( 2177270962aSWarner Losh IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath 2187270962aSWarner Losh ); 2197270962aSWarner Losh 2207270962aSWarner Losh /** 2217270962aSWarner Losh Creates a new device path by appending a second device path to a first device path. 2227270962aSWarner Losh 2237270962aSWarner Losh This function creates a new device path by appending a copy of SecondDevicePath 2247270962aSWarner Losh to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path 2257270962aSWarner Losh device node from SecondDevicePath is retained. The newly created device path is 2267270962aSWarner Losh returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of 2277270962aSWarner Losh SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored, 2287270962aSWarner Losh and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and 2297270962aSWarner Losh SecondDevicePath are NULL, then a copy of an end-of-device-path is returned. 2307270962aSWarner Losh 2317270962aSWarner Losh If there is not enough memory for the newly allocated buffer, then NULL is returned. 2327270962aSWarner Losh The memory for the new device path is allocated from EFI boot services memory. 2337270962aSWarner Losh It is the responsibility of the caller to free the memory allocated. 2347270962aSWarner Losh 2357270962aSWarner Losh @param FirstDevicePath A pointer to a device path data structure. 2367270962aSWarner Losh @param SecondDevicePath A pointer to a device path data structure. 2377270962aSWarner Losh 2387270962aSWarner Losh @retval NULL If there is not enough memory for the newly allocated buffer. 2397270962aSWarner Losh @retval NULL If FirstDevicePath or SecondDevicePath is invalid. 2407270962aSWarner Losh @retval Others A pointer to the new device path if success. 2417270962aSWarner Losh Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL. 2427270962aSWarner Losh 2437270962aSWarner Losh **/ 2447270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL * 2457270962aSWarner Losh EFIAPI 2467270962aSWarner Losh UefiDevicePathLibAppendDevicePath ( 247*60de142cSJose Luis Duran IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath OPTIONAL, 2487270962aSWarner Losh IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL 2497270962aSWarner Losh ); 2507270962aSWarner Losh 2517270962aSWarner Losh /** 2527270962aSWarner Losh Creates a new path by appending the device node to the device path. 2537270962aSWarner Losh 2547270962aSWarner Losh This function creates a new device path by appending a copy of the device node 2557270962aSWarner Losh specified by DevicePathNode to a copy of the device path specified by DevicePath 2567270962aSWarner Losh in an allocated buffer. The end-of-device-path device node is moved after the 2577270962aSWarner Losh end of the appended device node. 2587270962aSWarner Losh If DevicePathNode is NULL then a copy of DevicePath is returned. 2597270962aSWarner Losh If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device 2607270962aSWarner Losh path device node is returned. 2617270962aSWarner Losh If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path 2627270962aSWarner Losh device node is returned. 2637270962aSWarner Losh If there is not enough memory to allocate space for the new device path, then 2647270962aSWarner Losh NULL is returned. 2657270962aSWarner Losh The memory is allocated from EFI boot services memory. It is the responsibility 2667270962aSWarner Losh of the caller to free the memory allocated. 2677270962aSWarner Losh 2687270962aSWarner Losh @param DevicePath A pointer to a device path data structure. 2697270962aSWarner Losh @param DevicePathNode A pointer to a single device path node. 2707270962aSWarner Losh 2717270962aSWarner Losh @retval NULL If there is not enough memory for the new device path. 2727270962aSWarner Losh @retval Others A pointer to the new device path if success. 2737270962aSWarner Losh A copy of DevicePathNode followed by an end-of-device-path node 2747270962aSWarner Losh if both FirstDevicePath and SecondDevicePath are NULL. 2757270962aSWarner Losh A copy of an end-of-device-path node if both FirstDevicePath 2767270962aSWarner Losh and SecondDevicePath are NULL. 2777270962aSWarner Losh 2787270962aSWarner Losh **/ 2797270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL * 2807270962aSWarner Losh EFIAPI 2817270962aSWarner Losh UefiDevicePathLibAppendDevicePathNode ( 282*60de142cSJose Luis Duran IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL, 2837270962aSWarner Losh IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL 2847270962aSWarner Losh ); 2857270962aSWarner Losh 2867270962aSWarner Losh /** 2877270962aSWarner Losh Creates a new device path by appending the specified device path instance to the specified device 2887270962aSWarner Losh path. 2897270962aSWarner Losh 2907270962aSWarner Losh This function creates a new device path by appending a copy of the device path 2917270962aSWarner Losh instance specified by DevicePathInstance to a copy of the device path specified 2927270962aSWarner Losh by DevicePath in a allocated buffer. 2937270962aSWarner Losh The end-of-device-path device node is moved after the end of the appended device 2947270962aSWarner Losh path instance and a new end-of-device-path-instance node is inserted between. 2957270962aSWarner Losh If DevicePath is NULL, then a copy if DevicePathInstance is returned. 2967270962aSWarner Losh If DevicePathInstance is NULL, then NULL is returned. 2977270962aSWarner Losh If DevicePath or DevicePathInstance is invalid, then NULL is returned. 2987270962aSWarner Losh If there is not enough memory to allocate space for the new device path, then 2997270962aSWarner Losh NULL is returned. 3007270962aSWarner Losh The memory is allocated from EFI boot services memory. It is the responsibility 3017270962aSWarner Losh of the caller to free the memory allocated. 3027270962aSWarner Losh 3037270962aSWarner Losh @param DevicePath A pointer to a device path data structure. 3047270962aSWarner Losh @param DevicePathInstance A pointer to a device path instance. 3057270962aSWarner Losh 3067270962aSWarner Losh @return A pointer to the new device path. 3077270962aSWarner Losh 3087270962aSWarner Losh **/ 3097270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL * 3107270962aSWarner Losh EFIAPI 3117270962aSWarner Losh UefiDevicePathLibAppendDevicePathInstance ( 312*60de142cSJose Luis Duran IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL, 3137270962aSWarner Losh IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL 3147270962aSWarner Losh ); 3157270962aSWarner Losh 3167270962aSWarner Losh /** 3177270962aSWarner Losh Creates a copy of the current device path instance and returns a pointer to the next device path 3187270962aSWarner Losh instance. 3197270962aSWarner Losh 3207270962aSWarner Losh This function creates a copy of the current device path instance. It also updates 3217270962aSWarner Losh DevicePath to point to the next device path instance in the device path (or NULL 3227270962aSWarner Losh if no more) and updates Size to hold the size of the device path instance copy. 3237270962aSWarner Losh If DevicePath is NULL, then NULL is returned. 3247270962aSWarner Losh If DevicePath points to a invalid device path, then NULL is returned. 3257270962aSWarner Losh If there is not enough memory to allocate space for the new device path, then 3267270962aSWarner Losh NULL is returned. 3277270962aSWarner Losh The memory is allocated from EFI boot services memory. It is the responsibility 3287270962aSWarner Losh of the caller to free the memory allocated. 3297270962aSWarner Losh If Size is NULL, then ASSERT(). 3307270962aSWarner Losh 3317270962aSWarner Losh @param DevicePath On input, this holds the pointer to the current 3327270962aSWarner Losh device path instance. On output, this holds 3337270962aSWarner Losh the pointer to the next device path instance 3347270962aSWarner Losh or NULL if there are no more device path 3357270962aSWarner Losh instances in the device path pointer to a 3367270962aSWarner Losh device path data structure. 3377270962aSWarner Losh @param Size On output, this holds the size of the device 3387270962aSWarner Losh path instance, in bytes or zero, if DevicePath 3397270962aSWarner Losh is NULL. 3407270962aSWarner Losh 3417270962aSWarner Losh @return A pointer to the current device path instance. 3427270962aSWarner Losh 3437270962aSWarner Losh **/ 3447270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL * 3457270962aSWarner Losh EFIAPI 3467270962aSWarner Losh UefiDevicePathLibGetNextDevicePathInstance ( 3477270962aSWarner Losh IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath, 3487270962aSWarner Losh OUT UINTN *Size 3497270962aSWarner Losh ); 3507270962aSWarner Losh 3517270962aSWarner Losh /** 3527270962aSWarner Losh Creates a device node. 3537270962aSWarner Losh 3547270962aSWarner Losh This function creates a new device node in a newly allocated buffer of size 3557270962aSWarner Losh NodeLength and initializes the device path node header with NodeType and NodeSubType. 3567270962aSWarner Losh The new device path node is returned. 3577270962aSWarner Losh If NodeLength is smaller than a device path header, then NULL is returned. 3587270962aSWarner Losh If there is not enough memory to allocate space for the new device path, then 3597270962aSWarner Losh NULL is returned. 3607270962aSWarner Losh The memory is allocated from EFI boot services memory. It is the responsibility 3617270962aSWarner Losh of the caller to free the memory allocated. 3627270962aSWarner Losh 3637270962aSWarner Losh @param NodeType The device node type for the new device node. 3647270962aSWarner Losh @param NodeSubType The device node sub-type for the new device node. 3657270962aSWarner Losh @param NodeLength The length of the new device node. 3667270962aSWarner Losh 3677270962aSWarner Losh @return The new device path. 3687270962aSWarner Losh 3697270962aSWarner Losh **/ 3707270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL * 3717270962aSWarner Losh EFIAPI 3727270962aSWarner Losh UefiDevicePathLibCreateDeviceNode ( 3737270962aSWarner Losh IN UINT8 NodeType, 3747270962aSWarner Losh IN UINT8 NodeSubType, 3757270962aSWarner Losh IN UINT16 NodeLength 3767270962aSWarner Losh ); 3777270962aSWarner Losh 3787270962aSWarner Losh /** 3797270962aSWarner Losh Determines if a device path is single or multi-instance. 3807270962aSWarner Losh 3817270962aSWarner Losh This function returns TRUE if the device path specified by DevicePath is 3827270962aSWarner Losh multi-instance. 3837270962aSWarner Losh Otherwise, FALSE is returned. 3847270962aSWarner Losh If DevicePath is NULL or invalid, then FALSE is returned. 3857270962aSWarner Losh 3867270962aSWarner Losh @param DevicePath A pointer to a device path data structure. 3877270962aSWarner Losh 3887270962aSWarner Losh @retval TRUE DevicePath is multi-instance. 3897270962aSWarner Losh @retval FALSE DevicePath is not multi-instance, or DevicePath 3907270962aSWarner Losh is NULL or invalid. 3917270962aSWarner Losh 3927270962aSWarner Losh **/ 3937270962aSWarner Losh BOOLEAN 3947270962aSWarner Losh EFIAPI 3957270962aSWarner Losh UefiDevicePathLibIsDevicePathMultiInstance ( 3967270962aSWarner Losh IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath 3977270962aSWarner Losh ); 3987270962aSWarner Losh 3997270962aSWarner Losh /** 4007270962aSWarner Losh Converts a device path to its text representation. 4017270962aSWarner Losh 4027270962aSWarner Losh @param DevicePath A Pointer to the device to be converted. 4037270962aSWarner Losh @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation 4047270962aSWarner Losh of the display node is used, where applicable. If DisplayOnly 4057270962aSWarner Losh is FALSE, then the longer text representation of the display node 4067270962aSWarner Losh is used. 4077270962aSWarner Losh @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text 4087270962aSWarner Losh representation for a device node can be used, where applicable. 4097270962aSWarner Losh 4107270962aSWarner Losh @return A pointer to the allocated text representation of the device path or 4117270962aSWarner Losh NULL if DeviceNode is NULL or there was insufficient memory. 4127270962aSWarner Losh 4137270962aSWarner Losh **/ 4147270962aSWarner Losh CHAR16 * 4157270962aSWarner Losh EFIAPI 4167270962aSWarner Losh UefiDevicePathLibConvertDevicePathToText ( 4177270962aSWarner Losh IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, 4187270962aSWarner Losh IN BOOLEAN DisplayOnly, 4197270962aSWarner Losh IN BOOLEAN AllowShortcuts 4207270962aSWarner Losh ); 4217270962aSWarner Losh 4227270962aSWarner Losh /** 4237270962aSWarner Losh Converts a device node to its string representation. 4247270962aSWarner Losh 4257270962aSWarner Losh @param DeviceNode A Pointer to the device node to be converted. 4267270962aSWarner Losh @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation 4277270962aSWarner Losh of the display node is used, where applicable. If DisplayOnly 4287270962aSWarner Losh is FALSE, then the longer text representation of the display node 4297270962aSWarner Losh is used. 4307270962aSWarner Losh @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text 4317270962aSWarner Losh representation for a device node can be used, where applicable. 4327270962aSWarner Losh 4337270962aSWarner Losh @return A pointer to the allocated text representation of the device node or NULL if DeviceNode 4347270962aSWarner Losh is NULL or there was insufficient memory. 4357270962aSWarner Losh 4367270962aSWarner Losh **/ 4377270962aSWarner Losh CHAR16 * 4387270962aSWarner Losh EFIAPI 4397270962aSWarner Losh UefiDevicePathLibConvertDeviceNodeToText ( 4407270962aSWarner Losh IN CONST EFI_DEVICE_PATH_PROTOCOL *DeviceNode, 4417270962aSWarner Losh IN BOOLEAN DisplayOnly, 4427270962aSWarner Losh IN BOOLEAN AllowShortcuts 4437270962aSWarner Losh ); 4447270962aSWarner Losh 4457270962aSWarner Losh /** 4467270962aSWarner Losh Convert text to the binary representation of a device node. 4477270962aSWarner Losh 4487270962aSWarner Losh @param TextDeviceNode TextDeviceNode points to the text representation of a device 4497270962aSWarner Losh node. Conversion starts with the first character and continues 4507270962aSWarner Losh until the first non-device node character. 4517270962aSWarner Losh 4527270962aSWarner Losh @return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was 4537270962aSWarner Losh insufficient memory or text unsupported. 4547270962aSWarner Losh 4557270962aSWarner Losh **/ 4567270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL * 4577270962aSWarner Losh EFIAPI 4587270962aSWarner Losh UefiDevicePathLibConvertTextToDeviceNode ( 4597270962aSWarner Losh IN CONST CHAR16 *TextDeviceNode 4607270962aSWarner Losh ); 4617270962aSWarner Losh 4627270962aSWarner Losh /** 4637270962aSWarner Losh Convert text to the binary representation of a device path. 4647270962aSWarner Losh 4657270962aSWarner Losh 4667270962aSWarner Losh @param TextDevicePath TextDevicePath points to the text representation of a device 4677270962aSWarner Losh path. Conversion starts with the first character and continues 4687270962aSWarner Losh until the first non-device node character. 4697270962aSWarner Losh 4707270962aSWarner Losh @return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or 4717270962aSWarner Losh there was insufficient memory. 4727270962aSWarner Losh 4737270962aSWarner Losh **/ 4747270962aSWarner Losh EFI_DEVICE_PATH_PROTOCOL * 4757270962aSWarner Losh EFIAPI 4767270962aSWarner Losh UefiDevicePathLibConvertTextToDevicePath ( 4777270962aSWarner Losh IN CONST CHAR16 *TextDevicePath 4787270962aSWarner Losh ); 4797270962aSWarner Losh #else 4807270962aSWarner Losh 4817270962aSWarner Losh /* 4827270962aSWarner Losh * Small FreeBSD shim layer. Fast and lose hacks to make this code work with FreeBSD. 4837270962aSWarner Losh */ 4847270962aSWarner Losh 4857270962aSWarner Losh #include <ctype.h> 4867270962aSWarner Losh 4877270962aSWarner Losh #define _PCD_GET_MODE_32_PcdMaximumDevicePathNodeCount 1000 4887270962aSWarner Losh #define MAX_UINTN UINTPTR_MAX 4897270962aSWarner Losh 4907270962aSWarner Losh #define AllocatePool(x) malloc(x) 4917270962aSWarner Losh #define AllocateZeroPool(x) calloc(1,x) 4927270962aSWarner Losh #define AsciiStrLen(s) strlen(s) 4937270962aSWarner Losh #define CopyGuid(dst, src) memcpy(dst, src, sizeof(uuid_t)) 4947270962aSWarner Losh #define CopyMem(d, s, l) memcpy(d, s, l) 4957270962aSWarner Losh #define FreePool(x) free(x) 4967270962aSWarner Losh #define LShiftU64(x, s) ((x) << s) 4977270962aSWarner Losh #define ReadUnaligned64(x) le64dec(x) 4987270962aSWarner Losh #define ReallocatePool(old, new, ptr) realloc(ptr, new) 4998af6a2c6SWarner Losh /* 5008af6a2c6SWarner Losh * Quirky StrCmp returns 0 if equal, 1 if not. This is what the code 5018af6a2c6SWarner Losh * expects, though that expectation is likely a bug (it casts the 5028af6a2c6SWarner Losh * return value. EDK2's StrCmp returns values just like C's strcmp, 5038af6a2c6SWarner Losh * but the parse code casts this to an UINTN, which is bogus. This 5048af6a2c6SWarner Losh * definition papers over that bogusness to do the right thing. If 5058af6a2c6SWarner Losh * iSCSI protocol string processing is ever fixed, we can remove this 5068af6a2c6SWarner Losh * bletcherous kludge. 5078af6a2c6SWarner Losh */ 5088af6a2c6SWarner Losh #define StrCmp(a, b) (strcmp(a, b) != 0) 5097270962aSWarner Losh #define StrCpyS(d, l, s) strcpy(d, s) 5107270962aSWarner Losh #define StrHexToUint64(x) strtoll(x, NULL, 16) 5117270962aSWarner Losh #define StrHexToUintn(x) strtoll(x, NULL, 16) 5127270962aSWarner Losh #define StrLen(x) strlen(x) 5137270962aSWarner Losh #define StrSize(x) (strlen(x) + 1) 5147270962aSWarner Losh #define StrnCatS(d, l, s, len) strncat(d, s, len) 5157270962aSWarner Losh #define StrnCmp(a, b, n) strncmp(a, b, n) 5167270962aSWarner Losh #define StrnLenS(str, max) strlen(str) 5177270962aSWarner Losh #define Strtoi(x) strtol(x, NULL, 0) 5187270962aSWarner Losh #define Strtoi64(x, y) *(long long *)y = strtoll(x, NULL, 0) 5197270962aSWarner Losh #define SwapBytes64(u64) bswap64(u64) 5207270962aSWarner Losh #define UnicodeStrToAsciiStrS(src, dest, len) strlcpy(dest, src, len) 5217270962aSWarner Losh #define ZeroMem(p,l) memset(p, 0, l) 5227270962aSWarner Losh 5237270962aSWarner Losh #undef ASSERT 5247270962aSWarner Losh #define ASSERT(x) 5257270962aSWarner Losh 5267270962aSWarner Losh /* 5277270962aSWarner Losh * Define AllocateCopyPool and others so that we "forget" about the 5287270962aSWarner Losh * previous non-static deifnition since we want these to be static 5297270962aSWarner Losh * inlines. 5307270962aSWarner Losh */ 5317270962aSWarner Losh #define AllocateCopyPool AllocateCopyPoolFreeBSD 5327270962aSWarner Losh #define CompareGuid CompareGuidFreeBSD 5337270962aSWarner Losh #define StrHexToBytes StrHexToBytesFreeBSD 5347270962aSWarner Losh #define StrToGuid StrToGuidFreeBSD 5357270962aSWarner Losh #define WriteUnaligned64 WriteUnaligned64FreeBSD 5367270962aSWarner Losh 5377270962aSWarner Losh static inline void * 5387270962aSWarner Losh AllocateCopyPool(size_t l, const void *p) 5397270962aSWarner Losh { 5407270962aSWarner Losh void *rv; 5417270962aSWarner Losh 5427270962aSWarner Losh rv = malloc(l); 5437270962aSWarner Losh if (rv == NULL) 5447270962aSWarner Losh return NULL; 5457270962aSWarner Losh memcpy(rv, p, l); 5467270962aSWarner Losh return (rv); 5477270962aSWarner Losh } 5487270962aSWarner Losh 5497270962aSWarner Losh static inline BOOLEAN 5507270962aSWarner Losh CompareGuid (const GUID *g1, const GUID *g2) 5517270962aSWarner Losh { 5527270962aSWarner Losh uint32_t ignored_status; 5537270962aSWarner Losh 5547270962aSWarner Losh return (uuid_compare((const uuid_t *)g1, (const uuid_t *)g2, 5557270962aSWarner Losh &ignored_status) == 0); 5567270962aSWarner Losh } 5577270962aSWarner Losh 5587270962aSWarner Losh static inline int 5597270962aSWarner Losh StrHexToBytes(const char *str, size_t len, uint8_t *buf, size_t buflen) 5607270962aSWarner Losh { 5617270962aSWarner Losh size_t i; 5627270962aSWarner Losh char hex[3]; 5637270962aSWarner Losh 5647270962aSWarner Losh /* 5657270962aSWarner Losh * Sanity check preconditions. 5667270962aSWarner Losh */ 56703307d7aSWarner Losh if (buflen != len / 2 || (len % 2) == 1) 5687270962aSWarner Losh return 1; 5697270962aSWarner Losh for (i = 0; i < len; i += 2) { 5707270962aSWarner Losh if (!isxdigit(str[i]) || !isxdigit(str[i + 1])) 5717270962aSWarner Losh return 1; 5727270962aSWarner Losh hex[0] = str[i]; 5737270962aSWarner Losh hex[1] = str[i + 1]; 5747270962aSWarner Losh hex[2] = '\0'; 5757270962aSWarner Losh buf[i / 2] = strtol(hex, NULL, 16); 5767270962aSWarner Losh } 5777270962aSWarner Losh return 0; 5787270962aSWarner Losh } 5797270962aSWarner Losh 5807270962aSWarner Losh static inline void 5817270962aSWarner Losh StrToGuid(const char *str, GUID *guid) 5827270962aSWarner Losh { 5837270962aSWarner Losh uint32_t status; 5847270962aSWarner Losh 5857270962aSWarner Losh uuid_from_string(str, (uuid_t *)guid, &status); 5867270962aSWarner Losh } 5877270962aSWarner Losh 5887270962aSWarner Losh static inline void 5897270962aSWarner Losh WriteUnaligned64(void *ptr, uint64_t val) 5907270962aSWarner Losh { 5917270962aSWarner Losh memcpy(ptr, &val, sizeof(val)); 5927270962aSWarner Losh } 5937270962aSWarner Losh 5947270962aSWarner Losh /* 5957270962aSWarner Losh * Hack to allow converting %g to %s in printfs. Hack because 5967270962aSWarner Losh * it's single entry, uses a static buffer, etc. Sufficient for 5977270962aSWarner Losh * the day for this file though. IF you ever have to convert 5987270962aSWarner Losh * two %g's in one format, punt. Did I mention this was super lame. 5997270962aSWarner Losh * Not to mention it's name.... Also, the error GUID is horrific. 6007270962aSWarner Losh */ 6017270962aSWarner Losh static inline const char * 6027270962aSWarner Losh guid_str(const GUID *g) 6037270962aSWarner Losh { 6047270962aSWarner Losh static char buf[36 + 1]; 6057270962aSWarner Losh char *str = NULL; 6067270962aSWarner Losh int32_t ignored_status; 6077270962aSWarner Losh 6087270962aSWarner Losh uuid_to_string((const uuid_t *)g, &str, &ignored_status); 6097270962aSWarner Losh if (str != NULL) 6107270962aSWarner Losh strlcpy(buf, str, sizeof(buf)); 6117270962aSWarner Losh else 6127270962aSWarner Losh strlcpy(buf, "groot-cannot-decode-guid-groot-smash", 6137270962aSWarner Losh sizeof(buf)); /* ^^^^^^^ 36 characters ^^^^^^^ */ 6147270962aSWarner Losh free(str); 6157270962aSWarner Losh return buf; 6167270962aSWarner Losh } 6177270962aSWarner Losh #define G(x) guid_str((const GUID *)(const void *)x) 6187270962aSWarner Losh #endif 6197270962aSWarner Losh 6207270962aSWarner Losh #undef GLOBAL_REMOVE_IF_UNREFERENCED 6217270962aSWarner Losh #define GLOBAL_REMOVE_IF_UNREFERENCED static 6227270962aSWarner Losh 6237270962aSWarner Losh #endif 624