Subject: Re: Bug#731709: grub-efi UEFI support based on debian-cd work complete (repos)

From: adrian15 <[email-masked]>
Date: Sat, 23 Jan 2016 01:58:46 +0100

Despite of being discussing how to implement all of these properly. I 
feel it's right to show you my current work so that you can comment on it.

I attach my updated patches.

1) The main differences from my original patches are:

* I use more additional bootloader functions
* The code ensures that each bootloader can be used in its role, being 
it a primary bootloader o a secondary bootloader. That way we avoid the 
user being able to generate non bootable isos.

2) For the time being I am not maintaining the syslinux-efi branch till 
we manage to get a consensus on this one. Then I will rebase it, change 
the partition type and re-test it.

3) As I have mentioned in another message I plan to work on giving the 
user the ability of creating isos using:
--bootloaders="grub-efi" thus using grub-efi as a primary bootloader.

4) The patches can be found as a git branch (based on live-build master 
branch) here:

https://github.com/adrian15/live-build/tree/efi_support_based_on_debian_cd_rebased_4

adrian15
-- 
Support free software. Donate to Super Grub Disk. Apoya el software 
libre. Dona a Super Grub Disk. http://www.supergrubdisk.org/donate/
commit ff8206aea1985760dfea9ec94b93686a242f137e
Author: Adrian Gibanel Lopez <[email-masked]>
Date:   Mon Jan 18 03:04:00 2016 +0000

    functions/default.sh : Define LB_PRIMARY_BOOTLOADER at the Set_defaults function which it's the right place where to do it

diff --git a/functions/defaults.sh b/functions/defaults.sh
index ddf4b19..334984f 100755
--- a/functions/defaults.sh
+++ b/functions/defaults.sh
@@ -537,6 +537,8 @@ Set_defaults ()
 		esac
 	fi
 
+	LB_PRIMARY_BOOTLOADER=$(echo "${LB_BOOTLOADERS}" | awk -F, '{ print $1 }')
+
 	# Setting checksums
 	case "${LB_MODE}" in
 		progress-linux)
@@ -845,9 +847,6 @@ Check_defaults ()
 		fi
 	fi
 
-
-	LB_PRIMARY_BOOTLOADER=$(echo "${LB_BOOTLOADERS}" | awk -F, '{ print $1 }')
-
 	if [ "${LB_PRIMARY_BOOTLOADER}" = "syslinux" ]
 	then
 		# syslinux + fat or ntfs, or extlinux + ext[234] or btrfs
commit b3ec155600f79b6ac078ae8003e806735044a372
Author: Adrian Gibanel Lopez <[email-masked]>
Date:   Wed Jan 20 00:53:53 2016 +0100

    Remove repeated LB_PRIMARY_BOOTLOADER definition

diff --git a/scripts/build/binary_hdd b/scripts/build/binary_hdd
index d0db382..0c9c5af 100755
--- a/scripts/build/binary_hdd
+++ b/scripts/build/binary_hdd
@@ -67,8 +67,6 @@ do
 	esac
 done
 
-LB_PRIMARY_BOOTLOADER=$(echo "${LB_BOOTLOADERS}" | awk -F, '{ print $1 }')
-
 case ${LB_PRIMARY_BOOTLOADER} in
 		syslinux)
 			case ${LB_BINARY_FILESYSTEM} in
diff --git a/scripts/build/binary_iso b/scripts/build/binary_iso
index d8b1553..67dfc85 100755
--- a/scripts/build/binary_iso
+++ b/scripts/build/binary_iso
@@ -107,8 +107,6 @@ then
 	XORRISO_OPTIONS="${XORRISO_OPTIONS} -V \"${LB_ISO_VOLUME}\""
 fi
 
-LB_PRIMARY_BOOTLOADER=$(echo "${LB_BOOTLOADERS}" | awk -F, '{ print $1 }')
-
 # Handle xorriso architecture specific options
 case "${LB_PRIMARY_BOOTLOADER}" in
 	grub)
commit e0abb8214b9ff71630220a24564a8770cea8658e
Author: Adrian Gibanel Lopez <[email-masked]>
Date:   Mon Jan 18 03:07:48 2016 +0000

    * Added: functions/bootloaders.sh . It has new bootloader functions that are heavily used in efi scenarios where a bootloader can act as a primary or a secondary bootloader.
    
    Since the introduction of the new switch:
    
    --bootloaders
    
    you can setup it like this:
    
    --bootloaders=syslinux,grub-efi
    
    .
    
    This means that syslinux is the primary bootloader and grub-efi is the secondary bootloader.
    
    * Added new bootloader functions: Check_Non_Primary_Bootloader and Check_Non_Secondary_Bootloader.
    
    These functions let each one of the bootloaders abort the build because
    they cannot perform a role either as a primary bootloader or as a secondary bootloader.
    
    * Added bootloader functions: Check_Primary_Bootloader_Role, Check_Secondary_Bootloader_Role and Check_Any_Bootloader_Role
    
    These functions let bootloaders to force their default role in a single line.

