blob: f16b957539a0ad2858c3ef2fe2034f756b985ebd [file] [log] [blame]
Michael Schwartza0d9bd52017-11-02 10:19:42 -07001#!/bin/bash -ex
2usage () {
Justin Yun56cb76a2019-10-10 11:59:51 +09003 echo "Create a Mixed Build archive with the given system and device archives."
4 echo
5 echo "Usage: $0 [-v <vendor_version>] [-m <modify_system_image_path>]"
6 echo " [-t <prebuilt_otatools_path>] [-p <override_vbmeta_image_path>]"
7 echo " [-b <override_boot_image_path>]"
Justin Yun14e23422019-10-10 12:04:26 +09008 echo " [-s] system_build_dir device_build_dir out_dir"
Justin Yun56cb76a2019-10-10 11:59:51 +09009 echo
10 echo "Options -v, -m, -t, -p, -b, -s must precede positional arguments."
11 echo
12 echo "vendor_version is the version of the vendor image when Keymaster v3"
13 echo " related modifications to the system image is necessary. Optional."
14 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"
16 echo " system image, needed for Keymaster v3. Optional."
17 echo "prebuilt_otatools_path is the path to otatools.zip file that has all"
Justin Yun14e23422019-10-10 12:04:26 +090018 echo " required host binaries to modify system image. It also must include"
19 echo " VINTF check tool to verify the compatibility of the given images."
20 echo " Optional."
Justin Yun56cb76a2019-10-10 11:59:51 +090021 echo "override_vbmeta_image_path is the path to a vbmeta.img to use"
22 echo " to override the existing vbmeta.img of device. Optional."
23 echo "override_boot_image_path is the path to a boot imgage to use to"
24 echo " override the existing boot.img of device. Optional."
25 echo "-s is used to fetch and flash both product.img and system.img from"
26 echo " the system_build_dir for devices with a product partition."
27 echo " product.img will be removed if system_build_dir does not have"
28 echo " product.img when -s option is declared."
29 echo " By default, only system.img is flashed to the target device for"
30 echo " independent system update. No parameter required. Optional"
31 echo "system_build_dir is the path to the system build"
32 echo " eg. aosp_arm64_ab-userdebug."
33 echo "device_build_dir is the path to the device build"
34 echo " eg. sailfish-user."
35 echo "out_dir is the path to where the new build will be placed."
Michael Schwartza0d9bd52017-11-02 10:19:42 -070036}
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 () {
Justin Yun56cb76a2019-10-10 11:59:51 +090043 echo "ERROR: $1" >&2
44 usage
45 exit 1
Michael Schwartza0d9bd52017-11-02 10:19:42 -070046}
47
48cleanup_and_exit () {
Justin Yun56cb76a2019-10-10 11:59:51 +090049 readonly result="$?"
50 rm -rf "$TEMP_DIR"
51 exit "$result"
Michael Schwartza0d9bd52017-11-02 10:19:42 -070052}
53
54trap cleanup_and_exit EXIT
55
Hyunwoo Ko22da1572019-05-16 18:01:35 +090056while getopts :v:m:p:b:t:s opt; do
Justin Yun56cb76a2019-10-10 11:59:51 +090057 case "$opt" in
58 v)
59 readonly VENDOR_VERSION="$OPTARG"
60 ;;
61 m)
62 readonly MODIFY_SYSTEM_SCRIPT="$OPTARG"
63 ;;
64 p)
65 readonly OVERRIDE_VBMETA_IMAGE_PATH="$OPTARG"
66 ;;
67 b)
68 readonly OVERRIDE_BOOT_IMAGE_PATH="$OPTARG"
69 ;;
70 t)
71 readonly OTATOOLS_ZIP="$OPTARG"
72 ;;
73 s)
74 readonly INCLUDE_PRODUCT=true
75 ;;
76 \?)
77 exit_badparam "Invalid options: -"$OPTARG""
78 ;;
79 :)
80 exit_badparam "Option -"$OPTARG" requires an argument."
81 ;;
82 esac
Jae Shin3af42012018-05-08 17:56:34 +090083done
84
Justin Yun56cb76a2019-10-10 11:59:51 +090085if [[ -z "${VENDOR_VERSION+x}" && ! -z "${MODIFY_SYSTEM_SCRIPT+x}" ]] || \
86 [[ ! -z "${VENDOR_VERSION+x}" && -z "${MODIFY_SYSTEM_SCRIPT+x}" ]]; then
87 exit_badparam "Options -v and -m must be set together."
Jae Shin3af42012018-05-08 17:56:34 +090088fi
89
90shift "$((OPTIND-1))"
91
Jae Shin3af42012018-05-08 17:56:34 +090092if [[ $# -lt 3 ]]; then
Justin Yun56cb76a2019-10-10 11:59:51 +090093 exit_badparam "Unexpected number of arguments"
Jae Shin3af42012018-05-08 17:56:34 +090094fi
95
Jae Shin341a55f2018-06-04 16:55:26 +090096readonly SYSTEM_DIR="$1"
Jae Shin3af42012018-05-08 17:56:34 +090097readonly DEVICE_DIR="$2"
98readonly DIST_DIR="$3"
Jae Shin3af42012018-05-08 17:56:34 +090099readonly 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
Justin Yun56cb76a2019-10-10 11:59:51 +0900103 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
Justin Yun56cb76a2019-10-10 11:59:51 +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
Justin Yun56cb76a2019-10-10 11:59:51 +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
Justin Yun56cb76a2019-10-10 11:59:51 +0900117 exit_badparam "Script not found: "$MODIFY_SYSTEM_SCRIPT""
Jae Shinf418b9b2018-08-09 12:57:20 +0900118fi
119
120if [[ ! -z "${OVERRIDE_VBMETA_IMAGE_PATH+x}" && ! -f "$OVERRIDE_VBMETA_IMAGE_PATH" ]]; then
Justin Yun56cb76a2019-10-10 11:59:51 +0900121 exit_badparam "Specified vbmeta.img not found: "$OVERRIDE_VBMETA_IMAGE_PATH""
Jae Shinf418b9b2018-08-09 12:57:20 +0900122fi
123
Hyunwoo Ko22da1572019-05-16 18:01:35 +0900124if [[ ! -z "${OVERRIDE_BOOT_IMAGE_PATH+x}" && ! -f "$OVERRIDE_BOOT_IMAGE_PATH" ]]; then
Justin Yun56cb76a2019-10-10 11:59:51 +0900125 exit_badparam "Specified boot image not found: "$OVERRIDE_BOOT_IMAGE_PATH""
Hyunwoo Ko22da1572019-05-16 18:01:35 +0900126fi
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
Justin Yun14e23422019-10-10 12:04:26 +0900137declare -a EXTRACT_SYSTEM_FILE_LIST
138EXTRACT_SYSTEM_FILE_LIST=(
Justin Yun56cb76a2019-10-10 11:59:51 +0900139 IMAGES/system.img \
140 IMAGES/vbmeta.img \
Justin Yun56cb76a2019-10-10 11:59:51 +0900141 "$SYSTEM_BUILD_PROP" \
Justin Yun07a77c82018-11-20 13:12:12 +0900142)
143
Justin Yun14e23422019-10-10 12:04:26 +0900144declare -a EXTRACT_VINTF_SYSTEM_FILE_LIST
145EXTRACT_VINTF_SYSTEM_FILE_LIST=(
146 "$SYSTEM_BUILD_PROP" \
147)
Justin Yun07a77c82018-11-20 13:12:12 +0900148
Justin Yun14e23422019-10-10 12:04:26 +0900149declare -a EXTRACT_DEVICE_FILE_LIST
150EXTRACT_DEVICE_FILE_LIST=(
151 */build.prop \
152 META/* \
153)
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700154
Justin Yun14e23422019-10-10 12:04:26 +0900155declare -A SYSTEM_SEARCH_PATH
156SYSTEM_SEARCH_PATH=( \
157 [/system]="SYSTEM" \
158 [/product]="PRODUCT SYSTEM/product" \
159 [/system_ext]="SYSTEM_EXT SYSTEM/system_ext" \
160)
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700161
Justin Yun14e23422019-10-10 12:04:26 +0900162declare -A DEVICE_SEARCH_PATH
163# Mixed build will not have /vendor to SYSTEM/vendor case
164DEVICE_SEARCH_PATH=( \
165 [/vendor]="VENDOR" \
166 [/odm]="ODM VENDOR/odm" \
167)
168
169###
170# Uncompress otatools.zip and get vintf file list.
171if [[ ! -f "$OTATOOLS_ZIP" ]]; then
172 echo "WARNING: otatools.zip is missing. Add \"-t otatools.zip\" to enable checkvintf"
173else
174 readonly OTATOOLS_AVAILABLE=true
Justin Yun56cb76a2019-10-10 11:59:51 +0900175 # Uncompress otatools
176 mkdir -p "$OTATOOLS_DIR"
177 unzip "$OTATOOLS_ZIP" bin/* lib64/* -d "$OTATOOLS_DIR"
178 # Set paths for using prebuilt host binaries.
179 export PATH="$OTATOOLS_DIR"/bin:"$PATH"
180 export LD_LIBRARY_PATH="$OTATOOLS_DIR"/lib64:"$LD_LIBRARY_PATH"
Justin Yun14e23422019-10-10 12:04:26 +0900181
182 # Add vintf file to extract file list
183 declare -a VINTF_DUMP_FILE_LIST
184 VINTF_DUMP_FILE_LIST=( "$(checkvintf --dump-file-list)" )
185
186 for vintf_file_list in ${VINTF_DUMP_FILE_LIST[*]}; do
187 if [[ "$vintf_file_list" == */ ]]; then
188 vintf_file_list="$vintf_file_list"\*
189 # Create system vintf file list for system target files archive
190 for system_dir in "${!SYSTEM_SEARCH_PATH[@]}"; do
191 if [[ "$vintf_file_list" == "$system_dir"/* ]]; then
192 for search_dir in ${SYSTEM_SEARCH_PATH["$system_dir"]}; do
193 search_file=${vintf_file_list/$system_dir/$search_dir}
194 unzip -l "$SYSTEM_TARGET_FILES_ARCHIVE" "$search_file" > /dev/null && \
195 EXTRACT_VINTF_SYSTEM_FILE_LIST+=( "$search_file" )
196 done
197 break
198 fi
199 done
200 # Create device vintf file list for device target files archive
201 for device_dir in "${!DEVICE_SEARCH_PATH[@]}"; do
202 if [[ "$vintf_file_list" == "$device_dir"/* ]]; then
203 for search_dir in ${DEVICE_SEARCH_PATH["$device_dir"]}; do
204 search_file=${vintf_file_list/$device_dir/$search_dir}
205 unzip -l "$DEVICE_TARGET_FILES_ARCHIVE" "$search_file" > /dev/null && \
206 EXTRACT_DEVICE_FILE_LIST+=( "$search_file" )
207 done
208 break
209 fi
210 done
211 fi
212 done
Justin Yun40da6782018-10-05 14:54:17 +0900213fi
214
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700215###
Justin Yun14e23422019-10-10 12:04:26 +0900216# Uncompress the system archives.
217if [[ "$INCLUDE_PRODUCT" == true ]]; then
218 unzip -l "$SYSTEM_TARGET_FILES_ARCHIVE" | grep -q IMAGES/product.img &&
219 EXTRACT_SYSTEM_FILE_LIST+=(IMAGES/product.img)
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700220fi
221
Justin Yun14e23422019-10-10 12:04:26 +0900222mkdir -p "$SYSTEM_ARTIFACTS_DIR"
223# Get system images.
224unzip "$SYSTEM_TARGET_FILES_ARCHIVE" "${EXTRACT_SYSTEM_FILE_LIST[@]}" \
225 -d "$SYSTEM_ARTIFACTS_DIR"
226
227###
228# Uncompress the device archives.
229mkdir -p "$DEVICE_IMAGES_DIR"
230# Get device images.
231unzip "$DEVICE_ARCHIVE" -d "$DEVICE_IMAGES_DIR"
232# Get the device meta data.
233unzip "$DEVICE_TARGET_FILES_ARCHIVE" "${EXTRACT_DEVICE_FILE_LIST[@]}" \
234 -d "$DEVICE_ARTIFACTS_DIR"
235
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700236###
Jae Shin3af42012018-05-08 17:56:34 +0900237# Modify system.img if vendor version is provided.
238if [[ ! -z "${VENDOR_VERSION+x}" ]]; then
Justin Yun56cb76a2019-10-10 11:59:51 +0900239 # Create copy of system target files package that can be modified
240 # since the original $SYSTEM_TARGET_FILES_ARCHIVE is a symlink to
241 # prebuilt files in cache
242 cp "$SYSTEM_TARGET_FILES_ARCHIVE" "$TEMP_DIR"
243 readonly COPY_SYSTEM_TARGET_FILES_ARCHIVE="$TEMP_DIR"/"$(basename "$SYSTEM_TARGET_FILES_ARCHIVE")"
Jae Shin3af42012018-05-08 17:56:34 +0900244
Justin Yun56cb76a2019-10-10 11:59:51 +0900245 # Check compatibility of security patch level
246 readonly SYSTEM_SPL=$(sed -n -r "s/^"$SPL_PROPERTY_NAME"=(.*)$/\1/p" "$SYSTEM_ARTIFACTS_DIR"/"$SYSTEM_BUILD_PROP")
247 readonly VENDOR_SPL=$(sed -n -r "s/^"$SPL_PROPERTY_NAME"=(.*)$/\1/p" "$DEVICE_ARTIFACTS_DIR"/"$SYSTEM_BUILD_PROP")
248 declare -a args
249 args=(-v "$VENDOR_VERSION" "$COPY_SYSTEM_TARGET_FILES_ARCHIVE")
250 if [[ "$SYSTEM_SPL" != "$VENDOR_SPL" ]]; then
251 echo "Security patch level mismatch detected..."
252 echo " SPL of system: "$SYSTEM_SPL""
253 echo " SPL of vendor: "$VENDOR_SPL""
254 args+=("$VENDOR_SPL")
255 fi
256 "$MODIFY_SYSTEM_SCRIPT" "${args[@]}"
257 # Replace system.img with newly modified system.img
258 unzip -o "$COPY_SYSTEM_TARGET_FILES_ARCHIVE" IMAGES/system.img -d "$SYSTEM_ARTIFACTS_DIR"
Jae Shin3af42012018-05-08 17:56:34 +0900259fi
260
Justin Yun14e23422019-10-10 12:04:26 +0900261# Check vintf
262if [[ "$OTATOOLS_AVAILABLE" == true ]]; then
263 # Overwrite VINTF system matrix to device artifacts dir
264 unzip -o "$SYSTEM_TARGET_FILES_ARCHIVE" "${EXTRACT_VINTF_SYSTEM_FILE_LIST[@]}" \
265 -d "$DEVICE_ARTIFACTS_DIR"
266 check_target_files_vintf "$DEVICE_ARTIFACTS_DIR"
267fi
268
Jae Shin3af42012018-05-08 17:56:34 +0900269###
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700270# Overwrite artifacts in the device archive to create the Mixed Build artifacts.
Jae Shin341a55f2018-06-04 16:55:26 +0900271cp "$SYSTEM_IMAGES_DIR"/system.img "$DEVICE_IMAGES_DIR"/
Justin Yun07a77c82018-11-20 13:12:12 +0900272if [[ "$INCLUDE_PRODUCT" == true ]]; then
Justin Yun74b00cb2019-01-04 14:45:08 +0900273 if [[ -f "$SYSTEM_IMAGES_DIR"/product.img ]]; then
274 cp "$SYSTEM_IMAGES_DIR"/product.img "$DEVICE_IMAGES_DIR"/
275 else
276 rm -f "$DEVICE_IMAGES_DIR"/product.img
277 # Removed product partition from required partition list
278 sed -i "/partition-exists=product$/d" "$DEVICE_IMAGES_DIR"/android-info.txt
279 fi
Justin Yun07a77c82018-11-20 13:12:12 +0900280fi
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700281
282# Only override vbmeta if it is already present since fastboot update will try
283# to flash whatever is in the archive.
284if [[ -f "$DEVICE_IMAGES_DIR"/vbmeta.img ]]; then
Justin Yun56cb76a2019-10-10 11:59:51 +0900285 readonly VBMETA_IMAGE_PATH="${OVERRIDE_VBMETA_IMAGE_PATH:-"$SYSTEM_IMAGES_DIR"/vbmeta.img}"
286 cp "$VBMETA_IMAGE_PATH" "$DEVICE_IMAGES_DIR"/
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700287fi
288
Hyunwoo Ko22da1572019-05-16 18:01:35 +0900289# Override boot.img with the provided boot image file since fastboot update cmd
290# will try to flash boot.img in the archive.
291if [[ ! -z "${OVERRIDE_BOOT_IMAGE_PATH+x}" && -f "$DEVICE_IMAGES_DIR"/boot.img ]]; then
Justin Yun56cb76a2019-10-10 11:59:51 +0900292 cp "$OVERRIDE_BOOT_IMAGE_PATH" "$DEVICE_IMAGES_DIR"/boot.img
Hyunwoo Ko22da1572019-05-16 18:01:35 +0900293fi
294
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700295###
296# Create the Mixed Build archive.
297(
Justin Yun56cb76a2019-10-10 11:59:51 +0900298 cd "$DEVICE_IMAGES_DIR"
299 zip -r mixed.zip ./*
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700300)
301
302###
303# Archive the artifacts.
304if [ -n "$DIST_DIR" ]; then
Justin Yun56cb76a2019-10-10 11:59:51 +0900305 mkdir -p "$DIST_DIR" || true
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700306fi
307# Archive all the device artifacts.
Michael Schwartz56e33882018-05-24 20:57:29 -0700308rsync --archive --verbose --copy-links --exclude='logs' \
309 "$DEVICE_DIR"/* "$DIST_DIR"
Michael Schwartza0d9bd52017-11-02 10:19:42 -0700310# Overwrite the image archive with the Mixed Build archive.
311OUT_ARCHIVE="$DIST_DIR"/"$(basename $DEVICE_ARCHIVE)"
312cp "$DEVICE_IMAGES_DIR"/mixed.zip "$OUT_ARCHIVE"
Justin Yun247e95a2019-01-07 10:36:49 +0900313# Overwrite android-info.txt with the updated one.
314cp "$DEVICE_IMAGES_DIR"/android-info.txt "$DIST_DIR"/