blob: d4d546584dd09939b1fb208d09394d2f841c04fa [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
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -040012# This file is sourced for partition constants.
13. "${BUILD_LIBRARY_DIR}/disk_layout_util.sh" || exit 1
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080014
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080015# Script must be run inside the chroot.
Greg Spencer798d75f2011-02-01 22:04:49 -080016restart_in_chroot_if_needed "$@"
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080017
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080018DEFINE_string board "" "Override board reported by target"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080019DEFINE_string device "" "Override boot device reported by target"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080020DEFINE_string partition "" "Override kernel partition reported by target"
Doug Andersonfcaed8a2014-07-09 11:34:29 -070021DEFINE_string rootoff "" "Override root offset"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080022DEFINE_string arch "" "Override architecture reported by target"
Nicolas Boichatabdf6642016-07-04 11:30:44 +080023DEFINE_boolean ignore_verity $FLAGS_FALSE "Update kernel even if system is using verity"
Olof Johansson8488f5a2011-04-20 17:27:37 -070024DEFINE_boolean reboot $FLAGS_TRUE "Reboot system after update"
Doug Anderson5a21b442012-12-07 11:47:31 -080025DEFINE_boolean vboot $FLAGS_TRUE "Update the vboot kernel"
Olof Johansson68cbfaf2013-04-23 14:06:28 -070026DEFINE_boolean syslinux $FLAGS_TRUE "Update the syslinux kernel"
Olof Johansson45225c92013-10-15 17:32:48 -070027DEFINE_boolean bootonce $FLAGS_FALSE "Mark kernel partition as boot once"
Olof Johansson4a7b2882013-10-16 11:59:58 -070028DEFINE_boolean remote_bootargs $FLAGS_FALSE "Use bootargs from running kernel on target"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080029
Doug Anderson549f3b52013-09-26 14:46:18 -070030ORIG_ARGS=("$@")
31
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080032# Parse command line.
33FLAGS "$@" || exit 1
34eval set -- "${FLAGS_ARGV}"
35
36# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080037# so will die prematurely if 'switch_to_strict_mode' is specified before now.
38switch_to_strict_mode
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080039
Mike Frysinger6b1abb22012-05-11 13:44:06 -040040cleanup() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080041 cleanup_remote_access
42 rm -rf "${TMP}"
43}
44
Mike Frysinger6b1abb22012-05-11 13:44:06 -040045learn_device() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080046 [ -n "${FLAGS_device}" ] && return
47 remote_sh df /mnt/stateful_partition
48 FLAGS_device=$(echo "${REMOTE_OUT}" | awk '/dev/ {print $1}' | sed s/1\$//)
49 info "Target reports root device is ${FLAGS_device}"
50}
51
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080052# Ask the target what the kernel partition is
Mike Frysinger6b1abb22012-05-11 13:44:06 -040053learn_partition_and_ro() {
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080054 ! remote_sh rootdev
Paul Taysoma64d9db2012-09-21 13:30:43 -070055 if [ "${REMOTE_OUT%%-*}" == "/dev/dm" ]; then
56 remote_sh rootdev -s
Olof Johansson8488f5a2011-04-20 17:27:37 -070057 REMOTE_VERITY=${FLAGS_TRUE}
Nicolas Boichatabdf6642016-07-04 11:30:44 +080058 if [[ ${FLAGS_ignore_verity} -eq ${FLAGS_TRUE} ]]; then
59 warn "System is using verity: not updating firmware/modules"
60 else
61 warn "System is using verity: First remove rootfs verification using"
62 warn "/usr/share/vboot/bin/make_dev_ssd.sh --remove_rootfs_verification"
63 warn "on the DUT, or add --ignore_verity parameter to this command."
64 die
65 fi
Olof Johansson8488f5a2011-04-20 17:27:37 -070066 else
67 REMOTE_VERITY=${FLAGS_FALSE}
68 info "System is not using verity: updating firmware and modules"
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080069 fi
Olof Johansson4996bfb2013-11-13 12:58:52 -080070 [ -n "${FLAGS_partition}" ] && return
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -040071 if [ "${REMOTE_OUT}" == "${FLAGS_device}${PARTITION_NUM_ROOT_A}" ]; then
72 FLAGS_partition="${FLAGS_device}${PARTITION_NUM_KERN_A}"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080073 else
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -040074 FLAGS_partition="${FLAGS_device}${PARTITION_NUM_KERN_B}"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080075 fi
76 if [ -z "${FLAGS_partition}" ]; then
Doug Andersonb2fe4652012-12-07 17:33:56 -080077 die "Partition required"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080078 fi
Doug Anderson5a21b442012-12-07 11:47:31 -080079 if [ ${REMOTE_VERITY} -eq ${FLAGS_TRUE} ]; then
80 info "Target reports kernel partition is ${FLAGS_partition}"
81 if [ ${FLAGS_vboot} -eq ${FLAGS_FALSE} ]; then
82 die "Must update vboot when target is using verity"
83 fi
84 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080085}
86
Olof Johansson4a7b2882013-10-16 11:59:58 -070087get_bootargs() {
88 if [ ${FLAGS_remote_bootargs} -eq ${FLAGS_TRUE} ] ; then
89 info "Using remote bootargs"
90 remote_sh cat /proc/cmdline && echo "${REMOTE_OUT}"
91 else
Doug Andersonfcaed8a2014-07-09 11:34:29 -070092 if [ -n "${FLAGS_rootoff}" ]; then
93 sed "s/PARTNROFF=1/PARTNROFF=${FLAGS_rootoff}/" \
94 "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt"
95 else
96 cat "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt"
97 fi
Olof Johansson4a7b2882013-10-16 11:59:58 -070098 fi
99}
100
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400101make_kernelimage() {
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800102 local bootloader_path
103 local kernel_image
Olof Johansson4a7b2882013-10-16 11:59:58 -0700104 local config_path="$(mktemp /tmp/config.txt.XXXXX)"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800105 if [[ "${FLAGS_arch}" == "arm" ]]; then
Taylor Hutt60da6422011-06-02 13:54:43 -0700106 name="bootloader.bin"
107 bootloader_path="${SRC_ROOT}/build/images/${FLAGS_board}/latest/${name}"
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800108 kernel_image="/build/${FLAGS_board}/boot/vmlinux.uimg"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800109 else
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800110 bootloader_path="/lib64/bootstub/bootstub.efi"
111 kernel_image="/build/${FLAGS_board}/boot/vmlinuz"
112 fi
Olof Johansson4a7b2882013-10-16 11:59:58 -0700113 get_bootargs > "${config_path}"
Kees Cook43a32132011-10-18 13:17:11 -0700114 vbutil_kernel --pack $TMP/new_kern.bin \
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800115 --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
116 --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
117 --version 1 \
Olof Johansson4a7b2882013-10-16 11:59:58 -0700118 --config ${config_path} \
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800119 --bootloader "${bootloader_path}" \
120 --vmlinuz "${kernel_image}" \
121 --arch "${FLAGS_arch}"
Olof Johansson4a7b2882013-10-16 11:59:58 -0700122 rm "${config_path}"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800123}
124
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400125copy_kernelimage() {
Doug Andersonadf8a002012-12-17 11:40:34 -0800126 remote_sh dd of="${FLAGS_partition}" bs=4K < "${TMP}/new_kern.bin"
Olof Johansson8488f5a2011-04-20 17:27:37 -0700127}
128
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -0400129check_kernelbuildtime() {
130 local version=$(readlink "/build/${FLAGS_board}/boot/vmlinuz" | cut -d- -f2-)
131 local build_dir="/build/${FLAGS_board}/lib/modules/${version}/build"
132 if [ "${build_dir}/Makefile" -nt "/build/${FLAGS_board}/boot/vmlinuz" ]; then
133 warn "Your build directory has been built more recently than"
134 warn "the installed kernel being updated to. Did you forget to"
135 warn "run 'cros_workon_make chromeos-kernel --install'?"
136 fi
137}
138
Olof Johansson45225c92013-10-15 17:32:48 -0700139mark_boot_once() {
140 local idx=${FLAGS_partition##*[^0-9]}
Chirantan Ekbotef25b4402014-05-06 12:42:18 -0700141 remote_sh cgpt add -i ${idx} -S 0 -T 1 -P 15 ${FLAGS_device%p}
Olof Johansson45225c92013-10-15 17:32:48 -0700142}
143
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700144update_syslinux_kernel() {
145 # ARM does not have the syslinux directory, so skip it when the
146 # partition or the syslinux vmlinuz target is missing.
147 echo "updating syslinux kernel"
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -0400148 remote_sh grep $(echo ${FLAGS_device}${PARTITION_NUM_EFI_SYSTEM} | cut -d/ -f3) /proc/partitions
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700149 if [ $(echo "$REMOTE_OUT" | wc -l) -eq 1 ]; then
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -0400150 remote_sh mkdir -p /tmp/${PARTITION_NUM_EFI_SYSTEM}
151 remote_sh mount ${FLAGS_device}${PARTITION_NUM_EFI_SYSTEM} /tmp/${PARTITION_NUM_EFI_SYSTEM}
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700152
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -0400153 if [ "$FLAGS_partition" = "${FLAGS_device}${PARTITION_NUM_KERN_A}" ]; then
154 target="/tmp/${PARTITION_NUM_EFI_SYSTEM}/syslinux/vmlinuz.A"
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700155 else
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -0400156 target="/tmp/${PARTITION_NUM_EFI_SYSTEM}/syslinux/vmlinuz.B"
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700157 fi
158 remote_sh "test ! -f $target || cp /boot/vmlinuz $target"
159
Steven 'Steve' Kendall019f38f2016-06-09 12:43:28 -0400160 remote_sh umount /tmp/${PARTITION_NUM_EFI_SYSTEM}
161 remote_sh rmdir /tmp/${PARTITION_NUM_EFI_SYSTEM}
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700162 fi
163}
164
Doug Anderson549f3b52013-09-26 14:46:18 -0700165multi_main() {
166 local host
167
168 IFS=","
169 for host in ${FLAGS_remote}; do
170 "$0" "${ORIG_ARGS[@]}" --remote="${host}" \
171 |& sed "s/^/${V_BOLD_YELLOW}${host}: ${V_VIDOFF}/" &
172 done
173 wait
174}
175
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400176main() {
Doug Anderson549f3b52013-09-26 14:46:18 -0700177 # If there are commas in the --remote, run the script in parallel.
178 if [[ ${FLAGS_remote} == *,* ]]; then
179 multi_main
180 return $?
181 fi
182
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800183 trap cleanup EXIT
184
Kees Cook43a32132011-10-18 13:17:11 -0700185 TMP=$(mktemp -d /tmp/update_kernel.XXXXXX)
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800186
187 remote_access_init
188
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800189 learn_arch
190
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800191 learn_board
192
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800193 learn_device
194
Olof Johansson8488f5a2011-04-20 17:27:37 -0700195 learn_partition_and_ro
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800196
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800197 remote_sh uname -r -v
198
199 old_kernel="${REMOTE_OUT}"
200
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -0400201 check_kernelbuildtime
202
Doug Anderson5a21b442012-12-07 11:47:31 -0800203 if [ ${FLAGS_vboot} -eq ${FLAGS_TRUE} ]; then
204 make_kernelimage
205 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800206
Olof Johansson8488f5a2011-04-20 17:27:37 -0700207 if [[ ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]]; then
Olof Johansson8488f5a2011-04-20 17:27:37 -0700208 remote_sh mount -o remount,rw /
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700209 echo "copying kernel"
Doug Anderson48b52002012-12-07 12:40:07 -0800210 remote_send_to /build/"${FLAGS_board}"/boot/ /boot/
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700211
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700212 if [ ${FLAGS_syslinux} -eq ${FLAGS_TRUE} ]; then
213 update_syslinux_kernel
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700214 fi
Olof Johansson9a83e4e2012-08-17 02:45:12 -0700215
216 echo "copying modules"
Andrew Chew9866d2f2015-04-07 10:50:59 -0700217 if [ -d /build/"${FLAGS_board}"/lib/modules/ ]; then
218 remote_send_to /build/"${FLAGS_board}"/lib/modules/ /lib/modules/
219 else
220 info "No modules. Skipping."
221 fi
Olof Johansson9a83e4e2012-08-17 02:45:12 -0700222
223 echo "copying firmware"
Doug Anderson48b52002012-12-07 12:40:07 -0800224 remote_send_to /build/"${FLAGS_board}"/lib/firmware/ /lib/firmware/
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800225 fi
226
Doug Anderson5a21b442012-12-07 11:47:31 -0800227 if [ ${FLAGS_vboot} -eq ${FLAGS_TRUE} ]; then
228 info "Copying vboot kernel image"
229 copy_kernelimage
230 else
231 info "Skipping update of vboot (per request)"
232 fi
Olof Johansson8488f5a2011-04-20 17:27:37 -0700233
Olof Johansson45225c92013-10-15 17:32:48 -0700234 if [ ${FLAGS_bootonce} -eq ${FLAGS_TRUE} ]; then
235 info "Marking kernel partition ${FLAGS_partition} as boot once"
236 mark_boot_once
237 fi
238
Jonathan Kliegmand6f3d072012-07-12 12:45:33 -0400239 # An early kernel panic can prevent the normal sync on reboot. Explicitly
240 # sync for safety to avoid random file system corruption.
241 remote_sh sync
242
Doug Andersonb2fe4652012-12-07 17:33:56 -0800243 if [ ${FLAGS_reboot} -eq ${FLAGS_TRUE} ]; then
Olof Johansson8488f5a2011-04-20 17:27:37 -0700244 remote_reboot
245
246 remote_sh uname -r -v
247 info "old kernel: ${old_kernel}"
248 info "new kernel: ${REMOTE_OUT}"
249 else
250 info "Not rebooting (per request)"
251 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800252}
253
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800254main "$@"