Skip to content

Commit 65cb8c3

Browse files
committed
Update (c) headers; auto-format
1 parent 9fdf812 commit 65cb8c3

File tree

8 files changed

+160
-82
lines changed

8 files changed

+160
-82
lines changed

Diff for: bin2hpm/__init__.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
"""
2-
SPDX-License-Identifier: BSD-3-Clause
3-
Copyright (c) 2021 Deutsches Elektronen-Synchrotron DESY.
4-
See LICENSE.txt for license details.
5-
"""
1+
###########################################################################
2+
# ____ _____________ __ __ __ _ _____ ___ _ #
3+
# / __ \/ ____/ ___/\ \/ / | \/ (_)__ _ _ __|_ _/ __| /_\ (R) #
4+
# / / / / __/ \__ \ \ / | |\/| | / _| '_/ _ \| || (__ / _ \ #
5+
# / /_/ / /___ ___/ / / / |_| |_|_\__|_| \___/|_| \___/_/ \_\ #
6+
# /_____/_____//____/ /_/ T E C H N O L O G Y L A B #
7+
# #
8+
# Copyright 2021 Deutsches Elektronen-Synchrotron DESY. #
9+
# SPDX-License-Identifier: BSD-3-Clause #
10+
# #
11+
###########################################################################
612

713
__version__ = "0.1.3"

Diff for: bin2hpm/__main__.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
"""
2-
SPDX-License-Identifier: BSD-3-Clause
3-
Copyright (c) 2021 Deutsches Elektronen-Synchrotron DESY.
4-
See LICENSE.txt for license details.
5-
"""
1+
###########################################################################
2+
# ____ _____________ __ __ __ _ _____ ___ _ #
3+
# / __ \/ ____/ ___/\ \/ / | \/ (_)__ _ _ __|_ _/ __| /_\ (R) #
4+
# / / / / __/ \__ \ \ / | |\/| | / _| '_/ _ \| || (__ / _ \ #
5+
# / /_/ / /___ ___/ / / / |_| |_|_\__|_| \___/|_| \___/_/ \_\ #
6+
# /_____/_____//____/ /_/ T E C H N O L O G Y L A B #
7+
# #
8+
# Copyright 2021 Deutsches Elektronen-Synchrotron DESY. #
9+
# SPDX-License-Identifier: BSD-3-Clause #
10+
# #
11+
###########################################################################
612

713
from bin2hpm.cli import main
814

Diff for: bin2hpm/bitfile.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
"""
2-
SPDX-License-Identifier: BSD-3-Clause
3-
Copyright (c) 2021 Deutsches Elektronen-Synchrotron DESY.
4-
See LICENSE.txt for license details.
5-
"""
1+
###########################################################################
2+
# ____ _____________ __ __ __ _ _____ ___ _ #
3+
# / __ \/ ____/ ___/\ \/ / | \/ (_)__ _ _ __|_ _/ __| /_\ (R) #
4+
# / / / / __/ \__ \ \ / | |\/| | / _| '_/ _ \| || (__ / _ \ #
5+
# / /_/ / /___ ___/ / / / |_| |_|_\__|_| \___/|_| \___/_/ \_\ #
6+
# /_____/_____//____/ /_/ T E C H N O L O G Y L A B #
7+
# #
8+
# Copyright 2021 Deutsches Elektronen-Synchrotron DESY. #
9+
# SPDX-License-Identifier: BSD-3-Clause #
10+
# #
11+
###########################################################################
612

713
import sys
814

15+
916
class BitfileReader():
1017
def __init__(self, input):
1118
self.inp = input
12-
19+
1320
def read_n(self, n):
1421
val, self.inp = self.inp[:n], self.inp[n:]
1522
return val
@@ -32,6 +39,7 @@ def read_str(self):
3239
n = self.read_u16()
3340
return self.read_n(n)
3441

42+
3543
def check_info(bs, section_expected, title):
3644
section_name = bs.read_u8()
3745
if section_name != ord(section_expected):
@@ -41,6 +49,7 @@ def check_info(bs, section_expected, title):
4149
section_info = section_info.decode('utf-8')
4250
print(f'{title}: {section_info}')
4351

