git: d9d1a1e7ab29 - main - libefivar: Add DevPathFromTextDns and DevPathToTextDns libraries

From: Warner Losh <imp_at_FreeBSD.org>
Date: Sun, 27 Feb 2022 16:47:40 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=d9d1a1e7ab29b4645415f5f572faea41efc5a0c2

commit d9d1a1e7ab29b4645415f5f572faea41efc5a0c2
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-02-24 00:39:13 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2022-02-27 16:12:29 +0000

    libefivar: Add DevPathFromTextDns and DevPathToTextDns libraries
    
    V3:
    * Fix the bug in DevPathFromTextDns()
    
    V2:
    * Add no IP instance case check.
    
    Obtained from:  https://github.com/tianocore/edk2/commit/9b9d0655c1547384c2b34cea94eab4b8b83bd1b9
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/581
---
 lib/libefivar/efivar-dp-format.c | 46 ++++++++++++++++++++
 lib/libefivar/efivar-dp-parse.c  | 94 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 140 insertions(+)

diff --git a/lib/libefivar/efivar-dp-format.c b/lib/libefivar/efivar-dp-format.c
index 667a794b34db..8aa8a4798389 100644
--- a/lib/libefivar/efivar-dp-format.c
+++ b/lib/libefivar/efivar-dp-format.c
@@ -1758,6 +1758,51 @@ DevPathToTextBluetoothLE (
     );
 }
 
