Makefile.bootimage revision 1.31
11.31Stsutsui#	$NetBSD: Makefile.bootimage,v 1.31 2023/12/03 07:17:11 tsutsui Exp $
21.1Stsutsui#
31.1Stsutsui# Copyright (c) 2009, 2010, 2011 Izumi Tsutsui.  All rights reserved.
41.1Stsutsui#
51.1Stsutsui# Redistribution and use in source and binary forms, with or without
61.1Stsutsui# modification, are permitted provided that the following conditions
71.1Stsutsui# are met:
81.1Stsutsui# 1. Redistributions of source code must retain the above copyright
91.1Stsutsui#    notice, this list of conditions and the following disclaimer.
101.1Stsutsui# 2. Redistributions in binary form must reproduce the above copyright
111.1Stsutsui#    notice, this list of conditions and the following disclaimer in the
121.1Stsutsui#    documentation and/or other materials provided with the distribution.
131.1Stsutsui#
141.1Stsutsui# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
151.1Stsutsui# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161.1Stsutsui# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171.1Stsutsui# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
181.1Stsutsui# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191.1Stsutsui# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201.1Stsutsui# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211.1Stsutsui# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221.1Stsutsui# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231.1Stsutsui# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241.1Stsutsui
251.1Stsutsui#
261.1Stsutsui# Makefile to create a bootable FS image for USB flash or emulators
271.1Stsutsui#
281.1Stsutsui
291.1Stsutsui#
301.1Stsutsui# Required variables:
311.1Stsutsui#	RELEASEDIR
321.1Stsutsui#		Should be defined in nbmake-${MACHINE}
331.1Stsutsui#	IMGBASE
341.1Stsutsui#		Basename of the image
351.1Stsutsui#
361.1Stsutsui# Optional variables:
371.1Stsutsui#	USE_MBR
381.1Stsutsui#		set yes if target disk image requires MBR partition
391.6Stsutsui#		(default: no)
401.23Sjoerg#	USE_GPT
411.23Sjoerg#		set yes if target disk image requires GPT partition
421.23Sjoerg#		(default: no)
431.31Stsutsui#	USE_GPTMBR
441.31Stsutsui#		set yes if target disk image requires GPT partition
451.31Stsutsui#		with hybrid MBR boot code
461.31Stsutsui#		(default: no; valid only if USE_GPT=yes)
471.5Stsutsui#	MBR_BOOTCODE
481.5Stsutsui#		optional MBR bootcode which should be installed by fdisk(8)
491.5Stsutsui#		(default: empty)
501.5Stsutsui#		- specified MBR_BOOTCODE must exist in ${DESTDIR}/usr/mdec
511.5Stsutsui#		- if MBR_BOOTCODE is not specified,
521.5Stsutsui#		  MBR_DEFAULT_BOOTCODE (default: mbr) will be used
531.5Stsutsui#		  if the target ${MACHINE} has the one in /usr/mdec
541.1Stsutsui#	USE_SUNLABEL
551.1Stsutsui#		set yes if target disk image requires Sun's label
561.1Stsutsui#		(default: no)
571.1Stsutsui#	INSTALLBOOT_AFTER_DISKLABEL (untested)
581.1Stsutsui#		set yes if the target ${MACHINE} requires disklabel
591.1Stsutsui#		to run installboot(8), like hp300
601.1Stsutsui#		(default: empty)
611.1Stsutsui#	IMAGEMB
621.1Stsutsui#		target image size in MB
631.1Stsutsui#		(default: 2048)
641.1Stsutsui#	SWAPMB
651.1Stsutsui#		swap size in target image in MB
661.1Stsutsui#		(default: 128)
671.23Sjoerg#	EFIMB
681.23Sjoerg#		EFI partition size in target image in MB
691.23Sjoerg#		(default: 128)
701.24Stsutsui#	FATMB
711.24Stsutsui#		FAT partition size in target image in MB
721.24Stsutsui#		(default: 0)
731.31Stsutsui#	GPTSECTORS
741.31Stsutsui#		Size of a region reserved for the secondary GPT table/entry
751.31Stsutsui#		at the end of the target image in sectors
761.31Stsutsui#		(default: 2048 if USE_GPT=yes, otherwise 0)
771.1Stsutsui#	KERN_SET
781.1Stsutsui#		kernel set name which should be extracted into image
791.1Stsutsui#		(default: kern-GENERIC)
801.1Stsutsui#	SETS
811.1Stsutsui#		binary sets that should be extracted into image
821.29Smaya#		(default: modules base etc comp games gpufw man misc
831.29Smaya#		          rescue tests text xbase xcomp xetc xfont xserver)
841.1Stsutsui#	SETS_DIR
851.1Stsutsui#		directory path where binary sets are stored
861.1Stsutsui#		(default: ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets)
871.1Stsutsui#	IMGFILE_EXTRA
881.1Stsutsui#		list of additional files to be copied into images,
891.1Stsutsui#		containing one or more tuples of the form:
901.1Stsutsui#			FILE	TARGETPATH
911.1Stsutsui#		for installation image etc.
921.1Stsutsui#		(default: empty)
931.1Stsutsui#	IMGDIR_EXTRA
941.1Stsutsui#		list of additional directories to be copied into images,
951.1Stsutsui#		containing one or more tuples of the form:
961.1Stsutsui#			DIR	TARGETPATH
971.1Stsutsui#		for installation image etc.
981.1Stsutsui#		(default: empty)
991.6Stsutsui#		XXX: currently permissions in IMGDIR_EXTRA are not handled
1001.1Stsutsui#	IMGDIR_EXCLUDE
1011.1Stsutsui#		pax(1) options to exclude files which should not copied
1021.1Stsutsui#		into TARGETPATH in IMGDIR_EXTRA
1031.1Stsutsui#		(default: empty)
1041.1Stsutsui#	FSTAB_IN
1051.1Stsutsui#		template file of /etc/fstab
1061.1Stsutsui#		(default: ${DISTRIBDIR}/common/bootimage/fstab.in)
1071.1Stsutsui#	SPEC_IN
1081.1Stsutsui#		default files of spec file for makefs(8)
1091.1Stsutsui#		(default: ${DISTRIBDIR}/common/bootimage/spec.in)
1101.1Stsutsui#	SPEC_EXTRA
1111.1Stsutsui#		additional files of spec file for makefs(8)
1121.1Stsutsui#		(default: empty)
1131.1Stsutsui#	IMGMAKEFSOPTIONS
1141.1Stsutsui#		options passed to makefs(8) to create root file system
1151.1Stsutsui#		(default: -o bsize=16384,fsize=2048,density=8192)
1161.5Stsutsui#	INSTALLBOOTOPTIONS
1171.5Stsutsui#		options passed to installboot(8), e.g., -o console=com0
1181.5Stsutsui#		(default: empty)
1191.1Stsutsui#	PRIMARY_BOOT
1201.1Stsutsui#		primary boot loader that should be installed into
1211.1Stsutsui#		the target image via installboot(8)
1221.1Stsutsui#		(default: empty)
1231.1Stsutsui#	SECONDARY_BOOT
1241.1Stsutsui#		secondary bootloader that should be put into the target image
1251.1Stsutsui#		(default: empty)
1261.1Stsutsui#	SECONDARY_BOOT_ARG
1271.1Stsutsui#		extra arguments that should be passed to installboot(8)
1281.1Stsutsui#		to specify the secondary bootloader
1291.1Stsutsui#		(default: empty)
1301.1Stsutsui#	DISKPROTO_IN
1311.1Stsutsui#		template file of disklabel -R
1321.1Stsutsui#		(default: ${DISTRIBDIR}/common/bootimage/diskproto.in
1331.1Stsutsui#		       or ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in)
1341.23Sjoerg#	EFIBOOT
1351.23Sjoerg#		EFI bootloader
1361.23Sjoerg#		(default: empty)
1371.24Stsutsui#	MBRFAT
1381.24Stsutsui#		MBR partition type value for FAT
1391.24Stsutsui#		(default: 6 ("16-bit FAT, more than 32M"))
1401.24Stsutsui#	FATMAKEFSOPTIONS
1411.24Stsutsui#		options passed to makefs(8) to create a FAT partition
1421.24Stsutsui#		(default: -o fat_type=16)
1431.24Stsutsui#	FATFILES
1441.24Stsutsui#		list of additional files to be copied into FAT partition
1451.24Stsutsui#		(default: empty)
1461.26Sjmcneill#	IMAGEHOSTNAME
1471.26Sjmcneill#		if set, sets the default hostname in rc.conf (default: empty)
1481.1Stsutsui#
1491.1Stsutsui
1501.1Stsutsui.include <bsd.own.mk> 		#
1511.1Stsutsui.include <bsd.endian.mk>	# for TARGET_ENDIANNESS
1521.1Stsutsui
1531.1Stsutsui.include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib"
1541.1Stsutsui
1551.13Schristos.if ${MKREPRO_TIMESTAMP:Uno} != "no"
1561.13SchristosMAKEFS_TIMESTAMP=-T "${MKREPRO_TIMESTAMP}"
1571.23SjoergGPT_TIMESTAMP=-T "${MKREPRO_TIMESTAMP}"
1581.15SchristosPAX_TIMESTAMP=--timestamp "${MKREPRO_TIMESTAMP}"
1591.15Schristos.endif
1601.12Schristos
1611.1Stsutsui.if empty(IMGBASE)
1621.1Stsutsui.BEGIN:
1631.1Stsutsui	@echo "Error: IMGBASE is not set"
1641.1Stsutsui	@false
1651.1Stsutsui.endif
1661.1Stsutsui
1671.1Stsutsui# should be defined elsewhere? 
1681.1StsutsuiCAT?=		cat
1691.1StsutsuiCHMOD?=		chmod
1701.1StsutsuiDD?=		dd
1711.1StsutsuiMKDIR?=		mkdir -p
1721.1StsutsuiRM?=		rm
1731.1Stsutsui
1741.1Stsutsui#
1751.1Stsutsui# common definitions for image
1761.1Stsutsui#
1771.1StsutsuiUSE_MBR?=	no
1781.23SjoergUSE_GPT?=	no
1791.25SjmcneillUSE_GPTMBR?=	no
1801.1StsutsuiUSE_SUNLABEL?=	no
1811.1StsutsuiINSTALLBOOT_AFTER_DISKLABEL?=	no
1821.1Stsutsui
1831.1Stsutsui#
1841.1Stsutsui# size parameters for image
1851.1Stsutsui#
1861.1StsutsuiIMAGEMB?=	2048		# 2048MB
1871.1StsutsuiSWAPMB?=	128		# 128MB
1881.23Sjoerg.if ${USE_GPT} == "no"
1891.23SjoergEFIMB?=		0
1901.23SjoergGPTSECTORS=	0
1911.23Sjoerg.else
1921.23SjoergEFIMB?=		128		# 128MB
1931.23SjoergGPTSECTORS=	2048
1941.23Sjoerg.endif
1951.24StsutsuiFATMB?=		0
1961.1Stsutsui
1971.3Stsutsui# XXX: SWAPMB could be zero and expr(1) returns exit status 1 in that case
1981.1StsutsuiIMAGESECTORS!=	expr ${IMAGEMB} \* 1024 \* 1024 / 512
1991.3StsutsuiSWAPSECTORS!=	expr ${SWAPMB} \* 1024 \* 1024 / 512 || true
2001.23SjoergEFISECTORS!=	expr ${EFIMB} \* 1024 \* 1024 / 512 || true
2011.24StsutsuiFATSECTORS!=	expr ${FATMB} \* 1024 \* 1024 / 512 || true
2021.1Stsutsui
2031.23Sjoerg.if ${USE_MBR} == "no" && ${USE_GPT} == "no"
2041.1StsutsuiLABELSECTORS?=	0
2051.1Stsutsui.else
2061.1Stsutsui#LABELSECTORS?=	63	# historical
2071.1Stsutsui#LABELSECTORS?=	32	# 16KB aligned
2081.1StsutsuiLABELSECTORS?=	2048	# 1MB aligned for modern flash devices
2091.1Stsutsui.endif
2101.1Stsutsui
2111.23SjoergFSSECTORS!=	expr ${IMAGESECTORS} - ${SWAPSECTORS} - ${LABELSECTORS} \
2121.30Smrg		    - ${EFISECTORS} - ${GPTSECTORS} - ${FATSECTORS} || test $$? -eq 1 || exit 1
2131.30SmrgFSSIZE!=	expr ${FSSECTORS} \* 512 || test $$? -eq 1 || exit 1
2141.1Stsutsui
2151.1Stsutsui# parameters for disklabel and MBR
2161.1StsutsuiHEADS=		64
2171.1StsutsuiSECTORS=	32
2181.1StsutsuiCYLINDERS!=	expr ${IMAGESECTORS} / \( ${HEADS} \* ${SECTORS} \)
2191.1StsutsuiSECPERCYLINDERS!= expr ${HEADS} \* ${SECTORS}
2201.1StsutsuiMBRHEADS=	255
2211.1StsutsuiMBRSECTORS=	63
2221.1StsutsuiMBRCYLINDERS!=	expr ${IMAGESECTORS} / \( ${MBRHEADS} \* ${MBRSECTORS} \)
2231.1StsutsuiMBRNETBSD=	169
2241.24StsutsuiMBRFAT?=	6	# 16-bit FAT, more than 32M
2251.1Stsutsui
2261.24StsutsuiBSDPARTSECTORS!= expr ${IMAGESECTORS} - ${LABELSECTORS} \
2271.24Stsutsui		    - ${EFISECTORS} - ${GPTSECTORS} - ${FATSECTORS}
2281.30SmrgFSOFFSET!=	expr ${LABELSECTORS} + ${EFISECTORS} + ${FATSECTORS} || test $$? -eq 1 || exit 1
2291.24StsutsuiSWAPOFFSET!=	expr ${LABELSECTORS} + ${FSSECTORS} \
2301.30Smrg		    + ${EFISECTORS} + ${FATSECTORS} || test $$? -eq 1 || exit 1
2311.24StsutsuiFATOFFSET=	${LABELSECTORS}
2321.1Stsutsui
2331.1Stsutsui# parameters for sunlabel
2341.1StsutsuiFSCYLINDERS!=	expr ${FSSECTORS} / \( ${HEADS} \* ${SECTORS} \)
2351.3StsutsuiSWAPCYLINDERS!=	expr ${SWAPSECTORS} / \( ${HEADS} \* ${SECTORS} \) || true
2361.1Stsutsui
2371.23Sjoerg.if ${USE_GPT} != "no"
2381.23SjoergBOOTDISK_UUID=`${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} show -i 2 | ${TOOL_AWK} '/^GUID/ {print $$2}'`
2391.23SjoergSWAPDISK_UUID=`${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} show -i 3 | ${TOOL_AWK} '/^GUID/ {print $$2}'`
2401.23Sjoerg.endif
2411.1Stsutsui
2421.1Stsutsui#
2431.1Stsutsui# definitions to create root fs
2441.1Stsutsui#
2451.29SmayaSETS_DEFAULT=	modules base etc comp games gpufw man misc rescue tests text
2461.1Stsutsui.if ${MKX11} != "no"
2471.1StsutsuiSETS_DEFAULT+=	xbase xcomp xetc xfont xserver
2481.1Stsutsui.endif
2491.1Stsutsui
2501.1StsutsuiKERN_SET?=	kern-GENERIC
2511.1StsutsuiSETS?=		${SETS_DEFAULT}
2521.1StsutsuiIMG_SETS=	${KERN_SET} ${SETS}
2531.1StsutsuiSETS_DIR?=	${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
2541.1Stsutsui
2551.1StsutsuiFSTAB_IN?=	${DISTRIBDIR}/common/bootimage/fstab.in
2561.1StsutsuiSPEC_IN?=	${DISTRIBDIR}/common/bootimage/spec.in
2571.1Stsutsui
2581.1StsutsuiIMGMAKEFSOPTIONS?=	-o bsize=16384,fsize=2048,density=8192
2591.24StsutsuiFATMAKEFSOPTIONS?=	-o fat_type=16
2601.1Stsutsui
2611.1StsutsuiWORKDIR?=       work
2621.1StsutsuiWORKSPEC?=      work.spec
2631.1StsutsuiWORKFSTAB?=	work.fstab
2641.1StsutsuiWORKRCCONF?=	work.rc.conf
2651.1StsutsuiWORKFS?=        work.rootfs
2661.1StsutsuiTARGETFS?=      imgroot.fs
2671.19SaymericCOMPRESS_PROGRAM=${"${USE_XZ_SETS:Uno}"!="no":?${TOOL_XZ}:${TOOL_GZIP}}
2681.18SmartinTAR_SUFF=	${"${USE_XZ_SETS:Uno}"!="no":?tar.xz:tgz}
2691.1Stsutsui
2701.1StsutsuiCLEANFILES+=	${WORKSPEC} ${WORKFSTAB} ${WORKRCCONF} ${WORKFS}
2711.1StsutsuiCLEANFILES+=	${TARGETFS}
2721.1Stsutsui
2731.1Stsutsui#
2741.23Sjoerg# definitions to create image
2751.23Sjoerg#
2761.23Sjoerg.if ${SWAPSECTORS} == 0
2771.23Sjoerg.  if ${USE_MBR} != "no"
2781.23SjoergDISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.noswap.mbr.in
2791.23Sjoerg.  else
2801.23SjoergDISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.noswap.in
2811.23Sjoerg.  endif
2821.23Sjoerg.else
2831.23Sjoerg.  if ${USE_MBR} != "no"
2841.23SjoergDISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.mbr.in
2851.23Sjoerg.  else
2861.23SjoergDISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.in
2871.23Sjoerg.  endif
2881.23Sjoerg.endif
2891.23Sjoerg
2901.23SjoergMBR_DEFAULT_BOOTCODE?=	mbr
2911.23Sjoerg
2921.23SjoergWORKMBR?=	work.mbr
2931.23SjoergWORKMBRTRUNC?=	work.mbr.truncated
2941.23SjoergWORKSWAP?=	work.swap
2951.23SjoergWORKEFI?=	work.efi
2961.23SjoergWORKGPT?=	work.gpt
2971.23SjoergWORKLABEL?=	work.diskproto
2981.23SjoergWORKIMG?=	work.img
2991.23SjoergEFIWORKDIR?=	work.efidir
3001.24StsutsuiWORKFAT?=	work.fat
3011.24StsutsuiWORKFATDIR?=	work.fatdir
3021.23Sjoerg
3031.23Sjoerg.if ${USE_GPT} != "no"
3041.23Sjoerg${WORKFSTAB}: ${WORKMBR}
3051.27Stsutsui
3061.27Stsutsui.BEGIN:
3071.27Stsutsui	@if [ -f ${WORKMBR} ]; then			 		\
3081.27Stsutsui		st_size=`${TOOL_STAT} -f %z ${WORKMBR}`;		\
3091.27Stsutsui		if [ "$$st_size" -ne "$$((${IMAGESECTORS} * 512))" ]; then \
3101.27Stsutsui			echo Removing stale ${WORKMBR} ...;		\
3111.27Stsutsui			rm -f ${WORKMBR};				\
3121.27Stsutsui		fi							\
3131.27Stsutsui	fi
3141.23Sjoerg.endif
3151.23Sjoerg
3161.23Sjoerg${WORKFSTAB}:
3171.23Sjoerg	@echo Preparing /etc/fstab ...
3181.23Sjoerg.if ${USE_GPT} != "no"
3191.23Sjoerg.  if ${SWAPSECTORS} != 0
3201.23Sjoerg	${TOOL_SED}							\
3211.23Sjoerg	    -e "s/ROOT.a/NAME=${BOOTDISK_UUID}/"			\
3221.23Sjoerg	    -e "s/ROOT.b/NAME=${SWAPDISK_UUID}/"			\
3231.23Sjoerg	    < ${FSTAB_IN} > ${WORKFSTAB}
3241.23Sjoerg.  else
3251.23Sjoerg	${TOOL_SED}							\
3261.23Sjoerg	    -e "s/ROOT.a/NAME=${BOOTDISK_UUID}/"			\
3271.23Sjoerg	    -e "/ROOT.b/d"						\
3281.23Sjoerg	    < ${FSTAB_IN} > ${WORKFSTAB}
3291.23Sjoerg.  endif
3301.23Sjoerg.else
3311.23Sjoerg.  if ${SWAPSECTORS} != 0
3321.23Sjoerg	${TOOL_SED} < ${FSTAB_IN} > ${WORKFSTAB}
3331.23Sjoerg.  else
3341.23Sjoerg	${TOOL_SED}							\
3351.23Sjoerg	    -e "/ROOT.b/d"						\
3361.23Sjoerg	    < ${FSTAB_IN} > ${WORKFSTAB}
3371.23Sjoerg.  endif
3381.23Sjoerg.endif
3391.23Sjoerg
3401.23Sjoerg#
3411.1Stsutsui# create root file system for the image
3421.1Stsutsui#
3431.23Sjoerg${TARGETFS}: prepare_md_post ${WORKFSTAB}
3441.1Stsutsui	@if [ ! -d ${RELEASEDIR}/${RELEASEMACHINEDIR} ]; then 		\
3451.1Stsutsui		echo "Missing ${RELEASEDIR}/${RELEASEMACHINEDIR}, aborting"; \
3461.1Stsutsui		false; 							\
3471.1Stsutsui	fi;
3481.14Schristos	@${MKDIR} ${MKDIRPERM} ${WORKDIR}
3491.1Stsutsui.for set in ${IMG_SETS}
3501.18Smartin	@if [ ! -f ${SETS_DIR}/${set}.${TAR_SUFF} ]; then 		\
3511.18Smartin		echo "Missing ${SETS_DIR}/${set}.${TAR_SUFF}, aborting";\
3521.1Stsutsui		false; 							\
3531.1Stsutsui	fi
3541.18Smartin	@echo Extracting ${set}.${TAR_SUFF} ...
3551.19Saymeric	@(cd ${WORKDIR}; ${TOOL_PAX} ${PAX_TIMESTAMP} -rn \
3561.19Saymeric		--use-compress-program=${COMPRESS_PROGRAM:Q} \
3571.19Saymeric		-f ${SETS_DIR}/${set}.${TAR_SUFF} .)
3581.1Stsutsui.endfor
3591.1Stsutsui.if defined(SECONDARY_BOOT)
3601.1Stsutsui	@echo Copying secondary boot...
3611.21Stsutsui	${INSTALL} ${COPY} -m 0444 ${WORKDIR}/usr/mdec/${SECONDARY_BOOT} ${WORKDIR}
3621.1Stsutsui.endif
3631.14Schristos	${INSTALL} ${COPY} -m 0644 ${WORKFSTAB} ${WORKDIR}/etc/fstab
3641.1Stsutsui	@echo Setting rc_configured=YES in /etc/rc.conf ...
3651.1Stsutsui	${TOOL_SED} "s/rc_configured=NO/rc_configured=YES/"		\
3661.1Stsutsui	    < ${WORKDIR}/etc/rc.conf > ${WORKRCCONF}
3671.26Sjmcneill.if defined(IMAGEHOSTNAME)
3681.26Sjmcneill	@echo Setting hostname=${IMAGEHOSTNAME} in /etc/rc.conf ...
3691.26Sjmcneill	echo "hostname=${IMAGEHOSTNAME}" >> ${WORKRCCONF}
3701.26Sjmcneill.endif
3711.26Sjmcneill.if ${SWAPSECTORS} == 0
3721.26Sjmcneill	@echo Setting no_swap=YES in /etc/rc.conf ...
3731.26Sjmcneill	echo "no_swap=YES" >> ${WORKRCCONF}
3741.26Sjmcneill	@echo Setting savecore=NO in /etc/rc.conf ...
3751.26Sjmcneill	echo "savecore=NO" >> ${WORKRCCONF}
3761.26Sjmcneill.endif
3771.28Sjmcneill.if defined(RC_CONF_EXTRA)
3781.28Sjmcneill	@echo Adding contents of ${RC_CONF_EXTRA} to /etc/rc.conf ...
3791.28Sjmcneill	cat ${RC_CONF_EXTRA} >> ${WORKRCCONF}
3801.28Sjmcneill.endif
3811.14Schristos	${INSTALL} ${COPY} -m 0644 ${WORKRCCONF} ${WORKDIR}/etc/rc.conf
3821.1Stsutsui.if defined(IMGDIR_EXTRA)
3831.1Stsutsui	@echo Copying extra dirs...
3841.1Stsutsui.for _SRCDIR _TARGET in ${IMGDIR_EXTRA}
3851.1Stsutsui	@if [ ! -d ${_SRCDIR} ]; then 					\
3861.1Stsutsui		echo "${_SRCDIR} is not directory, aborting";		\
3871.1Stsutsui		false; 							\
3881.1Stsutsui	fi
3891.14Schristos	${MKDIR} ${MKDIRPERM} ${WORKDIR}/${_TARGET}
3901.1Stsutsui	(cd ${_SRCDIR} ;						\
3911.21Stsutsui	    ${TOOL_PAX} ${PAX_TIMESTAMP} -rw -pe -v			\
3921.1Stsutsui	    ${IMGDIR_EXCLUDE}						\
3931.1Stsutsui	    . ${.OBJDIR}/${WORKDIR}/${_TARGET} )
3941.1Stsutsui.endfor
3951.1Stsutsui.endif
3961.1Stsutsui.if defined(IMGFILE_EXTRA)
3971.1Stsutsui	@echo Copying extra files...
3981.1Stsutsui.for _SRC _TARGET in ${IMGFILE_EXTRA}
3991.1Stsutsui	@if [ ! -f ${_SRC} ]; then 					\
4001.1Stsutsui		echo "${_SRC} in IMGFILE_EXTRA not found, aborting";	\
4011.1Stsutsui		false; 							\
4021.1Stsutsui	fi
4031.1Stsutsui	@if [ -f ${_SRC} ]; then 					\
4041.21Stsutsui		echo ${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
4051.21Stsutsui		${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
4061.1Stsutsui	fi
4071.1Stsutsui.endfor
4081.1Stsutsui.endif
4091.1Stsutsui	@echo Preparing spec files for makefs...
4101.1Stsutsui	${RM} -f ${WORKSPEC}
4111.1Stsutsui	cat ${WORKDIR}/etc/mtree/* |					\
4121.1Stsutsui	    ${TOOL_SED} -e 's/ size=[0-9]*//' > ${WORKSPEC}
4131.10Ssnj	${HOST_SH} ${WORKDIR}/dev/MAKEDEV -s all ipty |			\
4141.1Stsutsui	    ${TOOL_SED} -e '/^\. type=dir/d' -e 's,^\.,./dev,' >> ${WORKSPEC}
4151.1Stsutsui	cat ${SPEC_IN} >> ${WORKSPEC}
4161.1Stsutsui.if defined(SECONDARY_BOOT)
4171.1Stsutsui	echo "./${SECONDARY_BOOT} type=file uname=root gname=wheel mode=0444" \
4181.1Stsutsui	    >> ${WORKSPEC}
4191.1Stsutsui.endif
4201.1Stsutsui.if defined(SPEC_EXTRA)
4211.1Stsutsui	cat ${SPEC_EXTRA} >> ${WORKSPEC}
4221.1Stsutsui.endif
4231.4Sriz	@echo Creating rootfs...
4241.1Stsutsui	# XXX /var/spool/ftp/hidden is unreadable
4251.1Stsutsui	${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden
4261.1Stsutsui	${TOOL_MAKEFS} -M ${FSSIZE} -m ${FSSIZE}			\
4271.1Stsutsui	    -B ${TARGET_ENDIANNESS}					\
4281.1Stsutsui	    -F ${WORKSPEC} -N ${WORKDIR}/etc				\
4291.13Schristos	    ${MAKEFS_TIMESTAMP}						\
4301.1Stsutsui	    ${IMGMAKEFSOPTIONS}						\
4311.1Stsutsui	    ${WORKFS} ${WORKDIR}
4321.1Stsutsui.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} == "no"
4331.5Stsutsui	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKFS} \
4341.1Stsutsui	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT} ${SECONDARY_BOOT_ARG}
4351.1Stsutsui.endif
4361.1Stsutsui	@echo done.
4371.1Stsutsui	mv ${WORKFS} ${.TARGET}
4381.1Stsutsui
4391.23SjoergTARGET_BLOCKS=
4401.23Sjoerg.if ${USE_MBR} != "no" || ${USE_GPT} != "no"
4411.23SjoergTARGET_BLOCKS+=	${WORKMBRTRUNC}
4421.23Sjoerg.endif
4431.23Sjoerg.if ${USE_GPT} != "no"
4441.23SjoergTARGET_BLOCKS+=	${WORKEFI}
4451.23Sjoerg.endif
4461.24Stsutsui.if ${FATSECTORS} != 0
4471.24StsutsuiTARGET_BLOCKS+=	${WORKFAT}
4481.24Stsutsui.endif
4491.23SjoergTARGET_BLOCKS+=	 ${TARGETFS}
4501.23Sjoerg.if ${SWAPSECTORS} != 0
4511.23SjoergTARGET_BLOCKS+=	${WORKSWAP}
4521.23Sjoerg.endif
4531.23Sjoerg.if ${USE_GPT} != "no"
4541.23SjoergTARGET_BLOCKS+=	${WORKGPT}
4551.1Stsutsui.endif
4561.1Stsutsui
4571.23SjoergCLEANFILES+=	${WORKMBR} ${WORKMBRTRUNC} ${WORKSWAP}
4581.23SjoergCLEANFILES+=	${WORKEFI} ${WORKGPT}
4591.24StsutsuiCLEANFILES+=	${WORKFAT}
4601.23SjoergCLEANFILES+=	${WORKLABEL}.tmp ${WORKLABEL}
4611.23SjoergCLEANFILES+=	${WORKIMG} ${IMGBASE}.img
4621.5Stsutsui
4631.23Sjoerg${WORKSWAP}:
4641.23Sjoerg.if ${SWAPSECTORS} == 0
4651.23Sjoerg	printf "" > ${WORKSWAP}
4661.23Sjoerg.else
4671.23Sjoerg	${DD} if=/dev/zero of=${WORKSWAP} seek=$$((${SWAPSECTORS} - 1)) count=1
4681.23Sjoerg.endif
4691.1Stsutsui
4701.23Sjoerg${WORKMBRTRUNC}: ${WORKMBR}
4711.23Sjoerg	${DD} if=${WORKMBR} count=${LABELSECTORS} of=${WORKMBRTRUNC}
4721.1Stsutsui
4731.1Stsutsui${WORKLABEL}:
4741.1Stsutsui	${TOOL_SED}							\
4751.1Stsutsui	    -e "s/@@SECTORS@@/${SECTORS}/"				\
4761.1Stsutsui	    -e "s/@@HEADS@@/${HEADS}/"					\
4771.1Stsutsui	    -e "s/@@SECPERCYLINDERS@@/${SECPERCYLINDERS}/"		\
4781.1Stsutsui	    -e "s/@@CYLINDERS@@/${CYLINDERS}/"				\
4791.1Stsutsui	    -e "s/@@IMAGESECTORS@@/${IMAGESECTORS}/"			\
4801.1Stsutsui	    -e "s/@@FSSECTORS@@/${FSSECTORS}/"				\
4811.1Stsutsui	    -e "s/@@FSOFFSET@@/${FSOFFSET}/"				\
4821.1Stsutsui	    -e "s/@@SWAPSECTORS@@/${SWAPSECTORS}/"			\
4831.1Stsutsui	    -e "s/@@SWAPOFFSET@@/${SWAPOFFSET}/"			\
4841.1Stsutsui	    -e "s/@@BSDPARTSECTORS@@/${BSDPARTSECTORS}/"		\
4851.24Stsutsui	    -e "s/@@FATSECTORS@@/${FATSECTORS}/"			\
4861.24Stsutsui	    -e "s/@@FATOFFSET@@/${FATOFFSET}/"				\
4871.1Stsutsui	    < ${DISKPROTO_IN} > ${WORKLABEL}.tmp
4881.1Stsutsui	mv ${WORKLABEL}.tmp ${WORKLABEL}
4891.1Stsutsui
4901.23Sjoerg${WORKEFI}: ${TARGETFS}
4911.23Sjoerg	@echo create EFI system partition...
4921.23Sjoerg	@${MKDIR} ${MKDIRPERM} ${EFIWORKDIR}/EFI/boot
4931.23Sjoerg.for f in ${EFIBOOT}
4941.23Sjoerg	${INSTALL} ${COPY} -m 0644 ${f} ${EFIWORKDIR}/EFI/boot/`basename ${f}`
4951.23Sjoerg.endfor
4961.23Sjoerg	${RM} -f ${WORKEFI}
4971.23Sjoerg	${TOOL_MAKEFS} -M ${EFIMB}m -m ${EFIMB}m			\
4981.23Sjoerg	    -B ${TARGET_ENDIANNESS}					\
4991.23Sjoerg	    ${MAKEFS_TIMESTAMP}						\
5001.23Sjoerg	    -t msdos -o F=32,c=1					\
5011.23Sjoerg	    ${WORKEFI} ${EFIWORKDIR}
5021.23Sjoerg
5031.24Stsutsui${WORKFAT}: ${TARGETFS} ${FATFILES}
5041.24Stsutsui	@echo create FAT partition for bootstrap files...
5051.24Stsutsui	@${MKDIR} ${MKDIRPERM} ${WORKFATDIR}
5061.24Stsutsui.if defined(FATFILES)
5071.24Stsutsui	@echo Copying files for FAT partition...
5081.24Stsutsui.for f in ${FATFILES}
5091.24Stsutsui	@if [ ! -f ${f} ]; then 					\
5101.24Stsutsui		echo "${f} in FATFILES not found, aborting";		\
5111.24Stsutsui		false; 							\
5121.24Stsutsui	fi
5131.24Stsutsui	${INSTALL} ${COPY} -m 0644 ${f} ${WORKFATDIR}
5141.24Stsutsui.endfor
5151.24Stsutsui.endif
5161.24Stsutsui	${RM} -f ${WORKFAT}
5171.24Stsutsui	${TOOL_MAKEFS} -M ${FATMB}m -m ${FATMB}m			\
5181.24Stsutsui	    ${MAKEFS_TIMESTAMP}						\
5191.24Stsutsui	    -t msdos							\
5201.24Stsutsui	    ${FATMAKEFSOPTIONS}						\
5211.24Stsutsui	    ${WORKFAT} ${WORKFATDIR}
5221.24Stsutsui
5231.23Sjoerg${WORKMBR}:
5241.23Sjoerg.if ${USE_GPT} != "no"
5251.23Sjoerg	@echo creating GPT header and partition entries...
5261.23Sjoerg	${RM} -f ${WORKMBR}
5271.23Sjoerg	${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
5281.23Sjoerg	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} create
5291.23Sjoerg	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${EFISECTORS} -t efi -l "EFI system"
5301.23Sjoerg	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${FSSECTORS} -t ffs
5311.23Sjoerg.  if ${SWAPSECTORS} != 0
5321.23Sjoerg	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${SWAPSECTORS} -t swap
5331.23Sjoerg.  endif
5341.23Sjoerg.elif ${USE_MBR} != "no"
5351.1Stsutsui	@echo creating MBR labels...
5361.1Stsutsui	${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
5371.24Stsutsui.  if ${FATSECTORS} != 0
5381.24Stsutsui	# create BSD partition
5391.24Stsutsui	${TOOL_FDISK} -f -i -u \
5401.24Stsutsui	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
5411.24Stsutsui	    -1 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS}		\
5421.24Stsutsui	    -F ${WORKMBR}
5431.24Stsutsui	# create FAT partition
5441.24Stsutsui	${TOOL_FDISK} -f -u \
5451.24Stsutsui	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
5461.24Stsutsui	    -0 -s ${MBRFAT}/${FATOFFSET}/${FATSECTORS}			\
5471.24Stsutsui	    -F ${WORKMBR}
5481.24Stsutsui.  else
5491.24Stsutsui	# create BSD partition
5501.5Stsutsui	${TOOL_FDISK} -f -i -u \
5511.1Stsutsui	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
5521.1Stsutsui	    -0 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS}		\
5531.1Stsutsui	    -F ${WORKMBR}
5541.24Stsutsui.  endif
5551.23Sjoerg.  if empty(MBR_BOOTCODE)
5561.5Stsutsui	@if [ -f ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} ]; then 	\
5571.5Stsutsui		echo ${TOOL_FDISK} -f					\
5581.5Stsutsui		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
5591.5Stsutsui		    -F ${WORKMBR};					\
5601.5Stsutsui		${TOOL_FDISK} -f					\
5611.5Stsutsui		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
5621.5Stsutsui		    -F ${WORKMBR};					\
5631.5Stsutsui	fi
5641.23Sjoerg.  else
5651.5Stsutsui	@if [ ! -f ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} ]; then 		\
5661.5Stsutsui		echo "${MBR_BOOTCODE} is not found in DESTDIR/usr/mdec, aborting"; \
5671.5Stsutsui		false; 							\
5681.5Stsutsui	fi
5691.5Stsutsui	${TOOL_FDISK} -f -i -c ${WORKDIR}/usr/mdec/${MBR_BOOTCODE}	\
5701.5Stsutsui	    -F ${WORKMBR}
5711.23Sjoerg.  endif
5721.5Stsutsui.endif
5731.23Sjoerg
5741.23Sjoerg${WORKGPT}: ${WORKMBR}
5751.23Sjoerg	@echo create GPT image...
5761.23Sjoerg	${DD} if=${WORKMBR} of=${WORKGPT} skip=$$((${IMAGESECTORS} - 2048)) \
5771.23Sjoerg	    count=2048
5781.23Sjoerg
5791.23Sjoerg.if ${USE_GPT} == "no"
5801.23Sjoerg${IMGBASE}.img:	${WORKLABEL}
5811.1Stsutsui.endif
5821.23Sjoerg
5831.23Sjoerg${IMGBASE}.img:	${TARGET_BLOCKS}
5841.23Sjoerg	${CAT} ${TARGET_BLOCKS} > ${WORKIMG}
5851.23Sjoerg.if ${USE_GPT} != "no"
5861.25Sjmcneill.if ${USE_GPTMBR} != "no"
5871.23Sjoerg	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKIMG} biosboot -i 2		\
5881.23Sjoerg	    -c ${.OBJDIR}/${WORKDIR}/usr/mdec/gptmbr.bin
5891.25Sjmcneill.endif
5901.23Sjoerg	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKIMG} set -a bootme -i 2
5911.23Sjoerg.endif	# USE_GPT != "no"
5921.1Stsutsui.if ${USE_SUNLABEL} != "no"
5931.1Stsutsui	@echo Creating sun disklabel...
5941.1Stsutsui	printf 'V ncyl %d\nV nhead %d\nV nsect %d\na %d %d/0/0\nb %d %d/0/0\nW\n' \
5951.1Stsutsui	    ${CYLINDERS} ${HEADS} ${SECTORS} \
5961.1Stsutsui	    ${FSOFFSET} ${FSCYLINDERS} ${FSCYLINDERS} ${SWAPCYLINDERS} | \
5971.1Stsutsui	    ${TOOL_SUNLABEL} -nq ${WORKIMG}
5981.1Stsutsui.endif
5991.23Sjoerg.if ${USE_GPT} == "no"
6001.1Stsutsui	${TOOL_DISKLABEL} -R -F ${WORKIMG} ${WORKLABEL}
6011.1Stsutsui.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} != "no"
6021.11Sriastrad	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKIMG} \
6031.1Stsutsui	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT}
6041.1Stsutsui.endif
6051.23Sjoerg.endif
6061.1Stsutsui	mv ${WORKIMG} ${.TARGET}
6071.1Stsutsui
6081.1StsutsuiCLEANFILES+=	${IMGBASE}.img.gz ${IMGBASE}.img.gz.tmp
6091.1Stsutsui
6101.1Stsutsui${IMGBASE}.img.gz:	${IMGBASE}.img
6111.9Sapb	${TOOL_GZIP_N} -9c ${IMGBASE}.img > ${.TARGET}.tmp
6121.1Stsutsui	mv ${.TARGET}.tmp ${.TARGET}
6131.1Stsutsui
6141.1Stsutsuiclean:
6151.1Stsutsui	@if [ -d ${WORKDIR}/var/spool/ftp/hidden ]; then 		\
6161.1Stsutsui		${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden;		\
6171.1Stsutsui	fi	# XXX
6181.1Stsutsui	${RM} -fr ${WORKDIR}
6191.23Sjoerg	${RM} -fr ${EFIWORKDIR}
6201.24Stsutsui	${RM} -fr ${WORKFATDIR}
6211.1Stsutsui
6221.1Stsutsuiprepare_md_post: .PHONY
6231.1Stsutsuiimage_md_post: .PHONY
6241.1Stsutsuiimage_md_pre: .PHONY
6251.1Stsutsui
6261.1Stsutsui.include <bsd.prog.mk>
627