52+
4453
def parse_bitfile(inp):
4554
print('Parsing bitfile...')
4655
bs = BitfileReader(inp)
@@ -59,6 +68,7 @@ def parse_bitfile(inp):
5968
raise ValueError('Bitstream section e missing')
6069

6170
payload_size = bs.read_u32()
62-
print(f' Image size: 0x{payload_size:08x} ({int(payload_size/1024)}KiB)\n')
71+
print(
72+
f' Image size: 0x{payload_size:08x} ({int(payload_size/1024)}KiB)\n')
6373

6474
return bs.read_n(payload_size)

Diff for: bin2hpm/cli.py

+30-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
"""
2-
SPDX-License-Identifier: BSD-3-Clause
3-
Copyright (c) 2021 Deutsches Elektronen-Synchrotron DESY.
4-
See LICENSE.txt for license details.
5-
"""
1+
###########################################################################
2+
# ____ _____________ __ __ __ _ _____ ___ _ #
3+
# / __ \/ ____/ ___/\ \/ / | \/ (_)__ _ _ __|_ _/ __| /_\ (R) #
4+
# / / / / __/ \__ \ \ / | |\/| | / _| '_/ _ \| || (__ / _ \ #
5+
# / /_/ / /___ ___/ / / / |_| |_|_\__|_| \___/|_| \___/_/ \_\ #
6+
# /_____/_____//____/ /_/ T E C H N O L O G Y L A B #
7+
# #
8+
# Copyright 2021 Deutsches Elektronen-Synchrotron DESY. #
9+
# SPDX-License-Identifier: BSD-3-Clause #
10+
# #
11+
###########################################################################
612

713
import argparse
814
import os
@@ -11,73 +17,75 @@
1117

1218
from bin2hpm import hpm_conv, bitfile, __version__
1319

20+
1421
def swap32(i):
1522
return struct.unpack("<I", struct.pack(">I", i))[0]
1623

24+
1725
def main():
1826
parser = argparse.ArgumentParser(
1927
description='HPM.1 update file converter'
2028
)
2129
parser.add_argument('infile',
2230
type=str,
2331
help='Input file'
24-
)
32+
)
2533
parser.add_argument('--version',
2634
action='version',
2735
version='%(prog)s ' + __version__
28-
)
36+
)
2937
parser.add_argument('-o', '--outfile',
3038
type=str,
3139
help='output file (derived from input file if not set)'
32-
)
40+
)
3341
parser.add_argument('-v', '--file-version',
3442
default='0.0',
3543
type=lambda x: [int(v, 10) for v in x.split('.')],
3644
help='file version information (format major.minor, e.g. 1.2)'
37-
)
45+
)
3846
parser.add_argument('-a', '--auxillary',
3947
type=lambda x: int(x, 16),
4048
default='00000000',
4149
help='additional metadata, hex format, 4 bytes'
42-
)
50+
)
4351
parser.add_argument('-c', '--component',
4452
type=int,
4553
default=1,
4654
help='HPM component ID (default 1)'
47-
)
55+
)
4856
parser.add_argument('-d', '--device',
4957
type=lambda x: int(x, 16),
5058
default='0',
5159
help='HPM device ID (hex, default 0)'
52-
)
60+
)
5361
parser.add_argument('-m', '--manufacturer',
5462
type=lambda x: int(x, 16),
5563
default='000000',
5664
help='IANA manufacturer ID (hex, 6 bytes max)'
57-
)
65+
)
5866
parser.add_argument('-p', '--product',
5967
type=lambda x: int(x, 16),
6068
default='0000',
6169
help='IANA product ID (hex, 4 bytes max)'
62-
)
70+
)
6371
parser.add_argument('-r', '--compress',
6472
action='store_true',
6573
help='Enable RLE compression (requires DESY MMC)'
66-
)
74+
)
6775
parser.add_argument('-s', '--description',
6876
type=str,
6977
help='Additional description string (max. 21 chars)'
70-
)
78+
)
7179

