-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathkdzmanager.sh
executable file
·225 lines (210 loc) · 8.73 KB
/
kdzmanager.sh
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
#!/bin/bash
##########################################################################################
#
# SALT - [S]teadfasterX [A]ll-in-one [L]G [T]ool
#
# Copyright (C): 2017-2022, steadfasterX <steadfastX|boun.cr>
#
# LG KDZ MANAGER
#
##########################################################################################
# the vars for the lgup-ng
VARS="${0%/*}/salt.vars"
source $VARS
[ $? -ne 0 ] && "ERROR: Missing requirement <$VARS>." && exit 3
# the functions for the lglaf GUI
FUNCS="${0%/*}/salt.func"
source $FUNCS
[ $? -ne 0 ] && "ERROR: Missing requirement <$FUNCS>." && exit 3
F_LOG "KDZMGR started.."
F_HELP(){
echo -e "\nCopyright (C) 2017-$(date +%Y): steadfasterX <steadfastX | boun.cr>"
echo -e "LICENSE: LGPLv2 (https://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt)\n"
echo -e "\nUsage:\n----------------------------\n"
echo -e "\tactions (one or both):"
echo -e "\t-x | --extract [KDZ FILENAME] extract a kdz file plus the resulting dz file"
echo -e "\t note: the current dir will be used as target dir by default"
echo -e "\t extractedkdz/ and extracteddz/ will be created here"
echo -e "\t-d | --extractdir <target dir> if you want to specify another target dir for the KDZ"
echo -e "\t-s | --slice '<part#> <part#>' extract only these slices (must be number(s) separated by space and quoted)"
echo
echo -e "\t--flash [PATH TO IMAGE FILES] will flash all image files of that directory (except userdata)"
echo -e "\t[--with-userdata --flash ...] will flash all image files including userdata (like a factory reset)"
echo
echo -e "\n\tgeneral:"
echo -e "\t-h | --help this output"
echo -e "\t-t | --test test mode: will not extract/flash but print what would be done"
echo -e "\t-b | --batch batch mode: no questions - DANGEROUS!"
echo -e "\t-D | --debug debug mode"
echo -e "\n"
echo -e "\n\tExamples:\n"
echo -e "\tkdzmanager.sh -x ~/Downloads/h815v20p.kdz (extract only)"
echo -e "\tkdzmanager.sh --with-userdata --flash extracted (will flash all files from dir extracted - with userdata!)"
echo -e "\tkdzmanager.sh --test --flash my/dir/ (will tell you what would be flashed from my/dir - without flashing!)"
echo -e "\tkdzmanager.sh -x ~/Downloads/h815v20p.kdz --flash ./extracteddz/ (will extract and flash the result in 1 run)"
echo
}
[ $# -eq 0 ] && F_HELP && exit
FLASHING=0
EXTRACT=0
TESTMODE=0
UDATA=0
BATCH=0
WCACHE=0
DEBUG=0
LISTMODE=0
# check the args!
while [ ! -z $1 ];do
case "$1" in
--with-userdata)
UDATA=1
shift
;;
--with-cache)
WCACHE=1
shift
;;
--flash)
IMGPATH="$2"
[ -z "$IMGPATH" ] && echo -e "\nextracting requires the full path to your extracted KDZ/DZ! e.g. $0 full/path/to/extracted/" && F_HELP && exit
[ ! -d "$IMGPATH" ] && echo -e "\nERROR!! $IMGPATH DOES NOT EXISTS!?" && F_HELP && exit
if [ ! -d "$LAFPATH" ];then
echo -e "\nERROR: Expected LG LAF NG here: $LAFPATH"
[ "$BATCH" -eq 0 ] && read -p "Should I download it for you? (y/N) " DLLAF
if [ "$DLLAF" == "y" ]||[ "$BATCH" -eq 1 ];then
git clone $LAFGIT $LAFPATH
else
exit
fi
fi
FLASHING=1
shift 2
;;
-x|--extract)
FULLKDZ="$2"
[ -z "$FULLKDZ" ] && echo -e "\nextracting requires the full path to a KDZ! e.g. $0 full/path/to/kdz/kdzfilename.kdz" && F_HELP && exit
[ ! -f "$FULLKDZ" ] && echo -e "\nERROR!! $FULLKDZ DOES NOT EXISTS!?" && F_HELP && exit
if [ ! -d "$KDZTOOLS" ];then
echo -e "\nERROR: Expected kdztools here: $KDZTOOLS"
[ "$BATCH" -eq 0 ] && read -p "Should I download it for you? (y/N) " DLKDZ
if [ "$DLKDZ" == "y" ]||[ "$BATCH" -eq 1 ];then
git clone $KDZGIT $KDZTOOLS
else
exit
fi
fi
shift 2
EXTRACT=1
;;
-d|--extractdir)
KDZDIR="$2"
shift 2
;;
-t|--test)
TESTMODE=1
shift
;;
-b|--batch) BATCH=1 ; shift ;;
-D|--debug) DEBUG=1 ; shift ;;
-s|--slice) SELPARTS="$2"; shift 2;;
-l|--list) LISTMODE=1; shift;;
*)
F_HELP
exit
;;
esac
done
# list partitions of a DZ file
FK_LISTPARTS(){
DZFILE=$(find "${KDZDIR}/extractedkdz/" -name '*\.dz')
echo "DZFILE is $DZFILE" >> $LOG
if [ "$BATCH" -eq 1 ];then
$PYTHONBIN ${KDZTOOLS}/undz -b -l -f "${DZFILE}" 2>>$LOG
else
$PYTHONBIN ${KDZTOOLS}/undz -l -f "${DZFILE}"
fi
}
# extract a KDZ file
FK_EXTRACTKDZ(){
if [ "$BATCH" -eq 1 ];then
$PYTHONBIN ${KDZTOOLS}/unkdz -f "$FULLKDZ" -x -d "${KDZDIR}/extractedkdz" 2>>$LOG
else
$PYTHONBIN ${KDZTOOLS}/unkdz -f "$FULLKDZ" -x -d "${KDZDIR}/extractedkdz"
fi
}
# extract a DZ
FK_EXTRACTPARTS(){
DZFILE=$(find "${KDZDIR}/extractedkdz/" -name '*\.dz')
echo "DZFILE is $DZFILE" >> $LOG
if [ "$BATCH" -eq 1 ];then
$PYTHONBIN ${KDZTOOLS}/undz -b -s $SELPARTS -f "${DZFILE}" -d "${KDZDIR}/extracteddz"
else
$PYTHONBIN ${KDZTOOLS}/undz -s $SELPARTS -f "${DZFILE}" -d "${KDZDIR}/extracteddz"
fi
# delete unneeded parse files
find "${KDZDIR}/extracteddz/" -name "*.params" -delete
# rename GPT files to ensure they will not flashed by accident
for gpt in $(find "${KDZDIR}/extracteddz/" -type f |grep -i GPT);do mv -v "$gpt" "${gpt/\.image/.gpt}";done
}
if [ $LISTMODE -eq 1 ];then
BATCH=1
FK_EXTRACTKDZ 2>&1 >>$LOG
FK_LISTPARTS 2>>$LOG | sort -u -t : -k 2 | sort -t : -k 3| $EGREPBIN -v "(unallocated)"
else
# extract KDZ and DZ
if [ $EXTRACT -eq 1 ];then
[ -z "$KDZDIR" ] && KDZDIR="$(echo ~/Downloads)"
echo -e "\nWill extract all files to: $KDZDIR"
echo -e "\n\n***********\nWARNING:\n***********\nKDZ files contain the userdata image which can be very big (e.g. 23 GB on a LG G4)\nEnsure you have enough free disk space before continuing!\nYou can continue even when you have not enough free space but the result will be incomplete (still enough maybe)\n"
[ "$BATCH" -eq 0 ] && read -p "I understood and want to continue (press ENTER)" DUMMY
if [ $TESTMODE -eq 0 ];then
DZFILE=$(find "${KDZDIR}/extractedkdz/" -name '*\.dz' 2>/dev/null)
echo "DZFILE is $DZFILE" >> $LOG
[ ! -f "$DZFILE" ] && FK_EXTRACTKDZ
FK_EXTRACTPARTS
# delete userdata partition when not needed
[ "$UDATA" -eq 0 ] && echo "You have selected to delete userdata partition" && rm -rfv "${KDZDIR}/extracteddz/userdata*"
# delete cache partition when not needed
[ "$WCACHE" -eq 0 ] && echo "You have selected to delete cache partition" && rm -rfv "${KDZDIR}/extracteddz/cache*"
# clean DZ file
[ $DEBUG -eq 0 ] && rm -fv "${KDZDIR}/extractedkdz/*.dz"
else
echo "TESTMODE only:"
echo "CMD: $PYTHONBIN ${KDZTOOLS}/unkdz -f $FULLKDZ -x -d ${KDZDIR}/extractedkdz"
echo "CMD: $PYTHONBIN ${KDZTOOLS}/undz -s -f ${KDZDIR}/extractedkdz/*.dz -d ${KDZDIR}/extracteddz"
fi
echo -e "\n\nALL FINISHED! Your extracted files are in: ${KDZDIR}/extracteddz\n"
fi
# flash partitions
if [ $FLASHING -eq 1 ];then
# authenticate once
if [ $TESTMODE -eq 0 ];then
sudo $PYTHONBIN ${LAFPATH}/lglaf.py --skip-hello --showproto
else
echo "TESTMODE only:"
echo "CMD: sudo $PYTHONBIN ${LAFPATH}/lglaf.py --skip-hello --showproto"
fi
# check if we want to leave out userdata (default)
# gpt will be handled in a different manner (not ready yet)
if [ $UDATA -eq 0 ];then
GREPOUT="userdata|gpt"
else
GREPOUT="gpt"
fi
# flash
for part in $(find "$IMGPATH" -maxdepth f -type 0 -name *.image | $EGREPBIN -vi "($GREPOUT)");do
RMPATH="${part##*/}"
REMPART="${RMPATH/\.image/}"
echo -e "... flashing: $part to ${REMPART}"
# redirecting the misleading error output (sorry dirty workaround atm..)
if [ $TESTMODE -eq 0 ];then
sudo $PYTHONBIN ${LAFPATH}/partitions.py --restore "$part" $REMPART 2>/dev/null
else
echo "TESTMODE only:"
echo "CMD: sudo $PYTHONBIN ${LAFPATH}/partitions.py --restore $part $REMPART"
fi
done
fi
fi
[ "$BATCH" -eq 0 ] && echo -e "\n\nAll done.\n\n"
F_LOG "KDZMGR ended.."