Makefile.bootimage revision 1.9
1# $NetBSD: Makefile.bootimage,v 1.9 2014/08/05 15:40:58 apb 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 empty(IMGBASE) 131.BEGIN: 132 @echo "Error: IMGBASE is not set" 133 @false 134.endif 135 136# should be defined elsewhere? 137CAT?= cat 138CHMOD?= chmod 139CP?= cp 140DD?= dd 141MKDIR?= mkdir -p 142RM?= rm 143 144# 145# common definitions for image 146# 147BOOTDISK?= sd0 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 216 217CLEANFILES+= ${WORKSPEC} ${WORKFSTAB} ${WORKRCCONF} ${WORKFS} 218CLEANFILES+= ${TARGETFS} 219 220# 221# create root file system for the image 222# 223${TARGETFS}: prepare_md_post 224 @if [ ! -d ${RELEASEDIR}/${RELEASEMACHINEDIR} ]; then \ 225 echo "Missing ${RELEASEDIR}/${RELEASEMACHINEDIR}, aborting"; \ 226 false; \ 227 fi; 228 @${MKDIR} ${WORKDIR} 229.for set in ${IMG_SETS} 230 @if [ ! -f ${SETS_DIR}/${set}.tgz ]; then \ 231 echo "Missing ${SETS_DIR}/${set}.tgz, aborting"; \ 232 false; \ 233 fi 234 @echo Extracting ${set}.tgz ... 235 @(cd ${WORKDIR}; ${TOOL_PAX} -rnz -f ${SETS_DIR}/${set}.tgz .) 236.endfor 237.if defined(SECONDARY_BOOT) 238 @echo Copying secondary boot... 239 ${CP} -f ${WORKDIR}/usr/mdec/${SECONDARY_BOOT} ${WORKDIR} 240.endif 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"; \ 253 false; \ 254 fi 255 ${MKDIR} ${WORKDIR}/${_TARGET} 256 (cd ${_SRCDIR} ; \ 257 ${TOOL_PAX} -rw -pe -v \ 258 ${IMGDIR_EXCLUDE} \ 259 . ${.OBJDIR}/${WORKDIR}/${_TARGET} ) 260.endfor 261.endif 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"; \ 267 false; \ 268 fi 269 @if [ -f ${_SRC} ]; then \ 270 echo ${CP} ${_SRC} ${WORKDIR}/${_TARGET}; \ 271 ${CP} ${_SRC} ${WORKDIR}/${_TARGET}; \ 272 fi 273.endfor 274.endif 275 @echo Preparing spec files for makefs... 276 ${RM} -f ${WORKSPEC} 277 cat ${WORKDIR}/etc/mtree/* | \ 278 ${TOOL_SED} -e 's/ size=[0-9]*//' > ${WORKSPEC} 279 ${HOST_SH} ${WORKDIR}/dev/MAKEDEV -s all | \ 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" \ 284 >> ${WORKSPEC} 285.endif 286.if defined(SPEC_EXTRA) 287 cat ${SPEC_EXTRA} >> ${WORKSPEC} 288.endif 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} \ 296 ${WORKFS} ${WORKDIR} 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} 300.endif 301 @echo done. 302 mv ${WORKFS} ${.TARGET} 303 304# 305# definitions to create image 306# 307.if ${USE_MBR} != "no" 308DISKPROTO_IN?= ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in 309.else 310DISKPROTO_IN?= ${DISTRIBDIR}/common/bootimage/diskproto.in 311.endif 312 313MBR_DEFAULT_BOOTCODE?= mbr 314 315OMIT_SWAPIMG?= no 316 317WORKMBR?= work.mbr 318WORKSWAP?= work.swap 319WORKLABEL?= work.diskproto 320WORKIMG?= work.img 321 322CLEANFILES+= ${WORKMBR} ${WORKSWAP} 323CLEANFILES+= ${WORKLABEL}.tmp ${WORKLABEL} 324CLEANFILES+= ${WORKIMG} ${IMGBASE}.img 325 326${WORKLABEL}: 327 ${TOOL_SED} \ 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} 340 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} \ 348 -F ${WORKMBR} 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} \ 353 -F ${WORKMBR}; \ 354 ${TOOL_FDISK} -f \ 355 -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} \ 356 -F ${WORKMBR}; \ 357 fi 358.else 359 @if [ ! -f ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} ]; then \ 360 echo "${MBR_BOOTCODE} is not found in DESTDIR/usr/mdec, aborting"; \ 361 false; \ 362 fi 363 ${TOOL_FDISK} -f -i -c ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} \ 364 -F ${WORKMBR} 365.endif 366 ${DD} if=${WORKMBR} count=${LABELSECTORS} | \ 367 ${CAT} - ${TARGETFS} > ${WORKIMG} 368.else 369 ${CP} ${TARGETFS} ${WORKIMG} 370.endif 371.if ${OMIT_SWAPIMG} == "no" 372 ${DD} if=/dev/zero of=${WORKSWAP} seek=$$((${SWAPSECTORS} - 1)) count=1 373 ${CAT} ${WORKSWAP} >> ${WORKIMG} 374.endif 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} 381.endif 382 ${TOOL_DISKLABEL} -R -F ${WORKIMG} ${WORKLABEL} 383.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} != "no" 384 ${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTOINS} ${WORKIMG} \ 385 ${WORKDIR}/usr/mdec/${PRIMARY_BOOT} 386.endif 387 mv ${WORKIMG} ${.TARGET} 388 389 390CLEANFILES+= ${IMGBASE}.img.gz ${IMGBASE}.img.gz.tmp 391 392${IMGBASE}.img.gz: ${IMGBASE}.img 393 ${TOOL_GZIP_N} -9c ${IMGBASE}.img > ${.TARGET}.tmp 394 mv ${.TARGET}.tmp ${.TARGET} 395 396clean: 397 @if [ -d ${WORKDIR}/var/spool/ftp/hidden ]; then \ 398 ${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden; \ 399 fi # XXX 400 ${RM} -fr ${WORKDIR} 401 402prepare_md_post: .PHONY 403image_md_post: .PHONY 404image_md_pre: .PHONY 405 406.include <bsd.prog.mk> 407