#
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/ocswp-grub2.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

insmod play
play 960 440 1 0 4 440 1


menuentry "Clonezilla live preseed (VGA 800x600)" --id live-default {
  search --set -f /live/vmlinuz
  $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet loglevel=0 noswap edd=on nomodeset enforcing=0 noeject locales=en_US.UTF-8 keyboard-layouts=us ocs_prerun1="dhclient -v eth0" ocs_prerun2="sleep 2" ocs_prerun3="mount -t nfs 192.168.56.254:/home/partimag /home/partimag" ocs_prerun4="sleep 2" ocs_live_run="ocs-sr -g auto -e1 auto -e2 -c -r -j2 -k0 -scr -p true restoredisk ask_user ask_user"  ocs_live_extra_param="" ocs_live_batch="no" vga=788 net.ifnames=0  nosplash i915.blacklist=yes radeonhd.blacklist=yes nouveau.blacklist=yes vmwgfx.enable_fbdev=1
  $initrd_cmd /live/initrd.img
}

menuentry --hotkey=r "Clonezilla live (VGA 800x600 & To RAM)" --id live-toram {
  search --set -f /live/vmlinuz
  $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet loglevel=0 noswap edd=on nomodeset enforcing=0 noeject locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no" vga=788 toram=live,syslinux,EFI,boot,.disk,utils net.ifnames=0  nosplash i915.blacklist=yes radeonhd.blacklist=yes nouveau.blacklist=yes vmwgfx.enable_fbdev=1
  $initrd_cmd /live/initrd.img
}

menuentry --hotkey=l "Clonezilla 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 loglevel=0 noswap edd=on nomodeset enforcing=0 noeject locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no" nomodeset toram=live,syslinux,EFI,boot,.disk,utils net.ifnames=0  nosplash live_console_font_size=16x32
  $initrd_cmd /live/initrd.img
}

menuentry --hotkey=s "Clonezilla live (Speech synthesis)"{
  search --set -f /live/vmlinuz
  $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet loglevel=0 noswap edd=on nomodeset enforcing=0 noeject locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no" vga=788 net.ifnames=0  nosplash i915.blacklist=yes radeonhd.blacklist=yes nouveau.blacklist=yes vmwgfx.enable_fbdev=1 speakup.synth=soft ---
  $initrd_cmd /live/initrd.img
}
submenu 'Other modes of Clonezilla live' {
  menuentry "Clonezilla live (VGA 1024x768)"{
    search --set -f /live/vmlinuz
    $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet loglevel=0 noswap edd=on nomodeset enforcing=0 noeject locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no" vga=791 net.ifnames=0  nosplash i915.blacklist=yes radeonhd.blacklist=yes nouveau.blacklist=yes vmwgfx.enable_fbdev=1
    $initrd_cmd /live/initrd.img
  }
  
  menuentry "Clonezilla live (VGA 640x480)"{
    search --set -f /live/vmlinuz
    $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet loglevel=0 noswap edd=on nomodeset enforcing=0 noeject locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no" vga=785 net.ifnames=0  nosplash i915.blacklist=yes radeonhd.blacklist=yes nouveau.blacklist=yes vmwgfx.enable_fbdev=1
    $initrd_cmd /live/initrd.img
  }
  
  menuentry --hotkey=K "Clonezilla live (KMS)"{
    search --set -f /live/vmlinuz
    $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet loglevel=0 noswap edd=on enforcing=0 noeject locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no" vga=791 net.ifnames=0  nosplash
    $initrd_cmd /live/initrd.img
  }

  menuentry --hotkey=R "Clonezilla live (KMS & To RAM)" {
    search --set -f /live/vmlinuz
    $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet loglevel=0 noswap edd=on enforcing=0 noeject locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no" vga=791 toram=live,syslinux,EFI,boot,.disk,utils net.ifnames=0  nosplash
    $initrd_cmd /live/initrd.img
  }
  
  menuentry "Clonezilla 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 loglevel=0 noswap edd=on enforcing=0 noeject locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no" vga=791 toram=live,syslinux,EFI,boot,.disk,utils net.ifnames=0  nosplash live_console_font_size=16x32
    $initrd_cmd /live/initrd.img
  }

  menuentry "Clonezilla live Safe graphic settings (vga=normal)"{
    search --set -f /live/vmlinuz
    $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet loglevel=0 noswap edd=on nomodeset enforcing=0 noeject locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no" net.ifnames=0 nomodeset vga=normal nosplash
    $initrd_cmd /live/initrd.img
  }
  
  menuentry "Clonezilla live (Failsafe mode)"{
    search --set -f /live/vmlinuz
    $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet loglevel=0 noswap edd=on nomodeset enforcing=0 noeject locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no" 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 (VGA 800x600 & To RAM)" --id memtester {
  search --set -f /live/vmlinuz
  $linux_cmd /live/vmlinuz boot=live union=overlay username=user config components quiet loglevel=0 noswap edd=on nomodeset enforcing=0 noeject locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no" vga=788 toram=live,syslinux,EFI,boot,.disk,utils net.ifnames=0  nosplash i915.blacklist=yes radeonhd.blacklist=yes nouveau.blacklist=yes vmwgfx.enable_fbdev=1 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 "FreeDOS"{
#  linux16 memdisk
#  initrd /live/freedos.img
#}

menuentry "Network boot via iPXE" {
  search --no-floppy --set=root -f /live/ipxe.efi
  chainloader /live/ipxe.efi + 1
}

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

menuentry "Clonezilla live 3.2.1-9-amd64 info"{
  echo Clonezilla, the OpenSource Clone System.
  echo NCHC Free Software Labs, Taiwan.
  echo clonezilla.org, clonezilla.nchc.org.tw
  echo \* Boot menu for EFI machine \| Created at time: Mon 03 Mar 2025 01:13:20 PM GMT
  echo \* Clonezilla live version: 3.2.1-9-amd64. (C) 2003-2025, NCHC, Taiwan
  echo \* Disclaimer: Clonezilla comes with ABSOLUTELY NO WARRANTY
  sleep --verbose --interruptible 10
}