7280
force_fmt = parser.add_mutually_exclusive_group(required=False)
7381
force_fmt.add_argument('-b', '--bitfile',
7482
action='store_true',
7583
help='Force bitfile mode'
76-
)
84+
)
7785
force_fmt.add_argument('-n', '--binfile',
7886
action='store_true',
7987
help='Force binfile mode'
80-
)
88+
)
8189

8290
args = parser.parse_args()
8391

@@ -102,8 +110,10 @@ def main():
102110

103111
# Print general information
104112
print(f'bin2hpm v{__version__} (C) 2021 DESY\n')
105-
print(f'Input file {args.infile}, length {os.path.getsize(args.infile)} bytes\n')
106-
print(f'IANA Manuf., Product 0x{args.manufacturer:06x}, 0x{args.product:04x}')
113+
print(
114+
f'Input file {args.infile}, length {os.path.getsize(args.infile)} bytes\n')
115+
print(
116+
f'IANA Manuf., Product 0x{args.manufacturer:06x}, 0x{args.product:04x}')
107117
print(f'Component {args.component}, Device {args.device}')
108118
print(f'FW version {v_maj}.{v_min:02d} / 0x{args.auxillary:08x}\n')
109119

Diff for: bin2hpm/hpm.py

+49-32
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,76 @@
1-
"""
2-
SPDX-License-Identifier: BSD-3-Clause
3-
Copyright (c) 2021 Deutsches Elektronen-Synchrotron DESY.
4-
See LICENSE.txt for license details.
5-
"""
1+
###########################################################################
2+
# ____ _____________ __ __ __ _ _____ ___ _ #
3+
# / __ \/ ____/ ___/\ \/ / | \/ (_)__ _ _ __|_ _/ __| /_\ (R) #
4+
# / / / / __/ \__ \ \ / | |\/| | / _| '_/ _ \| || (__ / _ \ #
5+
# / /_/ / /___ ___/ / / / |_| |_|_\__|_| \___/|_| \___/_/ \_\ #
6+
# /_____/_____//____/ /_/ T E C H N O L O G Y L A B #
7+
# #
8+
# Copyright 2021 Deutsches Elektronen-Synchrotron DESY. #
9+
# SPDX-License-Identifier: BSD-3-Clause #
10+
# #
11+
###########################################################################
612

713
from datetime import datetime
814
import sys
915
from enum import Enum
10-
import hashlib
11-
16+
import hashlib
17+
18+
1219
class UpgradeActionType(Enum):
1320
Backup = 0
1421
Prepare = 1
1522
Upload = 2
1623

1724
# name - default value - size
1825

26+
1927
UPG_IMG_HEADER = (
20-
( 'signature', 'PICMGFWU', 8 ),
21-
( 'format_version', 0, 1 ),
22-
( 'device_id', 0, 1 ),
23-
( 'manufacturer_id', 0, 3),
24-
( 'product_id', 0, 2 ),
25-
( 'time', int(datetime.now().timestamp()), 4 ),
26-
( 'img_caps', 0, 1 ),
27-
( 'components', 0, 1 ),
28-
( 'selftest_to', 0, 1 ),
29-
( 'rollback_to', 0, 1 ),
30-
( 'inaccess_to', 2, 1 ),
31-
( 'earliest_compat_rev', 0, 2 ),
32-
( 'version_major', 0, 1 ),
33-
( 'version_minor', 0, 1 ),
34-
( 'version_aux', 0, 4 ),
35-
( 'oem_data_len', 0, 2 ),
28+
('signature', 'PICMGFWU', 8),
29+
('format_version', 0, 1),
30+
('device_id', 0, 1),
31+
('manufacturer_id', 0, 3),
32+
('product_id', 0, 2),
33+
('time', int(datetime.now().timestamp()), 4),
34+
('img_caps', 0, 1),
35+
('components', 0, 1),
36+
('selftest_to', 0, 1),
37+
('rollback_to', 0, 1),
38+
('inaccess_to', 2, 1),
39+
('earliest_compat_rev', 0, 2),
40+
('version_major', 0, 1),
41+
('version_minor', 0, 1),
42+
('version_aux', 0, 4),
43+
('oem_data_len', 0, 2),
3644
)
3745

