1#!/usr/bin/env bash 2set -e 3 4# 5# This script creates a bootable image and should at some point in the future 6# be replaced by makefs. 7# 8 9# 10# Source settings if present 11# 12: ${SETTINGS_MINIX=.settings} 13if [ -f "${SETTINGS_MINIX}" ] 14then 15 echo "Sourcing settings from ${SETTINGS_MINIX}" 16 # Display the content (so we can check in the build logs 17 # what the settings contain. 18 cat ${SETTINGS_MINIX} | sed "s,^,CONTENT ,g" 19 . ${SETTINGS_MINIX} 20fi 21 22: ${ARCH=evbearm-el} 23: ${OBJ=../obj.${ARCH}} 24: ${CROSS_TOOLS=${OBJ}/"tooldir.`uname -s`-`uname -r`-`uname -m`"/bin} 25: ${CROSS_PREFIX=${CROSS_TOOLS}/arm-elf32-minix-} 26: ${JOBS=1} 27: ${DESTDIR=${OBJ}/destdir.$ARCH} 28: ${RELEASETOOLSDIR=./releasetools/} 29: ${FSTAB=${DESTDIR}/etc/fstab} 30: ${BUILDVARS=-V MKGCCCMDS=yes -V MKLLVM=no} 31# These BUILDVARS are for building with LLVM: 32#: ${BUILDVARS=-V MKLIBCXX=no -V MKKYUA=no -V MKATF=no -V MKLLVMCMDS=no} 33: ${BUILDSH=build.sh} 34: ${CREATE_IMAGE_ONLY=0} 35: ${RC=minix_x86.rc} 36 37# Where the kernel & boot modules will be 38MODDIR=${DESTDIR}/boot/minix/.temp 39 40# 41# Directory where to store temporary file system images 42# 43: ${IMG_DIR=${OBJ}/img} 44: ${MLO=MLO} 45: ${UBOOT=u-boot.img} 46 47# Beagleboard-xm 48: ${U_BOOT_BIN_DIR=build/omap3_beagle/} 49: ${CONSOLE=tty02} 50 51# BeagleBone (and black) 52#: ${U_BOOT_BIN_DIR=build/am335x_evm/} 53#: ${CONSOLE=tty00} 54 55# 56# We host u-boot binaries. 57# 58U_BOOT_GIT_VERSION=cb5178f12787c690cb1c888d88733137e5a47b15 59 60# 61# All sized are written in 512 byte blocks 62# 63# we create a disk image of about 2 gig's 64# for alignment reasons, prefer sizes which are multiples of 4096 bytes 65# 66: ${IMG_SIZE=$(( 2*(2**30) / 512))} 67: ${FAT_SIZE=$(( 10*(2**20) / 512))} 68: ${ROOT_SIZE=$(( 64*(2**20) / 512))} 69: ${HOME_SIZE=$(( 128*(2**20) / 512))} 70: ${USR_SIZE=$(( 1792*(2**20) / 512))} 71 72# 73# Do some math to determine the start addresses of the partitions. 74# Don't leave holes so the 'partition' invocation later is easy. 75# 76FAT_START=2048 77ROOT_START=$(($FAT_START + $FAT_SIZE)) 78USR_START=$(($ROOT_START + $ROOT_SIZE)) 79HOME_START=$(($USR_START + $USR_SIZE)) 80 81case $(uname -s) in 82Darwin) 83 MKFS_VFAT_CMD=newfs_msdos 84 MKFS_VFAT_OPTS="-h 64 -u 32 -S 512 -s ${FAT_SIZE} -o 0" 85;; 86FreeBSD) 87 MKFS_VFAT_CMD=newfs_msdos 88 MKFS_VFAT_OPTS= 89;; 90*) 91 MKFS_VFAT_CMD=mkfs.vfat 92 MKFS_VFAT_OPTS= 93;; 94esac 95 96if [ -n "$BASE_URL" ] 97then 98 #we no longer download u-boot but do a checkout 99 #BASE_URL used to be the base url for u-boot 100 #Downloads 101 echo "Warning:** Setting BASE_URL (u-boot) is no longer possible use U_BOOT_BIN_DIR" 102 echo "Look in ${RELEASETOOLSDIR}/arm_sdimage.sh for suggested values" 103 exit 1 104fi 105 106if [ ! -f ${BUILDSH} ] 107then 108 echo "Please invoke me from the root source dir, where ${BUILDSH} is." 109 exit 1 110fi 111 112export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:${PATH} 113 114for needed in mcopy dd ${MKFS_VFAT_CMD} git 115do 116 if ! which $needed 2>&1 > /dev/null 117 then 118 echo "**Skipping image creation: missing tool '$needed'" 119 exit 1 120 fi 121done 122 123: ${IMG=minix_arm_sd.img} 124 125# 126# Are we going to build the minix sources? 127# 128 129if [ ${CREATE_IMAGE_ONLY} -eq 1 ] 130then 131 if [ ! -d ${DESTDIR} ] 132 then 133 echo "Minix source code does'nt appear to have been built." 134 echo "Please try with \$CREATE_IMAGE_ONLY set to 0." 135 exit 1 136 fi 137fi 138 139# 140# Artifacts from this script are stored in the IMG_DIR 141# 142rm -rf ${IMG_DIR} 143 144if [ -f ${IMG} ] # IMG might be a block device 145then rm -f ${IMG} 146fi 147 148mkdir -p ${IMG_DIR} 149 150# 151# Download the stage 1 bootloader and u-boot 152# 153${RELEASETOOLSDIR}/fetch_u-boot.sh -o ${RELEASETOOLSDIR}/u-boot -n $U_BOOT_GIT_VERSION 154cp ${RELEASETOOLSDIR}/u-boot/${U_BOOT_BIN_DIR}/u-boot.img ${IMG_DIR}/ 155cp ${RELEASETOOLSDIR}/u-boot/${U_BOOT_BIN_DIR}/MLO ${IMG_DIR}/ 156 157if [ ${CREATE_IMAGE_ONLY} -eq 0 ] 158then 159 echo "Going to build Minix source code..." 160 # 161 # Remove the generated files to allow us call build.sh without '-V SLOPPY_FLIST=yes'. 162 # 163 rm -f ${FSTAB} 164 165 # 166 # Now start the build. 167 # 168 sh ${BUILDSH} -j ${JOBS} -m ${ARCH} -O ${OBJ} -D ${DESTDIR} ${BUILDVARS} -U -u distribution 169 170fi 171 172# 173# create a fstab entry in /etc this is normally done during the 174# setup phase on x86 175# 176cat >${FSTAB} <<END_FSTAB 177/dev/c0d0p2 /usr mfs rw 0 2 178/dev/c0d0p3 /home mfs rw 0 2 179none /sys devman rw,rslabel=devman 0 0 180END_FSTAB 181 182rm -f ${DESTDIR}/SETS.* 183 184${CROSS_TOOLS}/nbpwd_mkdb -V 0 -p -d ${DESTDIR} ${DESTDIR}/etc/master.passwd 185 186# 187# make the different file system. this part is *also* hacky. We first convert 188# the METALOG.sanitised using mtree into a input METALOG containing uids and 189# gids. 190# After that we do some magic processing to add device nodes (also missing from METALOG) 191# and convert the METALOG into a proto file that can be used by mkfs.mfs 192# 193echo "Creating the file systems" 194 195# 196# read METALOG and use mtree to convert the user and group names into uid and gids 197# FIX put "input somewhere clean" 198# 199cat ${DESTDIR}/METALOG.sanitised | ${CROSS_TOOLS}/nbmtree -N ${DESTDIR}/etc -C -K device > ${IMG_DIR}/input 200 201# add rc (if any) 202if [ -f ${RC} ]; then 203 cp ${RC} ${DESTDIR}/usr/etc/rc.local 204 echo "./usr/etc/rc.local type=file uid=0 gid=0 mode=0644" >> ${IMG_DIR}/input 205fi 206 207# add fstab 208echo "./etc/fstab type=file uid=0 gid=0 mode=0755 size=747 time=1365060731.000000000" >> ${IMG_DIR}/input 209 210# fill root.img (skipping /usr entries while keeping the /usr directory) 211cat ${IMG_DIR}/input | grep -v "^./usr/" | ${CROSS_TOOLS}/nbtoproto -b ${DESTDIR} -o ${IMG_DIR}/root.proto 212 213# 214# Create proto files for /usr and /home using toproto. 215# 216cat ${IMG_DIR}/input | grep "^\./usr/\|^. " | sed "s,\./usr,\.,g" | ${CROSS_TOOLS}/nbtoproto -b ${DESTDIR}/usr -o ${IMG_DIR}/usr.proto 217cat ${IMG_DIR}/input | grep "^\./home/\|^. " | sed "s,\./home,\.,g" | ${CROSS_TOOLS}/nbtoproto -b ${DESTDIR}/home -o ${IMG_DIR}/home.proto 218 219# 220# Create the FAT partition, which contains the bootloader files, kernel and modules 221# 222dd if=/dev/zero of=${IMG_DIR}/fat.img bs=512 count=1 seek=$(($FAT_SIZE -1)) 2>/dev/null 223 224# 225# Format the fat partition and put the bootloaders 226# uEnv and the kernel command line in the FAT partition 227# 228${MKFS_VFAT_CMD} ${MKFS_VFAT_OPTS} ${IMG_DIR}/fat.img 229 230# 231# Create a uEnv.txt file 232# -n default to network boot 233# -p add a prefix to the network booted files (e.g. xm/" 234# -c set console e.g. tty02 or tty00 235# -v set verbosity e.g. 0 to 3 236#${RELEASETOOLSDIR}/gen_uEnv.txt.sh -c ${CONSOLE} -n -p bb/ > ${IMG_DIR}/uEnv.txt 237${RELEASETOOLSDIR}/gen_uEnv.txt.sh -c ${CONSOLE} > ${IMG_DIR}/uEnv.txt 238 239echo "Copying configuration kernel and boot modules" 240mcopy -bsp -i ${IMG_DIR}/fat.img ${IMG_DIR}/$MLO ::MLO 241mcopy -bsp -i ${IMG_DIR}/fat.img ${IMG_DIR}/$UBOOT ::u-boot.img 242mcopy -bsp -i ${IMG_DIR}/fat.img ${IMG_DIR}/uEnv.txt ::uEnv.txt 243 244# 245# Do some last processing of the kernel and servers and then put them on the FAT 246# partition. 247# 248${CROSS_PREFIX}objcopy ${OBJ}/minix/kernel/kernel -O binary ${OBJ}/kernel.bin 249 250mcopy -bsp -i ${IMG_DIR}/fat.img ${OBJ}/kernel.bin ::kernel.bin 251 252for f in servers/vm/vm servers/rs/rs servers/pm/pm servers/sched/sched \ 253 servers/vfs/vfs servers/ds/ds fs/mfs/mfs fs/pfs/pfs \ 254 ../sbin/init/init 255do 256 fn=`basename $f`.elf 257 cp ${OBJ}/minix/${f} ${OBJ}/${fn} 258 ${CROSS_PREFIX}strip -s ${OBJ}/${fn} 259 mcopy -bsp -i ${IMG_DIR}/fat.img ${OBJ}/${fn} ::${fn} 260done 261 262for f in tty/tty/tty storage/memory/memory 263do 264 fn=`basename $f`.elf 265 cp ${OBJ}/minix/drivers/${f} ${OBJ}/${fn} 266 ${CROSS_PREFIX}strip -s ${OBJ}/${fn} 267 mcopy -bsp -i ${IMG_DIR}/fat.img ${OBJ}/${fn} ::${fn} 268done 269 270# 271# For tftp booting 272# 273cp ${IMG_DIR}/uEnv.txt ${OBJ}/ 274 275# 276# Create the empty image where we later will put the partitions in. 277# Make sure it is at least 2GB, otherwise the SD card will not be detected 278# correctly in qemu / HW. 279# 280dd if=/dev/zero of=${IMG} bs=512 count=1 seek=$(($IMG_SIZE -1)) 281 282 283# 284# Generate /root, /usr and /home partition images. 285# 286echo "Writing Minix filesystem images" 287echo " - ROOT" 288_ROOT_SIZE=$((`${CROSS_TOOLS}/nbmkfs.mfs -I $((${ROOT_START} * 512)) -b $((${ROOT_SIZE} / 8)) ${IMG} ${IMG_DIR}/root.proto`/512)) 289echo " - USR" 290_USR_SIZE=$((`${CROSS_TOOLS}/nbmkfs.mfs -I $((${USR_START} * 512)) -b $((${USR_SIZE} / 8)) ${IMG} ${IMG_DIR}/usr.proto`/512)) 291echo " - HOME" 292_HOME_SIZE=$((`${CROSS_TOOLS}/nbmkfs.mfs -I $((${HOME_START} * 512)) -b $((${HOME_SIZE} / 8)) ${IMG} ${IMG_DIR}/home.proto`/512)) 293 294# 295# Write the partition table using the natively compiled 296# minix partition utility 297# 298${CROSS_TOOLS}/nbpartition -f -m ${IMG} ${FAT_START} "c:${FAT_SIZE}*" \ 299 81:${_ROOT_SIZE} 81:${_USR_SIZE} 81:${_HOME_SIZE} 300 301# 302# Merge the partitions into a single image. 303# 304echo "Merging file systems" 305dd if=${IMG_DIR}/fat.img of=${IMG} seek=$FAT_START conv=notrunc 306