Upgrade grub from 2.00 to 2.02

The 2.00 version of the ebuild used a git repo that contained a copy
of the contents of the 2.00 tarball, plus autogenerated build
files. There were only a couple of CrOS-specific patches in that repo.

The new 2.02 ebuild no longer uses that git repo, so it isn't
cros-workon-able now. Instead it uses the 2.02 release tarball with a
couple of CrOS patches applied. It also calls `autogen.sh` now so that
the applied patches don't need to include autogenerated build files.

BUG=None
TEST=sudo emerge grub && build_image, verify that both UEFI and legacy BIOS booting works

Change-Id: I2fe837f4bc3f6ec4cab75b2e92628ef671eba43f
Reviewed-on: https://chromium-review.googlesource.com/1066750
Commit-Ready: Nicholas Bishop <nbishop@neverware.com>
Tested-by: Nicholas Bishop <nbishop@neverware.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/chromeos/config/env/sys-boot/grub b/chromeos/config/env/sys-boot/grub
index ec35fc7..3efe7328 100644
--- a/chromeos/config/env/sys-boot/grub
+++ b/chromeos/config/env/sys-boot/grub
@@ -19,7 +19,7 @@
 
 # We need to build grub with GCC, for host builds.
 cros_pre_src_prepare_use_gcc() {
-       if use cros_host; then
+       if [[ $(cros_target) == "cros_host" ]]; then
                cros_use_gcc
        fi
 }
