-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcbootflash-cpp.cpp
More file actions
416 lines (367 loc) · 13.2 KB
/
mcbootflash-cpp.cpp
File metadata and controls
416 lines (367 loc) · 13.2 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <string.h> // memcpy
#include <iostream>
#include <sstream>
#include <iomanip>
#include <array>
#include <vector>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
template <std::size_t N>
std::string arrayToHexString(const std::array<uint8_t, N> &arr)
{
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (size_t i = 0; i < N; ++i)
{
ss << std::setw(2) << static_cast<unsigned>(arr[i]);
if (i < N - 1)
{
ss << " ";
}
}
return ss.str();
}
/// @brief Sent by the bootloader in response to a command.
enum ResponseCode
{
SUCCESS = 0x01,
UNSUPPORTED_COMMAND = 0xFF,
BAD_ADDRESS = 0xFE,
BAD_LENGTH = 0xFD,
VERIFY_FAIL = 0xFC
};
/// @brief The MCC 16-bit bootloader supports these commands.
enum CommandCode
{
READ_VERSION = 0x00,
READ_FLASH = 0x01,
WRITE_FLASH = 0x02,
ERASE_FLASH = 0x03,
CALC_CHECKSUM = 0x08,
RESET_DEVICE = 0x09,
SELF_VERIFY = 0x0A,
GET_MEMORY_ADDRESS_RANGE = 0x0B,
};
class Packet
{
private:
uint8_t command;
uint16_t data_length;
uint32_t unlock_sequence;
uint32_t address;
public:
Packet(uint8_t command, uint16_t data_length = 0, uint32_t unlock_sequence = 0, uint32_t address = 0)
: command(command), data_length(data_length), unlock_sequence(unlock_sequence), address(address) {}
std::array<uint8_t, 11> toBytes() const
{
std::array<uint8_t, 11> buffer;
uint8_t *bufPtr = buffer.data();
memcpy(bufPtr, &command, sizeof(command));
bufPtr += sizeof(command);
memcpy(bufPtr, &data_length, sizeof(data_length));
bufPtr += sizeof(data_length);
memcpy(bufPtr, &unlock_sequence, sizeof(unlock_sequence));
bufPtr += sizeof(unlock_sequence);
memcpy(bufPtr, &address, sizeof(address));
return buffer;
}
// Déserialisation
void fromBytes(const std::array<uint8_t, 11> &buffer)
{
uint8_t command;
uint16_t data_length;
uint32_t unlock_sequence;
uint32_t address;
const uint8_t *bufPtr = buffer.data();
memcpy(&command, bufPtr, sizeof(command));
bufPtr += sizeof(command);
memcpy(&data_length, bufPtr, sizeof(data_length));
bufPtr += sizeof(data_length);
memcpy(&unlock_sequence, bufPtr, sizeof(unlock_sequence));
bufPtr += sizeof(unlock_sequence);
memcpy(&address, bufPtr, sizeof(address));
this->command = command;
this->data_length = data_length;
this->unlock_sequence = unlock_sequence;
this->address = address;
}
static size_t getSize()
{
return sizeof(uint8_t) + sizeof(uint16_t) + 2 * sizeof(uint32_t);
}
};
TEST_CASE("Packet class creation, fromBytes, toBytes")
{
Packet p(CommandCode::GET_MEMORY_ADDRESS_RANGE);
// std::cout << arrayToHexString(p.toBytes()) << std::endl;
CHECK(arrayToHexString(p.toBytes()) == "0b 00 00 00 00 00 00 00 00 00 00");
}
typedef Packet ResponseBase;
typedef Packet Command;
class Version : public ResponseBase
{
private:
uint16_t version;
uint16_t max_packet_length;
uint16_t device_id;
uint16_t erase_size;
uint16_t write_size;
public:
Version(
uint8_t command,
uint16_t data_length = 0,
uint32_t unlock_sequence = 0,
uint32_t address = 0,
uint16_t version = 0,
uint16_t max_packet_length = 0,
uint16_t device_id = 0,
uint16_t erase_size = 0,
uint16_t write_size = 0)
: ResponseBase(command, data_length, unlock_sequence, address),
version(version),
max_packet_length(max_packet_length),
device_id(device_id),
erase_size(erase_size),
write_size(write_size)
{
}
std::array<uint8_t, 37> toBytes() const
{
std::array<uint8_t, 11> baseBuffer = ResponseBase::toBytes();
// la liste d'initialisation vide {} met tous les octets à 0 pour la répétabilité lors des tests
std::array<uint8_t, 37> buffer{};
memcpy(buffer.data(), baseBuffer.data(), baseBuffer.size());
// On copie chaque champ dans le buffer, en ignorant les champs non utilisés
memcpy(buffer.data() + baseBuffer.size(), &version, sizeof(version));
memcpy(buffer.data() + baseBuffer.size() + 2, &max_packet_length, sizeof(max_packet_length));
memcpy(buffer.data() + baseBuffer.size() + 6, &device_id, sizeof(device_id));
memcpy(buffer.data() + baseBuffer.size() + 10, &erase_size, sizeof(erase_size));
memcpy(buffer.data() + baseBuffer.size() + 12, &write_size, sizeof(write_size));
return buffer;
}
void fromBytes(const std::array<uint8_t, 37> &buffer)
{
std::array<uint8_t, 11> baseBuffer;
memcpy(baseBuffer.data(), buffer.data(), baseBuffer.size());
ResponseBase::fromBytes(baseBuffer);
memcpy(&version, buffer.data() + baseBuffer.size(), sizeof(version));
memcpy(&max_packet_length, buffer.data() + baseBuffer.size() + 2, sizeof(max_packet_length));
memcpy(&device_id, buffer.data() + baseBuffer.size() + 6, sizeof(device_id));
memcpy(&erase_size, buffer.data() + baseBuffer.size() + 10, sizeof(erase_size));
memcpy(&write_size, buffer.data() + baseBuffer.size() + 12, sizeof(write_size));
}
static size_t getSize()
{
return ResponseBase::getSize() + 26; // il y a des octets ignorés
}
};
TEST_CASE("Version class getSize ?")
{
Version v(ResponseCode::SUCCESS);
CHECK(v.getSize() == 37);
}
TEST_CASE("Version class SUCCESS simple")
{
Version v((uint8_t)ResponseCode::SUCCESS);
CHECK(arrayToHexString(v.toBytes()) == "01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00");
}
TEST_CASE("Version class SUCCESS avec valeurs différentes")
{
Version v((uint8_t)ResponseCode::SUCCESS,
0, 0, 0, 42, 43, 45, 48, 34);
CHECK(arrayToHexString(v.toBytes()) == "01 00 00 00 00 00 00 00 00 00 00 2a 00 2b 00 00 00 2d 00 00 00 30 00 22 00 00 00 00 00 00 00 00 00 00 00 00 00");
}
class Response : public Packet
{
private:
ResponseCode success;
public:
Response(uint8_t command, uint16_t data_length = 0, uint32_t unlock_sequence = 0, uint32_t address = 0, ResponseCode success = ResponseCode::UNSUPPORTED_COMMAND)
: Packet(command, data_length, unlock_sequence, address), success(success) {}
std::array<uint8_t, 12> toBytes() const
{
std::array<uint8_t, 11> baseBuffer = Packet::toBytes();
std::array<uint8_t, 12> buffer;
std::copy(baseBuffer.begin(), baseBuffer.end(), buffer.begin());
buffer[11] = static_cast<uint8_t>(success);
return buffer;
}
void fromBytes(const std::array<uint8_t, 12> &buffer)
{
std::array<uint8_t, 11> baseBuffer;
std::copy(buffer.begin(), buffer.begin() + 11, baseBuffer.begin());
Packet::fromBytes(baseBuffer);
success = static_cast<ResponseCode>(buffer[11]);
}
static size_t getSize()
{
return Packet::getSize() + sizeof(ResponseCode);
}
};
TEST_CASE("Response class BAD_LENGTH")
{
Response r((uint8_t)ResponseCode::BAD_LENGTH);
CHECK(arrayToHexString(r.toBytes()) == "fd 00 00 00 00 00 00 00 00 00 00 ff");
}
TEST_CASE("Response class BAD_ADDRESS")
{
Response r((uint8_t)ResponseCode::BAD_ADDRESS);
CHECK(arrayToHexString(r.toBytes()) == "fe 00 00 00 00 00 00 00 00 00 00 ff");
}
/// @brief Response to `GET_MEMORY_RANGE` command.
/*
Layout::
| [Response] | uint32 | uint32 |
| [Response] | program_start | program_end |
Parameters
----------
program_start : uint32
Low end of address space to which application firmware can be flashed.
program_end : uint32
High end of address space to which application firmware can be flashed.
*/
class MemoryRange : public Response
{
private:
uint32_t program_start;
uint32_t program_end;
public:
MemoryRange(
uint8_t command,
uint32_t program_start,
uint32_t program_end,
uint16_t data_length = 0,
uint32_t unlock_sequence = 0,
uint32_t address = 0,
ResponseCode success = ResponseCode::UNSUPPORTED_COMMAND)
: Response(command, data_length, unlock_sequence, address, success), program_start(program_start), program_end(program_end)
{
}
uint32_t getProgramStart() const { return program_start; }
uint32_t getProgramEnd() const { return program_end; }
std::array<uint8_t, 20> toBytes() const
{
std::array<uint8_t, 12> baseBuffer = Response::toBytes();
std::array<uint8_t, 20> buffer;
// destination : buffer. source : baseBuffer, nombre à copier : size
memcpy(buffer.data(), baseBuffer.data(), baseBuffer.size());
// Copier program_start dans buffer
memcpy(buffer.data() + baseBuffer.size(), &program_start, sizeof(program_start));
// Copier program_end dans buffer
memcpy(buffer.data() + baseBuffer.size() + sizeof(program_start), &program_end, sizeof(program_end));
return buffer;
}
void fromBytes(const std::array<uint8_t, 20> &buffer)
{
std::array<uint8_t, 12> baseBuffer;
// Copier les premiers 12 octets de buffer dans baseBuffer
memcpy(baseBuffer.data(), buffer.data(), baseBuffer.size());
Response::fromBytes(baseBuffer);
// Copier program_start depuis buffer
memcpy(&program_start, buffer.data() + baseBuffer.size(), sizeof(program_start));
// Copier program_end depuis buffer
memcpy(&program_end, buffer.data() + baseBuffer.size() + sizeof(program_start), sizeof(program_end));
}
static size_t getSize()
{
return Response::getSize() + sizeof(program_start) + sizeof(program_end);
}
void print() const
{
std::cout << "Program Start: " << program_start << std::endl;
std::cout << "Program End: " << program_end << std::endl;
}
};
TEST_CASE("MemoryRange class")
{
MemoryRange r((uint8_t)ResponseCode::BAD_LENGTH, 0, 0);
CHECK(arrayToHexString(r.toBytes()) == "fd 00 00 00 00 00 00 00 00 00 00 ff 00 00 00 00 00 00 00 00");
}
TEST_CASE("MemoryRange class 2")
{
MemoryRange r((uint8_t)ResponseCode::BAD_ADDRESS, 0, 0);
CHECK(arrayToHexString(r.toBytes()) == "fe 00 00 00 00 00 00 00 00 00 00 ff 00 00 00 00 00 00 00 00");
}
TEST_CASE("MemoryRange success")
{
MemoryRange r((uint8_t)ResponseCode::SUCCESS, 422, 788);
CHECK(arrayToHexString(r.toBytes()) == "01 00 00 00 00 00 00 00 00 00 00 ff a6 01 00 00 14 03 00 00");
}
// TEST_CASE("MemoryRange from bytes success")
// {
// MemoryRange r((uint8_t)ResponseCode::SUCCESS, 0, 0);
// r.fromBytes(hexStringToBytes(std::string("01 00 00 00 00 00 00 00 00 00 00 ff a6 01 00 00 14 03 00 00")));
// CHECK(arrayToHexString(r.toBytes()) ==);
// }
class Checksum : public Response
{
private:
uint16_t checksum;
public:
Checksum(
uint8_t command,
uint16_t checksum = 0)
: Response(command), checksum(checksum)
{
}
std::array<uint8_t, 14> toBytes() const
{
std::array<uint8_t, 12> baseBuffer = Response::toBytes();
std::array<uint8_t, 14> buffer;
memcpy(buffer.data(), baseBuffer.data(), baseBuffer.size());
memcpy(buffer.data() + baseBuffer.size(), &checksum, sizeof(checksum));
return buffer;
}
void fromBytes(const std::array<uint8_t, 14> &buffer)
{
std::array<uint8_t, 12> baseBuffer;
memcpy(baseBuffer.data(), buffer.data(), baseBuffer.size());
Response::fromBytes(baseBuffer);
memcpy(&checksum, buffer.data() + baseBuffer.size(), sizeof(checksum));
}
static size_t getSize()
{
return Response::getSize() + sizeof(checksum);
}
};
TEST_CASE("Checksum class is 42")
{
Checksum c(ResponseCode::SUCCESS, 42);
CHECK(arrayToHexString(c.toBytes()) == "01 00 00 00 00 00 00 00 00 00 00 ff 2a 00");
}
TEST_CASE("Checksum class is 78")
{
Checksum c(ResponseCode::BAD_ADDRESS, 78);
CHECK(arrayToHexString(c.toBytes()) == "fe 00 00 00 00 00 00 00 00 00 00 ff 4e 00");
}
/**
* Bootloader attributes.
*
* Attributes:
* version - Bootloader version number.
* max_packet_length - Maximum number of bytes which can be sent to the bootloader
* per packet. Includes the size of the packet itself plus
* associated data.
* device_id - A device-specific identifier.
* erase_size - Size of a flash erase page in bytes. When erasing flash, the size
* of the memory area which should be erased is given in number of
* erase pages.
* write_size - Size of a write block in bytes. When writing to flash, the data
* must align with a write block.
* memory_start - Start address of the program memory range.
* memory_end - End address of the program memory range. The range is half-open,
* i.e., this address is not part of the program memory range.
* has_checksum - Indicates whether or not the bootloader supports the `CALC_CHECKSUM`
* command.
*/
typedef struct
{
int version;
int max_packet_length;
int device_id;
int erase_size;
int write_size;
int memory_start;
int memory_end;
bool has_checksum;
} BootAttrs;