forked from Xilinx/bootgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdo-binary.c
executable file
·1709 lines (1674 loc) · 63.4 KB
/
cdo-binary.c
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 2019-2020 Xilinx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include "cdo-binary.h"
static uint32_t verbose;
static uint32_t auto_align;
#define u32xe(v) (be ? u32be(v) : u32le(v))
#define u32xe_lo(v) u32xe((uint32_t)(v))
#define u32xe_hi(v) u32xe((uint32_t)(v >> 32))
#define u32pair(hi, lo) (((uint64_t)(hi) << 32) | (lo))
#define HDR_IDN_WRD (0x584C4E58U)
enum {
/*General Commands */
CMD1_MASK_POLL = 0x101U,
CMD1_MASK_WRITE = 0x102U,
CMD1_WRITE = 0x103U,
CMD1_DELAY = 0x104U,
CMD1_DMA_WRITE = 0x105U,
CMD1_MASK_POLL64 = 0x106U,
CMD1_MASK_WRITE64 = 0x107U,
CMD1_WRITE64 = 0x108U,
CMD1_DMA_XFER = 0x109U,
/* NPI Commands */
CMD1_NPI_SEQ = 0x201U,
CMD1_NPI_PRECFG = 0x202U,
CMD1_NPI_WRITE = 0x203U,
CMD1_NPI_SHUTDN = 0x204U,
/* CFU Commands */
CMD1_CFU_SET_CRC32 = 0x301U,
CMD1_CFU_DECOMPRESS = 0x302U,
CMD1_CFU_CRAM_RW = 0x303U,
CMD1_CFU_SEU_GO = 0x304U,
CMD1_CFU_CRC8_DIS = 0x305U,
CMD1_CFU_SSI_PER_SLR_PR = 0x306U,
CMD1_CFU_GSR_GSC = 0x307U,
CMD1_CFU_GCAP_CLK_EN = 0x308U,
CMD1_CFU_CFRAME_READ = 0x309U,
CMD1_END = 0xffffffffU,
};
enum {
/*General Commands */
CMD2_END_MARK = 0x100U,
CMD2_MASK_POLL = 0x101U,
CMD2_MASK_WRITE = 0x102U,
CMD2_WRITE = 0x103U,
CMD2_DELAY = 0x104U,
CMD2_DMA_WRITE = 0x105U,
CMD2_MASK_POLL64 = 0x106U,
CMD2_MASK_WRITE64 = 0x107U,
CMD2_WRITE64 = 0x108U,
CMD2_DMA_XFER = 0x109U,
CMD2_INIT_SEQ = 0x10aU,
CMD2_CFRAME_READ = 0x10bU,
CMD2_SET = 0x10cU,
CMD2_DMA_WRITE_KEYHOLE = 0x10dU,
CMD2_SSIT_SYNC_MASTER = 0x10eU,
CMD2_SSIT_SYNC_SLAVES = 0x10fU,
CMD2_SSIT_WAIT_SLAVES = 0x110U,
CMD2_NOP = 0x111U,
CMD2_GET_DEVICE_ID = 0x112U,
CMD2_EVENT_LOGGING = 0x113U,
CMD2_SET_BOARD = 0x114U,
CMD2_GET_BOARD = 0x115U,
CMD2_SET_PLM_WDT = 0x116U,
/* PM Commands */
CMD2_PM_GET_API_VERSION = 0x201U,
CMD2_PM_GET_DEVICE_STATUS = 0x203U,
CMD2_PM_GET_OP_CHARACTERISTIC = 0x204U,
CMD2_PM_REGISTER_NOTIFIER = 0x205U,
CMD2_PM_REQUEST_SUSPEND = 0x206U,
CMD2_PM_SELF_SUSPEND = 0x207U,
CMD2_PM_FORCE_POWERDOWN = 0x208U,
CMD2_PM_ABORT_SUSPEND = 0x209U,
CMD2_PM_REQUEST_WAKEUP = 0x20aU,
CMD2_PM_SET_WAKEUP_SOURCE = 0x20bU,
CMD2_PM_SYSTEM_SHUTDOWN = 0x20cU,
CMD2_PM_REQUEST_DEVICE = 0x20dU,
CMD2_PM_RELEASE_DEVICE = 0x20eU,
CMD2_PM_SET_REQUIREMENT = 0x20fU,
CMD2_PM_SET_MAX_LATENCY = 0x210U,
CMD2_PM_RESET_ASSERT = 0x211U,
CMD2_PM_RESET_GET_STATUS = 0x212U,
CMD2_PM_PINCTRL_REQUEST = 0x21cU,
CMD2_PM_PINCTRL_RELEASE = 0x21dU,
CMD2_PM_PINCTRL_GET_FUNCTION = 0x21eU,
CMD2_PM_PINCTRL_SET_FUNCTION = 0x21fU,
CMD2_PM_PINCTRL_CONFIG_PARAM_GET = 0x220U,
CMD2_PM_PINCTRL_CONFIG_PARAM_SET = 0x221U,
CMD2_PM_IOCTL = 0x222U,
CMD2_PM_QUERY_DATA = 0x223U,
CMD2_PM_CLOCK_ENABLE = 0x224U,
CMD2_PM_CLOCK_DISABLE = 0x225U,
CMD2_PM_CLOCK_GETSTATE = 0x226U,
CMD2_PM_CLOCK_SETDIVIDER = 0x227U,
CMD2_PM_CLOCK_GETDIVIDER = 0x228U,
CMD2_PM_CLOCK_SETRATE = 0x229U,
CMD2_PM_CLOCK_GETRATE = 0x22aU,
CMD2_PM_CLOCK_SETPARENT = 0x22bU,
CMD2_PM_CLOCK_GETPARENT = 0x22cU,
CMD2_PM_PLL_SET_PARAMETER = 0x230U,
CMD2_PM_PLL_GET_PARAMETER = 0x231U,
CMD2_PM_PLL_SET_MODE = 0x232U,
CMD2_PM_PLL_GET_MODE = 0x233U,
CMD2_PM_ADD_SUBSYSTEM = 0x236U,
CMD2_PM_DESTROY_SUBSYSTEM = 0x237U,
CMD2_PM_DESCRIBE_NODES = 0x238U,
CMD2_PM_ADD_NODE = 0x239U,
CMD2_PM_ADD_NODE_PARENT = 0x23aU,
CMD2_PM_ADD_NODE_NAME = 0x23bU,
CMD2_PM_ADD_REQUIREMENT = 0x23cU,
CMD2_PM_SET_CURRENT_SUBSYSTEM = 0x23dU,
CMD2_PM_INIT_NODE = 0x23eU,
CMD2_PM_FEATURE_CHECK = 0x23fU,
CMD2_PM_ISO_CONTROL = 0x240U,
/* NPI Commands */
CMD2_NPI_SEQ = 0x301U,
CMD2_NPI_PRECFG = 0x302U,
CMD2_NPI_WRITE = 0x303U,
CMD2_NPI_SHUTDN = 0x304U,
/* CFU Commands */
CMD2_CFU_SET_CRC32 = 0x401U,
CMD2_CFU_DECOMPRESS = 0x402U,
CMD2_CFU_CRAM_RW = 0x403U,
CMD2_CFU_SEU_GO = 0x404U,
CMD2_CFU_CRC8_DIS = 0x405U,
CMD2_CFU_SSI_PER_SLR_PR = 0x406U,
CMD2_CFU_GSR_GSC = 0x407U,
CMD2_CFU_GCAP_CLK_EN = 0x408U,
CMD2_CFU_CFI_TYPE = 0x409U,
/* Loader Commands */
CMD2_LDR_SET_IMAGE_INFO = 0x704U,
/* EM Commands */
CMD2_EM_SET_ACTION = 0x801U,
CMD2_END = 0xffffffffU
};
void cdobinary_set_verbose(uint32_t level) {
verbose = level;
}
void cdobinary_set_auto_align(uint32_t enable) {
auto_align = enable;
}
static void byte_swap_buffer(uint32_t * p, uint32_t count, uint32_t be) {
if (is_be_host() != be) {
uint32_t i;
for (i = 0; i < count; i++) {
p[i] = u32swap(p[i]);
}
}
}
static uint32_t decode_v1(CdoSequence * seq, uint32_t * p, uint32_t l, uint32_t be) {
uint32_t i = u32xe(p[0]) + 1;
uint32_t sections = u32xe(p[3]);
uint32_t secno = 0;
while (secno < sections && i + 3 <= l) {
uint32_t secid = u32xe(p[i++]);
uint32_t seclen = u32xe(p[i++]);
uint32_t secend = l;
uint32_t seccmds = u32xe(p[i++]);
uint32_t cmdno = 0;
if (verbose) {
fprintf(stderr, "info: decoding section %#"PRIx32" len %"PRIu32" cmds %"PRIu32" at %"PRIu32"\n", secid, seclen, seccmds, i - 3);
}
cdocmd_add_section(seq, secid);
while (i < l && cmdno < seccmds) {
uint32_t id = u32xe(p[i++]);
uint32_t args = 0;
if (verbose) {
fprintf(stderr, "info: decoding command %#"PRIx32" at %"PRIu32"\n", id, i - 1);
}
switch (id) {
case CMD1_MASK_POLL:
args = 4;
if (secend - i < args) break;
cdocmd_add_mask_poll(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]), u32xe(p[i+3])*1000, 0);
break;
case CMD1_MASK_WRITE:
args = 3;
if (secend - i < args) break;
cdocmd_add_mask_write(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]));
break;
case CMD1_WRITE:
args = 2;
if (secend - i < args) break;
cdocmd_add_write(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD1_DELAY:
args = 1;
if (secend - i < args) break;
cdocmd_add_delay(seq, u32xe(p[i+0])*1000);
break;
case CMD1_DMA_WRITE:
args = 3;
if (secend - i < args) break;
args += u32xe(p[i+1]) + u32xe(p[i+2]);
if (secend - i < args) break;
cdocmd_add_block_write(seq, u32xe(p[i+0]), u32xe(p[i+1]), &p[i+3+u32xe(p[i+2])], be);
break;
case CMD1_MASK_POLL64:
args = 5;
if (secend - i < args) break;
cdocmd_add_mask_poll(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32xe(p[i+2]), u32xe(p[i+3]), u32xe(p[i+4])*1000, 0);
break;
case CMD1_MASK_WRITE64:
args = 4;
if (secend - i < args) break;
cdocmd_add_mask_write(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32xe(p[i+2]), u32xe(p[i+3]));
break;
case CMD1_WRITE64:
args = 3;
if (secend - i < args) break;
cdocmd_add_write(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32xe(p[i+2]));
break;
case CMD1_DMA_XFER:
args = 6;
if (secend - i < args) break;
if (u32xe(p[i+0]) == 0 && u32xe(p[i+1]) == 0) {
args += u32xe(p[i+4]);
if (secend - i < args) break;
cdocmd_add_dma_xfer(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32pair(u32xe(p[i+2]), u32xe(p[i+3])), u32xe(p[i+4]), u32xe(p[i+5]), &p[i+6], be);
} else {
cdocmd_add_dma_xfer(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32pair(u32xe(p[i+2]), u32xe(p[i+3])), u32xe(p[i+4]), u32xe(p[i+5]), NULL, be);
}
break;
case CMD1_NPI_SEQ:
args = 2;
if (secend - i < args) break;
cdocmd_add_npi_seq(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD1_NPI_PRECFG:
args = 2;
if (secend - i < args) break;
cdocmd_add_npi_precfg(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD1_NPI_WRITE:
args = 3;
if (secend - i < args) break;
args += u32xe(p[i+2]);
if (secend - i < args) break;
cdocmd_add_npi_write(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]), &p[i+3], be);
break;
case CMD1_NPI_SHUTDN:
args = 2;
if (secend - i < args) break;
cdocmd_add_npi_shutdown(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD1_CFU_SET_CRC32:
args = 1;
if (secend - i < args) break;
if (u32xe(p[i+0]) == 0) {
cdocmd_add_cfu_set_crc32(seq, 0, 0);
break;
}
args = 2;
if (secend - i < args) break;
cdocmd_add_cfu_set_crc32(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD1_CFU_DECOMPRESS:
args = 1;
if (secend - i < args) break;
cdocmd_add_cfu_decompress(seq, u32xe(p[i+0]));
break;
case CMD1_CFU_CRAM_RW:
args = 1;
if (secend - i < args) break;
cdocmd_add_cfu_cram_rw(seq, u32xe(p[i+0]));
break;
case CMD1_CFU_SEU_GO:
args = 1;
if (secend - i < args) break;
cdocmd_add_cfu_seu_go(seq, u32xe(p[i+0]));
break;
case CMD1_CFU_CRC8_DIS:
args = 1;
if (secend - i < args) break;
cdocmd_add_cfu_crc8_dis(seq, u32xe(p[i+0]));
break;
case CMD1_CFU_SSI_PER_SLR_PR:
args = 1;
if (secend - i < args) break;
cdocmd_add_cfu_ssi_per_slr_pr(seq, u32xe(p[i+0]));
break;
case CMD1_CFU_GSR_GSC:
args = 1;
if (secend - i < args) break;
cdocmd_add_cfu_gsr_gsc(seq, u32xe(p[i+0]));
break;
case CMD1_CFU_GCAP_CLK_EN:
args = 1;
if (secend - i < args) break;
cdocmd_add_cfu_gcap_clk_en(seq, u32xe(p[i+0]));
break;
case CMD1_CFU_CFRAME_READ:
args = 20;
if (secend - i < args) break;
cdocmd_add_cframe_read(seq, u32xe(p[i+0]), u32pair(u32xe(p[i+1]), u32xe(p[i+2])), u32xe(p[i+3]), 16, &p[i+4], be);
break;
case CMD1_END:
goto end_of_section;
default:
fprintf(stderr, "unknown command %"PRIx32"\n", id);
return 1;
}
if (secend - i < args) {
fprintf(stderr, "incomplete command payload %"PRIu32", expected %"PRIu32"\n", secend - i, args);
return 1;
}
i += args;
cmdno++;
}
if (cmdno < seccmds) {
fprintf(stderr, "incomplete command count %"PRIu32", expected %"PRIu32"\n", cmdno, seccmds);
return 1;
}
end_of_section:
secno++;
}
if (secno < sections) {
fprintf(stderr, "incomplete section count %"PRIu32", expected %"PRIu32"\n", secno, sections);
return 1;
}
if (i < l) {
fprintf(stderr, "warning: mismatching CDO word count %"PRIu32", expected %"PRIu32"\n", i, l);
}
return 0;
}
static uint32_t decode_v2(CdoSequence * seq, uint32_t * p, uint32_t l, uint32_t be) {
uint32_t hdrlen = u32xe(p[0]);
uint32_t len = u32xe(p[3]);
uint32_t total_len = hdrlen + 1 + len;
uint32_t i = hdrlen + 1;
uint32_t cmdpos = 0;
uint32_t hdr;
if (l < total_len) {
fprintf(stderr, "warning: incomplete CDO buffer %"PRIu32", expected %"PRIu32"\n", l, total_len);
total_len = l;
} else if (l > total_len) {
l = total_len;
}
while (i < l) {
uint32_t args;
cmdpos = i;
hdr = u32xe(p[i++]);
if (verbose) {
fprintf(stderr, "info: decoding command %#"PRIx32" at %"PRIu32"\n", hdr, cmdpos);
}
args = (hdr >> 16) & 255;
if (args == 255) {
if (hdr == CMD2_END) goto found_end_marker;
if (i >= l) {
fprintf(stderr, "incomplete CDO command %#"PRIx32" at %"PRIu32"\n", hdr, cmdpos);
return 1;
}
args = u32xe(p[i++]);
}
if (l - i < args) {
fprintf(stderr, "incomplete CDO command %#"PRIx32" at %"PRIu32"\n", hdr, cmdpos);
return 1;
}
switch (hdr & 0xffff) {
case CMD2_END_MARK:
goto found_end_marker;
case CMD2_MASK_POLL:
if (args == 4) {
cdocmd_add_mask_poll(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]), u32xe(p[i+3]), 0);
} else if (args == 5) {
cdocmd_add_mask_poll(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]), u32xe(p[i+3]), u32xe(p[i+4]));
} else {
goto unexpected;
}
break;
case CMD2_MASK_WRITE:
if (args != 3) goto unexpected;
cdocmd_add_mask_write(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]));
break;
case CMD2_WRITE:
if (args != 2) goto unexpected;
cdocmd_add_write(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_DELAY:
if (args != 1) goto unexpected;
cdocmd_add_delay(seq, u32xe(p[i+0]));
break;
case CMD2_DMA_WRITE:
if (args < 2) goto unexpected;
cdocmd_add_block_write(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), args - 2, &p[i+2], be);
break;
case CMD2_DMA_WRITE_KEYHOLE:
if (args < 3) goto unexpected;
cdocmd_add_keyhole_write(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32xe(p[i+2]), args - 3, &p[i+3], be);
break;
case CMD2_SSIT_SYNC_MASTER:
if (args != 0) goto unexpected;
cdocmd_add_ssit_sync_master(seq);
break;
case CMD2_SSIT_SYNC_SLAVES:
if (args != 2) goto unexpected;
cdocmd_add_ssit_sync_slaves(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_SSIT_WAIT_SLAVES:
if (args != 2) goto unexpected;
cdocmd_add_ssit_wait_slaves(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_MASK_POLL64:
if (args == 5) {
cdocmd_add_mask_poll(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32xe(p[i+2]), u32xe(p[i+3]), u32xe(p[i+4]), 0);
} else if (args == 6) {
cdocmd_add_mask_poll(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32xe(p[i+2]), u32xe(p[i+3]), u32xe(p[i+4]), u32xe(p[i+5]));
} else {
goto unexpected;
}
break;
case CMD2_MASK_WRITE64:
if (args != 4) goto unexpected;
cdocmd_add_mask_write(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32xe(p[i+2]), u32xe(p[i+3]));
break;
case CMD2_WRITE64:
if (args != 3) goto unexpected;
cdocmd_add_write(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32xe(p[i+2]));
break;
case CMD2_DMA_XFER:
if (args != 6) goto unexpected;
cdocmd_add_dma_xfer(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32pair(u32xe(p[i+2]), u32xe(p[i+3])), u32xe(p[i+4]), u32xe(p[i+5]), NULL, be);
break;
case CMD2_CFRAME_READ:
if (args < 4) goto unexpected;
cdocmd_add_cframe_read(seq, u32xe(p[i+0]), u32pair(u32xe(p[i+1]), u32xe(p[i+2])), u32xe(p[i+3]), args - 4, &p[i+4], be);
break;
case CMD2_SET:
if (args != 4) goto unexpected;
cdocmd_add_block_set(seq, u32pair(u32xe(p[i+0]), u32xe(p[i+1])), u32xe(p[i+2]), u32xe(p[i+3]));
break;
case CMD2_NOP:
cdocmd_add_nop(seq, args, &p[i+0], be);
break;
case CMD2_EVENT_LOGGING:
if (args < 1) goto unexpected;
cdocmd_add_event_logging(seq, u32xe(p[i+0]), args - 1, &p[i+1], be);
break;
case CMD2_SET_BOARD: {
uint32_t * tmpbuf;
tmpbuf = malloc((args + 1) * sizeof *tmpbuf);
memcpy(tmpbuf, &p[i+0], args * sizeof *tmpbuf);
tmpbuf[args] = 0;
byte_swap_buffer(tmpbuf, args, be);
cdocmd_add_set_board(seq, (char *)tmpbuf);
free(tmpbuf);
break;
}
case CMD2_SET_PLM_WDT:
if (args != 2) goto unexpected;
cdocmd_add_set_plm_wdt(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_NPI_SEQ:
if (args != 2) goto unexpected;
cdocmd_add_npi_seq(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_NPI_PRECFG:
if (args != 2) goto unexpected;
cdocmd_add_npi_precfg(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_NPI_WRITE:
if (args < 2) goto unexpected;
cdocmd_add_npi_write(seq, u32xe(p[i+0]), u32xe(p[i+1]), args - 2, &p[i+2], be);
break;
case CMD2_NPI_SHUTDN:
if (args != 2) goto unexpected;
cdocmd_add_npi_shutdown(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_GET_API_VERSION:
if (args != 0) goto unexpected;
cdocmd_add_pm_get_api_version(seq);
break;
case CMD2_PM_GET_DEVICE_STATUS:
if (args != 1) goto unexpected;
cdocmd_add_pm_get_device_status(seq, u32xe(p[i+0]));
break;
case CMD2_PM_REGISTER_NOTIFIER:
if (args != 4) goto unexpected;
cdocmd_add_pm_register_notifier(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]), u32xe(p[i+3]));
break;
case CMD2_PM_REQUEST_SUSPEND:
if (args != 4) goto unexpected;
cdocmd_add_pm_request_suspend(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]), u32xe(p[i+3]));
break;
case CMD2_PM_SELF_SUSPEND:
if (args != 5) goto unexpected;
cdocmd_add_pm_self_suspend(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]), u32pair(u32xe(p[i+4]), u32xe(p[i+3])));
break;
case CMD2_PM_FORCE_POWERDOWN:
if (args != 2) goto unexpected;
cdocmd_add_pm_force_powerdown(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_ABORT_SUSPEND:
if (args != 2) goto unexpected;
cdocmd_add_pm_abort_suspend(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_REQUEST_WAKEUP:
if (args != 4) goto unexpected;
cdocmd_add_pm_request_wakeup(seq, u32xe(p[i+0]), u32pair(u32xe(p[i+2]), u32xe(p[i+1])), u32xe(p[i+3]));
break;
case CMD2_PM_SET_WAKEUP_SOURCE:
if (args != 3) goto unexpected;
cdocmd_add_pm_set_wakeup_source(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]));
break;
case CMD2_PM_SYSTEM_SHUTDOWN:
if (args != 2) goto unexpected;
cdocmd_add_pm_system_shutdown(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_REQUEST_DEVICE:
if (args != 4) goto unexpected;
cdocmd_add_pm_request_device(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]), u32xe(p[i+3]));
break;
case CMD2_PM_RELEASE_DEVICE:
if (args != 1) goto unexpected;
cdocmd_add_pm_release_device(seq, u32xe(p[i+0]));
break;
case CMD2_PM_SET_REQUIREMENT:
if (args != 4) goto unexpected;
cdocmd_add_pm_set_requirement(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]), u32xe(p[i+3]));
break;
case CMD2_PM_SET_MAX_LATENCY:
if (args != 2) goto unexpected;
cdocmd_add_pm_set_max_latency(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_RESET_ASSERT:
if (args != 2) goto unexpected;
cdocmd_add_pm_reset_assert(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_RESET_GET_STATUS:
if (args != 1) goto unexpected;
cdocmd_add_pm_reset_get_status(seq, u32xe(p[i+0]));
break;
case CMD2_PM_PINCTRL_REQUEST:
if (args != 1) goto unexpected;
cdocmd_add_pm_pinctrl_request(seq, u32xe(p[i+0]));
break;
case CMD2_PM_PINCTRL_RELEASE:
if (args != 1) goto unexpected;
cdocmd_add_pm_pinctrl_release(seq, u32xe(p[i+0]));
break;
case CMD2_PM_PINCTRL_GET_FUNCTION:
if (args != 1) goto unexpected;
cdocmd_add_pm_pinctrl_get_function(seq, u32xe(p[i+0]));
break;
case CMD2_PM_PINCTRL_SET_FUNCTION:
if (args != 2) goto unexpected;
cdocmd_add_pm_pinctrl_set_function(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_PINCTRL_CONFIG_PARAM_GET:
if (args != 2) goto unexpected;
cdocmd_add_pm_pinctrl_config_param_get(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_PINCTRL_CONFIG_PARAM_SET:
if (args != 3) goto unexpected;
cdocmd_add_pm_pinctrl_config_param_set(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]));
break;
case CMD2_PM_IOCTL:
if (args < 2) goto unexpected;
cdocmd_add_pm_ioctl(seq, u32xe(p[i+0]), u32xe(p[i+1]), &p[i+2], args-2, be);
break;
case CMD2_PM_QUERY_DATA:
if (args < 1) goto unexpected;
cdocmd_add_pm_query_data(seq, u32xe(p[i+0]), &p[i+1], args-1, be);
break;
case CMD2_PM_CLOCK_ENABLE:
if (args != 1) goto unexpected;
cdocmd_add_pm_clock_enable(seq, u32xe(p[i+0]));
break;
case CMD2_PM_CLOCK_DISABLE:
if (args != 1) goto unexpected;
cdocmd_add_pm_clock_disable(seq, u32xe(p[i+0]));
break;
case CMD2_PM_CLOCK_GETSTATE:
if (args != 1) goto unexpected;
cdocmd_add_pm_clock_getstate(seq, u32xe(p[i+0]));
break;
case CMD2_PM_CLOCK_SETDIVIDER:
if (args != 2) goto unexpected;
cdocmd_add_pm_clock_setdivider(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_CLOCK_GETDIVIDER:
if (args != 1) goto unexpected;
cdocmd_add_pm_clock_getdivider(seq, u32xe(p[i+0]));
break;
case CMD2_PM_CLOCK_SETRATE:
if (args != 2) goto unexpected;
cdocmd_add_pm_clock_setrate(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_CLOCK_GETRATE:
if (args != 1) goto unexpected;
cdocmd_add_pm_clock_getrate(seq, u32xe(p[i+0]));
break;
case CMD2_PM_CLOCK_SETPARENT:
if (args != 2) goto unexpected;
cdocmd_add_pm_clock_setparent(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_CLOCK_GETPARENT:
if (args != 1) goto unexpected;
cdocmd_add_pm_clock_getparent(seq, u32xe(p[i+0]));
break;
case CMD2_PM_PLL_SET_PARAMETER:
if (args != 3) goto unexpected;
cdocmd_add_pm_pll_set_parameter(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]));
break;
case CMD2_PM_PLL_GET_PARAMETER:
if (args != 2) goto unexpected;
cdocmd_add_pm_pll_get_parameter(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_PLL_SET_MODE:
if (args != 2) goto unexpected;
cdocmd_add_pm_pll_set_mode(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_PM_PLL_GET_MODE:
if (args != 1) goto unexpected;
cdocmd_add_pm_pll_get_mode(seq, u32xe(p[i+0]));
break;
case CMD2_PM_ADD_SUBSYSTEM:
if (args != 1) goto unexpected;
cdocmd_add_pm_add_subsystem(seq, u32xe(p[i+0]));
break;
case CMD2_PM_DESTROY_SUBSYSTEM:
if (args != 1) goto unexpected;
cdocmd_add_pm_destroy_subsystem(seq, u32xe(p[i+0]));
break;
case CMD2_PM_DESCRIBE_NODES:
cdocmd_add_pm_describe_nodes(seq, &p[i+0], args, be);
break;
case CMD2_PM_ADD_NODE:
if (args < 1) goto unexpected;
cdocmd_add_pm_add_node(seq, u32xe(p[i+0]), args - 1, &p[i+1], be);
break;
case CMD2_PM_ADD_NODE_PARENT:
if (args < 2) goto unexpected;
cdocmd_add_pm_add_node_parent(seq, u32xe(p[i+0]), args - 1, &p[i+1], be);
break;
case CMD2_PM_ADD_NODE_NAME: {
uint32_t * tmpbuf;
if (args < 2) goto unexpected;
tmpbuf = malloc(args * sizeof *tmpbuf);
memcpy(tmpbuf, &p[i+1], (args - 1) * sizeof *tmpbuf);
tmpbuf[args - 1] = 0;
byte_swap_buffer(tmpbuf, args - 1, be);
cdocmd_add_pm_add_node_name(seq, u32xe(p[i+0]), (char *)tmpbuf);
free(tmpbuf);
break;
}
case CMD2_PM_ADD_REQUIREMENT:
if (args < 3) goto unexpected;
cdocmd_add_pm_add_requirement(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]), args - 3, &p[i+3], be);
break;
case CMD2_PM_SET_CURRENT_SUBSYSTEM:
if (args != 1) goto unexpected;
cdocmd_add_pm_set_current_subsystem(seq, u32xe(p[i+0]));
break;
case CMD2_PM_INIT_NODE:
if (args < 2) goto unexpected;
cdocmd_add_pm_init_node(seq, u32xe(p[i+0]), u32xe(p[i+1]), args - 2, &p[i+2], be);
break;
case CMD2_PM_FEATURE_CHECK:
if (args != 1) goto unexpected;
cdocmd_add_pm_feature_check(seq, u32xe(p[i+0]));
break;
case CMD2_PM_ISO_CONTROL:
if (args != 2) goto unexpected;
cdocmd_add_pm_iso_control(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_CFU_SET_CRC32:
if (args < 1) goto unexpected;
if (u32xe(p[i+0]) == 0) {
cdocmd_add_cfu_set_crc32(seq, 0, 0);
break;
}
if (args < 2) goto unexpected;
cdocmd_add_cfu_set_crc32(seq, u32xe(p[i+0]), u32xe(p[i+1]));
break;
case CMD2_CFU_DECOMPRESS:
if (args < 1) goto unexpected;
cdocmd_add_cfu_decompress(seq, u32xe(p[i+0]));
break;
case CMD2_CFU_CRAM_RW:
if (args < 1) goto unexpected;
cdocmd_add_cfu_cram_rw(seq, u32xe(p[i+0]));
break;
case CMD2_CFU_SEU_GO:
if (args < 1) goto unexpected;
cdocmd_add_cfu_seu_go(seq, u32xe(p[i+0]));
break;
case CMD2_CFU_CRC8_DIS:
if (args < 1) goto unexpected;
cdocmd_add_cfu_crc8_dis(seq, u32xe(p[i+0]));
break;
case CMD2_CFU_SSI_PER_SLR_PR:
if (args < 1) goto unexpected;
cdocmd_add_cfu_ssi_per_slr_pr(seq, u32xe(p[i+0]));
break;
case CMD2_CFU_GSR_GSC:
if (args < 1) goto unexpected;
cdocmd_add_cfu_gsr_gsc(seq, u32xe(p[i+0]));
break;
case CMD2_CFU_GCAP_CLK_EN:
if (args < 1) goto unexpected;
cdocmd_add_cfu_gcap_clk_en(seq, u32xe(p[i+0]));
break;
case CMD2_CFU_CFI_TYPE:
if (args < 1) goto unexpected;
cdocmd_add_cfu_cfi_type(seq, u32xe(p[i+0]));
break;
case CMD2_EM_SET_ACTION:
if (args != 3) goto unexpected;
cdocmd_add_em_set_action(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]));
break;
case CMD2_LDR_SET_IMAGE_INFO:
if (args != 4) goto unexpected;
cdocmd_add_ldr_set_image_info(seq, u32xe(p[i+0]), u32xe(p[i+1]), u32xe(p[i+2]), u32xe(p[i+3]));
break;
default:
cdocmd_add_generic_command(seq, hdr & 0xffff, p + i, args, be);
break;
}
i += args;
}
if (i < total_len) {
fprintf(stderr, "warning: mismatching CDO word count %"PRIu32", expected %"PRIu32"\n", i, total_len);
}
found_end_marker:
return 0;
unexpected:
fprintf(stderr, "invalid payload size command %#"PRIx32" at %"PRIu32"\n", hdr, cmdpos);
return 1;
}
static uint32_t checksum32(uint32_t * p, uint32_t l, uint32_t be) {
uint32_t csum = 0;
while (l-- > 0) csum += u32xe(*p++);
return ~csum;
}
int is_cdo_data(void * data, size_t size) {
uint32_t * p = (uint32_t *)data;
uint32_t hdrlen = 0;
uint32_t identification;
uint32_t csum;
uint32_t be = 0;
if (size > 4) {
hdrlen = u32xe(p[0]);
if (hdrlen >= 0x10000) {
be = 1;
hdrlen = u32xe(p[0]);
if (hdrlen >= 0x10000) return 0;
}
}
if (hdrlen < 4 || hdrlen >= size/4) return 0;
csum = checksum32(p, hdrlen, be);
if (u32xe(p[hdrlen]) != csum) return 0;
identification = u32xe(p[1]);
if (identification != 0x584C4E58 && identification != 0x004F4443) return 0;
return 1;
}
CdoSequence * decode_cdo_binary(const void * data, size_t size) {
CdoSequence * seq;
uint32_t (*decode)(CdoSequence * seq, uint32_t * p, uint32_t l, uint32_t be) = NULL;
uint32_t * p = (uint32_t *)data;
uint32_t hdrlen = 0;
uint32_t version;
uint32_t identification;
uint32_t csum;
uint32_t be = 0;
if (size > 4) {
hdrlen = u32xe(p[0]);
if (hdrlen >= 0x10000) {
be = 1;
hdrlen = u32xe(p[0]);
if (hdrlen >= 0x10000) {
fprintf(stderr, "invalid header length %#"PRIx32"\n", hdrlen);
return NULL;
}
}
}
if (hdrlen < 4 || hdrlen >= size/4) {
fprintf(stderr, "invalid header length %"PRIu32"\n", hdrlen);
return NULL;
}
csum = checksum32(p, hdrlen, be);
if (u32xe(p[hdrlen]) != csum) {
fprintf(stderr, "invalid header checksum %#"PRIx32"\n", csum);
return NULL;
}
identification = u32xe(p[1]);
version = u32xe(p[2]);
if (version < 0x200) {
if (identification != 0x584C4E58) {
fprintf(stderr, "invalid identification word %#"PRIx32"\n", identification);
return NULL;
}
decode = decode_v1;
} else if (version < 0x300) {
if (identification != 0x004F4443) {
fprintf(stderr, "invalid identification word %#"PRIx32"\n", identification);
return NULL;
}
decode = decode_v2;
} else {
fprintf(stderr, "unsupported version %#"PRIx32"\n", version);
return NULL;
}
seq = cdocmd_create_sequence();
seq->version = version;
if (decode(seq, p, size/4, be) != 0) {
cdocmd_delete_sequence(seq);
return NULL;
}
return seq;
}
static uint32_t * cdobuf;
static uint32_t cdocap;
static void clear_cdobuf(void) {
cdobuf = NULL;
cdocap = 0;
}
static uint32_t * grow_cdobuf(uint32_t len) {
if (cdocap < len) {
do {
cdocap = cdocap ? cdocap * 2 : 1;
} while (cdocap < len);
cdobuf = (uint32_t *)realloc(cdobuf, cdocap * sizeof *cdobuf);
}
return cdobuf;
}
static void * encode_v1(CdoSequence * seq, size_t * sizep, uint32_t be) {
LINK * l = seq->cmds.next;
uint32_t align = 1; /* TODO: is this needed? */
uint32_t hdrlen = 4;
uint32_t pos = hdrlen + 1;
uint32_t * p = grow_cdobuf(pos);
CdoCommand * section_cmd = NULL;
uint32_t section_pos = 0;
uint32_t sections = 0;
uint32_t commands = 0;
p[0] = u32xe(pos - 1);
p[1] = u32xe(0x584C4E58);
p[2] = u32xe(seq->version);
if (l != &seq->cmds) {
CdoCommand * cmd = all2cmds(l);
if (cmd->type != CdoCmdSection) {
fprintf(stderr, "missing section information\n");
clear_cdobuf();
return NULL;
}
}
while (l != &seq->cmds) {
CdoCommand * cmd = all2cmds(l);
l = l->next;
switch (cmd->type) {
case CdoCmdSection:
if (section_cmd != NULL) {
p[section_pos+0] = u32xe(section_cmd->id);
p[section_pos+1] = u32xe(pos - section_pos - 2);
p[section_pos+2] = u32xe(commands - 1);
}
section_cmd = cmd;
section_pos = pos;
commands = 0;
sections++;
pos += 3;
break;
#if 0
case CdoCmdSetBaseAddress:
fprintf(f, "set_base_address ");
print_x64(f, cmd->dstaddr);
fprintf(f, "\n");
break;
#endif
case CdoCmdComment:
continue;
case CdoCmdWrite:
if (cmd->count == 1) {
if ((cmd->dstaddr >> 32) == 0) {
p = grow_cdobuf(pos + 3);
p[pos++] = u32xe(CMD1_WRITE);
p[pos++] = u32xe_lo(cmd->dstaddr);
} else {
p = grow_cdobuf(pos + 4);
p[pos++] = u32xe(CMD1_WRITE64);
p[pos++] = u32xe_lo(cmd->dstaddr);
}
} else {
uint32_t offset = -(pos + 4) & (align - 1);
p = grow_cdobuf(pos + 4 + offset + cmd->count);
p[pos++] = u32xe(CMD1_DMA_WRITE);
p[pos++] = u32xe_lo(cmd->dstaddr);
p[pos++] = u32xe(cmd->count);
p[pos++] = u32xe(offset);
while (offset-- > 0) p[pos++] = u32xe(0);
}
memcpy(p + pos, cmd->buf, cmd->count * sizeof(uint32_t));
byte_swap_buffer(p + pos, cmd->count, be);
pos += cmd->count;
break;
#if 0
case CdoCmdSetBlock:
fprintf(f, "set ");
print_x64(f, cmd->dstaddr);
fprintf(f, " %"PRIu32" ", cmd->count);
print_x64(f, cmd->value);
fprintf(f, "\n");
break;
#endif
case CdoCmdMaskWrite:
if ((cmd->dstaddr >> 32) == 0) {
p = grow_cdobuf(pos + 4);
p[pos++] = u32xe(CMD1_MASK_WRITE);
p[pos++] = u32xe_lo(cmd->dstaddr);
} else {
p = grow_cdobuf(pos + 5);
p[pos++] = u32xe(CMD1_MASK_WRITE64);
p[pos++] = u32xe_hi(cmd->dstaddr);
p[pos++] = u32xe_lo(cmd->dstaddr);
}
p[pos++] = u32xe(cmd->mask);
p[pos++] = u32xe(cmd->value);
break;
case CdoCmdMaskPoll:
if ((cmd->dstaddr >> 32) == 0) {
p = grow_cdobuf(pos + 4);
p[pos++] = u32xe(CMD1_MASK_POLL);
p[pos++] = u32xe_lo(cmd->dstaddr);
} else {
p = grow_cdobuf(pos + 5);
p[pos++] = u32xe(CMD1_MASK_POLL64);
p[pos++] = u32xe_hi(cmd->dstaddr);
p[pos++] = u32xe_lo(cmd->dstaddr);
}
p[pos++] = u32xe(cmd->mask);
p[pos++] = u32xe(cmd->value);
p[pos++] = u32xe((cmd->count + 999)/1000);
break;
#if 0
case CdoCmdMaskPollEq:
fprintf(f, "mask_poll_eq ");
print_x64(f, cmd->dstaddr);
fprintf(f, " ");
print_x64(f, cmd->mask);
fprintf(f, " ");
print_x64(f, cmd->value);
fprintf(f, "\n");
break;
#endif
case CdoCmdDelay:
p = grow_cdobuf(pos + 2);
p[pos++] = u32xe(CMD1_DELAY);
p[pos++] = u32xe((cmd->value + 999)/1000);
break;
case CdoCmdDmaXfer:
p = grow_cdobuf(pos + 8);
p[pos++] = u32xe(CMD1_DMA_XFER);