Skip to contentSkip to conversation

Obtaining the MAC address of the boot NIC for a PXE boot

DISCUSSION Andrey Borzenkov 4 participants 21 messages May 1, 2013 – May 15, 2013 1 message not in the archive Networking

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

В Mon, 29 Apr 2013 05:55:32 -0700 (PDT)
Rigoberto Corujo <[email protected]> пишет:

Quoted 9 lines

Hello everyone,

With pxelinux, you can specify "IPAPPEND 2" in your boot loader configuration file and the MAC address of the boot NIC automatically gets appended to your kernel parameters as "BOOTIF=XX:XX:XX:XX:XX:XX".

I am now trying to become familiar with Grub 2, because if its EFI support.   I noticed that with Grub 2, some of the documentation, which may be outdated, says that there is a "net_pxe_mac" variable which, as I understand it, should contain the MAC address of the boot NIC.  However, I've built the latest Grub 2 sources and, when I PXE boot and drop into a grub shell prompt, if type "set", I don't see "net_pxe_mac" listed as one of the variables.  I know there is a "net_ls_addr" command which prints out something like "efinet8 3c:4a:92:b2:6a:e8 10.1.1.114", but I don't know how to use Grub scripting to parse out the MAC address.  I also don't know if there will ever be a case where "net_ls_addr" will print out multiple entries on a PXE boot, making it difficult to determine which one corresponds to the boot NIC.

Basically, I'm using the "ksdevice=bootif" kernel parameter when PXE booting my Red Hat kernel.  With PXE Linux, "bootif" provides the MAC address of the boot NIC.  I was hoping to do something like "ksdevice=${net_pxe_mac}", but since "net_pxe_mac" does not appear to be getting set, I need to find some other method.

As it stands currently, net_pxe_* variables are defined on PC BIOS platform only. For UEFI (which you apparently have) GRUB2 defines net_efinetNN_* variables where efinetNN is symbolic name for interface that was used to boot GRUB2.

May be GRUB2 should also define net_pxe_* namespace for the case of UEFI. There is no real way to find out which interface was used for booting and even if there were, grub does not support nested variables substitution or eval'ing (like ${net_${boot_if}_mac).

Probably, adding "eval" support is really the most simple. Could you test the patch below. What it does, is

- it adds "eval" command (same as known from UNIX shells) which executes its argument

- it exports boot interface as "efi_boot_interface" variable

This should allow you to do

if $grub_platform = efi ; then
eval "set net_pxe_mac=\$net_${efi_boot_interface}_mac"
fi

and then use $net_pxe_mac on both platforms.

If it works for you, I will clean it up; for consistency this is probably needed for ieee1275 as well.

---
 grub-core/Makefile.core.def        |  5 ++++
 grub-core/commands/eval.c          | 51 ++++++++++++++++++++++++++++++++++++++
 grub-core/net/drivers/efi/efinet.c |  2 ++
 3 files changed, 58 insertions(+)
grub-core/Makefile.core.def +5 -0
index 7e19acb..8844f2f 100644
@@ -699,6 +699,11 @@ module = {
 };
 
 module = {
+  name = eval;
+  common = commands/eval.c;
+};
+
+module = {
   name = extcmd;
   common = commands/extcmd.c;
   common = lib/arg.c;
grub-core/commands/eval.c new file +51 -0

51 lines not on this page · raw message

grub-core/net/drivers/efi/efinet.c +2 -0
index 2b344d6..f7d8fd5 100644
@@ -23,6 +23,7 @@
 #include <grub/efi/api.h>
 #include <grub/efi/efi.h>
 #include <grub/i18n.h>
+#include <grub/env.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -250,6 +251,7 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
 				    &pxe_mode->dhcp_ack,
 				    sizeof (pxe_mode->dhcp_ack),
 				    1, device, path);
+    grub_env_set ("efi_boot_interface", card->name);
     return;
   }
 }
Signature · 1 line
tg: (93daf86..) e/uefi_pxe_vars (depends on: master)
#2 lore raw
Quoted 8 lines

As it stands currently, net_pxe_* variables are defined on PC BIOS platform only. For UEFI (which you apparently have) GRUB2 defines net_efinetNN_* variables where efinetNN is symbolic name for interface that was used to boot GRUB2.

May be GRUB2 should also define net_pxe_* namespace for the case of UEFI.

"pxe" is the name of drivers used for BIOS. But it's ok to define net_boot_* on all platforms and make it alias to the boot interface.

> There is no real way to find out which interface was used for

Quoted 3 lines

booting and even if there were, grub does not support nested variables substitution or eval'ing (like ${net_${boot_if}_mac).

Bash doesn't support such construction either. We generally prefer to stay reasonably close to bash to allow for easy reference and testing.

Quoted 5 lines

Probably, adding "eval" support is really the most simple. Could you test the patch below. What it does, is

- it adds "eval" command (same as known from UNIX shells) which executes its argument

eval is generally good

Quoted 3 lines

- it exports boot interface as "efi_boot_interface" variable

This is needlessly EFI-specific. Similar variable would be useful on many platforms. But a possible problem is that it's not known when using native drivers (and on some platforms you have to). Perhaps making something similar to "root" partition would be useful:
E.g.
net_default_interface=pxe
echo $net_default_mac
With net_default_interface defined to boot one on boot.
This would parallel net_default_server.

Quoted 6 lines

This should allow you to do

if $grub_platform = efi ; then
eval "set net_pxe_mac=\$net_${efi_boot_interface}_mac"
fi

Defining system variables manually looks dirty.

grub-core/commands/eval.c line 30

