blob: da2eb84b64397594e22b1b3b99252c72b2a27ede [file] [log] [blame]
David McMahon49302942010-02-18 16:55:35 -08001# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
rspangler@google.comd74220d2009-10-09 20:56:14 +00002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# Common constants for build scripts
6# This must evaluate properly for both /bin/bash and /bin/sh
7
8# All scripts should die on error unless commands are specifically excepted
9# by prefixing with '!' or surrounded by 'set +e' / 'set -e'.
10# TODO: Re-enable this once shflags is less prone to dying.
11#set -e
12
13# The number of jobs to pass to tools that can run in parallel (such as make
14# and dpkg-buildpackage
Greg Spencer798d75f2011-02-01 22:04:49 -080015NUM_JOBS=$(grep -c "^processor" /proc/cpuinfo)
rspangler@google.comd74220d2009-10-09 20:56:14 +000016
Simon Glass142ca062011-02-09 13:39:43 -080017# True if we have the 'pv' utility - also set up COMMON_PV_CAT for convenience
18COMMON_PV_OK=1
19COMMON_PV_CAT=pv
20pv -V >/dev/null 2>&1 || COMMON_PV_OK=0
21if [ $COMMON_PV_OK -eq 0 ]; then
22 COMMON_PV_CAT=cat
23fi
24
Greg Spencer798d75f2011-02-01 22:04:49 -080025# Make sure we have the location and name of the calling script, using
26# the current value if it is already set.
27SCRIPT_LOCATION=${SCRIPT_LOCATION:-$(dirname "$(readlink -f "$0")")}
28SCRIPT_NAME=${SCRIPT_NAME:-$(basename "$0")}
rspangler@google.comd74220d2009-10-09 20:56:14 +000029
Anton Staaf30acb0b2011-01-26 16:00:20 -080030# Detect whether we're inside a chroot or not
31if [ -e /etc/debian_chroot ]
rspangler@google.comd74220d2009-10-09 20:56:14 +000032then
Anton Staaf30acb0b2011-01-26 16:00:20 -080033 INSIDE_CHROOT=1
rspangler@google.comd74220d2009-10-09 20:56:14 +000034else
Anton Staaf30acb0b2011-01-26 16:00:20 -080035 INSIDE_CHROOT=0
rspangler@google.comd74220d2009-10-09 20:56:14 +000036fi
37
Anton Staaf30acb0b2011-01-26 16:00:20 -080038# Construct a list of possible locations for the source tree. This list is
39# based on various environment variables and globals that may have been set
40# by the calling script.
41function get_gclient_root_list() {
42 if [ $INSIDE_CHROOT -eq 1 ]; then
43 echo "/home/${USER}/trunk"
44
45 if [ -n "${SUDO_USER}" ]; then echo "/home/${SUDO_USER}/trunk"; fi
46 fi
47
48 if [ -n "${COMMON_SH}" ]; then echo "$(dirname "$COMMON_SH")/../.."; fi
49 if [ -n "${BASH_SOURCE}" ]; then echo "$(dirname "$BASH_SOURCE")/../.."; fi
50}
51
52# Based on the list of possible source locations we set GCLIENT_ROOT if it is
53# not already defined by looking for a src directory in each seach path
54# location. If we do not find a valid looking root we error out.
55function get_gclient_root() {
56 if [ -n "${GCLIENT_ROOT}" ]; then
57 return
58 fi
59
60 for path in $(get_gclient_root_list); do
61 if [ -d "${path}/src" ]; then
62 GCLIENT_ROOT=${path}
63 break
64 fi
65 done
66
67 if [ -z "${GCLIENT_ROOT}" ]; then
68 # Using dash or sh, we don't know where we are. $0 refers to the calling
69 # script, not ourselves, so that doesn't help us.
70 echo "Unable to determine location for common.sh. If you are sourcing"
71 echo "common.sh from a script run via dash or sh, you must do it in the"
72 echo "following way:"
73 echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"'
74 echo ' . "$COMMON_SH"'
75 echo "where the first line is the relative path from your script to"
76 echo "common.sh."
77 exit 1
78 fi
79}
80
81# Find root of source tree
82get_gclient_root
83
rspangler@google.comd74220d2009-10-09 20:56:14 +000084# Canonicalize the directories for the root dir and the calling script.
85# readlink is part of coreutils and should be present even in a bare chroot.
tedbo4f44d9e2010-01-08 17:26:11 -080086# This is better than just using
rspangler@google.comd74220d2009-10-09 20:56:14 +000087# FOO = "$(cd $FOO ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -080088# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +000089# Note that 'realpath' is equivalent to 'readlink -f'.
Greg Spencer798d75f2011-02-01 22:04:49 -080090SCRIPT_LOCATION=$(readlink -f $SCRIPT_LOCATION)
91GCLIENT_ROOT=$(readlink -f $GCLIENT_ROOT)
rspangler@google.comd74220d2009-10-09 20:56:14 +000092
93# Other directories should always be pathed down from GCLIENT_ROOT.
94SRC_ROOT="$GCLIENT_ROOT/src"
95SRC_INTERNAL="$GCLIENT_ROOT/src-internal"
96SCRIPTS_DIR="$SRC_ROOT/scripts"
97
98# Load developer's custom settings. Default location is in scripts dir,
99# since that's available both inside and outside the chroot. By convention,
100# settings from this file are variables starting with 'CHROMEOS_'
101CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}"
Greg Spencer798d75f2011-02-01 22:04:49 -0800102if [ -f $CHROMEOS_DEV_SETTINGS ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000103 # Turn on exit-on-error during custom settings processing
Greg Spencer798d75f2011-02-01 22:04:49 -0800104 SAVE_OPTS=$(set +o)
rspangler@google.comd74220d2009-10-09 20:56:14 +0000105 set -e
106
107 # Read settings
108 . $CHROMEOS_DEV_SETTINGS
109
110 # Restore previous state of exit-on-error
111 eval "$SAVE_OPTS"
112fi
113
114# Load shflags
Anush Elangovan56de1dd2010-08-02 23:56:07 -0700115if [[ -f /usr/lib/shflags ]]; then
116 . /usr/lib/shflags
Raja Aluric6e23cd2010-11-02 15:55:57 -0700117elif [ -f ./lib/shflags/shflags ]; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800118 . ./lib/shflags/shflags
Anush Elangovan56de1dd2010-08-02 23:56:07 -0700119else
120 . "${SRC_ROOT}/scripts/lib/shflags/shflags"
121fi
rspangler@google.comd74220d2009-10-09 20:56:14 +0000122
Bill Richardson10d27c22010-01-20 13:38:50 -0800123# Our local mirror
124DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000125
Bill Richardson10d27c22010-01-20 13:38:50 -0800126# Upstream mirrors and build suites come in 2 flavors
127# DEV - development chroot, used to build the chromeos image
128# IMG - bootable image, to run on actual hardware
rspangler@google.comd74220d2009-10-09 20:56:14 +0000129
Bill Richardson10d27c22010-01-20 13:38:50 -0800130DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
131DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
132
133DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
134DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000135
136# Default location for chroot
137DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"}
138
139# All output files from build should go under $DEFAULT_BUILD_ROOT, so that
140# they don't pollute the source directory.
141DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"}
142
David McMahon49302942010-02-18 16:55:35 -0800143# Set up a global ALL_BOARDS value
Raja Alurid44bcf92010-11-11 17:46:53 -0800144if [ -d $SRC_ROOT/overlays ]; then
145 ALL_BOARDS=$(cd $SRC_ROOT/overlays;ls -1d overlay-* 2>&-|sed 's,overlay-,,g')
David Rochberg3b910702010-12-02 10:45:21 -0500146fi
David McMahon49302942010-02-18 16:55:35 -0800147# Strip CR
148ALL_BOARDS=$(echo $ALL_BOARDS)
149# Set a default BOARD
150#DEFAULT_BOARD=x86-generic # or...
151DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}')
152
David Jamesff072012010-11-30 13:22:05 -0800153# Enable --fast by default.
Greg Spencer798d75f2011-02-01 22:04:49 -0800154DEFAULT_FAST=${FLAGS_TRUE}
David James03668642010-07-28 17:08:29 -0700155
Simon Glass142ca062011-02-09 13:39:43 -0800156
157# Standard filenames
158CHROMEOS_IMAGE_NAME="chromiumos_image.bin"
159CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin"
Chris Sosab885b802011-02-16 15:33:11 -0800160CHROMEOS_FACTORY_TEST_IMAGE_NAME="chromiumos_factory_image.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800161
rspangler@google.comd74220d2009-10-09 20:56:14 +0000162# Directory locations inside the dev chroot
163CHROOT_TRUNK_DIR="/home/$USER/trunk"
164
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700165# Install make for portage ebuilds. Used by build_image and gmergefs.
Chris Masoned11ce172010-11-09 14:22:08 -0800166# TODO: Is /usr/local/autotest-chrome still used by anyone?
Daniel Erate82f07c2010-12-21 13:39:22 -0800167DEFAULT_INSTALL_MASK="
168 *.a
169 *.la
170 /etc/init.d
171 /etc/runlevels
172 /lib/rc
173 /usr/bin/Xnest
174 /usr/bin/Xvfb
175 /usr/include
176 /usr/lib/debug
177 /usr/lib/gcc
178 /usr/lib/gtk-2.0/include
179 /usr/lib/pkgconfig
180 /usr/local/autotest
181 /usr/local/autotest-chrome
182 /usr/man
183 /usr/share/aclocal
184 /usr/share/doc
185 /usr/share/gettext
186 /usr/share/gtk-2.0
187 /usr/share/gtk-doc
188 /usr/share/info
189 /usr/share/man
190 /usr/share/openrc
191 /usr/share/pkgconfig
192 /usr/share/readline
Chris Wolfed13775f2011-07-26 16:34:38 -0400193 /usr/src
Daniel Erate82f07c2010-12-21 13:39:22 -0800194 "
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700195
Daniel Erate82f07c2010-12-21 13:39:22 -0800196FACTORY_INSTALL_MASK="
197 /opt/Qualcomm
198 /opt/Synaptics
199 /opt/google/chrome
200 /opt/google/o3d
201 /opt/google/talkplugin
202 /opt/netscape
203 /usr/lib/debug
204 /usr/lib/dri
205 /usr/lib/python2.6/test
206 /usr/local/autotest
207 /usr/local/autotest-chrome
208 /usr/local/autotest-pkgs
209 /usr/share/X11
210 /usr/share/chewing
211 /usr/share/fonts
212 /usr/share/ibus-pinyin
213 /usr/share/libhangul
214 /usr/share/locale
215 /usr/share/m17n
216 /usr/share/mime
217 /usr/share/sounds
218 /usr/share/tts
219 /usr/share/zoneinfo
220 "
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800221
Gaurav Shah786ceeb2011-07-26 15:03:37 -0700222# Determine and set up variables needed for fancy color output (if supported).
223V_REVERSE=
224V_VIDOFF=
225V_BOLD_RED=
226V_BOLD_GREEN=
227V_BOLD_YELLOW=
228
229if tput colors >/dev/null 2>&1; then
230 V_REVERSE="$(tput rev)"
231 V_VIDOFF="$(tput sgr0)"
232 V_BOLD_RED="$(tput bold; tput setaf 1)"
233 V_BOLD_GREEN="$(tput bold; tput setaf 2)"
234 V_BOLD_YELLOW="$(tput bold; tput setaf 3)"
235fi
David McMahonb7eb3a22010-02-09 14:07:40 -0800236
rspangler@google.comd74220d2009-10-09 20:56:14 +0000237# -----------------------------------------------------------------------------
238# Functions
239
Chris Sosaacada732010-02-23 15:20:03 -0800240function setup_board_warning {
tedbo373c3902010-04-12 10:52:40 -0700241 echo
242 echo "$V_REVERSE================= WARNING ======================$V_VIDOFF"
Chris Sosaacada732010-02-23 15:20:03 -0800243 echo
244 echo "*** No default board detected in " \
245 "$GCLIENT_ROOT/src/scripts/.default_board"
246 echo "*** Either run setup_board with default flag set"
247 echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board"
248 echo
249}
250
251
252# Sets the default board variable for calling script
253function get_default_board {
tedbo373c3902010-04-12 10:52:40 -0700254 DEFAULT_BOARD=
255
Chris Sosaacada732010-02-23 15:20:03 -0800256 if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800257 DEFAULT_BOARD=$(cat "$GCLIENT_ROOT/src/scripts/.default_board")
Chris Sosaacada732010-02-23 15:20:03 -0800258 fi
259}
260
261
Don Garrett640a0582010-05-04 16:54:28 -0700262# Enter a chroot and restart the current script if needed
263function restart_in_chroot_if_needed {
David Rochberg3b910702010-12-02 10:45:21 -0500264 # NB: Pass in ARGV: restart_in_chroot_if_needed "$@"
Greg Spencer798d75f2011-02-01 22:04:49 -0800265 if [ $INSIDE_CHROOT -ne 1 ]; then
Chris Sosafd2cdec2011-03-24 16:06:59 -0700266 # Get inside_chroot path for script.
267 local chroot_path="$(reinterpret_path_for_chroot "$0")"
Don Garrett640a0582010-05-04 16:54:28 -0700268 exec $SCRIPTS_DIR/enter_chroot.sh -- \
Chris Sosafd2cdec2011-03-24 16:06:59 -0700269 "$chroot_path" "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700270 fi
271}
272
rspangler@google.comd74220d2009-10-09 20:56:14 +0000273# Fail unless we're inside the chroot. This guards against messing up your
274# workstation.
275function assert_inside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800276 if [ $INSIDE_CHROOT -ne 1 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000277 echo "This script must be run inside the chroot. Run this first:"
278 echo " $SCRIPTS_DIR/enter_chroot.sh"
279 exit 1
280 fi
281}
282
283# Fail if we're inside the chroot. This guards against creating or entering
284# nested chroots, among other potential problems.
285function assert_outside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800286 if [ $INSIDE_CHROOT -ne 0 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000287 echo "This script must be run outside the chroot."
288 exit 1
289 fi
290}
291
derat@google.com86dcc8e2009-11-21 19:49:49 +0000292function assert_not_root_user {
Greg Spencer798d75f2011-02-01 22:04:49 -0800293 if [ $(id -u) = 0 ]; then
derat@google.com86dcc8e2009-11-21 19:49:49 +0000294 echo "This script must be run as a non-root user."
295 exit 1
296 fi
297}
298
Luigi Semenzato1f82e122010-03-23 12:43:08 -0700299# Check that all arguments are flags; that is, there are no remaining arguments
300# after parsing from shflags. Allow (with a warning) a single empty-string
301# argument.
302#
303# TODO: fix buildbot so that it doesn't pass the empty-string parameter,
304# then change this function.
305#
306# Usage: check_flags_only_and_allow_null_arg "$@" && set --
307function check_flags_only_and_allow_null_arg {
308 do_shift=1
309 if [[ $# == 1 && -z "$@" ]]; then
310 echo "$0: warning: ignoring null argument" >&2
311 shift
312 do_shift=0
313 fi
314 if [[ $# -gt 0 ]]; then
315 echo "error: invalid arguments: \"$@\"" >&2
316 flags_help
317 exit 1
318 fi
319 return $do_shift
320}
321
Chris Sosad9798522010-06-16 14:29:50 -0700322function info {
Anton Staaf6adf1a32011-03-02 10:13:14 -0800323 echo -e >&2 "${V_BOLD_GREEN}INFO ${CROS_LOG_PREFIX:-""}: $1${V_VIDOFF}"
Will Drewry55b42c92010-10-20 15:44:11 -0500324}
Chris Sosad9798522010-06-16 14:29:50 -0700325
robotboy2ea03ac2010-02-11 15:30:55 -0800326function warn {
Anton Staaf6adf1a32011-03-02 10:13:14 -0800327 echo -e >&2 "${V_BOLD_YELLOW}WARNING ${CROS_LOG_PREFIX:-""}: $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800328}
329
330function error {
Anton Staaf6adf1a32011-03-02 10:13:14 -0800331 echo -e >&2 "${V_BOLD_RED}ERROR ${CROS_LOG_PREFIX:-""}: $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800332}
David James546747b2010-03-23 15:19:43 -0700333
334function die {
335 error "$1"
336 exit 1
337}
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700338
339# Retry an emerge command according to $FLAGS_retries
David Jamesfd7403a2010-07-09 12:00:17 -0700340# The $EMERGE_JOBS flags will only be added the first time the command is run
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700341function eretry () {
Greg Spencer798d75f2011-02-01 22:04:49 -0800342 local i
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700343 for i in $(seq $FLAGS_retries); do
Greg Spencer798d75f2011-02-01 22:04:49 -0800344 echo "Retrying $@"
345 "$@" $EMERGE_JOBS && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700346 done
Greg Spencer798d75f2011-02-01 22:04:49 -0800347 "$@" && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700348 return 1
349}
350
351# Removes single quotes around parameter
352# Arguments:
353# $1 - string which optionally has surrounding quotes
354# Returns:
355# None, but prints the string without quotes.
356function remove_quotes() {
357 echo "$1" | sed -e "s/^'//; s/'$//"
358}
tedbo373c3902010-04-12 10:52:40 -0700359
360# Writes stdin to the given file name as root using sudo in overwrite mode.
361#
362# $1 - The output file name.
363function sudo_clobber() {
364 sudo tee "$1" > /dev/null
365}
366
367# Writes stdin to the given file name as root using sudo in append mode.
368#
369# $1 - The output file name.
370function sudo_append() {
371 sudo tee -a "$1" > /dev/null
372}
robotboy98912212010-04-12 14:08:14 -0700373
374# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
375#
376# $1 - The path to unmount.
377function safe_umount {
378 path=${1:?}
379 shift
380
381 if ! sudo umount -d "${path}"; then
382 warn "Failed to unmount ${path}"
383 warn "Doing a lazy unmount"
384
385 sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}"
386 fi
387}
Chris Sosa702618f2010-05-14 12:52:32 -0700388
Chris Sosad4455022010-05-20 10:14:06 -0700389# Fixes symlinks that are incorrectly prefixed with the build root ${1}
390# rather than the real running root '/'.
391# TODO(sosa) - Merge setup - cleanup below with this method.
392fix_broken_symlinks() {
393 local build_root="${1}"
394 local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*")
Greg Spencer798d75f2011-02-01 22:04:49 -0800395 local symlink
Chris Sosad4455022010-05-20 10:14:06 -0700396 for symlink in ${symlinks}; do
397 echo "Fixing ${symlink}"
398 local target=$(ls -l "${symlink}" | cut -f 2 -d '>')
399 # Trim spaces from target (bashism).
400 target=${target/ /}
401 # Make new target (removes rootfs prefix).
402 new_target=$(echo ${target} | sed "s#${build_root}##")
403
404 echo "Fixing symlink ${symlink}"
405 sudo unlink "${symlink}"
406 sudo ln -sf "${new_target}" "${symlink}"
407 done
408}
409
Chris Sosa702618f2010-05-14 12:52:32 -0700410# Sets up symlinks for the developer root. It is necessary to symlink
411# usr and local since the developer root is mounted at /usr/local and
412# applications expect to be installed under /usr/local/bin, etc.
413# This avoids packages installing into /usr/local/usr/local/bin.
414# ${1} specifies the symlink target for the developer root.
415# ${2} specifies the symlink target for the var directory.
416# ${3} specifies the location of the stateful partition.
417setup_symlinks_on_root() {
418 # Give args better names.
419 local dev_image_target=${1}
420 local var_target=${2}
421 local dev_image_root="${3}/dev_image"
422
423 # If our var target is actually the standard var, we are cleaning up the
424 # symlinks (could also check for /usr/local for the dev_image_target).
425 if [ ${var_target} = "/var" ]; then
426 echo "Cleaning up /usr/local symlinks for ${dev_image_root}"
427 else
428 echo "Setting up symlinks for /usr/local for ${dev_image_root}"
429 fi
430
431 # Set up symlinks that should point to ${dev_image_target}.
Greg Spencer798d75f2011-02-01 22:04:49 -0800432 local path
Chris Sosa702618f2010-05-14 12:52:32 -0700433 for path in usr local; do
434 if [ -h "${dev_image_root}/${path}" ]; then
435 sudo unlink "${dev_image_root}/${path}"
436 elif [ -e "${dev_image_root}/${path}" ]; then
437 die "${dev_image_root}/${path} should be a symlink if exists"
438 fi
439 sudo ln -s ${dev_image_target} "${dev_image_root}/${path}"
440 done
441
442 # Setup var symlink.
443 if [ -h "${dev_image_root}/var" ]; then
444 sudo unlink "${dev_image_root}/var"
445 elif [ -e "${dev_image_root}/var" ]; then
446 die "${dev_image_root}/var should be a symlink if it exists"
447 fi
448
449 sudo ln -s "${var_target}" "${dev_image_root}/var"
450}
Nick Sandersd2509272010-06-16 03:50:04 -0700451
Will Drewry55b42c92010-10-20 15:44:11 -0500452# These two helpers clobber the ro compat value in our root filesystem.
453#
454# When the system is built with --enable_rootfs_verification, bit-precise
455# integrity checking is performed. That precision poses a usability issue on
456# systems that automount partitions with recognizable filesystems, such as
457# ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be
458# automatically updated even if no other writes are performed to the
459# filesystem. In addition, ext2+ does not support a "read-only" flag for a
460# given filesystem. That said, forward and backward compatibility of
461# filesystem features are supported by tracking if a new feature breaks r/w or
462# just write compatibility. We abuse the read-only compatibility flag[1] in
463# the filesystem header by setting the high order byte (le) to FF. This tells
464# the kernel that features R24-R31 are all enabled. Since those features are
465# undefined on all ext-based filesystem, all standard kernels will refuse to
466# mount the filesystem as read-write -- only read-only[2].
467#
468# [1] 32-bit flag we are modifying:
469# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417
470# [2] Mount behavior is enforced here:
471# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857
472#
473# N.B., if the high order feature bits are used in the future, we will need to
474# revisit this technique.
475disable_rw_mount() {
476 local rootfs="$1"
477 local offset="${2-0}" # in bytes
Will Drewry9b7cb512010-10-20 18:11:24 -0500478 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
479 printf '\377' |
Will Drewry55b42c92010-10-20 15:44:11 -0500480 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
481 conv=notrunc count=1 bs=1
482}
483
484enable_rw_mount() {
485 local rootfs="$1"
486 local offset="${2-0}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500487 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
488 printf '\000' |
Will Drewry55b42c92010-10-20 15:44:11 -0500489 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
490 conv=notrunc count=1 bs=1
491}
492
Nick Sandersd2509272010-06-16 03:50:04 -0700493# Get current timestamp. Assumes common.sh runs at startup.
494start_time=$(date +%s)
495
496# Print time elsapsed since start_time.
497print_time_elapsed() {
Greg Spencer798d75f2011-02-01 22:04:49 -0800498 local end_time=$(date +%s)
499 local elapsed_seconds=$(($end_time - $start_time))
500 local minutes=$(($elapsed_seconds / 60))
501 local seconds=$(($elapsed_seconds % 60))
Olof Johansson6d491382010-08-09 16:05:50 -0500502 echo "Elapsed time: ${minutes}m${seconds}s"
Nick Sandersd2509272010-06-16 03:50:04 -0700503}
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700504
Anton Staaf9bcd8412011-01-24 15:27:14 -0800505# The board and variant command line options can be used in a number of ways
506# to specify the board and variant. The board can encode both pieces of
507# information separated by underscores. Or the variant can be passed using
508# the separate variant option. This function extracts the canonical board and
509# variant information and provides it in the BOARD, VARIANT and BOARD_VARIANT
510# variables.
511get_board_and_variant() {
512 local flags_board="${1}"
513 local flags_variant="${2}"
514
515 BOARD=$(echo "$flags_board" | cut -d '_' -f 1)
516 VARIANT=${flags_variant:-$(echo "$flags_board" | cut -s -d '_' -f 2)}
517
518 if [ -n "$VARIANT" ]; then
519 BOARD_VARIANT="${BOARD}_${VARIANT}"
520 else
521 BOARD_VARIANT="${BOARD}"
522 fi
523}
Simon Glass142ca062011-02-09 13:39:43 -0800524
525# This function converts a chromiumos image into a test image, either
526# in place or by copying to a new test image filename first. It honors
527# the following flags (see mod_image_for_test.sh)
528#
529# --factory
530# --factory_install
531# --force_copy
532#
533# On entry, pass the directory containing the image, and the image filename
534# On exit, it puts the pathname of the resulting test image into
535# CHROMEOS_RETURN_VAL
536# (yes this is ugly, but perhaps less ugly than the alternatives)
537#
538# Usage:
539# SRC_IMAGE=$(prepare_test_image "directory" "imagefile")
540prepare_test_image() {
541 # If we're asked to modify the image for test, then let's make a copy and
542 # modify that instead.
543 # Check for manufacturing image.
544 local args
545
546 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
547 args="--factory"
548 fi
549
550 # Check for install shim.
551 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then
552 args="--factory_install"
553 fi
554
555 # Check for forcing copy of image
556 if [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ]; then
557 args="${args} --force_copy"
558 fi
559
560 # Modify the image for test, creating a new test image
561 "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} \
562 --image="$1/$2" --noinplace ${args}
563
564 # From now on we use the just-created test image
Simon Glass6e448ae2011-03-03 11:20:54 -0800565 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
566 CHROMEOS_RETURN_VAL="$1/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
567 else
568 CHROMEOS_RETURN_VAL="$1/${CHROMEOS_TEST_IMAGE_NAME}"
569 fi
Simon Glass142ca062011-02-09 13:39:43 -0800570}
Anton Staaf6f5262d2011-03-02 09:35:54 -0800571
572# Check that the specified file exists. If the file path is empty or the file
573# doesn't exist on the filesystem generate useful error messages. Otherwise
574# show the user the name and path of the file that will be used. The padding
575# parameter can be used to tabulate multiple name:path pairs. For example:
576#
577# check_for_file "really long name" "...:" "file.foo"
578# check_for_file "short name" ".........:" "another.bar"
579#
580# Results in the following output:
581#
582# Using really long name...: file.foo
583# Using short name.........: another.bar
584#
585# If tabulation is not required then passing "" for padding generates the
586# output "Using <name> <path>"
587check_for_file() {
588 local name=$1
589 local padding=$2
590 local path=$3
591
592 if [ -z "${path}" ]; then
593 die "No ${name} file specified."
594 fi
595
596 if [ ! -e "${path}" ]; then
597 die "No ${name} file found at: ${path}"
598 else
599 info "Using ${name}${padding} ${path}"
600 fi
601}
602
603# Check that the specified tool exists. If it does not exist in the PATH
604# generate a useful error message indicating how to install the ebuild
605# that contains the required tool.
606check_for_tool() {
607 local tool=$1
608 local ebuild=$2
609
610 if ! which "${tool}" >/dev/null ; then
611 error "The ${tool} utility was not found in your path. Run the following"
612 error "command in your chroot to install it: sudo -E emerge ${ebuild}"
613 exit 1
614 fi
615}
Chris Sosafd2cdec2011-03-24 16:06:59 -0700616
617# Reinterprets path from outside the chroot for use inside.
618# Returns "" if "" given.
619# $1 - The path to reinterpret.
620function reinterpret_path_for_chroot() {
621 if [ $INSIDE_CHROOT -ne 1 ]; then
622 if [ -z "${1}" ]; then
623 echo ""
624 else
625 local path_abs_path=$(readlink -f "${1}")
626 local gclient_root_abs_path=$(readlink -f "${GCLIENT_ROOT}")
627
628 # Strip the repository root from the path.
629 local relative_path=$(echo ${path_abs_path} \
630 | sed s:${gclient_root_abs_path}/::)
631
632 if [ "${relative_path}" = "${path_abs_path}" ]; then
633 die "Error reinterpreting path. Path ${1} is not within source tree."
634 fi
635
636 # Prepend the chroot repository path.
637 echo "/home/${USER}/trunk/${relative_path}"
638 fi
639 else
640 # Path is already inside the chroot :).
641 echo "${1}"
642 fi
643}
Gabe Black83d8b822011-08-01 17:50:09 -0700644
645# Find a firmware component using one of a set of prefixes, or an override if
646# one is given.
647# The first argument is a list of path prefixes which are prepended to the
648# component name, relative to the current board's build directory.
649# The second argument is the name of the component to look for.
650# The third argument is an optional override. If it's a non-empty string, that
651# value is returned instead of searching. This makes it a little easier to
652# accept overrides in calling scripts.
653# The return value is the override if one is set, the first path at which a
654# component is found, or the last path searched if none is found. Later checks
655# in the calling scripts can identify that the file doesn't exist and handle
656# the error appropriately.
657function find_fw_component() {
658 local prefixes=$1
659 local component=$2
660 local override=$3
661 if [ ! -z "${override}" ]; then
662 echo "${override}"
663 exit 0
664 fi
665 local path=""
666 for prefix in ${prefixes}; do
667 path="/build/${BOARD_VARIANT}/${prefix}/${component}"
668 if [ -e "${path}" ]; then
669 break
670 fi
671 done
672 echo "${path}"
673}
674
675# A wrapper for find_fw_component which provides a set of prefixes which make
676# sense for u-boot.
677function find_u_boot_component () {
678 local component=$1
679 local override=$2
680 find_fw_component "firmware u-boot" "${component}" "${override}"
681}
682
683# A wrapper for find_fw_component which provides a set of prefixes which make
684# sense for coreboot.
685function find_coreboot_component () {
686 local component=$1
687 local override=$2
688 find_fw_component "firmware coreboot" "${component}" "${override}"
689}