Fix OPSYS constant in pkg_install tools
[minix3.git] / releasetools / arm_sdimage.sh
blob51578628e56f6afb5a43fd2628d4c820d4c30f75
1 #!/usr/bin/env bash
2 set -e
5 # This script creates a bootable image and should at some point in the future
6 # be replaced by the proper NetBSD infrastructure.
10 # Source settings if present
12 : ${SETTINGS_MINIX=.settings}
13 if [ -f "${SETTINGS_MINIX}" ]
14 then
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}
22 : ${ARCH=evbearm-el}
23 : ${OBJ=../obj.${ARCH}}
24 : ${TOOLCHAIN_TRIPLET=arm-elf32-minix-}
25 : ${BUILDSH=build.sh}
27 : ${SETS="minix-base minix-comp minix-games minix-man minix-tests tests"}
28 : ${IMG=minix_arm_sd.img}
30 # ARM definitions:
31 : ${BUILDVARS=-V MKGCCCMDS=yes -V MKLLVM=no}
32 # These BUILDVARS are for building with LLVM:
33 #: ${BUILDVARS=-V MKLIBCXX=no -V MKKYUA=no -V MKATF=no -V MKLLVMCMDS=no}
34 : ${FAT_SIZE=$(( 10*(2**20) / 512))} # This is in sectors
36 # Beagleboard-xm
37 : ${U_BOOT_BIN_DIR=build/omap3_beagle/}
38 : ${CONSOLE=tty02}
40 # BeagleBone (and black)
41 #: ${U_BOOT_BIN_DIR=build/am335x_evm/}
42 #: ${CONSOLE=tty00}
45 # We host u-boot binaries.
47 : ${MLO=MLO}
48 : ${UBOOT=u-boot.img}
49 U_BOOT_GIT_VERSION=cb5178f12787c690cb1c888d88733137e5a47b15
51 if [ ! -f ${BUILDSH} ]
52 then
53 echo "Please invoke me from the root source dir, where ${BUILDSH} is."
54 exit 1
57 if [ -n "$BASE_URL" ]
58 then
59 #we no longer download u-boot but do a checkout
60 #BASE_URL used to be the base url for u-boot
61 #Downloads
62 echo "Warning:** Setting BASE_URL (u-boot) is no longer possible use U_BOOT_BIN_DIR"
63 echo "Look in ${RELEASETOOLSDIR}/arm_sdimage.sh for suggested values"
64 exit 1
67 case $(uname -s) in
68 Darwin)
69 MKFS_VFAT_CMD=newfs_msdos
70 MKFS_VFAT_OPTS="-h 64 -u 32 -S 512 -s ${FAT_SIZE} -o 0"
72 FreeBSD)
73 MKFS_VFAT_CMD=newfs_msdos
74 MKFS_VFAT_OPTS=
77 MKFS_VFAT_CMD=mkfs.vfat
78 MKFS_VFAT_OPTS=
80 esac
82 export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:${PATH}
84 for needed in mcopy dd ${MKFS_VFAT_CMD} git
86 if ! which $needed 2>&1 > /dev/null
87 then
88 echo "**Skipping image creation: missing tool '$needed'"
89 exit 1
91 done
93 # we create a disk image of about 2 gig's
94 # for alignment reasons, prefer sizes which are multiples of 4096 bytes
95 : ${IMG_SIZE=$(( 2*(2**30) ))}
96 : ${ROOT_SIZE=$(( 64*(2**20) ))}
97 : ${HOME_SIZE=$(( 128*(2**20) ))}
98 : ${USR_SIZE=$(( 1792*(2**20) ))}
100 # set up disk creation environment
101 . releasetools/image.defaults
102 . releasetools/image.functions
104 # all sizes are written in 512 byte blocks
105 ROOTSIZEARG="-b $((${ROOT_SIZE} / 512 / 8))"
106 USRSIZEARG="-b $((${USR_SIZE} / 512 / 8))"
107 HOMESIZEARG="-b $((${HOME_SIZE} / 512 / 8))"
109 # where the kernel & boot modules will be
110 MODDIR=${DESTDIR}/boot/minix/.temp
112 echo "Building work directory..."
113 build_workdir "$SETS"
115 echo "Adding extra files..."
117 # create a fstab entry in /etc
118 cat >${ROOT_DIR}/etc/fstab <<END_FSTAB
119 /dev/c0d0p2 /usr mfs rw 0 2
120 /dev/c0d0p3 /home mfs rw 0 2
121 none /sys devman rw,rslabel=devman 0 0
122 none /dev/pts ptyfs rw,rslabel=ptyfs 0 0
123 END_FSTAB
124 add_file_spec "etc/fstab" extra.fstab
126 echo "Bundling packages..."
127 bundle_packages "$BUNDLE_PACKAGES"
129 echo "Creating specification files..."
130 create_input_spec
131 create_protos "usr home"
134 # Create the FAT partition, which contains the bootloader files, kernel and modules
136 dd if=/dev/zero of=${WORK_DIR}/fat.img bs=512 count=1 seek=$(($FAT_SIZE -1)) 2>/dev/null
139 # Format the fat partition and put the bootloaders
140 # uEnv and the kernel command line in the FAT partition
142 ${MKFS_VFAT_CMD} ${MKFS_VFAT_OPTS} ${WORK_DIR}/fat.img
145 # Download the stage 1 bootloader and u-boot
147 ${RELEASETOOLSDIR}/fetch_u-boot.sh -o ${RELEASETOOLSDIR}/u-boot -n $U_BOOT_GIT_VERSION
148 cp ${RELEASETOOLSDIR}/u-boot/${U_BOOT_BIN_DIR}/MLO ${WORK_DIR}/
149 cp ${RELEASETOOLSDIR}/u-boot/${U_BOOT_BIN_DIR}/u-boot.img ${WORK_DIR}/
152 # Create a uEnv.txt file
153 # -n default to network boot
154 # -p add a prefix to the network booted files (e.g. xm/"
155 # -c set console e.g. tty02 or tty00
156 # -v set verbosity e.g. 0 to 3
157 #${RELEASETOOLSDIR}/gen_uEnv.txt.sh -c ${CONSOLE} -n -p bb/ > ${WORK_DIR}/uEnv.txt
158 ${RELEASETOOLSDIR}/gen_uEnv.txt.sh -c ${CONSOLE} > ${WORK_DIR}/uEnv.txt
160 echo "Copying configuration kernel and boot modules"
161 mcopy -bsp -i ${WORK_DIR}/fat.img ${WORK_DIR}/$MLO ::MLO
162 mcopy -bsp -i ${WORK_DIR}/fat.img ${WORK_DIR}/$UBOOT ::u-boot.img
163 mcopy -bsp -i ${WORK_DIR}/fat.img ${WORK_DIR}/uEnv.txt ::uEnv.txt
166 # Do some last processing of the kernel and servers and then put them on the FAT
167 # partition.
169 ${CROSS_PREFIX}objcopy ${OBJ}/minix/kernel/kernel -O binary ${OBJ}/kernel.bin
170 mcopy -bsp -i ${WORK_DIR}/fat.img ${OBJ}/kernel.bin ::kernel.bin
172 for f in servers/vm/vm servers/rs/rs servers/pm/pm servers/sched/sched \
173 servers/vfs/vfs servers/ds/ds servers/mib/mib fs/pfs/pfs fs/mfs/mfs \
174 ../sbin/init/init
176 fn=`basename $f`.elf
177 cp ${OBJ}/minix/${f} ${OBJ}/${fn}
178 ${CROSS_PREFIX}strip -s ${OBJ}/${fn}
179 mcopy -bsp -i ${WORK_DIR}/fat.img ${OBJ}/${fn} ::${fn}
180 done
182 for f in tty/tty/tty storage/memory/memory
184 fn=`basename $f`.elf
185 cp ${OBJ}/minix/drivers/${f} ${OBJ}/${fn}
186 ${CROSS_PREFIX}strip -s ${OBJ}/${fn}
187 mcopy -bsp -i ${WORK_DIR}/fat.img ${OBJ}/${fn} ::${fn}
188 done
191 # For tftp booting
193 cp ${WORK_DIR}/uEnv.txt ${OBJ}/
195 # Clean image
196 if [ -f ${IMG} ] # IMG might be a block device
197 then
198 rm -f ${IMG}
202 # Create the empty image where we later will put the partitions in.
203 # Make sure it is at least 2GB, otherwise the SD card will not be detected
204 # correctly in qemu / HW.
206 dd if=/dev/zero of=${IMG} bs=512 count=1 seek=$((($IMG_SIZE / 512) -1))
209 # Generate /root, /usr and /home partition images.
211 echo "Writing disk image..."
212 FAT_START=2048 # those are sectors
213 ROOT_START=$(($FAT_START + $FAT_SIZE))
214 echo " * ROOT"
215 _ROOT_SIZE=$(${CROSS_TOOLS}/nbmkfs.mfs -d ${ROOTSIZEARG} -I $((${ROOT_START}*512)) ${IMG} ${WORK_DIR}/proto.root)
216 _ROOT_SIZE=$(($_ROOT_SIZE / 512))
217 USR_START=$((${ROOT_START} + ${_ROOT_SIZE}))
218 echo " * USR"
219 _USR_SIZE=$(${CROSS_TOOLS}/nbmkfs.mfs -d ${USRSIZEARG} -I $((${USR_START}*512)) ${IMG} ${WORK_DIR}/proto.usr)
220 _USR_SIZE=$(($_USR_SIZE / 512))
221 HOME_START=$((${USR_START} + ${_USR_SIZE}))
222 echo " * HOME"
223 _HOME_SIZE=$(${CROSS_TOOLS}/nbmkfs.mfs -d ${HOMESIZEARG} -I $((${HOME_START}*512)) ${IMG} ${WORK_DIR}/proto.home)
224 _HOME_SIZE=$(($_HOME_SIZE / 512))
227 # Write the partition table using the natively compiled
228 # minix partition utility
230 ${CROSS_TOOLS}/nbpartition -f -m ${IMG} ${FAT_START} "c:${FAT_SIZE}*" 81:${_ROOT_SIZE} 81:${_USR_SIZE} 81:${_HOME_SIZE}
233 # Merge the partitions into a single image.
235 echo "Merging file systems"
236 dd if=${WORK_DIR}/fat.img of=${IMG} seek=$FAT_START conv=notrunc
238 echo "Disk image at `pwd`/${IMG}"
239 echo "To boot this image on kvm:"
240 echo "qemu-system-arm -M beaglexm -serial stdio -drive if=sd,cache=writeback,file=`pwd`/${IMG}"