-
Notifications
You must be signed in to change notification settings - Fork 191
/
make_bios.py
executable file
·115 lines (92 loc) · 4.16 KB
/
make_bios.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
#!/usr/local/bin/python3
"""
Copyright (c) 2024 Deciso B.V.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""
import os
import argparse
import zipfile
import shutil
import hashlib
def extract_zip(zip_path, extract_to):
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
for member in zip_ref.namelist():
# Remove the top-level directory name
member_name = member.split('/', 1)[1] if '/' in member else member
target_path = os.path.join(extract_to, member_name)
if not member.endswith('/'):
with zip_ref.open(member) as source, open(target_path, 'wb') as target:
shutil.copyfileobj(source, target)
else:
os.makedirs(target_path, exist_ok=True)
def extract_file(file_path, extract_to):
if zipfile.is_zipfile(file_path):
extract_zip(file_path, extract_to)
else:
shutil.copy(file_path, os.path.join(extract_to, 'LATEST.FD'))
def create_tar_bz2(source_dir, output_file):
shutil.make_archive(
base_name=output_file,
format='gztar',
root_dir=source_dir
)
def merge_files(file1_path, file2_path, output_path):
temp_dir = 'temp_extracted'
os.makedirs(temp_dir, exist_ok=True)
extract_file(file1_path, temp_dir)
extract_file(file2_path, temp_dir)
output_dir_name = os.path.splitext(os.path.basename(output_path))[0]
combined_dir = os.path.join(temp_dir, output_dir_name)
os.makedirs(combined_dir, exist_ok=True)
for item in os.listdir(temp_dir):
item_path = os.path.join(temp_dir, item)
if item != output_dir_name:
shutil.move(item_path, combined_dir)
create_tar_bz2(combined_dir, output_path)
shutil.rmtree(temp_dir)
def calculate_sha256(file_path):
sha256_hash = hashlib.sha256()
with open(file_path, 'rb') as f:
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--platform', help='BIOS platform [A10|A20|A30]')
parser.add_argument('--version', help='BIOS version (for example: 13a)')
parser.add_argument('--source', help='BIOS ROM image file (.fd)')
args = parser.parse_args()
if not args.platform or args.platform not in ['A10', 'A20', 'A30']:
print('invalid platform, expected A10, A20 or A30')
exit(1)
if not args.version:
print('No version specified')
exit(1)
if not args.source.lower().endswith('.fd'):
print('invalid source file')
exit(1)
if not os.path.isfile(args.source):
print('source file not found')
exit(1)
static = 'source/hardware/files/BIOS_update_sources.zip'
source = args.source
output = f'source/hardware/files/{args.platform}_v{args.version}_bios'
merge_files(static, source, output)
print(calculate_sha256(f'{output}.tar.gz'), f'{output}.tar.gz')