1#!/bin/bash -ex
2usage () {
3  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>]"
8  echo "    [-s] system_build_dir device_build_dir out_dir"
9  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"
18  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."
21  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."
36}
37
38# 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 () {
49  readonly result="$?"
50  rm -rf "$TEMP_DIR"
51  exit "$result"
52}
53
54trap cleanup_and_exit EXIT
55
56while getopts :v:m:p:b:t:s opt; do
57  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
83done
84
85if [[ -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."
88fi
89
90shift "$((OPTIND-1))"
91
92if [[ $# -lt 3 ]]; then
93  exit_badparam "Unexpected number of arguments"
94fi
95
96readonly SYSTEM_DIR="$1"
97readonly DEVICE_DIR="$2"
98readonly DIST_DIR="$3"
99readonly TEMP_DIR="$(mktemp -d /tmp/"$(basename $0)"_XXXXXXXX)"
100
101readonly SYSTEM_TARGET_FILES_ARCHIVE="$(find "$SYSTEM_DIR" -name "*-target_files-*.zip" -print)"
102if [[ ! -f "$SYSTEM_TARGET_FILES_ARCHIVE" ]]; then
103  exit_badparam "Could not find system target files archive in $SYSTEM_DIR."
104fi
105
106readonly DEVICE_ARCHIVE="$(find "$DEVICE_DIR" -name "*-img-*.zip" -print)"
107if [[ ! -f "$DEVICE_ARCHIVE" ]]; then
108  exit_badparam "Could not find device img archive in $DEVICE_DIR."
109fi
110
111readonly DEVICE_TARGET_FILES_ARCHIVE="$(find "$DEVICE_DIR" -name "*-target_files-*.zip" -print)"
112if [[ ! -f "$DEVICE_TARGET_FILES_ARCHIVE" ]]; then
113  exit_badparam "Could not find device target_files archive in $DEVICE_DIR."
114fi
115
116if [[ ! -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
124if [[ ! -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
128readonly DEVICE_ARTIFACTS_DIR="$TEMP_DIR"/device_archive_artifacts
129readonly DEVICE_IMAGES_DIR="$DEVICE_ARTIFACTS_DIR"/IMAGES
130readonly SYSTEM_ARTIFACTS_DIR="$TEMP_DIR"/system_artifacts
131readonly SYSTEM_IMAGES_DIR="$SYSTEM_ARTIFACTS_DIR"/IMAGES
132readonly OTATOOLS_DIR="$TEMP_DIR"/otatools
133
134readonly SPL_PROPERTY_NAME="ro.build.version.security_patch"
135readonly SYSTEM_BUILD_PROP="SYSTEM/build.prop"
136
137declare -a EXTRACT_SYSTEM_FILE_LIST
138EXTRACT_SYSTEM_FILE_LIST=(
139  IMAGES/system.img \
140  IMAGES/vbmeta.img \
141  "$SYSTEM_BUILD_PROP" \
142)
143
144declare -a EXTRACT_VINTF_SYSTEM_FILE_LIST
145EXTRACT_VINTF_SYSTEM_FILE_LIST=(
146  "$SYSTEM_BUILD_PROP" \
147)
148
149declare -a EXTRACT_DEVICE_FILE_LIST
150EXTRACT_DEVICE_FILE_LIST=(
151  */build.prop \
152  META/* \
153)
154
155declare -A SYSTEM_SEARCH_PATH
156SYSTEM_SEARCH_PATH=( \
157  [/system]="SYSTEM" \
158  [/product]="PRODUCT SYSTEM/product" \
159  [/system_ext]="SYSTEM_EXT SYSTEM/system_ext" \
160)
161
162declare -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
175  # 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"
181
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
213fi
214
215###
216# 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)
220fi
221
222mkdir -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
236###
237# Modify system.img if vendor version is provided.
238if [[ ! -z "${VENDOR_VERSION+x}" ]]; then
239  # 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")"
244
245  # 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"
259fi
260
261# 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
269###
270# Overwrite artifacts in the device archive to create the Mixed Build artifacts.
271cp "$SYSTEM_IMAGES_DIR"/system.img "$DEVICE_IMAGES_DIR"/
272if [[ "$INCLUDE_PRODUCT" == true ]]; then
273  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
280fi
281
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
285  readonly VBMETA_IMAGE_PATH="${OVERRIDE_VBMETA_IMAGE_PATH:-"$SYSTEM_IMAGES_DIR"/vbmeta.img}"
286  cp "$VBMETA_IMAGE_PATH" "$DEVICE_IMAGES_DIR"/
287fi
288
289# 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
292  cp "$OVERRIDE_BOOT_IMAGE_PATH" "$DEVICE_IMAGES_DIR"/boot.img
293fi
294
295###
296# Create the Mixed Build archive.
297(
298  cd "$DEVICE_IMAGES_DIR"
299  zip -r mixed.zip ./*
300)
301
302###
303# Archive the artifacts.
304if [ -n "$DIST_DIR" ]; then
305  mkdir -p "$DIST_DIR" || true
306fi
307# Archive all the device artifacts.
308rsync --archive --verbose --copy-links --exclude='logs' \
309  "$DEVICE_DIR"/* "$DIST_DIR"
310# Overwrite the image archive with the Mixed Build archive.
311OUT_ARCHIVE="$DIST_DIR"/"$(basename $DEVICE_ARCHIVE)"
312cp "$DEVICE_IMAGES_DIR"/mixed.zip "$OUT_ARCHIVE"
313# Overwrite android-info.txt with the updated one.
314cp "$DEVICE_IMAGES_DIR"/android-info.txt "$DIST_DIR"/
315