mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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