Clone of official tools

Committer:
Anders Blomdell
Date:
Thu Feb 04 17:17:13 2021 +0100
Revision:
47:21ae3e5a7128
Parent:
36:96847d42f010
Add a few normpath calls

Who changed what in which revision?

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