
set timeout="5"
set timeout_style="menu"

if [ -s ${prefix}/grubenv ]; then
  load_env --skip-sig --file=${prefix}/grubenv
fi

if [ "${next_entry}" ]; then
  set default="${next_entry}"
  unset next_entry
  save_env next_entry
else
   set default="boot"
fi

if [ "${linux_gfx_mode}" ]; then
  save_env linux_gfx_mode
else
  set linux_gfx_mode=keep
  save_env linux_gfx_mode
fi

if [ x"${feature_menuentry_id}" = xy ]; then
  menuentry_id_option="--id"
else
  menuentry_id_option=""
fi

export menuentry_id_option

function load_video {
  if [ x"${feature_all_video_module}" = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
}

if [ x"${feature_default_font_path}" = xy ]; then
  font=unicode
else
  font=${prefix}/fonts/unicode.pf2
fi

if loadfont ${font}; then
  set gfxmode=auto
  load_video
  insmod gfxterm
  set locale_dir=$prefix/locale
  set lang=en_US
  insmod gettext
fi

terminal_output gfxterm

if serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1; then
  terminal_input --append serial
  terminal_output --append serial
fi

set color_normal=white/black
set menu_color_normal=light-cyan/black
set menu_color_highlight=black/cyan

insmod png
background_image ${prefix}/logo.png

set RR_CMDLINE="earlyprintk earlycon=uart8250,io,0x3f8,115200n8 console=ttyS0,115200n8 root=/dev/ram rootwait intremap=off amd_iommu_intr=legacy net.ifnames=0 panic=5 split_lock_detect=off pcie_aspm=off intel_pstate=disable amd_pstate=disable nox2apic nomodeset nowatchdog"

insmod part_gpt
insmod lvm
search --set=root --label "RR3" --no-floppy
if [ -e /initrd-rru ]; then set RRU=/initrd-rru; fi
if [ -e /microcode.img ]; then set MCI=/microcode.img; fi

if [ -s /zImage-dsm -a -s /initrd-dsm ]; then
  if [ "${default}" = "direct" ]; then
    set timeout="1"
    if [ -s ${prefix}/rsysenv ]; then
      load_env --skip-sig --file=${prefix}/rsysenv
    fi
    menuentry 'Boot DSM kernel directly' ${menuentry_id_option} direct {
      set gfxpayload="${linux_gfx_mode}"
      echo "RRVersion: ${rr_version}"
      echo "${rr_booting}"
      echo -n "Boot Time: "; date
      echo ""
      echo "Model:   ${dsm_model}"
      echo "version: ${dsm_version}"
      echo "kernel:  ${dsm_kernel}"
      echo "LKM:     ${dsm_lkm}"
      echo "MEV:     ${sys_mev}"
      echo "DMI:     ${sys_dmi}"
      echo "CPU:     ${sys_cpu}"
      echo "MEM:     ${sys_mem}"
      echo "Cmdline:"
      echo "${dsm_cmdline}"
      echo ""
      echo "Loading DSM kernel..."
      linux /zImage-dsm ${dsm_cmdline}
      echo "Loading DSM initramfs..."
      initrd ${MCI} /initrd-dsm
      echo "Booting..."
      echo "Access http://find.synology.com/ to connect the DSM via web."
    }
  fi
  menuentry 'Boot DSM' ${menuentry_id_option} boot {
    set gfxpayload="${linux_gfx_mode}"
    echo "Loading kernel..."
    linux /bzImage-rr ${RR_CMDLINE} ${rr_cmdline}
    echo "Loading initramfs..."
    initrd ${MCI} /initrd-rr ${RRU}
    echo "Booting..."
  }
  menuentry 'Boot Recovery' ${menuentry_id_option} recovery {
    set gfxpayload="${linux_gfx_mode}"
    echo "Loading kernel..."
    linux /bzImage-rr ${RR_CMDLINE} ${rr_cmdline} recovery
    echo "Loading initramfs..."
    initrd ${MCI} /initrd-rr ${RRU}
    echo "Booting..."
  }
  menuentry 'Force re-install DSM' ${menuentry_id_option} junior {
    set gfxpayload="${linux_gfx_mode}"
    echo "Loading kernel..."
    linux /bzImage-rr ${RR_CMDLINE} ${rr_cmdline} force_junior
    echo "Loading initramfs..."
    initrd ${MCI} /initrd-rr ${RRU}
    echo "Booting..."
  }
fi

menuentry 'Configure loader' ${menuentry_id_option} config {
  set gfxpayload="${linux_gfx_mode}"
  echo "Loading kernel..."
  linux /bzImage-rr earlycon=uart8250,io,0x3f8,115200n8 console=ttyS0,115200n8 ${RR_CMDLINE} ${rr_cmdline} IWANTTOCHANGETHECONFIG
  echo "Loading initramfs..."
  initrd ${MCI} /initrd-rr ${RRU}
  echo "Booting..."
}

menuentry 'Configure loader (verbose)' ${menuentry_id_option} verbose {
  set gfxpayload="${linux_gfx_mode}"
  echo "Loading kernel..."
  linux /bzImage-rr ${RR_CMDLINE} ${rr_cmdline} earlycon=tty2 console=tty2 IWANTTOCHANGETHECONFIG
  echo "Loading initramfs..."
  initrd ${MCI} /initrd-rr ${RRU}
  echo "Booting..."
}

if [ "${grub_platform}" = "efi" ]; then
  insmod bli
  menuentry 'UEFI Firmware Settings' ${menuentry_id_option} uefi {
    fwsetup
  }
fi

if [ -e ${prefix}/memtest ]; then
  menuentry 'Start Memtest86+' ${menuentry_id_option} memtest {
    echo "Loading memtest86+..."
    linux ${prefix}/memtest 
  }
fi

if [ "${linux_gfx_mode}" = "keep" ]; then
  menuentry 'Change vesa to text video mode' ${menuentry_id_option} videomode {
    set linux_gfx_mode=text
    save_env linux_gfx_mode
    configfile ${prefix}/grub.cfg
  }
else
  menuentry 'Change text to vesa video mode' ${menuentry_id_option} videomode {
    set linux_gfx_mode=keep
    save_env linux_gfx_mode
    configfile ${prefix}/grub.cfg
  }
fi
