-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
__init__.py
1348 lines (1221 loc) · 44.8 KB
/
__init__.py
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
# SPDX-FileCopyrightText: 2014-2022 Fredrik Ahlberg, Angus Gratton,
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
#
# SPDX-License-Identifier: GPL-2.0-or-later
# PYTHON_ARGCOMPLETE_OK
__all__ = [
"chip_id",
"detect_chip",
"dump_mem",
"elf2image",
"erase_flash",
"erase_region",
"flash_id",
"get_security_info",
"image_info",
"load_ram",
"make_image",
"merge_bin",
"read_flash",
"read_flash_status",
"read_flash_sfdp",
"read_mac",
"read_mem",
"run",
"verify_flash",
"version",
"write_flash",
"write_flash_status",
"write_mem",
]
__version__ = "4.8.1"
import argparse
import inspect
import os
import shlex
import sys
import time
import traceback
from esptool.bin_image import intel_hex_to_bin
from esptool.cmds import (
DETECTED_FLASH_SIZES,
chip_id,
detect_chip,
detect_flash_size,
dump_mem,
elf2image,
erase_flash,
erase_region,
flash_id,
read_flash_sfdp,
get_security_info,
image_info,
load_ram,
make_image,
merge_bin,
read_flash,
read_flash_status,
read_mac,
read_mem,
run,
verify_flash,
version,
write_flash,
write_flash_status,
write_mem,
)
from esptool.config import load_config_file
from esptool.loader import (
DEFAULT_CONNECT_ATTEMPTS,
DEFAULT_OPEN_PORT_ATTEMPTS,
StubFlasher,
ESPLoader,
list_ports,
)
from esptool.targets import CHIP_DEFS, CHIP_LIST, ESP32ROM
from esptool.util import (
FatalError,
NotImplementedInROMError,
flash_size_bytes,
strip_chip_name,
)
from itertools import chain, cycle, repeat
import serial
def main(argv=None, esp=None):
"""
Main function for esptool
argv - Optional override for default arguments parsing (that uses sys.argv),
can be a list of custom arguments as strings. Arguments and their values
need to be added as individual items to the list
e.g. "-b 115200" thus becomes ['-b', '115200'].
esp - Optional override of the connected device previously
returned by get_default_connected_device()
"""
external_esp = esp is not None
parser = argparse.ArgumentParser(
description="esptool.py v%s - Espressif chips ROM Bootloader Utility"
% __version__,
prog="esptool",
)
parser.add_argument(
"--chip",
"-c",
help="Target chip type",
type=strip_chip_name,
choices=["auto"] + CHIP_LIST,
default=os.environ.get("ESPTOOL_CHIP", "auto"),
)
parser.add_argument(
"--port",
"-p",
help="Serial port device",
default=os.environ.get("ESPTOOL_PORT", None),
)
parser.add_argument(
"--baud",
"-b",
help="Serial port baud rate used when flashing/reading",
type=arg_auto_int,
default=os.environ.get("ESPTOOL_BAUD", ESPLoader.ESP_ROM_BAUD),
)
parser.add_argument(
"--port-filter",
action="append",
help="Serial port device filter, can be vid=NUMBER, pid=NUMBER, name=SUBSTRING, serial=SUBSTRING",
type=str,
default=[],
)
parser.add_argument(
"--before",
help="What to do before connecting to the chip",
choices=["default_reset", "usb_reset", "no_reset", "no_reset_no_sync"],
default=os.environ.get("ESPTOOL_BEFORE", "default_reset"),
)
parser.add_argument(
"--after",
"-a",
help="What to do after esptool.py is finished",
choices=["hard_reset", "soft_reset", "no_reset", "no_reset_stub"],
default=os.environ.get("ESPTOOL_AFTER", "hard_reset"),
)
parser.add_argument(
"--no-stub",
help="Disable launching the flasher stub, only talk to ROM bootloader. "
"Some features will not be available.",
action="store_true",
)
# --stub-version can be set with --no-stub so the tests wouldn't fail if this option is implied globally
parser.add_argument(
"--stub-version",
default=os.environ.get("ESPTOOL_STUB_VERSION", StubFlasher.STUB_SUBDIRS[0]),
choices=StubFlasher.STUB_SUBDIRS,
# not a public option and is not subject to the semantic versioning policy
help=argparse.SUPPRESS,
)
parser.add_argument(
"--trace",
"-t",
help="Enable trace-level output of esptool.py interactions.",
action="store_true",
)
parser.add_argument(
"--override-vddsdio",
help="Override ESP32 VDDSDIO internal voltage regulator (use with care)",
choices=ESP32ROM.OVERRIDE_VDDSDIO_CHOICES,
nargs="?",
)
parser.add_argument(
"--connect-attempts",
help=(
"Number of attempts to connect, negative or 0 for infinite. "
"Default: %d." % DEFAULT_CONNECT_ATTEMPTS
),
type=int,
default=os.environ.get("ESPTOOL_CONNECT_ATTEMPTS", DEFAULT_CONNECT_ATTEMPTS),
)
subparsers = parser.add_subparsers(
dest="operation", help="Run esptool.py {command} -h for additional help"
)
def add_spi_connection_arg(parent):
parent.add_argument(
"--spi-connection",
"-sc",
help="Override default SPI Flash connection. "
"Value can be SPI, HSPI or a comma-separated list of 5 I/O numbers "
"to use for SPI flash (CLK,Q,D,HD,CS). Not supported with ESP8266.",
action=SpiConnectionAction,
)
parser_load_ram = subparsers.add_parser(
"load_ram", help="Download an image to RAM and execute"
)
parser_load_ram.add_argument(
"filename", help="Firmware image", action=AutoHex2BinAction
)
parser_dump_mem = subparsers.add_parser(
"dump_mem", help="Dump arbitrary memory to disk"
)
parser_dump_mem.add_argument("address", help="Base address", type=arg_auto_int)
parser_dump_mem.add_argument(
"size", help="Size of region to dump", type=arg_auto_int
)
parser_dump_mem.add_argument("filename", help="Name of binary dump")
parser_read_mem = subparsers.add_parser(
"read_mem", help="Read arbitrary memory location"
)
parser_read_mem.add_argument("address", help="Address to read", type=arg_auto_int)
parser_write_mem = subparsers.add_parser(
"write_mem", help="Read-modify-write to arbitrary memory location"
)
parser_write_mem.add_argument("address", help="Address to write", type=arg_auto_int)
parser_write_mem.add_argument("value", help="Value", type=arg_auto_int)
parser_write_mem.add_argument(
"mask",
help="Mask of bits to write",
type=arg_auto_int,
nargs="?",
default="0xFFFFFFFF",
)
def add_spi_flash_subparsers(
parent: argparse.ArgumentParser,
allow_keep: bool,
auto_detect: bool,
size_only: bool = False,
):
"""Add common parser arguments for SPI flash properties"""
extra_keep_args = ["keep"] if allow_keep else []
if auto_detect and allow_keep:
extra_fs_message = ", detect, or keep"
flash_sizes = ["detect", "keep"]
elif auto_detect:
extra_fs_message = ", or detect"
flash_sizes = ["detect"]
elif allow_keep:
extra_fs_message = ", or keep"
flash_sizes = ["keep"]
else:
extra_fs_message = ""
flash_sizes = []
if not size_only:
parent.add_argument(
"--flash_freq",
"-ff",
help="SPI Flash frequency",
choices=extra_keep_args
+ [
"80m",
"60m",
"48m",
"40m",
"30m",
"26m",
"24m",
"20m",
"16m",
"15m",
"12m",
],
default=os.environ.get("ESPTOOL_FF", "keep" if allow_keep else None),
)
parent.add_argument(
"--flash_mode",
"-fm",
help="SPI Flash mode",
choices=extra_keep_args + ["qio", "qout", "dio", "dout"],
default=os.environ.get("ESPTOOL_FM", "keep" if allow_keep else "qio"),
)
parent.add_argument(
"--flash_size",
"-fs",
help="SPI Flash size in MegaBytes "
"(1MB, 2MB, 4MB, 8MB, 16MB, 32MB, 64MB, 128MB) "
"plus ESP8266-only (256KB, 512KB, 2MB-c1, 4MB-c1)" + extra_fs_message,
choices=flash_sizes
+ [
"256KB",
"512KB",
"1MB",
"2MB",
"2MB-c1",
"4MB",
"4MB-c1",
"8MB",
"16MB",
"32MB",
"64MB",
"128MB",
],
default=os.environ.get("ESPTOOL_FS", "keep" if allow_keep else "1MB"),
)
add_spi_connection_arg(parent)
parser_write_flash = subparsers.add_parser(
"write_flash", help="Write a binary blob to flash"
)
parser_write_flash.add_argument(
"addr_filename",
metavar="<address> <filename>",
help="Address followed by binary filename, separated by space",
action=AddrFilenamePairAction,
)
parser_write_flash.add_argument(
"--erase-all",
"-e",
help="Erase all regions of flash (not just write areas) before programming",
action="store_true",
)
add_spi_flash_subparsers(parser_write_flash, allow_keep=True, auto_detect=True)
parser_write_flash.add_argument(
"--no-progress", "-p", help="Suppress progress output", action="store_true"
)
parser_write_flash.add_argument(
"--verify",
help="Verify just-written data on flash "
"(mostly superfluous, data is read back during flashing)",
action="store_true",
)
parser_write_flash.add_argument(
"--encrypt",
help="Apply flash encryption when writing data "
"(required correct efuse settings)",
action="store_true",
)
# In order to not break backward compatibility,
# our list of encrypted files to flash is a new parameter
parser_write_flash.add_argument(
"--encrypt-files",
metavar="<address> <filename>",
help="Files to be encrypted on the flash. "
"Address followed by binary filename, separated by space.",
action=AddrFilenamePairAction,
)
parser_write_flash.add_argument(
"--ignore-flash-encryption-efuse-setting",
help="Ignore flash encryption efuse settings ",
action="store_true",
)
parser_write_flash.add_argument(
"--force",
help="Force write, skip security and compatibility checks. Use with caution!",
action="store_true",
)
compress_args = parser_write_flash.add_mutually_exclusive_group(required=False)
compress_args.add_argument(
"--compress",
"-z",
help="Compress data in transfer (default unless --no-stub is specified)",
action="store_true",
default=None,
)
compress_args.add_argument(
"--no-compress",
"-u",
help="Disable data compression during transfer "
"(default if --no-stub is specified)",
action="store_true",
)
subparsers.add_parser("run", help="Run application code in flash")
parser_image_info = subparsers.add_parser(
"image_info", help="Dump headers from a binary file (bootloader or application)"
)
parser_image_info.add_argument(
"filename", help="Image file to parse", action=AutoHex2BinAction
)
parser_image_info.add_argument(
"--version",
"-v",
help="Output format version (1 - legacy, 2 - extended)",
choices=["1", "2"],
default="1",
)
parser_make_image = subparsers.add_parser(
"make_image", help="Create an application image from binary files"
)
parser_make_image.add_argument("output", help="Output image file")
parser_make_image.add_argument(
"--segfile", "-f", action="append", help="Segment input file"
)
parser_make_image.add_argument(
"--segaddr",
"-a",
action="append",
help="Segment base address",
type=arg_auto_int,
)
parser_make_image.add_argument(
"--entrypoint",
"-e",
help="Address of entry point",
type=arg_auto_int,
default=0,
)
parser_elf2image = subparsers.add_parser(
"elf2image", help="Create an application image from ELF file"
)
parser_elf2image.add_argument("input", help="Input ELF file")
parser_elf2image.add_argument(
"--output",
"-o",
help="Output filename prefix (for version 1 image), "
"or filename (for version 2 single image)",
type=str,
)
parser_elf2image.add_argument(
"--version",
"-e",
help="Output image version",
choices=["1", "2", "3"],
default="1",
)
parser_elf2image.add_argument(
# it kept for compatibility
# Minimum chip revision (deprecated, consider using --min-rev-full)
"--min-rev",
"-r",
help=argparse.SUPPRESS,
type=int,
choices=range(256),
metavar="{0, ... 255}",
default=0,
)
parser_elf2image.add_argument(
"--min-rev-full",
help="Minimal chip revision (in format: major * 100 + minor)",
type=int,
choices=range(65536),
metavar="{0, ... 65535}",
default=0,
)
parser_elf2image.add_argument(
"--max-rev-full",
help="Maximal chip revision (in format: major * 100 + minor)",
type=int,
choices=range(65536),
metavar="{0, ... 65535}",
default=65535,
)
parser_elf2image.add_argument(
"--secure-pad",
action="store_true",
help="Pad image so once signed it will end on a 64KB boundary. "
"For Secure Boot v1 images only.",
)
parser_elf2image.add_argument(
"--secure-pad-v2",
action="store_true",
help="Pad image to 64KB, so once signed its signature sector will"
"start at the next 64K block. For Secure Boot v2 images only.",
)
parser_elf2image.add_argument(
"--elf-sha256-offset",
help="If set, insert SHA256 hash (32 bytes) of the input ELF file "
"at specified offset in the binary.",
type=arg_auto_int,
default=None,
)
parser_elf2image.add_argument(
"--dont-append-digest",
dest="append_digest",
help="Don't append a SHA256 digest of the entire image after the checksum. "
"This argument is not supported and ignored for ESP8266.",
action="store_false",
default=True,
)
parser_elf2image.add_argument(
"--use_segments",
help="If set, ELF segments will be used instead of ELF sections "
"to generate the image.",
action="store_true",
)
parser_elf2image.add_argument(
"--flash-mmu-page-size",
help="Change flash MMU page size.",
choices=["64KB", "32KB", "16KB", "8KB"],
)
parser_elf2image.add_argument(
"--pad-to-size",
help="The block size with which the final binary image after padding "
"must be aligned to. Value 0xFF is used for padding, similar to erase_flash",
default=None,
)
parser_elf2image.add_argument(
"--ram-only-header",
help="Order segments of the output so IRAM and DRAM are placed at the "
"beginning and force the main header segment number to RAM segments "
"quantity. This will make the other segments invisible to the ROM "
"loader. Use this argument with care because the ROM loader will load "
"only the RAM segments although the other segments being present in "
"the output. Implies --dont-append-digest",
action="store_true",
default=None,
)
add_spi_flash_subparsers(parser_elf2image, allow_keep=False, auto_detect=False)
subparsers.add_parser("read_mac", help="Read MAC address from OTP ROM")
subparsers.add_parser("chip_id", help="Read Chip ID from OTP ROM")
parser_flash_id = subparsers.add_parser(
"flash_id", help="Read SPI flash manufacturer and device ID"
)
add_spi_connection_arg(parser_flash_id)
parser_read_status = subparsers.add_parser(
"read_flash_status", help="Read SPI flash status register"
)
add_spi_connection_arg(parser_read_status)
parser_read_status.add_argument(
"--bytes",
help="Number of bytes to read (1-3)",
type=int,
choices=[1, 2, 3],
default=2,
)
parser_write_status = subparsers.add_parser(
"write_flash_status", help="Write SPI flash status register"
)
add_spi_connection_arg(parser_write_status)
parser_write_status.add_argument(
"--non-volatile",
help="Write non-volatile bits (use with caution)",
action="store_true",
)
parser_write_status.add_argument(
"--bytes",
help="Number of status bytes to write (1-3)",
type=int,
choices=[1, 2, 3],
default=2,
)
parser_write_status.add_argument("value", help="New value", type=arg_auto_int)
parser_read_flash = subparsers.add_parser(
"read_flash", help="Read SPI flash content"
)
add_spi_flash_subparsers(
parser_read_flash, allow_keep=True, auto_detect=True, size_only=True
)
parser_read_flash.add_argument("address", help="Start address", type=arg_auto_int)
parser_read_flash.add_argument(
"size",
help="Size of region to dump. Use `ALL` to read to the end of flash.",
type=arg_auto_size,
)
parser_read_flash.add_argument("filename", help="Name of binary dump")
parser_read_flash.add_argument(
"--no-progress", "-p", help="Suppress progress output", action="store_true"
)
parser_verify_flash = subparsers.add_parser(
"verify_flash", help="Verify a binary blob against flash"
)
parser_verify_flash.add_argument(
"addr_filename",
help="Address and binary file to verify there, separated by space",
action=AddrFilenamePairAction,
)
parser_verify_flash.add_argument(
"--diff", "-d", help="Show differences", choices=["no", "yes"], default="no"
)
add_spi_flash_subparsers(parser_verify_flash, allow_keep=True, auto_detect=True)
parser_erase_flash = subparsers.add_parser(
"erase_flash", help="Perform Chip Erase on SPI flash"
)
parser_erase_flash.add_argument(
"--force",
help="Erase flash even if security features are enabled. Use with caution!",
action="store_true",
)
add_spi_connection_arg(parser_erase_flash)
parser_erase_region = subparsers.add_parser(
"erase_region", help="Erase a region of the flash"
)
parser_erase_region.add_argument(
"--force",
help="Erase region even if security features are enabled. Use with caution!",
action="store_true",
)
add_spi_connection_arg(parser_erase_region)
parser_erase_region.add_argument(
"address", help="Start address (must be multiple of 4096)", type=arg_auto_int
)
parser_erase_region.add_argument(
"size",
help="Size of region to erase (must be multiple of 4096). "
"Use `ALL` to erase to the end of flash.",
type=arg_auto_size,
)
parser_read_flash_sfdp = subparsers.add_parser(
"read_flash_sfdp",
help="Read SPI flash SFDP (Serial Flash Discoverable Parameters)",
)
add_spi_flash_subparsers(parser_read_flash_sfdp, allow_keep=True, auto_detect=True)
parser_read_flash_sfdp.add_argument("addr", type=arg_auto_int)
parser_read_flash_sfdp.add_argument("bytes", type=int)
parser_merge_bin = subparsers.add_parser(
"merge_bin",
help="Merge multiple raw binary files into a single file for later flashing",
)
parser_merge_bin.add_argument(
"--output", "-o", help="Output filename", type=str, required=True
)
parser_merge_bin.add_argument(
"--format",
"-f",
help="Format of the output file",
choices=["raw", "uf2", "hex"],
default="raw",
)
uf2_group = parser_merge_bin.add_argument_group("UF2 format")
uf2_group.add_argument(
"--chunk-size",
help="Specify the used data part of the 512 byte UF2 block. "
"A common value is 256. By default the largest possible value will be used.",
default=None,
type=arg_auto_chunk_size,
)
uf2_group.add_argument(
"--md5-disable",
help="Disable MD5 checksum in UF2 output",
action="store_true",
)
add_spi_flash_subparsers(parser_merge_bin, allow_keep=True, auto_detect=False)
raw_group = parser_merge_bin.add_argument_group("RAW format")
raw_group.add_argument(
"--target-offset",
"-t",
help="Target offset where the output file will be flashed",
type=arg_auto_int,
default=0,
)
raw_group.add_argument(
"--fill-flash-size",
help="If set, the final binary file will be padded with FF "
"bytes up to this flash size.",
choices=[
"256KB",
"512KB",
"1MB",
"2MB",
"4MB",
"8MB",
"16MB",
"32MB",
"64MB",
"128MB",
],
)
parser_merge_bin.add_argument(
"addr_filename",
metavar="<address> <filename>",
help="Address followed by binary filename, separated by space",
action=AddrFilenamePairAction,
)
subparsers.add_parser("get_security_info", help="Get some security-related data")
subparsers.add_parser("version", help="Print esptool version")
# internal sanity check - every operation matches a module function of the same name
for operation in subparsers.choices.keys():
assert operation in globals(), "%s should be a module function" % operation
# Enable argcomplete only on Unix-like systems
if sys.platform != "win32":
try:
import argcomplete
argcomplete.autocomplete(parser)
except ImportError:
pass
argv = expand_file_arguments(argv or sys.argv[1:])
args = parser.parse_args(argv)
print("esptool.py v%s" % __version__)
load_config_file(verbose=True)
StubFlasher.set_preferred_stub_subdir(args.stub_version)
# Parse filter arguments into separate lists
args.filterVids = []
args.filterPids = []
args.filterNames = []
args.filterSerials = []
for f in args.port_filter:
kvp = f.split("=")
if len(kvp) != 2:
raise FatalError("Option --port-filter argument must consist of key=value")
if kvp[0] == "vid":
args.filterVids.append(arg_auto_int(kvp[1]))
elif kvp[0] == "pid":
args.filterPids.append(arg_auto_int(kvp[1]))
elif kvp[0] == "name":
args.filterNames.append(kvp[1])
elif kvp[0] == "serial":
args.filterSerials.append(kvp[1])
else:
raise FatalError("Option --port-filter argument key not recognized")
# operation function can take 1 arg (args), 2 args (esp, arg)
# or be a member function of the ESPLoader class.
if args.operation is None:
parser.print_help()
sys.exit(1)
# Forbid the usage of both --encrypt, which means encrypt all the given files,
# and --encrypt-files, which represents the list of files to encrypt.
# The reason is that allowing both at the same time increases the chances of
# having contradictory lists (e.g. one file not available in one of list).
if (
args.operation == "write_flash"
and args.encrypt
and args.encrypt_files is not None
):
raise FatalError(
"Options --encrypt and --encrypt-files "
"must not be specified at the same time."
)
operation_func = globals()[args.operation]
operation_args = inspect.getfullargspec(operation_func).args
if (
operation_args[0] == "esp"
): # operation function takes an ESPLoader connection object
if args.before != "no_reset_no_sync":
initial_baud = min(
ESPLoader.ESP_ROM_BAUD, args.baud
) # don't sync faster than the default baud rate
else:
initial_baud = args.baud
if args.port is None:
ser_list = get_port_list(
args.filterVids, args.filterPids, args.filterNames, args.filterSerials
)
print("Found %d serial ports" % len(ser_list))
else:
ser_list = [args.port]
open_port_attempts = os.environ.get(
"ESPTOOL_OPEN_PORT_ATTEMPTS", DEFAULT_OPEN_PORT_ATTEMPTS
)
try:
open_port_attempts = int(open_port_attempts)
except ValueError:
raise SystemExit("Invalid value for ESPTOOL_OPEN_PORT_ATTEMPTS")
if open_port_attempts != 1:
if args.port is None or args.chip == "auto":
print(
"WARNING: The ESPTOOL_OPEN_PORT_ATTEMPTS (open_port_attempts) option can only be used with --port and --chip arguments."
)
else:
esp = esp or connect_loop(
args.port,
initial_baud,
args.chip,
open_port_attempts,
args.trace,
args.before,
)
esp = esp or get_default_connected_device(
ser_list,
port=args.port,
connect_attempts=args.connect_attempts,
initial_baud=initial_baud,
chip=args.chip,
trace=args.trace,
before=args.before,
)
if esp is None:
raise FatalError(
"Could not connect to an Espressif device "
"on any of the %d available serial ports." % len(ser_list)
)
if esp.secure_download_mode:
print("Chip is %s in Secure Download Mode" % esp.CHIP_NAME)
else:
print("Chip is %s" % (esp.get_chip_description()))
print("Features: %s" % ", ".join(esp.get_chip_features()))
print("Crystal is %dMHz" % esp.get_crystal_freq())
read_mac(esp, args)
if not args.no_stub:
if esp.secure_download_mode:
print(
"WARNING: Stub loader is not supported in Secure Download Mode, "
"setting --no-stub"
)
args.no_stub = True
elif not esp.IS_STUB and esp.stub_is_disabled:
print(
"WARNING: Stub loader has been disabled for compatibility, "
"setting --no-stub"
)
args.no_stub = True
else:
try:
esp = esp.run_stub()
except Exception:
# The CH9102 bridge (PID: 0x55D4) can have issues on MacOS
if sys.platform == "darwin" and esp._get_pid() == 0x55D4:
print(
"\nNote: If issues persist, "
"try installing the WCH USB-to-Serial MacOS driver."
)
raise
if args.override_vddsdio:
esp.override_vddsdio(args.override_vddsdio)
if args.baud > initial_baud:
try:
esp.change_baud(args.baud)
except NotImplementedInROMError:
print(
"WARNING: ROM doesn't support changing baud rate. "
"Keeping initial baud rate %d" % initial_baud
)
def _define_spi_conn(spi_connection):
"""Prepare SPI configuration string and value for flash_spi_attach()"""
clk, q, d, hd, cs = spi_connection
spi_config_txt = f"CLK:{clk}, Q:{q}, D:{d}, HD:{hd}, CS:{cs}"
value = (hd << 24) | (cs << 18) | (d << 12) | (q << 6) | clk
return spi_config_txt, value
# Override the common SPI flash parameter stuff if configured to do so
if hasattr(args, "spi_connection") and args.spi_connection is not None:
spi_config = args.spi_connection
if args.spi_connection == "SPI":
value = 0
elif args.spi_connection == "HSPI":
value = 1
else:
esp.check_spi_connection(args.spi_connection)
# Encode the pin numbers as a 32-bit integer with packed 6-bit values,
# the same way the ESP ROM takes them
spi_config, value = _define_spi_conn(args.spi_connection)
print(f"Configuring SPI flash mode ({spi_config})...")
esp.flash_spi_attach(value)
elif args.no_stub:
if esp.CHIP_NAME != "ESP32" or esp.secure_download_mode:
print("Enabling default SPI flash mode...")
# ROM loader doesn't enable flash unless we explicitly do it
esp.flash_spi_attach(0)
else:
# ROM doesn't attach in-package flash chips
spi_chip_pads = esp.get_chip_spi_pads()
spi_config_txt, value = _define_spi_conn(spi_chip_pads)
if spi_chip_pads != (0, 0, 0, 0, 0):
print(
"Attaching flash from eFuses' SPI pads configuration"
f"({spi_config_txt})..."
)
else:
print("Enabling default SPI flash mode...")
esp.flash_spi_attach(value)
# XMC chip startup sequence
XMC_VENDOR_ID = 0x20
def is_xmc_chip_strict():
id = esp.flash_id()
rdid = ((id & 0xFF) << 16) | ((id >> 16) & 0xFF) | (id & 0xFF00)
vendor_id = (rdid >> 16) & 0xFF
mfid = (rdid >> 8) & 0xFF
cpid = rdid & 0xFF
if vendor_id != XMC_VENDOR_ID:
return False
matched = False
if mfid == 0x40:
if cpid >= 0x13 and cpid <= 0x20:
matched = True
elif mfid == 0x41:
if cpid >= 0x17 and cpid <= 0x20:
matched = True
elif mfid == 0x50:
if cpid >= 0x15 and cpid <= 0x16:
matched = True
return matched
def flash_xmc_startup():
# If the RDID value is a valid XMC one, may skip the flow
fast_check = True
if fast_check and is_xmc_chip_strict():
return # Successful XMC flash chip boot-up detected by RDID, skipping.
sfdp_mfid_addr = 0x10
mf_id = esp.read_spiflash_sfdp(sfdp_mfid_addr, 8)
if mf_id != XMC_VENDOR_ID: # Non-XMC chip detected by SFDP Read, skipping.
return
print(
"WARNING: XMC flash chip boot-up failure detected! "
"Running XMC25QHxxC startup flow"
)
esp.run_spiflash_command(0xB9) # Enter DPD
esp.run_spiflash_command(0x79) # Enter UDPD
esp.run_spiflash_command(0xFF) # Exit UDPD
time.sleep(0.002) # Delay tXUDPD
esp.run_spiflash_command(0xAB) # Release Power-Down
time.sleep(0.00002)
# Check for success
if not is_xmc_chip_strict():
print("WARNING: XMC flash boot-up fix failed.")
print("XMC flash chip boot-up fix successful!")
# Check flash chip connection
if not esp.secure_download_mode:
try:
flash_id = esp.flash_id()
if flash_id in (0xFFFFFF, 0x000000):
print(
"WARNING: Failed to communicate with the flash chip, "
"read/write operations will fail. "
"Try checking the chip connections or removing "
"any other hardware connected to IOs."
)
if (
hasattr(args, "spi_connection")
and args.spi_connection is not None
):
print(
"Some GPIO pins might be used by other peripherals, "
"try using another --spi-connection combination."
)
except FatalError as e:
raise FatalError(f"Unable to verify flash chip connection ({e}).")
# Check if XMC SPI flash chip booted-up successfully, fix if not
if not esp.secure_download_mode:
try:
flash_xmc_startup()
except FatalError as e:
esp.trace(f"Unable to perform XMC flash chip startup sequence ({e}).")
if hasattr(args, "flash_size"):
print("Configuring flash size...")
if args.flash_size == "detect":
flash_size = detect_flash_size(esp, args)
elif args.flash_size == "keep":
flash_size = detect_flash_size(esp, args=None)
if not esp.IS_STUB:
print(
"WARNING: In case of failure, please set a specific --flash_size."
)