blob: 5ee3e305a322d0fffade0088d204273757b9894a [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
Greg Spencer798d75f2011-02-01 22:04:49 -08009# --- BEGIN COMMON.SH BOILERPLATE ---
10# Load common CrOS utilities. Inside the chroot this file is installed in
11# /usr/lib/crosutils. Outside the chroot we find it relative to the script's
12# location.
13find_common_sh() {
14 local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")"))
15 local path
16
17 SCRIPT_ROOT=
18 for path in "${common_paths[@]}"; do
19 if [ -r "${path}/common.sh" ]; then
20 SCRIPT_ROOT=${path}
21 break
22 fi
23 done
24}
25
26find_common_sh
27. "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1)
28# --- END COMMON.SH BOILERPLATE ---
Will Drewry69563b72010-06-24 16:12:58 -050029
30get_default_board
31
32# Flags.
33DEFINE_string arch "x86" \
34 "The boot architecture: arm or x86. (Default: x86)"
35DEFINE_string to "/tmp/vmlinuz.image" \
36 "The path to the kernel image to be created. (Default: /tmp/vmlinuz.image)"
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +080037DEFINE_string hd_vblock "/tmp/vmlinuz_hd.vblock" \
38 "The path to the installed kernel's vblock (Default: /tmp/vmlinuz_hd.vblock)"
Will Drewry69563b72010-06-24 16:12:58 -050039DEFINE_string vmlinuz "vmlinuz" \
40 "The path to the kernel (Default: vmlinuz)"
41DEFINE_string working_dir "/tmp/vmlinuz.working" \
42 "Working directory for in-progress files. (Default: /tmp/vmlinuz.working)"
43DEFINE_boolean keep_work ${FLAGS_FALSE} \
44 "Keep temporary files (*.keyblock, *.vbpubk). (Default: false)"
45DEFINE_string keys_dir "${SRC_ROOT}/platform/vboot_reference/tests/testkeys" \
Bill Richardson2ace49e2010-07-01 10:23:27 -070046 "Directory with the RSA signing keys. (Defaults to test keys)"
Tan Gao843b70a2010-08-17 09:41:48 -070047DEFINE_boolean use_dev_keys ${FLAGS_FALSE} \
48 "Use developer keys for signing. (Default: false)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050049# Note, to enable verified boot, the caller would manually pass:
Will Drewryb910de82011-02-23 13:26:50 -060050# --boot_args='dm="... %U+1 %U+1 ..." \
Will Drewry69563b72010-06-24 16:12:58 -050051# --root=/dev/dm-0
52DEFINE_string boot_args "noinitrd" \
53 "Additional boot arguments to pass to the commandline (Default: noinitrd)"
Will Drewryb910de82011-02-23 13:26:50 -060054# By default, we use a firmware enumerated value, but it isn't reliable for
55# production use. If +%d can be added upstream, then we can use:
56# root=PARTUID=uuid+1
Will Drewry69563b72010-06-24 16:12:58 -050057DEFINE_string root "/dev/sd%D%P" \
Will Drewryb910de82011-02-23 13:26:50 -060058 "Expected device root partition"
Will Drewrybcbf1c42010-07-03 10:23:30 -050059# If provided, will automatically add verified boot arguments.
60DEFINE_string rootfs_image "" \
61 "Optional path to the rootfs device or image.(Default: \"\")"
62DEFINE_string rootfs_hash "" \
63 "Optional path to output the rootfs hash to. (Default: \"\")"
Will Drewry1670d482010-07-09 13:08:38 -070064DEFINE_integer verity_error_behavior 2 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050065 "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \
66(Default: 2)"
Will Drewry1670d482010-07-09 13:08:38 -070067DEFINE_integer verity_tree_depth 1 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050068 "Optional Verified boot hash tree depth. (Default: 1)"
Will Drewryb910de82011-02-23 13:26:50 -060069DEFINE_integer verity_max_ios -1 \
70 "Optional number of outstanding I/O operations. (Default: -1)"
Will Drewry1670d482010-07-09 13:08:38 -070071DEFINE_string verity_hash_alg "sha1" \
72 "Cryptographic hash algorithm used for dm-verity. (Default: sha1)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050073
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +080074# TODO(clchiou): Remove this flag after arm verified boot is stable
75DEFINE_boolean crosbug12352_arm_kernel_signing ${FLAGS_FALSE} \
76 "Sign kernel partition for ARM images (temporary hack)."
77
Will Drewry69563b72010-06-24 16:12:58 -050078# Parse flags
79FLAGS "$@" || exit 1
80eval set -- "${FLAGS_ARGV}"
81
82# Die on error
83set -e
84
Will Drewry1670d482010-07-09 13:08:38 -070085verity_args=
Will Drewrybcbf1c42010-07-03 10:23:30 -050086# Even with a rootfs_image, root= is not changed unless specified.
87if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -050088 # Gets the number of blocks. 4096 byte blocks _are_ expected.
89 root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
90 grep "Block count" |
91 tr -d ' ' |
92 cut -f2 -d:)
Will Drewrybcbf1c42010-07-03 10:23:30 -050093 root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
94 grep "Block size" |
95 tr -d ' ' |
96 cut -f2 -d:)
Will Drewry78992a32010-07-21 14:02:20 -050097 info "rootfs is ${root_fs_blocks} blocks of ${root_fs_block_sz} bytes"
Will Drewrybcbf1c42010-07-03 10:23:30 -050098 if [[ ${root_fs_block_sz} -ne 4096 ]]; then
99 error "Root file system blocks are not 4k!"
100 fi
101
102 info "Generating root fs hash tree."
103 # Runs as sudo in case the image is a block device.
Will Drewry1670d482010-07-09 13:08:38 -0700104 table=$(sudo verity create ${FLAGS_verity_tree_depth} \
105 ${FLAGS_verity_hash_alg} \
Will Drewrybcbf1c42010-07-03 10:23:30 -0500106 ${FLAGS_rootfs_image} \
107 ${root_fs_blocks} \
108 ${FLAGS_rootfs_hash})
Will Drewry821d07c2010-07-03 17:14:58 -0700109 if [[ -f "${FLAGS_rootfs_hash}" ]]; then
110 sudo chmod a+r "${FLAGS_rootfs_hash}"
111 fi
Will Drewrybcbf1c42010-07-03 10:23:30 -0500112 # Don't claim the root device unless the root= flag is pointed to
113 # the verified boot device. Doing so will claim /dev/sdDP out from
114 # under the system.
115 if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then
Kenneth Watersed54d932010-09-21 10:29:54 -0700116 if [[ "${FLAGS_arch}" = "x86" ]]; then
Will Drewryb910de82011-02-23 13:26:50 -0600117 base_root='%U+1' # kern_guid + 1
Kenneth Watersed54d932010-09-21 10:29:54 -0700118 elif [[ "${FLAGS_arch}" = "arm" ]]; then
119 base_root='/dev/${devname}${rootpart}'
120 fi
121 table=${table//HASH_DEV/${base_root}}
122 table=${table//ROOT_DEV/${base_root}}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500123 fi
Will Drewry78992a32010-07-21 14:02:20 -0500124 verity_args="dm=\"vroot none ro,${table}\""
Will Drewry1670d482010-07-09 13:08:38 -0700125 info "dm-verity configuration: ${verity_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500126fi
127
128mkdir -p "${FLAGS_working_dir}"
Will Drewryd6435d42010-10-20 15:37:46 -0500129
130# Only let dm-verity block if rootfs verification is configured.
131dev_wait=0
132if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then
133 dev_wait=1
134fi
135
Will Drewrybcbf1c42010-07-03 10:23:30 -0500136cat <<EOF > "${FLAGS_working_dir}/boot.config"
137root=${FLAGS_root}
Olof Johanssone0b75512011-01-26 14:31:26 -0800138quiet
139loglevel=1
140rootwait
141ro
Will Drewry1670d482010-07-09 13:08:38 -0700142dm_verity.error_behavior=${FLAGS_verity_error_behavior}
143dm_verity.max_bios=${FLAGS_verity_max_ios}
Will Drewryd6435d42010-10-20 15:37:46 -0500144dm_verity.dev_wait=${dev_wait}
Will Drewry1670d482010-07-09 13:08:38 -0700145${verity_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500146${FLAGS_boot_args}
147EOF
148
149WORK="${WORK} ${FLAGS_working_dir}/boot.config"
150info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
151
Will Drewry69563b72010-06-24 16:12:58 -0500152if [[ "${FLAGS_arch}" = "x86" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500153 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
154 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
155 # BIOS will use a separate signed kernel partition, which we'll create now.
156 # FIXME: remove serial output, debugging messages.
Will Drewrybcbf1c42010-07-03 10:23:30 -0500157 cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \
158 > "${FLAGS_working_dir}/config.txt"
Nick Sandersada8a1f2010-10-26 02:56:51 -0700159console=tty2
Will Drewry69563b72010-06-24 16:12:58 -0500160init=/sbin/init
161add_efi_memmap
162boot=local
Will Drewry69563b72010-06-24 16:12:58 -0500163noresume
164noswap
165i915.modeset=1
Will Drewry69563b72010-06-24 16:12:58 -0500166cros_secure
Bill Richardson8bfa4682010-07-23 17:24:15 -0700167kern_guid=%U
Luigi Semenzato195aebe2010-10-20 17:35:00 -0700168tpm_tis.force=1
169tpm_tis.interrupts=0
Vadim Bendebury8e623f62011-02-28 10:28:31 -0800170nmi_watchdog=panic,lapic
Will Drewry69563b72010-06-24 16:12:58 -0500171EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500172 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500173
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800174 bootloader_path="/lib64/bootstub/bootstub.efi"
175 kernel_image="${FLAGS_vmlinuz}"
176
177 sign_the_kernel=${FLAGS_TRUE}
178elif [[ "${FLAGS_arch}" = "arm" ]]; then
179 cp "${FLAGS_working_dir}/boot.config" "${FLAGS_working_dir}/config.txt"
180 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
181
182 kernel_script="${FLAGS_working_dir}/kernel.scr"
183 kernel_script_img="${FLAGS_working_dir}/kernel.scr.uimg"
184
185 echo -n 'setenv bootargs ${bootargs} ' > "${kernel_script}"
186 tr '\n' ' ' < "${FLAGS_working_dir}/boot.config" >> "${kernel_script}"
187 echo >> "${kernel_script}"
188
189 mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
190 -n kernel_script -d "${kernel_script}" "${kernel_script_img}"
191
192 WORK="${WORK} ${kernel_script} ${kernel_script_img}"
193
194 bootloader_path="${kernel_script_img}"
195 kernel_image="${FLAGS_vmlinuz/vmlinuz/vmlinux.uimg}"
196
197 sign_the_kernel=${FLAGS_crosbug12352_arm_kernel_signing}
198else
199 error "Unknown arch: ${FLAGS_arch}"
200fi
201
202if [[ "${sign_the_kernel}" -eq "${FLAGS_TRUE}" ]]; then
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800203 # We sign the image with the recovery_key, because this is what goes onto the
204 # USB key. We can only boot from the USB drive in recovery mode.
Tan Gao843b70a2010-08-17 09:41:48 -0700205 # For dev install shim, we need to use the installer keyblock instead of
206 # the recovery keyblock because of the difference in flags.
207 if [ ${FLAGS_use_dev_keys} -eq ${FLAGS_TRUE} ]; then
208 USB_KEYBLOCK=installer_kernel.keyblock
209 info "DEBUG: use dev install signing key"
210 else
211 USB_KEYBLOCK=recovery_kernel.keyblock
212 info "DEBUG: use recovery signing key"
213 fi
Bill Richardson2ace49e2010-07-01 10:23:27 -0700214
Will Drewrybcbf1c42010-07-03 10:23:30 -0500215 # Create and sign the kernel blob
216 vbutil_kernel \
217 --pack "${FLAGS_to}" \
Tan Gao843b70a2010-08-17 09:41:48 -0700218 --keyblock "${FLAGS_keys_dir}/${USB_KEYBLOCK}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800219 --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \
Will Drewrybcbf1c42010-07-03 10:23:30 -0500220 --version 1 \
221 --config "${FLAGS_working_dir}/config.txt" \
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800222 --bootloader "${bootloader_path}" \
223 --vmlinuz "${kernel_image}" \
224 --arch "${FLAGS_arch}"
Will Drewry69563b72010-06-24 16:12:58 -0500225
Will Drewrybcbf1c42010-07-03 10:23:30 -0500226 # And verify it.
227 vbutil_kernel \
228 --verify "${FLAGS_to}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800229 --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk"
230
231
232 # Now we re-sign the same image using the normal keys. This is the kernel
233 # image that is put on the hard disk by the installer. Note: To save space on
234 # the USB image, we're only emitting the new verfication block, and the
235 # installer just replaces that part of the hard disk's kernel partition.
236 vbutil_kernel \
237 --repack "${FLAGS_hd_vblock}" \
238 --vblockonly \
239 --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \
240 --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \
241 --oldblob "${FLAGS_to}"
242
243
244 # To verify it, we have to replace the vblock from the original image.
245 tempfile=$(mktemp)
246 trap "rm -f $tempfile" EXIT
247 cat "${FLAGS_hd_vblock}" > $tempfile
248 dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile
249
250 vbutil_kernel \
251 --verify $tempfile \
252 --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk"
253
254 rm -f $tempfile
255 trap - EXIT
Will Drewry69563b72010-06-24 16:12:58 -0500256
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800257else
258 # FIXME: This stuff is unsigned. This part should be removed or made
259 # non-default after ARM verified boot is stable.
Kenneth Watersed54d932010-09-21 10:29:54 -0700260
261 kernel_size=$((($(stat -c %s "${kernel_image}") + 511) / 512))
262 script_size=16
263
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800264 # Add more scripts to boot script image for loading kernel image
Allen Martin5b2f49d2010-12-09 16:35:39 -0800265 printf 'read ${devtype} ${devnum}:${kernelpart} ${loadaddr} %x %x\n' \
Kenneth Watersed54d932010-09-21 10:29:54 -0700266 ${script_size} ${kernel_size} >> "${kernel_script}"
267 echo 'bootm ${loadaddr}' >> ${kernel_script}
268 mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
269 -n kernel_script -d "${kernel_script}" "${kernel_script_img}"
270
271 if [ $(stat -c %s "${kernel_script_img}") -gt $((512 * ${script_size})) ]
272 then
273 echo 'Kernel script too large for reserved space.'
274 exit 1
275 fi
276
277 # Assemble image
278 rm -f "${FLAGS_to}"
279 dd if="${kernel_script_img}" of="${FLAGS_to}" bs=512 count="${script_size}"
280 dd if="${kernel_image}" of="${FLAGS_to}" bs=512 seek="${script_size}"
Kenneth Waters9bc78b32010-09-22 13:54:44 -0700281
282 # TODO: HACK: Until the kernel partition contains a signed image, create a
283 # phony hd.vblock to keep chromeos-install and cros_generate_update_payload
284 # working.
285 dd if="${FLAGS_to}" of="${FLAGS_hd_vblock}" bs=64K count=1
Will Drewry69563b72010-06-24 16:12:58 -0500286fi
287
288set +e # cleanup failure is a-ok
289
290if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500291 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500292 rm ${WORK}
293 rmdir ${FLAGS_working_dir}
294fi
295
Will Drewrybcbf1c42010-07-03 10:23:30 -0500296info "Kernel partition image emitted: ${FLAGS_to}"
297
298if [[ -f ${FLAGS_rootfs_hash} ]]; then
299 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
300fi