Skip to content

Commit b2dd71f

Browse files
committed
Build HPM file outside of cli.py, support using it from a different module
1 parent 62d8e83 commit b2dd71f

2 files changed

Lines changed: 48 additions & 41 deletions

File tree

bin2hpm/cli.py

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import sys
1010
import struct
1111

12-
from bin2hpm import hpm, rle, bitfile, __version__
12+
from bin2hpm import hpm_conv, bitfile, __version__
1313

1414
def swap32(i):
1515
return struct.unpack("<I", struct.pack(">I", i))[0]
@@ -107,22 +107,10 @@ def main():
107107
print(f'Component {args.component}, Device {args.device}')
108108
print(f'FW version {v_maj}.{v_min:02d} / 0x{args.auxillary:08x}\n')
109109

110-
# Build HPM upgrade image header
111-
result = hpm.upg_image_hdr(
112-
device_id=args.device,
113-
manufacturer_id=args.manufacturer,
114-
product_id=args.product,
115-
components=components,
116-
version_major=v_maj,
117-
version_minor=v_min,
118-
version_aux=v_aux,
119-
)
120-
121-
# Append HPM upgrade action (HPM prepare action)
122-
result += hpm.upg_action_hdr(
123-
action_type=hpm.UpgradeActionType.Prepare,
124-
components=components
125-
)
110+
# Determine outfile name
111+
outfile = args.outfile
112+
if not outfile:
113+
outfile = os.path.splitext(args.infile)[0] + '.hpm'
126114

127115
# Read input file
128116
with open(args.infile, 'rb') as f:
@@ -132,37 +120,19 @@ def main():
132120
if bitmode:
133121
img_data = bitfile.parse_bitfile(img_data)
134122

135-
# Compress data if compression enabled
136-
if args.compress:
137-
enc_data = rle.encode(img_data)
138-
print('Verifying compressed data...', end='')
139-
if rle.decode(enc_data) != img_data:
140-
print(f'RLE compression verify mismatch', file=sys.stderr)
141-
sys.exit(-1)
142-
print('OK\n')
143-
144-
img_comp_hdr = b'COMPRESSED\x00'
145-
img_comp_hdr += int.to_bytes(len(img_data), length=4, byteorder='big')
146-
img_data = img_comp_hdr + enc_data
147-
148-
# Append HPM upgrade action image
149-
result += hpm.upg_action_img(
123+
# Do the actual conversion
124+
result = hpm_conv.hpm_conv(
150125
img_data,
126+
args.compress,
127+
device_id=args.device,
128+
manufacturer_id=args.manufacturer,
129+
product_id=args.product,
151130
components=components,
152131
version_major=v_maj,
153132
version_minor=v_min,
154133
version_aux=v_aux,
155134
desc_str=args.description or os.path.basename(args.infile)[:20]
156135
)
157-
158-
# Append MD5 hash
159-
result += hpm.upg_img_hash(result)
160-
161-
# Determine outfile name
162-
outfile = args.outfile
163-
if not outfile:
164-
outfile = os.path.splitext(args.infile)[0] + '.hpm'
165-
166136
# Write HPM file
167137
with open(outfile, 'wb') as f:
168138
f.write(result)

bin2hpm/hpm_conv.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from bin2hpm import hpm, rle
2+
import sys
3+
4+
# supported arguments:
5+
# device_id, manufacturer_id, product_id, components, version_major, version_minor, version_aux
6+
def hpm_conv(img_data, compression_enable, **kwargs):
7+
# Build HPM upgrade image header
8+
result = hpm.upg_image_hdr(**kwargs)
9+
10+
# Append HPM upgrade action (HPM prepare action)
11+
result += hpm.upg_action_hdr(**{
12+
'action_type': hpm.UpgradeActionType.Prepare,
13+
**kwargs
14+
})
15+
16+
# Compress data if compression enabled
17+
if compression_enable:
18+
enc_data = rle.encode(img_data)
19+
print('Verifying compressed data...', end='')
20+
if rle.decode(enc_data) != img_data:
21+
print(f'RLE compression verify mismatch', file=sys.stderr)
22+
sys.exit(-1)
23+
print('OK\n')
24+
25+
img_comp_hdr = b'COMPRESSED\x00'
26+
img_comp_hdr += int.to_bytes(len(img_data), length=4, byteorder='big')
27+
img_data = img_comp_hdr + enc_data
28+
29+
# Append HPM upgrade action image
30+
result += hpm.upg_action_img(
31+
img_data,
32+
**kwargs
33+
)
34+
35+
# Append MD5 hash
36+
result += hpm.upg_img_hash(result)
37+
return result

0 commit comments

Comments
 (0)