blob: b5cb818b4336b240c8c90525d1908e27bc3fcd94 [file] [log] [blame]
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -08001#!/bin/bash
2
3# Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Script to update the kernel on a live running ChromiumOS instance.
8
Greg Spencer798d75f2011-02-01 22:04:49 -08009# --- BEGIN COMMON.SH BOILERPLATE ---
10# Load common CrOS utilities. Inside the chroot this file is installed in
11# /usr/lib/crosutils. Outside the chroot we find it relative to the script's
12# location.
13find_common_sh() {
14 local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")"))
15 local path
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080016
Greg Spencer798d75f2011-02-01 22:04:49 -080017 SCRIPT_ROOT=
18 for path in "${common_paths[@]}"; do
19 if [ -r "${path}/common.sh" ]; then
20 SCRIPT_ROOT=${path}
21 break
22 fi
23 done
24}
25
26find_common_sh
27. "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1)
28# --- END COMMON.SH BOILERPLATE ---
29
30. "${SCRIPT_ROOT}/remote_access.sh"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080031
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080032# Script must be run inside the chroot.
Greg Spencer798d75f2011-02-01 22:04:49 -080033restart_in_chroot_if_needed "$@"
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080034
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080035DEFINE_string board "" "Override board reported by target"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080036DEFINE_string device "" "Override boot device reported by target"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080037DEFINE_string partition "" "Override kernel partition reported by target"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080038DEFINE_string arch "" "Override architecture reported by target"
Olof Johansson5a46bfb2010-12-22 12:14:21 -080039DEFINE_boolean modules false "Update modules on target"
40DEFINE_boolean firmware false "Update firmware on target"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080041
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080042# Parse command line.
43FLAGS "$@" || exit 1
44eval set -- "${FLAGS_ARGV}"
45
46# Only now can we die on error. shflags functions leak non-zero error codes,
47# so will die prematurely if 'set -e' is specified before now.
48set -e
49
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080050function cleanup {
51 cleanup_remote_access
52 rm -rf "${TMP}"
53}
54
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080055function learn_device() {
56 [ -n "${FLAGS_device}" ] && return
57 remote_sh df /mnt/stateful_partition
58 FLAGS_device=$(echo "${REMOTE_OUT}" | awk '/dev/ {print $1}' | sed s/1\$//)
59 info "Target reports root device is ${FLAGS_device}"
60}
61
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080062# Ask the target what the kernel partition is
63function learn_partition() {
64 [ -n "${FLAGS_partition}" ] && return
65 remote_sh cat /proc/cmdline
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080066 if echo "${REMOTE_OUT}" | egrep -q "${FLAGS_device}3"; then
67 FLAGS_partition="${FLAGS_device}2"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080068 else
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080069 FLAGS_partition="${FLAGS_device}4"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080070 fi
71 if [ -z "${FLAGS_partition}" ]; then
72 error "Partition required"
73 exit 1
74 fi
75 info "Target reports kernel partition is ${FLAGS_partition}"
76}
77
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080078function make_kernelimage() {
79
80 if [[ "${FLAGS_arch}" == "arm" ]]; then
81 ./build_kernel_image.sh --arch=arm \
82 --root='/dev/${devname}${rootpart}' \
83 --vmlinuz=/build/${FLAGS_board}/boot/vmlinux.uimg --to new_kern.bin
84 else
85 vbutil_kernel --pack new_kern.bin \
86 --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
87 --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
88 --version 1 \
89 --config ../build/images/${FLAGS_board}/latest/config.txt \
90 --bootloader /lib64/bootstub/bootstub.efi \
91 --vmlinuz /build/${FLAGS_board}/boot/vmlinuz
92 fi
93}
94
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080095function main() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080096 trap cleanup EXIT
97
98 TMP=$(mktemp -d /tmp/image_to_live.XXXX)
99
100 remote_access_init
101
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800102 learn_arch
103
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800104 learn_board
105
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800106 learn_device
107
108 learn_partition
109
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800110 remote_sh uname -r -v
111
112 old_kernel="${REMOTE_OUT}"
113
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800114 make_kernelimage
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800115
116 remote_cp_to new_kern.bin /tmp
117
118 remote_sh dd if=/tmp/new_kern.bin of="${FLAGS_partition}"
119
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800120 if [[ ${FLAGS_modules} -eq ${FLAGS_TRUE} ]]; then
121 echo "copying modules"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800122 tar -C /build/"${FLAGS_board}"/lib/modules -cjf /tmp/new_modules.tar .
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800123
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800124 remote_cp_to /tmp/new_modules.tar /tmp/
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800125
126 remote_sh mount -o remount,rw /
127 remote_sh tar -C /lib/modules -xjf /tmp/new_modules.tar
128 fi
129
130 if [[ ${FLAGS_firmware} -eq ${FLAGS_TRUE} ]]; then
131 echo "copying firmware"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800132 tar -C /build/"${FLAGS_board}"/lib/firmware -cjf /tmp/new_firmware.tar .
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800133
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800134 remote_cp_to /tmp/new_firmware.tar /tmp/
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800135
136 remote_sh mount -o remount,rw /
137 remote_sh tar -C /lib/firmware -xjf /tmp/new_firmware.tar
138 fi
139
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800140 remote_reboot
141
142 remote_sh uname -r -v
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800143 info "old kernel: ${old_kernel}"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800144 info "new kernel: ${REMOTE_OUT}"
145}
146
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800147main "$@"