1 # $NetBSD: Makefile.bootimage,v 1.11 2015/05/08 17:46:03 riastradh Exp $
3 # Copyright (c) 2009, 2010, 2011 Izumi Tsutsui. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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.
26 # Makefile to create a bootable FS image for USB flash or emulators
32 # Should be defined in nbmake-${MACHINE}
34 # Basename of the image
38 # device name of target bootable disk specified in /etc/fstab
41 # set yes if target disk image requires MBR partition
44 # optional MBR bootcode which should be installed by fdisk(8)
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
51 # set yes if target disk image requires Sun's label
53 # INSTALLBOOT_AFTER_DISKLABEL (untested)
54 # set yes if the target ${MACHINE} requires disklabel
55 # to run installboot(8), like hp300
58 # target image size in MB
61 # swap size in target image in MB
64 # kernel set name which should be extracted into image
65 # (default: kern-GENERIC)
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)
71 # directory path where binary sets are stored
72 # (default: ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets)
74 # list of additional files to be copied into images,
75 # containing one or more tuples of the form:
77 # for installation image etc.
80 # list of additional directories to be copied into images,
81 # containing one or more tuples of the form:
83 # for installation image etc.
85 # XXX: currently permissions in IMGDIR_EXTRA are not handled
87 # pax(1) options to exclude files which should not copied
88 # into TARGETPATH in IMGDIR_EXTRA
91 # template file of /etc/fstab
92 # (default: ${DISTRIBDIR}/common/bootimage/fstab.in)
94 # default files of spec file for makefs(8)
95 # (default: ${DISTRIBDIR}/common/bootimage/spec.in)
97 # additional files of spec file for makefs(8)
100 # options passed to makefs(8) to create root file system
101 # (default: -o bsize=16384,fsize=2048,density=8192)
103 # options passed to installboot(8), e.g., -o console=com0
106 # primary boot loader that should be installed into
107 # the target image via installboot(8)
110 # secondary bootloader that should be put into the target image
113 # extra arguments that should be passed to installboot(8)
114 # to specify the secondary bootloader
117 # template file of disklabel -R
118 # (default: ${DISTRIBDIR}/common/bootimage/diskproto.in
119 # or ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in)
121 # no need to put swap partition into image (for USB stick)
125 .include <bsd.own.mk> #
126 .include <bsd.endian.mk> # for TARGET_ENDIANNESS
128 .include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib"
132 @echo "Error: IMGBASE is not set"
136 # should be defined elsewhere?
145 # common definitions for image
150 INSTALLBOOT_AFTER_DISKLABEL?= no
153 # size parameters for image
155 IMAGEMB?= 2048 # 2048MB
158 # XXX: SWAPMB could be zero and expr(1) returns exit status 1 in that case
159 IMAGESECTORS!= expr ${IMAGEMB} \* 1024 \* 1024 / 512
160 SWAPSECTORS!= expr ${SWAPMB} \* 1024 \* 1024 / 512 || true
162 .if ${USE_MBR} == "no"
165 #LABELSECTORS?= 63 # historical
166 #LABELSECTORS?= 32 # 16KB aligned
167 LABELSECTORS?= 2048 # 1MB aligned for modern flash devices
170 FSSECTORS!= expr ${IMAGESECTORS} - ${SWAPSECTORS} - ${LABELSECTORS}
171 FSSIZE!= expr ${FSSECTORS} \* 512
173 # parameters for disklabel and MBR
176 CYLINDERS!= expr ${IMAGESECTORS} / \( ${HEADS} \* ${SECTORS} \)
177 SECPERCYLINDERS!= expr ${HEADS} \* ${SECTORS}
180 MBRCYLINDERS!= expr ${IMAGESECTORS} / \( ${MBRHEADS} \* ${MBRSECTORS} \)
183 BSDPARTSECTORS!= expr ${IMAGESECTORS} - ${LABELSECTORS}
184 FSOFFSET= ${LABELSECTORS}
185 SWAPOFFSET!= expr ${LABELSECTORS} + ${FSSECTORS}
187 # parameters for sunlabel
188 FSCYLINDERS!= expr ${FSSECTORS} / \( ${HEADS} \* ${SECTORS} \)
189 SWAPCYLINDERS!= expr ${SWAPSECTORS} / \( ${HEADS} \* ${SECTORS} \) || true
193 # definitions to create root fs
195 SETS_DEFAULT= modules base etc comp games man misc tests text
197 SETS_DEFAULT+= xbase xcomp xetc xfont xserver
200 KERN_SET?= kern-GENERIC
201 SETS?= ${SETS_DEFAULT}
202 IMG_SETS= ${KERN_SET} ${SETS}
203 SETS_DIR?= ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
205 FSTAB_IN?= ${DISTRIBDIR}/common/bootimage/fstab.in
206 SPEC_IN?= ${DISTRIBDIR}/common/bootimage/spec.in
208 IMGMAKEFSOPTIONS?= -o bsize=16384,fsize=2048,density=8192
212 WORKFSTAB?= work.fstab
213 WORKRCCONF?= work.rc.conf
215 TARGETFS?= imgroot.fs
217 CLEANFILES+= ${WORKSPEC} ${WORKFSTAB} ${WORKRCCONF} ${WORKFS}
218 CLEANFILES+= ${TARGETFS}
221 # create root file system for the image
223 ${TARGETFS}: prepare_md_post
224 @if [ ! -d ${RELEASEDIR}/${RELEASEMACHINEDIR} ]; then \
225 echo "Missing ${RELEASEDIR}/${RELEASEMACHINEDIR}, aborting"; \
229 .for set in ${IMG_SETS}
230 @if [ ! -f ${SETS_DIR}/${set}.tgz ]; then \
231 echo "Missing ${SETS_DIR}/${set}.tgz, aborting"; \
234 @echo Extracting ${set}.tgz ...
235 @(cd ${WORKDIR}; ${TOOL_PAX} -rnz -f ${SETS_DIR}/${set}.tgz .)
237 .if defined(SECONDARY_BOOT)
238 @echo Copying secondary boot...
239 ${CP} -f ${WORKDIR}/usr/mdec/${SECONDARY_BOOT} ${WORKDIR}
241 @echo Preparing /etc/fstab ...
242 ${TOOL_SED} "s/@@BOOTDISK@@/${BOOTDISK}/" < ${FSTAB_IN} > ${WORKFSTAB}
243 ${CP} ${WORKFSTAB} ${WORKDIR}/etc/fstab
244 @echo Setting rc_configured=YES in /etc/rc.conf ...
245 ${TOOL_SED} "s/rc_configured=NO/rc_configured=YES/" \
246 < ${WORKDIR}/etc/rc.conf > ${WORKRCCONF}
247 ${CP} ${WORKRCCONF} ${WORKDIR}/etc/rc.conf
248 .if defined(IMGDIR_EXTRA)
249 @echo Copying extra dirs...
250 .for _SRCDIR _TARGET in ${IMGDIR_EXTRA}
251 @if [ ! -d ${_SRCDIR} ]; then \
252 echo "${_SRCDIR} is not directory, aborting"; \
255 ${MKDIR} ${WORKDIR}/${_TARGET}
257 ${TOOL_PAX} -rw -pe -v \
259 . ${.OBJDIR}/${WORKDIR}/${_TARGET} )
262 .if defined(IMGFILE_EXTRA)
263 @echo Copying extra files...
264 .for _SRC _TARGET in ${IMGFILE_EXTRA}
265 @if [ ! -f ${_SRC} ]; then \
266 echo "${_SRC} in IMGFILE_EXTRA not found, aborting"; \
269 @if [ -f ${_SRC} ]; then \
270 echo ${CP} ${_SRC} ${WORKDIR}/${_TARGET}; \
271 ${CP} ${_SRC} ${WORKDIR}/${_TARGET}; \
275 @echo Preparing spec files for makefs...
277 cat ${WORKDIR}/etc/mtree/* | \
278 ${TOOL_SED} -e 's/ size=[0-9]*//' > ${WORKSPEC}
279 ${HOST_SH} ${WORKDIR}/dev/MAKEDEV -s all ipty | \
280 ${TOOL_SED} -e '/^\. type=dir/d' -e 's,^\.,./dev,' >> ${WORKSPEC}
281 cat ${SPEC_IN} >> ${WORKSPEC}
282 .if defined(SECONDARY_BOOT)
283 echo "./${SECONDARY_BOOT} type=file uname=root gname=wheel mode=0444" \
286 .if defined(SPEC_EXTRA)
287 cat ${SPEC_EXTRA} >> ${WORKSPEC}
289 @echo Creating rootfs...
290 # XXX /var/spool/ftp/hidden is unreadable
291 ${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden
292 ${TOOL_MAKEFS} -M ${FSSIZE} -m ${FSSIZE} \
293 -B ${TARGET_ENDIANNESS} \
294 -F ${WORKSPEC} -N ${WORKDIR}/etc \
295 ${IMGMAKEFSOPTIONS} \
297 .if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} == "no"
298 ${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKFS} \
299 ${WORKDIR}/usr/mdec/${PRIMARY_BOOT} ${SECONDARY_BOOT_ARG}
302 mv ${WORKFS} ${.TARGET}
305 # definitions to create image
307 .if ${USE_MBR} != "no"
308 DISKPROTO_IN?= ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in
310 DISKPROTO_IN?= ${DISTRIBDIR}/common/bootimage/diskproto.in
313 MBR_DEFAULT_BOOTCODE?= mbr
319 WORKLABEL?= work.diskproto
322 CLEANFILES+= ${WORKMBR} ${WORKSWAP}
323 CLEANFILES+= ${WORKLABEL}.tmp ${WORKLABEL}
324 CLEANFILES+= ${WORKIMG} ${IMGBASE}.img
328 -e "s/@@SECTORS@@/${SECTORS}/" \
329 -e "s/@@HEADS@@/${HEADS}/" \
330 -e "s/@@SECPERCYLINDERS@@/${SECPERCYLINDERS}/" \
331 -e "s/@@CYLINDERS@@/${CYLINDERS}/" \
332 -e "s/@@IMAGESECTORS@@/${IMAGESECTORS}/" \
333 -e "s/@@FSSECTORS@@/${FSSECTORS}/" \
334 -e "s/@@FSOFFSET@@/${FSOFFSET}/" \
335 -e "s/@@SWAPSECTORS@@/${SWAPSECTORS}/" \
336 -e "s/@@SWAPOFFSET@@/${SWAPOFFSET}/" \
337 -e "s/@@BSDPARTSECTORS@@/${BSDPARTSECTORS}/" \
338 < ${DISKPROTO_IN} > ${WORKLABEL}.tmp
339 mv ${WORKLABEL}.tmp ${WORKLABEL}
341 ${IMGBASE}.img: ${TARGETFS} ${WORKLABEL}
342 .if ${USE_MBR} != "no"
343 @echo creating MBR labels...
344 ${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
345 ${TOOL_FDISK} -f -i -u \
346 -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS} \
347 -0 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS} \
349 .if empty(MBR_BOOTCODE)
350 @if [ -f ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} ]; then \
351 echo ${TOOL_FDISK} -f \
352 -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} \
355 -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} \
359 @if [ ! -f ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} ]; then \
360 echo "${MBR_BOOTCODE} is not found in DESTDIR/usr/mdec, aborting"; \
363 ${TOOL_FDISK} -f -i -c ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} \
366 ${DD} if=${WORKMBR} count=${LABELSECTORS} | \
367 ${CAT} - ${TARGETFS} > ${WORKIMG}
369 ${CP} ${TARGETFS} ${WORKIMG}
371 .if ${OMIT_SWAPIMG} == "no"
372 ${DD} if=/dev/zero of=${WORKSWAP} seek=$$((${SWAPSECTORS} - 1)) count=1
373 ${CAT} ${WORKSWAP} >> ${WORKIMG}
375 .if ${USE_SUNLABEL} != "no"
376 @echo Creating sun disklabel...
377 printf 'V ncyl %d\nV nhead %d\nV nsect %d\na %d %d/0/0\nb %d %d/0/0\nW\n' \
378 ${CYLINDERS} ${HEADS} ${SECTORS} \
379 ${FSOFFSET} ${FSCYLINDERS} ${FSCYLINDERS} ${SWAPCYLINDERS} | \
380 ${TOOL_SUNLABEL} -nq ${WORKIMG}
382 ${TOOL_DISKLABEL} -R -F ${WORKIMG} ${WORKLABEL}
383 .if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} != "no"
384 ${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKIMG} \
385 ${WORKDIR}/usr/mdec/${PRIMARY_BOOT}
387 mv ${WORKIMG} ${.TARGET}
390 CLEANFILES+= ${IMGBASE}.img.gz ${IMGBASE}.img.gz.tmp
392 ${IMGBASE}.img.gz: ${IMGBASE}.img
393 ${TOOL_GZIP_N} -9c ${IMGBASE}.img > ${.TARGET}.tmp
394 mv ${.TARGET}.tmp ${.TARGET}
397 @if [ -d ${WORKDIR}/var/spool/ftp/hidden ]; then \
398 ${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden; \
402 prepare_md_post: .PHONY
403 image_md_post: .PHONY
406 .include <bsd.prog.mk>