Blame SOURCES/0457-Search-for-specific-config-file-for-netboot.patch

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