1#!/bin/bash -ex
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#       http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16usage () {
17    echo "Modifies the system image in a target_files.zip package so that it is"
18    echo "    compatible with vendor images of older Android versions."
19    echo "    This script is intended to be run on the Android build servers"
20    echo "    for inter branch mixed build targets."
21    echo
22    echo "Usage: $0 [-v <vendor_version>]"
23    echo "    <target_files_path> [<new_security_patch_level>]"
24    echo
25    echo "vendor_version is the version of the vendor image"
26    echo "    e.g. 8.1.0 for Android version O-MR1"
27    echo "new_security_patch_level is the value to replace the SPL in the"
28    echo "    original system.img"
29    echo "target_files_path is the path to the *-target_files-*.zip file"
30}
31
32# Print error message and exit.
33# Usage: exit_badparam message
34#
35# message is a string to be displayed before exit.
36exit_badparam () {
37    echo "ERROR: $1" >&2
38    usage
39    exit 1
40}
41
42cleanup_and_exit () {
43    readonly result="$?"
44    rm -rf "$TEMP_DIR"
45    exit "$result"
46}
47
48trap cleanup_and_exit EXIT
49
50while getopts :v: opt; do
51    case "$opt" in
52        v)
53            readonly VENDOR_VERSION="$OPTARG"
54            ;;
55        \?)
56            exit_badparam "Invalid options: -"$OPTARG""
57            ;;
58        :)
59            exit_badparam "Option -"$OPTARG" requires an argument."
60            ;;
61    esac
62done
63shift "$((OPTIND-1))"
64
65if [[ $# -lt 1 || $# -gt 2 ]]; then
66    exit_badparam "Unexpected number of arguments"
67fi
68
69readonly SYSTEM_TARGET_FILES="$1"
70readonly NEW_SPL="$2"
71
72if [[ ! -f "$SYSTEM_TARGET_FILES" ]]; then
73    exit_badparam "Could not find system target files package, "$SYSTEM_TARGET_FILES""
74fi
75
76# SPL must have YYYY-MM-DD format
77if [[ $# -eq 2 ]] && [[ ! "$NEW_SPL" =~ ^[0-9]{4}-(0[0-9]|1[012])-([012][0-9]|3[01])$ ]]; then
78    exit_badparam "<new_security_patch_level> must have YYYY-MM-DD format"
79fi
80
81if [[ -z "${ANDROID_BUILD_TOP+x}" ]]; then
82    build_top=""
83else
84    build_top="$ANDROID_BUILD_TOP"/
85fi
86
87readonly add_img_to_target_files="$build_top"build/make/tools/releasetools/add_img_to_target_files.py
88
89# Check required script
90if [[ ! -f "$add_img_to_target_files" ]]; then
91    echo "Error: Cannot find script,", "$add_img_to_target_files"
92    echo "Please run lunch or run from root of source tree."
93    exit 1
94fi
95
96readonly TEMP_DIR="$(mktemp -d /tmp/"$(basename $0)"_XXXXXXXX)"
97readonly SPL_PROPERTY_NAME="ro.build.version.security_patch"
98readonly RELEASE_VERSION_PROPERTY_NAME="ro.build.version.release"
99readonly VNDK_VERSION_PROPERTY="ro.vndk.version"
100readonly VNDK_VERSION_PROPERTY_OMR1="$VNDK_VERSION_PROPERTY"=27
101
102readonly BUILD_PROP_PATH="SYSTEM/build.prop"
103readonly PROP_DEFAULT_PATH="SYSTEM/etc/prop.default"
104
105# Unzip build.prop and prop.default from target_files.zip
106unzip "$SYSTEM_TARGET_FILES" "$BUILD_PROP_PATH" "$PROP_DEFAULT_PATH" -d "$TEMP_DIR"
107
108readonly BUILD_PROP_FILE="$TEMP_DIR"/"$BUILD_PROP_PATH"
109readonly PROP_DEFAULT_FILE="$TEMP_DIR"/"$PROP_DEFAULT_PATH"
110
111if [[ -f "$BUILD_PROP_FILE" ]]; then
112    readonly CURRENT_SPL=$(sed -n -r "s/^"$SPL_PROPERTY_NAME"=(.*)$/\1/p" "$BUILD_PROP_FILE")
113    readonly CURRENT_VERSION=$(sed -n -r "s/^"$RELEASE_VERSION_PROPERTY_NAME"=(.*)$/\1/p" "$BUILD_PROP_FILE")
114    echo "Reading build.prop..."
115    echo "  Current security patch level: "$CURRENT_SPL""
116    echo "  Current release version: "$CURRENT_VERSION""
117
118    # Update SPL to <new_security_patch_level>
119    if [[ "$NEW_SPL" != "" ]]; then
120        if [[ "$CURRENT_SPL" == "" ]]; then
121            echo "ERROR: Cannot find "$SPL_PROPERTY_NAME" in "$BUILD_PROP_PATH""
122            exit 1
123        else
124            # Replace the property inplace
125            sed -i "s/^"$SPL_PROPERTY_NAME"=.*$/"$SPL_PROPERTY_NAME"="$NEW_SPL"/" "$BUILD_PROP_FILE"
126            echo "Replacing..."
127            echo "  New security patch level: "$NEW_SPL""
128        fi
129    fi
130
131    # Update release version to <vendor_version>
132    if [[ "$VENDOR_VERSION" != "" ]]; then
133        if [[ "$CURRENT_VERSION" == "" ]]; then
134            echo "ERROR: Cannot find "$RELEASE_VERSION_PROPERTY_NAME" in "$BUILD_PROP_PATH""
135            exit 1
136        else
137            # Replace the property inplace
138            sed -i "s/^"$RELEASE_VERSION_PROPERTY_NAME"=.*$/"$RELEASE_VERSION_PROPERTY_NAME"="$VENDOR_VERSION"/" "$BUILD_PROP_FILE"
139            echo "Replacing..."
140            echo "  New release version for vendor.img: "$VENDOR_VERSION""
141        fi
142
143        if [[ "$VENDOR_VERSION" == "8.1.0" ]]; then
144            # add ro.vndk.version for O-MR1
145            if [[ -f "$PROP_DEFAULT_FILE" ]]; then
146                readonly CURRENT_VNDK_VERSION=$(sed -n -r "s/^"$VNDK_VERSION_PROPERTY"=(.*)$/\1/p" "$PROP_DEFAULT_FILE")
147                if [[ "$CURRENT_VNDK_VERSION" != "" ]]; then
148                    echo "WARNING: "$VNDK_VERSION_PROPERTY" is already set to "$CURRENT_VNDK_VERSION" in "$PROP_DEFAULT_PATH""
149                    echo "DID NOT overwrite "$VNDK_VERSION_PROPERTY""
150                else
151                    echo "Adding \""$VNDK_VERSION_PROPERTY_OMR1"\" to "$PROP_DEFAULT_PATH" for O-MR1 vendor image."
152                    sed -i -e "\$a\#\n\# FOR O-MR1 DEVICES\n\#\n"$VNDK_VERSION_PROPERTY_OMR1"" "$PROP_DEFAULT_FILE"
153                fi
154            else
155                echo "ERROR: Cannot find "$PROP_DEFAULT_PATH" in "$SYSTEM_TARGET_FILES""
156            fi
157        fi
158    fi
159else
160    echo "ERROR: Cannot find "$BUILD_PROP_PATH" in "$SYSTEM_TARGET_FILES""
161    exit 1
162fi
163
164# Re-zip build.prop and prop.default
165(
166    cd "$TEMP_DIR"
167    zip -ur "$SYSTEM_TARGET_FILES" ./*
168)
169
170# Rebuild system.img
171zip -d "$SYSTEM_TARGET_FILES" IMAGES/system\*
172"$add_img_to_target_files" -a "$SYSTEM_TARGET_FILES"
173
174echo "Done."
175