forked from AdrianDC/advanced_development_shell_tools
/
android_tools.rc
executable file
·359 lines (328 loc) · 10.3 KB
/
android_tools.rc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
# ======================================
# Developed by Adrian DC - 2015-2016
# ======================================
# === Binary Editor ===
function binaryeditor()
{
if [ -z "$2" ]; then
echo " Usage : binaryeditor binariespath patternsearch [replacement]";
return;
fi;
local FILES="$1";
local PATTERN="$2";
local REPLACEMENT="$3";
local REPLACE="";
local STRINGS="";
PATTERN=${PATTERN//\*/.\*};
PATTERN=${PATTERN//./.};
echo "";
if [ ! -z "${REPLACEMENT}" ]; then
echo -e " \e[1;31m=== Replacing '${PATTERN}' with '${REPLACEMENT}' in ${FILES} ===\e[0m";
else
echo -e " \e[1;31m=== Searching '${PATTERN}' in ${FILES} ===\e[0m";
fi;
echo "";
for FILE in $(find $FILES -not -path "*.git/*" -not -path "*.repo/*" | sort);
do
FILE=$(readlink -f "$FILE");
if [ -d $FILE ]; then continue; fi;
if [ ! -z "${REPLACEMENT}" ]; then
STRINGS=$(strings ${FILE} | grep "${PATTERN}" | sort -u -r);
if [ ! -z "${STRINGS}" ]; then
printf " \e[1;37m${FILE} :\e[0m ${STRINGS} ";
for OLD_STRING in ${STRINGS};
do
NEW_STRING=${OLD_STRING//${PATTERN}/${REPLACEMENT}};
OLD_STRING_HEX="$(echo -n ${OLD_STRING} | xxd -g 0 -u -ps -c 256)00";
NEW_STRING_HEX="$(echo -n ${NEW_STRING} | xxd -g 0 -u -ps -c 256)00";
if [ ${#NEW_STRING_HEX} -le ${#OLD_STRING_HEX} ]; then
while [ ${#NEW_STRING_HEX} -lt ${#OLD_STRING_HEX} ];
do
NEW_STRING_HEX="${NEW_STRING_HEX}00";
done;
printf "\e[1;36m[y/N] \e[0m";
read -s -d'' -s -n1 REPLACE;
if [[ "${REPLACE}" == "y" || "${REPLACE}" == "Y" ]]; then
hexdump -ve '1/1 "%.2X"' ${FILE} | \
sed "s/${OLD_STRING_HEX}/${NEW_STRING_HEX}/g" | \
xxd -r -p > ${FILE}.tmp;
chmod --reference ${FILE} ${FILE}.tmp;
mv ${FILE}.tmp ${FILE};
echo -e "\e[1;32mDone!\e[0m";
else
echo -e "\e[1;33mIgnored.\e[0m";
fi;
else
echo -e "\e[1;36m[N] \e[1;33mString too long...\e[0m";
fi;
done;
else
STRINGS=$(strings ${FILE} | grep ${REPLACEMENT} | sort -u -r);
if [ ! -z "${STRINGS}" ]; then
echo -e " \e[1;37m${FILE}:\e[0m ${REPLACEMENT} \e[1;33mFound.\e[0m";
fi;
fi;
else
STRINGS=$(strings ${FILE} | grep "${PATTERN}" | sort -u -r | tr '\n' ',');
if [ ! -z "${STRINGS}" ]; then
echo -e " \e[1;37m${FILE}:\e[1;33m ${STRINGS}\e[0m";
fi;
fi;
done;
echo "";
}
# === Libraries Hunter ===
function librarieshunter()
{
if [ -z "$1" ]; then
echo " Usage : librarieshunter binariespath";
return;
fi;
local FILES="$1";
local FILE_NAME='';
local FILE_REFERENCED='';
local LIB_NAME='';
# Scan the libraries
echo "";
echo -e " \e[1;31m=== Searching missing libraries in ${FILES} ===\e[0m";
echo "";
for FILE in $(find ${FILES} -not -path "*.git/*" -not -path "*.repo/*" | sort);
do
FILE=$(readlink -f "${FILE}");
if [ -d ${FILE} ]; then continue; fi;
# Internal references
STRINGS=$(strings ${FILE} | grep "\\.so" | sort -u -r | tr '\n' ',');
if [ ! -z "${STRINGS}" ]; then
STRINGS=${STRINGS//,/ };
echo -e " \e[1;37m${FILE}:\e[1;33m ${STRINGS}\e[0m";
# Search missing references
for LIB in ${STRINGS};
do
if [[ ! "${LIB}" =~ '.so' ]]; then continue; fi;
LIB_NAME=$(basename ${LIB});
if [ -z "$(find $(dirname ${FILES}) -name ${LIB_NAME})" ]; then
echo -e " \e[1;37m- Missing reference: \e[1;31m${LIB_NAME}\e[0m";
fi;
done;
# Search unused library
FILE_NAME=$(basename "${FILE}");
FILE_REFERENCED='';
for FILE_SUB in $(find ${FILES} -not -path "*.git/*" -not -path "*.repo/*" | sort);
do
FILE_SUB=$(readlink -f "${FILE_SUB}");
if [ -d "${FILE_SUB}" ] || [ "${FILE}" = "${FILE_SUB}" ]; then continue; fi;
STRINGS_SUB=$(strings ${FILE_SUB} | grep "${FILE_NAME}" | sort -u -r | tr '\n' ',');
if [ ! -z "${STRINGS_SUB}" ]; then
FILE_REFERENCED='true';
break;
fi;
done;
if [ -z "${FILE_REFERENCED}" ]; then
echo -e " \e[1;31m- Unused library: ${FILE_NAME}\e[0m";
fi;
fi;
done;
echo "";
}
# === Audio LegacyToHAL helper ===
function audioltoh()
{
if [ -z "$2" ]; then
echo " Usage : audioltoh legacy_config hal_mixer [debug]";
return;
fi;
Legacy="$1";
HAL="$2";
Debug="$3";
TempFile=$(mktemp);
echo "";
echo -e " \e[1;37m === Checking Legacy rules to HAL presence ===\e[0m";
echo "";
grep -i -e "'.*'" "$Legacy" | uniq \
| sed "s/^[ \t]*'\(.*\)'\:\(.*\)\:\(.*\)/\1/" \
| sort | uniq >$TempFile;
kate $TempFile;
while read line; do
if ! grep -q "$line" $HAL; then
echo -e " \e[1;33mLegacy: \e[1;34m$line - \e[1;31mMissing\e[0m";
elif [ ! -z "$Debug" ]; then
echo -e " \e[1;33mLegacy: \e[1;34m$line - \e[1;34mFound\e[0m";
fi;
done < "$TempFile";
echo "";
echo -e " \e[1;34m > Done checking Legacy rules to HAL\e[0m";
echo "";
echo "";
echo -e " \e[1;37m === Checking HAL rules to Legacy presence ===\e[0m";
echo "";
return;
grep -i -e "<ctl name=\"".*"\" value=\".*\" \/>" "$HAL" \
| sort | uniq -u \
| sed "s/.*name=\"\(.*\)\" .*value=\"\(.*\)\" \/>/\1 | \2/" >$TempFile;
while read line;
do
if [ -z $(grep -q "$Line" "$HAL") ]; then
echo -e " \e[1;33mHAL: \e[1;34m$line - \e[1;31mMissing\e[0m";
elif [ ! -z "$Debug" ]; then
echo -e " \e[1;33mHAL: \e[1;34m$line - \e[1;34mFound\e[0m";
fi;
done <$TempFile;
echo "";
echo -e " \e[1;34m > Done checking HAL rules to Legacy\e[0m";
echo "";
rm -f $TempFile;
}
# === Vendor Checker ===
function vendorcheck()
{
if [ -z "$2" ]; then
echo " Usage: vendorcheck deviceproprietarytx vendordevicedir";
return;
fi;
local currentdir=${PWD};
local repodir="$currentdir";
while [[ "${repodir}" != '/' && ! -d "${repodir}/.repo" ]]; do
repodir=$(readlink -f "${repodir}/..");
done;
if [ -d "${repodir}/.repo" ]; then
cd ${repodir};
fi;
VendorDir="$2";
VendorPhone=$(echo "$2" | sed 's/.*\/\([^\/]*\)\/proprietary.*/\1/');
VendorMk="../$VendorPhone-vendor-blobs.mk";
VendorError=0;
echo ""; i=0;
while read FilePath; do
i=$((i+1));
if [[ "$FilePath" =~ '#' ]] || [[ "$FilePath" == '' ]]; then
continue;
fi;
if [[ "$FilePath" == -* ]]; then
FilePath=$(echo "$FilePath" | cut -c2-);
fi;
if [[ "$FilePath" =~ ':' ]]; then
FilePath=$(echo "$FilePath" | sed 's/\(.*\):\(.*\)/\2/');
fi;
if [ -f "$VendorDir/$FilePath" ]; then
echo -e " \e[1;37mLine $i: $FilePath - File OK\e[0m";
else
echo -e " \e[1;31mLine $i: $FilePath - File Missing\e[0m";
VendorError=$(($VendorError+1));
sleep 0.1;
fi;
done < "$1";
echo "";
if [ $VendorError -gt 0 ]; then
echo -e " \e[1;37m> Press Enter to continue...\e[0m";
read key;
fi;
echo "";
VendorError=0;
i=0;
for FilePath in $(find $VendorDir -type f | sort); do
i=$((i+1));
FilePath=$(echo "$FilePath" | sed 's/.*proprietary\/\(.*\)/\1/g');
if [ ! -z $(grep -a "$FilePath" "$1" | head -1) ]; then
echo -e " \e[1;37mLine $i: $FilePath - Reference OK\e[0m";
elif [[ "$FilePath" =~ "RPM.bin" ]]; then
echo -e " \e[1;33mLine $i: $FilePath - Reference Ignored\e[0m";
else
echo -e " \e[1;31mLine $i: $FilePath - Reference Missing\e[0m";
VendorError=$(($VendorError+1));
sleep 0.1;
fi;
done;
echo "";
if [ $VendorError -gt 0 ]; then
echo -e " \e[1;37m> Press Enter to continue...\e[0m";
read key;
fi;
i=0;
TempFile=$(mktemp);
TempFile2=$(mktemp);
cat "$VendorDir/$VendorMk" \
| sed 's/.*:system\/\([^\|]*\)\( \\\|\)/\1/g' \
| sed '/^$/d' \
| sed '/^#.*$/d' \
| sed 's/^-//g' \
| sed '/^PRODUCT_COPY_FILES.*$/d' \
| sort > "$TempFile";
cat "$1" \
| sed '/^$/d' \
| sed '/^#.*$/d' \
| sed 's/^.*:\(.*\)$/\1/g' \
| sed 's/^-//g' \
| sort > "$TempFile2";
echo "";
echo " Cleaning the vendor blobs list...";
FilesToAdd="";
for FileLine in $(diff --old-line-format="-%L" \
--unchanged-line-format="#%L" \
--new-line-format="+%L" \
"$TempFile" "$TempFile2"); do
FileStatus="${FileLine:0:1}";
FilePath=$(echo "${FileLine:1}" | sed 's/\//\\\//g' | sed 's/\./\\./g');
case "$FileStatus" in
"+") FilesToAdd="$FilesToAdd ${FilePath//\\/}";;
"-") sed -i "/$FilePath/d" "$VendorDir/$VendorMk";;
*);;
esac;
done;
echo "";
echo " Populating the vendor blobs list...";
i=$(awk '/PRODUCT_COPY_FILES/{print NR + 1}' "$VendorDir/$VendorMk");
for FilePath in $FilesToAdd; do
FileLine="vendor/sony/$VendorPhone/proprietary/$FilePath:system/$FilePath \\";
FileCurrent=$(sed "$i!d" "$VendorDir/$VendorMk");
while [ " $FileLine" \> "$FileCurrent" ] && [ ! -z "$FileCurrent" ]; do
i=$((i+1));
FileCurrent=$(sed "$i!d" "$VendorDir/$VendorMk");
done;
if [ -z "$FileCurrent" ]; then i=$((i-1)); fi;
sed -i "${i}i\ \ \ \ $FileLine\\" "$VendorDir/$VendorMk";
done;
cat "$VendorDir/$VendorMk" \
| sed 's/.*:system\/\([^\|]*\)\( \\\|\)/\1/g' \
| sed '/^$/d' \
| sed '/^#.*$/d' \
| sed 's/^-//g' \
| sed '/^PRODUCT_COPY_FILES.*$/d' \
| sort > "$TempFile";
echo "";
meld "$TempFile" "$TempFile2";
rm -f $TempFile $TempFile2;
cd "$currentdir";
}
# === Vendor Checker Helper ===
function vendorch()
{
if [ -z "$1" ]; then
echo " Usage: vendorch devicename [vendor]";
return;
fi;
local vendor="$2";
if [ -z "$vendor" ]; then
vendor="sony";
fi;
vendorcheck device/$vendor/$1/proprietary-files.txt vendor/$vendor/$1/proprietary;
}
# === Hastebin ===
haste()
{
# Based upon https://github.com/seejohnrun/haste-client
# Usage : command | haste or haste file
tmp=$(mktemp);
if [ ! -z "$1" ] && [ -f "$1" ]; then
cat "$1" | tee $tmp;
else
cat | tee $tmp;
fi;
echo "";
url=$(timeout 10 curl -X POST -s --data-binary @"${tmp}" --connect-timeout 5 http://hastebin.com/documents \
| awk -F '"' '{ print "http://hastebin.com/"$4; }');
echo "$url";
xdg-open "$url" &
echo "";
rm $tmp;
}