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

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