Modified grub.cfg to set default value based on partition priority

In this commit, template of grub.cfg is modified to set the default
parition to boot based on priority of the partition in the disk.
Grub reads the priority of partitions in the GPT disk with the help
of grub command "gptpriority". This change is required, say when
a new image is installed with image_to_live, system reboots to the
default image instead of newly installed image. This commit will
ensure the image booted is with high priority (in above case newly
installed image) instead of just booting default always. Note: This
above scenario works fine with coreboot, and the changes is to make
EFI boot behave the sameway

BUG=None
TEST=Tested by changing the parition priority manually using cgpt and
     verified that system boots to high priority image on reboot. And
     also verified image_to_live boots to newly installed image.

CQ-DEPEND=CL:I7cce1079b356e8459409113a4c17d62b25c5cd4e

Change-Id: I930d0f3f1a3f8f54edd1dce7f8259e3c261af9a1
Reviewed-on: https://gerrit.chromium.org/gerrit/58276
Reviewed-by: David James <davidjames@chromium.org>
Tested-by: Sathish Kuppuswamy <sathish.kuppuswamy@intel.com>
Commit-Queue: Josh Triplett <josh@joshtriplett.org>
diff --git a/build_library/create_legacy_bootloader_templates.sh b/build_library/create_legacy_bootloader_templates.sh
index 8cecb94..0e7bedb 100755
--- a/build_library/create_legacy_bootloader_templates.sh
+++ b/build_library/create_legacy_bootloader_templates.sh
@@ -161,7 +161,8 @@
     # Use the newer grub2 1.99+
     grub_args=(
       -p /efi/boot
-      part_gpt fat ext2 hfs hfsplus normal boot chain configfile linux
+      part_gpt gptpriority test fat ext2 hfs hfsplus normal boot chain
+      configfile linux
     )
     sudo grub2-mkimage -O x86_64-efi \
       -o "${FLAGS_to}/efi/boot/bootx64.efi" "${grub_args[@]}"
@@ -171,7 +172,7 @@
     # Remove this else case after a few weeks (sometime in Dec 2011)
     grub_args=(
       -p /efi/boot
-      part_gpt fat ext2 normal boot sh chain configfile linux
+      part_gpt gptpriority test fat ext2 normal boot sh chain configfile linux
     )
     sudo grub-mkimage -o "${FLAGS_to}/efi/boot/bootx64.efi" "${grub_args[@]}"
     sudo i386-grub-mkimage -o "${FLAGS_to}/efi/boot/bootia32.efi" \
@@ -181,7 +182,17 @@
   #  DMTABLEA, DMTABLEB -> '0 xxxx verity ... '
   # This should be replaced during postinst when updating the ESP.
   cat <<EOF | sudo dd of="${FLAGS_to}/efi/boot/grub.cfg" 2>/dev/null
-set default=0
+defaultA=0
+defaultB=1
+gptpriority \$grubdisk 2 prioA
+gptpriority \$grubdisk 4 prioB
+
+if [ \$prioA -lt \$prioB ]; then
+  set default=\$defaultB
+else
+  set default=\$defaultA
+fi
+
 set timeout=2
 
 # NOTE: These magic grub variables are a Chrome OS hack. They are not portable.
@@ -210,8 +221,10 @@
 }
 EOF
   if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
-    sudo sed -i -e 's/^set default=.*/set default=2/' \
-       "${FLAGS_to}/efi/boot/grub.cfg"
+    sudo sed -i \
+      -e '/^defaultA=/s:=.*:=2:' \
+      -e '/^defaultB=/s:=.*:=3:' \
+      "${FLAGS_to}/efi/boot/grub.cfg"
   fi
   info "Emitted ${FLAGS_to}/efi/boot/grub.cfg"
   exit 0