-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathupdate.cpp
More file actions
1512 lines (1318 loc) · 37.7 KB
/
update.cpp
File metadata and controls
1512 lines (1318 loc) · 37.7 KB
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 <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include "defs.h"
#include "gettime.h"
#include "Amldbglog.h"
#include "AmlLibusb.h"
#include "AmlUsbScanX3.h"
#include "UsbRomDrv.h"
#include "scan.h"
#define AML_CHIP_ID_LEN 12
static bool simg_probe (const unsigned char *buf, unsigned int len) {
const unsigned char magic[] = {0x3A, 0xFF, 0x26, 0xED};
const unsigned char magic2[] = {0x28, 0, 0x12, 0};
if (len < 28 || memcmp(buf, magic, 4) != 0 || GET_U16(&buf[4]) != 1 ||
memcmp(&buf[8], magic2, 4) != 0) {
return false;
}
aml_printf("[update]sparse format detected\n");
return true;
}
static bool is_file_format_sparse (const char *filename) {
FILE *stream = fopen(filename, "rb");
if (stream == NULL) {
aml_printf("[update]ERR(L%d):", 68);
aml_printf("Fail to open file in mode rb\n");
return false;
}
unsigned char *buf = new unsigned char[0x2000];
size_t len = fread(buf, 1, 0x2000, stream);
fclose(stream);
bool ret = simg_probe(buf, (unsigned int) len);
if (buf != NULL) {
delete[] buf;
}
return ret;
}
#ifndef BUGFIX
struct DownloadProgressInfo {
long nBytes;
int percentageBegin;
int percentageEnd;
int percentageRange;
int percentage;
unsigned long unshownBytes;
unsigned int startTime;
unsigned int percentBytes;
unsigned int updateBytes;
unsigned int updatePercentBytes;
char prompt[16];
DownloadProgressInfo (long nBytes_, const char *prompt_);
int update_progress (unsigned int len);
};
DownloadProgressInfo::DownloadProgressInfo (long nBytes_, const char *prompt_) {
nBytes = nBytes_;
percentageBegin = 0;
percentageEnd = 100;
percentage = 0;
unshownBytes = 0;
percentageRange = percentageEnd - percentageBegin;
memset(prompt, 0, sizeof(prompt));
memcpy(prompt, prompt_, sizeof(prompt) - 1);
if (nBytes < percentageRange) {
nBytes = percentageRange;
}
percentBytes = nBytes / percentageRange;
updateBytes = aml_max(percentBytes, 0x400000);
updatePercentBytes = updateBytes / percentBytes;
}
int DownloadProgressInfo::update_progress (unsigned int len) {
unshownBytes += len;
if (unshownBytes == len && percentageBegin == percentage) {
startTime = timeGetTime();
}
if (unshownBytes < updateBytes) {
return 0;
}
percentage += unshownBytes / percentBytes;
unshownBytes %= percentBytes;
printf("%s %%%d\r", prompt, percentage);
fflush(stdout);
if (updatePercentBytes + percentage >= percentageEnd) {
printf("\b\b\b\b\b\b\b\b\b\r");
printf(
"[%s]OK:<%ld>MB in <%u>Sec\n", prompt, nBytes >> 20,
(unsigned int) ((timeGetTime() - startTime) / 1000));
}
return 0;
}
#else
struct DownloadProgressInfo {
size_t nBytes;
time_t startTime;
size_t unshownBytes;
size_t transferSize;
size_t updateBytes;
char prompt[16];
DownloadProgressInfo (size_t nBytes_, const char *prompt_);
void update_progress (size_t len);
};
DownloadProgressInfo::DownloadProgressInfo (
size_t nBytes_, const char *prompt_) {
nBytes = nBytes_;
startTime = 0;
unshownBytes = 0;
transferSize = 0;
updateBytes = aml_max(nBytes / 100, 0x400000);
memset(prompt, 0, sizeof(prompt));
memcpy(prompt, prompt_, sizeof(prompt) - 1);
}
void DownloadProgressInfo::update_progress (size_t len) {
if (transferSize == 0) {
startTime = timeGetTime();
}
unshownBytes += len;
transferSize += len;
if (unshownBytes < updateBytes && transferSize < nBytes) {
return;
}
unshownBytes = 0;
if (transferSize < nBytes) {
printf("%s %%%d\r", prompt, (int) (100 * transferSize / nBytes));
fflush(stdout);
} else {
printf(
"[%s]OK:<%ld>MB in <%u>Sec\n", prompt, nBytes >> 20,
(unsigned int) ((timeGetTime() - startTime) / 1000));
}
}
#endif
#ifndef BUGFIX
static std::string strToLower (const std::string &src) {
std::string dst(src);
std::transform(src.begin(), src.end(), dst.begin(), tolower);
return dst;
}
#endif
static unsigned long simple_strtoul (
const char *cp, char **endp, unsigned int base) {
#ifndef BUGFIX
if (cp[0] == '0') {
cp++;
if (cp[1] == 'x' && isxdigit(cp[2])) {
base = 16;
cp += 2;
}
if (base == 0) {
base = 8;
}
}
if (base == 0) {
base = 10;
}
unsigned long result = 0;
for (; isxdigit(*cp); cp++) {
unsigned int d;
if (*cp - '0' > 9) {
d = (islower(*cp) ? toupper(*cp) : *cp) - '7';
} else {
d = *cp - '0';
}
if (d >= base) {
break;
}
result = base * result + d;
}
if (endp) {
*endp = (char *) cp;
}
return result;
#else
return strtoul(cp, endp, base);
#endif
}
static void _print_memory_view (
char *buf, unsigned int size, unsigned int addr) {
for (unsigned int line = 0; line < (size + 15) >> 4; line++) {
printf("\n%08X: ", addr);
unsigned int len = 16 * (line + 1) <= size ? 16 : size & 0xF;
for (unsigned int col = 0; col < (len + 3) >> 2; col++) {
printf("%08x ", *(unsigned int *) &buf[4 * (4 * line + col)]);
}
addr += 16;
}
putchar('\n');
}
static int WriteMediaFile (struct AmlUsbRomRW *rom, const char *filename) {
FILE *stream = fopen(filename, "rb");
if (stream == NULL) {
aml_printf("Open file %s failed\n", filename);
return -1;
}
fseeko(stream, 0, SEEK_END);
unsigned long long fileSize = ftello(stream);
unsigned long long percentSize = fileSize / 100;
unsigned long long updateSize = aml_max(fileSize / 100, 0x400000u);
fseek(stream, 0, SEEK_SET);
time_t startTime = timeGetTime();
char *buf = (char *) malloc(0x10000);
unsigned int addr = 0;
unsigned long long transferSize = 0;
unsigned long long shownSize = 0;
unsigned long long remain = fileSize;
while (remain != 0) {
unsigned int bulkSize = aml_min(remain, 0x10000u);
fread(buf, 1, bulkSize, stream);
unsigned int actualLen;
rom->buffer = buf;
rom->bufferLen = bulkSize;
rom->pDataSize = &actualLen;
rom->address = addr;
if (AmlWriteMedia(rom) != 0) {
aml_printf("AmlWriteMedia failed\n");
break;
}
transferSize += bulkSize;
remain -= bulkSize;
addr++;
if (addr == 1) {
puts("Downloading....");
}
if (transferSize - shownSize >= updateSize) {
int percentage = 100 * transferSize / fileSize;
printf(
"[%3d%%/%5uMB]\r", percentage, (unsigned int) (transferSize >> 20));
fflush(stdout);
shownSize = percentSize * percentage;
}
}
aml_printf(
"[update]Cost time %dSec \n",
(int) (timeGetTime() - startTime) / 1000);
aml_printf(
"[update]Transfer size 0x%llxB(%lluMB)\n", transferSize,
transferSize >> 20);
free(buf);
fclose(stream);
return remain == 0 ? 0 : -1;
}
static int ReadMediaFile (
struct AmlUsbRomRW *rom, const char *filename, long size) {
FILE *stream = NULL;
if (filename) {
stream = fopen(filename, "wb");
if (stream == NULL) {
aml_printf("Open file %s failed\n", filename);
return -1;
}
}
char *buf = (char *) malloc(0x10000);
DownloadProgressInfo info(size, "Uploading");
unsigned int offset = 0;
while (size != 0) {
unsigned int actualLen;
rom->buffer = buf;
rom->bufferLen = aml_min(size, 0x10000u);
rom->pDataSize = &actualLen;
if (AmlReadMedia(rom) != 0) {
aml_printf("AmlReadMedia failed\n");
break;
}
if (filename) {
fwrite(buf, 1, actualLen, stream);
info.update_progress(actualLen);
} else {
_print_memory_view(buf, actualLen, offset);
}
offset += actualLen;
size -= actualLen;
}
if (buf != NULL) {
free(buf);
}
if (stream != NULL) {
fclose(stream);
}
return size != 0 ? -1 : 0;
}
static void update_help () {
puts("====>Amlogic update USB tool(Ver 1.7.2) 2018/04<=============");
puts(
"update\t<command>\t[device name]\t<arg0>\t<arg1>\t<arg2>\t...\n"
"\nCommon Commands:\n"
"update <partition>: Burn a partition with a partition image\n"
"update <mwrite> : Burn data to media or memory\n"
"update <mread> : Dump a data from media or memory to pc and save as a file\n"
"update <tplcmd> : like bulkcmd\n"
"update <bulkcmd> : pass and exec a command platform bootloader can support\n"
"update <write> : Down a file to memory\n"
"update <run> : Run code from memory address\n"
"update <read> : Dump data from memory:\n"
"update <wreg> : set one 32bits reg:\n"
"update <rreg> : Dump data from reg:\n"
"update <password> : unlock chip:\n"
"update <chipinfo> : get chip info at page index:\n"
"update <chipid> : get chip id\n"
"update <bl2_boot> : boot fip format u-boot.bin\n"
"\nCommon Commands format:\n"
"update partition partName imgFilePath [imgFileFmt] [sha1VeryFile]\n"
"\t\te.g.--\tupdate partition boot z:\\a\\b\\boot.img [normal] //format normal is optional\n"
"\t\te.g.--\tupdate partition system z:\\xxxx\\system.img [sparse] //format sparse is optional\n"
"\t\te.g.--\tupdate partition upgrade z:\\xxxx\\upgrade.ubifs.img ubifs //format ubifs is MANDATORY\n"
"\nupdate bulkcmd \"burning cmd or u-boot cmd\"\n"
"\t\te.g.--\tupdate bulkcmd \"disk_intial 0\" //cmd to init flash for usb burning\n"
"\t\te.g.--\tupdate bulkcmd \"printenv\" //uboot command 'printenv'\n"
"\nupdate mread store/mem partName/memAddress\n"
"\t\te.g.--\tupdate mread store boot normal 0x200000 d:\boot.dump //upload 32M of boot partition in path d:\boot.dump\n"
"\t\te.g.--\tupdate mread mem 0x1080000 normal d:\\mem_2M.dump //upload 2M memory at address 0x1080000 in path d:\\mem_2M.dump\n"
"\t\te.g.--\tupdate chipinfo pageIndex dumpFilePath nBytes startOffset\n"
"\nupdate cwr/write filePath memAddress [nBytes] [fileOffset]\n"
"\t\te.g.--\tupdate cwr/write z:/u-boot.bin 0xffa0000 0x10000\n"
"\t\te.g.--\tupdate cwr/write z:/u-boot.bin.usb.bl2 0xffa0000\n"
"\t\te.g.--\tupdate write z:/u-boot.bin.usb.tpl 0x200c000\n"
"\t\te.g.--\tupdate write z:/u-boot.bin 0x200c000 0 0xc000");
puts("====>Amlogic update USB tool(Ver 1.7.2) 2018/04<=============");
}
static int update_scan (
void **result, int print_devices, int dev_no, int *success,
char *scan_mass_storage) {
int ret = 0;
*success = -1;
char *buf = new char[0x800];
memset(buf, 0, 0x800);
char *candidateDevices[16] = {};
candidateDevices[0] = buf;
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);
ret = -182;
goto end;
}
if (dev_no != -2 && nDevices) {
if (dev_no + 1 > nDevices) {
aml_printf("[update]ERR(L%d):", 190);
aml_printf(
"dev_no(%d) too only, only [%d] (%s)devices Connected\n", dev_no,
nDevices, vendorName);
ret = -191;
goto end;
}
if (!scan_mass_storage) {
if (!result) {
aml_printf("[update]ERR(L%d):", 202);
aml_printf("handle for (%s) NULL is invalid\n", vendorName);
ret = -203;
goto end;
}
*result = 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_devices) {
printf(
"\t-------%sdevices list -------\n",
scan_mass_storage ? "Mass Storage " : "MPtool ");
for (int i = 0; i < nDevices; ++i) {
printf("WorldCup[%02d].%s\n", i, candidateDevices[i]);
}
}
end:
if (buf != NULL) {
delete[] buf;
}
return ret;
}
static int do_cmd_mwrtie (
const char **argv, int argc, struct AmlUsbRomRW &rom) {
FILE *stream = NULL;
int ret = 0;
do {
const char *readFile = argv[0];
stream = fopen(readFile, "rb");
if (stream == NULL) {
aml_printf("Open file %s failed\n", readFile);
ret = -236;
goto end;
}
fseeko(stream, 0, SEEK_END);
off_t fileSize = ftello(stream);
fclose(stream);
stream = NULL;
aml_printf("file size is 0x%llx\n", (unsigned long long) fileSize);
if (!fileSize) {
aml_printf("file size 0!!\n");
return -103;
}
char buf[128] = {};
snprintf(
buf, sizeof(buf), "download %s %s %s 0x%lX", argv[1], argv[2], argv[3],
(long) fileSize);
buf[66] = 1;
unsigned int actualLen;
rom.buffer = buf;
rom.bufferLen = 68;
rom.pDataSize = &actualLen;
if (AmlUsbTplCmd(&rom) != 0) {
goto end;
}
memset(buf, 0, 0x80);
int retry;
for (retry = 1; retry != 0; retry--) {
rom.buffer = buf;
rom.bufferLen = 64;
rom.pDataSize = &actualLen;
if (AmlUsbReadStatus(&rom) == 0) {
break;
}
usleep(1000000);
}
if (retry == 0) {
aml_printf("Read status failed\n");
ret = -300;
goto end;
}
if (strncmp(rom.buffer, "success", 7) != 0) {
aml_printf("[update]ERR(L%d):", 306);
aml_printf("cmdret=[%s]\n", rom.buffer);
ret = -307;
goto end;
}
if (WriteMediaFile(&rom, readFile) != 0) {
aml_printf("ERR:write data to media failed\n");
ret = -314;
goto end;
}
strcpy(buf, "download get_status");
buf[66] = 1;
rom.buffer = buf;
rom.bufferLen = 68;
rom.pDataSize = &actualLen;
if (AmlUsbBulkCmd(&rom) != 0) {
aml_printf("[update]ERR(L%d):", 327);
aml_printf("AmlUsbBulkCmd failed!\n");
ret = -328;
goto end;
}
const char *verifyFile = argc <= 4 ? NULL : argv[4];
if (verifyFile != NULL) {
memset(buf, 0, 0x80);
stream = fopen(verifyFile, "rb");
if (stream == NULL) {
aml_printf("Open file %s failed\n", verifyFile);
goto end;
}
strcpy(buf, "verify ");
fread(&buf[7], 1, 0x79, stream);
fclose(stream);
stream = NULL;
buf[66] = 1;
rom.buffer = buf;
rom.bufferLen = 68;
rom.pDataSize = &actualLen;
if (AmlUsbBulkCmd(&rom) != 0) {
aml_printf("ERR: AmlUsbBulkCmd failed!\n");
ret = -354;
goto end;
}
}
} while (0);
ret = 0;
aml_printf("[update]mwrite success\n");
end:
if (stream != NULL) {
fclose(stream);
}
if (rom.device != NULL) {
rom.device = NULL;
}
return ret;
}
static int update_sub_cmd_run_and_rreg (
struct AmlUsbRomRW &rom, const char *cmd, const char **argv, int argc) {
if (argc <= 0) {
aml_printf("[update]ERR(L%d):", 381);
aml_printf(
"argc(%d)<2, address for cmd[%s] not assigned\n",
#ifndef BUGFIX
argv,
#else
argc,
#endif
cmd);
return -382;
}
rom.address =
#ifndef BUGFIX
simple_strtoul(strToLower(argv[0]).c_str(), NULL, 0);
#else
strtoul(argv[0], NULL, 0);
#endif
if (strcmp(cmd, "run") == 0) {
aml_printf("[update]Run at Addr %x\n", rom.address);
rom.buffer = (char *) &rom.address;
int ret = AmlUsbRunBinCode(&rom);
if (ret) {
puts("ERR: Run cmd failed");
}
return ret;
}
unsigned int addr =
#ifndef BUGFIX
simple_strtoul(strToLower(argv[1]).c_str(), NULL, 0);
#else
strtoul(argv[1], NULL, 0);
#endif
const char *saveFile = argc <= 2 ? NULL : argv[2];
FILE *saveStream = saveFile == NULL ? NULL : fopen(saveFile, "wb");
char *buf = new char[0x100000];
bool isRreg = strcmp(cmd, "rreg") == 0;
FILE *readStream = NULL;
unsigned int transferSize;
if (isRreg) {
transferSize = rom.address;
} else {
readStream = fopen(argv[0], "rb");
fseeko(readStream, 0, SEEK_END);
transferSize = ftell(readStream);
fseek(readStream, 0, SEEK_SET);
}
aml_printf("[update]Total tansfer size 0x%x\n", transferSize);
int ret = 0;
unsigned int offset = 0;
while (offset < transferSize) {
unsigned int bulkSize = aml_min(transferSize - offset, 0x100000u);
if (readStream != NULL) {
fread(buf, bulkSize, 1, readStream);
}
ret = Aml_Libusb_Ctrl_RdWr(rom.device, addr, buf, bulkSize, isRreg, 5000);
if (ret) {
aml_printf("[update]ret=%d\n", ret);
break;
}
if (isRreg) {
if (saveStream != NULL) {
fwrite(buf, bulkSize, 1, saveStream);
} else {
_print_memory_view(buf, bulkSize, addr);
}
}
offset += bulkSize;
addr += bulkSize;
}
if (buf != NULL) {
delete[] buf;
}
if (saveStream != NULL) {
fclose(saveStream);
}
return ret;
}
static int update_sub_cmd_set_password (
struct AmlUsbRomRW &rom, const char **argv, int argc) {
(void) argc;
const char *pwdFile = argv[0];
FILE *stream = fopen(pwdFile, "rb");
if (stream == NULL) {
aml_printf("[update]ERR(L%d):", 478);
aml_printf("Open file %s failed\n", pwdFile);
return -1;
}
fseeko(stream, 0, SEEK_END);
size_t totalFileSz = ftello(stream);
if (totalFileSz > 64) {
aml_printf("[update]ERR(L%d):", 485);
aml_printf(
"size of file(%s) is %lld too large!!\n", pwdFile,
(long long) totalFileSz);
fclose(stream);
return -487;
}
fseek(stream, 0, SEEK_SET);
char buf[64];
size_t rdLen = fread(buf, 1, totalFileSz, stream);
if (rdLen != totalFileSz) {
aml_printf("[update]ERR(L%d):", 494);
aml_printf(
"FAil in read pwd file (%s), rdLen(%d) != TotalFileSz(%d)\n", pwdFile,
(int) rdLen, (int) totalFileSz);
fclose(stream);
return -496;
}
fclose(stream);
int ret = 0;
for (int i = 0; i <= 1 && (ret & 0x80000000) == 0; ++i) {
ret = Aml_Libusb_Password(rom.device, buf, rdLen, 8000);
if (!i) {
printf("Setting PWD..");
usleep(5000000);
}
}
aml_printf("Pwd returned %d\n", ret);
return ret <= 0 ? ret : 0;
}
static int update_sub_cmd_get_chipinfo (
struct AmlUsbRomRW &rom, const char **argv, 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):", 526);
aml_printf("pageSzMax %d < nBytes %d\n", 64, nBytes);
return -527;
}
char *buf = new char[0x40];
if (!buf) {
aml_printf("[update]ERR(L%d):", 523);
aml_printf("Fail in alloc buff\n");
return -524;
}
int ret;
FILE *stream = NULL;
int actualLen = Aml_Libusb_get_chipinfo(rom.device, buf, 64, pageIndex, 8000);
if (actualLen != 64) {
aml_printf("[update]ERR(L%d):", 537);
aml_printf("Fail in get chipinfo, want %d, actual %d\n", 64, actualLen);
ret = -538;
goto end;
}
if (strcmp("-", saveFile) == 0) {
_print_memory_view(&buf[startOffset], nBytes, startOffset);
ret = actualLen;
} else {
stream = fopen(saveFile, "wb");
if (stream == NULL) {
aml_printf("[update]ERR(L%d):", 547);
aml_printf("FAil in open file %s\n", saveFile);
ret = -548;
goto end;
}
if ((int) fwrite(&buf[startOffset], 1, nBytes, stream) != nBytes) {
aml_printf("[update]ERR(L%d):", 553);
aml_printf("FAil in save file %s\n", saveFile);
ret = -554;
goto end;
}
ret = 0;
}
end:
if (buf != NULL) {
delete[] buf;
}
if (stream != NULL) {
fclose(stream);
}
return ret;
}
static int update_sub_cmd_read_write (
struct AmlUsbRomRW &rom, const char *cmd, const char **argv, int argc) {
if (argc <= 1) {
aml_printf("[update]ERR(L%d):", 582);
aml_printf(
"cmd[%s] must at least %d parameters, but only %d\n", cmd, 2, argc);
return 583;
}
unsigned int addr =
#ifndef BUGFIX
simple_strtoul(strToLower(argv[1]).c_str(), NULL, 0);
#else
strtoul(argv[1], NULL, 0);
#endif
rom.address = addr;
FILE *stream = NULL;
char *buf = NULL;
int ret;
if (strcmp(cmd, "wreg") == 0) {
unsigned int val = htole32(simple_strtoul(argv[0], NULL, 16));
rom.buffer = (char *) &val;
rom.bufferLen = 4;
ret = Aml_Libusb_Ctrl_RdWr(
rom.device, rom.address, rom.buffer, rom.bufferLen, 0, 5000);
if (ret != 0) {
aml_printf("[update]ERR(L%d):", 601);
aml_printf("write register failed\n");
} else {
aml_printf("[update]operate finished!\n");
}
} else if (strcmp(cmd, "read") == 0 || strcmp("dump", cmd) == 0) {
const char *dumpFilename = argc <= 2 ? NULL : argv[2];
FILE *dumpStream = dumpFilename ? fopen(dumpFilename, "wb") : NULL;
unsigned int transferSize =
#ifndef BUGFIX
simple_strtoul(strToLower(argv[0]).c_str(), NULL, 0);
#else
strtoul(argv[0], NULL, 0);
#endif
#ifndef BUGFIX
rom.address = simple_strtoul(strToLower(argv[1]).c_str(), 0, 0);
addr = rom.address;
#endif
DownloadProgressInfo info(transferSize, "DUMP");
buf = new char[0x20000];
while (transferSize != 0) {
unsigned int bulkSize = aml_min(transferSize, 0x200u);
if (strcmp("dump", cmd) == 0) {
if (transferSize >= 0x10000) {
bulkSize = 0x10000;
} else if (transferSize >= 0x1000) {
bulkSize = 0x1000;
}
}
unsigned int actualLen;
rom.buffer = buf;
rom.bufferLen = bulkSize;
rom.pDataSize = &actualLen;
if (AmlUsbReadLargeMem(&rom) != 0) {
aml_printf("[update]ERR(L%d):", 638);
aml_printf("read device failed\n");
ret = -639;
goto end;
}
if (dumpStream == NULL) {
_print_memory_view(buf, bulkSize, rom.address);
} else {
unsigned int dumpFileSize = fwrite(buf, 1, bulkSize, dumpStream);
if (dumpFileSize != bulkSize) {
aml_printf("[update]ERR(L%d):", 647);
aml_printf(
"Want to write %dB to path[%s], but only %dB\n", bulkSize,
dumpFilename, dumpFileSize);
ret = -648;
break;
}
info.update_progress(bulkSize);
}
transferSize -= actualLen;
rom.address += actualLen;
}
if (dumpStream != NULL) {
fclose(dumpStream);
dumpStream = NULL;
}
ret = 0;
} else if (strcmp(cmd, "write") == 0 || strcmp(cmd, "cwr") == 0) {
stream = fopen(argv[0], "rb");
if (stream == NULL) {
aml_printf("[update]ERR(L%d):", 688);
aml_printf("ERR: can not open the %s file\n", argv[0]);
ret = -689;
goto end;
}
bool isCwr = strcmp(cmd, "write") != 0;
buf = (char *) malloc(0x10008);
unsigned int transferSize =
argc <= 2 ? 0 : simple_strtoul(argv[2], NULL, 0);
unsigned int skip = argc <= 3 ? 0 : simple_strtoul(argv[3], NULL, 0);
if (transferSize == 0) {
fseek(stream, 0, SEEK_END);
transferSize = ftell(stream) - skip;
}
fseek(stream, skip, SEEK_SET);
unsigned int nBytes = 0;
while (transferSize != 0) {
unsigned int bulkSize = aml_min(transferSize, isCwr ? 0x40 : 0x10000);
fread(buf + 8, 1, bulkSize, stream);
unsigned int actualLen;
rom.buffer = buf + 4;
rom.bufferLen = bulkSize;
rom.pDataSize = &actualLen;
if (isCwr) {
SET_U32(&rom.buffer[0], rom.address);
rom.bufferLen += 4;
ret = AmlUsbCtrlWr(&rom);
} else {
rom.buffer = buf + 8;
ret = AmlUsbWriteLargeMem(&rom);
}
if (ret != 0) {
puts("ERR: write data to device failed");
ret = -735;
goto end;
}
transferSize -= bulkSize;
rom.address += bulkSize;
nBytes += bulkSize;
printf("..");
}
printf("\nTransfer Complete! total size is %d Bytes\n", nBytes);
ret = 0;
} else {
ret = 0;
}
end:
if (stream != NULL) {
fclose(stream);
}
if (buf != NULL) {
free(buf);
}
if (rom.device != NULL) {
rom.device = NULL;
}
return ret;
}
static int update_sub_cmd_identify_host (
struct AmlUsbRomRW &rom, int idLen, char *id) {
if (idLen <= 3) {
aml_printf("[update]ERR(L%d):", 764);
aml_printf("idLen %d invalid\n", idLen);
return -765;
}
unsigned int actualLen = 0;
char tmp[16];
char *buf = id == NULL ? tmp : id;
rom.buffer = buf;
rom.bufferLen = idLen;
rom.pDataSize = &actualLen;
if (AmlUsbIdentifyHost(&rom) != 0) {
puts("ERR: get info from device failed");
return 774;
}
printf("This firmware version is ");
bool err = true;
for (unsigned int i = 0; i < actualLen; i++) {
printf("%d%c", buf[i], i + 1 >= actualLen ? '\n' : '-');
if (i <= 3 && buf[i] != '\0') {
err = false;
}
}
if (err) {
aml_printf("[update]ERR(L%d):", 783);
aml_printf("fw ver is error! Maybe SOC wrong state\n");
return -784;
}
if (buf[3] == 0) {
if (buf[4] == 1 && idLen > 5) {
printf("Need Password...Password check %s\n", buf[5] == 1 ? "OK" : "NG");
}
if (buf[7] && idLen > 7) {
printf(
"SupportedPageMap--\t map 6 0x%02x, map 7 0x%02x\n", buf[6], buf[7]);
}
}
return 0;
}
static int update_sub_cmd_get_chipid (
struct AmlUsbRomRW &rom, const char **argv) {
(void) argv;
char id[16];
int ret = update_sub_cmd_identify_host(rom, 4, id);
if (ret) {
aml_printf("[update]ERR(L%d):", 854);
aml_printf("Fail in identifyHost, ret=%d\n", ret);
return -855;
}
if (id[3] != 0 && id[3] != 8) {
aml_printf("[update]ERR(L%d):", 859);
aml_printf("romStage not bl1/bl2, cannot support chipid\n");
return -860;
}
int idVer = id[1] | (id[0] << 8);
aml_printf("[update]idVer is 0x%x\n", idVer);