dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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