Maxim mbed development library
tools/add_fib.py@0:0e018d759a2a, 2016-11-08 (annotated)
- Committer:
- switches
- Date:
- Tue Nov 08 18:27:11 2016 +0000
- Revision:
- 0:0e018d759a2a
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:0e018d759a2a | 1 | """ |
switches | 0:0e018d759a2a | 2 | @copyright (c) 2012 ON Semiconductor. All rights reserved. |
switches | 0:0e018d759a2a | 3 | ON Semiconductor is supplying this software for use with ON Semiconductor |
switches | 0:0e018d759a2a | 4 | processor based microcontrollers only. |
switches | 0:0e018d759a2a | 5 | THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED |
switches | 0:0e018d759a2a | 6 | OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF |
switches | 0:0e018d759a2a | 7 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. |
switches | 0:0e018d759a2a | 8 | ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, |
switches | 0:0e018d759a2a | 9 | INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
switches | 0:0e018d759a2a | 10 | """ |
switches | 0:0e018d759a2a | 11 | |
switches | 0:0e018d759a2a | 12 | from __future__ import absolute_import |
switches | 0:0e018d759a2a | 13 | from __future__ import print_function |
switches | 0:0e018d759a2a | 14 | |
switches | 0:0e018d759a2a | 15 | import itertools |
switches | 0:0e018d759a2a | 16 | import binascii |
switches | 0:0e018d759a2a | 17 | import intelhex |
switches | 0:0e018d759a2a | 18 | |
switches | 0:0e018d759a2a | 19 | FIB_BASE = 0x2000 |
switches | 0:0e018d759a2a | 20 | FLASH_BASE = 0x3000 |
switches | 0:0e018d759a2a | 21 | FW_REV = 0x01000100 |
switches | 0:0e018d759a2a | 22 | def ranges(i): |
switches | 0:0e018d759a2a | 23 | for _, b in itertools.groupby(enumerate(i), lambda x_y: x_y[1] - x_y[0]): |
switches | 0:0e018d759a2a | 24 | b = list(b) |
switches | 0:0e018d759a2a | 25 | yield b[0][1], b[-1][1] |
switches | 0:0e018d759a2a | 26 | |
switches | 0:0e018d759a2a | 27 | |
switches | 0:0e018d759a2a | 28 | def add_fib_at_start(arginput): |
switches | 0:0e018d759a2a | 29 | input_file = arginput + ".bin" |
switches | 0:0e018d759a2a | 30 | file_name_hex = arginput + "_fib.hex" |
switches | 0:0e018d759a2a | 31 | file_name_bin = arginput + ".bin" |
switches | 0:0e018d759a2a | 32 | |
switches | 0:0e018d759a2a | 33 | # Read in hex file |
switches | 0:0e018d759a2a | 34 | input_hex_file = intelhex.IntelHex() |
switches | 0:0e018d759a2a | 35 | input_hex_file.padding = 0x00 |
switches | 0:0e018d759a2a | 36 | input_hex_file.loadbin(input_file, offset=FLASH_BASE) |
switches | 0:0e018d759a2a | 37 | |
switches | 0:0e018d759a2a | 38 | output_hex_file = intelhex.IntelHex() |
switches | 0:0e018d759a2a | 39 | output_hex_file.padding = 0x00 |
switches | 0:0e018d759a2a | 40 | |
switches | 0:0e018d759a2a | 41 | # Get the starting and ending address |
switches | 0:0e018d759a2a | 42 | addresses = input_hex_file.addresses() |
switches | 0:0e018d759a2a | 43 | addresses.sort() |
switches | 0:0e018d759a2a | 44 | start_end_pairs = list(ranges(addresses)) |
switches | 0:0e018d759a2a | 45 | regions = len(start_end_pairs) |
switches | 0:0e018d759a2a | 46 | |
switches | 0:0e018d759a2a | 47 | if regions == 1: |
switches | 0:0e018d759a2a | 48 | start, end = start_end_pairs[0] |
switches | 0:0e018d759a2a | 49 | else: |
switches | 0:0e018d759a2a | 50 | start = min(min(start_end_pairs)) |
switches | 0:0e018d759a2a | 51 | end = max(max(start_end_pairs)) |
switches | 0:0e018d759a2a | 52 | |
switches | 0:0e018d759a2a | 53 | assert start >= FLASH_BASE, ("Error - start 0x%x less than begining of user\ |
switches | 0:0e018d759a2a | 54 | flash area" %start) |
switches | 0:0e018d759a2a | 55 | # Compute checksum over the range (don't include data at location of crc) |
switches | 0:0e018d759a2a | 56 | size = end - start + 1 |
switches | 0:0e018d759a2a | 57 | data = input_hex_file.tobinarray(start=start, size=size) |
switches | 0:0e018d759a2a | 58 | crc32 = binascii.crc32(data) & 0xFFFFFFFF |
switches | 0:0e018d759a2a | 59 | |
switches | 0:0e018d759a2a | 60 | fw_rev = FW_REV |
switches | 0:0e018d759a2a | 61 | |
switches | 0:0e018d759a2a | 62 | checksum = (start + size + crc32 + fw_rev) & 0xFFFFFFFF |
switches | 0:0e018d759a2a | 63 | |
switches | 0:0e018d759a2a | 64 | print("Writing FIB: base 0x%08X, size 0x%08X, crc32 0x%08X, fw rev 0x%08X,\ |
switches | 0:0e018d759a2a | 65 | checksum 0x%08X" % (start, size, crc32, fw_rev, checksum)) |
switches | 0:0e018d759a2a | 66 | |
switches | 0:0e018d759a2a | 67 | #expected initial values used by daplink to validate that it is a valid bin |
switches | 0:0e018d759a2a | 68 | #file added as dummy values in this file because the fib area preceeds the |
switches | 0:0e018d759a2a | 69 | #application area the bootloader will ignore these dummy values |
switches | 0:0e018d759a2a | 70 | # 00 is stack pointer (RAM address) |
switches | 0:0e018d759a2a | 71 | # 04 is Reset vector (FLASH address) |
switches | 0:0e018d759a2a | 72 | # 08 NMI_Handler (FLASH address) |
switches | 0:0e018d759a2a | 73 | # 0C HardFault_Handler(FLASH address) |
switches | 0:0e018d759a2a | 74 | # 10 dummy |
switches | 0:0e018d759a2a | 75 | dummy_sp = 0x3FFFFC00 |
switches | 0:0e018d759a2a | 76 | dummy_reset_vector = 0x00003625 |
switches | 0:0e018d759a2a | 77 | dummy_nmi_handler = 0x00003761 |
switches | 0:0e018d759a2a | 78 | dummy_hardfault_handler = 0x00003691 |
switches | 0:0e018d759a2a | 79 | dummy_blank = 0x00000000 |
switches | 0:0e018d759a2a | 80 | |
switches | 0:0e018d759a2a | 81 | #expected fib structure |
switches | 0:0e018d759a2a | 82 | #typedef struct fib{ |
switches | 0:0e018d759a2a | 83 | #uint32_t base; /**< Base offset of firmware, indicating what flash the |
switches | 0:0e018d759a2a | 84 | # firmware is in. (will never be 0x11111111) */ |
switches | 0:0e018d759a2a | 85 | #uint32_t size; /**< Size of the firmware */ |
switches | 0:0e018d759a2a | 86 | #uint32_t crc; /**< CRC32 for firmware correctness check */ |
switches | 0:0e018d759a2a | 87 | #uint32_t rev; /**< Revision number */ |
switches | 0:0e018d759a2a | 88 | #uint32_t checksum; /**< Check-sum of information block */ |
switches | 0:0e018d759a2a | 89 | #}fib_t, *fib_pt; |
switches | 0:0e018d759a2a | 90 | |
switches | 0:0e018d759a2a | 91 | fib_start = FIB_BASE |
switches | 0:0e018d759a2a | 92 | dummy_fib_size = 20 |
switches | 0:0e018d759a2a | 93 | fib_size = 20 |
switches | 0:0e018d759a2a | 94 | user_code_start = FLASH_BASE |
switches | 0:0e018d759a2a | 95 | |
switches | 0:0e018d759a2a | 96 | # Write FIB to the file in little endian |
switches | 0:0e018d759a2a | 97 | output_hex_file[fib_start + 0] = (dummy_sp >> 0) & 0xFF |
switches | 0:0e018d759a2a | 98 | output_hex_file[fib_start + 1] = (dummy_sp >> 8) & 0xFF |
switches | 0:0e018d759a2a | 99 | output_hex_file[fib_start + 2] = (dummy_sp >> 16) & 0xFF |
switches | 0:0e018d759a2a | 100 | output_hex_file[fib_start + 3] = (dummy_sp >> 24) & 0xFF |
switches | 0:0e018d759a2a | 101 | |
switches | 0:0e018d759a2a | 102 | output_hex_file[fib_start + 4] = (dummy_reset_vector >> 0) & 0xFF |
switches | 0:0e018d759a2a | 103 | output_hex_file[fib_start + 5] = (dummy_reset_vector >> 8) & 0xFF |
switches | 0:0e018d759a2a | 104 | output_hex_file[fib_start + 6] = (dummy_reset_vector >> 16) & 0xFF |
switches | 0:0e018d759a2a | 105 | output_hex_file[fib_start + 7] = (dummy_reset_vector >> 24) & 0xFF |
switches | 0:0e018d759a2a | 106 | |
switches | 0:0e018d759a2a | 107 | output_hex_file[fib_start + 8] = (dummy_nmi_handler >> 0) & 0xFF |
switches | 0:0e018d759a2a | 108 | output_hex_file[fib_start + 9] = (dummy_nmi_handler >> 8) & 0xFF |
switches | 0:0e018d759a2a | 109 | output_hex_file[fib_start + 10] = (dummy_nmi_handler >> 16) & 0xFF |
switches | 0:0e018d759a2a | 110 | output_hex_file[fib_start + 11] = (dummy_nmi_handler >> 24) & 0xFF |
switches | 0:0e018d759a2a | 111 | |
switches | 0:0e018d759a2a | 112 | output_hex_file[fib_start + 12] = (dummy_hardfault_handler >> 0) & 0xFF |
switches | 0:0e018d759a2a | 113 | output_hex_file[fib_start + 13] = (dummy_hardfault_handler >> 8) & 0xFF |
switches | 0:0e018d759a2a | 114 | output_hex_file[fib_start + 14] = (dummy_hardfault_handler >> 16) & 0xFF |
switches | 0:0e018d759a2a | 115 | output_hex_file[fib_start + 15] = (dummy_hardfault_handler >> 24) & 0xFF |
switches | 0:0e018d759a2a | 116 | |
switches | 0:0e018d759a2a | 117 | output_hex_file[fib_start + 16] = (dummy_blank >> 0) & 0xFF |
switches | 0:0e018d759a2a | 118 | output_hex_file[fib_start + 17] = (dummy_blank >> 8) & 0xFF |
switches | 0:0e018d759a2a | 119 | output_hex_file[fib_start + 18] = (dummy_blank >> 16) & 0xFF |
switches | 0:0e018d759a2a | 120 | output_hex_file[fib_start + 19] = (dummy_blank >> 24) & 0xFF |
switches | 0:0e018d759a2a | 121 | |
switches | 0:0e018d759a2a | 122 | # Write FIB to the file in little endian |
switches | 0:0e018d759a2a | 123 | output_hex_file[fib_start + 20] = (start >> 0) & 0xFF |
switches | 0:0e018d759a2a | 124 | output_hex_file[fib_start + 21] = (start >> 8) & 0xFF |
switches | 0:0e018d759a2a | 125 | output_hex_file[fib_start + 22] = (start >> 16) & 0xFF |
switches | 0:0e018d759a2a | 126 | output_hex_file[fib_start + 23] = (start >> 24) & 0xFF |
switches | 0:0e018d759a2a | 127 | |
switches | 0:0e018d759a2a | 128 | output_hex_file[fib_start + 24] = (size >> 0) & 0xFF |
switches | 0:0e018d759a2a | 129 | output_hex_file[fib_start + 25] = (size >> 8) & 0xFF |
switches | 0:0e018d759a2a | 130 | output_hex_file[fib_start + 26] = (size >> 16) & 0xFF |
switches | 0:0e018d759a2a | 131 | output_hex_file[fib_start + 27] = (size >> 24) & 0xFF |
switches | 0:0e018d759a2a | 132 | |
switches | 0:0e018d759a2a | 133 | output_hex_file[fib_start + 28] = (crc32 >> 0) & 0xFF |
switches | 0:0e018d759a2a | 134 | output_hex_file[fib_start + 29] = (crc32 >> 8) & 0xFF |
switches | 0:0e018d759a2a | 135 | output_hex_file[fib_start + 30] = (crc32 >> 16) & 0xFF |
switches | 0:0e018d759a2a | 136 | output_hex_file[fib_start + 31] = (crc32 >> 24) & 0xFF |
switches | 0:0e018d759a2a | 137 | |
switches | 0:0e018d759a2a | 138 | output_hex_file[fib_start + 32] = (fw_rev >> 0) & 0xFF |
switches | 0:0e018d759a2a | 139 | output_hex_file[fib_start + 33] = (fw_rev >> 8) & 0xFF |
switches | 0:0e018d759a2a | 140 | output_hex_file[fib_start + 34] = (fw_rev >> 16) & 0xFF |
switches | 0:0e018d759a2a | 141 | output_hex_file[fib_start + 35] = (fw_rev >> 24) & 0xFF |
switches | 0:0e018d759a2a | 142 | |
switches | 0:0e018d759a2a | 143 | output_hex_file[fib_start + 36] = (checksum >> 0) & 0xFF |
switches | 0:0e018d759a2a | 144 | output_hex_file[fib_start + 37] = (checksum >> 8) & 0xFF |
switches | 0:0e018d759a2a | 145 | output_hex_file[fib_start + 38] = (checksum >> 16) & 0xFF |
switches | 0:0e018d759a2a | 146 | output_hex_file[fib_start + 39] = (checksum >> 24) & 0xFF |
switches | 0:0e018d759a2a | 147 | |
switches | 0:0e018d759a2a | 148 | #pad the rest of the file |
switches | 0:0e018d759a2a | 149 | for i in range(fib_start + dummy_fib_size + fib_size, user_code_start): |
switches | 0:0e018d759a2a | 150 | output_hex_file[i] = 0xFF |
switches | 0:0e018d759a2a | 151 | |
switches | 0:0e018d759a2a | 152 | #merge two hex files |
switches | 0:0e018d759a2a | 153 | output_hex_file.merge(input_hex_file, overlap='error') |
switches | 0:0e018d759a2a | 154 | |
switches | 0:0e018d759a2a | 155 | # Write out file(s) |
switches | 0:0e018d759a2a | 156 | output_hex_file.tofile(file_name_hex, 'hex') |
switches | 0:0e018d759a2a | 157 | output_hex_file.tofile(file_name_bin, 'bin') |
switches | 0:0e018d759a2a | 158 |