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

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