blob: 940e62d5e5748255da49e4d12cd9c031748c3f42 [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=
Michael Bestasf458ca02023-06-12 14:20:28 +030027EXTRACT_SRC=
28EXTRACT_STATE=-1
Steve Kondik5bd66602016-07-15 10:39:58 -070029VENDOR_STATE=-1
30VENDOR_RADIO_STATE=-1
31COMMON=-1
32ARCHES=
33FULLY_DEODEXED=-1
34
Michael Bestasfc97f2d2023-06-12 13:53:31 +030035KEEP_DUMP=${KEEP_DUMP:-0}
Chirayu Desai51ddd8d2022-05-26 14:50:35 +053036SKIP_CLEANUP=${SKIP_CLEANUP:-0}
Chirayu Desai5da0bf82022-10-19 02:58:42 +053037EXTRACT_TMP_DIR=$(mktemp -d)
Volodymyr Zhdanove54a1592020-10-22 01:33:24 +030038HOST="$(uname | tr '[:upper:]' '[:lower:]')"
Steve Kondik5bd66602016-07-15 10:39:58 -070039
40#
41# cleanup
42#
43# kill our tmpfiles with fire on exit
44#
45function cleanup() {
Chirayu Desai51ddd8d2022-05-26 14:50:35 +053046 if [ "$SKIP_CLEANUP" == "true" ] || [ "$SKIP_CLEANUP" == "1" ]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +053047 echo "Skipping cleanup of $EXTRACT_TMP_DIR"
Chirayu Desai51ddd8d2022-05-26 14:50:35 +053048 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +053049 rm -rf "${EXTRACT_TMP_DIR:?}"
Chirayu Desai51ddd8d2022-05-26 14:50:35 +053050 fi
Steve Kondik5bd66602016-07-15 10:39:58 -070051}
52
Gabriele Mb8e54572017-10-11 12:55:51 +020053trap cleanup 0
Steve Kondik5bd66602016-07-15 10:39:58 -070054
55#
Chirayu Desai795ce322022-04-05 03:32:25 +053056# setup_vendor_deps
57#
58# $1: Android root directory
59# Sets up common dependencies for extraction
60#
61function setup_vendor_deps() {
62 export ANDROID_ROOT="$1"
63 if [ ! -d "$ANDROID_ROOT" ]; then
64 echo "\$ANDROID_ROOT must be set and valid before including this script!"
65 exit 1
66 fi
67
68 export BINARIES_LOCATION="$ANDROID_ROOT"/vendor/omni/build/tools/${HOST}/bin
Aaron Klingb2649192023-08-26 15:04:17 -050069 export CLANG_BINUTILS="$ANDROID_ROOT"/prebuilts/clang/host/${HOST}-x86/llvm-binutils-stable
Chirayu Desai795ce322022-04-05 03:32:25 +053070
71 export SIMG2IMG="$BINARIES_LOCATION"/simg2img
72 export LPUNPACK="$BINARIES_LOCATION"/lpunpack
73 export OTA_EXTRACTOR="$BINARIES_LOCATION"/ota_extractor
74 export SIGSCAN="$BINARIES_LOCATION"/SigScan
Aaron Klingb2649192023-08-26 15:04:17 -050075 export OBJDUMP="$CLANG_BINUTILS"/llvm-objdump
Chirayu Desai795ce322022-04-05 03:32:25 +053076
77 for version in 0_8 0_9 0_17_2; do
78 export PATCHELF_${version}="$BINARIES_LOCATION"/patchelf-"${version}"
79 done
80
81 if [ -z "$PATCHELF_VERSION" ]; then
82 export PATCHELF_VERSION=0_9
83 fi
84
85 if [ -z "$PATCHELF" ]; then
86 local patchelf_variable="PATCHELF_${PATCHELF_VERSION}"
87 export PATCHELF=${!patchelf_variable}
88 fi
89}
90
91#
Steve Kondik5bd66602016-07-15 10:39:58 -070092# setup_vendor
93#
94# $1: device name
95# $2: vendor name
Michael Bestas6d908932020-12-19 03:29:25 +020096# $3: Android root directory
Steve Kondik5bd66602016-07-15 10:39:58 -070097# $4: is common device - optional, default to false
98# $5: cleanup - optional, default to true
Jake Whatley9843b322017-01-25 21:49:16 -050099# $6: custom vendor makefile name - optional, default to false
Steve Kondik5bd66602016-07-15 10:39:58 -0700100#
101# Must be called before any other functions can be used. This
102# sets up the internal state for a new vendor configuration.
103#
104function setup_vendor() {
105 local DEVICE="$1"
106 if [ -z "$DEVICE" ]; then
107 echo "\$DEVICE must be set before including this script!"
108 exit 1
109 fi
110
111 export VENDOR="$2"
112 if [ -z "$VENDOR" ]; then
113 echo "\$VENDOR must be set before including this script!"
114 exit 1
115 fi
116
Michael Bestas6d908932020-12-19 03:29:25 +0200117 export ANDROID_ROOT="$3"
118 if [ ! -d "$ANDROID_ROOT" ]; then
119 echo "\$ANDROID_ROOT must be set and valid before including this script!"
Steve Kondik5bd66602016-07-15 10:39:58 -0700120 exit 1
121 fi
122
123 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
Michael Bestas6d908932020-12-19 03:29:25 +0200124 if [ ! -d "$ANDROID_ROOT/$OUTDIR" ]; then
125 mkdir -p "$ANDROID_ROOT/$OUTDIR"
Steve Kondik5bd66602016-07-15 10:39:58 -0700126 fi
127
Jake Whatley9843b322017-01-25 21:49:16 -0500128 VNDNAME="$6"
129 if [ -z "$VNDNAME" ]; then
130 VNDNAME="$DEVICE"
131 fi
132
Michael Bestas6d908932020-12-19 03:29:25 +0200133 export PRODUCTMK="$ANDROID_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
134 export ANDROIDBP="$ANDROID_ROOT"/"$OUTDIR"/Android.bp
135 export ANDROIDMK="$ANDROID_ROOT"/"$OUTDIR"/Android.mk
Michael Bestas6d908932020-12-19 03:29:25 +0200136 export BOARDMK="$ANDROID_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -0700137
138 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
139 COMMON=1
140 else
141 COMMON=0
142 fi
143
Gabriele Mc44696d2017-05-01 18:22:04 +0200144 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700145 VENDOR_STATE=1
146 VENDOR_RADIO_STATE=1
147 else
148 VENDOR_STATE=0
149 VENDOR_RADIO_STATE=0
150 fi
Volodymyr Zhdanove54a1592020-10-22 01:33:24 +0300151
Chirayu Desai795ce322022-04-05 03:32:25 +0530152 setup_vendor_deps "$ANDROID_ROOT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700153}
154
Vladimir Oltean75d8e052018-06-24 20:22:41 +0300155# Helper functions for parsing a spec.
156# notes: an optional "|SHA1" that may appear in the format is stripped
157# early from the spec in the parse_file_list function, and
158# should not be present inside the input parameter passed
159# to these functions.
160
161#
162# input: spec in the form of "src[:dst][;args]"
163# output: "src"
164#
165function src_file() {
166 local SPEC="$1"
167 local SPLIT=(${SPEC//:/ })
168 local ARGS="$(target_args ${SPEC})"
169 # Regardless of there being a ":" delimiter or not in the spec,
170 # the source file is always either the first, or the only entry.
171 local SRC="${SPLIT[0]}"
172 # Remove target_args suffix, if present
173 echo "${SRC%;${ARGS}}"
174}
175
Steve Kondik5bd66602016-07-15 10:39:58 -0700176#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300177# input: spec in the form of "src[:dst][;args]"
178# output: "dst" if present, "src" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700179#
180function target_file() {
dianlujitao4918b8a2020-01-02 15:26:44 +0800181 local SPEC="${1%%;*}"
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300182 local SPLIT=(${SPEC//:/ })
183 local ARGS="$(target_args ${SPEC})"
184 local DST=
185 case ${#SPLIT[@]} in
186 1)
187 # The spec doesn't have a : delimiter
188 DST="${SPLIT[0]}"
189 ;;
190 *)
191 # The spec actually has a src:dst format
192 DST="${SPLIT[1]}"
193 ;;
194 esac
195 # Remove target_args suffix, if present
196 echo "${DST%;${ARGS}}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700197}
198
199#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300200# input: spec in the form of "src[:dst][;args]"
201# output: "args" if present, "" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700202#
203function target_args() {
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300204 local SPEC="$1"
205 local SPLIT=(${SPEC//;/ })
206 local ARGS=
207 case ${#SPLIT[@]} in
208 1)
209 # No ";" delimiter in the spec.
210 ;;
211 *)
212 # The "args" are whatever comes after the ";" character.
213 # Basically the spec stripped of whatever is to the left of ";".
214 ARGS="${SPEC#${SPLIT[0]};}"
215 ;;
216 esac
217 echo "${ARGS}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700218}
219
220#
221# prefix_match:
222#
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300223# input:
224# - $1: prefix
225# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
226# output:
227# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik5bd66602016-07-15 10:39:58 -0700228#
229function prefix_match() {
230 local PREFIX="$1"
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100231 local NEW_ARRAY=()
Vladimir Oltean7220f362018-04-02 22:37:09 +0300232 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
233 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700234 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300235 local ARGS=$(target_args "$LINE")
SamarV-1215556e2d2024-03-19 08:50:02 +0530236 if [[ -z "${ARGS}" || "${ARGS}" =~ 'SYMLINK' ]]; then
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100237 NEW_ARRAY+=("${FILE#$PREFIX}")
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300238 else
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100239 NEW_ARRAY+=("${FILE#$PREFIX};${ARGS}")
Vladimir Oltean011b6b62018-06-12 01:17:35 +0300240 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700241 fi
242 done
Sebastiano Barezzidf527c72022-03-25 16:59:58 +0100243 printf '%s\n' "${NEW_ARRAY[@]}" | LC_ALL=C sort
Steve Kondik5bd66602016-07-15 10:39:58 -0700244}
245
246#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400247# prefix_match_file:
248#
249# $1: the prefix to match on
250# $2: the file to match the prefix for
251#
252# Internal function which returns true if a filename contains the
253# specified prefix.
254#
255function prefix_match_file() {
256 local PREFIX="$1"
257 local FILE="$2"
258 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
259 return 0
260 else
261 return 1
262 fi
263}
264
265#
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -0700266# suffix_match_file:
267#
268# $1: the suffix to match on
269# $2: the file to match the suffix for
270#
271# Internal function which returns true if a filename contains the
272# specified suffix.
273#
274function suffix_match_file() {
275 local SUFFIX="$1"
276 local FILE="$2"
277 if [[ "$FILE" = *"$SUFFIX" ]]; then
278 return 0
279 else
280 return 1
281 fi
282}
283
284#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400285# truncate_file
286#
287# $1: the filename to truncate
288# $2: the argument to output the truncated filename to
289#
290# Internal function which truncates a filename by removing the first dir
291# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
292#
293function truncate_file() {
294 local FILE="$1"
295 RETURN_FILE="$2"
296 local FIND="${FILE%%/*}"
297 local LOCATION="${#FIND}+1"
298 echo ${FILE:$LOCATION}
299}
300
301#
Steve Kondik5bd66602016-07-15 10:39:58 -0700302# write_product_copy_files:
303#
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400304# $1: make treble compatible makefile - optional and deprecated, default to true
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400305#
Steve Kondik5bd66602016-07-15 10:39:58 -0700306# Creates the PRODUCT_COPY_FILES section in the product makefile for all
307# items in the list which do not start with a dash (-).
308#
309function write_product_copy_files() {
310 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
311 local TARGET=
312 local FILE=
313 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400314 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700315
316 if [ "$COUNT" -eq "0" ]; then
317 return 0
318 fi
319
320 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
321 for (( i=1; i<COUNT+1; i++ )); do
322 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
323 LINEEND=" \\"
324 if [ "$i" -eq "$COUNT" ]; then
325 LINEEND=""
326 fi
327
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300328 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400329 if prefix_match_file "product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400330 local OUTTARGET=$(truncate_file $TARGET)
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400331 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400332 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400333 elif prefix_match_file "system/product/" $TARGET ; then
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400334 local OUTTARGET=$(truncate_file $TARGET)
335 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
336 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Luca Stefani776be462020-09-09 15:53:58 +0200337 elif prefix_match_file "system_ext/" $TARGET ; then
338 local OUTTARGET=$(truncate_file $TARGET)
339 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
340 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
341 elif prefix_match_file "system/system_ext/" $TARGET ; then
342 local OUTTARGET=$(truncate_file $TARGET)
343 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
344 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400345 elif prefix_match_file "odm/" $TARGET ; then
346 local OUTTARGET=$(truncate_file $TARGET)
347 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
348 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400349 elif prefix_match_file "vendor/odm/" $TARGET ; then
350 local OUTTARGET=$(truncate_file $TARGET)
351 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
352 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
353 elif prefix_match_file "system/vendor/odm/" $TARGET ; then
354 local OUTTARGET=$(truncate_file $TARGET)
355 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
356 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
357 elif prefix_match_file "vendor/" $TARGET ; then
358 local OUTTARGET=$(truncate_file $TARGET)
359 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
360 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Alexander Koskovich44c8fac2022-01-22 22:27:29 -0700361 elif prefix_match_file "vendor_dlkm/" $TARGET ; then
362 local OUTTARGET=$(truncate_file $TARGET)
363 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR_DLKM)/%s%s\n' \
364 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab06688fc2019-10-05 00:09:41 -0400365 elif prefix_match_file "system/vendor/" $TARGET ; then
366 local OUTTARGET=$(truncate_file $TARGET)
367 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
368 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400369 elif prefix_match_file "system/" $TARGET ; then
370 local OUTTARGET=$(truncate_file $TARGET)
371 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
372 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Mohd Farazd683fe42022-07-23 14:33:59 +0200373 elif prefix_match_file "recovery/" $TARGET ; then
374 local OUTTARGET=$(truncate_file $TARGET)
375 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_RECOVERY)/%s%s\n' \
376 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
377 elif prefix_match_file "vendor_ramdisk/" $TARGET ; then
378 local OUTTARGET=$(truncate_file $TARGET)
379 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR_RAMDISK)/%s%s\n' \
380 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400381 else
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400382 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400383 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
384 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700385 done
386 return 0
387}
388
389#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700390# write_blueprint_packages:
Steve Kondik5bd66602016-07-15 10:39:58 -0700391#
392# $1: The LOCAL_MODULE_CLASS for the given module list
Luca Stefani776be462020-09-09 15:53:58 +0200393# $2: /system, /odm, /product, /system_ext, or /vendor partition
Steve Kondik5bd66602016-07-15 10:39:58 -0700394# $3: type-specific extra flags
395# $4: Name of the array holding the target list
396#
397# Internal function which writes out the BUILD_PREBUILT stanzas
398# for all modules in the list. This is called by write_product_packages
399# after the modules are categorized.
400#
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700401function write_blueprint_packages() {
402
403 local CLASS="$1"
404 local PARTITION="$2"
405 local EXTRA="$3"
406
407 # Yes, this is a horrible hack - we create a new array using indirection
408 local ARR_NAME="$4[@]"
409 local FILELIST=("${!ARR_NAME}")
410
411 local FILE=
412 local ARGS=
413 local BASENAME=
414 local EXTENSION=
415 local PKGNAME=
416 local SRC=
Tim Zimmermannd9320762021-12-23 07:06:17 +0100417 local STEM=
TheStrix6e24acc2020-04-10 18:20:19 +0530418 local OVERRIDEPKG=
SamarV-1217e6b4082024-02-26 14:39:34 +0530419 local REQUIREDPKG=
Aaron Klingb2649192023-08-26 15:04:17 -0500420 local DISABLE_CHECKELF=
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700421
422 for P in "${FILELIST[@]}"; do
423 FILE=$(target_file "$P")
424 ARGS=$(target_args "$P")
Tim Zimmermannd9320762021-12-23 07:06:17 +0100425 ARGS=(${ARGS//;/ })
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700426
427 BASENAME=$(basename "$FILE")
428 DIRNAME=$(dirname "$FILE")
429 EXTENSION=${BASENAME##*.}
430 PKGNAME=${BASENAME%.*}
431
Tim Zimmermanne93bb1b2021-12-23 10:15:26 +0100432 if [ "$CLASS" = "EXECUTABLES" ] && [ "$EXTENSION" != "sh" ]; then
433 PKGNAME="$BASENAME"
434 EXTENSION=""
435 fi
436
Tim Zimmermannd9320762021-12-23 07:06:17 +0100437 # Allow overriding module name
438 STEM=
Aaron Kling7f4d1812023-11-02 16:04:22 -0500439 if [ "$TARGET_ENABLE_CHECKELF" == "true" ]; then
440 DISABLE_CHECKELF=
441 else
442 DISABLE_CHECKELF="true"
443 fi
Tim Zimmermannd9320762021-12-23 07:06:17 +0100444 for ARG in "${ARGS[@]}"; do
445 if [[ "$ARG" =~ "MODULE" ]]; then
446 STEM="$PKGNAME"
447 PKGNAME=${ARG#*=}
Aaron Klingb2649192023-08-26 15:04:17 -0500448 elif [[ "$ARG" == "DISABLE_CHECKELF" ]]; then
449 DISABLE_CHECKELF="true"
Tim Zimmermannd9320762021-12-23 07:06:17 +0100450 fi
451 done
452
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700453 # Add to final package list
454 PACKAGE_LIST+=("$PKGNAME")
455
456 SRC="proprietary"
457 if [ "$PARTITION" = "system" ]; then
458 SRC+="/system"
459 elif [ "$PARTITION" = "vendor" ]; then
460 SRC+="/vendor"
461 elif [ "$PARTITION" = "product" ]; then
462 SRC+="/product"
Luca Stefani776be462020-09-09 15:53:58 +0200463 elif [ "$PARTITION" = "system_ext" ]; then
464 SRC+="/system_ext"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700465 elif [ "$PARTITION" = "odm" ]; then
466 SRC+="/odm"
467 fi
468
469 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
470 printf 'cc_prebuilt_library_shared {\n'
471 printf '\tname: "%s",\n' "$PKGNAME"
Tim Zimmermannd9320762021-12-23 07:06:17 +0100472 if [ ! -z "$STEM" ]; then
473 printf '\tstem: "%s",\n' "$STEM"
474 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700475 printf '\towner: "%s",\n' "$VENDOR"
476 printf '\tstrip: {\n'
477 printf '\t\tnone: true,\n'
478 printf '\t},\n'
479 printf '\ttarget: {\n'
480 if [ "$EXTRA" = "both" ]; then
481 printf '\t\tandroid_arm: {\n'
482 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
Aaron Klingb2649192023-08-26 15:04:17 -0500483 if [ -z "$DISABLE_CHECKELF" ]; then
484 printf '\t\t\tshared_libs: [%s],\n' "$(basename -s .so $(${OBJDUMP} -x "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/lib/"$FILE" 2>/dev/null |grep NEEDED) 2>/dev/null |grep -v ^NEEDED$ |sed 's/-3.9.1//g' |sed 's/\(.*\)/"\1",/g' |tr '\n' ' ')"
485 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700486 printf '\t\t},\n'
487 printf '\t\tandroid_arm64: {\n'
488 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
Aaron Klingb2649192023-08-26 15:04:17 -0500489 if [ -z "$DISABLE_CHECKELF" ]; then
490 printf '\t\t\tshared_libs: [%s],\n' "$(basename -s .so $(${OBJDUMP} -x "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/lib64/"$FILE" 2>/dev/null |grep NEEDED) 2>/dev/null |grep -v ^NEEDED$ |sed 's/-3.9.1//g' |sed 's/\(.*\)/"\1",/g' |tr '\n' ' ')"
491 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700492 printf '\t\t},\n'
493 elif [ "$EXTRA" = "64" ]; then
494 printf '\t\tandroid_arm64: {\n'
495 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
Aaron Klingb2649192023-08-26 15:04:17 -0500496 if [ -z "$DISABLE_CHECKELF" ]; then
497 printf '\t\t\tshared_libs: [%s],\n' "$(basename -s .so $(${OBJDUMP} -x "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/lib64/"$FILE" 2>/dev/null |grep NEEDED) 2>/dev/null |grep -v ^NEEDED$ |sed 's/-3.9.1//g' |sed 's/\(.*\)/"\1",/g' |tr '\n' ' ')"
498 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700499 printf '\t\t},\n'
500 else
501 printf '\t\tandroid_arm: {\n'
502 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
Aaron Klingb2649192023-08-26 15:04:17 -0500503 if [ -z "$DISABLE_CHECKELF" ]; then
504 printf '\t\t\tshared_libs: [%s],\n' "$(basename -s .so $(${OBJDUMP} -x "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/lib/"$FILE" 2>/dev/null |grep NEEDED) 2>/dev/null |grep -v ^NEEDED$ |sed 's/-3.9.1//g' |sed 's/\(.*\)/"\1",/g' |tr '\n' ' ')"
505 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700506 printf '\t\t},\n'
507 fi
508 printf '\t},\n'
509 if [ "$EXTRA" != "none" ]; then
510 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
511 fi
Aaron Klingb2649192023-08-26 15:04:17 -0500512 if [ ! -z "$DISABLE_CHECKELF" ]; then
513 printf '\tcheck_elf_files: false,\n'
514 fi
Aaron Klingc46cd092023-08-29 20:06:47 -0500515 elif [ "$CLASS" = "RFSA" ]; then
516 printf 'prebuilt_rfsa {\n'
517 printf '\tname: "%s",\n' "$PKGNAME"
518 printf '\tfilename: "%s",\n' "$BASENAME"
519 printf '\towner: "%s",\n' "$VENDOR"
520 printf '\tsrc: "%s/lib/rfsa/%s",\n' "$SRC" "$FILE"
Chirayu Desaia7741012021-12-04 07:17:28 +0530521 elif [ "$CLASS" = "APEX" ]; then
522 printf 'prebuilt_apex {\n'
523 printf '\tname: "%s",\n' "$PKGNAME"
524 printf '\towner: "%s",\n' "$VENDOR"
525 SRC="$SRC/apex"
526 printf '\tsrc: "%s/%s",\n' "$SRC" "$FILE"
527 printf '\tfilename: "%s",\n' "$FILE"
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700528 elif [ "$CLASS" = "APPS" ]; then
529 printf 'android_app_import {\n'
530 printf '\tname: "%s",\n' "$PKGNAME"
531 printf '\towner: "%s",\n' "$VENDOR"
532 if [ "$EXTRA" = "priv-app" ]; then
533 SRC="$SRC/priv-app"
534 else
535 SRC="$SRC/app"
536 fi
537 printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
LuK1337508e85f2021-08-23 18:18:57 +0200538 USE_PLATFORM_CERTIFICATE="true"
539 for ARG in "${ARGS[@]}"; do
540 if [ "$ARG" = "PRESIGNED" ]; then
541 USE_PLATFORM_CERTIFICATE="false"
Michael Bestasab47e912024-03-06 13:32:05 +0200542 printf '\tpreprocessed: true,\n'
LuK1337508e85f2021-08-23 18:18:57 +0200543 printf '\tpresigned: true,\n'
544 elif [[ "$ARG" =~ "OVERRIDES" ]]; then
545 OVERRIDEPKG=${ARG#*=}
Arian72ac8362021-09-27 17:49:19 +0200546 OVERRIDEPKG=${OVERRIDEPKG//,/\", \"}
LuK1337508e85f2021-08-23 18:18:57 +0200547 printf '\toverrides: ["%s"],\n' "$OVERRIDEPKG"
SamarV-1217e6b4082024-02-26 14:39:34 +0530548 elif [[ "$ARG" =~ "REQUIRED" ]]; then
549 REQUIREDPKG=${ARG#*=}
550 REQUIRED_PACKAGES_LIST+="$REQUIREDPKG,"
551 printf '\trequired: ["%s"],\n' "${REQUIREDPKG//,/\", \"}"
SamarV-1215556e2d2024-03-19 08:50:02 +0530552 elif [[ "$ARG" =~ "SYMLINK" ]]; then
553 continue
LuK1337508e85f2021-08-23 18:18:57 +0200554 elif [ ! -z "$ARG" ]; then
555 USE_PLATFORM_CERTIFICATE="false"
556 printf '\tcertificate: "%s",\n' "$ARG"
557 fi
558 done
559 if [ "$USE_PLATFORM_CERTIFICATE" = "true" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700560 printf '\tcertificate: "platform",\n'
561 fi
562 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
563 printf 'dex_import {\n'
564 printf '\tname: "%s",\n' "$PKGNAME"
565 printf '\towner: "%s",\n' "$VENDOR"
566 printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
567 elif [ "$CLASS" = "ETC" ]; then
568 if [ "$EXTENSION" = "xml" ]; then
569 printf 'prebuilt_etc_xml {\n'
570 else
571 printf 'prebuilt_etc {\n'
572 fi
573 printf '\tname: "%s",\n' "$PKGNAME"
574 printf '\towner: "%s",\n' "$VENDOR"
575 printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
LuK1337f7f18712020-10-06 19:29:02 +0200576 printf '\tfilename_from_src: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700577 elif [ "$CLASS" = "EXECUTABLES" ]; then
Aaron Klingb2649192023-08-26 15:04:17 -0500578 if ! objdump -a "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/bin/"$FILE" 2>/dev/null |grep -c 'file format elf' > /dev/null; then
579 # This is not an elf file, assume it's a shell script that doesn't have an extension
580 # Setting extension here does not change the target extension, only the module type
581 EXTENSION="sh"
582 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700583 if [ "$EXTENSION" = "sh" ]; then
584 printf 'sh_binary {\n'
585 else
586 printf 'cc_prebuilt_binary {\n'
587 fi
588 printf '\tname: "%s",\n' "$PKGNAME"
589 printf '\towner: "%s",\n' "$VENDOR"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200590 if [ "$EXTENSION" != "sh" ]; then
Aaron Kling8a23e7b2023-09-12 13:02:16 -0500591 printf '\ttarget: {\n'
592 if objdump -a "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/bin/"$FILE" |grep -c 'file format elf64' > /dev/null; then
593 printf '\t\tandroid_arm64: {\n'
Aaron Klingb2649192023-08-26 15:04:17 -0500594 else
Aaron Kling8a23e7b2023-09-12 13:02:16 -0500595 printf '\t\tandroid_arm: {\n'
596 fi
597 printf '\t\t\tsrcs: ["%s/bin/%s"],\n' "$SRC" "$FILE"
598 if [ -z "$DISABLE_CHECKELF" ]; then
599 printf '\t\t\tshared_libs: [%s],\n' "$(basename -s .so $(${OBJDUMP} -x "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/bin/"$FILE" 2>/dev/null |grep NEEDED) 2>/dev/null |grep -v ^NEEDED$ |sed 's/-3.9.1//g' |sed 's/\(.*\)/"\1",/g' |tr '\n' ' ')"
600 fi
601 printf '\t\t},\n'
602 printf '\t},\n'
603 if objdump -a "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/bin/"$FILE" |grep -c 'file format elf64' > /dev/null; then
604 printf '\tcompile_multilib: "%s",\n' "64"
605 else
606 printf '\tcompile_multilib: "%s",\n' "32"
607 fi
608 if [ ! -z "$DISABLE_CHECKELF" ]; then
Aaron Klingb2649192023-08-26 15:04:17 -0500609 printf '\tcheck_elf_files: false,\n'
610 fi
Tim Zimmermann54606d62021-12-23 09:26:54 +0100611 printf '\tstrip: {\n'
612 printf '\t\tnone: true,\n'
613 printf '\t},\n'
Mohd Faraz2509b6e2022-10-02 09:48:52 +0200614 printf '\tprefer: true,\n'
615 else
616 printf '\tsrc: "%s/bin/%s",\n' "$SRC" "$FILE"
Aaron Kling74b654d2023-08-29 22:36:54 -0500617 printf '\tfilename: "%s",\n' "$BASENAME"
Sebastiano Barezzifd4b2b32021-07-14 21:33:10 +0200618 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700619 else
620 printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
621 fi
622 if [ "$CLASS" = "APPS" ]; then
623 printf '\tdex_preopt: {\n'
624 printf '\t\tenabled: false,\n'
625 printf '\t},\n'
Jyotiraditya Panda45f50af2024-02-19 05:35:33 +0900626 if [ "$DIRNAME" != "." ] && [[ "$DIRNAME" == */* ]]; then
627 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
628 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700629 fi
Aaron Klingc46cd092023-08-29 20:06:47 -0500630 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] || [ "$CLASS" = "RFSA" ] ; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700631 if [ "$DIRNAME" != "." ]; then
Aaron Kling68cc8bb2023-10-10 03:52:45 -0500632 if [ "$EXTENSION" = "sh" ]; then
633 printf '\tsub_dir: "%s",\n' "$DIRNAME"
634 else
635 printf '\trelative_install_path: "%s",\n' "$DIRNAME"
636 fi
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700637 fi
638 fi
Andreas Schneiderdbcf9db2020-05-25 17:03:17 +0200639 if [ "$CLASS" = "ETC" ] ; then
640 if [ "$DIRNAME" != "." ]; then
641 printf '\tsub_dir: "%s",\n' "$DIRNAME"
642 fi
643 fi
Mohd Faraz2509b6e2022-10-02 09:48:52 +0200644 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700645 printf '\tprefer: true,\n'
646 fi
647 if [ "$EXTRA" = "priv-app" ]; then
648 printf '\tprivileged: true,\n'
649 fi
650 if [ "$PARTITION" = "vendor" ]; then
651 printf '\tsoc_specific: true,\n'
652 elif [ "$PARTITION" = "product" ]; then
653 printf '\tproduct_specific: true,\n'
Luca Stefani776be462020-09-09 15:53:58 +0200654 elif [ "$PARTITION" = "system_ext" ]; then
655 printf '\tsystem_ext_specific: true,\n'
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700656 elif [ "$PARTITION" = "odm" ]; then
657 printf '\tdevice_specific: true,\n'
658 fi
659 printf '}\n\n'
660 done
661}
662
663#
Steve Kondik5bd66602016-07-15 10:39:58 -0700664# write_product_packages:
665#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -0700666# This function will create prebuilt entries in the
667# Android.bp and associated PRODUCT_PACKAGES list in the
Steve Kondik5bd66602016-07-15 10:39:58 -0700668# product makefile for all files in the blob list which
669# start with a single dash (-) character.
670#
671function write_product_packages() {
672 PACKAGE_LIST=()
673
Chenyang Zhongc487f382022-02-10 21:40:41 -0500674 # Sort the package list for comm
675 PRODUCT_PACKAGES_LIST=($( printf '%s\n' "${PRODUCT_PACKAGES_LIST[@]}" | LC_ALL=C sort))
676
Steve Kondik5bd66602016-07-15 10:39:58 -0700677 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
678
679 if [ "$COUNT" = "0" ]; then
680 return 0
681 fi
682
683 # Figure out what's 32-bit, what's 64-bit, and what's multilib
684 # I really should not be doing this in bash due to shitty array passing :(
685 local T_LIB32=( $(prefix_match "lib/") )
686 local T_LIB64=( $(prefix_match "lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500687 local MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
688 local LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
689 local LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik5bd66602016-07-15 10:39:58 -0700690
691 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700692 write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700693 fi
694 if [ "${#LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700695 write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700696 fi
697 if [ "${#LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700698 write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700699 fi
700
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400701 local T_S_LIB32=( $(prefix_match "system/lib/") )
702 local T_S_LIB64=( $(prefix_match "system/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500703 local S_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
704 local S_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
705 local S_LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400706
707 if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700708 write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400709 fi
710 if [ "${#S_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700711 write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400712 fi
713 if [ "${#S_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700714 write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400715 fi
716
Steve Kondik5bd66602016-07-15 10:39:58 -0700717 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
718 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
Aaron Klingc46cd092023-08-29 20:06:47 -0500719 local V_RFSA=( $(prefix_match "vendor/lib/rfsa/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500720 local V_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
721 local V_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Aaron Klingc46cd092023-08-29 20:06:47 -0500722 local V_LIB32=( $(LC_ALL=C grep -v 'rfsa/' <(printf '%s\n' "${V_LIB32[@]}")) )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500723 local V_LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik5bd66602016-07-15 10:39:58 -0700724
725 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700726 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700727 fi
728 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700729 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700730 fi
731 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700732 write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500733 fi
Aaron Klingc46cd092023-08-29 20:06:47 -0500734 if [ "${#V_RFSA[@]}" -gt "0" ]; then
735 write_blueprint_packages "RFSA" "vendor" "" "V_RFSA" >> "$ANDROIDBP"
736 fi
razorlovesa0d296b2019-07-29 02:21:34 -0500737
738 local T_P_LIB32=( $(prefix_match "product/lib/") )
739 local T_P_LIB64=( $(prefix_match "product/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500740 local P_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
741 local P_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
742 local P_LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
razorlovesa0d296b2019-07-29 02:21:34 -0500743
744 if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700745 write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500746 fi
747 if [ "${#P_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700748 write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500749 fi
750 if [ "${#P_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700751 write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700752 fi
753
Luca Stefani776be462020-09-09 15:53:58 +0200754 local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
755 local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500756 local SE_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
757 local SE_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
758 local SE_LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_SE_LIB64[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
Luca Stefani776be462020-09-09 15:53:58 +0200759
760 if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
761 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
762 fi
763 if [ "${#SE_LIB32[@]}" -gt "0" ]; then
764 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
765 fi
766 if [ "${#SE_LIB64[@]}" -gt "0" ]; then
767 write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
768 fi
769
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700770 local T_O_LIB32=( $(prefix_match "odm/lib/") )
771 local T_O_LIB64=( $(prefix_match "odm/lib64/") )
Pablo Cholaky32d80cf2022-01-16 13:52:07 -0500772 local O_MULTILIBS=( $(LC_ALL=C comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
773 local O_LIB32=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
774 local O_LIB64=( $(LC_ALL=C comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700775
776 if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700777 write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700778 fi
779 if [ "${#O_LIB32[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700780 write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700781 fi
782 if [ "${#O_LIB64[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700783 write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700784 fi
785
Chirayu Desaia7741012021-12-04 07:17:28 +0530786 # APEX
787 local APEX=( $(prefix_match "apex/") )
788 if [ "${#APEX[@]}" -gt "0" ]; then
789 write_blueprint_packages "APEX" "" "" "APEX" >> "$ANDROIDBP"
790 fi
791 local S_APEX=( $(prefix_match "system/apex/") )
792 if [ "${#S_APEX[@]}" -gt "0" ]; then
793 write_blueprint_packages "APEX" "system" "" "S_APEX" >> "$ANDROIDBP"
794 fi
795 local V_APEX=( $(prefix_match "vendor/apex/") )
796 if [ "${#V_APEX[@]}" -gt "0" ]; then
797 write_blueprint_packages "APEX" "vendor" "" "V_APEX" >> "$ANDROIDBP"
798 fi
799 local SE_APEX=( $(prefix_match "system_ext/apex/") )
800 if [ "${#SE_APEX[@]}" -gt "0" ]; then
801 write_blueprint_packages "APEX" "system_ext" "" "SE_APEX" >> "$ANDROIDBP"
802 fi
803
Steve Kondik5bd66602016-07-15 10:39:58 -0700804 # Apps
805 local APPS=( $(prefix_match "app/") )
806 if [ "${#APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100807 write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700808 fi
809 local PRIV_APPS=( $(prefix_match "priv-app/") )
810 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100811 write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700812 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400813 local S_APPS=( $(prefix_match "system/app/") )
814 if [ "${#S_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100815 write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400816 fi
817 local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
818 if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100819 write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400820 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700821 local V_APPS=( $(prefix_match "vendor/app/") )
822 if [ "${#V_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100823 write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700824 fi
825 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
826 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100827 write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500828 fi
829 local P_APPS=( $(prefix_match "product/app/") )
830 if [ "${#P_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100831 write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500832 fi
833 local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
834 if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100835 write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700836 fi
Luca Stefani776be462020-09-09 15:53:58 +0200837 local SE_APPS=( $(prefix_match "system_ext/app/") )
838 if [ "${#SE_APPS[@]}" -gt "0" ]; then
839 write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
840 fi
841 local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
842 if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
843 write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
844 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700845 local O_APPS=( $(prefix_match "odm/app/") )
846 if [ "${#O_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100847 write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700848 fi
849 local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
850 if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
mickael saibi64b0b752019-11-17 18:35:05 +0100851 write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700852 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700853
854 # Framework
855 local FRAMEWORK=( $(prefix_match "framework/") )
856 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700857 write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700858 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400859 local S_FRAMEWORK=( $(prefix_match "system/framework/") )
860 if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700861 write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400862 fi
Christian Oder974b5902017-10-08 23:15:52 +0200863 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200864 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700865 write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500866 fi
867 local P_FRAMEWORK=( $(prefix_match "product/framework/") )
868 if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700869 write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
Christian Oder974b5902017-10-08 23:15:52 +0200870 fi
Luca Stefani776be462020-09-09 15:53:58 +0200871 local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
Alexander Koskovich052c77d2020-09-16 17:58:53 -0700872 if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
Luca Stefani776be462020-09-09 15:53:58 +0200873 write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
874 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700875 local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
876 if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700877 write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700878 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700879
880 # Etc
881 local ETC=( $(prefix_match "etc/") )
882 if [ "${#ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700883 write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700884 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400885 local S_ETC=( $(prefix_match "system/etc/") )
Luca Weiss737940e2022-09-27 14:52:41 +0200886 if [ "${#S_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700887 write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400888 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700889 local V_ETC=( $(prefix_match "vendor/etc/") )
890 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700891 write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500892 fi
893 local P_ETC=( $(prefix_match "product/etc/") )
894 if [ "${#P_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700895 write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700896 fi
Luca Stefani776be462020-09-09 15:53:58 +0200897 local SE_ETC=( $(prefix_match "system_ext/etc/") )
898 if [ "${#SE_ETC[@]}" -gt "0" ]; then
899 write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
900 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700901 local O_ETC=( $(prefix_match "odm/etc/") )
902 if [ "${#O_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700903 write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700904 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700905
906 # Executables
907 local BIN=( $(prefix_match "bin/") )
908 if [ "${#BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700909 write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700910 fi
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400911 local S_BIN=( $(prefix_match "system/bin/") )
Luca Weiss737940e2022-09-27 14:52:41 +0200912 if [ "${#S_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700913 write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba6373e52019-09-28 23:37:36 -0400914 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700915 local V_BIN=( $(prefix_match "vendor/bin/") )
916 if [ "${#V_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700917 write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
razorlovesa0d296b2019-07-29 02:21:34 -0500918 fi
919 local P_BIN=( $(prefix_match "product/bin/") )
920 if [ "${#P_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700921 write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
Steve Kondik5bd66602016-07-15 10:39:58 -0700922 fi
Luca Stefani776be462020-09-09 15:53:58 +0200923 local SE_BIN=( $(prefix_match "system_ext/bin/") )
924 if [ "${#SE_BIN[@]}" -gt "0" ]; then
925 write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
926 fi
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700927 local O_BIN=( $(prefix_match "odm/bin/") )
928 if [ "${#O_BIN[@]}" -gt "0" ]; then
Rashed Abdel-Tawabb91adc02019-09-20 10:30:38 -0700929 write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
Rashed Abdel-Tawaba94af582019-09-20 07:32:39 -0700930 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700931
SamarV-1215556e2d2024-03-19 08:50:02 +0530932 write_package_definition "${PACKAGE_LIST[@]}" >> "$PRODUCTMK"
933}
Steve Kondik5bd66602016-07-15 10:39:58 -0700934
SamarV-1215556e2d2024-03-19 08:50:02 +0530935
936#
937# write_symlink_packages:
938#
939# Creates symlink entries in the Android.bp and related PRODUCT_PACKAGES
940# list in the product makefile for all files in the blob list which has
941# SYMLINK argument.
942#
943function write_symlink_packages() {
944 local FILE=
945 local ARGS=
946 local ARCH=
947 local BASENAME=
948 local PKGNAME=
949 local PREFIX=
950 local SYMLINK_BASENAME=
951 local SYMLINK_PACKAGES=()
952
953 # Sort the symlinks list for comm
954 PRODUCT_SYMLINKS_LIST=($( printf '%s\n' "${PRODUCT_SYMLINKS_LIST[@]}" | LC_ALL=C sort))
955
956 local COUNT=${#PRODUCT_SYMLINKS_LIST[@]}
957
958 if [ "$COUNT" = "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700959 return 0
960 fi
961
SamarV-1215556e2d2024-03-19 08:50:02 +0530962 for LINE in "${PRODUCT_SYMLINKS_LIST[@]}"; do
Bruno Martinsbe7f3cb2024-06-23 15:54:02 +0100963 FILE=$(target_file "$LINE")
Bruno Martinsf96fd122024-03-28 14:31:02 +0000964 if [[ "$LINE" =~ '/lib64/' || "$LINE" =~ '/lib/arm64/' ]]; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530965 ARCH="64"
Bruno Martinsf96fd122024-03-28 14:31:02 +0000966 elif [[ "$LINE" =~ '/lib/' ]]; then
967 ARCH="32"
Steve Kondik5bd66602016-07-15 10:39:58 -0700968 fi
SamarV-1215556e2d2024-03-19 08:50:02 +0530969 BASENAME=$(basename "$FILE")
970 ARGS=$(target_args "$LINE")
971 ARGS=(${ARGS//;/ })
972 for ARG in "${ARGS[@]}"; do
973 if [[ "$ARG" =~ "SYMLINK" ]]; then
974 SYMLINKS=${ARG#*=}
975 SYMLINKS=(${SYMLINKS//,/ })
976 for SYMLINK in "${SYMLINKS[@]}"; do
977 SYMLINK_BASENAME=$(basename "$SYMLINK")
Bruno Martinsad05f512024-06-28 00:34:33 +0100978 PKGNAME="${BASENAME%.*}_${SYMLINK_BASENAME%.*}_symlink${ARCH}"
979 if [[ "${SYMLINK_PACKAGES[@]}" =~ "$PKGNAME" ]]; then
980 PKGNAME+="_$(grep -o "$PKGNAME" <<< ${SYMLINK_PACKAGES[*]} | wc -l)"
981 fi
SamarV-1215556e2d2024-03-19 08:50:02 +0530982 {
983 printf 'install_symlink {\n'
984 printf '\tname: "%s",\n' "$PKGNAME"
Mashopy06100c92024-04-23 21:52:04 +0200985 if prefix_match_file "vendor/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530986 PREFIX='vendor/'
987 printf '\tsoc_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200988 elif prefix_match_file "product/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530989 PREFIX='product/'
990 printf '\tproduct_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200991 elif prefix_match_file "system_ext/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530992 PREFIX='system_ext/'
993 printf '\tsystem_ext_specific: true,\n'
Mashopy06100c92024-04-23 21:52:04 +0200994 elif prefix_match_file "odm/" "$SYMLINK"; then
SamarV-1215556e2d2024-03-19 08:50:02 +0530995 PREFIX='odm/'
996 printf '\tdevice_specific: true,\n'
997 fi
998 printf '\tinstalled_location: "%s",\n' "${SYMLINK#"$PREFIX"}"
999 printf '\tsymlink_target: "/%s",\n' "$FILE"
1000 printf '}\n\n'
1001 } >> "$ANDROIDBP"
1002 SYMLINK_PACKAGES+=("$PKGNAME")
1003 done
1004 fi
1005 done
Steve Kondik5bd66602016-07-15 10:39:58 -07001006 done
SamarV-1215556e2d2024-03-19 08:50:02 +05301007
1008 write_package_definition "${SYMLINK_PACKAGES[@]}" >> "$PRODUCTMK"
Steve Kondik5bd66602016-07-15 10:39:58 -07001009}
1010
1011#
Michael Bestasfe71eb32023-06-11 18:59:10 +03001012# write_single_product_copy_files:
1013#
1014# $1: the file to be copied
1015#
1016# Creates a PRODUCT_COPY_FILES section in the product makefile for the
1017# item provided in $1.
1018#
1019function write_single_product_copy_files() {
1020 local FILE="$1"
1021 if [ -z "$FILE" ]; then
1022 echo "A file must be provided to write_single_product_copy_files()!"
1023 exit 1
1024 fi
1025
1026 local TARGET=$(target_file "$FILE")
1027 local OUTTARGET=$(truncate_file $TARGET)
1028
1029 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
1030 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s\n' \
1031 "$OUTDIR" "$TARGET" "$OUTTARGET" >> "$PRODUCTMK"
1032}
1033
1034#
1035# write_single_product_packages:
1036#
1037# $1: the package to be built
1038#
1039# Creates a PRODUCT_PACKAGES section in the product makefile for the
1040# item provided in $1.
1041#
1042function write_single_product_packages() {
1043 local PACKAGE="$1"
1044 if [ -z "$PACKAGE" ]; then
1045 echo "A package must be provided to write_single_product_packages()!"
1046 exit 1
1047 fi
1048
1049 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
1050 printf ' %s%s\n' "$PACKAGE" >> "$PRODUCTMK"
1051}
1052
1053#
Michael Bestas431a8002023-06-11 20:04:45 +03001054# write_rro_androidmanifest:
1055#
1056# $2: target package for the RRO overlay
1057#
1058# Creates an AndroidManifest.xml for an RRO overlay.
1059#
1060function write_rro_androidmanifest() {
1061 local TARGET_PACKAGE="$1"
1062
1063 cat << EOF
1064<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1065 package="$TARGET_PACKAGE.vendor"
1066 android:versionCode="1"
1067 android:versionName="1.0">
1068 <application android:hasCode="false" />
1069 <overlay
1070 android:targetPackage="$TARGET_PACKAGE"
1071 android:isStatic="true"
1072 android:priority="0"/>
1073</manifest>
1074EOF
1075}
1076
1077#
1078# write_rro_blueprint:
1079#
1080# $1: package name for the RRO overlay
1081# $2: target partition for the RRO overlay
1082#
1083# Creates an Android.bp for an RRO overlay.
1084#
1085function write_rro_blueprint() {
1086 local PKGNAME="$1"
1087 local PARTITION="$2"
1088
1089 printf 'runtime_resource_overlay {\n'
1090 printf '\tname: "%s",\n' "$PKGNAME"
1091 printf '\ttheme: "%s",\n' "$PKGNAME"
1092 printf '\tsdk_version: "%s",\n' "current"
1093 printf '\taaptflags: ["%s"],\n' "--keep-raw-values"
1094
1095 if [ "$PARTITION" = "vendor" ]; then
1096 printf '\tsoc_specific: true,\n'
1097 elif [ "$PARTITION" = "product" ]; then
1098 printf '\tproduct_specific: true,\n'
1099 elif [ "$PARTITION" = "system_ext" ]; then
1100 printf '\tsystem_ext_specific: true,\n'
1101 elif [ "$PARTITION" = "odm" ]; then
1102 printf '\tdevice_specific: true,\n'
1103 fi
1104 printf '}\n'
1105}
1106
1107#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001108# write_blueprint_header:
Steve Kondik5bd66602016-07-15 10:39:58 -07001109#
1110# $1: file which will be written to
1111#
Michael Bestasa2934df2020-12-19 03:50:32 +02001112# writes out the warning message regarding manual file modifications.
Steve Kondik5bd66602016-07-15 10:39:58 -07001113# note that this is not an append operation, and should
1114# be executed first!
1115#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001116function write_blueprint_header() {
1117 if [ -f $1 ]; then
1118 rm $1
1119 fi
1120
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001121 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1122
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001123 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +02001124// Automatically generated file. DO NOT MODIFY
1125//
1126// This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001127
1128EOF
1129}
1130
1131#
1132# write_makefile_header:
1133#
1134# $1: file which will be written to
1135#
Michael Bestasa2934df2020-12-19 03:50:32 +02001136# writes out the warning message regarding manual file modifications.
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001137# note that this is not an append operation, and should
1138# be executed first!
1139#
1140function write_makefile_header() {
Jake Whatley9843b322017-01-25 21:49:16 -05001141 if [ -f $1 ]; then
1142 rm $1
1143 fi
1144
Steve Kondik5bd66602016-07-15 10:39:58 -07001145 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1146
Jake Whatley9843b322017-01-25 21:49:16 -05001147 cat << EOF >> $1
Michael Bestasa2934df2020-12-19 03:50:32 +02001148# Automatically generated file. DO NOT MODIFY
Steve Kondik5bd66602016-07-15 10:39:58 -07001149#
Steve Kondik5bd66602016-07-15 10:39:58 -07001150# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1151
1152EOF
1153}
1154
1155#
Michael Bestas431a8002023-06-11 20:04:45 +03001156# write_xml_header:
1157#
1158# $1: file which will be written to
1159#
1160# writes out the warning message regarding manual file modifications.
1161# note that this is not an append operation, and should
1162# be executed first!
1163#
1164function write_xml_header() {
1165 if [ -f $1 ]; then
1166 rm $1
1167 fi
1168
1169 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1170 [ "$COMMON" -eq 1 ] && local VENDOR="${VENDOR_COMMON:-$VENDOR}"
1171
1172 cat << EOF >> $1
1173<?xml version="1.0" encoding="utf-8"?>
1174<!--
1175 Automatically generated file. DO NOT MODIFY
1176
1177 This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
1178-->
1179EOF
1180}
1181
1182#
1183# write_rro_package:
1184#
1185# $1: the RRO package name
1186# $2: the RRO target package
1187# $3: the partition for the RRO overlay
1188#
1189# Generates the file structure for an RRO overlay.
1190#
1191function write_rro_package() {
1192 local PKGNAME="$1"
1193 if [ -z "$PKGNAME" ]; then
1194 echo "A package name must be provided to write_rro_package()!"
1195 exit 1
1196 fi
1197
1198 local TARGET_PACKAGE="$2"
1199 if [ -z "$TARGET_PACKAGE" ]; then
1200 echo "A target package must be provided to write_rro_package()!"
1201 exit 1
1202 fi
1203
1204 local PARTITION="$3"
1205 if [ -z "$PARTITION" ]; then
1206 PARTITION="vendor"
1207 fi
1208
1209 local RROBP="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/Android.bp
1210 local RROMANIFEST="$ANDROID_ROOT"/"$OUTDIR"/rro_overlays/"$PKGNAME"/AndroidManifest.xml
1211
1212 write_blueprint_header "$RROBP"
1213 write_xml_header "$RROMANIFEST"
1214
1215 write_rro_blueprint "$PKGNAME" "$PARTITION" >> "$RROBP"
1216 write_rro_androidmanifest "$TARGET_PACKAGE" >> "$RROMANIFEST"
1217}
1218
1219#
SamarV-1215556e2d2024-03-19 08:50:02 +05301220# write_package_definition:
1221#
1222# $@: list of packages
1223#
1224# writes out the final PRODUCT_PACKAGES list
1225#
1226function write_package_definition() {
1227 local PACKAGE_LIST=("${@}")
1228 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
1229
1230 if [ "$PACKAGE_COUNT" -eq "0" ]; then
1231 return 0
1232 fi
1233
1234 printf '\n%s\n' "PRODUCT_PACKAGES += \\"
1235 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
SamarV-1217e6b4082024-02-26 14:39:34 +05301236 local SKIP=false
SamarV-1215556e2d2024-03-19 08:50:02 +05301237 local LINEEND=" \\"
1238 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
1239 LINEEND=""
1240 fi
SamarV-1217e6b4082024-02-26 14:39:34 +05301241 for PKG in $(tr "," "\n" <<< "$REQUIRED_PACKAGES_LIST"); do
1242 if [[ $PKG == "${PACKAGE_LIST[$i - 1]}" ]]; then
1243 SKIP=true
1244 break
1245 fi
1246 done
1247 # Skip adding of the package to product makefile if it's in the required list
1248 if [[ $SKIP == false ]]; then
1249 printf ' %s%s\n' "${PACKAGE_LIST[$i - 1]}" "$LINEEND" >> "$PRODUCTMK"
1250 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301251 done
1252}
1253
1254#
Steve Kondik5bd66602016-07-15 10:39:58 -07001255# write_headers:
1256#
1257# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -05001258# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001259#
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001260# Calls write_makefile_header for each of the makefiles and
1261# write_blueprint_header for Android.bp and creates the initial
1262# path declaration and device guard for the Android.mk
Steve Kondik5bd66602016-07-15 10:39:58 -07001263#
1264function write_headers() {
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001265 write_makefile_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001266
1267 GUARD="$2"
1268 if [ -z "$GUARD" ]; then
1269 GUARD="TARGET_DEVICE"
1270 fi
1271
Steve Kondik5bd66602016-07-15 10:39:58 -07001272 cat << EOF >> "$ANDROIDMK"
1273LOCAL_PATH := \$(call my-dir)
1274
1275EOF
1276 if [ "$COMMON" -ne 1 ]; then
1277 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001278ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -07001279
1280EOF
1281 else
1282 if [ -z "$1" ]; then
1283 echo "Argument with devices to be added to guard must be set!"
1284 exit 1
1285 fi
1286 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -05001287ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -07001288
1289EOF
1290 fi
1291
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001292 write_makefile_header "$BOARDMK"
1293 write_makefile_header "$PRODUCTMK"
1294 write_blueprint_header "$ANDROIDBP"
1295
1296 cat << EOF >> "$ANDROIDBP"
1297soong_namespace {
Aaron Klingb2649192023-08-26 15:04:17 -05001298 imports: [
1299EOF
1300
1301 if [ ! -z "$DEVICE_COMMON" -a "$COMMON" -ne 1 ]; then
1302 cat << EOF >> "$ANDROIDBP"
1303 "vendor/${VENDOR_COMMON:-$VENDOR}/$DEVICE_COMMON",
1304EOF
1305 fi
1306 vendor_imports "$ANDROIDBP"
1307
1308 cat << EOF >> "$ANDROIDBP"
1309 ],
Rashed Abdel-Tawabe2047042019-09-20 07:06:09 -07001310}
1311
1312EOF
1313
1314 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
1315 cat << EOF >> "$PRODUCTMK"
1316PRODUCT_SOONG_NAMESPACES += \\
1317 vendor/$VENDOR/$DEVICE
1318
1319EOF
Steve Kondik5bd66602016-07-15 10:39:58 -07001320}
1321
1322#
1323# write_footers:
1324#
1325# Closes the inital guard and any other finalization tasks. Must
1326# be called as the final step.
1327#
1328function write_footers() {
1329 cat << EOF >> "$ANDROIDMK"
1330endif
1331EOF
1332}
1333
1334# Return success if adb is up and not in recovery
1335function _adb_connected {
1336 {
Jake Whatley9843b322017-01-25 21:49:16 -05001337 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -07001338 then
1339 return 0
1340 fi
1341 } 2>/dev/null
1342
1343 return 1
1344};
1345
1346#
1347# parse_file_list:
1348#
1349# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001350# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001351#
1352# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
1353#
1354function parse_file_list() {
1355 if [ -z "$1" ]; then
1356 echo "An input file is expected!"
1357 exit 1
1358 elif [ ! -f "$1" ]; then
1359 echo "Input file "$1" does not exist!"
1360 exit 1
1361 fi
1362
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001363 if [ -n "$2" ]; then
1364 echo "Using section \"$2\""
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301365 LIST=$EXTRACT_TMP_DIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +02001366 # Match all lines starting with first line found to start* with '#'
1367 # comment and contain** $2, and ending with first line to be empty*.
1368 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
1369 # **the $2 match is case-insensitive
1370 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -04001371 else
1372 LIST=$1
1373 fi
1374
Steve Kondik5bd66602016-07-15 10:39:58 -07001375 PRODUCT_PACKAGES_LIST=()
1376 PRODUCT_PACKAGES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001377 PRODUCT_PACKAGES_FIXUP_HASHES=()
SamarV-1215556e2d2024-03-19 08:50:02 +05301378 PRODUCT_SYMLINKS_LIST=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001379 PRODUCT_COPY_FILES_LIST=()
1380 PRODUCT_COPY_FILES_HASHES=()
Vladimir Olteande985fe2019-01-17 03:07:34 +02001381 PRODUCT_COPY_FILES_FIXUP_HASHES=()
Steve Kondik5bd66602016-07-15 10:39:58 -07001382
1383 while read -r line; do
1384 if [ -z "$line" ]; then continue; fi
1385
1386 # If the line has a pipe delimiter, a sha1 hash should follow.
1387 # This indicates the file should be pinned and not overwritten
1388 # when extracting files.
1389 local SPLIT=(${line//\|/ })
1390 local COUNT=${#SPLIT[@]}
1391 local SPEC=${SPLIT[0]}
1392 local HASH="x"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001393 local FIXUP_HASH="x"
Steve Kondik5bd66602016-07-15 10:39:58 -07001394 if [ "$COUNT" -gt "1" ]; then
1395 HASH=${SPLIT[1]}
1396 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001397 if [ "$COUNT" -gt "2" ]; then
1398 FIXUP_HASH=${SPLIT[2]}
1399 fi
SamarV-1215556e2d2024-03-19 08:50:02 +05301400 if [[ "$SPEC" =~ 'SYMLINK=' ]]; then
1401 PRODUCT_SYMLINKS_LIST+=("${SPEC#-}")
1402 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001403 # if line starts with a dash, it needs to be packaged
1404 if [[ "$SPEC" =~ ^- ]]; then
1405 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
1406 PRODUCT_PACKAGES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001407 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Chirayu Desaia7741012021-12-04 07:17:28 +05301408 # if line contains apex, apk, jar or vintf fragment, it needs to be packaged
1409 elif suffix_match_file ".apex" "$(src_file "$SPEC")" || \
1410 suffix_match_file ".apk" "$(src_file "$SPEC")" || \
Michael Bestasea90aef2021-11-15 22:18:04 +02001411 suffix_match_file ".jar" "$(src_file "$SPEC")" || \
Aaron Kling7f4d1812023-11-02 16:04:22 -05001412 [[ "$TARGET_ENABLE_CHECKELF" == "true" && \
1413 ( "${SPEC%%;*}" == *".so" || \
1414 "$SPEC" == *"bin/"* || \
1415 "$SPEC" == *"lib/rfsa"* ) ]] || \
Michael Bestasea90aef2021-11-15 22:18:04 +02001416 [[ "$SPEC" == *"etc/vintf/manifest/"* ]]; then
1417 PRODUCT_PACKAGES_LIST+=("$SPEC")
1418 PRODUCT_PACKAGES_HASHES+=("$HASH")
1419 PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001420 else
1421 PRODUCT_COPY_FILES_LIST+=("$SPEC")
1422 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Vladimir Olteande985fe2019-01-17 03:07:34 +02001423 PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
Steve Kondik5bd66602016-07-15 10:39:58 -07001424 fi
1425
Chirayu Desaif1a21302022-09-13 00:29:33 +05301426 done < <(grep -v -E '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -07001427}
1428
1429#
1430# write_makefiles:
1431#
1432# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001433# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -07001434#
SamarV-1215556e2d2024-03-19 08:50:02 +05301435# Calls write_product_copy_files, write_product_packages and
1436# lastly write_symlink_packages on the given file and appends
1437# to the Android.bp as well as the product makefile.
Steve Kondik5bd66602016-07-15 10:39:58 -07001438#
1439function write_makefiles() {
1440 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -04001441 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -07001442 write_product_packages
SamarV-1215556e2d2024-03-19 08:50:02 +05301443 write_symlink_packages
Steve Kondik5bd66602016-07-15 10:39:58 -07001444}
1445
1446#
1447# append_firmware_calls_to_makefiles:
1448#
Michael Bestasf62d4032022-02-22 19:06:41 +02001449# $1: file containing the list of items to extract
1450#
Michael Bestasc72d0f62022-04-16 21:06:28 +03001451# Appends the calls to all images present in radio folder to Android.mk
1452# and radio AB_OTA_PARTITIONS to BoardConfigVendor.mk
Michael Bestasf62d4032022-02-22 19:06:41 +02001453#
Michael Bestasc72d0f62022-04-16 21:06:28 +03001454function append_firmware_calls_to_makefiles() {
Michael Bestasf62d4032022-02-22 19:06:41 +02001455 parse_file_list "$1"
1456
1457 local FILELIST=(${PRODUCT_COPY_FILES_LIST[@]})
1458 local COUNT=${#FILELIST[@]}
1459
SamarV-1217e56a0b2024-03-09 12:58:18 +05301460 if [[ ${FILELIST[*]} =~ ";AB" ]]; then
1461 printf '%s\n' "AB_OTA_PARTITIONS += \\" >> "$BOARDMK"
1462 fi
1463
Michael Bestasf62d4032022-02-22 19:06:41 +02001464 for (( i=1; i<COUNT+1; i++ )); do
1465 local DST_FILE=$(target_file "${FILELIST[$i-1]}")
1466 local ARGS=$(target_args "${FILELIST[$i-1]}")
LuK133711d86862023-12-11 01:34:29 +01001467 local SHA1=$(get_hash "$ANDROID_ROOT"/"$OUTDIR"/radio/"$DST_FILE")
Michael Bestasc72d0f62022-04-16 21:06:28 +03001468 DST_FILE_NAME=(${DST_FILE//.img/ })
Michael Bestasf62d4032022-02-22 19:06:41 +02001469 ARGS=(${ARGS//;/ })
1470 LINEEND=" \\"
1471 if [ "$i" -eq "$COUNT" ]; then
1472 LINEEND=""
1473 fi
1474
1475 for ARG in "${ARGS[@]}"; do
1476 if [[ "$ARG" =~ "AB" ]]; then
Michael Bestasc72d0f62022-04-16 21:06:28 +03001477 printf ' %s%s\n' "$DST_FILE_NAME" "$LINEEND" >> "$BOARDMK"
Michael Bestasf62d4032022-02-22 19:06:41 +02001478 fi
1479 done
LuK133711d86862023-12-11 01:34:29 +01001480 printf '%s\n' "\$(call add-radio-file-sha1-checked,radio/$DST_FILE,$SHA1)" >> "$ANDROIDMK"
Michael Bestasf62d4032022-02-22 19:06:41 +02001481 done
Michael Bestasc72d0f62022-04-16 21:06:28 +03001482 printf '\n' >> "$ANDROIDMK"
Michael Bestasf62d4032022-02-22 19:06:41 +02001483}
1484
1485#
Steve Kondik5bd66602016-07-15 10:39:58 -07001486# get_file:
1487#
1488# $1: input file
1489# $2: target file/folder
1490# $3: source of the file (can be "adb" or a local folder)
1491#
1492# Silently extracts the input file to defined target
1493# Returns success if file can be pulled from the device or found locally
1494#
1495function get_file() {
1496 local SRC="$3"
1497
1498 if [ "$SRC" = "adb" ]; then
1499 # try to pull
LuK13370f7f0d12022-08-19 21:49:56 +02001500 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
1501 adb pull "${1#/system}" "$2" >/dev/null 2>&1 && return 0
1502 adb pull "system/$1" "$2" >/dev/null 2>&1 && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001503
1504 return 1
1505 else
1506 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001507 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
1508 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +02001509 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -07001510
LuK1337dbb77cc2023-12-04 19:03:10 +01001511 # try /vendor/odm for devices without /odm partition
1512 [[ "$1" == /system/odm/* ]] && cp -r "$SRC/vendor/${1#/system}" "$2" 2>/dev/null && return 0
1513
Steve Kondik5bd66602016-07-15 10:39:58 -07001514 return 1
1515 fi
1516};
1517
1518#
1519# oat2dex:
1520#
1521# $1: extracted apk|jar (to check if deodex is required)
1522# $2: odexed apk|jar to deodex
1523# $3: source of the odexed apk|jar
1524#
1525# Convert apk|jar .odex in the corresposing classes.dex
1526#
1527function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +00001528 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -07001529 local OEM_TARGET="$2"
1530 local SRC="$3"
1531 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -05001532 local OAT=
Steve Kondik5bd66602016-07-15 10:39:58 -07001533
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001534 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Michael Bestas6d908932020-12-19 03:29:25 +02001535 export BAKSMALIJAR="$ANDROID_ROOT"/vendor/omni/build/tools/smali/baksmali.jar
1536 export SMALIJAR="$ANDROID_ROOT"/vendor/omni/build/tools/smali/smali.jar
Steve Kondik5bd66602016-07-15 10:39:58 -07001537 fi
1538
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001539 if [ -z "$VDEXEXTRACTOR" ]; then
micky3870cdacd02024-07-17 21:28:44 -04001540 export VDEXEXTRACTOR="$BINARIES_LOCATION"/vdexExtractor
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001541 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001542
codeworkx85eda752018-09-23 12:36:57 +02001543 if [ -z "$CDEXCONVERTER" ]; then
micky3870cdacd02024-07-17 21:28:44 -04001544 export CDEXCONVERTER="$BINARIES_LOCATION"/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +02001545 fi
1546
Steve Kondik5bd66602016-07-15 10:39:58 -07001547 # Extract existing boot.oats to the temp folder
1548 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001549 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -07001550 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301551 mkdir -p "$EXTRACT_TMP_DIR/system/framework/$ARCH"
1552 if get_file "/system/framework/$ARCH" "$EXTRACT_TMP_DIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001553 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -05001554 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301555 rmdir "$EXTRACT_TMP_DIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001556 fi
1557 done
1558 fi
1559
1560 if [ -z "$ARCHES" ]; then
1561 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
1562 fi
1563
theimpulson9a911af2019-08-14 03:25:12 +00001564 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001565 return;
1566 fi
1567
theimpulson9a911af2019-08-14 03:25:12 +00001568 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001569 return 0 # target apk|jar is already odexed, return
1570 fi
1571
1572 for ARCH in $ARCHES; do
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301573 BOOTOAT="$EXTRACT_TMP_DIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -07001574
Joe Maplesfb3941c2018-01-05 14:51:33 -05001575 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
1576 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001577
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301578 if get_file "$OAT" "$EXTRACT_TMP_DIR" "$SRC"; then
1579 if get_file "$VDEX" "$EXTRACT_TMP_DIR" "$SRC"; then
1580 "$VDEXEXTRACTOR" -o "$EXTRACT_TMP_DIR/" -i "$EXTRACT_TMP_DIR/$(basename "$VDEX")" > /dev/null
1581 CLASSES=$(ls "$EXTRACT_TMP_DIR/$(basename "${OEM_TARGET%.*}")_classes"*)
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001582 for CLASS in $CLASSES; do
1583 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1584 # Check if we have to deal with CompactDex
1585 if [[ "$CLASS" == *.cdex ]]; then
1586 "$CDEXCONVERTER" "$CLASS" &>/dev/null
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301587 mv "$CLASS.new" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001588 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301589 mv "$CLASS" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001590 fi
1591 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001592 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301593 java -jar "$BAKSMALIJAR" deodex -o "$EXTRACT_TMP_DIR/dexout" -b "$BOOTOAT" -d "$EXTRACT_TMP_DIR" "$EXTRACT_TMP_DIR/$(basename "$OAT")"
1594 java -jar "$SMALIJAR" assemble "$EXTRACT_TMP_DIR/dexout" -o "$EXTRACT_TMP_DIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001595 fi
theimpulson9a911af2019-08-14 03:25:12 +00001596 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301597 JAROAT="$EXTRACT_TMP_DIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +02001598 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -05001599 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +02001600 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -05001601 fi
Sebastiano Barezzie85075b2023-08-10 16:07:13 +02001602 if [ ! -f "$JARVDEX" ]; then
1603 JARVDEX="/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).vdex"
1604 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -05001605 # try to extract classes.dex from boot.vdex for frameworks jars
1606 # fallback to boot.oat if vdex is not available
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301607 if get_file "$JARVDEX" "$EXTRACT_TMP_DIR" "$SRC"; then
1608 "$VDEXEXTRACTOR" -o "$EXTRACT_TMP_DIR/" -i "$EXTRACT_TMP_DIR/$(basename "$JARVDEX")" > /dev/null
Sebastiano Barezzie85075b2023-08-10 16:07:13 +02001609 CLASSES=$(ls "$EXTRACT_TMP_DIR/$(basename "${JARVDEX%.*}")_classes"* 2> /dev/null)
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001610 for CLASS in $CLASSES; do
1611 NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
1612 # Check if we have to deal with CompactDex
1613 if [[ "$CLASS" == *.cdex ]]; then
1614 "$CDEXCONVERTER" "$CLASS" &>/dev/null
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301615 mv "$CLASS.new" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001616 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301617 mv "$CLASS" "$EXTRACT_TMP_DIR/$NEWCLASS"
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001618 fi
1619 done
Joe Maplesfb3941c2018-01-05 14:51:33 -05001620 else
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301621 java -jar "$BAKSMALIJAR" deodex -o "$EXTRACT_TMP_DIR/dexout" -b "$BOOTOAT" -d "$EXTRACT_TMP_DIR" "$JAROAT/$OEM_TARGET"
1622 java -jar "$SMALIJAR" assemble "$EXTRACT_TMP_DIR/dexout" -o "$EXTRACT_TMP_DIR/classes.dex"
Joe Maplesfb3941c2018-01-05 14:51:33 -05001623 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001624 else
1625 continue
1626 fi
1627
Steve Kondik5bd66602016-07-15 10:39:58 -07001628 done
Rashed Abdel-Tawabd7124972018-03-15 12:55:22 -07001629
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301630 rm -rf "$EXTRACT_TMP_DIR/dexout"
Steve Kondik5bd66602016-07-15 10:39:58 -07001631}
1632
1633#
1634# init_adb_connection:
1635#
1636# Starts adb server and waits for the device
1637#
1638function init_adb_connection() {
1639 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
1640 if ! _adb_connected; then
1641 echo "No device is online. Waiting for one..."
1642 echo "Please connect USB and/or enable USB debugging"
1643 until _adb_connected; do
1644 sleep 1
1645 done
1646 echo "Device Found."
1647 fi
1648
1649 # Retrieve IP and PORT info if we're using a TCP connection
Chirayu Desaif1a21302022-09-13 00:29:33 +05301650 TCPIPPORT=$(adb devices | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
Steve Kondik5bd66602016-07-15 10:39:58 -07001651 | head -1 | awk '{print $1}')
1652 adb root &> /dev/null
1653 sleep 0.3
1654 if [ -n "$TCPIPPORT" ]; then
1655 # adb root just killed our connection
1656 # so reconnect...
1657 adb connect "$TCPIPPORT"
1658 fi
1659 adb wait-for-device &> /dev/null
1660 sleep 0.3
1661}
1662
1663#
1664# fix_xml:
1665#
1666# $1: xml file to fix
1667#
1668function fix_xml() {
1669 local XML="$1"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301670 local TEMP_XML="$EXTRACT_TMP_DIR/`basename "$XML"`.temp"
Steve Kondik5bd66602016-07-15 10:39:58 -07001671
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +02001672 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
1673 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -07001674
1675 mv "$TEMP_XML" "$XML"
1676}
1677
Vladimir Olteande985fe2019-01-17 03:07:34 +02001678function get_hash() {
1679 local FILE="$1"
1680
1681 if [ "$(uname)" == "Darwin" ]; then
1682 shasum "${FILE}" | awk '{print $1}'
1683 else
1684 sha1sum "${FILE}" | awk '{print $1}'
1685 fi
1686}
1687
Vladimir Olteana7d20492019-01-17 03:05:52 +02001688function print_spec() {
1689 local SPEC_PRODUCT_PACKAGE="$1"
1690 local SPEC_SRC_FILE="$2"
1691 local SPEC_DST_FILE="$3"
1692 local SPEC_ARGS="$4"
1693 local SPEC_HASH="$5"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001694 local SPEC_FIXUP_HASH="$6"
Vladimir Olteana7d20492019-01-17 03:05:52 +02001695
1696 local PRODUCT_PACKAGE=""
1697 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
1698 PRODUCT_PACKAGE="-"
1699 fi
1700 local SRC=""
1701 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
1702 SRC="${SPEC_SRC_FILE}:"
1703 fi
1704 local DST=""
1705 if [ ! -z "${SPEC_DST_FILE}" ]; then
1706 DST="${SPEC_DST_FILE}"
1707 fi
1708 local ARGS=""
1709 if [ ! -z "${SPEC_ARGS}" ]; then
1710 ARGS=";${SPEC_ARGS}"
1711 fi
1712 local HASH=""
1713 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
1714 HASH="|${SPEC_HASH}"
1715 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02001716 local FIXUP_HASH=""
1717 if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
1718 FIXUP_HASH="|${SPEC_FIXUP_HASH}"
1719 fi
1720 printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
1721}
1722
1723# To be overridden by device-level extract-files.sh
1724# Parameters:
1725# $1: spec name of a blob. Can be used for filtering.
1726# If the spec is "src:dest", then $1 is "dest".
1727# If the spec is "src", then $1 is "src".
1728# $2: path to blob file. Can be used for fixups.
1729#
1730function blob_fixup() {
1731 :
Vladimir Olteana7d20492019-01-17 03:05:52 +02001732}
1733
Aaron Klingb2649192023-08-26 15:04:17 -05001734# To be overridden by device-level extract-files.sh
1735# Parameters:
1736# $1: Path to vendor Android.bp
1737#
1738function vendor_imports() {
1739 :
1740}
1741
Steve Kondik5bd66602016-07-15 10:39:58 -07001742#
Michael Bestasf458ca02023-06-12 14:20:28 +03001743# prepare_images:
Steve Kondik5bd66602016-07-15 10:39:58 -07001744#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001745# Positional parameters:
Michael Bestasf458ca02023-06-12 14:20:28 +03001746# $1: path to extracted system folder or an ota zip file
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001747#
Michael Bestasf458ca02023-06-12 14:20:28 +03001748function prepare_images() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001749 # Consume positional parameters
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001750 local SRC="$1"; shift
Michael Bestasfc97f2d2023-06-12 13:53:31 +03001751 local KEEP_DUMP_DIR="$SRC"
Steve Kondik5bd66602016-07-15 10:39:58 -07001752
Dan Pasanen0cc05012017-03-21 09:06:11 -05001753 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
Michael Bestas53125372023-12-13 18:06:36 +02001754 local BASENAME=$(basename "$SRC")
1755 local DIRNAME=$(dirname "$SRC")
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301756 DUMPDIR="$EXTRACT_TMP_DIR"/system_dump
Michael Bestas53125372023-12-13 18:06:36 +02001757 KEEP_DUMP_DIR="$DIRNAME"/"${BASENAME%.zip}"
1758 if [ "$KEEP_DUMP" == "true" ] || [ "$KEEP_DUMP" == "1" ]; then
1759 rm -rf "$KEEP_DUMP_DIR"
1760 mkdir "$KEEP_DUMP_DIR"
1761 fi
Dan Pasanen0cc05012017-03-21 09:06:11 -05001762
1763 # Check if we're working with the same zip that was passed last time.
1764 # If so, let's just use what's already extracted.
1765 MD5=`md5sum "$SRC"| awk '{print $1}'`
1766 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1767
1768 if [ "$MD5" != "$OLDMD5" ]; then
1769 rm -rf "$DUMPDIR"
1770 mkdir "$DUMPDIR"
1771 unzip "$SRC" -d "$DUMPDIR"
1772 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1773
Chirayu Desai817dc762021-11-26 05:47:25 +05301774 # Extract A/B OTA
Dan Pasanen0cc05012017-03-21 09:06:11 -05001775 if [ -a "$DUMPDIR"/payload.bin ]; then
Michael Bestasc0e50db2023-12-13 19:43:49 +02001776 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
1777 do
1778 "$OTA_EXTRACTOR" --payload "$DUMPDIR"/payload.bin --output_dir "$DUMPDIR" --partitions "$PARTITION" &2>&1
1779 done
1780 wait
Dan Pasanen0cc05012017-03-21 09:06:11 -05001781 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001782
Luca Stefani776be462020-09-09 15:53:58 +02001783 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
dianlujitao85ddca62020-04-21 23:03:20 +08001784 do
1785 # If OTA is block based, extract it.
dianlujitaoe2cbe262020-04-21 23:01:13 +08001786 if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
1787 echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
1788 brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
1789 rm "$DUMPDIR"/"$PARTITION".new.dat.br
1790 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001791 if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
1792 echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
Michael Bestas6d908932020-12-19 03:29:25 +02001793 python "$ANDROID_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
dianlujitao85ddca62020-04-21 23:03:20 +08001794 rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
1795 mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
Chirayu Desai62ed12a2021-11-26 05:47:25 +05301796 extract_img_data "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/"$PARTITION"/
1797 rm "$DUMPDIR"/"$PARTITION".img
dianlujitao85ddca62020-04-21 23:03:20 +08001798 fi
Chirayu Desai817dc762021-11-26 05:47:25 +05301799 if [ -a "$DUMPDIR"/"$PARTITION".img ]; then
1800 extract_img_data "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/"$PARTITION"/
1801 fi
dianlujitao85ddca62020-04-21 23:03:20 +08001802 done
Dan Pasanen0cc05012017-03-21 09:06:11 -05001803 fi
1804
Michael Bestasfc97f2d2023-06-12 13:53:31 +03001805 if [ "$KEEP_DUMP" == "true" ] || [ "$KEEP_DUMP" == "1" ]; then
1806 rm -rf "$KEEP_DUMP_DIR"/system_dump
1807 cp -a "$DUMPDIR" "$KEEP_DUMP_DIR"/system_dump
1808 fi
1809
Dan Pasanen0cc05012017-03-21 09:06:11 -05001810 SRC="$DUMPDIR"
1811 fi
1812
Michael Bestasf2501c32022-02-18 23:45:09 +02001813 if [ -d "$SRC" ] && [ -f "$SRC"/super.img ]; then
1814 DUMPDIR="$TMPDIR"/super_dump
1815 mkdir -p "$DUMPDIR"
1816
1817 echo "Unpacking super.img"
1818 "$SIMG2IMG" "$SRC"/super.img "$DUMPDIR"/super.raw
1819
1820 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
1821 do
1822 echo "Preparing "$PARTITION""
1823 if "$LPUNPACK" -p "$PARTITION"_a "$DUMPDIR"/super.raw "$DUMPDIR" ; then
1824 mv "$DUMPDIR"/"$PARTITION"_a.img "$DUMPDIR"/"$PARTITION".img
1825 else
1826 "$LPUNPACK" -p "$PARTITION" "$DUMPDIR"/super.raw "$DUMPDIR"
1827 fi
1828 done
Michael Bestas7c01b952023-06-12 14:21:54 +03001829 rm "$DUMPDIR"/super.raw
Michael Bestasf2501c32022-02-18 23:45:09 +02001830
Michael Bestasfc97f2d2023-06-12 13:53:31 +03001831 if [ "$KEEP_DUMP" == "true" ] || [ "$KEEP_DUMP" == "1" ]; then
1832 rm -rf "$KEEP_DUMP_DIR"/super_dump
1833 cp -a "$DUMPDIR" "$KEEP_DUMP_DIR"/super_dump
1834 fi
1835
Michael Bestasf2501c32022-02-18 23:45:09 +02001836 SRC="$DUMPDIR"
1837 fi
1838
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301839 if [ -d "$SRC" ] && [ -f "$SRC"/system.img ]; then
Chirayu Desai5da0bf82022-10-19 02:58:42 +05301840 DUMPDIR="$EXTRACT_TMP_DIR"/system_dump
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301841 mkdir -p "$DUMPDIR"
1842
1843 for PARTITION in "system" "odm" "product" "system_ext" "vendor"
1844 do
1845 echo "Extracting "$PARTITION""
1846 local IMAGE="$SRC"/"$PARTITION".img
1847 if [ -f "$IMAGE" ]; then
LuK133707657db2023-11-30 18:11:51 +01001848 if [[ $(file -b "$IMAGE") == EROFS* ]]; then
1849 fsck.erofs --extract="$DUMPDIR"/"$PARTITION" "$IMAGE"
1850 elif [[ $(file -b "$IMAGE") == Linux* ]]; then
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301851 extract_img_data "$IMAGE" "$DUMPDIR"/"$PARTITION"
1852 elif [[ $(file -b "$IMAGE") == Android* ]]; then
Chirayu Desai501ffd62022-03-17 06:27:33 +05301853 "$SIMG2IMG" "$IMAGE" "$DUMPDIR"/"$PARTITION".raw
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301854 extract_img_data "$DUMPDIR"/"$PARTITION".raw "$DUMPDIR"/"$PARTITION"/
1855 else
1856 echo "Unsupported "$IMAGE""
1857 fi
1858 fi
1859 done
1860
Michael Bestasfc97f2d2023-06-12 13:53:31 +03001861 if [ "$KEEP_DUMP" == "true" ] || [ "$KEEP_DUMP" == "1" ]; then
1862 rm -rf "$KEEP_DUMP_DIR"/output
1863 cp -a "$DUMPDIR" "$KEEP_DUMP_DIR"/output
1864 fi
1865
Chirayu Desaia3850bd2021-11-26 05:47:25 +05301866 SRC="$DUMPDIR"
1867 fi
1868
Michael Bestasf458ca02023-06-12 14:20:28 +03001869 EXTRACT_SRC="$SRC"
1870 EXTRACT_STATE=1
1871}
1872
1873#
1874# extract:
1875#
1876# Positional parameters:
1877# $1: file containing the list of items to extract (aka proprietary-files.txt)
1878# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
1879# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
1880#
1881# Non-positional parameters (coming after $2):
1882# --section: preferred way of selecting the portion to parse and extract from
1883# proprietary-files.txt
1884# --kang: if present, this option will activate the printing of hashes for the
1885# extracted blobs. Useful with --section for subsequent pinning of
1886# blobs taken from other origins.
1887#
1888function extract() {
1889 # Consume positional parameters
1890 local PROPRIETARY_FILES_TXT="$1"; shift
1891 local SRC="$1"; shift
1892 local SECTION=""
1893 local KANG=false
1894
1895 # Consume optional, non-positional parameters
1896 while [ "$#" -gt 0 ]; do
1897 case "$1" in
1898 -s|--section)
1899 SECTION="$2"; shift
1900 ;;
1901 -k|--kang)
1902 KANG=true
1903 DISABLE_PINNING=1
1904 ;;
1905 *)
1906 # Backwards-compatibility with the old behavior, where $3, if
1907 # present, denoted an optional positional ${SECTION} argument.
1908 # Users of ${SECTION} are encouraged to migrate from setting it as
1909 # positional $3, to non-positional --section ${SECTION}, the
1910 # reason being that it doesn't scale to have more than 1 optional
1911 # positional argument.
1912 SECTION="$1"
1913 ;;
1914 esac
1915 shift
1916 done
1917
1918 if [ -z "$OUTDIR" ]; then
1919 echo "Output dir not set!"
1920 exit 1
1921 fi
1922
1923 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
1924
1925 # Allow failing, so we can try $DEST and/or $FILE
1926 set +e
1927
1928 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
1929 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
1930 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
1931 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
1932 local COUNT=${#FILELIST[@]}
1933 local OUTPUT_ROOT="$ANDROID_ROOT"/"$OUTDIR"/proprietary
1934 local OUTPUT_TMP="$EXTRACT_TMP_DIR"/"$OUTDIR"/proprietary
1935
1936 if [ "$SRC" = "adb" ]; then
1937 init_adb_connection
1938 fi
1939
1940 if [ "$EXTRACT_STATE" -ne "1" ]; then
1941 prepare_images "$SRC"
1942 fi
1943
Steve Kondik5bd66602016-07-15 10:39:58 -07001944 if [ "$VENDOR_STATE" -eq "0" ]; then
1945 echo "Cleaning output directory ($OUTPUT_ROOT).."
1946 rm -rf "${OUTPUT_TMP:?}"
1947 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001948 if [ -d "$OUTPUT_ROOT" ]; then
1949 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1950 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001951 VENDOR_STATE=1
1952 fi
1953
Michael Bestasf458ca02023-06-12 14:20:28 +03001954 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${EXTRACT_SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001955
1956 for (( i=1; i<COUNT+1; i++ )); do
1957
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001958 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001959 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001960 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001961 local OUTPUT_DIR=
1962 local TMP_DIR=
1963 local SRC_FILE=
1964 local DST_FILE=
Vladimir Olteana7d20492019-01-17 03:05:52 +02001965 local IS_PRODUCT_PACKAGE=false
1966
1967 # Note: this relies on the fact that the ${FILELIST[@]} array
1968 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1969 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1970 IS_PRODUCT_PACKAGE=true
1971 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001972
Michael Bestasbda30202020-12-28 04:44:52 +02001973 OUTPUT_DIR="${OUTPUT_ROOT}"
1974 TMP_DIR="${OUTPUT_TMP}"
1975 SRC_FILE="/system/${SPEC_SRC_FILE}"
1976 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001977
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001978 # Strip the file path in the vendor repo of "system", if present
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001979 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
dianlujitao4ddcfb72020-04-06 12:43:16 +08001980 local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001981 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001982
Gabriele M58270a32017-11-13 23:15:29 +01001983 # Check pinned files
Vladimir Olteane688cf92019-01-17 02:47:02 +02001984 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Vladimir Olteande985fe2019-01-17 03:07:34 +02001985 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele M58270a32017-11-13 23:15:29 +01001986 local KEEP=""
Vladimir Olteande985fe2019-01-17 03:07:34 +02001987 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001988 if [ -f "${VENDOR_REPO_FILE}" ]; then
1989 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001990 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001991 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001992 fi
1993 if [ -f "$PINNED" ]; then
Vladimir Olteande985fe2019-01-17 03:07:34 +02001994 local TMP_HASH=$(get_hash "${PINNED}")
1995 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
Gabriele M58270a32017-11-13 23:15:29 +01001996 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001997 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1998 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001999 fi
2000 fi
2001 fi
2002 fi
2003
Vladimir Olteana7d20492019-01-17 03:05:52 +02002004 if [ "${KANG}" = false ]; then
2005 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
2006 fi
2007
Gabriele M58270a32017-11-13 23:15:29 +01002008 if [ "$KEEP" = "1" ]; then
Arian2d802382021-09-09 15:18:35 +02002009 if [ "${FIXUP_HASH}" != "x" ]; then
2010 printf ' + keeping pinned file with hash %s\n' "${FIXUP_HASH}"
2011 else
2012 printf ' + keeping pinned file with hash %s\n' "${HASH}"
2013 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07002014 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03002015 FOUND=false
2016 # Try Lineage target first.
2017 # Also try to search for files stripped of
2018 # the "/system" prefix, if we're actually extracting
2019 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03002020 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Michael Bestasf458ca02023-06-12 14:20:28 +03002021 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${EXTRACT_SRC} && {
Vladimir Olteanb5500d72018-06-24 21:06:12 +03002022 FOUND=true
2023 break
2024 }
2025 done
2026
2027 if [ "${FOUND}" = false ]; then
Bruno Martins74e00eb2021-04-10 14:36:50 +01002028 colored_echo red " !! ${BLOB_DISPLAY_NAME}: file not found in source"
Vladimir Oltean11329372018-10-18 00:44:02 +03002029 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07002030 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07002031
Arian5f98d792021-09-09 15:24:25 +02002032 # Blob fixup pipeline has 2 parts: one that is fixed and
2033 # one that is user-configurable
2034 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2035 # Deodex apk|jar if that's the case
2036 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
Michael Bestasf458ca02023-06-12 14:20:28 +03002037 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$EXTRACT_SRC"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302038 if [ -f "$EXTRACT_TMP_DIR/classes.dex" ]; then
2039 touch -t 200901010000 "$EXTRACT_TMP_DIR/classes"*
2040 zip -gjq "${VENDOR_REPO_FILE}" "$EXTRACT_TMP_DIR/classes"*
2041 rm "$EXTRACT_TMP_DIR/classes"*
Arian5f98d792021-09-09 15:24:25 +02002042 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
2043 fi
2044 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
2045 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07002046 fi
Arian5f98d792021-09-09 15:24:25 +02002047 # Now run user-supplied fixup function
2048 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
2049 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
Steve Kondik5bd66602016-07-15 10:39:58 -07002050
Arian5f98d792021-09-09 15:24:25 +02002051 if [ -f "${VENDOR_REPO_FILE}" ]; then
2052 local DIR=$(dirname "${VENDOR_REPO_FILE}")
2053 local TYPE="${DIR##*/}"
Michael Bestasbda30202020-12-28 04:44:52 +02002054 if [ "$TYPE" = "bin" ]; then
Arian5f98d792021-09-09 15:24:25 +02002055 chmod 755 "${VENDOR_REPO_FILE}"
2056 else
2057 chmod 644 "${VENDOR_REPO_FILE}"
2058 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07002059 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07002060
Arian5f98d792021-09-09 15:24:25 +02002061 if [ "${KANG}" = true ]; then
2062 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
2063 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02002064
Arian5f98d792021-09-09 15:24:25 +02002065 # Check and print whether the fixup pipeline actually did anything.
2066 # This isn't done right after the fixup pipeline because we want this print
2067 # to come after print_spec above, when in kang mode.
2068 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
2069 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
2070 # Now sanity-check the spec for this blob.
2071 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
2072 colored_echo yellow "WARNING: The ${BLOB_DISPLAY_NAME} file was fixed up, but it is pinned."
2073 colored_echo yellow "This is a mistake and you want to either remove the hash completely, or add an extra one."
2074 fi
Vladimir Olteande985fe2019-01-17 03:07:34 +02002075 fi
Vladimir Olteana7d20492019-01-17 03:05:52 +02002076 fi
2077
Steve Kondik5bd66602016-07-15 10:39:58 -07002078 done
2079
2080 # Don't allow failing
2081 set -e
2082}
2083
2084#
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04002085# extract2:
2086#
2087# Positional parameters:
2088# $1: file containing the list of items to extract (aka proprietary-files.txt)
2089#
2090# Non-positional parameters (coming after $2):
2091# --section: selects the portion to parse and extracts from proprietary-files.txt
2092# --kang: if present, this option will activate the printing of hashes for the
2093# extracted blobs. Useful with --section for subsequent pinning of
2094# blobs taken from other origins.
2095#
2096function extract2() {
2097 # Consume positional parameters
2098 local PROPRIETARY_FILES_TXT="$1"; shift
2099 local SECTION=""
2100 local KANG=false
2101
2102 # Consume optional, non-positional parameters
2103 while [ "$#" -gt 0 ]; do
2104 case "$1" in
2105 --adb)
2106 ADB=true
2107 ;;
2108 --system)
2109 SYSTEM_SRC="$2"; shift
2110 ;;
2111 --vendor)
2112 VENDOR_SRC="$2"; shift
2113 ;;
2114 --odm)
2115 ODM_SRC="$2"; shift
2116 ;;
2117 --product)
2118 PRODUCT_SRC="$2"; shift
2119 ;;
2120 -s|--section)
2121 SECTION="$2"; shift
2122 ;;
2123 -k|--kang)
2124 KANG=true
2125 DISABLE_PINNING=1
2126 ;;
2127 esac
2128 shift
2129 done
2130
2131 if [ -z "$ADB" ] || [ -z "$SYSTEM_SRC" && -z "$VENDOR_SRC" && -z "$ODM_SRC" && -z "$PRODUCT_SRC" ]; then
2132 echo "No sources set! You must select --adb or pass paths to partition dumps."
2133 exit 1
2134 fi
2135
2136 if [ -z "$OUTDIR" ]; then
2137 echo "Output dir not set!"
2138 exit 1
2139 fi
2140
2141 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
2142
2143 # Allow failing, so we can try $DEST and/or $FILE
2144 set +e
2145
2146 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
2147 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
2148 local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
2149 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
2150 local COUNT=${#FILELIST[@]}
Michael Bestas6d908932020-12-19 03:29:25 +02002151 local OUTPUT_ROOT="$ANDROID_ROOT"/"$OUTDIR"/proprietary
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302152 local OUTPUT_TMP="$EXTRACT_TMP_DIR"/"$OUTDIR"/proprietary
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04002153
2154 if [ "$ADB" = true ]; then
2155 init_adb_connection
2156 fi
2157
2158 if [ "$VENDOR_STATE" -eq "0" ]; then
2159 echo "Cleaning output directory ($OUTPUT_ROOT).."
2160 rm -rf "${OUTPUT_TMP:?}"
2161 mkdir -p "${OUTPUT_TMP:?}"
2162 if [ -d "$OUTPUT_ROOT" ]; then
2163 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
2164 fi
2165 VENDOR_STATE=1
2166 fi
2167
2168 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
2169
2170 for (( i=1; i<COUNT+1; i++ )); do
2171
2172 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
2173 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
2174 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
2175 local OUTPUT_DIR=
2176 local TMP_DIR=
2177 local SRC_FILE=
2178 local DST_FILE=
2179 local IS_PRODUCT_PACKAGE=false
2180
2181 # Note: this relies on the fact that the ${FILELIST[@]} array
2182 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
2183 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
2184 IS_PRODUCT_PACKAGE=true
2185 fi
2186
2187 if [ "${SPEC_ARGS}" = "rootfs" ]; then
2188 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
2189 TMP_DIR="${OUTPUT_TMP}/rootfs"
2190 else
2191 OUTPUT_DIR="${OUTPUT_ROOT}"
2192 TMP_DIR="${OUTPUT_TMP}"
2193 fi
2194 SRC_FILE="${SPEC_SRC_FILE}"
2195 DST_FILE="${SPEC_DST_FILE}"
2196
2197 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE}"
2198 local BLOB_DISPLAY_NAME="${DST_FILE}"
2199 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
2200
2201 # Check pinned files
2202 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
2203 local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
2204 local KEEP=""
2205 if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
2206 if [ -f "${VENDOR_REPO_FILE}" ]; then
2207 local PINNED="${VENDOR_REPO_FILE}"
2208 else
2209 local PINNED="${TMP_DIR}${DST_FILE}"
2210 fi
2211 if [ -f "$PINNED" ]; then
2212 local TMP_HASH=$(get_hash "${PINNED}")
2213 if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
2214 KEEP="1"
2215 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
2216 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
2217 fi
2218 fi
2219 fi
2220 fi
2221
2222 if [ "${KANG}" = false ]; then
2223 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
2224 fi
2225
2226 if [ "$KEEP" = "1" ]; then
2227 printf ' + keeping pinned file with hash %s\n' "${HASH}"
2228 else
2229 FOUND=false
2230 PARTITION_SOURCE_DIR=
2231 # Try Lineage target first.
2232 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
2233 PARTITION=$(echo "$CANDIDATE" | cut -d/ -f1)
2234 if [ "$PARTITION" = "system" ]; then
2235 PARTITION_SOURCE_DIR="$SYSTEM_SRC"
2236 elif [ "$PARTITION" = "vendor" ]; then
2237 PARTITION_SOURCE_DIR="$VENDOR_SRC"
2238 elif [ "$PARTITION" = "product" ]; then
2239 PARTITION_SOURCE_DIR="$PRODUCT_SRC"
2240 elif [ "$PARTITION" = "odm" ]; then
2241 PARTITION_SOURCE_DIR="$ODM_SRC"
2242 fi
2243 CANDIDATE_RELATIVE_NAME=$(echo "$CANDIDATE" | cut -d/ -f2-)
2244 get_file ${CANDIDATE_RELATIVE_NAME} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
2245 FOUND=true
2246 break
2247 }
2248 # Search with the full system/ prefix if the file was not found on the system partition
2249 # because we may be searching in a mounted system-as-root system.img
2250 if [[ "${FOUND}" = false && "$PARTITION" = "system" ]]; then
2251 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${PARTITION_SOURCE_DIR} && {
2252 FOUND=true
2253 break
2254 }
2255 fi
2256 done
2257
2258 if [ -z "${PARTITION_SOURCE_DIR}" ]; then
2259 echo "$CANDIDATE has no preceeding partition path. Prepend system/, vendor/, product/, or odm/ to this entry."
2260 fi
2261
2262 if [ "${FOUND}" = false ]; then
2263 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
2264 continue
2265 fi
2266 fi
2267
2268 # Blob fixup pipeline has 2 parts: one that is fixed and
2269 # one that is user-configurable
2270 local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2271 # Deodex apk|jar if that's the case
2272 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
2273 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "${SYSTEM_SRC}"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302274 if [ -f "$EXTRACT_TMP_DIR/classes.dex" ]; then
2275 zip -gjq "${VENDOR_REPO_FILE}" "$EXTRACT_TMP_DIR/classes"*
2276 rm "$EXTRACT_TMP_DIR/classes"*
Rashed Abdel-Tawab5b97a982019-09-29 01:19:57 -04002277 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
2278 fi
2279 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
2280 fix_xml "${VENDOR_REPO_FILE}"
2281 fi
2282 # Now run user-supplied fixup function
2283 blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
2284 local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
2285
2286 if [ -f "${VENDOR_REPO_FILE}" ]; then
2287 local DIR=$(dirname "${VENDOR_REPO_FILE}")
2288 local TYPE="${DIR##*/}"
2289 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
2290 chmod 755 "${VENDOR_REPO_FILE}"
2291 else
2292 chmod 644 "${VENDOR_REPO_FILE}"
2293 fi
2294 fi
2295
2296 if [ "${KANG}" = true ]; then
2297 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
2298 fi
2299
2300 # Check and print whether the fixup pipeline actually did anything.
2301 # This isn't done right after the fixup pipeline because we want this print
2302 # to come after print_spec above, when in kang mode.
2303 if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
2304 printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
2305 # Now sanity-check the spec for this blob.
2306 if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
2307 printf "WARNING: The %s file was fixed up, but it is pinned.\n" ${BLOB_DISPLAY_NAME}
2308 printf "This is a mistake and you want to either remove the hash completely, or add an extra one.\n"
2309 fi
2310 fi
2311
2312 done
2313
2314 # Don't allow failing
2315 set -e
2316}
2317
2318#
Michael Bestasf3c59082023-11-30 20:26:02 +02002319# To be overridden by device-level extract-files.sh
2320#
2321function prepare_firmware() {
2322 :
2323}
2324
2325#
Steve Kondik5bd66602016-07-15 10:39:58 -07002326# extract_firmware:
2327#
2328# $1: file containing the list of items to extract
2329# $2: path to extracted radio folder
2330#
2331function extract_firmware() {
2332 if [ -z "$OUTDIR" ]; then
2333 echo "Output dir not set!"
2334 exit 1
2335 fi
2336
2337 parse_file_list "$1"
2338
2339 # Don't allow failing
2340 set -e
2341
2342 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
2343 local COUNT=${#FILELIST[@]}
2344 local SRC="$2"
Michael Bestas6d908932020-12-19 03:29:25 +02002345 local OUTPUT_DIR="$ANDROID_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07002346
2347 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
2348 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
2349 rm -rf "${OUTPUT_DIR:?}/"*
2350 VENDOR_RADIO_STATE=1
2351 fi
2352
2353 echo "Extracting $COUNT files in $1 from $SRC:"
2354
Michael Bestasf3c59082023-11-30 20:26:02 +02002355 prepare_firmware
2356
Steve Kondik5bd66602016-07-15 10:39:58 -07002357 for (( i=1; i<COUNT+1; i++ )); do
Michael Bestasf62d4032022-02-22 19:06:41 +02002358 local SRC_FILE=$(src_file "${FILELIST[$i-1]}")
2359 local DST_FILE=$(target_file "${FILELIST[$i-1]}")
Michael Bestas6975c642023-07-18 15:55:55 +03002360 local COPY_FILE=
Michael Bestasf62d4032022-02-22 19:06:41 +02002361
2362 printf ' - %s \n' "radio/$DST_FILE"
Steve Kondik5bd66602016-07-15 10:39:58 -07002363
2364 if [ ! -d "$OUTPUT_DIR" ]; then
2365 mkdir -p "$OUTPUT_DIR"
2366 fi
Chirayu Desai10418682022-05-25 23:37:27 +05302367 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
2368 # Extract A/B OTA
2369 if [ -a "$DUMPDIR"/payload.bin ]; then
Michael Bestasd898eea2023-12-13 18:00:41 +02002370 "$OTA_EXTRACTOR" --payload "$DUMPDIR"/payload.bin --output_dir "$DUMPDIR" --partitions $(basename "${DST_FILE%.*}") 2>&1
Michael Bestas6975c642023-07-18 15:55:55 +03002371 if [ -f "$DUMPDIR/$(basename $DST_FILE)" ]; then
2372 COPY_FILE="$DUMPDIR/$(basename $DST_FILE)"
2373 fi
Chirayu Desai10418682022-05-25 23:37:27 +05302374 fi
Michael Bestased3cae92023-04-13 16:38:34 +03002375 else
Chirayu Desai10418682022-05-25 23:37:27 +05302376 if [ -f "$SRC/$SRC_FILE" ]; then
Michael Bestas6975c642023-07-18 15:55:55 +03002377 COPY_FILE="$SRC/$SRC_FILE"
2378 elif [ -f "$SRC/$DST_FILE" ]; then
2379 COPY_FILE="$SRC/$DST_FILE"
Chirayu Desai10418682022-05-25 23:37:27 +05302380 fi
Michael Bestas2664f082024-01-02 23:08:02 +02002381 if [[ $(file -b "$COPY_FILE") == Android* ]]; then
2382 "$SIMG2IMG" "$COPY_FILE" "$SRC"/"$(basename "$COPY_FILE").raw"
2383 COPY_FILE="$SRC"/"$(basename "$COPY_FILE").raw"
2384 fi
Michael Bestased3cae92023-04-13 16:38:34 +03002385 fi
Michael Bestas6975c642023-07-18 15:55:55 +03002386
2387 if [ -f "$COPY_FILE" ]; then
2388 cp "$COPY_FILE" "$OUTPUT_DIR/$DST_FILE"
2389 chmod 644 "$OUTPUT_DIR/$DST_FILE"
2390 else
2391 colored_echo yellow "${DST_FILE} not found, skipping copy"
2392 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07002393 done
2394}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002395
2396function extract_img_data() {
2397 local image_file="$1"
2398 local out_dir="$2"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302399 local logFile="$EXTRACT_TMP_DIR/debugfs.log"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002400
2401 if [ ! -d "$out_dir" ]; then
2402 mkdir -p "$out_dir"
2403 fi
2404
2405 if [[ "$HOST_OS" == "Darwin" ]]; then
2406 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
2407 echo "[-] Failed to extract data from '$image_file'"
2408 abort 1
2409 }
2410 else
2411 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
2412 do
2413 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
2414 echo "[-] Failed to extract data from '$image_file'"
2415 abort 1
2416 }
2417 done
2418 fi
2419
2420 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
2421 if grep -Fq "$symlink_err" "$logFile"; then
2422 echo "[-] Symlinks have not been properly processed from $image_file"
Michael Bestas1b570252021-12-02 21:05:36 +02002423 echo "[!] You might not have a compatible debugfs version"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002424 abort 1
2425 fi
2426}
2427
2428declare -ra VENDOR_SKIP_FILES=(
2429 "bin/toybox_vendor"
2430 "bin/toolbox"
2431 "bin/grep"
2432 "build.prop"
2433 "compatibility_matrix.xml"
2434 "default.prop"
2435 "etc/NOTICE.xml.gz"
2436 "etc/vintf/compatibility_matrix.xml"
2437 "etc/vintf/manifest.xml"
2438 "etc/wifi/wpa_supplicant.conf"
2439 "manifest.xml"
2440 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
2441 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
2442 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
2443 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
2444 "overlay/framework-res__auto_generated_rro.apk"
2445 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
2446)
2447
2448function array_contains() {
2449 local element
2450 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
2451 return 1
2452}
2453
2454function generate_prop_list_from_image() {
2455 local image_file="$1"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302456 local image_dir="$EXTRACT_TMP_DIR/image-temp"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002457 local output_list="$2"
Chirayu Desai5da0bf82022-10-19 02:58:42 +05302458 local output_list_tmp="$EXTRACT_TMP_DIR/_proprietary-blobs.txt"
Michael Bestasca5d78a2021-12-02 20:58:40 +02002459 local -n skipped_files="$3"
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002460 local component="$4"
2461 local partition="$component"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002462
Chirayu Desai203cc522021-12-04 01:18:45 +05302463 mkdir -p "$image_dir"
2464
LuK133707657db2023-11-30 18:11:51 +01002465 if [[ $(file -b "$image_file") == EROFS* ]]; then
2466 fsck.erofs --extract="$image_dir" "$image_file"
2467 elif [[ $(file -b "$image_file") == Linux* ]]; then
Chirayu Desai203cc522021-12-04 01:18:45 +05302468 extract_img_data "$image_file" "$image_dir"
2469 elif [[ $(file -b "$image_file") == Android* ]]; then
Chirayu Desai501ffd62022-03-17 06:27:33 +05302470 "$SIMG2IMG" "$image_file" "$image_dir"/"$(basename "$image_file").raw"
Chirayu Desai203cc522021-12-04 01:18:45 +05302471 extract_img_data "$image_dir"/"$(basename "$image_file").raw" "$image_dir"
2472 rm "$image_dir"/"$(basename "$image_file").raw"
2473 else
2474 echo "Unsupported "$image_file""
2475 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002476
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002477 if [ -z "$component" ]; then
2478 partition="vendor"
2479 elif [[ "$component" == "carriersettings" ]]; then
2480 partition="product"
2481 fi
2482
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002483 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
2484 do
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002485 if [[ "$component" == "carriersettings" ]] && ! prefix_match_file "etc/CarrierSettings" "$FILE" ; then
2486 continue
2487 fi
Alessandro Astone60697c42020-12-04 01:04:01 +01002488 if suffix_match_file ".odex" "$FILE" || suffix_match_file ".vdex" "$FILE" ; then
2489 continue
2490 fi
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002491 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
2492 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
2493 continue
2494 fi
2495 # Skip device defined skipped files since they will be re-generated at build time
Michael Bestasca5d78a2021-12-02 20:58:40 +02002496 if array_contains "$FILE" "${skipped_files[@]}"; then
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002497 continue
2498 fi
Michael Bestasfc1a22e2023-06-11 17:45:46 +03002499 echo "$partition/$FILE" >> "$output_list_tmp"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002500 done
2501
2502 # Sort merged file with all lists
Michael Bestas1e6efb32021-11-15 19:28:56 +02002503 LC_ALL=C sort -u "$output_list_tmp" > "$output_list"
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07002504
2505 # Clean-up
2506 rm -f "$output_list_tmp"
2507}
Bruno Martins0f425f12021-04-10 14:57:32 +01002508
2509function colored_echo() {
2510 IFS=" "
2511 local color=$1;
2512 shift
2513 if ! [[ $color =~ '^[0-9]$' ]] ; then
2514 case $(echo $color | tr '[:upper:]' '[:lower:]') in
2515 black) color=0 ;;
2516 red) color=1 ;;
2517 green) color=2 ;;
2518 yellow) color=3 ;;
2519 blue) color=4 ;;
2520 magenta) color=5 ;;
2521 cyan) color=6 ;;
2522 white|*) color=7 ;; # white or invalid color
2523 esac
2524 fi
Bruno Martins5064db22021-06-21 14:47:40 +01002525 if [ -t 1 ] ; then tput setaf $color; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002526 printf '%s\n' "$*"
Bruno Martins5064db22021-06-21 14:47:40 +01002527 if [ -t 1 ] ; then tput sgr0; fi
Bruno Martins0f425f12021-04-10 14:57:32 +01002528}