blob: 4cb0bb6a8c9a7cdcc865971e04a881149891d499 [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
Doug Andersonb2fe4652012-12-07 17:33:56 -080060 die "Partition required"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080061 fi
62 info "Target reports kernel partition is ${FLAGS_partition}"
63}
64
Mike Frysinger6b1abb22012-05-11 13:44:06 -040065make_kernelimage() {
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080066 local bootloader_path
67 local kernel_image
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080068 if [[ "${FLAGS_arch}" == "arm" ]]; then
Taylor Hutt60da6422011-06-02 13:54:43 -070069 name="bootloader.bin"
70 bootloader_path="${SRC_ROOT}/build/images/${FLAGS_board}/latest/${name}"
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080071 kernel_image="/build/${FLAGS_board}/boot/vmlinux.uimg"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080072 else
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080073 bootloader_path="/lib64/bootstub/bootstub.efi"
74 kernel_image="/build/${FLAGS_board}/boot/vmlinuz"
75 fi
Kees Cook43a32132011-10-18 13:17:11 -070076 vbutil_kernel --pack $TMP/new_kern.bin \
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080077 --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
78 --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
79 --version 1 \
Mandeep Singh Baines981555b2011-06-01 16:36:54 -070080 --config "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt" \
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080081 --bootloader "${bootloader_path}" \
82 --vmlinuz "${kernel_image}" \
83 --arch "${FLAGS_arch}"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080084}
85
Mike Frysinger6b1abb22012-05-11 13:44:06 -040086copy_kernelimage() {
Kees Cook43a32132011-10-18 13:17:11 -070087 remote_cp_to $TMP/new_kern.bin /tmp
Olof Johansson8488f5a2011-04-20 17:27:37 -070088 remote_sh dd if=/tmp/new_kern.bin of="${FLAGS_partition}"
89}
90
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -040091check_kernelbuildtime() {
92 local version=$(readlink "/build/${FLAGS_board}/boot/vmlinuz" | cut -d- -f2-)
93 local build_dir="/build/${FLAGS_board}/lib/modules/${version}/build"
94 if [ "${build_dir}/Makefile" -nt "/build/${FLAGS_board}/boot/vmlinuz" ]; then
95 warn "Your build directory has been built more recently than"
96 warn "the installed kernel being updated to. Did you forget to"
97 warn "run 'cros_workon_make chromeos-kernel --install'?"
98 fi
99}
100
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400101main() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800102 trap cleanup EXIT
103
Kees Cook43a32132011-10-18 13:17:11 -0700104 TMP=$(mktemp -d /tmp/update_kernel.XXXXXX)
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800105
106 remote_access_init
107
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800108 learn_arch
109
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800110 learn_board
111
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800112 learn_device
113
Olof Johansson8488f5a2011-04-20 17:27:37 -0700114 learn_partition_and_ro
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800115
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800116 remote_sh uname -r -v
117
118 old_kernel="${REMOTE_OUT}"
119
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -0400120 check_kernelbuildtime
121
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800122 make_kernelimage
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800123
Olof Johansson8488f5a2011-04-20 17:27:37 -0700124 if [[ ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]]; then
Kees Cook43a32132011-10-18 13:17:11 -0700125 tar -C /build/"${FLAGS_board}"/lib/modules -cjf $TMP/new_modules.tar .
126 tar -C /build/"${FLAGS_board}"/lib/firmware -cjf $TMP/new_firmware.tar .
127 tar -C /build/"${FLAGS_board}"/boot -cjf $TMP/new_boot.tar .
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800128
Olof Johansson8488f5a2011-04-20 17:27:37 -0700129 remote_sh mount -o remount,rw /
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700130 echo "copying kernel"
Kees Cook43a32132011-10-18 13:17:11 -0700131 remote_cp_to $TMP/new_boot.tar /tmp/
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700132 remote_sh tar -C /boot -xjf /tmp/new_boot.tar
133
134 # ARM does not have the syslinux directory, so skip it when the
135 # partition or the syslinux vmlinuz target is missing.
136 echo "updating syslinux kernel"
137 remote_sh grep $(echo ${FLAGS_device}12 | cut -d/ -f3) /proc/partitions
138 if [ $(echo "$REMOTE_OUT" | wc -l) -eq 1 ]; then
139 remote_sh mkdir -p /tmp/12
140 remote_sh mount ${FLAGS_device}12 /tmp/12
141
142 if [ "$FLAGS_partition" = "${FLAGS_device}2" ]; then
143 target="/tmp/12/syslinux/vmlinuz.A"
144 else
145 target="/tmp/12/syslinux/vmlinuz.B"
146 fi
147 remote_sh "test ! -f $target || cp /boot/vmlinuz $target"
148
149 remote_sh umount /tmp/12
150 remote_sh rmdir /tmp/12
151 fi
Olof Johansson9a83e4e2012-08-17 02:45:12 -0700152
153 echo "copying modules"
154 remote_cp_to $TMP/new_modules.tar /tmp/
155 remote_sh tar -C /lib/modules -xjf /tmp/new_modules.tar
156
157 echo "copying firmware"
158 remote_cp_to $TMP/new_firmware.tar /tmp/
159 remote_sh tar -C /lib/firmware -xjf /tmp/new_firmware.tar
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800160 fi
161
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700162 echo "copying kernel image"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800163
Olof Johansson8488f5a2011-04-20 17:27:37 -0700164 copy_kernelimage
165
Jonathan Kliegmand6f3d072012-07-12 12:45:33 -0400166 # An early kernel panic can prevent the normal sync on reboot. Explicitly
167 # sync for safety to avoid random file system corruption.
168 remote_sh sync
169
Doug Andersonb2fe4652012-12-07 17:33:56 -0800170 if [ ${FLAGS_reboot} -eq ${FLAGS_TRUE} ]; then
Olof Johansson8488f5a2011-04-20 17:27:37 -0700171 echo "rebooting"
172
173 remote_reboot
174
175 remote_sh uname -r -v
176 info "old kernel: ${old_kernel}"
177 info "new kernel: ${REMOTE_OUT}"
178 else
179 info "Not rebooting (per request)"
180 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800181}
182
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800183main "$@"