blob: b00b0c5c284f22b8bdbe7b3b856f4f107c13bc0b [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"
Nicolas Boichatabdf6642016-07-04 11:30:44 +080021DEFINE_boolean ignore_verity $FLAGS_FALSE "Update kernel even if system is using verity"
Olof Johansson8488f5a2011-04-20 17:27:37 -070022DEFINE_boolean reboot $FLAGS_TRUE "Reboot system after update"
Doug Anderson5a21b442012-12-07 11:47:31 -080023DEFINE_boolean vboot $FLAGS_TRUE "Update the vboot kernel"
Olof Johansson68cbfaf2013-04-23 14:06:28 -070024DEFINE_boolean syslinux $FLAGS_TRUE "Update the syslinux kernel"
Olof Johansson45225c92013-10-15 17:32:48 -070025DEFINE_boolean bootonce $FLAGS_FALSE "Mark kernel partition as boot once"
Olof Johansson4a7b2882013-10-16 11:59:58 -070026DEFINE_boolean remote_bootargs $FLAGS_FALSE "Use bootargs from running kernel on target"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080027
Doug Anderson549f3b52013-09-26 14:46:18 -070028ORIG_ARGS=("$@")
29
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080030# Parse command line.
31FLAGS "$@" || exit 1
32eval set -- "${FLAGS_ARGV}"
33
34# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080035# so will die prematurely if 'switch_to_strict_mode' is specified before now.
36switch_to_strict_mode
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080037
Mike Frysinger6b1abb22012-05-11 13:44:06 -040038cleanup() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080039 cleanup_remote_access
40 rm -rf "${TMP}"
41}
42
Mike Frysinger6b1abb22012-05-11 13:44:06 -040043learn_device() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080044 [ -n "${FLAGS_device}" ] && return
45 remote_sh df /mnt/stateful_partition
46 FLAGS_device=$(echo "${REMOTE_OUT}" | awk '/dev/ {print $1}' | sed s/1\$//)
47 info "Target reports root device is ${FLAGS_device}"
48}
49
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080050# Ask the target what the kernel partition is
Mike Frysinger6b1abb22012-05-11 13:44:06 -040051learn_partition_and_ro() {
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080052 ! remote_sh rootdev
Paul Taysoma64d9db2012-09-21 13:30:43 -070053 if [ "${REMOTE_OUT%%-*}" == "/dev/dm" ]; then
54 remote_sh rootdev -s
Olof Johansson8488f5a2011-04-20 17:27:37 -070055 REMOTE_VERITY=${FLAGS_TRUE}
Nicolas Boichatabdf6642016-07-04 11:30:44 +080056 if [[ ${FLAGS_ignore_verity} -eq ${FLAGS_TRUE} ]]; then
57 warn "System is using verity: not updating firmware/modules"
58 else
59 warn "System is using verity: First remove rootfs verification using"
60 warn "/usr/share/vboot/bin/make_dev_ssd.sh --remove_rootfs_verification"
61 warn "on the DUT, or add --ignore_verity parameter to this command."
62 die
63 fi
Olof Johansson8488f5a2011-04-20 17:27:37 -070064 else
65 REMOTE_VERITY=${FLAGS_FALSE}
66 info "System is not using verity: updating firmware and modules"
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080067 fi
Olof Johansson4996bfb2013-11-13 12:58:52 -080068 [ -n "${FLAGS_partition}" ] && return
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -040069 if [ "${REMOTE_OUT}" == "${FLAGS_device}${PARTITION_NUM_ROOT_A}" ]; then
70 FLAGS_partition="${FLAGS_device}${PARTITION_NUM_KERN_A}"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080071 else
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -040072 FLAGS_partition="${FLAGS_device}${PARTITION_NUM_KERN_B}"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080073 fi
74 if [ -z "${FLAGS_partition}" ]; then
Doug Andersonb2fe4652012-12-07 17:33:56 -080075 die "Partition required"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080076 fi
Doug Anderson5a21b442012-12-07 11:47:31 -080077 if [ ${REMOTE_VERITY} -eq ${FLAGS_TRUE} ]; then
78 info "Target reports kernel partition is ${FLAGS_partition}"
79 if [ ${FLAGS_vboot} -eq ${FLAGS_FALSE} ]; then
80 die "Must update vboot when target is using verity"
81 fi
82 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080083}
84
Olof Johansson4a7b2882013-10-16 11:59:58 -070085get_bootargs() {
Mike Frysingerd4b6f952017-04-10 16:42:17 -040086 local local_config="${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt"
87
88 # Autodetect by default. https://crbug.com/316239
89 # This isn't quite right if people use --noremote_bootargs, but that's not
90 # a scenario people do today, so we won't worry about it.
91 if [[ ${FLAGS_remote_bootargs} -eq ${FLAGS_FALSE} && \
92 ! -e "${local_config}" ]]; then
93 warn "Local kernel config does not exist: ${local_config}"
94 FLAGS_remote_bootargs=${FLAGS_TRUE}
95 fi
96
Olof Johansson4a7b2882013-10-16 11:59:58 -070097 if [ ${FLAGS_remote_bootargs} -eq ${FLAGS_TRUE} ] ; then
98 info "Using remote bootargs"
99 remote_sh cat /proc/cmdline && echo "${REMOTE_OUT}"
100 else
Doug Andersonfcaed8a2014-07-09 11:34:29 -0700101 if [ -n "${FLAGS_rootoff}" ]; then
Mike Frysingerd4b6f952017-04-10 16:42:17 -0400102 sed "s/PARTNROFF=1/PARTNROFF=${FLAGS_rootoff}/" "${local_config}"
Doug Andersonfcaed8a2014-07-09 11:34:29 -0700103 else
Mike Frysingerd4b6f952017-04-10 16:42:17 -0400104 cat "${local_config}"
Doug Andersonfcaed8a2014-07-09 11:34:29 -0700105 fi
Olof Johansson4a7b2882013-10-16 11:59:58 -0700106 fi
107}
108
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400109make_kernelimage() {
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800110 local bootloader_path
111 local kernel_image
Olof Johansson4a7b2882013-10-16 11:59:58 -0700112 local config_path="$(mktemp /tmp/config.txt.XXXXX)"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800113 if [[ "${FLAGS_arch}" == "arm" ]]; then
Taylor Hutt60da6422011-06-02 13:54:43 -0700114 name="bootloader.bin"
115 bootloader_path="${SRC_ROOT}/build/images/${FLAGS_board}/latest/${name}"
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800116 kernel_image="/build/${FLAGS_board}/boot/vmlinux.uimg"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800117 else
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800118 bootloader_path="/lib64/bootstub/bootstub.efi"
119 kernel_image="/build/${FLAGS_board}/boot/vmlinuz"
120 fi
Olof Johansson4a7b2882013-10-16 11:59:58 -0700121 get_bootargs > "${config_path}"
Kees Cook43a32132011-10-18 13:17:11 -0700122 vbutil_kernel --pack $TMP/new_kern.bin \
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800123 --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
124 --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
125 --version 1 \
Olof Johansson4a7b2882013-10-16 11:59:58 -0700126 --config ${config_path} \
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800127 --bootloader "${bootloader_path}" \
128 --vmlinuz "${kernel_image}" \
129 --arch "${FLAGS_arch}"
Olof Johansson4a7b2882013-10-16 11:59:58 -0700130 rm "${config_path}"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800131}
132
Edward Hyunkoo Jeea979a902017-03-08 13:44:26 -0800133copy_kernelmodules() {
134 echo "copying modules"
135 local modules_dir=/build/"${FLAGS_board}"/lib/modules/
136 if [ ! -d "${modules_dir}" ]; then
137 info "No modules. Skipping."
138 return
139 fi
140 remote_send_to "${modules_dir}" /lib/modules/
141 local kernel_release
142 remote_sh "cd /lib/modules; echo *"
143 for kernel_release in "${REMOTE_OUT}"; do
144 local system_map="${modules_dir}"/"${kernel_release}"/build/System.map
145 if [ -r "${system_map}" ]; then
146 remote_sh mktemp -d /tmp/update_kernel_system_map_"${kernel_release}".XXXXXX
147 local temp_dir="${REMOTE_OUT}"
148 remote_cp_to "${system_map}" "${temp_dir}"
149 remote_sh depmod -ae -F "${temp_dir}"/System.map "${kernel_release}"
150 remote_sh rm -rf "${temp_dir}"
151 fi
152 done
153}
154
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400155copy_kernelimage() {
Doug Andersonadf8a002012-12-17 11:40:34 -0800156 remote_sh dd of="${FLAGS_partition}" bs=4K < "${TMP}/new_kern.bin"
Olof Johansson8488f5a2011-04-20 17:27:37 -0700157}
158
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -0400159check_kernelbuildtime() {
160 local version=$(readlink "/build/${FLAGS_board}/boot/vmlinuz" | cut -d- -f2-)
161 local build_dir="/build/${FLAGS_board}/lib/modules/${version}/build"
162 if [ "${build_dir}/Makefile" -nt "/build/${FLAGS_board}/boot/vmlinuz" ]; then
163 warn "Your build directory has been built more recently than"
164 warn "the installed kernel being updated to. Did you forget to"
165 warn "run 'cros_workon_make chromeos-kernel --install'?"
166 fi
167}
168
Olof Johansson45225c92013-10-15 17:32:48 -0700169mark_boot_once() {
170 local idx=${FLAGS_partition##*[^0-9]}
Chirantan Ekbotef25b4402014-05-06 12:42:18 -0700171 remote_sh cgpt add -i ${idx} -S 0 -T 1 -P 15 ${FLAGS_device%p}
Olof Johansson45225c92013-10-15 17:32:48 -0700172}
173
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700174update_syslinux_kernel() {
175 # ARM does not have the syslinux directory, so skip it when the
176 # partition or the syslinux vmlinuz target is missing.
177 echo "updating syslinux kernel"
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -0400178 remote_sh grep $(echo ${FLAGS_device}${PARTITION_NUM_EFI_SYSTEM} | cut -d/ -f3) /proc/partitions
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700179 if [ $(echo "$REMOTE_OUT" | wc -l) -eq 1 ]; then
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -0400180 remote_sh mkdir -p /tmp/${PARTITION_NUM_EFI_SYSTEM}
181 remote_sh mount ${FLAGS_device}${PARTITION_NUM_EFI_SYSTEM} /tmp/${PARTITION_NUM_EFI_SYSTEM}
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700182
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -0400183 if [ "$FLAGS_partition" = "${FLAGS_device}${PARTITION_NUM_KERN_A}" ]; then
184 target="/tmp/${PARTITION_NUM_EFI_SYSTEM}/syslinux/vmlinuz.A"
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700185 else
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -0400186 target="/tmp/${PARTITION_NUM_EFI_SYSTEM}/syslinux/vmlinuz.B"
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700187 fi
188 remote_sh "test ! -f $target || cp /boot/vmlinuz $target"
189
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -0400190 remote_sh umount /tmp/${PARTITION_NUM_EFI_SYSTEM}
191 remote_sh rmdir /tmp/${PARTITION_NUM_EFI_SYSTEM}
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700192 fi
193}
194
Doug Anderson549f3b52013-09-26 14:46:18 -0700195multi_main() {
196 local host
197
198 IFS=","
199 for host in ${FLAGS_remote}; do
200 "$0" "${ORIG_ARGS[@]}" --remote="${host}" \
201 |& sed "s/^/${V_BOLD_YELLOW}${host}: ${V_VIDOFF}/" &
202 done
203 wait
204}
205
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400206main() {
Doug Anderson549f3b52013-09-26 14:46:18 -0700207 # If there are commas in the --remote, run the script in parallel.
208 if [[ ${FLAGS_remote} == *,* ]]; then
209 multi_main
210 return $?
211 fi
212
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800213 trap cleanup EXIT
214
Kees Cook43a32132011-10-18 13:17:11 -0700215 TMP=$(mktemp -d /tmp/update_kernel.XXXXXX)
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800216
217 remote_access_init
218
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800219 learn_arch
220
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800221 learn_board
222
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800223 learn_device
224
Ian Coolidgec3d5d912017-03-07 14:21:28 -0800225 learn_partition_layout
226
Olof Johansson8488f5a2011-04-20 17:27:37 -0700227 learn_partition_and_ro
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800228
Brian Norris9774b0d2017-06-15 13:44:26 -0700229 if ! remote_sh "test -e '${FLAGS_partition}'"; then
230 die_notrace "Could not find kernel partition on DUT; path='${FLAGS_partition}'"
231 fi
232
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800233 remote_sh uname -r -v
234
235 old_kernel="${REMOTE_OUT}"
236
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -0400237 check_kernelbuildtime
238
Doug Anderson5a21b442012-12-07 11:47:31 -0800239 if [ ${FLAGS_vboot} -eq ${FLAGS_TRUE} ]; then
240 make_kernelimage
241 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800242
Olof Johansson8488f5a2011-04-20 17:27:37 -0700243 if [[ ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]]; then
Olof Johansson8488f5a2011-04-20 17:27:37 -0700244 remote_sh mount -o remount,rw /
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700245 echo "copying kernel"
Doug Anderson48b52002012-12-07 12:40:07 -0800246 remote_send_to /build/"${FLAGS_board}"/boot/ /boot/
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700247
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700248 if [ ${FLAGS_syslinux} -eq ${FLAGS_TRUE} ]; then
249 update_syslinux_kernel
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700250 fi
Olof Johansson9a83e4e2012-08-17 02:45:12 -0700251
Edward Hyunkoo Jeea979a902017-03-08 13:44:26 -0800252 copy_kernelmodules
Olof Johansson9a83e4e2012-08-17 02:45:12 -0700253
254 echo "copying firmware"
Doug Anderson48b52002012-12-07 12:40:07 -0800255 remote_send_to /build/"${FLAGS_board}"/lib/firmware/ /lib/firmware/
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800256 fi
257
Doug Anderson5a21b442012-12-07 11:47:31 -0800258 if [ ${FLAGS_vboot} -eq ${FLAGS_TRUE} ]; then
259 info "Copying vboot kernel image"
260 copy_kernelimage
261 else
262 info "Skipping update of vboot (per request)"
263 fi
Olof Johansson8488f5a2011-04-20 17:27:37 -0700264
Olof Johansson45225c92013-10-15 17:32:48 -0700265 if [ ${FLAGS_bootonce} -eq ${FLAGS_TRUE} ]; then
266 info "Marking kernel partition ${FLAGS_partition} as boot once"
267 mark_boot_once
268 fi
269
Jonathan Kliegmand6f3d072012-07-12 12:45:33 -0400270 # An early kernel panic can prevent the normal sync on reboot. Explicitly
271 # sync for safety to avoid random file system corruption.
272 remote_sh sync
273
Doug Andersonb2fe4652012-12-07 17:33:56 -0800274 if [ ${FLAGS_reboot} -eq ${FLAGS_TRUE} ]; then
Olof Johansson8488f5a2011-04-20 17:27:37 -0700275 remote_reboot
276
277 remote_sh uname -r -v
278 info "old kernel: ${old_kernel}"
279 info "new kernel: ${REMOTE_OUT}"
280 else
281 info "Not rebooting (per request)"
282 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800283}
284
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800285main "$@"