aboutsummaryrefslogtreecommitdiff
path: root/libnxt/make_flash_header.py
blob: 79018915a5bb79a3b51dbcc3b558b98d10d8779e (plain)
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
#!/usr/bin/env python
#
# Take the flash_routine.bin file, and embed it as an array of bytes
# in a flash_routine.h, ready for packaging with the C firmware
# flasher.
#
# If a file name is provided on the commandline, load that file as the
# firmware flashing routine instead.
#

from sys import argv

if len(argv) == 2:
    fname = argv[1]
else:
    fname = 'flash_write/flash_write.bin'

fwbin = file(fname)

# Build the char representation in memory

def char_by_char(f):
    while True:
        d = f.read(1)
        if d == '':
            raise StopIteration
        yield d

data = []
for c in char_by_char(fwbin):
    data.append("0x%s" % c.encode('hex'))

for i in range(0, len(data), 12):
    data[i] = "\n" + data[i]

data_str = ', '.join(data)
len_data = "0x%X" % len(data)

# Read in the template
tplfile = file('flash_routine.h.base')
template = tplfile.read()
tplfile.close()

# Replace the values in the template
template = template.replace('___FLASH_BIN___', data_str)
template = template.replace('___FLASH_LEN___', len_data)

# Output the done header
out = file('flash_routine.h', 'w')
out.write(template)
out.close()