blob: b4e393e9b5f8d0a0735e874fd58cb6a61910f555 [file] [log] [blame]
Will Drewry69563b72010-06-24 16:12:58 -05001#!/bin/bash
2
3# Copyright (c) 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# Helper script that generates the signed kernel image
8
9. "$(dirname "$0")/common.sh"
10
11get_default_board
12
13# Flags.
14DEFINE_string arch "x86" \
15 "The boot architecture: arm or x86. (Default: x86)"
16DEFINE_string to "/tmp/vmlinuz.image" \
17 "The path to the kernel image to be created. (Default: /tmp/vmlinuz.image)"
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +080018DEFINE_string hd_vblock "/tmp/vmlinuz_hd.vblock" \
19 "The path to the installed kernel's vblock (Default: /tmp/vmlinuz_hd.vblock)"
Will Drewry69563b72010-06-24 16:12:58 -050020DEFINE_string vmlinuz "vmlinuz" \
21 "The path to the kernel (Default: vmlinuz)"
22DEFINE_string working_dir "/tmp/vmlinuz.working" \
23 "Working directory for in-progress files. (Default: /tmp/vmlinuz.working)"
24DEFINE_boolean keep_work ${FLAGS_FALSE} \
25 "Keep temporary files (*.keyblock, *.vbpubk). (Default: false)"
26DEFINE_string keys_dir "${SRC_ROOT}/platform/vboot_reference/tests/testkeys" \
Bill Richardson2ace49e2010-07-01 10:23:27 -070027 "Directory with the RSA signing keys. (Defaults to test keys)"
Tan Gao843b70a2010-08-17 09:41:48 -070028DEFINE_boolean use_dev_keys ${FLAGS_FALSE} \
29 "Use developer keys for signing. (Default: false)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050030# Note, to enable verified boot, the caller would manually pass:
Will Drewry69563b72010-06-24 16:12:58 -050031# --boot_args='dm="... /dev/sd%D%P /dev/sd%D%P ..." \
32# --root=/dev/dm-0
33DEFINE_string boot_args "noinitrd" \
34 "Additional boot arguments to pass to the commandline (Default: noinitrd)"
35DEFINE_string root "/dev/sd%D%P" \
36 "Expected device root (Default: root=/dev/sd%D%P)"
37
Will Drewrybcbf1c42010-07-03 10:23:30 -050038# If provided, will automatically add verified boot arguments.
39DEFINE_string rootfs_image "" \
40 "Optional path to the rootfs device or image.(Default: \"\")"
41DEFINE_string rootfs_hash "" \
42 "Optional path to output the rootfs hash to. (Default: \"\")"
Will Drewry1670d482010-07-09 13:08:38 -070043DEFINE_integer verity_error_behavior 2 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050044 "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \
45(Default: 2)"
Will Drewry1670d482010-07-09 13:08:38 -070046DEFINE_integer verity_tree_depth 1 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050047 "Optional Verified boot hash tree depth. (Default: 1)"
Will Drewry1670d482010-07-09 13:08:38 -070048DEFINE_integer verity_max_ios 1024 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050049 "Optional number of outstanding I/O operations. (Default: 1024)"
Will Drewry1670d482010-07-09 13:08:38 -070050DEFINE_string verity_hash_alg "sha1" \
51 "Cryptographic hash algorithm used for dm-verity. (Default: sha1)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050052
Will Drewry69563b72010-06-24 16:12:58 -050053# Parse flags
54FLAGS "$@" || exit 1
55eval set -- "${FLAGS_ARGV}"
56
57# Die on error
58set -e
59
Will Drewry1670d482010-07-09 13:08:38 -070060verity_args=
Will Drewrybcbf1c42010-07-03 10:23:30 -050061# Even with a rootfs_image, root= is not changed unless specified.
62if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -050063 # Gets the number of blocks. 4096 byte blocks _are_ expected.
64 root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
65 grep "Block count" |
66 tr -d ' ' |
67 cut -f2 -d:)
Will Drewrybcbf1c42010-07-03 10:23:30 -050068 root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
69 grep "Block size" |
70 tr -d ' ' |
71 cut -f2 -d:)
Will Drewry78992a32010-07-21 14:02:20 -050072 info "rootfs is ${root_fs_blocks} blocks of ${root_fs_block_sz} bytes"
Will Drewrybcbf1c42010-07-03 10:23:30 -050073 if [[ ${root_fs_block_sz} -ne 4096 ]]; then
74 error "Root file system blocks are not 4k!"
75 fi
76
77 info "Generating root fs hash tree."
78 # Runs as sudo in case the image is a block device.
Will Drewry1670d482010-07-09 13:08:38 -070079 table=$(sudo verity create ${FLAGS_verity_tree_depth} \
80 ${FLAGS_verity_hash_alg} \
Will Drewrybcbf1c42010-07-03 10:23:30 -050081 ${FLAGS_rootfs_image} \
82 ${root_fs_blocks} \
83 ${FLAGS_rootfs_hash})
Will Drewry821d07c2010-07-03 17:14:58 -070084 if [[ -f "${FLAGS_rootfs_hash}" ]]; then
85 sudo chmod a+r "${FLAGS_rootfs_hash}"
86 fi
Will Drewrybcbf1c42010-07-03 10:23:30 -050087 # Don't claim the root device unless the root= flag is pointed to
88 # the verified boot device. Doing so will claim /dev/sdDP out from
89 # under the system.
90 if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then
Kenneth Watersed54d932010-09-21 10:29:54 -070091 if [[ "${FLAGS_arch}" = "x86" ]]; then
92 base_root='/dev/sd%D%P'
93 elif [[ "${FLAGS_arch}" = "arm" ]]; then
94 base_root='/dev/${devname}${rootpart}'
95 fi
96 table=${table//HASH_DEV/${base_root}}
97 table=${table//ROOT_DEV/${base_root}}
Will Drewrybcbf1c42010-07-03 10:23:30 -050098 fi
Will Drewry78992a32010-07-21 14:02:20 -050099 verity_args="dm=\"vroot none ro,${table}\""
Will Drewry1670d482010-07-09 13:08:38 -0700100 info "dm-verity configuration: ${verity_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500101fi
102
103mkdir -p "${FLAGS_working_dir}"
104cat <<EOF > "${FLAGS_working_dir}/boot.config"
105root=${FLAGS_root}
Will Drewry1670d482010-07-09 13:08:38 -0700106dm_verity.error_behavior=${FLAGS_verity_error_behavior}
107dm_verity.max_bios=${FLAGS_verity_max_ios}
108${verity_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500109${FLAGS_boot_args}
110EOF
111
112WORK="${WORK} ${FLAGS_working_dir}/boot.config"
113info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
114
Will Drewry69563b72010-06-24 16:12:58 -0500115# FIXME: At the moment, we're working on signed images for x86 only. ARM will
116# support this before shipping, but at the moment they don't.
117if [[ "${FLAGS_arch}" = "x86" ]]; then
118
Will Drewrybcbf1c42010-07-03 10:23:30 -0500119 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
120 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
121 # BIOS will use a separate signed kernel partition, which we'll create now.
122 # FIXME: remove serial output, debugging messages.
123 mkdir -p ${FLAGS_working_dir}
124 cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \
125 > "${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500126earlyprintk=serial,ttyS0,115200
127console=ttyS0,115200
128init=/sbin/init
129add_efi_memmap
130boot=local
131rootwait
Will Drewry69563b72010-06-24 16:12:58 -0500132ro
133noresume
134noswap
135i915.modeset=1
136loglevel=7
137cros_secure
Bill Richardson8bfa4682010-07-23 17:24:15 -0700138kern_guid=%U
Will Drewry69563b72010-06-24 16:12:58 -0500139EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500140 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500141
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800142 # We sign the image with the recovery_key, because this is what goes onto the
143 # USB key. We can only boot from the USB drive in recovery mode.
Tan Gao843b70a2010-08-17 09:41:48 -0700144 # For dev install shim, we need to use the installer keyblock instead of
145 # the recovery keyblock because of the difference in flags.
146 if [ ${FLAGS_use_dev_keys} -eq ${FLAGS_TRUE} ]; then
147 USB_KEYBLOCK=installer_kernel.keyblock
148 info "DEBUG: use dev install signing key"
149 else
150 USB_KEYBLOCK=recovery_kernel.keyblock
151 info "DEBUG: use recovery signing key"
152 fi
Bill Richardson2ace49e2010-07-01 10:23:27 -0700153
Will Drewrybcbf1c42010-07-03 10:23:30 -0500154 # Create and sign the kernel blob
155 vbutil_kernel \
156 --pack "${FLAGS_to}" \
Tan Gao843b70a2010-08-17 09:41:48 -0700157 --keyblock "${FLAGS_keys_dir}/${USB_KEYBLOCK}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800158 --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \
Will Drewrybcbf1c42010-07-03 10:23:30 -0500159 --version 1 \
160 --config "${FLAGS_working_dir}/config.txt" \
161 --bootloader /lib64/bootstub/bootstub.efi \
162 --vmlinuz "${FLAGS_vmlinuz}"
Will Drewry69563b72010-06-24 16:12:58 -0500163
Will Drewrybcbf1c42010-07-03 10:23:30 -0500164 # And verify it.
165 vbutil_kernel \
166 --verify "${FLAGS_to}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800167 --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk"
168
169
170 # Now we re-sign the same image using the normal keys. This is the kernel
171 # image that is put on the hard disk by the installer. Note: To save space on
172 # the USB image, we're only emitting the new verfication block, and the
173 # installer just replaces that part of the hard disk's kernel partition.
174 vbutil_kernel \
175 --repack "${FLAGS_hd_vblock}" \
176 --vblockonly \
177 --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \
178 --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \
179 --oldblob "${FLAGS_to}"
180
181
182 # To verify it, we have to replace the vblock from the original image.
183 tempfile=$(mktemp)
184 trap "rm -f $tempfile" EXIT
185 cat "${FLAGS_hd_vblock}" > $tempfile
186 dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile
187
188 vbutil_kernel \
189 --verify $tempfile \
190 --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk"
191
192 rm -f $tempfile
193 trap - EXIT
Will Drewry69563b72010-06-24 16:12:58 -0500194
Will Drewrybcbf1c42010-07-03 10:23:30 -0500195elif [[ "${FLAGS_arch}" = "arm" ]]; then
Kenneth Watersed54d932010-09-21 10:29:54 -0700196 # FIXME: This stuff is unsigned, and will likely change with vboot_reference
197 # but it doesn't technically have to.
198
199 kernel_script="${FLAGS_working_dir}/kernel.scr"
200 kernel_script_img="${FLAGS_working_dir}/kernel.scr.uimg"
201 # HACK: !! Kernel image construction requires some stuff from portage, not
202 # sure how to get that information here cleanly !!
203 kernel_image="${FLAGS_vmlinuz/vmlinuz/vmlinux.uimg}"
204 WORK="${WORK} ${kernel_script} ${kernel_script_img}"
205
206 kernel_size=$((($(stat -c %s "${kernel_image}") + 511) / 512))
207 script_size=16
208
209 # Build boot script image
210 echo -n 'setenv bootargs ${bootargs} ' > "${kernel_script}"
211 tr '\n' ' ' <"${FLAGS_working_dir}/boot.config" >> "${kernel_script}"
212 echo >> "${kernel_script}"
213 printf 'read ${devtype} 0:${kernelpart} ${loadaddr} %x %x\n' \
214 ${script_size} ${kernel_size} >> "${kernel_script}"
215 echo 'bootm ${loadaddr}' >> ${kernel_script}
216 mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
217 -n kernel_script -d "${kernel_script}" "${kernel_script_img}"
218
219 if [ $(stat -c %s "${kernel_script_img}") -gt $((512 * ${script_size})) ]
220 then
221 echo 'Kernel script too large for reserved space.'
222 exit 1
223 fi
224
225 # Assemble image
226 rm -f "${FLAGS_to}"
227 dd if="${kernel_script_img}" of="${FLAGS_to}" bs=512 count="${script_size}"
228 dd if="${kernel_image}" of="${FLAGS_to}" bs=512 seek="${script_size}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500229else
230 error "Unknown arch: ${FLAGS_arch}"
Will Drewry69563b72010-06-24 16:12:58 -0500231fi
232
233set +e # cleanup failure is a-ok
234
235if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500236 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500237 rm ${WORK}
238 rmdir ${FLAGS_working_dir}
239fi
240
Will Drewrybcbf1c42010-07-03 10:23:30 -0500241info "Kernel partition image emitted: ${FLAGS_to}"
242
243if [[ -f ${FLAGS_rootfs_hash} ]]; then
244 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
245fi