| 16 | parser.add_argument('--addr', default='0', help='set the base address of the binary file, default to 0.') |
| 17 | |
| 18 | class File(object): |
| 19 | def __init__(self, name): |
| 20 | self._name = name |
| 21 | self._data = open(name, 'rb').read() |
| 22 | |
| 23 | @property |
| 24 | def name(self): |
| 25 | return self._name |
| 26 | |
| 27 | @property |
| 28 | def c_name(self): |
| 29 | return '_' + self._name.replace('.', '_') |
| 30 | |
| 31 | @property |
| 32 | def bin_name(self): |
| 33 | # Pad to 4 bytes boundary with \0 |
| 34 | pad_len = 4 |
| 35 | bn = self._name + '\0' * (pad_len - len(self._name) % pad_len) |
| 36 | return bn |
| 37 | |
| 38 | def c_data(self, prefix=''): |
| 39 | '''Get the C code represent of the file content.''' |
| 40 | head = 'static const rt_uint8_t %s[] = {\n' % \ |
| 41 | (prefix + self.c_name) |
| 42 | tail = '\n};' |
| 43 | |
| 44 | if self.entry_size == 0: |
| 45 | return '' |
| 46 | if len(self._data) > 0 and type(self._data[0]) == int: |
| 47 | return head + ','.join(('0x%02x' % i for i in self._data)) + tail |
| 48 | else: |
| 49 | return head + ','.join(('0x%02x' % ord(i) for i in self._data)) + tail |
| 50 | |
| 51 | @property |
| 52 | def entry_size(self): |
| 53 | return len(self._data) |
| 54 | |
| 55 | def bin_data(self, base_addr=0x0): |
| 56 | return bytes(self._data) |
| 57 | |
| 58 | def dump(self, indent=0): |
| 59 | print('%s%s' % (' ' * indent, self._name)) |
| 60 | |
| 61 | class Folder(object): |
| 62 | bin_fmt = struct.Struct('IIII') |
no outgoing calls
no test coverage detected