bc092b
From 653191c5d89a6b3e0b70c339fb6dca0f0b682030 Mon Sep 17 00:00:00 2001
f4c76c
From: Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
f4c76c
Date: Tue, 27 Nov 2012 17:18:53 -0200
bc092b
Subject: [PATCH 062/176] DHCP client ID and UUID options added.
f4c76c
f4c76c
---
f4c76c
 grub-core/net/bootp.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++-----
f4c76c
 include/grub/net.h    |  2 ++
f4c76c
 2 files changed, 81 insertions(+), 8 deletions(-)
f4c76c
f4c76c
diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c
bc092b
index 9e2fdb795..f03eeab2f 100644
f4c76c
--- a/grub-core/net/bootp.c
f4c76c
+++ b/grub-core/net/bootp.c
f4c76c
@@ -25,6 +25,49 @@
f4c76c
 #include <grub/net/udp.h>
f4c76c
 #include <grub/datetime.h>
f4c76c
 
f4c76c
+static char *
f4c76c
+grub_env_write_readonly (struct grub_env_var *var __attribute__ ((unused)),
f4c76c
+			 const char *val __attribute__ ((unused)))
f4c76c
+{
f4c76c
+  return NULL;
f4c76c
+}
f4c76c
+
f4c76c
+static void
f4c76c
+set_env_limn_ro (const char *intername, const char *suffix,
f4c76c
+		 const char *value, grub_size_t len)
f4c76c
+{
f4c76c
+  char *varname, *varvalue;
f4c76c
+  char *ptr;
f4c76c
+  varname = grub_xasprintf ("net_%s_%s", intername, suffix);
f4c76c
+  if (!varname)
f4c76c
+    return;
f4c76c
+  for (ptr = varname; *ptr; ptr++)
f4c76c
+    if (*ptr == ':')
f4c76c
+      *ptr = '_';
f4c76c
+  varvalue = grub_malloc (len + 1);
f4c76c
+  if (!varvalue)
f4c76c
+    {
f4c76c
+      grub_free (varname);
f4c76c
+      return;
f4c76c
+    }
f4c76c
+
f4c76c
+  grub_memcpy (varvalue, value, len);
f4c76c
+  varvalue[len] = 0;
f4c76c
+  grub_env_set (varname, varvalue);
f4c76c
+  grub_register_variable_hook (varname, 0, grub_env_write_readonly);
f4c76c
+  grub_env_export (varname);
f4c76c
+  grub_free (varname);
f4c76c
+  grub_free (varvalue);
f4c76c
+}
f4c76c
+
f4c76c
+static char
f4c76c
+hexdigit (grub_uint8_t val)
f4c76c
+{
f4c76c
+  if (val < 10)
f4c76c
+    return val + '0';
f4c76c
+  return val + 'a' - 10;
f4c76c
+}
f4c76c
+
f4c76c
 static void
f4c76c
 parse_dhcp_vendor (const char *name, const void *vend, int limit, int *mask)
f4c76c
 {
f4c76c
@@ -55,6 +98,9 @@ parse_dhcp_vendor (const char *name, const void *vend, int limit, int *mask)
f4c76c
 
f4c76c
       taglength = *ptr++;
f4c76c
 
f4c76c
+      grub_dprintf("net", "DHCP option %u (0x%02x) found with length %u.\n",
f4c76c
+                   tagtype, tagtype, taglength);
f4c76c
+
f4c76c
       switch (tagtype)
f4c76c
 	{
f4c76c
 	case GRUB_NET_BOOTP_NETMASK:
f4c76c
@@ -120,6 +166,39 @@ parse_dhcp_vendor (const char *name, const void *vend, int limit, int *mask)
f4c76c
                                      taglength);
f4c76c
           break;
f4c76c
 
f4c76c
+        case GRUB_NET_BOOTP_CLIENT_ID:
f4c76c
+	  set_env_limn_ro (name, "clientid", (char *) ptr, taglength);
f4c76c
+          break;
f4c76c
+
f4c76c
+        case GRUB_NET_BOOTP_CLIENT_UUID:
f4c76c
+            {
f4c76c
+              if (taglength != 17)
f4c76c
+                break;
f4c76c
+
f4c76c
+              /* The format is 9cfe245e-d0c8-bd45-a79f-54ea5fbd3d97 */
f4c76c
+
f4c76c
+              ptr += 1;
f4c76c
+              taglength -= 1;
f4c76c
+
f4c76c
+              char *val = grub_malloc (2 * taglength + 4 + 1);
f4c76c
+              int i = 0;
f4c76c
+              int j = 0;
f4c76c
+              for (i = 0; i < taglength; i++)
f4c76c
+                {
f4c76c
+                  val[2 * i + j] = hexdigit (ptr[i] >> 4);
f4c76c
+                  val[2 * i + 1 + j] = hexdigit (ptr[i] & 0xf);
f4c76c
+
f4c76c
+                  if ((i == 3) || (i == 5) || (i == 7) || (i == 9))
f4c76c
+                    {
f4c76c
+                      j++;
f4c76c
+                      val[2 * i + 1+ j] = '-';
f4c76c
+                    }
f4c76c
+                }
f4c76c
+
f4c76c
+              set_env_limn_ro (name, "clientuuid", (char *) val, 2 * taglength + 4);
f4c76c
+            }
f4c76c
+          break;
f4c76c
+
f4c76c
 	  /* If you need any other options please contact GRUB
f4c76c
 	     development team.  */
f4c76c
 	}
bc092b
@@ -302,14 +381,6 @@ grub_net_process_dhcp (struct grub_net_buff *nb,
f4c76c
     }
f4c76c
 }
f4c76c
 
f4c76c
-static char
f4c76c
-hexdigit (grub_uint8_t val)
f4c76c
-{
f4c76c
-  if (val < 10)
f4c76c
-    return val + '0';
f4c76c
-  return val + 'a' - 10;
f4c76c
-}
f4c76c
-
f4c76c
 static grub_err_t
f4c76c
 grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)),
f4c76c
 		  int argc, char **args)
f4c76c
diff --git a/include/grub/net.h b/include/grub/net.h
bc092b
index 1096b2432..e266bae23 100644
f4c76c
--- a/include/grub/net.h
f4c76c
+++ b/include/grub/net.h
bc092b
@@ -457,6 +457,8 @@ enum
f4c76c
     GRUB_NET_BOOTP_DOMAIN = 0x0f,
f4c76c
     GRUB_NET_BOOTP_ROOT_PATH = 0x11,
f4c76c
     GRUB_NET_BOOTP_EXTENSIONS_PATH = 0x12,
f4c76c
+    GRUB_NET_BOOTP_CLIENT_ID = 0x3d,
f4c76c
+    GRUB_NET_BOOTP_CLIENT_UUID = 0x61,
f4c76c
     GRUB_NET_BOOTP_END = 0xff
f4c76c
   };
f4c76c
 
f4c76c
-- 
bc092b
2.13.0
f4c76c