Quoted 4 lines
grub-core/commands/eval.c new file +4 -0
+static grub_err_t
+grub_cmd_eval (grub_extcmd_context_t ctxt __attribute__((__unused__)),
+	       int argc, char *argv[])
+{

You don't use any argument parsing. It's better to use command and not extcmd in this case. Also "eval" in bash concatenates its arguments.

grub-core/commands/eval.c line 43

Quoted 2 lines
grub-core/commands/eval.c new file +2 -0
+  cmd = grub_register_extcmd ("eval", grub_cmd_eval, 0,
+			      N_("[STRING]"), N_("Evaluate commands"),

This description seems a bit vague for either human-reading or translation.

#3 lore raw

Patch for net_default_*. Please test.

=== modified file 'grub-core/net/bootp.c'

net/bootp.c +3 -0
@@ -211,6 +211,9 @@
       grub_print_error ();
     }
 
+  if (is_def)
+    grub_env_set ("net_default_interface", name);
+
   if (device && !*device && bp->server_ip)
     {
       *device = grub_xasprintf ("tftp,%d.%d.%d.%d",

=== modified file 'grub-core/net/net.c'

net/net.c +64 -1

82 lines not on this page · raw message

#4 lore raw

В Sat, 04 May 2013 23:19:50 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 31 lines

Patch for net_default_*. Please test.

=== modified file 'grub-core/net/bootp.c'

net/bootp.c +3 -0
@@ -211,6 +211,9 @@
       grub_print_error ();
     }
 
+  if (is_def)
+    grub_env_set ("net_default_interface", name);
+
   if (device && !*device && bp->server_ip)
     {
       *device = grub_xasprintf ("tftp,%d.%d.%d.%d",

=== modified file 'grub-core/net/net.c'

net/net.c +1 -1
@@ -1,6 +1,6 @@
 /*
  *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2010,2011  Free Software Foundation, Inc.
+ *  Copyright (C) 2010,2011,2012,2013  Free Software Foundation, Inc.
  *
  *  GRUB is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -813,6 +813,65 @@
   return grub_net_default_server ? : "";
 }
 

net/net.c line 825

Quoted 10 lines
net/net.c +10 -0
+static const char *
+defip_get_env (struct grub_env_var *var __attribute__ ((unused)),
+	       const char *val)
+{
+  const char *intf = grub_env_get ("net_default_interface");
+  const char *ret = NULL;
+  if (intf)
+    {
+      char *buf = grub_xasprintf ("net_%s_ip", intf);
+      ret = grub_env_get (buf);

This will crash if grub_xasprintf fails.

Quoted 65 lines
net/net.c +53 -0
+      grub_free (buf);
+    }
+  return ret;
+}
+
+static char *
+defip_set_env (struct grub_env_var *var __attribute__ ((unused)),
+	       const char *val)
+{
+  const char *intf = grub_env_get ("net_default_interface");
+  if (intf)
+    {
+      char *buf = grub_xasprintf ("net_%s_ip", intf);
+      grub_env_set (buf, val);
+      grub_free (buf);
+    }
+  return NULL;
+}
+
+
+static const char *
+defmac_get_env (struct grub_env_var *var __attribute__ ((unused)),
+	       const char *val)
+{
+  const char *intf = grub_env_get ("net_default_interface");
+  const char *ret = NULL;
+  if (intf)
+    {
+      char *buf = grub_xasprintf ("net_%s_mac", intf);
+      ret = grub_env_get (buf);
+      grub_free (buf);
+    }
+  return ret;
+}
+
+static char *
+defmac_set_env (struct grub_env_var *var __attribute__ ((unused)),
+	       const char *val)
+{
+  const char *intf = grub_env_get ("net_default_interface");
+  if (intf)
+    {
+      char *buf = grub_xasprintf ("net_%s_mac", intf);
+      grub_env_set (buf, val);
+      grub_free (buf);
+    }
+  return NULL;
+}
+
 
 static void
 grub_net_network_level_interface_register (struct grub_net_network_level_interface *inter)
@@ -1560,6 +1619,10 @@
 			       defserver_set_env);
   grub_register_variable_hook ("pxe_default_server", defserver_get_env,
 			       defserver_set_env);
+  grub_register_variable_hook ("net_default_ip", defip_get_env,
+			       defip_set_env);
+  grub_register_variable_hook ("net_default_mac", defmac_get_env,
+			       defmac_set_env);
 
   cmd_addaddr = grub_register_command ("net_add_addr", grub_cmd_addaddr,
 					/* TRANSLATORS: HWADDRESS stands for
#5 lore raw
Quoted 34 lines · Andrey Borzenkov

В Sat, 04 May 2013 23:19:50 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 31 lines

Patch for net_default_*. Please test.

=== modified file 'grub-core/net/bootp.c'

net/bootp.c +3 -0
@@ -211,6 +211,9 @@
       grub_print_error ();
     }
 
+  if (is_def)
+    grub_env_set ("net_default_interface", name);
+
   if (device && !*device && bp->server_ip)
     {
       *device = grub_xasprintf ("tftp,%d.%d.%d.%d",

=== modified file 'grub-core/net/net.c'

net/net.c +1 -1
@@ -1,6 +1,6 @@
 /*
  *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2010,2011  Free Software Foundation, Inc.
+ *  Copyright (C) 2010,2011,2012,2013  Free Software Foundation, Inc.
  *
  *  GRUB is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -813,6 +813,65 @@
   return grub_net_default_server ? : "";
 }
 
Quoted 13 lines

net/net.c line 825

Quoted 10 lines
net/net.c +10 -0
+static const char *
+defip_get_env (struct grub_env_var *var __attribute__ ((unused)),
+	       const char *val)
+{
+  const char *intf = grub_env_get ("net_default_interface");
+  const char *ret = NULL;
+  if (intf)
+    {
+      char *buf = grub_xasprintf ("net_%s_ip", intf);
+      ret = grub_env_get (buf);

This will crash if grub_xasprintf fails.

Right, thanks. This patch was actually meant to just show the principle. Fixed this and another problem with it, tested and now comitting.

Quoted 72 lines
Quoted 55 lines
net/net.c +49 -0
+      grub_free (buf);
+    }
+  return ret;
+}
+
+static char *
+defip_set_env (struct grub_env_var *var __attribute__ ((unused)),
+	       const char *val)
+{
+  const char *intf = grub_env_get ("net_default_interface");
+  if (intf)
+    {
+      char *buf = grub_xasprintf ("net_%s_ip", intf);
+      grub_env_set (buf, val);
+      grub_free (buf);
+    }
+  return NULL;
+}
+
+
+static const char *
+defmac_get_env (struct grub_env_var *var __attribute__ ((unused)),
+	       const char *val)
+{
+  const char *intf = grub_env_get ("net_default_interface");
+  const char *ret = NULL;
+  if (intf)
+    {
+      char *buf = grub_xasprintf ("net_%s_mac", intf);
+      ret = grub_env_get (buf);
+      grub_free (buf);
+    }
+  return ret;
+}
+
+static char *
+defmac_set_env (struct grub_env_var *var __attribute__ ((unused)),
+	       const char *val)
+{
+  const char *intf = grub_env_get ("net_default_interface");
+  if (intf)
+    {
+      char *buf = grub_xasprintf ("net_%s_mac", intf);
+      grub_env_set (buf, val);
+      grub_free (buf);
+    }
+  return NULL;
+}
+
 
 static void
 grub_net_network_level_interface_register (struct grub_net_network_level_interface *inter)

net/net.c line 1628

Quoted 10 lines
net/net.c +4 -0
@@ -1560,6 +1619,10 @@
 			       defserver_set_env);
   grub_register_variable_hook ("pxe_default_server", defserver_get_env,
 			       defserver_set_env);
+  grub_register_variable_hook ("net_default_ip", defip_get_env,
+			       defip_set_env);
+  grub_register_variable_hook ("net_default_mac", defmac_get_env,
+			       defmac_set_env);
 
   cmd_addaddr = grub_register_command ("net_add_addr", grub_cmd_addaddr,
 					/* TRANSLATORS: HWADDRESS stands for

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#6 lore raw

Documentation for (Re: Obtaining the MAC address of the boot NIC for a PXE boot)

В Tue, 07 May 2013 12:03:51 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 2 lines

Fixed this and another problem with it, tested and now comitting.

Below documentation for new variables. It also changes variables description to indicate that they contain variable interface name (do we need to list possible values?). OK to commit?

From: Andrey Borzenkov <[email protected]>
To: [email protected]
Subject: [PATCH] document new net_default_(interface|ip|mac) variables

Also generalize description to indicate that other variables may apply to more than pxe interface.

---
 docs/grub.texi | 111 +++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 76 insertions(+), 35 deletions(-)
docs/grub.texi +76 -35
index bfd42ee..79bbcb0 100644
@@ -2134,36 +2134,49 @@ this should be changed both in the prefix and in any references to the
 device name in the configuration file.
 
 GRUB provides several environment variables which may be used to inspect or
-change the behaviour of the PXE device:
+change the behaviour of the PXE device. In the following description
+@var{<interface>} is placeholder for the name of network interface (platform
+dependent):
 
 @table @samp
-@item net_pxe_ip
-The IP address of this machine.  Read-only.
+@item net_@var{<interface>}_ip
+The network interface's IP address.  Read-only.
 
-@item net_pxe_mac
+@item net_@var{<interface>}_mac
 The network interface's MAC address.  Read-only.
 
-@item net_pxe_hostname
+@item net_@var{<interface>}_hostname
 The client host name provided by DHCP.  Read-only.
 
-@item net_pxe_domain
+@item net_@var{<interface>}_domain
 The client domain name provided by DHCP.  Read-only.
 
-@item net_pxe_rootpath
+@item net_@var{<interface>}_rootpath
 The path to the client's root disk provided by DHCP.  Read-only.
 
-@item net_pxe_extensionspath
+@item net_@var{<interface>}_extensionspath
 The path to additional DHCP vendor extensions provided by DHCP.  Read-only.
 
-@item net_pxe_boot_file
+@item net_@var{<interface>}_boot_file
 The boot file name provided by DHCP.  Read-only.
 
-@item net_pxe_dhcp_server_name
+@item net_@var{<interface>}_dhcp_server_name
 The name of the DHCP server responsible for these boot parameters.
 Read-only.
 
+@item net_default_interface
+The name of interface that was used to load grub.  Read-only.
+
+@item net_default_ip
+The IP address of default interface. Read-only. This is alias for the
+@samp{net_$@{net_default_interface@}_ip}.
+
+@item net_default_mac
+The default interface's MAC address.  Read-only.  This is alias for the
+@samp{net_$@{net_default_interface@}_mac}.
+
 @item net_default_server
-The default server.  Read-write, although setting this is only useful
+The default TFTP server.  Read-write, although setting this is only useful
 before opening a network device.
 
 @end table
@@ -2735,14 +2748,18 @@ These variables have special meaning to GRUB.
 * locale_dir::
 * menu_color_highlight::
 * menu_color_normal::
-* net_pxe_boot_file::
-* net_pxe_dhcp_server_name::
-* net_pxe_domain::
-* net_pxe_extensionspath::
-* net_pxe_hostname::
-* net_pxe_ip::
-* net_pxe_mac::
-* net_pxe_rootpath::
+* net_@var{<interface>}_boot_file::
+* net_@var{<interface>}_dhcp_server_name::
+* net_@var{<interface>}_domain::
+* net_@var{<interface>}_extensionspath::
+* net_@var{<interface>}_hostname::
+* net_@var{<interface>}_ip::
+* net_@var{<interface>}_mac::
+* net_@var{<interface>}_rootpath::
+* net_default_interface::
+* net_default_ip::
+* net_default_mac::
+* net_default_server::
 * pager::
 * prefix::
 * pxe_blksize::
@@ -3009,50 +3026,74 @@ variable changes those colors.  For the available color names,
 The default is the value of @samp{color_normal} (@pxref{color_normal}).
 
 
-@node net_pxe_boot_file
-@subsection net_pxe_boot_file
+@node net_@var{<interface>}_boot_file
+@subsection net_@var{<interface>}_boot_file
+
+@xref{Network}.
+
+
+@node net_@var{<interface>}_dhcp_server_name
+@subsection net_@var{<interface>}_dhcp_server_name
+
+@xref{Network}.
+
+
+@node net_@var{<interface>}_domain
+@subsection net_@var{<interface>}_domain
+
+@xref{Network}.
+
+
+@node net_@var{<interface>}_extensionspath
+@subsection net_@var{<interface>}_extensionspath
+
+@xref{Network}.
+
+
+@node net_@var{<interface>}_hostname
+@subsection net_@var{<interface>}_hostname
 
 @xref{Network}.
 
 
-@node net_pxe_dhcp_server_name
-@subsection net_pxe_dhcp_server_name
+@node net_@var{<interface>}_ip
+@subsection net_@var{<interface>}_ip
 
 @xref{Network}.
 
 
-@node net_pxe_domain
-@subsection net_pxe_domain
+@node net_@var{<interface>}_mac
+@subsection net_@var{<interface>}_mac
 
 @xref{Network}.
 
 
-@node net_pxe_extensionspath
-@subsection net_pxe_extensionspath
+@node net_@var{<interface>}_rootpath
+@subsection net_@var{<interface>}_rootpath
 
 @xref{Network}.
 
 
-@node net_pxe_hostname
-@subsection net_pxe_hostname
+@node net_default_interface
+@subsection net_default_interface
 
 @xref{Network}.
 
 
-@node net_pxe_ip
-@subsection net_pxe_ip
+@node net_default_ip
+@subsection net_default_ip
 
 @xref{Network}.
 
 
-@node net_pxe_mac
-@subsection net_pxe_mac
+@node net_default_mac
+@subsection net_default_mac
 
 @xref{Network}.
 
 
-@node net_pxe_rootpath
-@subsection net_pxe_rootpath
+@node net_default_server
+@subsection net_default_server
 
 @xref{Network}.
 
Signature · 1 line
tg: (48f959f..) u/default_interface (depends on: master)
#7 lore raw

Re: Documentation for (Re: Obtaining the MAC address of the boot NIC for a PXE boot)

Review

Quoted 4 lines

+@item net_default_interface

(quoted diff) +2 -0
+The name of interface that was used to load grub.  Read-only.
+

It's read-write. Also its value doesn't influence anything other than net_default_* variables. The real card is determined from routing table and server.

docs/grub.texi this should be changed both in the prefix and in any references to the · line 2180

Quoted 4 lines

@item net_default_server

docs/grub.texi +1 -1
-The default server.  Read-write, although setting this is only useful
+The default TFTP server.  Read-write, although setting this is only useful
 before opening a network device.

It's not restricted to tftp. You can do e.g. cat (http)/boot/grub/grub.cfg Other than these 2 problems patch is ok to commit.

#8 lore raw

Re: Documentation for (Re: Obtaining the MAC address of the boot NIC for a PXE boot)

В Tue, 07 May 2013 21:45:14 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 7 lines

Review

Quoted 4 lines

+@item net_default_interface

(quoted diff) +2 -0
+The name of interface that was used to load grub.  Read-only.
+

It's read-write. Also its value doesn't influence anything other than net_default_* variables.

Added clarification.

Quoted 7 lines

docs/grub.texi this should be changed both in the prefix and in any references to the · line 2180

Quoted 5 lines

@item net_default_server

docs/grub.texi +1 -1
-The default server.  Read-write, although setting this is only useful
+The default TFTP server.  Read-write, although setting this is only useful
 before opening a network device.

It's not restricted to tftp. You can do e.g. cat (http)/boot/grub/grub.cfg

OK, reverted and reference to Device syntax section

Quoted 2 lines

Other than these 2 problems patch is ok to commit.

pushed with above changes.

#9 lore raw

New command eval.

In reply to Vladimir 'φ-coder/phcoder' Serbinenko

В Sat, 04 May 2013 23:08:03 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 14 lines
Quoted 2 lines

- it adds "eval" command (same as known from UNIX shells) which executes its argument

eval is generally good

Review

Quoted 4 lines
(quoted diff) +4 -0
+static grub_err_t
+grub_cmd_eval (grub_extcmd_context_t ctxt __attribute__((__unused__)),
+	       int argc, char *argv[])
+{

You don't use any argument parsing. It's better to use command and not extcmd in this case. Also "eval" in bash concatenates its arguments.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+  cmd = grub_register_extcmd ("eval", grub_cmd_eval, 0,
+			      N_("[STRING]"), N_("Evaluate commands"),

This description seems a bit vague for either human-reading or translation.

Is patch below OK? I decided to split off pure execute_sourcecode so that eval context would not clobber positional arguments.

ChangeLog +8 -0
index cd8213a..8ca9874 100644
@@ -1,3 +1,11 @@
+2013-05-11  Andrey Borzenkov <[email protected]>
+
+	* grub-core/script/execute.c (grub_script_execute_sourcecode): Split
+	off new function grub_script_execute_new_scope. Change callers to use
+	either of them as appropriate.
+	* grub-core/commands/eval.c: New command eval.
+	* docs/grub.texi (Commands): Document it.
+
 2013-05-11  Vladimir Serbinenko  <[email protected]>
 
 	* util/grub-install.in: Gettextize "Not found" message.
docs/grub.texi +10 -0
index 75a5ea4..9dcfa2d 100644
@@ -3435,6 +3435,7 @@ you forget a command, you can run the command @command{help}
 * date::                        Display or set current date and time
 * drivemap::                    Map a drive to another
 * echo::                        Display a line of text
+* eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully
 * gettext::                     Translate a string
@@ -3812,6 +3813,15 @@ character will print that character.
 @end deffn
 
 
+@node eval
+@subsection eval
+
+@deffn Command eval string ...
+Concatenate arguments together using single space as separator and evaluate
+result as sequence of GRUB commands.
+@end deffn
+
+
 @node export
 @subsection export
 
grub-core/Makefile.core.def +5 -0
index 602b497..0fcc4e7 100644
@@ -699,6 +699,11 @@ module = {
 };
 
 module = {
+  name = eval;
+  common = commands/eval.c;
+};
+
+module = {
   name = extcmd;
   common = commands/extcmd.c;
   common = lib/arg.c;
grub-core/commands/eval.c new file +72 -0

72 lines not on this page · raw message

grub-core/normal/menu.c +1 -1
index fba19db..9b88290 100644
@@ -245,7 +245,7 @@ grub_menu_execute_entry(grub_menu_entry_t entry, int auto_boot)
   else
     grub_env_unset ("default");
 
-  grub_script_execute_sourcecode (entry->sourcecode, entry->argc, entry->args);
+  grub_script_execute_new_scope (entry->sourcecode, entry->argc, entry->args);
 
   if (errs_before != grub_err_printed_errors)
     grub_wait_after_message ();
grub-core/normal/menu_entry.c +1 -2
index fae258a..0f15a7e 100644
@@ -1120,7 +1120,6 @@ run (struct screen *screen)
   char *script;
   int errs_before;
   grub_menu_t menu = NULL;
-  char *dummy[1] = { NULL };
 
   grub_cls ();
   grub_printf ("  ");
@@ -1160,7 +1159,7 @@ run (struct screen *screen)
       }
     script[size] = '\0';
   }
-  grub_script_execute_sourcecode (script, 0, dummy);
+  grub_script_execute_sourcecode (script);
   grub_free (script);
 
   if (errs_before != grub_err_printed_errors)
grub-core/script/execute.c +23 -10
index 9babbee..afd5513 100644
@@ -856,19 +856,10 @@ grub_script_execute_sourcecode_getline (char **line,
 
 /* Execute a source script.  */
 grub_err_t
-grub_script_execute_sourcecode (const char *source, int argc, char **args)
+grub_script_execute_sourcecode (const char *source)
 {
   grub_err_t ret = 0;
   struct grub_script *parsed_script;
-  struct grub_script_scope new_scope;
-  struct grub_script_scope *old_scope;
-
-  new_scope.argv.argc = argc;
-  new_scope.argv.args = args;
-  new_scope.flags = 0;
-
-  old_scope = scope;
-  scope = &new_scope;
 
   while (source)
     {
@@ -880,13 +871,35 @@ grub_script_execute_sourcecode (const char *source, int argc, char **args)
       if (! parsed_script)
 	{
 	  ret = grub_errno;
+	  grub_free (line);
 	  break;
 	}
 
       ret = grub_script_execute (parsed_script);
+      grub_script_free (parsed_script);
       grub_free (line);
     }
 
+  return ret;
+}
+
+/* Execute a source script in new scope.  */
+grub_err_t
+grub_script_execute_new_scope (const char *source, int argc, char **args)
+{
+  grub_err_t ret = 0;
+  struct grub_script_scope new_scope;
+  struct grub_script_scope *old_scope;
+
+  new_scope.argv.argc = argc;
+  new_scope.argv.args = args;
+  new_scope.flags = 0;
+
+  old_scope = scope;
+  scope = &new_scope;
+
+  ret = grub_script_execute_sourcecode (source);
+
   scope = old_scope;
   return ret;
 }
include/grub/script_sh.h +2 -1
index 78602e4..57bdd4d 100644
@@ -329,7 +329,8 @@ grub_err_t grub_script_execute_cmdwhile (struct grub_script_cmd *cmd);
 
 /* Execute any GRUB pre-parsed command or script.  */
 grub_err_t grub_script_execute (struct grub_script *script);
-grub_err_t grub_script_execute_sourcecode (const char *source, int argc, char **args);
+grub_err_t grub_script_execute_sourcecode (const char *source);
+grub_err_t grub_script_execute_new_scope (const char *source, int argc, char **args);
 
 /* Break command for loops.  */
 grub_err_t grub_script_break (grub_command_t cmd, int argc, char *argv[]);
#10 lore raw

Re: New command eval.

grub-core/commands/eval.c line 39

Quoted 7 lines · Andrey Borzenkov
grub-core/commands/eval.c new file +7 -0
+  grub_size_t size = argc; /* +1 for final zero */
+  char *str, *p;
+  grub_err_t ret;
+
+  if (argc == 0)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing arguments"));
+

eval with no arguments returns success in bash, to match this behaviour, it should be GRUB_ERR_NONE and no use of grub_error

grub-core/commands/eval.c line 50

Quoted 11 lines
grub-core/commands/eval.c new file +11 -0
+  for (i = 0; i < argc; i++)
+    size += grub_strlen (argv[i]);
+
+  str = p = grub_malloc (size);
+  if (!str)
+    return grub_errno;
+
+  for (i = 0; i < argc; i++)
+    {
+      grub_strcpy (p, argv[i]);
+      p += grub_strlen (argv[i]);

These 2 operations should be condensed by using grub_stpcpy

Review

Quoted 5 lines
(quoted diff) +2 -1
+      *p++ = ' ';
 
 
-  grub_script_execute_sourcecode (script, 0, dummy);
+  grub_script_execute_sourcecode (script);

(in menu_entry.c) here you need a new scope for consistency.

#11 lore raw

Re: New command eval.

В Sat, 11 May 2013 19:02:58 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 27 lines · Andrey Borzenkov

Review

Quoted 8 lines
(quoted diff) +7 -0
+  grub_size_t size = argc; /* +1 for final zero */
+  char *str, *p;
+  grub_err_t ret;
+
+  if (argc == 0)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing arguments"));
+

eval with no arguments returns success in bash, to match this behaviour, it should be GRUB_ERR_NONE and no use of grub_error

Review

Quoted 11 lines
(quoted diff) +11 -0
+  for (i = 0; i < argc; i++)
+    size += grub_strlen (argv[i]);
+
+  str = p = grub_malloc (size);
+  if (!str)
+    return grub_errno;
+
+  for (i = 0; i < argc; i++)
+    {
+      grub_strcpy (p, argv[i]);
+      p += grub_strlen (argv[i]);

These 2 operations should be condensed by using grub_stpcpy

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +2 -1
+      *p++ = ' ';
 
 
-  grub_script_execute_sourcecode (script, 0, dummy);
+  grub_script_execute_sourcecode (script);

(in menu_entry.c) here you need a new scope for consistency.

ChangeLog +8 -0
index cd8213a..60c64ee 100644
@@ -1,3 +1,11 @@
+2013-05-11  Andrey Borzenkov <[email protected]>
+
+	* grub-core/script/execute.c (grub_script_execute_sourcecode): Split
+	off new function grub_script_execute_new_scope. Change callers to use
+	either of them as appropriate.
+	* grub-core/commands/eval.c: New command eval.
+	* docs/grub.texi (Commands): Document it.
+
 2013-05-11  Vladimir Serbinenko  <[email protected]>
 
 	* util/grub-install.in: Gettextize "Not found" message.
docs/grub.texi +10 -0
index 75a5ea4..9dcfa2d 100644
@@ -3435,6 +3435,7 @@ you forget a command, you can run the command @command{help}
 * date::                        Display or set current date and time
 * drivemap::                    Map a drive to another
 * echo::                        Display a line of text
+* eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully
 * gettext::                     Translate a string
@@ -3812,6 +3813,15 @@ character will print that character.
 @end deffn
 
 
+@node eval
+@subsection eval
+
+@deffn Command eval string ...
+Concatenate arguments together using single space as separator and evaluate
+result as sequence of GRUB commands.
+@end deffn
+
+
 @node export
 @subsection export
 
grub-core/Makefile.core.def +5 -0
index 602b497..0fcc4e7 100644
@@ -699,6 +699,11 @@ module = {
 };
 
 module = {
+  name = eval;
+  common = commands/eval.c;
+};
+
+module = {
   name = extcmd;
   common = commands/extcmd.c;
   common = lib/arg.c;
grub-core/commands/eval.c new file +71 -0

71 lines not on this page · raw message

grub-core/normal/menu.c +1 -1
index fba19db..9b88290 100644
@@ -245,7 +245,7 @@ grub_menu_execute_entry(grub_menu_entry_t entry, int auto_boot)
   else
     grub_env_unset ("default");
 
-  grub_script_execute_sourcecode (entry->sourcecode, entry->argc, entry->args);
+  grub_script_execute_new_scope (entry->sourcecode, entry->argc, entry->args);
 
   if (errs_before != grub_err_printed_errors)
     grub_wait_after_message ();
grub-core/normal/menu_entry.c +1 -1
index fae258a..506715c 100644
@@ -1160,7 +1160,7 @@ run (struct screen *screen)
       }
     script[size] = '\0';
   }
-  grub_script_execute_sourcecode (script, 0, dummy);
+  grub_script_execute_new_scope (script, 0, dummy);
   grub_free (script);
 
   if (errs_before != grub_err_printed_errors)
grub-core/script/execute.c +23 -10
index 9babbee..afd5513 100644
@@ -856,19 +856,10 @@ grub_script_execute_sourcecode_getline (char **line,
 
 /* Execute a source script.  */
 grub_err_t
-grub_script_execute_sourcecode (const char *source, int argc, char **args)
+grub_script_execute_sourcecode (const char *source)
 {
   grub_err_t ret = 0;
   struct grub_script *parsed_script;
-  struct grub_script_scope new_scope;
-  struct grub_script_scope *old_scope;
-
-  new_scope.argv.argc = argc;
-  new_scope.argv.args = args;
-  new_scope.flags = 0;
-
-  old_scope = scope;
-  scope = &new_scope;
 
   while (source)
     {
@@ -880,13 +871,35 @@ grub_script_execute_sourcecode (const char *source, int argc, char **args)
       if (! parsed_script)
 	{
 	  ret = grub_errno;
+	  grub_free (line);
 	  break;
 	}
 
       ret = grub_script_execute (parsed_script);
+      grub_script_free (parsed_script);
       grub_free (line);
     }
 
+  return ret;
+}
+
+/* Execute a source script in new scope.  */
+grub_err_t
+grub_script_execute_new_scope (const char *source, int argc, char **args)
+{
+  grub_err_t ret = 0;
+  struct grub_script_scope new_scope;
+  struct grub_script_scope *old_scope;
+
+  new_scope.argv.argc = argc;
+  new_scope.argv.args = args;
+  new_scope.flags = 0;
+
+  old_scope = scope;
+  scope = &new_scope;
+
+  ret = grub_script_execute_sourcecode (source);
+
   scope = old_scope;
   return ret;
 }
include/grub/script_sh.h +2 -1
index 78602e4..57bdd4d 100644
@@ -329,7 +329,8 @@ grub_err_t grub_script_execute_cmdwhile (struct grub_script_cmd *cmd);
 
 /* Execute any GRUB pre-parsed command or script.  */
 grub_err_t grub_script_execute (struct grub_script *script);
-grub_err_t grub_script_execute_sourcecode (const char *source, int argc, char **args);
+grub_err_t grub_script_execute_sourcecode (const char *source);
+grub_err_t grub_script_execute_new_scope (const char *source, int argc, char **args);
 
 /* Break command for loops.  */
 grub_err_t grub_script_break (grub_command_t cmd, int argc, char *argv[]);
#12 lore raw

Re: New command eval.

Patch is good. Could you add corresponding testcase to testsuite and commit?

Quoted 283 lines · Andrey Borzenkov

В Sat, 11 May 2013 19:02:58 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 34 lines · Andrey Borzenkov

grub-core/commands/eval.c line 39

Quoted 8 lines
grub-core/commands/eval.c new file +7 -0
+  grub_size_t size = argc; /* +1 for final zero */
+  char *str, *p;
+  grub_err_t ret;
+
+  if (argc == 0)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing arguments"));
+

eval with no arguments returns success in bash, to match this behaviour, it should be GRUB_ERR_NONE and no use of grub_error

grub-core/commands/eval.c

Quoted 11 lines
grub-core/commands/eval.c new file +11 -0
+  for (i = 0; i < argc; i++)
+    size += grub_strlen (argv[i]);
+
+  str = p = grub_malloc (size);
+  if (!str)
+    return grub_errno;
+
+  for (i = 0; i < argc; i++)
+    {
+      grub_strcpy (p, argv[i]);
+      p += grub_strlen (argv[i]);

These 2 operations should be condensed by using grub_stpcpy

Review

Quoted 5 lines
(quoted diff) +2 -1
+      *p++ = ' ';
 
 
-  grub_script_execute_sourcecode (script, 0, dummy);
+  grub_script_execute_sourcecode (script);

(in menu_entry.c) here you need a new scope for consistency.

ChangeLog +8 -0
index cd8213a..60c64ee 100644
@@ -1,3 +1,11 @@
+2013-05-11  Andrey Borzenkov <[email protected]>
+
+	* grub-core/script/execute.c (grub_script_execute_sourcecode): Split
+	off new function grub_script_execute_new_scope. Change callers to use
+	either of them as appropriate.
+	* grub-core/commands/eval.c: New command eval.
+	* docs/grub.texi (Commands): Document it.
+
 2013-05-11  Vladimir Serbinenko  <[email protected]>
 
 	* util/grub-install.in: Gettextize "Not found" message.
docs/grub.texi +10 -0
index 75a5ea4..9dcfa2d 100644
@@ -3435,6 +3435,7 @@ you forget a command, you can run the command @command{help}
 * date::                        Display or set current date and time
 * drivemap::                    Map a drive to another
 * echo::                        Display a line of text
+* eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully
 * gettext::                     Translate a string
@@ -3812,6 +3813,15 @@ character will print that character.
 @end deffn
 
 
+@node eval
+@subsection eval
+
+@deffn Command eval string ...
+Concatenate arguments together using single space as separator and evaluate
+result as sequence of GRUB commands.
+@end deffn
+
+
 @node export
 @subsection export
 
grub-core/Makefile.core.def +5 -0
index 602b497..0fcc4e7 100644
@@ -699,6 +699,11 @@ module = {
 };
 
 module = {
+  name = eval;
+  common = commands/eval.c;
+};
+
+module = {
   name = extcmd;
   common = commands/extcmd.c;
   common = lib/arg.c;
grub-core/commands/eval.c new file +71 -0
new file mode 100644
index 0000000..f826a4f
@@ -0,0 +1,71 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2009  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/script_sh.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+#include <grub/term.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_err_t
+grub_cmd_eval (grub_command_t cmd __attribute__((__unused__)),
+	       int argc, char *argv[])
+{
+  int i;
+  grub_size_t size = argc; /* +1 for final zero */
+  char *str, *p;
+  grub_err_t ret;
+
+  if (argc == 0)
+    return GRUB_ERR_NONE;
+
+  for (i = 0; i < argc; i++)
+    size += grub_strlen (argv[i]);
+
+  str = p = grub_malloc (size);
+  if (!str)
+    return grub_errno;
+
+  for (i = 0; i < argc; i++)
+    {
+      p = grub_stpcpy (p, argv[i]);
+      *p++ = ' ';
+    }
+  *--p = '\0';
+
+  ret = grub_script_execute_sourcecode (str);
+  grub_free (str);
+  return ret;
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(eval)
+{
+  cmd = grub_register_command ("eval", grub_cmd_eval, N_("STRING ..."),
+				N_("Evaluate arguments as GRUB commands"));
+}
+
+GRUB_MOD_FINI(eval)
+{
+  grub_unregister_command (cmd);
+}
+
grub-core/normal/menu.c +1 -1
index fba19db..9b88290 100644
@@ -245,7 +245,7 @@ grub_menu_execute_entry(grub_menu_entry_t entry, int auto_boot)
   else
     grub_env_unset ("default");
 
-  grub_script_execute_sourcecode (entry->sourcecode, entry->argc, entry->args);
+  grub_script_execute_new_scope (entry->sourcecode, entry->argc, entry->args);
 
   if (errs_before != grub_err_printed_errors)
     grub_wait_after_message ();
grub-core/normal/menu_entry.c +1 -1
index fae258a..506715c 100644
@@ -1160,7 +1160,7 @@ run (struct screen *screen)
       }
     script[size] = '\0';
   }
-  grub_script_execute_sourcecode (script, 0, dummy);
+  grub_script_execute_new_scope (script, 0, dummy);
   grub_free (script);
 
   if (errs_before != grub_err_printed_errors)
grub-core/script/execute.c +23 -10
index 9babbee..afd5513 100644
@@ -856,19 +856,10 @@ grub_script_execute_sourcecode_getline (char **line,
 
 /* Execute a source script.  */
 grub_err_t
-grub_script_execute_sourcecode (const char *source, int argc, char **args)
+grub_script_execute_sourcecode (const char *source)
 {
   grub_err_t ret = 0;
   struct grub_script *parsed_script;
-  struct grub_script_scope new_scope;
-  struct grub_script_scope *old_scope;
-
-  new_scope.argv.argc = argc;
-  new_scope.argv.args = args;
-  new_scope.flags = 0;
-
-  old_scope = scope;
-  scope = &new_scope;
 
   while (source)
     {
@@ -880,13 +871,35 @@ grub_script_execute_sourcecode (const char *source, int argc, char **args)
       if (! parsed_script)
 	{
 	  ret = grub_errno;
+	  grub_free (line);
 	  break;
 	}
 
       ret = grub_script_execute (parsed_script);
+      grub_script_free (parsed_script);
       grub_free (line);
     }
 
+  return ret;
+}
+
+/* Execute a source script in new scope.  */
+grub_err_t
+grub_script_execute_new_scope (const char *source, int argc, char **args)
+{
+  grub_err_t ret = 0;
+  struct grub_script_scope new_scope;
+  struct grub_script_scope *old_scope;
+
+  new_scope.argv.argc = argc;
+  new_scope.argv.args = args;
+  new_scope.flags = 0;
+
+  old_scope = scope;
+  scope = &new_scope;
+
+  ret = grub_script_execute_sourcecode (source);
+
   scope = old_scope;
   return ret;
 }
include/grub/script_sh.h +2 -1
index 78602e4..57bdd4d 100644
@@ -329,7 +329,8 @@ grub_err_t grub_script_execute_cmdwhile (struct grub_script_cmd *cmd);
 
 /* Execute any GRUB pre-parsed command or script.  */
 grub_err_t grub_script_execute (struct grub_script *script);
-grub_err_t grub_script_execute_sourcecode (const char *source, int argc, char **args);
+grub_err_t grub_script_execute_sourcecode (const char *source);
+grub_err_t grub_script_execute_new_scope (const char *source, int argc, char **args);
 
 /* Break command for loops.  */
 grub_err_t grub_script_break (grub_command_t cmd, int argc, char *argv[]);

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#13 lore raw

Re: New command eval.

In reply to Andrey Borzenkov

Hi,

What about the feature environment variable to allow dynamic detection of this enhancement?

--S

Quoted 276 lines · Andrey Borzenkov

В Sat, 04 May 2013 23:08:03 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 14 lines
Quoted 2 lines

- it adds "eval" command (same as known from UNIX shells) which executes its argument

eval is generally good

grub-core/commands/eval.c line 31

Quoted 4 lines
grub-core/commands/eval.c new file +4 -0
+static grub_err_t
+grub_cmd_eval (grub_extcmd_context_t ctxt __attribute__((__unused__)),
+           int argc, char *argv[])
+{

You don't use any argument parsing. It's better to use command and not extcmd in this case. Also "eval" in bash concatenates its arguments.

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+  cmd = grub_register_extcmd ("eval", grub_cmd_eval, 0,
+                  N_("[STRING]"), N_("Evaluate commands"),

This description seems a bit vague for either human-reading or translation.

Is patch below OK? I decided to split off pure execute_sourcecode so that eval context would not clobber positional arguments.

ChangeLog +8 -0
index cd8213a..8ca9874 100644
@@ -1,3 +1,11 @@
+2013-05-11  Andrey Borzenkov <[email protected]>
+
+    * grub-core/script/execute.c (grub_script_execute_sourcecode): Split
+    off new function grub_script_execute_new_scope. Change callers to use
+    either of them as appropriate.
+    * grub-core/commands/eval.c: New command eval.
+    * docs/grub.texi (Commands): Document it.
+

2013-05-11 Vladimir Serbinenko <[email protected]>

* util/grub-install.in: Gettextize "Not found" message.

docs/grub.texi +1 -0
index 75a5ea4..9dcfa2d 100644
@@ -3435,6 +3435,7 @@ you forget a command, you can run the command @command{help}
 * date::                        Display or set current date and time
 * drivemap::                    Map a drive to another
 * echo::                        Display a line of text
+* eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully
 * gettext::                     Translate a string
@@ -3812,6 +3813,15 @@ character will print that character.

@end deffn

+@node eval
+@subsection eval

docs/grub.texi +7 -0
+
+@deffn Command eval string ...
+Concatenate arguments together using single space as separator and evaluate
+result as sequence of GRUB commands.
+@end deffn
+
+

@node export
@subsection export

grub-core/Makefile.core.def +5 -0
index 602b497..0fcc4e7 100644
@@ -699,6 +699,11 @@ module = {
 };
 
 module = {
+  name = eval;
+  common = commands/eval.c;
+};
+
+module = {
  name = extcmd;
  common = commands/extcmd.c;
  common = lib/arg.c;
grub-core/commands/eval.c new file +72 -0
new file mode 100644
index 0000000..e514a1a
@@ -0,0 +1,72 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2009  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/script_sh.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+#include <grub/term.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_err_t
+grub_cmd_eval (grub_command_t cmd __attribute__((__unused__)),
+           int argc, char *argv[])
+{
+  int i;
+  grub_size_t size = argc; /* +1 for final zero */
+  char *str, *p;
+  grub_err_t ret;
+
+  if (argc == 0)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing arguments"));
+
+  for (i = 0; i < argc; i++)
+    size += grub_strlen (argv[i]);
+
+  str = p = grub_malloc (size);
+  if (!str)
+    return grub_errno;
+
+  for (i = 0; i < argc; i++)
+    {
+      grub_strcpy (p, argv[i]);
+      p += grub_strlen (argv[i]);
+      *p++ = ' ';
+    }
+  *--p = '\0';
+
+  ret = grub_script_execute_sourcecode (str);
+  grub_free (str);
+  return ret;
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(eval)
+{
+  cmd = grub_register_command ("eval", grub_cmd_eval, N_("STRING ..."),
+                N_("Evaluate arguments as GRUB commands"));
+}
+
+GRUB_MOD_FINI(eval)
+{
+  grub_unregister_command (cmd);
+}
+
grub-core/normal/menu.c +1 -1
index fba19db..9b88290 100644
@@ -245,7 +245,7 @@ grub_menu_execute_entry(grub_menu_entry_t entry, int auto_boot)
  else
    grub_env_unset ("default");
 
-  grub_script_execute_sourcecode (entry->sourcecode, entry->argc, entry->args);
+  grub_script_execute_new_scope (entry->sourcecode, entry->argc, entry->args);
 
  if (errs_before != grub_err_printed_errors)
    grub_wait_after_message ();
grub-core/normal/menu_entry.c +1 -2
index fae258a..0f15a7e 100644
@@ -1120,7 +1120,6 @@ run (struct screen *screen)
  char *script;
  int errs_before;
  grub_menu_t menu = NULL;
-  char *dummy[1] = { NULL };
 
  grub_cls ();
  grub_printf ("  ");
@@ -1160,7 +1159,7 @@ run (struct screen *screen)
      }
    script[size] = '\0';
  }
-  grub_script_execute_sourcecode (script, 0, dummy);
+  grub_script_execute_sourcecode (script);
  grub_free (script);
 
  if (errs_before != grub_err_printed_errors)
grub-core/script/execute.c +0 -0
index 9babbee..afd5513 100644
@@ -856,19 +856,10 @@ grub_script_execute_sourcecode_getline (char **line,
 
 /* Execute a source script.  */

grub_err_t

grub-core/script/execute.c +1 -1
-grub_script_execute_sourcecode (const char *source, int argc, char **args)
+grub_script_execute_sourcecode (const char *source)

{
grub_err_t ret = 0;
struct grub_script *parsed_script;

grub-core/script/execute.c +22 -9
-  struct grub_script_scope new_scope;
-  struct grub_script_scope *old_scope;
-
-  new_scope.argv.argc = argc;
-  new_scope.argv.args = args;
-  new_scope.flags = 0;
-
-  old_scope = scope;
-  scope = &new_scope;
 
  while (source)
    {
@@ -880,13 +871,35 @@ grub_script_execute_sourcecode (const char *source, int argc, char **args)
      if (! parsed_script)
   {
     ret = grub_errno;
+      grub_free (line);
     break;
   }
 
      ret = grub_script_execute (parsed_script);
+      grub_script_free (parsed_script);
      grub_free (line);
    }
 
+  return ret;
+}
+
+/* Execute a source script in new scope.  */
+grub_err_t
+grub_script_execute_new_scope (const char *source, int argc, char **args)
+{
+  grub_err_t ret = 0;
+  struct grub_script_scope new_scope;
+  struct grub_script_scope *old_scope;
+
+  new_scope.argv.argc = argc;
+  new_scope.argv.args = args;
+  new_scope.flags = 0;
+
+  old_scope = scope;
+  scope = &new_scope;
+
+  ret = grub_script_execute_sourcecode (source);
+
  scope = old_scope;
  return ret;
 }
include/grub/script_sh.h +2 -1
index 78602e4..57bdd4d 100644
@@ -329,7 +329,8 @@ grub_err_t grub_script_execute_cmdwhile (struct grub_script_cmd *cmd);
 
 /* Execute any GRUB pre-parsed command or script.  */
 grub_err_t grub_script_execute (struct grub_script *script);
-grub_err_t grub_script_execute_sourcecode (const char *source, int argc, char **args);
+grub_err_t grub_script_execute_sourcecode (const char *source);
+grub_err_t grub_script_execute_new_scope (const char *source, int argc, char **args);
 
 /* Break command for loops.  */
 grub_err_t grub_script_break (grub_command_t cmd, int argc, char *argv[]);

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#14 lore raw

Re: New command eval.

В Sat, 11 May 2013 17:07:19 -0700
Seth Goldberg <[email protected]> пишет:

Quoted 4 lines

Hi,

What about the feature environment variable to allow dynamic detection of this enhancement?

That really does not scale. Commands come and go. What about single command "has" (or probably better "implements") as in

implements --command eval|testspeed|...
implements --crypto_algorithm sha256|...
implements --network_protocol http|tftp|ipv6|...

Command "implements" itself may be instantiated under fetature_201_final (or whatever next version is).

It probably could even be extended to feature test of single commands, like

implements --command menuentry --feature id

Note that in this specific case (eval) this probably is not useful at all. If you want to test whether this command is present, you presumably are going to write alternative implementation without eval. In this case why bother to test in the first place?

#15 lore raw

Re: New command eval.

Quoted 10 lines · Andrey Borzenkov

В Sat, 11 May 2013 17:07:19 -0700
Seth Goldberg <[email protected]> пишет:

Quoted 4 lines

Hi,

What about the feature environment variable to allow dynamic detection of this enhancement?

That really does not scale. Commands come and go. What about single command "has" (or probably better "implements") as in

That's what we've had thus far, which is why I'm referring to it. Existing script that has to run regardless of the version of GRUB will continue to need to refer to the existing feature vars. Going forward, I'm OK with your scheme.

--S

Quoted 23 lines

implements --command eval|testspeed|...
implements --crypto_algorithm sha256|...
implements --network_protocol http|tftp|ipv6|...

Command "implements" itself may be instantiated under fetature_201_final (or whatever next version is).

It probably could even be extended to feature test of single commands, like

implements --command menuentry --feature id

Note that in this specific case (eval) this probably is not useful at all. If you want to test whether this command is present, you presumably are going to write alternative implementation without eval. In this case why bother to test in the first place?

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#17 lore raw
In reply to Andrey Borzenkov

----- Original Message -----

Quoted 167 lines

From: Andrey Borzenkov <[email protected]>
To: Rigoberto Corujo <[email protected]>
Cc: "[email protected]" <[email protected]>; [email protected]
Sent: Wednesday, May 1, 2013 10:59 AM
Subject: Re: Obtaining the MAC address of the boot NIC for a PXE boot

В Mon, 29 Apr 2013 05:55:32 -0700 (PDT)
Rigoberto Corujo <[email protected]> пишет:

Quoted 3 lines

Hello everyone,

With pxelinux, you can specify "IPAPPEND 2" in your boot loader

configuration file and the MAC address of the boot NIC automatically gets appended to your kernel parameters as "BOOTIF=XX:XX:XX:XX:XX:XX".

Quoted 2 lines

I am now trying to become familiar with Grub 2, because if its EFI support.

  I noticed that with Grub 2, some of the documentation, which may be outdated, says that there is a "net_pxe_mac" variable which, as I understand it, should contain the MAC address of the boot NIC.  However, I've built the latest Grub 2 sources and, when I PXE boot and drop into a grub shell prompt, if type "set", I don't see "net_pxe_mac" listed as one of
the variables.  I know there is a "net_ls_addr" command which prints out something like "efinet8 3c:4a:92:b2:6a:e8 10.1.1.114", but I don't know how to use Grub scripting to parse out the MAC address.  I also don't know if there will ever be a case where "net_ls_addr" will print out multiple entries on a PXE boot, making it difficult to determine which one corresponds to the boot NIC.

Quoted 3 lines

Basically, I'm using the "ksdevice=bootif" kernel parameter

when PXE booting my Red Hat kernel.  With PXE Linux, "bootif" provides the MAC address of the boot NIC.  I was hoping to do something like "ksdevice=${net_pxe_mac}", but since "net_pxe_mac" does not appear to be getting set, I need to find some other method.

Quoted 1 line

As it stands currently, net_pxe_* variables are defined on PC BIOS platform only. For UEFI (which you apparently have) GRUB2 defines net_efinetNN_* variables where efinetNN is symbolic name for interface that was used to boot GRUB2.

May be GRUB2 should also define net_pxe_* namespace for the case of UEFI. There is no real way to find out which interface was used for booting and even if there were, grub does not support nested variables substitution or eval'ing (like ${net_${boot_if}_mac).

Probably, adding "eval" support is really the most simple. Could you test the patch below. What it does, is

- it adds "eval" command (same as known from UNIX shells) which executes its argument

- it exports boot interface as "efi_boot_interface" variable

This should allow you to do

if $grub_platform = efi ; then
  eval "set net_pxe_mac=\$net_${efi_boot_interface}_mac" fi

and then use $net_pxe_mac on both platforms.

If it works for you, I will clean it up; for consistency this is probably needed for ieee1275 as well.

---
grub-core/Makefile.core.def        |  5 ++++ grub-core/commands/eval.c          | 51 ++++++++++++++++++++++++++++++++++++++ grub-core/net/drivers/efi/efinet.c |  2 ++

3 files changed, 58 insertions(+)
grub-core/Makefile.core.def +5 -0
index 7e19acb..8844f2f 100644
@@ -699,6 +699,11 @@ module = {
 };
 
 module = {
+  name = eval;
+  common = commands/eval.c;
+};
+
+module = {
    name = extcmd;
    common = commands/extcmd.c;
    common = lib/arg.c;
grub-core/commands/eval.c new file +34 -0
new file mode 100644
index 0000000..a21ff89
@@ -0,0 +1,51 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2009  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/extcmd.h>
+#include <grub/i18n.h>
+#include <grub/term.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_err_t
+grub_cmd_eval (grub_extcmd_context_t ctxt __attribute__((__unused__)),
+           int argc, char *argv[])
+{
+  char *dummy[1] = { NULL };
+
+  if (argc != 1)
+    return  grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument 

expected"));
+  return grub_script_execute_sourcecode (argv[0], 0, dummy);

grub-core/commands/eval.c new file +16 -0
+}
+
+static grub_extcmd_t cmd;
+
+GRUB_MOD_INIT(eval)
+{
+  cmd = grub_register_extcmd ("eval", grub_cmd_eval, 0,
+                  N_("[STRING]"), N_("Evaluate commands"),
+                              NULL);
+}
+
+GRUB_MOD_FINI(eval)
+{
+  grub_unregister_extcmd (cmd);
+}
+
grub-core/net/drivers/efi/efinet.c +2 -0
index 2b344d6..f7d8fd5 100644
@@ -23,6 +23,7 @@
 #include <grub/efi/api.h>
 #include <grub/efi/efi.h>
 #include <grub/i18n.h>
+#include <grub/env.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -250,6 +251,7 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char 
 **device,
                     &pxe_mode->dhcp_ack,
                     sizeof (pxe_mode->dhcp_ack),
                     1, device, path);
+    grub_env_set ("efi_boot_interface", card->name);
      return;
    }
Quoted 10 lines

}

Signature · 7 lines
tg: (93daf86..) e/uefi_pxe_vars (depends on: master)


_______________________________________________
Help-grub mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/help-grub

Thank you very much, Andrey.  This worked.  I did have to add the following lines to "grub-core/Makefile.core.am" to get a "eval.mod" generated, but I'm sure that it's probably because I missed an important step that would have automatically generated it.

if COND_x86_64_efi
platform_PROGRAMS += eval.module
MODULE_FILES += eval.module$(EXEEXT) eval_module_SOURCES  = commands/eval.c  ## platform sources nodist_eval_module_SOURCES  =  ## platform nodist sources eval_module_LDADD  =
eval_module_CFLAGS  = $(AM_CFLAGS) $(CFLAGS_MODULE) eval_module_LDFLAGS  = $(AM_LDFLAGS) $(LDFLAGS_MODULE) eval_module_CPPFLAGS  = $(AM_CPPFLAGS) $(CPPFLAGS_MODULE) eval_module_CCASFLAGS  = $(AM_CCASFLAGS) $(CCASFLAGS_MODULE) EXTRA_DIST +=
BUILT_SOURCES += $(nodist_eval_module_SOURCES) CLEANFILES += $(nodist_eval_module_SOURCES) MOD_FILES += eval.mod
MARKER_FILES += eval.marker
CLEANFILES += eval.marker

eval.marker: $(eval_module_SOURCES) $(nodist_eval_module_SOURCES)
        $(TARGET_CPP) -DGRUB_LST_GENERATOR $(CPPFLAGS_MARKER) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(eval_module_CPPFLAGS) $(CPPFLAGS) $^ > [email protected] || (rm -f $@; exit 1) grep 'MARKER' [email protected] > $@; rm -f [email protected]
endif

Anyway, thank you once again.

Rigoberto

#18 lore raw

В Wed, 8 May 2013 10:27:47 -0700 (PDT)
Rigoberto Corujo <[email protected]> пишет:

Quoted 3 lines

Thank you very much, Andrey.  This worked.  I did have to add the following lines to "grub-core/Makefile.core.am" to get a "eval.mod" generated, but I'm sure that it's probably because I missed an important step that would have automatically generated it.

In the meantime Vladimir pushed different implementation that provides net_default_mac:

`net_default_interface'
Initially set to name of network interface that was used to load grub. Read-write, although setting it affects only interpretation of net_default_ip' and net_default_mac'

`net_default_ip'
The IP address of default interface. Read-only. This is alias for the `net_${net_default_interface}_ip'.

`net_default_mac'
The default interface's MAC address. Read-only. This is alias for the `net_${net_default_interface}_mac'.

Could you verify that it works for you? Also if you would comment on documentation in Network section from user's perspective.

#19 lore raw

----- Original Message -----

Quoted 32 lines

From: Andrey Borzenkov <[email protected]>
To: Rigoberto Corujo <[email protected]>
Cc: "[email protected]" <[email protected]>; "[email protected]" <[email protected]>
Sent: Wednesday, May 8, 2013 3:25 PM
Subject: Re: Obtaining the MAC address of the boot NIC for a PXE boot

В Wed, 8 May 2013 10:27:47 -0700 (PDT)
Rigoberto Corujo <[email protected]> пишет:

Quoted 2 lines

Thank you very much, Andrey.  This worked.  I did have to add the following

lines to "grub-core/Makefile.core.am" to get a "eval.mod" generated, but I'm sure that it's probably because I missed an important step that would have automatically generated it.

Quoted 1 line

In the meantime Vladimir pushed different implementation that provides net_default_mac:

`net_default_interface'
    Initially set to name of network interface that was used to load grub.  Read-write, although setting it affects only interpretation of net_default_ip' and net_default_mac'

`net_default_ip'
    The IP address of default interface.  Read-only. This is alias for the `net_${net_default_interface}_ip'.

`net_default_mac'
    The default interface's MAC address.  Read-only.  This is alias for the `net_${net_default_interface}_mac'.

Quoted 9 lines

Could you verify that it works for you? Also if you would comment on documentation in Network section from user's perspective.

_______________________________________________
Help-grub mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/help-grub

Thank you Andrey.  I can confirm that the net_default_interface, net_default_ip, and net_default_mac variables worked as expected.  I did notice, however, then when I'm in the grub shell and I type "set", these variables show up in the list, but they have no values, which makes it appear as if something is wrong.  If I echo the variables, however, I do see their correct values.

As far as the documentation, I would suggest that for the description of net_default_mac, something similar to the following text be added:

The net_default_mac variable may be used in your GRUB configuration file as a substitute for the BOOTIF=<hardware-address-of-boot-interface> option that gets appended to the Linux kernel parameters by the pxelinux IPAPPEND option.

I think most pxelinux users that are switching to GRUB 2 will do a Google search for BOOTIF and IPAPPEND, looking to see how to obtain the boot NIC's MAC address with GRUB 2, and having those two keywords in the documentation would make it easy to find the answer.

Thank you very much, once again.

Rigoberto

#20 lore raw

В Fri, 10 May 2013 09:12:57 -0700 (PDT)
Rigoberto Corujo <[email protected]> пишет:

Quoted 3 lines

Thank you Andrey.  I can confirm that the net_default_interface, net_default_ip, and net_default_mac variables worked as expected.  I did notice, however, then when I'm in the grub shell and I type "set", these variables show up in the list, but they have no values, which makes it appear as if something is wrong.  If I echo the variables, however, I do see their correct values.

Does it behave better with patch below?

grub-core/kern/corecmd.c +1 -1
index cfab676..1700eb4 100644
@@ -40,7 +40,7 @@ grub_core_cmd_set (struct grub_command *cmd __attribute__ ((unused)),
     {
       struct grub_env_var *env;
       FOR_SORTED_ENV (env)
-	grub_printf ("%s=%s\n", env->name, env->value);
+	grub_printf ("%s=%s\n", env->name, grub_env_get (env->name));
       return 0;
     }
 
#21 lore raw
Quoted 22 lines

________________________________
From: Andrey Borzenkov <[email protected]>
To: Rigoberto Corujo <[email protected]>
Cc: "[email protected]" <[email protected]>; "[email protected]" <[email protected]>
Sent: Saturday, May 11, 2013 12:47 AM
Subject: Re: Obtaining the MAC address of the boot NIC for a PXE boot

В Fri, 10 May 2013 09:12:57 -0700 (PDT)
Rigoberto Corujo <[email protected]> пишет:

Quoted 3 lines

Thank you Andrey.  I can confirm that the net_default_interface, net_default_ip, and net_default_mac variables worked as expected.  I did notice, however, then when I'm in the grub shell and I type "set", these variables show up in the list, but they have no values, which makes it appear as if something is wrong.  If I echo the variables, however, I do see their correct values.

Does it behave better with patch below?

grub-core/kern/corecmd.c +0 -0
index cfab676..1700eb4 100644

grub-core/kern/corecmd.c grub_core_cmd_set (struct grub_command *cmd __attribute__ ((unused)), · line 46

Quoted 8 lines
grub-core/kern/corecmd.c +1 -1
@@ -40,7 +40,7 @@ grub_core_cmd_set (struct grub_command *cmd __attribute__ ((unused)),
      {
        struct grub_env_var *env;
        FOR_SORTED_ENV (env)
-    grub_printf ("%s=%s\n", env->name, env->value);
+    grub_printf ("%s=%s\n", env->name, grub_env_get (env->name));
        return 0;
      }
 

Yes, Andrey.  With the patch, I can now see the values of the net_default_interface, net_default_ip, and net_default_mac variables when I type "set" at the grub shell.

Thank you.

Rigoberto