blob: ce5ae24e4d75901e0d599840e8c552c043c8c236 [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
Bruno Martinsbe7f3cb2024-06-23 15:54:02 +0100813 FILE=$(target_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")
Bruno Martinsad05f512024-06-28 00:34:33 +0100828 PKGNAME="${BASENAME%.*}_${SYMLINK_BASENAME%.*}_symlink${ARCH}"
829 if [[ "${SYMLINK_PACKAGES[@]}" =~ "$PKGNAME" ]]; then
830 PKGNAME+="_$(grep -o "$PKGNAME" <<< ${SYMLINK_PACKAGES[*]} | wc -l)"
831 fi
SamarV-1215556e2d2024-03-19 08:50:02 +0530832 {
833 printf 'install_symlink {\n'
834 printf '\tname: "%s",\n' "$PKGNAME"
Mashopy06100c92024-04-23 21:52:04 +0200835 if prefix_match_file "vendor/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530836 PREFIX='vendor/'
837 printf '\tsoc_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200838 elif prefix_match_file "product/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530839 PREFIX='product/'
840 printf '\tproduct_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200841 elif prefix_match_file "system_ext/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530842 PREFIX='system_ext/'
843 printf '\tsystem_ext_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200844 elif prefix_match_file "odm/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530845 PREFIX='odm/'
846 printf '\tdevice_specific: true,\n'
847 fi
848 printf '\tinstalled_location: "%s",\n' "${SYMLINK#"$PREFIX"}"
849 printf '\tsymlink_target: "/%s",\n' "$FILE"
850 printf '}\n\n'
851 } >> "$ANDROIDBP"
852 SYMLINK_PACKAGES+=("$PKGNAME")
853 done
854 fi
855 done
Steve Kondik5bd66602016-07-15 10:39:58 -0700856 done
SamarV-1215556e2d2024-03-19 08:50:02 +0530857
858 write_package_definition "${SYMLINK_PACKAGES[@]}" >> "$PRODUCTMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700859}
860
861#
Michael Bestasfe71eb32023-06-11 18:59:10 +0300862# write_single_product_copy_files:
863#
864# $1: the file to be copied
865#
866# Creates a PRODUCT_COPY_FILES section in the product makefile for the
867# item provided in $1.
868#
869function write_single_product_copy_files() {
870 local FILE="$1"
871 if [ -z "$FILE" ]; then
872 echo "A file must be provided to write_single_product_copy_files()!"
873 exit 1
874 fi
875
876 local TARGET=$(target_file "$FILE")
877 local OUTTARGET=$(truncate_file $TARGET)
878
879 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
880 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s\n' \
881 "$OUTDIR" "$TARGET" "$OUTTARGET" >> "$PRODUCTMK"
882}
883
884#
885# write_single_product_packages:
886#
887# $1: the package to be built
888#
889# Creates a PRODUCT_PACKAGES section in the product makefile for the
890# item provided in $1.
891#
892function write_single_product_packages() {
893 local PACKAGE="$1"
894 if [ -z "$PACKAGE" ]; then
895 echo "A package must be provided to write_single_product_packages()!"
896 exit 1
897 fi
898
899 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
900 printf ' %s%s\n' "$PACKAGE" >> "$PRODUCTMK"
901}
902
903#
Michael Bestas431a8002023-06-11 20:04:45 +0300904# write_rro_androidmanifest:
905#
906# $2: target package for the RRO overlay
907#
908# Creates an AndroidManifest.xml for an RRO overlay.
909#
910function write_rro_androidmanifest() {
911 local TARGET_PACKAGE="$1"
912
913 cat << EOF
914<manifest xmlns:android="http://schemas.android.com/apk/res/android"
915 package="$TARGET_PACKAGE.vendor"
916 android:versionCode="1"
917 android:versionName="1.0">
918 <application android:hasCode="false" />
919 <overlay
920 android:targetPackage="$TARGET_PACKAGE"
921 android:isStatic="true"
922 android:priority="0"/>
923</manifest>
924EOF
925}
926
927#
928# write_rro_blueprint:
929#
930# $1: package name for the RRO overlay
931# $2: target partition for the RRO overlay
932#
933# Creates an Android.bp for an RRO overlay.
934#
935function write_rro_blueprint() {
936 local PKGNAME="$1"
937 local PARTITION="$2"
938
939 printf 'runtime_resource_overlay {\n'
940 printf '\tname: "%s",\n' "$PKGNAME"
941 printf '\ttheme: "%s",\n' "$PKGNAME"
942 printf '\tsdk_version: "%s",\n' "current"
943 printf '\taaptflags: ["%s"],\n' "--keep-raw-values"
944
945 if [ "$PARTITION" = "vendor" ]; then
946 printf '\tsoc_specific: true,\n'
947 elif [ "$PARTITION" = "product" ]; then
948 printf '\tproduct_specific: true,\n'
949 elif [ "$PARTITION" = "system_ext" ]; then
950 printf '\tsystem_ext_specific: true,\n'
951 elif [ "$PARTITION" = "odm" ]; then
952 printf '\tdevice_specific: true,\n'
953 fi
954 printf '}\n'
955}
956
957#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700958# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -0700959#
960# $1: file which will be written to
961#
Michael Bestasa2934df2020-12-19 03:50:32 +0200962# writes out the warning message regarding manual file modifications.
Steve Kondik5bd66602016-07-15 10:39:58 -0700963# note that this is not an append operation, and should
964# be executed first!
965#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700966function write_blueprint_header() {
967 if [ -f $1 ]; then
968 rm $1
969 fi
970
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700971 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
972
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700973 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +0200974// Automatically generated file. DO NOT MODIFY
975//
976// This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700977
978EOF
979}
980
981#
982# write_makefile_header:
983#
984# $1: file which will be written to
985#
Michael Bestasa2934df2020-12-19 03:50:32 +0200986# writes out the warning message regarding manual file modifications.
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700987# note that this is not an append operation, and should
988# be executed first!
989#
990function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500991 if [ -f $1 ]; then
992 rm $1
993 fi
994
Steve Kondik5bd66602016-07-15 10:39:58 -0700995 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
996
Jake Whatley9843b322017-01-25 21:49:16 -0500997 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +0200998# Automatically generated file. DO NOT MODIFY
Steve Kondik5bd66602016-07-15 10:39:58 -0700999#
Steve Kondik5bd66602016-07-15 10:39:58 -07001000# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1001
1002EOF
1003}
1004
1005#
Michael Bestas431a8002023-06-11 20:04:45 +03001006# write_xml_header:
1007#
1008# $1: file which will be written to
1009#
1010# writes out the warning message regarding manual file modifications.
1011# note that this is not an append operation, and should
1012# be executed first!
1013#
1014function write_xml_header() {
1015 if [ -f $1 ]; then
1016 rm $1
1017 fi
1018
1019 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1020 [ "$COMMON" -eq 1 ] && local VENDOR="${VENDOR_COMMON:-$VENDOR}"
1021
1022 cat << EOF >> $1
1023<?xml version="1.0" encoding="utf-8"?>
1024<!--
1025 Automatically generated file. DO NOT MODIFY
1026
1027 This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1028-->
1029EOF
1030}
1031
1032#
1033# write_rro_package:
1034#
1035# $1: the RRO package name
1036# $2: the RRO target package
1037# $3: the partition for the RRO overlay
1038#
1039# Generates the file structure for an RRO overlay.
1040#
1041function write_rro_package() {
1042 local PKGNAME="$1"
1043 if [ -z "$PKGNAME" ]; then
1044 echo "A package name must be provided to write_rro_package()!"
1045 exit 1
1046 fi
1047
1048 local TARGET_PACKAGE="$2"
1049 if [ -z "$TARGET_PACKAGE" ]; then
1050 echo "A target package must be provided to write_rro_package()!"
1051 exit 1
1052 fi
1053
1054 local PARTITION="$3"
1055 if [ -z "$PARTITION" ]; then
1056 PARTITION="vendor"
1057 fi
1058
1059 local RROBP="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/Android.bp
1060 local RROMANIFEST="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/AndroidManifest.xml
1061
1062 write_blueprint_header "$RROBP"
1063 write_xml_header "$RROMANIFEST"
1064
1065 write_rro_blueprint "$PKGNAME" "$PARTITION" >> "$RROBP"
1066 write_rro_androidmanifest "$TARGET_PACKAGE" >> "$RROMANIFEST"
1067}
1068
1069#
SamarV-1215556e2d2024-03-19 08:50:02 +05301070# write_package_definition:
1071#
1072# $@: list of packages
1073#
1074# writes out the final PRODUCT_PACKAGES list
1075#
1076function write_package_definition() {
1077 local PACKAGE_LIST=("${@}")
1078 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
1079
1080 if [ "$PACKAGE_COUNT" -eq "0" ]; then
1081 return 0
1082 fi
1083
1084 printf '\n%s\n' "PRODUCT_PACKAGES += \\"
1085 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
SamarV-1217e6b4082024-02-26 14:39:34 +05301086 local SKIP=false
SamarV-1215556e2d2024-03-19 08:50:02 +05301087 local LINEEND=" \\"
1088 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
1089 LINEEND=""
1090 fi
SamarV-1217e6b4082024-02-26 14:39:34 +05301091 for PKG in $(tr "," "\n" <<< "$REQUIRED_PACKAGES_LIST"); do
1092 if [[ $PKG == "${PACKAGE_LIST[$i - 1]}" ]]; then
1093 SKIP=true
1094 break
1095 fi
1096 done
1097 # Skip adding of the package to product makefile if it's in the required list
1098 if [[ $SKIP == false ]]; then
1099 printf ' %s%s\n' "${PACKAGE_LIST[$i - 1]}" "$LINEEND" >> "$PRODUCTMK"
1100 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301101 done
1102}
1103
1104#
Steve Kondik5bd66602016-07-15 10:39:58 -07001105# write_headers:
1106#
1107# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -05001108# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001109#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001110# Calls write_makefile_header for each of the makefiles and
1111# write_blueprint_header for Android.bp and creates the initial
1112# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -07001113#
1114function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001115 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001116
1117 GUARD="$2"
1118 if [ -z "$GUARD" ]; then
1119 GUARD="TARGET_DEVICE"
1120 fi
1121
Steve Kondik5bd66602016-07-15 10:39:58 -07001122 cat << EOF >> "$ANDROIDMK"
1123LOCAL_PATH := \$(call my-dir)
1124
1125EOF
1126 if [ "$COMMON" -ne 1 ]; then
1127 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001128ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -07001129
1130EOF
1131 else
1132 if [ -z "$1" ]; then
1133 echo "Argument with devices to be added to guard must be set!"
1134 exit 1
1135 fi
1136 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001137ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -07001138
1139EOF
1140 fi
1141
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001142 write_makefile_header "$BOARDMK"
1143 write_makefile_header "$PRODUCTMK"
1144 write_blueprint_header "$ANDROIDBP"
1145
1146 cat << EOF >> "$ANDROIDBP"
1147soong_namespace {
1148}
1149
1150EOF
1151
1152 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1153 cat << EOF >> "$PRODUCTMK"
1154PRODUCT_SOONG_NAMESPACES += \\
1155 vendor/$VENDOR/$DEVICE
1156
1157EOF
Steve Kondik5bd66602016-07-15 10:39:58 -07001158}
1159
1160#
1161# write_footers:
1162#
1163# Closes the inital guard and any other finalization tasks. Must
1164# be called as the final step.
1165#
1166function write_footers() {
1167 cat << EOF >> "$ANDROIDMK"
1168endif
1169EOF
1170}
1171
1172# Return success if adb is up and not in recovery
1173function _adb_connected {
1174 {
Jake Whatley9843b322017-01-25 21:49:16 -05001175 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -07001176 then
1177 return 0
1178 fi
1179 } 2>/dev/null
1180
1181 return 1
1182};
1183
1184#
1185# parse_file_list:
1186#
1187# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001188# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001189#
1190# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
1191#
1192function parse_file_list() {
1193 if [ -z "$1" ]; then
1194 echo "An input file is expected!"
1195 exit 1
1196 elif [ ! -f "$1" ]; then
1197 echo "Input file "$1" does not exist!"
1198 exit 1
1199 fi
1200
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001201 if [ -n "$2" ]; then
1202 echo "Using section \"$2\""
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001203 LIST=$TMPDIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +02001204 # Match all lines starting with first line found to start* with '#'
1205 # comment and contain** $2, and ending with first line to be empty*.
1206 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1207 # **the $2 match is case-insensitive
1208 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001209 else
1210 LIST=$1
1211 fi
1212
Steve Kondik5bd66602016-07-15 10:39:58 -07001213 PRODUCT_PACKAGES_LIST=()
1214 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001215 PRODUCT_PACKAGES_FIXUP_HASHES=()
SamarV-1215556e2d2024-03-19 08:50:02 +05301216 PRODUCT_SYMLINKS_LIST=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001217 PRODUCT_COPY_FILES_LIST=()
1218 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001219 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001220
1221 while read -r line; do
1222 if [ -z "$line" ]; then continue; fi
1223
1224 # If the line has a pipe delimiter, a sha1 hash should follow.
1225 # This indicates the file should be pinned and not overwritten
1226 # when extracting files.
1227 local SPLIT=(${line//\|/ })
1228 local COUNT=${#SPLIT[@]}
1229 local SPEC=${SPLIT[0]}
1230 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001231 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -07001232 if [ "$COUNT" -gt "1" ]; then
1233 HASH=${SPLIT[1]}
1234 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001235 if [ "$COUNT" -gt "2" ]; then
1236 FIXUP_HASH=${SPLIT[2]}
1237 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301238 if [[ "$SPEC" =~ 'SYMLINK=' ]]; then
1239 PRODUCT_SYMLINKS_LIST+=("${SPEC#-}")
1240 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001241 # if line starts with a dash, it needs to be packaged
1242 if [[ "$SPEC" =~ ^- ]]; then
1243 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1244 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001245 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Michael Bestasea90aef2021-11-15 22:18:04 +02001246 # if line contains apk, jar or vintf fragment, it needs to be packaged
1247 elif suffix_match_file ".apk" "$(src_file "$SPEC")" || \
1248 suffix_match_file ".jar" "$(src_file "$SPEC")" || \
1249 [[ "$SPEC" == *"etc/vintf/manifest/"* ]]; then
1250 PRODUCT_PACKAGES_LIST+=("$SPEC")
1251 PRODUCT_PACKAGES_HASHES+=("$HASH")
1252 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001253 else
1254 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1255 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001256 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001257 fi
1258
Chirayu Desaif1a21302022-09-13 00:29:33 +05301259 done < <(grep -v -E '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -07001260}
1261
1262#
1263# write_makefiles:
1264#
1265# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001266# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001267#
SamarV-1215556e2d2024-03-19 08:50:02 +05301268# Calls write_product_copy_files, write_product_packages and
1269# lastly write_symlink_packages on the given file and appends
1270# to the Android.bp as well as the product makefile.
Steve Kondik5bd66602016-07-15 10:39:58 -07001271#
1272function write_makefiles() {
1273 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001274 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -07001275 write_product_packages
SamarV-1215556e2d2024-03-19 08:50:02 +05301276 write_symlink_packages
Steve Kondik5bd66602016-07-15 10:39:58 -07001277}
1278
1279#
1280# append_firmware_calls_to_makefiles:
1281#
1282# Appends to Android.mk the calls to all images present in radio folder
1283# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
1284#
1285function append_firmware_calls_to_makefiles() {
1286 cat << EOF >> "$ANDROIDMK"
1287ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
1288
1289RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
1290\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
1291 \$(call add-radio-file,radio/\$(f)))
1292\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
1293
1294endif
1295
1296EOF
1297}
1298
1299#
1300# get_file:
1301#
1302# $1: input file
1303# $2: target file/folder
1304# $3: source of the file (can be "adb" or a local folder)
1305#
1306# Silently extracts the input file to defined target
1307# Returns success if file can be pulled from the device or found locally
1308#
1309function get_file() {
1310 local SRC="$3"
1311
1312 if [ "$SRC" = "adb" ]; then
1313 # try to pull
LuK13370f7f0d12022-08-19 21:49:56 +02001314 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1315 adb pull "${1#/system}" "$2" >/dev/null 2>&1 && return 0
1316 adb pull "system/$1" "$2" >/dev/null 2>&1 && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001317
1318 return 1
1319 else
1320 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001321 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1322 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001323 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001324
LuK1337dbb77cc2023-12-04 19:03:10 +01001325 # try /vendor/odm for devices without /odm partition
1326 [[ "$1" == /system/odm/* ]] && cp -r "$SRC/vendor/${1#/system}" "$2" 2>/dev/null && return 0
1327
Steve Kondik5bd66602016-07-15 10:39:58 -07001328 return 1
1329 fi
1330};
1331
1332#
1333# oat2dex:
1334#
1335# $1: extracted apk|jar (to check if deodex is required)
1336# $2: odexed apk|jar to deodex
1337# $3: source of the odexed apk|jar
1338#
1339# Convert apk|jar .odex in the corresposing classes.dex
1340#
1341function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001342 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001343 local OEM_TARGET="$2"
1344 local SRC="$3"
1345 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001346 local OAT=
Steve Kondik5bd66602016-07-15 10:39:58 -07001347
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001348 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
1349 export BAKSMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1350 export SMALIJAR="$OMNI_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001351 fi
1352
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001353 if [ -z "$VDEXEXTRACTOR" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001354 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001355 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001356
codeworkx85eda752018-09-23 12:36:57 +02001357 if [ -z "$CDEXCONVERTER" ]; then
Han Wang7a0b0bd2020-03-10 09:40:47 +02001358 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/${HOST}/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001359 fi
1360
Steve Kondik5bd66602016-07-15 10:39:58 -07001361 # Extract existing boot.oats to the temp folder
1362 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001363 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001364 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -05001365 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001366 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001367 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001368 else
1369 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001370 fi
1371 done
1372 fi
1373
1374 if [ -z "$ARCHES" ]; then
1375 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1376 fi
1377
theimpulson9a911af2019-08-14 03:25:12 +00001378 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001379 return;
1380 fi
1381
theimpulson9a911af2019-08-14 03:25:12 +00001382 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001383 return 0 # target apk|jar is already odexed, return
1384 fi
1385
1386 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -05001387 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001388
Joe Maplesfb3941c2018-01-05 14:51:33 -05001389 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1390 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001391
Joe Maplesfb3941c2018-01-05 14:51:33 -05001392 if get_file "$OAT" "$TMPDIR" "$SRC"; then
1393 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
1394 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001395 CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
1396 for CLASS in $CLASSES; do
1397 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1398 # Check if we have to deal with CompactDex
1399 if [[ "$CLASS" == *.cdex ]]; then
1400 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1401 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1402 else
1403 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1404 fi
1405 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001406 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001407 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
1408 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001409 fi
theimpulson9a911af2019-08-14 03:25:12 +00001410 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001411 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001412 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001413 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001414 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001415 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001416 # try to extract classes.dex from boot.vdex for frameworks jars
1417 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +02001418 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani6f92e6b2018-10-31 19:16:05 +01001419 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001420 CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
1421 for CLASS in $CLASSES; do
1422 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1423 # Check if we have to deal with CompactDex
1424 if [[ "$CLASS" == *.cdex ]]; then
1425 "$CDEXCONVERTER" "$CLASS" &>/dev/null
1426 mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
1427 else
1428 mv "$CLASS" "$TMPDIR/$NEWCLASS"
1429 fi
1430 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001431 else
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001432 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
1433 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001434 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001435 else
1436 continue
1437 fi
1438
Steve Kondik5bd66602016-07-15 10:39:58 -07001439 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001440
1441 rm -rf "$TMPDIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001442}
1443
1444#
1445# init_adb_connection:
1446#
1447# Starts adb server and waits for the device
1448#
1449function init_adb_connection() {
1450 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1451 if ! _adb_connected; then
1452 echo "No device is online. Waiting for one..."
1453 echo "Please connect USB and/or enable USB debugging"
1454 until _adb_connected; do
1455 sleep 1
1456 done
1457 echo "Device Found."
1458 fi
1459
1460 # Retrieve IP and PORT info if we're using a TCP connection
Chirayu Desaif1a21302022-09-13 00:29:33 +05301461 TCPIPPORT=$(adb devices | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
Steve Kondik5bd66602016-07-15 10:39:58 -07001462 | head -1 | awk '{print $1}')
1463 adb root &> /dev/null
1464 sleep 0.3
1465 if [ -n "$TCPIPPORT" ]; then
1466 # adb root just killed our connection
1467 # so reconnect...
1468 adb connect "$TCPIPPORT"
1469 fi
1470 adb wait-for-device &> /dev/null
1471 sleep 0.3
1472}
1473
1474#
1475# fix_xml:
1476#
1477# $1: xml file to fix
1478#
1479function fix_xml() {
1480 local XML="$1"
1481 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
1482
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001483 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1484 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001485
1486 mv "$TEMP_XML" "$XML"
1487}
1488
Vladimir Olteande985fe2019-01-17 03:07:34 +02001489function get_hash() {
1490 local FILE="$1"
1491
1492 if [ "$(uname)" == "Darwin" ]; then
1493 shasum "${FILE}" | awk '{print $1}'
1494 else
1495 sha1sum "${FILE}" | awk '{print $1}'
1496 fi
1497}
1498
Vladimir Olteana7d20492019-01-17 03:05:52 +02001499function print_spec() {
1500 local SPEC_PRODUCT_PACKAGE="$1"
1501 local SPEC_SRC_FILE="$2"
1502 local SPEC_DST_FILE="$3"
1503 local SPEC_ARGS="$4"
1504 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001505 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001506
1507 local PRODUCT_PACKAGE=""
1508 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1509 PRODUCT_PACKAGE="-"
1510 fi
1511 local SRC=""
1512 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1513 SRC="${SPEC_SRC_FILE}:"
1514 fi
1515 local DST=""
1516 if [ ! -z "${SPEC_DST_FILE}" ]; then
1517 DST="${SPEC_DST_FILE}"
1518 fi
1519 local ARGS=""
1520 if [ ! -z "${SPEC_ARGS}" ]; then
1521 ARGS=";${SPEC_ARGS}"
1522 fi
1523 local HASH=""
1524 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1525 HASH="|${SPEC_HASH}"
1526 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001527 local FIXUP_HASH=""
1528 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1529 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1530 fi
1531 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1532}
1533
1534# To be overridden by device-level extract-files.sh
1535# Parameters:
1536# $1: spec name of a blob. Can be used for filtering.
1537# If the spec is "src:dest", then $1 is "dest".
1538# If the spec is "src", then $1 is "src".
1539# $2: path to blob file. Can be used for fixups.
1540#
1541function blob_fixup() {
1542 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001543}
1544
Steve Kondik5bd66602016-07-15 10:39:58 -07001545#
1546# extract:
1547#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001548# Positional parameters:
1549# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -05001550# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001551# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1552#
1553# Non-positional parameters (coming after $2):
1554# --section: preferred way of selecting the portion to parse and extract from
1555# proprietary-files.txt
Vladimir Olteana7d20492019-01-17 03:05:52 +02001556# --kang: if present, this option will activate the printing of hashes for the
1557# extracted blobs. Useful with --section for subsequent pinning of
1558# blobs taken from other origins.
Steve Kondik5bd66602016-07-15 10:39:58 -07001559#
1560function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001561 # Consume positional parameters
1562 local PROPRIETARY_FILES_TXT="$1"; shift
1563 local SRC="$1"; shift
1564 local SECTION=""
Vladimir Olteana7d20492019-01-17 03:05:52 +02001565 local KANG=false
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001566
1567 # Consume optional, non-positional parameters
1568 while [ "$#" -gt 0 ]; do
1569 case "$1" in
1570 -s|--section)
1571 SECTION="$2"; shift
1572 ;;
Vladimir Olteana7d20492019-01-17 03:05:52 +02001573 -k|--kang)
1574 KANG=true
1575 DISABLE_PINNING=1
1576 ;;
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001577 *)
1578 # Backwards-compatibility with the old behavior, where $3, if
1579 # present, denoted an optional positional ${SECTION} argument.
1580 # Users of ${SECTION} are encouraged to migrate from setting it as
1581 # positional $3, to non-positional --section ${SECTION}, the
1582 # reason being that it doesn't scale to have more than 1 optional
1583 # positional argument.
1584 SECTION="$1"
1585 ;;
1586 esac
1587 shift
1588 done
1589
Steve Kondik5bd66602016-07-15 10:39:58 -07001590 if [ -z "$OUTDIR" ]; then
1591 echo "Output dir not set!"
1592 exit 1
1593 fi
1594
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001595 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001596
1597 # Allow failing, so we can try $DEST and/or $FILE
1598 set +e
1599
1600 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1601 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteande985fe2019-01-17 03:07:34 +02001602 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
Vladimir Olteana7d20492019-01-17 03:05:52 +02001603 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik5bd66602016-07-15 10:39:58 -07001604 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +00001605 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -07001606 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1607
1608 if [ "$SRC" = "adb" ]; then
1609 init_adb_connection
1610 fi
1611
Dan Pasanen0cc05012017-03-21 09:06:11 -05001612 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold9baced42017-11-10 16:33:38 +01001613 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001614
1615 # Check if we're working with the same zip that was passed last time.
1616 # If so, let's just use what's already extracted.
1617 MD5=`md5sum "$SRC"| awk '{print $1}'`
1618 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1619
1620 if [ "$MD5" != "$OLDMD5" ]; then
1621 rm -rf "$DUMPDIR"
1622 mkdir "$DUMPDIR"
1623 unzip "$SRC" -d "$DUMPDIR"
1624 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1625
1626 # Stop if an A/B OTA zip is detected. We cannot extract these.
1627 if [ -a "$DUMPDIR"/payload.bin ]; then
1628 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1629 exit 1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001630 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001631
Luca Stefani776be462020-09-09 15:53:58 +02001632 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitao85ddca62020-04-21 23:03:20 +08001633 do
1634 # If OTA is block based, extract it.
dianlujitaoe2cbe262020-04-21 23:01:13 +08001635 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1636 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1637 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1638 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1639 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001640 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1641 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
1642 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
1643 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1644 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
Chirayu Desai62ed12a2021-11-26 05:47:25 +05301645 extract_img_data "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/"$PARTITION"/
1646 rm "$DUMPDIR"/"$PARTITION".img
dianlujitao85ddca62020-04-21 23:03:20 +08001647 fi
1648 done
Dan Pasanen0cc05012017-03-21 09:06:11 -05001649 fi
1650
1651 SRC="$DUMPDIR"
1652 fi
1653
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301654 if [ -d "$SRC" ] && [ -f "$SRC"/system.img ]; then
1655 DUMPDIR="$TMPDIR"/system_dump
1656 mkdir -p "$DUMPDIR"
1657
1658 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
1659 do
1660 echo "Extracting "$PARTITION""
1661 local IMAGE="$SRC"/"$PARTITION".img
1662 if [ -f "$IMAGE" ]; then
1663 if [[ $(file -b "$IMAGE") == Linux* ]]; then
1664 extract_img_data "$IMAGE" "$DUMPDIR"/"$PARTITION"
1665 elif [[ $(file -b "$IMAGE") == Android* ]]; then
1666 simg2img "$IMAGE" "$DUMPDIR"/"$PARTITION".raw
1667 extract_img_data "$DUMPDIR"/"$PARTITION".raw "$DUMPDIR"/"$PARTITION"/
1668 else
1669 echo "Unsupported "$IMAGE""
1670 fi
1671 fi
1672 done
1673
1674 SRC="$DUMPDIR"
1675 fi
1676
Steve Kondik5bd66602016-07-15 10:39:58 -07001677 if [ "$VENDOR_STATE" -eq "0" ]; then
1678 echo "Cleaning output directory ($OUTPUT_ROOT).."
1679 rm -rf "${OUTPUT_TMP:?}"
1680 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001681 if [ -d "$OUTPUT_ROOT" ]; then
1682 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1683 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001684 VENDOR_STATE=1
1685 fi
1686
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001687 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001688
1689 for (( i=1; i<COUNT+1; i++ )); do
1690
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001691 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001692 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001693 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001694 local OUTPUT_DIR=
1695 local TMP_DIR=
1696 local SRC_FILE=
1697 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001698 local IS_PRODUCT_PACKAGE=false
1699
1700 # Note: this relies on the fact that the ${FILELIST[@]} array
1701 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1702 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1703 IS_PRODUCT_PACKAGE=true
1704 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001705
Michael Bestasbda30202020-12-28 04:44:52 +02001706 OUTPUT_DIR="${OUTPUT_ROOT}"
1707 TMP_DIR="${OUTPUT_TMP}"
1708 SRC_FILE="/system/${SPEC_SRC_FILE}"
1709 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001710
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001711 # Strip the file path in the vendor repo of "system", if present
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001712 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitao4ddcfb72020-04-06 12:43:16 +08001713 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001714 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001715
Gabriele M58270a32017-11-13 23:15:29 +01001716 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001717 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001718 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001719 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001720 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001721 if [ -f "${VENDOR_REPO_FILE}" ]; then
1722 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001723 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001724 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001725 fi
1726 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001727 local TMP_HASH=$(get_hash "${PINNED}")
1728 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001729 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001730 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1731 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001732 fi
1733 fi
1734 fi
1735 fi
1736
Vladimir Olteana7d20492019-01-17 03:05:52 +02001737 if [ "${KANG}" = false ]; then
1738 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1739 fi
1740
Gabriele M58270a32017-11-13 23:15:29 +01001741 if [ "$KEEP" = "1" ]; then
Arian2d802382021-09-09 15:18:35 +02001742 if [ "${FIXUP_HASH}" != "x" ]; then
1743 printf ' + keeping pinned file with hash %s\n' "${FIXUP_HASH}"
1744 else
1745 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1746 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001747 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001748 FOUND=false
1749 # Try Lineage target first.
1750 # Also try to search for files stripped of
1751 # the "/system" prefix, if we're actually extracting
1752 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001753 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001754 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1755 FOUND=true
1756 break
1757 }
1758 done
1759
1760 if [ "${FOUND}" = false ]; then
Bruno Martins74e00eb2021-04-10 14:36:50 +01001761 colored_echo red " !! ${BLOB_DISPLAY_NAME}: file not found in source"
Vladimir Oltean11329372018-10-18 00:44:02 +03001762 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001763 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001764
Arian5f98d792021-09-09 15:24:25 +02001765 # Blob fixup pipeline has 2 parts: one that is fixed and
1766 # one that is user-configurable
1767 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
1768 # Deodex apk|jar if that's the case
1769 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
1770 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
1771 if [ -f "$TMPDIR/classes.dex" ]; then
1772 touch -t 200901010000 "$TMPDIR/classes"*
1773 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
1774 rm "$TMPDIR/classes"*
1775 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
1776 fi
1777 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1778 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001779 fi
Arian5f98d792021-09-09 15:24:25 +02001780 # Now run user-supplied fixup function
1781 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
1782 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07001783
Arian5f98d792021-09-09 15:24:25 +02001784 if [ -f "${VENDOR_REPO_FILE}" ]; then
1785 local DIR=$(dirname "${VENDOR_REPO_FILE}")
1786 local TYPE="${DIR##*/}"
Michael Bestasbda30202020-12-28 04:44:52 +02001787 if [ "$TYPE" = "bin" ]; then
Arian5f98d792021-09-09 15:24:25 +02001788 chmod 755 "${VENDOR_REPO_FILE}"
1789 else
1790 chmod 644 "${VENDOR_REPO_FILE}"
1791 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001792 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001793
Arian5f98d792021-09-09 15:24:25 +02001794 if [ "${KANG}" = true ]; then
1795 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
1796 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001797
Arian5f98d792021-09-09 15:24:25 +02001798 # Check and print whether the fixup pipeline actually did anything.
1799 # This isn't done right after the fixup pipeline because we want this print
1800 # to come after print_spec above, when in kang mode.
1801 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
1802 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
1803 # Now sanity-check the spec for this blob.
1804 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
1805 colored_echo yellow "WARNING: The ${BLOB_DISPLAY_NAME} file was fixed up, but it is pinned."
1806 colored_echo yellow "This is a mistake and you want to either remove the hash completely, or add an extra one."
1807 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001808 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02001809 fi
1810
Steve Kondik5bd66602016-07-15 10:39:58 -07001811 done
1812
1813 # Don't allow failing
1814 set -e
1815}
1816
1817#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04001818# extract2:
1819#
1820# Positional parameters:
1821# $1: file containing the list of items to extract (aka proprietary-files.txt)
1822#
1823# Non-positional parameters (coming after $2):
1824# --section: selects the portion to parse and extracts from proprietary-files.txt
1825# --kang: if present, this option will activate the printing of hashes for the
1826# extracted blobs. Useful with --section for subsequent pinning of
1827# blobs taken from other origins.
1828#
1829function extract2() {
1830 # Consume positional parameters
1831 local PROPRIETARY_FILES_TXT="$1"; shift
1832 local SECTION=""
1833 local KANG=false
1834
1835 # Consume optional, non-positional parameters
1836 while [ "$#" -gt 0 ]; do
1837 case "$1" in
1838 --adb)
1839 ADB=true
1840 ;;
1841 --system)
1842 SYSTEM_SRC="$2"; shift
1843 ;;
1844 --vendor)
1845 VENDOR_SRC="$2"; shift
1846 ;;
1847 --odm)
1848 ODM_SRC="$2"; shift
1849 ;;
1850 --product)
1851 PRODUCT_SRC="$2"; shift
1852 ;;
1853 -s|--section)
1854 SECTION="$2"; shift
1855 ;;
1856 -k|--kang)
1857 KANG=true
1858 DISABLE_PINNING=1
1859 ;;
1860 esac
1861 shift
1862 done
1863
1864 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
1865 echo "No sources set! You must select --adb or pass paths to partition dumps."
1866 exit 1
1867 fi
1868
1869 if [ -z "$OUTDIR" ]; then
1870 echo "Output dir not set!"
1871 exit 1
1872 fi
1873
1874 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1875
1876 # Allow failing, so we can try $DEST and/or $FILE
1877 set +e
1878
1879 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1880 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1881 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1882 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1883 local COUNT=${#FILELIST[@]}
1884 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
1885 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1886
1887 if [ "$ADB" = true ]; then
1888 init_adb_connection
1889 fi
1890
1891 if [ "$VENDOR_STATE" -eq "0" ]; then
1892 echo "Cleaning output directory ($OUTPUT_ROOT).."
1893 rm -rf "${OUTPUT_TMP:?}"
1894 mkdir -p "${OUTPUT_TMP:?}"
1895 if [ -d "$OUTPUT_ROOT" ]; then
1896 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1897 fi
1898 VENDOR_STATE=1
1899 fi
1900
1901 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
1902
1903 for (( i=1; i<COUNT+1; i++ )); do
1904
1905 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
1906 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
1907 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
1908 local OUTPUT_DIR=
1909 local TMP_DIR=
1910 local SRC_FILE=
1911 local DST_FILE=
1912 local IS_PRODUCT_PACKAGE=false
1913
1914 # Note: this relies on the fact that the ${FILELIST[@]} array
1915 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1916 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1917 IS_PRODUCT_PACKAGE=true
1918 fi
1919
1920 if [ "${SPEC_ARGS}" = "rootfs" ]; then
1921 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1922 TMP_DIR="${OUTPUT_TMP}/rootfs"
1923 else
1924 OUTPUT_DIR="${OUTPUT_ROOT}"
1925 TMP_DIR="${OUTPUT_TMP}"
1926 fi
1927 SRC_FILE="${SPEC_SRC_FILE}"
1928 DST_FILE="${SPEC_DST_FILE}"
1929
1930 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
1931 local BLOB_DISPLAY_NAME="${DST_FILE}"
1932 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
1933
1934 # Check pinned files
1935 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1936 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
1937 local KEEP=""
1938 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
1939 if [ -f "${VENDOR_REPO_FILE}" ]; then
1940 local PINNED="${VENDOR_REPO_FILE}"
1941 else
1942 local PINNED="${TMP_DIR}${DST_FILE}"
1943 fi
1944 if [ -f "$PINNED" ]; then
1945 local TMP_HASH=$(get_hash "${PINNED}")
1946 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
1947 KEEP="1"
1948 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1949 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
1950 fi
1951 fi
1952 fi
1953 fi
1954
1955 if [ "${KANG}" = false ]; then
1956 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1957 fi
1958
1959 if [ "$KEEP" = "1" ]; then
1960 printf ' + keeping pinned file with hash %s\n' "${HASH}"
1961 else
1962 FOUND=false
1963 PARTITION_SOURCE_DIR=
1964 # Try Lineage target first.
1965 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
1966 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
1967 if [ "$PARTITION" = "system" ]; then
1968 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
1969 elif [ "$PARTITION" = "vendor" ]; then
1970 PARTITION_SOURCE_DIR="$VENDOR_SRC"
1971 elif [ "$PARTITION" = "product" ]; then
1972 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
1973 elif [ "$PARTITION" = "odm" ]; then
1974 PARTITION_SOURCE_DIR="$ODM_SRC"
1975 fi
1976 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
1977 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1978 FOUND=true
1979 break
1980 }
1981 # Search with the full system/ prefix if the file was not found on the system partition
1982 # because we may be searching in a mounted system-as-root system.img
1983 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
1984 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
1985 FOUND=true
1986 break
1987 }
1988 fi
1989 done
1990
1991 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
1992 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
1993 fi
1994
1995 if [ "${FOUND}" = false ]; then
1996 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
1997 continue
1998 fi
1999 fi
2000
2001 # Blob fixup pipeline has 2 parts: one that is fixed and
2002 # one that is user-configurable
2003 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2004 # Deodex apk|jar if that's the case
2005 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
2006 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
2007 if [ -f "$TMPDIR/classes.dex" ]; then
2008 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
2009 rm "$TMPDIR/classes"*
2010 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
2011 fi
2012 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
2013 fix_xml "${VENDOR_REPO_FILE}"
2014 fi
2015 # Now run user-supplied fixup function
2016 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
2017 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2018
2019 if [ -f "${VENDOR_REPO_FILE}" ]; then
2020 local DIR=$(dirname "${VENDOR_REPO_FILE}")
2021 local TYPE="${DIR##*/}"
2022 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
2023 chmod 755 "${VENDOR_REPO_FILE}"
2024 else
2025 chmod 644 "${VENDOR_REPO_FILE}"
2026 fi
2027 fi
2028
2029 if [ "${KANG}" = true ]; then
2030 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
2031 fi
2032
2033 # Check and print whether the fixup pipeline actually did anything.
2034 # This isn't done right after the fixup pipeline because we want this print
2035 # to come after print_spec above, when in kang mode.
2036 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
2037 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
2038 # Now sanity-check the spec for this blob.
2039 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
2040 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
2041 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
2042 fi
2043 fi
2044
2045 done
2046
2047 # Don't allow failing
2048 set -e
2049}
2050
2051#
Steve Kondik5bd66602016-07-15 10:39:58 -07002052# extract_firmware:
2053#
2054# $1: file containing the list of items to extract
2055# $2: path to extracted radio folder
2056#
2057function extract_firmware() {
2058 if [ -z "$OUTDIR" ]; then
2059 echo "Output dir not set!"
2060 exit 1
2061 fi
2062
2063 parse_file_list "$1"
2064
2065 # Don't allow failing
2066 set -e
2067
2068 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
2069 local COUNT=${#FILELIST[@]}
2070 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00002071 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07002072
2073 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
2074 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
2075 rm -rf "${OUTPUT_DIR:?}/"*
2076 VENDOR_RADIO_STATE=1
2077 fi
2078
2079 echo "Extracting $COUNT files in $1 from $SRC:"
2080
2081 for (( i=1; i<COUNT+1; i++ )); do
2082 local FILE="${FILELIST[$i-1]}"
2083 printf ' - %s \n' "/radio/$FILE"
2084
2085 if [ ! -d "$OUTPUT_DIR" ]; then
2086 mkdir -p "$OUTPUT_DIR"
2087 fi
2088 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
2089 chmod 644 "$OUTPUT_DIR/$FILE"
2090 done
2091}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002092
2093function extract_img_data() {
2094 local image_file="$1"
2095 local out_dir="$2"
2096 local logFile="$TMPDIR/debugfs.log"
2097
2098 if [ ! -d "$out_dir" ]; then
2099 mkdir -p "$out_dir"
2100 fi
2101
2102 if [[ "$HOST_OS" == "Darwin" ]]; then
2103 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
2104 echo "[-] Failed to extract data from '$image_file'"
2105 abort 1
2106 }
2107 else
2108 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
2109 do
2110 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
2111 echo "[-] Failed to extract data from '$image_file'"
2112 abort 1
2113 }
2114 done
2115 fi
2116
2117 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
2118 if grep -Fq "$symlink_err" "$logFile"; then
2119 echo "[-] Symlinks have not been properly processed from $image_file"
2120 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
2121 abort 1
2122 fi
2123}
2124
2125declare -ra VENDOR_SKIP_FILES=(
2126 "bin/toybox_vendor"
2127 "bin/toolbox"
2128 "bin/grep"
2129 "build.prop"
2130 "compatibility_matrix.xml"
2131 "default.prop"
2132 "etc/NOTICE.xml.gz"
2133 "etc/vintf/compatibility_matrix.xml"
2134 "etc/vintf/manifest.xml"
2135 "etc/wifi/wpa_supplicant.conf"
2136 "manifest.xml"
2137 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
2138 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
2139 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
2140 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
2141 "overlay/framework-res__auto_generated_rro.apk"
2142 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
2143)
2144
2145function array_contains() {
2146 local element
2147 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
2148 return 1
2149}
2150
2151function generate_prop_list_from_image() {
2152 local image_file="$1"
2153 local image_dir="$TMPDIR/image-temp"
2154 local output_list="$2"
2155 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
Michael Bestasca5d78a2021-12-02 20:58:40 +02002156 local -n skipped_files="$3"
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002157 local component="$4"
2158 local partition="$component"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002159
Chirayu Desai203cc522021-12-04 01:18:45 +05302160 mkdir -p "$image_dir"
2161
2162 if [[ $(file -b "$image_file") == Linux* ]]; then
2163 extract_img_data "$image_file" "$image_dir"
2164 elif [[ $(file -b "$image_file") == Android* ]]; then
2165 simg2img "$image_file" "$image_dir"/"$(basename "$image_file").raw"
2166 extract_img_data "$image_dir"/"$(basename "$image_file").raw" "$image_dir"
2167 rm "$image_dir"/"$(basename "$image_file").raw"
2168 else
2169 echo "Unsupported "$image_file""
2170 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002171
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002172 if [ -z "$component" ]; then
2173 partition="vendor"
2174 elif [[ "$component" == "carriersettings" ]]; then
2175 partition="product"
2176 fi
2177
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002178 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
2179 do
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002180 if [[ "$component" == "carriersettings" ]] && ! prefix_match_file "etc/CarrierSettings" "$FILE" ; then
2181 continue
2182 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002183 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
2184 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
2185 continue
2186 fi
2187 # Skip device defined skipped files since they will be re-generated at build time
Michael Bestasca5d78a2021-12-02 20:58:40 +02002188 if array_contains "$FILE" "${skipped_files[@]}"; then
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002189 continue
2190 fi
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002191 echo "$partition/$FILE" >> "$output_list_tmp"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002192 done
2193
2194 # Sort merged file with all lists
2195 sort -u "$output_list_tmp" > "$output_list"
2196
2197 # Clean-up
2198 rm -f "$output_list_tmp"
2199}
Bruno Martins0f425f12021-04-10 14:57:32 +01002200
2201function colored_echo() {
2202 IFS=" "
2203 local color=$1;
2204 shift
2205 if ! [[ $color =~ '^[0-9]$' ]] ; then
2206 case $(echo $color | tr '[:upper:]' '[:lower:]') in
2207 black) color=0 ;;
2208 red) color=1 ;;
2209 green) color=2 ;;
2210 yellow) color=3 ;;
2211 blue) color=4 ;;
2212 magenta) color=5 ;;
2213 cyan) color=6 ;;
2214 white|*) color=7 ;; # white or invalid color
2215 esac
2216 fi
Bruno Martins5064db22021-06-21 14:47:40 +01002217 if [ -t 1 ] ; then tput setaf $color; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002218 printf '%s\n' "$*"
Bruno Martins5064db22021-06-21 14:47:40 +01002219 if [ -t 1 ] ; then tput sgr0; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002220}