-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathmetadata.h
709 lines (625 loc) · 27.5 KB
/
metadata.h
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
#pragma once
#include <memory>
#include <iterator>
#include "boot/picobin.h"
#include <map>
#include <cassert>
#include "elf_file.h"
#if ENABLE_DEBUG_LOG
#define DEBUG_LOG(...) printf(__VA_ARGS__)
#else
#define DEBUG_LOG(...) ((void)0)
#endif
struct item;
template<typename InputIterator> std::vector<uint32_t> lsb_bytes_to_words(InputIterator begin, InputIterator end) {
using InputType = typename std::iterator_traits<InputIterator>::value_type;
static_assert(sizeof(InputType) == 1, "");
std::vector<uint32_t> rc;
size_t size = end - begin;
assert(!(size & 3));
if (size) {
rc.reserve(size / 4);
for(auto it = begin; it < end; ) {
uint32_t word = *it++;
word |= (*it++) << 8;
word |= (*it++) << 16;
word |= (*it++) << 24;
rc.push_back(word);
}
}
return rc;
}
template<typename InputIterator, typename OutputType = std::uint8_t> std::vector<OutputType> words_to_lsb_bytes(InputIterator begin, InputIterator end) {
static_assert(sizeof(OutputType) == 1, "");
std::vector<OutputType> rc;
size_t size = end - begin;
if (size) {
rc.reserve(size * 4);
for(auto it = begin; it < end; ) {
uint32_t word = *it++;
rc.push_back(word & 0xff);
rc.push_back((word >> 8) & 0xff);
rc.push_back((word >> 16) & 0xff);
rc.push_back((word >> 24) & 0xff);
}
}
return rc;
}
struct item_writer_context {
item_writer_context(uint32_t base_addr) : base_addr(base_addr), word_offset(0) {}
std::map<std::shared_ptr<item>, uint32_t> item_word_offsets;
uint32_t base_addr;
uint32_t word_offset;
};
struct item {
explicit item() {};
virtual ~item() = default;
virtual uint8_t type() const = 0;
static uint32_t decode_size(uint32_t item_header) {
uint32_t size;
if (item_header & 0x80) {
size = (uint16_t)(item_header >> 8);
} else {
size = (uint8_t)(item_header >> 8);
}
return size;
}
virtual std::vector<uint32_t> to_words(item_writer_context &ctx) const = 0;
virtual uint32_t encode_type_and_size(unsigned int size) const {
assert(size < PICOBIN_MAX_BLOCK_SIZE);
if (size < 256) {
return (size << 8u) | type();
} else {
// todo byte order
return (size << 8u) | 0x80 | type();
}
}
};
// always use single byte size
struct single_byte_size_item : public item {
virtual uint32_t encode_type_and_size(unsigned int size) const override {
assert(size < PICOBIN_MAX_BLOCK_SIZE);
assert(size < 128);
assert(!(type() & 128));
return (size << 8u) | type();
}
};
// always use double byte size
struct double_byte_size_item : public item {
virtual uint32_t encode_type_and_size(unsigned int size) const override {
assert(size < PICOBIN_MAX_BLOCK_SIZE);
assert(type() & 128);
// todo byte order
return (size << 8u) | 0x80 | type();
}
};
struct ignored_item : public double_byte_size_item {
uint8_t type() const override { return PICOBIN_BLOCK_ITEM_2BS_IGNORED; }
ignored_item() = default;
explicit ignored_item(uint32_t size, std::vector<uint32_t> data) : size(size), data(data) {}
template <typename I> static std::shared_ptr<item> parse(I& it, I end, uint32_t header) {
uint32_t size = item::decode_size(header);
std::vector<uint32_t> data;
for (unsigned int i=1; i < size; i++) {
data.push_back(*it++);
}
return std::make_shared<ignored_item>(size, data);
}
std::vector<uint32_t> to_words(item_writer_context& ctx) const override {
std::vector<uint32_t> ret;
ret.push_back(encode_type_and_size(size));
ret.insert(ret.end(), data.begin(), data.end());
return ret;
}
uint32_t size;
std::vector<uint32_t> data;
};
enum image_type_image_type {
type_invalid=PICOBIN_IMAGE_TYPE_IMAGE_TYPE_INVALID,
type_exe=PICOBIN_IMAGE_TYPE_IMAGE_TYPE_EXE,
type_data=PICOBIN_IMAGE_TYPE_IMAGE_TYPE_DATA
};
enum image_type_exe_security {
sec_unspecified=PICOBIN_IMAGE_TYPE_EXE_SECURITY_UNSPECIFIED,
sec_ns=PICOBIN_IMAGE_TYPE_EXE_SECURITY_NS,
sec_s=PICOBIN_IMAGE_TYPE_EXE_SECURITY_S
};
enum image_type_exe_cpu {
cpu_arm=PICOBIN_IMAGE_TYPE_EXE_CPU_ARM,
cpu_riscv=PICOBIN_IMAGE_TYPE_EXE_CPU_RISCV,
cpu_varmulet=PICOBIN_IMAGE_TYPE_EXE_CPU_VARMULET
};
enum image_type_exe_chip {
chip_rp2040=PICOBIN_IMAGE_TYPE_EXE_CHIP_RP2040,
chip_rp2350=PICOBIN_IMAGE_TYPE_EXE_CHIP_RP2350
};
struct image_type_item : public single_byte_size_item {
uint8_t type() const override { return PICOBIN_BLOCK_ITEM_1BS_IMAGE_TYPE; }
image_type_item() = default;
explicit image_type_item(uint16_t flags) : flags(flags) {}
template <typename I> static std::shared_ptr<item> parse(I it, I end, uint32_t header) {
return std::make_shared<image_type_item>(header >> 16);
}
std::vector<uint32_t> to_words(item_writer_context& ctx) const override {
return {encode_type_and_size(1) | (flags << 16)};
}
image_type_image_type image_type() const { return static_cast<image_type_image_type>((flags & PICOBIN_IMAGE_TYPE_IMAGE_TYPE_BITS) >> PICOBIN_IMAGE_TYPE_IMAGE_TYPE_LSB); }
image_type_exe_security security() const { return static_cast<image_type_exe_security>((flags & PICOBIN_IMAGE_TYPE_EXE_SECURITY_BITS) >> PICOBIN_IMAGE_TYPE_EXE_SECURITY_LSB); }
image_type_exe_cpu cpu() const { return static_cast<image_type_exe_cpu>((flags & PICOBIN_IMAGE_TYPE_EXE_CPU_BITS) >> PICOBIN_IMAGE_TYPE_EXE_CPU_LSB); }
image_type_exe_chip chip() const { return static_cast<image_type_exe_chip>((flags & PICOBIN_IMAGE_TYPE_EXE_CHIP_BITS) >> PICOBIN_IMAGE_TYPE_EXE_CHIP_LSB); }
bool tbyb() const { return flags & PICOBIN_IMAGE_TYPE_EXE_TBYB_BITS; }
uint16_t flags;
};
struct partition_table_item : public single_byte_size_item {
struct partition {
uint8_t permissions = 0;
uint16_t first_sector = 0;
uint16_t last_sector = 0;
uint32_t flags = 0;
uint64_t id;
std::string name;
std::vector<uint32_t> extra_families;
std::vector<uint32_t> to_words() const {
std::vector<uint32_t> ret = {
((permissions << PICOBIN_PARTITION_PERMISSIONS_LSB) & PICOBIN_PARTITION_PERMISSIONS_BITS)
| ((first_sector << PICOBIN_PARTITION_LOCATION_FIRST_SECTOR_LSB) & PICOBIN_PARTITION_LOCATION_FIRST_SECTOR_BITS)
| ((last_sector << PICOBIN_PARTITION_LOCATION_LAST_SECTOR_LSB) & PICOBIN_PARTITION_LOCATION_LAST_SECTOR_BITS),
((permissions << PICOBIN_PARTITION_PERMISSIONS_LSB) & PICOBIN_PARTITION_PERMISSIONS_BITS)
| flags,
};
if (flags & PICOBIN_PARTITION_FLAGS_HAS_ID_BITS) {
ret.push_back((uint32_t)id);
ret.push_back((uint32_t)(id >> 32));
}
if (extra_families.size() > 0) {
assert(extra_families.size() == (flags & PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_BITS) >> PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_LSB);
ret.insert(ret.end(), extra_families.begin(), extra_families.end());
}
if (name.size() > 0) {
char size = name.size();
std::vector<char> name_vec = {size};
for (char c : name) {
name_vec.push_back(c);
}
while (name_vec.size() % 4 != 0) name_vec.push_back(0);
while (name_vec.size() > 0) {
ret.push_back(*(uint32_t*)name_vec.data());
name_vec.erase(name_vec.begin(), name_vec.begin() + 4);
}
}
return ret;
}
};
uint8_t type() const override { return PICOBIN_BLOCK_ITEM_PARTITION_TABLE; }
partition_table_item() = default;
explicit partition_table_item(uint32_t unpartitioned_flags, bool singleton) : unpartitioned_flags(unpartitioned_flags), singleton(singleton) {}
template <typename I> static std::shared_ptr<item> parse(I& it, I end, uint32_t header) {
uint32_t size = decode_size(header);
uint8_t singleton_count = header >> 24;
bool singleton = singleton_count & 0x80;
uint8_t partition_count = singleton_count & 0x0f;
uint32_t unpartitioned_flags = *it++;
auto pt = std::make_shared<partition_table_item>(unpartitioned_flags, singleton);
std::vector<uint32_t> data;
for (unsigned int i=2; i < size; i++) {
data.push_back(*it++);
}
size_t i=0;
while (i < data.size()) {
partition new_p;
uint32_t permissions_locations = data[i++];
new_p.permissions = (permissions_locations & PICOBIN_PARTITION_PERMISSIONS_BITS) >> PICOBIN_PARTITION_PERMISSIONS_LSB;
new_p.first_sector = (permissions_locations & PICOBIN_PARTITION_LOCATION_FIRST_SECTOR_BITS) >> PICOBIN_PARTITION_LOCATION_FIRST_SECTOR_LSB;
new_p.last_sector = (permissions_locations & PICOBIN_PARTITION_LOCATION_LAST_SECTOR_BITS) >> PICOBIN_PARTITION_LOCATION_LAST_SECTOR_LSB;
uint32_t permissions_flags = data[i++];
uint8_t permissions2 = (permissions_flags & PICOBIN_PARTITION_PERMISSIONS_BITS) >> PICOBIN_PARTITION_PERMISSIONS_LSB;
if (new_p.permissions != permissions2) {
printf("Permissions mismatch %02x %02x\n", new_p.permissions, permissions2);
assert(false);
}
new_p.flags = permissions_flags & (~PICOBIN_PARTITION_PERMISSIONS_BITS);
if (new_p.flags & PICOBIN_PARTITION_FLAGS_HAS_ID_BITS) {
uint32_t low = data[i++];
uint32_t high = data[i++];
new_p.id = (uint64_t)low | ((uint64_t)high << 32);
}
uint8_t num_extra_families = (new_p.flags & PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_BITS) >> PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_LSB;
for (int fam=0; fam < num_extra_families; fam++) {
new_p.extra_families.push_back(data[i++]);
}
if (new_p.flags & PICOBIN_PARTITION_FLAGS_HAS_NAME_BITS) {
auto bytes = words_to_lsb_bytes(data.begin() + i++, data.end());
int name_size = bytes[0];
// This works neatly - accounts for the size byte at the start
i += name_size / 4;
new_p.name = std::string((char*)(bytes.data() + 1), name_size);
}
pt->partitions.push_back(new_p);
}
return pt;
}
std::vector<uint32_t> to_words(item_writer_context& ctx) const override {
std::vector<uint32_t> partition_words;
for (auto p : partitions) {
auto words = p.to_words();
partition_words.insert(partition_words.end(), words.begin(), words.end());
}
std::vector<uint32_t> ret = {
encode_type_and_size(2 + partition_words.size()) | (singleton << 31) | ((uint8_t)partitions.size() << 24),
unpartitioned_flags
};
ret.insert(ret.end(), partition_words.begin(), partition_words.end());
return ret;
}
uint32_t unpartitioned_flags;
bool singleton;
std::vector<partition> partitions;
};
struct vector_table_item : public single_byte_size_item {
uint8_t type() const override { return PICOBIN_BLOCK_ITEM_1BS_VECTOR_TABLE; }
vector_table_item() = default;
explicit vector_table_item(uint32_t addr) : addr(addr) {}
template <typename I> static std::shared_ptr<item> parse(I& it, I end, uint32_t header) {
if (decode_size(header) != 2) {
printf("Bad block size %d for vector table item\n", decode_size(header));
assert(false);
}
return std::make_shared<vector_table_item>(*it++);
}
std::vector<uint32_t> to_words(item_writer_context& ctx) const override {
return {encode_type_and_size(2), addr};
}
uint32_t addr;
};
struct rolling_window_delta_item : public single_byte_size_item {
uint8_t type() const override { return PICOBIN_BLOCK_ITEM_1BS_ROLLING_WINDOW_DELTA; }
rolling_window_delta_item() = default;
explicit rolling_window_delta_item(uint32_t addr) : addr(addr) {}
template <typename I> static std::shared_ptr<item> parse(I& it, I end, uint32_t header) {
if (decode_size(header) != 2) {
printf("Bad block size %d for rolling window delta item\n", decode_size(header));
assert(false);
}
return std::make_shared<rolling_window_delta_item>(*it++);
}
std::vector<uint32_t> to_words(item_writer_context& ctx) const override {
return {encode_type_and_size(2), (uint32_t)addr};
}
int32_t addr;
};
struct entry_point_item : public single_byte_size_item {
uint8_t type() const override { return PICOBIN_BLOCK_ITEM_1BS_ENTRY_POINT; }
entry_point_item() = default;
explicit entry_point_item(uint32_t ep, uint32_t sp) : ep(ep), sp(sp) {}
explicit entry_point_item(uint32_t ep, uint32_t sp, uint32_t splim) : ep(ep), sp(sp), splim(splim), splim_set(true) {}
template <typename I> static std::shared_ptr<item> parse(I& it, I end, uint32_t header) {
uint32_t size = decode_size(header);
uint32_t ep = *it++;
uint32_t sp = *it++;
if (size == 3) {
return std::make_shared<entry_point_item>(ep, sp);
} else if (size == 4) {
uint32_t splim = *it++;
return std::make_shared<entry_point_item>(ep, sp, splim);
} else {
printf("Bad block size %d for entry point item\n", size);
assert(false);
return nullptr;
}
}
std::vector<uint32_t> to_words(item_writer_context& ctx) const override {
std::vector<uint32_t> ret = {encode_type_and_size(splim_set ? 4 : 3), ep, sp};
if (splim_set) ret.push_back(splim);
return ret;
}
uint32_t ep;
uint32_t sp;
uint32_t splim;
bool splim_set = false;
};
struct load_map_item : public item {
struct entry {
uint32_t storage_address;
uint32_t runtime_address;
uint32_t size;
};
uint8_t type() const override { return PICOBIN_BLOCK_ITEM_LOAD_MAP; }
load_map_item() = default;
explicit load_map_item(bool absolute, std::vector<entry> entries) : absolute(absolute), entries(entries) {}
template <typename I> static std::shared_ptr<item> parse(I& it, I end, uint32_t header, uint32_t current_addr) {
uint8_t num_entries = (header >> 24) & 0x7f;
bool absolute = (header >> 24) & 0x80;
std::vector<entry> entries;
for (int i=0; i < num_entries; i++) {
uint32_t storage_address = (uint32_t)*it++;
uint32_t runtime_address = (uint32_t)*it++;
uint32_t size = (uint32_t)*it++;
if (storage_address != 0) {
if (absolute) {
size -= runtime_address;
} else {
storage_address += current_addr;
}
}
entries.push_back({
storage_address,
runtime_address,
size
});
}
return std::make_shared<load_map_item>(absolute, entries);
}
std::vector<uint32_t> to_words(item_writer_context& ctx) const override {
assert(entries.size() < 256);
std::vector<uint32_t> rc = {
encode_type_and_size(1 + 3 * entries.size()) | (((uint8_t)entries.size())<<24) | (absolute ? 1 << 31 : 0)
};
for(const auto &entry : entries) {
// todo byte order
if (absolute) {
rc.push_back(entry.storage_address);
rc.push_back(entry.runtime_address);
if (entry.storage_address != 0) {
rc.push_back(entry.runtime_address + entry.size);
} else {
rc.push_back(entry.size);
}
} else {
if (entry.storage_address != 0) {
rc.push_back(entry.storage_address - ctx.base_addr - ctx.word_offset * 4);
} else {
rc.push_back(entry.storage_address);
}
rc.push_back(entry.runtime_address);
rc.push_back(entry.size);
}
}
return rc;
}
bool absolute;
std::vector<entry> entries;
};
struct version_item : public item {
uint8_t type() const override { return PICOBIN_BLOCK_ITEM_1BS_VERSION; }
version_item() = default;
explicit version_item(uint16_t major, uint16_t minor) : version_item(major, minor, 0, {}) {}
version_item(uint16_t major, uint16_t minor, uint16_t rollback, std::vector<uint16_t> otp_rows) :
rollback(rollback), otp_rows(std::move(otp_rows)) {
// NOTE: A linux sysroot might define `major` and `minor` as macros so we
// can't use regular initialization in the form `major(major)` since we woudn't
// be able to tell if we're calling a macro or not.
this->major = major;
this->minor = minor;
}
template <typename I> static std::shared_ptr<item> parse(I& it, I end, uint32_t header) {
// todo validate
uint32_t major_minor = *it++;
uint16_t major = major_minor >> 16;
uint16_t minor = major_minor & 0xffff;
unsigned int otp_row_count = header >> 24;
uint16_t rollback = 0;
std::vector<uint16_t> otp_rows;
if (otp_row_count) {
unsigned int pair = *it++;
// todo endian
rollback = (uint16_t) pair;
for(unsigned int i=0;i<otp_row_count;i++) {
if (i&1) pair = *it++;
otp_rows.push_back((uint16_t)(pair >> ((1^(i&1))*16)));
}
}
return std::make_shared<version_item>(major, minor, rollback, otp_rows);
}
std::vector<uint32_t> to_words(item_writer_context& ctx) const override {
assert(otp_rows.size() < 256);
unsigned int size = 2 + (!otp_rows.empty() + otp_rows.size() + 1) / 2;
std::vector<uint32_t> rc = {
(uint32_t)(encode_type_and_size(size) | (otp_rows.size() << 24))
};
uint32_t major_minor = (uint16_t)major << 16 | (uint16_t)minor;
rc.push_back(major_minor);
if (!otp_rows.empty()) {
rc.push_back(rollback);
for(unsigned int i=0;i<otp_rows.size();i++) {
if (i&1) {
rc.push_back(otp_rows[i]);
} else {
rc[rc.size()-1] |= otp_rows[i] << 16;
}
}
}
return rc;
}
uint16_t major;
uint16_t minor;
uint16_t rollback;
std::vector<uint16_t> otp_rows;
};
struct hash_def_item : public single_byte_size_item {
uint8_t type() const override { return PICOBIN_BLOCK_ITEM_1BS_HASH_DEF; }
hash_def_item() = default;
explicit hash_def_item(uint8_t hash_type) : hash_type(hash_type) {}
explicit hash_def_item(uint8_t hash_type, uint16_t block_words_to_hash) : hash_type(hash_type), block_words_to_hash(block_words_to_hash) {}
template <typename I> static std::shared_ptr<item> parse(I& it, I end, uint32_t header) {
uint8_t hash_type = (header & 0xff000000) >> 24;
uint16_t block_words_to_hash = *it++;
return std::make_shared<hash_def_item>(hash_type, block_words_to_hash);
}
std::vector<uint32_t> to_words(item_writer_context& ctx) const override {
return {
encode_type_and_size(2) | (hash_type << 24),
block_words_to_hash == 0 ? (ctx.word_offset + 2) : block_words_to_hash
};
}
uint8_t hash_type;
uint16_t block_words_to_hash = 0;
};
struct signature_item : public item {
uint8_t type() const override { return PICOBIN_BLOCK_ITEM_SIGNATURE; }
signature_item() = default;
explicit signature_item(uint8_t sig_type) : sig_type(sig_type) {}
explicit signature_item(uint8_t sig_type, std::vector<uint8_t> signature_bytes, std::vector<uint8_t> public_key_bytes) : sig_type(sig_type), signature_bytes(signature_bytes), public_key_bytes(public_key_bytes) {}
template <typename I> static std::shared_ptr<item> parse(I& it, I end, uint32_t header) {
auto sig_block_size = decode_size(header);
uint8_t sig_type = (header & 0xff000000) >> 24;
assert(sig_block_size == 0x21);
std::vector<uint8_t> signature_bytes;
std::vector<uint8_t> public_key_bytes;
{
std::vector<uint32_t> words;
for (int i=0; i < 16; i++) {
words.push_back(*it++);
}
auto bytes = words_to_lsb_bytes(words.begin(), words.end());
std::copy(bytes.begin(), bytes.end(), std::back_inserter(public_key_bytes));
}
{
std::vector<uint32_t> words;
for (int i=0; i < 16; i++) {
words.push_back(*it++);
}
auto bytes = words_to_lsb_bytes(words.begin(), words.end());
std::copy(bytes.begin(), bytes.end(), std::back_inserter(signature_bytes));
}
return std::make_shared<signature_item>(sig_type, signature_bytes, public_key_bytes);
}
std::vector<uint32_t> to_words(item_writer_context& ctx) const override {
assert(signature_bytes.size() % 4 == 0);
assert(public_key_bytes.size() % 4 == 0);
std::vector<uint32_t> rc = {
encode_type_and_size(1 + public_key_bytes.size()/4 + signature_bytes.size()/4) | (sig_type << 24),
};
auto words = lsb_bytes_to_words(public_key_bytes.begin(), public_key_bytes.end());
std::copy(words.begin(), words.end(), std::back_inserter(rc));
words = lsb_bytes_to_words(signature_bytes.begin(), signature_bytes.end());
std::copy(words.begin(), words.end(), std::back_inserter(rc));
return rc;
}
uint8_t sig_type;
std::vector<uint8_t> signature_bytes;
std::vector<uint8_t> public_key_bytes;
};
struct hash_value_item : public item {
uint8_t type() const override { return PICOBIN_BLOCK_ITEM_HASH_VALUE; }
hash_value_item() = default;
explicit hash_value_item(std::vector<uint8_t> hash_bytes) : hash_bytes(hash_bytes) {}
template <typename I> static std::shared_ptr<item> parse(I& it, I end, uint32_t header) {
auto hash_size = decode_size(header) - 1;
std::vector<uint32_t> hash_words;
for (unsigned int i=0; i < hash_size; i++) {
hash_words.push_back(*it++);
}
return std::make_shared<hash_value_item>(words_to_lsb_bytes(hash_words.begin(), hash_words.end()));
}
std::vector<uint32_t> to_words(item_writer_context& ctx) const override {
assert(hash_bytes.size() % 4 == 0);
std::vector<uint32_t> rc = {
encode_type_and_size(1 + hash_bytes.size()/4),
};
auto words = lsb_bytes_to_words(hash_bytes.begin(), hash_bytes.end());
std::copy(words.begin(), words.end(), std::back_inserter(rc));
return rc;
}
std::vector<uint8_t> hash_bytes;
};
struct block {
explicit block(uint32_t physical_addr, uint32_t next_block_rel=0, uint32_t next_block_rel_index=0, std::vector<std::shared_ptr<item>> items = {})
: physical_addr(physical_addr),
next_block_rel(next_block_rel),
next_block_rel_index(next_block_rel_index),
items( std::move(items)) {}
template <typename I> static std::unique_ptr<block> parse(uint32_t physical_addr, I next_block_rel_loc, I it, I end) {
I block_base = it;
uint32_t current_addr = physical_addr + 4; // for the ffffded3
std::vector<std::shared_ptr<item>> items;
while (it < end) {
auto item_base = it;
uint32_t header = *it++;
uint32_t size = item::decode_size(header);
std::shared_ptr<item> i;
switch ((uint8_t)header) {
case PICOBIN_BLOCK_ITEM_1BS_IMAGE_TYPE:
i = image_type_item::parse(it, end, header);
break;
case PICOBIN_BLOCK_ITEM_PARTITION_TABLE:
i = partition_table_item::parse(it, end, header);
break;
case PICOBIN_BLOCK_ITEM_1BS_VECTOR_TABLE:
i = vector_table_item::parse(it, end, header);
break;
case PICOBIN_BLOCK_ITEM_1BS_ROLLING_WINDOW_DELTA:
i = rolling_window_delta_item::parse(it, end, header);
break;
case PICOBIN_BLOCK_ITEM_1BS_VERSION:
i = version_item::parse(it, end, header);
break;
case PICOBIN_BLOCK_ITEM_1BS_ENTRY_POINT:
i = entry_point_item::parse(it, end, header);
break;
case PICOBIN_BLOCK_ITEM_LOAD_MAP:
i = load_map_item::parse(it, end, header, current_addr);
break;
case PICOBIN_BLOCK_ITEM_1BS_HASH_DEF:
i = hash_def_item::parse(it, end, header);
break;
case PICOBIN_BLOCK_ITEM_HASH_VALUE:
i = hash_value_item::parse(it, end, header);
break;
case PICOBIN_BLOCK_ITEM_SIGNATURE:
i = signature_item::parse(it, end, header);
break;
case PICOBIN_BLOCK_ITEM_2BS_IGNORED:
i = ignored_item::parse(it, end, header);
break;
default:
DEBUG_LOG("Ignoring block type: %02x\n", (uint8_t)header);
i = ignored_item::parse(it, end, header);
break;
}
if (i) {
items.push_back(i);
}
current_addr += (size*4);
if (it != item_base + size) {
printf("WARNING: item at %08x had wrong size %x (expected %x)\n", (unsigned int)(physical_addr + (item_base - block_base) * 4), (int)(it - item_base), (int)size);
it = item_base + size;
assert(false);
}
}
uint32_t next_block_rel = *next_block_rel_loc;
uint32_t next_block_rel_index = next_block_rel_loc - block_base + 1;
return std::make_unique<block>(physical_addr, next_block_rel, next_block_rel_index, items);
}
std::vector<uint32_t> to_words() {
std::vector<uint32_t> words;
words.push_back(PICOBIN_BLOCK_MARKER_START);
item_writer_context ctx(physical_addr);
for(const auto &item : items) {
ctx.word_offset = words.size();
ctx.item_word_offsets[item] = ctx.word_offset;
const auto item_words = item->to_words(ctx);
std::copy(item_words.begin(), item_words.end(), std::back_inserter(words));
}
// todo should use a real item struct
assert(words.size() <= PICOBIN_MAX_BLOCK_SIZE - 3);
// todo byte order
words.push_back(PICOBIN_BLOCK_ITEM_2BS_LAST | (words.size() - 1) << 8);
words.push_back(next_block_rel);
words.push_back(PICOBIN_BLOCK_MARKER_END);
return words;
}
template <typename I> std::shared_ptr<I> get_item() {
I tmp = I();
uint8_t type = tmp.type();
auto it = std::find_if(items.begin(), items.end(), [type](std::shared_ptr<item> i) { return i->type() == type; });
if (it != std::end(items)) {
return std::dynamic_pointer_cast<I>(*it);
} else {
return nullptr;
}
}
uint32_t physical_addr;
uint32_t next_block_rel;
uint32_t next_block_rel_index;
std::vector<std::shared_ptr<item>> items;
};