forked from NordicSemiconductor/pc-nrfutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbl_dfu_sett.py
executable file
·294 lines (248 loc) · 11.2 KB
/
bl_dfu_sett.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#
# Copyright (c) 2016 Nordic Semiconductor ASA
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# 3. Neither the name of Nordic Semiconductor ASA nor the names of other
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# 4. This software must only be used in or with a processor manufactured by Nordic
# Semiconductor ASA, or in or with a processor manufactured by a third party that
# is used in combination with a processor manufactured by Nordic Semiconductor.
#
# 5. Any software provided in binary or object form under this license must not be
# reverse engineered, decompiled, modified and/or disassembled.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Python standard library
import os
import time
import shutil
import logging
import tempfile
import struct
import binascii
from enum import Enum
# Nordic libraries
from nordicsemi.dfu import intelhex
from nordicsemi.dfu.intelhex import IntelHexError
from nordicsemi.dfu.nrfhex import *
from nordicsemi.dfu.package import Package
from pc_ble_driver_py.exceptions import NordicSemiException
logger = logging.getLogger(__name__)
class BLDFUSettingsStructV1(object):
def __init__(self):
self.uint32_count = (4 + 2 + (3 * 2) + 2 + (3 + 1 + 4) + 1)
self.offs_crc = 0
self.offs_sett_ver = 1
self.offs_app_ver = 2
self.offs_bl_ver = 3
self.offs_bank_layout = 4
self.offs_bank_current = 5
self.offs_bank0_img_sz = 6
self.offs_bank0_img_crc = 7
self.offs_bank0_bank_code = 8
class BLDFUSettings(object):
""" Class to abstract a bootloader and its settings """
flash_page_51_sz = 0x400
flash_page_52_sz = 0x1000
bl_sett_51_addr = 0x0003FC00
bl_sett_52_addr = 0x0007F000
bl_sett_52_qfab_addr = 0x0003F000
bl_sett_52810_addr = 0x0002F000
bl_sett_52840_addr = 0x000FF000
def __init__(self, ):
"""
"""
# instantiate a hex object
self.ihex = intelhex.IntelHex()
self.temp_dir = None
self.hex_file = ""
def __del__(self):
"""
Destructor removes the temporary directory
:return:
"""
if self.temp_dir is not None:
shutil.rmtree(self.temp_dir)
def set_arch(self, arch):
if arch == 'NRF51':
self.arch = nRFArch.NRF51
self.arch_str = 'nRF51'
self.flash_page_sz = BLDFUSettings.flash_page_51_sz
self.bl_sett_addr = BLDFUSettings.bl_sett_51_addr
elif arch == 'NRF52':
self.arch = nRFArch.NRF52
self.arch_str = 'nRF52'
self.flash_page_sz = BLDFUSettings.flash_page_52_sz
self.bl_sett_addr = BLDFUSettings.bl_sett_52_addr
elif arch == 'NRF52QFAB':
self.arch = nRFArch.NRF52
self.arch_str = 'nRF52QFAB'
self.flash_page_sz = BLDFUSettings.flash_page_52_sz
self.bl_sett_addr = BLDFUSettings.bl_sett_52_qfab_addr
elif arch == 'NRF52810':
self.arch = nRFArch.NRF52
self.arch_str = 'NRF52810'
self.flash_page_sz = BLDFUSettings.flash_page_52_sz
self.bl_sett_addr = BLDFUSettings.bl_sett_52810_addr
elif arch == 'NRF52840':
self.arch = nRFArch.NRF52840
self.arch_str = 'NRF52840'
self.flash_page_sz = BLDFUSettings.flash_page_52_sz
self.bl_sett_addr = BLDFUSettings.bl_sett_52840_addr
else:
raise RuntimeError("Unknown architecture")
def generate(self, arch, app_file, app_ver, bl_ver, bl_sett_ver, custom_bl_sett_addr):
"""
Populates the settings object based on the given parameters.
:param arch: Architecture family string, e.g. NRF51
:param app_file: Path to application file
:param app_ver: Application version number
:param bl_ver: Bootloader version number
:param bl_sett_ver: Bootloader settings version number
:param custom_bl_sett_addr: Custom start address for the settings page
:return:
"""
# Set the architecture
self.set_arch(arch)
if custom_bl_sett_addr is not None:
self.bl_sett_addr = custom_bl_sett_addr
if bl_sett_ver == 1:
self.setts = BLDFUSettingsStructV1()
else:
raise NordicSemiException("Unknown bootloader settings version")
self.bl_sett_ver = bl_sett_ver & 0xffffffff
self.bl_ver = bl_ver & 0xffffffff
if app_ver is not None:
self.app_ver = app_ver & 0xffffffff
else:
self.app_ver = 0x0 & 0xffffffff
if app_file is not None:
# load application to find out size and CRC
self.temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_bl_sett_")
self.app_bin = Package.normalize_firmware_to_bin(self.temp_dir, app_file)
# calculate application size and CRC32
self.app_sz = int(Package.calculate_file_size(self.app_bin)) & 0xffffffff
self.app_crc = int(Package.calculate_crc(32, self.app_bin)) & 0xffffffff
else:
self.app_sz = 0x0 & 0xffffffff
self.app_crc = 0x0 & 0xffffffff
# build the uint32_t array
arr = [0x0] * self.setts.uint32_count
# additional harcoded values
self.bank_layout = 0x0 & 0xffffffff
self.bank_current = 0x0 & 0xffffffff
self.bank0_bank_code = 0x1 & 0xffffffff
# fill in the settings
arr[self.setts.offs_sett_ver] = self.bl_sett_ver
arr[self.setts.offs_app_ver] = self.app_ver
arr[self.setts.offs_bl_ver] = self.bl_ver
arr[self.setts.offs_bank_layout] = self.bank_layout
arr[self.setts.offs_bank_current] = self.bank_current
arr[self.setts.offs_bank0_img_sz] = self.app_sz
arr[self.setts.offs_bank0_img_crc] = self.app_crc
arr[self.setts.offs_bank0_bank_code] = self.bank0_bank_code
# calculate the CRC32 from the filled-in settings
crc_format_str = '<' + ('I' * (self.setts.uint32_count - 1))
crc_arr = arr[1:]
crc_data = struct.pack(crc_format_str, *crc_arr)
self.crc = binascii.crc32(crc_data) & 0xffffffff
# fill in the calculated CRC32
arr[self.setts.offs_crc] = self.crc
format_str = '<' + ('I' * self.setts.uint32_count)
# Get the packed data to insert into the hex instance
data = struct.pack(format_str, *arr)
# insert the data at the correct address
self.ihex.puts(self.bl_sett_addr, data)
def probe_settings(self, base):
# Unpack CRC and version
fmt = '<I'
crc = struct.unpack(fmt, self.ihex.gets(base + 0, 4))[0] & 0xffffffff
ver = struct.unpack(fmt, self.ihex.gets(base + 4, 4))[0] & 0xffffffff
if ver == 1:
self.setts = BLDFUSettingsStructV1()
else:
raise RuntimeError("Unknown Bootloader DFU settings version: {0}".format(ver))
# calculate the CRC32 over the data
crc_data = self.ihex.gets(base + 4, (self.setts.uint32_count - 1) * 4)
_crc = binascii.crc32(crc_data) & 0xffffffff
if _crc != crc:
raise RuntimeError("CRC32 mismtach: flash: {0} calculated: {1}".format(crc, _crc))
self.crc = crc
fmt = '<' + ('I' * (self.setts.uint32_count))
arr = struct.unpack(fmt, self.ihex.gets(base, (self.setts.uint32_count) * 4))
self.bl_sett_ver = arr[self.setts.offs_sett_ver] & 0xffffffff
self.app_ver = arr[self.setts.offs_app_ver] & 0xffffffff
self.bl_ver = arr[self.setts.offs_bl_ver] & 0xffffffff
self.bank_layout = arr[self.setts.offs_bank_layout] & 0xffffffff
self.bank_current = arr[self.setts.offs_bank_current] & 0xffffffff
self.app_sz = arr[self.setts.offs_bank0_img_sz] & 0xffffffff
self.app_crc = arr[self.setts.offs_bank0_img_crc] & 0xffffffff
self.bank0_bank_code = arr[self.setts.offs_bank0_bank_code] & 0xffffffff
def fromhexfile(self, f, arch=None):
self.hex_file = f
self.ihex.fromfile(f, format='hex')
# check the 3 possible addresses for CRC matches
try:
self.probe_settings(BLDFUSettings.bl_sett_51_addr)
self.set_arch('NRF51')
except Exception as e:
try:
self.probe_settings(BLDFUSettings.bl_sett_52_addr)
self.set_arch('NRF52')
except Exception as e:
try:
self.probe_settings(BLDFUSettings.bl_sett_52_qfab_addr)
self.set_arch('NRF52QFAB')
except Exception as e:
try:
self.probe_settings(BLDFUSettings.bl_sett_52810_addr)
self.set_arch('NRF52810')
except Exception as e:
try:
self.probe_settings(BLDFUSettings.bl_sett_52840_addr)
self.set_arch('NRF52840')
except Exception as e:
raise NordicSemiException("Failed to parse .hex file: {0}".format(e))
self.bl_sett_addr = self.ihex.minaddr()
def __str__(self):
s = """
Bootloader DFU Settings:
* File: {0}
* Family: {1}
* Start Address: 0x{2:08X}
* CRC: 0x{3:08X}
* Settings Version: 0x{4:08X} ({4})
* App Version: 0x{5:08X} ({5})
* Bootloader Version: 0x{6:08X} ({6})
* Bank Layout: 0x{7:08X}
* Current Bank: 0x{8:08X}
* Application Size: 0x{9:08X} ({9} bytes)
* Application CRC: 0x{10:08X}
* Bank0 Bank Code: 0x{11:08X}
""".format(self.hex_file, self.arch_str, self.bl_sett_addr, self.crc, self.bl_sett_ver, self.app_ver, self.bl_ver, self.bank_layout, self.bank_current, self.app_sz, self.app_crc, self.bank0_bank_code)
return s
def tohexfile(self, f):
self.hex_file = f
self.ihex.tofile(f, format='hex')