a5bd9f6
From 773d8cadb15a4bea164863268068e2f421b8324c Mon Sep 17 00:00:00 2001
5e0db24
From: Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
5e0db24
Date: Tue, 27 Nov 2012 17:22:07 -0200
a5bd9f6
Subject: [PATCH 359/364] Search for specific config file for netboot
5e0db24
5e0db24
This patch implements a search for a specific configuration when the config
5e0db24
file is on a remoteserver. It uses the following order:
5e0db24
   1) DHCP client UUID option.
5e0db24
   2) MAC address (in lower case hexadecimal with dash separators);
5e0db24
   3) IP (in upper case hexadecimal) or IPv6;
5e0db24
   4) The original grub.cfg file.
5e0db24
5e0db24
This procedure is similar to what is used by pxelinux and yaboot:
5e0db24
http://www.syslinux.org/wiki/index.php/PXELINUX#config
5e0db24
5e0db24
This should close the bugzilla:
5e0db24
https://bugzilla.redhat.com/show_bug.cgi?id=873406
5e0db24
---
a5bd9f6
 grub-core/net/net.c     | 118 ++++++++++++++++++++++++++++++++++++++++++++++++
a5bd9f6
 grub-core/normal/main.c |  18 ++++++--
a5bd9f6
 include/grub/net.h      |   3 ++
5e0db24
 3 files changed, 135 insertions(+), 4 deletions(-)
5e0db24
5e0db24
diff --git a/grub-core/net/net.c b/grub-core/net/net.c
a5bd9f6
index aebbe4b..9a601d7 100644
5e0db24
--- a/grub-core/net/net.c
5e0db24
+++ b/grub-core/net/net.c
5e0db24
@@ -1548,6 +1548,124 @@ grub_net_restore_hw (void)
5e0db24
   return GRUB_ERR_NONE;
5e0db24
 }
5e0db24
 
5e0db24
+grub_err_t
5e0db24
+grub_net_search_configfile (char *config)
5e0db24
+{
5e0db24
+  grub_size_t config_len;
5e0db24
+  char *suffix;
5e0db24
+
5e0db24
+  auto int search_through (grub_size_t num_tries, grub_size_t slice_size);
5e0db24
+  int search_through (grub_size_t num_tries, grub_size_t slice_size)
5e0db24
+    {
5e0db24
+      while (num_tries-- > 0)
5e0db24
+        {
5e0db24
+	  grub_dprintf ("net", "probe %s\n", config);
5e0db24
+
5e0db24
+          grub_file_t file;
5e0db24
+          file = grub_file_open (config);
5e0db24
+
5e0db24
+          if (file)
5e0db24
+            {
5e0db24
+              grub_file_close (file);
5e0db24
+              grub_dprintf ("net", "found!\n");
5e0db24
+              return 0;
5e0db24
+            }
5e0db24
+          else
5e0db24
+            {
5e0db24
+              if (grub_errno == GRUB_ERR_IO)
5e0db24
+                grub_errno = GRUB_ERR_NONE;
5e0db24
+            }
5e0db24
+
5e0db24
+          if (grub_strlen (suffix) < slice_size)
5e0db24
+            break;
5e0db24
+
5e0db24
+          config[grub_strlen (config) - slice_size] = '\0';
5e0db24
+        }
5e0db24
+
5e0db24
+      return 1;
5e0db24
+    }
5e0db24
+
5e0db24
+  config_len = grub_strlen (config);
5e0db24
+  config[config_len] = '-';
5e0db24
+  suffix = config + config_len + 1;
5e0db24
+
5e0db24
+  struct grub_net_network_level_interface *inf;
5e0db24
+  FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
5e0db24
+    {
5e0db24
+      /* By the Client UUID. */
5e0db24
+
5e0db24
+      char client_uuid_var[sizeof ("net_") + grub_strlen (inf->name) +
5e0db24
+                           sizeof ("_clientuuid") + 1];
5e0db24
+      grub_snprintf (client_uuid_var, sizeof (client_uuid_var),
5e0db24
+                     "net_%s_clientuuid", inf->name);
5e0db24
+
5e0db24
+      const char *client_uuid;
5e0db24
+      client_uuid = grub_env_get (client_uuid_var);
5e0db24
+
5e0db24
+      if (client_uuid)
5e0db24
+        {
5e0db24
+          grub_strcpy (suffix, client_uuid);
5e0db24
+          if (search_through (1, 0) == 0) return GRUB_ERR_NONE;
5e0db24
+        }
5e0db24
+
5e0db24
+      /* By the MAC address. */
5e0db24
+
5e0db24
+      /* Add ethernet type */
5e0db24
+      grub_strcpy (suffix, "01-");
5e0db24
+
5e0db24
+      grub_net_hwaddr_to_str (&inf->hwaddress, suffix + 3);
5e0db24
+
5e0db24
+      char *ptr;
5e0db24
+      for (ptr = suffix; *ptr; ptr++)
5e0db24
+        if (*ptr == ':')
5e0db24
+          *ptr = '-';
5e0db24
+
5e0db24
+      if (search_through (1, 0) == 0) return GRUB_ERR_NONE;
5e0db24
+
5e0db24
+      /* By IP address */
5e0db24
+
5e0db24
+      switch ((&inf->address)->type)
5e0db24
+        {
5e0db24
+        case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
5e0db24
+            {
5e0db24
+              grub_uint32_t n = grub_be_to_cpu32 ((&inf->address)->ipv4);
5e0db24
+              grub_snprintf (suffix, GRUB_NET_MAX_STR_ADDR_LEN, "%02X%02X%02X%02X", \
5e0db24
+                             ((n >> 24) & 0xff), ((n >> 16) & 0xff), \
5e0db24
+                             ((n >> 8) & 0xff), ((n >> 0) & 0xff));
5e0db24
+
5e0db24
+              if (search_through (8, 1) == 0) return GRUB_ERR_NONE;
5e0db24
+              break;
5e0db24
+            }
5e0db24
+        case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
5e0db24
+            {
5e0db24
+              char buf[GRUB_NET_MAX_STR_ADDR_LEN];
5e0db24
+              struct grub_net_network_level_address base;
5e0db24
+              base.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
5e0db24
+              grub_memcpy (&base.ipv6, ((&inf->address)->ipv6), 16);
5e0db24
+              grub_net_addr_to_str (&base, buf);
5e0db24
+
5e0db24
+              for (ptr = buf; *ptr; ptr++)
5e0db24
+                if (*ptr == ':')
5e0db24
+                  *ptr = '-';
5e0db24
+
5e0db24
+              grub_snprintf (suffix, GRUB_NET_MAX_STR_ADDR_LEN, "%s", buf);
5e0db24
+              if (search_through (1, 0) == 0) return GRUB_ERR_NONE;
5e0db24
+              break;
5e0db24
+            }
5e0db24
+        case GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV:
5e0db24
+          return grub_error (GRUB_ERR_BUG, "shouldn't reach here");
5e0db24
+        default:
5e0db24
+          return grub_error (GRUB_ERR_BUG,
5e0db24
+                             "unsupported address type %d", (&inf->address)->type);
5e0db24
+        }
5e0db24
+    }
5e0db24
+
5e0db24
+  /* Remove the remaining minus sign at the end. */
5e0db24
+  config[config_len] = '\0';
5e0db24
+
5e0db24
+  return GRUB_ERR_NONE;
5e0db24
+}
5e0db24
+
5e0db24
 static struct grub_preboot *fini_hnd;
