forked from onekey-sec/ubi_reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
executable file
·212 lines (157 loc) · 6.21 KB
/
__init__.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
#!/usr/bin/env python
#############################################################
# ubi_reader/ubi
# (c) 2013 Jason Pruitt (jrspruitt@gmail.com)
#
# 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 re
from ubireader.debug import error, log
from ubireader.ubi.block import sort, extract_blocks
from ubireader.ubi.defines import UBI_EC_HDR_MAGIC, FILE_CHUNK_SZ
from ubireader.ubi import display
from ubireader.ubi.image import description as image
from ubireader.ubi.block import layout
class ubi():
"""UBI object
Arguments:
Obj:image -- UBI image object
Attributes:
Int:leb_size -- Size of Logical Erase Blocks.
Int:peb_size -- Size of Physical Erase Blocks.
Int:first_peb_num -- Number of the first UBI PEB in file.
Int:min_io -- Size of min I/O from vid_hdr_offset.
List:images -- List of UBI image objects.
List:data_blocks_list -- List of all data blocks in file.
List:layout_blocks_list -- List of all layout blocks in file.
List:int_vol_blocks_list -- List of internal volumes minus layout.
List:unknown_blocks_list -- List of blocks with unknown types. *
Dict:blocks -- Dict keyed by PEB number of all blocks.
"""
def __init__(self, ubi_file):
self.__name__ = 'UBI'
self._file = ubi_file
self._first_peb_num = 0
self._blocks = extract_blocks(self)
self._block_count = len(self.blocks)
if self._block_count <= 0:
error(self, 'Fatal', 'No blocks found.')
layout_list, data_list, int_vol_list, unknown_list = sort.by_type(self.blocks)
if len(layout_list) < 2:
error(self, 'Fatal', 'Less than 2 layout blocks found.')
self._layout_blocks_list = layout_list
self._data_blocks_list = data_list
self._int_vol_blocks_list = int_vol_list
self._unknown_blocks_list = unknown_list
arbitrary_block = self.blocks.itervalues().next()
self._min_io_size = arbitrary_block.ec_hdr.vid_hdr_offset
self._leb_size = self.file.block_size - arbitrary_block.ec_hdr.data_offset
layout_pairs = layout.group_pairs(self.blocks, self.layout_blocks_list)
layout_infos = layout.associate_blocks(self.blocks, layout_pairs, self.first_peb_num)
self._images = []
for i in range(0, len(layout_infos)):
self._images.append(image(self.blocks, layout_infos[i]))
def _get_file(self):
"""UBI File object
Returns:
Obj -- UBI File object.
"""
return self._file
file = property(_get_file)
def _get_images(self):
"""Get UBI images.
Returns:
List -- Of volume objects groupled by image.
"""
return self._images
images = property(_get_images)
def _get_data_blocks_list(self):
"""Get all UBI blocks found in file that are data blocks.
Returns:
List -- List of block objects.
"""
return self._data_blocks_list
data_blocks_list = property(_get_data_blocks_list)
def _get_layout_blocks_list(self):
"""Get all UBI blocks found in file that are layout volume blocks.
Returns:
List -- List of block objects.
"""
return self._layout_blocks_list
layout_blocks_list = property(_get_layout_blocks_list)
def _get_int_vol_blocks_list(self):
"""Get all UBI blocks found in file that are internal volume blocks.
Returns:
List -- List of block objects.
This does not include layout blocks.
"""
return self._int_vol_blocks_list
int_vol_blocks_list = property(_get_int_vol_blocks_list)
def _get_unknown_blocks_list(self):
"""Get all UBI blocks found in file of unknown type..
Returns:
List -- List of block objects.
"""
return self._unknown_blocks_list
unknown_blocks_list = property(_get_unknown_blocks_list)
def _get_block_count(self):
"""Total amount of UBI blocks in file.
Returns:
Int -- Number of blocks
"""
return self._block_count
block_count = property(_get_block_count)
def _set_first_peb_num(self, i):
self._first_peb_num = i
def _get_first_peb_num(self):
"""First Physical Erase Block with UBI data
Returns:
Int -- Number of the first PEB.
"""
return self._first_peb_num
first_peb_num = property(_get_first_peb_num, _set_first_peb_num)
def _get_leb_size(self):
"""LEB size of UBI blocks in file.
Returns:
Int -- LEB Size.
"""
return self._leb_size
leb_size = property(_get_leb_size)
def _get_peb_size(self):
"""PEB size of UBI blocks in file.
Returns:
Int -- PEB Size.
"""
return self.file.block_size
peb_size = property(_get_peb_size)
def _get_min_io_size(self):
"""Min I/O Size
Returns:
Int -- Min I/O Size.
"""
return self._min_io_size
min_io_size = property(_get_min_io_size)
def _get_blocks(self):
"""Main Dict of UBI Blocks
Passed around for lists of indexes to be made or to be returned
filtered through a list. So there isn't multiple copies of blocks,
as there can be thousands.
"""
return self._blocks
blocks = property(_get_blocks)
def display(self, tab=''):
"""Print information about this object.
Argument:
Str:tab -- '\t' for spacing this object.
"""
return display.ubi(self, tab)