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