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