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

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