forked from AdrianDC/advanced_development_shell_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroid_kernel_editors.rc
228 lines (199 loc) · 5.53 KB
/
android_kernel_editors.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
#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# === Build paths ===
export ANDROID_DEVELOPMENT_SHELL_TOOLS_MKBOOT=${ANDROID_DEVELOPMENT_SHELL_TOOLS_DIR}/android_mkboot;
export android_development_shell_tools_mkelf=${ANDROID_DEVELOPMENT_SHELL_TOOLS_DIR}/android_files/mkelf.py;
# === Boot Tools ===
function boottools()
{
# Usage
if [ -z "${1}" ] || [ ! -f "${1}" ]; then
echo '';
echo ' Usage: boottools <boot.img> (Android bootimage editor)';
echo '';
return;
fi;
# Variables
local boot_file=${1};
local out_folder=${2};
# Extraction
if [ -z "${out_folder}" ]; then
out_folder=$(mktemp -d);
fi;
# Unpack
rm -rf "${out_folder}";
"${ANDROID_DEVELOPMENT_SHELL_TOOLS_MKBOOT}/mkboot" "${boot_file}" "${out_folder}";
# Edition mode
if [ -z "${2}" ]; then
# Edit
diropen "${out_folder}";
echo '';
echo -n ' Press Enter to continue [ENTER] ';
read -r;
echo '';
# Repack
if [ ! -f "${boot_file}.old" ]; then
cp "${boot_file}" "${boot_file}.old";
fi;
rm -f "${boot_file}.tmp";
mv "${boot_file}" "${boot_file}.tmp";
"${ANDROID_DEVELOPMENT_SHELL_TOOLS_MKBOOT}/mkboot" "${out_folder}" "${boot_file}";
if [ -f "${boot_file}.tmp" ]; then
rm -f "${boot_file}.tmp";
fi;
rm -rf "${out_folder}";
fi;
# End of process
echo '';
}
# === Boot ELF ===
function bootelf()
{
# Usage
if [ -z "${1}" ] || [ ! -f "${1}" ]; then
echo '';
echo ' Usage: bootelf <boot.img> (Sony ELF 8960 bootimage editor)';
echo '';
return;
fi;
# Variables
local boot_file=${1};
local out_folder=${2};
local pwd_folder;
# Path generations
boot_file=$(readlink -f "${boot_file}");
if [ -z "${out_folder}" ]; then
out_folder=$(mktemp -d);
else
rm -rf "${out_folder}";
mkdir -p "${out_folder}";
out_folder=$(readlink -f "${out_folder}");
fi;
# Unpack
pwd_folder=$(pwd);
cp "${boot_file}" "${out_folder}/boot.elf";
cd "${out_folder}/";
7z e ./boot.elf;
rm ./boot.elf;
# If the boot.img hasn't been extracted correctly
if [ ! -f ./0 ] || [ ! -f ./1 ] || [ ! -f ./2 ] || [ ! -f ./3 ]; then
echo '';
echo ' [ The boot.img is corrupted, extraction failed ]';
echo '';
read -r;
exit;
fi;
# Reorganize and rename the extracted files
mv -f ./0 ./kernel;
mv -f ./1 ./ramdisk.img;
mv -f ./2 ./RPM.bin;
mv -f ./3 ./cmdline.txt;
# Extract the ramdisk image
echo '';
echo ' [ Extracting ramdisk image ]';
echo '';
cp ./ramdisk.img ./ramdisk.cpio.gz;
mkdir ./ramdisk;
cd ./ramdisk/;
gzip -d ../ramdisk.cpio.gz;
cpio -i -F ../ramdisk.cpio;
# Extract the cpio archives
echo '';
echo ' [ Extracting ramdisk cpios ]';
echo '';
cd ./sbin/;
if [ -f ./ramdisk.cpio ]; then
mkdir ./ramdiskcpio;
cd ./ramdiskcpio/;
cpio -i -F ../ramdisk.cpio;
cd ../;
fi;
if [ -f ./ramdisk-recovery.cpio ]; then
mkdir ./ramdiskrecoverycpio;
cd ./ramdiskrecoverycpio/;
cpio -i -F ../ramdisk-recovery.cpio;
cd ../;
fi;
# Edition mode
if [ -z "${2}" ]; then
# Edit
diropen "${out_folder}";
echo '';
echo -n ' Press Enter to continue [ENTER] ';
read -r;
echo '';
# Backup original file
if [ ! -f "${boot_file}.old" ]; then
cp "${boot_file}" "${boot_file}.old";
fi;
# Compile the ramdisk.cpio and ramdisk.recovery.cpio if needed
echo '';
echo ' [ Compiling the ramdisk cpios ]';
echo '';
cd "${out_folder}/ramdisk/sbin/";
if [ -d ./ramdiskcpio ]; then
cd ./ramdiskcpio/;
rm -f ../ramdisk.cpio;
find . | cpio -o -R 0:0 -H newc -O ../ramdisk.cpio;
cd ../;
rm -rf ./ramdiskcpio;
fi;
if [ -d ./ramdiskrecoverycpio ]; then
cd ./ramdiskrecoverycpio/;
rm -f ../ramdisk-recovery.cpio;
find . | cpio -o -R 0:0 -H newc -O ../ramdisk-recovery.cpio;
cd ../;
rm -rf ./ramdiskrecoverycpio;
fi;
# Compile ramdisk image
echo '';
echo ' [ Compiling the ramdisk image ]';
echo '';
cd "${out_folder}/ramdisk/";
rm -f ../ramdisk.cpio;
find . | cpio -o -R 0:0 -H newc -O ../ramdisk.cpio;
rm -f ../ramdisk.cpio.gz;
gzip -9 -n ../ramdisk.cpio;
rm -f ../ramdisk.img;
mv ../ramdisk.cpio.gz ../ramdisk.img;
# boot.img contents missing
cd "${out_folder}/";
if [ ! -f ./kernel ] || [ ! -f ./ramdisk.img ] || [ ! -f ./RPM.bin ] || [ ! -f ./cmdline.txt ]; then
echo '';
echo ' [ A file is missing for the boot.img compilation ]';
echo '';
read -r;
exit;
fi;
# Compile elf image
echo '';
echo ' [ Compiling the boot.img ]';
echo '';
cp "${android_development_shell_tools_mkelf}" ./mkelf.py;
python ./mkelf.py -o boot-new.elf kernel@0x80208000 ramdisk.img@0x81900000,ramdisk RPM.bin@0x00020000,rpm cmdline.txt@0x00000000,cmdline;
# Replace input bootimage
mv ./boot-new.elf "${boot_file}";
echo '';
echo ' [ Done ]';
echo '';
# Delete temp folder
rm -rf "${out_folder}";
fi;
# End of process
cd "${pwd_folder}";
echo '';
}