blob: 1291b50237c89124a0f57bd20730687afbccb994 [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 Johansson8488f5a2011-04-20 17:27:37 -070039DEFINE_boolean reboot $FLAGS_TRUE "Reboot system after update"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080040
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080041# Parse command line.
42FLAGS "$@" || exit 1
43eval set -- "${FLAGS_ARGV}"
44
45# Only now can we die on error. shflags functions leak non-zero error codes,
46# so will die prematurely if 'set -e' is specified before now.
47set -e
48
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080049function cleanup {
50 cleanup_remote_access
51 rm -rf "${TMP}"
52}
53
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080054function learn_device() {
55 [ -n "${FLAGS_device}" ] && return
56 remote_sh df /mnt/stateful_partition
57 FLAGS_device=$(echo "${REMOTE_OUT}" | awk '/dev/ {print $1}' | sed s/1\$//)
58 info "Target reports root device is ${FLAGS_device}"
59}
60
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080061# Ask the target what the kernel partition is
Olof Johansson8488f5a2011-04-20 17:27:37 -070062function learn_partition_and_ro() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080063 [ -n "${FLAGS_partition}" ] && return
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080064 ! remote_sh rootdev
65 if [ "${REMOTE_OUT}" == "/dev/dm-0" ]; then
66 remote_sh ls /sys/block/dm-0/slaves
67 REMOTE_OUT="/dev/${REMOTE_OUT}"
Olof Johansson8488f5a2011-04-20 17:27:37 -070068 REMOTE_VERITY=${FLAGS_TRUE}
69 info "System is using verity: not updating firmware"
70 else
71 REMOTE_VERITY=${FLAGS_FALSE}
72 info "System is not using verity: updating firmware and modules"
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080073 fi
74 if [ "${REMOTE_OUT}" == "${FLAGS_device}3" ]; then
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080075 FLAGS_partition="${FLAGS_device}2"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080076 else
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080077 FLAGS_partition="${FLAGS_device}4"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080078 fi
79 if [ -z "${FLAGS_partition}" ]; then
80 error "Partition required"
81 exit 1
82 fi
83 info "Target reports kernel partition is ${FLAGS_partition}"
84}
85
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080086function make_kernelimage() {
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080087 local bootloader_path
88 local kernel_image
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080089 if [[ "${FLAGS_arch}" == "arm" ]]; then
Mandeep Singh Baines981555b2011-06-01 16:36:54 -070090 bootloader_path="${SRC_ROOT}/build/images/${FLAGS_board}/latest/kernel.scr.uimg"
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080091 kernel_image="/build/${FLAGS_board}/boot/vmlinux.uimg"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080092 else
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080093 bootloader_path="/lib64/bootstub/bootstub.efi"
94 kernel_image="/build/${FLAGS_board}/boot/vmlinuz"
95 fi
96 vbutil_kernel --pack new_kern.bin \
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080097 --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
98 --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
99 --version 1 \
Mandeep Singh Baines981555b2011-06-01 16:36:54 -0700100 --config "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt" \
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800101 --bootloader "${bootloader_path}" \
102 --vmlinuz "${kernel_image}" \
103 --arch "${FLAGS_arch}"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800104}
105
Olof Johansson8488f5a2011-04-20 17:27:37 -0700106function copy_kernelimage() {
107 if [ "${FLAGS_arch}" == "arm" -a ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]; then
108 remote_cp_to /build/${FLAGS_board}/boot/vmlinux.uimg /boot
109 fi
110
111 remote_cp_to new_kern.bin /tmp
112
113 remote_sh dd if=/tmp/new_kern.bin of="${FLAGS_partition}"
114}
115
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800116function main() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800117 trap cleanup EXIT
118
119 TMP=$(mktemp -d /tmp/image_to_live.XXXX)
120
121 remote_access_init
122
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800123 learn_arch
124
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800125 learn_board
126
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800127 learn_device
128
Olof Johansson8488f5a2011-04-20 17:27:37 -0700129 learn_partition_and_ro
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800130
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800131 remote_sh uname -r -v
132
133 old_kernel="${REMOTE_OUT}"
134
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800135 make_kernelimage
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800136
Olof Johansson8488f5a2011-04-20 17:27:37 -0700137 if [[ ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]]; then
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800138 tar -C /build/"${FLAGS_board}"/lib/modules -cjf /tmp/new_modules.tar .
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800139 tar -C /build/"${FLAGS_board}"/lib/firmware -cjf /tmp/new_firmware.tar .
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800140
Olof Johansson8488f5a2011-04-20 17:27:37 -0700141 remote_sh mount -o remount,rw /
142 echo "copying modules"
143 remote_cp_to /tmp/new_modules.tar /tmp/
144
145 remote_sh tar -C /lib/modules -xjf /tmp/new_modules.tar
146
147 echo "copying firmware"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800148 remote_cp_to /tmp/new_firmware.tar /tmp/
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800149
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800150 remote_sh tar -C /lib/firmware -xjf /tmp/new_firmware.tar
151 fi
152
Olof Johansson8488f5a2011-04-20 17:27:37 -0700153 echo "copying kernel"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800154
Olof Johansson8488f5a2011-04-20 17:27:37 -0700155 copy_kernelimage
156
157 if [ "${FLAGS_reboot}" -eq ${FLAGS_TRUE} ]; then
158 echo "rebooting"
159
160 remote_reboot
161
162 remote_sh uname -r -v
163 info "old kernel: ${old_kernel}"
164 info "new kernel: ${REMOTE_OUT}"
165 else
166 info "Not rebooting (per request)"
167 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800168}
169
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800170main "$@"