xref: /netbsd-src/distrib/common/bootimage/Makefile.bootimage (revision c34236556bea94afcaca1782d7d228301edc3ea0)
1#	$NetBSD: Makefile.bootimage,v 1.12 2015/11/25 00:50:57 christos 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#	BOOTDISK
38#		device name of target bootable disk specified in /etc/fstab
39#		(default: sd0)
40#	USE_MBR
41#		set yes if target disk image requires MBR 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#	KERN_SET
64#		kernel set name which should be extracted into image
65#		(default: kern-GENERIC)
66#	SETS
67#		binary sets that should be extracted into image
68#		(default: modules base etc comp games man misc tests text
69#			  xbase xcomp xetc xfont xserver)
70#	SETS_DIR
71#		directory path where binary sets are stored
72#		(default: ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets)
73#	IMGFILE_EXTRA
74#		list of additional files to be copied into images,
75#		containing one or more tuples of the form:
76#			FILE	TARGETPATH
77#		for installation image etc.
78#		(default: empty)
79#	IMGDIR_EXTRA
80#		list of additional directories to be copied into images,
81#		containing one or more tuples of the form:
82#			DIR	TARGETPATH
83#		for installation image etc.
84#		(default: empty)
85#		XXX: currently permissions in IMGDIR_EXTRA are not handled
86#	IMGDIR_EXCLUDE
87#		pax(1) options to exclude files which should not copied
88#		into TARGETPATH in IMGDIR_EXTRA
89#		(default: empty)
90#	FSTAB_IN
91#		template file of /etc/fstab
92#		(default: ${DISTRIBDIR}/common/bootimage/fstab.in)
93#	SPEC_IN
94#		default files of spec file for makefs(8)
95#		(default: ${DISTRIBDIR}/common/bootimage/spec.in)
96#	SPEC_EXTRA
97#		additional files of spec file for makefs(8)
98#		(default: empty)
99#	IMGMAKEFSOPTIONS
100#		options passed to makefs(8) to create root file system
101#		(default: -o bsize=16384,fsize=2048,density=8192)
102#	INSTALLBOOTOPTIONS
103#		options passed to installboot(8), e.g., -o console=com0
104#		(default: empty)
105#	PRIMARY_BOOT
106#		primary boot loader that should be installed into
107#		the target image via installboot(8)
108#		(default: empty)
109#	SECONDARY_BOOT
110#		secondary bootloader that should be put into the target image
111#		(default: empty)
112#	SECONDARY_BOOT_ARG
113#		extra arguments that should be passed to installboot(8)
114#		to specify the secondary bootloader
115#		(default: empty)
116#	DISKPROTO_IN
117#		template file of disklabel -R
118#		(default: ${DISTRIBDIR}/common/bootimage/diskproto.in
119#		       or ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in)
120#	OMIT_SWAPIMG
121#		no need to put swap partition into image (for USB stick)
122#		(default: no)
123#
124
125.include <bsd.own.mk> 		#
126.include <bsd.endian.mk>	# for TARGET_ENDIANNESS
127
128.include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib"
129
130.if ${MKREPRO:Uno} == "yes"
131TIMESTAMP=-T ${NETBSDSRCDIR}/sys/sys/param.h
132.endif
133
134.if empty(IMGBASE)
135.BEGIN:
136	@echo "Error: IMGBASE is not set"
137	@false
138.endif
139
140# should be defined elsewhere?
141CAT?=		cat
142CHMOD?=		chmod
143CP?=		cp
144DD?=		dd
145MKDIR?=		mkdir -p
146RM?=		rm
147
148#
149# common definitions for image
150#
151BOOTDISK?=	sd0
152USE_MBR?=	no
153USE_SUNLABEL?=	no
154INSTALLBOOT_AFTER_DISKLABEL?=	no
155
156#
157# size parameters for image
158#
159IMAGEMB?=	2048		# 2048MB
160SWAPMB?=	128		# 128MB
161
162# XXX: SWAPMB could be zero and expr(1) returns exit status 1 in that case
163IMAGESECTORS!=	expr ${IMAGEMB} \* 1024 \* 1024 / 512
164SWAPSECTORS!=	expr ${SWAPMB} \* 1024 \* 1024 / 512 || true
165
166.if ${USE_MBR} == "no"
167LABELSECTORS?=	0
168.else
169#LABELSECTORS?=	63	# historical
170#LABELSECTORS?=	32	# 16KB aligned
171LABELSECTORS?=	2048	# 1MB aligned for modern flash devices
172.endif
173
174FSSECTORS!=	expr ${IMAGESECTORS} - ${SWAPSECTORS} - ${LABELSECTORS}
175FSSIZE!=	expr ${FSSECTORS} \* 512
176
177# parameters for disklabel and MBR
178HEADS=		64
179SECTORS=	32
180CYLINDERS!=	expr ${IMAGESECTORS} / \( ${HEADS} \* ${SECTORS} \)
181SECPERCYLINDERS!= expr ${HEADS} \* ${SECTORS}
182MBRHEADS=	255
183MBRSECTORS=	63
184MBRCYLINDERS!=	expr ${IMAGESECTORS} / \( ${MBRHEADS} \* ${MBRSECTORS} \)
185MBRNETBSD=	169
186
187BSDPARTSECTORS!= expr ${IMAGESECTORS} - ${LABELSECTORS}
188FSOFFSET=	${LABELSECTORS}
189SWAPOFFSET!=	expr ${LABELSECTORS} + ${FSSECTORS}
190
191# parameters for sunlabel
192FSCYLINDERS!=	expr ${FSSECTORS} / \( ${HEADS} \* ${SECTORS} \)
193SWAPCYLINDERS!=	expr ${SWAPSECTORS} / \( ${HEADS} \* ${SECTORS} \) || true
194
195
196#
197# definitions to create root fs
198#
199SETS_DEFAULT=	modules base etc comp games man misc tests text
200.if ${MKX11} != "no"
201SETS_DEFAULT+=	xbase xcomp xetc xfont xserver
202.endif
203
204KERN_SET?=	kern-GENERIC
205SETS?=		${SETS_DEFAULT}
206IMG_SETS=	${KERN_SET} ${SETS}
207SETS_DIR?=	${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
208
209FSTAB_IN?=	${DISTRIBDIR}/common/bootimage/fstab.in
210SPEC_IN?=	${DISTRIBDIR}/common/bootimage/spec.in
211
212IMGMAKEFSOPTIONS?=	-o bsize=16384,fsize=2048,density=8192
213
214WORKDIR?=       work
215WORKSPEC?=      work.spec
216WORKFSTAB?=	work.fstab
217WORKRCCONF?=	work.rc.conf
218WORKFS?=        work.rootfs
219TARGETFS?=      imgroot.fs
220
221CLEANFILES+=	${WORKSPEC} ${WORKFSTAB} ${WORKRCCONF} ${WORKFS}
222CLEANFILES+=	${TARGETFS}
223
224#
225# create root file system for the image
226#
227${TARGETFS}: prepare_md_post
228	@if [ ! -d ${RELEASEDIR}/${RELEASEMACHINEDIR} ]; then 		\
229		echo "Missing ${RELEASEDIR}/${RELEASEMACHINEDIR}, aborting"; \
230		false; 							\
231	fi;
232	@${MKDIR} ${WORKDIR}
233.for set in ${IMG_SETS}
234	@if [ ! -f ${SETS_DIR}/${set}.tgz ]; then 			\
235		echo "Missing ${SETS_DIR}/${set}.tgz, aborting";	\
236		false; 							\
237	fi
238	@echo Extracting ${set}.tgz ...
239	@(cd ${WORKDIR}; ${TOOL_PAX} -rnz -f ${SETS_DIR}/${set}.tgz .)
240.endfor
241.if defined(SECONDARY_BOOT)
242	@echo Copying secondary boot...
243	${CP} -f ${WORKDIR}/usr/mdec/${SECONDARY_BOOT} ${WORKDIR}
244.endif
245	@echo Preparing /etc/fstab ...
246	${TOOL_SED} "s/@@BOOTDISK@@/${BOOTDISK}/" < ${FSTAB_IN} > ${WORKFSTAB}
247	${CP} ${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	${CP} ${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} ${WORKDIR}/${_TARGET}
260	(cd ${_SRCDIR} ;						\
261	    ${TOOL_PAX} -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 ${CP} ${_SRC} ${WORKDIR}/${_TARGET};		\
275		${CP} ${_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	    ${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
374	${CP} ${TARGETFS} ${WORKIMG}
375.endif
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