Index: head/sys/boot/efi/boot1/Makefile
===================================================================
--- head/sys/boot/efi/boot1/Makefile	(revision 293424)
+++ head/sys/boot/efi/boot1/Makefile	(revision 293425)
@@ -1,109 +1,116 @@
 # $FreeBSD$
 
 MAN=
 
 .include <bsd.own.mk>
 
 # In-tree GCC does not support __attribute__((ms_abi)).
 .if ${COMPILER_TYPE} != "gcc"
 
 MK_SSP=		no
 
 PROG=		boot1.sym
 INTERNALPROG=
 
 # architecture-specific loader code
 SRCS=	boot1.c self_reloc.c start.S
 
 CFLAGS+=	-I.
 CFLAGS+=	-I${.CURDIR}/../include
 CFLAGS+=	-I${.CURDIR}/../include/${MACHINE}
 CFLAGS+=	-I${.CURDIR}/../../../contrib/dev/acpica/include
 CFLAGS+=	-I${.CURDIR}/../../..
 
 # Always add MI sources and REGULAR efi loader bits
 .PATH:		${.CURDIR}/../loader/arch/${MACHINE}
 .PATH:		${.CURDIR}/../loader
 .PATH:		${.CURDIR}/../../common
 CFLAGS+=	-I${.CURDIR}/../../common
 
 FILES=	boot1.efi boot1.efifat
 FILESMODE_boot1.efi=	${BINMODE}
 
 LDSCRIPT=	${.CURDIR}/../loader/arch/${MACHINE}/ldscript.${MACHINE}
 LDFLAGS=	-Wl,-T${LDSCRIPT} -Wl,-Bsymbolic -shared
 
 .if ${MACHINE_CPUARCH} == "aarch64"
 CFLAGS+=	-msoft-float -mgeneral-regs-only
 .endif
 .if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
 CFLAGS+=	-fPIC
 LDFLAGS+=	-Wl,-znocombreloc
 .endif
 
 .if ${MACHINE_CPUARCH} == "arm" || ${MACHINE_CPUARCH} == "i386"
 #
 # Add libstand for the runtime functions used by the compiler - for example
 # __aeabi_* (arm) or __divdi3 (i386).
 #
 DPADD+=		${LIBSTAND}
 LDADD+=		-lstand
 .endif
 
 DPADD+=		${LDSCRIPT}
 
 OBJCOPY?=	objcopy
 OBJDUMP?=	objdump
 
 .if ${MACHINE_CPUARCH} == "amd64"
 EFI_TARGET=	efi-app-x86_64
 .elif ${MACHINE_CPUARCH} == "i386"
 EFI_TARGET=	efi-app-ia32
 .else
 EFI_TARGET=	binary
 .endif
 
 boot1.efi: ${PROG}
 	if [ `${OBJDUMP} -t ${.ALLSRC} | fgrep '*UND*' | wc -l` != 0 ]; then \
 		${OBJDUMP} -t ${.ALLSRC} | fgrep '*UND*'; \
 		exit 1; \
 	fi
 	${OBJCOPY} -j .peheader -j .text -j .sdata -j .data \
 		-j .dynamic -j .dynsym -j .rel.dyn \
 		-j .rela.dyn -j .reloc -j .eh_frame \
 		--output-target=${EFI_TARGET} ${.ALLSRC} ${.TARGET}
 
 boot1.o: ${.CURDIR}/../../common/ufsread.c
 
 # The following inserts our objects into a template FAT file system
 # created by generate-fat.sh
 
 .include "${.CURDIR}/Makefile.fat"
+BOOT1_MAXSIZE?=	131072
 
 boot1.efifat: boot1.efi
