blob: f2fdafdee06fdbe87c71cf3e776d88ebbe18e236 [file] [log] [blame]
Michael Schwartza0d9bd52017-11-02 10:19:42 -07001#!/bin/bash -ex
2usage () {
Jae Shin341a55f2018-06-04 16:55:26 +09003 echo "Create a Mixed Build archive with the given system and device archives."
Michael Schwartza0d9bd52017-11-02 10:19:42 -07004 echo
Justin Yunec96edb2018-10-26 12:21:24 +09005 echo "Usage: $0 [-v <vendor_version>] [-m <modify_system_image_path>]"
6 echo " [-t <prebuilt_otatools_path>] [-p <override_vbmeta_image_path>]"
Hyunwoo Koaabbd912019-05-16 18:01:35 +09007 echo " [-b <override_boot_image_path>]"
Justin Yun07a77c82018-11-20 13:12:12 +09008 echo " [-s] system_build_dir device_build_dir out_dir [check_tool]"
Michael Schwartza0d9bd52017-11-02 10:19:42 -07009 echo
Hyunwoo Koaabbd912019-05-16 18:01:35 +090010 echo "Options -v, -m, -t, -p, -b, -s must precede positional arguments."
Jae Shin3af42012018-05-08 17:56:34 +090011 echo
Jae Shind5fd24f2018-08-14 14:20:12 +090012 echo "vendor_version is the version of the vendor image when Keymaster v3"
13 echo " related modifications to the system image is necessary. Optional."
Jae Shin3af42012018-05-08 17:56:34 +090014 echo " eg. 8.1.0 for a mixed build of GSI and O-MR1 vendor image."
15 echo "modify_system_image_path is the path to the script that modifies the"
Jae Shind5fd24f2018-08-14 14:20:12 +090016 echo " system image, needed for Keymaster v3. Optional."
Justin Yunec96edb2018-10-26 12:21:24 +090017 echo "prebuilt_otatools_path is the path to otatools.zip file that has all"
18 echo " required host binaries to modify system image. Optional."
Jae Shin341a55f2018-06-04 16:55:26 +090019 echo "override_vbmeta_image_path is the path to a vbmeta.img to use"
20 echo " to override the existing vbmeta.img of device. Optional."
Hyunwoo Koaabbd912019-05-16 18:01:35 +090021 echo "override_boot_image_path is the path to a boot imgage to use to"
22 echo " override the existing boot.img of device. Optional."
Justin Yun07a77c82018-11-20 13:12:12 +090023 echo "-s is used to fetch and flash both product.img and system.img from"
24 echo " the system_build_dir for devices with a product partition."
Justin Yun74b00cb2019-01-04 14:45:08 +090025 echo " product.img will be removed if system_build_dir does not have"
26 echo " product.img when -s option is declared."
Justin Yun07a77c82018-11-20 13:12:12 +090027 echo " By default, only system.img is flashed to the target device for"
28 echo " independent system update. No parameter required. Optional"
Jae Shin341a55f2018-06-04 16:55:26 +090029 echo "system_build_dir is the path to the system build"
Michael Schwartza0d9bd52017-11-02 10:19:42 -070030 echo " eg. aosp_arm64_ab-userdebug."
31 echo "device_build_dir is the path to the device build"
32 echo " eg. sailfish-user."
33 echo "out_dir is the path to where the new build will be placed."
34 echo "check_tool is the path to the checkvintf executable that will be"
35 echo " used to verify the compatibility of the given images. Optional."
36}
37
Michael Schwartza0d9bd52017-11-02 10:19:42 -070038# Print error message and exit.
39# Usage: exit_badparam message
40#
41# message is a string to be displayed before exit.
42exit_badparam () {
43 echo "ERROR: $1" >&2
44 usage
45 exit 1
46}
47
48cleanup_and_exit () {
Michael Schwartz66fe2832017-12-22 11:48:14 -080049 readonly result="$?"
Michael Schwartza0d9bd52017-11-02 10:19:42 -070050 rm -rf "$TEMP_DIR"
Michael Schwartz66fe2832017-12-22 11:48:14 -080051 exit "$result"
Michael Schwartza0d9bd52017-11-02 10:19:42 -070052}
53
54trap cleanup_and_exit EXIT
55
Hyunwoo Koaabbd912019-05-16 18:01:35 +090056while getopts :v:m:p:b:t:s opt; do
Jae Shin3af42012018-05-08 17:56:34 +090057 case "$opt" in
58 v)
59 readonly VENDOR_VERSION="$OPTARG"
60 ;;
61 m)
62 readonly MODIFY_SYSTEM_SCRIPT="$OPTARG"
63 ;;
Jae Shin341a55f2018-06-04 16:55:26 +090064 p)
65 readonly OVERRIDE_VBMETA_IMAGE_PATH="$OPTARG"
66 ;;
Hyunwoo Koaabbd912019-05-16 18:01:35 +090067 b)
68 readonly OVERRIDE_BOOT_IMAGE_PATH="$OPTARG"
69 ;;
Justin Yun40da6782018-10-05 14:54:17 +090070 t)
71 readonly OTATOOLS_ZIP="$OPTARG"
72 ;;
Justin Yun07a77c82018-11-20 13:12:12 +090073 s)
74 readonly INCLUDE_PRODUCT=true
75 ;;
Jae Shin3af42012018-05-08 17:56:34 +090076 \?)
77 exit_badparam "Invalid options: -"$OPTARG""
78 ;;
79 :)
80 exit_badparam "Option -"$OPTARG" requires an argument."
81 ;;
82 esac
83done
84
Jae Shin341a55f2018-06-04 16:55:26 +090085if [[ -z "${VENDOR_VERSION+x}" && ! -z "${MODIFY_SYSTEM_SCRIPT+x}" ]] || [[ ! -z "${VENDOR_VERSION+x}" && -z "${MODIFY_SYSTEM_SCRIPT+x}" ]]; then
Jae Shin3af42012018-05-08 17:56:34 +090086 exit_badparam "Options -v and -m must be set together."
87fi
88
89shift "$((OPTIND-1))"
90
Jae Shin3af42012018-05-08 17:56:34 +090091if [[ $# -lt 3 ]]; then
92 exit_badparam "Unexpected number of arguments"
93fi
94
Jae Shin341a55f2018-06-04 16:55:26 +090095readonly SYSTEM_DIR="$1"
Jae Shin3af42012018-05-08 17:56:34 +090096readonly DEVICE_DIR="$2"
97readonly DIST_DIR="$3"
98readonly CHECK_TOOL="$4"
99readonly TEMP_DIR="$(mktemp -d /tmp/"$(basename $0)"_XXXXXXXX)"
100
Justin Yun83631d62018-10-05 14:24:12 +0900101readonly SYSTEM_TARGET_FILES_ARCHIVE="$(find "$SYSTEM_DIR" -name "*-target_files-*.zip" -print)"
Jae Shin341a55f2018-06-04 16:55:26 +0900102if [[ ! -f "$SYSTEM_TARGET_FILES_ARCHIVE" ]]; then
103 exit_badparam "Could not find system target files archive in $SYSTEM_DIR."
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700104fi
105
Justin Yun83631d62018-10-05 14:24:12 +0900106readonly DEVICE_ARCHIVE="$(find "$DEVICE_DIR" -name "*-img-*.zip" -print)"
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700107if [[ ! -f "$DEVICE_ARCHIVE" ]]; then
Jae Shin3af42012018-05-08 17:56:34 +0900108 exit_badparam "Could not find device img archive in $DEVICE_DIR."
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700109fi
110
Justin Yun83631d62018-10-05 14:24:12 +0900111readonly DEVICE_TARGET_FILES_ARCHIVE="$(find "$DEVICE_DIR" -name "*-target_files-*.zip" -print)"
Paul Trautrimd2d8ac92018-06-29 16:19:10 +0900112if [[ ! -f "$DEVICE_TARGET_FILES_ARCHIVE" ]]; then
Jae Shin3af42012018-05-08 17:56:34 +0900113 exit_badparam "Could not find device target_files archive in $DEVICE_DIR."
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700114fi
115
Jae Shinf418b9b2018-08-09 12:57:20 +0900116if [[ ! -z "${MODIFY_SYSTEM_SCRIPT+x}" && ! -f "$MODIFY_SYSTEM_SCRIPT" ]]; then
117 exit_badparam "Script not found: "$MODIFY_SYSTEM_SCRIPT""
118fi
119
120if [[ ! -z "${OVERRIDE_VBMETA_IMAGE_PATH+x}" && ! -f "$OVERRIDE_VBMETA_IMAGE_PATH" ]]; then
121 exit_badparam "Specified vbmeta.img not found: "$OVERRIDE_VBMETA_IMAGE_PATH""
122fi
123
Hyunwoo Koaabbd912019-05-16 18:01:35 +0900124if [[ ! -z "${OVERRIDE_BOOT_IMAGE_PATH+x}" && ! -f "$OVERRIDE_BOOT_IMAGE_PATH" ]]; then
125 exit_badparam "Specified boot image not found: "$OVERRIDE_BOOT_IMAGE_PATH""
126fi
127
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700128readonly DEVICE_ARTIFACTS_DIR="$TEMP_DIR"/device_archive_artifacts
129readonly DEVICE_IMAGES_DIR="$DEVICE_ARTIFACTS_DIR"/IMAGES
Jae Shin341a55f2018-06-04 16:55:26 +0900130readonly SYSTEM_ARTIFACTS_DIR="$TEMP_DIR"/system_artifacts
131readonly SYSTEM_IMAGES_DIR="$SYSTEM_ARTIFACTS_DIR"/IMAGES
Justin Yun40da6782018-10-05 14:54:17 +0900132readonly OTATOOLS_DIR="$TEMP_DIR"/otatools
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700133
Jae Shin3af42012018-05-08 17:56:34 +0900134readonly SPL_PROPERTY_NAME="ro.build.version.security_patch"
135readonly SYSTEM_BUILD_PROP="SYSTEM/build.prop"
136
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700137###
138# Uncompress the archives.
Justin Yun07a77c82018-11-20 13:12:12 +0900139declare -a EXTRACT_FILE_LIST
140EXTRACT_FILE_LIST=(
141 IMAGES/system.img \
142 IMAGES/vbmeta.img \
143 META/system_matrix.xml \
144 META/system_manifest.xml \
145 "$SYSTEM_BUILD_PROP" \
146)
147
148if [[ "$INCLUDE_PRODUCT" == true ]]; then
Justin Yun74b00cb2019-01-04 14:45:08 +0900149 unzip -l "$SYSTEM_TARGET_FILES_ARCHIVE" | grep -q IMAGES/product.img &&
Justin Yun07a77c82018-11-20 13:12:12 +0900150 EXTRACT_FILE_LIST+=(IMAGES/product.img)
151fi
152
Jae Shin341a55f2018-06-04 16:55:26 +0900153mkdir -p "$SYSTEM_ARTIFACTS_DIR"
154# Get the system images and meta data.
Justin Yun07a77c82018-11-20 13:12:12 +0900155# ${EXTRACT_FILE_LIST[*]} cannot be quoted to list each file for unzipping
156unzip "$SYSTEM_TARGET_FILES_ARCHIVE" ${EXTRACT_FILE_LIST[*]} -d "$SYSTEM_ARTIFACTS_DIR"
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700157
158mkdir -p "$DEVICE_IMAGES_DIR"
159# Get the device images.
160unzip "$DEVICE_ARCHIVE" -d "$DEVICE_IMAGES_DIR"
161# Get the device meta data.
162unzip "$DEVICE_TARGET_FILES_ARCHIVE" \
Jae Shin3af42012018-05-08 17:56:34 +0900163 META/vendor_matrix.xml META/vendor_manifest.xml "$SYSTEM_BUILD_PROP" \
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700164 -d "$DEVICE_ARTIFACTS_DIR"
165
Justin Yun40da6782018-10-05 14:54:17 +0900166if [[ -f "$OTATOOLS_ZIP" ]]; then
167 # Uncompress otatools
168 mkdir -p "$OTATOOLS_DIR"
Justin Yunec96edb2018-10-26 12:21:24 +0900169 unzip "$OTATOOLS_ZIP" bin/* lib64/* -d "$OTATOOLS_DIR"
Justin Yun40da6782018-10-05 14:54:17 +0900170 # Set paths for using prebuilt host binaries.
171 export PATH="$OTATOOLS_DIR"/bin:"$PATH"
172 export LD_LIBRARY_PATH="$OTATOOLS_DIR"/lib64:"$LD_LIBRARY_PATH"
173fi
174
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700175###
Jae Shin341a55f2018-06-04 16:55:26 +0900176# Check compatibility between the system and device.
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700177if [[ -f "$CHECK_TOOL" ]]; then
Jae Shin58bd7712018-06-07 10:25:57 +0900178 chmod 755 "$CHECK_TOOL"
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700179 "$CHECK_TOOL" \
180 "$DEVICE_ARTIFACTS_DIR"/META/vendor_manifest.xml \
Jae Shin341a55f2018-06-04 16:55:26 +0900181 "$SYSTEM_ARTIFACTS_DIR"/META/system_matrix.xml
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700182 "$CHECK_TOOL" \
Jae Shin341a55f2018-06-04 16:55:26 +0900183 "$SYSTEM_ARTIFACTS_DIR"/META/system_manifest.xml \
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700184 "$DEVICE_ARTIFACTS_DIR"/META/vendor_matrix.xml
185fi
186
187###
Jae Shin3af42012018-05-08 17:56:34 +0900188# Modify system.img if vendor version is provided.
189if [[ ! -z "${VENDOR_VERSION+x}" ]]; then
Jae Shin341a55f2018-06-04 16:55:26 +0900190 # Create copy of system target files package that can be modified
191 # since the original $SYSTEM_TARGET_FILES_ARCHIVE is a symlink to
Jae Shin3af42012018-05-08 17:56:34 +0900192 # prebuilt files in cache
Jae Shin341a55f2018-06-04 16:55:26 +0900193 cp "$SYSTEM_TARGET_FILES_ARCHIVE" "$TEMP_DIR"
194 readonly COPY_SYSTEM_TARGET_FILES_ARCHIVE="$TEMP_DIR"/"$(basename "$SYSTEM_TARGET_FILES_ARCHIVE")"
Jae Shin3af42012018-05-08 17:56:34 +0900195
196 # Check compatibility of security patch level
Jae Shin341a55f2018-06-04 16:55:26 +0900197 readonly SYSTEM_SPL=$(sed -n -r "s/^"$SPL_PROPERTY_NAME"=(.*)$/\1/p" "$SYSTEM_ARTIFACTS_DIR"/"$SYSTEM_BUILD_PROP")
Jae Shin3af42012018-05-08 17:56:34 +0900198 readonly VENDOR_SPL=$(sed -n -r "s/^"$SPL_PROPERTY_NAME"=(.*)$/\1/p" "$DEVICE_ARTIFACTS_DIR"/"$SYSTEM_BUILD_PROP")
199 declare -a args
Jae Shin341a55f2018-06-04 16:55:26 +0900200 args=(-v "$VENDOR_VERSION" "$COPY_SYSTEM_TARGET_FILES_ARCHIVE")
Jae Shin3af42012018-05-08 17:56:34 +0900201 if [[ "$SYSTEM_SPL" != "$VENDOR_SPL" ]]; then
202 echo "Security patch level mismatch detected..."
203 echo " SPL of system: "$SYSTEM_SPL""
204 echo " SPL of vendor: "$VENDOR_SPL""
205 args+=("$VENDOR_SPL")
206 fi
207 "$MODIFY_SYSTEM_SCRIPT" "${args[@]}"
208 # Replace system.img with newly modified system.img
Jae Shin341a55f2018-06-04 16:55:26 +0900209 unzip -o "$COPY_SYSTEM_TARGET_FILES_ARCHIVE" IMAGES/system.img -d "$SYSTEM_ARTIFACTS_DIR"
Jae Shin3af42012018-05-08 17:56:34 +0900210fi
211
212###
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700213# Overwrite artifacts in the device archive to create the Mixed Build artifacts.
Jae Shin341a55f2018-06-04 16:55:26 +0900214cp "$SYSTEM_IMAGES_DIR"/system.img "$DEVICE_IMAGES_DIR"/
Justin Yun07a77c82018-11-20 13:12:12 +0900215if [[ "$INCLUDE_PRODUCT" == true ]]; then
Justin Yun74b00cb2019-01-04 14:45:08 +0900216 if [[ -f "$SYSTEM_IMAGES_DIR"/product.img ]]; then
217 cp "$SYSTEM_IMAGES_DIR"/product.img "$DEVICE_IMAGES_DIR"/
218 else
219 rm -f "$DEVICE_IMAGES_DIR"/product.img
220 # Removed product partition from required partition list
221 sed -i "/partition-exists=product$/d" "$DEVICE_IMAGES_DIR"/android-info.txt
222 fi
Justin Yun07a77c82018-11-20 13:12:12 +0900223fi
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700224
225# Only override vbmeta if it is already present since fastboot update will try
226# to flash whatever is in the archive.
227if [[ -f "$DEVICE_IMAGES_DIR"/vbmeta.img ]]; then
Jae Shin341a55f2018-06-04 16:55:26 +0900228 readonly VBMETA_IMAGE_PATH="${OVERRIDE_VBMETA_IMAGE_PATH:-"$SYSTEM_IMAGES_DIR"/vbmeta.img}"
229 cp "$VBMETA_IMAGE_PATH" "$DEVICE_IMAGES_DIR"/
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700230fi
231
Hyunwoo Koaabbd912019-05-16 18:01:35 +0900232# Override boot.img with the provided boot image file since fastboot update cmd
233# will try to flash boot.img in the archive.
234if [[ ! -z "${OVERRIDE_BOOT_IMAGE_PATH+x}" && -f "$DEVICE_IMAGES_DIR"/boot.img ]]; then
235 cp "$OVERRIDE_BOOT_IMAGE_PATH" "$DEVICE_IMAGES_DIR"/boot.img
236fi
237
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700238###
239# Create the Mixed Build archive.
240(
241 cd "$DEVICE_IMAGES_DIR"
242 zip -r mixed.zip ./*
243)
244
245###
246# Archive the artifacts.
247if [ -n "$DIST_DIR" ]; then
248 mkdir -p "$DIST_DIR" || true
249fi
250# Archive all the device artifacts.
Michael Schwartz56e33882018-05-24 20:57:29 -0700251rsync --archive --verbose --copy-links --exclude='logs' \
252 "$DEVICE_DIR"/* "$DIST_DIR"
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700253# Overwrite the image archive with the Mixed Build archive.
254OUT_ARCHIVE="$DIST_DIR"/"$(basename $DEVICE_ARCHIVE)"
255cp "$DEVICE_IMAGES_DIR"/mixed.zip "$OUT_ARCHIVE"
Justin Yun247e95a2019-01-07 10:36:49 +0900256# Overwrite android-info.txt with the updated one.
257cp "$DEVICE_IMAGES_DIR"/android-info.txt "$DIST_DIR"/