blob: c25538855dfc34f10420e98a746a598242aa9f07 [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"
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"
Doug Anderson5a21b442012-12-07 11:47:31 -080021DEFINE_boolean vboot $FLAGS_TRUE "Update the vboot kernel"
Olof Johansson68cbfaf2013-04-23 14:06:28 -070022DEFINE_boolean syslinux $FLAGS_TRUE "Update the syslinux kernel"
Olof Johansson45225c92013-10-15 17:32:48 -070023DEFINE_boolean bootonce $FLAGS_FALSE "Mark kernel partition as boot once"
Olof Johansson4a7b2882013-10-16 11:59:58 -070024DEFINE_boolean remote_bootargs $FLAGS_FALSE "Use bootargs from running kernel on target"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080025
Doug Anderson549f3b52013-09-26 14:46:18 -070026ORIG_ARGS=("$@")
27
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080028# Parse command line.
29FLAGS "$@" || exit 1
30eval set -- "${FLAGS_ARGV}"
31
32# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080033# so will die prematurely if 'switch_to_strict_mode' is specified before now.
34switch_to_strict_mode
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080035
Mike Frysinger6b1abb22012-05-11 13:44:06 -040036cleanup() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080037 cleanup_remote_access
38 rm -rf "${TMP}"
39}
40
Mike Frysinger6b1abb22012-05-11 13:44:06 -040041learn_device() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080042 [ -n "${FLAGS_device}" ] && return
43 remote_sh df /mnt/stateful_partition
44 FLAGS_device=$(echo "${REMOTE_OUT}" | awk '/dev/ {print $1}' | sed s/1\$//)
45 info "Target reports root device is ${FLAGS_device}"
46}
47
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080048# Ask the target what the kernel partition is
Mike Frysinger6b1abb22012-05-11 13:44:06 -040049learn_partition_and_ro() {
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080050 ! remote_sh rootdev
Paul Taysoma64d9db2012-09-21 13:30:43 -070051 if [ "${REMOTE_OUT%%-*}" == "/dev/dm" ]; then
52 remote_sh rootdev -s
Olof Johansson8488f5a2011-04-20 17:27:37 -070053 REMOTE_VERITY=${FLAGS_TRUE}
Doug Anderson2023fb82013-02-13 11:53:55 -080054 warn "System is using verity: not updating firmware/modules"
Olof Johansson8488f5a2011-04-20 17:27:37 -070055 else
56 REMOTE_VERITY=${FLAGS_FALSE}
57 info "System is not using verity: updating firmware and modules"
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080058 fi
Olof Johansson4996bfb2013-11-13 12:58:52 -080059 [ -n "${FLAGS_partition}" ] && return
Mandeep Singh Bainese39579a2011-03-04 15:58:57 -080060 if [ "${REMOTE_OUT}" == "${FLAGS_device}3" ]; then
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080061 FLAGS_partition="${FLAGS_device}2"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080062 else
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080063 FLAGS_partition="${FLAGS_device}4"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080064 fi
65 if [ -z "${FLAGS_partition}" ]; then
Doug Andersonb2fe4652012-12-07 17:33:56 -080066 die "Partition required"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080067 fi
Doug Anderson5a21b442012-12-07 11:47:31 -080068 if [ ${REMOTE_VERITY} -eq ${FLAGS_TRUE} ]; then
69 info "Target reports kernel partition is ${FLAGS_partition}"
70 if [ ${FLAGS_vboot} -eq ${FLAGS_FALSE} ]; then
71 die "Must update vboot when target is using verity"
72 fi
73 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080074}
75
Olof Johansson4a7b2882013-10-16 11:59:58 -070076get_bootargs() {
77 if [ ${FLAGS_remote_bootargs} -eq ${FLAGS_TRUE} ] ; then
78 info "Using remote bootargs"
79 remote_sh cat /proc/cmdline && echo "${REMOTE_OUT}"
80 else
81 cat "${SRC_ROOT}/build/images/${FLAGS_board}/latest/config.txt"
82 fi
83}
84
Mike Frysinger6b1abb22012-05-11 13:44:06 -040085make_kernelimage() {
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080086 local bootloader_path
87 local kernel_image
Olof Johansson4a7b2882013-10-16 11:59:58 -070088 local config_path="$(mktemp /tmp/config.txt.XXXXX)"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080089 if [[ "${FLAGS_arch}" == "arm" ]]; then
Taylor Hutt60da6422011-06-02 13:54:43 -070090 name="bootloader.bin"
91 bootloader_path="${SRC_ROOT}/build/images/${FLAGS_board}/latest/${name}"
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080092 kernel_image="/build/${FLAGS_board}/boot/vmlinux.uimg"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080093 else
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +080094 bootloader_path="/lib64/bootstub/bootstub.efi"
95 kernel_image="/build/${FLAGS_board}/boot/vmlinuz"
96 fi
Olof Johansson4a7b2882013-10-16 11:59:58 -070097 get_bootargs > "${config_path}"
Kees Cook43a32132011-10-18 13:17:11 -070098 vbutil_kernel --pack $TMP/new_kern.bin \
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080099 --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
100 --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
101 --version 1 \
Olof Johansson4a7b2882013-10-16 11:59:58 -0700102 --config ${config_path} \
Tom Wai-Hong Tam6b50a072011-05-25 17:00:15 +0800103 --bootloader "${bootloader_path}" \
104 --vmlinuz "${kernel_image}" \
105 --arch "${FLAGS_arch}"
Olof Johansson4a7b2882013-10-16 11:59:58 -0700106 rm "${config_path}"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800107}
108
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400109copy_kernelimage() {
Doug Andersonadf8a002012-12-17 11:40:34 -0800110 remote_sh dd of="${FLAGS_partition}" bs=4K < "${TMP}/new_kern.bin"
Olof Johansson8488f5a2011-04-20 17:27:37 -0700111}
112
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -0400113check_kernelbuildtime() {
114 local version=$(readlink "/build/${FLAGS_board}/boot/vmlinuz" | cut -d- -f2-)
115 local build_dir="/build/${FLAGS_board}/lib/modules/${version}/build"
116 if [ "${build_dir}/Makefile" -nt "/build/${FLAGS_board}/boot/vmlinuz" ]; then
117 warn "Your build directory has been built more recently than"
118 warn "the installed kernel being updated to. Did you forget to"
119 warn "run 'cros_workon_make chromeos-kernel --install'?"
120 fi
121}
122
Olof Johansson45225c92013-10-15 17:32:48 -0700123mark_boot_once() {
124 local idx=${FLAGS_partition##*[^0-9]}
Chirantan Ekbotef25b4402014-05-06 12:42:18 -0700125 remote_sh cgpt add -i ${idx} -S 0 -T 1 -P 15 ${FLAGS_device%p}
Olof Johansson45225c92013-10-15 17:32:48 -0700126}
127
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700128update_syslinux_kernel() {
129 # ARM does not have the syslinux directory, so skip it when the
130 # partition or the syslinux vmlinuz target is missing.
131 echo "updating syslinux kernel"
132 remote_sh grep $(echo ${FLAGS_device}12 | cut -d/ -f3) /proc/partitions
133 if [ $(echo "$REMOTE_OUT" | wc -l) -eq 1 ]; then
134 remote_sh mkdir -p /tmp/12
135 remote_sh mount ${FLAGS_device}12 /tmp/12
136
137 if [ "$FLAGS_partition" = "${FLAGS_device}2" ]; then
138 target="/tmp/12/syslinux/vmlinuz.A"
139 else
140 target="/tmp/12/syslinux/vmlinuz.B"
141 fi
142 remote_sh "test ! -f $target || cp /boot/vmlinuz $target"
143
144 remote_sh umount /tmp/12
145 remote_sh rmdir /tmp/12
146 fi
147}
148
Doug Anderson549f3b52013-09-26 14:46:18 -0700149multi_main() {
150 local host
151
152 IFS=","
153 for host in ${FLAGS_remote}; do
154 "$0" "${ORIG_ARGS[@]}" --remote="${host}" \
155 |& sed "s/^/${V_BOLD_YELLOW}${host}: ${V_VIDOFF}/" &
156 done
157 wait
158}
159
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400160main() {
Doug Anderson549f3b52013-09-26 14:46:18 -0700161 # If there are commas in the --remote, run the script in parallel.
162 if [[ ${FLAGS_remote} == *,* ]]; then
163 multi_main
164 return $?
165 fi
166
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800167 trap cleanup EXIT
168
Kees Cook43a32132011-10-18 13:17:11 -0700169 TMP=$(mktemp -d /tmp/update_kernel.XXXXXX)
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800170
171 remote_access_init
172
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800173 learn_arch
174
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800175 learn_board
176
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800177 learn_device
178
Olof Johansson8488f5a2011-04-20 17:27:37 -0700179 learn_partition_and_ro
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800180
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800181 remote_sh uname -r -v
182
183 old_kernel="${REMOTE_OUT}"
184
Jonathan Kliegman775bc8e2012-08-14 12:30:49 -0400185 check_kernelbuildtime
186
Doug Anderson5a21b442012-12-07 11:47:31 -0800187 if [ ${FLAGS_vboot} -eq ${FLAGS_TRUE} ]; then
188 make_kernelimage
189 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800190
Olof Johansson8488f5a2011-04-20 17:27:37 -0700191 if [[ ${REMOTE_VERITY} -eq ${FLAGS_FALSE} ]]; then
Olof Johansson8488f5a2011-04-20 17:27:37 -0700192 remote_sh mount -o remount,rw /
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700193 echo "copying kernel"
Doug Anderson48b52002012-12-07 12:40:07 -0800194 remote_send_to /build/"${FLAGS_board}"/boot/ /boot/
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700195
Olof Johansson68cbfaf2013-04-23 14:06:28 -0700196 if [ ${FLAGS_syslinux} -eq ${FLAGS_TRUE} ]; then
197 update_syslinux_kernel
Kees Cook7d7d2ef2011-10-18 13:12:12 -0700198 fi
Olof Johansson9a83e4e2012-08-17 02:45:12 -0700199
200 echo "copying modules"
Doug Anderson48b52002012-12-07 12:40:07 -0800201 remote_send_to /build/"${FLAGS_board}"/lib/modules/ /lib/modules/
Olof Johansson9a83e4e2012-08-17 02:45:12 -0700202
203 echo "copying firmware"
Doug Anderson48b52002012-12-07 12:40:07 -0800204 remote_send_to /build/"${FLAGS_board}"/lib/firmware/ /lib/firmware/
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800205 fi
206
Doug Anderson5a21b442012-12-07 11:47:31 -0800207 if [ ${FLAGS_vboot} -eq ${FLAGS_TRUE} ]; then
208 info "Copying vboot kernel image"
209 copy_kernelimage
210 else
211 info "Skipping update of vboot (per request)"
212 fi
Olof Johansson8488f5a2011-04-20 17:27:37 -0700213
Olof Johansson45225c92013-10-15 17:32:48 -0700214 if [ ${FLAGS_bootonce} -eq ${FLAGS_TRUE} ]; then
215 info "Marking kernel partition ${FLAGS_partition} as boot once"
216 mark_boot_once
217 fi
218
Jonathan Kliegmand6f3d072012-07-12 12:45:33 -0400219 # An early kernel panic can prevent the normal sync on reboot. Explicitly
220 # sync for safety to avoid random file system corruption.
221 remote_sh sync
222
Doug Andersonb2fe4652012-12-07 17:33:56 -0800223 if [ ${FLAGS_reboot} -eq ${FLAGS_TRUE} ]; then
Olof Johansson8488f5a2011-04-20 17:27:37 -0700224 remote_reboot
225
226 remote_sh uname -r -v
227 info "old kernel: ${old_kernel}"
228 info "new kernel: ${REMOTE_OUT}"
229 else
230 info "Not rebooting (per request)"
231 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800232}
233
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800234main "$@"