xref: /minix/releasetools/arm_sdimage.sh (revision 39d31d9f)
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
67export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:${PATH}
68
69# we create a disk image of about 2 gig's
70# for alignment reasons, prefer sizes which are multiples of 4096 bytes
71: ${IMG_SIZE=$((     2*(2**30) ))}
72: ${ROOT_SIZE=$((   64*(2**20) ))}
73: ${HOME_SIZE=$((  128*(2**20) ))}
74: ${USR_SIZE=$((  1792*(2**20) ))}
75
76# set up disk creation environment
77. releasetools/image.defaults
78. releasetools/image.functions
79
80# all sizes are written in 512 byte blocks
81ROOTSIZEARG="-b $((${ROOT_SIZE} / 512 / 8))"
82USRSIZEARG="-b $((${USR_SIZE} / 512 / 8))"
83HOMESIZEARG="-b $((${HOME_SIZE} / 512 / 8))"
84
85# where the kernel & boot modules will be
86MODDIR=${DESTDIR}/boot/minix/.temp
87
88echo "Building work directory..."
89build_workdir "$SETS"
90
91echo "Adding extra files..."
92
93# create a fstab entry in /etc
94cat >${ROOT_DIR}/etc/fstab <<END_FSTAB
95/dev/c0d0p2	/usr		mfs	rw			0	2
96/dev/c0d0p3	/home		mfs	rw			0	2
97none		/sys		devman	rw,rslabel=devman	0	0
98none		/dev/pts	ptyfs	rw,rslabel=ptyfs	0	0
99END_FSTAB
100add_file_spec "etc/fstab" extra.fstab
101
102echo "Bundling packages..."
103bundle_packages "$BUNDLE_PACKAGES"
104
105echo "Creating specification files..."
106create_input_spec
107create_protos "usr home"
108
109# Download the stage 1 bootloader and u-boot
110#
111${RELEASETOOLSDIR}/fetch_u-boot.sh -o ${RELEASETOOLSDIR}/u-boot -n $U_BOOT_GIT_VERSION
112
113# Clean image
114if [ -f ${IMG} ]	# IMG might be a block device
115then
116	rm -f ${IMG}
117fi
118
119#
120# Create the empty image where we later will put the partitions in.
121# Make sure it is at least 2GB, otherwise the SD card will not be detected
122# correctly in qemu / HW.
123#
124dd if=/dev/zero of=${IMG} bs=512 count=1 seek=$((($IMG_SIZE / 512) -1))
125
126#
127# Generate /root, /usr and /home partition images.
128#
129echo "Writing disk image..."
130FAT_START=2048 # those are sectors
131ROOT_START=$(($FAT_START + $FAT_SIZE))
132echo " * ROOT"
133_ROOT_SIZE=$(${CROSS_TOOLS}/nbmkfs.mfs -d ${ROOTSIZEARG} -I $((${ROOT_START}*512)) ${IMG} ${WORK_DIR}/proto.root)
134_ROOT_SIZE=$(($_ROOT_SIZE / 512))
135USR_START=$((${ROOT_START} + ${_ROOT_SIZE}))
136echo " * USR"
137_USR_SIZE=$(${CROSS_TOOLS}/nbmkfs.mfs  -d ${USRSIZEARG}  -I $((${USR_START}*512))  ${IMG} ${WORK_DIR}/proto.usr)
138_USR_SIZE=$(($_USR_SIZE / 512))
139HOME_START=$((${USR_START} + ${_USR_SIZE}))
140echo " * HOME"
141_HOME_SIZE=$(${CROSS_TOOLS}/nbmkfs.mfs -d ${HOMESIZEARG} -I $((${HOME_START}*512)) ${IMG} ${WORK_DIR}/proto.home)
142_HOME_SIZE=$(($_HOME_SIZE / 512))
143echo " * BOOT"
144rm -rf ${ROOT_DIR}/*
145cp ${RELEASETOOLSDIR}/u-boot/${U_BOOT_BIN_DIR}/MLO ${ROOT_DIR}/
146cp ${RELEASETOOLSDIR}/u-boot/${U_BOOT_BIN_DIR}/u-boot.img ${ROOT_DIR}/
147
148# Create a uEnv.txt file
149# -n default to network boot
150# -p add a prefix to the network booted files (e.g. xm/"
151# -c set console e.g. tty02 or tty00
152# -v set verbosity e.g. 0 to 3
153#${RELEASETOOLSDIR}/gen_uEnv.txt.sh -c ${CONSOLE} -n -p bb/ > ${WORK_DIR}/uEnv.txt
154${RELEASETOOLSDIR}/gen_uEnv.txt.sh -c ${CONSOLE}  > ${ROOT_DIR}/uEnv.txt
155
156# Do some last processing of the kernel and servers and then put them on the FAT
157# partition.
158${CROSS_PREFIX}objcopy ${OBJ}/minix/kernel/kernel -O binary ${ROOT_DIR}/kernel.bin
159
160for f in servers/vm/vm servers/rs/rs servers/pm/pm servers/sched/sched \
161	servers/vfs/vfs servers/ds/ds servers/mib/mib fs/pfs/pfs fs/mfs/mfs \
162	../sbin/init/init drivers/tty/tty/tty drivers/storage/memory/memory
163do
164    fn=`basename $f`.elf
165    cp ${OBJ}/minix/${f} ${ROOT_DIR}/${fn}
166    ${CROSS_PREFIX}strip -s ${ROOT_DIR}/${fn}
167done
168cat >${WORK_DIR}/boot.mtree <<EOF
169. type=dir
170./MLO type=file
171./u-boot.img type=file
172./uEnv.txt type=file
173./kernel.bin type=file
174./ds.elf type=file
175./rs.elf type=file
176./pm.elf type=file
177./sched.elf type=file
178./vfs.elf type=file
179./memory.elf type=file
180./tty.elf type=file
181./mib.elf type=file
182./vm.elf type=file
183./pfs.elf type=file
184./mfs.elf type=file
185./init.elf type=file
186EOF
187
188#
189# Create the FAT partition, which contains the bootloader files, kernel and modules
190#
191${CROSS_TOOLS}/nbmakefs -t msdos -s ${FAT_SIZE}b -o F=16,c=1 \
192	-F ${WORK_DIR}/boot.mtree ${WORK_DIR}/fat.img ${ROOT_DIR}
193
194#
195# Write the partition table using the natively compiled
196# minix partition utility
197#
198${CROSS_TOOLS}/nbpartition -f -m ${IMG} ${FAT_START} \
199	"c:${FAT_SIZE}*" 81:${_ROOT_SIZE} 81:${_USR_SIZE} 81:${_HOME_SIZE}
200
201#
202# Merge the partitions into a single image.
203#
204echo "Merging file systems"
205dd if=${WORK_DIR}/fat.img of=${IMG} seek=$FAT_START conv=notrunc
206
207echo "Disk image at `pwd`/${IMG}"
208echo "To boot this image on kvm:"
209echo "qemu-system-arm -M beaglexm -serial stdio -drive if=sd,cache=writeback,file=`pwd`/${IMG}"
210