mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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