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

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