diff --git a/functions/bootloaders.sh b/functions/bootloaders.sh
new file mode 100644
index 0000000..0829903
--- /dev/null
+++ b/functions/bootloaders.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+
+## live-build(7) - System Build Scripts
+## Copyright (C) 2016 Adrian Gibanel Lopez <[email-masked]>
+##
+## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
+## This is free software, and you are welcome to redistribute it
+## under certain conditions; see COPYING for details.
+
+Is_Primary_Bootloader ()
+{
+	EVAL_PRIMARY_BOOTLOADER="${1}"
+
+	if [ "${LB_PRIMARY_BOOTLOADER}" = "${EVAL_PRIMARY_BOOTLOADER}" ]
+	then
+		return 0
+	else
+		return 1
+	fi
+
+}
+
+Is_Bootloader ()
+{
+	EVAL_BOOTLOADER="${1}"
+	OLDIFS="$IFS"
+	IFS=","
+	for BOOTLOADER in ${LB_BOOTLOADERS}
+	do
+		if [ "${BOOTLOADER}" = "${EVAL_BOOTLOADER}" ]
+		then
+			IFS="$OLDIFS"
+			return 0
+		fi
+	done
+	IFS="$OLDIFS"
+	return 1
+}
+
+Is_Secondary_Bootloader ()
+{
+	EVAL_SECONDARY_BOOTLOADER="${1}"
+
+	if Is_Primary_Bootloader "${EVAL_SECONDARY_BOOTLOADER}"
+	then
+		return 1
+	else
+		if Is_Bootloader "${EVAL_SECONDARY_BOOTLOADER}"
+		then
+			return 0
+		fi
+	fi
+	return 1
+
+}
+
+Check_Non_Primary_Bootloader ()
+{
+	NON_PRIMARY_BOOTLOADER="${1}"
+
+	if Is_Primary_Bootloader "${NON_PRIMARY_BOOTLOADER}"
+	then
+		Echo_error "Bootloader: ${NON_PRIMARY_BOOTLOADER} not supported as a primary bootloader."
+		exit 1
+	else
+		return 0
+	fi
+}
+
+
+Check_Non_Secondary_Bootloader ()
+{
+	NON_SECONDARY_BOOTLOADER="${1}"
+
+	if Is_Secondary_Bootloader "${NON_SECONDARY_BOOTLOADER}"
+	then
+		Echo_error "Bootloader: ${NON_SECONDARY_BOOTLOADER} not supported as a secondary bootloader."
+		exit 1
+	else
+		return 0
+	fi
+}
+
+Check_Primary_Bootloader_Role ()
+{
+	PRIMARY_BOOTLOADER_ROLE="${1}"
+	Check_Non_Secondary_Bootloader "${PRIMARY_BOOTLOADER_ROLE}"
+
+	if Is_Primary_Bootloader "${PRIMARY_BOOTLOADER_ROLE}"
+	then
+		return 0
+	else
+		exit 0
+	fi
+
+}
+
+Check_Secondary_Bootloader_Role ()
+{
+	SECONDARY_BOOTLOADER_ROLE="${1}"
+	Check_Non_Primary_Bootloader "${SECONDARY_BOOTLOADER_ROLE}"
+
+	if Is_Secondary_Bootloader "${SECONDARY_BOOTLOADER_ROLE}"
+	then
+		return 0
+	else
+		exit 0
+	fi
+
+}
+
+Check_Any_Bootloader_Role ()
+{
+	ANY_BOOTLOADER_ROLE="${1}"
+
+	if Is_Primary_Bootloader "${ANY_BOOTLOADER_ROLE}"
+	then
+		return 0
+	fi
+
+	if Is_Secondary_Bootloader "${ANY_BOOTLOADER_ROLE}"
+	then
+		return 0
+	fi
+
+	exit 0
+
+}
\ No newline at end of file
commit d32998e4c1b6332877efdf78b0f5dc4c74bac91a
Author: Adrian Gibanel Lopez <[email-masked]>
Date:   Mon Jan 18 03:15:47 2016 +0000

    binary_loopback_cfg now renders grub.cfg by default.
    The binary parts of grub-pc are left for the original binary_grub-pc.
    As a consequence both /boot/grub/grub.cfg and /boot/grub/loopback.cfg files will be present in any Debian Live CD.
    
    This might be useful to be reused from binary_grub-* bootloaders.

diff --git a/scripts/build/binary_grub-pc b/scripts/build/binary_grub-pc
index 1eae93a..998c3d4 100755
--- a/scripts/build/binary_grub-pc
+++ b/scripts/build/binary_grub-pc
@@ -70,52 +70,6 @@ Restore_cache cache/packages.binary
 # Installing depends
 Install_package
 
-# Local functions
-Grub_live_entry ()
-{
-	LABEL="${1}"
-	KERNEL="${2}"
-	INITRD="${3}"
-	APPEND="${4}"
-
-	LINUX_LIVE="${LINUX_LIVE}\nmenuentry \"Debian GNU/Linux - ${LABEL}\" {"
-	LINUX_LIVE="${LINUX_LIVE}\nlinux\t\t/${KERNEL} ${INITFS:+boot=${INITFS} }config LB_BOOTAPPEND_LIVE ${APPEND}"
-	LINUX_LIVE="${LINUX_LIVE}\ninitrd\t\t/${INITRD}"
-	LINUX_LIVE="${LINUX_LIVE}\n}"
-}
-
-Grub_install_entry ()
-{
-	LABEL="${1}"
-	KERNEL="${2}"
-	INITRD="${3}"
-	APPEND="${4}"
-
-	if [ "${LABEL}" != "rescue" ] && [ "${LABEL}" != "rescuegui" ]
-	then
-		APPEND="${APPEND} quiet"
-	fi
-
-	LINUX_INSTALL="${LINUX_INSTALL}\nmenuentry \"Debian GNU/Linux - ${LABEL}\" {"
-	LINUX_INSTALL="${LINUX_INSTALL}\nlinux\t\t/${KERNEL} ${APPEND} LB_BOOTAPPEND_INSTALL"
-	LINUX_INSTALL="${LINUX_INSTALL}\ninitrd\t\t/${INITRD}"
-	LINUX_INSTALL="${LINUX_INSTALL}\n}"
-}
-
-if [ -e "config/bootloaders/grub-pc" ]
-then
-	# Internal local copy
-	_SOURCE="config/bootloaders/grub-pc"
-else
-	# Internal system copy
-	if [ -n "${LIVE_BUILD}" ]
-	then
-		_SOURCE="${LIVE_BUILD}/share/bootloaders/grub-pc"
-	else
-		_SOURCE="/usr/share/live/build/bootloaders/grub-pc"
-	fi
-fi
-
 case "${LB_INITRAMFS}" in
 	live-boot)
 		INITFS="live"
