-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathimx_sdp.c
1500 lines (1365 loc) · 36.7 KB
/
imx_sdp.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
/*
* imx_sdp:
* Implementation of the Serial Download Protocol (SDP) for i.MX/Vybrid
* series processors.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include "portable.h"
#include "imx_sdp.h"
#include "image.h"
#define FT_APP 0xaa
#define FT_CSF 0xcc
#define FT_DCD 0xee
#define FT_LOAD_ONLY 0x00
int debugmode = 0;
#define get_min(a, b) (((a) < (b)) ? (a) : (b))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#ifndef offsetof
#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
//#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
struct load_desc {
struct sdp_work *curr;
FILE* xfile;
unsigned fsize;
int verify;
unsigned char *buf_start;
unsigned buf_size;
unsigned buf_cnt;
unsigned buf_offset;
unsigned dladdr;
unsigned max_length;
unsigned plugin;
unsigned header_addr;
unsigned header_offset;
unsigned char writeable_header[1024];
};
/* Boot data image indicator */
#define PLUGIN_IMAGE_FLAG_MASK (0x0001) /* bit 0 is plugin */
#define HDMI_IMAGE_FLAG_MASK (0x0002) /* bit 1 is HDMI */
struct boot_data {
uint32_t dest;
uint32_t image_len;
uint32_t plugin;
};
/* Command tags and parameters */
#define IVT_HEADER_TAG 0xD1
#define IVT_VERSION 0x40
#define IVT_VERSION_IMX8M 0x41
#define DCD_HEADER_TAG 0xD2
#define DCD_VERSION 0x40
#define DCD_VERSION_IMX8M 0x41
#pragma pack (1)
struct ivt_header {
uint8_t tag;
uint16_t length;
uint8_t version;
};
#pragma pack ()
struct flash_header_v2 {
struct ivt_header header;
uint32_t start_addr;
uint32_t reserv1;
uint32_t dcd_ptr;
uint32_t boot_data_ptr; /* struct boot_data * */
uint32_t self_ptr; /* struct flash_header_v2 *, this - boot_data.start = offset linked at */
uint32_t app_code_csf;
uint32_t reserv2;
};
#pragma pack (1)
struct write_dcd_command {
uint8_t tag;
uint16_t length;
uint8_t param;
};
#pragma pack ()
struct dcd_v2 {
struct ivt_header header;
struct write_dcd_command write_dcd_command;
unsigned char *addr_data;
};
/*
* MX51 header type
*/
struct flash_header_v1 {
uint32_t app_start_addr;
#define APP_BARKER 0xb1
#define DCD_BARKER 0xb17219e9
uint32_t app_barker;
uint32_t csf_ptr;
uint32_t dcd_ptr_ptr;
uint32_t srk_ptr;
uint32_t dcd_ptr;
uint32_t app_dest_ptr;
};
static void print_sdp_work(struct sdp_work *curr)
{
printf("== work item\n");
printf("filename %s\n", curr->filename);
printf("load_size %d bytes\n", curr->load_size);
printf("load_addr 0x%08x\n", curr->load_addr);
printf("dcd %u\n", curr->dcd);
printf("clear_dcd %u\n", curr->clear_dcd);
printf("plug %u\n", curr->plug);
printf("jump_mode %d\n", curr->jump_mode);
printf("jump_addr 0x%08x\n", curr->jump_addr);
printf("== end work item\n");
return;
}
static long get_file_size(FILE *xfile)
{
long size;
fseek(xfile, 0, SEEK_END);
size = ftell(xfile);
rewind(xfile);
// printf("filesize=%lx\n", size);
return size;
}
static int do_response(struct sdp_dev *dev, int report, unsigned int *result,
bool silent)
{
unsigned char tmp[64] = { 0 };
int last_trans, err;
err = dev->ops->transfer(dev, report, tmp, sizeof(tmp), 4, &last_trans);
if ((!silent && err) || debugmode)
printf("report %d in err=%i, last_trans=%i %02x %02x %02x %02x\n",
report, err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
/* At least 4 bytes required for a valid result */
if (last_trans < 4)
return -1;
/*
* Most results are symetric, but likely are meant to be big endian
* as everything else is...
*/
*result = BE32(*((unsigned int*)tmp));
return err;
}
static int do_command(struct sdp_dev *dev, unsigned char *cmd, int cmd_size, int retry)
{
int last_trans, err = -4;
dbg_printf("sending command cmd=%02x %02x %02x %02x\n", cmd[0], cmd[1],cmd[2],cmd[3]);
while (retry) {
err = dev->ops->transfer(dev, 1, (unsigned char *)cmd,
cmd_size, 0, &last_trans);
if (err || debugmode)
printf("%s err=%i, last_trans=%i\n", __func__, err, last_trans);
if (!err)
return 0;
retry--;
}
return err;
}
static int read_memory(struct sdp_dev *dev, unsigned addr, unsigned char *dest, unsigned cnt)
{
unsigned char buf[MAX_PROTOCOL_SIZE];
int cmd_size;
int retry = 0;
int last_trans;
int err;
int rem;
unsigned char tmp[64];
unsigned int sec;
if (!dev->ops->fill_read_reg)
return 0;
cmd_size = dev->ops->fill_read_reg(buf, addr, cnt);
if (cmd_size <= 0)
return cmd_size;
dbg_printf("%s: addr=%08x, cnt=%08x\n", __func__, addr, cnt);
err = do_command(dev, buf, cmd_size, 5);
if (err)
return err;
err = do_response(dev, 3, &sec, false);
if (err)
return err;
rem = cnt;
retry = 0;
while (rem) {
tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
err = dev->ops->transfer(dev, 4, tmp, 64, rem > 64 ? 64 : rem, &last_trans);
if (err) {
printf("r4 in err=%i, last_trans=%i %02x %02x %02x %02x cnt=%d rem=%d\n", err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3], cnt, rem);
if (retry++ > 8)
break;
continue;
}
retry = 0;
if ((last_trans > rem) || (last_trans > 64)) {
if ((last_trans == 64) && (cnt == rem)) {
/* Last transfer is expected to be too large for HID */
} else {
printf("err: %02x %02x %02x %02x cnt=%d rem=%d last_trans=%i\n", tmp[0], tmp[1], tmp[2], tmp[3], cnt, rem, last_trans);
}
last_trans = rem;
if (last_trans > 64)
last_trans = 64;
}
memcpy(dest, tmp, last_trans);
dest += last_trans;
rem -= last_trans;
}
dbg_printf("%s: %d addr=%08x, val=%02x %02x %02x %02x\n", __func__, err, addr, dest[0], dest[1], dest[2], dest[3]);
return err;
}
static int write_memory(struct sdp_dev *dev, unsigned addr, unsigned val)
{
unsigned char buf[MAX_PROTOCOL_SIZE];
int cmd_size;
int last_trans;
int err = 0;
unsigned char tmp[64] = { 0 };
unsigned int sec;
if (!dev->ops->fill_write_reg)
return 0;
cmd_size = dev->ops->fill_write_reg(buf, addr, val);
if (cmd_size <= 0)
return cmd_size;
dbg_printf("%s: addr=%08x, val=%08x\n", __func__, addr, val);
err = do_command(dev, buf, cmd_size, 5);
if (err)
return err;
err = do_response(dev, 3, &sec, false);
if (err)
return err;
err = dev->ops->transfer(dev, 4, tmp, sizeof(tmp), 4, &last_trans);
dbg_printf("report 4, err=%i, last_trans=%i %02x %02x %02x %02x %02x %02x %02x %02x\n",
err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3],
tmp[4], tmp[5], tmp[6], tmp[7]);
if (err)
printf("w4 in err=%i, last_trans=%i %02x %02x %02x %02x\n", err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
return err;
}
static void perform_mem_work(struct sdp_dev *dev, struct mem_work *mem)
{
unsigned tmp, tmp2;
while (mem) {
switch (mem->type) {
case MEM_TYPE_READ:
read_memory(dev, mem->vals[0], (unsigned char *)&tmp, 4);
printf("*%x is %x\n", mem->vals[0], tmp);
break;
case MEM_TYPE_WRITE:
write_memory(dev, mem->vals[0], mem->vals[1]);
printf("%x write %x\n", mem->vals[0], mem->vals[1]);
break;
case MEM_TYPE_MODIFY:
read_memory(dev, mem->vals[0], (unsigned char *)&tmp, 4);
tmp2 = (tmp & ~mem->vals[1]) | mem->vals[2];
printf("%x = %x to %x\n", mem->vals[0], tmp, tmp2);
write_memory(dev, mem->vals[0], tmp2);
break;
}
mem = mem->next;
}
}
static int do_status(struct sdp_dev *dev)
{
unsigned char buf[MAX_PROTOCOL_SIZE];
int cmd_size;
unsigned int hab_security, status;
int retry = 0;
int err;
if (!dev->ops->fill_status)
return 0;
cmd_size = dev->ops->fill_status(buf);
if (cmd_size <= 0)
return cmd_size;
err = do_command(dev, buf, cmd_size, 5);
if (err)
return err;
while (retry < 5) {
err = do_response(dev, 3, &hab_security, false);
if (!err)
break;
retry++;
}
if (err)
return err;
printf("HAB security state: %s (0x%08x)\n", hab_security == HAB_SECMODE_PROD ?
"production mode" : "development mode", hab_security);
if (dev->mode == MODE_HID) {
err = do_response(dev, 4, &status, false);
if (err)
return err;
}
return 0;
}
static int do_data_transfer(struct sdp_dev *dev, unsigned char *buf, int len)
{
int err;
int retry = 10;
int max = dev->max_transfer;
int last_trans;
int cnt;
int transferSize = 0;
while (retry) {
cnt = get_min(len, max);
err = dev->ops->transfer(dev, 2, buf, cnt, 0, &last_trans);
if (!err) {
if (cnt > last_trans)
cnt = last_trans;
if (!cnt) {
printf("Nothing transferred, err=%i transferSize=%i\n", err, transferSize);
return -EIO;
}
transferSize += cnt;
buf += cnt;
len -= cnt;
if (!len)
return transferSize;
retry = 10;
max = dev->max_transfer;
continue;
}
printf("report 2 out err=%i, last_trans=%i len=0x%x max=0x%x retry=%i\n",
err, last_trans, len, max, retry);
if (max >= 16) {
max >>= 1;
max &= ~0x03;
} else {
max <<= 1;
}
/* Wait a few ms before retrying transfer */
msleep(10);
retry--;
}
printf("Giving up\n");
return err;
}
static int write_dcd(struct sdp_dev *dev, struct dcd_v2 *dcd)
{
unsigned char buf[MAX_PROTOCOL_SIZE];
int cmd_size;
int length = BE16(dcd->header.length);
int err;
unsigned transferSize=0;
if (length > HAB_MAX_DCD_SIZE) {
printf("DCD is too big (%d bytes)\n", length);
return -1;
}
if (!dev->ops->fill_dl_dcd)
return 0;
cmd_size = dev->ops->fill_dl_dcd(buf, dev->dcd_addr, length);
if (cmd_size <= 0)
return cmd_size;
printf("loading DCD table @%#x\n", dev->dcd_addr);
err = do_command(dev, buf, cmd_size, 5);
if (err)
return err;
err = do_data_transfer(dev, (unsigned char *)dcd, length);
if (err < 0)
return err;
transferSize = err;
printf("\n<<<%i, %i bytes>>>\n", length, transferSize);
if (dev->mode == MODE_HID) {
unsigned int sec, status;
err = do_response(dev, 3, &sec, false);
if (err)
return err;
err = do_response(dev, 4, &status, false);
if (err)
return err;
if (status == 0x128a8a12UL)
printf("succeeded");
else
printf("failed");
printf(" (security 0x%08x, status 0x%08x)\n", sec, status);
} else {
do_status(dev);
}
return transferSize;
}
static int write_dcd_table_ivt(struct sdp_dev *dev, struct dcd_v2 *dcdhdr)
{
int length = BE16(dcdhdr->header.length);
unsigned char *dcd = (unsigned char *)&dcdhdr->write_dcd_command;
unsigned char *dcd_end;
int err = 0;
printf("main dcd length %x\n", length);
dcd_end = ((unsigned char *)dcdhdr) + length;
while (dcd < dcd_end) {
unsigned s_length = (dcd[1] << 8) + dcd[2];
unsigned sub_tag = (dcd[0] << 24) + (dcd[3] & 0x7);
unsigned flags = (dcd[3] & 0xf8);
unsigned char *s_end = dcd + s_length;
printf("sub dcd length %x\n", s_length);
switch(sub_tag) {
/* Write Data Command */
case 0xcc000004:
if (flags & 0xe8) {
printf("error: Write Data Command with unsupported flags, flags %x.\n", flags);
return -1;
}
dcd += 4;
if (s_end > dcd_end) {
printf("error s_end(%p) > dcd_end(%p)\n", s_end, dcd_end);
return -1;
}
while (dcd < s_end) {
unsigned addr = (dcd[0] << 24) + (dcd[1] << 16) + (dcd[2] << 8) + dcd[3];
unsigned val = (dcd[4] << 24) + (dcd[5] << 16) + (dcd[6] << 8) + dcd[7];
dcd += 8;
dbg_printf("write data *0x%08x = 0x%08x\n", addr, val);
err = write_memory(dev, addr, val);
if (err < 0)
return err;
}
break;
/* Check Data Command */
case 0xcf000004: {
unsigned addr, count, mask, val;
dcd += 4;
addr = (dcd[0] << 24) + (dcd[1] << 16) + (dcd[2] << 8) + dcd[3];
mask = (dcd[4] << 24) + (dcd[5] << 16) + (dcd[6] << 8) + dcd[7];
count = 10000;
switch (s_length) {
case 12:
dcd += 8;
break;
case 16:
count = (dcd[8] << 24) + (dcd[9] << 16) + (dcd[10] << 8) + dcd[11];
dcd += 12;
break;
default:
printf("error s_end(%p) > dcd_end(%p)\n", s_end, dcd_end);
return -1;
}
dbg_printf("Check Data Command, at addr %x, mask %x\n",addr, mask);
while (count) {
val = 0;
err = read_memory(dev, addr, (unsigned char*)&val, 4);
if (err < 0) {
printf("Check Data Command(%x) error(%d) @%x=%x mask %x\n", flags, err, addr, val, mask);
return err;
}
if ((flags == 0x00) && ((val & mask) == 0) )
break;
else if ((flags == 0x08) && ((val & mask) != mask) )
break;
else if ((flags == 0x10) && ((val & mask) == mask) )
break;
else if ((flags == 0x18) && ((val & mask) != 0) )
break;
else if (flags & 0xe0) {
printf("error: Check Data Command with unsupported flags, flags %x.\n", flags);
return -1;
}
count--;
}
if (!count)
printf("!!!Check Data Command(%x) expired without condition met @%x=%x mask %x\n", flags, addr, val, mask);
else
printf("Check Data Command(%x) success @%x=%x mask %x\n", flags, addr, val, mask);
break;
}
default:
printf("Unknown sub tag, dcd[0] 0x%2x, dcd[3] 0x%2x\n", dcd[0], dcd[3]);
return -1;
}
}
return err;
}
static int get_dcd_range_old(struct flash_header_v1 *hdr,
unsigned char *file_start, unsigned cnt,
unsigned char **pstart, unsigned char **pend)
{
unsigned char *dcd_end;
unsigned m_length;
#define cvt_dest_to_src_old (((unsigned char *)&hdr->dcd_ptr) - hdr->dcd_ptr_ptr)
unsigned char* dcd;
unsigned val;
unsigned char* file_end = file_start + cnt;
if (!hdr->dcd_ptr) {
printf("No dcd table, barker=%x\n", hdr->app_barker);
*pstart = *pend = ((unsigned char *)hdr) + sizeof(struct flash_header_v1);
return 0; //nothing to do
}
dcd = hdr->dcd_ptr + cvt_dest_to_src_old;
if ((dcd < file_start) || ((dcd + 8) > file_end)) {
printf("bad dcd_ptr %08x\n", hdr->dcd_ptr);
return -1;
}
val = (dcd[0] << 0) + (dcd[1] << 8) + (dcd[2] << 16) + (dcd[3] << 24);
if (val != DCD_BARKER) {
printf("Unknown tag\n");
return -1;
}
dcd += 4;
m_length = (dcd[0] << 0) + (dcd[1] << 8) + (dcd[2] << 16) + (dcd[3] << 24);
printf("main dcd length %x\n", m_length);
dcd += 4;
dcd_end = dcd + m_length;
if (dcd_end > file_end) {
printf("bad dcd length %08x\n", m_length);
return -1;
}
*pstart = dcd;
*pend = dcd_end;
return 0;
}
static int write_dcd_table_old(struct sdp_dev *dev, struct flash_header_v1 *hdr, unsigned char *file_start, unsigned cnt)
{
unsigned val;
unsigned char *dcd_end;
unsigned char* dcd;
int err = get_dcd_range_old(hdr, file_start, cnt, &dcd, &dcd_end);
if (err < 0)
return err;
while (dcd < dcd_end) {
unsigned type = (dcd[0] << 0) + (dcd[1] << 8) + (dcd[2] << 16) + (dcd[3] << 24);
unsigned addr = (dcd[4] << 0) + (dcd[5] << 8) + (dcd[6] << 16) + (dcd[7] << 24);
val = (dcd[8] << 0) + (dcd[9] << 8) + (dcd[10] << 16) + (dcd[11] << 24);
dcd += 12;
if (type!=4) {
printf("!!!unknown type=%08x *0x%08x = 0x%08x\n", type, addr, val);
} else {
printf("type=%08x *0x%08x = 0x%08x\n", type, addr, val);
err = write_memory(dev, addr, val);
if (err < 0)
return err;
}
}
return err;
}
static void diff_long(unsigned char *src1, unsigned char *src2, unsigned cnt, unsigned skip)
{
unsigned char buf[8*9 + 2];
unsigned *s1 = (unsigned *)src1;
unsigned *s2 = (unsigned *)src2;
unsigned i, j;
while (cnt >= 4) {
unsigned char *p = buf;
unsigned max = get_min(cnt >> 2, 8);
for (i = 0; i < (skip >> 2); i++) {
for (j=0; j < 9; j++)
*p++ = ' ';
}
for (; i < max; i++) {
unsigned s1v = *s1++;
unsigned diff = s1v ^ *s2++;
unsigned c;
*p++ = ' ';
if (i == 4)
*p++ = ' ';
for (j = 0; j < 8; j++) {
unsigned changed = diff & 0xf0000000;
c = ' ';
if (changed) {
if ((s1v & changed) == 0)
c = '^';
else if ((s1v & changed) == changed)
c = 'v';
else
c = '-';
}
*p++ = c;
diff <<= 4;
s1v <<= 4;
}
}
*p = 0;
printf(" %s\n", buf);
cnt -= max << 2;
}
}
void dump_long(unsigned char *src, unsigned cnt, unsigned addr, unsigned skip)
{
unsigned *p = (unsigned *)src;
int i = skip >> 2;
while (cnt >= 4) {
printf("%08x:", addr);
while (skip >= 4) {
printf(" ");
skip -= 4;
}
while (cnt >= 4) {
printf((i==4) ? " %08x":" %08x", p[0]);
p++;
cnt -= 4;
addr += 4;
i++;
if (i==8)
break;
}
printf("\n");
i = 0;
}
}
void dump_bytes(unsigned char *src, unsigned cnt, unsigned addr)
{
unsigned char *p = src;
int i;
while (cnt >= 16) {
printf("%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", addr,
p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
p += 16;
cnt -= 16;
addr += 16;
}
if (cnt) {
printf("%08x:", addr);
i = 0;
while (cnt) {
printf(" %02x", p[0]);
p++;
cnt--;
i++;
if (cnt) if (i == 4) {
i = 0;
printf(" ");
}
}
printf("\n");
}
}
static void fetch_data(struct load_desc *ld, unsigned foffset, unsigned char **p, unsigned *cnt)
{
unsigned skip = foffset - ld->header_offset;
unsigned buf_cnt = ld->buf_cnt;
if ((ld->curr->jump_mode >= J_ADDR_HEADER) &&
(skip < sizeof(ld->writeable_header))) {
*p = &ld->writeable_header[skip];
*cnt = sizeof(ld->writeable_header) - skip;
return;
}
skip = foffset - ld->buf_offset;
if (skip >= buf_cnt) {
fseek(ld->xfile, foffset, SEEK_SET);
ld->buf_offset = foffset;
buf_cnt = ld->buf_cnt = fread(ld->buf_start, 1, ld->buf_size, ld->xfile);
skip = 0;
}
if ((foffset < ld->header_offset) &&
(ld->header_offset < ld->buf_offset + buf_cnt))
buf_cnt = ld->header_offset - ld->buf_offset;
*p = &ld->buf_start[skip];
*cnt = buf_cnt - skip;
}
static int verify_memory(struct sdp_dev *dev, struct load_desc *ld, unsigned foffset,
unsigned size)
{
int mismatch = 0;
unsigned verified = 0;
unsigned total = size;
unsigned addr = ld->dladdr;
while (size) {
unsigned char *p;
unsigned cnt;
unsigned char mem_buf[64];
int align_cnt = foffset & 0x3f;
unsigned offset = foffset;
fetch_data(ld, foffset, &p, &cnt);
if (align_cnt) {
align_cnt = 64 - align_cnt;
if (cnt > align_cnt)
cnt = align_cnt;
}
if (cnt <= 0) {
printf("Unexpected end of file, size=0x%x, cnt=%i\n", size, cnt);
return -1;
}
if (cnt > size)
cnt = size;
size -= cnt;
foffset += cnt;
while (cnt) {
int ret;
unsigned request = get_min(cnt, sizeof(mem_buf));
ret = read_memory(dev, addr, mem_buf, request);
if (ret < 0) {
printf("verified 0x%x of 0x%x before usb error\n", verified, total);
return ret;
}
if (memcmp(p, mem_buf, request)) {
unsigned char * m = mem_buf;
if (!mismatch)
printf("!!!!mismatch\n");
mismatch++;
while (request) {
unsigned skip = addr & 0x1f;
unsigned max = 0x20 - skip;
unsigned req = get_min(request, (int)max);
if (memcmp(p, m, req)) {
dump_long(p, req, offset, skip);
dump_long(m, req, addr, skip);
diff_long(p, m, req, skip);
}
p += req;
m+= req;
offset += req;
addr += req;
cnt -= req;
request -= req;
}
if (mismatch >= 5)
return -1;
}
p += request;
offset += request;
addr += request;
cnt -= request;
verified += request;
}
}
if (!mismatch)
printf("Verify success\n");
return mismatch ? -1 : 0;
}
static int load_file(struct sdp_dev *dev, struct load_desc *ld, unsigned foffset,
unsigned fsize, unsigned char type)
{
unsigned char buf[MAX_PROTOCOL_SIZE];
int cmd_size;
int err;
unsigned transferSize=0;
unsigned char *p;
unsigned cnt;
unsigned char combine_buf[1024];
if (!dev->no_hid_cmd) {
if (!dev->ops->fill_write_file)
return 0;
cmd_size = dev->ops->fill_write_file(buf, ld->dladdr, fsize, type);
if (cmd_size <= 0)
return cmd_size;
dbg_printf("%s:cmd_size=%x\n", __func__, cmd_size);
dbg_dump_long(buf, cmd_size, 0, 0);
do_command(dev, buf, cmd_size, 5);
}
if (dev->mode == MODE_BULK) {
unsigned int sec;
err = do_response(dev, 3, &sec, false);
if (err)
return err;
}
while (transferSize < fsize) {
unsigned remaining = (fsize-transferSize);
fetch_data(ld, foffset, &p, &cnt);
/* Avoid short packets, they may signal end of transfer */
if (cnt < sizeof(combine_buf)) {
unsigned next_cnt;
memcpy(combine_buf, p, cnt);
while (cnt < sizeof(combine_buf)) {
fetch_data(ld, foffset + cnt, &p, &next_cnt);
if (!next_cnt)
break;
if (next_cnt > sizeof(combine_buf) - cnt)
next_cnt = sizeof(combine_buf) - cnt;
memcpy(&combine_buf[cnt], p, next_cnt);
cnt += next_cnt;
}
p = combine_buf;
} else {
cnt &= -sizeof(combine_buf); /* round down to multiple of 1024 */
}
if (cnt > remaining)
cnt = remaining;
if (!cnt)
break;
if (cnt > dev->max_transfer)
cnt = dev->max_transfer;
dbg_printf("%s:foffset=%x, cnt=%x, remaining=%x\n", __func__, foffset, cnt, remaining);
dbg_dump_long(p, cnt, ld->dladdr + transferSize, 0);
err = do_data_transfer(dev, p, cnt);
if (err < 0) {
printf("%s:foffset=%x, cnt=%x, remaining=%x, err=%d\n", __func__, foffset, cnt, remaining, err);
return err;
}
if (!err)
break;
transferSize += err;
foffset += err;
}
printf("\n<<<%i, %i bytes>>>\n", fsize, transferSize);
if (dev->mode == MODE_HID) {
unsigned int sec, status;
err = do_response(dev, 3, &sec, false);
if (err)
return err;
err = do_response(dev, 4, &status, false);
if (err)
return err;
if (status == 0x88888888UL)
printf("succeeded");
else
printf("failed");
printf(" (security 0x%08x, status 0x%08x)\n", sec, status);
} else if (dev->mode == MODE_BULK) {
do_status(dev);
}
return transferSize;
}
static int jump(struct sdp_dev *dev, unsigned int header_addr)
{
unsigned char buf[MAX_PROTOCOL_SIZE];
int cmd_size;
int err;
unsigned int sec, status;
if (!dev->ops->fill_jump)
return 0;
cmd_size = dev->ops->fill_jump(buf, header_addr);
if (cmd_size <= 0)
return cmd_size;
printf("jumping to 0x%08x\n", header_addr);
err = do_command(dev, buf, cmd_size, 5);
if (err)
return err;
err = do_response(dev, 3, &sec, false);
if (err)
return err;
err = do_response(dev, 4, &status, true);
/*
* Documentation says: "This report is sent by device only in case of
* an error jumping to the given address..."
* If Report 4 fails, this is a good sign
* If Report 4 responds, there has been something gone wrong...
*/
if (!err) {
printf("failed (security 0x%08x, status 0x%08x)\n", sec, status);
return err;
}
return 0;
}
static int load_file_from_desc(struct sdp_dev *dev, struct sdp_work *curr,
struct load_desc *ld)
{
int ret;
unsigned file_base;
unsigned char type;
unsigned skip = 0;
unsigned fsize;
unsigned transferSize=0;
unsigned cnt;
if (!ld->dladdr) {
printf("\nunknown load address\n");
return -3;
}
type = (curr->plug || curr->jump_mode) ? FT_APP : FT_LOAD_ONLY;
if (dev->mode == MODE_BULK && type == FT_APP) {
/*
* There is no jump command. boot ROM requires the download
* to start at header address
*/
ld->dladdr = ld->header_addr;
}
if (ld->verify) {
if ((type == FT_APP) && (dev->mode != MODE_HID)) {
type = FT_LOAD_ONLY;
ld->verify = 2;
}
}
file_base = ld->header_addr - ld->header_offset;
if (file_base > ld->dladdr) {
ld->max_length -= (file_base - ld->dladdr);
ld->dladdr = file_base;
}
dbg_printf("skip=%x cnt=%x dladdr=%x file_base=%x fsize=%x max_length=%x\n", skip, ld->buf_cnt, ld->dladdr, file_base, ld->fsize, ld->max_length);
skip = ld->dladdr - file_base;
fsize = ld->fsize;
if (skip > fsize) {
printf("skip(0x%08x) > fsize(0x%08x) file_base=0x%08x, header_offset=0x%x\n",
skip, fsize, file_base, ld->header_offset);
ret = -4;
goto cleanup;
}
fsize -= skip;
if (fsize > ld->max_length)
fsize = ld->max_length;
printf("\nloading binary file(%s) to %08x, skip=%x, fsize=%x type=%x\n", curr->filename, ld->dladdr, skip, fsize, type);
ret = load_file(dev, ld, skip, fsize, type);
if (ret < 0)
goto cleanup;
transferSize = ret;
if (ld->verify) {
ret = verify_memory(dev, ld, skip, fsize);
if (ret < 0)
goto cleanup;
if (ld->verify == 2) {
cnt = fsize;
if (cnt > 64)
cnt = 64;
/*
* This will set the right header address