forked from onekey-sec/ubi_reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubi_utils_info.py
executable file
·299 lines (233 loc) · 11.3 KB
/
ubi_utils_info.py
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
#!/usr/bin/env python
#############################################################
# ubi_reader
# (c) 2013 Jason Pruitt ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#############################################################
import os
import sys
import time
import argparse
import ConfigParser
from modules import settings
from modules.ubi import ubi
from modules.ubi.defines import UBI_EC_HDR_MAGIC, PRINT_VOL_TYPE_LIST, UBI_VTBL_AUTORESIZE_FLG
from modules.ubifs import ubifs
from modules.ubifs.defines import PRINT_UBIFS_KEY_HASH, PRINT_UBIFS_COMPR
from modules.ubi_io import ubi_file, leb_virtual_file
from modules.debug import error, log
from modules.utils import guess_filetype, guess_start_offset, guess_peb_size
def create_output_dir(outpath):
if os.path.exists(outpath):
if os.listdir(outpath):
error(create_output_dir, 'Fatal', 'Output directory is not empty. %s' % outpath)
else:
try:
os.makedirs(outpath)
log(create_output_dir, 'Created output path: %s' % outpath)
except Exception, e:
error(create_output_dir, 'Fatal', '%s' % e)
def get_ubi_params(ubi_obj):
"""Get ubi_obj utils params
Arguments:
Obj:ubi -- UBI object.
Returns:
Dict -- Dict keyed to volume with Dict of args and flags.
"""
ubi_flags = {'min_io_size':'-m',
'max_bud_bytes':'-j',
'leb_size':'-e',
'default_compr':'-x',
'sub_page_size':'-s',
'fanout':'-f',
'key_hash':'-k',
'orph_lebs':'-p',
'log_lebs':'-l',
'max_leb_cnt': '-c',
'peb_size':'-p',
'sub_page_size':'-s',
'vid_hdr_offset':'-O',
'version':'-x',
'image_seq':'-Q',
'alignment':'-a',
'vol_id':'-n',
'name':'-N'}
ubi_params = {}
ubi_args = {}
ini_params = {}
for image in ubi_obj.images:
img_seq = image.image_seq
ubi_params[img_seq] = {}
ubi_args[img_seq] = {}
ini_params[img_seq] = {}
for volume in image.volumes:
ubi_args[img_seq][volume] = {}
ini_params[img_seq][volume] = {}
# Get ubinize.ini settings
ini_params[img_seq][volume]['vol_type'] = PRINT_VOL_TYPE_LIST[image.volumes[volume].vol_rec.vol_type]
if image.volumes[volume].vol_rec.flags == UBI_VTBL_AUTORESIZE_FLG:
ini_params[img_seq][volume]['vol_flags'] = 'autoresize'
else:
ini_params[img_seq][volume]['vol_flags'] = image.volumes[volume].vol_rec.flags
ini_params[img_seq][volume]['vol_id'] = image.volumes[volume].vol_id
ini_params[img_seq][volume]['vol_name'] = image.volumes[volume].name.rstrip('\x00')
ini_params[img_seq][volume]['vol_alignment'] = image.volumes[volume].vol_rec.alignment
ini_params[img_seq][volume]['vol_size'] = image.volumes[volume].vol_rec.reserved_pebs * ubi_obj.leb_size
# Create file object backed by UBI blocks.
lebv_file = leb_virtual_file(ubi_obj, image.volumes[volume].get_blocks(ubi_obj.blocks))
# Create UBIFS object
ubifs_obj = ubifs(lebv_file)
for key, value in ubifs_obj.superblock_node:
if key == 'key_hash':
value = PRINT_UBIFS_KEY_HASH[value]
elif key == 'default_compr':
value = PRINT_UBIFS_COMPR[value]
if key in ubi_flags:
ubi_args[img_seq][volume][key] = value
for key, value in image.volumes[volume].vol_rec:
if key == 'name':
value = value.rstrip('\x00')
if key in ubi_flags:
ubi_args[img_seq][volume][key] = value
ubi_args[img_seq][volume]['version'] = image.version
ubi_args[img_seq][volume]['vid_hdr_offset'] = image.vid_hdr_offset
ubi_args[img_seq][volume]['sub_page_size'] = ubi_args[img_seq][volume]['vid_hdr_offset']
ubi_args[img_seq][volume]['sub_page_size'] = ubi_args[img_seq][volume]['vid_hdr_offset']
ubi_args[img_seq][volume]['image_seq'] = image.image_seq
ubi_args[img_seq][volume]['peb_size'] = ubi_obj.peb_size
ubi_args[img_seq][volume]['vol_id'] = image.volumes[volume].vol_id
ubi_params[img_seq][volume] = {'flags':ubi_flags, 'args':ubi_args[img_seq][volume], 'ini':ini_params[img_seq][volume]}
return ubi_params
def print_ubi_params(ubi_obj):
ubi_params = get_ubi_params(ubi_obj)
for img_params in ubi_params:
for volume in ubi_params[img_params]:
ubi_flags = ubi_params[img_params][volume]['flags']
ubi_args = ubi_params[img_params][volume]['args']
ini_params = ubi_params[img_params][volume]['ini']
sorted_keys = sorted(ubi_params[img_params][volume]['args'])
print '\nVolume %s' % volume
for key in sorted_keys:
if len(key)< 8:
name = '%s\t' % key
else:
name = key
print '\t%s\t%s %s' % (name, ubi_flags[key], ubi_args[key])
print '\n\t#ubinize.ini#'
print '\t[%s]' % ini_params['vol_name']
for key in ini_params:
if key != 'name':
print '\t%s=%s' % (key, ini_params[key])
def make_files(ubi, outpath):
ubi_params = get_ubi_params(ubi)
for img_params in ubi_params:
config = ConfigParser.ConfigParser()
img_outpath = os.path.join(outpath, 'img-%s' % img_params)
if not os.path.exists(img_outpath):
os.mkdir(img_outpath)
ini_path = os.path.join(img_outpath, 'img-%s.ini' % img_params)
ubi_file = os.path.join('img-%s.ubi' % img_params)
script_path = os.path.join(img_outpath, 'create_ubi_img-%s.sh' % img_params)
ubifs_files =[]
buf = '#!/bin/sh\n'
print 'Writing to: %s' % script_path
with open(script_path, 'w') as fscr:
with open(ini_path, 'w') as fini:
print 'Writing to: %s' % ini_path
vol_idx = 0
for volume in ubi_params[img_params]:
ubifs_files.append(os.path.join('img-%s_%s.ubifs' % (img_params, vol_idx)))
ini_params = ubi_params[img_params][volume]['ini']
ini_file = 'img-%s.ini' % img_params
config.add_section(volume)
config.set(volume, 'mode', 'ubi')
config.set(volume, 'image', ubifs_files[vol_idx])
for i in ini_params:
config.set(volume, i, ini_params[i])
ubi_flags = ubi_params[img_params][volume]['flags']
ubi_args = ubi_params[img_params][volume]['args']
leb = '%s %s' % (ubi_flags['leb_size'], ubi_args['leb_size'])
peb = '%s %s' % (ubi_flags['peb_size'], ubi_args['peb_size'])
min_io = '%s %s' % (ubi_flags['min_io_size'], ubi_args['min_io_size'])
leb_cnt = '%s %s' % (ubi_flags['max_leb_cnt'], ubi_args['max_leb_cnt'])
vid_hdr = '%s %s' % (ubi_flags['vid_hdr_offset'], ubi_args['vid_hdr_offset'])
sub_page = '%s %s' % (ubi_flags['sub_page_size'], ubi_args['sub_page_size'])
buf += '/usr/sbin/mkfs.ubifs %s %s %s -r $%s %s\n' % (min_io, leb, leb_cnt, (vol_idx+1), ubifs_files[vol_idx])
vol_idx += 1
config.write(fini)
buf += '/usr/sbin/ubinize %s %s %s %s -o %s %s\n' % (peb, min_io, sub_page, vid_hdr, ubi_file, ini_file)
fscr.write(buf)
os.chmod(script_path, 0755)
if __name__=='__main__':
start = time.time()
description = 'Determine settings for recreating UBI image.'
usage = 'ubi_utils_info.py [options] filepath'
parser = argparse.ArgumentParser(usage=usage, description=description)
parser.add_argument('-r', '--show-only', action='store_true', dest='show_only',
help='Print parameters to screen only. (default: false)')
parser.add_argument('-l', '--log', action='store_true', dest='log',
help='Print extraction information to screen.')
parser.add_argument('-v', '--verbose-log', action='store_true', dest='verbose',
help='Prints nearly everything about anything to screen.')
parser.add_argument('-p', '--peb-size', type=int, dest='block_size',
help='Specify PEB size.')
parser.add_argument('-s', '--start-offset', type=int, dest='start_offset',
help='Specify offset of UBI data in file. (default: 0)')
parser.add_argument('-n', '--end-offset', type=int, dest='end_offset',
help='Specify end offset of UBI data in file.')
parser.add_argument('-o', '--output-dir', dest='outpath',
help='Specify output directory path.')
parser.add_argument('filepath', help='File to extract contents of.')
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
if args.filepath:
path = args.filepath
if not os.path.exists(path):
parser.error("File path doesn't exist.")
if args.start_offset:
start_offset = args.start_offset
else:
start_offset = guess_start_offset(path)
if args.end_offset:
end_offset = args.end_offset
else:
end_offset = None
filetype = guess_filetype(path, start_offset)
if filetype != UBI_EC_HDR_MAGIC:
parser.error('File does not look like UBI data.')
img_name = os.path.basename(path)
if args.outpath:
outpath = os.path.abspath(os.path.join(args.outpath, img_name))
else:
outpath = os.path.join(settings.output_dir, img_name)
settings.logging_on = args.log
settings.logging_on_verbose = args.verbose
if args.block_size:
block_size = args.block_size
else:
block_size = guess_peb_size(path)
if not block_size:
parser.error('Block size could not be determined.')
# Create file object.
ufile_obj = ubi_file(path, block_size, start_offset, end_offset)
# Create UBI object
ubi_obj = ubi(ufile_obj)
# Print info.
print_ubi_params(ubi_obj)
if not args.show_only:
create_output_dir(outpath)
# Create build scripts.
make_files(ubi_obj, outpath)