2020-04-26 20:21:28 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
|
|
|
|
2020-06-09 22:30:21 +02:00
|
|
|
import binascii
|
2020-04-26 20:21:28 +02:00
|
|
|
import intelhex
|
|
|
|
import sys
|
|
|
|
|
|
|
|
def generate_c(ihex):
|
|
|
|
print('/* this file is auto-generated - DO NOT EDIT */')
|
|
|
|
print()
|
|
|
|
print('#include <stdint.h>')
|
|
|
|
print()
|
|
|
|
print('struct segment {')
|
|
|
|
print(' uint32_t start;');
|
|
|
|
print(' uint32_t end;');
|
2020-06-09 22:30:21 +02:00
|
|
|
print(' uint32_t crc32;')
|
2020-04-26 20:21:28 +02:00
|
|
|
print(' const uint8_t *data;')
|
|
|
|
print('};')
|
|
|
|
print()
|
|
|
|
|
2020-11-04 20:06:03 +01:00
|
|
|
segments = []
|
|
|
|
chunk = 32 * 1024
|
|
|
|
for (start, end) in ihex.segments():
|
|
|
|
while start + chunk < end:
|
|
|
|
segments.append((start, start + chunk))
|
|
|
|
start += chunk
|
|
|
|
if start < end:
|
|
|
|
segments.append((start, end))
|
|
|
|
|
|
|
|
for i, segment in enumerate(segments):
|
2020-04-26 20:21:28 +02:00
|
|
|
print(f'static const uint8_t segment{i}[] = {{', end='')
|
|
|
|
|
|
|
|
for j in range(segment[0], segment[1]):
|
|
|
|
if 0 == j % 12:
|
|
|
|
print('\n ', end='')
|
|
|
|
print(f' 0x{ihex[j]:02x},', end='')
|
|
|
|
|
|
|
|
print('\n};\n')
|
|
|
|
print(f'const struct segment segments[] = {{')
|
2020-11-04 20:06:03 +01:00
|
|
|
for i, segment in enumerate(segments):
|
2020-06-09 22:30:21 +02:00
|
|
|
sg = ihex.tobinarray(start=segment[0], end=segment[1]-1)
|
|
|
|
crc = binascii.crc32(sg)
|
|
|
|
print(f' 0x{segment[0]:08x}, 0x{segment[1]:08x}, 0x{crc:08x}, segment{i},')
|
2020-04-26 20:21:28 +02:00
|
|
|
print('};')
|
|
|
|
|
|
|
|
ihex = intelhex.IntelHex()
|
|
|
|
ihex.loadhex(sys.argv[1])
|
|
|
|
generate_c(ihex)
|