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