blob: 2c9972379d19653878f1182918de9330b2ba9912 [file] [log] [blame]
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -08001#!/bin/bash
2
3# Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved.
4# 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
9# Load common constants. This should be the first executable line.
10# The path to common.sh should be relative to your script's location.
11
12. "$(dirname $0)/common.sh"
13. "$(dirname $0)/remote_access.sh"
14
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080015# Script must be run inside the chroot.
16restart_in_chroot_if_needed $*
17
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"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080021DEFINE_string arch "" "Override architecture reported by target"
Olof Johansson5a46bfb2010-12-22 12:14:21 -080022DEFINE_boolean modules false "Update modules on target"
23DEFINE_boolean firmware false "Update firmware on target"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080024
Mandeep Singh Baines2f3b5fc2011-01-14 14:20:12 -080025# Parse command line.
26FLAGS "$@" || exit 1
27eval set -- "${FLAGS_ARGV}"
28
29# Only now can we die on error. shflags functions leak non-zero error codes,
30# so will die prematurely if 'set -e' is specified before now.
31set -e
32
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080033function cleanup {
34 cleanup_remote_access
35 rm -rf "${TMP}"
36}
37
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080038function learn_device() {
39 [ -n "${FLAGS_device}" ] && return
40 remote_sh df /mnt/stateful_partition
41 FLAGS_device=$(echo "${REMOTE_OUT}" | awk '/dev/ {print $1}' | sed s/1\$//)
42 info "Target reports root device is ${FLAGS_device}"
43}
44
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080045# Ask the target what the kernel partition is
46function learn_partition() {
47 [ -n "${FLAGS_partition}" ] && return
48 remote_sh cat /proc/cmdline
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080049 if echo "${REMOTE_OUT}" | egrep -q "${FLAGS_device}3"; then
50 FLAGS_partition="${FLAGS_device}2"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080051 else
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080052 FLAGS_partition="${FLAGS_device}4"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080053 fi
54 if [ -z "${FLAGS_partition}" ]; then
55 error "Partition required"
56 exit 1
57 fi
58 info "Target reports kernel partition is ${FLAGS_partition}"
59}
60
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080061function make_kernelimage() {
62
63 if [[ "${FLAGS_arch}" == "arm" ]]; then
64 ./build_kernel_image.sh --arch=arm \
65 --root='/dev/${devname}${rootpart}' \
66 --vmlinuz=/build/${FLAGS_board}/boot/vmlinux.uimg --to new_kern.bin
67 else
68 vbutil_kernel --pack new_kern.bin \
69 --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
70 --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
71 --version 1 \
72 --config ../build/images/${FLAGS_board}/latest/config.txt \
73 --bootloader /lib64/bootstub/bootstub.efi \
74 --vmlinuz /build/${FLAGS_board}/boot/vmlinuz
75 fi
76}
77
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080078function main() {
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080079 trap cleanup EXIT
80
81 TMP=$(mktemp -d /tmp/image_to_live.XXXX)
82
83 remote_access_init
84
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080085 learn_arch
86
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080087 learn_board
88
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080089 learn_device
90
91 learn_partition
92
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080093 remote_sh uname -r -v
94
95 old_kernel="${REMOTE_OUT}"
96
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080097 make_kernelimage
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080098
99 remote_cp_to new_kern.bin /tmp
100
101 remote_sh dd if=/tmp/new_kern.bin of="${FLAGS_partition}"
102
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800103 if [[ ${FLAGS_modules} -eq ${FLAGS_TRUE} ]]; then
104 echo "copying modules"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800105 tar -C /build/"${FLAGS_board}"/lib/modules -cjf /tmp/new_modules.tar .
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800106
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800107 remote_cp_to /tmp/new_modules.tar /tmp/
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800108
109 remote_sh mount -o remount,rw /
110 remote_sh tar -C /lib/modules -xjf /tmp/new_modules.tar
111 fi
112
113 if [[ ${FLAGS_firmware} -eq ${FLAGS_TRUE} ]]; then
114 echo "copying firmware"
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800115 tar -C /build/"${FLAGS_board}"/lib/firmware -cjf /tmp/new_firmware.tar .
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800116
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800117 remote_cp_to /tmp/new_firmware.tar /tmp/
Olof Johansson5a46bfb2010-12-22 12:14:21 -0800118
119 remote_sh mount -o remount,rw /
120 remote_sh tar -C /lib/firmware -xjf /tmp/new_firmware.tar
121 fi
122
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800123 remote_reboot
124
125 remote_sh uname -r -v
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800126 info "old kernel: ${old_kernel}"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800127 info "new kernel: ${REMOTE_OUT}"
128}
129
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800130main "$@"