blob: c92bc6bf03d43bdf55cb823b5a73ede6dfa9faa4 [file] [log] [blame]
Steve Kondik5bd66602016-07-15 10:39:58 -07001#!/bin/bash
2#
3# Copyright (C) 2016 The CyanogenMod Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18PRODUCT_COPY_FILES_LIST=()
19PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +020020PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -070021PRODUCT_PACKAGES_LIST=()
22PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +020023PRODUCT_PACKAGES_FIXUP_HASHES=()
SamarV-1215556e2d2024-03-19 08:50:02 +053024PRODUCT_SYMLINKS_LIST=()
Steve Kondik5bd66602016-07-15 10:39:58 -070025PACKAGE_LIST=()
SamarV-1217e6b4082024-02-26 14:39:34 +053026REQUIRED_PACKAGES_LIST=
Steve Kondik5bd66602016-07-15 10:39:58 -070027VENDOR_STATE=-1
28VENDOR_RADIO_STATE=-1
29COMMON=-1
30ARCHES=
31FULLY_DEODEXED=-1
32
Chirayu Desai51ddd8d2022-05-26 14:50:35 +053033SKIP_CLEANUP=${SKIP_CLEANUP:-0}
34TMPDIR=${TMPDIR:-$(mktemp -d)}
Volodymyr Zhdanove54a1592020-10-22 01:33:24 +030035HOST="$(uname | tr '[:upper:]' '[:lower:]')"
Steve Kondik5bd66602016-07-15 10:39:58 -070036
37#
38# cleanup
39#
40# kill our tmpfiles with fire on exit
41#
42function cleanup() {
Chirayu Desai51ddd8d2022-05-26 14:50:35 +053043 if [ "$SKIP_CLEANUP" == "true" ] || [ "$SKIP_CLEANUP" == "1" ]; then
44 echo "Skipping cleanup of $TMPDIR"
45 else
46 rm -rf "${TMPDIR:?}"
47 fi
Steve Kondik5bd66602016-07-15 10:39:58 -070048}
49
Gabriele Mb8e54572017-10-11 12:55:51 +020050trap cleanup 0
Steve Kondik5bd66602016-07-15 10:39:58 -070051
52#
53# setup_vendor
54#
55# $1: device name
56# $2: vendor name
theimpulson9a911af2019-08-14 03:25:12 +000057# $3: OMNI root directory
Steve Kondik5bd66602016-07-15 10:39:58 -070058# $4: is common device - optional, default to false
59# $5: cleanup - optional, default to true
Jake Whatley9843b322017-01-25 21:49:16 -050060# $6: custom vendor makefile name - optional, default to false
Steve Kondik5bd66602016-07-15 10:39:58 -070061#
62# Must be called before any other functions can be used. This
63# sets up the internal state for a new vendor configuration.
64#
65function setup_vendor() {
66 local DEVICE="$1"
67 if [ -z "$DEVICE" ]; then
68 echo "\$DEVICE must be set before including this script!"
69 exit 1
70 fi
71
72 export VENDOR="$2"
73 if [ -z "$VENDOR" ]; then
74 echo "\$VENDOR must be set before including this script!"
75 exit 1
76 fi
77
theimpulson9a911af2019-08-14 03:25:12 +000078 export OMNI_ROOT="$3"
79 if [ ! -d "$OMNI_ROOT" ]; then
80 echo "\$OMNI_ROOT must be set and valid before including this script!"
Steve Kondik5bd66602016-07-15 10:39:58 -070081 exit 1
82 fi
83
84 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
theimpulson9a911af2019-08-14 03:25:12 +000085 if [ ! -d "$OMNI_ROOT/$OUTDIR" ]; then
86 mkdir -p "$OMNI_ROOT/$OUTDIR"
Steve Kondik5bd66602016-07-15 10:39:58 -070087 fi
88
Jake Whatley9843b322017-01-25 21:49:16 -050089 VNDNAME="$6"
90 if [ -z "$VNDNAME" ]; then
91 VNDNAME="$DEVICE"
92 fi
93
theimpulsonbb72ab82019-08-14 06:03:32 +000094 export PRODUCTMK="$OMNI_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -070095 export ANDROIDBP="$OMNI_ROOT"/"$OUTDIR"/Android.bp
theimpulson9a911af2019-08-14 03:25:12 +000096 export ANDROIDMK="$OMNI_ROOT"/"$OUTDIR"/Android.mk
97 export BOARDMK="$OMNI_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -070098
99 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
100 COMMON=1
101 else
102 COMMON=0
103 fi
104
Gabriele Mc44696d2017-05-01 18:22:04 +0200105 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700106 VENDOR_STATE=1
107 VENDOR_RADIO_STATE=1
108 else
109 VENDOR_STATE=0
110 VENDOR_RADIO_STATE=0
111 fi
Volodymyr Zhdanove54a1592020-10-22 01:33:24 +0300112
113 if [ -z "$PATCHELF" ]; then
114 export PATCHELF="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/bin/patchelf
115 fi
Michael Bestasf55ac292022-03-23 23:15:23 +0200116
117 if [ -z "$SIGSCAN" ]; then
118 export SIGSCAN="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/bin/SigScan
119 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700120}
121
Vladimir Oltean75d8e052018-06-24 20:22:41 +0300122# Helper functions for parsing a spec.
123# notes: an optional "|SHA1" that may appear in the format is stripped
124# early from the spec in the parse_file_list function, and
125# should not be present inside the input parameter passed
126# to these functions.
127
128#
129# input: spec in the form of "src[:dst][;args]"
130# output: "src"
131#
132function src_file() {
133 local SPEC="$1"
134 local SPLIT=(${SPEC//:/ })
135 local ARGS="$(target_args ${SPEC})"
136 # Regardless of there being a ":" delimiter or not in the spec,
137 # the source file is always either the first, or the only entry.
138 local SRC="${SPLIT[0]}"
139 # Remove target_args suffix, if present
140 echo "${SRC%;${ARGS}}"
141}
142
Steve Kondik5bd66602016-07-15 10:39:58 -0700143#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300144# input: spec in the form of "src[:dst][;args]"
145# output: "dst" if present, "src" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700146#
147function target_file() {
dianlujitao4918b8a2020-01-02 15:26:44 +0800148 local SPEC="${1%%;*}"
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300149 local SPLIT=(${SPEC//:/ })
150 local ARGS="$(target_args ${SPEC})"
151 local DST=
152 case ${#SPLIT[@]} in
153 1)
154 # The spec doesn't have a : delimiter
155 DST="${SPLIT[0]}"
156 ;;
157 *)
158 # The spec actually has a src:dst format
159 DST="${SPLIT[1]}"
160 ;;
161 esac
162 # Remove target_args suffix, if present
163 echo "${DST%;${ARGS}}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700164}
165
166#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300167# input: spec in the form of "src[:dst][;args]"
168# output: "args" if present, "" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700169#
170function target_args() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300171 local SPEC="$1"
172 local SPLIT=(${SPEC//;/ })
173 local ARGS=
174 case ${#SPLIT[@]} in
175 1)
176 # No ";" delimiter in the spec.
177 ;;
178 *)
179 # The "args" are whatever comes after the ";" character.
180 # Basically the spec stripped of whatever is to the left of ";".
181 ARGS="${SPEC#${SPLIT[0]};}"
182 ;;
183 esac
184 echo "${ARGS}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700185}
186
187#
188# prefix_match:
189#
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300190# input:
191# - $1: prefix
192# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
193# output:
194# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik5bd66602016-07-15 10:39:58 -0700195#
196function prefix_match() {
197 local PREFIX="$1"
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100198 local NEW_ARRAY=()
Vladimir Oltean7220f362018-04-02 22:37:09 +0300199 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
200 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700201 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300202 local ARGS=$(target_args "$LINE")
SamarV-1215556e2d2024-03-19 08:50:02 +0530203 if [[ -z "${ARGS}" || "${ARGS}" =~ 'SYMLINK' ]]; then
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100204 NEW_ARRAY+=("${FILE#$PREFIX}")
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300205 else
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100206 NEW_ARRAY+=("${FILE#$PREFIX};${ARGS}")
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300207 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700208 fi
209 done
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100210 printf '%s\n' "${NEW_ARRAY[@]}" | LC_ALL=C sort
Steve Kondik5bd66602016-07-15 10:39:58 -0700211}
212
213#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400214# prefix_match_file:
215#
216# $1: the prefix to match on
217# $2: the file to match the prefix for
218#
219# Internal function which returns true if a filename contains the
220# specified prefix.
221#
222function prefix_match_file() {
223 local PREFIX="$1"
224 local FILE="$2"
225 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
226 return 0
227 else
228 return 1
229 fi
230}
231
232#
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -0700233# suffix_match_file:
234#
235# $1: the suffix to match on
236# $2: the file to match the suffix for
237#
238# Internal function which returns true if a filename contains the
239# specified suffix.
240#
241function suffix_match_file() {
242 local SUFFIX="$1"
243 local FILE="$2"
244 if [[ "$FILE" = *"$SUFFIX" ]]; then
245 return 0
246 else
247 return 1
248 fi
249}
250
251#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400252# truncate_file
253#
254# $1: the filename to truncate
255# $2: the argument to output the truncated filename to
256#
257# Internal function which truncates a filename by removing the first dir
258# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
259#
260function truncate_file() {
261 local FILE="$1"
262 RETURN_FILE="$2"
263 local FIND="${FILE%%/*}"
264 local LOCATION="${#FIND}+1"
265 echo ${FILE:$LOCATION}
266}
267
268#
Steve Kondik5bd66602016-07-15 10:39:58 -0700269# write_product_copy_files:
270#
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400271# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400272#
Steve Kondik5bd66602016-07-15 10:39:58 -0700273# Creates the PRODUCT_COPY_FILES section in the product makefile for all
274# items in the list which do not start with a dash (-).
275#
276function write_product_copy_files() {
277 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
278 local TARGET=
279 local FILE=
280 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400281 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700282
283 if [ "$COUNT" -eq "0" ]; then
284 return 0
285 fi
286
287 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
288 for (( i=1; i<COUNT+1; i++ )); do
289 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
290 LINEEND=" \\"
291 if [ "$i" -eq "$COUNT" ]; then
292 LINEEND=""
293 fi
294
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300295 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400296 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400297 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400298 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400299 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400300 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400301 local OUTTARGET=$(truncate_file $TARGET)
302 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
303 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Luca Stefani776be462020-09-09 15:53:58 +0200304 elif prefix_match_file "system_ext/" $TARGET ; then
305 local OUTTARGET=$(truncate_file $TARGET)
306 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
307 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
308 elif prefix_match_file "system/system_ext/" $TARGET ; then
309 local OUTTARGET=$(truncate_file $TARGET)
310 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
311 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400312 elif prefix_match_file "odm/" $TARGET ; then
313 local OUTTARGET=$(truncate_file $TARGET)
314 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
315 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400316 elif prefix_match_file "vendor/odm/" $TARGET ; then
317 local OUTTARGET=$(truncate_file $TARGET)
318 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
319 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
320 elif prefix_match_file "system/vendor/odm/" $TARGET ; then
321 local OUTTARGET=$(truncate_file $TARGET)
322 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
323 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
324 elif prefix_match_file "vendor/" $TARGET ; then
325 local OUTTARGET=$(truncate_file $TARGET)
326 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
327 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Alexander Koskovich44c8fac2022-01-22 22:27:29 -0700328 elif prefix_match_file "vendor_dlkm/" $TARGET ; then
329 local OUTTARGET=$(truncate_file $TARGET)
330 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR_DLKM)/%s%s\n' \
331 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400332 elif prefix_match_file "system/vendor/" $TARGET ; then
333 local OUTTARGET=$(truncate_file $TARGET)
334 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
335 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400336 elif prefix_match_file "system/" $TARGET ; then
337 local OUTTARGET=$(truncate_file $TARGET)
338 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
339 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400340 else
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400341 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400342 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
343 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700344 done
345 return 0
346}
347
348#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700349# write_blueprint_packages:
Steve Kondik5bd66602016-07-15 10:39:58 -0700350#
351# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani776be462020-09-09 15:53:58 +0200352# $2: /system, /odm, /product, /system_ext, or /vendor partition
Steve Kondik5bd66602016-07-15 10:39:58 -0700353# $3: type-specific extra flags
354# $4: Name of the array holding the target list
355#
356# Internal function which writes out the BUILD_PREBUILT stanzas
357# for all modules in the list. This is called by write_product_packages
358# after the modules are categorized.
359#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700360function write_blueprint_packages() {
361
362 local CLASS="$1"
363 local PARTITION="$2"
364 local EXTRA="$3"
365
366 # Yes, this is a horrible hack - we create a new array using indirection
367 local ARR_NAME="$4[@]"
368 local FILELIST=("${!ARR_NAME}")
369
370 local FILE=
371 local ARGS=
372 local BASENAME=
373 local EXTENSION=
374 local PKGNAME=
375 local SRC=
TheStrix6e24acc2020-04-10 18:20:19 +0530376 local OVERRIDEPKG=
SamarV-1217e6b4082024-02-26 14:39:34 +0530377 local REQUIREDPKG=
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700378
379 for P in "${FILELIST[@]}"; do
380 FILE=$(target_file "$P")
381 ARGS=$(target_args "$P")
382
383 BASENAME=$(basename "$FILE")
384 DIRNAME=$(dirname "$FILE")
385 EXTENSION=${BASENAME##*.}
386 PKGNAME=${BASENAME%.*}
387
388 # Add to final package list
389 PACKAGE_LIST+=("$PKGNAME")
390
391 SRC="proprietary"
392 if [ "$PARTITION" = "system" ]; then
393 SRC+="/system"
394 elif [ "$PARTITION" = "vendor" ]; then
395 SRC+="/vendor"
396 elif [ "$PARTITION" = "product" ]; then
397 SRC+="/product"
Luca Stefani776be462020-09-09 15:53:58 +0200398 elif [ "$PARTITION" = "system_ext" ]; then
399 SRC+="/system_ext"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700400 elif [ "$PARTITION" = "odm" ]; then
401 SRC+="/odm"
402 fi
403
404 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
405 printf 'cc_prebuilt_library_shared {\n'
406 printf '\tname: "%s",\n' "$PKGNAME"
407 printf '\towner: "%s",\n' "$VENDOR"
408 printf '\tstrip: {\n'
409 printf '\t\tnone: true,\n'
410 printf '\t},\n'
411 printf '\ttarget: {\n'
412 if [ "$EXTRA" = "both" ]; then
413 printf '\t\tandroid_arm: {\n'
414 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
415 printf '\t\t},\n'
416 printf '\t\tandroid_arm64: {\n'
417 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
418 printf '\t\t},\n'
419 elif [ "$EXTRA" = "64" ]; then
420 printf '\t\tandroid_arm64: {\n'
421 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
422 printf '\t\t},\n'
423 else
424 printf '\t\tandroid_arm: {\n'
425 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
426 printf '\t\t},\n'
427 fi
428 printf '\t},\n'
429 if [ "$EXTRA" != "none" ]; then
430 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
431 fi
dianlujitao848101c2020-09-12 00:15:13 +0800432 printf '\tcheck_elf_files: false,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700433 elif [ "$CLASS" = "APPS" ]; then
434 printf 'android_app_import {\n'
435 printf '\tname: "%s",\n' "$PKGNAME"
436 printf '\towner: "%s",\n' "$VENDOR"
437 if [ "$EXTRA" = "priv-app" ]; then
438 SRC="$SRC/priv-app"
439 else
440 SRC="$SRC/app"
441 fi
442 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
TheStrix6e24acc2020-04-10 18:20:19 +0530443 ARGS=(${ARGS//;/ })
LuK1337508e85f2021-08-23 18:18:57 +0200444 USE_PLATFORM_CERTIFICATE="true"
445 for ARG in "${ARGS[@]}"; do
446 if [ "$ARG" = "PRESIGNED" ]; then
447 USE_PLATFORM_CERTIFICATE="false"
Michael Bestasab47e912024-03-06 13:32:05 +0200448 printf '\tpreprocessed: true,\n'
LuK1337508e85f2021-08-23 18:18:57 +0200449 printf '\tpresigned: true,\n'
450 elif [[ "$ARG" =~ "OVERRIDES" ]]; then
451 OVERRIDEPKG=${ARG#*=}
Arian72ac8362021-09-27 17:49:19 +0200452 OVERRIDEPKG=${OVERRIDEPKG//,/\", \"}
LuK1337508e85f2021-08-23 18:18:57 +0200453 printf '\toverrides: ["%s"],\n' "$OVERRIDEPKG"
SamarV-1217e6b4082024-02-26 14:39:34 +0530454 elif [[ "$ARG" =~ "REQUIRED" ]]; then
455 REQUIREDPKG=${ARG#*=}
456 REQUIRED_PACKAGES_LIST+="$REQUIREDPKG,"
457 printf '\trequired: ["%s"],\n' "${REQUIREDPKG//,/\", \"}"
SamarV-1215556e2d2024-03-19 08:50:02 +0530458 elif [[ "$ARG" =~ "SYMLINK" ]]; then
459 continue
LuK1337508e85f2021-08-23 18:18:57 +0200460 elif [ ! -z "$ARG" ]; then
461 USE_PLATFORM_CERTIFICATE="false"
462 printf '\tcertificate: "%s",\n' "$ARG"
463 fi
464 done
465 if [ "$USE_PLATFORM_CERTIFICATE" = "true" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700466 printf '\tcertificate: "platform",\n'
467 fi
468 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
469 printf 'dex_import {\n'
470 printf '\tname: "%s",\n' "$PKGNAME"
471 printf '\towner: "%s",\n' "$VENDOR"
472 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
473 elif [ "$CLASS" = "ETC" ]; then
474 if [ "$EXTENSION" = "xml" ]; then
475 printf 'prebuilt_etc_xml {\n'
476 else
477 printf 'prebuilt_etc {\n'
478 fi
479 printf '\tname: "%s",\n' "$PKGNAME"
480 printf '\towner: "%s",\n' "$VENDOR"
481 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
LuK1337f7f18712020-10-06 19:29:02 +0200482 printf '\tfilename_from_src: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700483 elif [ "$CLASS" = "EXECUTABLES" ]; then
484 if [ "$EXTENSION" = "sh" ]; then
485 printf 'sh_binary {\n'
486 else
487 printf 'cc_prebuilt_binary {\n'
488 fi
489 printf '\tname: "%s",\n' "$PKGNAME"
490 printf '\towner: "%s",\n' "$VENDOR"
Michael Bestasbda30202020-12-28 04:44:52 +0200491 printf '\tsrcs: ["%s/bin/%s"],\n' "$SRC" "$FILE"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200492 if [ "$EXTENSION" != "sh" ]; then
493 printf '\tcheck_elf_files: false,\n'
494 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700495 unset EXTENSION
496 else
497 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
498 fi
499 if [ "$CLASS" = "APPS" ]; then
500 printf '\tdex_preopt: {\n'
501 printf '\t\tenabled: false,\n'
502 printf '\t},\n'
Jyotiraditya Panda45f50af2024-02-19 05:35:33 +0900503 if [ "$DIRNAME" != "." ] && [[ "$DIRNAME" == */* ]]; then
504 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
505 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700506 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200507 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700508 if [ "$DIRNAME" != "." ]; then
Andreas Schneider408526a2020-05-23 15:58:43 +0200509 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700510 fi
511 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200512 if [ "$CLASS" = "ETC" ] ; then
513 if [ "$DIRNAME" != "." ]; then
514 printf '\tsub_dir: "%s",\n' "$DIRNAME"
515 fi
516 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700517 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
518 printf '\tprefer: true,\n'
519 fi
520 if [ "$EXTRA" = "priv-app" ]; then
521 printf '\tprivileged: true,\n'
522 fi
523 if [ "$PARTITION" = "vendor" ]; then
524 printf '\tsoc_specific: true,\n'
525 elif [ "$PARTITION" = "product" ]; then
526 printf '\tproduct_specific: true,\n'
Luca Stefani776be462020-09-09 15:53:58 +0200527 elif [ "$PARTITION" = "system_ext" ]; then
528 printf '\tsystem_ext_specific: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700529 elif [ "$PARTITION" = "odm" ]; then
530 printf '\tdevice_specific: true,\n'
531 fi
532 printf '}\n\n'
533 done
534}
535
536#
Steve Kondik5bd66602016-07-15 10:39:58 -0700537# write_product_packages:
538#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700539# This function will create prebuilt entries in the
540# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700541# product makefile for all files in the blob list which
542# start with a single dash (-) character.
543#
544function write_product_packages() {
545 PACKAGE_LIST=()
546
Chenyang Zhongc487f382022-02-10 21:40:41 -0500547 # Sort the package list for comm
548 PRODUCT_PACKAGES_LIST=($( printf '%s\n' "${PRODUCT_PACKAGES_LIST[@]}" | LC_ALL=C sort))
549
Steve Kondik5bd66602016-07-15 10:39:58 -0700550 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
551
552 if [ "$COUNT" = "0" ]; then
553 return 0
554 fi
555
556 # Figure out what's 32-bit, what's 64-bit, and what's multilib
557 # I really should not be doing this in bash due to shitty array passing :(
558 local T_LIB32=( $(prefix_match "lib/") )
559 local T_LIB64=( $(prefix_match "lib64/") )
560 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
561 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
562 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
563
564 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700565 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700566 fi
567 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700568 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700569 fi
570 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700571 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700572 fi
573
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400574 local T_S_LIB32=( $(prefix_match "system/lib/") )
575 local T_S_LIB64=( $(prefix_match "system/lib64/") )
576 local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
577 local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
578 local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
579
580 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700581 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400582 fi
583 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700584 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400585 fi
586 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700587 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400588 fi
589
Steve Kondik5bd66602016-07-15 10:39:58 -0700590 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
591 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
592 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
593 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
594 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
595
596 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700597 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700598 fi
599 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700600 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700601 fi
602 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700603 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500604 fi
605
606 local T_P_LIB32=( $(prefix_match "product/lib/") )
607 local T_P_LIB64=( $(prefix_match "product/lib64/") )
608 local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
609 local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
610 local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
611
612 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700613 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500614 fi
615 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700616 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500617 fi
618 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700619 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700620 fi
621
Luca Stefani776be462020-09-09 15:53:58 +0200622 local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
623 local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
624 local SE_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
625 local SE_LIB32=( $(comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
626 local SE_LIB64=( $(comm -23 <(printf '%s\n' "${T_SE_LIB64[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
627
628 if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
629 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
630 fi
631 if [ "${#SE_LIB32[@]}" -gt "0" ]; then
632 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
633 fi
634 if [ "${#SE_LIB64[@]}" -gt "0" ]; then
635 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
636 fi
637
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700638 local T_O_LIB32=( $(prefix_match "odm/lib/") )
639 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
640 local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
641 local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
642 local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
643
644 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700645 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700646 fi
647 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700648 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700649 fi
650 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700651 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700652 fi
653
Steve Kondik5bd66602016-07-15 10:39:58 -0700654 # Apps
655 local APPS=( $(prefix_match "app/") )
656 if [ "${#APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100657 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700658 fi
659 local PRIV_APPS=( $(prefix_match "priv-app/") )
660 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100661 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700662 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400663 local S_APPS=( $(prefix_match "system/app/") )
664 if [ "${#S_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100665 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400666 fi
667 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
668 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100669 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400670 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700671 local V_APPS=( $(prefix_match "vendor/app/") )
672 if [ "${#V_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100673 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700674 fi
675 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
676 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100677 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500678 fi
679 local P_APPS=( $(prefix_match "product/app/") )
680 if [ "${#P_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100681 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500682 fi
683 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
684 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100685 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700686 fi
Luca Stefani776be462020-09-09 15:53:58 +0200687 local SE_APPS=( $(prefix_match "system_ext/app/") )
688 if [ "${#SE_APPS[@]}" -gt "0" ]; then
689 write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
690 fi
691 local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
692 if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
693 write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
694 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700695 local O_APPS=( $(prefix_match "odm/app/") )
696 if [ "${#O_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100697 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700698 fi
699 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
700 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100701 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700702 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700703
704 # Framework
705 local FRAMEWORK=( $(prefix_match "framework/") )
706 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700707 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700708 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400709 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
710 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700711 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400712 fi
Christian Oder974b5902017-10-08 23:15:52 +0200713 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200714 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700715 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500716 fi
717 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
718 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700719 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oder974b5902017-10-08 23:15:52 +0200720 fi
Luca Stefani776be462020-09-09 15:53:58 +0200721 local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
Alexander Koskovich052c77d2020-09-16 17:58:53 -0700722 if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
Luca Stefani776be462020-09-09 15:53:58 +0200723 write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
724 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700725 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
726 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700727 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700728 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700729
730 # Etc
731 local ETC=( $(prefix_match "etc/") )
732 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700733 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700734 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400735 local S_ETC=( $(prefix_match "system/etc/") )
Luca Weiss737940e2022-09-27 14:52:41 +0200736 if [ "${#S_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700737 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400738 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700739 local V_ETC=( $(prefix_match "vendor/etc/") )
740 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700741 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500742 fi
743 local P_ETC=( $(prefix_match "product/etc/") )
744 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700745 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700746 fi
Luca Stefani776be462020-09-09 15:53:58 +0200747 local SE_ETC=( $(prefix_match "system_ext/etc/") )
748 if [ "${#SE_ETC[@]}" -gt "0" ]; then
749 write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
750 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700751 local O_ETC=( $(prefix_match "odm/etc/") )
752 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700753 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700754 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700755
756 # Executables
757 local BIN=( $(prefix_match "bin/") )
758 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700759 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700760 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400761 local S_BIN=( $(prefix_match "system/bin/") )
Luca Weiss737940e2022-09-27 14:52:41 +0200762 if [ "${#S_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700763 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400764 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700765 local V_BIN=( $(prefix_match "vendor/bin/") )
766 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700767 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500768 fi
769 local P_BIN=( $(prefix_match "product/bin/") )
770 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700771 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700772 fi
Luca Stefani776be462020-09-09 15:53:58 +0200773 local SE_BIN=( $(prefix_match "system_ext/bin/") )
774 if [ "${#SE_BIN[@]}" -gt "0" ]; then
775 write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
776 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700777 local O_BIN=( $(prefix_match "odm/bin/") )
778 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700779 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700780 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700781
SamarV-1215556e2d2024-03-19 08:50:02 +0530782 write_package_definition "${PACKAGE_LIST[@]}" >> "$PRODUCTMK"
783}
Steve Kondik5bd66602016-07-15 10:39:58 -0700784
SamarV-1215556e2d2024-03-19 08:50:02 +0530785
786#
787# write_symlink_packages:
788#
789# Creates symlink entries in the Android.bp and related PRODUCT_PACKAGES
790# list in the product makefile for all files in the blob list which has
791# SYMLINK argument.
792#
793function write_symlink_packages() {
794 local FILE=
795 local ARGS=
796 local ARCH=
797 local BASENAME=
798 local PKGNAME=
799 local PREFIX=
800 local SYMLINK_BASENAME=
801 local SYMLINK_PACKAGES=()
802
803 # Sort the symlinks list for comm
804 PRODUCT_SYMLINKS_LIST=($( printf '%s\n' "${PRODUCT_SYMLINKS_LIST[@]}" | LC_ALL=C sort))
805
806 local COUNT=${#PRODUCT_SYMLINKS_LIST[@]}
807
808 if [ "$COUNT" = "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700809 return 0
810 fi
811
SamarV-1215556e2d2024-03-19 08:50:02 +0530812 for LINE in "${PRODUCT_SYMLINKS_LIST[@]}"; do
813 FILE=$(src_file "$LINE")
Bruno Martinsf96fd122024-03-28 14:31:02 +0000814 if [[ "$LINE" =~ '/lib64/' || "$LINE" =~ '/lib/arm64/' ]]; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530815 ARCH="64"
Bruno Martinsf96fd122024-03-28 14:31:02 +0000816 elif [[ "$LINE" =~ '/lib/' ]]; then
817 ARCH="32"
Steve Kondik5bd66602016-07-15 10:39:58 -0700818 fi
SamarV-1215556e2d2024-03-19 08:50:02 +0530819 BASENAME=$(basename "$FILE")
820 ARGS=$(target_args "$LINE")
821 ARGS=(${ARGS//;/ })
822 for ARG in "${ARGS[@]}"; do
823 if [[ "$ARG" =~ "SYMLINK" ]]; then
824 SYMLINKS=${ARG#*=}
825 SYMLINKS=(${SYMLINKS//,/ })
826 for SYMLINK in "${SYMLINKS[@]}"; do
827 SYMLINK_BASENAME=$(basename "$SYMLINK")
828 PKGNAME=${BASENAME%.*}_${SYMLINK_BASENAME%.*}_symlink${ARCH}
829 {
830 printf 'install_symlink {\n'
831 printf '\tname: "%s",\n' "$PKGNAME"
Mashopy06100c92024-04-23 21:52:04 +0200832 if prefix_match_file "vendor/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530833 PREFIX='vendor/'
834 printf '\tsoc_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200835 elif prefix_match_file "product/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530836 PREFIX='product/'
837 printf '\tproduct_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200838 elif prefix_match_file "system_ext/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530839 PREFIX='system_ext/'
840 printf '\tsystem_ext_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200841 elif prefix_match_file "odm/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530842 PREFIX='odm/'
843 printf '\tdevice_specific: true,\n'
844 fi
845 printf '\tinstalled_location: "%s",\n' "${SYMLINK#"$PREFIX"}"
846 printf '\tsymlink_target: "/%s",\n' "$FILE"
847 printf '}\n\n'
848 } >> "$ANDROIDBP"
849 SYMLINK_PACKAGES+=("$PKGNAME")
850 done
851 fi
852 done
Steve Kondik5bd66602016-07-15 10:39:58 -0700853 done
SamarV-1215556e2d2024-03-19 08:50:02 +0530854
855 write_package_definition "${SYMLINK_PACKAGES[@]}" >> "$PRODUCTMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700856}
857
858#
Michael Bestasfe71eb32023-06-11 18:59:10 +0300859# write_single_product_copy_files:
860#
861# $1: the file to be copied
862#
863# Creates a PRODUCT_COPY_FILES section in the product makefile for the
864# item provided in $1.
865#
866function write_single_product_copy_files() {
867 local FILE="$1"
868 if [ -z "$FILE" ]; then
869 echo "A file must be provided to write_single_product_copy_files()!"
870 exit 1
871 fi
872
873 local TARGET=$(target_file "$FILE")
874 local OUTTARGET=$(truncate_file $TARGET)
875
876 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
877 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s\n' \
878 "$OUTDIR" "$TARGET" "$OUTTARGET" >> "$PRODUCTMK"
879}
880
881#
882# write_single_product_packages:
883#
884# $1: the package to be built
885#
886# Creates a PRODUCT_PACKAGES section in the product makefile for the
887# item provided in $1.
888#
889function write_single_product_packages() {
890 local PACKAGE="$1"
891 if [ -z "$PACKAGE" ]; then
892 echo "A package must be provided to write_single_product_packages()!"
893 exit 1
894 fi
895
896 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
897 printf ' %s%s\n' "$PACKAGE" >> "$PRODUCTMK"
898}
899
900#
Michael Bestas431a8002023-06-11 20:04:45 +0300901# write_rro_androidmanifest:
902#
903# $2: target package for the RRO overlay
904#
905# Creates an AndroidManifest.xml for an RRO overlay.
906#
907function write_rro_androidmanifest() {
908 local TARGET_PACKAGE="$1"
909
910 cat << EOF
911<manifest xmlns:android="http://schemas.android.com/apk/res/android"
912 package="$TARGET_PACKAGE.vendor"
913 android:versionCode="1"
914 android:versionName="1.0">
915 <application android:hasCode="false" />
916 <overlay
917 android:targetPackage="$TARGET_PACKAGE"
918 android:isStatic="true"
919 android:priority="0"/>
920</manifest>
921EOF
922}
923
924#
925# write_rro_blueprint:
926#
927# $1: package name for the RRO overlay
928# $2: target partition for the RRO overlay
929#
930# Creates an Android.bp for an RRO overlay.
931#
932function write_rro_blueprint() {
933 local PKGNAME="$1"
934 local PARTITION="$2"
935
936 printf 'runtime_resource_overlay {\n'
937 printf '\tname: "%s",\n' "$PKGNAME"
938 printf '\ttheme: "%s",\n' "$PKGNAME"
939 printf '\tsdk_version: "%s",\n' "current"
940 printf '\taaptflags: ["%s"],\n' "--keep-raw-values"
941
942 if [ "$PARTITION" = "vendor" ]; then
943 printf '\tsoc_specific: true,\n'
944 elif [ "$PARTITION" = "product" ]; then
945 printf '\tproduct_specific: true,\n'
946 elif [ "$PARTITION" = "system_ext" ]; then
947 printf '\tsystem_ext_specific: true,\n'
948 elif [ "$PARTITION" = "odm" ]; then
949 printf '\tdevice_specific: true,\n'
950 fi
951 printf '}\n'
952}
953
954#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700955# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -0700956#
957# $1: file which will be written to
958#
Michael Bestasa2934df2020-12-19 03:50:32 +0200959# writes out the warning message regarding manual file modifications.
Steve Kondik5bd66602016-07-15 10:39:58 -0700960# note that this is not an append operation, and should
961# be executed first!
962#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700963function write_blueprint_header() {
964 if [ -f $1 ]; then
965 rm $1
966 fi
967
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700968 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
969
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700970 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +0200971// Automatically generated file. DO NOT MODIFY
972//
973// This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700974
975EOF
976}
977
978#
979# write_makefile_header:
980#
981# $1: file which will be written to
982#
Michael Bestasa2934df2020-12-19 03:50:32 +0200983# writes out the warning message regarding manual file modifications.
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700984# note that this is not an append operation, and should
985# be executed first!
986#
987function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500988 if [ -f $1 ]; then
989 rm $1
990 fi
991
Steve Kondik5bd66602016-07-15 10:39:58 -0700992 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
993
Jake Whatley9843b322017-01-25 21:49:16 -0500994 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +0200995# Automatically generated file. DO NOT MODIFY
Steve Kondik5bd66602016-07-15 10:39:58 -0700996#
Steve Kondik5bd66602016-07-15 10:39:58 -0700997# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
998
999EOF
1000}
1001
1002#
Michael Bestas431a8002023-06-11 20:04:45 +03001003# write_xml_header:
1004#
1005# $1: file which will be written to
1006#
1007# writes out the warning message regarding manual file modifications.
1008# note that this is not an append operation, and should
1009# be executed first!
1010#
1011function write_xml_header() {
1012 if [ -f $1 ]; then
1013 rm $1
1014 fi
1015
1016 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1017 [ "$COMMON" -eq 1 ] && local VENDOR="${VENDOR_COMMON:-$VENDOR}"
1018
1019 cat << EOF >> $1
1020<?xml version="1.0" encoding="utf-8"?>
1021<!--
1022 Automatically generated file. DO NOT MODIFY
1023
1024 This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1025-->
1026EOF
1027}
1028
1029#
1030# write_rro_package:
1031#
1032# $1: the RRO package name
1033# $2: the RRO target package
1034# $3: the partition for the RRO overlay
1035#
1036# Generates the file structure for an RRO overlay.
1037#
1038function write_rro_package() {
1039 local PKGNAME="$1"
1040 if [ -z "$PKGNAME" ]; then
1041 echo "A package name must be provided to write_rro_package()!"
1042 exit 1
1043 fi
1044
1045 local TARGET_PACKAGE="$2"
1046 if [ -z "$TARGET_PACKAGE" ]; then
1047 echo "A target package must be provided to write_rro_package()!"
1048 exit 1
1049 fi
1050
1051 local PARTITION="$3"
1052 if [ -z "$PARTITION" ]; then
1053 PARTITION="vendor"
1054 fi
1055
1056 local RROBP="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/Android.bp
1057 local RROMANIFEST="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/AndroidManifest.xml
1058
1059 write_blueprint_header "$RROBP"
1060 write_xml_header "$RROMANIFEST"
1061
1062 write_rro_blueprint "$PKGNAME" "$PARTITION" >> "$RROBP"
1063 write_rro_androidmanifest "$TARGET_PACKAGE" >> "$RROMANIFEST"
1064}
1065
1066#
SamarV-1215556e2d2024-03-19 08:50:02 +05301067# write_package_definition:
1068#
1069# $@: list of packages
1070#
1071# writes out the final PRODUCT_PACKAGES list
1072#
1073function write_package_definition() {
1074 local PACKAGE_LIST=("${@}")
1075 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
1076
1077 if [ "$PACKAGE_COUNT" -eq "0" ]; then
1078 return 0
1079 fi
1080
1081 printf '\n%s\n' "PRODUCT_PACKAGES += \\"
1082 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
SamarV-1217e6b4082024-02-26 14:39:34 +05301083 local SKIP=false
SamarV-1215556e2d2024-03-19 08:50:02 +05301084 local LINEEND=" \\"
1085 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
1086 LINEEND=""
1087 fi
SamarV-1217e6b4082024-02-26 14:39:34 +05301088 for PKG in $(tr "," "\n" <<< "$REQUIRED_PACKAGES_LIST"); do
1089 if [[ $PKG == "${PACKAGE_LIST[$i - 1]}" ]]; then
1090 SKIP=true
1091 break
1092 fi
1093 done
1094 # Skip adding of the package to product makefile if it's in the required list
1095 if [[ $SKIP == false ]]; then
1096 printf ' %s%s\n' "${PACKAGE_LIST[$i - 1]}" "$LINEEND" >> "$PRODUCTMK"
1097 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301098 done
1099}
1100
1101#
Steve Kondik5bd66602016-07-15 10:39:58 -07001102# write_headers:
1103#
1104# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -05001105# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001106#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001107# Calls write_makefile_header for each of the makefiles and
1108# write_blueprint_header for Android.bp and creates the initial
1109# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -07001110#
1111function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001112 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001113
1114 GUARD="$2"
1115 if [ -z "$GUARD" ]; then
1116 GUARD="TARGET_DEVICE"
1117 fi
1118
Steve Kondik5bd66602016-07-15 10:39:58 -07001119 cat << EOF >> "$ANDROIDMK"
1120LOCAL_PATH := \$(call my-dir)
1121
1122EOF
1123 if [ "$COMMON" -ne 1 ]; then
1124 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001125ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -07001126
1127EOF
1128 else
1129 if [ -z "$1" ]; then
1130 echo "Argument with devices to be added to guard must be set!"
1131 exit 1
1132 fi
1133 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001134ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -07001135
1136EOF
1137 fi
1138
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001139 write_makefile_header "$BOARDMK"
1140 write_makefile_header "$PRODUCTMK"
1141 write_blueprint_header "$ANDROIDBP"
1142
1143 cat << EOF >> "$ANDROIDBP"
1144soong_namespace {
1145}
1146
1147EOF
1148
1149 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1150 cat << EOF >> "$PRODUCTMK"
1151PRODUCT_SOONG_NAMESPACES += \\
1152 vendor/$VENDOR/$DEVICE
1153
1154EOF
Steve Kondik5bd66602016-07-15 10:39:58 -07001155}
1156
1157#
1158# write_footers:
1159#
1160# Closes the inital guard and any other finalization tasks. Must
1161# be called as the final step.
1162#
1163function write_footers() {
1164 cat << EOF >> "$ANDROIDMK"
1165endif
1166EOF
1167}
1168
1169# Return success if adb is up and not in recovery
1170function _adb_connected {
1171 {
Jake Whatley9843b322017-01-25 21:49:16 -05001172 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -07001173 then
1174 return 0
1175 fi
1176 } 2>/dev/null
1177
1178 return 1
1179};
1180
1181#
1182# parse_file_list:
1183#
1184# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001185# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001186#
1187# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
1188#
1189function parse_file_list() {
1190 if [ -z "$1" ]; then
1191 echo "An input file is expected!"
1192 exit 1
1193 elif [ ! -f "$1" ]; then
1194 echo "Input file "$1" does not exist!"
1195 exit 1
1196 fi
1197
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001198 if [ -n "$2" ]; then
1199 echo "Using section \"$2\""
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001200 LIST=$TMPDIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +02001201 # Match all lines starting with first line found to start* with '#'
1202 # comment and contain** $2, and ending with first line to be empty*.
1203 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1204 # **the $2 match is case-insensitive
1205 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001206 else
1207 LIST=$1
1208 fi
1209
Steve Kondik5bd66602016-07-15 10:39:58 -07001210 PRODUCT_PACKAGES_LIST=()
1211 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001212 PRODUCT_PACKAGES_FIXUP_HASHES=()
SamarV-1215556e2d2024-03-19 08:50:02 +05301213 PRODUCT_SYMLINKS_LIST=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001214 PRODUCT_COPY_FILES_LIST=()
1215 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001216 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001217
1218 while read -r line; do
1219 if [ -z "$line" ]; then continue; fi
1220
1221 # If the line has a pipe delimiter, a sha1 hash should follow.
1222 # This indicates the file should be pinned and not overwritten
1223 # when extracting files.
1224 local SPLIT=(${line//\|/ })
1225 local COUNT=${#SPLIT[@]}
1226 local SPEC=${SPLIT[0]}
1227 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001228 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -07001229 if [ "$COUNT" -gt "1" ]; then
1230 HASH=${SPLIT[1]}
1231 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001232 if [ "$COUNT" -gt "2" ]; then
1233 FIXUP_HASH=${SPLIT[2]}
1234 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301235 if [[ "$SPEC" =~ 'SYMLINK=' ]]; then
1236 PRODUCT_SYMLINKS_LIST+=("${SPEC#-}")
1237 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001238 # if line starts with a dash, it needs to be packaged
1239 if [[ "$SPEC" =~ ^- ]]; then
1240 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1241 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001242 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Michael Bestasea90aef2021-11-15 22:18:04 +02001243 # if line contains apk, jar or vintf fragment, it needs to be packaged
1244 elif suffix_match_file ".apk" "$(src_file "$SPEC")" || \
1245 suffix_match_file ".jar" "$(src_file "$SPEC")" || \
1246 [[ "$SPEC" == *"etc/vintf/manifest/"* ]]; then
1247 PRODUCT_PACKAGES_LIST+=("$SPEC")
1248 PRODUCT_PACKAGES_HASHES+=("$HASH")
1249 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001250 else
1251 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1252 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001253 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001254 fi
1255
Chirayu Desaif1a21302022-09-13 00:29:33 +05301256 done < <(grep -v -E '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -07001257}
1258
1259#
1260# write_makefiles:
1261#
1262# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001263# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001264#
SamarV-1215556e2d2024-03-19 08:50:02 +05301265# Calls write_product_copy_files, write_product_packages and
1266# lastly write_symlink_packages on the given file and appends
1267# to the Android.bp as well as the product makefile.
Steve Kondik5bd66602016-07-15 10:39:58 -07001268#
1269function write_makefiles() {
1270 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001271 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -07001272 write_product_packages
SamarV-1215556e2d2024-03-19 08:50:02 +05301273 write_symlink_packages
Steve Kondik5bd66602016-07-15 10:39:58 -07001274}
1275
1276#
1277# append_firmware_calls_to_makefiles:
1278#
1279# Appends to Android.mk the calls to all images present in radio folder
1280# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1281#
1282function append_firmware_calls_to_makefiles() {
1283 cat << EOF >> "$ANDROIDMK"
1284ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1285
1286RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1287\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1288 \$(call add-radio-file,radio/\$(f)))
1289\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1290
1291endif
1292
1293EOF
1294}
1295
1296#
1297# get_file:
1298#
1299# $1: input file
1300# $2: target file/folder
1301# $3: source of the file (can be "adb" or a local folder)
1302#
1303# Silently extracts the input file to defined target
1304# Returns success if file can be pulled from the device or found locally
1305#
1306function get_file() {
1307 local SRC="$3"
1308
1309 if [ "$SRC" = "adb" ]; then
1310 # try to pull
LuK13370f7f0d12022-08-19 21:49:56 +02001311 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1312 adb pull "${1#/system}" "$2" >/dev/null 2>&1 && return 0
1313 adb pull "system/$1" "$2" >/dev/null 2>&1 && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001314
1315 return 1
1316 else
1317 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001318 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1319 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001320 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001321
LuK1337dbb77cc2023-12-04 19:03:10 +01001322 # try /vendor/odm for devices without /odm partition
1323 [[ "$1" == /system/odm/* ]] && cp -r "$SRC/vendor/${1#/system}" "$2" 2>/dev/null && return 0
1324
Steve Kondik5bd66602016-07-15 10:39:58 -07001325 return 1
1326 fi
1327};
1328
1329#
1330# oat2dex:
1331#
1332# $1: extracted apk|jar (to check if deodex is required)
1333# $2: odexed apk|jar to deodex
1334# $3: source of the odexed apk|jar
1335#
1336# Convert apk|jar .odex in the corresposing classes.dex
1337#
1338function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001339 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001340 local OEM_TARGET="$2"
1341 local SRC="$3"
1342 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001343 local OAT=
Steve Kondik5bd66602016-07-15 10:39:58 -07001344
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001345 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1346 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1347 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001348 fi
1349
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001350 if [ -z "$VDEXEXTRACTOR" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001351 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001352 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001353
codeworkx85eda752018-09-23 12:36:57 +02001354 if [ -z "$CDEXCONVERTER" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001355 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001356 fi
1357
Steve Kondik5bd66602016-07-15 10:39:58 -07001358 # Extract existing boot.oats to the temp folder
1359 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001360 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001361 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -05001362 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001363 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001364 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001365 else
1366 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001367 fi
1368 done
1369 fi
1370
1371 if [ -z "$ARCHES" ]; then
1372 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1373 fi
1374
theimpulson9a911af2019-08-14 03:25:12 +00001375 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001376 return;
1377 fi
1378
theimpulson9a911af2019-08-14 03:25:12 +00001379 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001380 return 0 # target apk|jar is already odexed, return
1381 fi
1382
1383 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -05001384 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001385
Joe Maplesfb3941c2018-01-05 14:51:33 -05001386 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1387 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001388
Joe Maplesfb3941c2018-01-05 14:51:33 -05001389 if get_file "$OAT" "$TMPDIR" "$SRC"; then
1390 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
1391 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001392 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1393 for CLASS in $CLASSES; do
1394 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1395 # Check if we have to deal with CompactDex
1396 if [[ "$CLASS" == *.cdex ]]; then
1397 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1398 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1399 else
1400 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1401 fi
1402 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001403 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001404 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1405 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001406 fi
theimpulson9a911af2019-08-14 03:25:12 +00001407 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001408 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001409 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001410 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001411 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001412 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001413 # try to extract classes.dex from boot.vdex for frameworks jars
1414 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +02001415 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani6f92e6b2018-10-31 19:16:05 +01001416 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001417 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1418 for CLASS in $CLASSES; do
1419 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1420 # Check if we have to deal with CompactDex
1421 if [[ "$CLASS" == *.cdex ]]; then
1422 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1423 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1424 else
1425 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1426 fi
1427 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001428 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001429 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1430 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001431 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001432 else
1433 continue
1434 fi
1435
Steve Kondik5bd66602016-07-15 10:39:58 -07001436 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001437
1438 rm -rf "$TMPDIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001439}
1440
1441#
1442# init_adb_connection:
1443#
1444# Starts adb server and waits for the device
1445#
1446function init_adb_connection() {
1447 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1448 if ! _adb_connected; then
1449 echo "No device is online. Waiting for one..."
1450 echo "Please connect USB and/or enable USB debugging"
1451 until _adb_connected; do
1452 sleep 1
1453 done
1454 echo "Device Found."
1455 fi
1456
1457 # Retrieve IP and PORT info if we're using a TCP connection
Chirayu Desaif1a21302022-09-13 00:29:33 +05301458 TCPIPPORT=$(adb devices | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
Steve Kondik5bd66602016-07-15 10:39:58 -07001459 | head -1 | awk '{print $1}')
1460 adb root &> /dev/null
1461 sleep 0.3
1462 if [ -n "$TCPIPPORT" ]; then
1463 # adb root just killed our connection
1464 # so reconnect...
1465 adb connect "$TCPIPPORT"
1466 fi
1467 adb wait-for-device &> /dev/null
1468 sleep 0.3
1469}
1470
1471#
1472# fix_xml:
1473#
1474# $1: xml file to fix
1475#
1476function fix_xml() {
1477 local XML="$1"
1478 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1479
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001480 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1481 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001482
1483 mv "$TEMP_XML" "$XML"
1484}
1485
Vladimir Olteande985fe2019-01-17 03:07:34 +02001486function get_hash() {
1487 local FILE="$1"
1488
1489 if [ "$(uname)" == "Darwin" ]; then
1490 shasum "${FILE}" | awk '{print $1}'
1491 else
1492 sha1sum "${FILE}" | awk '{print $1}'
1493 fi
1494}
1495
Vladimir Olteana7d20492019-01-17 03:05:52 +02001496function print_spec() {
1497 local SPEC_PRODUCT_PACKAGE="$1"
1498 local SPEC_SRC_FILE="$2"
1499 local SPEC_DST_FILE="$3"
1500 local SPEC_ARGS="$4"
1501 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001502 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001503
1504 local PRODUCT_PACKAGE=""
1505 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1506 PRODUCT_PACKAGE="-"
1507 fi
1508 local SRC=""
1509 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1510 SRC="${SPEC_SRC_FILE}:"
1511 fi
1512 local DST=""
1513 if [ ! -z "${SPEC_DST_FILE}" ]; then
1514 DST="${SPEC_DST_FILE}"
1515 fi
1516 local ARGS=""
1517 if [ ! -z "${SPEC_ARGS}" ]; then
1518 ARGS=";${SPEC_ARGS}"
1519 fi
1520 local HASH=""
1521 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1522 HASH="|${SPEC_HASH}"
1523 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001524 local FIXUP_HASH=""
1525 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1526 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1527 fi
1528 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1529}
1530
1531# To be overridden by device-level extract-files.sh
1532# Parameters:
1533# $1: spec name of a blob. Can be used for filtering.
1534# If the spec is "src:dest", then $1 is "dest".
1535# If the spec is "src", then $1 is "src".
1536# $2: path to blob file. Can be used for fixups.
1537#
1538function blob_fixup() {
1539 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001540}
1541
Steve Kondik5bd66602016-07-15 10:39:58 -07001542#
1543# extract:
1544#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001545# Positional parameters:
1546# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001547# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001548# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1549#
1550# Non-positional parameters (coming after $2):
1551# --section: preferred way of selecting the portion to parse and extract from
1552# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001553# --kang: if present, this option will activate the printing of hashes for the
1554# extracted blobs. Useful with --section for subsequent pinning of
1555# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001556#
1557function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001558 # Consume positional parameters
1559 local PROPRIETARY_FILES_TXT="$1"; shift
1560 local SRC="$1"; shift
1561 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001562 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001563
1564 # Consume optional, non-positional parameters
1565 while [ "$#" -gt 0 ]; do
1566 case "$1" in
1567 -s|--section)
1568 SECTION="$2"; shift
1569 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001570 -k|--kang)
1571 KANG=true
1572 DISABLE_PINNING=1
1573 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001574 *)
1575 # Backwards-compatibility with the old behavior, where $3, if
1576 # present, denoted an optional positional ${SECTION} argument.
1577 # Users of ${SECTION} are encouraged to migrate from setting it as
1578 # positional $3, to non-positional --section ${SECTION}, the
1579 # reason being that it doesn't scale to have more than 1 optional
1580 # positional argument.
1581 SECTION="$1"
1582 ;;
1583 esac
1584 shift
1585 done
1586
Steve Kondik5bd66602016-07-15 10:39:58 -07001587 if [ -z "$OUTDIR" ]; then
1588 echo "Output dir not set!"
1589 exit 1
1590 fi
1591
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001592 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001593
1594 # Allow failing, so we can try $DEST and/or $FILE
1595 set +e
1596
1597 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1598 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001599 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001600 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001601 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001602 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001603 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1604
1605 if [ "$SRC" = "adb" ]; then
1606 init_adb_connection
1607 fi
1608
Dan Pasanen0cc05012017-03-21 09:06:11 -05001609 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold9baced42017-11-10 16:33:38 +01001610 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001611
1612 # Check if we're working with the same zip that was passed last time.
1613 # If so, let's just use what's already extracted.
1614 MD5=`md5sum "$SRC"| awk '{print $1}'`
1615 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1616
1617 if [ "$MD5" != "$OLDMD5" ]; then
1618 rm -rf "$DUMPDIR"
1619 mkdir "$DUMPDIR"
1620 unzip "$SRC" -d "$DUMPDIR"
1621 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1622
1623 # Stop if an A/B OTA zip is detected. We cannot extract these.
1624 if [ -a "$DUMPDIR"/payload.bin ]; then
1625 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1626 exit 1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001627 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001628
Luca Stefani776be462020-09-09 15:53:58 +02001629 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitao85ddca62020-04-21 23:03:20 +08001630 do
1631 # If OTA is block based, extract it.
dianlujitaoe2cbe262020-04-21 23:01:13 +08001632 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1633 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1634 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1635 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1636 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001637 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1638 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
1639 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
1640 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1641 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
Chirayu Desai62ed12a2021-11-26 05:47:25 +05301642 extract_img_data "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/"$PARTITION"/
1643 rm "$DUMPDIR"/"$PARTITION".img
dianlujitao85ddca62020-04-21 23:03:20 +08001644 fi
1645 done
Dan Pasanen0cc05012017-03-21 09:06:11 -05001646 fi
1647
1648 SRC="$DUMPDIR"
1649 fi
1650
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301651 if [ -d "$SRC" ] && [ -f "$SRC"/system.img ]; then
1652 DUMPDIR="$TMPDIR"/system_dump
1653 mkdir -p "$DUMPDIR"
1654
1655 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
1656 do
1657 echo "Extracting "$PARTITION""
1658 local IMAGE="$SRC"/"$PARTITION".img
1659 if [ -f "$IMAGE" ]; then
1660 if [[ $(file -b "$IMAGE") == Linux* ]]; then
1661 extract_img_data "$IMAGE" "$DUMPDIR"/"$PARTITION"
1662 elif [[ $(file -b "$IMAGE") == Android* ]]; then
1663 simg2img "$IMAGE" "$DUMPDIR"/"$PARTITION".raw
1664 extract_img_data "$DUMPDIR"/"$PARTITION".raw "$DUMPDIR"/"$PARTITION"/
1665 else
1666 echo "Unsupported "$IMAGE""
1667 fi
1668 fi
1669 done
1670
1671 SRC="$DUMPDIR"
1672 fi
1673
Steve Kondik5bd66602016-07-15 10:39:58 -07001674 if [ "$VENDOR_STATE" -eq "0" ]; then
1675 echo "Cleaning output directory ($OUTPUT_ROOT).."
1676 rm -rf "${OUTPUT_TMP:?}"
1677 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001678 if [ -d "$OUTPUT_ROOT" ]; then
1679 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1680 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001681 VENDOR_STATE=1
1682 fi
1683
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001684 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001685
1686 for (( i=1; i<COUNT+1; i++ )); do
1687
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001688 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001689 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001690 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001691 local OUTPUT_DIR=
1692 local TMP_DIR=
1693 local SRC_FILE=
1694 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001695 local IS_PRODUCT_PACKAGE=false
1696
1697 # Note: this relies on the fact that the ${FILELIST[@]} array
1698 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1699 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1700 IS_PRODUCT_PACKAGE=true
1701 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001702
Michael Bestasbda30202020-12-28 04:44:52 +02001703 OUTPUT_DIR="${OUTPUT_ROOT}"
1704 TMP_DIR="${OUTPUT_TMP}"
1705 SRC_FILE="/system/${SPEC_SRC_FILE}"
1706 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001707
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001708 # Strip the file path in the vendor repo of "system", if present
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001709 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitao4ddcfb72020-04-06 12:43:16 +08001710 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001711 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001712
Gabriele M58270a32017-11-13 23:15:29 +01001713 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001714 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001715 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001716 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001717 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001718 if [ -f "${VENDOR_REPO_FILE}" ]; then
1719 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001720 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001721 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001722 fi
1723 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001724 local TMP_HASH=$(get_hash "${PINNED}")
1725 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001726 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001727 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1728 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001729 fi
1730 fi
1731 fi
1732 fi
1733
Vladimir Olteana7d20492019-01-17 03:05:52 +02001734 if [ "${KANG}" = false ]; then
1735 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1736 fi
1737
Gabriele M58270a32017-11-13 23:15:29 +01001738 if [ "$KEEP" = "1" ]; then
Arian2d802382021-09-09 15:18:35 +02001739 if [ "${FIXUP_HASH}" != "x" ]; then
1740 printf ' + keeping pinned file with hash %s\n' "${FIXUP_HASH}"
1741 else
1742 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1743 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001744 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001745 FOUND=false
1746 # Try Lineage target first.
1747 # Also try to search for files stripped of
1748 # the "/system" prefix, if we're actually extracting
1749 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001750 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001751 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1752 FOUND=true
1753 break
1754 }
1755 done
1756
1757 if [ "${FOUND}" = false ]; then
Bruno Martins74e00eb2021-04-10 14:36:50 +01001758 colored_echo red " !! ${BLOB_DISPLAY_NAME}: file not found in source"
Vladimir Oltean11329372018-10-18 00:44:02 +03001759 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001760 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001761
Arian5f98d792021-09-09 15:24:25 +02001762 # Blob fixup pipeline has 2 parts: one that is fixed and
1763 # one that is user-configurable
1764 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1765 # Deodex apk|jar if that's the case
1766 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1767 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1768 if [ -f "$TMPDIR/classes.dex" ]; then
1769 touch -t 200901010000 "$TMPDIR/classes"*
1770 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1771 rm "$TMPDIR/classes"*
1772 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1773 fi
1774 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1775 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001776 fi
Arian5f98d792021-09-09 15:24:25 +02001777 # Now run user-supplied fixup function
1778 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1779 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001780
Arian5f98d792021-09-09 15:24:25 +02001781 if [ -f "${VENDOR_REPO_FILE}" ]; then
1782 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1783 local TYPE="${DIR##*/}"
Michael Bestasbda30202020-12-28 04:44:52 +02001784 if [ "$TYPE" = "bin" ]; then
Arian5f98d792021-09-09 15:24:25 +02001785 chmod 755 "${VENDOR_REPO_FILE}"
1786 else
1787 chmod 644 "${VENDOR_REPO_FILE}"
1788 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001789 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001790
Arian5f98d792021-09-09 15:24:25 +02001791 if [ "${KANG}" = true ]; then
1792 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1793 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001794
Arian5f98d792021-09-09 15:24:25 +02001795 # Check and print whether the fixup pipeline actually did anything.
1796 # This isn't done right after the fixup pipeline because we want this print
1797 # to come after print_spec above, when in kang mode.
1798 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1799 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1800 # Now sanity-check the spec for this blob.
1801 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1802 colored_echo yellow "WARNING: The ${BLOB_DISPLAY_NAME} file was fixed up, but it is pinned."
1803 colored_echo yellow "This is a mistake and you want to either remove the hash completely, or add an extra one."
1804 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001805 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001806 fi
1807
Steve Kondik5bd66602016-07-15 10:39:58 -07001808 done
1809
1810 # Don't allow failing
1811 set -e
1812}
1813
1814#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001815# extract2:
1816#
1817# Positional parameters:
1818# $1: file containing the list of items to extract (aka proprietary-files.txt)
1819#
1820# Non-positional parameters (coming after $2):
1821# --section: selects the portion to parse and extracts from proprietary-files.txt
1822# --kang: if present, this option will activate the printing of hashes for the
1823# extracted blobs. Useful with --section for subsequent pinning of
1824# blobs taken from other origins.
1825#
1826function extract2() {
1827 # Consume positional parameters
1828 local PROPRIETARY_FILES_TXT="$1"; shift
1829 local SECTION=""
1830 local KANG=false
1831
1832 # Consume optional, non-positional parameters
1833 while [ "$#" -gt 0 ]; do
1834 case "$1" in
1835 --adb)
1836 ADB=true
1837 ;;
1838 --system)
1839 SYSTEM_SRC="$2"; shift
1840 ;;
1841 --vendor)
1842 VENDOR_SRC="$2"; shift
1843 ;;
1844 --odm)
1845 ODM_SRC="$2"; shift
1846 ;;
1847 --product)
1848 PRODUCT_SRC="$2"; shift
1849 ;;
1850 -s|--section)
1851 SECTION="$2"; shift
1852 ;;
1853 -k|--kang)
1854 KANG=true
1855 DISABLE_PINNING=1
1856 ;;
1857 esac
1858 shift
1859 done
1860
1861 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1862 echo "No sources set! You must select --adb or pass paths to partition dumps."
1863 exit 1
1864 fi
1865
1866 if [ -z "$OUTDIR" ]; then
1867 echo "Output dir not set!"
1868 exit 1
1869 fi
1870
1871 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1872
1873 # Allow failing, so we can try $DEST and/or $FILE
1874 set +e
1875
1876 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1877 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1878 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1879 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1880 local COUNT=${#FILELIST[@]}
1881 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
1882 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1883
1884 if [ "$ADB" = true ]; then
1885 init_adb_connection
1886 fi
1887
1888 if [ "$VENDOR_STATE" -eq "0" ]; then
1889 echo "Cleaning output directory ($OUTPUT_ROOT).."
1890 rm -rf "${OUTPUT_TMP:?}"
1891 mkdir -p "${OUTPUT_TMP:?}"
1892 if [ -d "$OUTPUT_ROOT" ]; then
1893 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1894 fi
1895 VENDOR_STATE=1
1896 fi
1897
1898 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1899
1900 for (( i=1; i<COUNT+1; i++ )); do
1901
1902 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1903 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1904 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1905 local OUTPUT_DIR=
1906 local TMP_DIR=
1907 local SRC_FILE=
1908 local DST_FILE=
1909 local IS_PRODUCT_PACKAGE=false
1910
1911 # Note: this relies on the fact that the ${FILELIST[@]} array
1912 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1913 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1914 IS_PRODUCT_PACKAGE=true
1915 fi
1916
1917 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1918 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1919 TMP_DIR="${OUTPUT_TMP}/rootfs"
1920 else
1921 OUTPUT_DIR="${OUTPUT_ROOT}"
1922 TMP_DIR="${OUTPUT_TMP}"
1923 fi
1924 SRC_FILE="${SPEC_SRC_FILE}"
1925 DST_FILE="${SPEC_DST_FILE}"
1926
1927 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1928 local BLOB_DISPLAY_NAME="${DST_FILE}"
1929 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1930
1931 # Check pinned files
1932 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1933 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1934 local KEEP=""
1935 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
1936 if [ -f "${VENDOR_REPO_FILE}" ]; then
1937 local PINNED="${VENDOR_REPO_FILE}"
1938 else
1939 local PINNED="${TMP_DIR}${DST_FILE}"
1940 fi
1941 if [ -f "$PINNED" ]; then
1942 local TMP_HASH=$(get_hash "${PINNED}")
1943 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
1944 KEEP="1"
1945 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1946 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
1947 fi
1948 fi
1949 fi
1950 fi
1951
1952 if [ "${KANG}" = false ]; then
1953 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1954 fi
1955
1956 if [ "$KEEP" = "1" ]; then
1957 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1958 else
1959 FOUND=false
1960 PARTITION_SOURCE_DIR=
1961 # Try Lineage target first.
1962 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
1963 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
1964 if [ "$PARTITION" = "system" ]; then
1965 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
1966 elif [ "$PARTITION" = "vendor" ]; then
1967 PARTITION_SOURCE_DIR="$VENDOR_SRC"
1968 elif [ "$PARTITION" = "product" ]; then
1969 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
1970 elif [ "$PARTITION" = "odm" ]; then
1971 PARTITION_SOURCE_DIR="$ODM_SRC"
1972 fi
1973 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
1974 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1975 FOUND=true
1976 break
1977 }
1978 # Search with the full system/ prefix if the file was not found on the system partition
1979 # because we may be searching in a mounted system-as-root system.img
1980 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
1981 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1982 FOUND=true
1983 break
1984 }
1985 fi
1986 done
1987
1988 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
1989 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
1990 fi
1991
1992 if [ "${FOUND}" = false ]; then
1993 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
1994 continue
1995 fi
1996 fi
1997
1998 # Blob fixup pipeline has 2 parts: one that is fixed and
1999 # one that is user-configurable
2000 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2001 # Deodex apk|jar if that's the case
2002 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
2003 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
2004 if [ -f "$TMPDIR/classes.dex" ]; then
2005 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
2006 rm "$TMPDIR/classes"*
2007 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
2008 fi
2009 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
2010 fix_xml "${VENDOR_REPO_FILE}"
2011 fi
2012 # Now run user-supplied fixup function
2013 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
2014 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2015
2016 if [ -f "${VENDOR_REPO_FILE}" ]; then
2017 local DIR=$(dirname "${VENDOR_REPO_FILE}")
2018 local TYPE="${DIR##*/}"
2019 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
2020 chmod 755 "${VENDOR_REPO_FILE}"
2021 else
2022 chmod 644 "${VENDOR_REPO_FILE}"
2023 fi
2024 fi
2025
2026 if [ "${KANG}" = true ]; then
2027 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
2028 fi
2029
2030 # Check and print whether the fixup pipeline actually did anything.
2031 # This isn't done right after the fixup pipeline because we want this print
2032 # to come after print_spec above, when in kang mode.
2033 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
2034 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
2035 # Now sanity-check the spec for this blob.
2036 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
2037 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
2038 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
2039 fi
2040 fi
2041
2042 done
2043
2044 # Don't allow failing
2045 set -e
2046}
2047
2048#
Steve Kondik5bd66602016-07-15 10:39:58 -07002049# extract_firmware:
2050#
2051# $1: file containing the list of items to extract
2052# $2: path to extracted radio folder
2053#
2054function extract_firmware() {
2055 if [ -z "$OUTDIR" ]; then
2056 echo "Output dir not set!"
2057 exit 1
2058 fi
2059
2060 parse_file_list "$1"
2061
2062 # Don't allow failing
2063 set -e
2064
2065 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
2066 local COUNT=${#FILELIST[@]}
2067 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00002068 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07002069
2070 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
2071 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
2072 rm -rf "${OUTPUT_DIR:?}/"*
2073 VENDOR_RADIO_STATE=1
2074 fi
2075
2076 echo "Extracting $COUNT files in $1 from $SRC:"
2077
2078 for (( i=1; i<COUNT+1; i++ )); do
2079 local FILE="${FILELIST[$i-1]}"
2080 printf ' - %s \n' "/radio/$FILE"
2081
2082 if [ ! -d "$OUTPUT_DIR" ]; then
2083 mkdir -p "$OUTPUT_DIR"
2084 fi
2085 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
2086 chmod 644 "$OUTPUT_DIR/$FILE"
2087 done
2088}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002089
2090function extract_img_data() {
2091 local image_file="$1"
2092 local out_dir="$2"
2093 local logFile="$TMPDIR/debugfs.log"
2094
2095 if [ ! -d "$out_dir" ]; then
2096 mkdir -p "$out_dir"
2097 fi
2098
2099 if [[ "$HOST_OS" == "Darwin" ]]; then
2100 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
2101 echo "[-] Failed to extract data from '$image_file'"
2102 abort 1
2103 }
2104 else
2105 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
2106 do
2107 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
2108 echo "[-] Failed to extract data from '$image_file'"
2109 abort 1
2110 }
2111 done
2112 fi
2113
2114 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
2115 if grep -Fq "$symlink_err" "$logFile"; then
2116 echo "[-] Symlinks have not been properly processed from $image_file"
2117 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
2118 abort 1
2119 fi
2120}
2121
2122declare -ra VENDOR_SKIP_FILES=(
2123 "bin/toybox_vendor"
2124 "bin/toolbox"
2125 "bin/grep"
2126 "build.prop"
2127 "compatibility_matrix.xml"
2128 "default.prop"
2129 "etc/NOTICE.xml.gz"
2130 "etc/vintf/compatibility_matrix.xml"
2131 "etc/vintf/manifest.xml"
2132 "etc/wifi/wpa_supplicant.conf"
2133 "manifest.xml"
2134 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
2135 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
2136 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
2137 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
2138 "overlay/framework-res__auto_generated_rro.apk"
2139 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
2140)
2141
2142function array_contains() {
2143 local element
2144 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
2145 return 1
2146}
2147
2148function generate_prop_list_from_image() {
2149 local image_file="$1"
2150 local image_dir="$TMPDIR/image-temp"
2151 local output_list="$2"
2152 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
Michael Bestasca5d78a2021-12-02 20:58:40 +02002153 local -n skipped_files="$3"
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002154 local component="$4"
2155 local partition="$component"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002156
Chirayu Desai203cc522021-12-04 01:18:45 +05302157 mkdir -p "$image_dir"
2158
2159 if [[ $(file -b "$image_file") == Linux* ]]; then
2160 extract_img_data "$image_file" "$image_dir"
2161 elif [[ $(file -b "$image_file") == Android* ]]; then
2162 simg2img "$image_file" "$image_dir"/"$(basename "$image_file").raw"
2163 extract_img_data "$image_dir"/"$(basename "$image_file").raw" "$image_dir"
2164 rm "$image_dir"/"$(basename "$image_file").raw"
2165 else
2166 echo "Unsupported "$image_file""
2167 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002168
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002169 if [ -z "$component" ]; then
2170 partition="vendor"
2171 elif [[ "$component" == "carriersettings" ]]; then
2172 partition="product"
2173 fi
2174
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002175 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
2176 do
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002177 if [[ "$component" == "carriersettings" ]] && ! prefix_match_file "etc/CarrierSettings" "$FILE" ; then
2178 continue
2179 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002180 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
2181 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
2182 continue
2183 fi
2184 # Skip device defined skipped files since they will be re-generated at build time
Michael Bestasca5d78a2021-12-02 20:58:40 +02002185 if array_contains "$FILE" "${skipped_files[@]}"; then
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002186 continue
2187 fi
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002188 echo "$partition/$FILE" >> "$output_list_tmp"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002189 done
2190
2191 # Sort merged file with all lists
2192 sort -u "$output_list_tmp" > "$output_list"
2193
2194 # Clean-up
2195 rm -f "$output_list_tmp"
2196}
Bruno Martins0f425f12021-04-10 14:57:32 +01002197
2198function colored_echo() {
2199 IFS=" "
2200 local color=$1;
2201 shift
2202 if ! [[ $color =~ '^[0-9]$' ]] ; then
2203 case $(echo $color | tr '[:upper:]' '[:lower:]') in
2204 black) color=0 ;;
2205 red) color=1 ;;
2206 green) color=2 ;;
2207 yellow) color=3 ;;
2208 blue) color=4 ;;
2209 magenta) color=5 ;;
2210 cyan) color=6 ;;
2211 white|*) color=7 ;; # white or invalid color
2212 esac
2213 fi
Bruno Martins5064db22021-06-21 14:47:40 +01002214 if [ -t 1 ] ; then tput setaf $color; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002215 printf '%s\n' "$*"
Bruno Martins5064db22021-06-21 14:47:40 +01002216 if [ -t 1 ] ; then tput sgr0; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002217}