Debian Codegraph @

3rdparty/vboot/utility/dev_debug_vboot

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/bin/sh`. -ue
2 # Copyright 2011 The ChromiumOS Authors
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5 #
6 # Usage: dev_debug_vboot [ --cleanup | DIRECTORY ]
7 #
8 # This extracts some useful debugging information about verified boot. A short
9 # summary is printed on stdout, more detailed information and working files are
10 # left in a log directory.
11 #
12 ##############################################################################
13
14 # Clean up PATH for root use. Note that we're assuming [ is always built-in.
15 [scip-shell system . . `[`. "${EUID:-0}" = 0 ] && PATH=/bin:/sbin:/usr/bin:/usr/sbinscip-shell filesystem . . `/bin:/sbin:/usr/bin:/usr/sbin`.
16
17 PUBLOGFILE="/var/log/debug_vboot_noisy.logscip-shell filesystem . . `/var/log/debug_vboot_noisy.log`."
18
19 OPT_CLEANUP=
20 OPT_BIOS=
21 OPT_FORCE=
22 OPT_IMAGE=
23 OPT_KERNEL=
24 OPT_VERBOSE=
25
26 FLAG_SAVE_LOG_FILE=yes
27
28 LOGFILE=/dev/stdoutscip-shell filesystem . . `/dev/stdout`.
29 TMPDIR=
30
31 ##############################################################################
32
33 usagescip-shell shell coreboot 25.09+dfsg-1 usage().() {
34 local prog
35
36 prog=${0##*/}
37 catscip-shell system . . cat. <<EOF
38
39 Usage: $prog [options] [DIRECTORY]
40
41 This logs as much as it can about the verified boot process. With no arguments
42 it will attempt to read the current BIOS, extract the firmware keys, and use
43 those keys to validate all the ChromeOS kernel partitions it can find. A
44 summary output is printed on stdout, and the detailed log is copied to
45 $PUBLOGFILE afterwards.
46
47 If a directory is given, it will attempt to use the components from that
48 directory and will leave the detailed log in that directory.
49
50 Options:
51
52 -b FILE, --bios FILE Specify the BIOS image to use
53 -i FILE, --image FILE Specify the disk image to use
54 -k FILE, --kernel FILE Specify the kernel partition image to use
55 -v Spew the detailed log to stdout
56
57 -c, --cleanup Delete the DIRECTORY when done
58
59 -h, --help Print this help message and exit
60
61 EOF
62 exit 0
63 }
64
65 cleanupscip-shell shell coreboot 25.09+dfsg-1 cleanup().() {
66 if [scip-shell system . . `[`. -n "${FLAG_SAVE_LOG_FILE}" ]; then
67 if cpscip-shell system . . cp. -f "${LOGFILE}" "${PUBLOGFILE}" 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then
68 infoscip-shell shell coreboot 25.09+dfsg-1 info(). "Exporting log file as ${PUBLOGFILE}"
69 fi
70 fi
71 if [scip-shell system . . `[`. -n "${OPT_CLEANUP}" ] && [scip-shell system . . `[`. -d "${TMPDIR}" ] ; then
72 cd /scip-shell filesystem . . `/`.
73 rmscip-shell system . . rm. -rf "${TMPDIR}"
74 fi
75 }
76
77 diescip-shell shell coreboot 25.09+dfsg-1 die().() {
78 echoscip-shell system . . echo. "$*" 1>&2
79 exit 1
80 }
81
82 infoscip-shell shell coreboot 25.09+dfsg-1 info().() {
83 echoscip-shell system . . echo. "$@"
84 echoscip-shell system . . echo. "#" "$@" >> "$LOGFILE"
85 }
86
87 infonscip-shell shell coreboot 25.09+dfsg-1 infon().() {
88 echoscip-shell system . . echo. -n "$@"
89 echoscip-shell system . . echo. "#" "$@" >> "$LOGFILE"
90 }
91
92 debugscip-shell shell coreboot 25.09+dfsg-1 debug().() {
93 echoscip-shell system . . echo. "#" "$@" >> "$LOGFILE"
94 }
95
96 logscip-shell shell coreboot 25.09+dfsg-1 log().() {
97 echoscip-shell system . . echo. "+" "$@" >> "$LOGFILE"
98 "$@" >> "$LOGFILE" 2>&1
99 }
100
101 logheadscip-shell shell coreboot 25.09+dfsg-1 loghead().() {
102 echoscip-shell system . . echo. "+" "$@" "| head" >> "$LOGFILE"
103 "$@" | headscip-shell system . . head. >> "$LOGFILE" 2>&1
104 }
105
106 logdiescip-shell shell coreboot 25.09+dfsg-1 logdie().() {
107 echoscip-shell system . . echo. "+ERROR:" "$@" >> "$LOGFILE"
108 diescip-shell shell coreboot 25.09+dfsg-1 die(). "$@"
109 }
110
111 resultscip-shell shell coreboot 25.09+dfsg-1 result().() {
112 LAST_RESULT=$?
113 if [scip-shell system . . `[`. "${LAST_RESULT}" = "0" ]; then
114 infoscip-shell shell coreboot 25.09+dfsg-1 info(). "OK"
115 else
116 infoscip-shell shell coreboot 25.09+dfsg-1 info(). "FAILED"
117 fi
118 }
119
120 require_utilsscip-shell shell coreboot 25.09+dfsg-1 require_utils().() {
121 local missing
122
123 missing=
124 for tool in $* ; do
125 if ! type "$tool" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 ; then
126 missing="$missing $tool"
127 fi
128 done
129 if [scip-shell system . . `[`. -n "$missing" ]; then
130 logdiescip-shell shell coreboot 25.09+dfsg-1 logdie(). "can't find these programs: $missing"
131 fi
132 }
133
134 extract_kerns_from_filescip-shell shell coreboot 25.09+dfsg-1 extract_kerns_from_file().() {
135 local start
136 local size
137 local part
138 local rest
139
140 debugscip-shell shell coreboot 25.09+dfsg-1 debug(). "Extracting kernel partitions from $1 ..."
141 cgpt find -v -t kernel "$1" | grepscip-shell system . . grep. 'Label:' |
142 while read start size part rest; do
143 name="part_${part}"
144 logscip-shell shell coreboot 25.09+dfsg-1 log(). dd if="$1" bs=512 skip=${start} count=${size} of="${name}" &&
145 echoscip-shell system . . echo. "${name}"
146 done
147 }
148
149 format_as_tpm_versionscip-shell shell coreboot 25.09+dfsg-1 format_as_tpm_version().() {
150 local data_key_ver="$1"
151 local ver="$2"
152 printfscip-shell system . . printf. '0x%04x%04x' "${data_key_ver}" "${ver}"
153 }
154
155 fix_old_namesscip-shell shell coreboot 25.09+dfsg-1 fix_old_names().() {
156 # Convert any old-style names to new-style
157 [scip-shell system . . `[`. -f GBB_Area ] && logscip-shell shell coreboot 25.09+dfsg-1 log(). mv -f GBB_Area GBB
158 [scip-shell system . . `[`. -f Firmware_A_Key ] && logscip-shell shell coreboot 25.09+dfsg-1 log(). mv -f Firmware_A_Key VBLOCK_A
159 [scip-shell system . . `[`. -f Firmware_B_Key ] && logscip-shell shell coreboot 25.09+dfsg-1 log(). mv -f Firmware_B_Key VBLOCK_B
160 [scip-shell system . . `[`. -f Firmware_A_Data ] && logscip-shell shell coreboot 25.09+dfsg-1 log(). mv -f Firmware_A_Data FW_MAIN_A
161 [scip-shell system . . `[`. -f Firmware_B_Data ] && logscip-shell shell coreboot 25.09+dfsg-1 log(). mv -f Firmware_B_Data FW_MAIN_B
162 truescip-shell system . . true.
163 }
164
165 report_firmware_mismatchscip-shell shell coreboot 25.09+dfsg-1 report_firmware_mismatch().() {
166 # Check for mismatched OS/firmware and send UMA metrics
167 if ! type "chromeos-firmwareupdate" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 ; then
168 debugscip-shell shell coreboot 25.09+dfsg-1 debug(). "Skip checking firmware mismatch: missing 'chromeos-firmwareupdate'."
169 return 1
170 fi
171
172 local cros_fwid="$(crossystem fwid 2>/dev/nullscip-shell filesystem . . `/dev/null`.)"
173
174 local model="$(cros_config /scip-shell filesystem . . `/`. name || echoscip-shell system . . echo. unknown)"
175 local manifest="$(chromeos-firmwareupdate --manifest 2>/dev/nullscip-shell filesystem . . `/dev/null`.)"
176 local expect_fwid=$(echoscip-shell system . . echo. "${manifest}" |
177 jqscip-shell system . . jq. -c -r ".${model}.host.versions.rw" 2>/dev/nullscip-shell filesystem . . `/dev/null`.)
178
179 if [scip-shell system . . `[`. -z "${expect_fwid}" ] || [scip-shell system . . `[`. "${expect_fwid}" = "null" ]; then
180 debugscip-shell shell coreboot 25.09+dfsg-1 debug(). "Failed to get the expected fwid for model '${model}'."
181 elif [scip-shell system . . `[`. "${cros_fwid}" = "${expect_fwid}" ]; then
182 infoscip-shell shell coreboot 25.09+dfsg-1 info(). "Report UMA metrics: System firmware matched OS bundled firmware."
183 metrics_client -e "Platform.Firmware.Mismatch" 0 2
184 else
185 infoscip-shell shell coreboot 25.09+dfsg-1 info(). "Report UMA metrics: System firmware mismatched OS bundled firmware."
186 metrics_client -e "Platform.Firmware.Mismatch" 1 2
187 fi
188 }
189
190 ##############################################################################
191 # Here we go...
192
193 umask 022
194
195 # defaults
196 DEV_DEBUG_FORCE=
197
198 # override them?
199 [scip-shell system . . `[`. -f /etc/default/vboot_referencescip-shell filesystem . . `/etc/default/vboot_reference`. ] && . /etc/default/vboot_referencescip-shell source . . `/etc/default/vboot_reference`.
200
201 # Pre-parse args to replace actual args with a sanitized version.
202 TEMP=$(getoptscip-shell system . . getopt. -o hvb:i:k:cf --long help,bios:,image:,kernel:,cleanup,force \
203 -n $0 -- "$@")
204 eval set -- "$TEMP"
205
206 # Now look at them.
207 while truescip-shell system . . true. ; do
208 case "${1:-}" in
209 -b|--bios)
210 OPT_BIOS=$(readlinkscip-shell system . . readlink. -f "$2")
211 shift 2
212 FLAG_SAVE_LOG_FILE=
213 ;;
214 -i|--image=*)
215 OPT_IMAGE=$(readlinkscip-shell system . . readlink. -f "$2")
216 shift 2
217 FLAG_SAVE_LOG_FILE=
218 ;;
219 -k|--kernel)
220 OPT_KERNEL=$(readlinkscip-shell system . . readlink. -f "$2")
221 shift 2
222 FLAG_SAVE_LOG_FILE=
223 ;;
224 -c|--cleanup)
225 OPT_CLEANUP=yes
226 shift
227 ;;
228 -f|--force)
229 OPT_FORCE=yes
230 shift
231 ;;
232 -v)
233 OPT_VERBOSE=yes
234 shift
235 FLAG_SAVE_LOG_FILE=
236 ;;
237 -h|--help)
238 usagescip-shell shell coreboot 25.09+dfsg-1 usage().
239 break
240 ;;
241 --)
242 shift
243 break
244 ;;
245 *)
246 diescip-shell shell coreboot 25.09+dfsg-1 die(). "Internal error in option parsing"
247 ;;
248 esac
249 done
250
251 if [scip-shell system . . `[`. -z "${1:-}" ]; then
252 TMPDIR=$(mktempscip-shell system . . mktemp. -d /tmp/debug_vboot_XXXXXXXXXscip-shell filesystem . . `/tmp/debug_vboot_XXXXXXXXX`.)
253 else
254 TMPDIR="$1"
255 [scip-shell system . . `[`. -d ${TMPDIR} ] || diescip-shell shell coreboot 25.09+dfsg-1 die(). "$TMPDIR doesn't exist"
256 FLAG_SAVE_LOG_FILE=
257 fi
258 [scip-shell system . . `[`. -z "${OPT_VERBOSE}" ] && LOGFILE="${TMPDIR}/noisy.log"
259
260 [scip-shell system . . `[`. -d ${TMPDIR} ] || mkdirscip-shell system . . mkdir. -p ${TMPDIR} || exit 1
261 cd ${TMPDIR} || exit 1
262 echoscip-shell system . . echo. "Running $0 $*" > "$LOGFILE"
263 logscip-shell shell coreboot 25.09+dfsg-1 log(). date
264 debugscip-shell shell coreboot 25.09+dfsg-1 debug(). "DEV_DEBUG_FORCE=($DEV_DEBUG_FORCE)"
265 debugscip-shell shell coreboot 25.09+dfsg-1 debug(). "OPT_CLEANUP=($OPT_CLEANUP)"
266 debugscip-shell shell coreboot 25.09+dfsg-1 debug(). "OPT_BIOS=($OPT_BIOS)"
267 debugscip-shell shell coreboot 25.09+dfsg-1 debug(). "OPT_FORCE=($OPT_FORCE)"
268 debugscip-shell shell coreboot 25.09+dfsg-1 debug(). "OPT_IMAGE=($OPT_IMAGE)"
269 debugscip-shell shell coreboot 25.09+dfsg-1 debug(). "OPT_KERNEL=($OPT_KERNEL)"
270 debugscip-shell shell coreboot 25.09+dfsg-1 debug(). "FLAG_SAVE_LOG_FILE=($FLAG_SAVE_LOG_FILE)"
271 echoscip-shell system . . echo. "Saving verbose log as $LOGFILE"
272 trap cleanup EXIT
273
274 if [scip-shell system . . `[`. -n "${DEV_DEBUG_FORCE}" ] && [scip-shell system . . `[`. -z "${OPT_FORCE}" ]; then
275 infoscip-shell shell coreboot 25.09+dfsg-1 info(). "Not gonna do anything without the --force option."
276 exit 0
277 fi
278
279
280 # Make sure we have the programs we need
281 need="futility"
282 [scip-shell system . . `[`. -z "${OPT_BIOS}" ] && need="$need flashrom"
283 [scip-shell system . . `[`. -z "${OPT_KERNEL}" ] && need="$need cgpt"
284 require_utilsscip-shell shell coreboot 25.09+dfsg-1 require_utils(). $need
285
286
287 # Assuming we're on a ChromeOS device, see what we know.
288 set +e
289 logscip-shell shell coreboot 25.09+dfsg-1 log(). crossystem --all
290 logscip-shell shell coreboot 25.09+dfsg-1 log(). rootdev -s
291 logscip-shell shell coreboot 25.09+dfsg-1 log(). ls -aCF /rootscip-shell filesystem . . `/root`.
292 logscip-shell shell coreboot 25.09+dfsg-1 log(). ls -aCF /mnt/stateful_partitionscip-shell filesystem . . `/mnt/stateful_partition`.
293 devs=$(awkscip-shell system . . awk. '/(mmcblk[0-9])$|(sd[a-z])$|(nvme[0-9]+n[0-9]+)$/ {print "/dev/"$4}scip-shell filesystem . . `/(mmcblk[0-9])$|(sd[a-z])$|(nvme[0-9]+n[0-9]+)$/ {print "/dev/"$4}`.' /proc/partitionsscip-shell filesystem . . `/proc/partitions`.)
294 for d in $devs; do
295 logscip-shell shell coreboot 25.09+dfsg-1 log(). cgpt show $d
296 done
297 logscip-shell shell coreboot 25.09+dfsg-1 log(). futility flash --wp-status
298 tpm_fwver=$(crossystem tpm_fwver) || tpm_fwver="UNKNOWN"
299 tpm_kernver=$(crossystem tpm_kernver) || tpm_kernver="UNKNOWN"
300 set -e
301
302
303 infoscip-shell shell coreboot 25.09+dfsg-1 info(). "Extracting BIOS components..."
304 BIOS_IMAGE="${OPT_BIOS}"
305 if [scip-shell system . . `[`. -z "${BIOS_IMAGE}" ]; then
306 infoscip-shell shell coreboot 25.09+dfsg-1 info(). "Reading BIOS image from flash..."
307 BIOS_IMAGE="bios.rom"
308 if ! logscip-shell shell coreboot 25.09+dfsg-1 log(). futility read "${BIOS_IMAGE}" ; then
309 logdiescip-shell shell coreboot 25.09+dfsg-1 logdie(). "Fail to read BIOS."
310 fi
311 fi
312
313 # Extract all FMAP sections.
314 logscip-shell shell coreboot 25.09+dfsg-1 log(). futility dump_fmap -x "${BIOS_IMAGE}"
315 fix_old_namesscip-shell shell coreboot 25.09+dfsg-1 fix_old_names().
316
317 infoscip-shell shell coreboot 25.09+dfsg-1 info(). "Pulling root and recovery keys from GBB..."
318 logscip-shell shell coreboot 25.09+dfsg-1 log(). futility gbb -g --rootkey rootkey.vbpubk \
319 --recoverykey recoverykey.vbpubk \
320 "GBB" || logdiescip-shell shell coreboot 25.09+dfsg-1 logdie(). "Unable to extract keys from GBB"
321 logscip-shell shell coreboot 25.09+dfsg-1 log(). futility vbutil_key --unpack rootkey.vbpubk
322 logscip-shell shell coreboot 25.09+dfsg-1 log(). futility vbutil_key --unpack recoverykey.vbpubk
323 futility vbutil_key --unpack rootkey.vbpubk |
324 grepscip-shell system . . grep. -q b11d74edd286c144e1135b49e7f0bc20cf041f10 &&
325 infoscip-shell shell coreboot 25.09+dfsg-1 info(). " Looks like dev-keys"
326
327 # Okay if firmware verification fails.
328 set +e
329 logscip-shell shell coreboot 25.09+dfsg-1 log(). futility verify -P "${BIOS_IMAGE}"
330 # Rerun to get version numbers.
331 futility verify -P "${BIOS_IMAGE}" > tmp.txt
332 for fw in A B; do
333 infonscip-shell shell coreboot 25.09+dfsg-1 infon(). "Verify firmware ${fw} with root key: "
334 grepscip-shell system . . grep. -q "^bios::VBLOCK_${fw}::verified" tmp.txt ; resultscip-shell shell coreboot 25.09+dfsg-1 result().
335 if [scip-shell system . . `[`. "${LAST_RESULT}" = "0" ]; then
336 data_key_ver="$(sedscip-shell system . . sed. -nE "s/^bios::VBLOCK_${fw}::keyblock::data_key::version::(.*)$/\1/p" tmp.txt)"
337 fw_ver="$(sedscip-shell system . . sed. -nE "s/^bios::VBLOCK_${fw}::preamble::firmware_version::(.*)$/\1/p" tmp.txt)"
338 ver="$(format_as_tpm_versionscip-shell shell coreboot 25.09+dfsg-1 format_as_tpm_version(). "${data_key_ver}" "${fw_ver}")"
339 infoscip-shell shell coreboot 25.09+dfsg-1 info(). " TPM=${tpm_fwver}, this=${ver}"
340 fi
341 done
342 set -e
343
344 infoscip-shell shell coreboot 25.09+dfsg-1 info(). "Examining kernels..."
345 if [scip-shell system . . `[`. -n "${OPT_KERNEL}" ]; then
346 kernparts="${OPT_KERNEL}"
347 elif [scip-shell system . . `[`. -n "${OPT_IMAGE}" ]; then
348 if [scip-shell system . . `[`. -f "${OPT_IMAGE}" ]; then
349 kernparts=$(extract_kerns_from_filescip-shell shell coreboot 25.09+dfsg-1 extract_kerns_from_file(). "${OPT_IMAGE}")
350 else
351 kernparts=$(cgpt find -t kernel "${OPT_IMAGE}")
352 fi
353 else
354 kernparts=$(cgpt find -t kernel)
355 fi
356 [scip-shell system . . `[`. -n "${kernparts}" ] || logdiescip-shell shell coreboot 25.09+dfsg-1 logdie(). "No kernels found"
357
358 # Okay if any of the kernel verifications fails.
359 set +e
360 kc=0
361 for kname in ${kernparts}; do
362 if [scip-shell system . . `[`. -f "${kname}" ]; then
363 kfile="${kname}"
364 else
365 kfile="kern_${kc}"
366 debugscip-shell shell coreboot 25.09+dfsg-1 debug(). "copying ${kname} to ${kfile}..."
367 logscip-shell shell coreboot 25.09+dfsg-1 log(). dd if="${kname}" of="${kfile}"
368 fi
369
370 infonscip-shell shell coreboot 25.09+dfsg-1 infon(). "Kernel ${kname}: "
371 logscip-shell shell coreboot 25.09+dfsg-1 log(). futility vbutil_keyblock --unpack "${kfile}" ; resultscip-shell shell coreboot 25.09+dfsg-1 result().
372 if [scip-shell system . . `[`. "${LAST_RESULT}" != "0" ]; then
373 logheadscip-shell shell coreboot 25.09+dfsg-1 loghead(). od -Ax -tx1 "${kfile}"
374 else
375 # Test each kernel with each key
376 for key in VBLOCK_A VBLOCK_B recoverykey.vbpubk; do
377 infonscip-shell shell coreboot 25.09+dfsg-1 infon(). " Verify ${kname} with $key: "
378 logscip-shell shell coreboot 25.09+dfsg-1 log(). futility verify -P --publickey "${key}" "${kfile}" ; resultscip-shell shell coreboot 25.09+dfsg-1 result().
379 if [scip-shell system . . `[`. "${LAST_RESULT}" = "0" ]; then
380 # rerun to get version numbers
381 futility verify -P --publickey "${key}" "${kfile}" > tmp.txt
382 data_key_ver="$(sedscip-shell system . . sed. -nE "s/^kernel::keyblock::data_key::version::(.*)$/\1/p" tmp.txt)"
383 kernel_ver="$(sedscip-shell system . . sed. -nE "s/^kernel::preamble::kernel_version::(.*)$/\1/p" tmp.txt)"
384 ver="$(format_as_tpm_versionscip-shell shell coreboot 25.09+dfsg-1 format_as_tpm_version(). "${data_key_ver}" "${kernel_ver}")"
385 infoscip-shell shell coreboot 25.09+dfsg-1 info(). " TPM=${tpm_kernver} this=${ver}"
386 fi
387 done
388 fi
389
390 kc=$(exprscip-shell system . . expr. $kc + 1)
391 done
392
393 report_firmware_mismatchscip-shell shell coreboot 25.09+dfsg-1 report_firmware_mismatch(). || truescip-shell system . . true.
394
395 exit 0