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