blob: 2b9b2dc92f70bde4adc31ab63f32314aaa27c149 [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"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080021
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080022# Parse command line.
23FLAGS "$@" || exit 1
24eval set -- "${FLAGS_ARGV}"
25
26# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080027# so will die prematurely if 'switch_to_strict_mode' is specified before now.
28switch_to_strict_mode
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080029
Mike Frysinger6b1abb22012-05-11 13:44:06 -040030cleanup() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080031 cleanup_remote_access
32 rm -rf "${TMP}"
33}
34
Mike Frysinger6b1abb22012-05-11 13:44:06 -040035learn_device() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080036 [ -n "${FLAGS_device}" ] && return
37 remote_sh df /mnt/stateful_partition
38 FLAGS_device=$(echo "${REMOTE_OUT}" | awk '/dev/ {print $1}' | sed s/1\$//)
39 info "Target reports root device is ${FLAGS_device}"
40}
41
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080042# Ask the target what the kernel partition is
Mike Frysinger6b1abb22012-05-11 13:44:06 -040043learn_partition_and_ro() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080044 [ -n "${FLAGS_partition}" ] && return
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080045 ! remote_sh rootdev
Paul Taysoma64d9db2012-09-21 13:30:43 -070046 if [ "${REMOTE_OUT%%-*}" == "/dev/dm" ]; then
47 remote_sh rootdev -s
Olof Johansson8488f5a2011-04-20 17:27:37 -070048 REMOTE_VERITY=${FLAGS_TRUE}
49 info "System is using verity: not updating firmware"
50 else
51 REMOTE_VERITY=${FLAGS_FALSE}
52 info "System is not using verity: updating firmware and modules"
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080053 fi
54 if [ "${REMOTE_OUT}" == "${FLAGS_device}3" ]; then
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080055 FLAGS_partition="${FLAGS_device}2"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080056 else
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080057 FLAGS_partition="${FLAGS_device}4"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080058 fi
59 if [ -z "${FLAGS_partition}" ]; then
60 error "Partition required"
61 exit 1
62 fi
63 info "Target reports kernel partition is ${FLAGS_partition}"
64}
65
Mike Frysinger6b1abb22012-05-11 13:44:06 -040066make_kernelimage() {
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080067 local bootloader_path
68 local kernel_image
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080069 if [[ "${FLAGS_arch}" == "arm" ]]; then
Taylor Hutt60da6422011-06-02 13:54:43 -070070 name="bootloader.bin"
71 bootloader_path="${SRC_ROOT}/build/images/${FLAGS_board}/latest/${name}"
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080072 kernel_image="/build/${FLAGS_board}/boot/vmlinux.uimg"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080073 else
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080074 bootloader_path="/lib64/bootstub/bootstub.efi"
75 kernel_image="/build/${FLAGS_board}/boot/vmlinuz"
76 fi
Kees Cook43a32132011-10-18 13:17:11 -070077 vbutil_kernel --pack $TMP/new_kern.bin \
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080078 --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
79 --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
80 --version 1 \
Mandeep Singh Baines981555b2011-06-01 16:36:54 -070081 --config "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt" \
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080082 --bootloader "${bootloader_path}" \
83 --vmlinuz "${kernel_image}" \
84 --arch "${FLAGS_arch}"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080085}
86
Mike Frysinger6b1abb22012-05-11 13:44:06 -040087copy_kernelimage() {
Kees Cook43a32132011-10-18 13:17:11 -070088 remote_cp_to $TMP/new_kern.bin /tmp
Olof Johansson8488f5a2011-04-20 17:27:37 -070089 remote_sh dd if=/tmp/new_kern.bin of="${FLAGS_partition}"
90}
91
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -040092check_kernelbuildtime() {
93 local version=$(readlink "/build/${FLAGS_board}/boot/vmlinuz" | cut -d- -f2-)
94 local build_dir="/build/${FLAGS_board}/lib/modules/${version}/build"
95 if [ "${build_dir}/Makefile" -nt "/build/${FLAGS_board}/boot/vmlinuz" ]; then
96 warn "Your build directory has been built more recently than"
97 warn "the installed kernel being updated to. Did you forget to"
98 warn "run 'cros_workon_make chromeos-kernel --install'?"
99 fi
100}
101
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400102main() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800103 trap cleanup EXIT
104
Kees Cook43a32132011-10-18 13:17:11 -0700105 TMP=$(mktemp -d /tmp/update_kernel.XXXXXX)
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800106
107 remote_access_init
108
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800109 learn_arch
110
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800111 learn_board
112
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800113 learn_device
114
Olof Johansson8488f5a2011-04-20 17:27:37 -0700115 learn_partition_and_ro
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800116
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800117 remote_sh uname -r -v
118
119 old_kernel="${REMOTE_OUT}"
120
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -0400121 check_kernelbuildtime
122
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800123 make_kernelimage
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800124
Olof Johansson8488f5a2011-04-20 17:27:37 -0700125 if [[ ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]]; then
Kees Cook43a32132011-10-18 13:17:11 -0700126 tar -C /build/"${FLAGS_board}"/lib/modules -cjf $TMP/new_modules.tar .
127 tar -C /build/"${FLAGS_board}"/lib/firmware -cjf $TMP/new_firmware.tar .
128 tar -C /build/"${FLAGS_board}"/boot -cjf $TMP/new_boot.tar .
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800129
Olof Johansson8488f5a2011-04-20 17:27:37 -0700130 remote_sh mount -o remount,rw /
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700131 echo "copying kernel"
Kees Cook43a32132011-10-18 13:17:11 -0700132 remote_cp_to $TMP/new_boot.tar /tmp/
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700133 remote_sh tar -C /boot -xjf /tmp/new_boot.tar
134
135 # ARM does not have the syslinux directory, so skip it when the
136 # partition or the syslinux vmlinuz target is missing.
137 echo "updating syslinux kernel"
138 remote_sh grep $(echo ${FLAGS_device}12 | cut -d/ -f3) /proc/partitions
139 if [ $(echo "$REMOTE_OUT" | wc -l) -eq 1 ]; then
140 remote_sh mkdir -p /tmp/12
141 remote_sh mount ${FLAGS_device}12 /tmp/12
142
143 if [ "$FLAGS_partition" = "${FLAGS_device}2" ]; then
144 target="/tmp/12/syslinux/vmlinuz.A"
145 else
146 target="/tmp/12/syslinux/vmlinuz.B"
147 fi
148 remote_sh "test ! -f $target || cp /boot/vmlinuz $target"
149
150 remote_sh umount /tmp/12
151 remote_sh rmdir /tmp/12
152 fi
Olof Johansson9a83e4e2012-08-17 02:45:12 -0700153
154 echo "copying modules"
155 remote_cp_to $TMP/new_modules.tar /tmp/
156 remote_sh tar -C /lib/modules -xjf /tmp/new_modules.tar
157
158 echo "copying firmware"
159 remote_cp_to $TMP/new_firmware.tar /tmp/
160 remote_sh tar -C /lib/firmware -xjf /tmp/new_firmware.tar
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800161 fi
162
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700163 echo "copying kernel image"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800164
Olof Johansson8488f5a2011-04-20 17:27:37 -0700165 copy_kernelimage
166
Jonathan Kliegmand6f3d072012-07-12 12:45:33 -0400167 # An early kernel panic can prevent the normal sync on reboot. Explicitly
168 # sync for safety to avoid random file system corruption.
169 remote_sh sync
170
Olof Johansson8488f5a2011-04-20 17:27:37 -0700171 if [ "${FLAGS_reboot}" -eq ${FLAGS_TRUE} ]; then
172 echo "rebooting"
173
174 remote_reboot
175
176 remote_sh uname -r -v
177 info "old kernel: ${old_kernel}"
178 info "new kernel: ${REMOTE_OUT}"
179 else
180 info "Not rebooting (per request)"
181 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800182}
183
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800184main "$@"