Debian Codegraph @

3rdparty/vboot/scripts/image_signing/common.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 #
3 # Copyright 2011 The ChromiumOS Authors
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 # shellcheck disable=SC2039,SC2059,SC2155
8
9 # shellcheck source=./common_minimal.sh
10 . "$(dirnamescip-shell system . . dirname. "$0")/common_minimal.sh"
11 CROS_LOG_PREFIX="${PROG}: "
12
13 # Performs clean up by executing actions in the cleanup_actions array in
14 # reversed order.
15 cleanupscip-shell shell coreboot 25.09+dfsg-1 cleanup().() {
16 # Save the existing return value.
17 rv=$?
18 set +e
19
20 cleanup_temps_and_mounts
21 cleanup_loopbacksscip-shell shell coreboot 25.09+dfsg-1 cleanup_loopbacks().
22
23 set -e
24 return $rv
25 }
26
27 # ANSI color codes used when displaying messages.
28 # Taken from src/scripts/common.sh.
29 V_BOLD_GREEN="\e[1;32m"
30 V_BOLD_RED="\e[1;31m"
31 V_BOLD_YELLOW="\e[1;33m"
32 V_VIDOFF="\e[0m"
33
34 # Prints an informational message.
35 # Taken from src/scripts/common.sh.
36 # Arg: MESSAGE
37 infoscip-shell shell coreboot 25.09+dfsg-1 info().() {
38 echoscip-shell system . . echo. -e >&2 "${V_BOLD_GREEN}${CROS_LOG_PREFIX:-}INFO : $*${V_VIDOFF}"
39 }
40
41 # Prints a warning message.
42 # Taken from src/scripts/common.sh.
43 # Arg: MESSAGE
44 warnscip-shell shell coreboot 25.09+dfsg-1 warn().() {
45 echoscip-shell system . . echo. -e >&2 "${V_BOLD_YELLOW}${CROS_LOG_PREFIX:-}WARNING: $*${V_VIDOFF}"
46 }
47
48 # Prints the specified error and exit the script with an error code.
49 # Taken from src/scripts/common.sh.
50 # Args: MESSAGE
51 errorscip-shell shell coreboot 25.09+dfsg-1 error().() {
52 echoscip-shell system . . echo. -e >&2 "${V_BOLD_RED}${CROS_LOG_PREFIX:-}ERROR : $*${V_VIDOFF}"
53 }
54
55 TEMP_LOOP_LIST=$(mktempscip-shell system . . mktemp.)
56
57 # Setup a loopback device for a file and scan for partitions, with retries.
58 #
59 # $1 - The file to back the new loopback device.
60 # $2-$N - Additional arguments to pass to losetup.
61 loopback_partscanscip-shell shell coreboot 25.09+dfsg-1 loopback_partscan().() {
62 local lb_dev image="$1"
63 shift
64
65 # We know partition scanning & dev node creation can be racy with udev and
66 # the kernel, and the kernel does not sleep/wait for it to finish. We have
67 # to use the partx tool manually as it will sleep until things are finished.
68 lb_dev=$(sudoscip-shell system . . sudo. losetup --show -f "$@" "${image}")
69
70 # Cache the path so we can clean it up.
71 echoscip-shell system . . echo. "${lb_dev}" >>"${TEMP_LOOP_LIST}"
72
73 # Ignore problems deleting existing partitions. There shouldn't be any
74 # which will upset partx, but that's actually ok.
75 sudoscip-shell system . . sudo. partx -d "${lb_dev}" 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.
76 sudoscip-shell system . . sudo. partx -a "${lb_dev}"
77
78 echoscip-shell system . . echo. "${lb_dev}"
79 }
80
81 # Detach a loopback device set up earlier.
82 #
83 # $1 - The loop device to detach.
84 # $2-$N - Additional arguments to pass to losetup.
85 loopback_detachscip-shell shell coreboot 25.09+dfsg-1 loopback_detach().() {
86 # Retry the deletes before we detach. crbug.com/469259
87 local i
88 for (( i = 0; i < 10; i++ )); do
89 if sudo partx -d "$1"; then
90 break
91 fi
92 warn "Sleeping & retrying ..."
93 sync
94 sleep 1
95 done
96 sudoscip-shell system . . sudo. losetup --detach "$@"
97 }
98
99 # Clear out all loopback devices we setup.
100 cleanup_loopbacksscip-shell shell coreboot 25.09+dfsg-1 cleanup_loopbacks().() {
101 local line
102 while read -r line; do
103 infoscip-shell shell coreboot 25.09+dfsg-1 info(). "Cleanup: detaching ${line}"
104 loopback_detachscip-shell shell coreboot 25.09+dfsg-1 loopback_detach(). "${line}" 2>/dev/nullscip-shell filesystem . . `/dev/null`.
105 done <"${TEMP_LOOP_LIST}"
106 rmscip-shell system . . rm. -f "${TEMP_LOOP_LIST}"
107 }
108
109 # Usage: lsbval path-to-lsb-file key
110 # Returns the value for the given lsb-release file variable.
111 lsbvalscip-shell shell coreboot 25.09+dfsg-1 lsbval().() {
112 local lsbfile="$1"
113 local key="$2"
114 grepscip-shell system . . grep. "^${key}=" "${lsbfile}" | sedscip-shell system . . sed. "s/^${key}=//"
115 }
116
117 # Usage: get_board_from_lsb_release rootfs
118 # Returns the exact board name from /etc/lsb-release. This may contain
119 # dashes or other characters not suitable for variable names. See the
120 # next function for that.
121 get_board_from_lsb_releasescip-shell shell coreboot 25.09+dfsg-1 get_board_from_lsb_release().() {
122 local rootfs="$1"
123 lsbvalscip-shell shell coreboot 25.09+dfsg-1 lsbval(). "${rootfs}/etc/lsb-release" CHROMEOS_RELEASE_BOARD
124 }
125
126 # Usage: get_boardvar_from_lsb_release rootfs
127 # Returns the board name from /etc/lsb-release in a mangled form that can
128 # be used in variable names. e.g. dashes are turned into underscores.
129 get_boardvar_from_lsb_releasescip-shell shell coreboot 25.09+dfsg-1 get_boardvar_from_lsb_release().() {
130 get_board_from_lsb_releasescip-shell shell coreboot 25.09+dfsg-1 get_board_from_lsb_release(). "$@" | sedscip-shell system . . sed. 's:[-]:_:g'
131 }
132
133 # Usage: restore_lsb_selinux lsb-file
134 # restore lsb-release security.selinux attribute
135 restore_lsb_selinuxscip-shell shell coreboot 25.09+dfsg-1 restore_lsb_selinux().() {
136 sudoscip-shell system . . sudo. setfattr -n security.selinux -v "u:object_r:cros_conf_file:s0" "$1"
137 }
138
139 # Extracts a firmware updater bundle (for firmware image binaries) file
140 # (generated by src/platform/firmware/pack_firmware.sh).
141 # Args: INPUT_FILE OUTPUT_DIR
142 extract_firmware_bundlescip-shell shell coreboot 25.09+dfsg-1 extract_firmware_bundle().() {
143 local input="$(readlinkscip-shell system . . readlink. -f "$1")"
144 local output_dir="$2"
145 if [[ ! -s "${input}" ]]; then
146 return 1
147 elif grepscip-shell system . . grep. -q '^##CUTHERE##' "${input}"; then
148 # Bundle supports self-extraction (--unpack, or --sb_extract)
149 "${input}" --unpack "${output_dir}" ||
150 "${input}" --sb_extract "${output_dir}" ||
151 die "Extracting firmware autoupdate (--unpack) failed."
152 else
153 # Legacy bundle - try uudecode.
154 uudecode -o - "${input}" | tarscip-shell system . . tar. -C "${output_dir}" -zxf - 2>/dev/nullscip-shell filesystem . . `/dev/null`. ||
155 die "Extracting firmware autoupdate failed."
156 fi
157 }
158
159 # This will override the trap set in common_minmal.sh
160 trap "cleanup" INT TERM EXIT