xref: /minix/releasetools/arm_sdimage.sh (revision e1cdaee1)
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 the proper NetBSD infrastructure.
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: ${TOOLCHAIN_TRIPLET=arm-elf32-minix-}
25: ${BUILDSH=build.sh}
26
27: ${SETS="minix-base minix-comp minix-games minix-man minix-tests tests"}
28: ${IMG=minix_arm_sd.img}
29
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
35
36# Beagleboard-xm
37: ${U_BOOT_BIN_DIR=build/omap3_beagle/}
38: ${CONSOLE=tty02}
39
40# BeagleBone (and black)
41#: ${U_BOOT_BIN_DIR=build/am335x_evm/}
42#: ${CONSOLE=tty00}
43
44#
45# We host u-boot binaries.
46#
47: ${MLO=MLO}
48: ${UBOOT=u-boot.img}
49U_BOOT_GIT_VERSION=cb5178f12787c690cb1c888d88733137e5a47b15
50
51if [ ! -f ${BUILDSH} ]
52then
53	echo "Please invoke me from the root source dir, where ${BUILDSH} is."
54	exit 1
55fi
56
57if [ -n "$BASE_URL" ]
58then
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
65fi
66
67case $(uname -s) in
68Darwin)
69	MKFS_VFAT_CMD=newfs_msdos
70	MKFS_VFAT_OPTS="-h 64 -u 32 -S 512 -s ${FAT_SIZE} -o 0"
71;;
72FreeBSD)
73	MKFS_VFAT_CMD=newfs_msdos
74	MKFS_VFAT_OPTS=
75;;
76*)
77	MKFS_VFAT_CMD=mkfs.vfat
78	MKFS_VFAT_OPTS=
79;;
80esac
81
82export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:${PATH}
83
84for needed in mcopy dd ${MKFS_VFAT_CMD} git
85do
86	if ! which $needed 2>&1 > /dev/null
87	then
88		echo "**Skipping image creation: missing tool '$needed'"
89		exit 1
90	fi
91done
92
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) ))}
99
100# set up disk creation environment
101. releasetools/image.defaults
102. releasetools/image.functions
103
104# all sizes are written in 512 byte blocks
105ROOTSIZEARG="-b $((${ROOT_SIZE} / 512 / 8))"
106USRSIZEARG="-b $((${USR_SIZE} / 512 / 8))"
107HOMESIZEARG="-b $((${HOME_SIZE} / 512 / 8))"
108
109# where the kernel & boot modules will be
110MODDIR=${DESTDIR}/boot/minix/.temp
111
112echo "Building work directory..."
113build_workdir "$SETS"
114
115echo "Adding extra files..."
116
117# create a fstab entry in /etc
118cat >${ROOT_DIR}/etc/fstab <<END_FSTAB
119/dev/c0d0p2	/usr		mfs	rw			0	2
120/dev/c0d0p3	/home		mfs	rw			0	2
121none		/sys		devman	rw,rslabel=devman	0	0
122none		/dev/pts	ptyfs	rw,rslabel=ptyfs	0	0
123END_FSTAB
124add_file_spec "etc/fstab" extra.fstab
125
126echo "Bundling packages..."
127bundle_packages "$BUNDLE_PACKAGES"
128
129echo "Creating specification files..."
130create_input_spec
131create_protos "usr home"
132
133#
134# Create the FAT partition, which contains the bootloader files, kernel and modules
135#
136dd if=/dev/zero of=${WORK_DIR}/fat.img bs=512 count=1 seek=$(($FAT_SIZE -1)) 2>/dev/null
137
138#
139# Format the fat partition and put the bootloaders
140# uEnv and the kernel command line in the FAT partition
141#
142${MKFS_VFAT_CMD} ${MKFS_VFAT_OPTS} ${WORK_DIR}/fat.img
143
144#
145# Download the stage 1 bootloader and u-boot
146#
147${RELEASETOOLSDIR}/fetch_u-boot.sh -o ${RELEASETOOLSDIR}/u-boot -n $U_BOOT_GIT_VERSION
148cp ${RELEASETOOLSDIR}/u-boot/${U_BOOT_BIN_DIR}/MLO ${WORK_DIR}/
149cp ${RELEASETOOLSDIR}/u-boot/${U_BOOT_BIN_DIR}/u-boot.img ${WORK_DIR}/
150
151#
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
159
160echo "Copying configuration kernel and boot modules"
161mcopy -bsp -i ${WORK_DIR}/fat.img ${WORK_DIR}/$MLO ::MLO
162mcopy -bsp -i ${WORK_DIR}/fat.img ${WORK_DIR}/$UBOOT ::u-boot.img
163mcopy -bsp -i ${WORK_DIR}/fat.img ${WORK_DIR}/uEnv.txt ::uEnv.txt
164
165#
166# Do some last processing of the kernel and servers and then put them on the FAT
167# partition.
168#
169${CROSS_PREFIX}objcopy ${OBJ}/minix/kernel/kernel -O binary ${OBJ}/kernel.bin
170mcopy -bsp -i ${WORK_DIR}/fat.img ${OBJ}/kernel.bin ::kernel.bin
171
172for 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
175do
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}
180done
181
182for f in tty/tty/tty storage/memory/memory
183do
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}
188done
189
190#
191# For tftp booting
192#
193cp ${WORK_DIR}/uEnv.txt ${OBJ}/
194
195# Clean image
196if [ -f ${IMG} ]	# IMG might be a block device
197then
198	rm -f ${IMG}
199fi
200
201#
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.
205#
206dd if=/dev/zero of=${IMG} bs=512 count=1 seek=$((($IMG_SIZE / 512) -1))
207
208#
209# Generate /root, /usr and /home partition images.
210#
211echo "Writing disk image..."
212FAT_START=2048 # those are sectors
213ROOT_START=$(($FAT_START + $FAT_SIZE))
214echo " * 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))
217USR_START=$((${ROOT_START} + ${_ROOT_SIZE}))
218echo " * 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))
221HOME_START=$((${USR_START} + ${_USR_SIZE}))
222echo " * 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))
225
226#
227# Write the partition table using the natively compiled
228# minix partition utility
229#
230${CROSS_TOOLS}/nbpartition -f -m ${IMG} ${FAT_START} "c:${FAT_SIZE}*" 81:${_ROOT_SIZE} 81:${_USR_SIZE} 81:${_HOME_SIZE}
231
232#
233# Merge the partitions into a single image.
234#
235echo "Merging file systems"
236dd if=${WORK_DIR}/fat.img of=${IMG} seek=$FAT_START conv=notrunc
237
238echo "Disk image at `pwd`/${IMG}"
239echo "To boot this image on kvm:"
240echo "qemu-system-arm -M beaglexm -serial stdio -drive if=sd,cache=writeback,file=`pwd`/${IMG}"
241