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 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 9 | # --- 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. |
| 13 | find_common_sh() { |
| 14 | local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")")) |
| 15 | local path |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 16 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 17 | 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 | |
| 26 | find_common_sh |
Brian Harring | d5d5dbf | 2011-07-11 16:36:21 -0700 | [diff] [blame] | 27 | . "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load common.sh"; exit 1; } |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 28 | # --- END COMMON.SH BOILERPLATE --- |
| 29 | |
| 30 | . "${SCRIPT_ROOT}/remote_access.sh" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 31 | |
Mandeep Singh Baines | 2f3b5fc | 2011-01-14 14:20:12 -0800 | [diff] [blame] | 32 | # Script must be run inside the chroot. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 33 | restart_in_chroot_if_needed "$@" |
Mandeep Singh Baines | 2f3b5fc | 2011-01-14 14:20:12 -0800 | [diff] [blame] | 34 | |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 35 | DEFINE_string board "" "Override board reported by target" |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 36 | DEFINE_string device "" "Override boot device reported by target" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 37 | DEFINE_string partition "" "Override kernel partition reported by target" |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 38 | DEFINE_string arch "" "Override architecture reported by target" |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 39 | DEFINE_boolean reboot $FLAGS_TRUE "Reboot system after update" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 40 | |
Mandeep Singh Baines | 2f3b5fc | 2011-01-14 14:20:12 -0800 | [diff] [blame] | 41 | # Parse command line. |
| 42 | FLAGS "$@" || exit 1 |
| 43 | eval 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. |
| 47 | set -e |
| 48 | |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 49 | function cleanup { |
| 50 | cleanup_remote_access |
| 51 | rm -rf "${TMP}" |
| 52 | } |
| 53 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 54 | function 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 Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 61 | # Ask the target what the kernel partition is |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 62 | function learn_partition_and_ro() { |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 63 | [ -n "${FLAGS_partition}" ] && return |
Mandeep Singh Baines | e39579a | 2011-03-04 15:58:57 -0800 | [diff] [blame] | 64 | ! 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 Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 68 | 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 Baines | e39579a | 2011-03-04 15:58:57 -0800 | [diff] [blame] | 73 | fi |
| 74 | if [ "${REMOTE_OUT}" == "${FLAGS_device}3" ]; then |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 75 | FLAGS_partition="${FLAGS_device}2" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 76 | else |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 77 | FLAGS_partition="${FLAGS_device}4" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 78 | 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 Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 86 | function make_kernelimage() { |
Tom Wai-Hong Tam | 6b50a07 | 2011-05-25 17:00:15 +0800 | [diff] [blame] | 87 | local bootloader_path |
| 88 | local kernel_image |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 89 | if [[ "${FLAGS_arch}" == "arm" ]]; then |
Taylor Hutt | 60da642 | 2011-06-02 13:54:43 -0700 | [diff] [blame] | 90 | name="bootloader.bin" |
| 91 | bootloader_path="${SRC_ROOT}/build/images/${FLAGS_board}/latest/${name}" |
Tom Wai-Hong Tam | 6b50a07 | 2011-05-25 17:00:15 +0800 | [diff] [blame] | 92 | kernel_image="/build/${FLAGS_board}/boot/vmlinux.uimg" |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 93 | else |
Tom Wai-Hong Tam | 6b50a07 | 2011-05-25 17:00:15 +0800 | [diff] [blame] | 94 | bootloader_path="/lib64/bootstub/bootstub.efi" |
| 95 | kernel_image="/build/${FLAGS_board}/boot/vmlinuz" |
| 96 | fi |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame^] | 97 | vbutil_kernel --pack $TMP/new_kern.bin \ |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 98 | --keyblock /usr/share/vboot/devkeys/kernel.keyblock \ |
| 99 | --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \ |
| 100 | --version 1 \ |
Mandeep Singh Baines | 981555b | 2011-06-01 16:36:54 -0700 | [diff] [blame] | 101 | --config "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt" \ |
Tom Wai-Hong Tam | 6b50a07 | 2011-05-25 17:00:15 +0800 | [diff] [blame] | 102 | --bootloader "${bootloader_path}" \ |
| 103 | --vmlinuz "${kernel_image}" \ |
| 104 | --arch "${FLAGS_arch}" |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 107 | function copy_kernelimage() { |
| 108 | if [ "${FLAGS_arch}" == "arm" -a ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]; then |
| 109 | remote_cp_to /build/${FLAGS_board}/boot/vmlinux.uimg /boot |
| 110 | fi |
| 111 | |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame^] | 112 | remote_cp_to $TMP/new_kern.bin /tmp |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 113 | |
| 114 | remote_sh dd if=/tmp/new_kern.bin of="${FLAGS_partition}" |
| 115 | } |
| 116 | |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 117 | function main() { |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 118 | trap cleanup EXIT |
| 119 | |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame^] | 120 | TMP=$(mktemp -d /tmp/update_kernel.XXXXXX) |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 121 | |
| 122 | remote_access_init |
| 123 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 124 | learn_arch |
| 125 | |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 126 | learn_board |
| 127 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 128 | learn_device |
| 129 | |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 130 | learn_partition_and_ro |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 131 | |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 132 | remote_sh uname -r -v |
| 133 | |
| 134 | old_kernel="${REMOTE_OUT}" |
| 135 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 136 | make_kernelimage |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 137 | |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 138 | if [[ ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]]; then |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame^] | 139 | tar -C /build/"${FLAGS_board}"/lib/modules -cjf $TMP/new_modules.tar . |
| 140 | tar -C /build/"${FLAGS_board}"/lib/firmware -cjf $TMP/new_firmware.tar . |
| 141 | tar -C /build/"${FLAGS_board}"/boot -cjf $TMP/new_boot.tar . |
Olof Johansson | 5a46bfb | 2010-12-22 12:14:21 -0800 | [diff] [blame] | 142 | |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 143 | remote_sh mount -o remount,rw / |
| 144 | echo "copying modules" |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame^] | 145 | remote_cp_to $TMP/new_modules.tar /tmp/ |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 146 | remote_sh tar -C /lib/modules -xjf /tmp/new_modules.tar |
| 147 | |
| 148 | echo "copying firmware" |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame^] | 149 | remote_cp_to $TMP/new_firmware.tar /tmp/ |
Olof Johansson | 5a46bfb | 2010-12-22 12:14:21 -0800 | [diff] [blame] | 150 | remote_sh tar -C /lib/firmware -xjf /tmp/new_firmware.tar |
Kees Cook | 7d7d2ef | 2011-10-18 13:12:12 -0700 | [diff] [blame] | 151 | |
| 152 | echo "copying kernel" |
Kees Cook | 43a3213 | 2011-10-18 13:17:11 -0700 | [diff] [blame^] | 153 | remote_cp_to $TMP/new_boot.tar /tmp/ |
Kees Cook | 7d7d2ef | 2011-10-18 13:12:12 -0700 | [diff] [blame] | 154 | remote_sh tar -C /boot -xjf /tmp/new_boot.tar |
| 155 | |
| 156 | # ARM does not have the syslinux directory, so skip it when the |
| 157 | # partition or the syslinux vmlinuz target is missing. |
| 158 | echo "updating syslinux kernel" |
| 159 | remote_sh grep $(echo ${FLAGS_device}12 | cut -d/ -f3) /proc/partitions |
| 160 | if [ $(echo "$REMOTE_OUT" | wc -l) -eq 1 ]; then |
| 161 | remote_sh mkdir -p /tmp/12 |
| 162 | remote_sh mount ${FLAGS_device}12 /tmp/12 |
| 163 | |
| 164 | if [ "$FLAGS_partition" = "${FLAGS_device}2" ]; then |
| 165 | target="/tmp/12/syslinux/vmlinuz.A" |
| 166 | else |
| 167 | target="/tmp/12/syslinux/vmlinuz.B" |
| 168 | fi |
| 169 | remote_sh "test ! -f $target || cp /boot/vmlinuz $target" |
| 170 | |
| 171 | remote_sh umount /tmp/12 |
| 172 | remote_sh rmdir /tmp/12 |
| 173 | fi |
Olof Johansson | 5a46bfb | 2010-12-22 12:14:21 -0800 | [diff] [blame] | 174 | fi |
| 175 | |
Kees Cook | 7d7d2ef | 2011-10-18 13:12:12 -0700 | [diff] [blame] | 176 | echo "copying kernel image" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 177 | |
Olof Johansson | 8488f5a | 2011-04-20 17:27:37 -0700 | [diff] [blame] | 178 | copy_kernelimage |
| 179 | |
| 180 | if [ "${FLAGS_reboot}" -eq ${FLAGS_TRUE} ]; then |
| 181 | echo "rebooting" |
| 182 | |
| 183 | remote_reboot |
| 184 | |
| 185 | remote_sh uname -r -v |
| 186 | info "old kernel: ${old_kernel}" |
| 187 | info "new kernel: ${REMOTE_OUT}" |
| 188 | else |
| 189 | info "Not rebooting (per request)" |
| 190 | fi |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 193 | main "$@" |