1# $NetBSD: Makefile.bootimage,v 1.6 2012/04/22 07:23:42 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# 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.sys.mk> # for HOST_SH 126.include <bsd.own.mk> # 127.include <bsd.endian.mk> # for TARGET_ENDIANNESS 128 129.include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib" 130 131.if empty(IMGBASE) 132.BEGIN: 133 @echo "Error: IMGBASE is not set" 134 @false 135.endif 136 137# should be defined elsewhere? 138CAT?= cat 139CHMOD?= chmod 140CP?= cp 141DD?= dd 142MKDIR?= mkdir -p 143RM?= rm 144 145# 146# common definitions for image 147# 148BOOTDISK?= sd0 149USE_MBR?= no 150USE_SUNLABEL?= no 151INSTALLBOOT_AFTER_DISKLABEL?= no 152 153# 154# size parameters for image 155# 156IMAGEMB?= 2048 # 2048MB 157SWAPMB?= 128 # 128MB 158 159# XXX: SWAPMB could be zero and expr(1) returns exit status 1 in that case 160IMAGESECTORS!= expr ${IMAGEMB} \* 1024 \* 1024 / 512 161SWAPSECTORS!= expr ${SWAPMB} \* 1024 \* 1024 / 512 || true 162 163.if ${USE_MBR} == "no" 164LABELSECTORS?= 0 165.else 166#LABELSECTORS?= 63 # historical 167#LABELSECTORS?= 32 # 16KB aligned 168LABELSECTORS?= 2048 # 1MB aligned for modern flash devices 169.endif 170 171FSSECTORS!= expr ${IMAGESECTORS} - ${SWAPSECTORS} - ${LABELSECTORS} 172FSSIZE!= expr ${FSSECTORS} \* 512 173 174# parameters for disklabel and MBR 175HEADS= 64 176SECTORS= 32 177CYLINDERS!= expr ${IMAGESECTORS} / \( ${HEADS} \* ${SECTORS} \) 178SECPERCYLINDERS!= expr ${HEADS} \* ${SECTORS} 179MBRHEADS= 255 180MBRSECTORS= 63 181MBRCYLINDERS!= expr ${IMAGESECTORS} / \( ${MBRHEADS} \* ${MBRSECTORS} \) 182MBRNETBSD= 169 183 184BSDPARTSECTORS!= expr ${IMAGESECTORS} - ${LABELSECTORS} 185FSOFFSET= ${LABELSECTORS} 186SWAPOFFSET!= expr ${LABELSECTORS} + ${FSSECTORS} 187 188# parameters for sunlabel 189FSCYLINDERS!= expr ${FSSECTORS} / \( ${HEADS} \* ${SECTORS} \) 190SWAPCYLINDERS!= expr ${SWAPSECTORS} / \( ${HEADS} \* ${SECTORS} \) || true 191 192 193# 194# definitions to create root fs 195# 196SETS_DEFAULT= modules base etc comp games man misc tests text 197.if ${MKX11} != "no" 198SETS_DEFAULT+= xbase xcomp xetc xfont xserver 199.endif 200 201KERN_SET?= kern-GENERIC 202SETS?= ${SETS_DEFAULT} 203IMG_SETS= ${KERN_SET} ${SETS} 204SETS_DIR?= ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets 205 206FSTAB_IN?= ${DISTRIBDIR}/common/bootimage/fstab.in 207SPEC_IN?= ${DISTRIBDIR}/common/bootimage/spec.in 208 209IMGMAKEFSOPTIONS?= -o bsize=16384,fsize=2048,density=8192 210 211WORKDIR?= work 212WORKSPEC?= work.spec 213WORKFSTAB?= work.fstab 214WORKRCCONF?= work.rc.conf 215WORKFS?= work.rootfs 216TARGETFS?= imgroot.fs 217 218CLEANFILES+= ${WORKSPEC} ${WORKFSTAB} ${WORKRCCONF} ${WORKFS} 219CLEANFILES+= ${TARGETFS} 220 221# 222# create root file system for the image 223# 224${TARGETFS}: prepare_md_post 225 @if [ ! -d ${RELEASEDIR}/${RELEASEMACHINEDIR} ]; then \ 226 echo "Missing ${RELEASEDIR}/${RELEASEMACHINEDIR}, aborting"; \ 227 false; \ 228 fi; 229 @${MKDIR} ${WORKDIR} 230.for set in ${IMG_SETS} 231 @if [ ! -f ${SETS_DIR}/${set}.tgz ]; then \ 232 echo "Missing ${SETS_DIR}/${set}.tgz, aborting"; \ 233 false; \ 234 fi 235 @echo Extracting ${set}.tgz ... 236 @(cd ${WORKDIR}; ${TOOL_PAX} -rnz -f ${SETS_DIR}/${set}.tgz .) 237.endfor 238.if defined(SECONDARY_BOOT) 239 @echo Copying secondary boot... 240 ${CP} -f ${WORKDIR}/usr/mdec/${SECONDARY_BOOT} ${WORKDIR} 241.endif 242 @echo Preparing /etc/fstab ... 243 ${TOOL_SED} "s/@@BOOTDISK@@/${BOOTDISK}/" < ${FSTAB_IN} > ${WORKFSTAB} 244 ${CP} ${WORKFSTAB} ${WORKDIR}/etc/fstab 245 @echo Setting rc_configured=YES in /etc/rc.conf ... 246 ${TOOL_SED} "s/rc_configured=NO/rc_configured=YES/" \ 247 < ${WORKDIR}/etc/rc.conf > ${WORKRCCONF} 248 ${CP} ${WORKRCCONF} ${WORKDIR}/etc/rc.conf 249.if defined(IMGDIR_EXTRA) 250 @echo Copying extra dirs... 251.for _SRCDIR _TARGET in ${IMGDIR_EXTRA} 252 @if [ ! -d ${_SRCDIR} ]; then \ 253 echo "${_SRCDIR} is not directory, aborting"; \ 254 false; \ 255 fi 256 ${MKDIR} ${WORKDIR}/${_TARGET} 257 (cd ${_SRCDIR} ; \ 258 ${TOOL_PAX} -rw -pe -v \ 259 ${IMGDIR_EXCLUDE} \ 260 . ${.OBJDIR}/${WORKDIR}/${_TARGET} ) 261.endfor 262.endif 263.if defined(IMGFILE_EXTRA) 264 @echo Copying extra files... 265.for _SRC _TARGET in ${IMGFILE_EXTRA} 266 @if [ ! -f ${_SRC} ]; then \ 267 echo "${_SRC} in IMGFILE_EXTRA not found, aborting"; \ 268 false; \ 269 fi 270 @if [ -f ${_SRC} ]; then \ 271 echo ${CP} ${_SRC} ${WORKDIR}/${_TARGET}; \ 272 ${CP} ${_SRC} ${WORKDIR}/${_TARGET}; \ 273 fi 274.endfor 275.endif 276 @echo Preparing spec files for makefs... 277 ${RM} -f ${WORKSPEC} 278 cat ${WORKDIR}/etc/mtree/* | \ 279 ${TOOL_SED} -e 's/ size=[0-9]*//' > ${WORKSPEC} 280 ${HOST_SH} ${WORKDIR}/dev/MAKEDEV -s all | \ 281 ${TOOL_SED} -e '/^\. type=dir/d' -e 's,^\.,./dev,' >> ${WORKSPEC} 282 cat ${SPEC_IN} >> ${WORKSPEC} 283.if defined(SECONDARY_BOOT) 284 echo "./${SECONDARY_BOOT} type=file uname=root gname=wheel mode=0444" \ 285 >> ${WORKSPEC} 286.endif 287.if defined(SPEC_EXTRA) 288 cat ${SPEC_EXTRA} >> ${WORKSPEC} 289.endif 290 @echo Creating rootfs... 291 # XXX /var/spool/ftp/hidden is unreadable 292 ${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden 293 ${TOOL_MAKEFS} -M ${FSSIZE} -m ${FSSIZE} \ 294 -B ${TARGET_ENDIANNESS} \ 295 -F ${WORKSPEC} -N ${WORKDIR}/etc \ 296 ${IMGMAKEFSOPTIONS} \ 297 ${WORKFS} ${WORKDIR} 298.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} == "no" 299 ${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTIONS} ${WORKFS} \ 300 ${WORKDIR}/usr/mdec/${PRIMARY_BOOT} ${SECONDARY_BOOT_ARG} 301.endif 302 @echo done. 303 mv ${WORKFS} ${.TARGET} 304 305# 306# definitions to create image 307# 308.if ${USE_MBR} != "no" 309DISKPROTO_IN?= ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in 310.else 311DISKPROTO_IN?= ${DISTRIBDIR}/common/bootimage/diskproto.in 312.endif 313 314MBR_DEFAULT_BOOTCODE?= mbr 315 316OMIT_SWAPIMG?= no 317 318WORKMBR?= work.mbr 319WORKSWAP?= work.swap 320WORKLABEL?= work.diskproto 321WORKIMG?= work.img 322 323CLEANFILES+= ${WORKMBR} ${WORKSWAP} 324CLEANFILES+= ${WORKLABEL}.tmp ${WORKLABEL} 325CLEANFILES+= ${WORKIMG} ${IMGBASE}.img 326 327${WORKLABEL}: 328 ${TOOL_SED} \ 329 -e "s/@@SECTORS@@/${SECTORS}/" \ 330 -e "s/@@HEADS@@/${HEADS}/" \ 331 -e "s/@@SECPERCYLINDERS@@/${SECPERCYLINDERS}/" \ 332 -e "s/@@CYLINDERS@@/${CYLINDERS}/" \ 333 -e "s/@@IMAGESECTORS@@/${IMAGESECTORS}/" \ 334 -e "s/@@FSSECTORS@@/${FSSECTORS}/" \ 335 -e "s/@@FSOFFSET@@/${FSOFFSET}/" \ 336 -e "s/@@SWAPSECTORS@@/${SWAPSECTORS}/" \ 337 -e "s/@@SWAPOFFSET@@/${SWAPOFFSET}/" \ 338 -e "s/@@BSDPARTSECTORS@@/${BSDPARTSECTORS}/" \ 339 < ${DISKPROTO_IN} > ${WORKLABEL}.tmp 340 mv ${WORKLABEL}.tmp ${WORKLABEL} 341 342${IMGBASE}.img: ${TARGETFS} ${WORKLABEL} 343.if ${USE_MBR} != "no" 344 @echo creating MBR labels... 345 ${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1 346 ${TOOL_FDISK} -f -i -u \ 347 -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS} \ 348 -0 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS} \ 349 -F ${WORKMBR} 350.if empty(MBR_BOOTCODE) 351 @if [ -f ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} ]; then \ 352 echo ${TOOL_FDISK} -f \ 353 -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} \ 354 -F ${WORKMBR}; \ 355 ${TOOL_FDISK} -f \ 356 -i -c ${WORKDIR}/usr/mdec/${MBR_DEFAULT_BOOTCODE} \ 357 -F ${WORKMBR}; \ 358 fi 359.else 360 @if [ ! -f ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} ]; then \ 361 echo "${MBR_BOOTCODE} is not found in DESTDIR/usr/mdec, aborting"; \ 362 false; \ 363 fi 364 ${TOOL_FDISK} -f -i -c ${WORKDIR}/usr/mdec/${MBR_BOOTCODE} \ 365 -F ${WORKMBR} 366.endif 367 ${DD} if=${WORKMBR} count=${LABELSECTORS} | \ 368 ${CAT} - ${TARGETFS} > ${WORKIMG} 369.else 370 ${CP} ${TARGETFS} ${WORKIMG} 371.endif 372.if ${OMIT_SWAPIMG} == "no" 373 ${DD} if=/dev/zero of=${WORKSWAP} seek=$$((${SWAPSECTORS} - 1)) count=1 374 ${CAT} ${WORKSWAP} >> ${WORKIMG} 375.endif 376.if ${USE_SUNLABEL} != "no" 377 @echo Creating sun disklabel... 378 printf 'V ncyl %d\nV nhead %d\nV nsect %d\na %d %d/0/0\nb %d %d/0/0\nW\n' \ 379 ${CYLINDERS} ${HEADS} ${SECTORS} \ 380 ${FSOFFSET} ${FSCYLINDERS} ${FSCYLINDERS} ${SWAPCYLINDERS} | \ 381 ${TOOL_SUNLABEL} -nq ${WORKIMG} 382.endif 383 ${TOOL_DISKLABEL} -R -F ${WORKIMG} ${WORKLABEL} 384.if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} != "no" 385 ${TOOL_INSTALLBOOT} -v -m ${MACHINE} ${INSTALLBOOTOPTOINS} ${WORKIMG} \ 386 ${WORKDIR}/usr/mdec/${PRIMARY_BOOT} 387.endif 388 mv ${WORKIMG} ${.TARGET} 389 390 391CLEANFILES+= ${IMGBASE}.img.gz ${IMGBASE}.img.gz.tmp 392 393${IMGBASE}.img.gz: ${IMGBASE}.img 394 ${TOOL_GZIP} -9c ${IMGBASE}.img > ${.TARGET}.tmp 395 mv ${.TARGET}.tmp ${.TARGET} 396 397clean: 398 @if [ -d ${WORKDIR}/var/spool/ftp/hidden ]; then \ 399 ${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden; \ 400 fi # XXX 401 ${RM} -fr ${WORKDIR} 402 403prepare_md_post: .PHONY 404image_md_post: .PHONY 405image_md_pre: .PHONY 406 407.include <bsd.prog.mk> 408