#
set pref=/boot/grub
set default="0"
set timeout="30"
# For grub 2.04, a workaround to avoid boot failure is to add "rmmod tpm": https://bugs.debian.org/975835. However, it might fail in secure boot uEFI machine, and the error is like:
# error: verification requested but nobody cares: /live/vmlinuz.
# Out of range pointer 0x3000000004040
# Aborted. Press any key to exit. 
# rmmod tpm

# To set authentication, check
# https://www.gnu.org/software/grub/manual/grub/grub.html#Authentication-and-authorisation
# ‘password’ sets the password in plain text, requiring grub.cfg to be secure; ‘password_pbkdf2’ sets the password hashed using the Password-Based Key Derivation Function (RFC 2898), requiring the use of grub-mkpasswd-pbkdf2 (see Invoking grub-mkpasswd-pbkdf2) to generate password hashes.
# Example:
# set superusers="root"
# password_pbkdf2 root grub.pbkdf2.sha512.10000.biglongstring
# password user1 insecure
# 
# menuentry "May be run by any user" --unrestricted {
# 	set root=(hd0,1)
# 	linux /vmlinuz
# }
# 
# menuentry "Superusers only" --users "" {
# 	set root=(hd0,1)
# 	linux /vmlinuz single
# }
# 
# menuentry "May be run by user1 or a superuser" --users user1 {
# 	set root=(hd0,2)
# 	chainloader +1
# }

# Load graphics (only correspoonding ones will be found)
# (U)EFI
insmod efi_gop
insmod efi_uga
# legacy BIOS
# insmod vbe
if [ x$feature_default_font_path = xy ] ; then
   font=unicode
else
   font=$prefix/unifont.pf2
fi

# A workdaround to avoid no video output on some systems
# Ref: https://savannah.gnu.org/bugs/?65503#comment2
# https://sourceforge.net/p/clonezilla/bugs/424/
echo "Loading bootloader..."

if loadfont $font; then
  set gfxmode=auto
  insmod gfxterm
  # Set the language for boot menu prompt, e.g., en_US, zh_TW...
  set lang=en_US
  terminal_output gfxterm
fi
set hidden_timeout_quiet=false

insmod png
if background_image $pref/Gsplash.png; then
  set color_normal=black/black
  set color_highlight=magenta/black
else
  set color_normal=cyan/blue
  set color_highlight=white/blue
fi

# Uncomment the following for serial console
# The command serial initializes the serial unit 0 with the speed 38400bps.
# The serial unit 0 is usually called ‘COM1’. If COM2, use ‘--unit=1’ instead.
#serial --unit=0 --speed=38400
#terminal_input serial
#terminal_output serial

# Decide if the commands: linux/initrd (default) or linuxefi/initrdefi
set linux_cmd=linux
set initrd_cmd=initrd
export linux_cmd initrd_cmd
# Since "linux/initrd" works for uEFI boot, no matter it's secure boot or not. Just use them.
# if [ "${grub_platform}" = "efi" -a -e "/amd64-release.txt" ]; then
#   # Only amd64 release we switch to linuxefi/initrdefi since it works better with security boot (shim)
#   set linux_cmd=linuxefi
#   set initrd_cmd=initrdefi
# fi


menuentry "GParted Live (Default settings)" --id live-default {
  search --set -f /live/vmlinuz
  $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet noswap  net.ifnames=0  nosplash 
  $initrd_cmd /live/initrd.img
}

menuentry --hotkey=r "GParted Live (Default settings & To RAM)" --id live-toram {
  search --set -f /live/vmlinuz
  $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet noswap  toram=filesystem.squashfs net.ifnames=0  nosplash 
  $initrd_cmd /live/initrd.img
}

menuentry --hotkey=l "GParted Live (VGA with large font & To RAM)" --id live-vga-large-font-toram {
  search --set -f /live/vmlinuz
  $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet noswap nomodeset toram=filesystem.squashfs net.ifnames=0  nosplash live_console_font_size=16x32
  $initrd_cmd /live/initrd.img
}

submenu 'Other modes of GParted Live' {
  menuentry --hotkey=K "GParted Live (KMS)"{
    search --set -f /live/vmlinuz
    $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet noswap vga=791 net.ifnames=0  nosplash
    $initrd_cmd /live/initrd.img
  }

  menuentry --hotkey=R "GParted Live (KMS & To RAM)" {
    search --set -f /live/vmlinuz
    $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet noswap vga=791 toram=filesystem.squashfs net.ifnames=0  nosplash
    $initrd_cmd /live/initrd.img
  }
  
  menuentry "GParted Live (KMS with large font & To RAM)" --id live-kms-large-font-toram {
    search --set -f /live/vmlinuz
    $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet noswap vga=791 toram=filesystem.squashfs net.ifnames=0  nosplash live_console_font_size=16x32
    $initrd_cmd /live/initrd.img
  }

  menuentry "GParted Live Safe graphic settings (vga=normal)"{
    search --set -f /live/vmlinuz
    $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet noswap net.ifnames=0 nomodeset vga=normal nosplash
    $initrd_cmd /live/initrd.img
  }
  
  menuentry "GParted Live (Failsafe mode)"{
    search --set -f /live/vmlinuz
    $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet noswap acpi=off irqpoll noapic noapm nodma nomce nolapic nosmp net.ifnames=0 nomodeset vga=normal nosplash
    $initrd_cmd /live/initrd.img
  }
}  

menuentry "Local operating system (if available)" --id local-disk {
  echo "Booting first local disk..."
  # Generate boot menu automatically
  configfile /boot/grub/boot-local-efi.cfg
  # If not chainloaded, definitely no uEFI boot loader was found.
  echo "No uEFI boot loader was found!"
  sleep 15
}

menuentry --hotkey=m "Memtester (Default settings & To RAM)" --id memtester {
  search --set -f /live/vmlinuz
  $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet noswap  toram=filesystem.squashfs net.ifnames=0  nosplash  init=/bin/bash -- -c "/sbin/ocs-memtester"
  $initrd_cmd /live/initrd.img
}

menuentry "Memtest using Memtest86+" --id memtest86+ {
  search --no-floppy --set=root -f /live/mt86+x64.efi
  linux /live/mt86+x64.efi
}

menuentry 'uEFI firmware setup' 'uefi-firmware' {
  echo "Entering uEFI firmware setup..."
  fwsetup
}

menuentry "GParted Live 1.7.0-1-amd64 info"{
  echo GParted.
  echo Gnome Partition Editor.
  echo https://gparted.org
  echo \* Boot menu for EFI machine \| Created at time: Thu 30 Jan 2025 10:09:45 PM GMT
  echo \* GParted live version: 1.7.0-1-amd64. Live version maintainer: Steven Shiau
  echo \* Disclaimer: GParted live comes with ABSOLUTELY NO WARRANTY
  sleep --verbose --interruptible 10
}

