#### This goes into the memdisk (3rd alternative: hard drive not found and no config file in the cbfs)

### Add regular keyboard support
terminal_input --append at_keyboard

### We are standalone
set prefix=(memdisk)/boot/grub

### If grub.cfg is on the disk or the cbfs, try to load it
if [ -f "(ahci0,1)/grub.cfg" ] ; then
	source (ahci0,1)/grub.cfg
else
 if [ -f "(ahci0,2)/grub.cfg" ] ; then
	source (ahci0,2)/grub.cfg
 else
  if [ -f "(ahci0,3)/grub.cfg" ] ; then
	source (ahci0,3)/grub.cfg
  else
   if [ -f "(ahci0,4)/grub.cfg" ] ; then
	source (ahci0,4)/grub.cfg
   else
	if [ -f "(cbfsdisk)/etc/grub.cfg" ] ; then
		# play 1 beep to say something is wrong
		play 480 440 1
		source (cbfsdisk)/etc/grub.cfg
	else
		# play 2 beeps to say something is quite wrong
		play 480 440 1
		play 480 440 1
		# add integrated serial port:
		serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1
		terminal_input  --append serial
		terminal_output --append serial
		# add usb serial:
		insmod nativedisk
		insmod ehci
		insmod ohci
		insmod uhci
		insmod usb
		insmod usbserial_pl2303
		insmod usbserial_ftdi
		insmod usbserial_usbdebug
		serial --speed=115200 --word=8 --parity=no --stop=1 usb1
		terminal_input  --append serial_usb1
		terminal_output --append serial_usb1

		# Default to the first entry after 10 seconds to try and type blindly if needed
		set default=1
		set timeout=10
		# Here are the entries
		menuentry 'MEMDISK: SeaBIOS' {
			chainloader (cbfsdisk)/elf/seabios.elf
		}
		menuentry 'MEMDISK: a kernel with root on /dev/sda1' {
			linux  (ahci0,1)/bzImage root=/dev/sda1 ro quiet
		}
		menuentry 'MEMDISK: Indicate normal coreboot success' {
			cmosclean 0x30:0
			cmosclean 0x30:1
			cmosclean 0x30:2
			cmosclean 0x30:3
			cmosclean 0x30:4
			cmosclean 0x30:5
		}
		menuentry 'MEMDISK: to show coreboot CBMEM logs, type' {
			set pager=1
			cbmemc
			set pager=0
		}
		menuentry 'MEMDISK: to show coreboot CBFS content, type' {
			lscoreboot
			error
		}
		menuentry 'MEMDISK: to show coreboot TIMESTAMPS, type' {
			coreboot_boottime
			error
		}
		menuentry 'MEMDISK: reboot' {
			# reset to normal
			cmosclean 48:0
			cmosclean 48:1
			# reset boot count
			cmosclean 48:4
			cmosclean 48:5
			cmosclean 48:6
			cmosclean 48:7
			# rebooting makes it impossible to shut down with normal.
			reboot
		}
		menuentry 'MEMDISK: halt' {
			# reset to normal
			cmosclean 48:0
			cmosclean 48:1
			# reset boot count
			cmosclean 48:4
			cmosclean 48:5
			cmosclean 48:6
			cmosclean 48:7
			# rebooting makes it impossible to shut down with normal.
			halt
		}
	fi
   fi
  fi
 fi
fi
