Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:02dd72d1d465 1 """
borlanic 0:02dd72d1d465 2 mbed SDK
borlanic 0:02dd72d1d465 3 Copyright (c) 2011-2013 ARM Limited
borlanic 0:02dd72d1d465 4
borlanic 0:02dd72d1d465 5 Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:02dd72d1d465 6 you may not use this file except in compliance with the License.
borlanic 0:02dd72d1d465 7 You may obtain a copy of the License at
borlanic 0:02dd72d1d465 8
borlanic 0:02dd72d1d465 9 http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:02dd72d1d465 10
borlanic 0:02dd72d1d465 11 Unless required by applicable law or agreed to in writing, software
borlanic 0:02dd72d1d465 12 distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:02dd72d1d465 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:02dd72d1d465 14 See the License for the specific language governing permissions and
borlanic 0:02dd72d1d465 15 limitations under the License.
borlanic 0:02dd72d1d465 16
borlanic 0:02dd72d1d465 17
borlanic 0:02dd72d1d465 18 http://www.nxp.com/documents/user_manual/UM10360.pdf
borlanic 0:02dd72d1d465 19
borlanic 0:02dd72d1d465 20 32.3.1.1 Criterion for Valid User Code
borlanic 0:02dd72d1d465 21 The reserved Cortex-M3 exception vector location 7 (offset 0x1C in the vector table)
borlanic 0:02dd72d1d465 22 should contain the 2's complement of the check-sum of table entries 0 through 6. This
borlanic 0:02dd72d1d465 23 causes the checksum of the first 8 table entries to be 0. The boot loader code checksums
borlanic 0:02dd72d1d465 24 the first 8 locations in sector 0 of the flash. If the result is 0, then execution control is
borlanic 0:02dd72d1d465 25 transferred to the user code.
borlanic 0:02dd72d1d465 26 """
borlanic 0:02dd72d1d465 27 from struct import unpack, pack
borlanic 0:02dd72d1d465 28
borlanic 0:02dd72d1d465 29
borlanic 0:02dd72d1d465 30 def patch(bin_path):
borlanic 0:02dd72d1d465 31 with open(bin_path, 'r+b') as bin:
borlanic 0:02dd72d1d465 32 # Read entries 0 through 6 (Little Endian 32bits words)
borlanic 0:02dd72d1d465 33 vector = [unpack('<I', bin.read(4))[0] for _ in range(7)]
borlanic 0:02dd72d1d465 34
borlanic 0:02dd72d1d465 35 # location 7 (offset 0x1C in the vector table) should contain the 2's
borlanic 0:02dd72d1d465 36 # complement of the check-sum of table entries 0 through 6
borlanic 0:02dd72d1d465 37 bin.seek(0x1C)
borlanic 0:02dd72d1d465 38 bin.write(pack('<I', (~sum(vector) + 1) & 0xFFFFFFFF))
borlanic 0:02dd72d1d465 39
borlanic 0:02dd72d1d465 40
borlanic 0:02dd72d1d465 41 def is_patched(bin_path):
borlanic 0:02dd72d1d465 42 with open(bin_path, 'rb') as bin:
borlanic 0:02dd72d1d465 43 # The checksum of the first 8 table entries should be 0
borlanic 0:02dd72d1d465 44 return (sum([unpack('<I', bin.read(4))[0] for _ in range(8)]) & 0xFFFFFFFF) == 0
borlanic 0:02dd72d1d465 45
borlanic 0:02dd72d1d465 46
borlanic 0:02dd72d1d465 47 if __name__ == '__main__':
borlanic 0:02dd72d1d465 48 bin_path = "C:/Users/emimon01/releases/emilmont/build/test/LPC1768/ARM/MBED_A1/basic.bin"
borlanic 0:02dd72d1d465 49 patch(bin_path)
borlanic 0:02dd72d1d465 50 assert is_patched(bin_path), "The file is not patched"