blob: 93a44d92df7a47179e94f1d3b780b2d67d4eb6d7 [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"
Doug Andersonfcaed8a2014-07-09 11:34:29 -070019DEFINE_string rootoff "" "Override root offset"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080020DEFINE_string arch "" "Override architecture reported by target"
Olof Johansson8488f5a2011-04-20 17:27:37 -070021DEFINE_boolean reboot $FLAGS_TRUE "Reboot system after update"
Doug Anderson5a21b442012-12-07 11:47:31 -080022DEFINE_boolean vboot $FLAGS_TRUE "Update the vboot kernel"
Olof Johansson68cbfaf2013-04-23 14:06:28 -070023DEFINE_boolean syslinux $FLAGS_TRUE "Update the syslinux kernel"
Olof Johansson45225c92013-10-15 17:32:48 -070024DEFINE_boolean bootonce $FLAGS_FALSE "Mark kernel partition as boot once"
Olof Johansson4a7b2882013-10-16 11:59:58 -070025DEFINE_boolean remote_bootargs $FLAGS_FALSE "Use bootargs from running kernel on target"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080026
Doug Anderson549f3b52013-09-26 14:46:18 -070027ORIG_ARGS=("$@")
28
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080029# Parse command line.
30FLAGS "$@" || exit 1
31eval set -- "${FLAGS_ARGV}"
32
33# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080034# so will die prematurely if 'switch_to_strict_mode' is specified before now.
35switch_to_strict_mode
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080036
Mike Frysinger6b1abb22012-05-11 13:44:06 -040037cleanup() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080038 cleanup_remote_access
39 rm -rf "${TMP}"
40}
41
Mike Frysinger6b1abb22012-05-11 13:44:06 -040042learn_device() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080043 [ -n "${FLAGS_device}" ] && return
44 remote_sh df /mnt/stateful_partition
45 FLAGS_device=$(echo "${REMOTE_OUT}" | awk '/dev/ {print $1}' | sed s/1\$//)
46 info "Target reports root device is ${FLAGS_device}"
47}
48
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080049# Ask the target what the kernel partition is
Mike Frysinger6b1abb22012-05-11 13:44:06 -040050learn_partition_and_ro() {
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080051 ! remote_sh rootdev
Paul Taysoma64d9db2012-09-21 13:30:43 -070052 if [ "${REMOTE_OUT%%-*}" == "/dev/dm" ]; then
53 remote_sh rootdev -s
Olof Johansson8488f5a2011-04-20 17:27:37 -070054 REMOTE_VERITY=${FLAGS_TRUE}
Doug Anderson2023fb82013-02-13 11:53:55 -080055 warn "System is using verity: not updating firmware/modules"
Olof Johansson8488f5a2011-04-20 17:27:37 -070056 else
57 REMOTE_VERITY=${FLAGS_FALSE}
58 info "System is not using verity: updating firmware and modules"
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080059 fi
Olof Johansson4996bfb2013-11-13 12:58:52 -080060 [ -n "${FLAGS_partition}" ] && return
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080061 if [ "${REMOTE_OUT}" == "${FLAGS_device}3" ]; then
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080062 FLAGS_partition="${FLAGS_device}2"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080063 else
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080064 FLAGS_partition="${FLAGS_device}4"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080065 fi
66 if [ -z "${FLAGS_partition}" ]; then
Doug Andersonb2fe4652012-12-07 17:33:56 -080067 die "Partition required"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080068 fi
Doug Anderson5a21b442012-12-07 11:47:31 -080069 if [ ${REMOTE_VERITY} -eq ${FLAGS_TRUE} ]; then
70 info "Target reports kernel partition is ${FLAGS_partition}"
71 if [ ${FLAGS_vboot} -eq ${FLAGS_FALSE} ]; then
72 die "Must update vboot when target is using verity"
73 fi
74 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080075}
76
Olof Johansson4a7b2882013-10-16 11:59:58 -070077get_bootargs() {
78 if [ ${FLAGS_remote_bootargs} -eq ${FLAGS_TRUE} ] ; then
79 info "Using remote bootargs"
80 remote_sh cat /proc/cmdline && echo "${REMOTE_OUT}"
81 else
Doug Andersonfcaed8a2014-07-09 11:34:29 -070082 if [ -n "${FLAGS_rootoff}" ]; then
83 sed "s/PARTNROFF=1/PARTNROFF=${FLAGS_rootoff}/" \
84 "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt"
85 else
86 cat "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt"
87 fi
Olof Johansson4a7b2882013-10-16 11:59:58 -070088 fi
89}
90
Mike Frysinger6b1abb22012-05-11 13:44:06 -040091make_kernelimage() {
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080092 local bootloader_path
93 local kernel_image
Olof Johansson4a7b2882013-10-16 11:59:58 -070094 local config_path="$(mktemp /tmp/config.txt.XXXXX)"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080095 if [[ "${FLAGS_arch}" == "arm" ]]; then
Taylor Hutt60da6422011-06-02 13:54:43 -070096 name="bootloader.bin"
97 bootloader_path="${SRC_ROOT}/build/images/${FLAGS_board}/latest/${name}"
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080098 kernel_image="/build/${FLAGS_board}/boot/vmlinux.uimg"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080099 else
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800100 bootloader_path="/lib64/bootstub/bootstub.efi"
101 kernel_image="/build/${FLAGS_board}/boot/vmlinuz"
102 fi
Olof Johansson4a7b2882013-10-16 11:59:58 -0700103 get_bootargs > "${config_path}"
Kees Cook43a32132011-10-18 13:17:11 -0700104 vbutil_kernel --pack $TMP/new_kern.bin \
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800105 --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
106 --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
107 --version 1 \
Olof Johansson4a7b2882013-10-16 11:59:58 -0700108 --config ${config_path} \
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800109 --bootloader "${bootloader_path}" \
110 --vmlinuz "${kernel_image}" \
111 --arch "${FLAGS_arch}"
Olof Johansson4a7b2882013-10-16 11:59:58 -0700112 rm "${config_path}"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800113}
114
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400115copy_kernelimage() {
Doug Andersonadf8a002012-12-17 11:40:34 -0800116 remote_sh dd of="${FLAGS_partition}" bs=4K < "${TMP}/new_kern.bin"
Olof Johansson8488f5a2011-04-20 17:27:37 -0700117}
118
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -0400119check_kernelbuildtime() {
120 local version=$(readlink "/build/${FLAGS_board}/boot/vmlinuz" | cut -d- -f2-)
121 local build_dir="/build/${FLAGS_board}/lib/modules/${version}/build"
122 if [ "${build_dir}/Makefile" -nt "/build/${FLAGS_board}/boot/vmlinuz" ]; then
123 warn "Your build directory has been built more recently than"
124 warn "the installed kernel being updated to. Did you forget to"
125 warn "run 'cros_workon_make chromeos-kernel --install'?"
126 fi
127}
128
Olof Johansson45225c92013-10-15 17:32:48 -0700129mark_boot_once() {
130 local idx=${FLAGS_partition##*[^0-9]}
Chirantan Ekbotef25b4402014-05-06 12:42:18 -0700131 remote_sh cgpt add -i ${idx} -S 0 -T 1 -P 15 ${FLAGS_device%p}
Olof Johansson45225c92013-10-15 17:32:48 -0700132}
133
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700134update_syslinux_kernel() {
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
153}
154
Doug Anderson549f3b52013-09-26 14:46:18 -0700155multi_main() {
156 local host
157
158 IFS=","
159 for host in ${FLAGS_remote}; do
160 "$0" "${ORIG_ARGS[@]}" --remote="${host}" \
161 |& sed "s/^/${V_BOLD_YELLOW}${host}: ${V_VIDOFF}/" &
162 done
163 wait
164}
165
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400166main() {
Doug Anderson549f3b52013-09-26 14:46:18 -0700167 # If there are commas in the --remote, run the script in parallel.
168 if [[ ${FLAGS_remote} == *,* ]]; then
169 multi_main
170 return $?
171 fi
172
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800173 trap cleanup EXIT
174
Kees Cook43a32132011-10-18 13:17:11 -0700175 TMP=$(mktemp -d /tmp/update_kernel.XXXXXX)
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800176
177 remote_access_init
178
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800179 learn_arch
180
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800181 learn_board
182
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800183 learn_device
184
Olof Johansson8488f5a2011-04-20 17:27:37 -0700185 learn_partition_and_ro
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800186
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800187 remote_sh uname -r -v
188
189 old_kernel="${REMOTE_OUT}"
190
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -0400191 check_kernelbuildtime
192
Doug Anderson5a21b442012-12-07 11:47:31 -0800193 if [ ${FLAGS_vboot} -eq ${FLAGS_TRUE} ]; then
194 make_kernelimage
195 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800196
Olof Johansson8488f5a2011-04-20 17:27:37 -0700197 if [[ ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]]; then
Olof Johansson8488f5a2011-04-20 17:27:37 -0700198 remote_sh mount -o remount,rw /
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700199 echo "copying kernel"
Doug Anderson48b52002012-12-07 12:40:07 -0800200 remote_send_to /build/"${FLAGS_board}"/boot/ /boot/
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700201
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700202 if [ ${FLAGS_syslinux} -eq ${FLAGS_TRUE} ]; then
203 update_syslinux_kernel
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700204 fi
Olof Johansson9a83e4e2012-08-17 02:45:12 -0700205
206 echo "copying modules"
Doug Anderson48b52002012-12-07 12:40:07 -0800207 remote_send_to /build/"${FLAGS_board}"/lib/modules/ /lib/modules/
Olof Johansson9a83e4e2012-08-17 02:45:12 -0700208
209 echo "copying firmware"
Doug Anderson48b52002012-12-07 12:40:07 -0800210 remote_send_to /build/"${FLAGS_board}"/lib/firmware/ /lib/firmware/
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800211 fi
212
Doug Anderson5a21b442012-12-07 11:47:31 -0800213 if [ ${FLAGS_vboot} -eq ${FLAGS_TRUE} ]; then
214 info "Copying vboot kernel image"
215 copy_kernelimage
216 else
217 info "Skipping update of vboot (per request)"
218 fi
Olof Johansson8488f5a2011-04-20 17:27:37 -0700219
Olof Johansson45225c92013-10-15 17:32:48 -0700220 if [ ${FLAGS_bootonce} -eq ${FLAGS_TRUE} ]; then
221 info "Marking kernel partition ${FLAGS_partition} as boot once"
222 mark_boot_once
223 fi
224
Jonathan Kliegmand6f3d072012-07-12 12:45:33 -0400225 # An early kernel panic can prevent the normal sync on reboot. Explicitly
226 # sync for safety to avoid random file system corruption.
227 remote_sh sync
228
Doug Andersonb2fe4652012-12-07 17:33:56 -0800229 if [ ${FLAGS_reboot} -eq ${FLAGS_TRUE} ]; then
Olof Johansson8488f5a2011-04-20 17:27:37 -0700230 remote_reboot
231
232 remote_sh uname -r -v
233 info "old kernel: ${old_kernel}"
234 info "new kernel: ${REMOTE_OUT}"
235 else
236 info "Not rebooting (per request)"
237 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800238}
239
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800240main "$@"