-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathupdate.cpp
1354 lines (1210 loc) · 36 KB
/
update.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
#include <algorithm>
#include <cctype>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <string>
#include "update.hpp"
#include "AmlTime.h"
#include "Amldbglog/Amldbglog.h"
#include "AmlUsbScanX3/AmlUsbScanX3.h"
#include "UsbRomDrv/UsbRomDll.hpp"
#include "UsbRomDrv/UsbRomDrv.hpp"
#include "AmlUsbScan/AmlUsbScan.h"
#include "defs.h"
std::string strToLower (const std::string &src) {
std::string dst(src);
std::transform(src.begin(), src.end(), dst.begin(), tolower);
return dst;
}
const char *_parse_integer_fixup_radix (const char *s, unsigned int *base) {
if (*base == 0) {
if (s[0] == '0') {
if (_tolower(s[1]) == 'x' && isxdigit(s[2])) {
*base = 16;
} else {
*base = 8;
}
} else {
*base = 10;
}
}
if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x') {
s += 2;
}
return s;
}
unsigned int _parse_integer (const char *s, unsigned int base, unsigned long *p) {
unsigned long res;
unsigned int rv;
res = 0;
rv = 0;
while (true) {
unsigned int c = *(unsigned char *) s;
unsigned int lc = c | 0x20; /* don't tolower() this line */
unsigned int val;
if ('0' <= c && c <= '9') {
val = c - '0';
} else if ('a' <= lc && lc <= 'f') {
val = lc - 'a' + 10;
} else {
break;
}
if (val >= base) {
break;
}
res = res * base + val;
rv++;
s++;
}
*p = res;
return rv;
}
unsigned long simple_strtoul (const char *cp, char **endp, unsigned int base) {
unsigned long result;
unsigned int rv;
cp = _parse_integer_fixup_radix(cp, &base);
rv = _parse_integer(cp, base, &result);
cp += rv;
if (endp) {
*endp = (char *) cp;
}
return result;
}
int _print_memory_view (char *buf, unsigned int size, unsigned int offset) {
for (int printedDq = 0; printedDq < (size + 15) >> 4; ++printedDq) {
unsigned int DqToPrint = 16 * (printedDq + 1) <= size ? 16 : size & 0xF;
printf("\n%08X: ", offset);
for (int i = 0; i < (DqToPrint + 3) >> 2; ++i) {
printf("%08x ", *(unsigned int *) &buf[4 * (4 * printedDq + i)]);
}
offset += 16;
}
putchar('\n');
return 0;
}
int update_help () {
puts("====>Amlogic update USB tool(Ver 1.5) 2017/05<=============");
puts("update\t<command>\t[device name]\t<arg0>\t<arg1>\t<arg2>\t...");
puts("\nCommon Commands:");
puts("update <partition>: Burn a partition with a partition image");
puts("update <mwrite> : Burn data to media or memory");
puts("update <mread> : Dump a data from media or memory to pc and save as a file");
puts("update <tplcmd> : like bulkcmd");
puts("update <bulkcmd> : pass and exec a command platform bootloader can support");
puts("update <write> : Down a file to memory");
puts("update <run> : Run code from memory address");
puts("update <read> : Dump data from memory:");
puts("update <wreg> : set one 32bits reg:");
puts("update <rreg> : Dump data from reg:");
puts("update <password> : unlock chip:");
puts("update <chipinfo> : get chip info at page index:");
puts("update <chipid> : get chip id");
puts("\nCommon Commands format:");
puts("update partition partName imgFilePath [imgFileFmt] [sha1VeryFile]");
puts(
"\t\te.g.--\tupdate partition boot z:\\a\\b\\boot.img [normal] //format normal is optional");
puts(
"\t\te.g.--\tupdate partition system z:\\xxxx\\system.img [sparse] //format sparse is optional");
puts(
"\t\te.g.--\tupdate partition upgrade z:\\xxxx\\upgrade.ubifs.img ubifs //format ubifs is MANDATORY");
puts("\nupdate bulkcmd \"burning cmd or u-boot cmd\"");
puts(
"\t\te.g.--\tupdate bulkcmd \"disk_intial 0\" //cmd to init flash for usb burning");
puts("\t\te.g.--\tupdate bulkcmd \"printenv\" //uboot command 'printenv'");
puts("\nupdate mread store/mem partName/memAddress");
puts(
"\t\te.g.--\tupdate mread store boot normal 0x200000 d:\boot.dump //upload 32M of boot partition in path d:\boot.dump");
puts(
"\t\te.g.--\tupdate mread mem 0x1080000 normal d:\\mem_2M.dump //upload 2M memory at address 0x1080000 in path d:\\mem_2M.dump");
puts("\t\te.g.--\tupdate chipinfo pageIndex dumpFilePath nBytes startOffset");
return puts("====>Amlogic update USB tool(Ver 1.5) 2017/05<=============");
}
int update_scan (void **resultDevices, int print_dev_list, unsigned int dev_no,
int *success, char *scan_mass_storage) {
int result = 0;
char *buf;
char *candidateDevices[16] = {};
*success = -1;
candidateDevices[0] = new char[0x800];
buf = candidateDevices[0];
memset(candidateDevices[0], 0, 0x800);
for (int i = 1; i <= 15; ++i) {
candidateDevices[i] = candidateDevices[i - 1] + 128;
}
const char *vendorName = scan_mass_storage ? "USB Mass Storage Device"
: "WorldCup Device";
int nDevices = AmlScanUsbX3Devices(vendorName, candidateDevices);
if (nDevices <= 0) {
aml_printf("[update]No [%s] device after scan\n", vendorName);
result = -175;
goto finish;
}
if (dev_no != -2 && nDevices) {
if (dev_no + 1 > nDevices) {
aml_printf("[update]ERR(L%d):", 183);
aml_printf("dev_no(%d) too only, only [%d] (%s)devices Connected\n", dev_no,
nDevices, vendorName);
result = -184;
goto finish;
}
if (!scan_mass_storage) {
if (!resultDevices) {
aml_printf("[update]ERR(L%d):", 195);
aml_printf("handle for (%s) NULL is invalid\n", vendorName);
result = -196;
goto finish;
}
*resultDevices = AmlGetDeviceHandle(vendorName, candidateDevices[dev_no]);
} else {
printf("%d.%s ==> ", dev_no, candidateDevices[dev_no]);
AmlGetMsNumber(candidateDevices[dev_no], 1, scan_mass_storage);
}
}
*success = 0;
if (print_dev_list) {
printf("\t-------%sdevices list -------\n",
scan_mass_storage ? "Mass Storage " : "MPtool ");
for (int j = 0; j < nDevices; ++j) {
printf("WorldCup[%02d].%s\n", j, candidateDevices[j]);
}
}
finish:
if (buf) {
delete[] buf;
}
return result;
}
int do_cmd_mwrtie (const char **argv, signed int argc, AmlUsbRomRW &rom) {
int result = -229;
const char *readFile = argv[0];
const char *storeOrMem = argv[1];
const char *partition = argv[2];
const char *fileType = argv[3];
const char *verifyFile = argc <= 4 ? nullptr : argv[4];
int retry; // [rsp+20h] [rbp-E0h]
off_t fileSize;
unsigned int dataSize; // [rsp+30h] [rbp-D0h]fp; // [rsp+38h] [rbp-C8h]
char buffer[128] = {};
FILE *fp = fopen(readFile, "rb");
if (!fp) {
aml_printf("Open file %s failed\n", readFile);
goto finish;
}
fseeko64(fp, 0, 2);
fileSize = ftello(fp);
fclose(fp);
fp = nullptr;
aml_printf("file size is 0x%llx\n", fileSize);
if (!fileSize) {
aml_printf("file size 0!!\n");
return -252;
}
snprintf((char *) buffer, sizeof(buffer), "download %s %s %s 0x%lx", storeOrMem,
partition, fileType, fileSize);
buffer[66] = 1;
rom.buffer = buffer;
rom.bufferLen = 68;
rom.pDataSize = &dataSize;
if (AmlUsbTplCmd(&rom)) {
goto finish;
}
retry = 1;
memset(buffer, 0, 0x80);
while (retry) {
rom.buffer = buffer;
rom.bufferLen = 64;
rom.pDataSize = &dataSize;
if (!(unsigned int) AmlUsbReadStatus(&rom)) {
break;
}
usleep(1000000);
--retry;
}
if (!retry) {
aml_printf("Read status failed\n");
result = -292;
goto finish;
}
if (strncmp((const char *) &rom.buffer, "success", 7) != 0) {
aml_printf("[update]ERR(L%d):", 298);
aml_printf("cmdret=[%s]\n", rom.buffer);
result = -299;
goto finish;
}
if (WriteMediaFile(&rom, readFile) != 0) {
aml_printf("ERR:write data to media failed\n");
result = -306;
goto finish;
}
strcpy((char *) buffer, "download get_status");
buffer[66] = 1;
rom.buffer = buffer;
rom.bufferLen = 68;
rom.pDataSize = &dataSize;
if (AmlUsbBulkCmd(&rom) != 0) {
aml_printf("[update]ERR(L%d):", 319);
aml_printf("AmlUsbBulkCmd failed!\n");
result = -320;
goto finish;
}
if (!verifyFile) {
result = 0;
aml_printf("[update]mwrite success\n");
goto finish;
}
memset(buffer, 0, 0x80);
fp = fopen(verifyFile, "rb");
if (!fp) {
aml_printf("Open file %s failed\n", verifyFile);
goto finish;
}
strcpy(buffer, "verify ");
fread(&buffer[7], 1, 0x79, fp);
fclose(fp);
fp = nullptr;
buffer[66] = 1;
rom.buffer = buffer;
rom.bufferLen = 68;
rom.pDataSize = &dataSize;
if (AmlUsbBulkCmd(&rom) != 0) {
aml_printf("ERR: AmlUsbBulkCmd failed!\n");
result = -346;
goto finish;
}
finish:
if (fp) {
fclose(fp);
}
if (rom.device) {
rom.device = nullptr;
}
return result;
}
int update_sub_cmd_run_and_rreg (AmlUsbRomRW &rom, const char *cmd, const char **argv,
signed int argc) {
if (argc <= 0) {
aml_printf("[update]ERR(L%d):", 373);
aml_printf("argc(%d)<2, address for cmd[%s] not assigned\n", argv, cmd);
return -374;
}
rom.address = simple_strtoul(strToLower(argv[0]).c_str(), nullptr, 0);
if (!strcmp(cmd, "run")) {
aml_printf("[update]Run at Addr %x\n", rom.address);
rom.buffer = (char *) &rom.address;
int result = AmlUsbRunBinCode(&rom);
if (result) {
puts("ERR: Run cmd failed");
}
return result;
}
unsigned int address = simple_strtoul(strToLower(argv[1]).c_str(), nullptr, 0);
const char *saveFile = argc <= 2 ? nullptr : argv[2];
FILE *saveFp = saveFile ? fopen(saveFile, "wb") : nullptr;
char *buffer = new char[0x100000];
bool isRreg = !strcmp(cmd, "rreg");
FILE *readFp = nullptr;
unsigned int transferSize;
if (isRreg) {
transferSize = simple_strtoul(strToLower(argv[0]).c_str(), nullptr, 0);
} else {
readFp = fopen(argv[0], "rb");
fseeko(readFp, 0, 2);
transferSize = ftell(readFp);
fseek(readFp, 0, 0);
}
aml_printf("[update]Total tansfer size 0x%x\n", transferSize);
int ret = 0;
unsigned int transferredSize = 0;
while (transferredSize < transferSize) {
unsigned int bulkSize = std::min(transferSize - transferredSize, 0x100000u);
if (readFp) {
fread(buffer, bulkSize, 1, readFp);
}
ret = Aml_Libusb_Ctrl_RdWr(rom.device, address, buffer, bulkSize, isRreg, 5000);
if (ret) {
aml_printf("[update]ret=%d\n", ret);
break;
}
if (isRreg) {
if (saveFp) {
fwrite(buffer, bulkSize, 1, saveFp);
} else {
_print_memory_view(buffer, bulkSize, address);
}
}
transferredSize += bulkSize;
address += bulkSize;
}
if (buffer) {
delete[] buffer;
}
if (saveFp) {
fclose(saveFp);
}
return ret;
}
int update_sub_cmd_set_password (AmlUsbRomRW &rom, const char **argv, int argc) {
const char *pwdFile = argv[0];
FILE *fp = fopen(pwdFile, "rb");
if (!fp) {
aml_printf("[update]ERR(L%d):", 470);
aml_printf("Open file %s failed\n", pwdFile);
return -1;
}
fseeko(fp, 0, 2);
off_t totalFileSz = ftello(fp);
if (totalFileSz > 64) {
aml_printf("[update]ERR(L%d):", 477);
aml_printf("size of file(%s) is %lld too large!!\n", pwdFile, totalFileSz);
fclose(fp);
return -479;
}
fseek(fp, 0, 0);
char ptr;
size_t rdLen = fread(&ptr, 1, totalFileSz, fp);
if (rdLen != totalFileSz) {
aml_printf("[update]ERR(L%d):", 486);
aml_printf("FAil in read pwd file (%s), rdLen(%d) != TotalFileSz(%d)\n", pwdFile,
rdLen, totalFileSz);
fclose(fp);
return -488;
}
fclose(fp);
int ret = 0;
for (int i = 0; i <= 1 && (ret & 0x80000000) == 0; ++i) {
ret = Aml_Libusb_Password(rom.device, &ptr, rdLen, 8000);
if (!i) {
printf("Setting PWD..");
usleep(5000000);
}
}
aml_printf("Pwd returned %d\n", ret);
return ret <= 0 ? ret : 0;
}
int update_sub_cmd_get_chipinfo (AmlUsbRomRW &rom, const char **argv, signed int argc) {
int pageIndex = atoi(argv[0]);
const char *saveFile = argc <= 1 ? "-" : argv[1];
int nBytes = argc <= 2 ? 64 : atoi(argv[2]);
int startOffset = argc <= 3 ? 0 : atoi(argv[3]);
aml_printf("[update]pageIndex %d, saveFile %s, nBytes %d, startOffset %d\n", pageIndex,
saveFile, nBytes, startOffset);
if (nBytes > 64) {
aml_printf("[update]ERR(L%d):", 518);
aml_printf("pageSzMax %d < nBytes %d\n", 64, nBytes);
return -519;
}
char *buf = new char[0x40];
if (!buf) {
aml_printf("[update]ERR(L%d):", 523);
aml_printf("Fail in alloc buff\n");
return -524;
}
int result;
FILE *fp = nullptr;
int read = Aml_Libusb_get_chipinfo(rom.device, buf, 64, pageIndex, 8000);
if (read != 64) {
aml_printf("[update]ERR(L%d):", 529);
aml_printf("Fail in get chipinfo, want %d, actual %d\n", 64, read);
result = -530;
goto finish;
}
if (strcmp("-", saveFile) == 0) {
_print_memory_view(&buf[startOffset], nBytes, startOffset);
result = read;
} else {
fp = fopen(saveFile, "wb");
if (!fp) {
aml_printf("[update]ERR(L%d):", 539);
aml_printf("FAil in open file %s\n", saveFile);
result = -540;
goto finish;
}
if (fwrite(&buf[startOffset], 1, nBytes, fp) != nBytes) {
aml_printf("[update]ERR(L%d):", 545);
aml_printf("FAil in save file %s\n", saveFile);
result = -546;
goto finish;
}
result = 0;
}
finish:;
if (buf) {
delete[] buf;
}
if (fp) {
fclose(fp);
}
return result;
}
int update_sub_cmd_read_write (AmlUsbRomRW &rom, const char *cmd, const char **argv,
int argc) {
if (argc <= 1) {
aml_printf("[update]ERR(L%d):", 574);
aml_printf("cmd[%s] must at least %d parameters, but only %d\n", cmd, 2, argc);
return 575;
}
unsigned int total_; // ST5C_4
unsigned int nBytes = 0; // [rsp+30h] [rbp-110h]
int result; // [rsp+34h] [rbp-10Ch]
int dataLen; // [rsp+38h] [rbp-108h] MAPDST
unsigned int dumpFileSize; // [rsp+60h] [rbp-E0h]
unsigned int bufLen = 0; // [rsp+70h] [rbp-D0h]
FILE *fp = nullptr; // [rsp+78h] [rbp-C8h]
char *buffer = nullptr; // [rsp+80h] [rbp-C0h] MAPDST
const char *readFile = nullptr; // [rsp+88h] [rbp-B8h]
unsigned int dataSize;
rom.address = simple_strtoul(strToLower(argv[1]).c_str(), 0, 0);
int offset = rom.address;
if (!strcmp(cmd, "wreg")) {
bufLen = simple_strtoul(argv[0], nullptr, 0x10);
rom.buffer = (char *) &bufLen;
rom.bufferLen = 4;
result = Aml_Libusb_Ctrl_RdWr(rom.device, offset, rom.buffer, rom.bufferLen, 0,
5000);
if (result) {
aml_printf("[update]ERR(L%d):", 593);
aml_printf("write register failed\n");
} else {
aml_printf("[update]operate finished!\n");
}
goto finish;
}
if (!strcmp(cmd, "read") || !strcmp("dump", cmd)) {
const char *dumpFilename = argc <= 2 ? nullptr : argv[2];
FILE *dumpFp = dumpFilename ? fopen(dumpFilename, "wb") : nullptr;
result = 0;
bufLen = simple_strtoul(strToLower(argv[0]).c_str(), nullptr, 0);
total_ = bufLen;
rom.address = simple_strtoul(strToLower(argv[1]).c_str(), 0, 0);
offset = rom.address;
DownloadProgressInfo info(total_, "DUMP");
buffer = new char[0x20000];
while (bufLen) {
if (!strcmp("dump", cmd)) {
if (bufLen >= 65536) {
dataLen = 65536;
} else if (bufLen >= 4096) {
dataLen = 4096;
} else {
dataLen = std::min(bufLen, 512u);
}
} else {
dataLen = std::min(bufLen, 512u);
}
rom.buffer = buffer;
rom.bufferLen = dataLen;
rom.pDataSize = &dataSize;
if (AmlUsbReadLargeMem(&rom) != 0) {
aml_printf("[update]ERR(L%d):", 630);
aml_printf("read device failed\n");
result = -631;
goto finish;
}
if (dumpFp) {
dumpFileSize = fwrite(buffer, 1, dataLen, dumpFp);
if (dumpFileSize != dataLen) {
aml_printf("[update]ERR(L%d):", 639);
aml_printf("Want to write %dB to path[%s], but only %dB\n", dataLen,
dumpFilename, dumpFileSize);
result = -640;
break;
}
info.update_progress(dataLen);
} else {
_print_memory_view(buffer, dataLen, rom.address);
}
bufLen -= dataSize;
rom.address += dataSize;
}
if (dumpFp) {
fclose(dumpFp);
dumpFp = nullptr;
}
goto finish;
}
if (strcmp(cmd, "write") && strcmp(cmd, "boot") && strcmp(cmd, "cwr") &&
strcmp(cmd, "write2")) {
result = 0;
goto finish;
}
readFile = argv[0];
fp = fopen(readFile, "rb");
if (!fp) {
aml_printf("[update]ERR(L%d):", 681);
aml_printf("ERR: can not open the %s file\n", readFile);
result = -682;
goto finish;
}
{
buffer = (char *) malloc(0x10008);
fseek(fp, 0, 2);
int readFileSize = ftell(fp);
fseek(fp, 0, 0);
while (readFileSize) {
int bulkSize = std::min(readFileSize, !strcmp(cmd, "write") ? 65536 : 64);
fread(buffer + 8, 1, bulkSize, fp);
rom.buffer = buffer + 4;
rom.bufferLen = bulkSize;
rom.pDataSize = &dataSize;
int resulta;
if (strcmp(cmd, "write")) {
rom.buffer = buffer + 8;
resulta = AmlUsbWriteLargeMem(&rom);
} else {
SET_INT_AT(rom.buffer, 0, rom.address);
rom.bufferLen += 4;
resulta = AmlUsbCtrlWr(&rom);
}
if (resulta) {
puts("ERR: write data to device failed");
result = -717;
goto finish;
}
readFileSize -= bulkSize;
rom.address += bulkSize;
nBytes += bulkSize;
fseek(fp, nBytes, 0);
printf("..");
}
printf("\nTransfer Complete! total size is %d Bytes\n", nBytes);
}
if (!strcmp(cmd, "boot")) {
rom.address = offset;
rom.buffer = (char *) &rom.address;
result = AmlUsbRunBinCode(&rom);
if (result) {
puts("ERR: Run cmd failed");
goto finish;
}
}
result = 0;
goto finish;
finish:
if (fp) {
fclose(fp);
}
if (buffer) {
free(buffer);
}
if (rom.device) {
rom.device = nullptr;
}
return result;
}
int update_sub_cmd_identify_host (AmlUsbRomRW &rom, int idLen, char *id) {
if (idLen <= 3) {
aml_printf("[update]ERR(L%d):", 757);
aml_printf("idLen %d invalid\n", idLen);
return -758;
}
unsigned int dataLen = 0;
char id_buf[16];
rom.buffer = id ? id : id_buf;
rom.bufferLen = idLen;
rom.pDataSize = &dataLen;
if (AmlUsbIdentifyHost(&rom)) {
puts("ERR: get info from device failed");
return -767;
}
printf("This firmware version is ");
bool fw_ver_check_error = true;
for (int dataPtr = 0; dataPtr < dataLen; dataPtr++) {
printf("%d%c", id[dataPtr], dataPtr + 1 >= dataLen ? '\n' : '-');
if (dataPtr <= 3 && id[dataPtr] != '\0') {
fw_ver_check_error = false;
}
}
if (fw_ver_check_error) {
aml_printf("[update]ERR(L%d):", 776);
aml_printf("fw ver is error! Maybe SOC wrong state\n");
return -777;
}
if (id[3] == 0) {
if (id[4] == 1 && idLen > 5) {
printf("Need Password...Password check %s\n", id[5] == 1 ? "OK" : "NG");
}
if (id[7] && idLen > 7) {
printf("SupportedPageMap--\t map 6 0x%02x, map 7 0x%02x\n", id[6], id[7]);
}
}
return 0;
}
int update_sub_cmd_get_chipid (AmlUsbRomRW &rom, const char **argv) {
char id[16];
int ret = update_sub_cmd_identify_host(rom, 4, id);
if (ret) {
aml_printf("[update]ERR(L%d):", 847);
aml_printf("Fail in identifyHost, ret=%d\n", ret);
return -848;
}
if (id[3] && id[3] != 8) {
aml_printf("[update]ERR(L%d):", 852);
aml_printf("romStage not bl1/bl2, cannot support chipid\n");
return -853;
}
int idVer = id[1] | (id[0] << 8);
aml_printf("[update]idVer is 0x%x\n", idVer);
if (idVer < 0x200 || (idVer > 0x205 && idVer != 0x300)) {
aml_printf("[update]ERR(L%d):", 890);
aml_printf("idVer(0x%x) cannot support chipid\n", idVer);
return -891;
}
char chipID[16];
if (idVer == 0x203) {
char buf[40];
aml_printf("[update]get chpid by chip info page\n");
int actual = Aml_Libusb_get_chipinfo(rom.device, buf, 0x40, 1, 8000);
if (actual != 64) {
aml_printf("[update]ERR(L%d):", 882);
aml_printf("Fail in get chipinfo, want %d, actual %d\n", 64, actual);
}
memcpy(chipID, &buf[20], AML_CHIP_ID_LEN);
} else {
unsigned int offset;
switch (idVer) {
case 0x200:
case 0x201:
offset = 0xD9013C24;
break;
case 0x202:
offset = 0xC8013C24;
break;
case 0x204:
offset = 0xD900D400;
break;
case 0x205:
case 0x300:
offset = 0xFFFCD400;
break;
default:
offset = 0;
}
if (Aml_Libusb_Ctrl_RdWr(rom.device, offset, chipID, 12, 1, 5000)) {
aml_printf("[update]ERR(L%d):", 898);
aml_printf("Fail in read chip id via ReadMem\n");
return -899;
}
}
printf("ChipID is:0x");
for (int i = 0; i < AML_CHIP_ID_LEN; ++i) {
printf("%02x", (unsigned char) chipID[i]);
}
putchar('\n');
return 0;
}
int update_sub_cmd_tplcmd (AmlUsbRomRW &rom, const char *tplCmd) {
unsigned int dataSize;
char dest[128] = {};
memcpy(dest, tplCmd, strlen(tplCmd) + 1);
dest[66] = 1;
AmlUsbRomRW cmd = {.device = rom
.device, .bufferLen = 68, .buffer = dest, .pDataSize = &dataSize};
if (AmlUsbTplCmd(&cmd) == 0) {
char reply[64] = {};
AmlUsbRomRW cmd = {.device = rom.device, .bufferLen = 64, .buffer = reply};
for (int retry = 5; retry > 0; --retry) {
if (AmlUsbReadStatus(&cmd) == 0) {
printf("reply %s \n", reply);
return 0;
}
}
}
puts("ERR: AmlUsbTplCmd failed!");
return 948;
}
int update_sub_cmd_mread (AmlUsbRomRW &rom, int argc, const char **argv) {
if (argc <= 3) {
update_help();
return 955;
}
const char *storeOrMem = argv[0];
const char *partition = argv[1];
const char *filetype = argv[2];
long readSize = strtoll(argv[3], nullptr, 0);
if (strcmp("normal", filetype) != 0 || !readSize) {
aml_printf("Err args in mread: check filetype and readSize\n");
return 968;
}
char buffer[128] = {};
snprintf((char *) buffer, sizeof(buffer), "upload %s %s %s 0x%lx", storeOrMem,
partition, filetype, readSize);
buffer[66] = 1;
rom.buffer = buffer;
rom.bufferLen = 68;
if (AmlUsbBulkCmd(&rom)) {
aml_printf("ERR: AmlUsbBulkCmd failed!\n");
return 983;
}
rom.bufferLen = readSize;
if (ReadMediaFile(&rom, argc <= 4 ? nullptr : argv[4], readSize)) {
aml_printf("ERR: ReadMediaFile failed!\n");
return 990;
}
return 0;
}
int main (int argc, const char **argv) {
aml_init();
if (argc == 1) {
update_help();
return 0;
}
const char *cmd = argv[1];
const char **cmdArgv = argv + 3;
int cmdArgc = argc - 3;
if (!strcmp(cmd, "help")) {
update_help();
return 0;
}
int dev_no; // [rsp+20h] [rbp-3D0h]
int result; // [rsp+28h] [rbp-3C8h]
int v24; // [rsp+3Ch] [rbp-3B4h]
const char *s1; // [rsp+48h] [rbp-3A8h]
const char *str_dev_no; // [rsp+58h] [rbp-398h]
AmlUsbRomRW rom = {}; // [rsp+80h] [rbp-370h]
char scan_mass_storage[4] = {}; // [rsp+140h] [rbp-2B0h]
char dest[512] = {}; // [rsp+1D0h] [rbp-220h]
dev_no = -2;
str_dev_no = nullptr;
FILE *fp = nullptr;
char *buffer = nullptr;
int success = 0;
result = -1015;
if (!strcmp(cmd, "scan")) {
if (argc == 2) {
update_scan(nullptr, 1, -2, &success, nullptr);
} else if (argc == 3) {
str_dev_no = argv[2];
if (!strncmp(str_dev_no, "mptool", 7)) {
update_scan(nullptr, 1, -2, &success, nullptr);
} else if (!strncmp(str_dev_no, "msdev", 6) &&
update_scan(nullptr, 1, -2, &success, scan_mass_storage) != 0) {
puts("can not find device");
}
}
return 0;
}
// find dev no
if (argc > 2) {
std::string strArgDev(argv[2]);
if (strArgDev.compare(0, 3, "dev") == 0) {
if (strArgDev.length() > 5) {
aml_printf("[update]ERR(L%d):", 1062);
aml_printf("devPara(%s) err\n", strArgDev.c_str());
goto finish;
}
dev_no = strtol(strArgDev.substr(3, -1).c_str(), nullptr, 10);
if (dev_no > 16) {
aml_printf("[update]ERR(L%d):", 1068);
aml_printf("dev_no(%d) too large\n", dev_no);
goto finish;
}
cmdArgv = argv + 3;
} else if (strArgDev.compare(0, 5, "path-") == 0) {
aml_printf("[update]devPath is [%s]\n", strArgDev.substr(5, -1).c_str());
rom.device = AmlGetDeviceHandle("WorldCup Device",
(char *) strArgDev.substr(5, -1).c_str());
} else {
dev_no = 0;
cmdArgv = argv + 2;
++cmdArgc;
}
} else {
dev_no = 0;
cmdArgc = 0;
}
if (!rom.device && update_scan((void **) &rom.device, 0, dev_no, &success, nullptr) != 0) {
aml_printf("[update]ERR(L%d):", 1090);
aml_printf("can not find device\n");
goto finish;
}
if (!rom.device) {
aml_printf("[update]ERR(L%d):", 1094);
aml_printf("can not open dev[%d] device, maybe it not exist!\n", dev_no);
goto finish;
}
if (!strcmp(cmd, "run") || !strcmp(cmd, "rreg")) {
return update_sub_cmd_run_and_rreg(rom, cmd, cmdArgv, cmdArgc);
}
if (!strcmp("password", cmd)) {
result = update_sub_cmd_set_password(rom, cmdArgv, cmdArgc);
goto finish;
}
if (!strcmp("chipinfo", cmd)) {
if (cmdArgc <= 0) {
aml_printf("[update]ERR(L%d):", 1112);
aml_printf("paraNum(%d) too small for chipinfo\n", argc);
} else {
result = update_sub_cmd_get_chipinfo(rom, cmdArgv, cmdArgc);
}
goto finish;
}
if (!strcmp(cmd, "chipid")) {
result = update_sub_cmd_get_chipid(rom, cmdArgv);
goto finish;
}
if (!strcmp(cmd, "write") || !strcmp(cmd, "read") || !strcmp(cmd, "wreg") ||
!strcmp(cmd, "dump") || !strcmp(cmd, "boot") || !strcmp(cmd, "cwr") ||
!strcmp(cmd, "write2")) {
result = update_sub_cmd_read_write(rom, cmd, cmdArgv, cmdArgc);
goto finish;
}
if (!strcmp(cmd, "msdev") || !strcmp(cmd, "msget") || !strcmp(cmd, "msset")) {
if (!strcmp(cmd, "msset")) {
dev_no = 0;
if (argc == 4) {
str_dev_no = argv[2];
if (strlen(str_dev_no) != 4 || strncmp(str_dev_no, "dev", 3) != 0) {
puts("please input dev_no like 'dev0'");
goto finish;
}
dev_no = str_dev_no[3] - 48;
} else if (argc != 3) {
update_help();
goto finish;
}
s1 = argv[argc - 1];
if (argc == 3 && !strncmp(s1, "dev", 3)) {
char yesno;
printf("if you want input cmd \"%s\" (Y/N): ", s1);
scanf("%c", &yesno);
if (strncasecmp(&yesno, "y", 1) != 0) {
goto finish;
}
}
} else if (argc == 2) {
dev_no = 0;
} else {
if (argc != 3) {
update_help();
return 0;
}