forked from SAFTehnika/Xamarin-NRF-DFU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecureDFU.cs
302 lines (274 loc) · 13.5 KB
/
SecureDFU.cs
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
295
296
297
298
299
300
301
302
// Copyright (c) 2018 SAF Tehnika. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Plugin.BluetoothLE;
using System.Diagnostics;
using System.Reactive.Linq;
using System.IO;
namespace Plugin.XamarinNordicDFU
{
partial class DFU
{
/// <summary>
/// Run actual filmware upgrade procedure (Secure DFU)
/// </summary>
/// <param name="device">Device which is switched to Secure DFU mode</param>
/// <param name="FirmwarePath"></param>
/// <param name="InitFilePath"></param>
/// <returns></returns>
private async Task RunSecureDFU(IDevice device, Stream FirmwarePacket, Stream InitPacket)
{
IGattCharacteristic controlPoint = null;
IGattCharacteristic packetPoint = null;
//await RefreshGattAsync(device);
device.Connect();
await device.ConnectWait().Timeout(DeviceConnectionTimeout);
// Request MTU only once
await device.RequestMtu(256).Timeout(OperationTimeout);
controlPoint = await device.GetKnownCharacteristics(DfuService, SecureDFUControlPointCharacteristic).Timeout(OperationTimeout);
packetPoint = await device.GetKnownCharacteristics(DfuService, SecureDFUPacketCharacteristic).Timeout(OperationTimeout);
await controlPoint.EnableNotifications(true);
try {
await SendInitPacket(device, InitPacket, controlPoint, packetPoint);
await SendFirmware(device, FirmwarePacket, controlPoint, packetPoint);
}
catch (Exception)
{
await Cleanup(controlPoint, device);
throw;
}
finally
{
await Cleanup(controlPoint, device);
}
}
/// <summary>
/// Close connections, try to reenter standart mode, unsubscribe notifications
/// </summary>
/// <returns></returns>
private async Task Cleanup(IGattCharacteristic controlPoint, IDevice device)
{
await controlPoint.DisableNotifications();
device.CancelConnection();
}
/// <summary>
/// Upload Init package (*.dat)
/// </summary>
/// <param name="device">Device in DFU mode</param>
/// <param name="InitFilePath">Init package path (*.dat)</param>
/// <param name="controlPoint">Secure DFU control point characteristic [Commands / Notifications]</param>
/// <param name="packetPoint">Secure DFU packet point characteristic [Data of images]</param>
/// <returns></returns>
private async Task SendInitPacket(IDevice device, Stream InitPacket, IGattCharacteristic controlPoint, IGattCharacteristic packetPoint)
{
Debug.WriteLineIf(LogLevelDebug, "Start of init packet send");
//FileStream file = new FileStream(InitFilePath, FileMode.Open, FileAccess.Read);
var file = InitPacket;
int imageSize = (int)file.Length;// Around ?~ 130bytes
var MTU = Math.Min(device.MtuSize, DFUMaximumMTU);
CRC32 crc = new CRC32();
ObjectInfo info = await SelectCommand(controlPoint, SecureDFUSelectCommandType.CommmandObject);
bool resumeSendingInitPacket = false;
if (info.offset > 0 && info.offset <= imageSize)
{
// Check if remote sent content is valid
byte[] buffer = new byte[info.offset];
file.Seek(0, SeekOrigin.Begin);
file.Read(buffer, 0, (int)info.offset);
crc.Update(buffer);
if (crc.Value == (uint)info.CRC32)
{
if (info.offset == imageSize)
{
Debug.WriteLineIf(LogLevelDebug, "Init packet already sent and valid");
await ExecuteCommand(controlPoint);
Debug.WriteLineIf(LogLevelDebug, "End of init packet send");
return;
}
else
{
Debug.WriteLineIf(LogLevelDebug, String.Format("-> " + info.offset + " bytes of Init packet were sent before"));
resumeSendingInitPacket = true;
Debug.WriteLineIf(LogLevelDebug, String.Format("Resuming sending Init packet..."));
}
}
else
{
crc.Reset();
info.offset = 0;
}
}
await SetPRN(controlPoint, 0);
for (int attempt = 1; attempt <= MaxRetries;)
{
if (!resumeSendingInitPacket)
{
// Allocate new object
await CreateCommand(controlPoint, SecureDFUCreateCommandType.CommmandObject, imageSize);
}
await TransferData(packetPoint, crc, file, offsetStart: info.offset, MTU:MTU, offsetEnd: imageSize);
ObjectChecksum check = await ReadChecksum(controlPoint);
info.offset = check.offset;
info.CRC32 = check.offset;
Debug.WriteLineIf(LogLevelDebug, String.Format("Checksum received (Offset = {0}, CRC = {1})", check.offset, check.CRC32));
uint localcrc = (uint)crc.Value;
uint remotecrc = (uint)check.CRC32;
if (localcrc == remotecrc)
{
// Everything is OK, we can proceed
break;
}
else
{
if (attempt < MaxRetries)
{
attempt++;
Debug.WriteLineIf(LogLevelDebug, String.Format("CRC does not match! Retrying...(" + attempt + "/" + MaxRetries + ")"));
// Restart
resumeSendingInitPacket = false;
info.offset = 0;
info.CRC32 = 0;
file.Seek(0, SeekOrigin.Begin);
crc.Reset();
}
else
{
Debug.WriteLineIf(LogLevelDebug, String.Format("CRC does not match!"));
device.CancelConnection();
return;
}
}
}
await ExecuteCommand(controlPoint);
Debug.WriteLineIf(LogLevelDebug, "End of init packet send");
}
/// <summary>
/// Upload Firmware image (*.bin)
/// </summary>
/// <param name="device">Device in DFU mode</param>
/// <param name="FirmwareFilePath">Fimware path (.bin)</param>
/// <param name="controlPoint">Secure DFU control point characteristic [Commands / Notifications]</param>
/// <param name="packetPoint">Secure DFU packet point characteristic [Data of images]</param>
/// <returns></returns>
private async Task SendFirmware(IDevice device, Stream FirmwarePacket, IGattCharacteristic controlPoint, IGattCharacteristic packetPoint)
{
//FileStream file = new FileStream(FirmwareFilePath, FileMode.Open, FileAccess.Read);
var file = FirmwarePacket;
long firmwareSize = file.Length;
var MTU = Math.Min(device.MtuSize, DFUMaximumMTU);
const int prn = 0;
ObjectInfo info = await SelectCommand(controlPoint, SecureDFUSelectCommandType.DataObject);
int objectSize = info.maxSize;// Use maximum available object size
Debug.WriteLineIf(LogLevelDebug, String.Format("Data object info received (Max size = {0}, Offset = {1}, CRC = {2})", objectSize, info.offset, info.CRC32));
CRC32 crc = new CRC32();
await SetPRN(controlPoint, prn);
// Try to allocate first object
int startAllocatedSize = (int)(firmwareSize - info.offset);
startAllocatedSize = Math.Min(startAllocatedSize, objectSize);
if (startAllocatedSize > 0)
{
await CreateCommand(controlPoint, SecureDFUCreateCommandType.DataObject, startAllocatedSize);
}
IDisposable dispose = null;
var LastOffsetFailed = 0;
var LastOffsetFailCount = 0;
// Run till all objects are transferred, object sizes must be page aligned
while (true)
{
byte[] lastData;
dispose = controlPoint.WhenNotificationReceived().Subscribe(
result =>
{
lastData = result.Data;
Debug.WriteLineIf(LogLevelDebug, String.Format("Notification {0}", BitConverter.ToString(result.Data)));
if(result.Data.Length == 11)
{
ObjectChecksum checks = new ObjectChecksum();
SetChecksum(checks, result.Data);
info.offset = checks.offset;
info.CRC32 = checks.CRC32;
Debug.WriteLineIf(LogLevelDebug, String.Format("{0} ::: PRN response check: {1}, offset: {2}", DateTime.Now.ToString("HH:mm:ss.ffffff"), checks.CRC32, checks.offset));
}
}
);
int endOffset = GetCurrentObjectEnd(info.offset, objectSize, firmwareSize);
int objectOffset = info.offset;
// Send single current object
for (int startoffset = objectOffset; ;)
{
if (startoffset < objectOffset)
{
if (objectOffset % objectSize == 0 || objectOffset == firmwareSize)
{
break;
}
}
int bytesWritten = await TransferData(packetPoint, crc, file, offsetStart: objectOffset, offsetEnd: endOffset, MTU: MTU);
objectOffset += bytesWritten;
DFUEvents.OnFimwareProgressChanged?.Invoke(objectOffset / (float)firmwareSize, DateTime.Now - DFUStartTime);
Debug.WriteLineIf(LogLevelDebug, String.Format("{0} ::: Written bytes {1}, progress: {2}, elapsed: {3}", DateTime.Now.ToString("HH:mm:ss.ffffff"), bytesWritten, objectOffset / (float)firmwareSize, DateTime.Now - DFUStartTime));
}
// if PRN with correct offset not received, force to calculate CRC and offset
if (info.offset != objectOffset) {
Debug.WriteLineIf(LogLevelDebug, String.Format("{0} ::: Force chekcsum calc", DateTime.Now.ToString("HH:mm:ss.ffffff")));
ObjectChecksum check = await ReadChecksum(controlPoint);
info.CRC32 = check.CRC32;
info.offset = check.offset;
}
dispose?.Dispose();
uint localcrc = (uint)crc.Value;
uint remotecrc = (uint)info.CRC32;
if (localcrc == remotecrc)
{
await ExecuteCommand(controlPoint, skipRegistring:true);
if (firmwareSize == info.offset)
{
// Firmware upload finished
break;
}
// Allocate next object
int allocateSize = objectSize;
if (info.offset + objectSize > firmwareSize)
{
allocateSize = (int)(firmwareSize - info.offset);
}
await CreateCommand(controlPoint, SecureDFUCreateCommandType.DataObject, allocateSize);
}
else
{
await Task.Delay(1000);
// Get current object start offset
int allocateSize = objectSize;
int currentObject = info.offset / objectSize;
int currentStartOffset = currentObject * objectSize;
if (currentStartOffset + objectSize > firmwareSize)
{
allocateSize = (int)(firmwareSize - currentStartOffset);
}
info.offset = currentStartOffset;
// Calculate crc of already sent data
file.Seek(0, SeekOrigin.Begin);
byte[] crcBuffer = new byte[currentStartOffset];
crc.Reset();
if (currentStartOffset > 0)
{
file.Read(crcBuffer,0, crcBuffer.Length);
crc.Update(crcBuffer);
}
if(LastOffsetFailed != currentStartOffset)
{
LastOffsetFailed = currentStartOffset;
LastOffsetFailCount = 0;
}
LastOffsetFailCount++;
if(LastOffsetFailCount == MaxRetries)
{
throw new Exception("Too much retries for one object");
}
// Allocate memory from current object start position
await CreateCommand(controlPoint, SecureDFUCreateCommandType.DataObject, allocateSize);
}
}
}
}
}