Skip to content

Commit 3e09b11

Browse files
committed
Add quiet mode
1 parent 54a502c commit 3e09b11

4 files changed

Lines changed: 42 additions & 27 deletions

File tree

bin2hpm/bitfile.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,39 @@ def read_str(self):
4040
return self.read_n(n)
4141

4242

43-
def check_info(bs, section_expected, title):
43+
def check_info(bs, section_expected, title, quiet_mode=False):
4444
section_name = bs.read_u8()
4545
if section_name != ord(section_expected):
4646
raise ValueError(f'Bitstream section {section_expected} missing')
4747

4848
section_info = bs.read_str()
4949
section_info = section_info.decode('utf-8')
50-
print(f'{title}: {section_info}')
5150

51+
if not quiet_mode:
52+
print(f'{title}: {section_info}')
5253

53-
def parse_bitfile(inp):
54-
print('Parsing bitfile...')
54+
55+
def parse_bitfile(inp, quiet_mode=False):
56+
if not quiet_mode:
57+
print('Parsing bitfile...')
5558
bs = BitfileReader(inp)
5659
prologue = bs.read_str()
5760
one = bs.read_u16()
5861
if len(prologue) != 9 or one != 1:
5962
raise ValueError('Bitstream header invalid')
6063

61-
check_info(bs, 'a', ' Design info')
62-
check_info(bs, 'b', ' Part name')
63-
check_info(bs, 'c', ' File date')
64-
check_info(bs, 'd', ' time')
64+
check_info(bs, 'a', ' Design info', quiet_mode)
65+
check_info(bs, 'b', ' Part name', quiet_mode)
66+
check_info(bs, 'c', ' File date', quiet_mode)
67+
check_info(bs, 'd', ' time', quiet_mode)
6568

6669
section_e = bs.read_u8()
6770
if section_e != ord('e'):
6871
raise ValueError('Bitstream section e missing')
6972

7073
payload_size = bs.read_u32()
71-
print(
72-
f' Image size: 0x{payload_size:08x} ({int(payload_size/1024)}KiB)\n')
74+
if not quiet_mode:
75+
print(
76+
f' Image size: 0x{payload_size:08x} ({int(payload_size/1024)}KiB)\n')
7377

7478
return bs.read_n(payload_size)

bin2hpm/cli.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def main():
7676
type=str,
7777
help='Additional description string (max. 21 chars)'
7878
)
79+
parser.add_argument('-q', '--quiet',
80+
action='store_true',
81+
help='Quiet mode'
82+
)
7983

8084
force_fmt = parser.add_mutually_exclusive_group(required=False)
8185
force_fmt.add_argument('-b', '--bitfile',
@@ -108,14 +112,15 @@ def main():
108112
print(f'Input file {args.infile} not found', file=sys.stderr)
109113
sys.exit(-1)
110114

111-
# Print general information
112-
print(f'bin2hpm v{__version__} (C) 2021 DESY\n')
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}')
117-
print(f'Component {args.component}, Device {args.device}')
118-
print(f'FW version {v_maj}.{v_min:02d} / 0x{args.auxillary:08x}\n')
115+
if not args.quiet:
116+
# Print general information
117+
print(f'bin2hpm v{__version__} (C) 2021 DESY\n')
118+
print(
119+
f'Input file {args.infile}, length {os.path.getsize(args.infile)} bytes\n')
120+
print(
121+
f'IANA Manuf., Product 0x{args.manufacturer:06x}, 0x{args.product:04x}')
122+
print(f'Component {args.component}, Device {args.device}')
123+
print(f'FW version {v_maj}.{v_min:02d} / 0x{args.auxillary:08x}\n')
119124

120125
# Determine outfile name
121126
outfile = args.outfile
@@ -128,12 +133,13 @@ def main():
128133

129134
# Parse bitfile if bitmode enabled
130135
if bitmode:
131-
img_data = bitfile.parse_bitfile(img_data)
136+
img_data = bitfile.parse_bitfile(img_data, args.quiet)
132137

133138
# Do the actual conversion
134139
result = hpm_conv.hpm_conv(
135140
img_data,
136141
args.compress,
142+
quiet_mode=args.quiet,
137143
device_id=args.device,
138144
manufacturer_id=args.manufacturer,
139145
product_id=args.product,
@@ -147,4 +153,5 @@ def main():
147153
with open(outfile, 'wb') as f:
148154
f.write(result)
149155

150-
print(f'Output file {outfile}, length {len(result)} bytes')
156+
if not args.quiet:
157+
print(f'Output file {outfile}, length {len(result)} bytes')

bin2hpm/hpm_conv.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# device_id, manufacturer_id, product_id, components, version_major, version_minor, version_aux
1818

1919

20-
def hpm_conv(img_data, compression_enable, **kwargs):
20+
def hpm_conv(img_data, compression_enable, quiet_mode=False, **kwargs):
2121
# Build HPM upgrade image header
2222
result = hpm.upg_image_hdr(**kwargs)
2323

@@ -29,12 +29,14 @@ def hpm_conv(img_data, compression_enable, **kwargs):
2929

3030
# Compress data if compression enabled
3131
if compression_enable:
32-
enc_data = rle.encode(img_data)
33-
print('Verifying compressed data...')
32+
enc_data = rle.encode(img_data, quiet_mode)
33+
if not quiet_mode:
34+
print('Verifying compressed data...')
3435
if rle.decode(enc_data) != img_data:
3536
print(f'RLE compression verify mismatch', file=sys.stderr)
3637
sys.exit(-1)
37-
print('RLE compression verify OK\n')
38+
if not quiet_mode:
39+
print('RLE compression verify OK\n')
3840

3941
img_comp_hdr = b'COMPRESSED\x00'
4042
img_comp_hdr += int.to_bytes(len(img_data), length=4, byteorder='big')

bin2hpm/rle.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ def to_byte(b):
1919
return bytes([b])
2020

2121

22-
def encode(data):
22+
def encode(data, quiet_mode=False):
2323
def check_block(data, k):
2424
n = 1
2525
while (len(data) - k) > n and data[k+n] == data[k] and n < RLE_MAX_BLKSIZE:
2626
n += 1
2727
return n, data[k]
2828

29-
print('Performing RLE compression ...')
29+
if not quiet_mode:
30+
print('Performing RLE compression ...')
3031
orig_len = len(data)
3132

3233
result = b''
@@ -58,7 +59,8 @@ def check_block(data, k):
5859
comp_len = len(result)
5960
comp_ratio = 100 * comp_len / orig_len
6061

61-
print(f'Compressed size {comp_len} bytes, ratio {comp_ratio:.1f}%')
62+
if not quiet_mode:
63+
print(f'Compressed size {comp_len} bytes, ratio {comp_ratio:.1f}%')
6264
return result
6365

6466

0 commit comments

Comments
 (0)