Skip to contentSkip to conversation

Second Distro-agnostic series taken from Fedora Rawhide

PATCH Superseded Leo Sandoval 4 participants 36 messages Oct 7, 2024 – Oct 15, 2024 Kbuild

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Second Distro-agnostic series taken from Fedora Rawhide

This is the second patch series, first version, taken from Fedora Rawhide spec [1] that are distro-agnostic. The goal is to merge most of them so all the community/distros would benefit.

Andrei Borzenkov (1):
btrfs: Fallback to old subvol name scheme to support old snapshot
config

Jeff Mahoney (1):
btrfs: Add ability to boot from subvolumes

Josef Bacik (1):
net/tcp: add window scaling support

Michael Chang (7):
bootp: New net_bootp6 command
btrfs: export btrfs_subvol and btrfs_subvolid
btrfs: grub2-btrfs-03-follow_default
btrfs: grub2-btrfs-04-grub2-install
btdfs: grub2-btrfs-05-grub2-mkconfig
btrfs: grub2-btrfs-06-subvol-mount
btrfs: Grub not working correctly with btrfs snapshots (bsc#1026511)

Peter Jones (9):
10_linux.in: Make grub2-mkconfig construct titles that look like the
ones we want elsewhere.
efinet and bootp: add support for dhcpv6 grub-get-kernel-settings.in: Add grub-get-kernel-settings and use it

    in 10_linux

backtrace: Make grub_fatal() also backtrace
grub.texi: Make our info pages say "grub2" where appropriate. 30_os-prober.in: just build chainloader entries, don't try any xnu
xnu.
btrfs: fix a bad null check
efi: Add grub_efi_allocate_pool() and grub_efi_free_pool() wrappers.
chainloader: Use grub_efi_...() memory helpers where reasonable.

Robert Marshall (1):
grub-set-password.in: Add friendly grub2 password config tool

    (#985962)
 Makefile.util.def                             |   20 +
 configure.ac                                  |    2 +
 docs/grub-dev.texi                            |    4 +-
 docs/grub.texi                                |  359 +++---
 docs/man/grub-get-kernel-settings.h2m         |    2 +
 docs/man/grub-set-password.h2m                |    2 +
 grub-core/Makefile.core.def                   |    3 +
 grub-core/fs/btrfs.c                          | 1090 ++++++++++++++++-
 grub-core/kern/misc.c                         |    6 +
 grub-core/lib/arm64/backtrace.c               |   62 +
 grub-core/lib/backtrace.c                     |    2 +
 grub-core/lib/i386/backtrace.c                |   14 +-
 grub-core/loader/efi/chainloader.c            |    4 +-
 grub-core/net/bootp.c                         |  914 +++++++++++++-
 grub-core/net/drivers/efi/efinet.c            |   84 +-
 grub-core/net/ip.c                            |   39 +
 grub-core/net/net.c                           |   72 ++
 grub-core/net/tcp.c                           |   42 +-
 grub-core/net/tftp.c                          |    3 +
 grub-core/osdep/linux/getroot.c               |  151 ++-
 grub-core/osdep/unix/config.c                 |   17 +-
 include/grub/btrfs.h                          |    1 +
 include/grub/efi/api.h                        |  129 +-
 include/grub/efi/efi.h                        |   36 +-
 include/grub/emu/config.h                     |    1 +
 include/grub/emu/getroot.h                    |    5 +
 include/grub/net.h                            |   79 ++
 .../bash-completion.d/grub-completion.bash.in |   22 +
 util/config.c                                 |   10 +
 util/grub-get-kernel-settings.in              |   88 ++
 util/grub-install.c                           |   63 +
 util/grub-mkconfig.in                         |    8 +-
 util/grub-mkconfig_lib.in                     |    4 +
 util/grub-mkrelpath.c                         |    6 +
 util/grub-set-password.in                     |  121 ++
 util/grub.d/00_header.in                      |   25 +-
 util/grub.d/01_users.in                       |   11 +
 util/grub.d/10_linux.in                       |   61 +-
 util/grub.d/20_linux_xen.in                   |    4 +
 util/grub.d/30_os-prober.in                   |   78 +-
 40 files changed, 3305 insertions(+), 339 deletions(-)
 create mode 100644 docs/man/grub-get-kernel-settings.h2m
 create mode 100644 docs/man/grub-set-password.h2m
 create mode 100644 grub-core/lib/arm64/backtrace.c
 create mode 100644 util/grub-get-kernel-settings.in
 create mode 100644 util/grub-set-password.in
 create mode 100644 util/grub.d/01_users.in
Signature · 7 lines
2.46.2


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

PATCH 18/37 10_linux.in: Make grub2-mkconfig construct titles that look like the ones we want elsewhere.

From: Peter Jones <[email protected]>

Resolves: rhbz#1215839

---
 util/grub.d/10_linux.in | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)
util/grub.d/10_linux.in +27 -7
index 3105c31e4..0115dd0c0 100644
@@ -80,6 +80,32 @@ case x"$GRUB_FS" in
 	;;
 esac
 
+mktitle ()
+{
+  local title_type
+  local version
+  local OS_NAME
+  local OS_VERS
+
+  title_type=$1 && shift
+  version=$1 && shift
+
+  OS_NAME="$(eval $(grep ^NAME= /etc/os-release) ; echo ${NAME})"
+  OS_VERS="$(eval $(grep ^VERSION= /etc/os-release) ; echo ${VERSION})"
+
+  case $title_type in
+    recovery)
+      title=$(printf '%s (%s) %s (recovery mode)' \
+                     "${OS_NAME}" "${version}" "${OS_VERS}")
+      ;;
+    *)
+      title=$(printf '%s (%s) %s' \
+                     "${OS_NAME}" "${version}" "${OS_VERS}")
+      ;;
+  esac
+  echo -n ${title}
+}
+
 title_correction_code=
 
 linux_entry ()
@@ -93,17 +119,11 @@ linux_entry ()
       boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
   fi
   if [ x$type != xsimple ] ; then
-      case $type in
-	  recovery)
-	      title="$(gettext_printf "%s, with Linux %s (recovery mode)" "${os}" "${version}")" ;;
-	  *)
-	      title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
-      esac
+      title=$(mktitle "$type" "$version")
       if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
 	  replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
 	  quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
 	  title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
-	  grub_warn "$(gettext_printf "Please don't use old title \`%s' for GRUB_DEFAULT, use \`%s' (for versions before 2.00) or \`%s' (for 2.00 or later)" "$GRUB_ACTUAL_DEFAULT" "$replacement_title" "gnulinux-advanced-$boot_device_id>gnulinux-$version-$type-$boot_device_id")"
       fi
       echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
   else
Signature · 7 lines
2.46.2


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

PATCH 19/37 grub-set-password.in: Add friendly grub2 password config tool (#985962)

In reply to Leo Sandoval

From: Robert Marshall <[email protected]>

Provided a tool for users to reset the grub2 root user password without having to alter the grub.cfg. The hashed password now lives in a root-only-readable configuration file.

Resolves: rhbz#985962

[pjones: fix the efidir in grub-setpassword and rename tool]
[luto: fix grub-setpassword -o's output path]
[rharwood: migrate man page to h2m, context]
---
 Makefile.util.def              |  13 ++++
 configure.ac                   |   1 +
 docs/man/grub-set-password.h2m |   2 +
 util/grub-mkconfig.in          |   2 +
 util/grub-set-password.in      | 121 +++++++++++++++++++++++++++++++++
 util/grub.d/01_users.in        |  11 +++
 6 files changed, 150 insertions(+)
 create mode 100644 docs/man/grub-set-password.h2m
 create mode 100644 util/grub-set-password.in
 create mode 100644 util/grub.d/01_users.in
Makefile.util.def +13 -0
index 09bfcadd9..95884956a 100644
@@ -455,6 +455,12 @@ script = {
   installdir = grubconf;
 };
 
+script = {
+  name = '01_users';
+  common = util/grub.d/01_users.in;
+  installdir = grubconf;
+};
+
 script = {
   name = '10_windows';
   common = util/grub.d/10_windows.in;
@@ -733,6 +739,13 @@ script = {
   installdir = sbin;
 };
 
+script = {
+  name = grub-set-password;
+  common = util/grub-set-password.in;
+  mansection = 8;
+  installdir = sbin;
+};
+
 script = {
   name = grub-mkconfig_lib;
   common = util/grub-mkconfig_lib.in;
configure.ac +1 -0
index ae3a49321..493e336ec 100644
@@ -77,6 +77,7 @@ grub_TRANSFORM([grub-mkrelpath])
 grub_TRANSFORM([grub-mkrescue])
 grub_TRANSFORM([grub-probe])
 grub_TRANSFORM([grub-reboot])
+grub_TRANSFORM([grub-set-password])
 grub_TRANSFORM([grub-script-check])
 grub_TRANSFORM([grub-set-default])
 grub_TRANSFORM([grub-sparc64-setup])
docs/man/grub-set-password.h2m new file +2 -0
new file mode 100644
index 000000000..10ee82f4d
@@ -0,0 +1,2 @@
+[NAME]
+grub-set-password \- generate the user.cfg file containing the hashed grub bootloader password
util/grub-mkconfig.in +2 -0
index d1bf4983b..9d5d7964b 100644
@@ -283,6 +283,8 @@ for i in "${grub_mkconfig_dir}"/* ; do
     *~) ;;
     # emacsen autosave files. FIXME: support other editors
     */\#*\#) ;;
+    # rpm config files of yore.
+    *.rpmsave|*.rpmnew|*.rpmorig) ;;
     *)
       if grub_file_is_not_garbage "$i" && test -x "$i" ; then
         echo
util/grub-set-password.in new file +121 -0

121 lines not on this page · raw message

util/grub.d/01_users.in new file +11 -0
new file mode 100644
index 000000000..db2f44bfb
@@ -0,0 +1,11 @@
+#!/bin/sh -e
+cat << EOF
+if [ -f \${prefix}/user.cfg ]; then
+  source \${prefix}/user.cfg
+  if [ -n "\${GRUB2_PASSWORD}" ]; then
+    set superusers="root"
+    export superusers
+    password_pbkdf2 root \${GRUB2_PASSWORD}
+  fi
+fi
+EOF
Signature · 7 lines
2.46.2


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

PATCH 20/37 net/tcp: add window scaling support

In reply to Leo Sandoval

From: Josef Bacik <[email protected]>

Sometimes we have to provision boxes across regions, such as California to Sweden. The http server has a 10 minute timeout, so if we can't get our 250mb image transferred fast enough our provisioning fails, which is not ideal. So add tcp window scaling on open connections and set the window size to 1mb. With this change we're able to get higher sustained transfers between regions and can transfer our image in well below 10 minutes. Without this patch we'd time out every time halfway through the transfer. Thanks,

---
 grub-core/net/tcp.c | 42 +++++++++++++++++++++++++++++-------------
 1 file changed, 29 insertions(+), 13 deletions(-)
grub-core/net/tcp.c +29 -13
index 93dee0caa..902ab7e8a 100644
@@ -106,6 +106,18 @@ struct tcphdr
   grub_uint16_t urgent;
 } GRUB_PACKED;
 
+struct tcp_scale_opt {
+  grub_uint8_t kind;
+  grub_uint8_t length;
+  grub_uint8_t scale;
+} GRUB_PACKED;
+
+struct tcp_synhdr {
+  struct tcphdr tcphdr;
+  struct tcp_scale_opt scale_opt;
+  grub_uint8_t padding;
+};
+
 struct tcp_pseudohdr
 {
   grub_uint32_t src;
@@ -571,7 +583,7 @@ grub_net_tcp_open (char *server,
   grub_net_tcp_socket_t socket;
   static grub_uint16_t in_port = 21550;
   struct grub_net_buff *nb;
-  struct tcphdr *tcph;
+  struct tcp_synhdr *tcph;
   int i;
   grub_uint8_t *nbd;
   grub_net_link_level_address_t ll_target_addr;
@@ -640,20 +652,24 @@ grub_net_tcp_open (char *server,
     }
 
   tcph = (void *) nb->data;
+  grub_memset(tcph, 0, sizeof (*tcph));
   socket->my_start_seq = grub_get_time_ms ();
   socket->my_cur_seq = socket->my_start_seq + 1;
-  socket->my_window = 8192;
-  tcph->seqnr = grub_cpu_to_be32 (socket->my_start_seq);
-  tcph->ack = grub_cpu_to_be32_compile_time (0);
-  tcph->flags = grub_cpu_to_be16_compile_time ((5 << 12) | TCP_SYN);
-  tcph->window = grub_cpu_to_be16 (socket->my_window);
-  tcph->urgent = 0;
-  tcph->src = grub_cpu_to_be16 (socket->in_port);
-  tcph->dst = grub_cpu_to_be16 (socket->out_port);
-  tcph->checksum = 0;
-  tcph->checksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_TCP,
-						   &socket->inf->address,
-						   &socket->out_nla);
+  socket->my_window = 32768;
+  tcph->tcphdr.seqnr = grub_cpu_to_be32 (socket->my_start_seq);
+  tcph->tcphdr.ack = grub_cpu_to_be32_compile_time (0);
+  tcph->tcphdr.flags = grub_cpu_to_be16_compile_time ((6 << 12) | TCP_SYN);
+  tcph->tcphdr.window = grub_cpu_to_be16 (socket->my_window);
+  tcph->tcphdr.urgent = 0;
+  tcph->tcphdr.src = grub_cpu_to_be16 (socket->in_port);
+  tcph->tcphdr.dst = grub_cpu_to_be16 (socket->out_port);
+  tcph->tcphdr.checksum = 0;
+  tcph->scale_opt.kind = 3;
+  tcph->scale_opt.length = 3;
+  tcph->scale_opt.scale = 5;
+  tcph->tcphdr.checksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_TCP,
+							  &socket->inf->address,
+							  &socket->out_nla);
 
   tcp_socket_register (socket);
 
Signature · 7 lines
2.46.2


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

PATCH 21/37 efinet and bootp: add support for dhcpv6

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

---
 grub-core/net/bootp.c              | 173 +++++++++++++++++++++++++++++
 grub-core/net/drivers/efi/efinet.c |  86 +++++++++-----
 grub-core/net/net.c                |  72 ++++++++++++
 grub-core/net/tftp.c               |   3 +
 include/grub/efi/api.h             | 129 ++++++++++++++++++++-
 include/grub/net.h                 |  60 ++++++++++
 6 files changed, 492 insertions(+), 31 deletions(-)
grub-core/net/bootp.c +173 -0
index abe45ef7b..02d2c2614 100644
@@ -905,6 +905,179 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
 
 static grub_command_t cmd_getdhcp, cmd_bootp, cmd_dhcp;
 
+struct grub_net_network_level_interface *
+grub_net_configure_by_dhcpv6_ack (const char *name,
+				  struct grub_net_card *card,
+				  grub_net_interface_flags_t flags
+				    __attribute__((__unused__)),
+				  const grub_net_link_level_address_t *hwaddr,
+				  const struct grub_net_dhcpv6_packet *packet,
+				  int is_def, char **device, char **path)
+{
+  struct grub_net_network_level_interface *inter = NULL;
+  struct grub_net_network_level_address addr;
+  int mask = -1;
+
+  if (!device || !path)
+    return NULL;
+
+  *device = 0;
+  *path = 0;
+
+  grub_dprintf ("net", "mac address is %02x:%02x:%02x:%02x:%02x:%02x\n",
+		hwaddr->mac[0], hwaddr->mac[1], hwaddr->mac[2],
+		hwaddr->mac[3], hwaddr->mac[4], hwaddr->mac[5]);
+
+  if (is_def)
+    grub_net_default_server = 0;
+
+  if (is_def && !grub_net_default_server && packet)
+    {
+      const grub_uint8_t *options = packet->dhcp_options;
+      unsigned int option_max = 1024 - OFFSET_OF (dhcp_options, packet);
+      unsigned int i;
+
+      for (i = 0; i < option_max - sizeof (grub_net_dhcpv6_option_t); )
+	{
+	  grub_uint16_t num, len;
+	  grub_net_dhcpv6_option_t *opt =
+	    (grub_net_dhcpv6_option_t *)(options + i);
+
+	  num = grub_be_to_cpu16(opt->option_num);
+	  len = grub_be_to_cpu16(opt->option_len);
+
+	  grub_dprintf ("net", "got dhcpv6 option %d len %d\n", num, len);
+
+	  if (len == 0)
+	    break;
+
+	  if (len + i > 1024)
+	    break;
+
+	  if (num == GRUB_NET_DHCP6_BOOTFILE_URL)
+	    {
+	      char *scheme, *userinfo, *host, *file;
+	      char *tmp;
+	      int hostlen;
+	      int port;
+	      int rc = extract_url_info ((const char *)opt->option_data,
+					 (grub_size_t)len,
+					 &scheme, &userinfo, &host, &port,
+					 &file);
+	      if (rc < 0)
+		continue;
+
+	      /* right now this only handles tftp. */
+	      if (grub_strcmp("tftp", scheme))
+		{
+		  grub_free (scheme);
+		  grub_free (userinfo);
+		  grub_free (host);
+		  grub_free (file);
+		  continue;
+		}
+	      grub_free (userinfo);
+
+	      hostlen = grub_strlen (host);
+	      if (hostlen > 2 && host[0] == '[' && host[hostlen-1] == ']')
+		{
+		  tmp = host+1;
+		  host[hostlen-1] = '\0';
+		}
+	      else
+		tmp = host;
+
+	      *device = grub_xasprintf ("%s,%s", scheme, tmp);
+	      grub_free (scheme);
+	      grub_free (host);
+
+	      if (file && *file)
+		{
+		  tmp = grub_strrchr (file, '/');
+		  if (tmp)
+		    *(tmp+1) = '\0';
+		  else
+		    file[0] = '\0';
+		}
+	      else if (!file)
+		file = grub_strdup ("");
+
+	      if (file[0] == '/')
+		{
+		  *path = grub_strdup (file+1);
+		  grub_free (file);
+		}
+	      else
+		*path = file;
+	    }
+	  else if (num == GRUB_NET_DHCP6_IA_NA)
+	    {
+	      const grub_net_dhcpv6_option_t *ia_na_opt;
+	      const grub_net_dhcpv6_opt_ia_na_t *ia_na =
+		(const grub_net_dhcpv6_opt_ia_na_t *)opt;
+	      unsigned int left = len - OFFSET_OF (options, ia_na);
+	      unsigned int j;
+
+	      if ((grub_uint8_t *)ia_na + left >
+		  (grub_uint8_t *)options + option_max)
+		left -= ((grub_uint8_t *)ia_na + left)
+		        - ((grub_uint8_t *)options + option_max);
+
+	      if (len < OFFSET_OF (option_data, opt)
+			+ sizeof (grub_net_dhcpv6_option_t))
+		{
+		  grub_dprintf ("net",
+				"found dhcpv6 ia_na option with no address\n");
+		  continue;
+		}
+
+	      for (j = 0; left > sizeof (grub_net_dhcpv6_option_t); )
+		{
+		  ia_na_opt = (const grub_net_dhcpv6_option_t *)
+			       (ia_na->options + j);
+		  grub_uint16_t ia_na_opt_num, ia_na_opt_len;
+
+		  ia_na_opt_num = grub_be_to_cpu16 (ia_na_opt->option_num);
+		  ia_na_opt_len = grub_be_to_cpu16 (ia_na_opt->option_len);
+		  if (ia_na_opt_len == 0)
+		    break;
+		  if (j + ia_na_opt_len > left)
+		    break;
+		  if (ia_na_opt_num == GRUB_NET_DHCP6_IA_ADDRESS)
+		    {
+		      const grub_net_dhcpv6_opt_ia_address_t *ia_addr;
+
+		      ia_addr = (const grub_net_dhcpv6_opt_ia_address_t *)
+				 ia_na_opt;
+		      addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+		      grub_memcpy(addr.ipv6, ia_addr->ipv6_address,
+				  sizeof (ia_addr->ipv6_address));
+		      inter = grub_net_add_addr (name, card, &addr, hwaddr, 0);
+		    }
+
+		  j += ia_na_opt_len;
+		  left -= ia_na_opt_len;
+		}
+	    }
+
+	  i += len + 4;
+	}
+
+      grub_print_error ();
+    }
+
+  if (is_def)
+    {
+      grub_env_set ("net_default_interface", name);
+      grub_env_export ("net_default_interface");
+    }
+
+    if (inter)
+      grub_net_add_ipv6_local (inter, mask);
+    return inter;
+}
+
+
 void
 grub_bootp_init (void)
 {
grub-core/net/drivers/efi/efinet.c +61 -25

141 lines not on this page · raw message

grub-core/net/net.c +72 -0

78 lines not on this page · raw message

grub-core/net/tftp.c +3 -0
index 409b1d09b..f300a9d40 100644
@@ -359,6 +359,7 @@ tftp_open (struct grub_file *file, const char *filename)
   file->not_easily_seekable = 1;
   file->data = data;
 
+  grub_dprintf("tftp", "resolving address for %s\n", file->device->net->server);
   err = grub_net_resolve_address (file->device->net->server, &addr);
   if (err)
     {
@@ -369,11 +370,13 @@ tftp_open (struct grub_file *file, const char *filename)
       return err;
     }
 
+  grub_dprintf("tftp", "opening connection\n");
   data->sock = grub_net_udp_open (addr,
 				  port ? port : TFTP_SERVER_PORT, tftp_receive,
 				  file);
   if (!data->sock)
     {
+      grub_dprintf("tftp", "connection failed\n");
       grub_free (data);
       return grub_errno;
     }
include/grub/efi/api.h +123 -6

149 lines not on this page · raw message

include/grub/net.h +60 -0

72 lines not on this page · raw message

Signature · 7 lines
2.46.2


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

PATCH 22/37 bootp: New net_bootp6 command

In reply to Leo Sandoval

From: Michael Chang <[email protected]>

Implement new net_bootp6 command for IPv6 network auto configuration via the DHCPv6 protocol (RFC3315).

[pjones: Put back our code to add a local route]
---
 grub-core/net/bootp.c              | 1059 +++++++++++++++++++++++-----
 grub-core/net/drivers/efi/efinet.c |   18 +-
 grub-core/net/ip.c                 |   39 +
 include/grub/efi/api.h             |    2 +-
 include/grub/net.h                 |   91 ++-
 5 files changed, 1001 insertions(+), 208 deletions(-)
grub-core/net/bootp.c +899 -160
index 02d2c2614..e0aec2523 100644
@@ -24,6 +24,98 @@
 #include <grub/net/netbuff.h>
 #include <grub/net/udp.h>
 #include <grub/datetime.h>
+#include <grub/time.h>
+#include <grub/list.h>
+
+static int
+dissect_url (const char *url, char **proto, char **host, char **path)
+{
+  const char *p, *ps;
+  grub_size_t l;
+
+  *proto = *host = *path = NULL;
+  ps = p = url;
+
+  while ((p = grub_strchr (p, ':')))
+    {
+      if (grub_strlen (p) < sizeof ("://") - 1)
+	break;
+      if (grub_memcmp (p, "://", sizeof ("://") - 1) == 0)
+	{
+	  l = p - ps;
+	  *proto = grub_malloc (l + 1);
+	  if (!*proto)
+	    {
+	      grub_print_error ();
+	      return 0;
+	    }
+
+	  grub_memcpy (*proto, ps, l);
+	  (*proto)[l] = '\0';
+	  p +=  sizeof ("://") - 1;
+	  break;
+	}
+      ++p;
+    }
+
+  if (!*proto)
+    {
+      grub_dprintf ("bootp", "url: %s is not valid, protocol not found\n", url);
+      return 0;
+    }
+
+  ps = p;
+  p = grub_strchr (p, '/');
+
+  if (!p)
+    {
+      grub_dprintf ("bootp", "url: %s is not valid, host/path not found\n", url);
+      grub_free (*proto);
+      *proto = NULL;
+      return 0;
+    }
+
+  l = p - ps;
+
+  if (l > 2 && ps[0] == '[' && ps[l - 1] == ']')
+    {
+      *host = grub_malloc (l - 1);
+      if (!*host)
+	{
+	  grub_print_error ();
+	  grub_free (*proto);
+	  *proto = NULL;
+	  return 0;
+	}
+      grub_memcpy (*host, ps + 1, l - 2);
+      (*host)[l - 2] = 0;
+    }
+  else
+    {
+      *host = grub_malloc (l + 1);
+      if (!*host)
+	{
+	  grub_print_error ();
+	  grub_free (*proto);
+	  *proto = NULL;
+	  return 0;
+	}
+      grub_memcpy (*host, ps, l);
+      (*host)[l] = 0;
+    }
+
+  *path = grub_strdup (p);
+  if (!*path)
+    {
+      grub_print_error ();
+      grub_free (*host);
+      grub_free (*proto);
+      *host = NULL;
+      *proto = NULL;
+      return 0;
+    }
+  return 1;
+}
 
 struct grub_dhcp_discover_options
 {
@@ -610,6 +702,584 @@ out:
   return err;
 }
 
+/* The default netbuff size for sending DHCPv6 packets which should be
+   large enough to hold the information */
+#define GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE 512
+
+struct grub_dhcp6_options
+{
+  grub_uint8_t *client_duid;
+  grub_uint16_t client_duid_len;
+  grub_uint8_t *server_duid;
+  grub_uint16_t server_duid_len;
+  grub_uint32_t iaid;
+  grub_uint32_t t1;
+  grub_uint32_t t2;
+  grub_net_network_level_address_t *ia_addr;
+  grub_uint32_t preferred_lifetime;
+  grub_uint32_t valid_lifetime;
+  grub_net_network_level_address_t *dns_server_addrs;
+  grub_uint16_t num_dns_server;
+  char *boot_file_proto;
+  char *boot_file_server_ip;
+  char *boot_file_path;
+};
+
+typedef struct grub_dhcp6_options *grub_dhcp6_options_t;
+
+struct grub_dhcp6_session
+{
+  struct grub_dhcp6_session *next;
+  struct grub_dhcp6_session **prev;
+  grub_uint32_t iaid;
+  grub_uint32_t transaction_id:24;
+  grub_uint64_t start_time;
+  struct grub_net_dhcp6_option_duid_ll duid;
+  struct grub_net_network_level_interface *iface;
+
+  /* The associated dhcpv6 options */
+  grub_dhcp6_options_t adv;
+  grub_dhcp6_options_t reply;
+};
+
+typedef struct grub_dhcp6_session *grub_dhcp6_session_t;
+
+typedef void (*dhcp6_option_hook_fn) (const struct grub_net_dhcp6_option *opt, void *data);
+
+static void
+foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt, grub_size_t size,
+		      dhcp6_option_hook_fn hook, void *hook_data);
+
+static void
+parse_dhcp6_iaaddr (const struct grub_net_dhcp6_option *opt, void *data)
+{
+  grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t )data;
+
+  grub_uint16_t code = grub_be_to_cpu16 (opt->code);
+  grub_uint16_t len = grub_be_to_cpu16 (opt->len);
+
+  if (code == GRUB_NET_DHCP6_OPTION_IAADDR)
+    {
+      const struct grub_net_dhcp6_option_iaaddr *iaaddr;
+      iaaddr = (const struct grub_net_dhcp6_option_iaaddr *)opt->data;
+
+      if (len < sizeof (*iaaddr))
+	{
+	  grub_dprintf ("bootp", "DHCPv6: code %u with insufficient length %u\n", code, len);
+	  return;
+	}
+      if (!dhcp6->ia_addr)
+	{
+	  dhcp6->ia_addr = grub_malloc (sizeof(*dhcp6->ia_addr));
+	  dhcp6->ia_addr->type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+	  dhcp6->ia_addr->ipv6[0] = grub_get_unaligned64 (iaaddr->addr);
+	  dhcp6->ia_addr->ipv6[1] = grub_get_unaligned64 (iaaddr->addr + 8);
+	  dhcp6->preferred_lifetime = grub_be_to_cpu32 (iaaddr->preferred_lifetime);
+	  dhcp6->valid_lifetime = grub_be_to_cpu32 (iaaddr->valid_lifetime);
+	}
+    }
+}
+
+static void
+parse_dhcp6_option (const struct grub_net_dhcp6_option *opt, void *data)
+{
+  grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t)data;
+  grub_uint16_t code = grub_be_to_cpu16 (opt->code);
+  grub_uint16_t len = grub_be_to_cpu16 (opt->len);
+
+  switch (code)
+    {
+      case GRUB_NET_DHCP6_OPTION_CLIENTID:
+
+	if (dhcp6->client_duid || !len)
+	  {
+	    grub_dprintf ("bootp", "Skipped DHCPv6 CLIENTID with length %u\n", len);
+	    break;
+	  }
+	dhcp6->client_duid = grub_malloc (len);
+	grub_memcpy (dhcp6->client_duid, opt->data, len);
+	dhcp6->client_duid_len = len;
+	break;
+
+      case GRUB_NET_DHCP6_OPTION_SERVERID:
+
+	if (dhcp6->server_duid || !len)
+	  {
+	    grub_dprintf ("bootp", "Skipped DHCPv6 SERVERID with length %u\n", len);
+	    break;
+	  }
+	dhcp6->server_duid = grub_malloc (len);
+	grub_memcpy (dhcp6->server_duid, opt->data, len);
+	dhcp6->server_duid_len = len;
+	break;
+
+      case GRUB_NET_DHCP6_OPTION_IA_NA:
+	{
+	  const struct grub_net_dhcp6_option_iana *ia_na;
+	  grub_uint16_t data_len;
+
+	  if (dhcp6->iaid || len < sizeof (*ia_na))
+	    {
+	      grub_dprintf ("bootp", "Skipped DHCPv6 IA_NA with length %u\n", len);
+	      break;
+	    }
+	  ia_na = (const struct grub_net_dhcp6_option_iana *)opt->data;
+	  dhcp6->iaid = grub_be_to_cpu32 (ia_na->iaid);
+	  dhcp6->t1 = grub_be_to_cpu32 (ia_na->t1);
+	  dhcp6->t2 = grub_be_to_cpu32 (ia_na->t2);
+
+	  data_len = len - sizeof (*ia_na);
+	  if (data_len)
+	    foreach_dhcp6_option ((const struct grub_net_dhcp6_option *)ia_na->data, data_len, parse_dhcp6_iaaddr, dhcp6);
+	}
+	break;
+
+      case GRUB_NET_DHCP6_OPTION_DNS_SERVERS:
+	{
+	  const grub_uint8_t *po;
+	  grub_uint16_t ln;
+	  grub_net_network_level_address_t *la;
+
+	  if (!len || len & 0xf)
+	    {
+	      grub_dprintf ("bootp", "Skip invalid length DHCPv6 DNS_SERVERS \n");
+	      break;
+	    }
+	  dhcp6->num_dns_server = ln = len >> 4;
+	  dhcp6->dns_server_addrs = la = grub_zalloc (ln * sizeof (*la));
+
+	  for (po = opt->data; ln > 0; po += 0x10, la++, ln--)
+	    {
+	      la->type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+	      la->ipv6[0] = grub_get_unaligned64 (po);
+	      la->ipv6[1] = grub_get_unaligned64 (po + 8);
+	      la->option = DNS_OPTION_PREFER_IPV6;
+	    }
+	}
+	break;
+
+      case GRUB_NET_DHCP6_OPTION_BOOTFILE_URL:
+	dissect_url ((const char *)opt->data,
+		      &dhcp6->boot_file_proto,
+		      &dhcp6->boot_file_server_ip,
+		      &dhcp6->boot_file_path);
+	break;
+
+      default:
+	break;
+    }
+}
+
+static void
+foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt, grub_size_t size, dhcp6_option_hook_fn hook, void *hook_data)
+{
+  while (size)
+    {
+      grub_uint16_t code, len;
+
+      if (size < sizeof (*opt))
+	{
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped with remaining size %" PRIxGRUB_SIZE "\n", size);
+	  break;
+	}
+      size -= sizeof (*opt);
+      len = grub_be_to_cpu16 (opt->len);
+      code = grub_be_to_cpu16 (opt->code);
+      if (size < len)
+	{
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped at out of bound length %u for option %u\n", len, code);
+	  break;
+	}
+      if (!len)
+	{
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped at zero length option %u\n", code);
+	  break;
+	}
+      else
+	{
+	  if (hook)
+	    hook (opt, hook_data);
+	  size -= len;
+	  opt = (const struct grub_net_dhcp6_option *)((grub_uint8_t *)opt + len + sizeof (*opt));
+	}
+    }
+}
+
+static grub_dhcp6_options_t
+grub_dhcp6_options_get (const struct grub_net_dhcp6_packet *v6h,
+			grub_size_t size)
+{
+  grub_dhcp6_options_t options;
+
+  if (size < sizeof (*v6h))
+    {
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("DHCPv6 packet size too small"));
+      return NULL;
+    }
+
+  options = grub_zalloc (sizeof(*options));
+  if (!options)
+    return NULL;
+
+  foreach_dhcp6_option ((const struct grub_net_dhcp6_option *)v6h->dhcp_options,
+		       size - sizeof (*v6h), parse_dhcp6_option, options);
+
+  return options;
+}
+
+static void
+grub_dhcp6_options_free (grub_dhcp6_options_t options)
+{
+  if (options->client_duid)
+    grub_free (options->client_duid);
+  if (options->server_duid)
+    grub_free (options->server_duid);
+  if (options->ia_addr)
+    grub_free (options->ia_addr);
+  if (options->dns_server_addrs)
+    grub_free (options->dns_server_addrs);
+  if (options->boot_file_proto)
+    grub_free (options->boot_file_proto);
+  if (options->boot_file_server_ip)
+    grub_free (options->boot_file_server_ip);
+  if (options->boot_file_path)
+    grub_free (options->boot_file_path);
+
+  grub_free (options);
+}
+
+static grub_dhcp6_session_t grub_dhcp6_sessions;
+#define FOR_DHCP6_SESSIONS_SAFE(var, next) FOR_LIST_ELEMENTS_SAFE (var, next, grub_dhcp6_sessions)
+#define FOR_DHCP6_SESSIONS(var) FOR_LIST_ELEMENTS (var, grub_dhcp6_sessions)
+
+static void
+grub_net_configure_by_dhcp6_info (const char *name,
+	  struct grub_net_card *card,
+	  grub_dhcp6_options_t dhcp6,
+	  int is_def,
+	  int flags,
+	  struct grub_net_network_level_interface **ret_inf)
+{
+  grub_net_network_level_netaddress_t netaddr;
+  struct grub_net_network_level_interface *inf;
+
+  if (dhcp6->ia_addr)
+    {
+      inf = grub_net_add_addr (name, card, dhcp6->ia_addr, &card->default_address, flags);
+
+      netaddr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+      netaddr.ipv6.base[0] = dhcp6->ia_addr->ipv6[0];
+      netaddr.ipv6.base[1] = 0;
+      netaddr.ipv6.masksize = 64;
+      grub_net_add_route (name, netaddr, inf);
+
+      if (ret_inf)
+	*ret_inf = inf;
+    }
+
+  if (dhcp6->dns_server_addrs)
+    {
+      grub_uint16_t i;
+
+      for (i = 0; i < dhcp6->num_dns_server; ++i)
+	grub_net_add_dns_server (dhcp6->dns_server_addrs + i);
+    }
+
+  if (dhcp6->boot_file_path)
+    grub_env_set_net_property (name, "boot_file", dhcp6->boot_file_path,
+			  grub_strlen (dhcp6->boot_file_path));
+
+  if (is_def && dhcp6->boot_file_server_ip)
+    {
+      grub_net_default_server = grub_strdup (dhcp6->boot_file_server_ip);
+      grub_env_set ("net_default_interface", name);
+      grub_env_export ("net_default_interface");
+    }
+}
+
+static void
+grub_dhcp6_session_add (struct grub_net_network_level_interface *iface,
+			grub_uint32_t iaid)
+{
+  grub_dhcp6_session_t se;
+  struct grub_datetime date;
+  grub_err_t err;
+  grub_int64_t t = 0;
+
+  se = grub_malloc (sizeof (*se));
+
+  err = grub_get_datetime (&date);
+  if (err || !grub_datetime2unixtime (&date, &t))
+    {
+      grub_errno = GRUB_ERR_NONE;
+      t = 0;
+    }
+
+  se->iface = iface;
+  se->iaid = iaid;
+  se->transaction_id = t;
+  se->start_time = grub_get_time_ms ();
+  se->duid.type = grub_cpu_to_be16_compile_time (3) ;
+  se->duid.hw_type = grub_cpu_to_be16_compile_time (1);
+  grub_memcpy (&se->duid.hwaddr, &iface->hwaddress.mac, sizeof (se->duid.hwaddr));
+  se->adv = NULL;
+  se->reply = NULL;
+  grub_list_push (GRUB_AS_LIST_P (&grub_dhcp6_sessions), GRUB_AS_LIST (se));
+}
+
+static void
+grub_dhcp6_session_remove (grub_dhcp6_session_t se)
+{
+  grub_list_remove (GRUB_AS_LIST (se));
+  if (se->adv)
+    grub_dhcp6_options_free (se->adv);
+  if (se->reply)
+    grub_dhcp6_options_free (se->reply);
+  grub_free (se);
+}
+
+static void
+grub_dhcp6_session_remove_all (void)
+{
+  grub_dhcp6_session_t se, next;
+
+  FOR_DHCP6_SESSIONS_SAFE (se, next)
+    {
+      grub_dhcp6_session_remove (se);
+    }
+  grub_dhcp6_sessions = NULL;
+}
+
+static grub_err_t
+grub_dhcp6_session_configure_network (grub_dhcp6_session_t se)
+{
+  char *name;
+
+  name = grub_xasprintf ("%s:dhcp6", se->iface->card->name);
+  if (!name)
+    return grub_errno;
+
+  grub_net_configure_by_dhcp6_info (name, se->iface->card, se->reply, 1, 0, 0);
+  grub_free (name);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_dhcp6_session_send_request (grub_dhcp6_session_t se)
+{
+  struct grub_net_buff *nb;
+  struct grub_net_dhcp6_option *opt;
+  struct grub_net_dhcp6_packet *v6h;
+  struct grub_net_dhcp6_option_iana *ia_na;
+  struct grub_net_dhcp6_option_iaaddr *iaaddr;
+  struct udphdr *udph;
+  grub_net_network_level_address_t multicast;
+  grub_net_link_level_address_t ll_multicast;
+  grub_uint64_t elapsed;
+  struct grub_net_network_level_interface *inf = se->iface;
+  grub_dhcp6_options_t dhcp6 = se->adv;
+  grub_err_t err = GRUB_ERR_NONE;
+
+  multicast.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+  multicast.ipv6[0] = grub_cpu_to_be64_compile_time (0xff02ULL << 48);
+  multicast.ipv6[1] = grub_cpu_to_be64_compile_time (0x10002ULL);
+
+  err = grub_net_link_layer_resolve (inf, &multicast, &ll_multicast);
+  if (err)
+    return err;
+
+  nb = grub_netbuff_alloc (GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
+
+  if (!nb)
+    return grub_errno;
+
+  err = grub_netbuff_reserve (nb, GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  err = grub_netbuff_push (nb, dhcp6->client_duid_len + sizeof (*opt));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+  opt = (struct grub_net_dhcp6_option *)nb->data;
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_CLIENTID);
+  opt->len = grub_cpu_to_be16 (dhcp6->client_duid_len);
+  grub_memcpy (opt->data, dhcp6->client_duid , dhcp6->client_duid_len);
+
+  err = grub_netbuff_push (nb, dhcp6->server_duid_len + sizeof (*opt));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+  opt = (struct grub_net_dhcp6_option *)nb->data;
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_SERVERID);
+  opt->len = grub_cpu_to_be16 (dhcp6->server_duid_len);
+  grub_memcpy (opt->data, dhcp6->server_duid , dhcp6->server_duid_len);
+
+  err = grub_netbuff_push (nb, sizeof (*ia_na) + sizeof (*opt));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  if (dhcp6->ia_addr)
+    {
+      err = grub_netbuff_push (nb, sizeof(*iaaddr) + sizeof (*opt));
+      if (err)
+	{
+	  grub_netbuff_free (nb);
+	  return err;
+	}
+    }
+  opt = (struct grub_net_dhcp6_option *)nb->data;
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA);
+  opt->len = grub_cpu_to_be16 (sizeof (*ia_na));
+  if (dhcp6->ia_addr)
+    opt->len += grub_cpu_to_be16 (sizeof(*iaaddr) + sizeof (*opt));
+
+  ia_na = (struct grub_net_dhcp6_option_iana *)opt->data;
+  ia_na->iaid = grub_cpu_to_be32 (dhcp6->iaid);
+
+  ia_na->t1 = grub_cpu_to_be32 (dhcp6->t1);
+  ia_na->t2 = grub_cpu_to_be32 (dhcp6->t2);
+
+  if (dhcp6->ia_addr)
+    {
+      opt = (struct grub_net_dhcp6_option *)ia_na->data;
+      opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IAADDR);
+      opt->len = grub_cpu_to_be16 (sizeof (*iaaddr));
+      iaaddr = (struct grub_net_dhcp6_option_iaaddr *)opt->data;
+      grub_set_unaligned64 (iaaddr->addr, dhcp6->ia_addr->ipv6[0]);
+      grub_set_unaligned64 (iaaddr->addr + 8, dhcp6->ia_addr->ipv6[1]);
+
+      iaaddr->preferred_lifetime = grub_cpu_to_be32 (dhcp6->preferred_lifetime);
+      iaaddr->valid_lifetime = grub_cpu_to_be32 (dhcp6->valid_lifetime);
+    }
+
+  err = grub_netbuff_push (nb, sizeof (*opt) + 2 * sizeof (grub_uint16_t));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  opt = (struct grub_net_dhcp6_option*) nb->data;
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ORO);
+  opt->len = grub_cpu_to_be16_compile_time (2 * sizeof (grub_uint16_t));
+  grub_set_unaligned16 (opt->data, grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_BOOTFILE_URL));
+  grub_set_unaligned16 (opt->data + 2, grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_DNS_SERVERS));
+
+  err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (grub_uint16_t));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+  opt = (struct grub_net_dhcp6_option*) nb->data;
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ELAPSED_TIME);
+  opt->len = grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t));
+
+  /* the time is expressed in hundredths of a second */
+  elapsed = grub_divmod64 (grub_get_time_ms () - se->start_time, 10, 0);
+
+  if (elapsed > 0xffff)
+    elapsed = 0xffff;
+
+  grub_set_unaligned16 (opt->data,  grub_cpu_to_be16 ((grub_uint16_t)elapsed));
+
+  err = grub_netbuff_push (nb, sizeof (*v6h));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  v6h = (struct grub_net_dhcp6_packet *) nb->data;
+  v6h->message_type = GRUB_NET_DHCP6_REQUEST;
+  v6h->transaction_id = se->transaction_id;
+
+  err = grub_netbuff_push (nb, sizeof (*udph));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  udph = (struct udphdr *) nb->data;
+  udph->src = grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT);
+  udph->dst = grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT);
+  udph->chksum = 0;
+  udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
+
+  udph->chksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
+						 &inf->address,
+						 &multicast);
+  err = grub_net_send_ip_packet (inf, &multicast, &ll_multicast, nb,
+				 GRUB_NET_IP_UDP);
+
+  grub_netbuff_free (nb);
+
+  return err;
+}
+
+struct grub_net_network_level_interface *
+grub_net_configure_by_dhcpv6_reply (const char *name,
+	struct grub_net_card *card,
+	grub_net_interface_flags_t flags,
+	const struct grub_net_dhcp6_packet *v6h,
+	grub_size_t size,
+	int is_def,
+	char **device, char **path)
+{
+  struct grub_net_network_level_interface *inf;
+  grub_dhcp6_options_t dhcp6;
+  int mask = -1;
+
+  dhcp6 = grub_dhcp6_options_get (v6h, size);
+  if (!dhcp6)
+    {
+      grub_print_error ();
+      return NULL;
+    }
+
+  grub_net_configure_by_dhcp6_info (name, card, dhcp6, is_def, flags, &inf);
+
+  if (device && dhcp6->boot_file_proto && dhcp6->boot_file_server_ip)
+    {
+      *device = grub_xasprintf ("%s,%s", dhcp6->boot_file_proto, dhcp6->boot_file_server_ip);
+      grub_print_error ();
+    }
+  if (path && dhcp6->boot_file_path)
+    {
+      *path = grub_strdup (dhcp6->boot_file_path);
+      grub_print_error ();
+      if (*path)
+	{
+	  char *slash;
+	  slash = grub_strrchr (*path, '/');
+	  if (slash)
+	    *slash = 0;
+	  else
+	    **path = 0;
+	}
+    }
+
+  grub_dhcp6_options_free (dhcp6);
+
+  if (inf)
+    grub_net_add_ipv6_local (inf, mask);
+
+  return inf;
+}
+
 /*
  * This is called directly from net/ip.c:handle_dgram(), because those
  * BOOTP/DHCP packets are a bit special due to their improper
@@ -678,6 +1348,77 @@ grub_net_process_dhcp (struct grub_net_buff *nb,
     }
 }
 
+grub_err_t
+grub_net_process_dhcp6 (struct grub_net_buff *nb,
+                       struct grub_net_card *card __attribute__ ((unused)))
+{
+  const struct grub_net_dhcp6_packet *v6h;
+  grub_dhcp6_session_t se;
+  grub_size_t size;
+  grub_dhcp6_options_t options;
+
+  v6h = (const struct grub_net_dhcp6_packet *) nb->data;
+  size = nb->tail - nb->data;
+
+  options = grub_dhcp6_options_get (v6h, size);
+  if (!options)
+    return grub_errno;
+
+  if (!options->client_duid || !options->server_duid || !options->ia_addr)
+    {
+      grub_dhcp6_options_free (options);
+      return grub_error (GRUB_ERR_BAD_ARGUMENT, "Bad DHCPv6 Packet");
+    }
+
+  FOR_DHCP6_SESSIONS (se)
+    {
+      if (se->transaction_id == v6h->transaction_id &&
+         grub_memcmp (options->client_duid, &se->duid, sizeof (se->duid)) == 0 &&
+         se->iaid == options->iaid)
+       break;
+    }
+
+  if (!se)
+    {
+      grub_dprintf ("bootp", "DHCPv6 session not found\n");
+      grub_dhcp6_options_free (options);
+      return GRUB_ERR_NONE;
+    }
+
+  if (v6h->message_type == GRUB_NET_DHCP6_ADVERTISE)
+    {
+      if (se->adv)
+       {
+         grub_dprintf ("bootp", "Skipped DHCPv6 Advertised .. \n");
+         grub_dhcp6_options_free (options);
+         return GRUB_ERR_NONE;
+       }
+
+      se->adv = options;
+      return grub_dhcp6_session_send_request (se);
+    }
+  else if (v6h->message_type == GRUB_NET_DHCP6_REPLY)
+    {
+      if (!se->adv)
+       {
+         grub_dprintf ("bootp", "Skipped DHCPv6 Reply .. \n");
+         grub_dhcp6_options_free (options);
+         return GRUB_ERR_NONE;
+       }
+
+      se->reply = options;
+      grub_dhcp6_session_configure_network (se);
+      grub_dhcp6_session_remove (se);
+      return GRUB_ERR_NONE;
+    }
+  else
+    {
+      grub_dhcp6_options_free (options);
+    }
+
+  return GRUB_ERR_NONE;
+}
+
 static grub_err_t
 grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)),
 		  int argc, char **args)
@@ -903,180 +1644,174 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
   return err;
 }
 
-static grub_command_t cmd_getdhcp, cmd_bootp, cmd_dhcp;
-
-struct grub_net_network_level_interface *
-grub_net_configure_by_dhcpv6_ack (const char *name,
-				  struct grub_net_card *card,
-				  grub_net_interface_flags_t flags
-				    __attribute__((__unused__)),
-				  const grub_net_link_level_address_t *hwaddr,
-				  const struct grub_net_dhcpv6_packet *packet,
-				  int is_def, char **device, char **path)
+static grub_err_t
+grub_cmd_bootp6 (struct grub_command *cmd __attribute__ ((unused)),
+                 int argc, char **args)
 {
-  struct grub_net_network_level_interface *inter = NULL;
-  struct grub_net_network_level_address addr;
-  int mask = -1;
-
-  if (!device || !path)
-    return NULL;
-
-  *device = 0;
-  *path = 0;
-
-  grub_dprintf ("net", "mac address is %02x:%02x:%02x:%02x:%02x:%02x\n",
-		hwaddr->mac[0], hwaddr->mac[1], hwaddr->mac[2],
-		hwaddr->mac[3], hwaddr->mac[4], hwaddr->mac[5]);
-
-  if (is_def)
-    grub_net_default_server = 0;
-
-  if (is_def && !grub_net_default_server && packet)
-    {
-      const grub_uint8_t *options = packet->dhcp_options;
-      unsigned int option_max = 1024 - OFFSET_OF (dhcp_options, packet);
-      unsigned int i;
-
-      for (i = 0; i < option_max - sizeof (grub_net_dhcpv6_option_t); )
-	{
-	  grub_uint16_t num, len;
-	  grub_net_dhcpv6_option_t *opt =
-	    (grub_net_dhcpv6_option_t *)(options + i);
-
-	  num = grub_be_to_cpu16(opt->option_num);
-	  len = grub_be_to_cpu16(opt->option_len);
+  struct grub_net_card *card;
+  grub_uint32_t iaid = 0;
+  int interval;
+  grub_err_t err;
+  grub_dhcp6_session_t se;
 
-	  grub_dprintf ("net", "got dhcpv6 option %d len %d\n", num, len);
+  err = GRUB_ERR_NONE;
 
-	  if (len == 0)
-	    break;
+  FOR_NET_CARDS (card)
+  {
+    struct grub_net_network_level_interface *iface;
 
-	  if (len + i > 1024)
-	    break;
+    if (argc > 0 && grub_strcmp (card->name, args[0]) != 0)
+      continue;
 
-	  if (num == GRUB_NET_DHCP6_BOOTFILE_URL)
-	    {
-	      char *scheme, *userinfo, *host, *file;
-	      char *tmp;
-	      int hostlen;
-	      int port;
-	      int rc = extract_url_info ((const char *)opt->option_data,
-					 (grub_size_t)len,
-					 &scheme, &userinfo, &host, &port,
-					 &file);
-	      if (rc < 0)
-		continue;
-
-	      /* right now this only handles tftp. */
-	      if (grub_strcmp("tftp", scheme))
-		{
-		  grub_free (scheme);
-		  grub_free (userinfo);
-		  grub_free (host);
-		  grub_free (file);
-		  continue;
-		}
-	      grub_free (userinfo);
-
-	      hostlen = grub_strlen (host);
-	      if (hostlen > 2 && host[0] == '[' && host[hostlen-1] == ']')
-		{
-		  tmp = host+1;
-		  host[hostlen-1] = '\0';
-		}
-	      else
-		tmp = host;
-
-	      *device = grub_xasprintf ("%s,%s", scheme, tmp);
-	      grub_free (scheme);
-	      grub_free (host);
-
-	      if (file && *file)
-		{
-		  tmp = grub_strrchr (file, '/');
-		  if (tmp)
-		    *(tmp+1) = '\0';
-		  else
-		    file[0] = '\0';
-		}
-	      else if (!file)
-		file = grub_strdup ("");
-
-	      if (file[0] == '/')
-		{
-		  *path = grub_strdup (file+1);
-		  grub_free (file);
-		}
-	      else
-		*path = file;
-	    }
-	  else if (num == GRUB_NET_DHCP6_IA_NA)
-	    {
-	      const grub_net_dhcpv6_option_t *ia_na_opt;
-	      const grub_net_dhcpv6_opt_ia_na_t *ia_na =
-		(const grub_net_dhcpv6_opt_ia_na_t *)opt;
-	      unsigned int left = len - OFFSET_OF (options, ia_na);
-	      unsigned int j;
-
-	      if ((grub_uint8_t *)ia_na + left >
-		  (grub_uint8_t *)options + option_max)
-		left -= ((grub_uint8_t *)ia_na + left)
-		        - ((grub_uint8_t *)options + option_max);
-
-	      if (len < OFFSET_OF (option_data, opt)
-			+ sizeof (grub_net_dhcpv6_option_t))
-		{
-		  grub_dprintf ("net",
-				"found dhcpv6 ia_na option with no address\n");
-		  continue;
-		}
-
-	      for (j = 0; left > sizeof (grub_net_dhcpv6_option_t); )
-		{
-		  ia_na_opt = (const grub_net_dhcpv6_option_t *)
-			       (ia_na->options + j);
-		  grub_uint16_t ia_na_opt_num, ia_na_opt_len;
-
-		  ia_na_opt_num = grub_be_to_cpu16 (ia_na_opt->option_num);
-		  ia_na_opt_len = grub_be_to_cpu16 (ia_na_opt->option_len);
-		  if (ia_na_opt_len == 0)
-		    break;
-		  if (j + ia_na_opt_len > left)
-		    break;
-		  if (ia_na_opt_num == GRUB_NET_DHCP6_IA_ADDRESS)
-		    {
-		      const grub_net_dhcpv6_opt_ia_address_t *ia_addr;
-
-		      ia_addr = (const grub_net_dhcpv6_opt_ia_address_t *)
-				 ia_na_opt;
-		      addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
-		      grub_memcpy(addr.ipv6, ia_addr->ipv6_address,
-				  sizeof (ia_addr->ipv6_address));
-		      inter = grub_net_add_addr (name, card, &addr, hwaddr, 0);
-		    }
-
-		  j += ia_na_opt_len;
-		  left -= ia_na_opt_len;
-		}
-	    }
+    iface = grub_net_ipv6_get_link_local (card, &card->default_address);
+    if (!iface)
+      {
+       grub_dhcp6_session_remove_all ();
+       return grub_errno;
+      }
 
-	  i += len + 4;
-	}
+    grub_dhcp6_session_add (iface, iaid++);
+  }
 
-      grub_print_error ();
+  for (interval = 200; interval < 10000; interval *= 2)
+    {
+      int done = 1;
+
+      FOR_DHCP6_SESSIONS (se)
+       {
+         struct grub_net_buff *nb;
+         struct grub_net_dhcp6_option *opt;
+         struct grub_net_dhcp6_packet *v6h;
+         struct grub_net_dhcp6_option_duid_ll *duid;
+         struct grub_net_dhcp6_option_iana *ia_na;
+         grub_net_network_level_address_t multicast;
+         grub_net_link_level_address_t ll_multicast;
+         struct udphdr *udph;
+
+         multicast.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+         multicast.ipv6[0] = grub_cpu_to_be64_compile_time (0xff02ULL << 48);
+         multicast.ipv6[1] = grub_cpu_to_be64_compile_time (0x10002ULL);
+
+         err = grub_net_link_layer_resolve (se->iface,
+                   &multicast, &ll_multicast);
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             return err;
+           }
+
+         nb = grub_netbuff_alloc (GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
+
+         if (!nb)
+           {
+             grub_dhcp6_session_remove_all ();
+             return grub_errno;
+           }
+
+         err = grub_netbuff_reserve (nb, GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (grub_uint16_t));
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         opt = (struct grub_net_dhcp6_option *)nb->data;
+         opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ELAPSED_TIME);
+         opt->len = grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t));
+         grub_set_unaligned16 (opt->data, 0);
+
+         err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (*duid));
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         opt = (struct grub_net_dhcp6_option *)nb->data;
+         opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_CLIENTID);
+         opt->len = grub_cpu_to_be16 (sizeof (*duid));
+
+         duid = (struct grub_net_dhcp6_option_duid_ll *) opt->data;
+         grub_memcpy (duid, &se->duid, sizeof (*duid));
+
+         err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (*ia_na));
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         opt = (struct grub_net_dhcp6_option *)nb->data;
+         opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA);
+         opt->len = grub_cpu_to_be16 (sizeof (*ia_na));
+         ia_na = (struct grub_net_dhcp6_option_iana *)opt->data;
+         ia_na->iaid = grub_cpu_to_be32 (se->iaid);
+         ia_na->t1 = 0;
+         ia_na->t2 = 0;
+
+         err = grub_netbuff_push (nb, sizeof (*v6h));
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         v6h = (struct grub_net_dhcp6_packet *)nb->data;
+         v6h->message_type = GRUB_NET_DHCP6_SOLICIT;
+         v6h->transaction_id = se->transaction_id;
+
+         grub_netbuff_push (nb, sizeof (*udph));
+
+         udph = (struct udphdr *) nb->data;
+         udph->src = grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT);
+         udph->dst = grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT);
+         udph->chksum = 0;
+         udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
+
+         udph->chksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
+                           &se->iface->address, &multicast);
+
+         err = grub_net_send_ip_packet (se->iface, &multicast,
+                   &ll_multicast, nb, GRUB_NET_IP_UDP);
+         done = 0;
+         grub_netbuff_free (nb);
+
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             return err;
+           }
+       }
+      if (!done)
+       grub_net_poll_cards (interval, 0);
     }
 
-  if (is_def)
+  FOR_DHCP6_SESSIONS (se)
     {
-      grub_env_set ("net_default_interface", name);
-      grub_env_export ("net_default_interface");
+      grub_error_push ();
+      err = grub_error (GRUB_ERR_FILE_NOT_FOUND,
+                       N_("couldn't autoconfigure %s"),
+                       se->iface->card->name);
     }
 
-    if (inter)
-      grub_net_add_ipv6_local (inter, mask);
-    return inter;
+  grub_dhcp6_session_remove_all ();
+
+  return err;
 }
 
+static grub_command_t cmd_getdhcp, cmd_bootp, cmd_dhcp, cmd_bootp6;
 
 void
 grub_bootp_init (void)
@@ -1090,11 +1825,15 @@ grub_bootp_init (void)
   cmd_getdhcp = grub_register_command ("net_get_dhcp_option", grub_cmd_dhcpopt,
 				       N_("VAR INTERFACE NUMBER DESCRIPTION"),
 				       N_("retrieve DHCP option and save it into VAR. If VAR is - then print the value."));
+  cmd_bootp6 = grub_register_command ("net_bootp6", grub_cmd_bootp6,
+				     N_("[CARD]"),
+				     N_("perform a DHCPv6 autoconfiguration"));
 }
 
 void
 grub_bootp_fini (void)
 {
+  grub_unregister_command (cmd_bootp6);
   grub_unregister_command (cmd_getdhcp);
   grub_unregister_command (cmd_bootp);
   grub_unregister_command (cmd_dhcp);
grub-core/net/drivers/efi/efinet.c +7 -11
index 2af8742f9..3e2432fb6 100644
@@ -410,9 +410,6 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
     pxe_mode = pxe->mode;
     if (pxe_mode->using_ipv6)
       {
-	grub_net_link_level_address_t hwaddr;
-	struct grub_net_network_level_interface *intf;
-
 	grub_dprintf ("efinet", "using ipv6 and dhcpv6\n");
 	grub_dprintf ("efinet", "dhcp_ack_received: %s%s\n",
 		      pxe_mode->dhcp_ack_received ? "yes" : "no",
@@ -420,14 +417,13 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
 	if (!pxe_mode->dhcp_ack_received)
 	  continue;
 
-	hwaddr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
-	grub_memcpy (hwaddr.mac,
-		     card->efi_net->mode->current_address,
-		     sizeof (hwaddr.mac));
-
-	inter = grub_net_configure_by_dhcpv6_ack (card->name, card, 0, &hwaddr,
-	      (const struct grub_net_dhcpv6_packet *)&pxe_mode->dhcp_ack.dhcpv6,
-	      1, device, path);
+	inter = grub_net_configure_by_dhcpv6_reply (card->name, card, 0,
+						    (struct grub_net_dhcp6_packet *)
+						    &pxe_mode->dhcp_ack,
+						    sizeof (pxe_mode->dhcp_ack),
+						    1, device, path);
+	if (grub_errno)
+	  grub_print_error ();
 	if (inter && device && path)
 	  grub_dprintf ("efinet", "device: `%s' path: `%s'\n", *device, *path);
       }
grub-core/net/ip.c +39 -0
index 3c3d0be0e..f967618e5 100644
@@ -240,6 +240,45 @@ handle_dgram (struct grub_net_buff *nb,
   {
     struct udphdr *udph;
     udph = (struct udphdr *) nb->data;
+
+    if (proto == GRUB_NET_IP_UDP && udph->dst == grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT))
+      {
+	if (udph->chksum)
+	  {
+	    grub_uint16_t chk, expected;
+	    chk = udph->chksum;
+	    udph->chksum = 0;
+	    expected = grub_net_ip_transport_checksum (nb,
+						       GRUB_NET_IP_UDP,
+						       source,
+						       dest);
+	    if (expected != chk)
+	      {
+		grub_dprintf ("net", "Invalid UDP checksum. "
+			      "Expected %x, got %x\n",
+			      grub_be_to_cpu16 (expected),
+			      grub_be_to_cpu16 (chk));
+		grub_netbuff_free (nb);
+		return GRUB_ERR_NONE;
+	      }
+	    udph->chksum = chk;
+	  }
+
+	err = grub_netbuff_pull (nb, sizeof (*udph));
+	if (err)
+	  {
+	    grub_netbuff_free (nb);
+	    return err;
+	  }
+
+	err = grub_net_process_dhcp6 (nb, card);
+	if (err)
+	  grub_print_error ();
+
+	grub_netbuff_free (nb);
+	return GRUB_ERR_NONE;
+      }
+
     if (proto == GRUB_NET_IP_UDP && grub_be_to_cpu16 (udph->dst) == 68)
       {
 	const struct grub_net_bootp_packet *bootp;
include/grub/efi/api.h +1 -1
index c6d702831..c1b869db5 100644
@@ -1570,7 +1570,7 @@ typedef struct grub_efi_pxe_ip_filter
 {
   grub_efi_uint8_t filters;
   grub_efi_uint8_t ip_count;
-  grub_efi_uint8_t reserved;
+  grub_efi_uint16_t reserved;
   grub_efi_ip_address_t ip_list[GRUB_EFI_PXE_MAX_IPCNT];
 } grub_efi_pxe_ip_filter_t;
 
include/grub/net.h +55 -36

123 lines not on this page · raw message

Signature · 7 lines
2.46.2


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

Re: [PATCH v1 22/37] bootp: New net_bootp6 command

Quoted 4 lines · Leo Sandoval

From: Michael Chang <[email protected]>

Implement new net_bootp6 command for IPv6 network auto configuration via the DHCPv6 protocol (RFC3315).

This would have marked the fourth attempt to upstream the patch by different people.

https://lists.gnu.org/archive/html/grub-devel/2016-08/msg00000.html
https://lists.gnu.org/archive/html/grub-devel/2023-01/msg00012.html
https://www.mail-archive.com/[email protected]/msg30432.html

If everything is still desirable, I could help by reposting the series in a separate thread to ease the review, but I’m not really sure because it seems to me that upstream isn't interested in the feature.

Thanks,
Michael

Quoted 1,367 lines
[pjones: Put back our code to add a local route]
---
 grub-core/net/bootp.c              | 1059 +++++++++++++++++++++++-----
 grub-core/net/drivers/efi/efinet.c |   18 +-
 grub-core/net/ip.c                 |   39 +
 include/grub/efi/api.h             |    2 +-
 include/grub/net.h                 |   91 ++-
 5 files changed, 1001 insertions(+), 208 deletions(-)
grub-core/net/bootp.c +899 -160
index 02d2c2614..e0aec2523 100644
@@ -24,6 +24,98 @@
 #include <grub/net/netbuff.h>
 #include <grub/net/udp.h>
 #include <grub/datetime.h>
+#include <grub/time.h>
+#include <grub/list.h>
+
+static int
+dissect_url (const char *url, char **proto, char **host, char **path)
+{
+  const char *p, *ps;
+  grub_size_t l;
+
+  *proto = *host = *path = NULL;
+  ps = p = url;
+
+  while ((p = grub_strchr (p, ':')))
+    {
+      if (grub_strlen (p) < sizeof ("://") - 1)
+	break;
+      if (grub_memcmp (p, "://", sizeof ("://") - 1) == 0)
+	{
+	  l = p - ps;
+	  *proto = grub_malloc (l + 1);
+	  if (!*proto)
+	    {
+	      grub_print_error ();
+	      return 0;
+	    }
+
+	  grub_memcpy (*proto, ps, l);
+	  (*proto)[l] = '\0';
+	  p +=  sizeof ("://") - 1;
+	  break;
+	}
+      ++p;
+    }
+
+  if (!*proto)
+    {
+      grub_dprintf ("bootp", "url: %s is not valid, protocol not found\n", url);
+      return 0;
+    }
+
+  ps = p;
+  p = grub_strchr (p, '/');
+
+  if (!p)
+    {
+      grub_dprintf ("bootp", "url: %s is not valid, host/path not found\n", url);
+      grub_free (*proto);
+      *proto = NULL;
+      return 0;
+    }
+
+  l = p - ps;
+
+  if (l > 2 && ps[0] == '[' && ps[l - 1] == ']')
+    {
+      *host = grub_malloc (l - 1);
+      if (!*host)
+	{
+	  grub_print_error ();
+	  grub_free (*proto);
+	  *proto = NULL;
+	  return 0;
+	}
+      grub_memcpy (*host, ps + 1, l - 2);
+      (*host)[l - 2] = 0;
+    }
+  else
+    {
+      *host = grub_malloc (l + 1);
+      if (!*host)
+	{
+	  grub_print_error ();
+	  grub_free (*proto);
+	  *proto = NULL;
+	  return 0;
+	}
+      grub_memcpy (*host, ps, l);
+      (*host)[l] = 0;
+    }
+
+  *path = grub_strdup (p);
+  if (!*path)
+    {
+      grub_print_error ();
+      grub_free (*host);
+      grub_free (*proto);
+      *host = NULL;
+      *proto = NULL;
+      return 0;
+    }
+  return 1;
+}
 
 struct grub_dhcp_discover_options
 {
@@ -610,6 +702,584 @@ out:
   return err;
 }
 
+/* The default netbuff size for sending DHCPv6 packets which should be
+   large enough to hold the information */
+#define GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE 512
+
+struct grub_dhcp6_options
+{
+  grub_uint8_t *client_duid;
+  grub_uint16_t client_duid_len;
+  grub_uint8_t *server_duid;
+  grub_uint16_t server_duid_len;
+  grub_uint32_t iaid;
+  grub_uint32_t t1;
+  grub_uint32_t t2;
+  grub_net_network_level_address_t *ia_addr;
+  grub_uint32_t preferred_lifetime;
+  grub_uint32_t valid_lifetime;
+  grub_net_network_level_address_t *dns_server_addrs;
+  grub_uint16_t num_dns_server;
+  char *boot_file_proto;
+  char *boot_file_server_ip;
+  char *boot_file_path;
+};
+
+typedef struct grub_dhcp6_options *grub_dhcp6_options_t;
+
+struct grub_dhcp6_session
+{
+  struct grub_dhcp6_session *next;
+  struct grub_dhcp6_session **prev;
+  grub_uint32_t iaid;
+  grub_uint32_t transaction_id:24;
+  grub_uint64_t start_time;
+  struct grub_net_dhcp6_option_duid_ll duid;
+  struct grub_net_network_level_interface *iface;
+
+  /* The associated dhcpv6 options */
+  grub_dhcp6_options_t adv;
+  grub_dhcp6_options_t reply;
+};
+
+typedef struct grub_dhcp6_session *grub_dhcp6_session_t;
+
+typedef void (*dhcp6_option_hook_fn) (const struct grub_net_dhcp6_option *opt, void *data);
+
+static void
+foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt, grub_size_t size,
+		      dhcp6_option_hook_fn hook, void *hook_data);
+
+static void
+parse_dhcp6_iaaddr (const struct grub_net_dhcp6_option *opt, void *data)
+{
+  grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t )data;
+
+  grub_uint16_t code = grub_be_to_cpu16 (opt->code);
+  grub_uint16_t len = grub_be_to_cpu16 (opt->len);
+
+  if (code == GRUB_NET_DHCP6_OPTION_IAADDR)
+    {
+      const struct grub_net_dhcp6_option_iaaddr *iaaddr;
+      iaaddr = (const struct grub_net_dhcp6_option_iaaddr *)opt->data;
+
+      if (len < sizeof (*iaaddr))
+	{
+	  grub_dprintf ("bootp", "DHCPv6: code %u with insufficient length %u\n", code, len);
+	  return;
+	}
+      if (!dhcp6->ia_addr)
+	{
+	  dhcp6->ia_addr = grub_malloc (sizeof(*dhcp6->ia_addr));
+	  dhcp6->ia_addr->type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+	  dhcp6->ia_addr->ipv6[0] = grub_get_unaligned64 (iaaddr->addr);
+	  dhcp6->ia_addr->ipv6[1] = grub_get_unaligned64 (iaaddr->addr + 8);
+	  dhcp6->preferred_lifetime = grub_be_to_cpu32 (iaaddr->preferred_lifetime);
+	  dhcp6->valid_lifetime = grub_be_to_cpu32 (iaaddr->valid_lifetime);
+	}
+    }
+}
+
+static void
+parse_dhcp6_option (const struct grub_net_dhcp6_option *opt, void *data)
+{
+  grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t)data;
+  grub_uint16_t code = grub_be_to_cpu16 (opt->code);
+  grub_uint16_t len = grub_be_to_cpu16 (opt->len);
+
+  switch (code)
+    {
+      case GRUB_NET_DHCP6_OPTION_CLIENTID:
+
+	if (dhcp6->client_duid || !len)
+	  {
+	    grub_dprintf ("bootp", "Skipped DHCPv6 CLIENTID with length %u\n", len);
+	    break;
+	  }
+	dhcp6->client_duid = grub_malloc (len);
+	grub_memcpy (dhcp6->client_duid, opt->data, len);
+	dhcp6->client_duid_len = len;
+	break;
+
+      case GRUB_NET_DHCP6_OPTION_SERVERID:
+
+	if (dhcp6->server_duid || !len)
+	  {
+	    grub_dprintf ("bootp", "Skipped DHCPv6 SERVERID with length %u\n", len);
+	    break;
+	  }
+	dhcp6->server_duid = grub_malloc (len);
+	grub_memcpy (dhcp6->server_duid, opt->data, len);
+	dhcp6->server_duid_len = len;
+	break;
+
+      case GRUB_NET_DHCP6_OPTION_IA_NA:
+	{
+	  const struct grub_net_dhcp6_option_iana *ia_na;
+	  grub_uint16_t data_len;
+
+	  if (dhcp6->iaid || len < sizeof (*ia_na))
+	    {
+	      grub_dprintf ("bootp", "Skipped DHCPv6 IA_NA with length %u\n", len);
+	      break;
+	    }
+	  ia_na = (const struct grub_net_dhcp6_option_iana *)opt->data;
+	  dhcp6->iaid = grub_be_to_cpu32 (ia_na->iaid);
+	  dhcp6->t1 = grub_be_to_cpu32 (ia_na->t1);
+	  dhcp6->t2 = grub_be_to_cpu32 (ia_na->t2);
+
+	  data_len = len - sizeof (*ia_na);
+	  if (data_len)
+	    foreach_dhcp6_option ((const struct grub_net_dhcp6_option *)ia_na->data, data_len, parse_dhcp6_iaaddr, dhcp6);
+	}
+	break;
+
+      case GRUB_NET_DHCP6_OPTION_DNS_SERVERS:
+	{
+	  const grub_uint8_t *po;
+	  grub_uint16_t ln;
+	  grub_net_network_level_address_t *la;
+
+	  if (!len || len & 0xf)
+	    {
+	      grub_dprintf ("bootp", "Skip invalid length DHCPv6 DNS_SERVERS \n");
+	      break;
+	    }
+	  dhcp6->num_dns_server = ln = len >> 4;
+	  dhcp6->dns_server_addrs = la = grub_zalloc (ln * sizeof (*la));
+
+	  for (po = opt->data; ln > 0; po += 0x10, la++, ln--)
+	    {
+	      la->type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+	      la->ipv6[0] = grub_get_unaligned64 (po);
+	      la->ipv6[1] = grub_get_unaligned64 (po + 8);
+	      la->option = DNS_OPTION_PREFER_IPV6;
+	    }
+	}
+	break;
+
+      case GRUB_NET_DHCP6_OPTION_BOOTFILE_URL:
+	dissect_url ((const char *)opt->data,
+		      &dhcp6->boot_file_proto,
+		      &dhcp6->boot_file_server_ip,
+		      &dhcp6->boot_file_path);
+	break;
+
+      default:
+	break;
+    }
+}
+
+static void
+foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt, grub_size_t size, dhcp6_option_hook_fn hook, void *hook_data)
+{
+  while (size)
+    {
+      grub_uint16_t code, len;
+
+      if (size < sizeof (*opt))
+	{
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped with remaining size %" PRIxGRUB_SIZE "\n", size);
+	  break;
+	}
+      size -= sizeof (*opt);
+      len = grub_be_to_cpu16 (opt->len);
+      code = grub_be_to_cpu16 (opt->code);
+      if (size < len)
+	{
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped at out of bound length %u for option %u\n", len, code);
+	  break;
+	}
+      if (!len)
+	{
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped at zero length option %u\n", code);
+	  break;
+	}
+      else
+	{
+	  if (hook)
+	    hook (opt, hook_data);
+	  size -= len;
+	  opt = (const struct grub_net_dhcp6_option *)((grub_uint8_t *)opt + len + sizeof (*opt));
+	}
+    }
+}
+
+static grub_dhcp6_options_t
+grub_dhcp6_options_get (const struct grub_net_dhcp6_packet *v6h,
+			grub_size_t size)
+{
+  grub_dhcp6_options_t options;
+
+  if (size < sizeof (*v6h))
+    {
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("DHCPv6 packet size too small"));
+      return NULL;
+    }
+
+  options = grub_zalloc (sizeof(*options));
+  if (!options)
+    return NULL;
+
+  foreach_dhcp6_option ((const struct grub_net_dhcp6_option *)v6h->dhcp_options,
+		       size - sizeof (*v6h), parse_dhcp6_option, options);
+
+  return options;
+}
+
+static void
+grub_dhcp6_options_free (grub_dhcp6_options_t options)
+{
+  if (options->client_duid)
+    grub_free (options->client_duid);
+  if (options->server_duid)
+    grub_free (options->server_duid);
+  if (options->ia_addr)
+    grub_free (options->ia_addr);
+  if (options->dns_server_addrs)
+    grub_free (options->dns_server_addrs);
+  if (options->boot_file_proto)
+    grub_free (options->boot_file_proto);
+  if (options->boot_file_server_ip)
+    grub_free (options->boot_file_server_ip);
+  if (options->boot_file_path)
+    grub_free (options->boot_file_path);
+
+  grub_free (options);
+}
+
+static grub_dhcp6_session_t grub_dhcp6_sessions;
+#define FOR_DHCP6_SESSIONS_SAFE(var, next) FOR_LIST_ELEMENTS_SAFE (var, next, grub_dhcp6_sessions)
+#define FOR_DHCP6_SESSIONS(var) FOR_LIST_ELEMENTS (var, grub_dhcp6_sessions)
+
+static void
+grub_net_configure_by_dhcp6_info (const char *name,
+	  struct grub_net_card *card,
+	  grub_dhcp6_options_t dhcp6,
+	  int is_def,
+	  int flags,
+	  struct grub_net_network_level_interface **ret_inf)
+{
+  grub_net_network_level_netaddress_t netaddr;
+  struct grub_net_network_level_interface *inf;
+
+  if (dhcp6->ia_addr)
+    {
+      inf = grub_net_add_addr (name, card, dhcp6->ia_addr, &card->default_address, flags);
+
+      netaddr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+      netaddr.ipv6.base[0] = dhcp6->ia_addr->ipv6[0];
+      netaddr.ipv6.base[1] = 0;
+      netaddr.ipv6.masksize = 64;
+      grub_net_add_route (name, netaddr, inf);
+
+      if (ret_inf)
+	*ret_inf = inf;
+    }
+
+  if (dhcp6->dns_server_addrs)
+    {
+      grub_uint16_t i;
+
+      for (i = 0; i < dhcp6->num_dns_server; ++i)
+	grub_net_add_dns_server (dhcp6->dns_server_addrs + i);
+    }
+
+  if (dhcp6->boot_file_path)
+    grub_env_set_net_property (name, "boot_file", dhcp6->boot_file_path,
+			  grub_strlen (dhcp6->boot_file_path));
+
+  if (is_def && dhcp6->boot_file_server_ip)
+    {
+      grub_net_default_server = grub_strdup (dhcp6->boot_file_server_ip);
+      grub_env_set ("net_default_interface", name);
+      grub_env_export ("net_default_interface");
+    }
+}
+
+static void
+grub_dhcp6_session_add (struct grub_net_network_level_interface *iface,
+			grub_uint32_t iaid)
+{
+  grub_dhcp6_session_t se;
+  struct grub_datetime date;
+  grub_err_t err;
+  grub_int64_t t = 0;
+
+  se = grub_malloc (sizeof (*se));
+
+  err = grub_get_datetime (&date);
+  if (err || !grub_datetime2unixtime (&date, &t))
+    {
+      grub_errno = GRUB_ERR_NONE;
+      t = 0;
+    }
+
+  se->iface = iface;
+  se->iaid = iaid;
+  se->transaction_id = t;
+  se->start_time = grub_get_time_ms ();
+  se->duid.type = grub_cpu_to_be16_compile_time (3) ;
+  se->duid.hw_type = grub_cpu_to_be16_compile_time (1);
+  grub_memcpy (&se->duid.hwaddr, &iface->hwaddress.mac, sizeof (se->duid.hwaddr));
+  se->adv = NULL;
+  se->reply = NULL;
+  grub_list_push (GRUB_AS_LIST_P (&grub_dhcp6_sessions), GRUB_AS_LIST (se));
+}
+
+static void
+grub_dhcp6_session_remove (grub_dhcp6_session_t se)
+{
+  grub_list_remove (GRUB_AS_LIST (se));
+  if (se->adv)
+    grub_dhcp6_options_free (se->adv);
+  if (se->reply)
+    grub_dhcp6_options_free (se->reply);
+  grub_free (se);
+}
+
+static void
+grub_dhcp6_session_remove_all (void)
+{
+  grub_dhcp6_session_t se, next;
+
+  FOR_DHCP6_SESSIONS_SAFE (se, next)
+    {
+      grub_dhcp6_session_remove (se);
+    }
+  grub_dhcp6_sessions = NULL;
+}
+
+static grub_err_t
+grub_dhcp6_session_configure_network (grub_dhcp6_session_t se)
+{
+  char *name;
+
+  name = grub_xasprintf ("%s:dhcp6", se->iface->card->name);
+  if (!name)
+    return grub_errno;
+
+  grub_net_configure_by_dhcp6_info (name, se->iface->card, se->reply, 1, 0, 0);
+  grub_free (name);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_dhcp6_session_send_request (grub_dhcp6_session_t se)
+{
+  struct grub_net_buff *nb;
+  struct grub_net_dhcp6_option *opt;
+  struct grub_net_dhcp6_packet *v6h;
+  struct grub_net_dhcp6_option_iana *ia_na;
+  struct grub_net_dhcp6_option_iaaddr *iaaddr;
+  struct udphdr *udph;
+  grub_net_network_level_address_t multicast;
+  grub_net_link_level_address_t ll_multicast;
+  grub_uint64_t elapsed;
+  struct grub_net_network_level_interface *inf = se->iface;
+  grub_dhcp6_options_t dhcp6 = se->adv;
+  grub_err_t err = GRUB_ERR_NONE;
+
+  multicast.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+  multicast.ipv6[0] = grub_cpu_to_be64_compile_time (0xff02ULL << 48);
+  multicast.ipv6[1] = grub_cpu_to_be64_compile_time (0x10002ULL);
+
+  err = grub_net_link_layer_resolve (inf, &multicast, &ll_multicast);
+  if (err)
+    return err;
+
+  nb = grub_netbuff_alloc (GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
+
+  if (!nb)
+    return grub_errno;
+
+  err = grub_netbuff_reserve (nb, GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  err = grub_netbuff_push (nb, dhcp6->client_duid_len + sizeof (*opt));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+  opt = (struct grub_net_dhcp6_option *)nb->data;
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_CLIENTID);
+  opt->len = grub_cpu_to_be16 (dhcp6->client_duid_len);
+  grub_memcpy (opt->data, dhcp6->client_duid , dhcp6->client_duid_len);
+
+  err = grub_netbuff_push (nb, dhcp6->server_duid_len + sizeof (*opt));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+  opt = (struct grub_net_dhcp6_option *)nb->data;
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_SERVERID);
+  opt->len = grub_cpu_to_be16 (dhcp6->server_duid_len);
+  grub_memcpy (opt->data, dhcp6->server_duid , dhcp6->server_duid_len);
+
+  err = grub_netbuff_push (nb, sizeof (*ia_na) + sizeof (*opt));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  if (dhcp6->ia_addr)
+    {
+      err = grub_netbuff_push (nb, sizeof(*iaaddr) + sizeof (*opt));
+      if (err)
+	{
+	  grub_netbuff_free (nb);
+	  return err;
+	}
+    }
+  opt = (struct grub_net_dhcp6_option *)nb->data;
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA);
+  opt->len = grub_cpu_to_be16 (sizeof (*ia_na));
+  if (dhcp6->ia_addr)
+    opt->len += grub_cpu_to_be16 (sizeof(*iaaddr) + sizeof (*opt));
+
+  ia_na = (struct grub_net_dhcp6_option_iana *)opt->data;
+  ia_na->iaid = grub_cpu_to_be32 (dhcp6->iaid);
+
+  ia_na->t1 = grub_cpu_to_be32 (dhcp6->t1);
+  ia_na->t2 = grub_cpu_to_be32 (dhcp6->t2);
+
+  if (dhcp6->ia_addr)
+    {
+      opt = (struct grub_net_dhcp6_option *)ia_na->data;
+      opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IAADDR);
+      opt->len = grub_cpu_to_be16 (sizeof (*iaaddr));
+      iaaddr = (struct grub_net_dhcp6_option_iaaddr *)opt->data;
+      grub_set_unaligned64 (iaaddr->addr, dhcp6->ia_addr->ipv6[0]);
+      grub_set_unaligned64 (iaaddr->addr + 8, dhcp6->ia_addr->ipv6[1]);
+
+      iaaddr->preferred_lifetime = grub_cpu_to_be32 (dhcp6->preferred_lifetime);
+      iaaddr->valid_lifetime = grub_cpu_to_be32 (dhcp6->valid_lifetime);
+    }
+
+  err = grub_netbuff_push (nb, sizeof (*opt) + 2 * sizeof (grub_uint16_t));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  opt = (struct grub_net_dhcp6_option*) nb->data;
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ORO);
+  opt->len = grub_cpu_to_be16_compile_time (2 * sizeof (grub_uint16_t));
+  grub_set_unaligned16 (opt->data, grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_BOOTFILE_URL));
+  grub_set_unaligned16 (opt->data + 2, grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_DNS_SERVERS));
+
+  err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (grub_uint16_t));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+  opt = (struct grub_net_dhcp6_option*) nb->data;
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ELAPSED_TIME);
+  opt->len = grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t));
+
+  /* the time is expressed in hundredths of a second */
+  elapsed = grub_divmod64 (grub_get_time_ms () - se->start_time, 10, 0);
+
+  if (elapsed > 0xffff)
+    elapsed = 0xffff;
+
+  grub_set_unaligned16 (opt->data,  grub_cpu_to_be16 ((grub_uint16_t)elapsed));
+
+  err = grub_netbuff_push (nb, sizeof (*v6h));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  v6h = (struct grub_net_dhcp6_packet *) nb->data;
+  v6h->message_type = GRUB_NET_DHCP6_REQUEST;
+  v6h->transaction_id = se->transaction_id;
+
+  err = grub_netbuff_push (nb, sizeof (*udph));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  udph = (struct udphdr *) nb->data;
+  udph->src = grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT);
+  udph->dst = grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT);
+  udph->chksum = 0;
+  udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
+
+  udph->chksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
+						 &inf->address,
+						 &multicast);
+  err = grub_net_send_ip_packet (inf, &multicast, &ll_multicast, nb,
+				 GRUB_NET_IP_UDP);
+
+  grub_netbuff_free (nb);
+
+  return err;
+}
+
+struct grub_net_network_level_interface *
+grub_net_configure_by_dhcpv6_reply (const char *name,
+	struct grub_net_card *card,
+	grub_net_interface_flags_t flags,
+	const struct grub_net_dhcp6_packet *v6h,
+	grub_size_t size,
+	int is_def,
+	char **device, char **path)
+{
+  struct grub_net_network_level_interface *inf;
+  grub_dhcp6_options_t dhcp6;
+  int mask = -1;
+
+  dhcp6 = grub_dhcp6_options_get (v6h, size);
+  if (!dhcp6)
+    {
+      grub_print_error ();
+      return NULL;
+    }
+
+  grub_net_configure_by_dhcp6_info (name, card, dhcp6, is_def, flags, &inf);
+
+  if (device && dhcp6->boot_file_proto && dhcp6->boot_file_server_ip)
+    {
+      *device = grub_xasprintf ("%s,%s", dhcp6->boot_file_proto, dhcp6->boot_file_server_ip);
+      grub_print_error ();
+    }
+  if (path && dhcp6->boot_file_path)
+    {
+      *path = grub_strdup (dhcp6->boot_file_path);
+      grub_print_error ();
+      if (*path)
+	{
+	  char *slash;
+	  slash = grub_strrchr (*path, '/');
+	  if (slash)
+	    *slash = 0;
+	  else
+	    **path = 0;
+	}
+    }
+
+  grub_dhcp6_options_free (dhcp6);
+
+  if (inf)
+    grub_net_add_ipv6_local (inf, mask);
+
+  return inf;
+}
+
 /*
  * This is called directly from net/ip.c:handle_dgram(), because those
  * BOOTP/DHCP packets are a bit special due to their improper
@@ -678,6 +1348,77 @@ grub_net_process_dhcp (struct grub_net_buff *nb,
     }
 }
 
+grub_err_t
+grub_net_process_dhcp6 (struct grub_net_buff *nb,
+                       struct grub_net_card *card __attribute__ ((unused)))
+{
+  const struct grub_net_dhcp6_packet *v6h;
+  grub_dhcp6_session_t se;
+  grub_size_t size;
+  grub_dhcp6_options_t options;
+
+  v6h = (const struct grub_net_dhcp6_packet *) nb->data;
+  size = nb->tail - nb->data;
+
+  options = grub_dhcp6_options_get (v6h, size);
+  if (!options)
+    return grub_errno;
+
+  if (!options->client_duid || !options->server_duid || !options->ia_addr)
+    {
+      grub_dhcp6_options_free (options);
+      return grub_error (GRUB_ERR_BAD_ARGUMENT, "Bad DHCPv6 Packet");
+    }
+
+  FOR_DHCP6_SESSIONS (se)
+    {
+      if (se->transaction_id == v6h->transaction_id &&
+         grub_memcmp (options->client_duid, &se->duid, sizeof (se->duid)) == 0 &&
+         se->iaid == options->iaid)
+       break;
+    }
+
+  if (!se)
+    {
+      grub_dprintf ("bootp", "DHCPv6 session not found\n");
+      grub_dhcp6_options_free (options);
+      return GRUB_ERR_NONE;
+    }
+
+  if (v6h->message_type == GRUB_NET_DHCP6_ADVERTISE)
+    {
+      if (se->adv)
+       {
+         grub_dprintf ("bootp", "Skipped DHCPv6 Advertised .. \n");
+         grub_dhcp6_options_free (options);
+         return GRUB_ERR_NONE;
+       }
+
+      se->adv = options;
+      return grub_dhcp6_session_send_request (se);
+    }
+  else if (v6h->message_type == GRUB_NET_DHCP6_REPLY)
+    {
+      if (!se->adv)
+       {
+         grub_dprintf ("bootp", "Skipped DHCPv6 Reply .. \n");
+         grub_dhcp6_options_free (options);
+         return GRUB_ERR_NONE;
+       }
+
+      se->reply = options;
+      grub_dhcp6_session_configure_network (se);
+      grub_dhcp6_session_remove (se);
+      return GRUB_ERR_NONE;
+    }
+  else
+    {
+      grub_dhcp6_options_free (options);
+    }
+
+  return GRUB_ERR_NONE;
+}
+
 static grub_err_t
 grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)),
 		  int argc, char **args)
@@ -903,180 +1644,174 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
   return err;
 }
 
-static grub_command_t cmd_getdhcp, cmd_bootp, cmd_dhcp;
-
-struct grub_net_network_level_interface *
-grub_net_configure_by_dhcpv6_ack (const char *name,
-				  struct grub_net_card *card,
-				  grub_net_interface_flags_t flags
-				    __attribute__((__unused__)),
-				  const grub_net_link_level_address_t *hwaddr,
-				  const struct grub_net_dhcpv6_packet *packet,
-				  int is_def, char **device, char **path)
+static grub_err_t
+grub_cmd_bootp6 (struct grub_command *cmd __attribute__ ((unused)),
+                 int argc, char **args)
 {
-  struct grub_net_network_level_interface *inter = NULL;
-  struct grub_net_network_level_address addr;
-  int mask = -1;
-
-  if (!device || !path)
-    return NULL;
-
-  *device = 0;
-  *path = 0;
-
-  grub_dprintf ("net", "mac address is %02x:%02x:%02x:%02x:%02x:%02x\n",
-		hwaddr->mac[0], hwaddr->mac[1], hwaddr->mac[2],
-		hwaddr->mac[3], hwaddr->mac[4], hwaddr->mac[5]);
-
-  if (is_def)
-    grub_net_default_server = 0;
-
-  if (is_def && !grub_net_default_server && packet)
-    {
-      const grub_uint8_t *options = packet->dhcp_options;
-      unsigned int option_max = 1024 - OFFSET_OF (dhcp_options, packet);
-      unsigned int i;
-
-      for (i = 0; i < option_max - sizeof (grub_net_dhcpv6_option_t); )
-	{
-	  grub_uint16_t num, len;
-	  grub_net_dhcpv6_option_t *opt =
-	    (grub_net_dhcpv6_option_t *)(options + i);
-
-	  num = grub_be_to_cpu16(opt->option_num);
-	  len = grub_be_to_cpu16(opt->option_len);
+  struct grub_net_card *card;
+  grub_uint32_t iaid = 0;
+  int interval;
+  grub_err_t err;
+  grub_dhcp6_session_t se;
 
-	  grub_dprintf ("net", "got dhcpv6 option %d len %d\n", num, len);
+  err = GRUB_ERR_NONE;
 
-	  if (len == 0)
-	    break;
+  FOR_NET_CARDS (card)
+  {
+    struct grub_net_network_level_interface *iface;
 
-	  if (len + i > 1024)
-	    break;
+    if (argc > 0 && grub_strcmp (card->name, args[0]) != 0)
+      continue;
 
-	  if (num == GRUB_NET_DHCP6_BOOTFILE_URL)
-	    {
-	      char *scheme, *userinfo, *host, *file;
-	      char *tmp;
-	      int hostlen;
-	      int port;
-	      int rc = extract_url_info ((const char *)opt->option_data,
-					 (grub_size_t)len,
-					 &scheme, &userinfo, &host, &port,
-					 &file);
-	      if (rc < 0)
-		continue;
-
-	      /* right now this only handles tftp. */
-	      if (grub_strcmp("tftp", scheme))
-		{
-		  grub_free (scheme);
-		  grub_free (userinfo);
-		  grub_free (host);
-		  grub_free (file);
-		  continue;
-		}
-	      grub_free (userinfo);
-
-	      hostlen = grub_strlen (host);
-	      if (hostlen > 2 && host[0] == '[' && host[hostlen-1] == ']')
-		{
-		  tmp = host+1;
-		  host[hostlen-1] = '\0';
-		}
-	      else
-		tmp = host;
-
-	      *device = grub_xasprintf ("%s,%s", scheme, tmp);
-	      grub_free (scheme);
-	      grub_free (host);
-
-	      if (file && *file)
-		{
-		  tmp = grub_strrchr (file, '/');
-		  if (tmp)
-		    *(tmp+1) = '\0';
-		  else
-		    file[0] = '\0';
-		}
-	      else if (!file)
-		file = grub_strdup ("");
-
-	      if (file[0] == '/')
-		{
-		  *path = grub_strdup (file+1);
-		  grub_free (file);
-		}
-	      else
-		*path = file;
-	    }
-	  else if (num == GRUB_NET_DHCP6_IA_NA)
-	    {
-	      const grub_net_dhcpv6_option_t *ia_na_opt;
-	      const grub_net_dhcpv6_opt_ia_na_t *ia_na =
-		(const grub_net_dhcpv6_opt_ia_na_t *)opt;
-	      unsigned int left = len - OFFSET_OF (options, ia_na);
-	      unsigned int j;
-
-	      if ((grub_uint8_t *)ia_na + left >
-		  (grub_uint8_t *)options + option_max)
-		left -= ((grub_uint8_t *)ia_na + left)
-		        - ((grub_uint8_t *)options + option_max);
-
-	      if (len < OFFSET_OF (option_data, opt)
-			+ sizeof (grub_net_dhcpv6_option_t))
-		{
-		  grub_dprintf ("net",
-				"found dhcpv6 ia_na option with no address\n");
-		  continue;
-		}
-
-	      for (j = 0; left > sizeof (grub_net_dhcpv6_option_t); )
-		{
-		  ia_na_opt = (const grub_net_dhcpv6_option_t *)
-			       (ia_na->options + j);
-		  grub_uint16_t ia_na_opt_num, ia_na_opt_len;
-
-		  ia_na_opt_num = grub_be_to_cpu16 (ia_na_opt->option_num);
-		  ia_na_opt_len = grub_be_to_cpu16 (ia_na_opt->option_len);
-		  if (ia_na_opt_len == 0)
-		    break;
-		  if (j + ia_na_opt_len > left)
-		    break;
-		  if (ia_na_opt_num == GRUB_NET_DHCP6_IA_ADDRESS)
-		    {
-		      const grub_net_dhcpv6_opt_ia_address_t *ia_addr;
-
-		      ia_addr = (const grub_net_dhcpv6_opt_ia_address_t *)
-				 ia_na_opt;
-		      addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
-		      grub_memcpy(addr.ipv6, ia_addr->ipv6_address,
-				  sizeof (ia_addr->ipv6_address));
-		      inter = grub_net_add_addr (name, card, &addr, hwaddr, 0);
-		    }
-
-		  j += ia_na_opt_len;
-		  left -= ia_na_opt_len;
-		}
-	    }
+    iface = grub_net_ipv6_get_link_local (card, &card->default_address);
+    if (!iface)
+      {
+       grub_dhcp6_session_remove_all ();
+       return grub_errno;
+      }
 
-	  i += len + 4;
-	}
+    grub_dhcp6_session_add (iface, iaid++);
+  }
 
-      grub_print_error ();
+  for (interval = 200; interval < 10000; interval *= 2)
+    {
+      int done = 1;
+
+      FOR_DHCP6_SESSIONS (se)
+       {
+         struct grub_net_buff *nb;
+         struct grub_net_dhcp6_option *opt;
+         struct grub_net_dhcp6_packet *v6h;
+         struct grub_net_dhcp6_option_duid_ll *duid;
+         struct grub_net_dhcp6_option_iana *ia_na;
+         grub_net_network_level_address_t multicast;
+         grub_net_link_level_address_t ll_multicast;
+         struct udphdr *udph;
+
+         multicast.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+         multicast.ipv6[0] = grub_cpu_to_be64_compile_time (0xff02ULL << 48);
+         multicast.ipv6[1] = grub_cpu_to_be64_compile_time (0x10002ULL);
+
+         err = grub_net_link_layer_resolve (se->iface,
+                   &multicast, &ll_multicast);
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             return err;
+           }
+
+         nb = grub_netbuff_alloc (GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
+
+         if (!nb)
+           {
+             grub_dhcp6_session_remove_all ();
+             return grub_errno;
+           }
+
+         err = grub_netbuff_reserve (nb, GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (grub_uint16_t));
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         opt = (struct grub_net_dhcp6_option *)nb->data;
+         opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ELAPSED_TIME);
+         opt->len = grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t));
+         grub_set_unaligned16 (opt->data, 0);
+
+         err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (*duid));
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         opt = (struct grub_net_dhcp6_option *)nb->data;
+         opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_CLIENTID);
+         opt->len = grub_cpu_to_be16 (sizeof (*duid));
+
+         duid = (struct grub_net_dhcp6_option_duid_ll *) opt->data;
+         grub_memcpy (duid, &se->duid, sizeof (*duid));
+
+         err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (*ia_na));
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         opt = (struct grub_net_dhcp6_option *)nb->data;
+         opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA);
+         opt->len = grub_cpu_to_be16 (sizeof (*ia_na));
+         ia_na = (struct grub_net_dhcp6_option_iana *)opt->data;
+         ia_na->iaid = grub_cpu_to_be32 (se->iaid);
+         ia_na->t1 = 0;
+         ia_na->t2 = 0;
+
+         err = grub_netbuff_push (nb, sizeof (*v6h));
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         v6h = (struct grub_net_dhcp6_packet *)nb->data;
+         v6h->message_type = GRUB_NET_DHCP6_SOLICIT;
+         v6h->transaction_id = se->transaction_id;
+
+         grub_netbuff_push (nb, sizeof (*udph));
+
+         udph = (struct udphdr *) nb->data;
+         udph->src = grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT);
+         udph->dst = grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT);
+         udph->chksum = 0;
+         udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
+
+         udph->chksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
+                           &se->iface->address, &multicast);
+
+         err = grub_net_send_ip_packet (se->iface, &multicast,
+                   &ll_multicast, nb, GRUB_NET_IP_UDP);
+         done = 0;
+         grub_netbuff_free (nb);
+
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             return err;
+           }
+       }
+      if (!done)
+       grub_net_poll_cards (interval, 0);
     }
 
-  if (is_def)
+  FOR_DHCP6_SESSIONS (se)
     {
-      grub_env_set ("net_default_interface", name);
-      grub_env_export ("net_default_interface");
+      grub_error_push ();
+      err = grub_error (GRUB_ERR_FILE_NOT_FOUND,
+                       N_("couldn't autoconfigure %s"),
+                       se->iface->card->name);
     }
 
-    if (inter)
-      grub_net_add_ipv6_local (inter, mask);
-    return inter;
+  grub_dhcp6_session_remove_all ();
+
+  return err;
 }
 
+static grub_command_t cmd_getdhcp, cmd_bootp, cmd_dhcp, cmd_bootp6;
 
 void
 grub_bootp_init (void)
@@ -1090,11 +1825,15 @@ grub_bootp_init (void)
   cmd_getdhcp = grub_register_command ("net_get_dhcp_option", grub_cmd_dhcpopt,
 				       N_("VAR INTERFACE NUMBER DESCRIPTION"),
 				       N_("retrieve DHCP option and save it into VAR. If VAR is - then print the value."));
+  cmd_bootp6 = grub_register_command ("net_bootp6", grub_cmd_bootp6,
+				     N_("[CARD]"),
+				     N_("perform a DHCPv6 autoconfiguration"));
 }
 
 void
 grub_bootp_fini (void)
 {
+  grub_unregister_command (cmd_bootp6);
   grub_unregister_command (cmd_getdhcp);
   grub_unregister_command (cmd_bootp);
   grub_unregister_command (cmd_dhcp);
grub-core/net/drivers/efi/efinet.c +7 -11
index 2af8742f9..3e2432fb6 100644
@@ -410,9 +410,6 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
     pxe_mode = pxe->mode;
     if (pxe_mode->using_ipv6)
       {
-	grub_net_link_level_address_t hwaddr;
-	struct grub_net_network_level_interface *intf;
-
 	grub_dprintf ("efinet", "using ipv6 and dhcpv6\n");
 	grub_dprintf ("efinet", "dhcp_ack_received: %s%s\n",
 		      pxe_mode->dhcp_ack_received ? "yes" : "no",
@@ -420,14 +417,13 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
 	if (!pxe_mode->dhcp_ack_received)
 	  continue;
 
-	hwaddr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
-	grub_memcpy (hwaddr.mac,
-		     card->efi_net->mode->current_address,
-		     sizeof (hwaddr.mac));
-
-	inter = grub_net_configure_by_dhcpv6_ack (card->name, card, 0, &hwaddr,
-	      (const struct grub_net_dhcpv6_packet *)&pxe_mode->dhcp_ack.dhcpv6,
-	      1, device, path);
+	inter = grub_net_configure_by_dhcpv6_reply (card->name, card, 0,
+						    (struct grub_net_dhcp6_packet *)
+						    &pxe_mode->dhcp_ack,
+						    sizeof (pxe_mode->dhcp_ack),
+						    1, device, path);
+	if (grub_errno)
+	  grub_print_error ();
 	if (inter && device && path)
 	  grub_dprintf ("efinet", "device: `%s' path: `%s'\n", *device, *path);
       }
grub-core/net/ip.c +39 -0
index 3c3d0be0e..f967618e5 100644
@@ -240,6 +240,45 @@ handle_dgram (struct grub_net_buff *nb,
   {
     struct udphdr *udph;
     udph = (struct udphdr *) nb->data;
+
+    if (proto == GRUB_NET_IP_UDP && udph->dst == grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT))
+      {
+	if (udph->chksum)
+	  {
+	    grub_uint16_t chk, expected;
+	    chk = udph->chksum;
+	    udph->chksum = 0;
+	    expected = grub_net_ip_transport_checksum (nb,
+						       GRUB_NET_IP_UDP,
+						       source,
+						       dest);
+	    if (expected != chk)
+	      {
+		grub_dprintf ("net", "Invalid UDP checksum. "
+			      "Expected %x, got %x\n",
+			      grub_be_to_cpu16 (expected),
+			      grub_be_to_cpu16 (chk));
+		grub_netbuff_free (nb);
+		return GRUB_ERR_NONE;
+	      }
+	    udph->chksum = chk;
+	  }
+
+	err = grub_netbuff_pull (nb, sizeof (*udph));
+	if (err)
+	  {
+	    grub_netbuff_free (nb);
+	    return err;
+	  }
+
+	err = grub_net_process_dhcp6 (nb, card);
+	if (err)
+	  grub_print_error ();
+
+	grub_netbuff_free (nb);
+	return GRUB_ERR_NONE;
+      }
+
     if (proto == GRUB_NET_IP_UDP && grub_be_to_cpu16 (udph->dst) == 68)
       {
 	const struct grub_net_bootp_packet *bootp;
include/grub/efi/api.h +1 -1
index c6d702831..c1b869db5 100644
@@ -1570,7 +1570,7 @@ typedef struct grub_efi_pxe_ip_filter
 {
   grub_efi_uint8_t filters;
   grub_efi_uint8_t ip_count;
-  grub_efi_uint8_t reserved;
+  grub_efi_uint16_t reserved;
   grub_efi_ip_address_t ip_list[GRUB_EFI_PXE_MAX_IPCNT];
 } grub_efi_pxe_ip_filter_t;
 
include/grub/net.h +55 -36
index bf8430a63..ff901b96e 100644
@@ -450,50 +450,65 @@ struct grub_net_bootp_packet
   grub_uint8_t vendor[0];
 } GRUB_PACKED;
 
-enum
-  {
-    GRUB_NET_DHCP6_IA_NA = 3,
-    GRUB_NET_DHCP6_IA_ADDRESS = 5,
-    GRUB_NET_DHCP6_BOOTFILE_URL = 59,
-  };
-
-struct grub_net_dhcpv6_option
+struct grub_net_dhcp6_packet
 {
-  grub_uint16_t option_num;
-  grub_uint16_t option_len;
-  grub_uint8_t option_data[];
+  grub_uint32_t message_type:8;
+  grub_uint32_t transaction_id:24;
+  grub_uint8_t dhcp_options[0];
 } GRUB_PACKED;
-typedef struct grub_net_dhcpv6_option grub_net_dhcpv6_option_t;
 
-struct grub_net_dhcpv6_opt_ia_na
-{
-  grub_uint16_t option_num;
-  grub_uint16_t option_len;
+struct grub_net_dhcp6_option {
+  grub_uint16_t code;
+  grub_uint16_t len;
+  grub_uint8_t data[0];
+} GRUB_PACKED;
+
+struct grub_net_dhcp6_option_iana {
   grub_uint32_t iaid;
   grub_uint32_t t1;
   grub_uint32_t t2;
-  grub_uint8_t options[];
+  grub_uint8_t data[0];
 } GRUB_PACKED;
-typedef struct grub_net_dhcpv6_opt_ia_na grub_net_dhcpv6_opt_ia_na_t;
 
-struct grub_net_dhcpv6_opt_ia_address
-{
-  grub_uint16_t option_num;
-  grub_uint16_t option_len;
-  grub_uint64_t ipv6_address[2];
+struct grub_net_dhcp6_option_iaaddr {
+  grub_uint8_t addr[16];
   grub_uint32_t preferred_lifetime;
   grub_uint32_t valid_lifetime;
-  grub_uint8_t options[];
+  grub_uint8_t data[0];
 } GRUB_PACKED;
-typedef struct grub_net_dhcpv6_opt_ia_address grub_net_dhcpv6_opt_ia_address_t;
 
-struct grub_net_dhcpv6_packet
+struct grub_net_dhcp6_option_duid_ll
 {
-  grub_uint32_t message_type:8;
-  grub_uint32_t transaction_id:24;
-  grub_uint8_t dhcp_options[1024];
+  grub_uint16_t type;
+  grub_uint16_t hw_type;
+  grub_uint8_t hwaddr[6];
 } GRUB_PACKED;
-typedef struct grub_net_dhcpv6_packet grub_net_dhcpv6_packet_t;
+
+enum
+  {
+    GRUB_NET_DHCP6_SOLICIT = 1,
+    GRUB_NET_DHCP6_ADVERTISE = 2,
+    GRUB_NET_DHCP6_REQUEST = 3,
+    GRUB_NET_DHCP6_REPLY = 7
+  };
+
+enum
+  {
+    DHCP6_CLIENT_PORT = 546,
+    DHCP6_SERVER_PORT = 547
+  };
+
+enum
+  {
+    GRUB_NET_DHCP6_OPTION_CLIENTID = 1,
+    GRUB_NET_DHCP6_OPTION_SERVERID = 2,
+    GRUB_NET_DHCP6_OPTION_IA_NA = 3,
+    GRUB_NET_DHCP6_OPTION_IAADDR = 5,
+    GRUB_NET_DHCP6_OPTION_ORO = 6,
+    GRUB_NET_DHCP6_OPTION_ELAPSED_TIME = 8,
+    GRUB_NET_DHCP6_OPTION_DNS_SERVERS = 23,
+    GRUB_NET_DHCP6_OPTION_BOOTFILE_URL = 59
+  };
 
 #define	GRUB_NET_BOOTP_RFC1048_MAGIC_0	0x63
 #define	GRUB_NET_BOOTP_RFC1048_MAGIC_1	0x82
@@ -531,12 +546,12 @@ grub_net_configure_by_dhcp_ack (const char *name,
 				int is_def, char **device, char **path);
 
 struct grub_net_network_level_interface *
-grub_net_configure_by_dhcpv6_ack (const char *name,
-				 struct grub_net_card *card,
-				 grub_net_interface_flags_t flags,
-				 const grub_net_link_level_address_t *hwaddr,
-				 const struct grub_net_dhcpv6_packet *packet,
-				 int is_def, char **device, char **path);
+grub_net_configure_by_dhcpv6_reply (const char *name,
+				    struct grub_net_card *card,
+				    grub_net_interface_flags_t flags,
+				    const struct grub_net_dhcp6_packet *v6,
+				    grub_size_t size,
+				    int is_def, char **device, char **path);
 
 int
 grub_ipv6_get_masksize(grub_uint16_t *mask);
@@ -553,6 +568,10 @@ void
 grub_net_process_dhcp (struct grub_net_buff *nb,
 		       struct grub_net_network_level_interface *iface);
 
+grub_err_t
+grub_net_process_dhcp6 (struct grub_net_buff *nb,
+			struct grub_net_card *card);
+
 int
 grub_net_hwaddr_cmp (const grub_net_link_level_address_t *a,
 		     const grub_net_link_level_address_t *b);
Signature · 7 lines
2.46.2


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

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

#7 lore raw

Re: [PATCH v1 22/37] bootp: New net_bootp6 command

Quoted 10 lines · Michael Chang via Grub-devel
Quoted 3 lines · Leo Sandoval

From: Michael Chang <[email protected]>

Implement new net_bootp6 command for IPv6 network auto configuration via

the

Quoted 1 line

DHCPv6 protocol (RFC3315).

This would have marked the fourth attempt to upstream the patch by different people.

Quoted 6 lines

https://lists.gnu.org/archive/html/grub-devel/2016-08/msg00000.html
https://lists.gnu.org/archive/html/grub-devel/2023-01/msg00012.html
https://www.mail-archive.com/[email protected]/msg30432.html

If everything is still desirable, I could help by reposting the series in a separate thread to ease the review,

Please do

Quoted 3 lines

but I’m not really sure because
it seems to me that upstream isn't interested in the feature.

We're. Just our throughput is limited and patches fall through the cracks

Quoted 1,441 lines

Thanks,
Michael

Quoted 48 lines
[pjones: Put back our code to add a local route]
---
 grub-core/net/bootp.c              | 1059 +++++++++++++++++++++++-----
 grub-core/net/drivers/efi/efinet.c |   18 +-
 grub-core/net/ip.c                 |   39 +
 include/grub/efi/api.h             |    2 +-
 include/grub/net.h                 |   91 ++-
 5 files changed, 1001 insertions(+), 208 deletions(-)
grub-core/net/bootp.c +27 -0
index 02d2c2614..e0aec2523 100644
@@ -24,6 +24,98 @@
 #include <grub/net/netbuff.h>
 #include <grub/net/udp.h>
 #include <grub/datetime.h>
+#include <grub/time.h>
+#include <grub/list.h>
+
+static int
+dissect_url (const char *url, char **proto, char **host, char **path)
+{
+  const char *p, *ps;
+  grub_size_t l;
+
+  *proto = *host = *path = NULL;
+  ps = p = url;
+
+  while ((p = grub_strchr (p, ':')))
+    {
+      if (grub_strlen (p) < sizeof ("://") - 1)
+     break;
+      if (grub_memcmp (p, "://", sizeof ("://") - 1) == 0)
+     {
+       l = p - ps;
+       *proto = grub_malloc (l + 1);
+       if (!*proto)
+         {
+           grub_print_error ();
+           return 0;
+         }
+
+       grub_memcpy (*proto, ps, l);

grub-core/net/bootp.c line 63

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+       (*proto)[l] = '\0';
+       p +=  sizeof ("://") - 1;
+       break;
+     }
+      ++p;
+    }
+
+  if (!*proto)
+    {
+      grub_dprintf ("bootp", "url: %s is not valid, protocol not

found\n", url);

grub-core/net/bootp.c

Quoted 9 lines
grub-core/net/bootp.c +9 -0
+      return 0;
+    }
+
+  ps = p;
+  p = grub_strchr (p, '/');
+
+  if (!p)
+    {
+      grub_dprintf ("bootp", "url: %s is not valid, host/path not

found\n", url);

Quoted 86 lines
grub-core/net/bootp.c +79 -0
+      grub_free (*proto);
+      *proto = NULL;
+      return 0;
+    }
+
+  l = p - ps;
+
+  if (l > 2 && ps[0] == '[' && ps[l - 1] == ']')
+    {
+      *host = grub_malloc (l - 1);
+      if (!*host)
+     {
+       grub_print_error ();
+       grub_free (*proto);
+       *proto = NULL;
+       return 0;
+     }
+      grub_memcpy (*host, ps + 1, l - 2);
+      (*host)[l - 2] = 0;
+    }
+  else
+    {
+      *host = grub_malloc (l + 1);
+      if (!*host)
+     {
+       grub_print_error ();
+       grub_free (*proto);
+       *proto = NULL;
+       return 0;
+     }
+      grub_memcpy (*host, ps, l);
+      (*host)[l] = 0;
+    }
+
+  *path = grub_strdup (p);
+  if (!*path)
+    {
+      grub_print_error ();
+      grub_free (*host);
+      grub_free (*proto);
+      *host = NULL;
+      *proto = NULL;
+      return 0;
+    }
+  return 1;
+}
 
 struct grub_dhcp_discover_options
 {
@@ -610,6 +702,584 @@ out:
   return err;
 }
 
+/* The default netbuff size for sending DHCPv6 packets which should be
+   large enough to hold the information */
+#define GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE 512
+
+struct grub_dhcp6_options
+{
+  grub_uint8_t *client_duid;
+  grub_uint16_t client_duid_len;
+  grub_uint8_t *server_duid;
+  grub_uint16_t server_duid_len;
+  grub_uint32_t iaid;
+  grub_uint32_t t1;
+  grub_uint32_t t2;
+  grub_net_network_level_address_t *ia_addr;
+  grub_uint32_t preferred_lifetime;
+  grub_uint32_t valid_lifetime;
+  grub_net_network_level_address_t *dns_server_addrs;
+  grub_uint16_t num_dns_server;
+  char *boot_file_proto;
+  char *boot_file_server_ip;
+  char *boot_file_path;
+};
+
+typedef struct grub_dhcp6_options *grub_dhcp6_options_t;
+
+struct grub_dhcp6_session
+{
+  struct grub_dhcp6_session *next;
+  struct grub_dhcp6_session **prev;
+  grub_uint32_t iaid;
+  grub_uint32_t transaction_id:24;
+  grub_uint64_t start_time;
+  struct grub_net_dhcp6_option_duid_ll duid;

grub-core/net/bootp.c out: · line 747

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+  struct grub_net_network_level_interface *iface;
+
+  /* The associated dhcpv6 options */
+  grub_dhcp6_options_t adv;
+  grub_dhcp6_options_t reply;
+};
+
+typedef struct grub_dhcp6_session *grub_dhcp6_session_t;
+
+typedef void (*dhcp6_option_hook_fn) (const struct

grub_net_dhcp6_option *opt, void *data);

grub-core/net/bootp.c out:

Quoted 3 lines
grub-core/net/bootp.c +3 -0
+
+static void
+foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt,

grub_size_t size,

Quoted 8 lines
grub-core/net/bootp.c +8 -0
+                   dhcp6_option_hook_fn hook, void *hook_data);
+
+static void
+parse_dhcp6_iaaddr (const struct grub_net_dhcp6_option *opt, void *data)
+{
+  grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t )data;
+
+  grub_uint16_t code = grub_be_to_cpu16 (opt->code);

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+  grub_uint16_t len = grub_be_to_cpu16 (opt->len);
+
+  if (code == GRUB_NET_DHCP6_OPTION_IAADDR)
+    {
+      const struct grub_net_dhcp6_option_iaaddr *iaaddr;
+      iaaddr = (const struct grub_net_dhcp6_option_iaaddr *)opt->data;
+
+      if (len < sizeof (*iaaddr))
+     {
+       grub_dprintf ("bootp", "DHCPv6: code %u with insufficient length

%u\n", code, len);

grub-core/net/bootp.c out:

Quoted 8 lines
grub-core/net/bootp.c +8 -0
+       return;
+     }
+      if (!dhcp6->ia_addr)
+     {
+       dhcp6->ia_addr = grub_malloc (sizeof(*dhcp6->ia_addr));
+       dhcp6->ia_addr->type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+       dhcp6->ia_addr->ipv6[0] = grub_get_unaligned64 (iaaddr->addr);
+       dhcp6->ia_addr->ipv6[1] = grub_get_unaligned64 (iaaddr->addr +

8);

grub-core/net/bootp.c out: · line 777

Quoted 1 line
grub-core/net/bootp.c +1 -0
+       dhcp6->preferred_lifetime = grub_be_to_cpu32

(iaaddr->preferred_lifetime);

grub-core/net/bootp.c out: · line 778

Quoted 1 line
grub-core/net/bootp.c +1 -0
+       dhcp6->valid_lifetime = grub_be_to_cpu32

(iaaddr->valid_lifetime);

Quoted 8 lines
grub-core/net/bootp.c +8 -0
+     }
+    }
+}
+
+static void
+parse_dhcp6_option (const struct grub_net_dhcp6_option *opt, void *data)
+{
+  grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t)data;

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+  grub_uint16_t code = grub_be_to_cpu16 (opt->code);
+  grub_uint16_t len = grub_be_to_cpu16 (opt->len);
+
+  switch (code)
+    {
+      case GRUB_NET_DHCP6_OPTION_CLIENTID:
+
+     if (dhcp6->client_duid || !len)
+       {
+         grub_dprintf ("bootp", "Skipped DHCPv6 CLIENTID with length

%u\n", len);

grub-core/net/bootp.c out:

Quoted 12 lines
grub-core/net/bootp.c +12 -0
+         break;
+       }
+     dhcp6->client_duid = grub_malloc (len);
+     grub_memcpy (dhcp6->client_duid, opt->data, len);
+     dhcp6->client_duid_len = len;
+     break;
+
+      case GRUB_NET_DHCP6_OPTION_SERVERID:
+
+     if (dhcp6->server_duid || !len)
+       {
+         grub_dprintf ("bootp", "Skipped DHCPv6 SERVERID with length

%u\n", len);

Quoted 5 lines
grub-core/net/bootp.c +5 -0
+         break;
+       }
+     dhcp6->server_duid = grub_malloc (len);
+     grub_memcpy (dhcp6->server_duid, opt->data, len);
+     dhcp6->server_duid_len = len;

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+     break;
+
+      case GRUB_NET_DHCP6_OPTION_IA_NA:
+     {
+       const struct grub_net_dhcp6_option_iana *ia_na;
+       grub_uint16_t data_len;
+
+       if (dhcp6->iaid || len < sizeof (*ia_na))
+         {
+           grub_dprintf ("bootp", "Skipped DHCPv6 IA_NA with length

%u\n", len);

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+           break;
+         }
+       ia_na = (const struct grub_net_dhcp6_option_iana *)opt->data;
+       dhcp6->iaid = grub_be_to_cpu32 (ia_na->iaid);
+       dhcp6->t1 = grub_be_to_cpu32 (ia_na->t1);
+       dhcp6->t2 = grub_be_to_cpu32 (ia_na->t2);
+
+       data_len = len - sizeof (*ia_na);
+       if (data_len)
+         foreach_dhcp6_option ((const struct grub_net_dhcp6_option

*)ia_na->data, data_len, parse_dhcp6_iaaddr, dhcp6);

grub-core/net/bootp.c out:

Quoted 12 lines
grub-core/net/bootp.c +12 -0
+     }
+     break;
+
+      case GRUB_NET_DHCP6_OPTION_DNS_SERVERS:
+     {
+       const grub_uint8_t *po;
+       grub_uint16_t ln;
+       grub_net_network_level_address_t *la;
+
+       if (!len || len & 0xf)
+         {
+           grub_dprintf ("bootp", "Skip invalid length DHCPv6

DNS_SERVERS \n");

Quoted 19 lines
grub-core/net/bootp.c +19 -0
+           break;
+         }
+       dhcp6->num_dns_server = ln = len >> 4;
+       dhcp6->dns_server_addrs = la = grub_zalloc (ln * sizeof (*la));
+
+       for (po = opt->data; ln > 0; po += 0x10, la++, ln--)
+         {
+           la->type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+           la->ipv6[0] = grub_get_unaligned64 (po);
+           la->ipv6[1] = grub_get_unaligned64 (po + 8);
+           la->option = DNS_OPTION_PREFER_IPV6;
+         }
+     }
+     break;
+
+      case GRUB_NET_DHCP6_OPTION_BOOTFILE_URL:
+     dissect_url ((const char *)opt->data,
+                   &dhcp6->boot_file_proto,
+                   &dhcp6->boot_file_server_ip,

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+                   &dhcp6->boot_file_path);
+     break;
+
+      default:
+     break;
+    }
+}
+
+static void
+foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt,

grub_size_t size, dhcp6_option_hook_fn hook, void *hook_data)

grub-core/net/bootp.c out:

Quoted 8 lines
grub-core/net/bootp.c +8 -0
+{
+  while (size)
+    {
+      grub_uint16_t code, len;
+
+      if (size < sizeof (*opt))
+     {
+       grub_dprintf ("bootp", "DHCPv6: Options stopped with remaining

size %" PRIxGRUB_SIZE "\n", size);

grub-core/net/bootp.c out:

Quoted 8 lines
grub-core/net/bootp.c +8 -0
+       break;
+     }
+      size -= sizeof (*opt);
+      len = grub_be_to_cpu16 (opt->len);
+      code = grub_be_to_cpu16 (opt->code);
+      if (size < len)
+     {
+       grub_dprintf ("bootp", "DHCPv6: Options stopped at out of bound

length %u for option %u\n", len, code);

grub-core/net/bootp.c out:

Quoted 5 lines
grub-core/net/bootp.c +5 -0
+       break;
+     }
+      if (!len)
+     {
+       grub_dprintf ("bootp", "DHCPv6: Options stopped at zero length

option %u\n", code);

grub-core/net/bootp.c out:

Quoted 8 lines
grub-core/net/bootp.c +8 -0
+       break;
+     }
+      else
+     {
+       if (hook)
+         hook (opt, hook_data);
+       size -= len;
+       opt = (const struct grub_net_dhcp6_option *)((grub_uint8_t *)opt

+ len + sizeof (*opt));

grub-core/net/bootp.c out:

Quoted 13 lines
grub-core/net/bootp.c +13 -0
+     }
+    }
+}
+
+static grub_dhcp6_options_t
+grub_dhcp6_options_get (const struct grub_net_dhcp6_packet *v6h,
+                     grub_size_t size)
+{
+  grub_dhcp6_options_t options;
+
+  if (size < sizeof (*v6h))
+    {
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("DHCPv6 packet size too

small"));

grub-core/net/bootp.c out:

Quoted 8 lines
grub-core/net/bootp.c +8 -0
+      return NULL;
+    }
+
+  options = grub_zalloc (sizeof(*options));
+  if (!options)
+    return NULL;
+
+  foreach_dhcp6_option ((const struct grub_net_dhcp6_option

*)v6h->dhcp_options,

Quoted 18 lines
grub-core/net/bootp.c +18 -0
+                    size - sizeof (*v6h), parse_dhcp6_option, options);
+
+  return options;
+}
+
+static void
+grub_dhcp6_options_free (grub_dhcp6_options_t options)
+{
+  if (options->client_duid)
+    grub_free (options->client_duid);
+  if (options->server_duid)
+    grub_free (options->server_duid);
+  if (options->ia_addr)
+    grub_free (options->ia_addr);
+  if (options->dns_server_addrs)
+    grub_free (options->dns_server_addrs);
+  if (options->boot_file_proto)
+    grub_free (options->boot_file_proto);

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+  if (options->boot_file_server_ip)
+    grub_free (options->boot_file_server_ip);
+  if (options->boot_file_path)
+    grub_free (options->boot_file_path);
+
+  grub_free (options);
+}
+
+static grub_dhcp6_session_t grub_dhcp6_sessions;
+#define FOR_DHCP6_SESSIONS_SAFE(var, next) FOR_LIST_ELEMENTS_SAFE (var,

next, grub_dhcp6_sessions)

grub-core/net/bootp.c out: · line 953

Quoted 1 line
grub-core/net/bootp.c +1 -0
+#define FOR_DHCP6_SESSIONS(var) FOR_LIST_ELEMENTS (var,

grub_dhcp6_sessions)

Quoted 5 lines
grub-core/net/bootp.c +5 -0
+
+static void
+grub_net_configure_by_dhcp6_info (const char *name,
+       struct grub_net_card *card,
+       grub_dhcp6_options_t dhcp6,

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+       int is_def,
+       int flags,
+       struct grub_net_network_level_interface **ret_inf)
+{
+  grub_net_network_level_netaddress_t netaddr;
+  struct grub_net_network_level_interface *inf;
+
+  if (dhcp6->ia_addr)
+    {
+      inf = grub_net_add_addr (name, card, dhcp6->ia_addr,

&card->default_address, flags);

Quoted 16 lines
grub-core/net/bootp.c +16 -0
+
+      netaddr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+      netaddr.ipv6.base[0] = dhcp6->ia_addr->ipv6[0];
+      netaddr.ipv6.base[1] = 0;
+      netaddr.ipv6.masksize = 64;
+      grub_net_add_route (name, netaddr, inf);
+
+      if (ret_inf)
+     *ret_inf = inf;
+    }
+
+  if (dhcp6->dns_server_addrs)
+    {
+      grub_uint16_t i;
+
+      for (i = 0; i < dhcp6->num_dns_server; ++i)

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+     grub_net_add_dns_server (dhcp6->dns_server_addrs + i);
+    }
+
+  if (dhcp6->boot_file_path)
+    grub_env_set_net_property (name, "boot_file", dhcp6->boot_file_path,
+                       grub_strlen (dhcp6->boot_file_path));
+
+  if (is_def && dhcp6->boot_file_server_ip)
+    {
+      grub_net_default_server = grub_strdup

(dhcp6->boot_file_server_ip);

Quoted 20 lines
grub-core/net/bootp.c +20 -0
+      grub_env_set ("net_default_interface", name);
+      grub_env_export ("net_default_interface");
+    }
+}
+
+static void
+grub_dhcp6_session_add (struct grub_net_network_level_interface *iface,
+                     grub_uint32_t iaid)
+{
+  grub_dhcp6_session_t se;
+  struct grub_datetime date;
+  grub_err_t err;
+  grub_int64_t t = 0;
+
+  se = grub_malloc (sizeof (*se));
+
+  err = grub_get_datetime (&date);
+  if (err || !grub_datetime2unixtime (&date, &t))
+    {
+      grub_errno = GRUB_ERR_NONE;

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+      t = 0;
+    }
+
+  se->iface = iface;
+  se->iaid = iaid;
+  se->transaction_id = t;
+  se->start_time = grub_get_time_ms ();
+  se->duid.type = grub_cpu_to_be16_compile_time (3) ;
+  se->duid.hw_type = grub_cpu_to_be16_compile_time (1);
+  grub_memcpy (&se->duid.hwaddr, &iface->hwaddress.mac, sizeof

(se->duid.hwaddr));

grub-core/net/bootp.c out:

Quoted 3 lines
grub-core/net/bootp.c +3 -0
+  se->adv = NULL;
+  se->reply = NULL;
+  grub_list_push (GRUB_AS_LIST_P (&grub_dhcp6_sessions), GRUB_AS_LIST

(se));

Quoted 25 lines
grub-core/net/bootp.c +25 -0
+}
+
+static void
+grub_dhcp6_session_remove (grub_dhcp6_session_t se)
+{
+  grub_list_remove (GRUB_AS_LIST (se));
+  if (se->adv)
+    grub_dhcp6_options_free (se->adv);
+  if (se->reply)
+    grub_dhcp6_options_free (se->reply);
+  grub_free (se);
+}
+
+static void
+grub_dhcp6_session_remove_all (void)
+{
+  grub_dhcp6_session_t se, next;
+
+  FOR_DHCP6_SESSIONS_SAFE (se, next)
+    {
+      grub_dhcp6_session_remove (se);
+    }
+  grub_dhcp6_sessions = NULL;
+}
+

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+static grub_err_t
+grub_dhcp6_session_configure_network (grub_dhcp6_session_t se)
+{
+  char *name;
+
+  name = grub_xasprintf ("%s:dhcp6", se->iface->card->name);
+  if (!name)
+    return grub_errno;
+
+  grub_net_configure_by_dhcp6_info (name, se->iface->card, se->reply,

1, 0, 0);

Quoted 25 lines
grub-core/net/bootp.c +25 -0
+  grub_free (name);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_dhcp6_session_send_request (grub_dhcp6_session_t se)
+{
+  struct grub_net_buff *nb;
+  struct grub_net_dhcp6_option *opt;
+  struct grub_net_dhcp6_packet *v6h;
+  struct grub_net_dhcp6_option_iana *ia_na;
+  struct grub_net_dhcp6_option_iaaddr *iaaddr;
+  struct udphdr *udph;
+  grub_net_network_level_address_t multicast;
+  grub_net_link_level_address_t ll_multicast;
+  grub_uint64_t elapsed;
+  struct grub_net_network_level_interface *inf = se->iface;
+  grub_dhcp6_options_t dhcp6 = se->adv;
+  grub_err_t err = GRUB_ERR_NONE;
+
+  multicast.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+  multicast.ipv6[0] = grub_cpu_to_be64_compile_time (0xff02ULL << 48);
+  multicast.ipv6[1] = grub_cpu_to_be64_compile_time (0x10002ULL);
+

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+  err = grub_net_link_layer_resolve (inf, &multicast, &ll_multicast);
+  if (err)
+    return err;
+
+  nb = grub_netbuff_alloc (GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
+
+  if (!nb)
+    return grub_errno;
+
+  err = grub_netbuff_reserve (nb,

GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);

Quoted 4 lines
grub-core/net/bootp.c +4 -0
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+    }
+
+  err = grub_netbuff_push (nb, dhcp6->client_duid_len + sizeof (*opt));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+  opt = (struct grub_net_dhcp6_option *)nb->data;
+  opt->code = grub_cpu_to_be16_compile_time

(GRUB_NET_DHCP6_OPTION_CLIENTID);

grub-core/net/bootp.c out:

Quoted 11 lines
grub-core/net/bootp.c +11 -0
+  opt->len = grub_cpu_to_be16 (dhcp6->client_duid_len);
+  grub_memcpy (opt->data, dhcp6->client_duid , dhcp6->client_duid_len);
+
+  err = grub_netbuff_push (nb, dhcp6->server_duid_len + sizeof (*opt));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+  opt = (struct grub_net_dhcp6_option *)nb->data;
+  opt->code = grub_cpu_to_be16_compile_time

(GRUB_NET_DHCP6_OPTION_SERVERID);

Quoted 11 lines
grub-core/net/bootp.c +11 -0
+  opt->len = grub_cpu_to_be16 (dhcp6->server_duid_len);
+  grub_memcpy (opt->data, dhcp6->server_duid , dhcp6->server_duid_len);
+
+  err = grub_netbuff_push (nb, sizeof (*ia_na) + sizeof (*opt));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  if (dhcp6->ia_addr)

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+    {
+      err = grub_netbuff_push (nb, sizeof(*iaaddr) + sizeof (*opt));
+      if (err)
+     {
+       grub_netbuff_free (nb);
+       return err;
+     }
+    }
+  opt = (struct grub_net_dhcp6_option *)nb->data;
+  opt->code = grub_cpu_to_be16_compile_time

(GRUB_NET_DHCP6_OPTION_IA_NA);

Quoted 4 lines
grub-core/net/bootp.c +4 -0
+  opt->len = grub_cpu_to_be16 (sizeof (*ia_na));
+  if (dhcp6->ia_addr)
+    opt->len += grub_cpu_to_be16 (sizeof(*iaaddr) + sizeof (*opt));
+

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+  ia_na = (struct grub_net_dhcp6_option_iana *)opt->data;
+  ia_na->iaid = grub_cpu_to_be32 (dhcp6->iaid);
+
+  ia_na->t1 = grub_cpu_to_be32 (dhcp6->t1);
+  ia_na->t2 = grub_cpu_to_be32 (dhcp6->t2);
+
+  if (dhcp6->ia_addr)
+    {
+      opt = (struct grub_net_dhcp6_option *)ia_na->data;
+      opt->code = grub_cpu_to_be16_compile_time

(GRUB_NET_DHCP6_OPTION_IAADDR);

grub-core/net/bootp.c out:

Quoted 6 lines
grub-core/net/bootp.c +6 -0
+      opt->len = grub_cpu_to_be16 (sizeof (*iaaddr));
+      iaaddr = (struct grub_net_dhcp6_option_iaaddr *)opt->data;
+      grub_set_unaligned64 (iaaddr->addr, dhcp6->ia_addr->ipv6[0]);
+      grub_set_unaligned64 (iaaddr->addr + 8, dhcp6->ia_addr->ipv6[1]);
+
+      iaaddr->preferred_lifetime = grub_cpu_to_be32

(dhcp6->preferred_lifetime);

grub-core/net/bootp.c out:

Quoted 4 lines
grub-core/net/bootp.c +4 -0
+      iaaddr->valid_lifetime = grub_cpu_to_be32 (dhcp6->valid_lifetime);
+    }
+
+  err = grub_netbuff_push (nb, sizeof (*opt) + 2 * sizeof

(grub_uint16_t));

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  opt = (struct grub_net_dhcp6_option*) nb->data;
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ORO);
+  opt->len = grub_cpu_to_be16_compile_time (2 * sizeof (grub_uint16_t));
+  grub_set_unaligned16 (opt->data, grub_cpu_to_be16_compile_time

(GRUB_NET_DHCP6_OPTION_BOOTFILE_URL));

grub-core/net/bootp.c out: · line 1178

Quoted 1 line
grub-core/net/bootp.c +1 -0
+  grub_set_unaligned16 (opt->data + 2, grub_cpu_to_be16_compile_time

(GRUB_NET_DHCP6_OPTION_DNS_SERVERS));

grub-core/net/bootp.c out:

Quoted 9 lines
grub-core/net/bootp.c +9 -0
+
+  err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (grub_uint16_t));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+  opt = (struct grub_net_dhcp6_option*) nb->data;
+  opt->code = grub_cpu_to_be16_compile_time

(GRUB_NET_DHCP6_OPTION_ELAPSED_TIME);

grub-core/net/bootp.c out:

Quoted 9 lines
grub-core/net/bootp.c +9 -0
+  opt->len = grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t));
+
+  /* the time is expressed in hundredths of a second */
+  elapsed = grub_divmod64 (grub_get_time_ms () - se->start_time, 10, 0);
+
+  if (elapsed > 0xffff)
+    elapsed = 0xffff;
+
+  grub_set_unaligned16 (opt->data,  grub_cpu_to_be16

((grub_uint16_t)elapsed));

Quoted 47 lines
grub-core/net/bootp.c +47 -0
+
+  err = grub_netbuff_push (nb, sizeof (*v6h));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  v6h = (struct grub_net_dhcp6_packet *) nb->data;
+  v6h->message_type = GRUB_NET_DHCP6_REQUEST;
+  v6h->transaction_id = se->transaction_id;
+
+  err = grub_netbuff_push (nb, sizeof (*udph));
+  if (err)
+    {
+      grub_netbuff_free (nb);
+      return err;
+    }
+
+  udph = (struct udphdr *) nb->data;
+  udph->src = grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT);
+  udph->dst = grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT);
+  udph->chksum = 0;
+  udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
+
+  udph->chksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
+                                              &inf->address,
+                                              &multicast);
+  err = grub_net_send_ip_packet (inf, &multicast, &ll_multicast, nb,
+                              GRUB_NET_IP_UDP);
+
+  grub_netbuff_free (nb);
+
+  return err;
+}
+
+struct grub_net_network_level_interface *
+grub_net_configure_by_dhcpv6_reply (const char *name,
+     struct grub_net_card *card,
+     grub_net_interface_flags_t flags,
+     const struct grub_net_dhcp6_packet *v6h,
+     grub_size_t size,
+     int is_def,
+     char **device, char **path)
+{
+  struct grub_net_network_level_interface *inf;
+  grub_dhcp6_options_t dhcp6;

grub-core/net/bootp.c out:

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+  int mask = -1;
+
+  dhcp6 = grub_dhcp6_options_get (v6h, size);
+  if (!dhcp6)
+    {
+      grub_print_error ();
+      return NULL;
+    }
+
+  grub_net_configure_by_dhcp6_info (name, card, dhcp6, is_def, flags,

&inf);

grub-core/net/bootp.c out:

Quoted 4 lines
grub-core/net/bootp.c +4 -0
+
+  if (device && dhcp6->boot_file_proto && dhcp6->boot_file_server_ip)
+    {
+      *device = grub_xasprintf ("%s,%s", dhcp6->boot_file_proto,

dhcp6->boot_file_server_ip);

Quoted 29 lines
grub-core/net/bootp.c +25 -0
+      grub_print_error ();
+    }
+  if (path && dhcp6->boot_file_path)
+    {
+      *path = grub_strdup (dhcp6->boot_file_path);
+      grub_print_error ();
+      if (*path)
+     {
+       char *slash;
+       slash = grub_strrchr (*path, '/');
+       if (slash)
+         *slash = 0;
+       else
+         **path = 0;
+     }
+    }
+
+  grub_dhcp6_options_free (dhcp6);
+
+  if (inf)
+    grub_net_add_ipv6_local (inf, mask);
+
+  return inf;
+}
+
 /*
  * This is called directly from net/ip.c:handle_dgram(), because those
  * BOOTP/DHCP packets are a bit special due to their improper

grub-core/net/bootp.c grub_net_process_dhcp (struct grub_net_buff *nb, · line 1353

Quoted 6 lines
grub-core/net/bootp.c +3 -0
@@ -678,6 +1348,77 @@ grub_net_process_dhcp (struct grub_net_buff *nb,
     }
 }
 
+grub_err_t
+grub_net_process_dhcp6 (struct grub_net_buff *nb,
+                       struct grub_net_card *card __attribute__

((unused)))

Quoted 4 lines
grub-core/net/bootp.c +4 -0
+{
+  const struct grub_net_dhcp6_packet *v6h;
+  grub_dhcp6_session_t se;
+  grub_size_t size;

grub-core/net/bootp.c grub_net_process_dhcp (struct grub_net_buff *nb,

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+  grub_dhcp6_options_t options;
+
+  v6h = (const struct grub_net_dhcp6_packet *) nb->data;
+  size = nb->tail - nb->data;
+
+  options = grub_dhcp6_options_get (v6h, size);
+  if (!options)
+    return grub_errno;
+
+  if (!options->client_duid || !options->server_duid ||

!options->ia_addr)

grub-core/net/bootp.c grub_net_process_dhcp (struct grub_net_buff *nb,

Quoted 9 lines
grub-core/net/bootp.c +9 -0
+    {
+      grub_dhcp6_options_free (options);
+      return grub_error (GRUB_ERR_BAD_ARGUMENT, "Bad DHCPv6 Packet");
+    }
+
+  FOR_DHCP6_SESSIONS (se)
+    {
+      if (se->transaction_id == v6h->transaction_id &&
+         grub_memcmp (options->client_duid, &se->duid, sizeof

(se->duid)) == 0 &&

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd

Quoted 49 lines
grub-core/net/bootp.c +45 -0
+         se->iaid == options->iaid)
+       break;
+    }
+
+  if (!se)
+    {
+      grub_dprintf ("bootp", "DHCPv6 session not found\n");
+      grub_dhcp6_options_free (options);
+      return GRUB_ERR_NONE;
+    }
+
+  if (v6h->message_type == GRUB_NET_DHCP6_ADVERTISE)
+    {
+      if (se->adv)
+       {
+         grub_dprintf ("bootp", "Skipped DHCPv6 Advertised .. \n");
+         grub_dhcp6_options_free (options);
+         return GRUB_ERR_NONE;
+       }
+
+      se->adv = options;
+      return grub_dhcp6_session_send_request (se);
+    }
+  else if (v6h->message_type == GRUB_NET_DHCP6_REPLY)
+    {
+      if (!se->adv)
+       {
+         grub_dprintf ("bootp", "Skipped DHCPv6 Reply .. \n");
+         grub_dhcp6_options_free (options);
+         return GRUB_ERR_NONE;
+       }
+
+      se->reply = options;
+      grub_dhcp6_session_configure_network (se);
+      grub_dhcp6_session_remove (se);
+      return GRUB_ERR_NONE;
+    }
+  else
+    {
+      grub_dhcp6_options_free (options);
+    }
+
+  return GRUB_ERR_NONE;
+}
+
 static grub_err_t
 grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)),
                int argc, char **args)
@@ -903,180 +1644,174 @@ grub_cmd_bootp (struct grub_command *cmd

__attribute__ ((unused)),

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),

Quoted 11 lines
grub-core/net/bootp.c +0 -8
   return err;
 }
 
-static grub_command_t cmd_getdhcp, cmd_bootp, cmd_dhcp;
-
-struct grub_net_network_level_interface *
-grub_net_configure_by_dhcpv6_ack (const char *name,
-                               struct grub_net_card *card,
-                               grub_net_interface_flags_t flags
-                                 __attribute__((__unused__)),
-                               const grub_net_link_level_address_t

*hwaddr,

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd · line 914

Quoted 1 line
grub-core/net/bootp.c +0 -1
-                               const struct grub_net_dhcpv6_packet

*packet,

Quoted 120 lines
grub-core/net/bootp.c +14 -101
-                               int is_def, char **device, char **path)
+static grub_err_t
+grub_cmd_bootp6 (struct grub_command *cmd __attribute__ ((unused)),
+                 int argc, char **args)
 {
-  struct grub_net_network_level_interface *inter = NULL;
-  struct grub_net_network_level_address addr;
-  int mask = -1;
-
-  if (!device || !path)
-    return NULL;
-
-  *device = 0;
-  *path = 0;
-
-  grub_dprintf ("net", "mac address is %02x:%02x:%02x:%02x:%02x:%02x\n",
-             hwaddr->mac[0], hwaddr->mac[1], hwaddr->mac[2],
-             hwaddr->mac[3], hwaddr->mac[4], hwaddr->mac[5]);
-
-  if (is_def)
-    grub_net_default_server = 0;
-
-  if (is_def && !grub_net_default_server && packet)
-    {
-      const grub_uint8_t *options = packet->dhcp_options;
-      unsigned int option_max = 1024 - OFFSET_OF (dhcp_options, packet);
-      unsigned int i;
-
-      for (i = 0; i < option_max - sizeof (grub_net_dhcpv6_option_t); )
-     {
-       grub_uint16_t num, len;
-       grub_net_dhcpv6_option_t *opt =
-         (grub_net_dhcpv6_option_t *)(options + i);
-
-       num = grub_be_to_cpu16(opt->option_num);
-       len = grub_be_to_cpu16(opt->option_len);
+  struct grub_net_card *card;
+  grub_uint32_t iaid = 0;
+  int interval;
+  grub_err_t err;
+  grub_dhcp6_session_t se;
 
-       grub_dprintf ("net", "got dhcpv6 option %d len %d\n", num, len);
+  err = GRUB_ERR_NONE;
 
-       if (len == 0)
-         break;
+  FOR_NET_CARDS (card)
+  {
+    struct grub_net_network_level_interface *iface;
 
-       if (len + i > 1024)
-         break;
+    if (argc > 0 && grub_strcmp (card->name, args[0]) != 0)
+      continue;
 
-       if (num == GRUB_NET_DHCP6_BOOTFILE_URL)
-         {
-           char *scheme, *userinfo, *host, *file;
-           char *tmp;
-           int hostlen;
-           int port;
-           int rc = extract_url_info ((const char *)opt->option_data,
-                                      (grub_size_t)len,
-                                      &scheme, &userinfo, &host, &port,
-                                      &file);
-           if (rc < 0)
-             continue;
-
-           /* right now this only handles tftp. */
-           if (grub_strcmp("tftp", scheme))
-             {
-               grub_free (scheme);
-               grub_free (userinfo);
-               grub_free (host);
-               grub_free (file);
-               continue;
-             }
-           grub_free (userinfo);
-
-           hostlen = grub_strlen (host);
-           if (hostlen > 2 && host[0] == '[' && host[hostlen-1] == ']')
-             {
-               tmp = host+1;
-               host[hostlen-1] = '\0';
-             }
-           else
-             tmp = host;
-
-           *device = grub_xasprintf ("%s,%s", scheme, tmp);
-           grub_free (scheme);
-           grub_free (host);
-
-           if (file && *file)
-             {
-               tmp = grub_strrchr (file, '/');
-               if (tmp)
-                 *(tmp+1) = '\0';
-               else
-                 file[0] = '\0';
-             }
-           else if (!file)
-             file = grub_strdup ("");
-
-           if (file[0] == '/')
-             {
-               *path = grub_strdup (file+1);
-               grub_free (file);
-             }
-           else
-             *path = file;
-         }
-       else if (num == GRUB_NET_DHCP6_IA_NA)
-         {
-           const grub_net_dhcpv6_option_t *ia_na_opt;
-           const grub_net_dhcpv6_opt_ia_na_t *ia_na =
-             (const grub_net_dhcpv6_opt_ia_na_t *)opt;
-           unsigned int left = len - OFFSET_OF (options, ia_na);
-           unsigned int j;
-

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),

Quoted 10 lines
grub-core/net/bootp.c +0 -10
-           if ((grub_uint8_t *)ia_na + left >
-               (grub_uint8_t *)options + option_max)
-             left -= ((grub_uint8_t *)ia_na + left)
-                     - ((grub_uint8_t *)options + option_max);
-
-           if (len < OFFSET_OF (option_data, opt)
-                     + sizeof (grub_net_dhcpv6_option_t))
-             {
-               grub_dprintf ("net",
-                             "found dhcpv6 ia_na option with no

address\n");

Quoted 15 lines
grub-core/net/bootp.c +0 -15
-               continue;
-             }
-
-           for (j = 0; left > sizeof (grub_net_dhcpv6_option_t); )
-             {
-               ia_na_opt = (const grub_net_dhcpv6_option_t *)
-                            (ia_na->options + j);
-               grub_uint16_t ia_na_opt_num, ia_na_opt_len;
-
-               ia_na_opt_num = grub_be_to_cpu16 (ia_na_opt->option_num);
-               ia_na_opt_len = grub_be_to_cpu16 (ia_na_opt->option_len);
-               if (ia_na_opt_len == 0)
-                 break;
-               if (j + ia_na_opt_len > left)
-                 break;

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),

Quoted 10 lines
grub-core/net/bootp.c +0 -10
-               if (ia_na_opt_num == GRUB_NET_DHCP6_IA_ADDRESS)
-                 {
-                   const grub_net_dhcpv6_opt_ia_address_t *ia_addr;
-
-                   ia_addr = (const grub_net_dhcpv6_opt_ia_address_t *)
-                              ia_na_opt;
-                   addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
-                   grub_memcpy(addr.ipv6, ia_addr->ipv6_address,
-                               sizeof (ia_addr->ipv6_address));
-                   inter = grub_net_add_addr (name, card, &addr,

hwaddr, 0);

Quoted 26 lines
grub-core/net/bootp.c +15 -9
-                 }
-
-               j += ia_na_opt_len;
-               left -= ia_na_opt_len;
-             }
-         }
+    iface = grub_net_ipv6_get_link_local (card, &card->default_address);
+    if (!iface)
+      {
+       grub_dhcp6_session_remove_all ();
+       return grub_errno;
+      }
 
-       i += len + 4;
-     }
+    grub_dhcp6_session_add (iface, iaid++);
+  }
 
-      grub_print_error ();
+  for (interval = 200; interval < 10000; interval *= 2)
+    {
+      int done = 1;
+
+      FOR_DHCP6_SESSIONS (se)
+       {
+         struct grub_net_buff *nb;

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+         struct grub_net_dhcp6_option *opt;
+         struct grub_net_dhcp6_packet *v6h;
+         struct grub_net_dhcp6_option_duid_ll *duid;
+         struct grub_net_dhcp6_option_iana *ia_na;
+         grub_net_network_level_address_t multicast;
+         grub_net_link_level_address_t ll_multicast;
+         struct udphdr *udph;
+
+         multicast.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
+         multicast.ipv6[0] = grub_cpu_to_be64_compile_time (0xff02ULL

<< 48);

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),

Quoted 11 lines
grub-core/net/bootp.c +11 -0
+         multicast.ipv6[1] = grub_cpu_to_be64_compile_time (0x10002ULL);
+
+         err = grub_net_link_layer_resolve (se->iface,
+                   &multicast, &ll_multicast);
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             return err;
+           }
+
+         nb = grub_netbuff_alloc

(GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),

Quoted 8 lines
grub-core/net/bootp.c +8 -0
+
+         if (!nb)
+           {
+             grub_dhcp6_session_remove_all ();
+             return grub_errno;
+           }
+
+         err = grub_netbuff_reserve (nb,

GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),

Quoted 8 lines
grub-core/net/bootp.c +8 -0
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         err = grub_netbuff_push (nb, sizeof (*opt) + sizeof

(grub_uint16_t));

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),

Quoted 9 lines
grub-core/net/bootp.c +9 -0
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         opt = (struct grub_net_dhcp6_option *)nb->data;
+         opt->code = grub_cpu_to_be16_compile_time

(GRUB_NET_DHCP6_OPTION_ELAPSED_TIME);

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd · line 1729

Quoted 1 line
grub-core/net/bootp.c +1 -0
+         opt->len = grub_cpu_to_be16_compile_time (sizeof

(grub_uint16_t));

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),

Quoted 12 lines
grub-core/net/bootp.c +12 -0
+         grub_set_unaligned16 (opt->data, 0);
+
+         err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (*duid));
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         opt = (struct grub_net_dhcp6_option *)nb->data;
+         opt->code = grub_cpu_to_be16_compile_time

(GRUB_NET_DHCP6_OPTION_CLIENTID);

Quoted 5 lines
grub-core/net/bootp.c +5 -0
+         opt->len = grub_cpu_to_be16 (sizeof (*duid));
+
+         duid = (struct grub_net_dhcp6_option_duid_ll *) opt->data;
+         grub_memcpy (duid, &se->duid, sizeof (*duid));
+

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+         err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (*ia_na));
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         opt = (struct grub_net_dhcp6_option *)nb->data;
+         opt->code = grub_cpu_to_be16_compile_time

(GRUB_NET_DHCP6_OPTION_IA_NA);

Quoted 17 lines
grub-core/net/bootp.c +17 -0
+         opt->len = grub_cpu_to_be16 (sizeof (*ia_na));
+         ia_na = (struct grub_net_dhcp6_option_iana *)opt->data;
+         ia_na->iaid = grub_cpu_to_be32 (se->iaid);
+         ia_na->t1 = 0;
+         ia_na->t2 = 0;
+
+         err = grub_netbuff_push (nb, sizeof (*v6h));
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             grub_netbuff_free (nb);
+             return err;
+           }
+
+         v6h = (struct grub_net_dhcp6_packet *)nb->data;
+         v6h->message_type = GRUB_NET_DHCP6_SOLICIT;
+         v6h->transaction_id = se->transaction_id;

grub-core/net/bootp.c grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),

Quoted 10 lines
grub-core/net/bootp.c +10 -0
+
+         grub_netbuff_push (nb, sizeof (*udph));
+
+         udph = (struct udphdr *) nb->data;
+         udph->src = grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT);
+         udph->dst = grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT);
+         udph->chksum = 0;
+         udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
+
+         udph->chksum = grub_net_ip_transport_checksum (nb,

GRUB_NET_IP_UDP,

Quoted 41 lines
grub-core/net/bootp.c +24 -6
+                           &se->iface->address, &multicast);
+
+         err = grub_net_send_ip_packet (se->iface, &multicast,
+                   &ll_multicast, nb, GRUB_NET_IP_UDP);
+         done = 0;
+         grub_netbuff_free (nb);
+
+         if (err)
+           {
+             grub_dhcp6_session_remove_all ();
+             return err;
+           }
+       }
+      if (!done)
+       grub_net_poll_cards (interval, 0);
     }
 
-  if (is_def)
+  FOR_DHCP6_SESSIONS (se)
     {
-      grub_env_set ("net_default_interface", name);
-      grub_env_export ("net_default_interface");
+      grub_error_push ();
+      err = grub_error (GRUB_ERR_FILE_NOT_FOUND,
+                       N_("couldn't autoconfigure %s"),
+                       se->iface->card->name);
     }
 
-    if (inter)
-      grub_net_add_ipv6_local (inter, mask);
-    return inter;
+  grub_dhcp6_session_remove_all ();
+
+  return err;
 }
 
+static grub_command_t cmd_getdhcp, cmd_bootp, cmd_dhcp, cmd_bootp6;
 
 void
 grub_bootp_init (void)

grub-core/net/bootp.c grub_bootp_init (void) · line 1825

Quoted 1 line
grub-core/net/bootp.c +0 -0
@@ -1090,11 +1825,15 @@ grub_bootp_init (void)
   cmd_getdhcp = grub_register_command ("net_get_dhcp_option",

grub_cmd_dhcpopt,

grub-core/net/bootp.c grub_bootp_init (void) · line 1826

Quoted 1 line
grub-core/net/bootp.c +0 -0
                                     N_("VAR INTERFACE NUMBER

DESCRIPTION"),

grub-core/net/bootp.c grub_bootp_init (void) · line 1827

Quoted 1 line
grub-core/net/bootp.c +0 -0
                                     N_("retrieve DHCP option and save

it into VAR. If VAR is - then print the value."));

grub-core/net/bootp.c grub_bootp_init (void)

Quoted 3 lines
grub-core/net/bootp.c +3 -0
+  cmd_bootp6 = grub_register_command ("net_bootp6", grub_cmd_bootp6,
+                                  N_("[CARD]"),
+                                  N_("perform a DHCPv6

autoconfiguration"));

Review

Quoted 10 lines
grub-core/net/bootp.c +1 -0
 }
 
 void
 grub_bootp_fini (void)
 {
+  grub_unregister_command (cmd_bootp6);
   grub_unregister_command (cmd_getdhcp);
   grub_unregister_command (cmd_bootp);
   grub_unregister_command (cmd_dhcp);
(quoted diff) +0 -0

b/grub-core/net/drivers/efi/efinet.c

grub-core/net/drivers/efi/efinet.c grub_efi_net_config_real (grub_efi_handle_t hnd,

Quoted 4 lines

index 2af8742f9..3e2432fb6 100644

grub-core/net/drivers/efi/efinet.c +0 -0
@@ -410,9 +410,6 @@ grub_efi_net_config_real (grub_efi_handle_t hnd,

char **device,

grub-core/net/drivers/efi/efinet.c grub_efi_net_config_real (grub_efi_handle_t hnd,

Quoted 10 lines
grub-core/net/drivers/efi/efinet.c +0 -3
     pxe_mode = pxe->mode;
     if (pxe_mode->using_ipv6)
       {
-     grub_net_link_level_address_t hwaddr;
-     struct grub_net_network_level_interface *intf;
-
      grub_dprintf ("efinet", "using ipv6 and dhcpv6\n");
      grub_dprintf ("efinet", "dhcp_ack_received: %s%s\n",
                    pxe_mode->dhcp_ack_received ? "yes" : "no",
@@ -420,14 +417,13 @@ grub_efi_net_config_real (grub_efi_handle_t hnd,

char **device,

grub-core/net/drivers/efi/efinet.c grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,

Quoted 9 lines
grub-core/net/drivers/efi/efinet.c +0 -6
      if (!pxe_mode->dhcp_ack_received)
        continue;
 
-     hwaddr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
-     grub_memcpy (hwaddr.mac,
-                  card->efi_net->mode->current_address,
-                  sizeof (hwaddr.mac));
-
-     inter = grub_net_configure_by_dhcpv6_ack (card->name, card, 0,

&hwaddr,

grub-core/net/drivers/efi/efinet.c grub_efi_net_config_real (grub_efi_handle_t hnd, · line 429

Quoted 1 line
grub-core/net/drivers/efi/efinet.c +0 -1
-           (const struct grub_net_dhcpv6_packet

*)&pxe_mode->dhcp_ack.dhcpv6,

grub-core/net/drivers/efi/efinet.c grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,

Quoted 3 lines
grub-core/net/drivers/efi/efinet.c +2 -1
-           1, device, path);
+     inter = grub_net_configure_by_dhcpv6_reply (card->name, card, 0,
+                                                 (struct

grub_net_dhcp6_packet *)

grub-core/net/drivers/efi/efinet.c grub_efi_net_config_real (grub_efi_handle_t hnd, · line 423

Quoted 2 lines
grub-core/net/drivers/efi/efinet.c +2 -0
+                                                 &pxe_mode->dhcp_ack,
+                                                 sizeof

(pxe_mode->dhcp_ack),

grub-core/net/drivers/efi/efinet.c grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,

Quoted 5 lines
grub-core/net/drivers/efi/efinet.c +3 -0
+                                                 1, device, path);
+     if (grub_errno)
+       grub_print_error ();
      if (inter && device && path)
        grub_dprintf ("efinet", "device: `%s' path: `%s'\n", *device,

*path);

grub-core/net/ip.c handle_dgram (struct grub_net_buff *nb, · line 244

Quoted 11 lines
grub-core/net/drivers/efi/efinet.c +0 -0
       }
grub-core/net/ip.c +2 -0
index 3c3d0be0e..f967618e5 100644
@@ -240,6 +240,45 @@ handle_dgram (struct grub_net_buff *nb,
   {
     struct udphdr *udph;
     udph = (struct udphdr *) nb->data;
+
+    if (proto == GRUB_NET_IP_UDP && udph->dst ==

grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT))

Quoted 102 lines
grub-core/net/ip.c +37 -0
+      {
+     if (udph->chksum)
+       {
+         grub_uint16_t chk, expected;
+         chk = udph->chksum;
+         udph->chksum = 0;
+         expected = grub_net_ip_transport_checksum (nb,
+                                                    GRUB_NET_IP_UDP,
+                                                    source,
+                                                    dest);
+         if (expected != chk)
+           {
+             grub_dprintf ("net", "Invalid UDP checksum. "
+                           "Expected %x, got %x\n",
+                           grub_be_to_cpu16 (expected),
+                           grub_be_to_cpu16 (chk));
+             grub_netbuff_free (nb);
+             return GRUB_ERR_NONE;
+           }
+         udph->chksum = chk;
+       }
+
+     err = grub_netbuff_pull (nb, sizeof (*udph));
+     if (err)
+       {
+         grub_netbuff_free (nb);
+         return err;
+       }
+
+     err = grub_net_process_dhcp6 (nb, card);
+     if (err)
+       grub_print_error ();
+
+     grub_netbuff_free (nb);
+     return GRUB_ERR_NONE;
+      }
+
     if (proto == GRUB_NET_IP_UDP && grub_be_to_cpu16 (udph->dst) == 68)
       {
      const struct grub_net_bootp_packet *bootp;
include/grub/efi/api.h +1 -1
index c6d702831..c1b869db5 100644
@@ -1570,7 +1570,7 @@ typedef struct grub_efi_pxe_ip_filter
 {
   grub_efi_uint8_t filters;
   grub_efi_uint8_t ip_count;
-  grub_efi_uint8_t reserved;
+  grub_efi_uint16_t reserved;
   grub_efi_ip_address_t ip_list[GRUB_EFI_PXE_MAX_IPCNT];
 } grub_efi_pxe_ip_filter_t;
 
include/grub/net.h +12 -21
index bf8430a63..ff901b96e 100644
@@ -450,50 +450,65 @@ struct grub_net_bootp_packet
   grub_uint8_t vendor[0];
 } GRUB_PACKED;
 
-enum
-  {
-    GRUB_NET_DHCP6_IA_NA = 3,
-    GRUB_NET_DHCP6_IA_ADDRESS = 5,
-    GRUB_NET_DHCP6_BOOTFILE_URL = 59,
-  };
-
-struct grub_net_dhcpv6_option
+struct grub_net_dhcp6_packet
 {
-  grub_uint16_t option_num;
-  grub_uint16_t option_len;
-  grub_uint8_t option_data[];
+  grub_uint32_t message_type:8;
+  grub_uint32_t transaction_id:24;
+  grub_uint8_t dhcp_options[0];
 } GRUB_PACKED;
-typedef struct grub_net_dhcpv6_option grub_net_dhcpv6_option_t;
 
-struct grub_net_dhcpv6_opt_ia_na
-{
-  grub_uint16_t option_num;
-  grub_uint16_t option_len;
+struct grub_net_dhcp6_option {
+  grub_uint16_t code;
+  grub_uint16_t len;
+  grub_uint8_t data[0];
+} GRUB_PACKED;
+
+struct grub_net_dhcp6_option_iana {
   grub_uint32_t iaid;
   grub_uint32_t t1;
   grub_uint32_t t2;
-  grub_uint8_t options[];
+  grub_uint8_t data[0];
 } GRUB_PACKED;
-typedef struct grub_net_dhcpv6_opt_ia_na grub_net_dhcpv6_opt_ia_na_t;
 
-struct grub_net_dhcpv6_opt_ia_address
-{
-  grub_uint16_t option_num;

include/grub/net.h struct grub_net_bootp_packet · line 488

Quoted 10 lines
include/grub/net.h +3 -4
-  grub_uint16_t option_len;
-  grub_uint64_t ipv6_address[2];
+struct grub_net_dhcp6_option_iaaddr {
+  grub_uint8_t addr[16];
   grub_uint32_t preferred_lifetime;
   grub_uint32_t valid_lifetime;
-  grub_uint8_t options[];
+  grub_uint8_t data[0];
 } GRUB_PACKED;
-typedef struct grub_net_dhcpv6_opt_ia_address

grub_net_dhcpv6_opt_ia_address_t;

Quoted 42 lines
include/grub/net.h +30 -5
-struct grub_net_dhcpv6_packet
+struct grub_net_dhcp6_option_duid_ll
 {
-  grub_uint32_t message_type:8;
-  grub_uint32_t transaction_id:24;
-  grub_uint8_t dhcp_options[1024];
+  grub_uint16_t type;
+  grub_uint16_t hw_type;
+  grub_uint8_t hwaddr[6];
 } GRUB_PACKED;
-typedef struct grub_net_dhcpv6_packet grub_net_dhcpv6_packet_t;
+
+enum
+  {
+    GRUB_NET_DHCP6_SOLICIT = 1,
+    GRUB_NET_DHCP6_ADVERTISE = 2,
+    GRUB_NET_DHCP6_REQUEST = 3,
+    GRUB_NET_DHCP6_REPLY = 7
+  };
+
+enum
+  {
+    DHCP6_CLIENT_PORT = 546,
+    DHCP6_SERVER_PORT = 547
+  };
+
+enum
+  {
+    GRUB_NET_DHCP6_OPTION_CLIENTID = 1,
+    GRUB_NET_DHCP6_OPTION_SERVERID = 2,
+    GRUB_NET_DHCP6_OPTION_IA_NA = 3,
+    GRUB_NET_DHCP6_OPTION_IAADDR = 5,
+    GRUB_NET_DHCP6_OPTION_ORO = 6,
+    GRUB_NET_DHCP6_OPTION_ELAPSED_TIME = 8,
+    GRUB_NET_DHCP6_OPTION_DNS_SERVERS = 23,
+    GRUB_NET_DHCP6_OPTION_BOOTFILE_URL = 59
+  };
 
 #define      GRUB_NET_BOOTP_RFC1048_MAGIC_0  0x63
 #define      GRUB_NET_BOOTP_RFC1048_MAGIC_1  0x82

include/grub/net.h grub_net_configure_by_dhcp_ack (const char *name, · line 537

Quoted 7 lines
include/grub/net.h +0 -4
@@ -531,12 +546,12 @@ grub_net_configure_by_dhcp_ack (const char *name,
                              int is_def, char **device, char **path);
 
 struct grub_net_network_level_interface *
-grub_net_configure_by_dhcpv6_ack (const char *name,
-                              struct grub_net_card *card,
-                              grub_net_interface_flags_t flags,
-                              const grub_net_link_level_address_t

*hwaddr,

include/grub/net.h grub_net_configure_by_dhcp_ack (const char *name, · line 538

Quoted 1 line
include/grub/net.h +0 -1
-                              const struct grub_net_dhcpv6_packet

*packet,

include/grub/net.h grub_net_configure_by_dhcp_ack (const char *name,

Quoted 7 lines
include/grub/net.h +6 -1
-                              int is_def, char **device, char **path);
+grub_net_configure_by_dhcpv6_reply (const char *name,
+                                 struct grub_net_card *card,
+                                 grub_net_interface_flags_t flags,
+                                 const struct grub_net_dhcp6_packet *v6,
+                                 grub_size_t size,
+                                 int is_def, char **device, char

**path);

Quoted 18 lines
include/grub/net.h +4 -1
 int
 grub_ipv6_get_masksize(grub_uint16_t *mask);
@@ -553,6 +568,10 @@ void
 grub_net_process_dhcp (struct grub_net_buff *nb,
                     struct grub_net_network_level_interface *iface);
 
+grub_err_t
+grub_net_process_dhcp6 (struct grub_net_buff *nb,
+                     struct grub_net_card *card);
+
 int
 grub_net_hwaddr_cmp (const grub_net_link_level_address_t *a,
                   const grub_net_link_level_address_t *b);
--

2.46.2

Quoted 10 lines
Quoted 4 lines

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

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

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

#8 lore raw

PATCH 23/37 grub-get-kernel-settings.in: Add grub-get-kernel-settings and use it in 10_linux

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

This patch adds grub-get-kernel-settings, which reads the system kernel installation configuration from /etc/sysconfig/kernel, and outputs ${GRUB_...} variables suitable for evaluation by grub-mkconfig. Those variables are then used by 10_linux to choose whether or not to create debug stanzas.

Resolves: rhbz#1226325
[rharwood: migrate man page to h2m]

---
 Makefile.util.def                             |  7 ++
 configure.ac                                  |  1 +
 docs/man/grub-get-kernel-settings.h2m         |  2 +
 .../bash-completion.d/grub-completion.bash.in | 22 +++++
 util/grub-get-kernel-settings.in              | 88 +++++++++++++++++++
 util/grub-mkconfig.in                         |  3 +
 util/grub.d/10_linux.in                       | 23 +++--
 7 files changed, 141 insertions(+), 5 deletions(-)
 create mode 100644 docs/man/grub-get-kernel-settings.h2m
 create mode 100644 util/grub-get-kernel-settings.in
Makefile.util.def +7 -0
index 95884956a..84a17ef5c 100644
@@ -725,6 +725,13 @@ script = {
   installdir = sbin;
 };
 
+script = {
+  name = grub-get-kernel-settings;
+  common = util/grub-get-kernel-settings.in;
+  mansection = 3;
+  installdir = sbin;
+};
+
 script = {
   name = grub-set-default;
   common = util/grub-set-default.in;
configure.ac +1 -0
index 493e336ec..3602b4ab6 100644
@@ -70,6 +70,7 @@ grub_TRANSFORM([grub-install])
 grub_TRANSFORM([grub-mkconfig])
 grub_TRANSFORM([grub-mkfont])
 grub_TRANSFORM([grub-mkimage])
+grub_TRANSFORM([grub-get-kernel-settings])
 grub_TRANSFORM([grub-glue-efi])
 grub_TRANSFORM([grub-mklayout])
 grub_TRANSFORM([grub-mkpasswd-pbkdf2])
docs/man/grub-get-kernel-settings.h2m new file +2 -0
new file mode 100644
index 000000000..b8051f01f
@@ -0,0 +1,2 @@
+[NAME]
+grub-get-kernel-settings \- Evaluate the system's kernel installation settings for use while making a grub configuration file
util/bash-completion.d/grub-completion.bash.in +22 -0
index 213ce1e57..da7fca5ef 100644
@@ -275,6 +275,28 @@ have ${__grub_sparc64_setup_program} && \
 unset __grub_sparc64_setup_program
 
 
+#
+# grub-get-kernel-settings
+#
+_grub_get_kernel_settings () {
+    local cur
+
+    COMPREPLY=()
+    cur=`_get_cword`
+
+    if [[ "$cur" == -* ]]; then
+        __grubcomp "$(__grub_get_options_from_help)"
+    else
+        # Default complete with a filename
+        _filedir
+    fi
+}
+__grub_get_kernel_settings_program="@grub_get_kernel_settings@"
+have ${__grub_get_kernel_settings_program} && \
+ complete -F _grub_get_kernel_settings -o filenames ${__grub_get_kernel_settings_program}
+unset __grub_get_kernel_settings_program
+
+
 #
 # grub-install
 #
util/grub-get-kernel-settings.in new file +88 -0

88 lines not on this page · raw message

util/grub-mkconfig.in +3 -0
index 9d5d7964b..91f761331 100644
@@ -45,6 +45,7 @@ grub_probe="${sbindir}/@grub_probe@"
 grub_file="${bindir}/@grub_file@"
 grub_editenv="${bindir}/@grub_editenv@"
 grub_script_check="${bindir}/@grub_script_check@"
+grub_get_kernel_settings="${sbindir}/@grub_get_kernel_settings@"
 
 export TEXTDOMAIN=@PACKAGE@
 export TEXTDOMAINDIR="@localedir@"
@@ -161,6 +162,8 @@ if test -f ${sysconfdir}/default/grub ; then
   . ${sysconfdir}/default/grub
 fi
 
+eval "$("${grub_get_kernel_settings}")" || true
+
 if [ "x${GRUB_DISABLE_UUID}" = "xtrue" ]; then
   if [ -z "${GRUB_DISABLE_LINUX_UUID}" ]; then
     GRUB_DISABLE_LINUX_UUID="true"
util/grub.d/10_linux.in +18 -5
index 0115dd0c0..b94af4a8d 100644
@@ -113,7 +113,8 @@ linux_entry ()
   os="$1"
   version="$2"
   type="$3"
-  args="$4"
+  isdebug="$4"
+  args="$5"
 
   if [ -z "$boot_device_id" ]; then
       boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
@@ -125,6 +126,9 @@ linux_entry ()
 	  quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
 	  title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
       fi
+      if [ x$isdebug = xdebug ]; then
+	  title="$title${GRUB_LINUX_DEBUG_TITLE_POSTFIX}"
+      fi
       echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
   else
       echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
@@ -320,11 +324,15 @@ for linux in ${reverse_sorted_list}; do
   fi
 
   if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xtrue ]; then
-    linux_entry "${OS}" "${version}" simple \
+    linux_entry "${OS}" "${version}" simple standard \
     "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
+    if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
+      linux_entry "${OS}" "${version}" simple debug \
+        "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} ${GRUB_CMDLINE_LINUX_DEBUG}"
+    fi
 
     submenu_indentation="$grub_tab"
-    
+
     if [ -z "$boot_device_id" ]; then
 	boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
     fi
@@ -333,10 +341,15 @@ for linux in ${reverse_sorted_list}; do
     is_top_level=false
   fi
 
-  linux_entry "${OS}" "${version}" advanced \
+  linux_entry "${OS}" "${version}" advanced standard \
               "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
+  if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
+    linux_entry "${OS}" "${version}" advanced debug \
+                "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} ${GRUB_CMDLINE_LINUX_DEBUG}"
+  fi
+
   if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
-    linux_entry "${OS}" "${version}" recovery \
+    linux_entry "${OS}" "${version}" recovery standard \
                 "${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}"
   fi
 done
Signature · 7 lines
2.46.2


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

PATCH 24/37 backtrace: Make grub_fatal() also backtrace

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

---
 grub-core/Makefile.core.def     |  3 ++
 grub-core/kern/misc.c           |  6 ++++
 grub-core/lib/arm64/backtrace.c | 62 +++++++++++++++++++++++++++++++++
 grub-core/lib/backtrace.c       |  2 ++
 grub-core/lib/i386/backtrace.c  | 14 +++++++-
 5 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/lib/arm64/backtrace.c
grub-core/Makefile.core.def +3 -0
index 0bffbfea9..17b581220 100644
@@ -199,6 +199,9 @@ kernel = {
 
   softdiv = lib/division.c;
 
+  x86 = lib/i386/backtrace.c;
+  x86 = lib/backtrace.c;
+
   i386 = kern/i386/dl.c;
   i386_xen = kern/i386/dl.c;
   i386_xen_pvh = kern/i386/dl.c;
grub-core/kern/misc.c +6 -0
index 11037dc02..465a8e74e 100644
@@ -26,6 +26,7 @@
 #include <grub/i18n.h>
 #include <grub/types.h>
 #include <grub/charset.h>
+#include <grub/backtrace.h>
 
 union printf_arg
 {
@@ -1301,6 +1302,11 @@ grub_printf_fmt_check (const char *fmt, const char *fmt_expected)
 void __attribute__ ((noreturn))
 grub_abort (void)
 {
+#ifndef GRUB_UTIL
+#if defined(__i386__) || defined(__x86_64__)
+  grub_backtrace();
+#endif
+#endif
   grub_printf ("\nAborted.");
 
 #ifndef GRUB_UTIL
grub-core/lib/arm64/backtrace.c new file +62 -0

62 lines not on this page · raw message

grub-core/lib/backtrace.c +2 -0
index 825a8800e..c0ad6ab8b 100644
@@ -29,6 +29,7 @@ GRUB_MOD_LICENSE ("GPLv3+");
 void
 grub_backtrace_print_address (void *addr)
 {
+#ifndef GRUB_UTIL
   grub_dl_t mod;
 
   FOR_DL_MODULES (mod)
@@ -44,6 +45,7 @@ grub_backtrace_print_address (void *addr)
 	}
   }
 
+#endif
   grub_printf ("%p", addr);
 }
 
grub-core/lib/i386/backtrace.c +13 -1
index c3e03c727..c67273db3 100644
@@ -15,11 +15,23 @@
  *  You should have received a copy of the GNU General Public License
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
+#include <config.h>
+#ifdef GRUB_UTIL
+#define REALLY_GRUB_UTIL GRUB_UTIL
+#undef GRUB_UTIL
+#endif
+
+#include <grub/symbol.h>
+#include <grub/dl.h>
+
+#ifdef REALLY_GRUB_UTIL
+#define GRUB_UTIL REALLY_GRUB_UTIL
+#undef REALLY_GRUB_UTIL
+#endif
 
 #include <grub/misc.h>
 #include <grub/command.h>
 #include <grub/err.h>
-#include <grub/dl.h>
 #include <grub/mm.h>
 #include <grub/term.h>
 #include <grub/backtrace.h>
Signature · 7 lines
2.46.2


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

Re: [PATCH v1 24/37] backtrace: Make grub_fatal() also backtrace

This increases the core size on i386-pc needlessly. And will increase even more when we improve backtrace command. This needs to be a hook and loaded together with normal. Also it needs to be cpu-agnostic

Quoted 173 lines · Leo Sandoval

From: Peter Jones <[email protected]>

---
 grub-core/Makefile.core.def     |  3 ++
 grub-core/kern/misc.c           |  6 ++++
 grub-core/lib/arm64/backtrace.c | 62 +++++++++++++++++++++++++++++++++
 grub-core/lib/backtrace.c       |  2 ++
 grub-core/lib/i386/backtrace.c  | 14 +++++++-
 5 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/lib/arm64/backtrace.c
grub-core/Makefile.core.def +3 -0
index 0bffbfea9..17b581220 100644
@@ -199,6 +199,9 @@ kernel = {
 
   softdiv = lib/division.c;
 
+  x86 = lib/i386/backtrace.c;
+  x86 = lib/backtrace.c;
+
   i386 = kern/i386/dl.c;
   i386_xen = kern/i386/dl.c;
   i386_xen_pvh = kern/i386/dl.c;
grub-core/kern/misc.c +6 -0
index 11037dc02..465a8e74e 100644
@@ -26,6 +26,7 @@
 #include <grub/i18n.h>
 #include <grub/types.h>
 #include <grub/charset.h>
+#include <grub/backtrace.h>
 
 union printf_arg
 {
@@ -1301,6 +1302,11 @@ grub_printf_fmt_check (const char *fmt, const char
 *fmt_expected)
 void __attribute__ ((noreturn))
 grub_abort (void)
 {
+#ifndef GRUB_UTIL
+#if defined(__i386__) || defined(__x86_64__)
+  grub_backtrace();
+#endif
+#endif
   grub_printf ("\nAborted.");
 

#ifndef GRUB_UTIL

grub-core/lib/arm64/backtrace.c new file +62 -0
new file mode 100644
index 000000000..1079b5380
@@ -0,0 +1,62 @@
+/*
+ *  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/misc.h>
+#include <grub/command.h>
+#include <grub/err.h>
+#include <grub/dl.h>
+#include <grub/mm.h>
+#include <grub/term.h>
+#include <grub/backtrace.h>
+
+#define MAX_STACK_FRAME 102400
+
+void
+grub_backtrace_pointer (int frame)
+{
+  while (1)
+    {
+      void *lp = __builtin_return_address (frame);
+      if (!lp)
+       break;
+
+      lp = __builtin_extract_return_addr (lp);
+
+      grub_printf ("%p: ", lp);
+      grub_backtrace_print_address (lp);
+      grub_printf (" (");
+      for (i = 0; i < 2; i++)
+       grub_printf ("%p,", ((void **)ptr) [i + 2]);
+      grub_printf ("%p)\n", ((void **)ptr) [i + 2]);
+      nptr = *(void **)ptr;
+      if (nptr < ptr || (void **) nptr - (void **) ptr > MAX_STACK_FRAME
+         || nptr == ptr)
+       {
+         grub_printf ("Invalid stack frame at %p (%p)\n", ptr, nptr);
+         break;
+       }
+      ptr = nptr;
+    }
+}
+
+void
+grub_backtrace (void)
+{
+  grub_backtrace_pointer (1);
+}
+
grub-core/lib/backtrace.c +2 -0
index 825a8800e..c0ad6ab8b 100644
@@ -29,6 +29,7 @@ GRUB_MOD_LICENSE ("GPLv3+");
 void
 grub_backtrace_print_address (void *addr)
 {
+#ifndef GRUB_UTIL
   grub_dl_t mod;
 
   FOR_DL_MODULES (mod)
@@ -44,6 +45,7 @@ grub_backtrace_print_address (void *addr)
        }
   }
 
+#endif
   grub_printf ("%p", addr);
 }
 
grub-core/lib/i386/backtrace.c +13 -2
index c3e03c727..c67273db3 100644
@@ -15,11 +15,23 @@
  *  You should have received a copy of the GNU General Public License
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
+#include <config.h>
+#ifdef GRUB_UTIL
+#define REALLY_GRUB_UTIL GRUB_UTIL
+#undef GRUB_UTIL
+#endif
+
+#include <grub/symbol.h>
+#include <grub/dl.h>
+
+#ifdef REALLY_GRUB_UTIL
+#define GRUB_UTIL REALLY_GRUB_UTIL
+#undef REALLY_GRUB_UTIL
+#endif
 
 #include <grub/misc.h>
 #include <grub/command.h>
 #include <grub/err.h>
-#include <grub/dl.h>
 #include <grub/mm.h>
 #include <grub/term.h>
 #include <grub/backtrace.h>
--

2.46.2

Quoted 5 lines

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

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

#11 lore raw

PATCH 25/37 grub.texi: Make our info pages say "grub2" where appropriate.

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

This needs to be hooked up to --program-transform=, but I haven't had time.

---
 docs/grub-dev.texi    |   4 +-
 docs/grub.texi        | 359 ++++++++++++++++++++++--------------------
 grub-core/kern/misc.c |   2 +-
 3 files changed, 191 insertions(+), 174 deletions(-)
docs/grub-dev.texi +2 -2
index 1276c5930..04c6678cb 100644
@@ -1,7 +1,7 @@
 \input texinfo
 @c -*-texinfo-*-
 @c %**start of header
-@setfilename grub-dev.info
+@setfilename grub2-dev.info
 @include version-dev.texi
 @settitle GNU GRUB Developers Manual @value{VERSION}
 @c Unify all our little indices for now.
@@ -32,7 +32,7 @@ Invariant Sections.
 
 @dircategory Kernel
 @direntry
-* grub-dev: (grub-dev).                 The GRand Unified Bootloader Dev
+* grub2-dev: (grub2-dev).                 The GRand Unified Bootloader Dev
 @end direntry
 
 @setchapternewpage odd
docs/grub.texi +188 -171

981 lines not on this page · raw message

grub-core/kern/misc.c +1 -1
index 465a8e74e..b1d399d37 100644
@@ -1303,7 +1303,7 @@ void __attribute__ ((noreturn))
 grub_abort (void)
 {
 #ifndef GRUB_UTIL
-#if defined(__i386__) || defined(__x86_64__)
+#if (defined(__i386__) || defined(__x86_64__)) && !defined(GRUB_MACHINE_EMU)
   grub_backtrace();
 #endif
 #endif
Signature · 7 lines
2.46.2


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

Re: [PATCH v1 25/37] grub.texi: Make our info pages say "grub2" where appropriate.

Quoted 4 lines · Leo Sandoval

From: Peter Jones <[email protected]>

This needs to be hooked up to --program-transform=, but I haven't had time.

Can you explain/expand on this? It is not clear (to me) what that means. Is something missing from this patch?

Thanks
Ross

Quoted 1,089 lines
---
  docs/grub-dev.texi    |   4 +-
  docs/grub.texi        | 359 ++++++++++++++++++++++--------------------
  grub-core/kern/misc.c |   2 +-
  3 files changed, 191 insertions(+), 174 deletions(-)
docs/grub-dev.texi +2 -2
index 1276c5930..04c6678cb 100644
@@ -1,7 +1,7 @@
  \input texinfo
  @c -*-texinfo-*-
  @c %**start of header
-@setfilename grub-dev.info
+@setfilename grub2-dev.info
  @include version-dev.texi
  @settitle GNU GRUB Developers Manual @value{VERSION}
  @c Unify all our little indices for now.
@@ -32,7 +32,7 @@ Invariant Sections.
 
  @dircategory Kernel
  @direntry
-* grub-dev: (grub-dev).                 The GRand Unified Bootloader Dev
+* grub2-dev: (grub2-dev).                 The GRand Unified Bootloader Dev
  @end direntry
 
  @setchapternewpage odd
docs/grub.texi +188 -171
index a225f9a88..cecf14d55 100644
@@ -1,7 +1,7 @@
  \input texinfo
  @c -*-texinfo-*-
  @c %**start of header
-@setfilename grub.info
+@setfilename grub2.info
  @include version.texi
  @settitle GNU GRUB Manual @value{VERSION}
  @c Unify all our little indices for now.
@@ -32,15 +32,15 @@ Invariant Sections.
 
  @dircategory Kernel
  @direntry
-* GRUB: (grub).                 The GRand Unified Bootloader
-* grub-install: (grub)Invoking grub-install.    Install GRUB on your drive
-* grub-mkconfig: (grub)Invoking grub-mkconfig.  Generate GRUB configuration
-* grub-mkpasswd-pbkdf2: (grub)Invoking grub-mkpasswd-pbkdf2.
-* grub-mkrelpath: (grub)Invoking grub-mkrelpath.
-* grub-mkrescue: (grub)Invoking grub-mkrescue.  Make a GRUB rescue image
-* grub-mount: (grub)Invoking grub-mount.        Mount a file system using GRUB
-* grub-probe: (grub)Invoking grub-probe.        Probe device information
-* grub-script-check: (grub)Invoking grub-script-check.
+* GRUB2: (grub2).                 The GRand Unified Bootloader
+* grub2-install: (grub2)Invoking grub2-install.    Install GRUB on your drive
+* grub2-mkconfig: (grub2)Invoking grub2-mkconfig.  Generate GRUB configuration
+* grub2-mkpasswd-pbkdf2: (grub2)Invoking grub2-mkpasswd-pbkdf2.
+* grub2-mkrelpath: (grub2)Invoking grub2-mkrelpath.
+* grub2-mkrescue: (grub2)Invoking grub2-mkrescue.  Make a GRUB rescue image
+* grub2-mount: (grub2)Invoking grub2-mount.        Mount a file system using GRUB
+* grub2-probe: (grub2)Invoking grub2-probe.        Probe device information
+* grub2-script-check: (grub2)Invoking grub2-script-check.
  @end direntry
 
  @setchapternewpage odd
@@ -223,7 +223,7 @@ surprising.
 
  @item
  @file{grub.cfg} is typically automatically generated by
-@command{grub-mkconfig} (@pxref{Simple configuration}).  This makes it
+@command{grub2-mkconfig} (@pxref{Simple configuration}).  This makes it
  easier to handle versioned kernel upgrades.
 
  @item
@@ -237,7 +237,7 @@ scripting language: variables, conditionals, and loops are available.
  @item
  A small amount of persistent storage is available across reboots, using the
  @command{save_env} and @command{load_env} commands in GRUB and the
-@command{grub-editenv} utility.  This is not available in all configurations
+@command{grub2-editenv} utility.  This is not available in all configurations
  (@pxref{Environment block}).
 
  @item
@@ -542,7 +542,7 @@ On OS which have device nodes similar to Unix-like OS GRUB tools use the
  OS name. E.g. for GNU/Linux:
 
  @example
-# @kbd{grub-install /dev/sda}
+# @kbd{grub2-install /dev/sda}
  @end example
 
  On AROS we use another syntax. For volumes:
@@ -565,7 +565,7 @@ For disks we use syntax:
  E.g.
 
  @example
-# @kbd{grub-install //:ata.device/0/0}
+# @kbd{grub2-install //:ata.device/0/0}
  @end example
 
  On Windows we use UNC path. For volumes it's typically
@@ -592,7 +592,7 @@ For disks it's
  E.g.
 
  @example
-# @kbd{grub-install \\?\PhysicalDrive0}
+# @kbd{grub2-install \\?\PhysicalDrive0}
  @end example
 
  Beware that you may need to further escape the backslashes depending on your
@@ -602,7 +602,7 @@ When compiled with cygwin support then cygwin drive names are automatically
  when needed. E.g.
 
  @example
-# @kbd{grub-install /dev/sda}
+# @kbd{grub2-install /dev/sda}
  @end example
 
  @node Installation
@@ -615,7 +615,7 @@ from the source tarball, or as a package for your OS.
 
  After you have done that, you need to install the boot loader on a
  drive (floppy or hard disk) by using the utility
-@command{grub-install} (@pxref{Invoking grub-install}) on a UNIX-like OS.
+@command{grub2-install} (@pxref{Invoking grub2-install}) on a UNIX-like OS.
 
  GRUB comes with boot images, which are normally put in the directory
  @file{/usr/lib/grub/<cpu>-<platform>} (for BIOS-based machines
@@ -626,22 +626,22 @@ loader needs to find them (usually @file{/boot}) will be called
  the @dfn{boot directory}.
 
  @menu
-* Installing GRUB using grub-install::
+* Installing GRUB using grub2-install::
  * Making a GRUB bootable CD-ROM::
  * Device map::
  * BIOS installation::
  @end menu
 
 
-@node Installing GRUB using grub-install
-@section Installing GRUB using grub-install
+@node Installing GRUB using grub2-install
+@section Installing GRUB using grub2-install
 
  For information on where GRUB should be installed on PC BIOS platforms,
  @pxref{BIOS installation}.
 
  In order to install GRUB under a UNIX-like OS (such
-as @sc{gnu}), invoke the program @command{grub-install} (@pxref{Invoking
-grub-install}) as the superuser (@dfn{root}).
+as @sc{gnu}), invoke the program @command{grub2-install} (@pxref{Invoking
+grub2-install}) as the superuser (@dfn{root}).
 
  The usage is basically very simple. You only need to specify one
  argument to the program, namely, where to install the boot loader. The
@@ -650,13 +650,13 @@ For example, under Linux the following will install GRUB into the MBR
  of the first IDE disk:
 
  @example
-# @kbd{grub-install /dev/sda}
+# @kbd{grub2-install /dev/sda}
  @end example
 
  Likewise, under GNU/Hurd, this has the same effect:
 
  @example
-# @kbd{grub-install /dev/hd0}
+# @kbd{grub2-install /dev/hd0}
  @end example
 
  But all the above examples assume that GRUB should put images under
@@ -670,7 +670,7 @@ boot floppy with a filesystem. Here is an example:
  # @kbd{mke2fs /dev/fd0}
  # @kbd{mount -t ext2 /dev/fd0 /mnt}
  # @kbd{mkdir /mnt/boot}
-# @kbd{grub-install --boot-directory=/mnt/boot /dev/fd0}
+# @kbd{grub2-install --boot-directory=/mnt/boot /dev/fd0}
  # @kbd{umount /mnt}
  @end group
  @end example
@@ -682,30 +682,37 @@ floppy instead of exposing the USB drive as a hard disk (they call it
  @example
  # @kbd{losetup /dev/loop0 /dev/sdb1}
  # @kbd{mount /dev/loop0 /mnt/usb}
-# @kbd{grub-install --boot-directory=/mnt/usb/bugbios --force --allow-floppy /dev/loop0}
+# @kbd{grub2-install --boot-directory=/mnt/usb/bugbios --force --allow-floppy /dev/loop0}
  @end example
 
  This install doesn't conflict with standard install as long as they are in
  separate directories.
 
+Note that @command{grub2-install} is actually just a shell script and the
+real task is done by other tools such as @command{grub2-mkimage}. Therefore,
+you may run those commands directly to install GRUB, without using
+@command{grub2-install}. Don't do that, however, unless you are very familiar
+with the internals of GRUB. Installing a boot loader on a running OS may be
+extremely dangerous.
+
  On EFI systems for fixed disk install you have to mount EFI System Partition.
  If you mount it at @file{/boot/efi} then you don't need any special arguments:
 
  @example
-# @kbd{grub-install}
+# @kbd{grub2-install}
  @end example
 
  Otherwise you need to specify where your EFI System partition is mounted:
 
  @example
-# @kbd{grub-install --efi-directory=/mnt/efi}
+# @kbd{grub2-install --efi-directory=/mnt/efi}
  @end example
 
  For removable installs you have to use @option{--removable} and specify both
  @option{--boot-directory} and @option{--efi-directory}:
 
  @example
-# @kbd{grub-install --efi-directory=/mnt/usb --boot-directory=/mnt/usb/boot --removable}
+# @kbd{grub2-install --efi-directory=/mnt/usb --boot-directory=/mnt/usb/boot --removable}
  @end example
 
  @node Making a GRUB bootable CD-ROM
@@ -725,10 +732,10 @@ usually also need to include a configuration file @file{grub.cfg} and some
  other GRUB modules.
 
  To make a simple generic GRUB rescue CD, you can use the
-@command{grub-mkrescue} program (@pxref{Invoking grub-mkrescue}):
+@command{grub2-mkrescue} program (@pxref{Invoking grub2-mkrescue}):
 
  @example
-$ @kbd{grub-mkrescue -o grub.iso}
+$ @kbd{grub2-mkrescue -o grub.iso}
  @end example
 
  You will often need to include other files in your image. To do this, first
@@ -751,7 +758,7 @@ directory @file{iso/}.
  Finally, make the image:
 
  @example
-$ @kbd{grub-mkrescue -o grub.iso iso}
+$ @kbd{grub2-mkrescue -o grub.iso iso}
  @end example
 
  This produces a file named @file{grub.iso}, which then can be burned
@@ -767,7 +774,7 @@ storage devices.
  @node Device map
  @section The map between BIOS drives and OS devices
 
-If the device map file exists, the GRUB utilities (@command{grub-probe},
+If the device map file exists, the GRUB utilities (@command{grub2-probe},
  etc.) read it to map BIOS drives to OS devices.  This file consists of lines
  like this:
 
@@ -1269,23 +1276,23 @@ need to write the whole thing by hand.
  @node Simple configuration
  @section Simple configuration handling
 
-The program @command{grub-mkconfig} (@pxref{Invoking grub-mkconfig})
+The program @command{grub2-mkconfig} (@pxref{Invoking grub2-mkconfig})
  generates @file{grub.cfg} files suitable for most cases.  It is suitable for
  use when upgrading a distribution, and will discover available kernels and
  attempt to generate menu entries for them.
 
-@command{grub-mkconfig} does have some limitations.  While adding extra
+@command{grub2-mkconfig} does have some limitations.  While adding extra
  custom menu entries to the end of the list can be done by editing
-@file{/etc/grub.d/40_custom} or creating @file{/boot/grub/custom.cfg},
+@file{/etc/grub.d/40_custom} or creating @file{/boot/grub2/custom.cfg},
  changing the order of menu entries or changing their titles may require
  making complex changes to shell scripts stored in @file{/etc/grub.d/}.  This
  may be improved in the future.  In the meantime, those who feel that it
  would be easier to write @file{grub.cfg} directly are encouraged to do so
  (@pxref{Booting}, and @ref{Shell-like scripting}), and to disable any system
-provided by their distribution to automatically run @command{grub-mkconfig}.
+provided by their distribution to automatically run @command{grub2-mkconfig}.
 
  The file @file{/etc/default/grub} controls the operation of
-@command{grub-mkconfig}.  It is sourced by a shell script, and so must be
+@command{grub2-mkconfig}.  It is sourced by a shell script, and so must be
  valid POSIX shell input; normally, it will just be a sequence of
  @samp{KEY=value} lines, but if the value contains spaces or other special
  characters then it must be quoted.  For example:
@@ -1323,7 +1330,7 @@ works it's not recommended since titles often contain unstable device names
  and may be translated
 
  If you set this to @samp{saved}, then the default menu entry will be that
-saved by @samp{GRUB_SAVEDEFAULT} or @command{grub-set-default}.  This relies on
+saved by @samp{GRUB_SAVEDEFAULT} or @command{grub2-set-default}.  This relies on
  the environment block, which may not be available in all situations
  (@pxref{Environment block}).
 
@@ -1334,7 +1341,7 @@ If this option is set to @samp{true}, then, when an entry is selected, save
  it as a new default entry for use by future runs of GRUB.  This is only
  useful if @samp{GRUB_DEFAULT=saved}; it is a separate option because
  @samp{GRUB_DEFAULT=saved} is useful without this option, in conjunction with
-@command{grub-set-default}.  Unset by default.
+@command{grub2-set-default}.  Unset by default.
  This option relies on the environment block, which may not be available in
  all situations (@pxref{Environment block}).
 
@@ -1481,15 +1488,15 @@ intel-uc.img intel-ucode.img amd-uc.img amd-ucode.img early_ucode.cpio microcode
  @end example
 
  @item GRUB_DISABLE_LINUX_UUID
-Normally, @command{grub-mkconfig} will generate menu entries that use
+Normally, @command{grub2-mkconfig} will generate menu entries that use
  universally-unique identifiers (UUIDs) to identify the root filesystem to
  the Linux kernel, using a @samp{root=UUID=...} kernel parameter.  This is
  usually more reliable, but in some cases it may not be appropriate.  To
  disable the use of UUIDs, set this option to @samp{true}.
 
  @item GRUB_DISABLE_LINUX_PARTUUID
-If @command{grub-mkconfig} cannot identify the root filesystem via its
-universally-unique indentifier (UUID), @command{grub-mkconfig} can use the UUID
+If @command{grub2-mkconfig} cannot identify the root filesystem via its
+universally-unique indentifier (UUID), @command{grub2-mkconfig} can use the UUID
  of the partition containing the filesystem to identify the root filesystem to
  the Linux kernel via a @samp{root=PARTUUID=...} kernel parameter.  This is not
  as reliable as using the filesystem UUID, but is more reliable than using the
@@ -1503,7 +1510,7 @@ If this option is set to @samp{true}, disable the generation of recovery
  mode menu entries.
 
  @item GRUB_DISABLE_UUID
-Normally, @command{grub-mkconfig} will generate menu entries that use
+Normally, @command{grub2-mkconfig} will generate menu entries that use
  universally-unique identifiers (UUIDs) to identify various filesystems to
  search for files.  This is usually more reliable, but in some cases it may
  not be appropriate.  To disable this use of UUIDs, set this option to
@@ -1514,12 +1521,12 @@ not be appropriate.  To disable this use of UUIDs, set this option to
  @item GRUB_VIDEO_BACKEND
  If graphical video support is required, either because the @samp{gfxterm}
  graphical terminal is in use or because @samp{GRUB_GFXPAYLOAD_LINUX} is set,
-then @command{grub-mkconfig} will normally load all available GRUB video
+then @command{grub2-mkconfig} will normally load all available GRUB video
  drivers and use the one most appropriate for your hardware.  If you need to
  override this for some reason, then you can set this option.
 
-After @command{grub-install} has been run, the available video drivers are
-listed in @file{/boot/grub/video.lst}.
+After @command{grub2-install} has been run, the available video drivers are
+listed in @file{/boot/grub2/video.lst}.
 
  @item GRUB_GFXMODE
  Set the resolution used on the @samp{gfxterm} graphical terminal.  Note that
@@ -1552,20 +1559,20 @@ boot sequence.  If you have problems, set this option to @samp{text} and
  GRUB will tell Linux to boot in normal text mode.
 
  @item GRUB_DISABLE_OS_PROBER
-The @command{grub-mkconfig} has a feature to use the external
+The @command{grub2-mkconfig} has a feature to use the external
  @command{os-prober} program to discover other operating systems installed on
  the same machine and generate appropriate menu entries for them. It is disabled
  by default since automatic and silent execution of @command{os-prober}, and
  creating boot entries based on that data, is a potential attack vector. Set
  this option to @samp{false} to enable this feature in the
-@command{grub-mkconfig} command.
+@command{grub2-mkconfig} command.
 
  @item GRUB_OS_PROBER_SKIP_LIST
  List of space-separated FS UUIDs of filesystems to be ignored from os-prober
  output. For efi chainloaders it's <UUID>@@<EFI FILE>
 
  @item GRUB_DISABLE_SUBMENU
-Normally, @command{grub-mkconfig} will generate top level menu entry for
+Normally, @command{grub2-mkconfig} will generate top level menu entry for
  the kernel with highest version number and put all other found kernels
  or alternative menu entries for recovery mode in submenu. For entries returned
  by @command{os-prober} first entry will be put on top level and all others
@@ -1573,11 +1580,11 @@ in submenu. If this option is set to @samp{true}, flat menu with all entries
  on top level will be generated instead. Changing this option will require
  changing existing values of @samp{GRUB_DEFAULT}, @samp{fallback} (@pxref{fallback})
  and @samp{default} (@pxref{default}) environment variables as well as saved
-default entry using @command{grub-set-default} and value used with
-@command{grub-reboot}.
+default entry using @command{grub2-set-default} and value used with
+@command{grub2-reboot}.
 
  @item GRUB_ENABLE_CRYPTODISK
-If set to @samp{y}, @command{grub-mkconfig} and @command{grub-install} will
+If set to @samp{y}, @command{grub2-mkconfig} and @command{grub2-install} will
  check for encrypted disks and generate additional commands needed to access
  them during boot.  Note that in this case unattended boot is not possible
  because GRUB will wait for passphrase to unlock encrypted container.
@@ -1636,7 +1643,7 @@ confusing @samp{GRUB_TIMEOUT_STYLE=countdown} or
 
  @end table
 
-For more detailed customisation of @command{grub-mkconfig}'s output, you may
+For more detailed customisation of @command{grub2-mkconfig}'s output, you may
  edit the scripts in @file{/etc/grub.d} directly.
  @file{/etc/grub.d/40_custom} is particularly useful for adding entire custom
  menu entries; simply type the menu entries you want to add at the end of
@@ -1644,14 +1651,14 @@ that file, making sure to leave at least the first two lines intact.
 
  @node Root Identifcation Heuristics
  @section Root Identifcation Heuristics
-If the target operating system uses the Linux kernel, @command{grub-mkconfig}
+If the target operating system uses the Linux kernel, @command{grub2-mkconfig}
  attempts to identify the root file system via a heuristic algoirthm.  This
  algorithm selects the identification method of the root file system by
  considering three factors.  The first is if an initrd for the target operating
  system is also present.  The second is @samp{GRUB_DISABLE_LINUX_UUID} and if set
-to @samp{true}, prevents @command{grub-mkconfig} from identifying the root file
+to @samp{true}, prevents @command{grub2-mkconfig} from identifying the root file
  system by its UUID.  The third is @samp{GRUB_DISABLE_LINUX_PARTUUID} and if set
-to @samp{true}, prevents @command{grub-mkconfig} from identifying the root file
+to @samp{true}, prevents @command{grub2-mkconfig} from identifying the root file
  system via the UUID of its enclosing partition.  If the variables are assigned
  any other value, that value is considered equivalent to @samp{false}.  The
  variables are also considered to be set to @samp{false} if they are not set.
@@ -1661,18 +1668,18 @@ filesystem to the initrd.  Most initrd images determine the root file system by
  checking the Linux kernel's command-line for the @samp{root} key and use its
  value as the identification method of the root file system.  To improve the
  reliability of booting, most initrd images also allow the root file system to be
-identified by its UUID.  Because of this behavior, the @command{grub-mkconfig}
+identified by its UUID.  Because of this behavior, the @command{grub2-mkconfig}
  command will set @samp{root} to @samp{root=UUID=...} to provide the initrd with
  the filesystem UUID of the root file system.
 
  If no initrd is detected or @samp{GRUB_DISABLE_LINUX_UUID} is set to @samp{true}
-then @command{grub-command} will identify the root filesystem by setting the
+then @command{grub2-command} will identify the root filesystem by setting the
  kernel command-line variable @samp{root} to @samp{root=PARTUUID=...} unless
  @samp{GRUB_DISABLE_LINUX_PARTUUID} is also set to @samp{true}.  If
  @samp{GRUB_DISABLE_LINUX_PARTUUID} is also set to @samp{true},
-@command{grub-command} will identify by its Linux device name.
+@command{grub2-command} will identify by its Linux device name.
 
-The following table summarizes the behavior of the @command{grub-mkconfig}
+The following table summarizes the behavior of the @command{grub2-mkconfig}
  command.
 
  @multitable {detected} {GRUB_DISABLE_LINUX_PARTUUID} {GRUB_DISABLE_LINUX_UUID} {Linux Root}
@@ -1900,7 +1907,7 @@ images as well.
  Mount this partition on/mnt/boot and disable GRUB in all OSes and manually
  install self-compiled latest GRUB with:
 
-@code{grub-install --boot-directory=/mnt/boot /dev/sda}
+@code{grub2-install --boot-directory=/mnt/boot /dev/sda}
 
  In all the OSes install GRUB tools but disable installing GRUB in bootsector,
  so you'll have menu.lst and grub.cfg available for use. Also disable os-prober
@@ -1910,20 +1917,20 @@ use by setting:
 
  in /etc/default/grub
 
-Then write a grub.cfg (/mnt/boot/grub/grub.cfg):
+Then write a grub.cfg (/mnt/boot/grub2/grub.cfg):
 
  @example
 
  menuentry "OS using grub2" @{
     insmod xfs
     search --set=root --label OS1 --hint hd0,msdos8
-   configfile /boot/grub/grub.cfg
+   configfile /boot/grub2/grub.cfg
  @}
 
  menuentry "OS using grub2-legacy" @{
     insmod ext2
     search --set=root --label OS2 --hint hd0,msdos6
-   legacy_configfile /boot/grub/menu.lst
+   legacy_configfile /boot/grub2/menu.lst
  @}
 
  menuentry "Windows XP" @{
@@ -1986,15 +1993,15 @@ GRUB supports embedding a configuration file directly into the core image,
  so that it is loaded before entering normal mode.  This is useful, for
  example, when it is not straightforward to find the real configuration file,
  or when you need to debug problems with loading that file.
-@command{grub-install} uses this feature when it is not using BIOS disk
+@command{grub2-install} uses this feature when it is not using BIOS disk
  functions or when installing to a different disk from the one containing
  @file{/boot/grub}, in which case it needs to use the @command{search}
  command (@pxref{search}) to find @file{/boot/grub}.
 
  To embed a configuration file, use the @option{-c} option to
-@command{grub-mkimage}.  The file is copied into the core image, so it may
+@command{grub2-mkimage}.  The file is copied into the core image, so it may
  reside anywhere on the file system, and may be removed after running
-@command{grub-mkimage}.
+@command{grub2-mkimage}.
 
  After the embedded configuration file (if any) is executed, GRUB will load
  the @samp{normal} module (@pxref{normal}), which will then read the real
@@ -2029,13 +2036,13 @@ included in the core image:
  @example
  @group
  search.fs_label grub root
-if [ -e /boot/grub/example/test1.cfg ]; then
+if [ -e /boot/grub2/example/test1.cfg ]; then
      set prefix=($root)/boot/grub
-    configfile /boot/grub/example/test1.cfg
+    configfile /boot/grub2/example/test1.cfg
  else
-    if [ -e /boot/grub/example/test2.cfg ]; then
+    if [ -e /boot/grub2/example/test2.cfg ]; then
          set prefix=($root)/boot/grub
-        configfile /boot/grub/example/test2.cfg
+        configfile /boot/grub2/example/test2.cfg
      else
          echo "Could not find an example configuration file!"
      fi
@@ -2559,7 +2566,7 @@ grub-mknetdir --net-directory=/srv/tftp --subdir=/boot/grub -d /usr/lib/grub/i38
  @end group
  @end example
 
-Then follow instructions printed out by grub-mknetdir on configuring your DHCP
+Then follow instructions printed out by grub2-mknetdir on configuring your DHCP
  server.
 
  The grub.cfg file is placed in the same directory as the path output by
@@ -2762,7 +2769,7 @@ team are:
  @end table
 
  To take full advantage of this function, install GRUB into the MBR
-(@pxref{Installing GRUB using grub-install}).
+(@pxref{Installing GRUB using grub2-install}).
 
  If you have a laptop which has a similar feature and not in the above list
  could you figure your address and contribute?
@@ -2823,7 +2830,7 @@ bytes.
  The sole function of @file{boot.img} is to read the first sector of the core
  image from a local disk and jump to it.  Because of the size restriction,
  @file{boot.img} cannot understand any file system structure, so
-@command{grub-install} hardcodes the location of the first sector of the
+@command{grub2-install} hardcodes the location of the first sector of the
  core image into @file{boot.img} when installing GRUB.
 
  @item diskboot.img
@@ -2853,7 +2860,7 @@ images.
 
  @item core.img
  This is the core image of GRUB.  It is built dynamically from the kernel
-image and an arbitrary list of modules by the @command{grub-mkimage}
+image and an arbitrary list of modules by the @command{grub2-mkimage}
  program.  Usually, it contains enough modules to access @file{/boot/grub},
  and loads everything else (including menu handling, the ability to load
  target operating systems, and so on) from the file system at run-time.  The
@@ -2905,7 +2912,7 @@ GRUB 2 has no single Stage 2 image.  Instead, it loads modules from
  In GRUB 2, images for booting from CD-ROM drives are now constructed using
  @file{cdboot.img} and @file{core.img}, making sure that the core image
  contains the @samp{iso9660} module.  It is usually best to use the
-@command{grub-mkrescue} program for this.
+@command{grub2-mkrescue} program for this.
 
  @item nbgrub
  There is as yet no equivalent for @file{nbgrub} in GRUB 2; it was used by
@@ -3073,8 +3080,8 @@ There are two ways to specify files, by @dfn{absolute file name} and by
 
  An absolute file name resembles a Unix absolute file name, using
  @samp{/} for the directory separator (not @samp{\} as in DOS). One
-example is @samp{(hd0,1)/boot/grub/grub.cfg}. This means the file
-@file{/boot/grub/grub.cfg} in the first partition of the first hard
+example is @samp{(hd0,1)/boot/grub2/grub.cfg}. This means the file
+@file{/boot/grub2/grub.cfg} in the first partition of the first hard
  disk. If you omit the device name in an absolute file name, GRUB uses
  GRUB's @dfn{root device} implicitly. So if you set the root device to,
  say, @samp{(hd1,1)} by the command @samp{set root=(hd1,1)} (@pxref{set}),
@@ -3082,8 +3089,8 @@ then @code{/boot/kernel} is the same as @code{(hd1,1)/boot/kernel}.
 
  On ZFS filesystem the first path component must be
  @var{volume}@samp{@@}[@var{snapshot}].
-So @samp{/rootvol@@snap-129/boot/grub/grub.cfg} refers to file
-@samp{/boot/grub/grub.cfg} in snapshot of volume @samp{rootvol} with name
+So @samp{/rootvol@@snap-129/boot/grub2/grub.cfg} refers to file
+@samp{/boot/grub2/grub.cfg} in snapshot of volume @samp{rootvol} with name
  @samp{snap-129}.  Trailing @samp{@@} after volume name is mandatory even if
  snapshot name is omitted.
 
@@ -3502,7 +3509,7 @@ The more recent release of Minix would then be identified as
  @samp{other>minix>minix-3.4.0}.
 
  This variable is often set by @samp{GRUB_DEFAULT} (@pxref{Simple
-configuration}), @command{grub-set-default}, or @command{grub-reboot}.
+configuration}), @command{grub2-set-default}, or @command{grub2-reboot}.
 
 
  @node fallback
@@ -3592,7 +3599,7 @@ If this variable is set, it names the language code that the
  example, French would be named as @samp{fr}, and Simplified Chinese as
  @samp{zh_CN}.
 
-@command{grub-mkconfig} (@pxref{Simple configuration}) will try to set a
+@command{grub2-mkconfig} (@pxref{Simple configuration}) will try to set a
  reasonable default for this variable based on the system locale.
 
 
@@ -3600,10 +3607,10 @@ reasonable default for this variable based on the system locale.
  @subsection locale_dir
 
  If this variable is set, it names the directory where translation files may
-be found (@pxref{gettext}), usually @file{/boot/grub/locale}.  Otherwise,
+be found (@pxref{gettext}), usually @file{/boot/grub2/locale}.  Otherwise,
  internationalization is disabled.
 
-@command{grub-mkconfig} (@pxref{Simple configuration}) will set a reasonable
+@command{grub2-mkconfig} (@pxref{Simple configuration}) will set a reasonable
  default for this variable if internationalization is needed and any
  translation files are available.
 
@@ -3740,7 +3747,7 @@ input.  The default is not to pause output.
 
  The location of the @samp{/boot/grub} directory as an absolute file name
  (@pxref{File name syntax}).  This is normally set by GRUB at startup based
-on information provided by @command{grub-install}.  GRUB modules are
+on information provided by @command{grub2-install}.  GRUB modules are
  dynamically loaded from this directory, so it must be set correctly in order
  for many parts of GRUB to work.
 
@@ -3852,17 +3859,17 @@ GRUB provides an ``environment block'' which can be used to save a small
  amount of state.
 
  The environment block is a preallocated 1024-byte file, which normally lives
-in @file{/boot/grub/grubenv} (although you should not assume this).  At boot
+in @file{/boot/grub2/grubenv} (although you should not assume this).  At boot
  time, the @command{load_env} command (@pxref{load_env}) loads environment
  variables from it, and the @command{save_env} (@pxref{save_env}) command
  saves environment variables to it.  From a running system, the
-@command{grub-editenv} utility can be used to edit the environment block.
+@command{grub2-editenv} utility can be used to edit the environment block.
 
  For safety reasons, this storage is only available when installed on a plain
  disk (no LVM or RAID), using a non-checksumming filesystem (no ZFS), and
  using BIOS or EFI functions (no ATA, USB or IEEE1275).
 
-@command{grub-mkconfig} uses this facility to implement
+@command{grub2-mkconfig} uses this facility to implement
  @samp{GRUB_SAVEDEFAULT} (@pxref{Simple configuration}).
 
  @node Modules
@@ -4924,7 +4931,7 @@ Translate @var{string} into the current language.
 
  The current language code is stored in the @samp{lang} variable in GRUB's
  environment (@pxref{lang}).  Translation files in MO format are read from
-@samp{locale_dir} (@pxref{locale_dir}), usually @file{/boot/grub/locale}.
+@samp{locale_dir} (@pxref{locale_dir}), usually @file{/boot/grub2/locale}.
  @end deffn
 
 
@@ -5276,7 +5283,7 @@ Define a user named @var{user} with password @var{clear-password}.
 
  @deffn Command password_pbkdf2 user hashed-password
  Define a user named @var{user} with password hash @var{hashed-password}.
-Use @command{grub-mkpasswd-pbkdf2} (@pxref{Invoking grub-mkpasswd-pbkdf2})
+Use @command{grub2-mkpasswd-pbkdf2} (@pxref{Invoking grub2-mkpasswd-pbkdf2})
  to generate password hashes.  @xref{Security}.
  @end deffn
 
@@ -6402,8 +6409,8 @@ The @samp{password} (@pxref{password}) and @samp{password_pbkdf2}
  which has an associated password.  @samp{password} sets the password in
  plain text, requiring @file{grub.cfg} to be secure; @samp{password_pbkdf2}
  sets the password hashed using the Password-Based Key Derivation Function
-(RFC 2898), requiring the use of @command{grub-mkpasswd-pbkdf2}
-(@pxref{Invoking grub-mkpasswd-pbkdf2}) to generate password hashes.
+(RFC 2898), requiring the use of @command{grub2-mkpasswd-pbkdf2}
+(@pxref{Invoking grub2-mkpasswd-pbkdf2}) to generate password hashes.
 
  In order to enable authentication support, the @samp{superusers} environment
  variable must be set to a list of usernames, separated by any of spaces,
@@ -6448,7 +6455,7 @@ menuentry "May be run by user1 or a superuser" --users user1 @{
  @end group
  @end example
 
-The @command{grub-mkconfig} program does not yet have built-in support for
+The @command{grub2-mkconfig} program does not yet have built-in support for
  generating configuration files with authentication.  You can use
  @file{/etc/grub.d/40_custom} to add simple superuser authentication, by
  adding @kbd{set superusers=} and @kbd{password} or @kbd{password_pbkdf2}
@@ -6474,8 +6481,18 @@ validation fails, then file @file{foo} cannot be opened.  This failure
  may halt or otherwise impact the boot process.
 
  An initial trusted public key can be embedded within the GRUB @file{core.img}
-using the @code{--pubkey} option to @command{grub-install}
-(@pxref{Invoking grub-install}).
+using the @code{--pubkey} option to @command{grub2-install}
+(@pxref{Invoking grub2-install}).
+
+@comment Unfortunately --pubkey is not yet supported by grub2-install,
+@comment but we should not bring up internal detail grub2-mkimage here
+@comment in the user guide (as opposed to developer's manual).
+
+@comment An initial trusted public key can be embedded within the GRUB
+@comment @file{core.img} using the @code{--pubkey} option to
+@comment @command{grub2-mkimage} (@pxref{Invoking grub2-install}).  Presently it
+@comment is necessary to write a custom wrapper around @command{grub2-mkimage}
+@comment using the @code{--grub-mkimage} flag to @command{grub2-install}.
 
  GRUB uses GPG-style detached signatures (meaning that a file
  @file{foo.sig} will be produced when file @file{foo} is signed), and
@@ -6495,8 +6512,8 @@ gpg --detach-sign /path/to/file
  For successful validation of all of GRUB's subcomponents and the
  loaded OS kernel, they must all be signed.  One way to accomplish this
  is the following (after having already produced the desired
-@file{grub.cfg} file, e.g., by running @command{grub-mkconfig}
-(@pxref{Invoking grub-mkconfig}):
+@file{grub.cfg} file, e.g., by running @command{grub2-mkconfig}
+(@pxref{Invoking grub2-mkconfig}):
 
  @example
  @group
@@ -6518,7 +6535,7 @@ See also: @ref{check_signatures}, @ref{verify_detached}, @ref{trust},
  Note that internally signature enforcement is controlled by setting
  the environment variable @code{check_signatures} equal to
  @code{enforce}.  Passing one or more @code{--pubkey} options to
-@command{grub-mkimage} implicitly defines @code{check_signatures}
+@command{grub2-mkimage} implicitly defines @code{check_signatures}
  equal to @code{enforce} in @file{core.img} prior to processing any
  configuration files.
 
@@ -6566,7 +6583,7 @@ UTF-8 strings as comma-separated values (CSV). See
  @uref{https://urldefense.com/v3/__https://github.com/rhboot/shim/blob/main/SBAT.md__;!!ACWV5N9M2RV99hQ!L0Qe1g1igyWH7sbVcazLtuN-1x6512lNfSNE40pc-B-zv3i8A3s2rADpr_pwb3Y-685v7NMgYq4fDOpxYOmh$ } for more details.
 
  To add a data section containing the SBAT information into the binary, the
-@option{--sbat} option of @command{grub-mkimage} command should be used. The content
+@option{--sbat} option of @command{grub2-mkimage} command should be used. The content
  of a CSV file, encoded with UTF-8, is copied as is to the .sbat data section into
  the generated EFI binary. The CSV file can be stored anywhere on the file system.
 
@@ -6977,10 +6994,10 @@ Required files are:
 
  GRUB's normal start-up procedure involves setting the @samp{prefix}
  environment variable to a value set in the core image by
-@command{grub-install}, setting the @samp{root} variable to match, loading
+@command{grub2-install}, setting the @samp{root} variable to match, loading
  the @samp{normal} module from the prefix, and running the @samp{normal}
  command (@pxref{normal}).  This command is responsible for reading
-@file{/boot/grub/grub.cfg}, running the menu, and doing all the useful
+@file{/boot/grub2/grub.cfg}, running the menu, and doing all the useful
  things GRUB is supposed to do.
 
  If, instead, you only get a rescue shell, this usually means that GRUB
@@ -7006,8 +7023,8 @@ normal
 
  However, any problem that leaves you in the rescue shell probably means that
  GRUB was not correctly installed.  It may be more useful to try to reinstall
-it properly using @kbd{grub-install @var{device}} (@pxref{Invoking
-grub-install}).  When doing this, there are a few things to remember:
+it properly using @kbd{grub2-install @var{device}} (@pxref{Invoking
+grub2-install}).  When doing this, there are a few things to remember:
 
  @itemize @bullet{}
  @item
@@ -7019,7 +7036,7 @@ is usually better to use UUIDs or file system labels and avoid depending on
  drive ordering entirely.
 
  @item
-At least on BIOS systems, if you tell @command{grub-install} to install GRUB
+At least on BIOS systems, if you tell @command{grub2-install} to install GRUB
  to a partition but GRUB has already been installed in the master boot
  record, then the GRUB installation in the partition will be ignored.
 
@@ -7054,33 +7071,33 @@ bootability on other machines.
  @chapter User-space utilities
 
  @menu
-* Invoking grub-install::       How to use the GRUB installer
-* Invoking grub-mkconfig::      Generate a GRUB configuration file
-* Invoking grub-mkpasswd-pbkdf2::
+* Invoking grub2-install::       How to use the GRUB installer
+* Invoking grub2-mkconfig::      Generate a GRUB configuration file
+* Invoking grub2-mkpasswd-pbkdf2::
                                  Generate GRUB password hashes
-* Invoking grub-mkrelpath::     Make system path relative to its root
-* Invoking grub-mkrescue::      Make a GRUB rescue image
-* Invoking grub-mount::         Mount a file system using GRUB
-* Invoking grub-probe::         Probe device information for GRUB
-* Invoking grub-script-check::  Check GRUB script file for syntax errors
+* Invoking grub2-mkrelpath::     Make system path relative to its root
+* Invoking grub2-mkrescue::      Make a GRUB rescue image
+* Invoking grub2-mount::         Mount a file system using GRUB
+* Invoking grub2-probe::         Probe device information for GRUB
+* Invoking grub2-script-check::  Check GRUB script file for syntax errors
  @end menu
 
 
-@node Invoking grub-install
-@section Invoking grub-install
+@node Invoking grub2-install
+@section Invoking grub2-install
 
-The program @command{grub-install} generates a GRUB core image using
-@command{grub-mkimage} and installs it on your system.  You must specify the
+The program @command{grub2-install} generates a GRUB core image using
+@command{grub2-mkimage} and installs it on your system.  You must specify the
  device name on which you want to install GRUB, like this:
 
  @example
-grub-install @var{install_device}
+grub2-install @var{install_device}
  @end example
 
  The device name @var{install_device} is an OS device name or a GRUB
  device name.
 
-@command{grub-install} accepts the following options:
+@command{grub2-install} accepts the following options:
 
  @table @option
  @item --help
@@ -7096,13 +7113,13 @@ separate partition or a removable disk.
  If this option is not specified then it defaults to @file{/boot}, so
 
  @example
-@kbd{grub-install /dev/sda}
+@kbd{grub2-install /dev/sda}
  @end example
 
  is equivalent to
 
  @example
-@kbd{grub-install --boot-directory=/boot/ /dev/sda}
+@kbd{grub2-install --boot-directory=/boot/ /dev/sda}
  @end example
 
  Here is an example in which you have a separate @dfn{boot} partition which is
@@ -7110,16 +7127,16 @@ mounted on
  @file{/mnt/boot}:
 
  @example
-@kbd{grub-install --boot-directory=/mnt/boot /dev/sdb}
+@kbd{grub2-install --boot-directory=/mnt/boot /dev/sdb}
  @end example
 
  @item --recheck
-Recheck the device map, even if @file{/boot/grub/device.map} already
+Recheck the device map, even if @file{/boot/grub2/device.map} already
  exists. You should use this option whenever you add/remove a disk
  into/from your computer.
 
  @item --no-rs-codes
-By default on x86 BIOS systems, @command{grub-install} will use some
+By default on x86 BIOS systems, @command{grub2-install} will use some
  extra space in the bootloader embedding area for Reed-Solomon
  error-correcting codes. This enables GRUB to still boot successfully
  if some blocks are corrupted.  The exact amount of protection offered
@@ -7132,17 +7149,17 @@ installation}) where GRUB does not reside in any unpartitioned space
  outside of the MBR.  Disable the Reed-Solomon codes with this option.
  @end table
 
-@node Invoking grub-mkconfig
-@section Invoking grub-mkconfig
+@node Invoking grub2-mkconfig
+@section Invoking grub2-mkconfig
 
-The program @command{grub-mkconfig} generates a configuration file for GRUB
+The program @command{grub2-mkconfig} generates a configuration file for GRUB
  (@pxref{Simple configuration}).
 
  @example
-grub-mkconfig -o /boot/grub/grub.cfg
+grub2-mkconfig -o /boot/grub2/grub.cfg
  @end example
 
-@command{grub-mkconfig} accepts the following options:
+@command{grub2-mkconfig} accepts the following options:
 
  @table @option
  @item --help
@@ -7158,17 +7175,17 @@ it to standard output.
  @end table
 
 
-@node Invoking grub-mkpasswd-pbkdf2
-@section Invoking grub-mkpasswd-pbkdf2
+@node Invoking grub2-mkpasswd-pbkdf2
+@section Invoking grub2-mkpasswd-pbkdf2
 
-The program @command{grub-mkpasswd-pbkdf2} generates password hashes for
+The program @command{grub2-mkpasswd-pbkdf2} generates password hashes for
  GRUB (@pxref{Security}).
 
  @example
-grub-mkpasswd-pbkdf2
+grub2-mkpasswd-pbkdf2
  @end example
 
-@command{grub-mkpasswd-pbkdf2} accepts the following options:
+@command{grub2-mkpasswd-pbkdf2} accepts the following options:
 
  @table @option
  @item -c @var{number}
@@ -7186,23 +7203,23 @@ Length of the salt.  Defaults to 64.
  @end table
 
 
-@node Invoking grub-mkrelpath
-@section Invoking grub-mkrelpath
+@node Invoking grub2-mkrelpath
+@section Invoking grub2-mkrelpath
 
-The program @command{grub-mkrelpath} makes a file system path relative to
+The program @command{grub2-mkrelpath} makes a file system path relative to
  the root of its containing file system.  For instance, if @file{/usr} is a
  mount point, then:
 
  @example
-$ @kbd{grub-mkrelpath /usr/share/grub/unicode.pf2}
+$ @kbd{grub2-mkrelpath /usr/share/grub/unicode.pf2}
  @samp{/share/grub/unicode.pf2}
  @end example
 
  This is mainly used internally by other GRUB utilities such as
-@command{grub-mkconfig} (@pxref{Invoking grub-mkconfig}), but may
+@command{grub2-mkconfig} (@pxref{Invoking grub2-mkconfig}), but may
  occasionally also be useful for debugging.
 
-@command{grub-mkrelpath} accepts the following options:
+@command{grub2-mkrelpath} accepts the following options:
 
  @table @option
  @item --help
@@ -7213,17 +7230,17 @@ Print the version number of GRUB and exit.
  @end table
 
 
-@node Invoking grub-mkrescue
-@section Invoking grub-mkrescue
+@node Invoking grub2-mkrescue
+@section Invoking grub2-mkrescue
 
-The program @command{grub-mkrescue} generates a bootable GRUB rescue image
+The program @command{grub2-mkrescue} generates a bootable GRUB rescue image
  (@pxref{Making a GRUB bootable CD-ROM}).
 
  @example
-grub-mkrescue -o grub.iso
+grub2-mkrescue -o grub.iso
  @end example
 
-All arguments not explicitly listed as @command{grub-mkrescue} options are
+All arguments not explicitly listed as @command{grub2-mkrescue} options are
  passed on directly to @command{xorriso} in @command{mkisofs} emulation mode.
  Options passed to @command{xorriso} will normally be interpreted as
  @command{mkisofs} options; if the option @samp{--} is used, then anything
@@ -7235,10 +7252,10 @@ commonly used to add extra files to the image:
  @example
  mkdir -p disk/boot/grub
  @r{(add extra files to @file{disk/boot/grub})}
-grub-mkrescue -o grub.iso disk
+grub2-mkrescue -o grub.iso disk
  @end example
 
-@command{grub-mkrescue} accepts the following options:
+@command{grub2-mkrescue} accepts the following options:
 
  @table @option
  @item --help
@@ -7266,15 +7283,15 @@ Use @var{file} as the @command{xorriso} program, rather than the built-in
  default.
 
  @item --grub-mkimage=@var{file}
-Use @var{file} as the @command{grub-mkimage} program, rather than the
+Use @var{file} as the @command{grub2-mkimage} program, rather than the
  built-in default.
  @end table
 
 
-@node Invoking grub-mount
-@section Invoking grub-mount
+@node Invoking grub2-mount
+@section Invoking grub2-mount
 
-The program @command{grub-mount} performs a read-only mount of any file
+The program @command{grub2-mount} performs a read-only mount of any file
  system or file system image that GRUB understands, using GRUB's file system
  drivers via FUSE.  (It is only available if FUSE development files were
  present when GRUB was built.)  This has a number of uses:
@@ -7306,13 +7323,13 @@ even if nobody has yet written a FUSE module specifically for that file
  system type.
  @end itemize
 
-Using @command{grub-mount} is normally as simple as:
+Using @command{grub2-mount} is normally as simple as:
 
  @example
-grub-mount /dev/sda1 /mnt
+grub2-mount /dev/sda1 /mnt
  @end example
 
-@command{grub-mount} must be given one or more images and a mount point as
+@command{grub2-mount} must be given one or more images and a mount point as
  non-option arguments (if it is given more than one image, it will treat them
  as a RAID set), and also accepts the following options:
 
@@ -7334,13 +7351,13 @@ Show debugging output for conditions matching @var{string}.
  @item -K prompt|@var{file}
  @itemx --zfs-key=prompt|@var{file}
  Load a ZFS encryption key.  If you use @samp{prompt} as the argument,
-@command{grub-mount} will read a passphrase from the terminal; otherwise, it
+@command{grub2-mount} will read a passphrase from the terminal; otherwise, it
  will read key material from the specified file.
 
  @item -r @var{device}
  @itemx --root=@var{device}
  Set the GRUB root device to @var{device}.  You do not normally need to set
-this; @command{grub-mount} will automatically set the root device to the
+this; @command{grub2-mount} will automatically set the root device to the
  root of the supplied file system.
 
  If @var{device} is just a number, then it will be treated as a partition
@@ -7349,7 +7366,7 @@ an entire disk in @file{disk.img}, then you can use this command to mount
  its second partition:
 
  @example
-grub-mount -r 2 disk.img mount-point
+grub2-mount -r 2 disk.img mount-point
  @end example
 
  @item -v
@@ -7358,18 +7375,18 @@ Print verbose messages.
  @end table
 
 
-@node Invoking grub-probe
-@section Invoking grub-probe
+@node Invoking grub2-probe
+@section Invoking grub2-probe
 
-The program @command{grub-probe} probes device information for a given path
+The program @command{grub2-probe} probes device information for a given path
  or device.
 
  @example
-grub-probe --target=fs /boot/grub
-grub-probe --target=drive --device /dev/sda1
+grub2-probe --target=fs /boot/grub
+grub2-probe --target=drive --device /dev/sda1
  @end example
 
-@command{grub-probe} must be given a path or device as a non-option
+@command{grub2-probe} must be given a path or device as a non-option
  argument, and also accepts the following options:
 
  @table @option
@@ -7382,16 +7399,16 @@ Print the version number of GRUB and exit.
  @item -d
  @itemx --device
  If this option is given, then the non-option argument is a system device
-name (such as @samp{/dev/sda1}), and @command{grub-probe} will print
+name (such as @samp{/dev/sda1}), and @command{grub2-probe} will print
  information about that device.  If it is not given, then the non-option
  argument is a filesystem path (such as @samp{/boot/grub}), and
-@command{grub-probe} will print information about the device containing that
+@command{grub2-probe} will print information about the device containing that
  part of the filesystem.
 
  @item -m @var{file}
  @itemx --device-map=@var{file}
  Use @var{file} as the device map (@pxref{Device map}) rather than the
-default, usually @samp{/boot/grub/device.map}.
+default, usually @samp{/boot/grub2/device.map}.
 
  @item -t @var{target}
  @itemx --target=@var{target}
@@ -7444,19 +7461,19 @@ Print verbose messages.
  @end table
 
 
-@node Invoking grub-script-check
-@section Invoking grub-script-check
+@node Invoking grub2-script-check
+@section Invoking grub2-script-check
 
-The program @command{grub-script-check} takes a GRUB script file
+The program @command{grub2-script-check} takes a GRUB script file
  (@pxref{Shell-like scripting}) and checks it for syntax errors, similar to
  commands such as @command{sh -n}.  It may take a @var{path} as a non-option
  argument; if none is supplied, it will read from standard input.
 
  @example
-grub-script-check /boot/grub/grub.cfg
+grub2-script-check /boot/grub2/grub.cfg
  @end example
 
-@command{grub-script-check} accepts the following options:
+@command{grub2-script-check} accepts the following options:
 
  @table @option
  @item --help
grub-core/kern/misc.c +0 -0
index 465a8e74e..b1d399d37 100644

grub-core/kern/misc.c void __attribute__ ((noreturn)) · line 1309

Quoted 8 lines
grub-core/kern/misc.c +1 -1
@@ -1303,7 +1303,7 @@ void __attribute__ ((noreturn))
  grub_abort (void)
  {
  #ifndef GRUB_UTIL
-#if defined(__i386__) || defined(__x86_64__)
+#if (defined(__i386__) || defined(__x86_64__)) && !defined(GRUB_MACHINE_EMU)
    grub_backtrace();
  #endif
  #endif

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

#13 lore raw

Re: [PATCH v1 25/37] grub.texi: Make our info pages say "grub2" where appropriate.

In reply to Leo Sandoval

Upstream uses "grub" for these commands. Rename to "grub2" is distro-specific

Quoted 1,270 lines · Leo Sandoval

From: Peter Jones <[email protected]>

This needs to be hooked up to --program-transform=, but I haven't had time.

---
 docs/grub-dev.texi    |   4 +-
 docs/grub.texi        | 359 ++++++++++++++++++++++--------------------
 grub-core/kern/misc.c |   2 +-
 3 files changed, 191 insertions(+), 174 deletions(-)
docs/grub-dev.texi +2 -2
index 1276c5930..04c6678cb 100644
@@ -1,7 +1,7 @@
 \input texinfo
 @c -*-texinfo-*-
 @c %**start of header
-@setfilename grub-dev.info
+@setfilename grub2-dev.info
 @include version-dev.texi
 @settitle GNU GRUB Developers Manual @value{VERSION}
 @c Unify all our little indices for now.
@@ -32,7 +32,7 @@ Invariant Sections.
 
 @dircategory Kernel
 @direntry
-* grub-dev: (grub-dev).                 The GRand Unified Bootloader Dev
+* grub2-dev: (grub2-dev).                 The GRand Unified Bootloader Dev
 @end direntry
 
 @setchapternewpage odd
docs/grub.texi +1 -4
index a225f9a88..cecf14d55 100644
@@ -1,7 +1,7 @@
 \input texinfo
 @c -*-texinfo-*-
 @c %**start of header
-@setfilename grub.info
+@setfilename grub2.info
 @include version.texi
 @settitle GNU GRUB Manual @value{VERSION}
 @c Unify all our little indices for now.
@@ -32,15 +32,15 @@ Invariant Sections.
 
 @dircategory Kernel
 @direntry
-* GRUB: (grub).                 The GRand Unified Bootloader
-* grub-install: (grub)Invoking grub-install.    Install GRUB on your drive
-* grub-mkconfig: (grub)Invoking grub-mkconfig.  Generate GRUB

configuration

docs/grub.texi +0 -4
-* grub-mkpasswd-pbkdf2: (grub)Invoking grub-mkpasswd-pbkdf2.
-* grub-mkrelpath: (grub)Invoking grub-mkrelpath.
-* grub-mkrescue: (grub)Invoking grub-mkrescue.  Make a GRUB rescue image
-* grub-mount: (grub)Invoking grub-mount.        Mount a file system using

GRUB

docs/grub.texi +2 -2
-* grub-probe: (grub)Invoking grub-probe.        Probe device information
-* grub-script-check: (grub)Invoking grub-script-check.
+* GRUB2: (grub2).                 The GRand Unified Bootloader
+* grub2-install: (grub2)Invoking grub2-install.    Install GRUB on your

drive
+* grub2-mkconfig: (grub2)Invoking grub2-mkconfig. Generate GRUB configuration

docs/grub.texi +3 -0
+* grub2-mkpasswd-pbkdf2: (grub2)Invoking grub2-mkpasswd-pbkdf2.
+* grub2-mkrelpath: (grub2)Invoking grub2-mkrelpath.
+* grub2-mkrescue: (grub2)Invoking grub2-mkrescue.  Make a GRUB rescue

image
+* grub2-mount: (grub2)Invoking grub2-mount. Mount a file system
using GRUB
+* grub2-probe: (grub2)Invoking grub2-probe. Probe device
information

docs/grub.texi +2 -1
+* grub2-script-check: (grub2)Invoking grub2-script-check.
 @end direntry
 
 @setchapternewpage odd
@@ -223,7 +223,7 @@ surprising.
 
 @item
 @file{grub.cfg} is typically automatically generated by
-@command{grub-mkconfig} (@pxref{Simple configuration}).  This makes it
+@command{grub2-mkconfig} (@pxref{Simple configuration}).  This makes it
 easier to handle versioned kernel upgrades.
 
 @item
@@ -237,7 +237,7 @@ scripting language: variables, conditionals, and loops

are available.
@item
A small amount of persistent storage is available across reboots, using
the
@command{save_env} and @command{load_env} commands in GRUB and the
-@command{grub-editenv} utility. This is not available in all configurations
+@command{grub2-editenv} utility. This is not available in all configurations
(@pxref{Environment block}).

@item

docs/grub.texi +0 -0
@@ -542,7 +542,7 @@ On OS which have device nodes similar to Unix-like OS

GRUB tools use the
OS name. E.g. for GNU/Linux:

@example

docs/grub.texi +3 -3
-# @kbd{grub-install /dev/sda}
+# @kbd{grub2-install /dev/sda}
 @end example
 
 On AROS we use another syntax. For volumes:
@@ -565,7 +565,7 @@ For disks we use syntax:
 E.g.
 
 @example
-# @kbd{grub-install //:ata.device/0/0}
+# @kbd{grub2-install //:ata.device/0/0}
 @end example
 
 On Windows we use UNC path. For volumes it's typically
@@ -592,7 +592,7 @@ For disks it's
 E.g.
 
 @example
-# @kbd{grub-install \\?\PhysicalDrive0}
+# @kbd{grub2-install \\?\PhysicalDrive0}
 @end example
 
 Beware that you may need to further escape the backslashes depending on

your

docs/grub.texi +0 -0
@@ -602,7 +602,7 @@ When compiled with cygwin support then cygwin drive

names are automatically
when needed. E.g.

@example

docs/grub.texi +2 -2
-# @kbd{grub-install /dev/sda}
+# @kbd{grub2-install /dev/sda}
 @end example
 
 @node Installation
@@ -615,7 +615,7 @@ from the source tarball, or as a package for your OS.
 
 After you have done that, you need to install the boot loader on a
 drive (floppy or hard disk) by using the utility
-@command{grub-install} (@pxref{Invoking grub-install}) on a UNIX-like OS.
+@command{grub2-install} (@pxref{Invoking grub2-install}) on a UNIX-like

OS.

GRUB comes with boot images, which are normally put in the directory @file{/usr/lib/grub/<cpu>-<platform>} (for BIOS-based machines

docs/grub.texi +0 -0
@@ -626,22 +626,22 @@ loader needs to find them (usually @file{/boot})

will be called
the @dfn{boot directory}.

@menu

docs/grub.texi +5 -5
-* Installing GRUB using grub-install::
+* Installing GRUB using grub2-install::
 * Making a GRUB bootable CD-ROM::
 * Device map::
 * BIOS installation::
 @end menu
 
 
-@node Installing GRUB using grub-install
-@section Installing GRUB using grub-install
+@node Installing GRUB using grub2-install
+@section Installing GRUB using grub2-install
 
 For information on where GRUB should be installed on PC BIOS platforms,
 @pxref{BIOS installation}.
 
 In order to install GRUB under a UNIX-like OS (such
-as @sc{gnu}), invoke the program @command{grub-install} (@pxref{Invoking
-grub-install}) as the superuser (@dfn{root}).
+as @sc{gnu}), invoke the program @command{grub2-install} (@pxref{Invoking
+grub2-install}) as the superuser (@dfn{root}).
 
 The usage is basically very simple. You only need to specify one
 argument to the program, namely, where to install the boot loader. The
@@ -650,13 +650,13 @@ For example, under Linux the following will install

GRUB into the MBR
of the first IDE disk:

@example

docs/grub.texi +3 -3
-# @kbd{grub-install /dev/sda}
+# @kbd{grub2-install /dev/sda}
 @end example
 
 Likewise, under GNU/Hurd, this has the same effect:
 
 @example
-# @kbd{grub-install /dev/hd0}
+# @kbd{grub2-install /dev/hd0}
 @end example
 
 But all the above examples assume that GRUB should put images under
@@ -670,7 +670,7 @@ boot floppy with a filesystem. Here is an example:
 # @kbd{mke2fs /dev/fd0}
 # @kbd{mount -t ext2 /dev/fd0 /mnt}
 # @kbd{mkdir /mnt/boot}
-# @kbd{grub-install --boot-directory=/mnt/boot /dev/fd0}
+# @kbd{grub2-install --boot-directory=/mnt/boot /dev/fd0}
 # @kbd{umount /mnt}
 @end group
 @end example
@@ -682,30 +682,37 @@ floppy instead of exposing the USB drive as a hard

disk (they call it
@example
# @kbd{losetup /dev/loop0 /dev/sdb1}
# @kbd{mount /dev/loop0 /mnt/usb}

docs/grub.texi +3 -3
-# @kbd{grub-install --boot-directory=/mnt/usb/bugbios --force
--allow-floppy /dev/loop0}
+# @kbd{grub2-install --boot-directory=/mnt/usb/bugbios --force
--allow-floppy /dev/loop0}
 @end example
 
 This install doesn't conflict with standard install as long as they are in
 separate directories.
 
+Note that @command{grub2-install} is actually just a shell script and the
+real task is done by other tools such as @command{grub2-mkimage}.

Therefore,
+you may run those commands directly to install GRUB, without using +@command{grub2-install}. Don't do that, however, unless you are very familiar
+with the internals of GRUB. Installing a boot loader on a running OS may be

docs/grub.texi +2 -0
+extremely dangerous.
+
 On EFI systems for fixed disk install you have to mount EFI System

Partition.
If you mount it at @file{/boot/efi} then you don't need any special
arguments:

@example

docs/grub.texi +2 -2
-# @kbd{grub-install}
+# @kbd{grub2-install}
 @end example
 
 Otherwise you need to specify where your EFI System partition is mounted:
 
 @example
-# @kbd{grub-install --efi-directory=/mnt/efi}
+# @kbd{grub2-install --efi-directory=/mnt/efi}
 @end example
 
 For removable installs you have to use @option{--removable} and specify

both
@option{--boot-directory} and @option{--efi-directory}:

@example

docs/grub.texi +1 -3
-# @kbd{grub-install --efi-directory=/mnt/usb
--boot-directory=/mnt/usb/boot --removable}
+# @kbd{grub2-install --efi-directory=/mnt/usb
--boot-directory=/mnt/usb/boot --removable}
 @end example
 
 @node Making a GRUB bootable CD-ROM
@@ -725,10 +732,10 @@ usually also need to include a configuration file

@file{grub.cfg} and some
other GRUB modules.

To make a simple generic GRUB rescue CD, you can use the
-@command{grub-mkrescue} program (@pxref{Invoking grub-mkrescue}):
+@command{grub2-mkrescue} program (@pxref{Invoking grub2-mkrescue}):

@example
-$ @kbd{grub-mkrescue -o grub.iso}
+$ @kbd{grub2-mkrescue -o grub.iso}
@end example

You will often need to include other files in your image. To do this,
first

docs/grub.texi +2 -2
@@ -751,7 +758,7 @@ directory @file{iso/}.
 Finally, make the image:
 
 @example
-$ @kbd{grub-mkrescue -o grub.iso iso}
+$ @kbd{grub2-mkrescue -o grub.iso iso}
 @end example
 
 This produces a file named @file{grub.iso}, which then can be burned
@@ -767,7 +774,7 @@ storage devices.
 @node Device map
 @section The map between BIOS drives and OS devices
 
-If the device map file exists, the GRUB utilities (@command{grub-probe},
+If the device map file exists, the GRUB utilities (@command{grub2-probe},
 etc.) read it to map BIOS drives to OS devices.  This file consists of

lines
like this:

docs/grub.texi +1 -1
@@ -1269,23 +1276,23 @@ need to write the whole thing by hand.
 @node Simple configuration
 @section Simple configuration handling
 
-The program @command{grub-mkconfig} (@pxref{Invoking grub-mkconfig})
+The program @command{grub2-mkconfig} (@pxref{Invoking grub2-mkconfig})
 generates @file{grub.cfg} files suitable for most cases.  It is suitable

for
use when upgrading a distribution, and will discover available kernels and attempt to generate menu entries for them.

-@command{grub-mkconfig} does have some limitations. While adding extra +@command{grub2-mkconfig} does have some limitations. While adding extra
custom menu entries to the end of the list can be done by editing
-@file{/etc/grub.d/40_custom} or creating @file{/boot/grub/custom.cfg}, +@file{/etc/grub.d/40_custom} or creating @file{/boot/grub2/custom.cfg},
changing the order of menu entries or changing their titles may require making complex changes to shell scripts stored in @file{/etc/grub.d/}.
This
may be improved in the future. In the meantime, those who feel that it would be easier to write @file{grub.cfg} directly are encouraged to do so (@pxref{Booting}, and @ref{Shell-like scripting}), and to disable any
system
-provided by their distribution to automatically run @command{grub-mkconfig}.
+provided by their distribution to automatically run @command{grub2-mkconfig}.

The file @file{/etc/default/grub} controls the operation of
-@command{grub-mkconfig}. It is sourced by a shell script, and so must be +@command{grub2-mkconfig}. It is sourced by a shell script, and so must be
valid POSIX shell input; normally, it will just be a sequence of @samp{KEY=value} lines, but if the value contains spaces or other special characters then it must be quoted. For example:

docs/grub.texi +0 -0
@@ -1323,7 +1330,7 @@ works it's not recommended since titles often

contain unstable device names
and may be translated

If you set this to @samp{saved}, then the default menu entry will be that
-saved by @samp{GRUB_SAVEDEFAULT} or @command{grub-set-default}. This relies on
+saved by @samp{GRUB_SAVEDEFAULT} or @command{grub2-set-default}. This relies on
the environment block, which may not be available in all situations (@pxref{Environment block}).

docs/grub.texi +0 -0
@@ -1334,7 +1341,7 @@ If this option is set to @samp{true}, then, when an

entry is selected, save
it as a new default entry for use by future runs of GRUB. This is only useful if @samp{GRUB_DEFAULT=saved}; it is a separate option because @samp{GRUB_DEFAULT=saved} is useful without this option, in conjunction
with
-@command{grub-set-default}. Unset by default. +@command{grub2-set-default}. Unset by default.
This option relies on the environment block, which may not be available in all situations (@pxref{Environment block}).

docs/grub.texi +0 -0
@@ -1481,15 +1488,15 @@ intel-uc.img intel-ucode.img amd-uc.img

amd-ucode.img early_ucode.cpio microcode
@end example

@item GRUB_DISABLE_LINUX_UUID

docs/grub.texi +1 -3
-Normally, @command{grub-mkconfig} will generate menu entries that use
+Normally, @command{grub2-mkconfig} will generate menu entries that use
 universally-unique identifiers (UUIDs) to identify the root filesystem to
 the Linux kernel, using a @samp{root=UUID=...} kernel parameter.  This is
 usually more reliable, but in some cases it may not be appropriate.  To
 disable the use of UUIDs, set this option to @samp{true}.
 
 @item GRUB_DISABLE_LINUX_PARTUUID
-If @command{grub-mkconfig} cannot identify the root filesystem via its
-universally-unique indentifier (UUID), @command{grub-mkconfig} can use

the UUID

docs/grub.texi +2 -0
+If @command{grub2-mkconfig} cannot identify the root filesystem via its
+universally-unique indentifier (UUID), @command{grub2-mkconfig} can use

the UUID
of the partition containing the filesystem to identify the root
filesystem to
the Linux kernel via a @samp{root=PARTUUID=...} kernel parameter. This
is not
as reliable as using the filesystem UUID, but is more reliable than using
the

docs/grub.texi +0 -0
@@ -1503,7 +1510,7 @@ If this option is set to @samp{true}, disable the

generation of recovery
mode menu entries.

@item GRUB_DISABLE_UUID

docs/grub.texi +1 -1
-Normally, @command{grub-mkconfig} will generate menu entries that use
+Normally, @command{grub2-mkconfig} will generate menu entries that use
 universally-unique identifiers (UUIDs) to identify various filesystems to
 search for files.  This is usually more reliable, but in some cases it may
 not be appropriate.  To disable this use of UUIDs, set this option to
@@ -1514,12 +1521,12 @@ not be appropriate.  To disable this use of UUIDs,

set this option to
@item GRUB_VIDEO_BACKEND
If graphical video support is required, either because the @samp{gfxterm} graphical terminal is in use or because @samp{GRUB_GFXPAYLOAD_LINUX} is
set,

docs/grub.texi +1 -1
-then @command{grub-mkconfig} will normally load all available GRUB video
+then @command{grub2-mkconfig} will normally load all available GRUB video
 drivers and use the one most appropriate for your hardware.  If you need

to
override this for some reason, then you can set this option.

docs/grub.texi +1 -2
-After @command{grub-install} has been run, the available video drivers are
-listed in @file{/boot/grub/video.lst}.
+After @command{grub2-install} has been run, the available video drivers

are
+listed in @file{/boot/grub2/video.lst}.

@item GRUB_GFXMODE
Set the resolution used on the @samp{gfxterm} graphical terminal. Note
that

docs/grub.texi +0 -0
@@ -1552,20 +1559,20 @@ boot sequence.  If you have problems, set this

option to @samp{text} and
GRUB will tell Linux to boot in normal text mode.

@item GRUB_DISABLE_OS_PROBER

docs/grub.texi +1 -1
-The @command{grub-mkconfig} has a feature to use the external
+The @command{grub2-mkconfig} has a feature to use the external
 @command{os-prober} program to discover other operating systems installed

on
the same machine and generate appropriate menu entries for them. It is
disabled
by default since automatic and silent execution of @command{os-prober},
and
creating boot entries based on that data, is a potential attack vector.
Set
this option to @samp{false} to enable this feature in the
-@command{grub-mkconfig} command.
+@command{grub2-mkconfig} command.

@item GRUB_OS_PROBER_SKIP_LIST
List of space-separated FS UUIDs of filesystems to be ignored from
os-prober
output. For efi chainloaders it's <UUID>@@<EFI FILE>

@item GRUB_DISABLE_SUBMENU

docs/grub.texi +1 -1
-Normally, @command{grub-mkconfig} will generate top level menu entry for
+Normally, @command{grub2-mkconfig} will generate top level menu entry for
 the kernel with highest version number and put all other found kernels
 or alternative menu entries for recovery mode in submenu. For entries

returned
by @command{os-prober} first entry will be put on top level and all others

docs/grub.texi +0 -0
@@ -1573,11 +1580,11 @@ in submenu. If this option is set to @samp{true},

flat menu with all entries
on top level will be generated instead. Changing this option will require changing existing values of @samp{GRUB_DEFAULT}, @samp{fallback}
(@pxref{fallback})
and @samp{default} (@pxref{default}) environment variables as well as
saved

docs/grub.texi +2 -3
-default entry using @command{grub-set-default} and value used with
-@command{grub-reboot}.
+default entry using @command{grub2-set-default} and value used with
+@command{grub2-reboot}.
 
 @item GRUB_ENABLE_CRYPTODISK
-If set to @samp{y}, @command{grub-mkconfig} and @command{grub-install}

will
+If set to @samp{y}, @command{grub2-mkconfig} and @command{grub2-install} will
check for encrypted disks and generate additional commands needed to
access
them during boot. Note that in this case unattended boot is not possible because GRUB will wait for passphrase to unlock encrypted container.

docs/grub.texi +0 -1
@@ -1636,7 +1643,7 @@ confusing @samp{GRUB_TIMEOUT_STYLE=countdown} or
 
 @end table
 
-For more detailed customisation of @command{grub-mkconfig}'s output, you

may
+For more detailed customisation of @command{grub2-mkconfig}'s output, you may
edit the scripts in @file{/etc/grub.d} directly. @file{/etc/grub.d/40_custom} is particularly useful for adding entire
custom
menu entries; simply type the menu entries you want to add at the end of

docs/grub.texi +0 -0
@@ -1644,14 +1651,14 @@ that file, making sure to leave at least the first

two lines intact.

@node Root Identifcation Heuristics
@section Root Identifcation Heuristics
-If the target operating system uses the Linux kernel, @command{grub-mkconfig}
+If the target operating system uses the Linux kernel, @command{grub2-mkconfig}
attempts to identify the root file system via a heuristic algoirthm. This algorithm selects the identification method of the root file system by considering three factors. The first is if an initrd for the target
operating
system is also present. The second is @samp{GRUB_DISABLE_LINUX_UUID} and
if set
-to @samp{true}, prevents @command{grub-mkconfig} from identifying the root file
+to @samp{true}, prevents @command{grub2-mkconfig} from identifying the root file
system by its UUID. The third is @samp{GRUB_DISABLE_LINUX_PARTUUID} and
if set
-to @samp{true}, prevents @command{grub-mkconfig} from identifying the root file
+to @samp{true}, prevents @command{grub2-mkconfig} from identifying the root file
system via the UUID of its enclosing partition. If the variables are
assigned
any other value, that value is considered equivalent to @samp{false}. The variables are also considered to be set to @samp{false} if they are not
set.

docs/grub.texi +0 -0
@@ -1661,18 +1668,18 @@ filesystem to the initrd.  Most initrd images

determine the root file system by
checking the Linux kernel's command-line for the @samp{root} key and use
its
value as the identification method of the root file system. To improve
the
reliability of booting, most initrd images also allow the root file
system to be
-identified by its UUID. Because of this behavior, the @command{grub-mkconfig}
+identified by its UUID. Because of this behavior, the @command{grub2-mkconfig}
command will set @samp{root} to @samp{root=UUID=...} to provide the
initrd with
the filesystem UUID of the root file system.

If no initrd is detected or @samp{GRUB_DISABLE_LINUX_UUID} is set to
@samp{true}
-then @command{grub-command} will identify the root filesystem by setting the
+then @command{grub2-command} will identify the root filesystem by setting the
kernel command-line variable @samp{root} to @samp{root=PARTUUID=...}
unless
@samp{GRUB_DISABLE_LINUX_PARTUUID} is also set to @samp{true}. If @samp{GRUB_DISABLE_LINUX_PARTUUID} is also set to @samp{true},
-@command{grub-command} will identify by its Linux device name. +@command{grub2-command} will identify by its Linux device name.

docs/grub.texi +1 -1
-The following table summarizes the behavior of the @command{grub-mkconfig}
+The following table summarizes the behavior of the

@command{grub2-mkconfig}
command.

@multitable {detected} {GRUB_DISABLE_LINUX_PARTUUID}
{GRUB_DISABLE_LINUX_UUID} {Linux Root}

docs/grub.texi +1 -1
@@ -1900,7 +1907,7 @@ images as well.
 Mount this partition on/mnt/boot and disable GRUB in all OSes and manually
 install self-compiled latest GRUB with:
 
-@code{grub-install --boot-directory=/mnt/boot /dev/sda}
+@code{grub2-install --boot-directory=/mnt/boot /dev/sda}
 
 In all the OSes install GRUB tools but disable installing GRUB in
 bootsector,

so you'll have menu.lst and grub.cfg available for use. Also disable
os-prober

docs/grub.texi +5 -5
@@ -1910,20 +1917,20 @@ use by setting:
 
 in /etc/default/grub
 
-Then write a grub.cfg (/mnt/boot/grub/grub.cfg):
+Then write a grub.cfg (/mnt/boot/grub2/grub.cfg):
 
 @example
 
 menuentry "OS using grub2" @{
    insmod xfs
    search --set=root --label OS1 --hint hd0,msdos8
-   configfile /boot/grub/grub.cfg
+   configfile /boot/grub2/grub.cfg
 @}
 
 menuentry "OS using grub2-legacy" @{
    insmod ext2
    search --set=root --label OS2 --hint hd0,msdos6
-   legacy_configfile /boot/grub/menu.lst
+   legacy_configfile /boot/grub2/menu.lst
 @}
 
 menuentry "Windows XP" @{
@@ -1986,15 +1993,15 @@ GRUB supports embedding a configuration file
 directly into the core image,
 so that it is loaded before entering normal mode.  This is useful, for
 example, when it is not straightforward to find the real configuration
 file,
 or when you need to debug problems with loading that file.
-@command{grub-install} uses this feature when it is not using BIOS disk
+@command{grub2-install} uses this feature when it is not using BIOS disk
 functions or when installing to a different disk from the one containing
 @file{/boot/grub}, in which case it needs to use the @command{search}
 command (@pxref{search}) to find @file{/boot/grub}.
 
 To embed a configuration file, use the @option{-c} option to
-@command{grub-mkimage}.  The file is copied into the core image, so it may
+@command{grub2-mkimage}.  The file is copied into the core image, so it

may
reside anywhere on the file system, and may be removed after running
-@command{grub-mkimage}.
+@command{grub2-mkimage}.

After the embedded configuration file (if any) is executed, GRUB will load the @samp{normal} module (@pxref{normal}), which will then read the real

docs/grub.texi +4 -6
@@ -2029,13 +2036,13 @@ included in the core image:
 @example
 @group
 search.fs_label grub root
-if [ -e /boot/grub/example/test1.cfg ]; then
+if [ -e /boot/grub2/example/test1.cfg ]; then
     set prefix=($root)/boot/grub
-    configfile /boot/grub/example/test1.cfg
+    configfile /boot/grub2/example/test1.cfg
 else
-    if [ -e /boot/grub/example/test2.cfg ]; then
+    if [ -e /boot/grub2/example/test2.cfg ]; then
         set prefix=($root)/boot/grub
-        configfile /boot/grub/example/test2.cfg
+        configfile /boot/grub2/example/test2.cfg
     else
         echo "Could not find an example configuration file!"
     fi
@@ -2559,7 +2566,7 @@ grub-mknetdir --net-directory=/srv/tftp
--subdir=/boot/grub -d /usr/lib/grub/i38
 @end group
 @end example
 
-Then follow instructions printed out by grub-mknetdir on configuring your

DHCP
+Then follow instructions printed out by grub2-mknetdir on configuring your DHCP
server.

The grub.cfg file is placed in the same directory as the path output by

docs/grub.texi +1 -1
@@ -2762,7 +2769,7 @@ team are:
 @end table
 
 To take full advantage of this function, install GRUB into the MBR
-(@pxref{Installing GRUB using grub-install}).
+(@pxref{Installing GRUB using grub2-install}).
 
 If you have a laptop which has a similar feature and not in the above list
 could you figure your address and contribute?
@@ -2823,7 +2830,7 @@ bytes.
 The sole function of @file{boot.img} is to read the first sector of the

core
image from a local disk and jump to it. Because of the size restriction, @file{boot.img} cannot understand any file system structure, so
-@command{grub-install} hardcodes the location of the first sector of the +@command{grub2-install} hardcodes the location of the first sector of the
core image into @file{boot.img} when installing GRUB.

@item diskboot.img

docs/grub.texi +1 -1
@@ -2853,7 +2860,7 @@ images.
 
 @item core.img
 This is the core image of GRUB.  It is built dynamically from the kernel
-image and an arbitrary list of modules by the @command{grub-mkimage}
+image and an arbitrary list of modules by the @command{grub2-mkimage}
 program.  Usually, it contains enough modules to access @file{/boot/grub},
 and loads everything else (including menu handling, the ability to load
 target operating systems, and so on) from the file system at run-time.

The

docs/grub.texi +0 -0
@@ -2905,7 +2912,7 @@ GRUB 2 has no single Stage 2 image.  Instead, it

loads modules from
In GRUB 2, images for booting from CD-ROM drives are now constructed using @file{cdboot.img} and @file{core.img}, making sure that the core image contains the @samp{iso9660} module. It is usually best to use the
-@command{grub-mkrescue} program for this. +@command{grub2-mkrescue} program for this.

@item nbgrub
There is as yet no equivalent for @file{nbgrub} in GRUB 2; it was used by

docs/grub.texi +0 -0
@@ -3073,8 +3080,8 @@ There are two ways to specify files, by

@dfn{absolute file name} and by

An absolute file name resembles a Unix absolute file name, using @samp{/} for the directory separator (not @samp{\} as in DOS). One

docs/grub.texi +2 -2
-example is @samp{(hd0,1)/boot/grub/grub.cfg}. This means the file
-@file{/boot/grub/grub.cfg} in the first partition of the first hard
+example is @samp{(hd0,1)/boot/grub2/grub.cfg}. This means the file
+@file{/boot/grub2/grub.cfg} in the first partition of the first hard
 disk. If you omit the device name in an absolute file name, GRUB uses
 GRUB's @dfn{root device} implicitly. So if you set the root device to,
 say, @samp{(hd1,1)} by the command @samp{set root=(hd1,1)} (@pxref{set}),
@@ -3082,8 +3089,8 @@ then @code{/boot/kernel} is the same as

@code{(hd1,1)/boot/kernel}.

On ZFS filesystem the first path component must be @var{volume}@samp{@@}[@var{snapshot}].

docs/grub.texi +2 -2
-So @samp{/rootvol@@snap-129/boot/grub/grub.cfg} refers to file
-@samp{/boot/grub/grub.cfg} in snapshot of volume @samp{rootvol} with name
+So @samp{/rootvol@@snap-129/boot/grub2/grub.cfg} refers to file
+@samp{/boot/grub2/grub.cfg} in snapshot of volume @samp{rootvol} with name
 @samp{snap-129}.  Trailing @samp{@@} after volume name is mandatory even

if
snapshot name is omitted.

docs/grub.texi +0 -0
@@ -3502,7 +3509,7 @@ The more recent release of Minix would then be

identified as
@samp{other>minix>minix-3.4.0}.

This variable is often set by @samp{GRUB_DEFAULT} (@pxref{Simple

docs/grub.texi +1 -1
-configuration}), @command{grub-set-default}, or @command{grub-reboot}.
+configuration}), @command{grub2-set-default}, or @command{grub2-reboot}.
 
 
 @node fallback
@@ -3592,7 +3599,7 @@ If this variable is set, it names the language code

that the
example, French would be named as @samp{fr}, and Simplified Chinese as @samp{zh_CN}.

-@command{grub-mkconfig} (@pxref{Simple configuration}) will try to set a +@command{grub2-mkconfig} (@pxref{Simple configuration}) will try to set a
reasonable default for this variable based on the system locale.

docs/grub.texi +0 -0
@@ -3600,10 +3607,10 @@ reasonable default for this variable based on the

system locale.
@subsection locale_dir

If this variable is set, it names the directory where translation files
may

docs/grub.texi +1 -2
-be found (@pxref{gettext}), usually @file{/boot/grub/locale}.  Otherwise,
+be found (@pxref{gettext}), usually @file{/boot/grub2/locale}.  Otherwise,
 internationalization is disabled.
 
-@command{grub-mkconfig} (@pxref{Simple configuration}) will set a

reasonable
+@command{grub2-mkconfig} (@pxref{Simple configuration}) will set a reasonable
default for this variable if internationalization is needed and any translation files are available.

docs/grub.texi +1 -1
@@ -3740,7 +3747,7 @@ input.  The default is not to pause output.
 
 The location of the @samp{/boot/grub} directory as an absolute file name
 (@pxref{File name syntax}).  This is normally set by GRUB at startup based
-on information provided by @command{grub-install}.  GRUB modules are
+on information provided by @command{grub2-install}.  GRUB modules are
 dynamically loaded from this directory, so it must be set correctly in

order
for many parts of GRUB to work.

docs/grub.texi +0 -0
@@ -3852,17 +3859,17 @@ GRUB provides an ``environment block'' which can

be used to save a small
amount of state.

The environment block is a preallocated 1024-byte file, which normally
lives
-in @file{/boot/grub/grubenv} (although you should not assume this). At boot
+in @file{/boot/grub2/grubenv} (although you should not assume this). At boot
time, the @command{load_env} command (@pxref{load_env}) loads environment variables from it, and the @command{save_env} (@pxref{save_env}) command saves environment variables to it. From a running system, the
-@command{grub-editenv} utility can be used to edit the environment block. +@command{grub2-editenv} utility can be used to edit the environment block.

For safety reasons, this storage is only available when installed on a
plain
disk (no LVM or RAID), using a non-checksumming filesystem (no ZFS), and using BIOS or EFI functions (no ATA, USB or IEEE1275).

-@command{grub-mkconfig} uses this facility to implement +@command{grub2-mkconfig} uses this facility to implement
@samp{GRUB_SAVEDEFAULT} (@pxref{Simple configuration}).

@node Modules

docs/grub.texi +1 -1
@@ -4924,7 +4931,7 @@ Translate @var{string} into the current language.
 
 The current language code is stored in the @samp{lang} variable in GRUB's
 environment (@pxref{lang}).  Translation files in MO format are read from
-@samp{locale_dir} (@pxref{locale_dir}), usually @file{/boot/grub/locale}.
+@samp{locale_dir} (@pxref{locale_dir}), usually @file{/boot/grub2/locale}.
 @end deffn
 
 
@@ -5276,7 +5283,7 @@ Define a user named @var{user} with password

@var{clear-password}.

@deffn Command password_pbkdf2 user hashed-password
Define a user named @var{user} with password hash @var{hashed-password}.

docs/grub.texi +1 -1
-Use @command{grub-mkpasswd-pbkdf2} (@pxref{Invoking grub-mkpasswd-pbkdf2})
+Use @command{grub2-mkpasswd-pbkdf2} (@pxref{Invoking

grub2-mkpasswd-pbkdf2})
to generate password hashes. @xref{Security}.
@end deffn

docs/grub.texi +2 -2
@@ -6402,8 +6409,8 @@ The @samp{password} (@pxref{password}) and
 @samp{password_pbkdf2}
 which has an associated password.  @samp{password} sets the password in
 plain text, requiring @file{grub.cfg} to be secure; @samp{password_pbkdf2}
 sets the password hashed using the Password-Based Key Derivation Function
-(RFC 2898), requiring the use of @command{grub-mkpasswd-pbkdf2}
-(@pxref{Invoking grub-mkpasswd-pbkdf2}) to generate password hashes.
+(RFC 2898), requiring the use of @command{grub2-mkpasswd-pbkdf2}
+(@pxref{Invoking grub2-mkpasswd-pbkdf2}) to generate password hashes.
 
 In order to enable authentication support, the @samp{superusers}

environment
variable must be set to a list of usernames, separated by any of spaces,

docs/grub.texi +1 -2
@@ -6448,7 +6455,7 @@ menuentry "May be run by user1 or a superuser"
--users user1 @{
 @end group
 @end example
 
-The @command{grub-mkconfig} program does not yet have built-in support for
+The @command{grub2-mkconfig} program does not yet have built-in support

for
generating configuration files with authentication. You can use @file{/etc/grub.d/40_custom} to add simple superuser authentication, by adding @kbd{set superusers=} and @kbd{password} or @kbd{password_pbkdf2}

docs/grub.texi +0 -0
@@ -6474,8 +6481,18 @@ validation fails, then file @file{foo} cannot be

opened. This failure
may halt or otherwise impact the boot process.

An initial trusted public key can be embedded within the GRUB
@file{core.img}

docs/grub.texi +10 -2
-using the @code{--pubkey} option to @command{grub-install}
-(@pxref{Invoking grub-install}).
+using the @code{--pubkey} option to @command{grub2-install}
+(@pxref{Invoking grub2-install}).
+
+@comment Unfortunately --pubkey is not yet supported by grub2-install,
+@comment but we should not bring up internal detail grub2-mkimage here
+@comment in the user guide (as opposed to developer's manual).
+
+@comment An initial trusted public key can be embedded within the GRUB
+@comment @file{core.img} using the @code{--pubkey} option to
+@comment @command{grub2-mkimage} (@pxref{Invoking grub2-install}).

Presently it
+@comment is necessary to write a custom wrapper around @command{grub2-mkimage}
+@comment using the @code{--grub-mkimage} flag to @command{grub2-install}.

GRUB uses GPG-style detached signatures (meaning that a file @file{foo.sig} will be produced when file @file{foo} is signed), and

docs/grub.texi +3 -4
@@ -6495,8 +6512,8 @@ gpg --detach-sign /path/to/file
 For successful validation of all of GRUB's subcomponents and the
 loaded OS kernel, they must all be signed.  One way to accomplish this
 is the following (after having already produced the desired
-@file{grub.cfg} file, e.g., by running @command{grub-mkconfig}
-(@pxref{Invoking grub-mkconfig}):
+@file{grub.cfg} file, e.g., by running @command{grub2-mkconfig}
+(@pxref{Invoking grub2-mkconfig}):
 
 @example
 @group
@@ -6518,7 +6535,7 @@ See also: @ref{check_signatures},
 @ref{verify_detached}, @ref{trust},
 Note that internally signature enforcement is controlled by setting
 the environment variable @code{check_signatures} equal to
 @code{enforce}.  Passing one or more @code{--pubkey} options to
-@command{grub-mkimage} implicitly defines @code{check_signatures}
+@command{grub2-mkimage} implicitly defines @code{check_signatures}
 equal to @code{enforce} in @file{core.img} prior to processing any
 configuration files.
@@ -6566,7 +6583,7 @@ UTF-8 strings as comma-separated values (CSV). See
 @uref{https://github.com/rhboot/shim/blob/main/SBAT.md} for more details.
 
 To add a data section containing the SBAT information into the binary, the
-@option{--sbat} option of @command{grub-mkimage} command should be used.

The content
+@option{--sbat} option of @command{grub2-mkimage} command should be used. The content
of a CSV file, encoded with UTF-8, is copied as is to the .sbat data
section into
the generated EFI binary. The CSV file can be stored anywhere on the file
system.

docs/grub.texi +1 -1
@@ -6977,10 +6994,10 @@ Required files are:
 
 GRUB's normal start-up procedure involves setting the @samp{prefix}
 environment variable to a value set in the core image by
-@command{grub-install}, setting the @samp{root} variable to match, loading
+@command{grub2-install}, setting the @samp{root} variable to match,

loading
the @samp{normal} module from the prefix, and running the @samp{normal} command (@pxref{normal}). This command is responsible for reading
-@file{/boot/grub/grub.cfg}, running the menu, and doing all the useful +@file{/boot/grub2/grub.cfg}, running the menu, and doing all the useful
things GRUB is supposed to do.

If, instead, you only get a rescue shell, this usually means that GRUB

docs/grub.texi +0 -0
@@ -7006,8 +7023,8 @@ normal
 
 However, any problem that leaves you in the rescue shell probably means

that
GRUB was not correctly installed. It may be more useful to try to
reinstall

docs/grub.texi +2 -2
-it properly using @kbd{grub-install @var{device}} (@pxref{Invoking
-grub-install}).  When doing this, there are a few things to remember:
+it properly using @kbd{grub2-install @var{device}} (@pxref{Invoking
+grub2-install}).  When doing this, there are a few things to remember:
 
 @itemize @bullet{}
 @item
@@ -7019,7 +7036,7 @@ is usually better to use UUIDs or file system labels

and avoid depending on
drive ordering entirely.

@item
-At least on BIOS systems, if you tell @command{grub-install} to install GRUB
+At least on BIOS systems, if you tell @command{grub2-install} to install GRUB
to a partition but GRUB has already been installed in the master boot record, then the GRUB installation in the partition will be ignored.

docs/grub.texi +10 -12
@@ -7054,33 +7071,33 @@ bootability on other machines.
 @chapter User-space utilities
 
 @menu
-* Invoking grub-install::       How to use the GRUB installer
-* Invoking grub-mkconfig::      Generate a GRUB configuration file
-* Invoking grub-mkpasswd-pbkdf2::
+* Invoking grub2-install::       How to use the GRUB installer
+* Invoking grub2-mkconfig::      Generate a GRUB configuration file
+* Invoking grub2-mkpasswd-pbkdf2::
                                 Generate GRUB password hashes
-* Invoking grub-mkrelpath::     Make system path relative to its root
-* Invoking grub-mkrescue::      Make a GRUB rescue image
-* Invoking grub-mount::         Mount a file system using GRUB
-* Invoking grub-probe::         Probe device information for GRUB
-* Invoking grub-script-check::  Check GRUB script file for syntax errors
+* Invoking grub2-mkrelpath::     Make system path relative to its root
+* Invoking grub2-mkrescue::      Make a GRUB rescue image
+* Invoking grub2-mount::         Mount a file system using GRUB
+* Invoking grub2-probe::         Probe device information for GRUB
+* Invoking grub2-script-check::  Check GRUB script file for syntax errors
 @end menu
 
 
-@node Invoking grub-install
-@section Invoking grub-install
+@node Invoking grub2-install
+@section Invoking grub2-install
 
-The program @command{grub-install} generates a GRUB core image using
-@command{grub-mkimage} and installs it on your system.  You must specify

the
+The program @command{grub2-install} generates a GRUB core image using +@command{grub2-mkimage} and installs it on your system. You must specify the
device name on which you want to install GRUB, like this:

@example

docs/grub.texi +4 -4
-grub-install @var{install_device}
+grub2-install @var{install_device}
 @end example
 
 The device name @var{install_device} is an OS device name or a GRUB
 device name.
 
-@command{grub-install} accepts the following options:
+@command{grub2-install} accepts the following options:
 
 @table @option
 @item --help
@@ -7096,13 +7113,13 @@ separate partition or a removable disk.
 If this option is not specified then it defaults to @file{/boot}, so
 
 @example
-@kbd{grub-install /dev/sda}
+@kbd{grub2-install /dev/sda}
 @end example
 
 is equivalent to
 
 @example
-@kbd{grub-install --boot-directory=/boot/ /dev/sda}
+@kbd{grub2-install --boot-directory=/boot/ /dev/sda}
 @end example
 
 Here is an example in which you have a separate @dfn{boot} partition

which is

docs/grub.texi +3 -3
@@ -7110,16 +7127,16 @@ mounted on
 @file{/mnt/boot}:
 
 @example
-@kbd{grub-install --boot-directory=/mnt/boot /dev/sdb}
+@kbd{grub2-install --boot-directory=/mnt/boot /dev/sdb}
 @end example
 
 @item --recheck
-Recheck the device map, even if @file{/boot/grub/device.map} already
+Recheck the device map, even if @file{/boot/grub2/device.map} already
 exists. You should use this option whenever you add/remove a disk
 into/from your computer.
 
 @item --no-rs-codes
-By default on x86 BIOS systems, @command{grub-install} will use some
+By default on x86 BIOS systems, @command{grub2-install} will use some
 extra space in the bootloader embedding area for Reed-Solomon
 error-correcting codes. This enables GRUB to still boot successfully
 if some blocks are corrupted.  The exact amount of protection offered
@@ -7132,17 +7149,17 @@ installation}) where GRUB does not reside in any

unpartitioned space
outside of the MBR. Disable the Reed-Solomon codes with this option. @end table

-@node Invoking grub-mkconfig
-@section Invoking grub-mkconfig
+@node Invoking grub2-mkconfig
+@section Invoking grub2-mkconfig

-The program @command{grub-mkconfig} generates a configuration file for GRUB
+The program @command{grub2-mkconfig} generates a configuration file for GRUB
(@pxref{Simple configuration}).

@example

docs/grub.texi +16 -16
-grub-mkconfig -o /boot/grub/grub.cfg
+grub2-mkconfig -o /boot/grub2/grub.cfg
 @end example
 
-@command{grub-mkconfig} accepts the following options:
+@command{grub2-mkconfig} accepts the following options:
 
 @table @option
 @item --help
@@ -7158,17 +7175,17 @@ it to standard output.
 @end table
 
 
-@node Invoking grub-mkpasswd-pbkdf2
-@section Invoking grub-mkpasswd-pbkdf2
+@node Invoking grub2-mkpasswd-pbkdf2
+@section Invoking grub2-mkpasswd-pbkdf2
 
-The program @command{grub-mkpasswd-pbkdf2} generates password hashes for
+The program @command{grub2-mkpasswd-pbkdf2} generates password hashes for
 GRUB (@pxref{Security}).
 
 @example
-grub-mkpasswd-pbkdf2
+grub2-mkpasswd-pbkdf2
 @end example
 
-@command{grub-mkpasswd-pbkdf2} accepts the following options:
+@command{grub2-mkpasswd-pbkdf2} accepts the following options:
 
 @table @option
 @item -c @var{number}
@@ -7186,23 +7203,23 @@ Length of the salt.  Defaults to 64.
 @end table
 
 
-@node Invoking grub-mkrelpath
-@section Invoking grub-mkrelpath
+@node Invoking grub2-mkrelpath
+@section Invoking grub2-mkrelpath
 
-The program @command{grub-mkrelpath} makes a file system path relative to
+The program @command{grub2-mkrelpath} makes a file system path relative to
 the root of its containing file system.  For instance, if @file{/usr} is a
 mount point, then:
 
 @example
-$ @kbd{grub-mkrelpath /usr/share/grub/unicode.pf2}
+$ @kbd{grub2-mkrelpath /usr/share/grub/unicode.pf2}
 @samp{/share/grub/unicode.pf2}
 @end example
 
 This is mainly used internally by other GRUB utilities such as
-@command{grub-mkconfig} (@pxref{Invoking grub-mkconfig}), but may
+@command{grub2-mkconfig} (@pxref{Invoking grub2-mkconfig}), but may
 occasionally also be useful for debugging.
 
-@command{grub-mkrelpath} accepts the following options:
+@command{grub2-mkrelpath} accepts the following options:
 
 @table @option
 @item --help
@@ -7213,17 +7230,17 @@ Print the version number of GRUB and exit.
 @end table
 
 
-@node Invoking grub-mkrescue
-@section Invoking grub-mkrescue
+@node Invoking grub2-mkrescue
+@section Invoking grub2-mkrescue
 
-The program @command{grub-mkrescue} generates a bootable GRUB rescue image
+The program @command{grub2-mkrescue} generates a bootable GRUB rescue

image
(@pxref{Making a GRUB bootable CD-ROM}).

@example

docs/grub.texi +2 -2
-grub-mkrescue -o grub.iso
+grub2-mkrescue -o grub.iso
 @end example
 
-All arguments not explicitly listed as @command{grub-mkrescue} options are
+All arguments not explicitly listed as @command{grub2-mkrescue} options

are
passed on directly to @command{xorriso} in @command{mkisofs} emulation
mode.
Options passed to @command{xorriso} will normally be interpreted as @command{mkisofs} options; if the option @samp{--} is used, then anything

docs/grub.texi +2 -2
@@ -7235,10 +7252,10 @@ commonly used to add extra files to the image:
 @example
 mkdir -p disk/boot/grub
 @r{(add extra files to @file{disk/boot/grub})}
-grub-mkrescue -o grub.iso disk
+grub2-mkrescue -o grub.iso disk
 @end example
 
-@command{grub-mkrescue} accepts the following options:
+@command{grub2-mkrescue} accepts the following options:
 
 @table @option
 @item --help
@@ -7266,15 +7283,15 @@ Use @var{file} as the @command{xorriso} program,

rather than the built-in
default.

@item --grub-mkimage=@var{file}

docs/grub.texi +4 -4
-Use @var{file} as the @command{grub-mkimage} program, rather than the
+Use @var{file} as the @command{grub2-mkimage} program, rather than the
 built-in default.
 @end table
 
 
-@node Invoking grub-mount
-@section Invoking grub-mount
+@node Invoking grub2-mount
+@section Invoking grub2-mount
 
-The program @command{grub-mount} performs a read-only mount of any file
+The program @command{grub2-mount} performs a read-only mount of any file
 system or file system image that GRUB understands, using GRUB's file

system
drivers via FUSE. (It is only available if FUSE development files were present when GRUB was built.) This has a number of uses:

docs/grub.texi +0 -0
@@ -7306,13 +7323,13 @@ even if nobody has yet written a FUSE module

specifically for that file
system type.
@end itemize

docs/grub.texi +3 -3
-Using @command{grub-mount} is normally as simple as:
+Using @command{grub2-mount} is normally as simple as:
 
 @example
-grub-mount /dev/sda1 /mnt
+grub2-mount /dev/sda1 /mnt
 @end example
 
-@command{grub-mount} must be given one or more images and a mount point as
+@command{grub2-mount} must be given one or more images and a mount point

as
non-option arguments (if it is given more than one image, it will treat
them
as a RAID set), and also accepts the following options:

docs/grub.texi +0 -0
@@ -7334,13 +7351,13 @@ Show debugging output for conditions matching

@var{string}.
@item -K prompt|@var{file}
@itemx --zfs-key=prompt|@var{file}
Load a ZFS encryption key. If you use @samp{prompt} as the argument,
-@command{grub-mount} will read a passphrase from the terminal; otherwise, it
+@command{grub2-mount} will read a passphrase from the terminal; otherwise, it
will read key material from the specified file.

@item -r @var{device}
@itemx --root=@var{device}
Set the GRUB root device to @var{device}. You do not normally need to set

docs/grub.texi +1 -1
-this; @command{grub-mount} will automatically set the root device to the
+this; @command{grub2-mount} will automatically set the root device to the
 root of the supplied file system.
 
 If @var{device} is just a number, then it will be treated as a partition
@@ -7349,7 +7366,7 @@ an entire disk in @file{disk.img}, then you can use

this command to mount
its second partition:

@example

docs/grub.texi +3 -4
-grub-mount -r 2 disk.img mount-point
+grub2-mount -r 2 disk.img mount-point
 @end example
 
 @item -v
@@ -7358,18 +7375,18 @@ Print verbose messages.
 @end table
 
 
-@node Invoking grub-probe
-@section Invoking grub-probe
+@node Invoking grub2-probe
+@section Invoking grub2-probe
 
-The program @command{grub-probe} probes device information for a given

path
+The program @command{grub2-probe} probes device information for a given path
or device.

@example

docs/grub.texi +4 -5
-grub-probe --target=fs /boot/grub
-grub-probe --target=drive --device /dev/sda1
+grub2-probe --target=fs /boot/grub
+grub2-probe --target=drive --device /dev/sda1
 @end example
 
-@command{grub-probe} must be given a path or device as a non-option
+@command{grub2-probe} must be given a path or device as a non-option
 argument, and also accepts the following options:
 
 @table @option
@@ -7382,16 +7399,16 @@ Print the version number of GRUB and exit.
 @item -d
 @itemx --device
 If this option is given, then the non-option argument is a system device
-name (such as @samp{/dev/sda1}), and @command{grub-probe} will print
+name (such as @samp{/dev/sda1}), and @command{grub2-probe} will print
 information about that device.  If it is not given, then the non-option
 argument is a filesystem path (such as @samp{/boot/grub}), and
-@command{grub-probe} will print information about the device containing

that
+@command{grub2-probe} will print information about the device containing that
part of the filesystem.

@item -m @var{file}
@itemx --device-map=@var{file}
Use @var{file} as the device map (@pxref{Device map}) rather than the

docs/grub.texi +4 -4
-default, usually @samp{/boot/grub/device.map}.
+default, usually @samp{/boot/grub2/device.map}.
 
 @item -t @var{target}
 @itemx --target=@var{target}
@@ -7444,19 +7461,19 @@ Print verbose messages.
 @end table
 
 
-@node Invoking grub-script-check
-@section Invoking grub-script-check
+@node Invoking grub2-script-check
+@section Invoking grub2-script-check
 
-The program @command{grub-script-check} takes a GRUB script file
+The program @command{grub2-script-check} takes a GRUB script file
 (@pxref{Shell-like scripting}) and checks it for syntax errors, similar to
 commands such as @command{sh -n}.  It may take a @var{path} as a

non-option
argument; if none is supplied, it will read from standard input.

@example

docs/grub.texi +2 -2
-grub-script-check /boot/grub/grub.cfg
+grub2-script-check /boot/grub2/grub.cfg
 @end example
 
-@command{grub-script-check} accepts the following options:
+@command{grub2-script-check} accepts the following options:
 
 @table @option
 @item --help
grub-core/kern/misc.c +1 -1
index 465a8e74e..b1d399d37 100644
@@ -1303,7 +1303,7 @@ void __attribute__ ((noreturn))
 grub_abort (void)
 {
 #ifndef GRUB_UTIL
-#if defined(__i386__) || defined(__x86_64__)
+#if (defined(__i386__) || defined(__x86_64__)) &&
 !defined(GRUB_MACHINE_EMU)
   grub_backtrace();
 #endif

#endif
--
2.46.2

Quoted 5 lines

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

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

#14 lore raw

PATCH 26/37 30_os-prober.in: just build chainloader entries, don't try any xnu xnu.

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

Since our bugs tell us that the xnu boot entries really just don't work most of the time, and they create piles of extra boot entries, because they can't quite figure out 32-vs-64 and other stuff like that.

It's rediculous, and we should just boot their bootloader through the chainloader instead.

So this patch does that.

Resolves: rhbz#893179

---
 util/grub.d/30_os-prober.in | 78 +++++++++----------------------------
 1 file changed, 18 insertions(+), 60 deletions(-)
util/grub.d/30_os-prober.in +18 -60
index 656301eaf..1aac097c1 100644
@@ -45,68 +45,25 @@ if [ -z "${OSPROBED}" ] ; then
 fi
 
 osx_entry() {
-    if [ x$2 = x32 ]; then
-        # TRANSLATORS: it refers to kernel architecture (32-bit)
-	bitstr="$(gettext "(32-bit)")"
-    else
-        # TRANSLATORS: it refers to kernel architecture (64-bit)
-	bitstr="$(gettext "(64-bit)")"
-    fi
     # TRANSLATORS: it refers on the OS residing on device %s
     onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
-        cat << EOF
-menuentry '$(echo "${LONGNAME} $bitstr $onstr" | grub_quote)' --class osx --class darwin --class os \$menuentry_id_option 'osprober-xnu-$2-$(grub_get_device_id "${DEVICE}")'  {
+    hints=""
+    for hint in `"${grub_probe}" --device ${device} --target=efi_hints 2> /dev/null` ; do
+      hints="${hints} --hint=${hint}"
+    done
+    cat << EOF
+menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' --class osx --class darwin --class os \$menuentry_id_option 'osprober-xnu-$2-$(grub_get_device_id "${DEVICE}")'  {
 EOF
 	save_default_entry | grub_add_tab
 	prepare_grub_to_access_device ${DEVICE} | grub_add_tab
 	cat << EOF
+	set gfxpayload=keep
         load_video
-        set do_resume=0
-        if [ /var/vm/sleepimage -nt10 / ]; then
-           if xnu_resume /var/vm/sleepimage; then
-             set do_resume=1
-           fi
-        fi
-        if [ \$do_resume = 0 ]; then
-           xnu_uuid ${OSXUUID} uuid
-           if [ -f /Extra/DSDT.aml ]; then
-              acpi -e /Extra/DSDT.aml
-           fi
-           if [ /kernelcache -nt /System/Library/Extensions ]; then
-              $1 /kernelcache boot-uuid=\${uuid} rd=*uuid
-           elif [ -f /System/Library/Kernels/kernel ]; then
-              $1 /System/Library/Kernels/kernel boot-uuid=\${uuid} rd=*uuid
-              xnu_kextdir /System/Library/Extensions
-           else
-              $1 /mach_kernel boot-uuid=\${uuid} rd=*uuid
-              if [ /System/Library/Extensions.mkext -nt /System/Library/Extensions ]; then
-                xnu_mkext /System/Library/Extensions.mkext
-              else
-                xnu_kextdir /System/Library/Extensions
-              fi
-           fi
-           if [ -f /Extra/Extensions.mkext ]; then
-              xnu_mkext /Extra/Extensions.mkext
-           fi
-           if [ -d /Extra/Extensions ]; then
-              xnu_kextdir /Extra/Extensions
-           fi
-           if [ -f /Extra/devprop.bin ]; then
-              xnu_devprop_load /Extra/devprop.bin
-           fi
-           if [ -f /Extra/splash.jpg ]; then
-              insmod jpeg
-              xnu_splash /Extra/splash.jpg
-           fi
-           if [ -f /Extra/splash.png ]; then
-              insmod png
-              xnu_splash /Extra/splash.png
-           fi
-           if [ -f /Extra/splash.tga ]; then
-              insmod tga
-              xnu_splash /Extra/splash.tga
-           fi
-        fi
+	insmod part_gpt
+	insmod hfsplus
+	search --no-floppy --fs-uuid --set=root ${hints} $(grub_get_device_id "${DEVICE}")
+	chainloader (\$root)/System/Library/CoreServices/boot.efi
+	boot
 }
 EOF
 }
@@ -299,11 +256,12 @@ EOF
       echo "$title_correction_code"
     ;;
     macosx)
-      if [ "${UUID}" ]; then
-	OSXUUID="${UUID}"
-	osx_entry xnu_kernel 32
-	osx_entry xnu_kernel64 64
-      fi
+      for subdevice in ${DEVICE%[[:digit:]]*}* ; do
+	parttype="`"${grub_probe}" --device ${device} --target=gpt_parttype "${subdevice}" 2> /dev/null`"
+	if [[ "$parttype" = "426f6f74-0000-11aa-aa11-00306543ecac" ]]; then
+	  DEVICE="${subdevice}" osx_entry
+	fi
+      done
     ;;
     hurd)
       onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
Signature · 7 lines
2.46.2


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

PATCH 27/37 btrfs: Add ability to boot from subvolumes

In reply to Leo Sandoval

From: Jeff Mahoney <[email protected]>

This patch adds the ability to specify a different root on a btrfs filesystem too boot from other than the default one.

btrfs-list-snapshots <dev> will list the subvolumes available on the filesystem.

set btrfs_subvol=<path> and set btrfs_subvolid=<subvolid> will specify which subvolume to use and any pathnames provided with either of those variables set will start using that root. If the subvolume or subvolume id doesn't exist, then an error case will result.

It is possible to boot into a separate GRUB instance by exporting the variable and loading the config file from the subvolume.

---
 grub-core/fs/btrfs.c | 554 +++++++++++++++++++++++++++++++++++++++++--
 include/grub/btrfs.h |   1 +
 2 files changed, 534 insertions(+), 21 deletions(-)
grub-core/fs/btrfs.c +533 -21
index ba0c58352..f14fe9c1b 100644
@@ -38,6 +38,9 @@
 #include <zstd.h>
 #include <grub/i18n.h>
 #include <grub/btrfs.h>
+#include <grub/command.h>
+#include <grub/env.h>
+#include <grub/extcmd.h>
 #include <grub/crypto.h>
 #include <grub/diskfilter.h>
 #include <grub/safemath.h>
@@ -79,9 +82,11 @@ struct grub_btrfs_superblock
   grub_uint64_t generation;
   grub_uint64_t root_tree;
   grub_uint64_t chunk_tree;
-  grub_uint8_t dummy2[0x20];
+  grub_uint8_t dummy2[0x18];
+  grub_uint64_t bytes_used;
   grub_uint64_t root_dir_objectid;
-  grub_uint8_t dummy3[0x41];
+  grub_uint64_t num_devices;
+  grub_uint8_t dummy3[0x39];
   struct grub_btrfs_device this_device;
   char label[0x100];
   grub_uint8_t dummy4[0x100];
@@ -121,6 +126,7 @@ struct grub_btrfs_data
   grub_uint64_t exttree;
   grub_size_t extsize;
   struct grub_btrfs_extent_data *extent;
+  grub_uint64_t fs_tree;
 };
 
 struct grub_btrfs_chunk_item
@@ -191,6 +197,14 @@ struct grub_btrfs_leaf_descriptor
   } *data;
 };
 
+struct grub_btrfs_root_ref
+{
+  grub_uint64_t dirid;
+  grub_uint64_t sequence;
+  grub_uint16_t name_len;
+  const char name[0];
+} __attribute__ ((packed));
+
 struct grub_btrfs_time
 {
   grub_int64_t sec;
@@ -236,6 +250,14 @@ struct grub_btrfs_extent_data
 
 #define GRUB_BTRFS_OBJECT_ID_CHUNK 0x100
 
+#define GRUB_BTRFS_ROOT_TREE_OBJECTID 1ULL
+#define GRUB_BTRFS_FS_TREE_OBJECTID 5ULL
+#define GRUB_BTRFS_ROOT_REF_KEY     156
+#define GRUB_BTRFS_ROOT_ITEM_KEY     132
+
+static grub_uint64_t btrfs_default_subvolid = 0;
+static char *btrfs_default_subvol = NULL;
+
 static grub_disk_addr_t superblock_sectors[] = { 64 * 2, 64 * 1024 * 2,
   256 * 1048576 * 2, 1048576ULL * 1048576ULL * 2
 };
@@ -1252,6 +1274,62 @@ grub_btrfs_read_logical (struct grub_btrfs_data *data, grub_disk_addr_t addr,
   return GRUB_ERR_NONE;
 }
 
+static grub_err_t
+get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
+            grub_uint64_t objectid, grub_uint64_t offset,
+            grub_uint64_t *fs_root);
+
+static grub_err_t
+lookup_root_by_id(struct grub_btrfs_data *data, grub_uint64_t id)
+{
+  grub_err_t err;
+  grub_uint64_t tree;
+
+  err = get_fs_root(data, data->sblock.root_tree, id, -1, &tree);
+  if (!err)
+    data->fs_tree = tree;
+  return err;
+}
+
+static grub_err_t
+find_path (struct grub_btrfs_data *data,
+	   const char *path, struct grub_btrfs_key *key,
+	   grub_uint64_t *tree, grub_uint8_t *type);
+
+static grub_err_t
+lookup_root_by_name(struct grub_btrfs_data *data, const char *path)
+{
+  grub_err_t err;
+  grub_uint64_t tree = 0;
+  grub_uint8_t type;
+  struct grub_btrfs_key key;
+
+  err = find_path (data, path, &key, &tree, &type);
+  if (err)
+      return grub_error(GRUB_ERR_FILE_NOT_FOUND, "couldn't locate %s\n", path);
+
+  if (key.object_id != grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK) || tree == 0)
+    return grub_error(GRUB_ERR_BAD_FILE_TYPE, "%s: not a subvolume\n", path);
+
+  data->fs_tree = tree;
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+btrfs_handle_subvol(struct grub_btrfs_data *data __attribute__ ((unused)))
+{
+  if (btrfs_default_subvol)
+    return lookup_root_by_name(data, btrfs_default_subvol);
+
+  if (btrfs_default_subvolid)
+    return lookup_root_by_id(data, btrfs_default_subvolid);
+
+  data->fs_tree = 0;
+
+  return GRUB_ERR_NONE;
+}
+
+
 static struct grub_btrfs_data *
 grub_btrfs_mount (grub_device_t dev)
 {
@@ -1287,6 +1365,13 @@ grub_btrfs_mount (grub_device_t dev)
   data->devices_attached[0].dev = dev;
   data->devices_attached[0].id = data->sblock.this_device.device_id;
 
+  err = btrfs_handle_subvol (data);
+  if (err)
+    {
+      grub_free (data);
+      return NULL;
+    }
+
   return data;
 }
 
@@ -1784,6 +1869,91 @@ get_root (struct grub_btrfs_data *data, struct grub_btrfs_key *key,
   return GRUB_ERR_NONE;
 }
 
+static grub_err_t
+find_pathname(struct grub_btrfs_data *data, grub_uint64_t objectid,
+              grub_uint64_t fs_root, const char *name, char **pathname)
+{
+  grub_err_t err;
+  struct grub_btrfs_key key = {
+    .object_id = objectid,
+    .type = GRUB_BTRFS_ITEM_TYPE_INODE_REF,
+    .offset = 0,
+  };
+  struct grub_btrfs_key key_out;
+  struct grub_btrfs_leaf_descriptor desc;
+  char *p = grub_strdup (name);
+  grub_disk_addr_t elemaddr;
+  grub_size_t elemsize;
+  grub_size_t alloc = grub_strlen(name) + 1;
+
+  err = lower_bound(data, &key, &key_out, fs_root,
+                    &elemaddr, &elemsize, &desc, 0);
+  if (err)
+    return grub_error(err, "lower_bound caught %d\n", err);
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
+    next(data, &desc, &elemaddr, &elemsize, &key_out);
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
+    {
+      return grub_error(GRUB_ERR_FILE_NOT_FOUND,
+                        "Can't find inode ref for {%"PRIuGRUB_UINT64_T
+                        ", %u, %"PRIuGRUB_UINT64_T"} %"PRIuGRUB_UINT64_T
+                        "/%"PRIuGRUB_SIZE"\n",
+                        key_out.object_id, key_out.type,
+                        key_out.offset, elemaddr, elemsize);
+    }
+
+
+  while (key_out.type == GRUB_BTRFS_ITEM_TYPE_INODE_REF &&
+         key_out.object_id != key_out.offset) {
+    struct grub_btrfs_inode_ref *inode_ref;
+    char *new;
+
+    inode_ref = grub_malloc(elemsize + 1);
+    if (!inode_ref)
+      return grub_error(GRUB_ERR_OUT_OF_MEMORY,
+                        "couldn't allocate memory for inode_ref (%"PRIuGRUB_SIZE")\n", elemsize);
+
+    err = grub_btrfs_read_logical(data, elemaddr, inode_ref, elemsize, 0);
+    if (err)
+      return grub_error(err, "read_logical caught %d\n", err);
+
+    alloc += grub_le_to_cpu16 (inode_ref->n) + 2;
+    new = grub_malloc(alloc);
+    if (!new)
+      return grub_error(GRUB_ERR_OUT_OF_MEMORY,
+                        "couldn't allocate memory for name (%"PRIuGRUB_SIZE")\n", alloc);
+
+    grub_memcpy(new, inode_ref->name, grub_le_to_cpu16 (inode_ref->n));
+    if (p)
+      {
+        new[grub_le_to_cpu16 (inode_ref->n)] = '/';
+        grub_strcpy (new + grub_le_to_cpu16 (inode_ref->n) + 1, p);
+        grub_free(p);
+      }
+    else
+      new[grub_le_to_cpu16 (inode_ref->n)] = 0;
+    grub_free(inode_ref);
+
+    p = new;
+
+    key.object_id = key_out.offset;
+
+    err = lower_bound(data, &key, &key_out, fs_root, &elemaddr,
+                      &elemsize, &desc, 0);
+    if (err)
+      return grub_error(err, "lower_bound caught %d\n", err);
+
+    if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
+      next(data, &desc, &elemaddr, &elemsize, &key_out);
+
+  }
+
+  *pathname = p;
+  return 0;
+}
+
 static grub_err_t
 find_path (struct grub_btrfs_data *data,
 	   const char *path, struct grub_btrfs_key *key,
@@ -1802,14 +1972,26 @@ find_path (struct grub_btrfs_data *data,
   char *origpath = NULL;
   unsigned symlinks_max = 32;
 
-  err = get_root (data, key, tree, type);
-  if (err)
-    return err;
-
   origpath = grub_strdup (path);
   if (!origpath)
     return grub_errno;
 
+  if (data->fs_tree)
+    {
+      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+      *tree = data->fs_tree;
+      /* This is a tree root, so everything starts at objectid 256 */
+      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+      key->offset = 0;
+    }
+  else
+    {
+      err = get_root (data, key, tree, type);
+      if (err)
+	return err;
+    }
+
   while (1)
     {
       while (path[0] == '/')
@@ -1982,13 +2164,25 @@ find_path (struct grub_btrfs_data *data,
 	  path = path_alloc = tmp;
 	  if (path[0] == '/')
 	    {
-	      err = get_root (data, key, tree, type);
-	      if (err)
+	      if (data->fs_tree)
+		{
+		  *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+		  *tree = data->fs_tree;
+		  /* This is a tree root, so everything starts at objectid 256 */
+		  key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+		  key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+		  key->offset = 0;
+		}
+	      else
 		{
-		  grub_free (direl);
-		  grub_free (path_alloc);
-		  grub_free (origpath);
-		  return err;
+		  err = get_root (data, key, tree, type);
+		  if (err)
+                    {
+                      grub_free (direl);
+                      grub_free (path_alloc);
+                      grub_free (origpath);
+                      return err;
+                    }
 		}
 	    }
 	  continue;
@@ -2256,6 +2450,20 @@ grub_btrfs_read (grub_file_t file, char *buf, grub_size_t len)
 				 data->tree, file->offset, buf, len);
 }
 
+static char *
+btrfs_unparse_uuid(struct grub_btrfs_data *data)
+{
+  return  grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
+			  grub_be_to_cpu16 (data->sblock.uuid[0]),
+			  grub_be_to_cpu16 (data->sblock.uuid[1]),
+			  grub_be_to_cpu16 (data->sblock.uuid[2]),
+			  grub_be_to_cpu16 (data->sblock.uuid[3]),
+			  grub_be_to_cpu16 (data->sblock.uuid[4]),
+			  grub_be_to_cpu16 (data->sblock.uuid[5]),
+			  grub_be_to_cpu16 (data->sblock.uuid[6]),
+			  grub_be_to_cpu16 (data->sblock.uuid[7]));
+}
+
 static grub_err_t
 grub_btrfs_uuid (grub_device_t device, char **uuid)
 {
@@ -2267,15 +2475,7 @@ grub_btrfs_uuid (grub_device_t device, char **uuid)
   if (!data)
     return grub_errno;
 
-  *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
-			  grub_be_to_cpu16 (data->sblock.uuid[0]),
-			  grub_be_to_cpu16 (data->sblock.uuid[1]),
-			  grub_be_to_cpu16 (data->sblock.uuid[2]),
-			  grub_be_to_cpu16 (data->sblock.uuid[3]),
-			  grub_be_to_cpu16 (data->sblock.uuid[4]),
-			  grub_be_to_cpu16 (data->sblock.uuid[5]),
-			  grub_be_to_cpu16 (data->sblock.uuid[6]),
-			  grub_be_to_cpu16 (data->sblock.uuid[7]));
+  *uuid = btrfs_unparse_uuid(data);
 
   grub_btrfs_unmount (data);
 
@@ -2396,6 +2596,242 @@ grub_btrfs_embed (grub_device_t device __attribute__ ((unused)),
 }
 #endif
 
+static grub_err_t
+grub_cmd_btrfs_info (grub_command_t cmd __attribute__ ((unused)), int argc,
+		     char **argv)
+{
+  grub_device_t dev;
+  char *devname;
+  struct grub_btrfs_data *data;
+  char *uuid;
+
+  if (argc < 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
+
+  devname = grub_file_get_device_name(argv[0]);
+
+  if (!devname)
+    return grub_errno;
+
+  dev = grub_device_open (devname);
+  grub_free (devname);
+  if (!dev)
+    return grub_errno;
+
+  data = grub_btrfs_mount (dev);
+  if (!data)
+    {
+      grub_device_close(dev);
+      return grub_error (GRUB_ERR_BAD_ARGUMENT, "failed to open fs");
+    }
+
+  if (data->sblock.label)
+    grub_printf("Label: '%s' ", data->sblock.label);
+  else
+    grub_printf("Label: none ");
+
+  uuid = btrfs_unparse_uuid(data);
+
+  grub_printf(" uuid: %s\n\tTotal devices %" PRIuGRUB_UINT64_T
+              " FS bytes used %" PRIuGRUB_UINT64_T "\n",
+	      uuid, grub_cpu_to_le64(data->sblock.num_devices),
+	      grub_cpu_to_le64(data->sblock.bytes_used));
+
+  grub_btrfs_unmount (data);
+
+  return 0;
+}
+
+static grub_err_t
+get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
+            grub_uint64_t objectid, grub_uint64_t offset,
+            grub_uint64_t *fs_root)
+{
+  grub_err_t err;
+  struct grub_btrfs_key key_in = {
+    .object_id = objectid,
+    .type = GRUB_BTRFS_ROOT_ITEM_KEY,
+    .offset = offset,
+  }, key_out;
+  struct grub_btrfs_leaf_descriptor desc;
+  grub_disk_addr_t elemaddr;
+  grub_size_t elemsize;
+  struct grub_btrfs_root_item ri;
+
+  err = lower_bound(data, &key_in, &key_out, tree,
+                    &elemaddr, &elemsize, &desc, 0);
+
+  if (err)
+    return err;
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM || elemaddr == 0)
+    return grub_error(GRUB_ERR_FILE_NOT_FOUND,
+                    N_("can't find fs root for subvol %"PRIuGRUB_UINT64_T"\n"),
+                    key_in.object_id);
+
+  err = grub_btrfs_read_logical (data, elemaddr, &ri, sizeof (ri), 0);
+  if (err)
+    return err;
+
+  *fs_root = ri.tree;
+
+  return GRUB_ERR_NONE;
+}
+
+static const struct grub_arg_option options[] = {
+  {"output", 'o', 0, N_("Output to a variable instead of the console."),
+   N_("VARNAME"), ARG_TYPE_STRING},
+  {"path-only", 'p', 0, N_("Show only the path of the subvolume."), 0, 0},
+  {"id-only", 'i', 0, N_("Show only the id of the subvolume."), 0, 0},
+  {0, 0, 0, 0, 0, 0}
+};
+
+static grub_err_t
+grub_cmd_btrfs_list_subvols (struct grub_extcmd_context *ctxt,
+			     int argc, char **argv)
+{
+  struct grub_btrfs_data *data;
+  grub_device_t dev;
+  char *devname;
+  grub_uint64_t tree;
+  struct grub_btrfs_key key_in = {
+    .object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_FS_TREE_OBJECTID),
+    .type = GRUB_BTRFS_ROOT_REF_KEY,
+    .offset = 0,
+  }, key_out;
+  struct grub_btrfs_leaf_descriptor desc;
+  grub_disk_addr_t elemaddr;
+  grub_uint64_t fs_root = 0;
+  grub_size_t elemsize;
+  grub_size_t allocated = 0;
+  int r = 0;
+  grub_err_t err;
+  char *buf = NULL;
+  int print = 1;
+  int path_only = ctxt->state[1].set;
+  int num_only = ctxt->state[2].set;
+  char *varname = NULL;
+  char *output = NULL;
+
+  if (ctxt->state[0].set) {
+    varname = ctxt->state[0].arg;
+    print = 0;
+  }
+
+  if (argc < 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
+
+  devname = grub_file_get_device_name(argv[0]);
+  if (!devname)
+    return grub_errno;
+
+  dev = grub_device_open (devname);
+  grub_free (devname);
+  if (!dev)
+    return grub_errno;
+
+  data = grub_btrfs_mount(dev);
+  if (!data)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "could not open device");
+
+  tree = data->sblock.root_tree;
+  err = get_fs_root(data, tree, grub_cpu_to_le64_compile_time (GRUB_BTRFS_FS_TREE_OBJECTID),
+                    0, &fs_root);
+  if (err)
+    goto out;
+
+  err = lower_bound(data, &key_in, &key_out, tree,
+                    &elemaddr, &elemsize, &desc, 0);
+
+  if (err)
+    {
+      grub_btrfs_unmount(data);
+      return err;
+    }
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF || elemaddr == 0)
+    {
+      r = next(data, &desc, &elemaddr, &elemsize, &key_out);
+    }
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF) {
+    err = GRUB_ERR_FILE_NOT_FOUND;
+    grub_error(GRUB_ERR_FILE_NOT_FOUND, N_("can't find root refs"));
+    goto out;
+  }
+
+  do
+    {
+      struct grub_btrfs_root_ref *ref;
+      char *p = NULL;
+
+      if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF)
+        {
+          r = 0;
+          break;
+        }
+
+      if (elemsize > allocated)
+        {
+          grub_free(buf);
+          allocated = 2 * elemsize;
+          buf = grub_malloc(allocated + 1);
+          if (!buf)
+            {
+              r = -grub_errno;
+              break;
+            }
+        }
+      ref = (struct grub_btrfs_root_ref *)buf;
+
+      err = grub_btrfs_read_logical(data, elemaddr, buf, elemsize, 0);
+      if (err)
+        {
+          r = -err;
+          break;
+        }
+        buf[elemsize] = 0;
+
+      find_pathname(data, ref->dirid, fs_root, ref->name, &p);
+
+      if (print)
+        {
+          if (num_only)
+            grub_printf("ID %"PRIuGRUB_UINT64_T"\n", key_out.offset);
+          else if (path_only)
+            grub_printf("%s\n", p);
+          else
+            grub_printf("ID %"PRIuGRUB_UINT64_T" path %s\n", key_out.offset, p);
+        } else {
+          char *old = output;
+          if (num_only)
+            output = grub_xasprintf("%s%"PRIuGRUB_UINT64_T"\n",
+                                    old ?: "", key_out.offset);
+          else if (path_only)
+            output = grub_xasprintf("%s%s\n", old ?: "", p);
+          else
+            output = grub_xasprintf("%sID %"PRIuGRUB_UINT64_T" path %s\n",
+                                    old ?: "", key_out.offset, p);
+
+          if (old)
+            grub_free(old);
+        }
+
+      r = next(data, &desc, &elemaddr, &elemsize, &key_out);
+  } while(r > 0);
+
+  if (output)
+    grub_env_set(varname, output);
+
+out:
+  free_iterator(&desc);
+  grub_btrfs_unmount(data);
+
+  grub_device_close (dev);
+
+  return 0;
+}
+
 static struct grub_fs grub_btrfs_fs = {
   .name = "btrfs",
   .fs_dir = grub_btrfs_dir,
@@ -2411,12 +2847,88 @@ static struct grub_fs grub_btrfs_fs = {
 #endif
 };
 
+static grub_command_t cmd_info;
+static grub_extcmd_t cmd_list_subvols;
+
+static char *
+subvolid_set_env (struct grub_env_var *var __attribute__ ((unused)),
+                  const char *val)
+{
+  unsigned long long result = 0;
+
+  grub_errno = GRUB_ERR_NONE;
+  if (*val)
+    {
+      result = grub_strtoull(val, NULL, 10);
+      if (grub_errno)
+        return NULL;
+    }
+
+  grub_free (btrfs_default_subvol);
+  btrfs_default_subvol = NULL;
+  btrfs_default_subvolid = result;
+  return grub_strdup(val);
+}
+
+static const char *
+subvolid_get_env (struct grub_env_var *var __attribute__ ((unused)),
+                  const char *val __attribute__ ((unused)))
+{
+  if (btrfs_default_subvol)
+    return grub_xasprintf("subvol:%s", btrfs_default_subvol);
+  else if (btrfs_default_subvolid)
+    return grub_xasprintf("%"PRIuGRUB_UINT64_T, btrfs_default_subvolid);
+  else
+    return "";
+}
+
+static char *
+subvol_set_env (struct grub_env_var *var __attribute__ ((unused)),
+                const char *val)
+{
+  grub_free (btrfs_default_subvol);
+  btrfs_default_subvol = grub_strdup (val);
+  btrfs_default_subvolid = 0;
+  return grub_strdup(val);
+}
+
+static const char *
+subvol_get_env (struct grub_env_var *var __attribute__ ((unused)),
+                const char *val __attribute__ ((unused)))
+{
+  if (btrfs_default_subvol)
+    return btrfs_default_subvol;
+  else if (btrfs_default_subvolid)
+    return grub_xasprintf("subvolid:%" PRIuGRUB_UINT64_T,
+                          btrfs_default_subvolid);
+  else
+    return "";
+}
+
 GRUB_MOD_INIT (btrfs)
 {
   grub_fs_register (&grub_btrfs_fs);
+  cmd_info = grub_register_command("btrfs-info", grub_cmd_btrfs_info,
+				   "DEVICE",
+				   "Print BtrFS info about DEVICE.");
+  cmd_list_subvols = grub_register_extcmd("btrfs-list-subvols",
+					 grub_cmd_btrfs_list_subvols, 0,
+					 "[-p|-n] [-o var] DEVICE",
+					 "Print list of BtrFS subvolumes on "
+					 "DEVICE.", options);
+  grub_register_variable_hook ("btrfs_subvol", subvol_get_env,
+                               subvol_set_env);
+  grub_register_variable_hook ("btrfs_subvolid", subvolid_get_env,
+                               subvolid_set_env);
 }
 
 GRUB_MOD_FINI (btrfs)
 {
+  grub_register_variable_hook ("btrfs_subvol", NULL, NULL);
+  grub_register_variable_hook ("btrfs_subvolid", NULL, NULL);
+  grub_unregister_command (cmd_info);
+  grub_unregister_extcmd (cmd_list_subvols);
   grub_fs_unregister (&grub_btrfs_fs);
 }
+
+// vim: si et sw=2:
include/grub/btrfs.h +1 -0
index 9d93fb6c1..234ad9767 100644
@@ -29,6 +29,7 @@ enum
     GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM = 0x84,
     GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF = 0x90,
     GRUB_BTRFS_ITEM_TYPE_DEVICE = 0xd8,
+    GRUB_BTRFS_ITEM_TYPE_ROOT_REF = 0x9c,
     GRUB_BTRFS_ITEM_TYPE_CHUNK = 0xe4
   };
 
Signature · 7 lines
2.46.2


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

PATCH 28/37 btrfs: fix a bad null check

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

current gcc complains:

grub-core/fs/btrfs.c: In function ‘grub_cmd_btrfs_info’:
grub-core/fs/btrfs.c:2745:7: error: the comparison will always evaluate as ‘true’ for the address of ‘label’ will never be NULL [-Werror=address]
2745 | if (data->sblock.label)

        |       ^~~~
  grub-core/fs/btrfs.c:92:8: note: ‘label’ declared here
     92 |   char label[0x100];
        |        ^~~~~

cc1: all warnings being treated as errors

Obviously this check should be on the first data byte instead of the symbol itself.

---
 grub-core/fs/btrfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/fs/btrfs.c +1 -1
index f14fe9c1b..8e2b1e9f7 100644
@@ -2625,7 +2625,7 @@ grub_cmd_btrfs_info (grub_command_t cmd __attribute__ ((unused)), int argc,
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "failed to open fs");
     }
 
-  if (data->sblock.label)
+  if (data->sblock.label[0])
     grub_printf("Label: '%s' ", data->sblock.label);
   else
     grub_printf("Label: none ");
Signature · 7 lines
2.46.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 28/37
#17 lore raw

Re: [PATCH v1 28/37] btrfs: fix a bad null check

LGTM

Quoted 36 lines · Leo Sandoval

From: Peter Jones <[email protected]>

current gcc complains:

grub-core/fs/btrfs.c: In function ‘grub_cmd_btrfs_info’:
grub-core/fs/btrfs.c:2745:7: error: the comparison will always evaluate
as ‘true’ for the address of ‘label’ will never be NULL [-Werror=address]
2745 | if (data->sblock.label)

        |       ^~~~
  grub-core/fs/btrfs.c:92:8: note: ‘label’ declared here
     92 |   char label[0x100];
        |        ^~~~~

cc1: all warnings being treated as errors

Obviously this check should be on the first data byte instead of the symbol itself.

---
 grub-core/fs/btrfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/fs/btrfs.c +1 -1
index f14fe9c1b..8e2b1e9f7 100644
@@ -2625,7 +2625,7 @@ grub_cmd_btrfs_info (grub_command_t cmd
 __attribute__ ((unused)), int argc,
       return grub_error (GRUB_ERR_BAD_ARGUMENT, "failed to open fs");
     }
 
-  if (data->sblock.label)
+  if (data->sblock.label[0])
     grub_printf("Label: '%s' ", data->sblock.label);
   else
     grub_printf("Label: none ");
Quoted 9 lines

--
2.46.2

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

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

#18 lore raw

PATCH 29/37 btrfs: export btrfs_subvol and btrfs_subvolid

In reply to Leo Sandoval

From: Michael Chang <[email protected]>

We should export btrfs_subvol and btrfs_subvolid to have both visible to subsidiary configuration files loaded using configfile.

---
 grub-core/fs/btrfs.c | 2 ++
 1 file changed, 2 insertions(+)
grub-core/fs/btrfs.c +2 -0
index 8e2b1e9f7..14e38a4df 100644
@@ -2920,6 +2920,8 @@ GRUB_MOD_INIT (btrfs)
                                subvol_set_env);
   grub_register_variable_hook ("btrfs_subvolid", subvolid_get_env,
                                subvolid_set_env);
+  grub_env_export ("btrfs_subvol");
+  grub_env_export ("btrfs_subvolid");
 }
 
 GRUB_MOD_FINI (btrfs)
Signature · 7 lines
2.46.2


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

PATCH 30/37 btrfs: grub2-btrfs-03-follow_default

In reply to Leo Sandoval

From: Michael Chang <[email protected]>

---
 grub-core/fs/btrfs.c | 107 ++++++++++++++++++++++++++++++-------------
 1 file changed, 76 insertions(+), 31 deletions(-)
grub-core/fs/btrfs.c +76 -31
index 14e38a4df..d47f9ab03 100644
@@ -1335,6 +1335,7 @@ grub_btrfs_mount (grub_device_t dev)
 {
   struct grub_btrfs_data *data;
   grub_err_t err;
+  const char *relpath = grub_env_get ("btrfs_relative_path");
 
   if (!dev->disk)
     {
@@ -1365,11 +1366,14 @@ grub_btrfs_mount (grub_device_t dev)
   data->devices_attached[0].dev = dev;
   data->devices_attached[0].id = data->sblock.this_device.device_id;
 
-  err = btrfs_handle_subvol (data);
-  if (err)
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
     {
-      grub_free (data);
-      return NULL;
+      err = btrfs_handle_subvol (data);
+      if (err)
+      {
+        grub_free (data);
+        return NULL;
+      }
     }
 
   return data;
@@ -1966,24 +1970,39 @@ find_path (struct grub_btrfs_data *data,
   grub_size_t allocated = 0;
   struct grub_btrfs_dir_item *direl = NULL;
   struct grub_btrfs_key key_out;
+  int follow_default;
   const char *ctoken;
   grub_size_t ctokenlen;
   char *path_alloc = NULL;
   char *origpath = NULL;
   unsigned symlinks_max = 32;
+  const char *relpath = grub_env_get ("btrfs_relative_path");
 
+  follow_default = 0;
   origpath = grub_strdup (path);
   if (!origpath)
     return grub_errno;
 
-  if (data->fs_tree)
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
     {
-      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
-      *tree = data->fs_tree;
-      /* This is a tree root, so everything starts at objectid 256 */
-      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
-      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
-      key->offset = 0;
+      if (data->fs_tree)
+        {
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+          *tree = data->fs_tree;
+          /* This is a tree root, so everything starts at objectid 256 */
+          key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+          key->offset = 0;
+        }
+      else
+        {
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+          *tree = data->sblock.root_tree;
+          key->object_id = data->sblock.root_dir_objectid;
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+          key->offset = 0;
+          follow_default = 1;
+        }
     }
   else
     {
@@ -1994,15 +2013,23 @@ find_path (struct grub_btrfs_data *data,
 
   while (1)
     {
-      while (path[0] == '/')
-	path++;
-      if (!path[0])
-	break;
-      slash = grub_strchr (path, '/');
-      if (!slash)
-	slash = path + grub_strlen (path);
-      ctoken = path;
-      ctokenlen = slash - path;
+      if (!follow_default)
+	{
+	  while (path[0] == '/')
+	    path++;
+	  if (!path[0])
+	    break;
+	  slash = grub_strchr (path, '/');
+	  if (!slash)
+	    slash = path + grub_strlen (path);
+	  ctoken = path;
+	  ctokenlen = slash - path;
+	}
+      else
+	{
+	  ctoken = "default";
+	  ctokenlen = sizeof ("default") - 1;
+	}
 
       if (*type != GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
 	{
@@ -2013,7 +2040,9 @@ find_path (struct grub_btrfs_data *data,
 
       if (ctokenlen == 1 && ctoken[0] == '.')
 	{
-	  path = slash;
+	  if (!follow_default)
+	    path = slash;
+	  follow_default = 0;
 	  continue;
 	}
       if (ctokenlen == 2 && ctoken[0] == '.' && ctoken[1] == '.')
@@ -2044,8 +2073,9 @@ find_path (struct grub_btrfs_data *data,
 	  *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
 	  key->object_id = key_out.offset;
 
-	  path = slash;
-
+	  if (!follow_default)
+	    path = slash;
+	  follow_default = 0;
 	  continue;
 	}
 
@@ -2114,7 +2144,9 @@ find_path (struct grub_btrfs_data *data,
 	  return err;
 	}
 
-      path = slash;
+      if (!follow_default)
+	path = slash;
+      follow_default = 0;
       if (cdirel->type == GRUB_BTRFS_DIR_ITEM_TYPE_SYMLINK)
 	{
 	  struct grub_btrfs_inode inode;
@@ -2164,14 +2196,26 @@ find_path (struct grub_btrfs_data *data,
 	  path = path_alloc = tmp;
 	  if (path[0] == '/')
 	    {
-	      if (data->fs_tree)
+              if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
 		{
-		  *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
-		  *tree = data->fs_tree;
-		  /* This is a tree root, so everything starts at objectid 256 */
-		  key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
-		  key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
-		  key->offset = 0;
+	          if (data->fs_tree)
+		    {
+		      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+		      *tree = data->fs_tree;
+		      /* This is a tree root, so everything starts at objectid 256 */
+		      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
+		      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+		      key->offset = 0;
+		    }
+		  else
+		    {
+	              *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
+	              *tree = data->sblock.root_tree;
+	              key->object_id = data->sblock.root_dir_objectid;
+	              key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+	              key->offset = 0;
+	              follow_default = 1;
+		    }
 		}
 	      else
 		{
@@ -2922,6 +2966,7 @@ GRUB_MOD_INIT (btrfs)
                                subvolid_set_env);
   grub_env_export ("btrfs_subvol");
   grub_env_export ("btrfs_subvolid");
+  grub_env_export ("btrfs_relative_path");
 }
 
 GRUB_MOD_FINI (btrfs)
Signature · 7 lines
2.46.2


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

PATCH 31/37 btrfs: grub2-btrfs-04-grub2-install

In reply to Leo Sandoval

From: Michael Chang <[email protected]>

---
 grub-core/osdep/linux/getroot.c |  7 +++++++
 grub-core/osdep/unix/config.c   | 17 +++++++++++++++--
 include/grub/emu/config.h       |  1 +
 util/config.c                   | 10 ++++++++++
 util/grub-install.c             | 14 ++++++++++++++
 util/grub-mkrelpath.c           |  6 ++++++
 6 files changed, 53 insertions(+), 2 deletions(-)
grub-core/osdep/linux/getroot.c +7 -0
index 7dd775d2a..7c29b3523 100644
@@ -373,6 +373,7 @@ get_btrfs_fs_prefix (const char *mount_path)
   return NULL;
 }
 
+int use_relative_path_on_btrfs = 0;
 
 char **
 grub_find_root_devices_from_mountinfo (const char *dir, char **relroot)
@@ -516,6 +517,12 @@ again:
 	{
 	  ret = grub_find_root_devices_from_btrfs (dir);
 	  fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
+	  if (use_relative_path_on_btrfs)
+	    {
+	      if (fs_prefix)
+	        free (fs_prefix);
+	      fs_prefix = xstrdup ("/");
+	    }
 	}
       else if (!retry && grub_strcmp (entries[i].fstype, "autofs") == 0)
 	{
grub-core/osdep/unix/config.c +15 -2
index 0b1f7618d..0ce0e309a 100644
@@ -82,6 +82,19 @@ grub_util_load_config (struct grub_util_config *cfg)
   if (v)
     cfg->grub_distributor = xstrdup (v);
 
+  v = getenv ("SUSE_BTRFS_SNAPSHOT_BOOTING");
+  if (v)
+    {
+      if (grub_strncmp(v, "true", sizeof ("true") - 1) == 0)
+        {
+          cfg->is_suse_btrfs_snapshot_enabled = 1;
+        }
+      else
+        {
+          cfg->is_suse_btrfs_snapshot_enabled = 0;
+        }
+    }
+
   cfgfile = grub_util_get_config_filename ();
   if (!grub_util_is_regular (cfgfile))
     return;
@@ -105,8 +118,8 @@ grub_util_load_config (struct grub_util_config *cfg)
       *ptr++ = *iptr;
     }
 
-  strcpy (ptr, "'; printf \"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\n\" "
-	  "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\"");
+  strcpy (ptr, "'; printf \"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\nSUSE_BTRFS_SNAPSHOT_BOOTING=%s\\n\" "
+	  "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\" \"$SUSE_BTRFS_SNAPSHOT_BOOTING\"");
 
   argv[2] = script;
   argv[3] = '\0';
include/grub/emu/config.h +1 -0
index 875d5896c..c9a7e5f4a 100644
@@ -37,6 +37,7 @@ struct grub_util_config
 {
   int is_cryptodisk_enabled;
   char *grub_distributor;
+  int is_suse_btrfs_snapshot_enabled;
 };
 
 void
util/config.c +10 -0
index ebcdd8f5e..f044a880a 100644
@@ -42,6 +42,16 @@ grub_util_parse_config (FILE *f, struct grub_util_config *cfg, int simple)
 	    cfg->is_cryptodisk_enabled = 1;
 	  continue;
 	}
+      if (grub_strncmp (ptr, "SUSE_BTRFS_SNAPSHOT_BOOTING=",
+			sizeof ("SUSE_BTRFS_SNAPSHOT_BOOTING=") - 1) == 0)
+	{
+	  ptr += sizeof ("SUSE_BTRFS_SNAPSHOT_BOOTING=") - 1;
+	  if (*ptr == '"' || *ptr == '\'')
+	    ptr++;
+	  if (grub_strncmp(ptr, "true", sizeof ("true") - 1) == 0)
+	    cfg->is_suse_btrfs_snapshot_enabled = 1;
+	  continue;
+	}
       if (grub_strncmp (ptr, "GRUB_DISTRIBUTOR=",
 			sizeof ("GRUB_DISTRIBUTOR=") - 1) == 0)
 	{
util/grub-install.c +14 -0
index 7dc5657bb..ec3ed4967 100644
@@ -843,6 +843,8 @@ try_open (const char *path)
 }
 #endif
 
+extern int use_relative_path_on_btrfs;
+
 int
 main (int argc, char *argv[])
 {
@@ -876,6 +878,9 @@ main (int argc, char *argv[])
 
   grub_util_load_config (&config);
 
+  if (config.is_suse_btrfs_snapshot_enabled)
+    use_relative_path_on_btrfs = 1;
+
   if (!bootloader_id && config.grub_distributor)
     {
       char *ptr;
@@ -1366,6 +1371,15 @@ main (int argc, char *argv[])
       relative_grubdir = xstrdup ("/");
     }
 
+  if (config.is_suse_btrfs_snapshot_enabled
+      && grub_strncmp(grub_fs->name, "btrfs", sizeof ("btrfs") - 1) == 0)
+    {
+      if (!load_cfg_f)
+        load_cfg_f = grub_util_fopen (load_cfg, "wb");
+      have_load_cfg = 1;
+      fprintf (load_cfg_f, "set btrfs_relative_path='y'\n");
+    }
+
   char *prefix_drive = NULL;
   char *install_drive = NULL;
 
util/grub-mkrelpath.c +6 -0
index 47a241a39..5db7a9a7d 100644
@@ -40,9 +40,12 @@ struct arguments
 };
 
 static struct argp_option options[] = {
+  {"relative",  'r', 0, 0, "use relative path on btrfs", 0},
   { 0, 0, 0, 0, 0, 0 }
 };
 
+extern int use_relative_path_on_btrfs;
+
 static error_t
 argp_parser (int key, char *arg, struct argp_state *state)
 {
@@ -52,6 +55,9 @@ argp_parser (int key, char *arg, struct argp_state *state)
 
   switch (key)
     {
+    case 'r':
+      use_relative_path_on_btrfs = 1;
+      break;
     case ARGP_KEY_ARG:
       if (state->arg_num == 0)
 	arguments->pathname = xstrdup (arg);
Signature · 7 lines
2.46.2


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

Re: [PATCH v1 31/37] btrfs: grub2-btrfs-04-grub2-install

Again, what do you try to achieve? Why aren't absolute paths enough?

Quoted 179 lines · Leo Sandoval

From: Michael Chang <[email protected]>

---
 grub-core/osdep/linux/getroot.c |  7 +++++++
 grub-core/osdep/unix/config.c   | 17 +++++++++++++++--
 include/grub/emu/config.h       |  1 +
 util/config.c                   | 10 ++++++++++
 util/grub-install.c             | 14 ++++++++++++++
 util/grub-mkrelpath.c           |  6 ++++++
 6 files changed, 53 insertions(+), 2 deletions(-)
grub-core/osdep/linux/getroot.c +7 -0
index 7dd775d2a..7c29b3523 100644
@@ -373,6 +373,7 @@ get_btrfs_fs_prefix (const char *mount_path)
   return NULL;
 }
 
+int use_relative_path_on_btrfs = 0;
 
 char **
 grub_find_root_devices_from_mountinfo (const char *dir, char **relroot)
@@ -516,6 +517,12 @@ again:
        {
          ret = grub_find_root_devices_from_btrfs (dir);
          fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
+         if (use_relative_path_on_btrfs)
+           {
+             if (fs_prefix)
+               free (fs_prefix);
+             fs_prefix = xstrdup ("/");
+           }
        }
       else if (!retry && grub_strcmp (entries[i].fstype, "autofs") == 0)
        {
grub-core/osdep/unix/config.c +13 -1
index 0b1f7618d..0ce0e309a 100644
@@ -82,6 +82,19 @@ grub_util_load_config (struct grub_util_config *cfg)
   if (v)
     cfg->grub_distributor = xstrdup (v);
 
+  v = getenv ("SUSE_BTRFS_SNAPSHOT_BOOTING");
+  if (v)
+    {
+      if (grub_strncmp(v, "true", sizeof ("true") - 1) == 0)
+        {
+          cfg->is_suse_btrfs_snapshot_enabled = 1;
+        }
+      else
+        {
+          cfg->is_suse_btrfs_snapshot_enabled = 0;
+        }
+    }
+
   cfgfile = grub_util_get_config_filename ();
   if (!grub_util_is_regular (cfgfile))
     return;
@@ -105,8 +118,8 @@ grub_util_load_config (struct grub_util_config *cfg)
       *ptr++ = *iptr;
     }
 
-  strcpy (ptr, "'; printf

\"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\n\" "

grub-core/osdep/unix/config.c +1 -1
-         "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\"");
+  strcpy (ptr, "'; printf

\"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\nSUSE_BTRFS_SNAPSHOT_BOOTING=%s\\n\" "
+ "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\"
\"$SUSE_BTRFS_SNAPSHOT_BOOTING\"");

   argv[2] = script;
   argv[3] = '\0';
include/grub/emu/config.h +1 -0
index 875d5896c..c9a7e5f4a 100644
@@ -37,6 +37,7 @@ struct grub_util_config
 {
   int is_cryptodisk_enabled;
   char *grub_distributor;
+  int is_suse_btrfs_snapshot_enabled;
 };
 
 void
util/config.c +10 -0
index ebcdd8f5e..f044a880a 100644
@@ -42,6 +42,16 @@ grub_util_parse_config (FILE *f, struct
 grub_util_config *cfg, int simple)
            cfg->is_cryptodisk_enabled = 1;
          continue;
        }
+      if (grub_strncmp (ptr, "SUSE_BTRFS_SNAPSHOT_BOOTING=",
+                       sizeof ("SUSE_BTRFS_SNAPSHOT_BOOTING=") - 1) == 0)
+       {
+         ptr += sizeof ("SUSE_BTRFS_SNAPSHOT_BOOTING=") - 1;
+         if (*ptr == '"' || *ptr == '\'')
+           ptr++;
+         if (grub_strncmp(ptr, "true", sizeof ("true") - 1) == 0)
+           cfg->is_suse_btrfs_snapshot_enabled = 1;
+         continue;
+       }
       if (grub_strncmp (ptr, "GRUB_DISTRIBUTOR=",
                        sizeof ("GRUB_DISTRIBUTOR=") - 1) == 0)
        {
util/grub-install.c +14 -0
index 7dc5657bb..ec3ed4967 100644
@@ -843,6 +843,8 @@ try_open (const char *path)
 }
 #endif
 
+extern int use_relative_path_on_btrfs;
+
 int
 main (int argc, char *argv[])
 {
@@ -876,6 +878,9 @@ main (int argc, char *argv[])
 
   grub_util_load_config (&config);
 
+  if (config.is_suse_btrfs_snapshot_enabled)
+    use_relative_path_on_btrfs = 1;
+
   if (!bootloader_id && config.grub_distributor)
     {
       char *ptr;
@@ -1366,6 +1371,15 @@ main (int argc, char *argv[])
       relative_grubdir = xstrdup ("/");
     }
 
+  if (config.is_suse_btrfs_snapshot_enabled
+      && grub_strncmp(grub_fs->name, "btrfs", sizeof ("btrfs") - 1) == 0)
+    {
+      if (!load_cfg_f)
+        load_cfg_f = grub_util_fopen (load_cfg, "wb");
+      have_load_cfg = 1;
+      fprintf (load_cfg_f, "set btrfs_relative_path='y'\n");
+    }
+
   char *prefix_drive = NULL;
   char *install_drive = NULL;
 
util/grub-mkrelpath.c +6 -0
index 47a241a39..5db7a9a7d 100644
@@ -40,9 +40,12 @@ struct arguments
 };
 
 static struct argp_option options[] = {
+  {"relative",  'r', 0, 0, "use relative path on btrfs", 0},
   { 0, 0, 0, 0, 0, 0 }
 };
 
+extern int use_relative_path_on_btrfs;
+
 static error_t
 argp_parser (int key, char *arg, struct argp_state *state)
 {
@@ -52,6 +55,9 @@ argp_parser (int key, char *arg, struct argp_state
 *state)
 
   switch (key)
     {
+    case 'r':
+      use_relative_path_on_btrfs = 1;
+      break;
     case ARGP_KEY_ARG:
       if (state->arg_num == 0)
        arguments->pathname = xstrdup (arg);
Quoted 9 lines

--
2.46.2

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

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

#22 lore raw

Re: [PATCH v1 31/37] btrfs: grub2-btrfs-04-grub2-install

Quoted 1 line · Vladimir 'phcoder' Serbinenko

Again, what do you try to achieve? Why aren't absolute paths enough?

The absolute path does not align with the default subvolume. As a result, after running btrfs set-default <SUBVOLID>, the system does not boot the new default subvolume as expected. Instead, it continues to boot from a fixed subvolume.

Thanks,
Michael

Quoted 186 lines · Leo Sandoval
Quoted 184 lines

From: Michael Chang <[email protected]>

---
 grub-core/osdep/linux/getroot.c |  7 +++++++
 grub-core/osdep/unix/config.c   | 17 +++++++++++++++--
 include/grub/emu/config.h       |  1 +
 util/config.c                   | 10 ++++++++++
 util/grub-install.c             | 14 ++++++++++++++
 util/grub-mkrelpath.c           |  6 ++++++
 6 files changed, 53 insertions(+), 2 deletions(-)
grub-core/osdep/linux/getroot.c +7 -0
index 7dd775d2a..7c29b3523 100644
@@ -373,6 +373,7 @@ get_btrfs_fs_prefix (const char *mount_path)
   return NULL;
 }
 
+int use_relative_path_on_btrfs = 0;
 
 char **
 grub_find_root_devices_from_mountinfo (const char *dir, char **relroot)
@@ -516,6 +517,12 @@ again:
        {
          ret = grub_find_root_devices_from_btrfs (dir);
          fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
+         if (use_relative_path_on_btrfs)
+           {
+             if (fs_prefix)
+               free (fs_prefix);
+             fs_prefix = xstrdup ("/");
+           }
        }
       else if (!retry && grub_strcmp (entries[i].fstype, "autofs") == 0)
        {
grub-core/osdep/unix/config.c +13 -1
index 0b1f7618d..0ce0e309a 100644
@@ -82,6 +82,19 @@ grub_util_load_config (struct grub_util_config *cfg)
   if (v)
     cfg->grub_distributor = xstrdup (v);
 
+  v = getenv ("SUSE_BTRFS_SNAPSHOT_BOOTING");
+  if (v)
+    {
+      if (grub_strncmp(v, "true", sizeof ("true") - 1) == 0)
+        {
+          cfg->is_suse_btrfs_snapshot_enabled = 1;
+        }
+      else
+        {
+          cfg->is_suse_btrfs_snapshot_enabled = 0;
+        }
+    }
+
   cfgfile = grub_util_get_config_filename ();
   if (!grub_util_is_regular (cfgfile))
     return;
@@ -105,8 +118,8 @@ grub_util_load_config (struct grub_util_config *cfg)
       *ptr++ = *iptr;
     }
 
-  strcpy (ptr, "'; printf

\"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\n\" "

grub-core/osdep/unix/config.c +1 -1
-         "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\"");
+  strcpy (ptr, "'; printf

\"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\nSUSE_BTRFS_SNAPSHOT_BOOTING=%s\\n\" "
+ "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\"
\"$SUSE_BTRFS_SNAPSHOT_BOOTING\"");

   argv[2] = script;
   argv[3] = '\0';
include/grub/emu/config.h +1 -0
index 875d5896c..c9a7e5f4a 100644
@@ -37,6 +37,7 @@ struct grub_util_config
 {
   int is_cryptodisk_enabled;
   char *grub_distributor;
+  int is_suse_btrfs_snapshot_enabled;
 };
 
 void
util/config.c +10 -0
index ebcdd8f5e..f044a880a 100644
@@ -42,6 +42,16 @@ grub_util_parse_config (FILE *f, struct
 grub_util_config *cfg, int simple)
            cfg->is_cryptodisk_enabled = 1;
          continue;
        }
+      if (grub_strncmp (ptr, "SUSE_BTRFS_SNAPSHOT_BOOTING=",
+                       sizeof ("SUSE_BTRFS_SNAPSHOT_BOOTING=") - 1) == 0)
+       {
+         ptr += sizeof ("SUSE_BTRFS_SNAPSHOT_BOOTING=") - 1;
+         if (*ptr == '"' || *ptr == '\'')
+           ptr++;
+         if (grub_strncmp(ptr, "true", sizeof ("true") - 1) == 0)
+           cfg->is_suse_btrfs_snapshot_enabled = 1;
+         continue;
+       }
       if (grub_strncmp (ptr, "GRUB_DISTRIBUTOR=",
                        sizeof ("GRUB_DISTRIBUTOR=") - 1) == 0)
        {
util/grub-install.c +14 -0
index 7dc5657bb..ec3ed4967 100644
@@ -843,6 +843,8 @@ try_open (const char *path)
 }
 #endif
 
+extern int use_relative_path_on_btrfs;
+
 int
 main (int argc, char *argv[])
 {
@@ -876,6 +878,9 @@ main (int argc, char *argv[])
 
   grub_util_load_config (&config);
 
+  if (config.is_suse_btrfs_snapshot_enabled)
+    use_relative_path_on_btrfs = 1;
+
   if (!bootloader_id && config.grub_distributor)
     {
       char *ptr;
@@ -1366,6 +1371,15 @@ main (int argc, char *argv[])
       relative_grubdir = xstrdup ("/");
     }
 
+  if (config.is_suse_btrfs_snapshot_enabled
+      && grub_strncmp(grub_fs->name, "btrfs", sizeof ("btrfs") - 1) == 0)
+    {
+      if (!load_cfg_f)
+        load_cfg_f = grub_util_fopen (load_cfg, "wb");
+      have_load_cfg = 1;
+      fprintf (load_cfg_f, "set btrfs_relative_path='y'\n");
+    }
+
   char *prefix_drive = NULL;
   char *install_drive = NULL;
 
util/grub-mkrelpath.c +6 -0
index 47a241a39..5db7a9a7d 100644
@@ -40,9 +40,12 @@ struct arguments
 };
 
 static struct argp_option options[] = {
+  {"relative",  'r', 0, 0, "use relative path on btrfs", 0},
   { 0, 0, 0, 0, 0, 0 }
 };
 
+extern int use_relative_path_on_btrfs;
+
 static error_t
 argp_parser (int key, char *arg, struct argp_state *state)
 {
@@ -52,6 +55,9 @@ argp_parser (int key, char *arg, struct argp_state
 *state)
 
   switch (key)
     {
+    case 'r':
+      use_relative_path_on_btrfs = 1;
+      break;
     case ARGP_KEY_ARG:
       if (state->arg_num == 0)
        arguments->pathname = xstrdup (arg);

--
2.46.2

Quoted 10 lines
Quoted 5 lines

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

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

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

#23 lore raw

Re: [PATCH v1 31/37] btrfs: grub2-btrfs-04-grub2-install

Quoted 9 lines · Michael Chang via Grub-devel
Quoted 1 line · Vladimir 'phcoder' Serbinenko

Again, what do you try to achieve? Why aren't absolute paths enough?

The absolute path does not align with the default subvolume. As a result, after running btrfs set-default <SUBVOLID>, the system does not boot the new default subvolume as expected. Instead, it continues to boot from a fixed subvolume.

Yes, this is the intended behavior. When you change mount points or mount options they are not propagated either. We can have a command to retrieve default subvolume:
btrfs_default_subvolume --set=subvol /
linux /$subvol/boot/vmlinuz
This way you can still e.g. do operations with different subvols like:
if cmp /$subvol/grub.cfg /vol2/grub.cfg; then
...
fi

Quoted 209 lines

Thanks,
Michael

Quoted 25 lines · Leo Sandoval
Quoted 23 lines

From: Michael Chang <[email protected]>

---
 grub-core/osdep/linux/getroot.c |  7 +++++++
 grub-core/osdep/unix/config.c   | 17 +++++++++++++++--
 include/grub/emu/config.h       |  1 +
 util/config.c                   | 10 ++++++++++
 util/grub-install.c             | 14 ++++++++++++++
 util/grub-mkrelpath.c           |  6 ++++++
 6 files changed, 53 insertions(+), 2 deletions(-)
grub-core/osdep/linux/getroot.c +0 -0
index 7dd775d2a..7c29b3523 100644
@@ -373,6 +373,7 @@ get_btrfs_fs_prefix (const char *mount_path)
   return NULL;
 }
 
Quoted 4 lines

grub-core/osdep/linux/getroot.c get_btrfs_fs_prefix (const char *mount_path)

Quoted 4 lines
grub-core/osdep/linux/getroot.c +1 -0
+int use_relative_path_on_btrfs = 0;
 
 char **
 grub_find_root_devices_from_mountinfo (const char *dir, char

**relroot)

Quoted 4 lines
Quoted 4 lines
grub-core/osdep/linux/getroot.c +0 -0
@@ -516,6 +517,12 @@ again:
        {
          ret = grub_find_root_devices_from_btrfs (dir);
          fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
Quoted 8 lines

grub-core/osdep/linux/getroot.c again:

Quoted 8 lines
grub-core/osdep/linux/getroot.c +6 -0
+         if (use_relative_path_on_btrfs)
+           {
+             if (fs_prefix)
+               free (fs_prefix);
+             fs_prefix = xstrdup ("/");
+           }
        }
       else if (!retry && grub_strcmp (entries[i].fstype, "autofs") ==

0)

Quoted 2 lines

Review

Quoted 2 lines
grub-core/osdep/linux/getroot.c +0 -0
        {
(quoted diff) +0 -0

b/grub-core/osdep/unix/config.c

Quoted 16 lines
Quoted 16 lines

index 0b1f7618d..0ce0e309a 100644

grub-core/osdep/unix/config.c +9 -0
@@ -82,6 +82,19 @@ grub_util_load_config (struct grub_util_config *cfg)
   if (v)
     cfg->grub_distributor = xstrdup (v);
 
+  v = getenv ("SUSE_BTRFS_SNAPSHOT_BOOTING");
+  if (v)
+    {
+      if (grub_strncmp(v, "true", sizeof ("true") - 1) == 0)
+        {
+          cfg->is_suse_btrfs_snapshot_enabled = 1;
+        }
+      else
+        {
Quoted 8 lines

grub-core/osdep/unix/config.c grub_util_load_config (struct grub_util_config

Quoted 8 lines
grub-core/osdep/unix/config.c +4 -0
+          cfg->is_suse_btrfs_snapshot_enabled = 0;
+        }
+    }
+
   cfgfile = grub_util_get_config_filename ();
   if (!grub_util_is_regular (cfgfile))
     return;
@@ -105,8 +118,8 @@ grub_util_load_config (struct grub_util_config

*cfg)

Quoted 8 lines

grub-core/osdep/unix/config.c

Quoted 8 lines
grub-core/osdep/unix/config.c +0 -1
       *ptr++ = *iptr;
     }
 
-  strcpy (ptr, "'; printf

\"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\n\" "

grub-core/osdep/unix/config.c +1 -1
-         "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\"");
+  strcpy (ptr, "'; printf

\"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\nSUSE_BTRFS_SNAPSHOT_BOOTING=%s\\n\"

Quoted 17 lines
Quoted 17 lines

"
+ "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\"
\"$SUSE_BTRFS_SNAPSHOT_BOOTING\"");

   argv[2] = script;
   argv[3] = '\0';
include/grub/emu/config.h +1 -0
index 875d5896c..c9a7e5f4a 100644
@@ -37,6 +37,7 @@ struct grub_util_config
 {
   int is_cryptodisk_enabled;
   char *grub_distributor;
+  int is_suse_btrfs_snapshot_enabled;
 };
 
Quoted 12 lines

util/config.c grub_util_parse_config (FILE *f, struct · line 47

Quoted 12 lines
include/grub/emu/config.h +0 -0
 void
util/config.c +2 -0
index ebcdd8f5e..f044a880a 100644
@@ -42,6 +42,16 @@ grub_util_parse_config (FILE *f, struct
 grub_util_config *cfg, int simple)
            cfg->is_cryptodisk_enabled = 1;
          continue;
        }
+      if (grub_strncmp (ptr, "SUSE_BTRFS_SNAPSHOT_BOOTING=",
+                       sizeof ("SUSE_BTRFS_SNAPSHOT_BOOTING=") - 1)

== 0)

Quoted 38 lines
Quoted 38 lines
util/config.c +8 -0
+       {
+         ptr += sizeof ("SUSE_BTRFS_SNAPSHOT_BOOTING=") - 1;
+         if (*ptr == '"' || *ptr == '\'')
+           ptr++;
+         if (grub_strncmp(ptr, "true", sizeof ("true") - 1) == 0)
+           cfg->is_suse_btrfs_snapshot_enabled = 1;
+         continue;
+       }
       if (grub_strncmp (ptr, "GRUB_DISTRIBUTOR=",
                        sizeof ("GRUB_DISTRIBUTOR=") - 1) == 0)
        {
util/grub-install.c +5 -0
index 7dc5657bb..ec3ed4967 100644
@@ -843,6 +843,8 @@ try_open (const char *path)
 }
 #endif
 
+extern int use_relative_path_on_btrfs;
+
 int
 main (int argc, char *argv[])
 {
@@ -876,6 +878,9 @@ main (int argc, char *argv[])
 
   grub_util_load_config (&config);
 
+  if (config.is_suse_btrfs_snapshot_enabled)
+    use_relative_path_on_btrfs = 1;
+
   if (!bootloader_id && config.grub_distributor)
     {
       char *ptr;
@@ -1366,6 +1371,15 @@ main (int argc, char *argv[])
       relative_grubdir = xstrdup ("/");
     }
 
Quoted 2 lines

util/grub-install.c main (int argc, char *argv[]) · line 1375

Quoted 2 lines
util/grub-install.c +2 -0
+  if (config.is_suse_btrfs_snapshot_enabled
+      && grub_strncmp(grub_fs->name, "btrfs", sizeof ("btrfs") - 1)

== 0)

Quoted 42 lines
Quoted 42 lines
util/grub-install.c +7 -0
+    {
+      if (!load_cfg_f)
+        load_cfg_f = grub_util_fopen (load_cfg, "wb");
+      have_load_cfg = 1;
+      fprintf (load_cfg_f, "set btrfs_relative_path='y'\n");
+    }
+
   char *prefix_drive = NULL;
   char *install_drive = NULL;
 
util/grub-mkrelpath.c +6 -0
index 47a241a39..5db7a9a7d 100644
@@ -40,9 +40,12 @@ struct arguments
 };
 
 static struct argp_option options[] = {
+  {"relative",  'r', 0, 0, "use relative path on btrfs", 0},
   { 0, 0, 0, 0, 0, 0 }
 };
 
+extern int use_relative_path_on_btrfs;
+
 static error_t
 argp_parser (int key, char *arg, struct argp_state *state)
 {
@@ -52,6 +55,9 @@ argp_parser (int key, char *arg, struct argp_state
 *state)
 
   switch (key)
     {
+    case 'r':
+      use_relative_path_on_btrfs = 1;
+      break;
     case ARGP_KEY_ARG:
       if (state->arg_num == 0)
        arguments->pathname = xstrdup (arg);

--
2.46.2

Quoted 10 lines
Quoted 5 lines

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

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

Quoted 5 lines

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

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

#24 lore raw

Re: [PATCH v1 31/37] btrfs: grub2-btrfs-04-grub2-install

Quoted 12 lines · Vladimir 'phcoder' Serbinenko
Quoted 9 lines · Michael Chang via Grub-devel
Quoted 1 line · Vladimir 'phcoder' Serbinenko

Again, what do you try to achieve? Why aren't absolute paths enough?

The absolute path does not align with the default subvolume. As a result, after running btrfs set-default <SUBVOLID>, the system does not boot the new default subvolume as expected. Instead, it continues to boot from a fixed subvolume.

Quoted 9 lines

Yes, this is the intended behavior. When you change mount points or mount options they are not propagated either. We can have a command to retrieve default subvolume:
btrfs_default_subvolume --set=subvol /
linux /$subvol/boot/vmlinuz
This way you can still e.g. do operations with different subvols like:
if cmp /$subvol/grub.cfg /vol2/grub.cfg; then
...
fi

I think I did try the approach above, but in the end, I gave up due to some problem I can't recall right now. Anyway, I think I will revisit it to see if it can replace the relative path scheme.

One of the bigger issues or challenges that comes to mind is that grub-mkrelpath can only output a 'path,' and for it to output /$subvol/boot/vmlinuz, it would need to output a 'relative path' with a /$subvol prefix. The /$subvol handling is currently missing in grub2-mkconfig and all relevant tools: for example, grub2-mkimage -p ... or early load.cfg may only reference a path without a defined $subvol, if they aimed to follow default is easy to go wrong.

Thanks,
Michael

Quoted 216 lines
Quoted 210 lines

Thanks,
Michael

Quoted 25 lines · Leo Sandoval
Quoted 23 lines

From: Michael Chang <[email protected]>

---
 grub-core/osdep/linux/getroot.c |  7 +++++++
 grub-core/osdep/unix/config.c   | 17 +++++++++++++++--
 include/grub/emu/config.h       |  1 +
 util/config.c                   | 10 ++++++++++
 util/grub-install.c             | 14 ++++++++++++++
 util/grub-mkrelpath.c           |  6 ++++++
 6 files changed, 53 insertions(+), 2 deletions(-)
grub-core/osdep/linux/getroot.c +0 -0
index 7dd775d2a..7c29b3523 100644
@@ -373,6 +373,7 @@ get_btrfs_fs_prefix (const char *mount_path)
   return NULL;
 }
 
Quoted 4 lines

grub-core/osdep/linux/getroot.c get_btrfs_fs_prefix (const char *mount_path)

Quoted 4 lines
grub-core/osdep/linux/getroot.c +1 -0
+int use_relative_path_on_btrfs = 0;
 
 char **
 grub_find_root_devices_from_mountinfo (const char *dir, char

**relroot)

Quoted 4 lines
Quoted 4 lines
grub-core/osdep/linux/getroot.c +0 -0
@@ -516,6 +517,12 @@ again:
        {
          ret = grub_find_root_devices_from_btrfs (dir);
          fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
Quoted 8 lines

grub-core/osdep/linux/getroot.c again:

Quoted 8 lines
grub-core/osdep/linux/getroot.c +6 -0
+         if (use_relative_path_on_btrfs)
+           {
+             if (fs_prefix)
+               free (fs_prefix);
+             fs_prefix = xstrdup ("/");
+           }
        }
       else if (!retry && grub_strcmp (entries[i].fstype, "autofs") ==

0)

Quoted 2 lines

Review

Quoted 2 lines
grub-core/osdep/linux/getroot.c +0 -0
        {
(quoted diff) +0 -0

b/grub-core/osdep/unix/config.c

Quoted 16 lines
Quoted 16 lines

index 0b1f7618d..0ce0e309a 100644

grub-core/osdep/unix/config.c +9 -0
@@ -82,6 +82,19 @@ grub_util_load_config (struct grub_util_config *cfg)
   if (v)
     cfg->grub_distributor = xstrdup (v);
 
+  v = getenv ("SUSE_BTRFS_SNAPSHOT_BOOTING");
+  if (v)
+    {
+      if (grub_strncmp(v, "true", sizeof ("true") - 1) == 0)
+        {
+          cfg->is_suse_btrfs_snapshot_enabled = 1;
+        }
+      else
+        {
Quoted 8 lines

grub-core/osdep/unix/config.c grub_util_load_config (struct grub_util_config

Quoted 8 lines
grub-core/osdep/unix/config.c +4 -0
+          cfg->is_suse_btrfs_snapshot_enabled = 0;
+        }
+    }
+
   cfgfile = grub_util_get_config_filename ();
   if (!grub_util_is_regular (cfgfile))
     return;
@@ -105,8 +118,8 @@ grub_util_load_config (struct grub_util_config

*cfg)

Quoted 8 lines

grub-core/osdep/unix/config.c

Quoted 8 lines
grub-core/osdep/unix/config.c +0 -1
       *ptr++ = *iptr;
     }
 
-  strcpy (ptr, "'; printf

\"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\n\" "

grub-core/osdep/unix/config.c +1 -1
-         "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\"");
+  strcpy (ptr, "'; printf

\"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\nSUSE_BTRFS_SNAPSHOT_BOOTING=%s\\n\"

Quoted 17 lines
Quoted 17 lines

"
+ "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\"
\"$SUSE_BTRFS_SNAPSHOT_BOOTING\"");

   argv[2] = script;
   argv[3] = '\0';
include/grub/emu/config.h +1 -0
index 875d5896c..c9a7e5f4a 100644
@@ -37,6 +37,7 @@ struct grub_util_config
 {
   int is_cryptodisk_enabled;
   char *grub_distributor;
+  int is_suse_btrfs_snapshot_enabled;
 };
 
Quoted 12 lines

util/config.c grub_util_parse_config (FILE *f, struct · line 47

Quoted 12 lines
include/grub/emu/config.h +0 -0
 void
util/config.c +2 -0
index ebcdd8f5e..f044a880a 100644
@@ -42,6 +42,16 @@ grub_util_parse_config (FILE *f, struct
 grub_util_config *cfg, int simple)
            cfg->is_cryptodisk_enabled = 1;
          continue;
        }
+      if (grub_strncmp (ptr, "SUSE_BTRFS_SNAPSHOT_BOOTING=",
+                       sizeof ("SUSE_BTRFS_SNAPSHOT_BOOTING=") - 1)

== 0)

Quoted 38 lines
Quoted 38 lines
util/config.c +8 -0
+       {
+         ptr += sizeof ("SUSE_BTRFS_SNAPSHOT_BOOTING=") - 1;
+         if (*ptr == '"' || *ptr == '\'')
+           ptr++;
+         if (grub_strncmp(ptr, "true", sizeof ("true") - 1) == 0)
+           cfg->is_suse_btrfs_snapshot_enabled = 1;
+         continue;
+       }
       if (grub_strncmp (ptr, "GRUB_DISTRIBUTOR=",
                        sizeof ("GRUB_DISTRIBUTOR=") - 1) == 0)
        {
util/grub-install.c +5 -0
index 7dc5657bb..ec3ed4967 100644
@@ -843,6 +843,8 @@ try_open (const char *path)
 }
 #endif
 
+extern int use_relative_path_on_btrfs;
+
 int
 main (int argc, char *argv[])
 {
@@ -876,6 +878,9 @@ main (int argc, char *argv[])
 
   grub_util_load_config (&config);
 
+  if (config.is_suse_btrfs_snapshot_enabled)
+    use_relative_path_on_btrfs = 1;
+
   if (!bootloader_id && config.grub_distributor)
     {
       char *ptr;
@@ -1366,6 +1371,15 @@ main (int argc, char *argv[])
       relative_grubdir = xstrdup ("/");
     }
 
Quoted 2 lines

util/grub-install.c main (int argc, char *argv[]) · line 1375

Quoted 2 lines
util/grub-install.c +2 -0
+  if (config.is_suse_btrfs_snapshot_enabled
+      && grub_strncmp(grub_fs->name, "btrfs", sizeof ("btrfs") - 1)

== 0)

Quoted 42 lines
Quoted 42 lines
util/grub-install.c +7 -0
+    {
+      if (!load_cfg_f)
+        load_cfg_f = grub_util_fopen (load_cfg, "wb");
+      have_load_cfg = 1;
+      fprintf (load_cfg_f, "set btrfs_relative_path='y'\n");
+    }
+
   char *prefix_drive = NULL;
   char *install_drive = NULL;
 
util/grub-mkrelpath.c +6 -0
index 47a241a39..5db7a9a7d 100644
@@ -40,9 +40,12 @@ struct arguments
 };
 
 static struct argp_option options[] = {
+  {"relative",  'r', 0, 0, "use relative path on btrfs", 0},
   { 0, 0, 0, 0, 0, 0 }
 };
 
+extern int use_relative_path_on_btrfs;
+
 static error_t
 argp_parser (int key, char *arg, struct argp_state *state)
 {
@@ -52,6 +55,9 @@ argp_parser (int key, char *arg, struct argp_state
 *state)
 
   switch (key)
     {
+    case 'r':
+      use_relative_path_on_btrfs = 1;
+      break;
     case ARGP_KEY_ARG:
       if (state->arg_num == 0)
        arguments->pathname = xstrdup (arg);

--
2.46.2

Quoted 10 lines
Quoted 5 lines

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

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

Quoted 5 lines

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

Quoted 4 lines

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

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

#25 lore raw

PATCH 32/37 btdfs: grub2-btrfs-05-grub2-mkconfig

In reply to Leo Sandoval

From: Michael Chang <[email protected]>

---
 util/grub-mkconfig.in       |  3 ++-
 util/grub-mkconfig_lib.in   |  4 ++++
 util/grub.d/00_header.in    | 25 ++++++++++++++++++++++++-
 util/grub.d/10_linux.in     |  4 ++++
 util/grub.d/20_linux_xen.in |  4 ++++
 5 files changed, 38 insertions(+), 2 deletions(-)
util/grub-mkconfig.in +2 -1
index 91f761331..ad5b778e3 100644
@@ -259,7 +259,8 @@ export GRUB_DEFAULT \
   GRUB_BADRAM \
   GRUB_OS_PROBER_SKIP_LIST \
   GRUB_DISABLE_SUBMENU \
-  GRUB_DEFAULT_DTB
+  GRUB_DEFAULT_DTB \
+  SUSE_BTRFS_SNAPSHOT_BOOTING
 
 if test "x${grub_cfg}" != "x"; then
   rm -f "${grub_cfg}.new"
util/grub-mkconfig_lib.in +4 -0
index 33e1750ae..0ba0e0e1c 100644
@@ -49,7 +49,11 @@ grub_warn ()
 
 make_system_path_relative_to_its_root ()
 {
+  if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ] ; then
+  "${grub_mkrelpath}" -r "$1"
+  else
   "${grub_mkrelpath}" "$1"
+  fi
 }
 
 is_path_readable_by_grub ()
util/grub.d/00_header.in +24 -1
index c2d8b0937..3e1b77265 100644
@@ -27,6 +27,14 @@ export TEXTDOMAINDIR="@localedir@"
 
 . "$pkgdatadir/grub-mkconfig_lib"
 
+if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ] &&
+   [ "x${GRUB_FS}" = "xbtrfs" ] ; then
+    cat <<EOF
+set btrfs_relative_path="y"
+export btrfs_relative_path
+EOF
+fi
+
 # Do this as early as possible, since other commands might depend on it.
 # (e.g. the `loadfont' command might need lvm or raid modules)
 for i in ${GRUB_PRELOAD_MODULES} ; do
@@ -45,7 +53,9 @@ if [ "x${GRUB_TIMEOUT_BUTTON}" = "x" ] ; then GRUB_TIMEOUT_BUTTON="$GRUB_TIMEOUT
 cat << EOF
 set pager=1
 
-if [ -s \$prefix/grubenv ]; then
+if [ -f \${config_directory}/grubenv ]; then
+  load_env -f \${config_directory}/grubenv
+elif [ -s \$prefix/grubenv ]; then
   load_env
 fi
 EOF
@@ -356,3 +366,16 @@ fi
 if [ "x${GRUB_BADRAM}" != "x" ] ; then
   echo "badram ${GRUB_BADRAM}"
 fi
+
+if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ] &&
+   [ "x${GRUB_ENABLE_BLSCFG}" = "xtrue" ] &&
+   [ "x${GRUB_FS}" = "xbtrfs" ] ; then
+    # Note: No $snapshot_num on *read-only* rollback!  (bsc#901487)
+    cat <<EOF
+if [ -n "\$extra_cmdline" ]; then
+  submenu "Bootable snapshot #\$snapshot_num" {
+    menuentry "If OK, run 'snapper rollback' and reboot." { true; }
+  }
+fi
+EOF
+fi
util/grub.d/10_linux.in +4 -0
index b94af4a8d..01d89461e 100644
@@ -68,10 +68,14 @@ fi
 
 case x"$GRUB_FS" in
     xbtrfs)
+	if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ]; then
+	GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} \${extra_cmdline}"
+	else
 	rootsubvol="`make_system_path_relative_to_its_root /`"
 	rootsubvol="${rootsubvol#/}"
 	if [ "x${rootsubvol}" != x ]; then
 	    GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
+	fi
 	fi;;
     xzfs)
 	rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
util/grub.d/20_linux_xen.in +4 -0
index 94dd8be13..400f88325 100644
@@ -75,10 +75,14 @@ fi
 
 case x"$GRUB_FS" in
     xbtrfs)
+	if [ "x${SUSE_BTRFS_SNAPSHOT_BOOTING}" = "xtrue" ]; then
+	GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} \${extra_cmdline}"
+	else
 	rootsubvol="`make_system_path_relative_to_its_root /`"
 	rootsubvol="${rootsubvol#/}"
 	if [ "x${rootsubvol}" != x ]; then
 	    GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
+	fi
 	fi;;
     xzfs)
 	rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
Signature · 7 lines
2.46.2


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

PATCH 33/37 btrfs: grub2-btrfs-06-subvol-mount

In reply to Leo Sandoval

From: Michael Chang <[email protected]>

---
 grub-core/fs/btrfs.c            | 195 +++++++++++++++++++++++++++++++-
 grub-core/osdep/linux/getroot.c | 148 +++++++++++++++++++++++-
 include/grub/emu/getroot.h      |   5 +
 util/grub-install.c             |  49 ++++++++
 4 files changed, 392 insertions(+), 5 deletions(-)
grub-core/fs/btrfs.c +193 -2
index d47f9ab03..d44a1c73b 100644
@@ -41,6 +41,7 @@
 #include <grub/command.h>
 #include <grub/env.h>
 #include <grub/extcmd.h>
+#include <grub/list.h>
 #include <grub/crypto.h>
 #include <grub/diskfilter.h>
 #include <grub/safemath.h>
@@ -266,6 +267,12 @@ static grub_err_t
 grub_btrfs_read_logical (struct grub_btrfs_data *data,
 			 grub_disk_addr_t addr, void *buf, grub_size_t size,
 			 int recursion_depth);
+static grub_err_t
+get_root (struct grub_btrfs_data *data, struct grub_btrfs_key *key,
+	  grub_uint64_t *tree, grub_uint8_t *type);
+
+grub_uint64_t
+find_mtab_subvol_tree (const char *path, char **path_in_subvol);
 
 static grub_err_t
 read_sblock (grub_disk_t disk, struct grub_btrfs_superblock *sb)
@@ -1302,9 +1309,26 @@ lookup_root_by_name(struct grub_btrfs_data *data, const char *path)
   grub_err_t err;
   grub_uint64_t tree = 0;
   grub_uint8_t type;
+  grub_uint64_t saved_tree;
   struct grub_btrfs_key key;
 
+  if (path[0] == '\0')
+    {
+      data->fs_tree = 0;
+      return GRUB_ERR_NONE;
+    }
+
+  err = get_root (data, &key, &tree, &type);
+  if (err)
+    return err;
+
+  saved_tree = data->fs_tree;
+  data->fs_tree = tree;
+
   err = find_path (data, path, &key, &tree, &type);
+
+  data->fs_tree = saved_tree;
+
   if (err)
       return grub_error(GRUB_ERR_FILE_NOT_FOUND, "couldn't locate %s\n", path);
 
@@ -2316,11 +2340,20 @@ grub_btrfs_dir (grub_device_t device, const char *path,
   grub_uint64_t tree;
   grub_uint8_t type;
   grub_size_t est_size = 0;
+  char *new_path = NULL;
 
   if (!data)
     return grub_errno;
 
-  err = find_path (data, path, &key_in, &tree, &type);
+  tree = find_mtab_subvol_tree (path, &new_path);
+
+  if (tree)
+    data->fs_tree = tree;
+
+  err = find_path (data, new_path ? new_path : path, &key_in, &tree, &type);
+  if (new_path)
+    grub_free (new_path);
+
   if (err)
     {
       grub_btrfs_unmount (data);
@@ -2447,11 +2480,21 @@ grub_btrfs_open (struct grub_file *file, const char *name)
   struct grub_btrfs_inode inode;
   grub_uint8_t type;
   struct grub_btrfs_key key_in;
+  grub_uint64_t tree;
+  char *new_path = NULL;
 
   if (!data)
     return grub_errno;
 
-  err = find_path (data, name, &key_in, &data->tree, &type);
+  tree = find_mtab_subvol_tree (name, &new_path);
+
+  if (tree)
+    data->fs_tree = tree;
+
+  err = find_path (data, new_path ? new_path : name, &key_in, &data->tree, &type);
+  if (new_path)
+    grub_free (new_path);
+
   if (err)
     {
       grub_btrfs_unmount (data);
@@ -2686,6 +2729,150 @@ grub_cmd_btrfs_info (grub_command_t cmd __attribute__ ((unused)), int argc,
   return 0;
 }
 
+struct grub_btrfs_mtab
+{
+  struct grub_btrfs_mtab *next;
+  struct grub_btrfs_mtab **prev;
+  char *path;
+  char *subvol;
+  grub_uint64_t tree;
+};
+
+typedef struct grub_btrfs_mtab* grub_btrfs_mtab_t;
+
+static struct grub_btrfs_mtab *btrfs_mtab;
+
+#define FOR_GRUB_MTAB(var) FOR_LIST_ELEMENTS (var, btrfs_mtab)
+#define FOR_GRUB_MTAB_SAFE(var, next) FOR_LIST_ELEMENTS_SAFE((var), (next), btrfs_mtab)
+
+static void
+add_mountpoint (const char *path, const char *subvol, grub_uint64_t tree)
+{
+  grub_btrfs_mtab_t m = grub_malloc (sizeof (*m));
+
+  m->path = grub_strdup (path);
+  m->subvol = grub_strdup (subvol);
+  m->tree = tree;
+  grub_list_push (GRUB_AS_LIST_P (&btrfs_mtab), GRUB_AS_LIST (m));
+}
+
+static grub_err_t
+grub_cmd_btrfs_mount_subvol (grub_command_t cmd __attribute__ ((unused)), int argc,
+		     char **argv)
+{
+  char *devname, *dirname, *subvol;
+  struct grub_btrfs_key key_in;
+  grub_uint8_t type;
+  grub_uint64_t tree;
+  grub_uint64_t saved_tree;
+  grub_err_t err;
+  struct grub_btrfs_data *data = NULL;
+  grub_device_t dev = NULL;
+
+  if (argc < 3)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "required <dev> <dir> and <subvol>");
+
+  devname = grub_file_get_device_name(argv[0]);
+  dev = grub_device_open (devname);
+  grub_free (devname);
+
+  if (!dev)
+    {
+      err = grub_errno;
+      goto err_out;
+    }
+
+  dirname = argv[1];
+  subvol = argv[2];
+
+  data = grub_btrfs_mount (dev);
+  if (!data)
+    {
+      err = grub_errno;
+      goto err_out;
+    }
+
+  err = find_path (data, dirname, &key_in, &tree, &type);
+  if (err)
+    goto err_out;
+
+  if (type !=  GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
+    {
+      err = grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
+      goto err_out;
+    }
+
+  err = get_root (data, &key_in, &tree, &type);
+
+  if (err)
+    goto err_out;
+
+  saved_tree = data->fs_tree;
+  data->fs_tree = tree;
+  err = find_path (data, subvol, &key_in, &tree, &type);
+  data->fs_tree = saved_tree;
+
+  if (err)
+    goto err_out;
+
+  if (key_in.object_id != grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK) || tree == 0)
+    {
+      err = grub_error (GRUB_ERR_BAD_FILE_TYPE, "%s: not a subvolume\n", subvol);
+      goto err_out;
+    }
+
+  grub_btrfs_unmount (data);
+  grub_device_close (dev);
+  add_mountpoint (dirname, subvol, tree);
+
+  return GRUB_ERR_NONE;
+
+err_out:
+
+  if (data)
+    grub_btrfs_unmount (data);
+
+  if (dev)
+    grub_device_close (dev);
+
+  return err;
+}
+
+grub_uint64_t
+find_mtab_subvol_tree (const char *path, char **path_in_subvol)
+{
+  grub_btrfs_mtab_t m, cm;
+  grub_uint64_t tree;
+
+  if (!path || !path_in_subvol)
+    return 0;
+
+  *path_in_subvol = NULL;
+  tree = 0;
+  cm = NULL;
+
+  FOR_GRUB_MTAB (m)
+    {
+      if (grub_strncmp (path, m->path, grub_strlen (m->path)) == 0)
+	{
+	  if (!cm)
+	    cm = m;
+	  else
+	    if (grub_strcmp (m->path, cm->path) > 0)
+	      cm = m;
+	}
+    }
+
+  if (cm)
+    {
+      const char *s = path + grub_strlen (cm->path);
+      *path_in_subvol = (s[0] == '\0') ? grub_strdup ("/") : grub_strdup (s);
+      tree = cm->tree;
+    }
+
+  return tree;
+}
+
 static grub_err_t
 get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
             grub_uint64_t objectid, grub_uint64_t offset,
@@ -2892,6 +3079,7 @@ static struct grub_fs grub_btrfs_fs = {
 };
 
 static grub_command_t cmd_info;
+static grub_command_t cmd_mount_subvol;
 static grub_extcmd_t cmd_list_subvols;
 
 static char *
@@ -2955,6 +3143,9 @@ GRUB_MOD_INIT (btrfs)
   cmd_info = grub_register_command("btrfs-info", grub_cmd_btrfs_info,
 				   "DEVICE",
 				   "Print BtrFS info about DEVICE.");
+  cmd_mount_subvol = grub_register_command("btrfs-mount-subvol", grub_cmd_btrfs_mount_subvol,
+				   "DEVICE DIRECTORY SUBVOL",
+				   "Set btrfs DEVICE the DIRECTORY a mountpoint of SUBVOL.");
   cmd_list_subvols = grub_register_extcmd("btrfs-list-subvols",
 					 grub_cmd_btrfs_list_subvols, 0,
 					 "[-p|-n] [-o var] DEVICE",
grub-core/osdep/linux/getroot.c +145 -3

175 lines not on this page · raw message

include/grub/emu/getroot.h +5 -0
index 73fa2d34a..9c642ae3f 100644
@@ -53,6 +53,11 @@ char **
 grub_find_root_devices_from_mountinfo (const char *dir, char **relroot);
 #endif
 
+#ifdef __linux__
+char *
+grub_util_get_btrfs_subvol (const char *path, char **mount_path);
+#endif
+
 /* Devmapper functions provided by getroot_devmapper.c.  */
 void
 grub_util_pull_devmapper (const char *os_dev);
util/grub-install.c +49 -0

55 lines not on this page · raw message

Signature · 7 lines
2.46.2


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

Re: [PATCH v1 33/37] btrfs: grub2-btrfs-06-subvol-mount

What do you try to achieve with this that can't be achieved with using full path? We should avoid using hidden state for directory parsing. Solaris GRUB legacy used it for ZFS and it was a mess.

Quoted 558 lines · Leo Sandoval

From: Michael Chang <[email protected]>

---
 grub-core/fs/btrfs.c            | 195 +++++++++++++++++++++++++++++++-
 grub-core/osdep/linux/getroot.c | 148 +++++++++++++++++++++++-
 include/grub/emu/getroot.h      |   5 +
 util/grub-install.c             |  49 ++++++++
 4 files changed, 392 insertions(+), 5 deletions(-)
grub-core/fs/btrfs.c +7 -0
index d47f9ab03..d44a1c73b 100644
@@ -41,6 +41,7 @@
 #include <grub/command.h>
 #include <grub/env.h>
 #include <grub/extcmd.h>
+#include <grub/list.h>
 #include <grub/crypto.h>
 #include <grub/diskfilter.h>
 #include <grub/safemath.h>
@@ -266,6 +267,12 @@ static grub_err_t
 grub_btrfs_read_logical (struct grub_btrfs_data *data,
                         grub_disk_addr_t addr, void *buf, grub_size_t
 size,
                         int recursion_depth);
+static grub_err_t
+get_root (struct grub_btrfs_data *data, struct grub_btrfs_key *key,
+         grub_uint64_t *tree, grub_uint8_t *type);
+
+grub_uint64_t
+find_mtab_subvol_tree (const char *path, char **path_in_subvol);
 
 static grub_err_t

read_sblock (grub_disk_t disk, struct grub_btrfs_superblock *sb)

grub-core/fs/btrfs.c +17 -0
@@ -1302,9 +1309,26 @@ lookup_root_by_name(struct grub_btrfs_data *data,
 const char *path)
   grub_err_t err;
   grub_uint64_t tree = 0;
   grub_uint8_t type;
+  grub_uint64_t saved_tree;
   struct grub_btrfs_key key;
 
+  if (path[0] == '\0')
+    {
+      data->fs_tree = 0;
+      return GRUB_ERR_NONE;
+    }
+
+  err = get_root (data, &key, &tree, &type);
+  if (err)
+    return err;
+
+  saved_tree = data->fs_tree;
+  data->fs_tree = tree;
+
   err = find_path (data, path, &key, &tree, &type);
+
+  data->fs_tree = saved_tree;
+
   if (err)
       return grub_error(GRUB_ERR_FILE_NOT_FOUND, "couldn't locate %s\n",

path);

grub-core/fs/btrfs.c +10 -1
@@ -2316,11 +2340,20 @@ grub_btrfs_dir (grub_device_t device, const char
 *path,
   grub_uint64_t tree;
   grub_uint8_t type;
   grub_size_t est_size = 0;
+  char *new_path = NULL;
 
   if (!data)
     return grub_errno;
 
-  err = find_path (data, path, &key_in, &tree, &type);
+  tree = find_mtab_subvol_tree (path, &new_path);
+
+  if (tree)
+    data->fs_tree = tree;
+
+  err = find_path (data, new_path ? new_path : path, &key_in, &tree,
 &type);
+  if (new_path)
+    grub_free (new_path);
+
   if (err)
     {
       grub_btrfs_unmount (data);
grub-core/fs/btrfs.c +11 -1
@@ -2447,11 +2480,21 @@ grub_btrfs_open (struct grub_file *file, const
 char *name)
   struct grub_btrfs_inode inode;
   grub_uint8_t type;
   struct grub_btrfs_key key_in;
+  grub_uint64_t tree;
+  char *new_path = NULL;
 
   if (!data)
     return grub_errno;
 
-  err = find_path (data, name, &key_in, &data->tree, &type);
+  tree = find_mtab_subvol_tree (name, &new_path);
+
+  if (tree)
+    data->fs_tree = tree;
+
+  err = find_path (data, new_path ? new_path : name, &key_in,
 &data->tree, &type);
+  if (new_path)
+    grub_free (new_path);
+
   if (err)
     {
       grub_btrfs_unmount (data);
grub-core/fs/btrfs.c +42 -0
@@ -2686,6 +2729,150 @@ grub_cmd_btrfs_info (grub_command_t cmd
 __attribute__ ((unused)), int argc,
   return 0;
 }
 
+struct grub_btrfs_mtab
+{
+  struct grub_btrfs_mtab *next;
+  struct grub_btrfs_mtab **prev;
+  char *path;
+  char *subvol;
+  grub_uint64_t tree;
+};
+
+typedef struct grub_btrfs_mtab* grub_btrfs_mtab_t;
+
+static struct grub_btrfs_mtab *btrfs_mtab;
+
+#define FOR_GRUB_MTAB(var) FOR_LIST_ELEMENTS (var, btrfs_mtab)
+#define FOR_GRUB_MTAB_SAFE(var, next) FOR_LIST_ELEMENTS_SAFE((var),
 (next), btrfs_mtab)
+
+static void
+add_mountpoint (const char *path, const char *subvol, grub_uint64_t tree)
+{
+  grub_btrfs_mtab_t m = grub_malloc (sizeof (*m));
+
+  m->path = grub_strdup (path);
+  m->subvol = grub_strdup (subvol);
+  m->tree = tree;
+  grub_list_push (GRUB_AS_LIST_P (&btrfs_mtab), GRUB_AS_LIST (m));
+}
+
+static grub_err_t
+grub_cmd_btrfs_mount_subvol (grub_command_t cmd __attribute__ ((unused)),
 int argc,
+                    char **argv)
+{
+  char *devname, *dirname, *subvol;
+  struct grub_btrfs_key key_in;
+  grub_uint8_t type;
+  grub_uint64_t tree;
+  grub_uint64_t saved_tree;
+  grub_err_t err;
+  struct grub_btrfs_data *data = NULL;
+  grub_device_t dev = NULL;
+
+  if (argc < 3)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "required <dev> <dir> and

<subvol>");

grub-core/fs/btrfs.c +45 -0
+
+  devname = grub_file_get_device_name(argv[0]);
+  dev = grub_device_open (devname);
+  grub_free (devname);
+
+  if (!dev)
+    {
+      err = grub_errno;
+      goto err_out;
+    }
+
+  dirname = argv[1];
+  subvol = argv[2];
+
+  data = grub_btrfs_mount (dev);
+  if (!data)
+    {
+      err = grub_errno;
+      goto err_out;
+    }
+
+  err = find_path (data, dirname, &key_in, &tree, &type);
+  if (err)
+    goto err_out;
+
+  if (type !=  GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
+    {
+      err = grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
+      goto err_out;
+    }
+
+  err = get_root (data, &key_in, &tree, &type);
+
+  if (err)
+    goto err_out;
+
+  saved_tree = data->fs_tree;
+  data->fs_tree = tree;
+  err = find_path (data, subvol, &key_in, &tree, &type);
+  data->fs_tree = saved_tree;
+
+  if (err)
+    goto err_out;
+
+  if (key_in.object_id != grub_cpu_to_le64_compile_time

(GRUB_BTRFS_OBJECT_ID_CHUNK) || tree == 0)

grub-core/fs/btrfs.c +2 -0
+    {
+      err = grub_error (GRUB_ERR_BAD_FILE_TYPE, "%s: not a subvolume\n",

subvol);

grub-core/fs/btrfs.c +49 -0
+      goto err_out;
+    }
+
+  grub_btrfs_unmount (data);
+  grub_device_close (dev);
+  add_mountpoint (dirname, subvol, tree);
+
+  return GRUB_ERR_NONE;
+
+err_out:
+
+  if (data)
+    grub_btrfs_unmount (data);
+
+  if (dev)
+    grub_device_close (dev);
+
+  return err;
+}
+
+grub_uint64_t
+find_mtab_subvol_tree (const char *path, char **path_in_subvol)
+{
+  grub_btrfs_mtab_t m, cm;
+  grub_uint64_t tree;
+
+  if (!path || !path_in_subvol)
+    return 0;
+
+  *path_in_subvol = NULL;
+  tree = 0;
+  cm = NULL;
+
+  FOR_GRUB_MTAB (m)
+    {
+      if (grub_strncmp (path, m->path, grub_strlen (m->path)) == 0)
+       {
+         if (!cm)
+           cm = m;
+         else
+           if (grub_strcmp (m->path, cm->path) > 0)
+             cm = m;
+       }
+    }
+
+  if (cm)
+    {
+      const char *s = path + grub_strlen (cm->path);
+      *path_in_subvol = (s[0] == '\0') ? grub_strdup ("/") : grub_strdup

(s);

grub-core/fs/btrfs.c +10 -0
+      tree = cm->tree;
+    }
+
+  return tree;
+}
+
 static grub_err_t
 get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
             grub_uint64_t objectid, grub_uint64_t offset,
@@ -2892,6 +3079,7 @@ static struct grub_fs grub_btrfs_fs = {
 };
 
 static grub_command_t cmd_info;
+static grub_command_t cmd_mount_subvol;
 static grub_extcmd_t cmd_list_subvols;
 
 static char *
@@ -2955,6 +3143,9 @@ GRUB_MOD_INIT (btrfs)
   cmd_info = grub_register_command("btrfs-info", grub_cmd_btrfs_info,
                                   "DEVICE",
                                   "Print BtrFS info about DEVICE.");
+  cmd_mount_subvol = grub_register_command("btrfs-mount-subvol",
 grub_cmd_btrfs_mount_subvol,
+                                  "DEVICE DIRECTORY SUBVOL",
+                                  "Set btrfs DEVICE the DIRECTORY a
 mountpoint of SUBVOL.");
   cmd_list_subvols = grub_register_extcmd("btrfs-list-subvols",
                                         grub_cmd_btrfs_list_subvols, 0,
                                         "[-p|-n] [-o var] DEVICE",
grub-core/osdep/linux/getroot.c +145 -3
index 7c29b3523..74a48c030 100644
@@ -103,6 +103,14 @@ struct btrfs_ioctl_search_key
   grub_uint32_t unused[9];
 };
 
+struct btrfs_ioctl_search_header {
+  grub_uint64_t transid;
+  grub_uint64_t objectid;
+  grub_uint64_t offset;
+  grub_uint32_t type;
+  grub_uint32_t len;
+};
+
 struct btrfs_ioctl_search_args {
   struct btrfs_ioctl_search_key key;
   grub_uint64_t buf[(4096 - sizeof(struct btrfs_ioctl_search_key))
@@ -375,6 +383,109 @@ get_btrfs_fs_prefix (const char *mount_path)
 
 int use_relative_path_on_btrfs = 0;
 
+static char *
+get_btrfs_subvol (const char *path)
+{
+  struct btrfs_ioctl_ino_lookup_args args;
+  grub_uint64_t tree_id;
+  int fd = -1;
+  char *ret = NULL;
+
+  fd = open (path, O_RDONLY);
+
+  if (fd < 0)
+    return NULL;
+
+  memset (&args, 0, sizeof(args));
+  args.objectid = GRUB_BTRFS_TREE_ROOT_OBJECTID;
+
+  if (ioctl (fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
+    goto error;
+
+  tree_id = args.treeid;
+
+  while (tree_id != GRUB_BTRFS_ROOT_VOL_OBJECTID)
+    {
+      struct btrfs_ioctl_search_args sargs;
+      struct grub_btrfs_root_backref *br;
+      struct btrfs_ioctl_search_header *search_header;
+      char *old;
+      grub_uint16_t len;
+      grub_uint64_t inode_id;
+
+      memset (&sargs, 0, sizeof(sargs));
+
+      sargs.key.tree_id = 1;
+      sargs.key.min_objectid = tree_id;
+      sargs.key.max_objectid = tree_id;
+
+      sargs.key.min_offset = 0;
+      sargs.key.max_offset = ~0ULL;
+      sargs.key.min_transid = 0;
+      sargs.key.max_transid = ~0ULL;
+      sargs.key.min_type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF;
+      sargs.key.max_type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF;
+
+      sargs.key.nr_items = 1;
+
+      if (ioctl (fd, BTRFS_IOC_TREE_SEARCH, &sargs) < 0)
+       goto error;
+
+      if (sargs.key.nr_items == 0)
+       goto error;
+
+      search_header = (struct btrfs_ioctl_search_header *)sargs.buf;
+      br = (struct grub_btrfs_root_backref *) (search_header + 1);
+
+      len = grub_le_to_cpu16 (br->n);
+      inode_id = grub_le_to_cpu64 (br->inode_id);
+      tree_id = search_header->offset;
+
+      old = ret;
+      ret = malloc (len + 1);
+      memcpy (ret, br->name, len);
+      ret[len] = '\0';
+
+      if (inode_id != GRUB_BTRFS_TREE_ROOT_OBJECTID)
+       {
+         char *s;
+
+         memset(&args, 0, sizeof(args));
+         args.treeid = search_header->offset;
+         args.objectid = inode_id;
+
+         if (ioctl (fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
+           goto error;
+
+         s = xasprintf ("%s%s", args.name, ret);
+         free (ret);
+         ret = s;
+       }
+
+      if (old)
+       {
+         char *s = xasprintf ("%s/%s", ret, old);
+         free (ret);
+         free (old);
+         ret = s;
+       }
+    }
+
+  close (fd);
+  return ret;
+
+error:
+
+  if (fd >= 0)
+    close (fd);
+  if (ret)
+    free (ret);
+
+  return NULL;
+}
+
+void (*grub_find_root_btrfs_mount_path_hook)(const char *mount_path);
+
 char **
 grub_find_root_devices_from_mountinfo (const char *dir, char **relroot)
 {
@@ -516,12 +627,15 @@ again:
       else if (grub_strcmp (entries[i].fstype, "btrfs") == 0)
        {
          ret = grub_find_root_devices_from_btrfs (dir);
-         fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
          if (use_relative_path_on_btrfs)
            {
-             if (fs_prefix)
-               free (fs_prefix);
              fs_prefix = xstrdup ("/");
+             if (grub_find_root_btrfs_mount_path_hook)
+               grub_find_root_btrfs_mount_path_hook (entries[i].enc_path);
+           }
+         else
+           {
+             fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
            }
        }
       else if (!retry && grub_strcmp (entries[i].fstype, "autofs") == 0)
@@ -1147,6 +1261,34 @@ grub_util_get_grub_dev_os (const char *os_dev)
   return grub_dev;
 }
 
+
+char *
+grub_util_get_btrfs_subvol (const char *path, char **mount_path)
+{
+  char *mp = NULL;
+
+  if (mount_path)
+    *mount_path = NULL;
+
+  auto void
+  mount_path_hook (const char *m)
+  {
+    mp = strdup (m);
+  }
+
+  grub_find_root_btrfs_mount_path_hook = mount_path_hook;
+  grub_free (grub_find_root_devices_from_mountinfo (path, NULL));
+  grub_find_root_btrfs_mount_path_hook = NULL;
+
+  if (!mp)
+    return NULL;
+
+  if (mount_path)
+    *mount_path = mp;
+
+  return get_btrfs_subvol (mp);
+}
+
 char *
 grub_make_system_path_relative_to_its_root_os (const char *path)
 {
include/grub/emu/getroot.h +5 -0
index 73fa2d34a..9c642ae3f 100644
@@ -53,6 +53,11 @@ char **
 grub_find_root_devices_from_mountinfo (const char *dir, char **relroot);
 #endif
 
+#ifdef __linux__
+char *
+grub_util_get_btrfs_subvol (const char *path, char **mount_path);
+#endif
+
 /* Devmapper functions provided by getroot_devmapper.c.  */
 void
 grub_util_pull_devmapper (const char *os_dev);
util/grub-install.c +49 -1
index ec3ed4967..d3ec2570d 100644
@@ -1615,6 +1615,55 @@ main (int argc, char *argv[])
       prefix_drive = xasprintf ("(%s)", grub_drives[0]);
     }
 
+#ifdef __linux__
+
+  if (config.is_suse_btrfs_snapshot_enabled
+      && grub_strncmp(grub_fs->name, "btrfs", sizeof ("btrfs") - 1) == 0)
+    {
+      char *subvol = NULL;
+      char *mount_path = NULL;
+      char **rootdir_devices = NULL;
+      char *rootdir_path = grub_util_path_concat (2, "/", rootdir);
+
+      if (grub_util_is_directory (rootdir_path))
+       rootdir_devices = grub_guess_root_devices (rootdir_path);
+
+      free (rootdir_path);
+
+      if (rootdir_devices && rootdir_devices[0])
+       if (grub_strcmp (rootdir_devices[0], grub_devices[0]) == 0)
+         subvol = grub_util_get_btrfs_subvol (platdir, &mount_path);
+
+      if (subvol && mount_path)
+       {
+         char *def_subvol;
+
+         def_subvol = grub_util_get_btrfs_subvol ("/", NULL);
+
+         if (def_subvol)
+           {
+             if (!load_cfg_f)
+               load_cfg_f = grub_util_fopen (load_cfg, "wb");
+             have_load_cfg = 1;
+
+             if (grub_strcmp (subvol, def_subvol) != 0)
+               fprintf (load_cfg_f, "btrfs-mount-subvol ($root) %s %s\n",
 mount_path, subvol);
+             free (def_subvol);
+           }
+       }
+
+      for (curdev = rootdir_devices; *curdev; curdev++)
+       free (*curdev);
+      if (rootdir_devices)
+       free (rootdir_devices);
+      if (subvol)
+       free (subvol);
+      if (mount_path)
+       free (mount_path);
+    }
+
+#endif
+
   char mkimage_target[200];
   const char *core_name = NULL;
 
--

2.46.2

Quoted 5 lines

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

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

#28 lore raw

Re: [PATCH v1 33/37] btrfs: grub2-btrfs-06-subvol-mount

Quoted 3 lines · Vladimir 'phcoder' Serbinenko

What do you try to achieve with this that can't be achieved with using full path? We should avoid using hidden state for directory parsing. Solaris GRUB legacy used it for ZFS and it was a mess.

If a path is relative to the default subvolume, it cannot access files outside of the default subvolume tree. This patch enables access to files in other subvolumes by mounting them to a specific directory

Thanks,
Michael

Quoted 561 lines · Leo Sandoval
Quoted 559 lines

From: Michael Chang <[email protected]>

---
 grub-core/fs/btrfs.c            | 195 +++++++++++++++++++++++++++++++-
 grub-core/osdep/linux/getroot.c | 148 +++++++++++++++++++++++-
 include/grub/emu/getroot.h      |   5 +
 util/grub-install.c             |  49 ++++++++
 4 files changed, 392 insertions(+), 5 deletions(-)
grub-core/fs/btrfs.c +7 -0
index d47f9ab03..d44a1c73b 100644
@@ -41,6 +41,7 @@
 #include <grub/command.h>
 #include <grub/env.h>
 #include <grub/extcmd.h>
+#include <grub/list.h>
 #include <grub/crypto.h>
 #include <grub/diskfilter.h>
 #include <grub/safemath.h>
@@ -266,6 +267,12 @@ static grub_err_t
 grub_btrfs_read_logical (struct grub_btrfs_data *data,
                         grub_disk_addr_t addr, void *buf, grub_size_t
 size,
                         int recursion_depth);
+static grub_err_t
+get_root (struct grub_btrfs_data *data, struct grub_btrfs_key *key,
+         grub_uint64_t *tree, grub_uint8_t *type);
+
+grub_uint64_t
+find_mtab_subvol_tree (const char *path, char **path_in_subvol);
 
 static grub_err_t

read_sblock (grub_disk_t disk, struct grub_btrfs_superblock *sb)

grub-core/fs/btrfs.c +17 -0
@@ -1302,9 +1309,26 @@ lookup_root_by_name(struct grub_btrfs_data *data,
 const char *path)
   grub_err_t err;
   grub_uint64_t tree = 0;
   grub_uint8_t type;
+  grub_uint64_t saved_tree;
   struct grub_btrfs_key key;
 
+  if (path[0] == '\0')
+    {
+      data->fs_tree = 0;
+      return GRUB_ERR_NONE;
+    }
+
+  err = get_root (data, &key, &tree, &type);
+  if (err)
+    return err;
+
+  saved_tree = data->fs_tree;
+  data->fs_tree = tree;
+
   err = find_path (data, path, &key, &tree, &type);
+
+  data->fs_tree = saved_tree;
+
   if (err)
       return grub_error(GRUB_ERR_FILE_NOT_FOUND, "couldn't locate %s\n",

path);

grub-core/fs/btrfs.c +10 -1
@@ -2316,11 +2340,20 @@ grub_btrfs_dir (grub_device_t device, const char
 *path,
   grub_uint64_t tree;
   grub_uint8_t type;
   grub_size_t est_size = 0;
+  char *new_path = NULL;
 
   if (!data)
     return grub_errno;
 
-  err = find_path (data, path, &key_in, &tree, &type);
+  tree = find_mtab_subvol_tree (path, &new_path);
+
+  if (tree)
+    data->fs_tree = tree;
+
+  err = find_path (data, new_path ? new_path : path, &key_in, &tree,
 &type);
+  if (new_path)
+    grub_free (new_path);
+
   if (err)
     {
       grub_btrfs_unmount (data);
grub-core/fs/btrfs.c +11 -1
@@ -2447,11 +2480,21 @@ grub_btrfs_open (struct grub_file *file, const
 char *name)
   struct grub_btrfs_inode inode;
   grub_uint8_t type;
   struct grub_btrfs_key key_in;
+  grub_uint64_t tree;
+  char *new_path = NULL;
 
   if (!data)
     return grub_errno;
 
-  err = find_path (data, name, &key_in, &data->tree, &type);
+  tree = find_mtab_subvol_tree (name, &new_path);
+
+  if (tree)
+    data->fs_tree = tree;
+
+  err = find_path (data, new_path ? new_path : name, &key_in,
 &data->tree, &type);
+  if (new_path)
+    grub_free (new_path);
+
   if (err)
     {
       grub_btrfs_unmount (data);
grub-core/fs/btrfs.c +42 -0
@@ -2686,6 +2729,150 @@ grub_cmd_btrfs_info (grub_command_t cmd
 __attribute__ ((unused)), int argc,
   return 0;
 }
 
+struct grub_btrfs_mtab
+{
+  struct grub_btrfs_mtab *next;
+  struct grub_btrfs_mtab **prev;
+  char *path;
+  char *subvol;
+  grub_uint64_t tree;
+};
+
+typedef struct grub_btrfs_mtab* grub_btrfs_mtab_t;
+
+static struct grub_btrfs_mtab *btrfs_mtab;
+
+#define FOR_GRUB_MTAB(var) FOR_LIST_ELEMENTS (var, btrfs_mtab)
+#define FOR_GRUB_MTAB_SAFE(var, next) FOR_LIST_ELEMENTS_SAFE((var),
 (next), btrfs_mtab)
+
+static void
+add_mountpoint (const char *path, const char *subvol, grub_uint64_t tree)
+{
+  grub_btrfs_mtab_t m = grub_malloc (sizeof (*m));
+
+  m->path = grub_strdup (path);
+  m->subvol = grub_strdup (subvol);
+  m->tree = tree;
+  grub_list_push (GRUB_AS_LIST_P (&btrfs_mtab), GRUB_AS_LIST (m));
+}
+
+static grub_err_t
+grub_cmd_btrfs_mount_subvol (grub_command_t cmd __attribute__ ((unused)),
 int argc,
+                    char **argv)
+{
+  char *devname, *dirname, *subvol;
+  struct grub_btrfs_key key_in;
+  grub_uint8_t type;
+  grub_uint64_t tree;
+  grub_uint64_t saved_tree;
+  grub_err_t err;
+  struct grub_btrfs_data *data = NULL;
+  grub_device_t dev = NULL;
+
+  if (argc < 3)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "required <dev> <dir> and

<subvol>");

grub-core/fs/btrfs.c +45 -0
+
+  devname = grub_file_get_device_name(argv[0]);
+  dev = grub_device_open (devname);
+  grub_free (devname);
+
+  if (!dev)
+    {
+      err = grub_errno;
+      goto err_out;
+    }
+
+  dirname = argv[1];
+  subvol = argv[2];
+
+  data = grub_btrfs_mount (dev);
+  if (!data)
+    {
+      err = grub_errno;
+      goto err_out;
+    }
+
+  err = find_path (data, dirname, &key_in, &tree, &type);
+  if (err)
+    goto err_out;
+
+  if (type !=  GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
+    {
+      err = grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
+      goto err_out;
+    }
+
+  err = get_root (data, &key_in, &tree, &type);
+
+  if (err)
+    goto err_out;
+
+  saved_tree = data->fs_tree;
+  data->fs_tree = tree;
+  err = find_path (data, subvol, &key_in, &tree, &type);
+  data->fs_tree = saved_tree;
+
+  if (err)
+    goto err_out;
+
+  if (key_in.object_id != grub_cpu_to_le64_compile_time

(GRUB_BTRFS_OBJECT_ID_CHUNK) || tree == 0)

grub-core/fs/btrfs.c +2 -0
+    {
+      err = grub_error (GRUB_ERR_BAD_FILE_TYPE, "%s: not a subvolume\n",

subvol);

grub-core/fs/btrfs.c +49 -0
+      goto err_out;
+    }
+
+  grub_btrfs_unmount (data);
+  grub_device_close (dev);
+  add_mountpoint (dirname, subvol, tree);
+
+  return GRUB_ERR_NONE;
+
+err_out:
+
+  if (data)
+    grub_btrfs_unmount (data);
+
+  if (dev)
+    grub_device_close (dev);
+
+  return err;
+}
+
+grub_uint64_t
+find_mtab_subvol_tree (const char *path, char **path_in_subvol)
+{
+  grub_btrfs_mtab_t m, cm;
+  grub_uint64_t tree;
+
+  if (!path || !path_in_subvol)
+    return 0;
+
+  *path_in_subvol = NULL;
+  tree = 0;
+  cm = NULL;
+
+  FOR_GRUB_MTAB (m)
+    {
+      if (grub_strncmp (path, m->path, grub_strlen (m->path)) == 0)
+       {
+         if (!cm)
+           cm = m;
+         else
+           if (grub_strcmp (m->path, cm->path) > 0)
+             cm = m;
+       }
+    }
+
+  if (cm)
+    {
+      const char *s = path + grub_strlen (cm->path);
+      *path_in_subvol = (s[0] == '\0') ? grub_strdup ("/") : grub_strdup

(s);

grub-core/fs/btrfs.c +10 -0
+      tree = cm->tree;
+    }
+
+  return tree;
+}
+
 static grub_err_t
 get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
             grub_uint64_t objectid, grub_uint64_t offset,
@@ -2892,6 +3079,7 @@ static struct grub_fs grub_btrfs_fs = {
 };
 
 static grub_command_t cmd_info;
+static grub_command_t cmd_mount_subvol;
 static grub_extcmd_t cmd_list_subvols;
 
 static char *
@@ -2955,6 +3143,9 @@ GRUB_MOD_INIT (btrfs)
   cmd_info = grub_register_command("btrfs-info", grub_cmd_btrfs_info,
                                   "DEVICE",
                                   "Print BtrFS info about DEVICE.");
+  cmd_mount_subvol = grub_register_command("btrfs-mount-subvol",
 grub_cmd_btrfs_mount_subvol,
+                                  "DEVICE DIRECTORY SUBVOL",
+                                  "Set btrfs DEVICE the DIRECTORY a
 mountpoint of SUBVOL.");
   cmd_list_subvols = grub_register_extcmd("btrfs-list-subvols",
                                         grub_cmd_btrfs_list_subvols, 0,
                                         "[-p|-n] [-o var] DEVICE",
grub-core/osdep/linux/getroot.c +145 -3
index 7c29b3523..74a48c030 100644
@@ -103,6 +103,14 @@ struct btrfs_ioctl_search_key
   grub_uint32_t unused[9];
 };
 
+struct btrfs_ioctl_search_header {
+  grub_uint64_t transid;
+  grub_uint64_t objectid;
+  grub_uint64_t offset;
+  grub_uint32_t type;
+  grub_uint32_t len;
+};
+
 struct btrfs_ioctl_search_args {
   struct btrfs_ioctl_search_key key;
   grub_uint64_t buf[(4096 - sizeof(struct btrfs_ioctl_search_key))
@@ -375,6 +383,109 @@ get_btrfs_fs_prefix (const char *mount_path)
 
 int use_relative_path_on_btrfs = 0;
 
+static char *
+get_btrfs_subvol (const char *path)
+{
+  struct btrfs_ioctl_ino_lookup_args args;
+  grub_uint64_t tree_id;
+  int fd = -1;
+  char *ret = NULL;
+
+  fd = open (path, O_RDONLY);
+
+  if (fd < 0)
+    return NULL;
+
+  memset (&args, 0, sizeof(args));
+  args.objectid = GRUB_BTRFS_TREE_ROOT_OBJECTID;
+
+  if (ioctl (fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
+    goto error;
+
+  tree_id = args.treeid;
+
+  while (tree_id != GRUB_BTRFS_ROOT_VOL_OBJECTID)
+    {
+      struct btrfs_ioctl_search_args sargs;
+      struct grub_btrfs_root_backref *br;
+      struct btrfs_ioctl_search_header *search_header;
+      char *old;
+      grub_uint16_t len;
+      grub_uint64_t inode_id;
+
+      memset (&sargs, 0, sizeof(sargs));
+
+      sargs.key.tree_id = 1;
+      sargs.key.min_objectid = tree_id;
+      sargs.key.max_objectid = tree_id;
+
+      sargs.key.min_offset = 0;
+      sargs.key.max_offset = ~0ULL;
+      sargs.key.min_transid = 0;
+      sargs.key.max_transid = ~0ULL;
+      sargs.key.min_type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF;
+      sargs.key.max_type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF;
+
+      sargs.key.nr_items = 1;
+
+      if (ioctl (fd, BTRFS_IOC_TREE_SEARCH, &sargs) < 0)
+       goto error;
+
+      if (sargs.key.nr_items == 0)
+       goto error;
+
+      search_header = (struct btrfs_ioctl_search_header *)sargs.buf;
+      br = (struct grub_btrfs_root_backref *) (search_header + 1);
+
+      len = grub_le_to_cpu16 (br->n);
+      inode_id = grub_le_to_cpu64 (br->inode_id);
+      tree_id = search_header->offset;
+
+      old = ret;
+      ret = malloc (len + 1);
+      memcpy (ret, br->name, len);
+      ret[len] = '\0';
+
+      if (inode_id != GRUB_BTRFS_TREE_ROOT_OBJECTID)
+       {
+         char *s;
+
+         memset(&args, 0, sizeof(args));
+         args.treeid = search_header->offset;
+         args.objectid = inode_id;
+
+         if (ioctl (fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
+           goto error;
+
+         s = xasprintf ("%s%s", args.name, ret);
+         free (ret);
+         ret = s;
+       }
+
+      if (old)
+       {
+         char *s = xasprintf ("%s/%s", ret, old);
+         free (ret);
+         free (old);
+         ret = s;
+       }
+    }
+
+  close (fd);
+  return ret;
+
+error:
+
+  if (fd >= 0)
+    close (fd);
+  if (ret)
+    free (ret);
+
+  return NULL;
+}
+
+void (*grub_find_root_btrfs_mount_path_hook)(const char *mount_path);
+
 char **
 grub_find_root_devices_from_mountinfo (const char *dir, char **relroot)
 {
@@ -516,12 +627,15 @@ again:
       else if (grub_strcmp (entries[i].fstype, "btrfs") == 0)
        {
          ret = grub_find_root_devices_from_btrfs (dir);
-         fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
          if (use_relative_path_on_btrfs)
            {
-             if (fs_prefix)
-               free (fs_prefix);
              fs_prefix = xstrdup ("/");
+             if (grub_find_root_btrfs_mount_path_hook)
+               grub_find_root_btrfs_mount_path_hook (entries[i].enc_path);
+           }
+         else
+           {
+             fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
            }
        }
       else if (!retry && grub_strcmp (entries[i].fstype, "autofs") == 0)
@@ -1147,6 +1261,34 @@ grub_util_get_grub_dev_os (const char *os_dev)
   return grub_dev;
 }
 
+
+char *
+grub_util_get_btrfs_subvol (const char *path, char **mount_path)
+{
+  char *mp = NULL;
+
+  if (mount_path)
+    *mount_path = NULL;
+
+  auto void
+  mount_path_hook (const char *m)
+  {
+    mp = strdup (m);
+  }
+
+  grub_find_root_btrfs_mount_path_hook = mount_path_hook;
+  grub_free (grub_find_root_devices_from_mountinfo (path, NULL));
+  grub_find_root_btrfs_mount_path_hook = NULL;
+
+  if (!mp)
+    return NULL;
+
+  if (mount_path)
+    *mount_path = mp;
+
+  return get_btrfs_subvol (mp);
+}
+
 char *
 grub_make_system_path_relative_to_its_root_os (const char *path)
 {
include/grub/emu/getroot.h +5 -0
index 73fa2d34a..9c642ae3f 100644
@@ -53,6 +53,11 @@ char **
 grub_find_root_devices_from_mountinfo (const char *dir, char **relroot);
 #endif
 
+#ifdef __linux__
+char *
+grub_util_get_btrfs_subvol (const char *path, char **mount_path);
+#endif
+
 /* Devmapper functions provided by getroot_devmapper.c.  */
 void
 grub_util_pull_devmapper (const char *os_dev);
util/grub-install.c +49 -1
index ec3ed4967..d3ec2570d 100644
@@ -1615,6 +1615,55 @@ main (int argc, char *argv[])
       prefix_drive = xasprintf ("(%s)", grub_drives[0]);
     }
 
+#ifdef __linux__
+
+  if (config.is_suse_btrfs_snapshot_enabled
+      && grub_strncmp(grub_fs->name, "btrfs", sizeof ("btrfs") - 1) == 0)
+    {
+      char *subvol = NULL;
+      char *mount_path = NULL;
+      char **rootdir_devices = NULL;
+      char *rootdir_path = grub_util_path_concat (2, "/", rootdir);
+
+      if (grub_util_is_directory (rootdir_path))
+       rootdir_devices = grub_guess_root_devices (rootdir_path);
+
+      free (rootdir_path);
+
+      if (rootdir_devices && rootdir_devices[0])
+       if (grub_strcmp (rootdir_devices[0], grub_devices[0]) == 0)
+         subvol = grub_util_get_btrfs_subvol (platdir, &mount_path);
+
+      if (subvol && mount_path)
+       {
+         char *def_subvol;
+
+         def_subvol = grub_util_get_btrfs_subvol ("/", NULL);
+
+         if (def_subvol)
+           {
+             if (!load_cfg_f)
+               load_cfg_f = grub_util_fopen (load_cfg, "wb");
+             have_load_cfg = 1;
+
+             if (grub_strcmp (subvol, def_subvol) != 0)
+               fprintf (load_cfg_f, "btrfs-mount-subvol ($root) %s %s\n",
 mount_path, subvol);
+             free (def_subvol);
+           }
+       }
+
+      for (curdev = rootdir_devices; *curdev; curdev++)
+       free (*curdev);
+      if (rootdir_devices)
+       free (rootdir_devices);
+      if (subvol)
+       free (subvol);
+      if (mount_path)
+       free (mount_path);
+    }
+
+#endif
+
   char mkimage_target[200];
   const char *core_name = NULL;
 
--

2.46.2

Quoted 10 lines
Quoted 5 lines

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

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

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

#29 lore raw

Re: [PATCH v1 33/37] btrfs: grub2-btrfs-06-subvol-mount

Quoted 10 lines · Michael Chang via Grub-devel
Quoted 3 lines · Vladimir 'phcoder' Serbinenko

What do you try to achieve with this that can't be achieved with using full path? We should avoid using hidden state for directory parsing. Solaris GRUB legacy used it for ZFS and it was a mess.

If a path is relative to the default subvolume, it cannot access files outside of the default subvolume tree. This patch enables access to files in other subvolumes by mounting them to a specific directory

This is incredibly useful functionality, especially if you have a non-hierarchical subvolume setup (sometimes called a flat setup) like Fedora and a few other distributions do. If you want snapshots to exist outside of the root subvolume or default subvolume, then this patch is needed or you cannot access them properly for boot to snapshot.

Signature · 6 lines
真実はいつも一つ!/ Always, there's only one truth!

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

PATCH 34/37 btrfs: Fallback to old subvol name scheme to support old snapshot config

In reply to Leo Sandoval

From: Andrei Borzenkov <[email protected]>

Ref: bsc#953538

---
 grub-core/fs/btrfs.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
grub-core/fs/btrfs.c +31 -1
index d44a1c73b..dba86d19b 100644
@@ -1339,11 +1339,41 @@ lookup_root_by_name(struct grub_btrfs_data *data, const char *path)
   return GRUB_ERR_NONE;
 }
 
+static grub_err_t
+lookup_root_by_name_fallback(struct grub_btrfs_data *data, const char *path)
+{
+  grub_err_t err;
+  grub_uint64_t tree = 0;
+  grub_uint8_t type;
+  struct grub_btrfs_key key;
+
+  err = find_path (data, path, &key, &tree, &type);
+  if (err)
+      return grub_error(GRUB_ERR_FILE_NOT_FOUND, "couldn't locate %s\n", path);
+
+  if (key.object_id != grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK) || tree == 0)
+    return grub_error(GRUB_ERR_BAD_FILE_TYPE, "%s: not a subvolume\n", path);
+
+  data->fs_tree = tree;
+  return GRUB_ERR_NONE;
+}
+
 static grub_err_t
 btrfs_handle_subvol(struct grub_btrfs_data *data __attribute__ ((unused)))
 {
   if (btrfs_default_subvol)
-    return lookup_root_by_name(data, btrfs_default_subvol);
+    {
+      grub_err_t err;
+      err = lookup_root_by_name(data, btrfs_default_subvol);
+
+      /* Fallback to old schemes */
+      if (err == GRUB_ERR_FILE_NOT_FOUND)
+	{
+	  err = GRUB_ERR_NONE;
+	  return lookup_root_by_name_fallback(data, btrfs_default_subvol);
+	}
+      return err;
+    }
 
   if (btrfs_default_subvolid)
     return lookup_root_by_id(data, btrfs_default_subvolid);
Signature · 7 lines
2.46.2


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

PATCH 35/37 btrfs: Grub not working correctly with btrfs snapshots (bsc#1026511)

In reply to Leo Sandoval

From: Michael Chang <[email protected]>

---
 grub-core/fs/btrfs.c | 238 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 238 insertions(+)
grub-core/fs/btrfs.c +238 -0
index dba86d19b..87e4dd1a0 100644
@@ -3093,6 +3093,238 @@ out:
   return 0;
 }
 
+static grub_err_t
+grub_btrfs_get_parent_subvol_path (struct grub_btrfs_data *data,
+		grub_uint64_t child_id,
+		const char *child_path,
+		grub_uint64_t *parent_id,
+		char **path_out)
+{
+  grub_uint64_t fs_root = 0;
+  struct grub_btrfs_key key_in = {
+    .object_id = child_id,
+    .type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF,
+    .offset = 0,
+  }, key_out;
+  struct grub_btrfs_root_ref *ref;
+  char *buf;
+  struct grub_btrfs_leaf_descriptor desc;
+  grub_size_t elemsize;
+  grub_disk_addr_t elemaddr;
+  grub_err_t err;
+  char *parent_path;
+
+  *parent_id = 0;
+  *path_out = 0;
+
+  err = lower_bound(data, &key_in, &key_out, data->sblock.root_tree,
+                    &elemaddr, &elemsize, &desc, 0);
+  if (err)
+    return err;
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF || elemaddr == 0)
+    next(data, &desc, &elemaddr, &elemsize, &key_out);
+
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF)
+    {
+      free_iterator(&desc);
+      return grub_error(GRUB_ERR_FILE_NOT_FOUND, N_("can't find root backrefs"));
+    }
+
+  buf = grub_malloc(elemsize + 1);
+  if (!buf)
+    {
+      free_iterator(&desc);
+      return grub_errno;
+    }
+
+  err = grub_btrfs_read_logical(data, elemaddr, buf, elemsize, 0);
+  if (err)
+    {
+      grub_free(buf);
+      free_iterator(&desc);
+      return err;
+    }
+
+  buf[elemsize] = 0;
+  ref = (struct grub_btrfs_root_ref *)buf;
+
+  err = get_fs_root(data, data->sblock.root_tree, grub_le_to_cpu64 (key_out.offset),
+                    0, &fs_root);
+  if (err)
+    {
+      grub_free(buf);
+      free_iterator(&desc);
+      return err;
+    }
+
+  find_pathname(data, grub_le_to_cpu64 (ref->dirid), fs_root, ref->name, &parent_path);
+
+  if (child_path)
+    {
+      *path_out = grub_xasprintf ("%s/%s", parent_path, child_path);
+      grub_free (parent_path);
+    }
+  else
+    *path_out = parent_path;
+
+  *parent_id = grub_le_to_cpu64 (key_out.offset);
+
+  grub_free(buf);
+  free_iterator(&desc);
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_btrfs_get_default_subvolume_id (struct grub_btrfs_data *data, grub_uint64_t *id)
+{
+  grub_err_t err;
+  grub_disk_addr_t elemaddr;
+  grub_size_t elemsize;
+  struct grub_btrfs_key key, key_out;
+  struct grub_btrfs_dir_item *direl = NULL;
+  const char *ctoken = "default";
+  grub_size_t ctokenlen = sizeof ("default") - 1;
+
+  *id = 0;
+  key.object_id = data->sblock.root_dir_objectid;
+  key.type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
+  key.offset = grub_cpu_to_le64 (~grub_getcrc32c (1, ctoken, ctokenlen));
+  err = lower_bound (data, &key, &key_out, data->sblock.root_tree, &elemaddr, &elemsize,
+			 NULL, 0);
+  if (err)
+    return err;
+
+  if (key_cmp (&key, &key_out) != 0)
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file not found"));
+
+  struct grub_btrfs_dir_item *cdirel;
+  direl = grub_malloc (elemsize + 1);
+  err = grub_btrfs_read_logical (data, elemaddr, direl, elemsize, 0);
+  if (err)
+    {
+      grub_free (direl);
+      return err;
+    }
+  for (cdirel = direl;
+       (grub_uint8_t *) cdirel - (grub_uint8_t *) direl
+       < (grub_ssize_t) elemsize;
+       cdirel = (void *) ((grub_uint8_t *) (direl + 1)
+       + grub_le_to_cpu16 (cdirel->n)
+       + grub_le_to_cpu16 (cdirel->m)))
+    {
+      if (ctokenlen == grub_le_to_cpu16 (cdirel->n)
+        && grub_memcmp (cdirel->name, ctoken, ctokenlen) == 0)
+      break;
+    }
+  if ((grub_uint8_t *) cdirel - (grub_uint8_t *) direl
+      >= (grub_ssize_t) elemsize)
+    {
+      grub_free (direl);
+      err = grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file not found"));
+      return err;
+    }
+
+  if (cdirel->key.type != GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM)
+    {
+      grub_free (direl);
+      err = grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file not found"));
+      return err;
+    }
+
+  *id = grub_le_to_cpu64 (cdirel->key.object_id);
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_cmd_btrfs_get_default_subvol (struct grub_extcmd_context *ctxt,
+			     int argc, char **argv)
+{
+  char *devname;
+  grub_device_t dev;
+  struct grub_btrfs_data *data;
+  grub_err_t err;
+  grub_uint64_t id;
+  char *subvol = NULL;
+  grub_uint64_t subvolid = 0;
+  char *varname = NULL;
+  char *output = NULL;
+  int path_only = ctxt->state[1].set;
+  int num_only = ctxt->state[2].set;
+
+  if (ctxt->state[0].set)
+    varname = ctxt->state[0].arg;
+
+  if (argc < 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
+
+  devname = grub_file_get_device_name(argv[0]);
+  if (!devname)
+    return grub_errno;
+
+  dev = grub_device_open (devname);
+  grub_free (devname);
+  if (!dev)
+    return grub_errno;
+
+  data = grub_btrfs_mount(dev);
+  if (!data)
+    {
+      grub_device_close (dev);
+      grub_dprintf ("btrfs", "failed to open fs\n");
+      grub_errno = GRUB_ERR_NONE;
+      return 0;
+    }
+
+  err = grub_btrfs_get_default_subvolume_id (data, &subvolid);
+  if (err)
+    {
+      grub_btrfs_unmount (data);
+      grub_device_close (dev);
+      return err;
+    }
+
+  id = subvolid;
+  while (id != GRUB_BTRFS_ROOT_VOL_OBJECTID)
+    {
+      grub_uint64_t parent_id;
+      char *path_out;
+
+      err = grub_btrfs_get_parent_subvol_path (data, grub_cpu_to_le64 (id), subvol, &parent_id, &path_out);
+      if (err)
+	{
+	  grub_btrfs_unmount (data);
+	  grub_device_close (dev);
+	  return err;
+	}
+
+      if (subvol)
+        grub_free (subvol);
+      subvol = path_out;
+      id = parent_id;
+    }
+
+  if (num_only && path_only)
+      output = grub_xasprintf ("%"PRIuGRUB_UINT64_T" /%s", subvolid, subvol);
+  else if (num_only)
+      output = grub_xasprintf ("%"PRIuGRUB_UINT64_T, subvolid);
+  else
+      output = grub_xasprintf ("/%s", subvol);
+
+  if (varname)
+    grub_env_set(varname, output);
+  else
+    grub_printf ("%s\n", output);
+
+  grub_free (output);
+  grub_free (subvol);
+
+  grub_btrfs_unmount (data);
+  grub_device_close (dev);
+
+  return GRUB_ERR_NONE;
+}
+
 static struct grub_fs grub_btrfs_fs = {
   .name = "btrfs",
   .fs_dir = grub_btrfs_dir,
@@ -3111,6 +3343,7 @@ static struct grub_fs grub_btrfs_fs = {
 static grub_command_t cmd_info;
 static grub_command_t cmd_mount_subvol;
 static grub_extcmd_t cmd_list_subvols;
+static grub_extcmd_t cmd_get_default_subvol;
 
 static char *
 subvolid_set_env (struct grub_env_var *var __attribute__ ((unused)),
@@ -3181,6 +3414,11 @@ GRUB_MOD_INIT (btrfs)
 					 "[-p|-n] [-o var] DEVICE",
 					 "Print list of BtrFS subvolumes on "
 					 "DEVICE.", options);
+  cmd_get_default_subvol = grub_register_extcmd("btrfs-get-default-subvol",
+					 grub_cmd_btrfs_get_default_subvol, 0,
+					 "[-p|-n] [-o var] DEVICE",
+					 "Print default BtrFS subvolume on "
+					 "DEVICE.", options);
   grub_register_variable_hook ("btrfs_subvol", subvol_get_env,
                                subvol_set_env);
   grub_register_variable_hook ("btrfs_subvolid", subvolid_get_env,
Signature · 7 lines
2.46.2


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

PATCH 36/37 efi: Add grub_efi_allocate_pool() and grub_efi_free_pool() wrappers.

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

---
 include/grub/efi/efi.h | 36 ++++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)
include/grub/efi/efi.h +32 -4
index a5cd99e5a..8d98203a7 100644
@@ -36,6 +36,10 @@ struct linux_arch_kernel_header {
   struct grub_pe_image_header pe_image_header;
 };
 
+/* Variables.  */
+extern grub_efi_system_table_t *EXPORT_VAR(grub_efi_system_table);
+extern grub_efi_handle_t EXPORT_VAR(grub_efi_image_handle);
+
 /* Functions.  */
 void *EXPORT_FUNC(grub_efi_locate_protocol) (grub_guid_t *protocol,
 					     void *registration);
@@ -71,6 +75,33 @@ EXPORT_FUNC(grub_efi_get_memory_map) (grub_efi_uintn_t *memory_map_size,
 				      grub_efi_uintn_t *descriptor_size,
 				      grub_efi_uint32_t *descriptor_version);
 void grub_efi_memory_fini (void);
+
+static inline grub_efi_status_t
+__attribute__((__unused__))
+grub_efi_allocate_pool (grub_efi_memory_type_t pool_type,
+			grub_efi_uintn_t buffer_size,
+			void **buffer)
+{
+  grub_efi_boot_services_t *b;
+  grub_efi_status_t status;
+
+  b = grub_efi_system_table->boot_services;
+  status = b->allocate_pool(pool_type, buffer_size, buffer);
+  return status;
+}
+
+static inline grub_efi_status_t
+__attribute__((__unused__))
+grub_efi_free_pool (void *buffer)
+{
+  grub_efi_boot_services_t *b;
+  grub_efi_status_t status;
+
+  b = grub_efi_system_table->boot_services;
+  status = b->free_pool(buffer);
+  return status;
+}
+
 grub_efi_loaded_image_t *EXPORT_FUNC(grub_efi_get_loaded_image) (grub_efi_handle_t image_handle);
 void EXPORT_FUNC(grub_efi_print_device_path) (grub_efi_device_path_t *dp);
 char *EXPORT_FUNC(grub_efi_get_filename) (grub_efi_device_path_t *dp);
@@ -140,10 +171,7 @@ void grub_efi_init (void);
 void grub_efi_fini (void);
 void grub_efi_set_prefix (void);
 
-/* Variables.  */
-extern grub_efi_system_table_t *EXPORT_VAR(grub_efi_system_table);
-extern grub_efi_handle_t EXPORT_VAR(grub_efi_image_handle);
-
+/* More variables.  */
 extern int EXPORT_VAR(grub_efi_is_finished);
 
 struct grub_net_card;
Signature · 7 lines
2.46.2


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

PATCH 37/37 chainloader: Use grub_efi_...() memory helpers where reasonable.

In reply to Leo Sandoval

From: Peter Jones <[email protected]>

This uses grub_efi_allocate_pool(), grub_efi_free_pool(), and grub_efi_free_pages() instead of open-coded efi_call_N() calls, so we get more reasonable type checking.

---
 grub-core/loader/efi/chainloader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/loader/efi/chainloader.c +2 -2
index 1de98f783..203692450 100644
@@ -95,7 +95,7 @@ grub_chainloader_boot (void *context)
     }
 
   if (exit_data)
-    b->free_pool (exit_data);
+    grub_efi_free_pool (exit_data);
 
   grub_loader_unset ();
 
@@ -419,7 +419,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
   grub_free (file_path);
 
   if (address)
-    b->free_pages (address, pages);
+    grub_efi_free_pages (address, pages);
 
   if (image_handle != NULL)
     b->unload_image (image_handle);
Signature · 7 lines
2.46.2


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

Re: [PATCH v1 37/37] chainloader: Use grub_efi_...() memory helpers where reasonable.

Quoted 5 lines · Leo Sandoval

From: Peter Jones <[email protected]>

This uses grub_efi_allocate_pool(), grub_efi_free_pool(), and grub_efi_free_pages() instead of open-coded efi_call_N() calls, so we get more reasonable type checking.

While the idea of putting wrappers around the EFI pool allocation calls seems reasonable (re: previous patch), this patch does not do what the comment says. Nothing seems to call grub_efi_allocate_pool(). But since something freed a pool in grub_chainloader_boot(), it must have been allocated somewhere. Was that part left out?

Thanks
Ross

Quoted 20 lines
---
  grub-core/loader/efi/chainloader.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/loader/efi/chainloader.c +1 -1
index 1de98f783..203692450 100644
@@ -95,7 +95,7 @@ grub_chainloader_boot (void *context)
      }
 
    if (exit_data)
-    b->free_pool (exit_data);
+    grub_efi_free_pool (exit_data);
 
    grub_loader_unset ();
 

grub-core/loader/efi/chainloader.c grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)), · line 425

Quoted 8 lines
grub-core/loader/efi/chainloader.c +1 -1
@@ -419,7 +419,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
    grub_free (file_path);
 
    if (address)
-    b->free_pages (address, pages);
+    grub_efi_free_pages (address, pages);
 
    if (image_handle != NULL)
      b->unload_image (image_handle);

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

#35 lore raw

Re: [PATCH v1 37/37] chainloader: Use grub_efi_...() memory helpers where reasonable.

Quoted 7 lines · On Mon, Oct 7, 2024 at 1:44 PM <[email protected]> wrote:
Quoted 5 lines · Leo Sandoval

From: Peter Jones <[email protected]>

This uses grub_efi_allocate_pool(), grub_efi_free_pool(), and grub_efi_free_pages() instead of open-coded efi_call_N() calls, so we get more reasonable type checking.

Quoted 6 lines

While the idea of putting wrappers around the EFI pool allocation calls seems reasonable (re: previous patch), this patch does not do what the comment says. Nothing seems to call grub_efi_allocate_pool(). But since something freed a pool in grub_chainloader_boot(), it must have been allocated somewhere. Was that part left out?

you are right in both points, comment is not precise and there are other places where grub_efi_allocate_pool()
wrapper can be used but it is left out. Adding the allocate wrapper deserves a separate patch, For the moment,
I will update the comment and bump the version.

Quoted 17 lines

Thanks
Ross

Review

Quoted 7 lines
---
  grub-core/loader/efi/chainloader.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/grub-core/loader/efi/chainloader.c

Quoted 6 lines

index 1de98f783..203692450 100644

grub-core/loader/efi/chainloader.c +0 -0
@@ -95,7 +95,7 @@ grub_chainloader_boot (void *context)
      }
 
Quoted 18 lines

grub-core/loader/efi/chainloader.c grub_cmd_chainloader (grub_command_t cmd

Quoted 7 lines
grub-core/loader/efi/chainloader.c +1 -1
    if (exit_data)
-    b->free_pool (exit_data);
+    grub_efi_free_pool (exit_data);
 
    grub_loader_unset ();
 
@@ -419,7 +419,7 @@ grub_cmd_chainloader (grub_command_t cmd

__attribute__ ((unused)),

Quoted 8 lines
grub-core/loader/efi/chainloader.c +1 -1
    grub_free (file_path);
 
    if (address)
-    b->free_pages (address, pages);
+    grub_efi_free_pages (address, pages);
 
    if (image_handle != NULL)
      b->unload_image (image_handle);

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