All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 2/2] Search for specific config file when it boots from tftp
@ 2012-08-14 12:57 Paulo Flabiano Smorigo/Brazil/IBM
  2012-09-05  5:58 ` Vladimir 'φ-coder/phcoder' Serbinenko
  0 siblings, 1 reply; 2+ messages in thread
From: Paulo Flabiano Smorigo/Brazil/IBM @ 2012-08-14 12:57 UTC (permalink / raw)
  To: grub-devel

[-- Attachment #1: Type: text/plain, Size: 1169 bytes --]


Hi all,

This patch implements a search for a specific configuration (based on  
MAC or IP) when the config file is on the tftp server. He uses the  
following order:

    1) By MAC address (in lower case hexadecimal with dash separators);
    2) By IP (in upper case hexadecimal). It removes one hex digit  
from the end and try again;
    3) The original grub.cfg file.

For example, MAC address e4:1f:13:f4:ad:c2 and IP 192.168.1.100 (C0A80164):

(tftp,192.168.1.1)//boot/grub/grub.cfg-01-e4-1f-13-f4-ad-c2
(tftp,192.168.1.1)//boot/grub/grub.cfg-C0A80164
(tftp,192.168.1.1)//boot/grub/grub.cfg-C0A8016
(tftp,192.168.1.1)//boot/grub/grub.cfg-C0A801
(tftp,192.168.1.1)//boot/grub/grub.cfg-C0A80
(tftp,192.168.1.1)//boot/grub/grub.cfg-C0A8
(tftp,192.168.1.1)//boot/grub/grub.cfg-C0A
(tftp,192.168.1.1)//boot/grub/grub.cfg-C0
(tftp,192.168.1.1)//boot/grub/grub.cfg-C
(tftp,192.168.1.1)//boot/grub/grub.cfg

This procedure is similar to what is used by pxelinux and yaboot:

http://www.syslinux.org/wiki/index.php/PXELINUX#config

-- 
Paulo Flabiano Smorigo
Software Engineer
Linux Technology Center - IBM Systems & Technology Group
freenode/bluenet: pfsmorigo or smow


[-- Attachment #2: search_configfile.patch --]
[-- Type: text/x-patch, Size: 5004 bytes --]

=== modified file 'ChangeLog'
--- ChangeLog	2012-08-14 08:56:19 +0000
+++ ChangeLog	2012-08-14 09:10:27 +0000
@@ -1,5 +1,14 @@
 2012-08-13 Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>

+	Search for specific config file when it boots from tftp.
+
+	* grub-core/normal/main.c: Call search function.
+	* grub-core/net/net.c: New function.
+	* include/grub/net.h: New function.
+
+
+2012-08-13 Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
+
 	* grub-core/kern/misc.c: Add %X option to printf functions.

 2012-08-05  Grégoire Sutre  <gregoire.sutre@gmail.com>

=== modified file 'grub-core/net/net.c'
--- grub-core/net/net.c	2012-06-22 20:02:47 +0000
+++ grub-core/net/net.c	2012-08-14 08:59:46 +0000
@@ -1548,6 +1548,96 @@
   return GRUB_ERR_NONE;
 }

+grub_err_t
+grub_net_search_configfile (char *config)
+{
+  grub_size_t config_len;
+  char *suffix;
+
+  auto int search_through (grub_size_t num_tries, grub_size_t slice_size);
+  int search_through (grub_size_t num_tries, grub_size_t slice_size)
+    {
+      while (num_tries-- > 0)
+        {
+	  grub_dprintf ("net", "probe %s\n", config);
+
+          grub_file_t file;
+          file = grub_file_open (config);
+
+          if (file)
+            {
+              grub_file_close (file);
+              grub_dprintf ("net", "found!\n");
+              return 0;
+            }
+          else
+            {
+              if (grub_errno == GRUB_ERR_IO)
+                grub_errno = GRUB_ERR_NONE;
+            }
+
+          if (grub_strlen (suffix) < slice_size)
+            break;
+
+          config[grub_strlen (config) - slice_size] = '\0';
+        }
+
+      return 1;
+    }
+
+  config_len = grub_strlen (config);
+  config[config_len] = '-';
+  suffix = config + config_len + 1;
+
+  struct grub_net_network_level_interface *inf;
+  FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
+    {
+      /* Try by the MAC address. */
+
+      /* Add ethernet type */
+      grub_strcpy (suffix, "01-");
+
+      grub_net_hwaddr_to_str (&inf->hwaddress, suffix + 3);
+
+      char *ptr;
+      for (ptr = suffix; *ptr; ptr++)
+        if (*ptr == ':')
+          *ptr = '-';
+
+      if (search_through (1, 3) == 0) return GRUB_ERR_NONE;
+
+      /* Then by IP address */
+
+      switch ((&inf->address)->type)
+        {
+        case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
+            {
+              break;
+            }
+        case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
+            {
+              grub_uint32_t n = grub_be_to_cpu32 ((&inf->address)->ipv4);
+              grub_snprintf (suffix, GRUB_NET_MAX_STR_ADDR_LEN, "%02X%02X%02X%02X", \
+                             ((n >> 24) & 0xff), ((n >> 16) & 0xff), \
+                             ((n >> 8) & 0xff), ((n >> 0) & 0xff));
+
+              if (search_through (8, 1) == 0) return GRUB_ERR_NONE;
+              break;
+            }
+        case GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV:
+          return grub_error (GRUB_ERR_BUG, "shouldn't reach here");
+        default:
+          return grub_error (GRUB_ERR_BUG,
+                             "unsupported address type %d", (&inf->address)->type);
+        }
+    }
+
+  /* Remove the remaining minus sign at the end. */
+  config[config_len] = '\0';
+
+  return GRUB_ERR_NONE;
+}
+
 static struct grub_preboot *fini_hnd;

 static grub_command_t cmd_addaddr, cmd_deladdr, cmd_addroute, cmd_delroute;

=== modified file 'grub-core/normal/main.c'
--- grub-core/normal/main.c	2012-06-28 00:06:36 +0000
+++ grub-core/normal/main.c	2012-08-14 08:56:59 +0000
@@ -32,6 +32,7 @@
 #include <grub/i18n.h>
 #include <grub/charset.h>
 #include <grub/script_sh.h>
+#include <grub/net.h>

 GRUB_MOD_LICENSE ("GPLv3+");

@@ -337,10 +338,18 @@

       prefix = grub_env_get ("prefix");
       if (prefix)
-	{
-	  config = grub_xasprintf ("%s/grub.cfg", prefix);
-	  if (! config)
-	    goto quit;
+        {
+          grub_size_t config_len;
+          config_len = grub_strlen (prefix) + sizeof ("/grub.cfg-XX-XX-XX-XX-XX-XX-XX");
+          config = grub_malloc (config_len);
+
+          if (! config)
+            goto quit;
+
+          grub_snprintf (config, config_len, "%s/grub.cfg", prefix);
+
+          if (grub_strncmp (prefix + 1, "tftp", sizeof ("tftp") - 1) == 0)
+            grub_net_search_configfile (config);

 	  grub_enter_normal_mode (config);
 	  grub_free (config);

=== modified file 'include/grub/net.h'
--- include/grub/net.h	2012-06-22 12:17:46 +0000
+++ include/grub/net.h	2012-08-14 08:56:59 +0000
@@ -523,4 +523,7 @@
 #define GRUB_NET_TRIES 40
 #define GRUB_NET_INTERVAL 400

+grub_err_t
+grub_net_search_configfile (char *config);
+
 #endif /* ! GRUB_NET_HEADER */


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH 2/2] Search for specific config file when it boots from tftp
  2012-08-14 12:57 [PATCH 2/2] Search for specific config file when it boots from tftp Paulo Flabiano Smorigo/Brazil/IBM
@ 2012-09-05  5:58 ` Vladimir 'φ-coder/phcoder' Serbinenko
  0 siblings, 0 replies; 2+ messages in thread
