#!ipxe
# This file is part of LTSP, https://ltsp.org
# Copyright 2019 the LTSP team, see AUTHORS
# SPDX-License-Identifier: GPL-3.0-or-later

# Configure iPXE for LTSP
# Documentation=man:ltsp-ipxe(8)

# Set the default image (img) based on arch, or to root-path if it's not empty
cpuid --ext 29 && set img x86_64 || set img x86_32
isset ${root-path} && set img ${root-path} ||
isset ${proxydhcp/dhcp-server} && set srv ${proxydhcp/dhcp-server} || set srv ${next-server}

# Define cmdline parameters common to all LTSP clients
set cmdline_ltsp BOOTIF=01-${mac:hexhyp}

goto ${mac} || goto start
# The following client-specific settings can be defined in ltsp.conf:
# DEFAULT_IMAGE (img): default menu item for that client
# KERNEL_PARAMETERS (cmdline_client): additional kernel parameters
# MENU_TIMEOUT (menu-timeout): menu milliseconds, 0=forever, -1=hide menu
# For those clients, :mac:address sections are generated below

:61:6c:6b:69:73:67

:start
# To completely hide the menu, set menu-timeout to -1
isset ${menu-timeout} || set menu-timeout 5000
iseq "${menu-timeout}" "-1" && goto ${img} ||
menu iPXE boot menu - ${hostname}:${srv}:${root-path} || goto ${img}
item --gap                        Boot an image from the network in LTSP mode:
item --key 1 bookworm             bookworm.img
item --key 2 bookworm-xfce        bookworm-xfce.img
item --key 3 focal                focal.img
item --key 4 lubuntu              lubuntu.img
item --key 5 r_bookworm           bookworm
item --key 6 r_focal              focal
item --key 7 r_lubuntu            lubuntu
item --key 8 r_ubuntu             ubuntu
item
item --gap                        Other options:
item --key m memtest              Memory test
item --key c config               Enter iPXE configuration
item --key s shell                Drop to iPXE shell
item --key d disk                 Boot from the first local disk
item
item --key x exit                 Exit iPXE and continue BIOS boot
choose --timeout ${menu-timeout} --default ${img} img || goto cancel
goto ${img}

:lubuntu
:focal
:bookworm-xfce
:bookworm
:images
# The "images" method can boot anything in /srv/ltsp/images
set cmdline_method root=/dev/nfs nfsroot=${srv}:/srv/ltsp ltsp.image=images/${img}.img loop.max_part=9
goto ltsp

:r_ubuntu
set img ubuntu && goto roots
:r_lubuntu
set img lubuntu && goto roots
:r_focal
set img focal && goto roots
:r_bookworm
set img bookworm && goto roots
:roots
# The "roots" method can boot all /srv/ltsp/roots
set cmdline_method root=/dev/nfs nfsroot=${srv}:/srv/ltsp/${img}
goto ltsp

:ltsp
# :images and :roots jump here after setting cmdline_method
set cmdline ${cmdline_method} ${cmdline_ltsp} ${cmdline_client}
# In EFI mode, iPXE requires initrds to be specified in the cmdline
kernel http://raspi4/tftp/ltsp/${img}/vmlinuz initrd=ltsp.img initrd=initrd.img ${cmdline}
initrd http://raspi4/tftp/ltsp/ltsp.img
initrd http://raspi4/tftp/ltsp/${img}/initrd.img
boot || goto failed

:memtest
iseq ${platform} pcbios && kernel memtest.0 || kernel memtest.efi
# Boot "fails" on normal memtest exit with Esc, so show the menu again
boot ||
goto start

:config
config
goto start

:shell
echo Type 'exit' to get the back to the menu
shell
goto start

:disk
# Boot the first local HDD
sanboot --no-describe --drive 0x80 || goto failed

:exit
# Exit with error, to fall back to the next boot option
# http://forum.ipxe.org/showthread.php?tid=6775
exit 1

:cancel
echo You cancelled the menu, dropping to shell
goto shell

:failed
echo Booting failed, dropping to shell
goto shell
