Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Taylor Hutt | 60da642 | 2011-06-02 13:54:43 -0700 | [diff] [blame] | 3 | # Copyright (c) 2009-2011 The Chromium OS Authors. All rights reserved. |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 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 | |
Brian Harring | aa13ea4 | 2012-03-15 18:31:03 -0700 | [diff] [blame] | 9 | SCRIPT_ROOT=$(dirname $(readlink -f "$0")) |
David James | 359d3e1 | 2012-07-10 13:09:48 -0700 | [diff] [blame] | 10 | . "${SCRIPT_ROOT}/common.sh" || exit 1 |
| 11 | . "${SCRIPT_ROOT}/remote_access.sh" || exit 1 |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 12 | |
Mandeep Singh Baines | 2f3b5fc | 2011-01-14 14:20:12 -0800 | [diff] [blame] | 13 | # Script must be run inside the chroot. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 14 | restart_in_chroot_if_needed "$@" |
Mandeep Singh Baines | 2f3b5fc | 2011-01-14 14:20:12 -0800 | [diff] [blame] | 15 | |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 16 | DEFINE_string board "" "Override board reported by target" |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 17 | DEFINE_string device "" "Override boot device reported by target" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 18 | DEFINE_string partition "" "Override kernel partition reported by target" |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 19 | DEFINE_string arch "" "Override architecture reported by target" |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 20 | DEFINE_boolean reboot $FLAGS_TRUE "Reboot system after update" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 21 | |
Mandeep Singh Baines | 2f3b5fc | 2011-01-14 14:20:12 -0800 | [diff] [blame] | 22 | # Parse command line. |
| 23 | FLAGS "$@" || exit 1 |
| 24 | eval set -- "${FLAGS_ARGV}" |
| 25 | |
| 26 | # Only now can we die on error. shflags functions leak non-zero error codes, |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 27 | # so will die prematurely if 'switch_to_strict_mode' is specified before now. |
| 28 | switch_to_strict_mode |
Mandeep Singh Baines | 2f3b5fc | 2011-01-14 14:20:12 -0800 | [diff] [blame] | 29 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 30 | cleanup() { |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 31 | cleanup_remote_access |
| 32 | rm -rf "${TMP}" |
| 33 | } |
| 34 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 35 | learn_device() { |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 36 | [ -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 Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 42 | # Ask the target what the kernel partition is |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 43 | learn_partition_and_ro() { |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 44 | [ -n "${FLAGS_partition}" ] && return |
Mandeep Singh Baines | e39579a | 2011-03-04 15:58:57 -0800 | [diff] [blame] | 45 | ! remote_sh rootdev |
| 46 | if [ "${REMOTE_OUT}" == "/dev/dm-0" ]; then |
| 47 | remote_sh ls /sys/block/dm-0/slaves |
| 48 | REMOTE_OUT="/dev/${REMOTE_OUT}" |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 49 | 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 Baines | e39579a | 2011-03-04 15:58:57 -0800 | [diff] [blame] | 54 | fi |
| 55 | if [ "${REMOTE_OUT}" == "${FLAGS_device}3" ]; then |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 56 | FLAGS_partition="${FLAGS_device}2" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 57 | else |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 58 | FLAGS_partition="${FLAGS_device}4" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 59 | fi |
| 60 | if [ -z "${FLAGS_partition}" ]; then |
| 61 | error "Partition required" |
| 62 | exit 1 |
| 63 | fi |
| 64 | info "Target reports kernel partition is ${FLAGS_partition}" |
| 65 | } |
| 66 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 67 | make_kernelimage() { |
Tom Wai-Hong Tam | 6b50a07 | 2011-05-25 17:00:15 +0800 | [diff] [blame] | 68 | local bootloader_path |
| 69 | local kernel_image |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 70 | if [[ "${FLAGS_arch}" == "arm" ]]; then |
Taylor Hutt | 60da642 | 2011-06-02 13:54:43 -0700 | [diff] [blame] | 71 | name="bootloader.bin" |
| 72 | bootloader_path="${SRC_ROOT}/build/images/${FLAGS_board}/latest/${name}" |
Tom Wai-Hong Tam | 6b50a07 | 2011-05-25 17:00:15 +0800 | [diff] [blame] | 73 | kernel_image="/build/${FLAGS_board}/boot/vmlinux.uimg" |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 74 | else |
Tom Wai-Hong Tam | 6b50a07 | 2011-05-25 17:00:15 +0800 | [diff] [blame] | 75 | bootloader_path="/lib64/bootstub/bootstub.efi" |
| 76 | kernel_image="/build/${FLAGS_board}/boot/vmlinuz" |
| 77 | fi |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame] | 78 | vbutil_kernel --pack $TMP/new_kern.bin \ |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 79 | --keyblock /usr/share/vboot/devkeys/kernel.keyblock \ |
| 80 | --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \ |
| 81 | --version 1 \ |
Mandeep Singh Baines | 981555b | 2011-06-01 16:36:54 -0700 | [diff] [blame] | 82 | --config "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt" \ |
Tom Wai-Hong Tam | 6b50a07 | 2011-05-25 17:00:15 +0800 | [diff] [blame] | 83 | --bootloader "${bootloader_path}" \ |
| 84 | --vmlinuz "${kernel_image}" \ |
| 85 | --arch "${FLAGS_arch}" |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 88 | copy_kernelimage() { |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 89 | if [ "${FLAGS_arch}" == "arm" -a ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]; then |
| 90 | remote_cp_to /build/${FLAGS_board}/boot/vmlinux.uimg /boot |
| 91 | fi |
| 92 | |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame] | 93 | remote_cp_to $TMP/new_kern.bin /tmp |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 94 | |
| 95 | remote_sh dd if=/tmp/new_kern.bin of="${FLAGS_partition}" |
| 96 | } |
| 97 | |
Jonathan Kliegman | 775bc8e | 2012-08-14 12:30:49 -0400 | [diff] [blame] | 98 | check_kernelbuildtime() { |
| 99 | local version=$(readlink "/build/${FLAGS_board}/boot/vmlinuz" | cut -d- -f2-) |
| 100 | local build_dir="/build/${FLAGS_board}/lib/modules/${version}/build" |
| 101 | if [ "${build_dir}/Makefile" -nt "/build/${FLAGS_board}/boot/vmlinuz" ]; then |
| 102 | warn "Your build directory has been built more recently than" |
| 103 | warn "the installed kernel being updated to. Did you forget to" |
| 104 | warn "run 'cros_workon_make chromeos-kernel --install'?" |
| 105 | fi |
| 106 | } |
| 107 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 108 | main() { |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 109 | trap cleanup EXIT |
| 110 | |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame] | 111 | TMP=$(mktemp -d /tmp/update_kernel.XXXXXX) |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 112 | |
| 113 | remote_access_init |
| 114 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 115 | learn_arch |
| 116 | |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 117 | learn_board |
| 118 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 119 | learn_device |
| 120 | |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 121 | learn_partition_and_ro |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 122 | |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 123 | remote_sh uname -r -v |
| 124 | |
| 125 | old_kernel="${REMOTE_OUT}" |
| 126 | |
Jonathan Kliegman | 775bc8e | 2012-08-14 12:30:49 -0400 | [diff] [blame] | 127 | check_kernelbuildtime |
| 128 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 129 | make_kernelimage |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 130 | |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 131 | if [[ ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]]; then |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame] | 132 | tar -C /build/"${FLAGS_board}"/lib/modules -cjf $TMP/new_modules.tar . |
| 133 | tar -C /build/"${FLAGS_board}"/lib/firmware -cjf $TMP/new_firmware.tar . |
| 134 | tar -C /build/"${FLAGS_board}"/boot -cjf $TMP/new_boot.tar . |
Olof Johansson | 5a46bfb | 2010-12-22 12:14:21 -0800 | [diff] [blame] | 135 | |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 136 | remote_sh mount -o remount,rw / |
Kees Cook | 7d7d2ef | 2011-10-18 13:12:12 -0700 | [diff] [blame] | 137 | echo "copying kernel" |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame] | 138 | remote_cp_to $TMP/new_boot.tar /tmp/ |
Kees Cook | 7d7d2ef | 2011-10-18 13:12:12 -0700 | [diff] [blame] | 139 | remote_sh tar -C /boot -xjf /tmp/new_boot.tar |
| 140 | |
| 141 | # ARM does not have the syslinux directory, so skip it when the |
| 142 | # partition or the syslinux vmlinuz target is missing. |
| 143 | echo "updating syslinux kernel" |
| 144 | remote_sh grep $(echo ${FLAGS_device}12 | cut -d/ -f3) /proc/partitions |
| 145 | if [ $(echo "$REMOTE_OUT" | wc -l) -eq 1 ]; then |
| 146 | remote_sh mkdir -p /tmp/12 |
| 147 | remote_sh mount ${FLAGS_device}12 /tmp/12 |
| 148 | |
| 149 | if [ "$FLAGS_partition" = "${FLAGS_device}2" ]; then |
| 150 | target="/tmp/12/syslinux/vmlinuz.A" |
| 151 | else |
| 152 | target="/tmp/12/syslinux/vmlinuz.B" |
| 153 | fi |
| 154 | remote_sh "test ! -f $target || cp /boot/vmlinuz $target" |
| 155 | |
| 156 | remote_sh umount /tmp/12 |
| 157 | remote_sh rmdir /tmp/12 |
| 158 | fi |
Olof Johansson | 9a83e4e | 2012-08-17 02:45:12 -0700 | [diff] [blame] | 159 | |
| 160 | echo "copying modules" |
| 161 | remote_cp_to $TMP/new_modules.tar /tmp/ |
| 162 | remote_sh tar -C /lib/modules -xjf /tmp/new_modules.tar |
| 163 | |
| 164 | echo "copying firmware" |
| 165 | remote_cp_to $TMP/new_firmware.tar /tmp/ |
| 166 | remote_sh tar -C /lib/firmware -xjf /tmp/new_firmware.tar |
Olof Johansson | 5a46bfb | 2010-12-22 12:14:21 -0800 | [diff] [blame] | 167 | fi |
| 168 | |
Kees Cook | 7d7d2ef | 2011-10-18 13:12:12 -0700 | [diff] [blame] | 169 | echo "copying kernel image" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 170 | |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 171 | copy_kernelimage |
| 172 | |
Jonathan Kliegman | d6f3d07 | 2012-07-12 12:45:33 -0400 | [diff] [blame] | 173 | # An early kernel panic can prevent the normal sync on reboot. Explicitly |
| 174 | # sync for safety to avoid random file system corruption. |
| 175 | remote_sh sync |
| 176 | |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 177 | if [ "${FLAGS_reboot}" -eq ${FLAGS_TRUE} ]; then |
| 178 | echo "rebooting" |
| 179 | |
| 180 | remote_reboot |
| 181 | |
| 182 | remote_sh uname -r -v |
| 183 | info "old kernel: ${old_kernel}" |
| 184 | info "new kernel: ${REMOTE_OUT}" |
| 185 | else |
| 186 | info "Not rebooting (per request)" |
| 187 | fi |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 190 | main "$@" |