/*-1* Copyright (c) 2017 Netflix, Inc.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22* SUCH DAMAGE.23*/2425/*26* Taken from MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.h27*/2829/** @file30Definition for Device Path library.3132Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>33SPDX-License-Identifier: BSD-2-Clause-Patent3435**/3637#ifndef _UEFI_DEVICE_PATH_LIB_H_38#define _UEFI_DEVICE_PATH_LIB_H_3940#include <Uefi.h>41#include <Protocol/DevicePathUtilities.h>42#include <Protocol/DebugPort.h>43#include <Protocol/DevicePathToText.h>44#include <Protocol/DevicePathFromText.h>45#include <Guid/PcAnsi.h>46#include <Library/DebugLib.h>47#include <Library/PrintLib.h>48#include <Library/BaseLib.h>49#include <Library/BaseMemoryLib.h>50#include <Library/MemoryAllocationLib.h>51#include <Library/UefiBootServicesTableLib.h>52#include <Library/DevicePathLib.h>53#include <Library/PcdLib.h>54#include <IndustryStandard/Bluetooth.h>5556#define IS_COMMA(a) ((a) == ',')57#define IS_HYPHEN(a) ((a) == '-')58#define IS_DOT(a) ((a) == '.')59#define IS_LEFT_PARENTH(a) ((a) == '(')60#define IS_RIGHT_PARENTH(a) ((a) == ')')61#define IS_SLASH(a) ((a) == '/')62#define IS_NULL(a) ((a) == '\0')6364//65// Private Data structure66//67typedef struct {68char *Str;69UINTN Count;70UINTN Capacity;71} POOL_PRINT;7273typedef74EFI_DEVICE_PATH_PROTOCOL *75(*DEVICE_PATH_FROM_TEXT) (76IN char *Str77);7879typedef80VOID81(*DEVICE_PATH_TO_TEXT) (82IN OUT POOL_PRINT *Str,83IN VOID *DevicePath,84IN BOOLEAN DisplayOnly,85IN BOOLEAN AllowShortcuts86);8788typedef struct {89UINT8 Type;90UINT8 SubType;91DEVICE_PATH_TO_TEXT Function;92} DEVICE_PATH_TO_TEXT_TABLE;9394typedef struct {95UINT8 Type;96const char *Text;97} DEVICE_PATH_TO_TEXT_GENERIC_TABLE;9899typedef struct {100const char *DevicePathNodeText;101DEVICE_PATH_FROM_TEXT Function;102} DEVICE_PATH_FROM_TEXT_TABLE;103104typedef struct {105BOOLEAN ClassExist;106UINT8 Class;107BOOLEAN SubClassExist;108UINT8 SubClass;109} USB_CLASS_TEXT;110111#define USB_CLASS_AUDIO 1112#define USB_CLASS_CDCCONTROL 2113#define USB_CLASS_HID 3114#define USB_CLASS_IMAGE 6115#define USB_CLASS_PRINTER 7116#define USB_CLASS_MASS_STORAGE 8117#define USB_CLASS_HUB 9118#define USB_CLASS_CDCDATA 10119#define USB_CLASS_SMART_CARD 11120#define USB_CLASS_VIDEO 14121#define USB_CLASS_DIAGNOSTIC 220122#define USB_CLASS_WIRELESS 224123124#define USB_CLASS_RESERVE 254125#define USB_SUBCLASS_FW_UPDATE 1126#define USB_SUBCLASS_IRDA_BRIDGE 2127#define USB_SUBCLASS_TEST 3128129#define RFC_1700_UDP_PROTOCOL 17130#define RFC_1700_TCP_PROTOCOL 6131132#pragma pack(1)133134typedef struct {135EFI_DEVICE_PATH_PROTOCOL Header;136EFI_GUID Guid;137UINT8 VendorDefinedData[1];138} VENDOR_DEFINED_HARDWARE_DEVICE_PATH;139140typedef struct {141EFI_DEVICE_PATH_PROTOCOL Header;142EFI_GUID Guid;143UINT8 VendorDefinedData[1];144} VENDOR_DEFINED_MESSAGING_DEVICE_PATH;145146typedef struct {147EFI_DEVICE_PATH_PROTOCOL Header;148EFI_GUID Guid;149UINT8 VendorDefinedData[1];150} VENDOR_DEFINED_MEDIA_DEVICE_PATH;151152typedef struct {153EFI_DEVICE_PATH_PROTOCOL Header;154UINT32 Hid;155UINT32 Uid;156UINT32 Cid;157CHAR8 HidUidCidStr[3];158} ACPI_EXTENDED_HID_DEVICE_PATH_WITH_STR;159160typedef struct {161EFI_DEVICE_PATH_PROTOCOL Header;162UINT16 NetworkProtocol;163UINT16 LoginOption;164UINT64 Lun;165UINT16 TargetPortalGroupTag;166CHAR8 TargetName[1];167} ISCSI_DEVICE_PATH_WITH_NAME;168169typedef struct {170EFI_DEVICE_PATH_PROTOCOL Header;171EFI_GUID Guid;172UINT8 VendorDefinedData[1];173} VENDOR_DEVICE_PATH_WITH_DATA;174175#pragma pack()176177#ifndef __FreeBSD__ /* Remove these on FreeBSD */178/**179Returns the size of a device path in bytes.180181This function returns the size, in bytes, of the device path data structure182specified by DevicePath including the end of device path node.183If DevicePath is NULL or invalid, then 0 is returned.184185@param DevicePath A pointer to a device path data structure.186187@retval 0 If DevicePath is NULL or invalid.188@retval Others The size of a device path in bytes.189190**/191UINTN192EFIAPI193UefiDevicePathLibGetDevicePathSize (194IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath195);196197/**198Creates a new copy of an existing device path.199200This function allocates space for a new copy of the device path specified by DevicePath.201If DevicePath is NULL, then NULL is returned. If the memory is successfully202allocated, then the contents of DevicePath are copied to the newly allocated203buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.204The memory for the new device path is allocated from EFI boot services memory.205It is the responsibility of the caller to free the memory allocated.206207@param DevicePath A pointer to a device path data structure.208209@retval NULL DevicePath is NULL or invalid.210@retval Others A pointer to the duplicated device path.211212**/213EFI_DEVICE_PATH_PROTOCOL *214EFIAPI215UefiDevicePathLibDuplicateDevicePath (216IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath217);218219/**220Creates a new device path by appending a second device path to a first device path.221222This function creates a new device path by appending a copy of SecondDevicePath223to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path224device node from SecondDevicePath is retained. The newly created device path is225returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of226SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,227and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and228SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.229230If there is not enough memory for the newly allocated buffer, then NULL is returned.231The memory for the new device path is allocated from EFI boot services memory.232It is the responsibility of the caller to free the memory allocated.233234@param FirstDevicePath A pointer to a device path data structure.235@param SecondDevicePath A pointer to a device path data structure.236237@retval NULL If there is not enough memory for the newly allocated buffer.238@retval NULL If FirstDevicePath or SecondDevicePath is invalid.239@retval Others A pointer to the new device path if success.240Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.241242**/243EFI_DEVICE_PATH_PROTOCOL *244EFIAPI245UefiDevicePathLibAppendDevicePath (246IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath OPTIONAL,247IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL248);249250/**251Creates a new path by appending the device node to the device path.252253This function creates a new device path by appending a copy of the device node254specified by DevicePathNode to a copy of the device path specified by DevicePath255in an allocated buffer. The end-of-device-path device node is moved after the256end of the appended device node.257If DevicePathNode is NULL then a copy of DevicePath is returned.258If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device259path device node is returned.260If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path261device node is returned.262If there is not enough memory to allocate space for the new device path, then263NULL is returned.264The memory is allocated from EFI boot services memory. It is the responsibility265of the caller to free the memory allocated.266267@param DevicePath A pointer to a device path data structure.268@param DevicePathNode A pointer to a single device path node.269270@retval NULL If there is not enough memory for the new device path.271@retval Others A pointer to the new device path if success.272A copy of DevicePathNode followed by an end-of-device-path node273if both FirstDevicePath and SecondDevicePath are NULL.274A copy of an end-of-device-path node if both FirstDevicePath275and SecondDevicePath are NULL.276277**/278EFI_DEVICE_PATH_PROTOCOL *279EFIAPI280UefiDevicePathLibAppendDevicePathNode (281IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,282IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL283);284285/**286Creates a new device path by appending the specified device path instance to the specified device287path.288289This function creates a new device path by appending a copy of the device path290instance specified by DevicePathInstance to a copy of the device path specified291by DevicePath in a allocated buffer.292The end-of-device-path device node is moved after the end of the appended device293path instance and a new end-of-device-path-instance node is inserted between.294If DevicePath is NULL, then a copy if DevicePathInstance is returned.295If DevicePathInstance is NULL, then NULL is returned.296If DevicePath or DevicePathInstance is invalid, then NULL is returned.297If there is not enough memory to allocate space for the new device path, then298NULL is returned.299The memory is allocated from EFI boot services memory. It is the responsibility300of the caller to free the memory allocated.301302@param DevicePath A pointer to a device path data structure.303@param DevicePathInstance A pointer to a device path instance.304305@return A pointer to the new device path.306307**/308EFI_DEVICE_PATH_PROTOCOL *309EFIAPI310UefiDevicePathLibAppendDevicePathInstance (311IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,312IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL313);314315/**316Creates a copy of the current device path instance and returns a pointer to the next device path317instance.318319This function creates a copy of the current device path instance. It also updates320DevicePath to point to the next device path instance in the device path (or NULL321if no more) and updates Size to hold the size of the device path instance copy.322If DevicePath is NULL, then NULL is returned.323If DevicePath points to a invalid device path, then NULL is returned.324If there is not enough memory to allocate space for the new device path, then325NULL is returned.326The memory is allocated from EFI boot services memory. It is the responsibility327of the caller to free the memory allocated.328If Size is NULL, then ASSERT().329330@param DevicePath On input, this holds the pointer to the current331device path instance. On output, this holds332the pointer to the next device path instance333or NULL if there are no more device path334instances in the device path pointer to a335device path data structure.336@param Size On output, this holds the size of the device337path instance, in bytes or zero, if DevicePath338is NULL.339340@return A pointer to the current device path instance.341342**/343EFI_DEVICE_PATH_PROTOCOL *344EFIAPI345UefiDevicePathLibGetNextDevicePathInstance (346IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,347OUT UINTN *Size348);349350/**351Creates a device node.352353This function creates a new device node in a newly allocated buffer of size354NodeLength and initializes the device path node header with NodeType and NodeSubType.355The new device path node is returned.356If NodeLength is smaller than a device path header, then NULL is returned.357If there is not enough memory to allocate space for the new device path, then358NULL is returned.359The memory is allocated from EFI boot services memory. It is the responsibility360of the caller to free the memory allocated.361362@param NodeType The device node type for the new device node.363@param NodeSubType The device node sub-type for the new device node.364@param NodeLength The length of the new device node.365366@return The new device path.367368**/369EFI_DEVICE_PATH_PROTOCOL *370EFIAPI371UefiDevicePathLibCreateDeviceNode (372IN UINT8 NodeType,373IN UINT8 NodeSubType,374IN UINT16 NodeLength375);376377/**378Determines if a device path is single or multi-instance.379380This function returns TRUE if the device path specified by DevicePath is381multi-instance.382Otherwise, FALSE is returned.383If DevicePath is NULL or invalid, then FALSE is returned.384385@param DevicePath A pointer to a device path data structure.386387@retval TRUE DevicePath is multi-instance.388@retval FALSE DevicePath is not multi-instance, or DevicePath389is NULL or invalid.390391**/392BOOLEAN393EFIAPI394UefiDevicePathLibIsDevicePathMultiInstance (395IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath396);397398/**399Converts a device path to its text representation.400401@param DevicePath A Pointer to the device to be converted.402@param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation403of the display node is used, where applicable. If DisplayOnly404is FALSE, then the longer text representation of the display node405is used.406@param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text407representation for a device node can be used, where applicable.408409@return A pointer to the allocated text representation of the device path or410NULL if DeviceNode is NULL or there was insufficient memory.411412**/413CHAR16 *414EFIAPI415UefiDevicePathLibConvertDevicePathToText (416IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,417IN BOOLEAN DisplayOnly,418IN BOOLEAN AllowShortcuts419);420421/**422Converts a device node to its string representation.423424@param DeviceNode A Pointer to the device node to be converted.425@param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation426of the display node is used, where applicable. If DisplayOnly427is FALSE, then the longer text representation of the display node428is used.429@param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text430representation for a device node can be used, where applicable.431432@return A pointer to the allocated text representation of the device node or NULL if DeviceNode433is NULL or there was insufficient memory.434435**/436CHAR16 *437EFIAPI438UefiDevicePathLibConvertDeviceNodeToText (439IN CONST EFI_DEVICE_PATH_PROTOCOL *DeviceNode,440IN BOOLEAN DisplayOnly,441IN BOOLEAN AllowShortcuts442);443444/**445Convert text to the binary representation of a device node.446447@param TextDeviceNode TextDeviceNode points to the text representation of a device448node. Conversion starts with the first character and continues449until the first non-device node character.450451@return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was452insufficient memory or text unsupported.453454**/455EFI_DEVICE_PATH_PROTOCOL *456EFIAPI457UefiDevicePathLibConvertTextToDeviceNode (458IN CONST CHAR16 *TextDeviceNode459);460461/**462Convert text to the binary representation of a device path.463464465@param TextDevicePath TextDevicePath points to the text representation of a device466path. Conversion starts with the first character and continues467until the first non-device node character.468469@return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or470there was insufficient memory.471472**/473EFI_DEVICE_PATH_PROTOCOL *474EFIAPI475UefiDevicePathLibConvertTextToDevicePath (476IN CONST CHAR16 *TextDevicePath477);478#else479480/*481* Small FreeBSD shim layer. Fast and lose hacks to make this code work with FreeBSD.482*/483484#include <ctype.h>485486#define _PCD_GET_MODE_32_PcdMaximumDevicePathNodeCount 1000487#define MAX_UINTN UINTPTR_MAX488489#define AllocatePool(x) malloc(x)490#define AllocateZeroPool(x) calloc(1,x)491#define AsciiStrLen(s) strlen(s)492#define CopyGuid(dst, src) memcpy(dst, src, sizeof(uuid_t))493#define CopyMem(d, s, l) memcpy(d, s, l)494#define FreePool(x) free(x)495#define LShiftU64(x, s) ((x) << s)496#define ReadUnaligned64(x) le64dec(x)497#define ReallocatePool(old, new, ptr) realloc(ptr, new)498/*499* Quirky StrCmp returns 0 if equal, 1 if not. This is what the code500* expects, though that expectation is likely a bug (it casts the501* return value. EDK2's StrCmp returns values just like C's strcmp,502* but the parse code casts this to an UINTN, which is bogus. This503* definition papers over that bogusness to do the right thing. If504* iSCSI protocol string processing is ever fixed, we can remove this505* bletcherous kludge.506*/507#define StrCmp(a, b) (strcmp(a, b) != 0)508#define StrCpyS(d, l, s) strcpy(d, s)509#define StrHexToUint64(x) strtoll(x, NULL, 16)510#define StrHexToUintn(x) strtoll(x, NULL, 16)511#define StrLen(x) strlen(x)512#define StrSize(x) (strlen(x) + 1)513#define StrnCatS(d, l, s, len) strncat(d, s, len)514#define StrnCmp(a, b, n) strncmp(a, b, n)515#define StrnLenS(str, max) strlen(str)516#define Strtoi(x) strtol(x, NULL, 0)517#define Strtoi64(x, y) *(long long *)y = strtoll(x, NULL, 0)518#define SwapBytes64(u64) bswap64(u64)519#define UnicodeStrToAsciiStrS(src, dest, len) strlcpy(dest, src, len)520#define ZeroMem(p,l) memset(p, 0, l)521522#undef ASSERT523#define ASSERT(x)524525/*526* Define AllocateCopyPool and others so that we "forget" about the527* previous non-static deifnition since we want these to be static528* inlines.529*/530#define AllocateCopyPool AllocateCopyPoolFreeBSD531#define CompareGuid CompareGuidFreeBSD532#define StrHexToBytes StrHexToBytesFreeBSD533#define StrToGuid StrToGuidFreeBSD534#define WriteUnaligned64 WriteUnaligned64FreeBSD535536static inline void *537AllocateCopyPool(size_t l, const void *p)538{539void *rv;540541rv = malloc(l);542if (rv == NULL)543return NULL;544memcpy(rv, p, l);545return (rv);546}547548static inline BOOLEAN549CompareGuid (const EFI_GUID *g1, const EFI_GUID *g2)550{551uint32_t ignored_status;552553return (uuid_compare((const uuid_t *)g1, (const uuid_t *)g2,554&ignored_status) == 0);555}556557static inline int558StrHexToBytes(const char *str, size_t len, uint8_t *buf, size_t buflen)559{560size_t i;561char hex[3];562563/*564* Sanity check preconditions.565*/566if (buflen != len / 2 || (len % 2) == 1)567return 1;568for (i = 0; i < len; i += 2) {569if (!isxdigit(str[i]) || !isxdigit(str[i + 1]))570return 1;571hex[0] = str[i];572hex[1] = str[i + 1];573hex[2] = '\0';574buf[i / 2] = strtol(hex, NULL, 16);575}576return 0;577}578579static inline void580StrToGuid(const char *str, EFI_GUID *guid)581{582uint32_t status;583584uuid_from_string(str, (uuid_t *)guid, &status);585}586587static inline void588WriteUnaligned64(void *ptr, uint64_t val)589{590memcpy(ptr, &val, sizeof(val));591}592593/*594* Hack to allow converting %g to %s in printfs. Hack because595* it's single entry, uses a static buffer, etc. Sufficient for596* the day for this file though. IF you ever have to convert597* two %g's in one format, punt. Did I mention this was super lame.598* Not to mention it's name.... Also, the error GUID is horrific.599*/600static inline const char *601guid_str(const EFI_GUID *g)602{603static char buf[36 + 1];604char *str = NULL;605int32_t ignored_status;606607uuid_to_string((const uuid_t *)g, &str, &ignored_status);608if (str != NULL)609strlcpy(buf, str, sizeof(buf));610else611strlcpy(buf, "groot-cannot-decode-guid-groot-smash",612sizeof(buf)); /* ^^^^^^^ 36 characters ^^^^^^^ */613free(str);614return buf;615}616#define G(x) guid_str((const EFI_GUID *)(const void *)x)617#endif618619#undef GLOBAL_REMOVE_IF_UNREFERENCED620#define GLOBAL_REMOVE_IF_UNREFERENCED static621622#endif623624625