+/**
+  Converts a DNS device path structure to its string representative.
+
+  @param Str             The string representative of input device.
+  @param DevPath         The input device path structure.
+  @param DisplayOnly     If DisplayOnly is TRUE, then the shorter text representation
+                         of the display node is used, where applicable. If DisplayOnly
+                         is FALSE, then the longer text representation of the display node
+                         is used.
+  @param AllowShortcuts  If AllowShortcuts is TRUE, then the shortcut forms of text
+                         representation for a device node can be used, where applicable.
+
+**/
+static VOID
+DevPathToTextDns (
+  IN OUT POOL_PRINT  *Str,
+  IN VOID            *DevPath,
+  IN BOOLEAN         DisplayOnly,
+  IN BOOLEAN         AllowShortcuts
+  )
+{
+  DNS_DEVICE_PATH  *DnsDevPath;
+  UINT32           DnsServerIpCount;
+  UINT32           DnsServerIpIndex;
+
+  DnsDevPath     = DevPath;
+  DnsServerIpCount = (UINT32) (DevicePathNodeLength(DnsDevPath) - sizeof (EFI_DEVICE_PATH_PROTOCOL) - sizeof (DnsDevPath->IsIPv6)) / sizeof (EFI_IP_ADDRESS);
+
+  UefiDevicePathLibCatPrint (Str, "Dns(");
+
+  for (DnsServerIpIndex = 0; DnsServerIpIndex < DnsServerIpCount; DnsServerIpIndex++) {
+    if (DnsDevPath->IsIPv6 == 0x00) {
+      CatIPv4Address (Str, &(DnsDevPath->DnsServerIp[DnsServerIpIndex].v4));
+    } else {
+      CatIPv6Address (Str, &(DnsDevPath->DnsServerIp[DnsServerIpIndex].v6));
+    }
+
+    if (DnsServerIpIndex < DnsServerIpCount - 1) {
+      UefiDevicePathLibCatPrint (Str, ",");
+    }
+  }
+
+  UefiDevicePathLibCatPrint (Str, ")");
+}
+
 /**
   Converts a URI device path structure to its string representative.
 
@@ -2291,6 +2336,7 @@ static const DEVICE_PATH_TO_TEXT_TABLE mUefiDevicePathLibToTextTable[] = {
   {MESSAGING_DEVICE_PATH, MSG_VENDOR_DP,                    DevPathToTextVendor         },
   {MESSAGING_DEVICE_PATH, MSG_ISCSI_DP,                     DevPathToTextiSCSI          },
   {MESSAGING_DEVICE_PATH, MSG_VLAN_DP,                      DevPathToTextVlan           },
+  {MESSAGING_DEVICE_PATH, MSG_DNS_DP,                       DevPathToTextDns            },
   {MESSAGING_DEVICE_PATH, MSG_URI_DP,                       DevPathToTextUri            },
   {MESSAGING_DEVICE_PATH, MSG_BLUETOOTH_DP,                 DevPathToTextBluetooth      },
   {MESSAGING_DEVICE_PATH, MSG_WIFI_DP,                      DevPathToTextWiFi           },
diff --git a/lib/libefivar/efivar-dp-parse.c b/lib/libefivar/efivar-dp-parse.c
index 80270aad1d5c..b755ea4a6146 100644
--- a/lib/libefivar/efivar-dp-parse.c
+++ b/lib/libefivar/efivar-dp-parse.c
@@ -2872,6 +2872,99 @@ DevPathFromTextBluetoothLE (
   return (EFI_DEVICE_PATH_PROTOCOL *) BluetoothLeDp;
 }
 
+/**
+  Converts a text device path node to DNS device path structure.
+
+  @param TextDeviceNode  The input Text device path node.
+
+  @return A pointer to the newly-created DNS device path structure.
+
+**/
+static
+EFI_DEVICE_PATH_PROTOCOL *
+DevPathFromTextDns (
+  IN CHAR16 *TextDeviceNode
+  )
+{
+  CHAR16            *DeviceNodeStr;
+  CHAR16            *DeviceNodeStrPtr;
+  UINT32            DnsServerIpCount;
+  UINT16            DnsDeviceNodeLength;
+  DNS_DEVICE_PATH   *DnsDeviceNode;
+  UINT32            DnsServerIpIndex;
+  CHAR16            *DnsServerIp;
+
+
+  //
+  // Count the DNS server address number.
+  //
+  DeviceNodeStr = UefiDevicePathLibStrDuplicate (TextDeviceNode);
+  if (DeviceNodeStr == NULL) {
+    return NULL;
+  }
+
+  DeviceNodeStrPtr = DeviceNodeStr;
+
+  DnsServerIpCount = 0;
+  while (DeviceNodeStrPtr != NULL && *DeviceNodeStrPtr != '\0') {
+    GetNextParamStr (&DeviceNodeStrPtr);
+    DnsServerIpCount ++;
+  }
+
+  FreePool (DeviceNodeStr);
+  DeviceNodeStr = NULL;
+
+  //
+  // One or more instances of the DNS server address in EFI_IP_ADDRESS,
+  // otherwise, NULL will be returned.
+  //
+  if (DnsServerIpCount == 0) {
+    return NULL;
+  }
+
+  //
+  // Create the DNS DeviceNode.
+  //
+  DnsDeviceNodeLength = (UINT16) (sizeof (EFI_DEVICE_PATH_PROTOCOL) + sizeof (UINT8) + DnsServerIpCount * sizeof (EFI_IP_ADDRESS));
+  DnsDeviceNode       = (DNS_DEVICE_PATH *) CreateDeviceNode (
+                                              MESSAGING_DEVICE_PATH,
+                                              MSG_DNS_DP,
+                                              DnsDeviceNodeLength
+                                              );
+  if (DnsDeviceNode == NULL) {
+    return NULL;
+  }
+
+  //
+  // Confirm the DNS server address is IPv4 or IPv6 type.
+  //
+  DeviceNodeStrPtr = TextDeviceNode;
+  while (!IS_NULL (*DeviceNodeStrPtr)) {
+    if (*DeviceNodeStrPtr == '.') {
+      DnsDeviceNode->IsIPv6 = 0x00;
+      break;
+    }
+
+    if (*DeviceNodeStrPtr == ':') {
+      DnsDeviceNode->IsIPv6 = 0x01;
+      break;
+    }
+
+    DeviceNodeStrPtr++;
+  }
+
+  for (DnsServerIpIndex = 0; DnsServerIpIndex < DnsServerIpCount; DnsServerIpIndex++) {
+    DnsServerIp = GetNextParamStr (&TextDeviceNode);
+    if (DnsDeviceNode->IsIPv6 == 0x00) {
+      StrToIpv4Address (DnsServerIp,  NULL, &(DnsDeviceNode->DnsServerIp[DnsServerIpIndex].v4), NULL);
+    } else {
+      StrToIpv6Address (DnsServerIp, NULL, &(DnsDeviceNode->DnsServerIp[DnsServerIpIndex].v6), NULL);
+    }
+  }
+
+  return (EFI_DEVICE_PATH_PROTOCOL *) DnsDeviceNode;
+}
+
 /**
   Converts a text device path node to URI device path structure.
 
@@ -3575,6 +3668,7 @@ GLOBAL_REMOVE_IF_UNREFERENCED DEVICE_PATH_FROM_TEXT_TABLE mUefiDevicePathLibDevP
   {"Unit",                    DevPathFromTextUnit                    },
   {"iSCSI",                   DevPathFromTextiSCSI                   },
   {"Vlan",                    DevPathFromTextVlan                    },
+  {"Dns",                     DevPathFromTextDns                     },
   {"Uri",                     DevPathFromTextUri                     },
   {"Bluetooth",               DevPathFromTextBluetooth               },
   {"Wi-Fi",                   DevPathFromTextWiFi                    },