forked from espressif/esptool-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESPLoader.js
1449 lines (1287 loc) · 57 KB
/
ESPLoader.js
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
'use strict';
class ESP8266ROM {
static CHIP_NAME = "ESP8266";
static CHIP_DETECT_MAGIC_VALUE = 0xfff0c101;
}
class ESP32ROM {
static CHIP_NAME = "ESP32";
static IMAGE_CHIP_ID = 0;
static CHIP_DETECT_MAGIC_VALUE = 0x00f01d83;
static EFUSE_RD_REG_BASE = 0x3ff5a000;
static DR_REG_SYSCON_BASE = 0x3ff66000;
static UART_CLKDIV_REG = 0x3ff40014;
static UART_CLKDIV_MASK = 0xFFFFF;
static UART_DATE_REG_ADDR = 0x60000078;
static XTAL_CLK_DIVIDER= 1;
static FLASH_WRITE_SIZE = 0x400;
static BOOTLOADER_FLASH_OFFSET = 0x1000;
static FLASH_SIZES = {'1MB':0x00, '2MB':0x10, '4MB':0x20, '8MB':0x30, '16MB':0x40};
static SPI_REG_BASE = 0x3ff42000;
static SPI_USR_OFFS = 0x1c;
static SPI_USR1_OFFS = 0x20;
static SPI_USR2_OFFS = 0x24;
static SPI_W0_OFFS = 0x80;
static SPI_MOSI_DLEN_OFFS = 0x28;
static SPI_MISO_DLEN_OFFS = 0x2c;
static TEXT_START = 0x400BE000;
static ENTRY = 0x400BE598;
static DATA_START = 0x3FFDEBA8;
static ROM_DATA = "CMD8Pw==";
static ROM_TEXT = "" +
"H4sICNv8hGAAA2VzcDMyc3R1Yi5iaW4AVRZ/UBTn9d3e3XIHqx72BvAkyd7KryOY"+
"QaQCji17l8sBmjZCEoRMpwkSTjOxmfNCCjLY3Gp6QOJ04DSFIzguV4lIpImEVIhl"+
"clB70QnOCM0YE2MDRFI1kBBEgwr79X1HnUn/ePu9fd/79b1f83QwlxuPAAhHv13M"+
"NeC5wQrmuyTIQzZjvUPPFA3PV7ZNsFoAsN4i0m1K7NTU62S5ckwHUYKT+Y93jph/"+
"oPQc5oEZgHl+Lnc+PayraZGS6/UeT0JxPmm6+9M/ygoG5AUKADrkn1wg5nuE0yFb"+
"A9N0j0hhH3w7Ab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m5FMqbqN//Pjg9ZGKItI5aH5WOa66NtL+"+
"W2VAMzQYPzsYd4WRdlBz5yP7kjp3t4zLp4P9SXJVsE/fZe8s6zoddUI+wffr5Xe1"+
"/OngcOvoZQ/34IW/7o+5Ad2Mf/zcSOKzpGX87IhFLz2ymJJ94FjwyLqgP3jRAsU9"+
"ty+eAMmyaJM/LihxABzY1MaEapUY5og1YVGQz1qecuxRQDIjfs5S4vi9AilCccbh"+
"bq1kpw61pyvSLykiWenX8RLeZ8RkvIX3m+4naQNFxhhgdyh0r8E95zCD21qKus6/"+
"YjiDW+mNANzr1LUT2ElJKyOShBjc24Bubh7Lmjp/Eifg5awjAiP9ZbJfx620qNfq"+
"wqvdldrZOj9LCYJ8mer+L0DR4a0UDQAA";
static read_efuse = async (loader, offset) => {
var addr = this.EFUSE_RD_REG_BASE + (4 * offset);
console.log("Read efuse " + addr);
return await loader.read_reg({addr: addr});
}
static get_pkg_version = async (loader) => {
var word3 = await this.read_efuse(loader, 3);
var pkg_version = (word3 >> 9) & 0x07;
pkg_version += ((word3 >> 2) & 0x1) << 3;
return pkg_version;
}
static get_chip_revision = async (loader) => {
var word3 = await this.read_efuse(loader, 3);
var word5 = await this.read_efuse(loader, 5);
var apb_ctl_date = await loader.read_reg({addr: this.DR_REG_SYSCON_BASE + 0x7C});
var rev_bit0 = (word3 >> 15) & 0x1;
var rev_bit1 = (word5 >> 20) & 0x1;
var rev_bit2 = (apb_ctl_date >> 31) & 0x1;
if (rev_bit0 != 0) {
if (rev_bit1 != 0) {
if (rev_bit2 != 0) {
return 3;
} else {
return 2;
}
} else {
return 1;
}
}
return 0;
}
static get_chip_description = async (loader) => {
var chip_desc = ["ESP32-D0WDQ6", "ESP32-D0WD", "ESP32-D2WD", "", "ESP32-U4WDH", "ESP32-PICO-D4", "ESP32-PICO-V3-02"];
var chip_name = "";
var pkg_version = await this.get_pkg_version(loader);
var chip_revision = await this.get_chip_revision(loader);
var rev3 = (chip_revision == 3);
var single_core = await this.read_efuse(loader, 3) & (1 << 0);
if (single_core != 0) {
chip_desc[0] = "ESP32-S0WDQ6";
chip_desc[1] = "ESP32-S0WD";
}
if (rev3) {
chip_desc[5] = "ESP32-PICO-V3";
}
if (pkg_version >= 0 && pkg_version <= 6) {
chip_name = chip_desc[pkg_version];
} else {
chip_name = "Unknown ESP32";
}
if (rev3 && (pkg_version === 0 || pkg_version === 1)) {
chip_name += "-V3";
}
return chip_name + " (revision " + chip_revision + ")";
}
static get_chip_features = async (loader) => {
var features = ["Wi-Fi"];
var word3 = await this.read_efuse(loader, 3);
var chip_ver_dis_bt = word3 & (1 << 1);
if (chip_ver_dis_bt === 0) {
features.push(" BT");
}
var chip_ver_dis_app_cpu = word3 & (1 << 0);
if (chip_ver_dis_app_cpu !== 0) {
features.push(" Single Core");
} else {
features.push(" Dual Core");
}
var chip_cpu_freq_rated = word3 & (1 << 13);
if (chip_cpu_freq_rated !== 0) {
var chip_cpu_freq_low = word3 & (1 << 12);
if (chip_cpu_freq_low !== 0) {
features.push(" 160MHz");
} else {
features.push(" 240MHz");
}
}
var pkg_version = await this.get_pkg_version(loader);
if ([2, 4, 5, 6].includes(pkg_version)) {
features.push(" Embedded Flash");
}
if (pkg_version === 6) {
features.push(" Embedded PSRAM");
}
var word4 = await this.read_efuse(loader, 4);
var adc_vref = (word4 >> 8) & 0x1F;
if (adc_vref !== 0) {
features.push(" VRef calibration in efuse");
}
var blk3_part_res = word3 >> 14 & 0x1;
if (blk3_part_res !== 0) {
features.push(" BLK3 partially reserved");
}
var word6 = await this.read_efuse(loader, 6);
var coding_scheme = word6 & 0x3;
var coding_scheme_arr = ["None", "3/4", "Repeat (UNSUPPORTED)", "Invalid"];
features.push(" Coding Scheme " + coding_scheme_arr[coding_scheme]);
return features;
}
static get_crystal_freq = async (loader) => {
var uart_div = await loader.read_reg({addr: this.UART_CLKDIV_REG}) & this.UART_CLKDIV_MASK;
var ets_xtal = (loader.transport.baudrate * uart_div) / 1000000 / this.XTAL_CLK_DIVIDER;
var norm_xtal;
if (ets_xtal > 33) {
norm_xtal = 40;
} else {
norm_xtal = 26;
}
if (Math.abs(norm_xtal - ets_xtal) > 1) {
loader.log("WARNING: Unsupported crystal in use");
}
return norm_xtal;
}
static _d2h(d) {
var h = (+d).toString(16);
return h.length === 1 ? '0' + h: h;
}
static read_mac = async (loader) => {
var mac0 = await this.read_efuse(loader, 1);
mac0 = mac0 >>> 0;
var mac1 = await this.read_efuse(loader, 2);
mac1 = mac1 >>> 0;
var mac = new Uint8Array(6);
mac[0] = (mac1 >> 8) & 0xff;
mac[1] = mac1 & 0xff;
mac[2] = (mac0 >> 24) & 0xff;
mac[3] = (mac0 >> 16) & 0xff;
mac[4] = (mac0 >> 8) & 0xff;
mac[5] = mac0 & 0xff;
return(this._d2h(mac[0])+":"+this._d2h(mac[1])+":"+this._d2h(mac[2])+":"+this._d2h(mac[3])+":"+this._d2h(mac[4])+":"+this._d2h(mac[5]));
}
static get_erase_size = function(offset, size) {
return size;
}
}
class ESP32S2ROM {
static CHIP_NAME = "ESP32-S2";
static IMAGE_CHIP_ID = 2;
static CHIP_DETECT_MAGIC_VALUE = 0x000007c6;
static MAC_EFUSE_REG = 0x3f41A044;
static EFUSE_BASE = 0x3f41A000;
static UART_CLKDIV_REG = 0x3f400014;
static UART_CLKDIV_MASK = 0xFFFFF;
static UART_DATE_REG_ADDR = 0x60000078;
static FLASH_WRITE_SIZE = 0x400;
static BOOTLOADER_FLASH_OFFSET = 0x1000;
static FLASH_SIZES = {'1MB':0x00, '2MB':0x10, '4MB':0x20, '8MB':0x30, '16MB':0x40};
static SPI_REG_BASE = 0x3f402000;
static SPI_USR_OFFS = 0x18;
static SPI_USR1_OFFS = 0x1c;
static SPI_USR2_OFFS = 0x20;
static SPI_W0_OFFS = 0x58;
static SPI_MOSI_DLEN_OFFS = 0x24;
static SPI_MISO_DLEN_OFFS = 0x28;
static TEXT_START = 0x40028000;
static ENTRY = 0x4002873C;
static DATA_START = 0x3FFE2BF4;
static ROM_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tc+gjbMQ0O5p9hRkbzVUKqO43"+
"bEN5sUboRMPxs3lac3yIOyHFvCeG045rx/mznHYqhR+OT/SOfSLjVfGTbY4cE1wN"+
"sKewd5n/OaJe65ks5FwbOt7u/Glxpxq/5ELVg/OX2pFPO797B7Qn1LGtY5D9vo4c"+
"W34uaONB+pF3oXBHcLfuEqqX9/pLSd+NJeX+Er14LMZed9HwukocW3r6nyD0hG5T"+
"5lUGBV+inIf/Am8w8EfhMoci6RMleHnQiuD71XzwevARlL/cmi84gg9/L+TC1aFI"+
"PhaAOeuxgvFPCyIcXl5oXscBJKBdOTgbirB0QdA+obr/HyfMWQisDwAA";
static get_pkg_version = async (loader) => {
var num_word = 3;
var block1_addr = this.EFUSE_BASE + 0x044;
var addr = block1_addr + (4 * num_word);
var word3 = await loader.read_reg({addr: addr});
var pkg_version = (word3 >> 21) & 0x0F;
return pkg_version;
}
static get_chip_description = async (loader) => {
var chip_desc = [ "ESP32-S2", "ESP32-S2FH16", "ESP32-S2FH32"];
var pkg_ver = await this.get_pkg_version(loader);
if (pkg_ver >= 0 && pkg_ver <=2) {
return chip_desc[pkg_ver];
} else {
return "unknown ESP32-S2";
}
}
static get_chip_features = async (loader) => {
var features = [ "Wi-Fi" ];
var pkg_ver = await this.get_pkg_version(loader);
if (pkg_ver == 1) {
features.push("Embedded 2MB Flash");
} else if (pkg_ver == 2) {
features.push("Embedded 4MB Flash");
}
var num_word = 4;
var block2_addr = this.EFUSE_BASE + 0x05C;
var addr = block2_addr + (4 * num_word);
var word4 = await loader.read_reg({addr: addr});
var block2_ver = (word4 >> 4) & 0x07;
if (block2_ver == 1) {
features.push("ADC and temperature sensor calibration in BLK2 of efuse");
}
return features;
}
static get_crystal_freq = async (loader) => {
return 40;
}
static _d2h(d) {
var h = (+d).toString(16);
return h.length === 1 ? '0' + h: h;
}
static read_mac = async (loader) => {
var mac0 = await loader.read_reg({addr: this.MAC_EFUSE_REG});
mac0 = mac0 >>> 0;
var mac1 = await loader.read_reg({addr: this.MAC_EFUSE_REG + 4});
mac1 = (mac1 >>> 0) & 0x0000ffff;
var mac = new Uint8Array(6);
mac[0] = (mac1 >> 8) & 0xff;
mac[1] = mac1 & 0xff;
mac[2] = (mac0 >> 24) & 0xff;
mac[3] = (mac0 >> 16) & 0xff;
mac[4] = (mac0 >> 8) & 0xff;
mac[5] = mac0 & 0xff;
return(this._d2h(mac[0])+":"+this._d2h(mac[1])+":"+this._d2h(mac[2])+":"+this._d2h(mac[3])+":"+this._d2h(mac[4])+":"+this._d2h(mac[5]));
}
static get_erase_size = function(offset, size) {
return size;
}
}
class ESP32S3BETA2ROM {
static CHIP_NAME = "ESP32-S3(beta2)";
static IMAGE_CHIP_ID = 4;
static CHIP_DETECT_MAGIC_VALUE = 0xeb004136;
static get_pkg_version = async (loader) => {
}
static get_chip_revision = async (loader) => {
}
static get_chip_description = async (loader) => {
}
static get_chip_features = async (loader) => {
}
static get_crystal_freq = async (loader) => {
}
static read_mac = async (loader) => {
}
}
class ESP32C3ROM {
static CHIP_NAME = "ESP32-C3";
static IMAGE_CHIP_ID = 5;
static CHIP_DETECT_MAGIC_VALUE = 0x6921506f;
static EFUSE_BASE = 0x60008800;
static MAC_EFUSE_REG = this.EFUSE_BASE + 0x044;
static UART_CLKDIV_REG = 0x3ff40014;
static UART_CLKDIV_MASK = 0xFFFFF;
static UART_DATE_REG_ADDR = 0x6000007C;
static FLASH_WRITE_SIZE = 0x400;
static BOOTLOADER_FLASH_OFFSET = 0x1000;
static FLASH_SIZES = {'1MB':0x00, '2MB':0x10, '4MB':0x20, '8MB':0x30, '16MB':0x40};
static SPI_REG_BASE = 0x60002000;
static SPI_USR_OFFS = 0x18;
static SPI_USR1_OFFS = 0x1C;
static SPI_USR2_OFFS = 0x20;
static SPI_MOSI_DLEN_OFFS = 0x24;
static SPI_MISO_DLEN_OFFS = 0x28;
static SPI_W0_OFFS = 0x58;
static TEXT_START = 0x40380000;
static ENTRY = 0x403805C8;
static DATA_START = 0x3FC96BA8;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QDwy4nrtPIaoBOa"+
"wWQb8i0yu4o83IXrbrvqkVdU8KY4oQzEdlISk5xOiBCqNgIkp5jA6rOoNuPlN86h"+
"0SR3z4+H5TfOoBHI3Wf0LCwLxMEdQYkW9eP5hQ4VosFQl3IV5Byun684qwIc7YZM"+
"fBR8XbaMIv8gp5tvA38t2c6Rh+XbT1CkUIPlATlFCpV1PPA3cU+o15SSvEMhNeLf"+
"FDd5gWYp3JOAKe3+RbLPhZoDhurVJnOjDIOvjQu3kx3AzQ8j7o0vuVg8Ij4pWEs5"+
"oVfDud4mm38X0c3TAlIPBhz0CTTzgmT7Sg3dsCqMG81ZdV62k8LxInFTDzCFc9Gp"+
"Ry/3rzN4QnijPsnlsNgTZHFlR/nVSuFNK8a0bVdsovA5Bgo3pYbrUjf92UIH3xv/"+
"i8X92b3xvw6UuPg/etPEjaXtCzf/0crUy+bG8haV0Zu2+yKl+ELGWUrb8XT4YBYo"+
"0jf1bei5N/4rj5Jdk64SXq8nrLmRHDEIIiMpXLsodBIwHMgRr6xoOaRuq5hVL69M"+
"rT6SQyfcbUmyn6851MmRg+B4BXzVKbxT/UcnbxVYBY2u0s5qrOas/xnZLb+FPMGR"+
"pnltVv+9jvHXkU4sXKfxG0NqhL7U68N6/PpKeyfjl0o+XMA5N/wc9+g6UfWl9s83"+
"J279YCuxbUpfYBg26DI+z0jM/CCTyLqft1P4h/4HprTSLAwNAAA=";
static get_pkg_version = async (loader) => {
var num_word = 3;
var block1_addr = this.EFUSE_BASE + 0x044;
var addr = block1_addr + (4 * num_word);
var word3 = await loader.read_reg({addr: addr});
var pkg_version = (word3 >> 21) & 0x0F;
return pkg_version;
}
static get_chip_revision = async (loader) => {
var block1_addr = this.EFUSE_BASE + 0x044;
var num_word = 3;
var pos = 18;
var addr = block1_addr + (4 * num_word);
var ret = (await loader.read_reg({addr: addr}) & (0x7 << pos)) >> pos;
return ret;
}
static get_chip_description = async (loader) => {
var desc;
var pkg_ver = await this.get_pkg_version(loader);
if (pkg_ver === 0) {
desc = "ESP32-C3";
} else {
desc = "unknown ESP32-C3";
}
var chip_rev = await this.get_chip_revision(loader);
desc += " (revision " + chip_rev + ")";
return desc;
}
static get_chip_features = async (loader) => {
return ["Wi-Fi"];
}
static get_crystal_freq = async (loader) => {
return 40;
}
static _d2h(d) {
var h = (+d).toString(16);
return h.length === 1 ? '0' + h: h;
}
static read_mac = async (loader) => {
var mac0 = await loader.read_reg({addr: this.MAC_EFUSE_REG});
mac0 = mac0 >>> 0;
var mac1 = await loader.read_reg({addr: this.MAC_EFUSE_REG + 4});
mac1 = (mac1 >>> 0) & 0x0000ffff;
var mac = new Uint8Array(6);
mac[0] = (mac1 >> 8) & 0xff;
mac[1] = mac1 & 0xff;
mac[2] = (mac0 >> 24) & 0xff;
mac[3] = (mac0 >> 16) & 0xff;
mac[4] = (mac0 >> 8) & 0xff;
mac[5] = mac0 & 0xff;
return(this._d2h(mac[0])+":"+this._d2h(mac[1])+":"+this._d2h(mac[2])+":"+this._d2h(mac[3])+":"+this._d2h(mac[4])+":"+this._d2h(mac[5]));
}
static get_erase_size = function(offset, size) {
return size;
}
}
class ESPLoader {
ESP_RAM_BLOCK = 0x1800;
ESP_FLASH_BEGIN = 0x02;
ESP_FLASH_DATA = 0x03;
ESP_FLASH_END = 0x04;
ESP_MEM_BEGIN = 0x05;
ESP_MEM_END = 0x06;
ESP_MEM_DATA = 0x07;
ESP_WRITE_REG = 0x09;
ESP_FLASH_DEFL_BEGIN = 0x10;
ESP_FLASH_DEFL_DATA = 0x11;
ESP_FLASH_DEFL_END = 0x12;
ESP_SPI_FLASH_MD5 = 0x13;
ESP_READ_REG = 0x0A;
ESP_SPI_ATTACH = 0x0D;
ESP_CHANGE_BAUDRATE = 0x0F;
// Only Stub supported commands
ESP_ERASE_FLASH = 0xD0;
ESP_ERASE_REGION = 0xD1;
ESP_IMAGE_MAGIC = 0xe9;
ESP_CHECKSUM_MAGIC = 0xef;
ERASE_REGION_TIMEOUT_PER_MB = 30000;
ERASE_WRITE_TIMEOUT_PER_MB = 40000;
MD5_TIMEOUT_PER_MB = 8000;
CHIP_ERASE_TIMEOUT = 120000;
MAX_TIMEOUT = this.CHIP_ERASE_TIMEOUT * 2;
CHIP_DETECT_MAGIC_REG_ADDR = 0x40001000;
DETECTED_FLASH_SIZES = {0x12: '256KB', 0x13: '512KB', 0x14: '1MB', 0x15: '2MB', 0x16: '4MB', 0x17: '8MB', 0x18: '16MB'};
constructor(transport, baudrate, terminal) {
this.transport = transport;
this.baudrate = baudrate;
this.terminal = terminal;
this.IS_STUB = false;
this.chip = null;
if (terminal) {
this.terminal.clear();
}
this.log("esptool.js v0.1-dev");
this.log("Serial port " + this.transport.get_info());
}
_sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
log(str) {
if (this.transport) {
this.terminal.writeln(str);
} else {
console.log(str);
}
}
write_char(str) {
if (this.transport) {
this.terminal.write(str);
} else {
console.log(str);
}
}
_short_to_bytearray(i) {
return [i & 0xff, (i >> 8) & 0xff];
}
_int_to_bytearray(i) {
return [i & 0xff, (i >> 8) & 0xff, (i >> 16) & 0xff, (i >> 24) & 0xff];
}
_bytearray_to_short(i, j) {
return (i | (j >> 8));
}
_bytearray_to_int(i, j, k, l) {
return (i | (j << 8) | (k << 16) | (l << 24));
}
_appendBuffer(buffer1, buffer2) {
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
}
_appendArray(arr1, arr2) {
var c = new Uint8Array(arr1.length + arr2.length);
c.set(arr1, 0);
c.set(arr2, arr1.length);
return c;
}
ui8ToBstr(u8Array) {
var i, len = u8Array.length, b_str = "";
for (i=0; i<len; i++) {
b_str += String.fromCharCode(u8Array[i]);
}
return b_str;
}
bstrToUi8(bStr) {
var i, len = bStr.length, u8_array = new Uint8Array(len);
for (var i = 0; i < len; i++) {
u8_array[i] = bStr.charCodeAt(i);
}
return u8_array;
}
flush_input = async () => {
try {
await this.transport.read({timeout:200});
} catch(e) {
}
}
command = async ({op=null, data=[], chk=0, wait_response=true, timeout=3000} = {}) => {
//console.log("command "+ op + " " + wait_response + " " + timeout);
if (op != null) {
var pkt = new Uint8Array(8 + data.length);
pkt[0] = 0x00;
pkt[1] = op;
pkt[2] = this._short_to_bytearray(data.length)[0];
pkt[3] = this._short_to_bytearray(data.length)[1];
pkt[4] = this._int_to_bytearray(chk)[0];
pkt[5] = this._int_to_bytearray(chk)[1];
pkt[6] = this._int_to_bytearray(chk)[2];
pkt[7] = this._int_to_bytearray(chk)[3];
var i;
for (i = 0; i < data.length; i++) {
pkt[8 + i] = data[i];
}
//console.log("Command " + pkt);
await this.transport.write(pkt);
}
if (wait_response) {
try {
var p = await this.transport.read({timeout: timeout});
//console.log("Response " + p);
const resp = p[0];
const op_ret = p[1];
const len_ret = this._bytearray_to_short(p[2], p[3]);
const val = this._bytearray_to_int(p[4], p[5], p[6], p[7]);
//console.log("Resp "+resp + " " + op_ret + " " + len_ret + " " + val );
const data = p.slice(8);
if (op == null || op_ret == op) {
return [val, data];
} else {
throw("invalid response");
}
} catch(e) {
if (e === "timeout") {
throw(e);
}
}
}
}
read_reg = async({addr, timeout = 3000} = {}) => {
var val, data;
var pkt = this._int_to_bytearray(addr);
val = await this.command({op:this.ESP_READ_REG, data:pkt, timeout:timeout});
return val[0];
}
write_reg = async({addr, value, mask = 0xFFFFFFFF, delay_us = 0, delay_after_us = 0} = {}) => {
var pkt = this._appendArray(this._int_to_bytearray(addr), this._int_to_bytearray(value));
pkt = this._appendArray(pkt, this._int_to_bytearray(mask));
pkt = this._appendArray(pkt, this._int_to_bytearray(delay_us));
if (delay_after_us > 0) {
pkt = this._appendArray(pkt, this._int_to_bytearray(this.chip.UART_DATE_REG_ADDR));
pkt = this._appendArray(pkt, this._int_to_bytearray(0));
pkt = this._appendArray(pkt, this._int_to_bytearray(0));
pkt = this._appendArray(pkt, this._int_to_bytearray(delay_after_us));
}
await this.check_command({op_description: "write target memory", op: this.ESP_WRITE_REG, data: pkt});
}
sync = async () => {
console.log("Sync");
var cmd = new Uint8Array(36);
var i;
cmd[0] = 0x07;
cmd[1] = 0x07;
cmd[2] = 0x12;
cmd[3] = 0x20;
for (i = 0; i < 32; i++) {
cmd[4 + i] = 0x55;
}
try {
const resp = await this.command({op:0x08, data:cmd, timeout:100});
return resp;
} catch(e) {
console.log("Sync err " + e);
throw(e);
}
}
_connect_attempt = async ({mode='default_reset', esp32r0_delay=false} = {}) => {
console.log("_connect_attempt " + esp32r0_delay);
if (mode !== 'no_reset') {
await this.transport.setDTR(false);
await this.transport.setRTS(true);
await this._sleep(100);
if (esp32r0_delay) {
//await this._sleep(1200);
await this._sleep(2000);
}
await this.transport.setDTR(true);
await this.transport.setRTS(false);
if (esp32r0_delay) {
//await this._sleep(400);
}
await this._sleep(50);
await this.transport.setDTR(false);
}
var i = 0;
while (1) {
try {
const res = await this.transport.read({timeout: 1000});
i += res.length;
//console.log("Len = " + res.length);
//var str = new TextDecoder().decode(res);
//this.log(str);
} catch (e) {
if (e === "timeout") {
break;
}
}
await this._sleep(50);
}
this.transport.slip_reader_enabled = true;
var i = 7;
while (i--) {
try {
var resp = await this.sync();
return "success";
} catch(error) {
if (error === "timeout") {
if (esp32r0_delay) {
this.write_char('_');
} else {
this.write_char('.');
}
}
}
await this._sleep(50);
}
return "error";
}
connect = async ({mode='default_reset', attempts=7, detecting=false} = {}) => {
var i;
var resp;
this.write_char('Connecting...');
await this.transport.connect();
for (i = 0 ; i < attempts; i++) {
resp = await this._connect_attempt({esp32r0_delay:false});
if (resp === "success") {
break;
}
resp = await this._connect_attempt({esp32r0_delay:true});
if (resp === "success") {
break;
}
}
if (resp !== "success") {
this.log("Failed to connect with the device");
return "error";
}
this.write_char('\n');
this.write_char('\r');
await this.flush_input();
if (!detecting) {
var chip_magic_value = await this.read_reg({addr:0x40001000});
console.log("Chip Magic " + chip_magic_value);
var chips = [ESP8266ROM, ESP32ROM, ESP32S2ROM, ESP32S3BETA2ROM, ESP32C3ROM];
chips.forEach(function (cls) {
if (chip_magic_value == cls.CHIP_DETECT_MAGIC_VALUE) {
console.log(cls);
this.chip = cls;
}
}, this);
}
}
detect_chip = async () => {
await this.connect();
this.write_char("Detecting chip type... ");
if (this.chip != null) {
this.log(this.chip.CHIP_NAME);
}
}
check_command = async ({op_description="", op=null, data=[], chk=0, timeout=3000} = {}) => {
console.log("check_command " + op) ;
var resp = await this.command({op:op, data:data, chk:chk, timeout:timeout});
if (resp[1].length > 4) {
return resp[1];
} else {
return resp[0];
}
}
mem_begin = async (size, blocks, blocksize, offset) => {
/* XXX: Add check to ensure that STUB is not getting overwritten */
console.log("mem_begin " + size + " " + blocks + " " + blocksize + " " + offset);
var pkt = this._appendArray(this._int_to_bytearray(size), this._int_to_bytearray(blocks));
pkt = this._appendArray(pkt, this._int_to_bytearray(blocksize));
pkt = this._appendArray(pkt, this._int_to_bytearray(offset));
await this.check_command({op_description: "write to target RAM", op: this.ESP_MEM_BEGIN, data: pkt});
}
checksum = function (data) {
var i;
var chk = 0xEF;
for (i = 0; i < data.length; i++) {
chk ^= data[i];
}
return chk;
}
mem_block = async (buffer, seq) => {
var pkt = this._appendArray(this._int_to_bytearray(buffer.length), this._int_to_bytearray(seq));
pkt = this._appendArray(pkt, this._int_to_bytearray(0));
pkt = this._appendArray(pkt, this._int_to_bytearray(0));
pkt = this._appendArray(pkt, buffer);
var checksum = this.checksum(buffer);
await this.check_command({op_description: "write to target RAM", op: this.ESP_MEM_DATA, data: pkt, chk: checksum});
}
mem_finish = async (entrypoint) => {
var is_entry = (entrypoint === 0) ? 1 : 0;
var pkt = this._appendArray(this._int_to_bytearray(is_entry), this._int_to_bytearray(entrypoint));
await this.check_command({op_description: "leave RAM download mode", op: this.ESP_MEM_END, data: pkt, timeout: 50}); // XXX: handle non-stub with diff timeout
}
flash_spi_attach = async (hspi_arg) => {
var pkt = this._int_to_bytearray(hspi_arg);
await this.check_command({op_description: "configure SPI flash pins", op: this.ESP_SPI_ATTACH, data: pkt});
}
timeout_per_mb = function(seconds_per_mb, size_bytes) {
var result = seconds_per_mb * (size_bytes / 1000000);
if (result < 3000) {
return 3000;
} else {
return result;
}
}
flash_begin = async (size, offset) => {
var num_blocks = Math.floor((size + this.FLASH_WRITE_SIZE - 1) / this.FLASH_WRITE_SIZE);
var erase_size = this.chip.get_erase_size(offset, size);
var d = new Date();
var t1 = d.getTime();
var timeout = 3000;
if (this.IS_STUB == false) {
timeout = this.timeout_per_mb(this.ERASE_REGION_TIMEOUT_PER_MB, size);
}
console.log("flash begin " + erase_size + " " + num_blocks + " " + this.FLASH_WRITE_SIZE + " " + offset + " " + size);
var pkt = this._appendArray(this._int_to_bytearray(erase_size), this._int_to_bytearray(num_blocks));
pkt = this._appendArray(pkt, this._int_to_bytearray(this.FLASH_WRITE_SIZE));
pkt = this._appendArray(pkt, this._int_to_bytearray(offset));
if (this.IS_STUB == false) {
pkt = this._appendArray(pkt, this._int_to_bytearray(0)); // XXX: Support encrypted
}
await this.check_command({op_description:"enter Flash download mode", op: this.ESP_FLASH_BEGIN, data: pkt, timeout: timeout});
var t2 = d.getTime();
if (size != 0 && this.IS_STUB == false) {
this.log("Took "+((t2-t1)/1000)+"."+((t2-t1)%1000)+"s to erase flash block");
}
}
flash_defl_begin = async (size, compsize, offset) => {
var num_blocks = Math.floor((compsize + this.FLASH_WRITE_SIZE - 1) / this.FLASH_WRITE_SIZE);
var erase_blocks = Math.floor((size + this.FLASH_WRITE_SIZE - 1) / this.FLASH_WRITE_SIZE);
var d = new Date();
var t1 = d.getTime();
let write_size, timeout;
if (this.IS_STUB) {
write_size = size;
timeout = 3000;
} else {
write_size = erase_blocks * this.FLASH_WRITE_SIZE;
timeout = this.timeout_per_mb(this.ERASE_REGION_TIMEOUT_PER_MB, write_size);
}
this.log("Compressed " + size + " bytes to " + compsize + "...");
var pkt = this._appendArray(this._int_to_bytearray(write_size), this._int_to_bytearray(num_blocks));
pkt = this._appendArray(pkt, this._int_to_bytearray(this.FLASH_WRITE_SIZE));