-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathimage_class.py
More file actions
executable file
·393 lines (363 loc) · 16.1 KB
/
image_class.py
File metadata and controls
executable file
·393 lines (363 loc) · 16.1 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Huawei Device Co., Ltd.
# 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.
import bisect
import copy
import os
import struct
import tempfile
from hashlib import sha256
from log_exception import UPDATE_LOGGER
from blocks_manager import BlocksManager
from utils import OPTIONS_MANAGER
from utils import EXTEND_VALUE
from utils import FILE_MAP_ZERO_KEY
from utils import FILE_MAP_NONZERO_KEY
from utils import FILE_MAP_COPY_KEY
from utils import MAX_BLOCKS_PER_GROUP
from utils import UPDATE_BIN_FILE_NAME
from utils import FORBIDEN_UPDATE_IMAGE_SET
class FullUpdateImage:
"""
Full image processing class
"""
def __init__(self, target_package_images_dir,
full_img_list, full_img_name_list,
verse_script, full_image_path_list,
no_zip=False):
self.target_package_images_dir = target_package_images_dir
self.full_img_list = full_img_list
self.full_img_name_list = full_img_name_list
self.verse_script = verse_script
self.full_image_path_list = full_image_path_list
self.no_zip = no_zip
def update_full_image(self):
"""
Processing of the full image
:return full_image_content_len_list: full image content length list
:return full_image_file_obj_list: full image temporary file list
"""
full_image_file_obj_list = []
full_image_content_len_list = []
for idx, each_name in enumerate(self.full_img_list):
full_image_content = self.get_full_image_content(
self.full_image_path_list[idx])
img_name = self.full_img_name_list[idx][:-4]
if full_image_content is False:
UPDATE_LOGGER.print_log(
"Get full image content failed!",
log_type=UPDATE_LOGGER.ERROR_LOG)
return False, False
each_img = tempfile.NamedTemporaryFile(
dir=self.target_package_images_dir,
prefix="full_image%s" % img_name, mode='wb')
each_img.write(full_image_content)
each_img.seek(0)
full_image_content_len_list.append(len(full_image_content))
full_image_file_obj_list.append(each_img)
# 全量流式升级处理
OPTIONS_MANAGER.full_image_new_data[each_name] = full_image_content
UPDATE_LOGGER.print_log(
"Image %s full processing completed" % img_name)
update_image_set = set(self.full_img_list) - FORBIDEN_UPDATE_IMAGE_SET
if not self.no_zip and len(update_image_set) != 0:
# No zip mode (no script command)
image_write_cmd = self.verse_script.full_image_update(UPDATE_BIN_FILE_NAME)
cmd = '%s_WRITE_FLAG%s' % (UPDATE_BIN_FILE_NAME, image_write_cmd)
if each_name not in FORBIDEN_UPDATE_IMAGE_SET:
self.verse_script.add_command(cmd=cmd)
UPDATE_LOGGER.print_log(
"All full image processing completed! image count: %d" %
len(self.full_img_list))
return full_image_content_len_list, full_image_file_obj_list
@staticmethod
def get_full_image_content(each_name):
"""
Obtain the full image content.
:param each_name: image name
:return content: full image content if available; false otherwise
"""
each_image_path = each_name
if not os.path.exists(each_image_path):
UPDATE_LOGGER.print_log(
"The file is missing "
"from the target package, "
"the component: %s cannot be full update processed. " %
each_image_path)
return False
with open(each_image_path, 'rb') as f_r:
content = f_r.read()
return content
class IncUpdateImage:
"""
Increment update image class
"""
def __init__(self, image_path, map_path):
"""
Initialize the inc image.
:param image_path: img file path
:param map_path: map file path
"""
self.image_path = image_path
self.map_path = map_path
self.offset_value_list = []
self.care_block_range = None
self.extended_range = None
self.reserved_blocks = BlocksManager("0")
self.file_map = []
self.offset_index = []
self.block_size = None
self.total_blocks = None
self.parse_raw_image_file(image_path, map_path)
def parse_raw_image_file(self, image_path, map_path):
"""
Parse the .img file.
:param image_path: img file path
:param map_path: map file path
"""
self.block_size = block_size = 4096
self.total_blocks = total_blocks = \
os.path.getsize(self.image_path) // self.block_size
reference = b'\0' * self.block_size
with open(image_path, 'rb') as f_r:
care_value_list, offset_value_list = [], []
nonzero_blocks = []
for i in range(self.total_blocks):
blocks_data = f_r.read(self.block_size)
if blocks_data != reference:
nonzero_blocks.append(i)
nonzero_blocks.append(i + 1)
self.care_block_range = BlocksManager(nonzero_blocks)
care_value_list = list(self.care_block_range.range_data)
for idx, value in enumerate(care_value_list):
if idx != 0 and (idx + 1) % 2 == 0:
be_value = int(care_value_list[idx - 1])
af_value = int(care_value_list[idx])
file_tell = be_value * block_size
offset_value_list.append(
(be_value, af_value - be_value,
file_tell, None))
self.offset_index = [i[0] for i in offset_value_list]
self.offset_value_list = offset_value_list
extended_range = \
self.care_block_range.extend_value_to_blocks(EXTEND_VALUE)
all_blocks = BlocksManager(range_data=(0, total_blocks))
self.extended_range = \
extended_range.get_intersect_with_other(all_blocks). \
get_subtract_with_other(self.care_block_range)
self.parse_block_map_file(map_path, f_r)
def parse_block_map_file(self, map_path, image_file_r):
"""
Parses the map file for blocks where files are contained in the image.
:param map_path: map file path
:param image_file_r: file reading object
:return:
"""
remain_range = self.care_block_range
temp_file_map = {}
with open(map_path, 'r') as f_r:
# Read the .map file and process each line.
for each_line in f_r.readlines():
each_map_path, ranges_value = each_line.split(None, 1)
each_range = BlocksManager(ranges_value)
each_range = each_range.get_subtract_with_other(BlocksManager("0"))
# each_range may not contained in the remain range.
intersect_range = each_range.get_intersect_with_other(remain_range)
if each_range.size() != intersect_range.size():
each_range = intersect_range
temp_file_map[each_map_path] = each_range
# After the processing is complete,
# remove each_range from remain_range.
remain_range = remain_range.get_subtract_with_other(each_range)
reserved_blocks = self.reserved_blocks
# Remove reserved blocks from all blocks.
remain_range = remain_range.get_subtract_with_other(reserved_blocks)
# Divide all blocks into zero_blocks
# (if there are many) and nonzero_blocks.
zero_blocks_list = []
nonzero_blocks_list = []
nonzero_groups_list = []
default_zero_block = ('\0' * self.block_size).encode()
nonzero_blocks_list, nonzero_groups_list, zero_blocks_list = \
self.apply_remain_range(
default_zero_block, image_file_r, nonzero_blocks_list,
nonzero_groups_list, remain_range, zero_blocks_list)
temp_file_map = self.get_file_map(
nonzero_blocks_list, nonzero_groups_list,
reserved_blocks, temp_file_map, zero_blocks_list)
self.file_map = temp_file_map
def apply_remain_range(self, *args):
"""
Implement traversal processing of remain_range.
"""
default_zero_block, image_file_r, \
nonzero_blocks_list, nonzero_groups_list, \
remain_range, zero_blocks_list = args
for start_value, end_value in remain_range:
for each_value in range(start_value, end_value):
# bisect 二分查找,b在self.offset_index中的位置
idx = bisect.bisect_right(self.offset_index, each_value) - 1
chunk_start, _, file_pos, fill_data = \
self.offset_value_list[idx]
data = self.get_file_data(self.block_size, chunk_start,
default_zero_block, each_value,
file_pos, fill_data, image_file_r)
zero_blocks_list, nonzero_blocks_list, nonzero_groups_list = \
self.get_zero_nonzero_blocks_list(
data, default_zero_block, each_value,
nonzero_blocks_list, nonzero_groups_list,
zero_blocks_list)
return nonzero_blocks_list, nonzero_groups_list, zero_blocks_list
@staticmethod
def get_file_map(*args):
"""
Obtain the file map.
nonzero_blocks_list nonzero blocks list,
nonzero_groups_list nonzero groups list,
reserved_blocks reserved blocks ,
temp_file_map temporary file map,
zero_blocks_list zero block list
:return temp_file_map file map
"""
nonzero_blocks_list, nonzero_groups_list, \
reserved_blocks, temp_file_map, zero_blocks_list = args
if nonzero_blocks_list:
nonzero_groups_list.append(nonzero_blocks_list)
if zero_blocks_list:
temp_file_map[FILE_MAP_ZERO_KEY] = \
BlocksManager(range_data=zero_blocks_list)
if nonzero_groups_list:
for i, blocks in enumerate(nonzero_groups_list):
temp_file_map["%s-%d" % (FILE_MAP_NONZERO_KEY, i)] = \
BlocksManager(range_data=blocks)
if reserved_blocks:
temp_file_map[FILE_MAP_COPY_KEY] = reserved_blocks
return temp_file_map
@staticmethod
def get_zero_nonzero_blocks_list(*args):
"""
Get zero_blocks_list, nonzero_blocks_list, and nonzero_groups_list.
data: block data,
default_zero_block: default to zero block,
each_value: each value,
nonzero_blocks_list: nonzero_blocks_list,
nonzero_groups_list: nonzero_groups_list,
zero_blocks_list: zero_blocks_list,
:return new_zero_blocks_list: new zero blocks list,
:return new_nonzero_blocks_list: new nonzero blocks list,
:return new_nonzero_groups_list: new nonzero groups list.
"""
data, default_zero_block, each_value, \
nonzero_blocks_list, nonzero_groups_list, \
zero_blocks_list = args
# Check whether the data block is equal to the default zero_blocks.
if data == default_zero_block:
zero_blocks_list.append(each_value)
zero_blocks_list.append(each_value + 1)
else:
nonzero_blocks_list.append(each_value)
nonzero_blocks_list.append(each_value + 1)
# The number of nonzero_blocks is greater than
# or equal to the upper limit.
if len(nonzero_blocks_list) >= MAX_BLOCKS_PER_GROUP:
nonzero_groups_list.append(nonzero_blocks_list)
nonzero_blocks_list = []
new_zero_blocks_list, new_nonzero_blocks_list, \
new_nonzero_groups_list = \
copy.copy(zero_blocks_list), \
copy.copy(nonzero_blocks_list),\
copy.copy(nonzero_groups_list)
return new_zero_blocks_list, new_nonzero_blocks_list, \
new_nonzero_groups_list
@staticmethod
def get_file_data(*args):
"""
Get the file data.
block_size: blocksize,
chunk_start: the start position of chunk,
default_zero_block: default to zero blocks,
each_value: each_value,
file_pos: file position,
fill_data: data,
image_file_r: read file object,
:return data: Get the file data.
"""
block_size, chunk_start, default_zero_block, each_value, \
file_pos, fill_data, image_file_r = args
if file_pos is not None:
file_pos += (each_value - chunk_start) * block_size
image_file_r.seek(file_pos, os.SEEK_SET)
data = image_file_r.read(block_size)
else:
if fill_data == default_zero_block[:4]:
data = default_zero_block
else:
data = None
return data
def range_sha256(self, ranges):
"""
range sha256 hash content
:param ranges: ranges value
:return:
"""
hash_obj = sha256()
for data in self.__get_blocks_set_data(ranges):
hash_obj.update(data)
return hash_obj.hexdigest()
def write_range_data_2_fd(self, ranges, file_obj):
"""
write range data to fd
:param ranges: ranges obj
:param file_obj: file obj
:return:
"""
for data in self.__get_blocks_set_data(ranges):
file_obj.write(data)
def get_ranges(self, ranges):
"""
get ranges value
:param ranges: ranges
:return: ranges value
"""
return [each_data for each_data in self.__get_blocks_set_data(ranges)]
def __get_blocks_set_data(self, blocks_set_data):
"""
Get the range data.
"""
with open(self.image_path, 'rb') as f_r:
for start, end in blocks_set_data:
diff_value = end - start
idx = bisect.bisect_right(self.offset_index, start) - 1
chunk_start, chunk_len, file_pos, fill_data = \
self.offset_value_list[idx]
remain = chunk_len - (start - chunk_start)
this_read = min(remain, diff_value)
if file_pos is not None:
pos = file_pos + ((start - chunk_start) * self.block_size)
f_r.seek(pos, os.SEEK_SET)
yield f_r.read(this_read * self.block_size)
else:
yield fill_data * (this_read * (self.block_size >> 2))
diff_value -= this_read
while diff_value > 0:
idx += 1
chunk_start, chunk_len, file_pos, fill_data = \
self.offset_value_list[idx]
this_read = min(chunk_len, diff_value)
if file_pos is not None:
f_r.seek(file_pos, os.SEEK_SET)
yield f_r.read(this_read * self.block_size)
else:
yield fill_data * (this_read * (self.block_size >> 2))
diff_value -= this_read