|
| 1 | +from uctypes import struct, addressof, LITTLE_ENDIAN, UINT16, UINT32 |
| 2 | + |
| 3 | + |
| 4 | +def make_binary(text, data, bss_size): |
| 5 | + if not isinstance(text, bytes): |
| 6 | + raise TypeError('text section must be binary bytes') |
| 7 | + if not isinstance(data, bytes): |
| 8 | + raise TypeError('data section must be binary bytes') |
| 9 | + binary_header_struct_def = dict( |
| 10 | + magic = 0 | UINT32, |
| 11 | + text_offset = 4 | UINT16, |
| 12 | + text_size = 6 | UINT16, |
| 13 | + data_size = 8 | UINT16, |
| 14 | + bss_size = 10 | UINT16, |
| 15 | + ) |
| 16 | + header = bytearray(12) |
| 17 | + h = struct(addressof(header), binary_header_struct_def, LITTLE_ENDIAN) |
| 18 | + # https://github.com/espressif/esp-idf/blob/master/components/ulp/ld/esp32.ulp.ld |
| 19 | + # ULP program binary should have the following format (all values little-endian): |
| 20 | + h.magic = 0x00706c75 # (4 bytes) |
| 21 | + h.text_offset = 12 # offset of .text section from binary start (2 bytes) |
| 22 | + h.text_size = len(text) # size of .text section (2 bytes) |
| 23 | + h.data_size = len(data) # size of .data section (2 bytes) |
| 24 | + h.bss_size = bss_size # size of .bss section (2 bytes) |
| 25 | + return bytes(header) + text + data |
| 26 | + |
0 commit comments