3846
UPG_ACTION_HEADER = (
39-
( 'action_type', 0, 1 ),
40-
( 'components', 0, 1 ),
47+
('action_type', 0, 1),
48+
('components', 0, 1),
4149
)
4250

4351
UPG_ACTION_IMG_INFO = (
44-
( 'version_major', 0, 1 ),
45-
( 'version_minor', 0, 1 ),
46-
( 'version_aux', 0, 4 ),
47-
( 'desc_str', '', 21 ),
48-
( 'img_length', 0, 4 ),
52+
('version_major', 0, 1),
53+
('version_minor', 0, 1),
54+
('version_aux', 0, 4),
55+
('desc_str', '', 21),
56+
('img_length', 0, 4),
4957
)
5058

59+
5160
def fixed_str(s, n):
5261
s = s[:n]
5362
s += '\x00' * (n - len(s))
5463
return s.encode('utf-8')
5564

65+
5666
def zero_cksum(input):
5767
return int.to_bytes((0x100 - sum(input)) & 0xff, length=1, byteorder='little')
5868

69+
5970
def to_bcd(value):
6071
return int(str(value), 16)
6172

73+
6274
def encode_generic(table, values):
6375
result = b''
6476
for name, default, size in table:
@@ -74,25 +86,29 @@ def encode_generic(table, values):
7486
elif isinstance(val, Enum):
7587
val = val.value
7688
result += int.to_bytes(val, length=size, byteorder='little')
77-
89+
7890
except OverflowError:
79-
print(f'{name} value of {val} doesn\'t fit in {size} bytes', file=sys.stderr)
91+
print(f'{name} value of {val} doesn\'t fit in {size} bytes',
92+
file=sys.stderr)
8093
sys.exit(-1)
8194
except TypeError:
8295
print(f'Couldn\'t encode {name} value of {val}', file=sys.stderr)
8396
sys.exit(-1)
8497
return result
8598

99+
86100
def upg_image_hdr(**kwargs):
87101
result = encode_generic(UPG_IMG_HEADER, kwargs)
88102
result += zero_cksum(result)
89103
return result
90104

105+
91106
def upg_action_hdr(**kwargs):
92107
result = encode_generic(UPG_ACTION_HEADER, kwargs)
93108
result += zero_cksum(result)
94109
return result
95110

111+
96112
def upg_action_img(img_data, **kwargs):
97113
result = upg_action_hdr(**{
98114
'action_type': UpgradeActionType.Upload,
@@ -105,5 +121,6 @@ def upg_action_img(img_data, **kwargs):
105121
result += img_data
106122
return result
107123

124+
108125
def upg_img_hash(hpm_img):
109126
return hashlib.md5(hpm_img).digest()

Diff for: bin2hpm/hpm_conv.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1+
###########################################################################
2+
# ____ _____________ __ __ __ _ _____ ___ _ #
3+
# / __ \/ ____/ ___/\ \/ / | \/ (_)__ _ _ __|_ _/ __| /_\ (R) #
4+
# / / / / __/ \__ \ \ / | |\/| | / _| '_/ _ \| || (__ / _ \ #
5+
# / /_/ / /___ ___/ / / / |_| |_|_\__|_| \___/|_| \___/_/ \_\ #
6+
# /_____/_____//____/ /_/ T E C H N O L O G Y L A B #
7+
# #
8+
# Copyright 2021 Deutsches Elektronen-Synchrotron DESY. #
9+
# SPDX-License-Identifier: BSD-3-Clause #
10+
# #
11+
###########################################################################
12+
113
from bin2hpm import hpm, rle
214
import sys
315

416
# supported arguments:
517
# device_id, manufacturer_id, product_id, components, version_major, version_minor, version_aux
18+
19+
620
def hpm_conv(img_data, compression_enable, **kwargs):
721
# Build HPM upgrade image header
822
result = hpm.upg_image_hdr(**kwargs)

0 commit comments

Comments
 (0)