forked from raspberrypi/picotool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
7451 lines (6849 loc) · 314 KB
/
main.cpp
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "cli.h"
#include "clipp/clipp.h"
#include <cinttypes>
#include <csignal>
#include <cstdio>
#include <regex>
#ifndef __APPLE__
#include <cuchar>
#endif
#include <cwchar>
#include <map>
#include <iostream>
#include <vector>
#include <set>
#include <array>
#include <cstring>
#include <cstdarg>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <memory>
#include <functional>
#include "boot/uf2.h"
#include "boot/picobin.h"
#if HAS_LIBUSB
#include "picoboot_connection_cxx.h"
#include "rp2350.rom.h"
#include "xip_ram_perms.h"
#else
#include "picoboot_connection.h"
#endif
#include "bintool.h"
#include "elf2uf2.h"
#include "pico/bootrom_constants.h"
#include "pico/binary_info.h"
#include "pico/stdio_usb/reset_interface.h"
#include "elf.h"
#include "otp.h"
#include "errors.h"
#include "hardware/regs/otp_data.h"
#include "nlohmann/json.hpp"
#if defined(__unix__) || defined(__APPLE__)
#include <unistd.h>
#endif
// missing __builtins on windows
#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
static __forceinline int __builtin_ctz(unsigned x) {
unsigned long r;
_BitScanForward(&r, x);
return (int)r;
}
#endif
// tsk namespace is polluted on windows
#ifdef _WIN32
#undef min
#undef max
#define _CRT_SECURE_NO_WARNINGS
#endif
#define OTP_PAGE_COUNT 64
#define OTP_PAGE_ROWS 64
#define OTP_ROW_COUNT (OTP_PAGE_COUNT * OTP_PAGE_ROWS)
using std::string;
using std::vector;
using std::pair;
using std::map;
using std::tuple;
using std::ios;
using json = nlohmann::json;
#if HAS_LIBUSB
typedef map<enum picoboot_device_result,vector<tuple<model_t, libusb_device *, libusb_device_handle *>>> device_map;
#else
typedef map<enum picoboot_device_result,vector<tuple<model_t, void *, void *>>> device_map;
#endif
auto memory_names = map<enum memory_type, string>{
{memory_type::sram, "RAM"},
{memory_type::sram_unstriped, "Unstriped RAM"},
{memory_type::flash, "Flash"},
{memory_type::xip_sram, "XIP RAM"},
{memory_type::rom, "ROM"}
};
static string tool_name = "picotool";
static string hex_string(int64_t value, int width=8, bool prefix=true) {
std::stringstream ss;
if (prefix) ss << "0x";
ss << std::setfill('0') << std::setw(width) << std::hex << value;
return ss.str();
}
static string HEX_string(int64_t value, int width=8, bool prefix=true) {
std::stringstream ss;
if (prefix) ss << "0x";
ss << std::setfill('0') << std::setw(width) << std::uppercase << std::hex << value;
return ss.str();
}
std::array<std::array<string, 30>, 10> pin_functions_rp2040{{
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"SPI0 RX", "SPI0 CSn", "SPI0 SCK", "SPI0 TX", "SPI0 RX", "SPI0 CSn", "SPI0 SCK", "SPI0 TX", "SPI1 RX", "SPI1 CSn", "SPI1 SCK", "SPI1 TX", "SPI1 RX", "SPI1 CSn", "SPI1 SCK", "SPI1 TX", "SPI0 RX", "SPI0 CSn", "SPI0 SCK", "SPI0 TX", "SPI0 RX", "SPI0 CSn", "SPI0 SCK", "SPI0 TX", "SPI1 RX", "SPI1 CSn", "SPI1 SCK", "SPI1 TX", "SPI1 RX", "SPI1 CSn"},
{"UART0 TX", "UART0 RX", "UART0 CTS", "UART0 RTS", "UART1 TX", "UART1 RX", "UART1 CTS", "UART1 RTS", "UART1 TX", "UART1 RX", "UART1 CTS", "UART1 RTS", "UART0 TX", "UART0 RX", "UART0 CTS", "UART0 RTS", "UART0 TX", "UART0 RX", "UART0 CTS", "UART0 RTS", "UART1 TX", "UART1 RX", "UART1 CTS", "UART1 RTS", "UART1 TX", "UART1 RX", "UART1 CTS", "UART1 RTS", "UART0 TX", "UART0 RX"},
{"I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL"},
{"PWM0 A", "PWM0 B", "PWM1 A", "PWM1 B", "PWM2 A", "PWM2 B", "PWM3 A", "PWM3 B", "PWM4 A", "PWM4 B", "PWM5 A", "PWM5 B", "PWM6 A", "PWM6 B", "PWM7 A", "PWM7 B", "PWM0 A", "PWM0 B", "PWM1 A", "PWM1 B", "PWM2 A", "PWM2 B", "PWM3 A", "PWM3 B", "PWM4 A", "PWM4 B", "PWM5 A", "PWM5 B", "PWM6 A", "PWM6 B"},
{"SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO"},
{"PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0"},
{"PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1"},
{"","","","","","","","","","","","","","","","","","","","","CLOCK GPIN0","CLOCK GPOUT0","CLOCK GPIN1","CLOCK GPOUT1","CLOCK GPOUT2","CLOCK GPOUT3","","","",""},
{"USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN"},
}};
std::array<std::array<string, 48>, 12> pin_functions_rp2350{{
{"JTAG TCK","JTAG TMS", "JTAG TDI", "JTAG TDO", "", "", "", "", "", "", "", "", "HSTX0", "HSTX1", "HSTX2", "HSTX3", "HSTX4", "HSTX5", "HSTX6", "HSTX7",},
{"SPI0 RX", "SPI0 CSn", "SPI0 SCK", "SPI0 TX", "SPI0 RX", "SPI0 CSn", "SPI0 SCK", "SPI0 TX", "SPI1 RX", "SPI1 CSn", "SPI1 SCK", "SPI1 TX", "SPI1 RX", "SPI1 CSn", "SPI1 SCK", "SPI1 TX", "SPI0 RX", "SPI0 CSn", "SPI0 SCK", "SPI0 TX", "SPI0 RX", "SPI0 CSn", "SPI0 SCK", "SPI0 TX", "SPI1 RX", "SPI1 CSn", "SPI1 SCK", "SPI1 TX", "SPI1 RX", "SPI1 CSn", "SPI1 SCK", "SPI1 TX", "SPI0 RX", "SPI0 CSn", "SPI0 SCK", "SPI0 TX", "SPI0 RX", "SPI0 CSn", "SPI0 SCK", "SPI0 TX", "SPI1 RX", "SPI1 CSn", "SPI1 SCK", "SPI1 TX", "SPI1 RX", "SPI1 CSn", "SPI1 SCK", "SPI1 TX"},
{"UART0 TX","UART0 RX", "UART0 CTS","UART0 RTS","UART1 TX", "UART1 RX", "UART1 CTS","UART1 RTS","UART1 TX", "UART1 RX", "UART1 CTS","UART1 RTS","UART0 TX", "UART0 RX", "UART0 CTS","UART0 RTS","UART0 TX", "UART0 RX", "UART0 CTS","UART0 RTS","UART1 TX", "UART1 RX", "UART1 CTS","UART1 RTS","UART1 TX", "UART1 RX", "UART1 CTS","UART1 RTS","UART0 TX", "UART0 RX", "UART0 CTS","UART0 RTS","UART0 TX", "UART0 RX", "UART0 CTS","UART0 RTS","UART1 TX", "UART1 RX", "UART1 CTS","UART1 RTS","UART1 TX", "UART1 RX", "UART1 CTS","UART1 RTS","UART0 TX", "UART0 RX", "UART0 CTS","UART0 RTS"},
{"I2C0 SDA","I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL", "I2C0 SDA", "I2C0 SCL", "I2C1 SDA", "I2C1 SCL"},
{"PWM0 A", "PWM0 B", "PWM1 A", "PWM1 B", "PWM2 A", "PWM2 B", "PWM3 A", "PWM3 B", "PWM4 A", "PWM4 B", "PWM5 A", "PWM5 B", "PWM6 A", "PWM6 B", "PWM7 A", "PWM7 B", "PWM0 A", "PWM0 B", "PWM1 A", "PWM1 B", "PWM2 A", "PWM2 B", "PWM3 A", "PWM3 B", "PWM4 A", "PWM4 B", "PWM5 A", "PWM5 B", "PWM6 A", "PWM6 B", "PWM7 A", "PWM7 B", "PWM8 A", "PWM8 B", "PWM9 A", "PWM9 B", "PWM10 A", "PWM10 B", "PWM11 A", "PWM11 B", "PWM8 A", "PWM8 B", "PWM9 A", "PWM9 B", "PWM10 A", "PWM10 B", "PWM11 A", "PWM11 B"},
{"SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO", "SIO"},
{"PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0", "PIO0"},
{"PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1", "PIO1"},
{"XIP CS1", "CORESIGHT TRACECLK","CORESIGHT TRACEDATA0","CORESIGHT TDATA1","CORESIGHT TDATA2","CORESIGHT TDATA3","","","XIP CS1", "", "","", "CLK GPIN", "CLK GPOUT","CLK GPIN", "CLK GPOUT","", "", "", "XIP CS1", "CLK GPIN", "CLK GPOUT","CLK GPIN", "CLK GPOUT","CLK GPOUT","CLK GPOUT","", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "XIP CS1"},
{"USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN", "USB OVCUR DET", "USB VBUS DET", "USB VBUS EN"},
{"", "", "UART0 TX", "UART0 RX", "", "", "UART1 TX", "UART1 RX", "", "", "UART1 TX", "UART1 RX", "", "", "UART0 TX", "UART0 RX", "", "", "UART0 TX", "UART0 RX", "", "", "UART1 TX", "UART1 RX", "", "", "UART1 TX", "UART1 RX", "", "", "UART0 TX", "UART0 RX", "", "", "UART0 TX", "UART0 RX", "", "", "UART1 TX", "UART1 RX", "", "", "UART1 TX", "UART1 RX", "", "", "UART0 TX", "UART0 RX"}
}};
std::map<uint32_t, otp_reg> otp_regs;
#if HAS_LIBUSB
auto bus_device_string = [](struct libusb_device *device) {
return string("Device at bus ") + std::to_string(libusb_get_bus_number(device)) + ", address " + std::to_string(libusb_get_device_address(device));
};
#endif
enum class filetype {bin, elf, uf2, pem, json};
const string getFiletypeName(enum filetype type)
{
switch (type)
{
case filetype::elf: return "ELF";
case filetype::bin: return "BIN";
case filetype::uf2: return "UF2";
case filetype::pem: return "PEM";
case filetype::json: return "JSON";
default: assert(false); return "ERROR_TYPE";
}
}
struct cancelled_exception : std::exception { };
struct not_mapped_exception : std::exception {
const char *what() const noexcept override {
return "Hmm uncaught not mapped";
}
};
// from -> to
struct range {
range() : from(0), to(0) {}
range(uint32_t from, uint32_t to) : from(from), to(to) {}
uint32_t from;
uint32_t to;
uint32_t len() const {
return to - from;
}
bool empty() const {
return from >= to;
}
bool contains(uint32_t addr) const { return addr>=from && addr<to; }
uint32_t clamp(uint32_t addr) const {
if (addr < from) addr = from;
if (addr > to) addr = to;
return addr;
}
void intersect(const range& other) {
from = other.clamp(from);
to = other.clamp(to);
}
bool intersects(const range& other) const {
return !(other.from >= to || other.to < from);
}
};
// ranges should not overlap
template <typename T> struct range_map {
range_map() = default;
struct mapping {
mapping(uint32_t offset, uint32_t max_offset) : offset(offset), max_offset(max_offset) {}
const uint32_t offset;
const uint32_t max_offset;
};
void insert(const range& r, T t) {
if (r.to != r.from) {
assert(r.to > r.from);
// check we don't overlap any existing map entries
auto f = m.upper_bound(r.from); // first element that starts after r.from
if (f != m.begin()) f--; // back up, to catch element that starts on or before r.from
for(; f != m.end() && f->first < r.to; f++) { // loop till we can't possibly overlap
range r2(f->first, f->second.first);
if (r2.intersects(r)) {
fail(ERROR_FORMAT, "Found overlapping memory ranges 0x%08x->0x%08x and 0x%08x->%08x\n",
r.from, r.to, r2.from, r2.to);
}
}
m.insert(std::make_pair(r.from, std::make_pair(r.to, t)));
}
}
pair<mapping, T> get(uint32_t p) {
auto f = m.upper_bound(p);
if (f == m.end()) {
if (m.empty())
throw not_mapped_exception();
} else if (f == m.begin()) {
throw not_mapped_exception();
}
f--;
assert(p >= f->first);
if (p >= f->second.first) {
throw not_mapped_exception();
}
return std::make_pair(mapping(p - f->first, f->second.first - f->first), f->second.second);
}
uint32_t next(uint32_t p) {
auto f = m.upper_bound(p);
if (f == m.end()) {
return std::numeric_limits<uint32_t>::max();
}
return f->first;
}
vector<range> ranges() {
vector<range> r;
r.reserve(m.size());
for(const auto &e : m) {
r.emplace_back(range(e.first, e.second.first));
}
return r;
}
size_t size() const { return m.size(); }
range_map<T> offset_by(uint32_t offset) {
range_map<T> rmap_offset;
for(const auto &e : m) {
rmap_offset.insert(range(e.first + offset, e.second.first + offset), e.second.second);
}
return rmap_offset;
}
private:
map<uint32_t, pair<uint32_t, T>> m;
};
using cli::group;
using cli::option;
using cli::integer;
using cli::hex;
using cli::value;
// todo can we derive from hex?
struct family_id : public cli::value_base<family_id> {
explicit family_id(string name) : value_base(std::move(name)) {}
template<typename T>
family_id &set(T &t) {
string nm = "<" + name() + ">";
// note we cannot capture "this"
on_action([&t, nm](string value) {
auto ovalue = value;
if (value == "data") {
t = DATA_FAMILY_ID;
} else if (value == "absolute") {
t = ABSOLUTE_FAMILY_ID;
} else if (value == "rp2040") {
t = RP2040_FAMILY_ID;
} else if (value == "rp2350-arm-s") {
t = RP2350_ARM_S_FAMILY_ID;
} else if (value == "rp2350-arm-ns") {
t = RP2350_ARM_NS_FAMILY_ID;
} else if (value == "rp2350-riscv") {
t = RP2350_RISCV_FAMILY_ID;
} else {
if (value.find("0x") == 0) value = value.substr(2);
size_t pos = 0;
long lvalue = std::numeric_limits<long>::max();
try {
lvalue = std::stoul(value, &pos, 16);
if (pos != value.length()) {
return "Garbage after hex value: " + value.substr(pos);
}
} catch (std::invalid_argument &) {
return ovalue + " is not a valid hex value";
} catch (std::out_of_range &) {
}
if (lvalue != (unsigned int) lvalue) {
return value + " is not a valid 32 bit value";
}
t = (unsigned int) lvalue;
}
return string("");
});
return *this;
}
};
string family_name(unsigned int family_id) {
if (family_id == DATA_FAMILY_ID) return "'data'";
if (family_id == ABSOLUTE_FAMILY_ID) return "'absolute'";
if (family_id == RP2040_FAMILY_ID) return "'rp2040'";
if (family_id == RP2350_ARM_S_FAMILY_ID) return "'rp2350-arm-s'";
if (family_id == RP2350_ARM_NS_FAMILY_ID) return "'rp2350-arm-ns'";
if (family_id == RP2350_RISCV_FAMILY_ID) return "'rp2350-riscv'";
if (!family_id) return "none";
return hex_string(family_id);
}
struct cmd {
explicit cmd(string name) : _name(std::move(name)) {}
virtual ~cmd() = default;
enum device_support { none, one, zero_or_more };
virtual group get_cli() = 0;
virtual string get_doc() const = 0;
virtual device_support get_device_support() { return one; }
virtual bool force_requires_pre_reboot() { return true; }
// return true if the command caused a reboot
virtual bool execute(device_map& devices) = 0;
virtual bool is_multi() const { return false; }
virtual bool requires_rp2350() const { return false; }
virtual std::vector<std::shared_ptr<cmd>> sub_commands() const { return std::vector<std::shared_ptr<cmd>>(); }
const string& name() { return _name; }
private:
string _name;
};
struct multi_cmd : public cmd {
explicit multi_cmd(std::string name, std::vector<std::shared_ptr<cmd>> sub_commands) : cmd(name), _sub_commands(sub_commands) {}
virtual group get_cli() override { assert(false); return group(); }
virtual bool execute(device_map& devices) override { assert(false); return false; }
virtual bool is_multi() const override { return true; }
virtual std::vector<std::shared_ptr<cmd>> sub_commands() const override {
return _sub_commands;
}
private:
std::vector<std::shared_ptr<cmd>> _sub_commands;
};
struct _settings {
std::array<std::string, 4> filenames;
std::array<std::string, 4> file_types;
uint32_t binary_start = FLASH_START;
int bus=-1;
int address=-1;
int vid=-1;
int pid=-1;
string ser;
uint32_t offset = 0;
uint32_t from = 0;
uint32_t to = 0;
uint32_t partition_size = 0;
bool offset_set = false;
bool range_set = false;
bool reboot_usb = false;
bool reboot_app_specified = false;
int reboot_diagnostic_partition = BOOT_PARTITION_NONE;
bool force = false;
bool force_no_reboot = false;
string switch_cpu;
uint32_t family_id = 0;
bool quiet = false;
bool verbose = false;
struct {
int redundancy = -1;
bool raw = false;
bool ecc = false;
bool fuzzy = false;
uint32_t value = 0;
uint8_t lock0 = 0;
uint8_t lock1 = 0;
int8_t led_pin = -1;
std::vector<uint32_t> pages;
bool list_pages = false;
bool list_no_descriptions = false;
std::vector<std::string> selectors;
uint32_t row = 0;
std::vector<std::string> extra_files;
} otp;
struct {
bool show_basic = false;
bool all = false;
bool show_pins = false;
bool show_device = false;
bool show_debug = false;
bool show_build = false;
} info;
struct {
string group;
string key;
string value;
} config;
struct {
bool verify = false;
bool execute = false;
bool no_overwrite = false;
bool no_overwrite_force = false;
bool update = false;
bool ignore_pt = false;
int partition = -1;
} load;
struct {
bool hash = false;
bool sign = false;
bool clear_sram = false;
uint16_t major_version = 0;
uint16_t minor_version = 0;
uint16_t rollback_version = 0;
std::vector<uint16_t> rollback_rows;
} seal;
struct {
uint32_t align = 0x1000;
} link;
struct {
bool all = false;
} save;
struct {
bool semantic = false;
string version;
} version;
struct {
bool hash = true;
bool sign = false;
bool singleton = false;
} partition;
struct {
bool abs_block = false;
#if SUPPORT_A2
uint32_t abs_block_loc = 0x11000000 - UF2_PAGE_SIZE;
#else
uint32_t abs_block_loc = 0;
#endif
} uf2;
};
_settings settings;
std::shared_ptr<cmd> selected_cmd;
auto device_selection =
(
(option("--bus") & integer("bus").min_value(0).max_value(255).set(settings.bus)
.if_missing([] { return "missing bus number"; })) % "Filter devices by USB bus number" +
(option("--address") & integer("addr").min_value(1).max_value(127).set(settings.address)
.if_missing([] { return "missing address"; })) % "Filter devices by USB device address" +
(option("--vid") & integer("vid").set(settings.vid).if_missing([] { return "missing vid"; })) % "Filter by vendor id" +
(option("--pid") & integer("pid").set(settings.pid)) % "Filter by product id" +
(option("--ser") & value("ser").set(settings.ser)) % "Filter by serial number"
+ option('f', "--force").set(settings.force) % "Force a device not in BOOTSEL mode but running compatible code to reset so the command can be executed. After executing the command (unless the command itself is a 'reboot') the device will be rebooted back to application mode" +
option('F', "--force-no-reboot").set(settings.force_no_reboot) % "Force a device not in BOOTSEL mode but running compatible code to reset so the command can be executed. After executing the command (unless the command itself is a 'reboot') the device will be left connected and accessible to picotool, but without the RPI-RP2 drive mounted"
).min(0).doc_non_optional(true).collapse_synopsys("device-selection");
#define file_types_x(i)\
(option ('t', "--type") & value("type").set(settings.file_types[i]))\
% "Specify file type (uf2 | elf | bin) explicitly, ignoring file extension"
#define named_file_types_x(types, i)\
(option ('t', "--type") & value("type").set(settings.file_types[i]))\
% "Specify file type (" types ") explicitly, ignoring file extension"
#define file_selection_x(i)\
(\
value("filename").with_exclusion_filter([](const string &value) {\
return value.find_first_of('-') == 0;\
}).set(settings.filenames[i]) % "The file name" +\
file_types_x(i)\
)
#define named_file_selection_x(name, i)\
(\
value(name).with_exclusion_filter([](const string &value) {\
return value.find_first_of('-') == 0;\
}).set(settings.filenames[i]) % "The file name" +\
file_types_x(i)\
)
#define named_typed_file_selection_x(name, i, types)\
(\
value(name).with_exclusion_filter([](const string &value) {\
return value.find_first_of('-') == 0;\
}).set(settings.filenames[i]) % "The file name" +\
named_file_types_x(types, i)\
)
#define optional_file_selection_x(name, i)\
(\
value(name).with_exclusion_filter([](const string &value) {\
return value.find_first_of('-') == 0;\
}).set(settings.filenames[i]).min(0) % "The file name" +\
file_types_x(i)\
).min(0).doc_non_optional(true)
#define optional_typed_file_selection_x(name, i, types)\
(\
value(name).with_exclusion_filter([](const string &value) {\
return value.find_first_of('-') == 0;\
}).set(settings.filenames[i]).min(0) % "The file name" +\
named_file_types_x(types, i)\
).min(0).doc_non_optional(true)
#define option_file_selection_x(option, i)\
(\
option & value("filename").with_exclusion_filter([](const string &value) {\
return value.find_first_of('-') == 0;\
}).set(settings.filenames[i]) % "The file name" +\
file_types_x(i)\
)
auto file_types = (option ('t', "--type") & value("type").set(settings.file_types[0]))
% "Specify file type (uf2 | elf | bin) explicitly, ignoring file extension";
auto file_selection =
(
value("filename").with_exclusion_filter([](const string &value) {
return value.find_first_of('-') == 0;
}).set(settings.filenames[0]) % "The file name" +
file_types
);
struct info_command : public cmd {
info_command() : cmd("info") {}
bool execute(device_map& devices) override;
device_support get_device_support() override {
if (settings.filenames[0].empty())
return zero_or_more;
else
return none;
}
group get_cli() override {
return (
(
option('b', "--basic").set(settings.info.show_basic) % "Include basic information. This is the default" +
option('p', "--pins").set(settings.info.show_pins) % "Include pin information" +
option('d', "--device").set(settings.info.show_device) % "Include device information" +
option("--debug").set(settings.info.show_debug) % "Include device debug information" +
option('l', "--build").set(settings.info.show_build) % "Include build attributes" +
option('a', "--all").set(settings.info.all) % "Include all information"
).min(0).doc_non_optional(true) % "Information to display" +
(
#if HAS_LIBUSB
device_selection % "To target one or more connected RP2040 device(s) in BOOTSEL mode (the default)" |
#endif
file_selection % "To target a file"
).major_group("TARGET SELECTION").min(0).doc_non_optional(true)
);
}
string get_doc() const override {
return "Display information from the target device(s) or file.\nWithout any arguments, this will display basic information for all connected RP2040 devices in BOOTSEL mode";
}
};
struct config_command : public cmd {
config_command() : cmd("config") {}
bool execute(device_map& devices) override;
device_support get_device_support() override {
if (settings.filenames[0].empty())
return zero_or_more;
else
return none;
}
group get_cli() override {
return (
(option('s', "--set") & (
value("key").set(settings.config.key) % "Variable name" +
value("value").set(settings.config.value) % "New value")
).force_expand_help(true) +
(option('g', "--group") & value("group").set(settings.config.group)) % "Filter by feature group" +
(
#if HAS_LIBUSB
device_selection % "To target one or more connected RP2040 device(s) in BOOTSEL mode (the default)" |
#endif
file_selection % "To target a file"
).major_group("TARGET SELECTION").min(0).doc_non_optional(true)
);
}
string get_doc() const override {
return "Display or change program configuration settings from the target device(s) or file.";
}
};
#if HAS_LIBUSB
struct verify_command : public cmd {
verify_command() : cmd("verify") {}
bool execute(device_map &devices) override;
group get_cli() override {
return (
device_selection % "Target device selection" +
file_selection % "The file to compare against" +
(
(option('r', "--range").set(settings.range_set) % "Compare a sub range of memory only" &
hex("from").set(settings.from) % "The lower address bound in hex" &
hex("to").set(settings.to) % "The upper address bound in hex").force_expand_help(true) +
(option('o', "--offset").set(settings.offset_set) % "Specify the load address when comparing with a BIN file" &
hex("offset").set(settings.offset) % "Load offset (memory address; default 0x10000000)").force_expand_help(true)
).min(0).doc_non_optional(true) % "Address options"
);
}
string get_doc() const override {
return "Check that the device contents match those in the file.";
}
};
struct save_command : public cmd {
save_command() : cmd("save") {}
bool execute(device_map &devices) override;
group get_cli() override {
return (
(
option('p', "--program") % "Save the installed program only. This is the default" |
option('a', "--all").doc_non_optional(true).set(settings.save.all) % "Save all of flash memory" |
(
option('r', "--range").set(settings.range_set) % "Save a range of memory. Note that UF2s always store complete 256 byte-aligned blocks of 256 bytes, and the range is expanded accordingly" &
hex("from").set(settings.from) % "The lower address bound in hex" &
hex("to").set(settings.to) % "The upper address bound in hex"
).min(0).doc_non_optional(true)
).min(0).doc_non_optional(true).no_match_beats_error(false) % "Selection of data to save" +
( // note this parenthesis seems to help with error messages for say save --foo
device_selection % "Source device selection" +
file_selection % "File to save to"
)
);
}
string get_doc() const override {
return "Save the program / memory stored in flash on the device to a file.";
}
};
struct load_command : public cmd {
load_command() : cmd("load") {}
bool execute(device_map &devices) override;
group get_cli() override {
return (
(
option("--ignore-partitions").set(settings.load.ignore_pt) % "When writing flash data, ignore the partition table and write to absolute space" +
(option("--family") % "Specify the family ID of the file to load" &
family_id("family_id").set(settings.family_id) % "family id to use for load").force_expand_help(true) +
(option('p', "--partition") % "Specify the partition to load into" &
integer("partition").set(settings.load.partition) % "partition to load into").force_expand_help(true) +
option('n', "--no-overwrite").set(settings.load.no_overwrite) % "When writing flash data, do not overwrite an existing program in flash. If picotool cannot determine the size/presence of the program in flash, the command fails" +
option('N', "--no-overwrite-unsafe").set(settings.load.no_overwrite_force) % "When writing flash data, do not overwrite an existing program in flash. If picotool cannot determine the size/presence of the program in flash, the load continues anyway" +
option('u', "--update").set(settings.load.update) % "Skip writing flash sectors that already contain identical data" +
option('v', "--verify").set(settings.load.verify) % "Verify the data was written correctly" +
option('x', "--execute").set(settings.load.execute) % "Attempt to execute the downloaded file as a program after the load"
).min(0).doc_non_optional(true) % "Post load actions" +
file_selection % "File to load from" +
(
option('o', "--offset").set(settings.offset_set) % "Specify the load address for a BIN file" &
hex("offset").set(settings.offset) % "Load offset (memory address; default 0x10000000)"
).force_expand_help(true) % "BIN file options" +
device_selection % "Target device selection"
);
}
string get_doc() const override {
return "Load the program / memory range stored in a file onto the device.";
}
};
#endif
#if HAS_MBEDTLS
struct encrypt_command : public cmd {
encrypt_command() : cmd("encrypt") {}
bool execute(device_map &devices) override;
virtual device_support get_device_support() override { return none; }
group get_cli() override {
return (
option("--quiet").set(settings.quiet) % "Don't print any output" +
option("--verbose").set(settings.verbose) % "Print verbose output" +
(
option("--hash").set(settings.seal.hash) % "Hash the encrypted file" +
option("--sign").set(settings.seal.sign) % "Sign the encrypted file"
).min(0).doc_non_optional(true) % "Signing Configuration" +
named_file_selection_x("infile", 0) % "File to load from" +
(
option('o', "--offset").set(settings.offset_set) % "Specify the load address for a BIN file" &
hex("offset").set(settings.offset) % "Load offset (memory address; default 0x10000000)"
).force_expand_help(true) % "BIN file options" +
named_file_selection_x("outfile", 1) % "File to save to" +
named_typed_file_selection_x("aes_key", 2, "bin") % "AES Key" +
optional_typed_file_selection_x("signing_key", 3, "pem") % "Signing Key file"
);
}
string get_doc() const override {
return "Encrypt the program.";
}
};
struct seal_command : public cmd {
seal_command() : cmd("seal") {}
bool execute(device_map &devices) override;
virtual device_support get_device_support() override { return none; }
group get_cli() override {
return (
option("--quiet").set(settings.quiet) % "Don't print any output" +
option("--verbose").set(settings.verbose) % "Print verbose output" +
(
option("--hash").set(settings.seal.hash) % "Hash the file" +
option("--sign").set(settings.seal.sign) % "Sign the file" +
option("--clear").set(settings.seal.clear_sram) % "Clear all of SRAM on load"
).min(0).doc_non_optional(true) % "Configuration" +
named_file_selection_x("infile", 0) % "File to load from" +
(
option('o', "--offset").set(settings.offset_set) % "Specify the load address for a BIN file" &
hex("offset").set(settings.offset) % "Load offset (memory address; default 0x10000000)"
).force_expand_help(true) % "BIN file options" +
named_file_selection_x("outfile", 1) % "File to save to" +
optional_typed_file_selection_x("key", 2, "pem") % "Key file" +
optional_typed_file_selection_x("otp", 3, "json") % "File to save OTP to (will edit existing file if it exists)" +
(
option("--major") &
integer("major").set(settings.seal.major_version)
).min(0) % "Add Major Version" +
(
option("--minor") &
integer("minor").set(settings.seal.minor_version)
).min(0) % "Add Minor Version" +
(
option("--rollback") &
integer("rollback").set(settings.seal.rollback_version) +
hex("rows").add_to(settings.seal.rollback_rows).min(0).repeatable()
).min(0) % "Add Rollback Version"
);
}
string get_doc() const override {
return "Add final metadata to a binary, optionally including a hash and/or signature.";
}
};
#endif
struct link_command : public cmd {
link_command() : cmd("link") {}
bool execute(device_map &devices) override;
virtual device_support get_device_support() override { return none; }
group get_cli() override {
return (
option("--quiet").set(settings.quiet) % "Don't print any output" +
option("--verbose").set(settings.verbose) % "Print verbose output" +
named_typed_file_selection_x("outfile", 0, "uf2 | bin") % "File to write to" +
named_file_selection_x("infile1", 1) % "Files to link" +
named_file_selection_x("infile2", 2) % "Files to link" +
optional_file_selection_x("infile3", 3) % "Files to link" +
option('p', "--pad") & hex("pad").set(settings.link.align) % "Specify alignment to pad to, defaults to 0x1000"
);
}
string get_doc() const override {
return "Link multiple binaries into one block loop.";
}
};
#if HAS_LIBUSB
struct partition_info_command : public cmd {
partition_info_command() : cmd("info") {}
bool execute(device_map &devices) override;
group get_cli() override {
return (
(option('m', "--family") & family_id("family_id").set(settings.family_id)) % "family id (will show target partition for said family)" +
device_selection % "Target device selection"
);
}
string get_doc() const override {
return "Print the device's partition table.";
}
void print_permissions(unsigned int p) const;
void insert_default_families(uint32_t flags_and_permissions, vector<std::string> &family_ids) const;
};
#endif
struct partition_create_command : public cmd {
partition_create_command() : cmd("create") {}
bool execute(device_map &devices) override;
virtual device_support get_device_support() override { return none; }
group get_cli() override {
return (
option("--quiet").set(settings.quiet) % "Don't print any output" +
option("--verbose").set(settings.verbose) % "Print verbose output" +
named_typed_file_selection_x("infile", 0, "json") % "partition table JSON" +
(named_file_selection_x("outfile", 1) % "output file" +
(
(option('o', "--offset").set(settings.offset_set) % "Specify the load address for UF2 file output" &
hex("offset").set(settings.offset) % "Load offset (memory address; default 0x10000000)").force_expand_help(true) +
(option("--family") % "Specify the family if for UF2 file output" &
family_id("family_id").set(settings.family_id) % "family id for UF2 (default absolute)").force_expand_help(true)
).min(0).force_expand_help(true) % "UF2 output options") +
optional_typed_file_selection_x("bootloader", 2, "elf") % "embed partition table into bootloader ELF" +
(
// todo why doesn't this set settings.partition.sign?
((option("--sign").set(settings.partition.sign) & value("keyfile").with_exclusion_filter([](const string &value) {
return value.find_first_of('-') == 0;
}).set(settings.filenames[3])) % "The file name" +
named_file_types_x("pem", 3)) % "Sign the partition table" +
(option("--no-hash").clear(settings.partition.hash) % "Don't hash the partition table") +
(option("--singleton").set(settings.partition.singleton) % "Singleton partition table")
).min(0).force_expand_help(true) % "Partition Table Options"
#if SUPPORT_A2
+ (
option("--abs-block").set(settings.uf2.abs_block) % "Enforce support for an absolute block" +
hex("abs_block_loc").set(settings.uf2.abs_block_loc).min(0) % "absolute block location (default to 0x10ffff00)"
).force_expand_help(true).min(0) % "Errata RP2350-E9 Fix"
#endif
);
}
string get_doc() const override {
return "Create a partition table from json";
}
void print_permissions(unsigned int p) const;
void insert_default_families(uint32_t flags_and_permissions, vector<std::string> &family_ids) const;
};
vector<std::shared_ptr<cmd>> partition_sub_commands {
#if HAS_LIBUSB
std::shared_ptr<cmd>(new partition_info_command()),
#endif
std::shared_ptr<cmd>(new partition_create_command()),
};
struct partition_command : public multi_cmd {
partition_command() : multi_cmd("partition", partition_sub_commands) {}
string get_doc() const override {
return "Commands related to RP2350 Partition Tables";
}
};
struct otp_list_command : public cmd {
otp_list_command() : cmd("list") {}
bool execute(device_map& devices) override;
virtual device_support get_device_support() override { return none; }
group get_cli() override {
return (
(
option('p', "--pages").set(settings.otp.list_pages) % "Show page number/page row number" +
option('n', "--no-descriptions").set(settings.otp.list_no_descriptions) % "Don't show descriptions" +
(option('i', "--include") & value("filename").add_to(settings.otp.extra_files)).min(0).max(1) % "Include extra otp definition" + // todo more than 1
(value("selector").add_to(settings.otp.selectors) %
"The row/field selector, each of which can select a whole row:\n\n" \
"ROW_NAME to select a whole row by name.\n" \
"ROW_NUMBER to select a whole row by number.\n" \
"PAGE:PAGE_ROW_NUMBER to select a whole row by page and number within page.\n\n" \
"... or can select a single field/subset of a row (where REG_SEL is one of the above row selectors):\n\n"
"REG_SEL.FIELD_NAME to select a field within a row by name.\n" \
"REG_SEL.n-m to select a range of bits within a row.\n" \
"REG_SEL.n to select a single bit within a row.\n" \
".FIELD_NAME to select any row's field by name.\n\n" \
".. or can selected multiple rows by using blank or '*' for PAGE or PAGE_ROW_NUMBER").repeatable().min(0)
) % "Row/Field Selection"
);
}
string get_doc() const override {
return "List matching known registers/fields";
}
};
#if HAS_LIBUSB
struct otp_get_command : public cmd {
otp_get_command() : cmd("get") {}
bool execute(device_map& devices) override;
virtual bool requires_rp2350() const override { return true; }
group get_cli() override {
return (
(
(option('c', "--copies") & integer("copies").min(1).set(settings.otp.redundancy)) % "Read multiple redundant values" +
option('r', "--raw").set(settings.otp.raw) % "Get raw 24 bit values" +
option('e', "--ecc").set(settings.otp.ecc) % "Use error correction" +
option('n', "--no-descriptions").set(settings.otp.list_no_descriptions) % "Don't show descriptions" +
(option('i', "--include") & value("filename").add_to(settings.otp.extra_files)).min(0).max(1) % "Include extra otp definition" // todo more than 1
).min(0).doc_non_optional(true) % "Row/field options" +
(
device_selection % "Target device selection"
).major_group("TARGET SELECTION").min(0).doc_non_optional(true) +
(
option('z', "--fuzzy").set(settings.otp.fuzzy) % "Allow fuzzy name searches in selector vs exact match" +
(value("selector").add_to(settings.otp.selectors) %
"The row/field selector, each of which can select a whole row:\n\n" \
"ROW_NAME to select a whole row by name.\n" \
"ROW_NUMBER to select a whole row by number.\n" \
"PAGE:PAGE_ROW_NUMBER to select a whole row by page and number within page.\n\n" \
"... or can select a single field/subset of a row (where REG_SEL is one of the above row selectors):\n\n"
"REG_SEL.FIELD_NAME to select a field within a row by name.\n" \
"REG_SEL.n-m to select a range of bits within a row.\n" \
"REG_SEL.n to select a single bit within a row.\n" \
".FIELD_NAME to select any row's field by name.\n\n" \
".. or can selected multiple rows by using blank or '*' for PAGE or PAGE_ROW_NUMBER").repeatable().min(0)
) % "Row/Field Selection"
);
}
string get_doc() const override {
return "Get the value of one or more OTP registers/fields";
}
};
// possible temporary
struct otp_dump_command : public cmd {
otp_dump_command() : cmd("dump") {}
bool execute(device_map& devices) override;
virtual bool requires_rp2350() const override { return true; }
group get_cli() override {
return (
(
option('r', "--raw").set(settings.otp.raw) % "Get raw 24 bit values" +
option('e', "--ecc").set(settings.otp.ecc) % "Use error correction"
).min(0).doc_non_optional(true) % "Row/field options" +
(
device_selection % "Target device selection"
).major_group("TARGET SELECTION").min(0).doc_non_optional(true)
);
}
string get_doc() const override {
return "Dump entire OTP";
}
};
struct otp_load_command : public cmd {
otp_load_command() : cmd("load") {}
bool execute(device_map &devices) override;
virtual bool requires_rp2350() const override { return true; }
group get_cli() override {
return (
(
option('r', "--raw").set(settings.otp.raw) % "Get raw 24 bit values" +
option('e', "--ecc").set(settings.otp.ecc) % "Use error correction" +
(option('s', "--start_row") & integer("row").set(settings.otp.row)) % "Start row to load at (note use 0x for hex)" +
(option('i', "--include") & value("filename").add_to(settings.otp.extra_files)).min(0).max(1) % "Include extra otp definition" // todo more than 1
).min(0).doc_non_optional(true) % "Row options" +
named_typed_file_selection_x("filename", 0, "json | bin") % "File to load row(s) from" +
device_selection % "Target device selection"
);
}
string get_doc() const override {