5e0db24
 
5e0db24
 static grub_command_t cmd_addaddr, cmd_deladdr, cmd_addroute, cmd_delroute;
5e0db24
diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
a5bd9f6
index 1f14826..b40d987 100644
5e0db24
--- a/grub-core/normal/main.c
5e0db24
+++ b/grub-core/normal/main.c
5e0db24
@@ -32,6 +32,7 @@
5e0db24
 #include <grub/i18n.h>
5e0db24
 #include <grub/charset.h>
5e0db24
 #include <grub/script_sh.h>
5e0db24
+#include <grub/net.h>
5e0db24
 #ifdef GRUB_MACHINE_IEEE1275
5e0db24
 #include <grub/ieee1275/ieee1275.h>
5e0db24
 #endif
a5bd9f6
@@ -392,10 +393,19 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
5e0db24
 
5e0db24
       prefix = grub_env_get ("prefix");
5e0db24
       if (prefix)
5e0db24
-	{
5e0db24
-	  config = grub_xasprintf ("%s/grub.cfg", prefix);
5e0db24
-	  if (! config)
5e0db24
-	    goto quit;
5e0db24
+        {
5e0db24
+          grub_size_t config_len;
5e0db24
+          config_len = grub_strlen (prefix) +
5e0db24
+                      sizeof ("/grub.cfg-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
5e0db24
+          config = grub_malloc (config_len);
5e0db24
+
5e0db24
+          if (! config)
5e0db24
+            goto quit;
5e0db24
+
5e0db24
+          grub_snprintf (config, config_len, "%s/grub.cfg", prefix);
5e0db24
+
5e0db24
+          if (grub_strncmp (prefix + 1, "tftp", sizeof ("tftp") - 1) == 0)
5e0db24
+            grub_net_search_configfile (config);
5e0db24
 
5e0db24
 	  grub_enter_normal_mode (config);
5e0db24
 	  grub_free (config);
5e0db24
diff --git a/include/grub/net.h b/include/grub/net.h
a5bd9f6
index 36ac906..c7d5ce0 100644
5e0db24
--- a/include/grub/net.h
5e0db24
+++ b/include/grub/net.h
a5bd9f6
@@ -537,4 +537,7 @@ extern char *grub_net_default_server;
a5bd9f6
 
b96417a
 #define VLANTAG_IDENTIFIER 0x8100
5e0db24
 
5e0db24
+grub_err_t
5e0db24
+grub_net_search_configfile (char *config);
5e0db24
+
5e0db24
 #endif /* ! GRUB_NET_HEADER */
5e0db24
-- 
a5bd9f6
1.8.1.4
5e0db24