init

Dependencies:   mbed

Committer:
Nathan Yonkee
Date:
Fri Mar 02 07:16:49 2018 -0700
Revision:
10:46a4cf51ee38
Parent:
9:d58e77ebd769
remove mbed-os

Who changed what in which revision?

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