Makefile.bootimage revision 1.25
1#	$NetBSD: Makefile.bootimage,v 1.25 2020/05/28 15:13:21 jmcneill 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 (untested)
54#		set yes if the target ${MACHINE} requires disklabel
55#		to run installboot(8), like hp300
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 man misc rescue tests
75#			  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#
139
140.include <bsd.own.mk> 		#
141.include <bsd.endian.mk>	# for TARGET_ENDIANNESS
142
143.include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib"
144
145.if ${MKREPRO_TIMESTAMP:Uno} != "no"
146MAKEFS_TIMESTAMP=-T "${MKREPRO_TIMESTAMP}"
147GPT_TIMESTAMP=-T "${MKREPRO_TIMESTAMP}"
148PAX_TIMESTAMP=--timestamp "${MKREPRO_TIMESTAMP}"
149.endif
150
151.if empty(IMGBASE)
152.BEGIN:
153	@echo "Error: IMGBASE is not set"
154	@false
155.endif
156
157# should be defined elsewhere? 
158CAT?=		cat
159CHMOD?=		chmod
160DD?=		dd
161MKDIR?=		mkdir -p
162RM?=		rm
163
164#
165# common definitions for image
166#
167USE_MBR?=	no
168USE_GPT?=	no
169USE_GPTMBR?=	no
170USE_SUNLABEL?=	no
171INSTALLBOOT_AFTER_DISKLABEL?=	no
172
173#
174# size parameters for image
175#
176IMAGEMB?=	2048		# 2048MB
177SWAPMB?=	128		# 128MB
178.if ${USE_GPT} == "no"
179EFIMB?=		0
180GPTSECTORS=	0
181.else
182EFIMB?=		128		# 128MB
183GPTSECTORS=	2048
184.endif
185FATMB?=		0
186
187# XXX: SWAPMB could be zero and expr(1) returns exit status 1 in that case
188IMAGESECTORS!=	expr ${IMAGEMB} \* 1024 \* 1024 / 512
189SWAPSECTORS!=	expr ${SWAPMB} \* 1024 \* 1024 / 512 || true
190EFISECTORS!=	expr ${EFIMB} \* 1024 \* 1024 / 512 || true
191FATSECTORS!=	expr ${FATMB} \* 1024 \* 1024 / 512 || true
192
193.if ${USE_MBR} == "no" && ${USE_GPT} == "no"
194LABELSECTORS?=	0
195.else
196#LABELSECTORS?=	63	# historical
197#LABELSECTORS?=	32	# 16KB aligned
198LABELSECTORS?=	2048	# 1MB aligned for modern flash devices
199.endif
200
201FSSECTORS!=	expr ${IMAGESECTORS} - ${SWAPSECTORS} - ${LABELSECTORS} \
202		    - ${EFISECTORS} - ${GPTSECTORS} - ${FATSECTORS}
203FSSIZE!=	expr ${FSSECTORS} \* 512
204
205# parameters for disklabel and MBR
206HEADS=		64
207SECTORS=	32
208CYLINDERS!=	expr ${IMAGESECTORS} / \( ${HEADS} \* ${SECTORS} \)
209SECPERCYLINDERS!= expr ${HEADS} \* ${SECTORS}
210MBRHEADS=	255
211MBRSECTORS=	63
212MBRCYLINDERS!=	expr ${IMAGESECTORS} / \( ${MBRHEADS} \* ${MBRSECTORS} \)
213MBRNETBSD=	169
214MBRFAT?=	6	# 16-bit FAT, more than 32M
215
216BSDPARTSECTORS!= expr ${IMAGESECTORS} - ${LABELSECTORS} \
217		    - ${EFISECTORS} - ${GPTSECTORS} - ${FATSECTORS}
218FSOFFSET!=	expr ${LABELSECTORS} + ${EFISECTORS} + ${FATSECTORS}
219SWAPOFFSET!=	expr ${LABELSECTORS} + ${FSSECTORS} \
220		    + ${EFISECTORS} + ${FATSECTORS}
221FATOFFSET=	${LABELSECTORS}
222
223# parameters for sunlabel
224FSCYLINDERS!=	expr ${FSSECTORS} / \( ${HEADS} \* ${SECTORS} \)
225SWAPCYLINDERS!=	expr ${SWAPSECTORS} / \( ${HEADS} \* ${SECTORS} \) || true
226
227.if ${USE_GPT} != "no"
228BOOTDISK_UUID=`${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} show -i 2 | ${TOOL_AWK} '/^GUID/ {print $$2}'`
229SWAPDISK_UUID=`${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} show -i 3 | ${TOOL_AWK} '/^GUID/ {print $$2}'`
230.endif
231
232#
233# definitions to create root fs
234#
235SETS_DEFAULT=	modules base etc comp games man misc rescue tests text
236.if ${MKX11} != "no"
237SETS_DEFAULT+=	xbase xcomp xetc xfont xserver
238.endif
239
240KERN_SET?=	kern-GENERIC
241SETS?=		${SETS_DEFAULT}
242IMG_SETS=	${KERN_SET} ${SETS}
243SETS_DIR?=	${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
244
245FSTAB_IN?=	${DISTRIBDIR}/common/bootimage/fstab.in
246SPEC_IN?=	${DISTRIBDIR}/common/bootimage/spec.in
247
248IMGMAKEFSOPTIONS?=	-o bsize=16384,fsize=2048,density=8192
249FATMAKEFSOPTIONS?=	-o fat_type=16
250
251WORKDIR?=       work
252WORKSPEC?=      work.spec
253WORKFSTAB?=	work.fstab
254WORKRCCONF?=	work.rc.conf
255WORKFS?=        work.rootfs
256TARGETFS?=      imgroot.fs
257COMPRESS_PROGRAM=${"${USE_XZ_SETS:Uno}"!="no":?${TOOL_XZ}:${TOOL_GZIP}}
258TAR_SUFF=	${"${USE_XZ_SETS:Uno}"!="no":?tar.xz:tgz}
259
260CLEANFILES+=	${WORKSPEC} ${WORKFSTAB} ${WORKRCCONF} ${WORKFS}
261CLEANFILES+=	${TARGETFS}
262
263#
264# definitions to create image
265#
266.if ${SWAPSECTORS} == 0
267.  if ${USE_MBR} != "no"
268DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.noswap.mbr.in
269.  else
270DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.noswap.in
271.  endif
272.else
273.  if ${USE_MBR} != "no"
274DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.mbr.in
275.  else
276DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.in
277.  endif
278.endif
279
280MBR_DEFAULT_BOOTCODE?=	mbr
281
282WORKMBR?=	work.mbr
283WORKMBRTRUNC?=	work.mbr.truncated
284WORKSWAP?=	work.swap
285WORKEFI?=	work.efi
286WORKGPT?=	work.gpt
287WORKLABEL?=	work.diskproto
288WORKIMG?=	work.img
289EFIWORKDIR?=	work.efidir
290WORKFAT?=	work.fat
291WORKFATDIR?=	work.fatdir
292
293.if ${USE_GPT} != "no"
294${WORKFSTAB}: ${WORKMBR}
295.endif
296
297${WORKFSTAB}:
298	@echo Preparing /etc/fstab ...
299.if ${USE_GPT} != "no"
300.  if ${SWAPSECTORS} != 0
301	${TOOL_SED}							\
302	    -e "s/ROOT.a/NAME=${BOOTDISK_UUID}/"			\
303	    -e "s/ROOT.b/NAME=${SWAPDISK_UUID}/"			\
304	    < ${FSTAB_IN} > ${WORKFSTAB}
305.  else
306	${TOOL_SED}							\
307	    -e "s/ROOT.a/NAME=${BOOTDISK_UUID}/"			\
308	    -e "/ROOT.b/d"						\
309	    < ${FSTAB_IN} > ${WORKFSTAB}
310.  endif
311.else
312.  if ${SWAPSECTORS} != 0
313	${TOOL_SED} < ${FSTAB_IN} > ${WORKFSTAB}
314.  else
315	${TOOL_SED}							\
316	    -e "/ROOT.b/d"						\
317	    < ${FSTAB_IN} > ${WORKFSTAB}
318.  endif
319.endif
320
321#
322# create root file system for the image
323#
324${TARGETFS}: prepare_md_post ${WORKFSTAB}
325	@if [ ! -d ${RELEASEDIR}/${RELEASEMACHINEDIR} ]; then 		\
326		echo "Missing ${RELEASEDIR}/${RELEASEMACHINEDIR}, aborting"; \
327		false; 							\
328	fi;
329	@${MKDIR} ${MKDIRPERM} ${WORKDIR}
330.for set in ${IMG_SETS}
331	@if [ ! -f ${SETS_DIR}/${set}.${TAR_SUFF} ]; then 		\
332		echo "Missing ${SETS_DIR}/${set}.${TAR_SUFF}, aborting";\
333		false; 							\
334	fi
335	@echo Extracting ${set}.${TAR_SUFF} ...
336	@(cd ${WORKDIR}; ${TOOL_PAX} ${PAX_TIMESTAMP} -rn \
337		--use-compress-program=${COMPRESS_PROGRAM:Q} \
338		-f ${SETS_DIR}/${set}.${TAR_SUFF} .)
339.endfor
340.if defined(SECONDARY_BOOT)
341	@echo Copying secondary boot...
342	${INSTALL} ${COPY} -m 0444 ${WORKDIR}/usr/mdec/${SECONDARY_BOOT} ${WORKDIR}
343.endif
344	${INSTALL} ${COPY} -m 0644 ${WORKFSTAB} ${WORKDIR}/etc/fstab
345	@echo Setting rc_configured=YES in /etc/rc.conf ...
346	${TOOL_SED} "s/rc_configured=NO/rc_configured=YES/"		\
347	    < ${WORKDIR}/etc/rc.conf > ${WORKRCCONF}
348	${INSTALL} ${COPY} -m 0644 ${WORKRCCONF} ${WORKDIR}/etc/rc.conf
349.if defined(IMGDIR_EXTRA)
350	@echo Copying extra dirs...
351.for _SRCDIR _TARGET in ${IMGDIR_EXTRA}
352	@if [ ! -d ${_SRCDIR} ]; then 					\
353		echo "${_SRCDIR} is not directory, aborting";		\
354		false; 							\
355	fi
356	${MKDIR} ${MKDIRPERM} ${WORKDIR}/${_TARGET}
357	(cd ${_SRCDIR} ;						\
358	    ${TOOL_PAX} ${PAX_TIMESTAMP} -rw -pe -v			\
359	    ${IMGDIR_EXCLUDE}						\
360	    . ${.OBJDIR}/${WORKDIR}/${_TARGET} )
361.endfor
362.endif
363.if defined(IMGFILE_EXTRA)
364	@echo Copying extra files...
365.for _SRC _TARGET in ${IMGFILE_EXTRA}
366	@if [ ! -f ${_SRC} ]; then 					\
367		echo "${_SRC} in IMGFILE_EXTRA not found, aborting";	\
368		false; 							\
369	fi
370	@if [ -f ${_SRC} ]; then 					\
371		echo ${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
372		${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
373	fi
374.endfor
375.endif
376	@echo Preparing spec files for makefs...
377	${RM} -f ${WORKSPEC}
378	cat ${WORKDIR}/etc/mtree/* |					\
379	    ${TOOL_SED} -e 's/ size=[0-9]*//' > ${WORKSPEC}
380	${HOST_SH} ${WORKDIR}/dev/MAKEDEV -s all ipty |			\
381	    ${TOOL_SED} -e '/^\. type=dir/d' -e 's,^\.,./dev,' >> ${WORKSPEC}
382	cat ${SPEC_IN} >> ${WORKSPEC}
383.if defined(SECONDARY_BOOT)
384	echo "./${SECONDARY_BOOT} type=file uname=root gname=wheel mode=0444" \
385	    >> ${WORKSPEC}
386.endif
387.if defined(SPEC_EXTRA)
388	cat ${SPEC_EXTRA} >> ${WORKSPEC}
389.endif
390	@echo Creating rootfs...
391	# XXX /var/spool/ftp/hidden is unreadable
392	${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden
393	${TOOL_MAKEFS} -M ${FSSIZE} -m ${FSSIZE}			\
394	    -B ${TARGET_ENDIANNESS}					\
395	    -F ${WORKSPEC} -N ${WORKDIR}/etc				\
396	    ${MAKEFS_TIMESTAMP}						\
397	    ${IMGMAKEFSOPTIONS}						\
398	    ${WORKFS} ${WORKDIR}
399.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} == "no"
400	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKFS} \
401	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT} ${SECONDARY_BOOT_ARG}
402.endif
403	@echo done.
404	mv ${WORKFS} ${.TARGET}
405
406TARGET_BLOCKS=
407.if ${USE_MBR} != "no" || ${USE_GPT} != "no"
408TARGET_BLOCKS+=	${WORKMBRTRUNC}
409.endif
410.if ${USE_GPT} != "no"
411TARGET_BLOCKS+=	${WORKEFI}
412.endif
413.if ${FATSECTORS} != 0
414TARGET_BLOCKS+=	${WORKFAT}
415.endif
416TARGET_BLOCKS+=	 ${TARGETFS}
417.if ${SWAPSECTORS} != 0
418TARGET_BLOCKS+=	${WORKSWAP}
419.endif
420.if ${USE_GPT} != "no"
421TARGET_BLOCKS+=	${WORKGPT}
422.endif
423
424CLEANFILES+=	${WORKMBR} ${WORKMBRTRUNC} ${WORKSWAP}
425CLEANFILES+=	${WORKEFI} ${WORKGPT}
426CLEANFILES+=	${WORKFAT}
427CLEANFILES+=	${WORKLABEL}.tmp ${WORKLABEL}
428CLEANFILES+=	${WORKIMG} ${IMGBASE}.img
429
430${WORKSWAP}:
431.if ${SWAPSECTORS} == 0
432	printf "" > ${WORKSWAP}
433.else
434	${DD} if=/dev/zero of=${WORKSWAP} seek=$$((${SWAPSECTORS} - 1)) count=1
435.endif
436
437${WORKMBRTRUNC}: ${WORKMBR}
438	${DD} if=${WORKMBR} count=${LABELSECTORS} of=${WORKMBRTRUNC}
439
440${WORKLABEL}:
441	${TOOL_SED}							\
442	    -e "s/@@SECTORS@@/${SECTORS}/"				\
443	    -e "s/@@HEADS@@/${HEADS}/"					\
444	    -e "s/@@SECPERCYLINDERS@@/${SECPERCYLINDERS}/"		\
445	    -e "s/@@CYLINDERS@@/${CYLINDERS}/"				\
446	    -e "s/@@IMAGESECTORS@@/${IMAGESECTORS}/"			\
447	    -e "s/@@FSSECTORS@@/${FSSECTORS}/"				\
448	    -e "s/@@FSOFFSET@@/${FSOFFSET}/"				\
449	    -e "s/@@SWAPSECTORS@@/${SWAPSECTORS}/"			\
450	    -e "s/@@SWAPOFFSET@@/${SWAPOFFSET}/"			\
451	    -e "s/@@BSDPARTSECTORS@@/${BSDPARTSECTORS}/"		\
452	    -e "s/@@FATSECTORS@@/${FATSECTORS}/"			\
453	    -e "s/@@FATOFFSET@@/${FATOFFSET}/"				\
454	    < ${DISKPROTO_IN} > ${WORKLABEL}.tmp
455	mv ${WORKLABEL}.tmp ${WORKLABEL}
456
457${WORKEFI}: ${TARGETFS}
458	@echo create EFI system partition...
459	@${MKDIR} ${MKDIRPERM} ${EFIWORKDIR}/EFI/boot
460.for f in ${EFIBOOT}
461	${INSTALL} ${COPY} -m 0644 ${f} ${EFIWORKDIR}/EFI/boot/`basename ${f}`
462.endfor
463	${RM} -f ${WORKEFI}
464	${TOOL_MAKEFS} -M ${EFIMB}m -m ${EFIMB}m			\
465	    -B ${TARGET_ENDIANNESS}					\
466	    ${MAKEFS_TIMESTAMP}						\
467	    -t msdos -o F=32,c=1					\
468	    ${WORKEFI} ${EFIWORKDIR}
469
470${WORKFAT}: ${TARGETFS} ${FATFILES}
471	@echo create FAT partition for bootstrap files...
472	@${MKDIR} ${MKDIRPERM} ${WORKFATDIR}
473.if defined(FATFILES)
474	@echo Copying files for FAT partition...
475.for f in ${FATFILES}
476	@if [ ! -f ${f} ]; then 					\
477		echo "${f} in FATFILES not found, aborting";		\
478		false; 							\
479	fi
480	${INSTALL} ${COPY} -m 0644 ${f} ${WORKFATDIR}
481.endfor
482.endif
483	${RM} -f ${WORKFAT}
484	${TOOL_MAKEFS} -M ${FATMB}m -m ${FATMB}m			\
485	    ${MAKEFS_TIMESTAMP}						\
486	    -t msdos							\
487	    ${FATMAKEFSOPTIONS}						\
488	    ${WORKFAT} ${WORKFATDIR}
489
490${WORKMBR}:
491.if ${USE_GPT} != "no"
492	@echo creating GPT header and partition entries...
493	${RM} -f ${WORKMBR}
494	${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
495	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} create
496	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${EFISECTORS} -t efi -l "EFI system"
497	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${FSSECTORS} -t ffs
498.  if ${SWAPSECTORS} != 0
499	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKMBR} add -a 1m -s ${SWAPSECTORS} -t swap
500.  endif
501.elif ${USE_MBR} != "no"
502	@echo creating MBR labels...
503	${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
504.  if ${FATSECTORS} != 0
505	# create BSD partition
506	${TOOL_FDISK} -f -i -u \
507	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
508	    -1 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS}		\
509	    -F ${WORKMBR}
510	# create FAT partition
511	${TOOL_FDISK} -f -u \
512	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
513	    -0 -s ${MBRFAT}/${FATOFFSET}/${FATSECTORS}			\
514	    -F ${WORKMBR}
515.  else
516	# create BSD partition
517	${TOOL_FDISK} -f -i -u \
518	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
519	    -0 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS}		\
520	    -F ${WORKMBR}
521.  endif
522.  if empty(MBR_BOOTCODE)
523	@if [ -f ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} ]; then 	\
524		echo ${TOOL_FDISK} -f					\
525		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
526		    -F ${WORKMBR};					\
527		${TOOL_FDISK} -f					\
528		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
529		    -F ${WORKMBR};					\
530	fi
531.  else
532	@if [ ! -f ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} ]; then 		\
533		echo "${MBR_BOOTCODE} is not found in DESTDIR/usr/mdec, aborting"; \
534		false; 							\
535	fi
536	${TOOL_FDISK} -f -i -c ${WORKDIR}/usr/mdec/${MBR_BOOTCODE}	\
537	    -F ${WORKMBR}
538.  endif
539.endif
540
541${WORKGPT}: ${WORKMBR}
542	@echo create GPT image...
543	${DD} if=${WORKMBR} of=${WORKGPT} skip=$$((${IMAGESECTORS} - 2048)) \
544	    count=2048
545
546.if ${USE_GPT} == "no"
547${IMGBASE}.img:	${WORKLABEL}
548.endif
549
550${IMGBASE}.img:	${TARGET_BLOCKS}
551	${CAT} ${TARGET_BLOCKS} > ${WORKIMG}
552.if ${USE_GPT} != "no"
553.if ${USE_GPTMBR} != "no"
554	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKIMG} biosboot -i 2		\
555	    -c ${.OBJDIR}/${WORKDIR}/usr/mdec/gptmbr.bin
556.endif
557	${TOOL_GPT} ${GPT_TIMESSTAMP} ${WORKIMG} set -a bootme -i 2
558.endif	# USE_GPT != "no"
559.if ${USE_SUNLABEL} != "no"
560	@echo Creating sun disklabel...
561	printf 'V ncyl %d\nV nhead %d\nV nsect %d\na %d %d/0/0\nb %d %d/0/0\nW\n' \
562	    ${CYLINDERS} ${HEADS} ${SECTORS} \
563	    ${FSOFFSET} ${FSCYLINDERS} ${FSCYLINDERS} ${SWAPCYLINDERS} | \
564	    ${TOOL_SUNLABEL} -nq ${WORKIMG}
565.endif
566.if ${USE_GPT} == "no"
567	${TOOL_DISKLABEL} -R -F ${WORKIMG} ${WORKLABEL}
568.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} != "no"
569	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKIMG} \
570	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT}
571.endif
572.endif
573	mv ${WORKIMG} ${.TARGET}
574
575CLEANFILES+=	${IMGBASE}.img.gz ${IMGBASE}.img.gz.tmp
576
577${IMGBASE}.img.gz:	${IMGBASE}.img
578	${TOOL_GZIP_N} -9c ${IMGBASE}.img > ${.TARGET}.tmp
579	mv ${.TARGET}.tmp ${.TARGET}
580
581clean:
582	@if [ -d ${WORKDIR}/var/spool/ftp/hidden ]; then 		\
583		${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden;		\
584	fi	# XXX
585	${RM} -fr ${WORKDIR}
586	${RM} -fr ${EFIWORKDIR}
587	${RM} -fr ${WORKFATDIR}
588
589prepare_md_post: .PHONY
590image_md_post: .PHONY
591image_md_pre: .PHONY
592
593.include <bsd.prog.mk>
594