Makefile.bootimage revision 1.21
1#	$NetBSD: Makefile.bootimage,v 1.21 2019/02/22 22:13:58 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#	MBR_BOOTCODE
41#		optional MBR bootcode which should be installed by fdisk(8)
42#		(default: empty)
43#		- specified MBR_BOOTCODE must exist in ${DESTDIR}/usr/mdec
44#		- if MBR_BOOTCODE is not specified,
45#		  MBR_DEFAULT_BOOTCODE (default: mbr) will be used
46#		  if the target ${MACHINE} has the one in /usr/mdec
47#	USE_SUNLABEL
48#		set yes if target disk image requires Sun's label
49#		(default: no)
50#	INSTALLBOOT_AFTER_DISKLABEL (untested)
51#		set yes if the target ${MACHINE} requires disklabel
52#		to run installboot(8), like hp300
53#		(default: empty)
54#	IMAGEMB
55#		target image size in MB
56#		(default: 2048)
57#	SWAPMB
58#		swap size in target image in MB
59#		(default: 128)
60#	KERN_SET
61#		kernel set name which should be extracted into image
62#		(default: kern-GENERIC)
63#	SETS
64#		binary sets that should be extracted into image
65#		(default: modules base etc comp games man misc tests text
66#			  xbase xcomp xetc xfont xserver)
67#	SETS_DIR
68#		directory path where binary sets are stored
69#		(default: ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets)
70#	IMGFILE_EXTRA
71#		list of additional files to be copied into images,
72#		containing one or more tuples of the form:
73#			FILE	TARGETPATH
74#		for installation image etc.
75#		(default: empty)
76#	IMGDIR_EXTRA
77#		list of additional directories to be copied into images,
78#		containing one or more tuples of the form:
79#			DIR	TARGETPATH
80#		for installation image etc.
81#		(default: empty)
82#		XXX: currently permissions in IMGDIR_EXTRA are not handled
83#	IMGDIR_EXCLUDE
84#		pax(1) options to exclude files which should not copied
85#		into TARGETPATH in IMGDIR_EXTRA
86#		(default: empty)
87#	FSTAB_IN
88#		template file of /etc/fstab
89#		(default: ${DISTRIBDIR}/common/bootimage/fstab.in)
90#	SPEC_IN
91#		default files of spec file for makefs(8)
92#		(default: ${DISTRIBDIR}/common/bootimage/spec.in)
93#	SPEC_EXTRA
94#		additional files of spec file for makefs(8)
95#		(default: empty)
96#	IMGMAKEFSOPTIONS
97#		options passed to makefs(8) to create root file system
98#		(default: -o bsize=16384,fsize=2048,density=8192)
99#	INSTALLBOOTOPTIONS
100#		options passed to installboot(8), e.g., -o console=com0
101#		(default: empty)
102#	PRIMARY_BOOT
103#		primary boot loader that should be installed into
104#		the target image via installboot(8)
105#		(default: empty)
106#	SECONDARY_BOOT
107#		secondary bootloader that should be put into the target image
108#		(default: empty)
109#	SECONDARY_BOOT_ARG
110#		extra arguments that should be passed to installboot(8)
111#		to specify the secondary bootloader
112#		(default: empty)
113#	DISKPROTO_IN
114#		template file of disklabel -R
115#		(default: ${DISTRIBDIR}/common/bootimage/diskproto.in
116#		       or ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in)
117#	OMIT_SWAPIMG
118#		no need to put swap partition into image (for USB stick)
119#		(default: no)
120#
121
122.include <bsd.own.mk> 		#
123.include <bsd.endian.mk>	# for TARGET_ENDIANNESS
124
125.include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib"
126
127.if ${MKREPRO_TIMESTAMP:Uno} != "no"
128MAKEFS_TIMESTAMP=-T "${MKREPRO_TIMESTAMP}"
129PAX_TIMESTAMP=--timestamp "${MKREPRO_TIMESTAMP}"
130.endif
131
132.if empty(IMGBASE)
133.BEGIN:
134	@echo "Error: IMGBASE is not set"
135	@false
136.endif
137
138# should be defined elsewhere? 
139CAT?=		cat
140CHMOD?=		chmod
141DD?=		dd
142MKDIR?=		mkdir -p
143RM?=		rm
144
145#
146# common definitions for image
147#
148USE_MBR?=	no
149USE_SUNLABEL?=	no
150INSTALLBOOT_AFTER_DISKLABEL?=	no
151
152#
153# size parameters for image
154#
155IMAGEMB?=	2048		# 2048MB
156SWAPMB?=	128		# 128MB
157
158# XXX: SWAPMB could be zero and expr(1) returns exit status 1 in that case
159IMAGESECTORS!=	expr ${IMAGEMB} \* 1024 \* 1024 / 512
160SWAPSECTORS!=	expr ${SWAPMB} \* 1024 \* 1024 / 512 || true
161
162.if ${USE_MBR} == "no"
163LABELSECTORS?=	0
164.else
165#LABELSECTORS?=	63	# historical
166#LABELSECTORS?=	32	# 16KB aligned
167LABELSECTORS?=	2048	# 1MB aligned for modern flash devices
168.endif
169
170FSSECTORS!=	expr ${IMAGESECTORS} - ${SWAPSECTORS} - ${LABELSECTORS}
171FSSIZE!=	expr ${FSSECTORS} \* 512
172
173# parameters for disklabel and MBR
174HEADS=		64
175SECTORS=	32
176CYLINDERS!=	expr ${IMAGESECTORS} / \( ${HEADS} \* ${SECTORS} \)
177SECPERCYLINDERS!= expr ${HEADS} \* ${SECTORS}
178MBRHEADS=	255
179MBRSECTORS=	63
180MBRCYLINDERS!=	expr ${IMAGESECTORS} / \( ${MBRHEADS} \* ${MBRSECTORS} \)
181MBRNETBSD=	169
182
183BSDPARTSECTORS!= expr ${IMAGESECTORS} - ${LABELSECTORS}
184FSOFFSET=	${LABELSECTORS}
185SWAPOFFSET!=	expr ${LABELSECTORS} + ${FSSECTORS}
186
187# parameters for sunlabel
188FSCYLINDERS!=	expr ${FSSECTORS} / \( ${HEADS} \* ${SECTORS} \)
189SWAPCYLINDERS!=	expr ${SWAPSECTORS} / \( ${HEADS} \* ${SECTORS} \) || true
190
191
192#
193# definitions to create root fs
194#
195SETS_DEFAULT=	modules base etc comp games man misc tests text
196.if ${MKX11} != "no"
197SETS_DEFAULT+=	xbase xcomp xetc xfont xserver
198.endif
199
200KERN_SET?=	kern-GENERIC
201SETS?=		${SETS_DEFAULT}
202IMG_SETS=	${KERN_SET} ${SETS}
203SETS_DIR?=	${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
204
205FSTAB_IN?=	${DISTRIBDIR}/common/bootimage/fstab.in
206SPEC_IN?=	${DISTRIBDIR}/common/bootimage/spec.in
207
208IMGMAKEFSOPTIONS?=	-o bsize=16384,fsize=2048,density=8192
209
210WORKDIR?=       work
211WORKSPEC?=      work.spec
212WORKFSTAB?=	work.fstab
213WORKRCCONF?=	work.rc.conf
214WORKFS?=        work.rootfs
215TARGETFS?=      imgroot.fs
216COMPRESS_PROGRAM=${"${USE_XZ_SETS:Uno}"!="no":?${TOOL_XZ}:${TOOL_GZIP}}
217TAR_SUFF=	${"${USE_XZ_SETS:Uno}"!="no":?tar.xz:tgz}
218
219CLEANFILES+=	${WORKSPEC} ${WORKFSTAB} ${WORKRCCONF} ${WORKFS}
220CLEANFILES+=	${TARGETFS}
221
222#
223# create root file system for the image
224#
225${TARGETFS}: prepare_md_post
226	@if [ ! -d ${RELEASEDIR}/${RELEASEMACHINEDIR} ]; then 		\
227		echo "Missing ${RELEASEDIR}/${RELEASEMACHINEDIR}, aborting"; \
228		false; 							\
229	fi;
230	@${MKDIR} ${MKDIRPERM} ${WORKDIR}
231.for set in ${IMG_SETS}
232	@if [ ! -f ${SETS_DIR}/${set}.${TAR_SUFF} ]; then 		\
233		echo "Missing ${SETS_DIR}/${set}.${TAR_SUFF}, aborting";\
234		false; 							\
235	fi
236	@echo Extracting ${set}.${TAR_SUFF} ...
237	@(cd ${WORKDIR}; ${TOOL_PAX} ${PAX_TIMESTAMP} -rn \
238		--use-compress-program=${COMPRESS_PROGRAM:Q} \
239		-f ${SETS_DIR}/${set}.${TAR_SUFF} .)
240.endfor
241.if defined(SECONDARY_BOOT)
242	@echo Copying secondary boot...
243	${INSTALL} ${COPY} -m 0444 ${WORKDIR}/usr/mdec/${SECONDARY_BOOT} ${WORKDIR}
244.endif
245	@echo Preparing /etc/fstab ...
246	${TOOL_SED} < ${FSTAB_IN} > ${WORKFSTAB}
247	${INSTALL} ${COPY} -m 0644 ${WORKFSTAB} ${WORKDIR}/etc/fstab
248	@echo Setting rc_configured=YES in /etc/rc.conf ...
249	${TOOL_SED} "s/rc_configured=NO/rc_configured=YES/"		\
250	    < ${WORKDIR}/etc/rc.conf > ${WORKRCCONF}
251	${INSTALL} ${COPY} -m 0644 ${WORKRCCONF} ${WORKDIR}/etc/rc.conf
252.if defined(IMGDIR_EXTRA)
253	@echo Copying extra dirs...
254.for _SRCDIR _TARGET in ${IMGDIR_EXTRA}
255	@if [ ! -d ${_SRCDIR} ]; then 					\
256		echo "${_SRCDIR} is not directory, aborting";		\
257		false; 							\
258	fi
259	${MKDIR} ${MKDIRPERM} ${WORKDIR}/${_TARGET}
260	(cd ${_SRCDIR} ;						\
261	    ${TOOL_PAX} ${PAX_TIMESTAMP} -rw -pe -v			\
262	    ${IMGDIR_EXCLUDE}						\
263	    . ${.OBJDIR}/${WORKDIR}/${_TARGET} )
264.endfor
265.endif
266.if defined(IMGFILE_EXTRA)
267	@echo Copying extra files...
268.for _SRC _TARGET in ${IMGFILE_EXTRA}
269	@if [ ! -f ${_SRC} ]; then 					\
270		echo "${_SRC} in IMGFILE_EXTRA not found, aborting";	\
271		false; 							\
272	fi
273	@if [ -f ${_SRC} ]; then 					\
274		echo ${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
275		${INSTALL} ${COPY} -m 0644 ${_SRC} ${WORKDIR}/${_TARGET}; \
276	fi
277.endfor
278.endif
279	@echo Preparing spec files for makefs...
280	${RM} -f ${WORKSPEC}
281	cat ${WORKDIR}/etc/mtree/* |					\
282	    ${TOOL_SED} -e 's/ size=[0-9]*//' > ${WORKSPEC}
283	${HOST_SH} ${WORKDIR}/dev/MAKEDEV -s all ipty |			\
284	    ${TOOL_SED} -e '/^\. type=dir/d' -e 's,^\.,./dev,' >> ${WORKSPEC}
285	cat ${SPEC_IN} >> ${WORKSPEC}
286.if defined(SECONDARY_BOOT)
287	echo "./${SECONDARY_BOOT} type=file uname=root gname=wheel mode=0444" \
288	    >> ${WORKSPEC}
289.endif
290.if defined(SPEC_EXTRA)
291	cat ${SPEC_EXTRA} >> ${WORKSPEC}
292.endif
293	@echo Creating rootfs...
294	# XXX /var/spool/ftp/hidden is unreadable
295	${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden
296	${TOOL_MAKEFS} -M ${FSSIZE} -m ${FSSIZE}			\
297	    -B ${TARGET_ENDIANNESS}					\
298	    -F ${WORKSPEC} -N ${WORKDIR}/etc				\
299	    ${MAKEFS_TIMESTAMP}						\
300	    ${IMGMAKEFSOPTIONS}						\
301	    ${WORKFS} ${WORKDIR}
302.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} == "no"
303	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKFS} \
304	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT} ${SECONDARY_BOOT_ARG}
305.endif
306	@echo done.
307	mv ${WORKFS} ${.TARGET}
308
309#
310# definitions to create image
311#
312.if ${USE_MBR} != "no"
313DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.mbr.in
314.else
315DISKPROTO_IN?=	${DISTRIBDIR}/common/bootimage/diskproto.in
316.endif
317
318MBR_DEFAULT_BOOTCODE?=	mbr
319
320OMIT_SWAPIMG?=	no
321
322WORKMBR?=	work.mbr
323WORKSWAP?=	work.swap
324WORKLABEL?=	work.diskproto
325WORKIMG?=	work.img
326
327CLEANFILES+=	${WORKMBR} ${WORKSWAP}
328CLEANFILES+=	${WORKLABEL}.tmp ${WORKLABEL}
329CLEANFILES+=	${WORKIMG} ${IMGBASE}.img
330
331${WORKLABEL}:
332	${TOOL_SED}							\
333	    -e "s/@@SECTORS@@/${SECTORS}/"				\
334	    -e "s/@@HEADS@@/${HEADS}/"					\
335	    -e "s/@@SECPERCYLINDERS@@/${SECPERCYLINDERS}/"		\
336	    -e "s/@@CYLINDERS@@/${CYLINDERS}/"				\
337	    -e "s/@@IMAGESECTORS@@/${IMAGESECTORS}/"			\
338	    -e "s/@@FSSECTORS@@/${FSSECTORS}/"				\
339	    -e "s/@@FSOFFSET@@/${FSOFFSET}/"				\
340	    -e "s/@@SWAPSECTORS@@/${SWAPSECTORS}/"			\
341	    -e "s/@@SWAPOFFSET@@/${SWAPOFFSET}/"			\
342	    -e "s/@@BSDPARTSECTORS@@/${BSDPARTSECTORS}/"		\
343	    < ${DISKPROTO_IN} > ${WORKLABEL}.tmp
344	mv ${WORKLABEL}.tmp ${WORKLABEL}
345
346${IMGBASE}.img:	${TARGETFS} ${WORKLABEL}
347.if ${USE_MBR} != "no"
348	@echo creating MBR labels...
349	${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
350	${TOOL_FDISK} -f -i -u \
351	    -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS}		\
352	    -0 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS}		\
353	    -F ${WORKMBR}
354.if empty(MBR_BOOTCODE)
355	@if [ -f ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} ]; then 	\
356		echo ${TOOL_FDISK} -f					\
357		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
358		    -F ${WORKMBR};					\
359		${TOOL_FDISK} -f					\
360		    -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE}	\
361		    -F ${WORKMBR};					\
362	fi
363.else
364	@if [ ! -f ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} ]; then 		\
365		echo "${MBR_BOOTCODE} is not found in DESTDIR/usr/mdec, aborting"; \
366		false; 							\
367	fi
368	${TOOL_FDISK} -f -i -c ${WORKDIR}/usr/mdec/${MBR_BOOTCODE}	\
369	    -F ${WORKMBR}
370.endif
371	${DD} if=${WORKMBR} count=${LABELSECTORS} |			\
372	    ${CAT} - ${TARGETFS} > ${WORKIMG}
373.else	# USE_MBR == "no"
374	${INSTALL} ${COPY} -m 0644 ${TARGETFS} ${WORKIMG}
375.endif	# USE_MBR != "no"
376.if ${OMIT_SWAPIMG} == "no"
377	${DD} if=/dev/zero of=${WORKSWAP} seek=$$((${SWAPSECTORS} - 1)) count=1
378	${CAT} ${WORKSWAP} >> ${WORKIMG}
379.endif
380.if ${USE_SUNLABEL} != "no"
381	@echo Creating sun disklabel...
382	printf 'V ncyl %d\nV nhead %d\nV nsect %d\na %d %d/0/0\nb %d %d/0/0\nW\n' \
383	    ${CYLINDERS} ${HEADS} ${SECTORS} \
384	    ${FSOFFSET} ${FSCYLINDERS} ${FSCYLINDERS} ${SWAPCYLINDERS} | \
385	    ${TOOL_SUNLABEL} -nq ${WORKIMG}
386.endif
387	${TOOL_DISKLABEL} -R -F ${WORKIMG} ${WORKLABEL}
388.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} != "no"
389	${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKIMG} \
390	    ${WORKDIR}/usr/mdec/${PRIMARY_BOOT}
391.endif
392	mv ${WORKIMG} ${.TARGET}
393
394
395CLEANFILES+=	${IMGBASE}.img.gz ${IMGBASE}.img.gz.tmp
396
397${IMGBASE}.img.gz:	${IMGBASE}.img
398	${TOOL_GZIP_N} -9c ${IMGBASE}.img > ${.TARGET}.tmp
399	mv ${.TARGET}.tmp ${.TARGET}
400
401clean:
402	@if [ -d ${WORKDIR}/var/spool/ftp/hidden ]; then 		\
403		${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden;		\
404	fi	# XXX
405	${RM} -fr ${WORKDIR}
406
407prepare_md_post: .PHONY
408image_md_post: .PHONY
409image_md_pre: .PHONY
410
411.include <bsd.prog.mk>
412