Home | History | Annotate | Line # | Download | only in bootimage
Makefile.bootimage revision 1.30.2.1
      1 #	$NetBSD: Makefile.bootimage,v 1.30.2.1 2025/02/20 19:13:29 martin Exp $
      2 #
      3 # Copyright (c) 2009, 2010, 2011 Izumi Tsutsui.  All rights reserved.
      4 #
      5 # Redistribution and use in source and binary forms, with or without
      6 # modification, are permitted provided that the following conditions
      7 # are met:
      8 # 1. Redistributions of source code must retain the above copyright
      9 #    notice, this list of conditions and the following disclaimer.
     10 # 2. Redistributions in binary form must reproduce the above copyright
     11 #    notice, this list of conditions and the following disclaimer in the
     12 #    documentation and/or other materials provided with the distribution.
     13 #
     14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     15 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     16 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     17 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     18 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     19 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     20 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     21 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24 
     25 #
     26 # Makefile to create a bootable FS image for USB flash or emulators
     27 #
     28 
     29 #
     30 # Required variables:
     31 #	RELEASEDIR
     32 #		Should be defined in nbmake-${MACHINE}
     33 #	IMGBASE
     34 #		Basename of the image
     35 #
     36 # Optional variables:
     37 #	USE_MBR
     38 #		set yes if target disk image requires MBR partition
     39 #		(default: no)
     40 #	USE_GPT
     41 #		set yes if target disk image requires GPT partition
     42 #		(default: no)
     43 #	MBR_BOOTCODE
     44 #		optional MBR bootcode which should be installed by fdisk(8)
     45 #		(default: empty)
     46 #		- specified MBR_BOOTCODE must exist in ${DESTDIR}/usr/mdec
     47 #		- if MBR_BOOTCODE is not specified,
     48 #		  MBR_DEFAULT_BOOTCODE (default: mbr) will be used
     49 #		  if the target ${MACHINE} has the one in /usr/mdec
     50 #	USE_SUNLABEL
     51 #		set yes if target disk image requires Sun's label
     52 #		(default: no)
     53 #	INSTALLBOOT_AFTER_DISKLABEL
     54 #		set yes if the target ${MACHINE} requires disklabel
     55 #		to run installboot(8), like hp300 and vax
     56 #		(default: empty)
     57 #	IMAGEMB
     58 #		target image size in MB
     59 #		(default: 2048)
     60 #	SWAPMB
     61 #		swap size in target image in MB
     62 #		(default: 128)
     63 #	EFIMB
     64 #		EFI partition size in target image in MB
     65 #		(default: 128)
     66 #	FATMB
     67 #		FAT partition size in target image in MB
     68 #		(default: 0)
     69 #	KERN_SET
     70 #		kernel set name which should be extracted into image
     71 #		(default: kern-GENERIC)
     72 #	SETS
     73 #		binary sets that should be extracted into image
     74 #		(default: modules base etc comp games gpufw man misc
     75 #		          rescue tests text xbase xcomp xetc xfont xserver)
     76 #	SETS_DIR
     77 #		directory path where binary sets are stored
     78 #		(default: ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets)
     79 #	IMGFILE_EXTRA
     80 #		list of additional files to be copied into images,
     81 #		containing one or more tuples of the form:
     82 #			FILE	TARGETPATH
     83 #		for installation image etc.
     84 #		(default: empty)
     85 #	IMGDIR_EXTRA
     86 #		list of additional directories to be copied into images,
     87 #		containing one or more tuples of the form:
     88 #			DIR	TARGETPATH
     89 #		for installation image etc.
     90 #		(default: empty)
     91 #		XXX: currently permissions in IMGDIR_EXTRA are not handled
     92 #	IMGDIR_EXCLUDE
     93 #		pax(1) options to exclude files which should not copied
     94 #		into TARGETPATH in IMGDIR_EXTRA
     95 #		(default: empty)
     96 #	FSTAB_IN
     97 #		template file of /etc/fstab
     98 #		(default: ${DISTRIBDIR}/common/bootimage/fstab.in)
     99 #	SPEC_IN
    100 #		default files of spec file for makefs(8)
    101 #		(default: ${DISTRIBDIR}/common/bootimage/spec.in)
    102 #	SPEC_EXTRA
    103 #		additional files of spec file for makefs(8)
    104 #		(default: empty)
    105 #	IMGMAKEFSOPTIONS
    106 #		options passed to makefs(8) to create root file system
    107 #		(default: -o bsize=16384,fsize=2048,density=8192)
    108 #	INSTALLBOOTOPTIONS
    109 #		options passed to installboot(8), e.g., -o console=com0
    110 #		(default: empty)
    111 #	PRIMARY_BOOT
    112 #		primary boot loader that should be installed into
    113 #		the target image via installboot(8)
    114 #		(default: empty)
    115 #	SECONDARY_BOOT
    116 #		secondary bootloader that should be put into the target image
    117 #		(default: empty)
    118 #	SECONDARY_BOOT_ARG
    119 #		extra arguments that should be passed to installboot(8)
    120 #		to specify the secondary bootloader
    121 #		(default: empty)
    122 #	DISKPROTO_IN
    123 #		template file of disklabel -R
    124 #		(default: ${DISTRIBDIR}/common/bootimage/diskproto.in
    125 #		       or ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in)
    126 #	EFIBOOT
    127 #		EFI bootloader
    128 #		(default: empty)
    129 #	MBRFAT
    130 #		MBR partition type value for FAT
    131 #		(default: 6 ("16-bit FAT, more than 32M"))
    132 #	FATMAKEFSOPTIONS
    133 #		options passed to makefs(8) to create a FAT partition
    134 #		(default: -o fat_type=16)
    135 #	FATFILES
    136 #		list of additional files to be copied into FAT partition
    137 #		(default: empty)
    138 #	IMAGEHOSTNAME
    139 #		if set, sets the default hostname in rc.conf (default: empty)
    140 #
    141 
    142 .include <bsd.own.mk> 		#
    143 .include <bsd.endian.mk>	# for TARGET_ENDIANNESS
    144 
    145 .include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib"
    146 
    147 .if ${MKREPRO_TIMESTAMP:Uno} != "no"
    148 MAKEFS_TIMESTAMP=-T "${MKREPRO_TIMESTAMP}"
    149 GPT_TIMESTAMP=-T "${MKREPRO_TIMESTAMP}"
    150 PAX_TIMESTAMP=--timestamp "${MKREPRO_TIMESTAMP}"
    151 .endif
    152 
    153 .if empty(IMGBASE)
    154 .BEGIN:
    155 	@echo "Error: IMGBASE is not set"
    156 	@false
    157 .endif
    158 
    159 # should be defined elsewhere? 
    160 CAT?=		cat
    161 CHMOD?=		chmod
    162 DD?=		dd
    163 MKDIR?=		mkdir -p
    164 RM?=		rm
    165 
    166 #
    167 # common definitions for image
    168 #
    169 USE_MBR?=	no
    170 USE_GPT?=	no
    171 USE_GPTMBR?=	no
    172 USE_SUNLABEL?=	no
    173 INSTALLBOOT_AFTER_DISKLABEL?=	no
    174 
    175 #
    176 # size parameters for image
    177 #
    178 IMAGEMB?=	2048		# 2048MB
    179 SWAPMB?=	128		# 128MB
    180 .if ${USE_GPT} == "no"
    181 EFIMB?=		0
    182 GPTSECTORS=	0
    183 .else
    184 EFIMB?=		128		# 128MB
    185 GPTSECTORS=	2048
    186 .endif
    187 FATMB?=		0
    188 
    189 # XXX: SWAPMB could be zero and expr(1) returns exit status 1 in that case
    190 IMAGESECTORS!=	expr ${IMAGEMB} \* 1024 \* 1024 / 512
    191 SWAPSECTORS!=	expr ${SWAPMB} \* 1024 \* 1024 / 512 || true
    192 EFISECTORS!=	expr ${EFIMB} \* 1024 \* 1024 / 512 || true
    193 FATSECTORS!=	expr ${FATMB} \* 1024 \* 1024 / 512 || true
    194 
    195 .if ${USE_MBR} == "no" && ${USE_GPT} == "no"
    196 LABELSECTORS?=	0
    197 .else
    198 #LABELSECTORS?=	63	# historical
    199 #LABELSECTORS?=	32	# 16KB aligned
    200 LABELSECTORS?=	2048	# 1MB aligned for modern flash devices
    201 .endif
    202 
    203 FSSECTORS!=	expr ${IMAGESECTORS} - ${SWAPSECTORS} - ${LABELSECTORS} \
    204 		    - ${EFISECTORS} - ${GPTSECTORS} - ${FATSECTORS} || test $$? -eq 1 || exit 1
    205 FSSIZE!=	expr ${FSSECTORS} \* 512 || test $$? -eq 1 || exit 1
    206 
    207 # parameters for disklabel and MBR
    208 HEADS=		64
    209 SECTORS=	32
    210 CYLINDERS!=	expr ${IMAGESECTORS} / \( ${HEADS} \* ${SECTORS} \)
    211 SECPERCYLINDERS!= expr ${HEADS} \* ${SECTORS}
    212 MBRHEADS=	255
    213 MBRSECTORS=	63
    214 MBRCYLINDERS!=	expr ${IMAGESECTORS} / \( ${MBRHEADS} \* ${MBRSECTORS} \)
    215 MBRNETBSD=	169
    216 MBRFAT?=	6	# 16-bit FAT, more than 32M
    217 
    218 BSDPARTSECTORS!= expr ${IMAGESECTORS} - ${LABELSECTORS} \
    219 		    - ${EFISECTORS} - ${GPTSECTORS} - ${FATSECTORS}
    220 FSOFFSET!=	expr ${LABELSECTORS} + ${EFISECTORS} + ${FATSECTORS} || test $$? -eq 1 || exit 1
    221 SWAPOFFSET!=	expr ${LABELSECTORS} + ${FSSECTORS} \
    222 		    + ${EFISECTORS} + ${FATSECTORS} || test $$? -eq 1 || exit 1
    223 FATOFFSET=	${LABELSECTORS}
    224 
    225 # parameters for sunlabel
    226 FSCYLINDERS!=	expr ${FSSECTORS} / \( ${HEADS} \* ${SECTORS} \)
    227 SWAPCYLINDERS!=	expr ${SWAPSECTORS} / \( ${HEADS} \* ${SECTORS} \) || true
    228 
    229 .if ${USE_GPT} != "no"
    230 BOOTDISK_UUID=`${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} show -i 2 | ${TOOL_AWK} '/^GUID/ {print $$2}'`
    231 SWAPDISK_UUID=`${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} show -i 3 | ${TOOL_AWK} '/^GUID/ {print $$2}'`
    232 .endif
    233 
    234 #
    235 # definitions to create root fs
    236 #
    237 SETS_DEFAULT=	modules base etc comp games gpufw man misc rescue tests text
    238 .if ${MKX11} != "no"
    239 SETS_DEFAULT+=	xbase xcomp xetc xfont xserver
    240 .endif
    241 
    242 KERN_SET?=	kern-GENERIC
    243 SETS?=		${SETS_DEFAULT}
    244 IMG_SETS=	${KERN_SET} ${SETS}
    245 SETS_DIR?=	${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
    246 
    247 FSTAB_IN?=	${DISTRIBDIR}/common/bootimage/fstab.in
    248 SPEC_IN?=	${DISTRIBDIR}/common/bootimage/spec.in
    249 
    250 IMGMAKEFSOPTIONS?=	-o bsize=16384,fsize=2048,density=8192
    251 FATMAKEFSOPTIONS?=	-o fat_type=16
    252 
    253 WORKDIR?=       work
    254 WORKSPEC?=      work.spec
    255 WORKFSTAB?=	work.fstab
    256 WORKRCCONF?=	work.rc.conf
    257 WORKFS?=        work.rootfs
    258 TARGETFS?=      imgroot.fs
    259 COMPRESS_PROGRAM=${"${USE_XZ_SETS:Uno}"!="no":?${TOOL_XZ}:${TOOL_GZIP}}
    260 TAR_SUFF=	${"${USE_XZ_SETS:Uno}"!="no":?tar.xz:tgz}
    261 
    262 CLEANFILES+=	${WORKSPEC} ${WORKFSTAB} ${WORKRCCONF} ${WORKFS}
    263 CLEANFILES+=	${TARGETFS}
    264 
    265 #
    266 # definitions to create image
    267 #
    268 .if ${SWAPSECTORS} == 0
    269 .  if ${USE_MBR} != "no"
    270 DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.noswap.mbr.in
    271 .  else
    272 DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.noswap.in
    273 .  endif
    274 .else
    275 .  if ${USE_MBR} != "no"
    276 DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.mbr.in
    277 .  else
    278 DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.in
    279 .  endif
    280 .endif
    281 
    282 MBR_DEFAULT_BOOTCODE?=	mbr
    283 
    284 WORKMBR?=	work.mbr
    285 WORKMBRTRUNC?=	work.mbr.truncated
    286 WORKSWAP?=	work.swap
    287 WORKEFI?=	work.efi
    288 WORKGPT?=	work.gpt
    289 WORKLABEL?=	work.diskproto
    290 WORKIMG?=	work.img
    291 EFIWORKDIR?=	work.efidir
    292 WORKFAT?=	work.fat
    293 WORKFATDIR?=	work.fatdir
    294 
    295 .if ${USE_GPT} != "no"
    296 ${WORKFSTAB}: ${WORKMBR}
    297 
    298 .BEGIN:
    299 	@if [ -f ${WORKMBR} ]; then			 		\
    300 		st_size=`${TOOL_STAT} -f %z ${WORKMBR}`;		\
    301 		if [ "$$st_size" -ne "$$((${IMAGESECTORS} * 512))" ]; then \
    302 			echo Removing stale ${WORKMBR} ...;		\
    303 			rm -f ${WORKMBR};				\
    304 		fi							\
    305 	fi
    306 .endif
    307 
    308 ${WORKFSTAB}:
    309 	@echo Preparing /etc/fstab ...
    310 .if ${USE_GPT} != "no"
    311 .  if ${SWAPSECTORS} != 0
    312 	${TOOL_SED}							\
    313 	    -e "s/ROOT.a/NAME=${BOOTDISK_UUID}/"			\
    314 	    -e "s/ROOT.b/NAME=${SWAPDISK_UUID}/"			\
    315 	    < ${FSTAB_IN} > ${WORKFSTAB}
    316 .  else
    317 	${TOOL_SED}							\
    318 	    -e "s/ROOT.a/NAME=${BOOTDISK_UUID}/"			\
    319 	    -e "/ROOT.b/d"						\
    320 	    < ${FSTAB_IN} > ${WORKFSTAB}
    321 .  endif
    322 .else
    323 .  if ${SWAPSECTORS} != 0
    324 	${TOOL_SED} < ${FSTAB_IN} > ${WORKFSTAB}
    325 .  else
    326 	${TOOL_SED}							\
    327 	    -e "/ROOT.b/d"						\
    328 	    < ${FSTAB_IN} > ${WORKFSTAB}
    329 .  endif
    330 .endif
    331 
    332 #
    333 # create root file system for the image
    334 #
    335 ${TARGETFS}: prepare_md_post ${WORKFSTAB}
    336 	@if [ ! -d ${RELEASEDIR}/${RELEASEMACHINEDIR} ]; then 		\
    337 		echo "Missing ${RELEASEDIR}/${RELEASEMACHINEDIR}, aborting"; \
    338 		false; 							\
    339 	fi;
    340 	@${MKDIR} ${MKDIRPERM} ${WORKDIR}
    341 .for set in ${IMG_SETS}
    342 	@if [ ! -f ${SETS_DIR}/${set}.${TAR_SUFF} ]; then 		\
    343 		echo "Missing ${SETS_DIR}/${set}.${TAR_SUFF}, aborting";\
    344 		false; 							\
    345 	fi
    346 	@echo Extracting ${set}.${TAR_SUFF} ...
    347 	@(cd ${WORKDIR}; ${TOOL_PAX} ${PAX_TIMESTAMP} -rn \
    348 		--use-compress-program=${COMPRESS_PROGRAM:Q} \
    349 		-f ${SETS_DIR}/${set}.${TAR_SUFF} .)
    350 .endfor
    351 .if defined(SECONDARY_BOOT)
    352 	@echo Copying secondary boot...
    353 	${INSTALL} ${COPY} -m 0444 ${WORKDIR}/usr/mdec/${SECONDARY_BOOT} ${WORKDIR}
    354 .endif
    355 	${INSTALL} ${COPY} -m 0644 ${WORKFSTAB} ${WORKDIR}/etc/fstab
    356 	@echo Setting rc_configured=YES in /etc/rc.conf ...
    357 	${TOOL_SED} "s/rc_configured=NO/rc_configured=YES/"		\
    358 	    < ${WORKDIR}/etc/rc.conf > ${WORKRCCONF}
    359 .if defined(IMAGEHOSTNAME)
    360 	@echo Setting hostname=${IMAGEHOSTNAME} in /etc/rc.conf ...
    361 	echo "hostname=${IMAGEHOSTNAME}" >> ${WORKRCCONF}
    362 .endif
    363 .if ${SWAPSECTORS} == 0
    364 	@echo Setting no_swap=YES in /etc/rc.conf ...
    365 	echo "no_swap=YES" >> ${WORKRCCONF}
    366 	@echo Setting savecore=NO in /etc/rc.conf ...
    367 	echo "savecore=NO" >> ${WORKRCCONF}
    368 .endif
    369 .if defined(RC_CONF_EXTRA)
    370 	@echo Adding contents of ${RC_CONF_EXTRA} to /etc/rc.conf ...
    371 	cat ${RC_CONF_EXTRA} >> ${WORKRCCONF}
    372 .endif
    373 	${INSTALL} ${COPY} -m 0644 ${WORKRCCONF} ${WORKDIR}/etc/rc.conf
    374 .if defined(IMGDIR_EXTRA)
    375 	@echo Copying extra dirs...
    376 .for _SRCDIR _TARGET in ${IMGDIR_EXTRA}
    377 	@if [ ! -d ${_SRCDIR} ]; then 					\
    378 		echo "${_SRCDIR} is not directory, aborting";		\
    379 		false; 							\
    380 	fi
    381 	${MKDIR} ${MKDIRPERM} ${WORKDIR}/${_TARGET}
    382 	(cd ${_SRCDIR} ;						\
    383 	    ${TOOL_PAX} ${PAX_TIMESTAMP} -rw -pe -v			\
    384 	    ${IMGDIR_EXCLUDE}						\
    385 	    . ${.OBJDIR}/${WORKDIR}/${_TARGET} )
    386 .endfor
    387 .endif
    388 .if defined(IMGFILE_EXTRA)
    389 	@echo Copying extra files...
    390 .for _SRC _TARGET in ${IMGFILE_EXTRA}
    391 	@if [ ! -f ${_SRC} ]; then 					\
    392 		echo "${_SRC} in IMGFILE_EXTRA not found, aborting";	\
    393 		false; 							\
    394 	fi
    395 	@if [ -f ${_SRC} ]; then 					\
    396 		echo ${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
    397 		${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
    398 	fi
    399 .endfor
    400 .endif
    401 	@echo Preparing spec files for makefs...
    402 	${RM} -f ${WORKSPEC}
    403 	cat ${WORKDIR}/etc/mtree/* |					\
    404 	    ${TOOL_SED} -e 's/ size=[0-9]*//' > ${WORKSPEC}
    405 	${HOST_SH} ${WORKDIR}/dev/MAKEDEV -s all ipty |			\
    406 	    ${TOOL_SED} -e '/^\. type=dir/d' -e 's,^\.,./dev,' >> ${WORKSPEC}
    407 	cat ${SPEC_IN} >> ${WORKSPEC}
    408 .if defined(SECONDARY_BOOT)
    409 	echo "./${SECONDARY_BOOT} type=file uname=root gname=wheel mode=0444" \
    410 	    >> ${WORKSPEC}
    411 .endif
    412 .if defined(SPEC_EXTRA)
    413 	cat ${SPEC_EXTRA} >> ${WORKSPEC}
    414 .endif
    415 	@echo Creating rootfs...
    416 	# XXX /var/spool/ftp/hidden is unreadable
    417 	${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden
    418 	${TOOL_MAKEFS} -M ${FSSIZE} -m ${FSSIZE}			\
    419 	    -B ${TARGET_ENDIANNESS}					\
    420 	    -F ${WORKSPEC} -N ${WORKDIR}/etc				\
    421 	    ${MAKEFS_TIMESTAMP}						\
    422 	    ${IMGMAKEFSOPTIONS}						\
    423 	    ${WORKFS} ${WORKDIR}
    424 .if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} == "no"
    425 	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKFS} \
    426 	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT} ${SECONDARY_BOOT_ARG}
    427 .endif
    428 	@echo done.
    429 	mv ${WORKFS} ${.TARGET}
    430 
    431 TARGET_BLOCKS=
    432 .if ${USE_MBR} != "no" || ${USE_GPT} != "no"
    433 TARGET_BLOCKS+=	${WORKMBRTRUNC}
    434 .endif
    435 .if ${USE_GPT} != "no"
    436 TARGET_BLOCKS+=	${WORKEFI}
    437 .endif
    438 .if ${FATSECTORS} != 0
    439 TARGET_BLOCKS+=	${WORKFAT}
    440 .endif
    441 TARGET_BLOCKS+=	 ${TARGETFS}
    442 .if ${SWAPSECTORS} != 0
    443 TARGET_BLOCKS+=	${WORKSWAP}
    444 .endif
    445 .if ${USE_GPT} != "no"
    446 TARGET_BLOCKS+=	${WORKGPT}
    447 .endif
    448 
    449 CLEANFILES+=	${WORKMBR} ${WORKMBRTRUNC} ${WORKSWAP}
    450 CLEANFILES+=	${WORKEFI} ${WORKGPT}
    451 CLEANFILES+=	${WORKFAT}
    452 CLEANFILES+=	${WORKLABEL}.tmp ${WORKLABEL}
    453 CLEANFILES+=	${WORKIMG} ${IMGBASE}.img
    454 
    455 ${WORKSWAP}:
    456 .if ${SWAPSECTORS} == 0
    457 	printf "" > ${WORKSWAP}
    458 .else
    459 	${DD} if=/dev/zero of=${WORKSWAP} seek=$$((${SWAPSECTORS} - 1)) count=1
    460 .endif
    461 
    462 ${WORKMBRTRUNC}: ${WORKMBR}
    463 	${DD} if=${WORKMBR} count=${LABELSECTORS} of=${WORKMBRTRUNC}
    464 
    465 ${WORKLABEL}:
    466 	${TOOL_SED}							\
    467 	    -e "s/@@SECTORS@@/${SECTORS}/"				\
    468 	    -e "s/@@HEADS@@/${HEADS}/"					\
    469 	    -e "s/@@SECPERCYLINDERS@@/${SECPERCYLINDERS}/"		\
    470 	    -e "s/@@CYLINDERS@@/${CYLINDERS}/"				\
    471 	    -e "s/@@IMAGESECTORS@@/${IMAGESECTORS}/"			\
    472 	    -e "s/@@FSSECTORS@@/${FSSECTORS}/"				\
    473 	    -e "s/@@FSOFFSET@@/${FSOFFSET}/"				\
    474 	    -e "s/@@SWAPSECTORS@@/${SWAPSECTORS}/"			\
    475 	    -e "s/@@SWAPOFFSET@@/${SWAPOFFSET}/"			\
    476 	    -e "s/@@BSDPARTSECTORS@@/${BSDPARTSECTORS}/"		\
    477 	    -e "s/@@FATSECTORS@@/${FATSECTORS}/"			\
    478 	    -e "s/@@FATOFFSET@@/${FATOFFSET}/"				\
    479 	    < ${DISKPROTO_IN} > ${WORKLABEL}.tmp
    480 	mv ${WORKLABEL}.tmp ${WORKLABEL}
    481 
    482 ${WORKEFI}: ${TARGETFS}
    483 	@echo create EFI system partition...
    484 	@${MKDIR} ${MKDIRPERM} ${EFIWORKDIR}/EFI/boot
    485 .for f in ${EFIBOOT}
    486 	${INSTALL} ${COPY} -m 0644 ${f} ${EFIWORKDIR}/EFI/boot/`basename ${f}`
    487 .endfor
    488 	${RM} -f ${WORKEFI}
    489 	${TOOL_MAKEFS} -M ${EFIMB}m -m ${EFIMB}m			\
    490 	    -B ${TARGET_ENDIANNESS}					\
    491 	    ${MAKEFS_TIMESTAMP}						\
    492 	    -t msdos -o F=32,c=1					\
    493 	    ${WORKEFI} ${EFIWORKDIR}
    494 
    495 ${WORKFAT}: ${TARGETFS} ${FATFILES}
    496 	@echo create FAT partition for bootstrap files...
    497 	@${MKDIR} ${MKDIRPERM} ${WORKFATDIR}
    498 .if defined(FATFILES)
    499 	@echo Copying files for FAT partition...
    500 .for f in ${FATFILES}
    501 	@if [ ! -f ${f} ]; then 					\
    502 		echo "${f} in FATFILES not found, aborting";		\
    503 		false; 							\
    504 	fi
    505 	${INSTALL} ${COPY} -m 0644 ${f} ${WORKFATDIR}
    506 .endfor
    507 .endif
    508 	${RM} -f ${WORKFAT}
    509 	${TOOL_MAKEFS} -M ${FATMB}m -m ${FATMB}m			\
    510 	    ${MAKEFS_TIMESTAMP}						\
    511 	    -t msdos							\
    512 	    ${FATMAKEFSOPTIONS}						\
    513 	    ${WORKFAT} ${WORKFATDIR}
    514 
    515 ${WORKMBR}:
    516 .if ${USE_GPT} != "no"
    517 	@echo creating GPT header and partition entries...
    518 	${RM} -f ${WORKMBR}
    519 	${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
    520 	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} create
    521 	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${EFISECTORS} -t efi -l "EFI system"
    522 	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${FSSECTORS} -t ffs
    523 .  if ${SWAPSECTORS} != 0
    524 	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${SWAPSECTORS} -t swap
    525 .  endif
    526 .elif ${USE_MBR} != "no"
    527 	@echo creating MBR labels...
    528 	${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
    529 .  if ${FATSECTORS} != 0
    530 	# create BSD partition
    531 	${TOOL_FDISK} -f -i -u \
    532 	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
    533 	    -1 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS}		\
    534 	    -F ${WORKMBR}
    535 	# create FAT partition
    536 	${TOOL_FDISK} -f -u \
    537 	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
    538 	    -0 -s ${MBRFAT}/${FATOFFSET}/${FATSECTORS}			\
    539 	    -F ${WORKMBR}
    540 .  else
    541 	# create BSD partition
    542 	${TOOL_FDISK} -f -i -u \
    543 	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
    544 	    -0 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS}		\
    545 	    -F ${WORKMBR}
    546 .  endif
    547 .  if empty(MBR_BOOTCODE)
    548 	@if [ -f ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} ]; then 	\
    549 		echo ${TOOL_FDISK} -f					\
    550 		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
    551 		    -F ${WORKMBR};					\
    552 		${TOOL_FDISK} -f					\
    553 		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
    554 		    -F ${WORKMBR};					\
    555 	fi
    556 .  else
    557 	@if [ ! -f ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} ]; then 		\
    558 		echo "${MBR_BOOTCODE} is not found in DESTDIR/usr/mdec, aborting"; \
    559 		false; 							\
    560 	fi
    561 	${TOOL_FDISK} -f -i -c ${WORKDIR}/usr/mdec/${MBR_BOOTCODE}	\
    562 	    -F ${WORKMBR}
    563 .  endif
    564 .endif
    565 
    566 ${WORKGPT}: ${WORKMBR}
    567 	@echo create GPT image...
    568 	${DD} if=${WORKMBR} of=${WORKGPT} skip=$$((${IMAGESECTORS} - 2048)) \
    569 	    count=2048
    570 
    571 .if ${USE_GPT} == "no"
    572 ${IMGBASE}.img:	${WORKLABEL}
    573 .endif
    574 
    575 ${IMGBASE}.img:	${TARGET_BLOCKS}
    576 	${CAT} ${TARGET_BLOCKS} > ${WORKIMG}
    577 .if ${USE_GPT} != "no"
    578 .if ${USE_GPTMBR} != "no"
    579 	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKIMG} biosboot -i 2		\
    580 	    -c ${.OBJDIR}/${WORKDIR}/usr/mdec/gptmbr.bin
    581 .endif
    582 	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKIMG} set -a bootme -i 2
    583 .endif	# USE_GPT != "no"
    584 .if ${USE_SUNLABEL} != "no"
    585 	@echo Creating sun disklabel...
    586 	printf 'V ncyl %d\nV nhead %d\nV nsect %d\na %d %d/0/0\nb %d %d/0/0\nW\n' \
    587 	    ${CYLINDERS} ${HEADS} ${SECTORS} \
    588 	    ${FSOFFSET} ${FSCYLINDERS} ${FSCYLINDERS} ${SWAPCYLINDERS} | \
    589 	    ${TOOL_SUNLABEL} -nq ${WORKIMG}
    590 .endif
    591 .if ${USE_GPT} == "no"
    592 	${TOOL_DISKLABEL} -R -F ${WORKIMG} ${WORKLABEL}
    593 .if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} != "no"
    594 	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKIMG} \
    595 	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT}
    596 .endif
    597 .endif
    598 	mv ${WORKIMG} ${.TARGET}
    599 
    600 CLEANFILES+=	${IMGBASE}.img.gz ${IMGBASE}.img.gz.tmp
    601 
    602 ${IMGBASE}.img.gz:	${IMGBASE}.img
    603 	${TOOL_GZIP_N} -9c ${IMGBASE}.img > ${.TARGET}.tmp
    604 	mv ${.TARGET}.tmp ${.TARGET}
    605 
    606 clean:
    607 	@if [ -d ${WORKDIR}/var/spool/ftp/hidden ]; then 		\
    608 		${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden;		\
    609 	fi	# XXX
    610 	${RM} -fr ${WORKDIR}
    611 	${RM} -fr ${EFIWORKDIR}
    612 	${RM} -fr ${WORKFATDIR}
    613 
    614 prepare_md_post: .PHONY
    615 image_md_post: .PHONY
    616 image_md_pre: .PHONY
    617 
    618 .include <bsd.prog.mk>
    619