blob: 6f2930809b63d16a0f8b50a2b272b9a638165d8c [file] [log] [blame]
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -08001#!/bin/bash
2
Taylor Hutt60da6422011-06-02 13:54:43 -07003# Copyright (c) 2009-2011 The Chromium OS Authors. All rights reserved.
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -08004# 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
Brian Harringaa13ea42012-03-15 18:31:03 -07009SCRIPT_ROOT=$(dirname $(readlink -f "$0"))
David James359d3e12012-07-10 13:09:48 -070010. "${SCRIPT_ROOT}/common.sh" || exit 1
11. "${SCRIPT_ROOT}/remote_access.sh" || exit 1
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080012
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080013# Script must be run inside the chroot.
Greg Spencer798d75f2011-02-01 22:04:49 -080014restart_in_chroot_if_needed "$@"
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080015
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080016DEFINE_string board "" "Override board reported by target"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080017DEFINE_string device "" "Override boot device reported by target"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080018DEFINE_string partition "" "Override kernel partition reported by target"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080019DEFINE_string arch "" "Override architecture reported by target"
Olof Johansson8488f5a2011-04-20 17:27:37 -070020DEFINE_boolean reboot $FLAGS_TRUE "Reboot system after update"
Doug Anderson5a21b442012-12-07 11:47:31 -080021DEFINE_boolean vboot $FLAGS_TRUE "Update the vboot kernel"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080022
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080023# Parse command line.
24FLAGS "$@" || exit 1
25eval set -- "${FLAGS_ARGV}"
26
27# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080028# so will die prematurely if 'switch_to_strict_mode' is specified before now.
29switch_to_strict_mode
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080030
Mike Frysinger6b1abb22012-05-11 13:44:06 -040031cleanup() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080032 cleanup_remote_access
33 rm -rf "${TMP}"
34}
35
Mike Frysinger6b1abb22012-05-11 13:44:06 -040036learn_device() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080037 [ -n "${FLAGS_device}" ] && return
38 remote_sh df /mnt/stateful_partition
39 FLAGS_device=$(echo "${REMOTE_OUT}" | awk '/dev/ {print $1}' | sed s/1\$//)
40 info "Target reports root device is ${FLAGS_device}"
41}
42
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080043# Ask the target what the kernel partition is
Mike Frysinger6b1abb22012-05-11 13:44:06 -040044learn_partition_and_ro() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080045 [ -n "${FLAGS_partition}" ] && return
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080046 ! remote_sh rootdev
Paul Taysoma64d9db2012-09-21 13:30:43 -070047 if [ "${REMOTE_OUT%%-*}" == "/dev/dm" ]; then
48 remote_sh rootdev -s
Olof Johansson8488f5a2011-04-20 17:27:37 -070049 REMOTE_VERITY=${FLAGS_TRUE}
50 info "System is using verity: not updating firmware"
51 else
52 REMOTE_VERITY=${FLAGS_FALSE}
53 info "System is not using verity: updating firmware and modules"
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080054 fi
55 if [ "${REMOTE_OUT}" == "${FLAGS_device}3" ]; then
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080056 FLAGS_partition="${FLAGS_device}2"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080057 else
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080058 FLAGS_partition="${FLAGS_device}4"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080059 fi
60 if [ -z "${FLAGS_partition}" ]; then
Doug Andersonb2fe4652012-12-07 17:33:56 -080061 die "Partition required"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080062 fi
Doug Anderson5a21b442012-12-07 11:47:31 -080063 if [ ${REMOTE_VERITY} -eq ${FLAGS_TRUE} ]; then
64 info "Target reports kernel partition is ${FLAGS_partition}"
65 if [ ${FLAGS_vboot} -eq ${FLAGS_FALSE} ]; then
66 die "Must update vboot when target is using verity"
67 fi
68 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080069}
70
Mike Frysinger6b1abb22012-05-11 13:44:06 -040071make_kernelimage() {
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080072 local bootloader_path
73 local kernel_image
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080074 if [[ "${FLAGS_arch}" == "arm" ]]; then
Taylor Hutt60da6422011-06-02 13:54:43 -070075 name="bootloader.bin"
76 bootloader_path="${SRC_ROOT}/build/images/${FLAGS_board}/latest/${name}"
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080077 kernel_image="/build/${FLAGS_board}/boot/vmlinux.uimg"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080078 else
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080079 bootloader_path="/lib64/bootstub/bootstub.efi"
80 kernel_image="/build/${FLAGS_board}/boot/vmlinuz"
81 fi
Kees Cook43a32132011-10-18 13:17:11 -070082 vbutil_kernel --pack $TMP/new_kern.bin \
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080083 --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
84 --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
85 --version 1 \
Mandeep Singh Baines981555b2011-06-01 16:36:54 -070086 --config "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt" \
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080087 --bootloader "${bootloader_path}" \
88 --vmlinuz "${kernel_image}" \
89 --arch "${FLAGS_arch}"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080090}
91
Mike Frysinger6b1abb22012-05-11 13:44:06 -040092copy_kernelimage() {
Kees Cook43a32132011-10-18 13:17:11 -070093 remote_cp_to $TMP/new_kern.bin /tmp
Olof Johansson8488f5a2011-04-20 17:27:37 -070094 remote_sh dd if=/tmp/new_kern.bin of="${FLAGS_partition}"
95}
96
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -040097check_kernelbuildtime() {
98 local version=$(readlink "/build/${FLAGS_board}/boot/vmlinuz" | cut -d- -f2-)
99 local build_dir="/build/${FLAGS_board}/lib/modules/${version}/build"
100 if [ "${build_dir}/Makefile" -nt "/build/${FLAGS_board}/boot/vmlinuz" ]; then
101 warn "Your build directory has been built more recently than"
102 warn "the installed kernel being updated to. Did you forget to"
103 warn "run 'cros_workon_make chromeos-kernel --install'?"
104 fi
105}
106
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400107main() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800108 trap cleanup EXIT
109
Kees Cook43a32132011-10-18 13:17:11 -0700110 TMP=$(mktemp -d /tmp/update_kernel.XXXXXX)
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800111
112 remote_access_init
113
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800114 learn_arch
115
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800116 learn_board
117
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800118 learn_device
119
Olof Johansson8488f5a2011-04-20 17:27:37 -0700120 learn_partition_and_ro
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800121
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800122 remote_sh uname -r -v
123
124 old_kernel="${REMOTE_OUT}"
125
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -0400126 check_kernelbuildtime
127
Doug Anderson5a21b442012-12-07 11:47:31 -0800128 if [ ${FLAGS_vboot} -eq ${FLAGS_TRUE} ]; then
129 make_kernelimage
130 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800131
Olof Johansson8488f5a2011-04-20 17:27:37 -0700132 if [[ ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]]; then
Kees Cook43a32132011-10-18 13:17:11 -0700133 tar -C /build/"${FLAGS_board}"/lib/modules -cjf $TMP/new_modules.tar .
134 tar -C /build/"${FLAGS_board}"/lib/firmware -cjf $TMP/new_firmware.tar .
135 tar -C /build/"${FLAGS_board}"/boot -cjf $TMP/new_boot.tar .
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800136
Olof Johansson8488f5a2011-04-20 17:27:37 -0700137 remote_sh mount -o remount,rw /
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700138 echo "copying kernel"
Kees Cook43a32132011-10-18 13:17:11 -0700139 remote_cp_to $TMP/new_boot.tar /tmp/
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700140 remote_sh tar -C /boot -xjf /tmp/new_boot.tar
141
142 # ARM does not have the syslinux directory, so skip it when the
143 # partition or the syslinux vmlinuz target is missing.
144 echo "updating syslinux kernel"
145 remote_sh grep $(echo ${FLAGS_device}12 | cut -d/ -f3) /proc/partitions
146 if [ $(echo "$REMOTE_OUT" | wc -l) -eq 1 ]; then
147 remote_sh mkdir -p /tmp/12
148 remote_sh mount ${FLAGS_device}12 /tmp/12
149
150 if [ "$FLAGS_partition" = "${FLAGS_device}2" ]; then
151 target="/tmp/12/syslinux/vmlinuz.A"
152 else
153 target="/tmp/12/syslinux/vmlinuz.B"
154 fi
155 remote_sh "test ! -f $target || cp /boot/vmlinuz $target"
156
157 remote_sh umount /tmp/12
158 remote_sh rmdir /tmp/12
159 fi
Olof Johansson9a83e4e2012-08-17 02:45:12 -0700160
161 echo "copying modules"
162 remote_cp_to $TMP/new_modules.tar /tmp/
163 remote_sh tar -C /lib/modules -xjf /tmp/new_modules.tar
164
165 echo "copying firmware"
166 remote_cp_to $TMP/new_firmware.tar /tmp/
167 remote_sh tar -C /lib/firmware -xjf /tmp/new_firmware.tar
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800168 fi
169
Doug Anderson5a21b442012-12-07 11:47:31 -0800170 if [ ${FLAGS_vboot} -eq ${FLAGS_TRUE} ]; then
171 info "Copying vboot kernel image"
172 copy_kernelimage
173 else
174 info "Skipping update of vboot (per request)"
175 fi
Olof Johansson8488f5a2011-04-20 17:27:37 -0700176
Jonathan Kliegmand6f3d072012-07-12 12:45:33 -0400177 # An early kernel panic can prevent the normal sync on reboot. Explicitly
178 # sync for safety to avoid random file system corruption.
179 remote_sh sync
180
Doug Andersonb2fe4652012-12-07 17:33:56 -0800181 if [ ${FLAGS_reboot} -eq ${FLAGS_TRUE} ]; then
Olof Johansson8488f5a2011-04-20 17:27:37 -0700182 echo "rebooting"
183
184 remote_reboot
185
186 remote_sh uname -r -v
187 info "old kernel: ${old_kernel}"
188 info "new kernel: ${REMOTE_OUT}"
189 else
190 info "Not rebooting (per request)"
191 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800192}
193
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800194main "$@"