You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

patch.py 1.9KB

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