xref: /btstack/chipset/em9301/convert_emp.py (revision c32dba203d4af5ccae95e4d7b5aa9cabd63fc4ec)
1#!/usr/bin/env python
2# BlueKitchen GmbH (c) 2018
3import sys
4
5usage = '''This script converts a set of configurations and patches in .emp format for EM9304
6into C files to be used with BTstack.
7
8Usage:
9$ ./convert_emp.py container.emp
10'''
11
12header = '''/**
13 * BASENAME.h converted from BASENAME.emp
14 */
15
16#ifndef __BASENAME_H
17#define __BASENAME_H
18
19#include <stdint.h>
20extern const uint8_t  container_blob_data[];
21extern const uint32_t container_blob_size;
22extern const char *   container_blob_name;
23
24#endif
25'''
26
27code_start = '''/**
28 * BASENAME.c converted from BASENAME.bin
29 * Size: SIZE bytes
30 */
31
32#include <stdint.h>
33
34const char *   container_blob_name = "BASENAME";
35
36const uint8_t  container_blob_data[] = {
37'''
38
39code_end = '''
40};
41const uint32_t container_blob_size = sizeof(container_blob_data);
42'''
43
44def convert_emp(basename):
45	emp_name = basename + '.emp'
46	print ('Reading %s' % emp_name)
47
48	with open (emp_name, 'rb') as fin:
49		firm = fin.read()
50		size = len(firm)
51
52		# reduce size by 4 as it ends with four zero bytes that indicate the end of the file
53		size -= 4
54		print ('Size %u' % size)
55
56
57		# don't write .h file as we would need to store its name in btstack_chipset_em9301.c, too
58		# with open(basename + '.h', 'w') as fout:
59		# 	fout.write(header.replace('BASENAME',basename));
60
61		with open(basename + '.c', 'w') as fout:
62			fout.write(code_start.replace('BASENAME',basename).replace('SIZE',str(size)));
63			fout.write('    ')
64			for i in range(0,size):
65				if i % 1000 == 0:
66					print ('- Write %05u/%05u' % (i, size))
67				byte = ord(firm[i])
68				fout.write("0x{0:02x}, ".format(byte))
69				if (i & 0x0f) == 0x0f:
70					fout.write('\n    ')
71			fout.write(code_end);
72			print ('Done\n')
73
74# check usage: 1 param
75if not len(sys.argv) == 2:
76    print(usage)
77    sys.exit(1)
78
79name = sys.argv[1]
80basename = name.replace('.emp','')
81convert_emp(basename)
82