1#	$NetBSD: Makefile.bootimage,v 1.50 2026/01/18 00:07:36 tsutsui 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#	USE_GPTMBR
44#		set yes if target disk image requires GPT partition
45#		with hybrid MBR boot code
46#		(default: no; valid only if USE_GPT=yes)
47#	USE_MDBOOTPART
48#		set yes if target disk image requires port specific
49#		boot partition
50#		(default: no; if yes target rules of WORKMDBOOTPART should
51#		 be defined in MD liveimage/Makefiles)
52#	MBR_BOOTCODE
53#		optional MBR bootcode which should be installed by fdisk(8)
54#		(default: empty)
55#		- specified MBR_BOOTCODE must exist in ${DESTDIR}/usr/mdec
56#		- if MBR_BOOTCODE is not specified,
57#		  MBR_DEFAULT_BOOTCODE (default: mbr) will be used
58#		  if the target ${MACHINE} has the one in /usr/mdec
59#	USE_SUNLABEL
60#		set yes if target disk image requires Sun's label
61#		(default: no)
62#	INSTALLBOOT_AFTER_DISKLABEL
63#		set yes if the target ${MACHINE} requires disklabel
64#		to run installboot(8), like hp300 and vax
65#		(default: empty)
66#	IMAGEMB
67#		target image size in MB
68#		(default: 2048)
69#	SWAPMB
70#		swap size in target image in MB
71#		(default: 128)
72#	EFIMB
73#		EFI partition size in target image in MB
74#		(default: 128)
75#	FATMB
76#		FAT partition size in target image in MB
77#		(default: 0)
78#	MDBOOTPARTMB
79#		port specific boot partition size in target image in MB
80#		(default: 0)
81#	GPTSECTORS
82#		Size of a region reserved for the secondary GPT table/entry
83#		at the end of the target image in sectors
84#		(default: 2048 if USE_GPT=yes, otherwise 0)
85#	KERN_SET
86#		kernel set name which should be extracted into image
87#		(default: kern-GENERIC)
88#	SETS
89#		binary sets that should be extracted into image
90#		(default: modules base etc comp games gpufw man manhtml misc
91#		          rescue tests text xbase xcomp xetc xfont xserver)
92#	SETS_DIR
93#		directory path where binary sets are stored
94#		(default: ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets)
95#	IMGFILE_EXTRA
96#		list of additional files to be copied into images,
97#		containing one or more tuples of the form:
98#			FILE	TARGETPATH
99#		for installation image etc.
100#		(default: empty)
101#	IMGDIR_EXTRA
102#		list of additional directories to be copied into images,
103#		containing one or more tuples of the form:
104#			DIR	TARGETPATH
105#		for installation image etc.
106#		(default: empty)
107#		XXX: currently permissions in IMGDIR_EXTRA are not handled
108#	IMGDIR_EXCLUDE
109#		pax(1) options to exclude files which should not copied
110#		into TARGETPATH in IMGDIR_EXTRA
111#		(default: empty)
112#	FSTAB_IN
113#		template file of /etc/fstab
114#		(default: ${DISTRIBDIR}/common/bootimage/fstab.in)
115#	SPEC_IN
116#		default files of spec file for makefs(8)
117#		(default: ${DISTRIBDIR}/common/bootimage/spec.in)
118#	SPEC_EXTRA
119#		additional files of spec file for makefs(8)
120#		(default: empty)
121#	IMGMAKEFSOPTIONS
122#		options passed to makefs(8) to create root file system
123#		(default: -o bsize=16384,fsize=2048,density=8192)
124#	IMGFFSVERSION
125#		version of FFS created by makefs(8)
126#		(default: 1)
127#	INSTALLBOOTOPTIONS
128#		options passed to installboot(8), e.g., -o console=com0
129#		(default: empty)
130#	PRIMARY_BOOT
131#		primary boot loader that should be installed into
132#		the target image via installboot(8)
133#		(default: empty)
134#	SECONDARY_BOOT
135#		secondary bootloader that should be put into the target image
136#		(default: empty)
137#	SECONDARY_BOOT_ARG
138#		extra arguments that should be passed to installboot(8)
139#		to specify the secondary bootloader
140#		(default: empty)
141#	DISKPROTO_IN
142#		template file of disklabel -R
143#		(default: ${DISTRIBDIR}/common/bootimage/diskproto.in
144#		       or ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in)
145#	EFIBOOT
146#		EFI bootloader
147#		(default: empty)
148#	MBRFAT
149#		MBR partition type value for FAT
150#		(default: 6 ("16-bit FAT, more than 32M"))
151#	FATMAKEFSOPTIONS
152#		options passed to makefs(8) to create a FAT partition
153#		(default: -o fat_type=16)
154#	FATFILES
155#		list of additional files to be copied into FAT partition
156#		(default: empty)
157#	IMAGEHOSTNAME
158#		if set, sets the default hostname in rc.conf (default: empty)
159#
160
161.include <bsd.own.mk> 		#
162.include <bsd.endian.mk>	# for TARGET_ENDIANNESS
163
164.include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib"
165
166.if ${MKREPRO_TIMESTAMP:Uno} != "no"
167MAKEFS_TIMESTAMP=-T "${MKREPRO_TIMESTAMP}"
168GPT_TIMESTAMP=-T "${MKREPRO_TIMESTAMP}"
169PAX_TIMESTAMP=--timestamp "${MKREPRO_TIMESTAMP}"
170.endif
171
172.if empty(IMGBASE)
173.BEGIN:
174	@echo "Error: IMGBASE is not set"
175	@false
176.endif
177
178# should be defined elsewhere? 
179CAT?=		cat
180CHMOD?=		chmod
181DD?=		dd
182MKDIR?=		mkdir -p
183RM?=		rm
184
185MAKESPEC=	${HOST_SH} ${NETBSDSRCDIR}/etc/makespec
186
187#
188# common definitions for image
189#
190USE_MBR?=	no
191USE_GPT?=	no
192USE_GPTMBR?=	no
193USE_MDBOOTPART?= no
194USE_SUNLABEL?=	no
195INSTALLBOOT_AFTER_DISKLABEL?=	no
196
197#
198# size parameters for image
199#
200IMAGEMB?=	2048		# 2048MB
201SWAPMB?=	128		# 128MB
202.if ${USE_GPT} == "no"
203EFIMB?=		0
204GPTSECTORS=	0
205.else
206EFIMB?=		128		# 128MB
207GPTSECTORS=	2048
208.endif
209FATMB?=		0
210MDBOOTPARTMB?=	0
211
212# XXX: SWAPMB could be zero and expr(1) returns exit status 1 in that case
213IMAGESECTORS!=	expr ${IMAGEMB} \* 1024 \* 1024 / 512
214SWAPSECTORS!=	expr ${SWAPMB} \* 1024 \* 1024 / 512 || true
215EFISECTORS!=	expr ${EFIMB} \* 1024 \* 1024 / 512 || true
216FATSECTORS!=	expr ${FATMB} \* 1024 \* 1024 / 512 || true
217
218.if ${USE_MDBOOTPART} != "no"
219MDBOOTPARTSECTORS!=	expr ${MDBOOTPARTMB} \* 1024 \* 1024 / 512 || true
220.else
221MDBOOTPARTSECTORS=	0
222.endif
223
224.if ${USE_MBR} == "no" && ${USE_GPT} == "no"
225LABELSECTORS?=	0
226.else
227#LABELSECTORS?=	63	# historical
228#LABELSECTORS?=	32	# 16KB aligned
229LABELSECTORS?=	2048	# 1MB aligned for modern flash devices
230.endif
231
232.if ${USE_MDBOOTPART} != "no"
233MDBOOTPARTOFFSET!=	expr ${LABELSECTORS} + ${EFISECTORS} + ${FATSECTORS} \
234			    || test $$? -eq 1 || exit 1
235.else
236MDBOOTPARTOFFSET=	0
237.endif
238
239FSSECTORS!=	expr ${IMAGESECTORS} - ${SWAPSECTORS} - ${LABELSECTORS} \
240		    - ${EFISECTORS} - ${GPTSECTORS} - ${FATSECTORS} \
241		    - ${MDBOOTPARTSECTORS} || test $$? -eq 1 || exit 1
242FSSIZE!=	expr ${FSSECTORS} \* 512 || test $$? -eq 1 || exit 1
243
244# parameters for disklabel and MBR
245HEADS=		64
246SECTORS=	32
247CYLINDERS!=	expr ${IMAGESECTORS} / \( ${HEADS} \* ${SECTORS} \)
248SECPERCYLINDERS!= expr ${HEADS} \* ${SECTORS}
249MBRHEADS=	255
250MBRSECTORS=	63
251MBRCYLINDERS!=	expr ${IMAGESECTORS} / \( ${MBRHEADS} \* ${MBRSECTORS} \)
252MBRNETBSD=	169
253MBRFAT?=	6	# 16-bit FAT, more than 32M
254
255BSDPARTSECTORS!= expr ${IMAGESECTORS} - ${LABELSECTORS} \
256		    - ${EFISECTORS} - ${GPTSECTORS} - ${FATSECTORS} \
257		    - ${MDBOOTPARTSECTORS}
258FSOFFSET!=	expr ${LABELSECTORS} + ${EFISECTORS} + ${FATSECTORS} \
259		    + ${MDBOOTPARTSECTORS} || test $$? -eq 1 || exit 1
260SWAPOFFSET!=	expr ${LABELSECTORS} + ${FSSECTORS} \
261		    + ${EFISECTORS} + ${FATSECTORS} \
262		    + ${MDBOOTPARTSECTORS} || test $$? -eq 1 || exit 1
263FATOFFSET=	${LABELSECTORS}
264
265# helper script for sunlabel
266DISKPROTO2SUNLABEL?=	${DISTRIBDIR}/common/bootimage/diskproto2sunlabel.awk
267
268.if ${USE_GPT} != "no"
269BOOTDISK_UUID=`${TOOL_GPT} ${GPT_TIMESTAMP} ${WORKMBR} show -i 2 | ${TOOL_AWK} '/^GUID/ {print $$2}'`
270SWAPDISK_UUID=`${TOOL_GPT} ${GPT_TIMESTAMP} ${WORKMBR} show -i 3 | ${TOOL_AWK} '/^GUID/ {print $$2}'`
271.endif
272
273#
274# definitions to create root fs
275#
276SETS_DEFAULT=	modules base etc comp games gpufw man misc rescue text
277.if ${MKATF} != "no"
278SETS_DEFAULT+=	tests
279.endif
280.if ${MKCOMPAT} != "no"
281SETS_DEFAULT+=	base32
282.endif
283.if ${MKCOMPAT} != "no" && \
284    (${MACHINE_ARCH} == "mips64eb" || ${MACHINE_ARCH} == "mips64el")
285SETS_DEFAULT+=	base64
286.endif
287.if ${MKHTML} != "no"
288SETS_DEFAULT+=	manhtml
289.endif
290.if ${MKX11} != "no"
291SETS_DEFAULT+=	xbase xcomp xetc xfont xserver
292.endif
293
294KERN_SET?=	kern-GENERIC
295SETS?=		${SETS_DEFAULT}
296IMG_SETS=	${KERN_SET} ${SETS}
297SETS_DIR?=	${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
298
299FSTAB_IN?=	${DISTRIBDIR}/common/bootimage/fstab.in
300.if ${KERNEL_DIR:Uno} == "yes"
301SPEC_IN?=	${DISTRIBDIR}/common/bootimage/spec-dir.in
302.else
303SPEC_IN?=	${DISTRIBDIR}/common/bootimage/spec.in
304.endif
305
306IMGMAKEFSOPTIONS?=	-o bsize=16384,fsize=2048,density=8192
307IMGFFSVERSION?=		1
308FATMAKEFSOPTIONS?=	-o fat_type=16
309
310WORKDIR?=       work
311WORKSPEC?=      work.spec
312WORKFSTAB?=	work.fstab
313WORKRCCONF?=	work.rc.conf
314WORKFS?=        work.rootfs
315TARGETFS?=      imgroot.fs
316COMPRESS_PROGRAM=${"${USE_XZ_SETS:Uno}"!="no":?${TOOL_XZ}:${TOOL_GZIP}}
317TAR_SUFF=	${"${USE_XZ_SETS:Uno}"!="no":?tar.xz:tgz}
318
319CLEANFILES+=	${WORKSPEC} ${WORKFSTAB} ${WORKRCCONF} ${WORKFS}
320CLEANFILES+=	${TARGETFS}
321
322#
323# definitions to create image
324#
325.if ${SWAPSECTORS} == 0
326.  if ${USE_MBR} != "no"
327DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.noswap.mbr.in
328.  else
329DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.noswap.in
330.  endif
331.else
332.  if ${USE_MBR} != "no"
333DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.mbr.in
334.  else
335DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.in
336.  endif
337.endif
338
339MBR_DEFAULT_BOOTCODE?=	mbr
340
341WORKMBR?=	work.mbr
342WORKMBRTRUNC?=	work.mbr.truncated
343WORKSWAP?=	work.swap
344WORKEFI?=	work.efi
345WORKGPT?=	work.gpt
346WORKLABEL?=	work.diskproto
347WORKIMG?=	work.img
348EFIWORKDIR?=	work.efidir
349WORKFAT?=	work.fat
350WORKFATDIR?=	work.fatdir
351WORKMDBOOTPART?= work.mdbootpart
352WORKMDBOOTPARTDIR?= work.mdbootpartdir
353
354.if ${USE_GPT} != "no"
355${WORKFSTAB}: ${WORKMBR}
356
357.BEGIN:
358	@if [ -f ${WORKMBR} ]; then			 		\
359		st_size=`${TOOL_STAT} -f %z ${WORKMBR}`;		\
360		if [ "$$st_size" -ne "$$((${IMAGESECTORS} * 512))" ]; then \
361			echo Removing stale ${WORKMBR} ...;		\
362			rm -f ${WORKMBR};				\
363		fi							\
364	fi
365.endif
366
367${WORKFSTAB}:
368	@echo Preparing /etc/fstab ...
369.if ${USE_GPT} != "no"
370.  if ${SWAPSECTORS} != 0
371	${TOOL_SED}							\
372	    -e "s/ROOT.a/NAME=NBImgRoot/"				\
373	    -e "s/ROOT.b/NAME=NBImgSwap/"				\
374	    < ${FSTAB_IN} > ${WORKFSTAB}
375.  else
376	${TOOL_SED}							\
377	    -e "s/ROOT.a/NAME=NBImgRoot/"				\
378	    -e "/ROOT.b/d"						\
379	    < ${FSTAB_IN} > ${WORKFSTAB}
380.  endif
381.else
382.  if ${SWAPSECTORS} != 0
383	${TOOL_SED} < ${FSTAB_IN} > ${WORKFSTAB}
384.  else
385	${TOOL_SED}							\
386	    -e "/ROOT.b/d"						\
387	    < ${FSTAB_IN} > ${WORKFSTAB}
388.  endif
389.endif
390
391#
392# create root file system for the image
393#
394${TARGETFS}: prepare_md_post ${WORKFSTAB}
395	@${RM} -f ${WORKSPEC}
396	@if [ ! -d ${RELEASEDIR}/${RELEASEMACHINEDIR} ]; then 		\
397		echo "Missing ${RELEASEDIR}/${RELEASEMACHINEDIR}, aborting"; \
398		false; 							\
399	fi;
400	@${MKDIR} ${MKDIRPERM} ${WORKDIR}
401.for set in ${IMG_SETS}
402	@if [ ! -f ${SETS_DIR}/${set}.${TAR_SUFF} ]; then 		\
403		echo "Missing ${SETS_DIR}/${set}.${TAR_SUFF}, aborting";\
404		false; 							\
405	fi
406	@echo Extracting ${set}.${TAR_SUFF} ...
407	@(cd ${WORKDIR}; ${TOOL_PAX} ${PAX_TIMESTAMP} -rn \
408		--use-compress-program=${COMPRESS_PROGRAM:Q} \
409		-f ${SETS_DIR}/${set}.${TAR_SUFF} .)
410.endfor
411.if defined(SECONDARY_BOOT)
412	@echo Copying secondary boot...
413	${INSTALL} ${COPY} -m 0444 ${WORKDIR}/usr/mdec/${SECONDARY_BOOT} ${WORKDIR}
414.endif
415	${INSTALL} ${COPY} -m 0644 ${WORKFSTAB} ${WORKDIR}/etc/fstab
416	@echo Setting rc_configured=YES in /etc/rc.conf ...
417	${TOOL_SED} "s/rc_configured=NO/rc_configured=YES/"		\
418	    < ${WORKDIR}/etc/rc.conf > ${WORKRCCONF}
419.if defined(IMAGEHOSTNAME)
420	@echo Setting hostname=${IMAGEHOSTNAME} in /etc/rc.conf ...
421	echo "hostname=${IMAGEHOSTNAME}" >> ${WORKRCCONF}
422.endif
423.if ${SWAPSECTORS} == 0
424	@echo Setting no_swap=YES in /etc/rc.conf ...
425	echo "no_swap=YES" >> ${WORKRCCONF}
426	@echo Setting savecore=NO in /etc/rc.conf ...
427	echo "savecore=NO" >> ${WORKRCCONF}
428.endif
429.if defined(RC_CONF_EXTRA)
430	@echo Adding contents of ${RC_CONF_EXTRA} to /etc/rc.conf ...
431	cat ${RC_CONF_EXTRA} >> ${WORKRCCONF}
432.endif
433	${INSTALL} ${COPY} -m 0644 ${WORKRCCONF} ${WORKDIR}/etc/rc.conf
434.if defined(IMGDIR_EXTRA)
435	@echo Copying extra dirs...
436.for _SRCDIR _TARGET in ${IMGDIR_EXTRA}
437	@if [ ! -d ${_SRCDIR} ]; then 					\
438		echo "${_SRCDIR} is not directory, aborting";		\
439		false; 							\
440	fi
441	${MKDIR} ${MKDIRPERM} ${WORKDIR}/${_TARGET}
442	(cd ${_SRCDIR} ;						\
443	    ${TOOL_PAX} ${PAX_TIMESTAMP} -rw -pe -v			\
444	    ${IMGDIR_EXCLUDE}						\
445	    . ${.OBJDIR}/${WORKDIR}/${_TARGET} )
446.endfor
447.endif
448.if defined(IMGFILE_EXTRA)
449	@echo Copying extra files...
450.for _SRC _TARGET in ${IMGFILE_EXTRA}
451	@if [ ! -f ${_SRC} ]; then 					\
452		echo "${_SRC} in IMGFILE_EXTRA not found, aborting";	\
453		false; 							\
454	fi
455	@if [ -f ${_SRC} ]; then 					\
456		echo ${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
457		${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
458		echo "./${_TARGET} type=file uname=root gname=wheel mode=0555" >> ${WORKSPEC}; \
459	fi
460.endfor
461.endif
462	@echo Preparing spec files for makefs...
463	cat ${WORKDIR}/etc/mtree/* |					\
464	    ${TOOL_SED} -e 's/ size=[0-9]*//' > ${WORKSPEC}
465	${HOST_SH} ${WORKDIR}/dev/MAKEDEV -s all ipty |			\
466	    ${TOOL_SED} -e '/^\. type=dir/d' -e 's,^\.,./dev,' >> ${WORKSPEC}
467	cat ${SPEC_IN} >> ${WORKSPEC}
468.if defined(SECONDARY_BOOT)
469	echo "./${SECONDARY_BOOT} type=file uname=root gname=wheel mode=0444" \
470	    >> ${WORKSPEC}
471.endif
472.if defined(SPEC_EXTRA)
473	cat ${SPEC_EXTRA} >> ${WORKSPEC}
474.endif
475	for i in ${WORKDIR}/netbsd.*; do \
476	    if [ -e $$i ]; then \
477		echo "./$$(basename $$i) type=file uname=root gname=wheel mode=0444" >> ${WORKSPEC}; \
478	    fi; \
479	done
480	if [ -d ${WORKDIR}/${RELEASEMACHINEDIR} ]; then \
481	    ${MAKESPEC} -d ${WORKDIR} ${RELEASEMACHINEDIR} >> ${WORKSPEC}; \
482	fi
483	@echo Creating rootfs...
484	# XXX /var/spool/ftp/hidden is unreadable
485	${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden
486	${TOOL_MAKEFS} -M ${FSSIZE} -m ${FSSIZE}			\
487	    -B ${TARGET_ENDIANNESS}					\
488	    -xx -F ${WORKSPEC} -N ${WORKDIR}/etc			\
489	    ${MAKEFS_TIMESTAMP}						\
490	    -t ffs							\
491	    ${IMGMAKEFSOPTIONS}						\
492	    -o version=${IMGFFSVERSION}					\
493	    ${WORKFS} ${WORKDIR}
494.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} == "no"
495	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKFS} \
496	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT} ${SECONDARY_BOOT_ARG}
497.endif
498	@echo done.
499	mv ${WORKFS} ${.TARGET}
500
501TARGET_BLOCKS=
502.if ${USE_MBR} != "no" || ${USE_GPT} != "no"
503TARGET_BLOCKS+=	${WORKMBRTRUNC}
504.endif
505.if ${USE_GPT} != "no"
506TARGET_BLOCKS+=	${WORKEFI}
507.endif
508.if ${FATSECTORS} != 0
509TARGET_BLOCKS+=	${WORKFAT}
510.endif
511.if ${USE_MDBOOTPART} != "no"
512TARGET_BLOCKS+=	${WORKMDBOOTPART}
513.endif
514TARGET_BLOCKS+=	 ${TARGETFS}
515.if ${SWAPSECTORS} != 0
516TARGET_BLOCKS+=	${WORKSWAP}
517.endif
518.if ${USE_GPT} != "no"
519TARGET_BLOCKS+=	${WORKGPT}
520.endif
521
522CLEANFILES+=	${WORKMBR} ${WORKMBRTRUNC} ${WORKSWAP}
523CLEANFILES+=	${WORKEFI} ${WORKGPT}
524CLEANFILES+=	${WORKFAT}
525CLEANFILES+=	${WORKMDBOOTPART}
526CLEANFILES+=	${WORKLABEL}.tmp ${WORKLABEL}
527CLEANFILES+=	${WORKIMG} ${IMGBASE}.img
528
529${WORKSWAP}:
530.if ${SWAPSECTORS} == 0
531	printf "" > ${WORKSWAP}
532.else
533	${DD} if=/dev/zero of=${WORKSWAP} seek=$$((${SWAPSECTORS} - 1)) count=1
534.endif
535
536${WORKMBRTRUNC}: ${WORKMBR}
537	${DD} if=${WORKMBR} count=${LABELSECTORS} of=${WORKMBRTRUNC}
538
539${WORKLABEL}:
540	${TOOL_SED}							\
541	    -e "s/@@SECTORS@@/${SECTORS}/"				\
542	    -e "s/@@HEADS@@/${HEADS}/"					\
543	    -e "s/@@SECPERCYLINDERS@@/${SECPERCYLINDERS}/"		\
544	    -e "s/@@CYLINDERS@@/${CYLINDERS}/"				\
545	    -e "s/@@IMAGESECTORS@@/${IMAGESECTORS}/"			\
546	    -e "s/@@FSSECTORS@@/${FSSECTORS}/"				\
547	    -e "s/@@FSOFFSET@@/${FSOFFSET}/"				\
548	    -e "s/@@SWAPSECTORS@@/${SWAPSECTORS}/"			\
549	    -e "s/@@SWAPOFFSET@@/${SWAPOFFSET}/"			\
550	    -e "s/@@BSDPARTSECTORS@@/${BSDPARTSECTORS}/"		\
551	    -e "s/@@FATSECTORS@@/${FATSECTORS}/"			\
552	    -e "s/@@FATOFFSET@@/${FATOFFSET}/"				\
553	    -e "s/@@MDBOOTPARTSECTORS@@/${MDBOOTPARTSECTORS}/"		\
554	    -e "s/@@MDBOOTPARTOFFSET@@/${MDBOOTPARTOFFSET}/"		\
555	    < ${DISKPROTO_IN} > ${WORKLABEL}.tmp
556	mv ${WORKLABEL}.tmp ${WORKLABEL}
557
558${WORKEFI}: ${TARGETFS}
559	@echo create EFI system partition...
560	@${MKDIR} ${MKDIRPERM} ${EFIWORKDIR}/EFI/boot
561.for f in ${EFIBOOT}
562	${INSTALL} ${COPY} -m 0644 ${f} ${EFIWORKDIR}/EFI/boot/`basename ${f}`
563.endfor
564	${RM} -f ${WORKEFI}
565	${TOOL_MAKEFS} -M ${EFIMB}m -m ${EFIMB}m			\
566	    ${MAKEFS_TIMESTAMP}						\
567	    -t msdos -o F=32,c=1					\
568	    ${WORKEFI} ${EFIWORKDIR}
569
570.if defined(FATFILES)
571${FATFILES}: ${TARGETFS}
572	# Some MD ${FATFILES} (bootloaders etc.) are assumed extracted
573	# to ${WORKDIR} from binary sets during ${TARGETFS} image build
574.endif
575
576${WORKFAT}: ${FATFILES}
577	@echo create FAT partition for bootstrap files...
578	@${MKDIR} ${MKDIRPERM} ${WORKFATDIR}
579.if defined(FATFILES)
580	@echo Copying files for FAT partition...
581.for f in ${FATFILES}
582	@if [ ! -f ${f} ]; then 					\
583		echo "${f} in FATFILES not found, aborting";		\
584		false; 							\
585	fi
586	${INSTALL} ${COPY} -m 0644 ${f} ${WORKFATDIR}
587.endfor
588.endif
589	${RM} -f ${WORKFAT}
590	${TOOL_MAKEFS} -M ${FATMB}m -m ${FATMB}m			\
591	    ${MAKEFS_TIMESTAMP}						\
592	    -t msdos							\
593	    ${FATMAKEFSOPTIONS}						\
594	    ${WORKFAT} ${WORKFATDIR}
595
596${WORKMBR}:
597.if ${USE_GPT} != "no"
598	@echo creating GPT header and partition entries...
599	${RM} -f ${WORKMBR}
600	${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
601	${TOOL_GPT} ${GPT_TIMESTAMP} ${WORKMBR} create
602	${TOOL_GPT} ${GPT_TIMESTAMP} ${WORKMBR} add -a 1m -s ${EFISECTORS} -t efi -l "EFI system"
603	${TOOL_GPT} ${GPT_TIMESTAMP} ${WORKMBR} add -a 1m -s ${FSSECTORS} -t ffs -l "NBImgRoot"
604.  if ${SWAPSECTORS} != 0
605	${TOOL_GPT} ${GPT_TIMESTAMP} ${WORKMBR} add -a 1m -s ${SWAPSECTORS} -t swap -l "NBImgSwap"
606.  endif
607.elif ${USE_MBR} != "no"
608	@echo creating MBR labels...
609	${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
610.  if ${FATSECTORS} != 0
611	# create BSD partition
612	${TOOL_FDISK} -f -i -u \
613	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
614	    -1 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS}		\
615	    -F ${WORKMBR}
616	# create FAT partition
617	${TOOL_FDISK} -f -u \
618	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
619	    -0 -s ${MBRFAT}/${FATOFFSET}/${FATSECTORS}			\
620	    -F ${WORKMBR}
621.  else
622	# create BSD partition
623	${TOOL_FDISK} -f -i -u \
624	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
625	    -0 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS}		\
626	    -F ${WORKMBR}
627.  endif
628.  if empty(MBR_BOOTCODE)
629	@if [ -f ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} ]; then 	\
630		echo ${TOOL_FDISK} -f					\
631		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
632		    -F ${WORKMBR};					\
633		${TOOL_FDISK} -f					\
634		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
635		    -F ${WORKMBR};					\
636	fi
637.  else
638	@if [ ! -f ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} ]; then 		\
639		echo "${MBR_BOOTCODE} is not found in DESTDIR/usr/mdec, aborting"; \
640		false; 							\
641	fi
642	${TOOL_FDISK} -f -i -c ${WORKDIR}/usr/mdec/${MBR_BOOTCODE}	\
643	    -F ${WORKMBR}
644.  endif
645.endif
646
647${WORKGPT}: ${WORKMBR}
648	@echo create GPT image...
649	${DD} if=${WORKMBR} of=${WORKGPT} \
650	    skip=$$((${IMAGESECTORS} - ${GPTSECTORS})) count=${GPTSECTORS}
651
652.if ${USE_GPT} == "no"
653${IMGBASE}.img:	${WORKLABEL}
654.endif
655
656${IMGBASE}.img:	${TARGET_BLOCKS}
657	${CAT} ${TARGET_BLOCKS} > ${WORKIMG}
658.if ${USE_GPT} != "no"
659.if ${USE_GPTMBR} != "no"
660	${TOOL_GPT} ${GPT_TIMESTAMP} ${WORKIMG} biosboot -i 2		\
661	    -c ${.OBJDIR}/${WORKDIR}/usr/mdec/gptmbr.bin
662.endif
663	${TOOL_GPT} ${GPT_TIMESTAMP} ${WORKIMG} set -a bootme -i 2
664.endif	# USE_GPT != "no"
665.if ${USE_SUNLABEL} != "no"
666	@echo Creating sun disklabel...
667	${TOOL_AWK} -f ${DISKPROTO2SUNLABEL} < ${WORKLABEL} | \
668	    ${TOOL_SUNLABEL} -nq ${WORKIMG}
669.endif
670.if ${USE_GPT} == "no"
671	${TOOL_DISKLABEL} -R -F ${WORKIMG} ${WORKLABEL}
672.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} != "no"
673	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKIMG} \
674	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT}
675.endif
676.endif
677	mv ${WORKIMG} ${.TARGET}
678
679CLEANFILES+=	${IMGBASE}.img.gz ${IMGBASE}.img.gz.tmp
680
681${IMGBASE}.img.gz:	${IMGBASE}.img
682	${TOOL_GZIP_N} -9c ${IMGBASE}.img > ${.TARGET}.tmp
683	mv ${.TARGET}.tmp ${.TARGET}
684
685clean:
686	@if [ -d ${WORKDIR}/var/spool/ftp/hidden ]; then 		\
687		${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden;		\
688	fi	# XXX
689	${RM} -fr ${WORKDIR}
690	${RM} -fr ${EFIWORKDIR}
691	${RM} -fr ${WORKFATDIR}
692	${RM} -fr ${WORKMDBOOTPARTDIR}
693
694prepare_md_post: .PHONY
695image_md_post: .PHONY
696image_md_pre: .PHONY
697
698.include <bsd.prog.mk>
699