forked from wendal/teclast_tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rk29xx_rom_tools.lua
191 lines (167 loc) · 5.49 KB
/
rk29xx_rom_tools.lua
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
print('RK29xx Image Pack/Unpack Tools')
print('Blog http://wendal.net')
print('Project: https://github.com/wendal/teclast_tools')
print('feedback: [email protected]')
function readHex(f)
local D1 = string.byte(f:read(1))
local D2 = string.byte(f:read(1))
local D3 = string.byte(f:read(1))
local D4 = string.byte(f:read(1))
--D = D1 + D2 *256 8 + D3 * 256 * 256 + D4 *256*256*256
return D1 + D2 *256 + D3 * 256 * 256 + D4 *256*256*256
end
dofile('BYTE.lua')
function writeHex(f,num)
local str = string.format('%08X',num)
local D1 = tonumber(string.sub(str,7),16)
local D2 = tonumber(string.sub(str,5,6),16)
local D3 = tonumber(string.sub(str,3,4),16)
local D4 = tonumber(string.sub(str,1,2),16)
--print(str)
--print(D1,D2,D3,D4)
f:write(B[D1 + 1])
f:write(B[D2 + 1])
f:write(B[D3 + 1])
f:write(B[D4 + 1])
end
BUFF=8192
function readDate(f,pos,len,dest)
print(string.format('offset(0x%X) len(0x%X)',pos,len))
f:seek('set',pos)
local destF = io.open(dest, 'wb+')
while len > 0 do
local data = nil
if len > BUFF then
data = f:read(BUFF)
len = len - BUFF
else
data = f:read(len)
len = 0
end
if data then
destF:write(data)
end
--print('len=',len, data)
end
destF:flush()
destF:close()
end
--unpack
function unpackROM()
print("Pls copy Image file into this folder,and rename to wendal.img \nNotify any errors!")
os.execute('pause')
local rom_file=io.open('wendal.img','rb')
if not rom_file then
print("wendal.img not found!!")
return
end
print("Reading file header...")
print("File header: "..rom_file:read(5))
rom_file:seek('set',25)
print("Read loader's offset")
local L_P = readHex(rom_file)
print("Read loader's len")
local L_L = readHex(rom_file)
print("Read update.img's offset")
local U_P = readHex(rom_file)
print("Read update.img's len")
local U_L = readHex(rom_file)
print("Output Loader")
readDate(rom_file,L_P,L_L,'RK29xxLoader(L).bin')
print("Output updata.img")
readDate(rom_file,U_P,U_L,'update.img')
print("Unpack updata.img to Temp folder")
os.execute('AFPTool.exe -unpack update.img Temp\\')
print("Unpack system.img to system folder")
os.execute('cramfsck -x system Temp/Image/system.img')
print("Enable root permission")
os.execute('copy /B su system\\bin\\ >nul')
os.execute('copy /B Superuser.apk system\\app\\ >nul')
os.execute('chmod -R 0777 system/*')
os.execute('chmod 6755 system/bin/su')
os.execute('chmod 6755 system/app/Superuser.apk')
print('Unpack -- All Done')
rom_file:close()
end
--´ò°ü
function packROM()
local SYSTEM_DIR = io.open('system/build.prop','r')
if SYSTEM_DIR then
SYSTEM_DIR:close()
--os.execute('chmod -R 777 system/*')
print('Packing system folder to system.img,overwrite to Temp\\Image\\system.img')
os.execute('mkcramfs -q system Temp/Image/system.img')
end
print('Packing files in Temp folder to update_new.img')
os.execute('Afptool -pack ./Temp update_new.img')
print('Get loader\'s and update_new.img\'s file len')
local L_P = 0x66 -- Loader's offset
local loader_file = io.open('RK29xxLoader(L).bin','rb+')
local L_L = loader_file:seek('end')
loader_file:seek('set',0) -- back to start of file
U_P = L_P + L_L -- update.img's offset, following loader
local update_file = io.open('update_new.img','rb+')
local U_L = update_file:seek('end')
update_file:seek('set',0) -- back to start of file
local T_File = io.open('wendal.img', 'rb+') -- open pre-packed image
local DestF = io.open('wendal_new.img', 'wb+') --open target file
local data = T_File:read(25)
DestF:write(data)
writeHex(DestF,L_P)
writeHex(DestF,L_L)
writeHex(DestF,U_P)
writeHex(DestF,U_L)
T_File:read(16) -- sjip 16 bytes , data is Unkown!!
data = T_File:read(102 - 25 - 16)
DestF:write(data)
DestF:flush()
T_File:close()
print('Start writing loader')
while L_L > 0 do
if L_L > BUFF then
data = loader_file:read(BUFF)
L_L = L_L - BUFF
else
data = loader_file:read(L_L)
L_L = 0
end
DestF:write(data)
end
print('Start writing update.img')
while U_L > 0 do
if U_L > BUFF then
data = update_file:read(BUFF)
U_L = U_L - BUFF
else
data = update_file:read(U_L)
U_L = 0
end
DestF:write(data)
end
DestF:flush()
DestF:close()
print('calculate MD5')
os.execute('md5sums.exe -u wendal_new.img > md5.txt')
local M_File = io.open('md5.txt','r')
local md5 = M_File:read(32)
M_File:close()
print('MD5='..md5)
print('Write MD5 into wendal_new.img')
DestF = io.open('wendal_new.img', 'ab+') --open target file
DestF:seek('end')
DestF:write(md5)
DestF:flush()
DestF:close()
print('Pack completa!! Target file --> wendal_new.img')
end
while true do
print('Pls input: 1-Unpack 2-Pack 3-Exit')
local m = io.read('*n')
if m == 1 then
unpackROM()
elseif m == 2 then
packROM()
elseif m == 3 then
os.exit(1)
end
end