diff --git a/sys-boot/grub/Manifest b/sys-boot/grub/Manifest
new file mode 100644
index 0000000..6ec5b6c
--- /dev/null
+++ b/sys-boot/grub/Manifest
@@ -0,0 +1 @@
+DIST grub-2.02.tar.xz 6113260 SHA256 810b3798d316394f94096ec2797909dbf23c858e48f7b3830826b8daa06b7b0f SHA512 cc6eb0a42b5c8df2f671cc128ff725afb3ff1f8832a196022e433cf0d3b75decfca2316d0aa5fabea75747d55e88f3d021dd93508563f8ca80fd7b9e7fe1f088 WHIRLPOOL a7f5d786064642f926faa444b9db1555ccfbb460655f81a0ca408bc5ad2acfb89fe596f81d161078944ce1b2dc00e82e8fb056f050bf026fc07f6417af7b9776
diff --git a/sys-boot/grub/files/0001-Forward-port-ChromeOS-specific-GRUB-environment-vari.patch b/sys-boot/grub/files/0001-Forward-port-ChromeOS-specific-GRUB-environment-vari.patch
new file mode 100644
index 0000000..62e6247
--- /dev/null
+++ b/sys-boot/grub/files/0001-Forward-port-ChromeOS-specific-GRUB-environment-vari.patch
@@ -0,0 +1,93 @@
+From 834b0793bae1e72d483dd2dccb74e2c81d35b0b1 Mon Sep 17 00:00:00 2001
+From: Josh Triplett <josh@joshtriplett.org>
+Date: Wed, 31 Jul 2013 13:54:36 -0700
+Subject: [PATCH 1/2] Forward-port ChromeOS-specific GRUB environment variables
+ for boot disk and partitions
+
+This commit forward-ports the following changes previously applied in the
+ChromeOS version of GRUB, with updates for changes to the surrounding code and
+GRUB API changes:
+
+144e40aca61589db1d286c82508f9328ec861158 Add variables to grub2 for Chrome OS bringup workarounds.
+as modified by e63c2ee074c3c0abb5717fc8b7df62cf32066782 Support standard EFI ordering
+and 5dfc8e1ae8c7d6836f2faef2545e4c5e6d516dff Add variable 'grubdisk' to export boot disk
+
+In particular, grub2 now has a grub_snprintf, and no longer has a grub_sprintf.
+
+BUG=chromium:265918
+TEST=With the complete patch series, built a complete x86-generic image,
+     and booted it via UEFI on both 32-bit and 64-bit UEFI platforms.
+
+CQ-DEPEND=I8d4cdde878efe73e15fddb250b10cfd4b88d88d1
+
+Change-Id: Ie865f3d5ab0ecd8e9138bfd5b576a8147a205757
+Reviewed-on: https://gerrit.chromium.org/gerrit/63985
+Tested-by: Josh Triplett <josh@joshtriplett.org>
+Reviewed-by: Liam McLoughlin <lmcloughlin@chromium.org>
+Commit-Queue: Josh Triplett <josh@joshtriplett.org>
+---
+ grub-core/disk/efi/efidisk.c | 41 ++++++++++++++++++++++++++++++++++++
+ 1 file changed, 41 insertions(+)
+
+diff --git a/grub-core/disk/efi/efidisk.c b/grub-core/disk/efi/efidisk.c
+index 5d2400f66..760f482e7 100644
+--- a/grub-core/disk/efi/efidisk.c
++++ b/grub-core/disk/efi/efidisk.c
+@@ -26,6 +26,7 @@
+ #include <grub/efi/api.h>
+ #include <grub/efi/efi.h>
+ #include <grub/efi/disk.h>
++#include <grub/env.h>
+ 
+ struct grub_efidisk_data
+ {
+@@ -882,6 +883,46 @@ grub_efidisk_get_device_name (grub_efi_handle_t *handle)
+ 				     ctx.partition_name);
+ 	  grub_free (ctx.partition_name);
+ 	}
++
++      {
++	// This block is a temporary workaround used by Chrome OS. We set
++	// some variables that we can use in the grub.cfg file to ensure that
++	// we get the kernel and rootfs from the boot device, regardless of
++	// which device that is.
++	grub_size_t tmpbuf_len = grub_strlen (parent->name) + 5;
++	char *tmpbuf = grub_malloc (tmpbuf_len);
++	if (! tmpbuf)
++	{
++	  grub_free (dev_name);
++	  grub_disk_close (parent);
++	  return 0;
++	}
++
++	grub_snprintf (tmpbuf, tmpbuf_len, "(%s,3)", parent->name);
++	grub_env_set ("grubpartA", tmpbuf);
++	grub_env_export ("grubpartA");
++	grub_snprintf (tmpbuf, tmpbuf_len, "(%s,5)", parent->name);
++	grub_env_set ("grubpartB", tmpbuf);
++	grub_env_export ("grubpartB");
++	grub_free (tmpbuf);
++
++	grub_env_set ("grubdisk", parent->name);
++	grub_env_export ("grubdisk");
++
++	// Translate hd0 to sda, hd1 to sdb, etc. parent->name is always
++	// either "fdN", "hdN", or "cdN". This trick won't work if N is > 9.
++	int index = parent->name[2] - '0';
++
++	char devname[] = "sdXN";
++	devname[2] = 'a' + index;
++	devname[3] = '3';
++	grub_env_set ("linuxpartA", devname);
++	grub_env_export ("linuxpartA");
++	devname[3] = '5';
++	grub_env_set ("linuxpartB", devname);
++	grub_env_export ("linuxpartB");
++      }
++
+       grub_disk_close (parent);
+ 
+       return dev_name;
+-- 
+2.17.0
+
diff --git a/sys-boot/grub/files/0002-Forward-port-gptpriority-command-to-GRUB-2.00.patch b/sys-boot/grub/files/0002-Forward-port-gptpriority-command-to-GRUB-2.00.patch
new file mode 100644
index 0000000..c62f406
--- /dev/null
+++ b/sys-boot/grub/files/0002-Forward-port-gptpriority-command-to-GRUB-2.00.patch
@@ -0,0 +1,188 @@
+From 6e05eb38d1f8e83af24f4c6c4304d027da4994ba Mon Sep 17 00:00:00 2001
+From: Josh Triplett <josh@joshtriplett.org>
+Date: Wed, 31 Jul 2013 14:03:00 -0700
+Subject: [PATCH 2/2] Forward-port gptpriority command to GRUB 2.00
+
+Forward-ported from git commit 9866f9750a38a175849ca04439922ac1ffe8a0e7
+with significant edits: Ported from the previous Ruby-based build system
+to the new autogen-based Makefile.am generator, and updated for GRUB API
+changes.  In particular, the previous version of gptpriority counted on
+GRUB's part_gpt to provide access to the original partition table data
+in a field of "struct grub_partition" that no longer exists; this
+updated version modifies part_gpt to directly export the relevant
+attribute field as a new field in grub_partition.
+
+Since this adds a new source file and a new module, and the GRUB ebuild
+expects source in "make dist" form (with configure and Makefile.in
+already generated), this commit also updates the corresponding generated
+files via autogen.sh.
+
+BUG=chromium:265918
+TEST=With the complete patch series, built a complete x86-generic image,
+     and booted it via UEFI on both 32-bit and 64-bit UEFI platforms.
+
+CQ-DEPEND=Ie865f3d5ab0ecd8e9138bfd5b576a8147a205757
+
+Change-Id: I238f4ad26af3b1d924d5d5ac5fbcd567415cfb3f
+Reviewed-on: https://gerrit.chromium.org/gerrit/63986
+Tested-by: Josh Triplett <josh@joshtriplett.org>
+Reviewed-by: Liam McLoughlin <lmcloughlin@chromium.org>
+Commit-Queue: Josh Triplett <josh@joshtriplett.org>
+---
+ grub-core/Makefile.core.def      |  5 ++
+ grub-core/commands/gptpriority.c | 99 ++++++++++++++++++++++++++++++++
+ grub-core/partmap/gpt.c          |  1 +
+ include/grub/partition.h         |  3 +
+ 4 files changed, 108 insertions(+)
+ create mode 100644 grub-core/commands/gptpriority.c
+
+diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
+index 2dfa22a92..bc170349a 100644
+--- a/grub-core/Makefile.core.def
++++ b/grub-core/Makefile.core.def
+@@ -821,6 +821,11 @@ module = {
+   common = commands/gptsync.c;
+ };
+ 
++module = {
++  name = gptpriority;
++  common = commands/gptpriority.c;
++};
++
+ module = {
+   name = halt;
+   nopc = commands/halt.c;
+diff --git a/grub-core/commands/gptpriority.c b/grub-core/commands/gptpriority.c
+new file mode 100644
+index 000000000..0bb40cb65
+--- /dev/null
++++ b/grub-core/commands/gptpriority.c
+@@ -0,0 +1,99 @@
++/* gptpriority.c - read the priority of gpt partitions  */
++/*
++ *  GRUB  --  GRand Unified Bootloader
++ *
++ *  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/command.h>
++#include <grub/dl.h>
++#include <grub/disk.h>
++#include <grub/partition.h>
++#include <grub/gpt_partition.h>
++#include <grub/misc.h>
++#include <grub/mm.h>
++#include <grub/fs.h>
++#include <grub/env.h>
++
++GRUB_MOD_LICENSE ("GPLv3+");
++
++static grub_err_t
++grub_cmd_gptpriority (grub_command_t cmd __attribute__ ((unused)),
++                      int argc, char **args)
++{
++  grub_disk_t disk;
++  grub_partition_t part;
++  char priority_str[3]; /* Maximum value 15 */
++
++  if (argc < 2 || argc > 3)
++    return grub_error (GRUB_ERR_BAD_ARGUMENT,
++                       "gptpriority DISKNAME PARTITIONNUM [VARNAME]");
++
++  /* Open the disk if it exists */
++  disk = grub_disk_open (args[0]);
++  if (!disk)
++    {
++      return grub_error (GRUB_ERR_BAD_ARGUMENT,
++                         "Not a disk");
++    }
++
++  part = grub_partition_probe (disk, args[1]);
++  if (!part)
++    {
++      grub_disk_close (disk);
++      return grub_error (GRUB_ERR_BAD_ARGUMENT,
++                         "No such partition");
++    }
++
++  if (grub_strcmp (part->partmap->name, "gpt"))
++    {
++      grub_disk_close (disk);
++      return grub_error (GRUB_ERR_BAD_PART_TABLE,
++                         "Not a GPT partition");
++    }
++
++  grub_snprintf (priority_str, sizeof(priority_str), "%" PRIuGRUB_UINT64_T,
++                 (part->gpt_attrib >> 48) & 0xfULL);
++
++  if (argc == 3)
++    {
++      grub_env_set (args[2], priority_str);
++      grub_env_export (args[2]);
++    }
++  else
++    {
++      grub_printf ("Priority is %s\n", priority_str);
++    }
++
++  grub_disk_close (disk);
++  return GRUB_ERR_NONE;
++}
++
++static grub_command_t cmd;
++
++GRUB_MOD_INIT(gptpriority)
++{
++  (void) mod;
++  cmd = grub_register_command ("gptpriority", grub_cmd_gptpriority,
++                               "gptpriority DISKNAME PARTITIONNUM [VARNAME]",
++                               "\nDisplays the priority of the specified "
++                               "partition from the given disk, and optionally"
++                               " stores it in an environment variable.\n\n"
++                               "E.g. gptpriority hd2 2 prioA");
++}
++
++GRUB_MOD_FINI(gptpriority)
++{
++  grub_unregister_command (cmd);
++}
+diff --git a/grub-core/partmap/gpt.c b/grub-core/partmap/gpt.c
+index 83bcba779..ff754dcda 100644
+--- a/grub-core/partmap/gpt.c
++++ b/grub-core/partmap/gpt.c
+@@ -108,6 +108,7 @@ grub_gpt_partition_map_iterate (grub_disk_t disk,
+ 	  part.index = last_offset;
+ 	  part.partmap = &grub_gpt_partition_map;
+ 	  part.parent = disk->partition;
++	  part.gpt_attrib = entry.attrib;
+ 
+ 	  grub_dprintf ("gpt", "GPT entry %d: start=%lld, length=%lld\n", i,
+ 			(unsigned long long) part.start,
+diff --git a/include/grub/partition.h b/include/grub/partition.h
+index 7adb7ec6e..639f1e610 100644
+--- a/include/grub/partition.h
++++ b/include/grub/partition.h
+@@ -87,6 +87,9 @@ struct grub_partition
+   /* The type of partition whne it's on MSDOS.
+      Used for embedding detection.  */
+   grub_uint8_t msdostype;
++
++  /* The attrib field for GPT. Needed for priority detection. */
++  grub_uint64_t gpt_attrib;
+ };
+ 
+ grub_partition_t EXPORT_FUNC(grub_partition_probe) (struct grub_disk *disk,
+-- 
+2.17.0
+
diff --git a/sys-boot/grub/grub-2.02-r1.ebuild b/sys-boot/grub/grub-2.02-r1.ebuild
new file mode 120000
index 0000000..bec419f
--- /dev/null
+++ b/sys-boot/grub/grub-2.02-r1.ebuild
@@ -0,0 +1 @@
+grub-2.02.ebuild
\ No newline at end of file
diff --git a/sys-boot/grub/grub-2.00-r22.ebuild b/sys-boot/grub/grub-2.02.ebuild
similarity index 85%
rename from sys-boot/grub/grub-2.00-r22.ebuild
rename to sys-boot/grub/grub-2.02.ebuild
index fefee62..f3f7ff2 100644
--- a/sys-boot/grub/grub-2.00-r22.ebuild
+++ b/sys-boot/grub/grub-2.02.ebuild
@@ -1,20 +1,17 @@
 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
 # Distributed under the terms of the GNU General Public License v2
 
-EAPI="4"
-CROS_WORKON_COMMIT="df6034c59cb2d847f9e780cf2def30c5c9b8305f"
-CROS_WORKON_TREE="a36ac8b6b01b250b05a29d6eb9c994c78c1153de"
-CROS_WORKON_PROJECT="chromiumos/third_party/grub2"
-CROS_WORKON_LOCALNAME="grub2"
+EAPI="5"
 
-inherit eutils toolchain-funcs multiprocessing cros-workon
+inherit eutils toolchain-funcs multiprocessing
 
 DESCRIPTION="GNU GRUB 2 boot loader"
 HOMEPAGE="http://www.gnu.org/software/grub/"
+SRC_URI="ftp://ftp.gnu.org/gnu/grub/${P}.tar.xz"
+
 LICENSE="GPL-3"
 SLOT="0"
 KEYWORDS="-* amd64"
-IUSE=""
 
 PROVIDE="virtual/bootloader"
 
@@ -24,6 +21,13 @@
 PLATFORMS=( "efi" "pc" )
 TARGETS=( "i386" "x86_64" )
 
+src_prepare() {
+	epatch "${FILESDIR}/0001-Forward-port-ChromeOS-specific-GRUB-environment-vari.patch"
+	epatch "${FILESDIR}/0002-Forward-port-gptpriority-command-to-GRUB-2.00.patch"
+
+	bash autogen.sh || die
+}
+
 src_configure() {
 	local platform target
 	# Fix timestamps to prevent unnecessary rebuilding
diff --git a/sys-boot/grub/grub-9999.ebuild b/sys-boot/grub/grub-9999.ebuild
deleted file mode 100644
index 5f0c63d..0000000
--- a/sys-boot/grub/grub-9999.ebuild
+++ /dev/null
@@ -1,79 +0,0 @@
-# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
-# Distributed under the terms of the GNU General Public License v2
-
-EAPI="4"
-CROS_WORKON_PROJECT="chromiumos/third_party/grub2"
-CROS_WORKON_LOCALNAME="grub2"
-
-inherit eutils toolchain-funcs multiprocessing cros-workon
-
-DESCRIPTION="GNU GRUB 2 boot loader"
-HOMEPAGE="http://www.gnu.org/software/grub/"
-LICENSE="GPL-3"
-SLOT="0"
-KEYWORDS="-* ~amd64"
-IUSE=""
-
-PROVIDE="virtual/bootloader"
-
-export STRIP_MASK="*.img *.mod *.module"
-
-# The ordering doesn't seem to matter.
-PLATFORMS=( "efi" "pc" )
-TARGETS=( "i386" "x86_64" )
-
-src_configure() {
-	local platform target
-	# Fix timestamps to prevent unnecessary rebuilding
-	find "${S}" -exec touch -r "${S}/configure" {} +
-	multijob_init
-	for platform in "${PLATFORMS[@]}" ; do
-		for target in "${TARGETS[@]}" ; do
-			mkdir -p ${target}-${platform}-build
-			pushd ${target}-${platform}-build >/dev/null
-			# GRUB defaults to a --program-prefix set based on target
-			# platform; explicitly set it to nothing to install unprefixed
-			# tools.  https://savannah.gnu.org/bugs/?39818
-			ECONF_SOURCE="${S}" multijob_child_init econf \
-				--disable-werror \
-				--disable-grub-mkfont \
-				--disable-grub-mount \
-				--disable-device-mapper \
-				--disable-efiemu \
-				--disable-libzfs \
-				--disable-nls \
-				--sbindir=/sbin \
-				--bindir=/bin \
-				--libdir=/$(get_libdir) \
-				--with-platform=${platform} \
-				--target=${target} \
-				--program-prefix=
-			popd >/dev/null
-		done
-	done
-	multijob_finish
-}
-
-src_compile() {
-	local platform target
-	multijob_init
-	for platform in "${PLATFORMS[@]}" ; do
-		for target in "${TARGETS[@]}" ; do
-			multijob_child_init \
-				emake -C ${target}-${platform}-build -j1
-		done
-	done
-	multijob_finish
-}
-
-src_install() {
-	local platform target
-	# The installations have several file conflicts that prevent
-	# parallel installation.
-	for platform in "${PLATFORMS[@]}" ; do
-		for target in "${TARGETS[@]}" ; do
-			emake -C ${target}-${platform}-build DESTDIR="${D}" \
-				install
-		done
-	done
-}