@@ -151,84 +105,8 @@ esac
 
 Check_multiarchitectures
 
-# Creating directory
-mkdir -p "${DESTDIR_LIVE}"
-
-# Setting boot parameters
-if [ "${LB_UNION_FILESYSTEM}" != "aufs" ]
-then
-	LB_BOOTAPPEND_LIVE="${LB_BOOTAPPEND_LIVE} union=${LB_UNION_FILESYSTEM}"
-fi
-
-if [ -n "${LB_NET_COW_PATH}" ]
-then
-	Echo_error "Net cow not yet supported on grub"
-	exit 1
-fi
-
-LB_BOOTAPPEND_LIVE="$(echo ${LB_BOOTAPPEND_LIVE} | sed -e 's|  ||')"
-
-# Assembling kernel configuration
-
-# Default entries
-DEFAULT_FLAVOUR="$(echo ${LB_LINUX_FLAVOURS} | awk '{ print $1 }')"
-DEFAULT_KERNEL="$(basename chroot/boot/vmlinuz-*${DEFAULT_FLAVOUR})"
-DEFAULT_INITRD="initrd.img-$(echo ${DEFAULT_KERNEL} | sed -e 's|vmlinuz-||')"
-
-Grub_live_entry "live" "$(basename ${DESTDIR_LIVE})/${DEFAULT_KERNEL}" "$(basename ${DESTDIR_LIVE})/${DEFAULT_INITRD}"
-
-if [ "${LB_BOOTAPPEND_FAILSAFE}" != "none" ]
-then
-	Grub_live_entry "live (fail-safe mode)" "$(basename ${DESTDIR_LIVE})/${DEFAULT_KERNEL}" "$(basename ${DESTDIR_LIVE})/${DEFAULT_INITRD}" "${LB_BOOTAPPEND_FAILSAFE}"
-fi
-
-for KERNEL in chroot/boot/vmlinuz-*
-do
-	VERSION="$(basename ${KERNEL} | sed -e 's|vmlinuz-||')"
-
-	Grub_live_entry "live, kernel ${VERSION}" "$(basename ${DESTDIR_LIVE})/$(basename ${KERNEL})" "$(basename ${DESTDIR_LIVE})/initrd.img-${VERSION}"
-	Grub_live_entry "live, kernel ${VERSION} (fail-safe mode)" "$(basename ${DESTDIR_LIVE})/$(basename ${KERNEL})" "$(basename ${DESTDIR_LIVE})/initrd.img-${VERSION}" "${LB_BOOTAPPEND_FAILSAFE}"
-done
-
-LINUX_LIVE="$(/bin/echo ${LINUX_LIVE} | sed -e 's|binary||g' -e 's|//|/|g')"
-
-# Assembling debian-installer configuration
-if [ "${LB_DEBIAN_INSTALLER}" != "false" ]
-then
-	LINUX_LIVE="#\t \"Live\"\n${LINUX_LIVE}"
-	LINUX_INSTALL="#\t \"Installer\"\n"
-
-	VMLINUZ_DI="install/vmlinuz"
-	INITRD_DI="install/initrd.gz"
-	APPEND_DI="vga=normal"
-
-	VMLINUZ_GI="install/gtk/vmlinuz"
-	INITRD_GI="install/gtk/initrd.gz"
-	APPEND_GI="video=vesa:ywrap,mtrr vga=788"
-
-	Grub_install_entry "install" "${VMLINUZ_DI}" "${INITRD_DI}" "${APPEND_DI}"
-	Grub_install_entry "installgui" "${VMLINUZ_GI}" "${INITRD_GI}" "${APPEND_GI}"
-	Grub_install_entry "expert" "${VMLINUZ_DI}" "${INITRD_DI}" "priority=low ${APPEND_DI}"
-	Grub_install_entry "expertgui" "${VMLINUZ_GI}" "${INITRD_GI}" "priority=low ${APPEND_GI}"
-	Grub_install_entry "rescue" "${VMLINUZ_DI}" "${INITRD_DI}" "rescue/enable=true ${APPEND_DI}"
-	Grub_install_entry "rescuegui" "${VMLINUZ_GI}" "${INITRD_GI}" "rescue/enable=true ${APPEND_GI}"
-	Grub_install_entry "auto" "${VMLINUZ_DI}" "${INITRD_DI}" "auto=true priority=critical ${APPEND_DI}"
-	Grub_install_entry "autogui" "${VMLINUZ_GI}" "${INITRD_GI}" "auto=true priority=critical ${APPEND_GI}"
-fi
-
-LINUX_INSTALL="$(/bin/echo ${LINUX_INSTALL} | sed -e 's|binary||g' -e 's|//|/|g')"
-
-# Assembling memtest configuration
-if [ -f "${DESTDIR_LIVE}"/memtest ]
-then
-	MEMTEST="#\t \"Other\"\n"
-	MEMTEST="${MEMTEST}\nmenuentry\t\"${LB_MEMTEST}\" {\nlinux16\t$(basename ${DESTDIR_LIVE})/memtest\n}"
-	MEMTEST="$(/bin/echo ${MEMTEST} | sed -e 's|//|/|g')"
-fi
-
 # Copying templates
 mkdir -p binary/boot/grub/i386-pc
