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