+	@set -- `ls -l boot1.efi`; \
+	x=$$(($$5-${BOOT1_MAXSIZE})); \
+	if [ $$x -ge 0 ]; then \
+	    echo "boot1 $$x bytes too large; regenerate FAT templates?" >&2 ;\
+	    exit 1; \
+	fi
 	echo ${.OBJDIR}
 	uudecode ${.CURDIR}/fat-${MACHINE}.tmpl.bz2.uu
 	mv fat-${MACHINE}.tmpl.bz2 ${.TARGET}.bz2
 	bzip2 -f -d ${.TARGET}.bz2
 	dd if=boot1.efi of=${.TARGET} seek=${BOOT1_OFFSET} conv=notrunc
 
 CLEANFILES= boot1.efi boot1.efifat
 
 .endif # ${COMPILER_TYPE} != "gcc"
 
 .include <bsd.prog.mk>
 
 beforedepend ${OBJS}: machine
 
 CLEANFILES+=   machine
 
 machine:
 	ln -sf ${.CURDIR}/../../../${MACHINE}/include machine
 
 .if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
 beforedepend ${OBJS}: x86
 CLEANFILES+=   x86
 
 x86:
 	ln -sf ${.CURDIR}/../../../x86/include x86
 .endif
Index: head/sys/boot/efi/boot1/generate-fat.sh
===================================================================
--- head/sys/boot/efi/boot1/generate-fat.sh	(revision 293424)
+++ head/sys/boot/efi/boot1/generate-fat.sh	(revision 293425)
@@ -1,69 +1,80 @@
 #!/bin/sh
 
 # This script generates the dummy FAT filesystem used for the EFI boot
 # blocks. It uses newfs_msdos to generate a template filesystem with the
 # relevant interesting files. These are then found by grep, and the offsets
 # written to a Makefile snippet.
 #
 # Because it requires root, and because it is overkill, we do not
 # do this as part of the normal build. If makefs(8) grows workable FAT
 # support, this should be revisited.
 
 # $FreeBSD$
 
 FAT_SIZE=1600 			#Size in 512-byte blocks of the produced image
 
 BOOT1_SIZE=128k
 
 #
 # Known filenames
 # amd64:   BOOTx64.efi
 # arm64:   BOOTaa64.efi
 # arm:     BOOTarm.efi
 # i386:    BOOTia32.efi
 #
 if [ -z "$2" ]; then
 	echo "Usage: $0 arch boot-filename"
 	exit 1
 fi
 
 ARCH=$1
 FILENAME=$2
 
 # Generate 800K FAT image
 OUTPUT_FILE=fat-${ARCH}.tmpl
 
 dd if=/dev/zero of=$OUTPUT_FILE bs=512 count=$FAT_SIZE
 DEVICE=`mdconfig -a -f $OUTPUT_FILE`
 newfs_msdos -F 12 -L EFI $DEVICE
 mkdir stub
 mount -t msdosfs /dev/$DEVICE stub
 
 # Create and bless a directory for the boot loader
 mkdir -p stub/efi/boot
 
 # Make a dummy file for boot1
 echo 'Boot1 START' | dd of=stub/efi/boot/$FILENAME cbs=$BOOT1_SIZE count=1 conv=block
 
 umount stub
 mdconfig -d -u $DEVICE
 rmdir stub
 
 # Locate the offset of the fake file
 BOOT1_OFFSET=$(hd $OUTPUT_FILE | grep 'Boot1 START' | cut -f 1 -d ' ')
 
 # Convert to number of blocks
 BOOT1_OFFSET=$(echo 0x$BOOT1_OFFSET | awk '{printf("%x\n",$1/512);}')
 
+# Record maximum boot1 size in bytes
+case $BOOT1_SIZE in
+*k)
+	BOOT1_MAXSIZE=$(expr ${BOOT1_SIZE%k} '*' 1024)
+	;;
+*)
+	BOOT1_MAXSIZE=$BOOT1_SIZE
+	;;
+esac
+
 echo '# This file autogenerated by generate-fat.sh - DO NOT EDIT' > Makefile.fat
 echo '# $FreeBSD$' >> Makefile.fat
 echo "BOOT1_OFFSET=0x$BOOT1_OFFSET" >> Makefile.fat
+echo "BOOT1_MAXSIZE=$BOOT1_MAXSIZE" >> Makefile.fat
 
 bzip2 $OUTPUT_FILE
 echo 'FAT template boot filesystem created by generate-fat.sh' > $OUTPUT_FILE.bz2.uu
 echo 'DO NOT EDIT' >> $OUTPUT_FILE.bz2.uu
 echo '$FreeBSD$' >> $OUTPUT_FILE.bz2.uu
 
 uuencode $OUTPUT_FILE.bz2 $OUTPUT_FILE.bz2 >> $OUTPUT_FILE.bz2.uu
 rm $OUTPUT_FILE.bz2