-cp -r "${_SOURCE}"/* binary/boot/grub
 
 case ${LIVE_IMAGE_TYPE} in
 	iso*)
@@ -245,14 +123,11 @@ then
 	FILES="$(echo ${FILES} | sed -e 's|chroot||g')"
 fi
 
+# We rely on: binary_loopback_cfg to generate grub.cfg and other configuration files
+
 # Copying grub
 cp ${FILES} binary/boot/grub/i386-pc
 
-sed -i -e "s|LINUX_LIVE|${LINUX_LIVE}|" -e "s|LINUX_INSTALL|${LINUX_INSTALL}|" -e "s|MEMTEST|${MEMTEST}|" binary/boot/grub/grub.cfg
-sed -i -e "s#LB_BOOTAPPEND_INSTALL#${LB_BOOTAPPEND_INSTALL}#" -e "s#LB_BOOTAPPEND_LIVE#${LB_BOOTAPPEND_LIVE}#" binary/boot/grub/grub.cfg
-
-sed -i -e 's|\ $||g' binary/boot/grub/grub.cfg
-
 # Saving cache
 Save_cache cache/packages.binary
 
diff --git a/scripts/build/binary_loopback_cfg b/scripts/build/binary_loopback_cfg
index 6034ddc..6031a8d 100755
--- a/scripts/build/binary_loopback_cfg
+++ b/scripts/build/binary_loopback_cfg
@@ -330,12 +330,14 @@ fi
 
 # Copying templates
 mkdir -p binary/boot/grub
-cp "${_SOURCE}"/grub.cfg binary/boot/grub/loopback.cfg
+cp "${_SOURCE}"/grub.cfg binary/boot/grub/grub.cfg
 
-sed -i -e "s|LINUX_LIVE|${LINUX_LIVE}|" -e "s|LINUX_INSTALL||" -e "s|MEMTEST|${MEMTEST}|" binary/boot/grub/loopback.cfg
-sed -i -e "s#LB_BOOTAPPEND_INSTALL##" -e "s#LB_BOOTAPPEND_LIVE#${LB_BOOTAPPEND_LIVE}#" binary/boot/grub/loopback.cfg
+sed -i -e "s|LINUX_LIVE|${LINUX_LIVE}|" -e "s|LINUX_INSTALL||" -e "s|MEMTEST|${MEMTEST}|" binary/boot/grub/grub.cfg
+sed -i -e "s#LB_BOOTAPPEND_INSTALL##" -e "s#LB_BOOTAPPEND_LIVE#${LB_BOOTAPPEND_LIVE}#" binary/boot/grub/grub.cfg
 
-sed -i -e 's|\ $||g' binary/boot/grub/loopback.cfg
+sed -i -e 's|\ $||g' binary/boot/grub/grub.cfg
+
+echo "source /boot/grub/grub.cfg" > binary/boot/grub/loopback.cfg
 
 # Creating stage file
 Create_stagefile .build/binary_loopback_cfg
commit 121bfa64b12a1efed35f29096f69909ee6abf203
Author: Adrian Gibanel Lopez <[email-masked]>
Date:   Mon Jan 18 03:01:15 2016 +0000

    Stolen efi-image and grub-cpmodules from src:live-installer
    
    These two scripts simplify the creation of efi images based on grub-efi.
    I have decided to simply steal them. If I had to include them thanks to a source package that would have mean that an src repo would have to be defined by default.
    TODO: Ask in a bug a RFE so that these two scripts are put into a binary that could be consumed by both live-installer and live-build packages.

diff --git a/scripts/build/efi-image b/scripts/build/efi-image
new file mode 100755
index 0000000..724095f
--- /dev/null
+++ b/scripts/build/efi-image
@@ -0,0 +1,85 @@
+#! /bin/sh
+set -e
+
+# Copyright (C) 2010, 2011 Canonical Ltd.
+# Author: Colin Watson <[email-masked]>
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; either version 2, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# Make an EFI boot image.
+
+if [ -z "$1" ] || [ -z "$2" ]; then
+	echo "usage: $0 OUTPUT-DIRECTORY GRUB-PLATFORM EFI-NAME NETBOOT-PREFIX"
+	exit 1
+fi
+
+outdir="$1"
+platform="$2"
+efi_name="$3"
+netboot_prefix="$4"
+
+memdisk_img=
+workdir=
+
+cleanup () {
+	[ -z "$memdisk_img" ] || rm -f "$memdisk_img"
+	[ -z "$workdir" ] || rm -rf "$workdir"
+}
+trap cleanup EXIT HUP INT QUIT TERM
+
+rm -rf "$outdir"
+mkdir -p "$outdir"
+
+memdisk_img="$(mktemp efi-image.XXXXXX)"
+workdir="$(mktemp -d efi-image.XXXXXX)"
+
+# Skeleton configuration file which finds the real boot disk.
+mkdir -p "$workdir/boot/grub"
+cat >"$workdir/boot/grub/grub.cfg" <<EOF
+search --file --set=root /.disk/info
+set prefix=(\$root)/boot/grub
+source \$prefix/$platform/grub.cfg
+EOF
+
+mkdir -p "$outdir/boot/grub/$platform"
+(for i in /usr/lib/grub/$platform/part_*.mod; do
+    i=`echo $i | sed 's?^.*/??g;s?\.mod$??g;'`
+	echo "insmod $i"
+ done; \
+ echo "source /boot/grub/grub.cfg") >"$outdir/boot/grub/$platform/grub.cfg"
+
+# Build the core image.
+(cd "$workdir"; tar -cf - boot) >"$memdisk_img"
+grub-mkimage -O "$platform" -m "$memdisk_img" \
+	-o "$workdir/boot$efi_name.efi" -p '(memdisk)/boot/grub' \
+	search iso9660 configfile normal memdisk tar part_msdos part_gpt fat
+
+grub-mkimage -O "$platform" \
+	-o "$outdir/bootnet$efi_name.efi" -p "$netboot_prefix/grub" \
+	search configfile normal efinet tftp net
+
+# Stuff it into a FAT filesystem, making it as small as possible.  24KiB
+# headroom seems to be enough; (x+31)/32*32 rounds up to multiple of 32.
+mkfs.msdos -C "$outdir/efi.img" \
+	$(( ($(stat -c %s "$workdir/boot$efi_name.efi") / 1024 + 55) \
+	    / 32 * 32 ))
+mmd -i "$outdir/efi.img" ::efi
+mmd -i "$outdir/efi.img" ::efi/boot
+mcopy -i "$outdir/efi.img" "$workdir/boot$efi_name.efi" \
+	"::efi/boot/boot$efi_name.efi"
+
+grub-cpmodules "$outdir" "$platform"
+
+exit 0
diff --git a/scripts/build/grub-cpmodules b/scripts/build/grub-cpmodules
new file mode 100755
index 0000000..437ebdf
--- /dev/null
+++ b/scripts/build/grub-cpmodules
@@ -0,0 +1,55 @@
+#! /bin/sh
+set -e
+
+# Copyright (C) 2010, 2011 Canonical Ltd.
+# Author: Colin Watson <[email-masked]>
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; either version 2, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# Copy GRUB modules.
+
+# TODO: take the modules list as parameter, and only include the needed modules, using moddeps.lst to get dependencies
+
+if [ -z "$1" ] || [ -z "$2" ]; then
+	echo "usage: $0 OUTPUT-DIRECTORY GRUB-PLATFORM"
+	exit 1
+fi
+
+outdir="$1"
+platform="$2"
+
+# Copy over GRUB modules, except for those already built in.
+cp -a "/usr/lib/grub/$platform"/*.lst "$outdir/boot/grub/$platform/"
+for x in "/usr/lib/grub/$platform"/*.mod; do
+	# TODO: Some of these exclusions are based on knowledge of module
+	# dependencies.  It would be nice to have a way to read the module
+	# list directly out of the image.
+	case $(basename "$x" .mod) in
+	    configfile|fshelp|iso9660|memdisk|search|search_fs_file|search_fs_uuid|search_label|tar)
+		# included in boot image
+		;;
+	    affs|afs|afs_be|befs|befs_be|minix|nilfs2|sfs|zfs|zfsinfo)
+		# unnecessary filesystem modules
+		;;
+	    example_functional_test|functional_test|hello)
+		# other cruft
+		;;
+	    *)
+		cp -a "$x" "$outdir/boot/grub/$platform/"
+		;;
+	esac
+done
+
+exit 0
commit 7b7d5f3b1fe74b46a97d36b14cb5ce6e2d0eb0e3
Author: Adrian Gibanel Lopez <[email-masked]>
Date:   Mon Jan 18 03:21:39 2016 +0000

    Support for EFI support by the means of grub-efi
    
    This work is based on debian-cd team work and uses,
    as much as possible, the same mkisofs options
    than the Debian Installation CD disk does.
    
    It assumes that /boot/grub/grub.cfg (and other design items)
    is generated by: binary_loopback_cfg .
    
    It relies on efi-image and grub-cpmodules being setup
    as build scripts on live-build package.
    In the future event of these two files being moved
    to a binary package (they are originally from:
    src: live-installer) the binary_grub-efi script would have
    to be rewritten to take the new paths into account.

diff --git a/manpages/en/lb_config.1 b/manpages/en/lb_config.1
index 8e1f4f7..7e3c956 100644
--- a/manpages/en/lb_config.1
+++ b/manpages/en/lb_config.1
@@ -39,7 +39,7 @@
 .br
 	[\fB\-\-bootappend\-live\fR \fIPARAMETER\fR|\fI"PARAMETERS"\fR]
 .br
-	[\fB\-\-bootloader\fR grub|grub2|syslinux]
+	[\fB\-\-bootloader\fR grub|grub2|syslinux|grub-efi]
 .br
 	[\fB\-\-cache\fR true|false]
 .br
@@ -263,7 +263,7 @@ defines the filesystem to be used in the image type. This only has an effect if
 sets boot parameters specific to debian\-installer, if included.
 .IP "\fB\-\-bootappend\-live\fR \fIPARAMETER\fR|""\fIPARAMETERS\fR""" 4
 sets boot parameters specific to debian\-live. A complete list of boot parameters can be found in the \fIlive\-boot\fR(7) and \fIlive\-config\fR(7) manual pages.
-.IP "\fB\-\-bootloader\fR grub|grub2|syslinux" 4
+.IP "\fB\-\-bootloader\fR grub|grub2|syslinux|grub-efi" 4
 defines which bootloader is being used in the generated image. This has only an effect if the selected binary image type does allow to choose the bootloader. For example, if you build a iso, always syslinux (or more precise, isolinux) is being used. Also note that some combinations of binary images types and bootloaders may be possible but live\-build does not support them yet. \fBlb config\fR will fail to create such a not yet supported configuration and give a explanation about it. For hdd images on amd64 and i386, the default is syslinux.
 .IP "\fB\-\-cache\fR true|false" 4
 defines globally if any cache should be used at all. Different caches can be controlled through the their own options.
diff --git a/scripts/build/binary b/scripts/build/binary
index 56c7bf8..7b0d743 100755
--- a/scripts/build/binary
+++ b/scripts/build/binary
@@ -69,6 +69,7 @@ lb binary_loadlin ${@}
 lb binary_win32-loader ${@}
 lb binary_includes ${@}
 lb binary_hooks ${@}
+lb binary_grub-efi ${@}
 lb binary_checksums ${@}
 
 if [ "${LB_BUILD_WITH_CHROOT}" != "true" ]
diff --git a/scripts/build/binary_grub-efi b/scripts/build/binary_grub-efi
new file mode 100755
index 0000000..faf3420
--- /dev/null
+++ b/scripts/build/binary_grub-efi
@@ -0,0 +1,255 @@
+#!/bin/sh
+
+## live-build(7) - System Build Scripts
+## Copyright (C) 2016 Adrian Gibanel Lopez <[email-masked]>
+##
+## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
+## This is free software, and you are welcome to redistribute it
+## under certain conditions; see COPYING for details.
+
+
+set -e
+
+# Including common functions
+[ -e "${LIVE_BUILD}/scripts/build.sh" ] && . "${LIVE_BUILD}/scripts/build.sh" || . /usr/lib/live/build.sh
+
+# Setting static variables
+DESCRIPTION="$(Echo 'prepares and installs Grub based EFI support into binary')"
+HELP=""
+USAGE="${PROGRAM} [--force]"
+
+Arguments "${@}"
+
+# Reading configuration files
+Read_conffiles config/all config/common config/bootstrap config/chroot config/binary config/source
+Set_defaults
+
+Check_Secondary_Bootloader_Role "grub-efi"
+
+Echo_message "Begin preparing Grub based EFI support..."
+
+# Requiring stage file
+Require_stagefile .build/config .build/bootstrap
+
+# Checking stage file
+Check_stagefile .build/binary_grub-efi
+
+# Checking lock file
+Check_lockfile .lock
+
+# Creating lock file
+Create_lockfile .lock
+
+# Check architecture
+Check_architectures amd64 i386
+Check_crossarchitectures
+
+case "${LB_ARCHITECTURES}" in
+	amd64)
+		_EFI_TYPE=efi64
+		;;
+	i386)
+		_EFI_TYPE=efi32
+		;;
+	*)
+		echo "ERROR: can't provide EFI boot support to architecture ${LB_ARCHITECTURES}" >&2
+		exit 1
+		;;
+esac
+
+
+# Checking depends
+case "${LB_BUILD_WITH_CHROOT}" in
+	true)
+		_CHROOT_DIR=""
+		_SYSLINUX_EFI_DIR="chroot/usr/lib/SYSLINUX.EFI/$_EFI_TYPE"
+		_SYSLINUX_COMMON_DIR="chroot/usr/lib/syslinux/modules/$_EFI_TYPE"
+
+		Check_package chroot /usr/lib/grub/x86_64-efi/configfile.mod grub-efi-amd64-bin
+		Check_package chroot /usr/lib/grub/i386-efi/configfile.mod grub-efi-ia32-bin
+		Check_package chroot /usr/bin/grub-mkimage grub-common
+		Check_package chroot /usr/bin/mcopy mtools
+		Check_package chroot /sbin/mkfs.msdos dosfstools
+		;;
+
+	false)
+		_CHROOT_DIR="chroot"
+
+		if [ ! -e /usr/lib/grub/x86_64-efi ]
+		then
+			# grub-efi-amd64-bin
+			Echo_error "/usr/lib/grub/x86_64-efi - no such directory"
+			exit 1
+		fi
+
+		if [ ! -e /usr/lib/grub/i386-efi ]
+		then
+			# grub-efi-ia32-bin
+			Echo_error "/usr/lib/grub/i386-efi - no such directory"
+			exit 1
+		fi
+
+		if [ ! -e /usr/bin/grub-mkimage ]
+		then
+			# grub-common
+			Echo_error "/usr/bin/grub-mkimage - no such file."
+			exit 1
+		fi
+
+		if [ ! -e /usr/bin/mcopy ]
+		then
+			# mtools
+			Echo_error "/usr/bin/mcopy - no such file."
+			exit 1
+		fi
+
+		if [ ! -e /sbin/mkfs.msdos ]
+		then
+			# dosfstools
+			Echo_error "/sbin/mkfs.msdos - no such file."
+			exit 1
+		fi
+		;;
+esac
+
+
+
+
+case "${LB_INITRAMFS}" in
+	live-boot)
+		INITFS="live"
+		;;
+
+	*)
+		INITFS=""
+		;;
+esac
+
+# Setting destination directory
+case "${LIVE_IMAGE_TYPE}" in
+	iso*|tar)
+		case "${LB_INITRAMFS}" in
+			live-boot)
+				DESTDIR_LIVE="binary/live"
+				;;
+
+			*)
+				DESTDIR_LIVE="binary/live"
+				;;
+		esac
+
+		DESTDIR_INSTALL="binary/install"
+		;;
+
+	hdd*|netboot)
+		Echo_warning "Bootloader in this image type not yet supported by live-build."
+		Echo_warning "This would produce a not bootable image, aborting (FIXME)."
+		exit 1
+	;;
+esac
+
+# Restoring cache
+Restore_cache cache/packages.binary
+
+# Installing depends
+Install_package
+
+# Cleanup files that we generate
+rm -rf binary/boot/efi.img binary/boot/grub/i386-efi/ binary/boot/grub/x86_64-efi
+
+# This is workaround till both efi-image and grub-cpmodules are put into a binary package
+case "${LB_BUILD_WITH_CHROOT}" in
+        true)
+		if [ ! -e "${LIVE_BUILD}" ] ; then
+			LIVE_BUILD_PATH="/usr/lib/live/build"
+		else
+			LIVE_BUILD_PATH="${LIVE_BUILD}/scripts/build"
+		fi
+		mkdir -p chroot/${LIVE_BUILD_PATH}
+		cp "${LIVE_BUILD_PATH}/efi-image" "chroot/${LIVE_BUILD_PATH}"
+		cp "${LIVE_BUILD_PATH}/grub-cpmodules" "chroot/${LIVE_BUILD_PATH}"
+        ;;
+esac
+#####
+cat >binary.sh <<END
+#!/bin/sh
+
+set -e
+
+PRE_EFI_IMAGE_PATH="${PATH}"
+if [ ! -e "${LIVE_BUILD}" ] ; then
+	LIVE_BUILD_PATH="/usr/lib/live/build"
+else
+	LIVE_BUILD_PATH="${LIVE_BUILD}/scripts/build"
+fi
+
+PATH="${PATH}:\${LIVE_BUILD_PATH}" # Make sure grub-cpmodules is used as if it was installed in the system
+
+"\${LIVE_BUILD_PATH}/efi-image" "${_CHROOT_DIR}/grub-efi-temp-x86_64-efi/" "x86_64-efi" "x64" "debian-live/amd64"
+mkdir -p ${_CHROOT_DIR}/grub-efi-temp/efi/boot
+mcopy -n -i ${_CHROOT_DIR}/grub-efi-temp-x86_64-efi/efi.img '::efi/boot/boot*.efi' ${_CHROOT_DIR}/grub-efi-temp/efi/boot
+cp -r "${_CHROOT_DIR}"/grub-efi-temp-x86_64-efi/* "${_CHROOT_DIR}/grub-efi-temp/"
+
+"\${LIVE_BUILD_PATH}/efi-image" "${_CHROOT_DIR}/grub-efi-temp-i386-efi/" "i386-efi" "ia32" "debian-live/i386"
+PATH="\${PRE_EFI_IMAGE_PATH}"
+mkdir -p ${_CHROOT_DIR}/grub-efi-temp/efi/boot
+mcopy -n -i ${_CHROOT_DIR}/grub-efi-temp-i386-efi/efi.img '::efi/boot/boot*.efi' ${_CHROOT_DIR}/grub-efi-temp/efi/boot
+cp -r "${_CHROOT_DIR}"/grub-efi-temp-i386-efi/* "${_CHROOT_DIR}/grub-efi-temp/"
+
+# The code below is adapted from tools/boot/jessie/boot-x86
+# in debian-cd
+
+# Stuff the EFI boot files into a FAT filesystem, making it as
+# small as possible.  24KiB headroom seems to be enough;
+# (x+31)/32*32 rounds up to multiple of 32.
+# This is the same as in efi-image, but we need to redo it here in
+# the case of a multi-arch amd64/i386 image
+
+size=0
+for file in ${_CHROOT_DIR}/grub-efi-temp/efi/boot/boot*.efi; do
+	size=\$((\$size + \$(stat -c %s "\$file")))
+done
+
+blocks=\$(((\$size / 1024 + 55) / 32 * 32 ))
+
+rm -f ${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img
+mkfs.msdos -C "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" \$blocks >/dev/null
+mmd -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ::efi
+mmd -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ::efi/boot
+mcopy -o -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ${_CHROOT_DIR}/grub-efi-temp/efi/boot/boot*.efi \
+	"::efi/boot"
+END
+
+case "${LB_BUILD_WITH_CHROOT}" in
+        true)
+			mv binary.sh chroot/
+			Chroot chroot "sh binary.sh"
+			rm -f chroot/binary.sh
+
+			# Saving cache
+			Save_cache cache/packages.binary
+
+			# Removing depends
+			Remove_package
+			;;
+
+		false)
+			sh binary.sh
+			rm -f binary.sh
+			;;
+esac
+
+# Remove unnecessary files
+rm -f chroot/grub-efi-temp/bootnetia32.efi
+rm -f chroot/grub-efi-temp/bootnetx64.efi
+
+mkdir -p binary
+cp -r chroot/grub-efi-temp/* binary/
+rm -rf chroot/grub-efi-temp-x86_64-efi
+rm -rf chroot/grub-efi-temp-i386-efi
+rm -rf chroot/grub-efi-temp
+
+# We rely on: binary_loopback_cfg to generate grub.cfg and other configuration files
+
+# Creating stage file
+Create_stagefile .build/binary_grub-efi
diff --git a/scripts/build/binary_iso b/scripts/build/binary_iso
index 67dfc85..e716034 100755
--- a/scripts/build/binary_iso
+++ b/scripts/build/binary_iso
@@ -144,6 +144,17 @@ case "${LB_PRIMARY_BOOTLOADER}" in
 		;;
 esac
 
+if Is_Secondary_Bootloader "grub-efi"
+then
+	if [ -e binary/boot/grub/efi.img ]
+	then
+		XORRISO_OPTIONS="${XORRISO_OPTIONS} -eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot"
+		XORRISO_OPTIONS="${XORRISO_OPTIONS} -isohybrid-gpt-basdat -isohybrid-apm-hfsplus"
+	else
+		Echo "No EFI boot code to include in the ISO"
+	fi
+fi
+
 #if [ "${LB_DEBIAN_INSTALLER}" != "live" ]
 #then
 #	XORRISO_OPTIONS="${XORRISO_OPTIONS} -m ${XORRISO_EXCLUDE}"
commit 920900684f7425dff57e47a073dbc2c2f6503022
Author: Adrian Gibanel Lopez <[email-masked]>
Date:   Mon Jan 18 03:13:37 2016 +0000

    Many binary bootloaders were rewritten to make use of the new bootloader role functions.
    
    Since the introduction of the new switch:
    
    --bootloaders
    
    you can setup it like this:
    
    --bootloaders=syslinux,grub-efi
    
    .
    
    This means that syslinux is the primary bootloader and grub-efi is the secondary bootloader.
    
    This commit modifies many of these bootloaders so that the current roles for these binaries was enforced:
    
    binary_grub-efi : Secondary bootloader
    binary_grub-legacy : Primary bootloader
    binary_grub-pc : Primary bootloader
    binary_syslinux : Primary bootloader
    binary_syslinux-efi : Secondary bootloader
    
    If a bootloader is tried to be used in a role that it's not meant to be used then it's ignored. That might lead to a non-bootable system.

diff --git a/scripts/build/binary_grub-legacy b/scripts/build/binary_grub-legacy
index f885c00..4057151 100755
--- a/scripts/build/binary_grub-legacy
+++ b/scripts/build/binary_grub-legacy
@@ -24,22 +24,7 @@ Arguments "${@}"
 Read_conffiles config/all config/common config/bootstrap config/chroot config/binary config/source
 Set_defaults
 
-FOUND_MYSELF=""
-IFS=","
-for BOOTLOADER in ${LB_BOOTLOADERS}
-do
-
-       case ${BOOTLOADER} in
-               "grub-legacy" )
-                       FOUND_MYSELF="True"
-                       break ;;
-       esac
-
-done
-
-if [ -z ${FOUND_MYSELF} ] ; then
-        exit 0
-fi
+Check_Primary_Bootloader_Role "grub-legacy"
 
 Echo_message "Begin installing grub-legacy..."
 
diff --git a/scripts/build/binary_grub-pc b/scripts/build/binary_grub-pc
index 998c3d4..b644519 100755
--- a/scripts/build/binary_grub-pc
+++ b/scripts/build/binary_grub-pc
@@ -24,24 +24,7 @@ Arguments "${@}"
 Read_conffiles config/all config/common config/bootstrap config/chroot config/binary config/source
 Set_defaults
 
-FOUND_MYSELF=""
-OLDIFS="$IFS"
-IFS=","
-for BOOTLOADER in ${LB_BOOTLOADERS}
-do
-
-       case ${BOOTLOADER} in
-               "grub-pc" )
-                       FOUND_MYSELF="True"
-                       break ;;
-       esac
-
-done
-IFS="$OLDIFS"
-
-if [ -z ${FOUND_MYSELF} ] ; then
-        exit 0
-fi
+Check_Primary_Bootloader_Role "grub-pc"
 
 Echo_message "Begin installing grub-pc..."
 
diff --git a/scripts/build/binary_iso b/scripts/build/binary_iso
index e716034..b11ceb0 100755
--- a/scripts/build/binary_iso
+++ b/scripts/build/binary_iso
@@ -138,7 +138,7 @@ case "${LB_PRIMARY_BOOTLOADER}" in
 		;;
 
 	*)
-		Echo_warning "Bootloader on your architecture not yet supported by live-build."
+		Echo_warning "Bootloader on your architecture not yet supported by live-build as the primary bootloader."
 		Echo_warning "This will produce a most likely not bootable image (Continuing in 5 seconds)."
 		sleep 5
 		;;
diff --git a/scripts/build/binary_syslinux b/scripts/build/binary_syslinux
index 024b563..8398692 100755
--- a/scripts/build/binary_syslinux
+++ b/scripts/build/binary_syslinux
@@ -24,24 +24,7 @@ Arguments "${@}"
 Read_conffiles config/all config/common config/bootstrap config/chroot config/binary config/source
 Set_defaults
 
-FOUND_MYSELF=""
-OLDIFS="$IFS"
-IFS=","
-for BOOTLOADER in ${LB_BOOTLOADERS}
-do
-
-       case ${BOOTLOADER} in
-               "syslinux" )
-                       FOUND_MYSELF="True"
-                       break ;;
-       esac
-
-done
-IFS="$OLDIFS"
-
-if [ -z ${FOUND_MYSELF} ] ; then
-        exit 0
-fi
+Check_Primary_Bootloader_Role "syslinux"
 
 Echo_message "Begin installing syslinux..."
 
commit 5eba3dff5a16a34c3c1eb5d54e3767339654e2d0
Author: Adrian Gibanel Lopez <[email-masked]>
Date:   Mon Jan 18 03:29:30 2016 +0000

    Make: syslinux,grub-efi the default bootloaders because
    UEFI is getting widely adopted but BIOS support is still
    needed.

diff --git a/functions/defaults.sh b/functions/defaults.sh
index 334984f..1231b61 100755
--- a/functions/defaults.sh
+++ b/functions/defaults.sh
@@ -532,7 +532,7 @@ Set_defaults ()
 	then
 		case "${LB_ARCHITECTURES}" in
 			amd64|i386)
-				LB_BOOTLOADERS="syslinux"
+				LB_BOOTLOADERS="syslinux,grub-efi"
 				;;
 		esac
 	fi