blob: 6d0156154aaf3db357c3abd558d34c6e19b02d49 [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"))
Brian Harringd5d5dbf2011-07-11 16:36:21 -070010. "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load common.sh"; exit 1; }
Greg Spencer798d75f2011-02-01 22:04:49 -080011. "${SCRIPT_ROOT}/remote_access.sh"
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,
27# so will die prematurely if 'set -e' is specified before now.
28set -e
29
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080030function cleanup {
31 cleanup_remote_access
32 rm -rf "${TMP}"
33}
34
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080035function learn_device() {
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 Bainesa63cd2d2010-12-02 11:58:26 -080042# Ask the target what the kernel partition is
Olof Johansson8488f5a2011-04-20 17:27:37 -070043function learn_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
46 if [ "${REMOTE_OUT}" == "/dev/dm-0" ]; then
47 remote_sh ls /sys/block/dm-0/slaves
48 REMOTE_OUT="/dev/${REMOTE_OUT}"
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
61 error "Partition required"
62 exit 1
63 fi
64 info "Target reports kernel partition is ${FLAGS_partition}"
65}
66
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080067function make_kernelimage() {
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080068 local bootloader_path
69 local kernel_image
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080070 if [[ "${FLAGS_arch}" == "arm" ]]; then
Taylor Hutt60da6422011-06-02 13:54:43 -070071 name="bootloader.bin"
72 bootloader_path="${SRC_ROOT}/build/images/${FLAGS_board}/latest/${name}"
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080073 kernel_image="/build/${FLAGS_board}/boot/vmlinux.uimg"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080074 else
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080075 bootloader_path="/lib64/bootstub/bootstub.efi"
76 kernel_image="/build/${FLAGS_board}/boot/vmlinuz"
77 fi
Kees Cook43a32132011-10-18 13:17:11 -070078 vbutil_kernel --pack $TMP/new_kern.bin \
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080079 --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
80 --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
81 --version 1 \
Mandeep Singh Baines981555b2011-06-01 16:36:54 -070082 --config "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt" \
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080083 --bootloader "${bootloader_path}" \
84 --vmlinuz "${kernel_image}" \
85 --arch "${FLAGS_arch}"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080086}
87
Olof Johansson8488f5a2011-04-20 17:27:37 -070088function copy_kernelimage() {
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 Cook43a32132011-10-18 13:17:11 -070093 remote_cp_to $TMP/new_kern.bin /tmp
Olof Johansson8488f5a2011-04-20 17:27:37 -070094
95 remote_sh dd if=/tmp/new_kern.bin of="${FLAGS_partition}"
96}
97
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080098function main() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080099 trap cleanup EXIT
100
Kees Cook43a32132011-10-18 13:17:11 -0700101 TMP=$(mktemp -d /tmp/update_kernel.XXXXXX)
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800102
103 remote_access_init
104
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800105 learn_arch
106
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800107 learn_board
108
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800109 learn_device
110
Olof Johansson8488f5a2011-04-20 17:27:37 -0700111 learn_partition_and_ro
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800112
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800113 remote_sh uname -r -v
114
115 old_kernel="${REMOTE_OUT}"
116
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800117 make_kernelimage
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800118
Olof Johansson8488f5a2011-04-20 17:27:37 -0700119 if [[ ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]]; then
Kees Cook43a32132011-10-18 13:17:11 -0700120 tar -C /build/"${FLAGS_board}"/lib/modules -cjf $TMP/new_modules.tar .
121 tar -C /build/"${FLAGS_board}"/lib/firmware -cjf $TMP/new_firmware.tar .
122 tar -C /build/"${FLAGS_board}"/boot -cjf $TMP/new_boot.tar .
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800123
Olof Johansson8488f5a2011-04-20 17:27:37 -0700124 remote_sh mount -o remount,rw /
125 echo "copying modules"
Kees Cook43a32132011-10-18 13:17:11 -0700126 remote_cp_to $TMP/new_modules.tar /tmp/
Olof Johansson8488f5a2011-04-20 17:27:37 -0700127 remote_sh tar -C /lib/modules -xjf /tmp/new_modules.tar
128
129 echo "copying firmware"
Kees Cook43a32132011-10-18 13:17:11 -0700130 remote_cp_to $TMP/new_firmware.tar /tmp/
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800131 remote_sh tar -C /lib/firmware -xjf /tmp/new_firmware.tar
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700132
133 echo "copying kernel"
Kees Cook43a32132011-10-18 13:17:11 -0700134 remote_cp_to $TMP/new_boot.tar /tmp/
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700135 remote_sh tar -C /boot -xjf /tmp/new_boot.tar
136
137 # ARM does not have the syslinux directory, so skip it when the
138 # partition or the syslinux vmlinuz target is missing.
139 echo "updating syslinux kernel"
140 remote_sh grep $(echo ${FLAGS_device}12 | cut -d/ -f3) /proc/partitions
141 if [ $(echo "$REMOTE_OUT" | wc -l) -eq 1 ]; then
142 remote_sh mkdir -p /tmp/12
143 remote_sh mount ${FLAGS_device}12 /tmp/12
144
145 if [ "$FLAGS_partition" = "${FLAGS_device}2" ]; then
146 target="/tmp/12/syslinux/vmlinuz.A"
147 else
148 target="/tmp/12/syslinux/vmlinuz.B"
149 fi
150 remote_sh "test ! -f $target || cp /boot/vmlinuz $target"
151
152 remote_sh umount /tmp/12
153 remote_sh rmdir /tmp/12
154 fi
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800155 fi
156
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700157 echo "copying kernel image"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800158
Olof Johansson8488f5a2011-04-20 17:27:37 -0700159 copy_kernelimage
160
161 if [ "${FLAGS_reboot}" -eq ${FLAGS_TRUE} ]; then
162 echo "rebooting"
163
164 remote_reboot
165
166 remote_sh uname -r -v
167 info "old kernel: ${old_kernel}"
168 info "new kernel: ${REMOTE_OUT}"
169 else
170 info "Not rebooting (per request)"
171 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800172}
173
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800174main "$@"