xref: /minix/releasetools/arm_sdimage.sh (revision ebfedea0)
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
180none		/dev/pts	ptyfs	rw,rslabel=ptyfs	0	0
181END_FSTAB
182
183rm -f ${DESTDIR}/SETS.*
184
185${CROSS_TOOLS}/nbpwd_mkdb -V 0 -p -d ${DESTDIR} ${DESTDIR}/etc/master.passwd
186
187#
188# make the different file system. this part is *also* hacky. We first convert
189# the METALOG.sanitised using mtree into a input METALOG containing uids and
190# gids.
191# After that we do some magic processing to add device nodes (also missing from METALOG)
192# and convert the METALOG into a proto file that can be used by mkfs.mfs
193#
194echo "Creating the file systems"
195
196#
197# read METALOG and use mtree to convert the user and group names into uid and gids
198# FIX put "input somewhere clean"
199#
200cat ${DESTDIR}/METALOG.sanitised | ${CROSS_TOOLS}/nbmtree -N ${DESTDIR}/etc -C -K device > ${IMG_DIR}/input
201
202# add rc (if any)
203if [ -f ${RC} ]; then
204    cp ${RC} ${DESTDIR}/usr/etc/rc.local
205    echo "./usr/etc/rc.local type=file uid=0 gid=0 mode=0644" >> ${IMG_DIR}/input
206fi
207
208# add fstab
209echo "./etc/fstab type=file uid=0 gid=0 mode=0755 size=747 time=1365060731.000000000" >> ${IMG_DIR}/input
210
211# fill root.img (skipping /usr entries while keeping the /usr directory)
212cat ${IMG_DIR}/input  | grep -v "^./usr/" | ${CROSS_TOOLS}/nbtoproto -b ${DESTDIR} -o ${IMG_DIR}/root.proto
213
214#
215# Create proto files for /usr and /home using toproto.
216#
217cat ${IMG_DIR}/input  | grep  "^\./usr/\|^. "  | sed "s,\./usr,\.,g" | ${CROSS_TOOLS}/nbtoproto -b ${DESTDIR}/usr -o ${IMG_DIR}/usr.proto
218cat ${IMG_DIR}/input  | grep  "^\./home/\|^. "  | sed "s,\./home,\.,g" | ${CROSS_TOOLS}/nbtoproto -b ${DESTDIR}/home -o ${IMG_DIR}/home.proto
219
220#
221# Create the FAT partition, which contains the bootloader files, kernel and modules
222#
223dd if=/dev/zero of=${IMG_DIR}/fat.img bs=512 count=1 seek=$(($FAT_SIZE -1)) 2>/dev/null
224
225#
226# Format the fat partition and put the bootloaders
227# uEnv and the kernel command line in the FAT partition
228#
229${MKFS_VFAT_CMD} ${MKFS_VFAT_OPTS} ${IMG_DIR}/fat.img
230
231#
232# Create a uEnv.txt file
233# -n default to network boot
234# -p add a prefix to the network booted files (e.g. xm/"
235# -c set console e.g. tty02 or tty00
236# -v set verbosity e.g. 0 to 3
237#${RELEASETOOLSDIR}/gen_uEnv.txt.sh -c ${CONSOLE} -n -p bb/ > ${IMG_DIR}/uEnv.txt
238${RELEASETOOLSDIR}/gen_uEnv.txt.sh -c ${CONSOLE}  > ${IMG_DIR}/uEnv.txt
239
240echo "Copying configuration kernel and boot modules"
241mcopy -bsp -i ${IMG_DIR}/fat.img  ${IMG_DIR}/$MLO ::MLO
242mcopy -bsp -i ${IMG_DIR}/fat.img ${IMG_DIR}/$UBOOT ::u-boot.img
243mcopy -bsp -i ${IMG_DIR}/fat.img ${IMG_DIR}/uEnv.txt ::uEnv.txt
244
245#
246# Do some last processing of the kernel and servers and then put them on the FAT
247# partition.
248#
249${CROSS_PREFIX}objcopy ${OBJ}/minix/kernel/kernel -O binary ${OBJ}/kernel.bin
250
251mcopy -bsp -i ${IMG_DIR}/fat.img ${OBJ}/kernel.bin ::kernel.bin
252
253for f in servers/vm/vm servers/rs/rs servers/pm/pm servers/sched/sched \
254	servers/vfs/vfs servers/ds/ds fs/mfs/mfs fs/pfs/pfs \
255	../sbin/init/init
256do
257    fn=`basename $f`.elf
258    cp ${OBJ}/minix/${f} ${OBJ}/${fn}
259    ${CROSS_PREFIX}strip -s ${OBJ}/${fn}
260    mcopy -bsp -i ${IMG_DIR}/fat.img  ${OBJ}/${fn} ::${fn}
261done
262
263for f in tty/tty/tty storage/memory/memory
264do
265    fn=`basename $f`.elf
266    cp ${OBJ}/minix/drivers/${f} ${OBJ}/${fn}
267    ${CROSS_PREFIX}strip -s ${OBJ}/${fn}
268    mcopy -bsp -i ${IMG_DIR}/fat.img  ${OBJ}/${fn} ::${fn}
269done
270
271#
272# For tftp booting
273#
274cp ${IMG_DIR}/uEnv.txt ${OBJ}/
275
276#
277# Create the empty image where we later will put the partitions in.
278# Make sure it is at least 2GB, otherwise the SD card will not be detected
279# correctly in qemu / HW.
280#
281dd if=/dev/zero of=${IMG} bs=512 count=1 seek=$(($IMG_SIZE -1))
282
283
284#
285# Generate /root, /usr and /home partition images.
286#
287echo "Writing Minix filesystem images"
288echo " - ROOT"
289_ROOT_SIZE=$((`${CROSS_TOOLS}/nbmkfs.mfs -I $((${ROOT_START} * 512)) -b $((${ROOT_SIZE} / 8)) ${IMG} ${IMG_DIR}/root.proto`/512))
290echo " - USR"
291_USR_SIZE=$((`${CROSS_TOOLS}/nbmkfs.mfs -I $((${USR_START} * 512)) -b $((${USR_SIZE} / 8))  ${IMG}  ${IMG_DIR}/usr.proto`/512))
292echo " - HOME"
293_HOME_SIZE=$((`${CROSS_TOOLS}/nbmkfs.mfs -I $((${HOME_START} * 512)) -b $((${HOME_SIZE} / 8)) ${IMG} ${IMG_DIR}/home.proto`/512))
294
295#
296# Write the partition table using the natively compiled
297# minix partition utility
298#
299${CROSS_TOOLS}/nbpartition -f -m ${IMG} ${FAT_START} "c:${FAT_SIZE}*" \
300	81:${_ROOT_SIZE} 81:${_USR_SIZE} 81:${_HOME_SIZE}
301
302#
303# Merge the partitions into a single image.
304#
305echo "Merging file systems"
306dd if=${IMG_DIR}/fat.img of=${IMG} seek=$FAT_START conv=notrunc
307