nRF51_OTA_strip.py: python script that strips an nRF51 image

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nRF51_OTA_strip.py Source File

nRF51_OTA_strip.py

00001 #/usr/bin/env python3
00002 
00003 # Remove the SoftDevice part of an nRF51 application, to make it
00004 # transferable over the air
00005 
00006 import intelhex
00007 
00008 from os.path import exists
00009 from sys import argv, exit
00010 
00011 
00012 if not argv[2:]:
00013     print("usage: %s <input hex file> <output name>" % argv[0])
00014     exit(1)
00015 
00016 in_file, out_file = argv[1:3]
00017 
00018 # Set start address if autodetection doesn't work
00019 start_address = None
00020 
00021 if exists(out_file):
00022     confirm = input("File %s exists. Replace (y/N)? " % out_file)
00023     if not confirm.lower().startswith("y"):
00024         print("Nothing to do.")
00025         exit(0)
00026 
00027 
00028 hex_in = intelhex.IntelHex()
00029 hex_in.fromfile(in_file, format='hex')
00030 
00031 # Try to guess where application starts: first word contains the
00032 # application's stack base
00033 
00034 for app_start in (
00035         0x16000, # Version 7.1.0 of S110 SoftDevice
00036         0x18000, #         8.0.0
00037         0x1c000, #         1.0      S130
00038         ):
00039 
00040     # Read first word (little-endian)
00041     try:
00042         w = hex_in.tobinarray(start=app_start, size=4)
00043     except intelhex.NotEnoughDataError:
00044         continue
00045     word = w[3] << 24 | w[2] << 16 | w[1] << 8 | w[0]
00046 
00047     # Assume stack pointer is at the end of RAM
00048     if word in (
00049             0x20004000, # 16K
00050             0x20008000, # 32K
00051             ):
00052         start_address = app_start
00053         print("Found application at %x" % start_address)
00054         break
00055 
00056 if not start_address:
00057     print("Application start address not found")
00058     exit(2)
00059 
00060 
00061 hex_out = hex_in[start_address:]
00062 hex_out.tofile(out_file, format="hex")