1#!/bin/sh
2#-
3# Copyright (c) 2013, 2014 The FreeBSD Foundation
4# All rights reserved.
5#
6# This software was developed by Glen Barber
7# under sponsorship from the FreeBSD Foundation.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28# SUCH DAMAGE.
29#
30# $FreeBSD$
31#
32
33# This script is intended to be called by release/release.sh to build ARM
34# images for release.  It is not intended to be run directly.  This sets up
35# the software needed within a build chroot, then runs crochet to provide
36# downloadable images for embedded devices.
37
38set -e
39
40before_build() {
41	WANT_UBOOT=
42	KNOWNHASH=
43	UBOOT_VERSION=
44	case ${KERNEL} in
45		BEAGLEBONE)
46			WANT_UBOOT=1
47			KNOWNHASH="7b6444bd23eb61068c43bd1d44ec7e7bfdbce5cadeca20c833eee186b4d3fd31"
48			UBOOT_VERSION="u-boot-2014.04"
49			;;
50		PANDABOARD)
51			WANT_UBOOT=1
52			KNOWNHASH="e08e20a6979bfca6eebb9a2b0e42aa4416af3d796332fd63a3470495a089d496"
53			UBOOT_VERSION="u-boot-2012.07"
54			;;
55		WANDBOARD-QUAD)
56			WANT_UBOOT=1
57			KNOWNHASH="b4f83b8db325c21671a997198ec3a373e2e00dde2fcf17be9b9afd7cfd727f56"
58			UBOOT_VERSION="u-boot-2014.07"
59			;;
60		*)
61			# Fallthrough.
62			;;
63	esac
64	if [ ! -z ${WANT_UBOOT} ]; then
65		chroot ${CHROOTDIR} fetch -o /tmp/crochet/${UBOOT_VERSION}.tar.bz2 \
66			http://people.freebsd.org/~gjb/${UBOOT_VERSION}.tar.bz2
67		UBOOT_HASH="$(sha256 -q ${CHROOTDIR}/tmp/crochet/${UBOOT_VERSION}.tar.bz2)"
68		if [ "${UBOOT_HASH}" != "${KNOWNHASH}" ]; then
69			echo "Checksum mismatch!  Exiting now."
70			exit 1
71		fi
72		chroot ${CHROOTDIR} tar xf /tmp/crochet/${UBOOT_VERSION}.tar.bz2 \
73			-C /tmp/crochet/ 
74	fi
75}
76
77install_crochet() {
78	chroot ${CHROOTDIR} svn co -q ${CROCHETSRC}/${CROCHETBRANCH} \
79		/tmp/crochet
80}
81
82install_uboot() {
83	# Only fetch u-boot sources if UBOOTSRC is set; otherwise it is
84	# not needed.
85	if [ -n "${UBOOTSRC}" ]; then
86		continue
87	else
88		return 0
89	fi
90	chroot ${CHROOTDIR} svn co -q ${UBOOTSRC}/${UBOOTBRANCH} \
91		/${UBOOTDIR}
92}
93
94main() {
95	# Fix broken ports that use kern.osreldate.
96	OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
97	export OSVERSION
98	REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
99	BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
100	UNAME_r=${REVISION}-${BRANCH}
101	export UNAME_r
102
103	# Build the 'xdev' target for crochet.
104	eval chroot ${CHROOTDIR} make -C /usr/src \
105		${XDEV_FLAGS} XDEV=${XDEV} XDEV_ARCH=${XDEV_ARCH} \
106		${WORLD_FLAGS} xdev
107
108	# Run the ldconfig(8) startup script so /var/run/ld-elf*.so.hints
109	# is created.
110	eval chroot ${CHROOTDIR} /etc/rc.d/ldconfig forcerestart
111	# Install security/ca_root_nss since we need to check the https
112	# certificate of github.
113	eval chroot ${CHROOTDIR} make -C /usr/ports/security/ca_root_nss \
114		OPTIONS_SET="ETCSYMLINK" BATCH=1 FORCE_PKG_REGISTER=1 \
115		install clean distclean
116	EMBEDDEDPORTS="${EMBEDDEDPORTS} devel/subversion"
117	for _PORT in ${EMBEDDEDPORTS}; do
118		eval chroot ${CHROOTDIR} make -C /usr/ports/${_PORT} \
119			BATCH=1 FORCE_PKG_REGISTER=1 install clean distclean
120	done
121
122	eval chroot ${CHROOTDIR} make -C /usr/src/gnu/usr.bin/cc \
123		WITH_GCC=1 ${WORLD_FLAGS} -j1 obj depend all install
124
125	mkdir -p ${CHROOTDIR}/tmp/crochet/work
126	before_build
127	install_crochet
128	install_uboot
129	eval chroot ${CHROOTDIR} /bin/sh /tmp/crochet/crochet.sh \
130		-c /tmp/external/${XDEV}/crochet-${KERNEL}.conf
131	mkdir -p ${CHROOTDIR}/R/
132	cp -p ${CHROOTDIR}/usr/obj/*.img ${CHROOTDIR}/R/
133	bzip2 ${CHROOTDIR}/R/FreeBSD*.img
134	cd ${CHROOTDIR}/R/ && sha256 FreeBSD*.img.bz2 > CHECKSUM.SHA256
135	cd ${CHROOTDIR}/R/ && md5 FreeBSD*.img.bz2 > CHECKSUM.MD5
136}
137
138main "$@"
139exit 0
140