BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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