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

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