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