From: Vladimir 'φ-coder/phcoder' Serbinenko @ 2012-09-05  5:58 UTC (permalink / raw)
  To: grub-devel

[-- Attachment #1: Type: text/plain, Size: 951 bytes --]

On 14.08.2012 14:57, Paulo Flabiano Smorigo/Brazil/IBM wrote:

> 
> Hi all,
> 
> This patch implements a search for a specific configuration (based on
> MAC or IP) when the config file is on the tftp server. He uses the
> following order:
> 
>    1) By MAC address (in lower case hexadecimal with dash separators);
>    2) By IP (in upper case hexadecimal). It removes one hex digit from
> the end and try again;
>    3) The original grub.cfg file.
> 

This seems more like something that should be done in grub.cfg.
Something more along the lines:
if [ -s "$prefix/grub.cfg.$mac" ]; then
  source "$prefix/grub.cfg.$mac"
  exit_file
fi

for i in 8 7 6 5 4 3 2 1; do
if [ -s "$prefix/grub.cfg.${ip:1:$i}" ]; then
  source "$prefix/grub.cfg.${ip:1:$i}"
  exit_file
fi

It seems like this would be more productive than just handling this
specific case manually in C.
-- 
Regards
Vladimir 'φ-coder/phcoder' Serbinenko


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 294 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-09-05  5:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-14 12:57 [PATCH 2/2] Search for specific config file when it boots from tftp Paulo Flabiano Smorigo/Brazil/IBM
2012-09-05  5:58 ` Vladimir 'φ-coder/phcoder' Serbinenko

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.