# License: GPL 
# Author: Steven Shiau <steven _at_ clonezilla org>
# Description:
# Grub2 config file to find the EFI boot loader in 1st hard drive, then boot
# Ref: git repository, docs/{osdetect.cfg,autoiso.cfg}
#      https://lists.gnu.org/archive/html/grub-devel/2013-11/msg00259.html
# This file is used by gen-grub-efi-nb-menu and ocs-live-boot-menu. 
# Not intended to be used directly.

# Possible EFI boot loader file:
# /EFI/redhat/grub.efi 
# /EFI/fedora/shim.efi 
# /EFI/fedora/grubx64.efi 
# /EFI/centos/grubx64.efi 
# /EFI/debian/grubx64.efi 
# /EFI/ubuntu/grubx64.efi 
# /EFI/mageia/grubx64.efi 
# /EFI/opensuse/grubx64.efi 
# /EFI/sled12/grubx64.efi 
# /EFI/SuSE/elilo.efi 
# /EFI/Boot/bootx64.efi 
# /EFI/Microsoft/Boot/bootmgfw.efi 
# /EFI/centos/grubaa64.efi 
# /EFI/debian/grubaa64.efi 
# /EFI/ubuntu/grubaa64.efi 
# /EFI/fedora/grubaa64.efi 
# /EFI/redhat/grubaa64.efi 
# /EFI/opensuse/grubaa64.efi 
# /EFI/Boot/bootaa64.efi 

# efidirs is only used in backup plan. If Ubuntu fixes regexp issue:
# https://bugs.launchpad.net/bugs/1829331
# then actually the backup plan can be removed in the future.
efidirs="redhat fedora centos debian ubuntu mageia opensuse sled12 SuSE Boot boot Microsoft/Boot"
efiblfiles="shim.efi grubx64.efi grub.efi elilo.efi bootx64.efi Boot/bootmgfw.efi grubaa64.efi bootaa64.efi"

function scan_dev {
    dev="$1"         # e.g., (hd0), (hd0,gpt1)
    partition="$2"   # e.g., (hd0,gpt1)
    fstype="$3"      # e.g., fat
    use_efidirs="$4" # e.g., yes
    echo "Processing dev: $dev"
    dirs_list=""
    vendor_list=""
    rc="1"
    if [ -z "$partition" ]; then
      # $partition: parenthesis removed from $dev
      regexp -s partition '\((hd0,.*)\)' $dev
      if [ -z "$partition" ]; then
         # Not hd0, continue searching the next one
         echo "Not partition on hd0, continue to the next one if available."
         return 1
      fi
    fi
    if [ -z "$fstype" ]; then
      # $fstype: filesystem type identified
      probe -s fstype -f $partition
    fi
    if test $fstype = "fat"; then
      # Found possible EFI partition since it definitely has to be FAT.
      # Search /EFI/*/*.efi
      obreak="no"
      efi_boot="no"
      set root=$partition
      efi_boot_append=""
      # Due to this bug (regexp issue), we have to explicitly show vendor dir
      # https://bugs.launchpad.net/bugs/1829331
      if [ "$use_efidirs" = "yes" ]; then
        for ivend in $efidirs; do 
          # prepend /EFI/
          vendor_list="$vendor_list /EFI/$ivend"
        done
      else
        vendor_list="/EFI/*"
      fi
      for idir in $vendor_list; do
        # We only need dir in 2nd level of path
        if [ ! -d "$idir" ]; then
          continue
        fi
        if [ "$idir" = "/EFI/boot" -o "$idir" = "/EFI/Boot" ]; then
          # Should not add /EFI/{boot,Boot} now, make it as the last one later.
          efi_boot="yes"
          efi_boot_append="$idir"
          continue
        fi
        dirs_list="$dirs_list $idir"
      done
      if [ "$efi_boot" = "yes" ]; then
        dirs_list="$dirs_list $efi_boot_append"
      fi
      for dir in $dirs_list; do
	for file in $efiblfiles; do
	    if test -f $dir/$file; then
              echo "Found EFI boot loader: $dir/$file"
              set timeout=10
	      menuentry "$dir (on $partition)" $partition "$dir/$file" {
	        set root=$2
                set efibl=$3
                chainloader $efibl
                boot
	      }
              obreak="yes"
              break
            fi
        done
        if [ "$obreak" = "yes" ]; then
          break
          rc=0
        fi
      done
    fi
    return $rc
echo "End creating menu..."
}

insmod regexp
for dev in (*); do
  if [ "$dev" = "(*)" ]; then
    # Encounter this issue (regexp fails):
    # https://bugs.launchpad.net/bugs/1829331
    # i.e., (*) cannot be expanded as (hd0), (cd0), (hd0,gpt1)...
    auto_detect="no"
  else
    auto_detect="yes"
  fi
  echo "Now auto checking $dev"
  scan_dev $dev
done
# Using backup plan if auto gen fails.
if [ "$auto_detect" = "no" ]; then
  echo "Using backup plan..."
  parts_list="gpt1 gpt2 gpt3 gpt4"
  for part in $parts_list; do
    scan_dev (hd0,$part) (hd0,$part) fat yes
  done
fi
