-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
cmds.py
1423 lines (1257 loc) · 52.9 KB
/
cmds.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
import hashlib
import io
import os
import struct
import sys
import time
import zlib
import itertools
from intelhex import IntelHex
from serial import SerialException
from .bin_image import ELFFile, ImageSegment, LoadFirmwareImage
from .bin_image import (
ESP8266ROMFirmwareImage,
ESP8266V2FirmwareImage,
ESP8266V3FirmwareImage,
)
from .loader import (
DEFAULT_CONNECT_ATTEMPTS,
DEFAULT_TIMEOUT,
ERASE_WRITE_TIMEOUT_PER_MB,
ESPLoader,
timeout_per_mb,
)
from .targets import CHIP_DEFS, CHIP_LIST, ROM_LIST
from .uf2_writer import UF2Writer
from .util import (
FatalError,
NotImplementedInROMError,
NotSupportedError,
UnsupportedCommandError,
)
from .util import (
div_roundup,
flash_size_bytes,
get_file_size,
hexify,
pad_to,
print_overwrite,
)
DETECTED_FLASH_SIZES = {
0x12: "256KB",
0x13: "512KB",
0x14: "1MB",
0x15: "2MB",
0x16: "4MB",
0x17: "8MB",
0x18: "16MB",
0x19: "32MB",
0x1A: "64MB",
0x1B: "128MB",
0x1C: "256MB",
0x20: "64MB",
0x21: "128MB",
0x22: "256MB",
0x32: "256KB",
0x33: "512KB",
0x34: "1MB",
0x35: "2MB",
0x36: "4MB",
0x37: "8MB",
0x38: "16MB",
0x39: "32MB",
0x3A: "64MB",
}
FLASH_MODES = {"qio": 0, "qout": 1, "dio": 2, "dout": 3}
def detect_chip(
port=ESPLoader.DEFAULT_PORT,
baud=ESPLoader.ESP_ROM_BAUD,
connect_mode="default_reset",
trace_enabled=False,
connect_attempts=DEFAULT_CONNECT_ATTEMPTS,
):
"""Use serial access to detect the chip type.
First, get_security_info command is sent to detect the ID of the chip
(supported only by ESP32-C3 and later, works even in the Secure Download Mode).
If this fails, we reconnect and fall-back to reading the magic number.
It's mapped at a specific ROM address and has a different value on each chip model.
This way we use one memory read and compare it to the magic number for each chip.
This routine automatically performs ESPLoader.connect() (passing
connect_mode parameter) as part of querying the chip.
"""
inst = None
detect_port = ESPLoader(port, baud, trace_enabled=trace_enabled)
if detect_port.serial_port.startswith("rfc2217:"):
detect_port.USES_RFC2217 = True
detect_port.connect(connect_mode, connect_attempts, detecting=True)
try:
print("Detecting chip type...", end="")
chip_id = detect_port.get_chip_id()
for cls in [
n for n in ROM_LIST if n.CHIP_NAME not in ("ESP8266", "ESP32", "ESP32-S2")
]:
# cmd not supported on ESP8266 and ESP32 + ESP32-S2 doesn't return chip_id
if chip_id == cls.IMAGE_CHIP_ID:
inst = cls(detect_port._port, baud, trace_enabled=trace_enabled)
try:
inst.read_reg(
ESPLoader.CHIP_DETECT_MAGIC_REG_ADDR
) # Dummy read to check Secure Download mode
except UnsupportedCommandError:
inst.secure_download_mode = True
inst._post_connect()
break
else:
err_msg = f"Unexpected chip ID value {chip_id}."
except (UnsupportedCommandError, struct.error, FatalError) as e:
# UnsupportedCommandError: ESP8266/ESP32 ROM
# struct.error: ESP32-S2
# FatalError: ESP8266/ESP32 STUB
print(" Unsupported detection protocol, switching and trying again...")
try:
# ESP32/ESP8266 are reset after an unsupported command, need to reconnect
# (not needed on ESP32-S2)
if not isinstance(e, struct.error):
detect_port.connect(
connect_mode, connect_attempts, detecting=True, warnings=False
)
print("Detecting chip type...", end="")
sys.stdout.flush()
chip_magic_value = detect_port.read_reg(
ESPLoader.CHIP_DETECT_MAGIC_REG_ADDR
)
for cls in ROM_LIST:
if chip_magic_value in cls.CHIP_DETECT_MAGIC_VALUE:
inst = cls(detect_port._port, baud, trace_enabled=trace_enabled)
inst._post_connect()
inst.check_chip_id()
break
else:
err_msg = f"Unexpected chip magic value {chip_magic_value:#010x}."
except UnsupportedCommandError:
raise FatalError(
"Unsupported Command Error received. "
"Probably this means Secure Download Mode is enabled, "
"autodetection will not work. Need to manually specify the chip."
)
finally:
if inst is not None:
print(" %s" % inst.CHIP_NAME, end="")
if detect_port.sync_stub_detected:
inst = inst.STUB_CLASS(inst)
inst.sync_stub_detected = True
print("") # end line
return inst
raise FatalError(
f"{err_msg} Failed to autodetect chip type."
"\nProbably it is unsupported by this version of esptool."
)
# "Operation" commands, executable at command line. One function each
#
# Each function takes either two args (<ESPLoader instance>, <args>) or a single <args>
# argument.
def load_ram(esp, args):
image = LoadFirmwareImage(esp.CHIP_NAME, args.filename)
print("RAM boot...")
for seg in image.segments:
size = len(seg.data)
print("Downloading %d bytes at %08x..." % (size, seg.addr), end=" ")
sys.stdout.flush()
esp.mem_begin(
size, div_roundup(size, esp.ESP_RAM_BLOCK), esp.ESP_RAM_BLOCK, seg.addr
)
seq = 0
while len(seg.data) > 0:
esp.mem_block(seg.data[0 : esp.ESP_RAM_BLOCK], seq)
seg.data = seg.data[esp.ESP_RAM_BLOCK :]
seq += 1
print("done!")
print("All segments done, executing at %08x" % image.entrypoint)
esp.mem_finish(image.entrypoint)
def read_mem(esp, args):
print("0x%08x = 0x%08x" % (args.address, esp.read_reg(args.address)))
def write_mem(esp, args):
esp.write_reg(args.address, args.value, args.mask, 0)
print("Wrote %08x, mask %08x to %08x" % (args.value, args.mask, args.address))
def dump_mem(esp, args):
with open(args.filename, "wb") as f:
for i in range(args.size // 4):
d = esp.read_reg(args.address + (i * 4))
f.write(struct.pack(b"<I", d))
if f.tell() % 1024 == 0:
print_overwrite(
"%d bytes read... (%d %%)" % (f.tell(), f.tell() * 100 // args.size)
)
sys.stdout.flush()
print_overwrite("Read %d bytes" % f.tell(), last_line=True)
print("Done!")
def detect_flash_size(esp, args=None):
# TODO: Remove the dependency on args in the next major release (v5.0)
if esp.secure_download_mode:
if args is not None and args.flash_size == "detect":
raise FatalError(
"Detecting flash size is not supported in secure download mode. "
"Need to manually specify flash size."
)
else:
return None
flash_id = esp.flash_id()
size_id = flash_id >> 16
flash_size = DETECTED_FLASH_SIZES.get(size_id)
if args is not None and args.flash_size == "detect":
if flash_size is None:
flash_size = "4MB"
print(
"WARNING: Could not auto-detect Flash size "
f"(FlashID={flash_id:#x}, SizeID={size_id:#x}), defaulting to 4MB"
)
else:
print("Auto-detected Flash size:", flash_size)
args.flash_size = flash_size
return flash_size
def _update_image_flash_params(esp, address, args, image):
"""
Modify the flash mode & size bytes if this looks like an executable bootloader image
"""
if len(image) < 8:
return image # not long enough to be a bootloader image
# unpack the (potential) image header
magic, _, flash_mode, flash_size_freq = struct.unpack("BBBB", image[:4])
if address != esp.BOOTLOADER_FLASH_OFFSET:
return image # not flashing bootloader offset, so don't modify this
if (args.flash_mode, args.flash_freq, args.flash_size) == ("keep",) * 3:
return image # all settings are 'keep', not modifying anything
# easy check if this is an image: does it start with a magic byte?
if magic != esp.ESP_IMAGE_MAGIC:
print(
"Warning: Image file at 0x%x doesn't look like an image file, "
"so not changing any flash settings." % address
)
return image
# make sure this really is an image, and not just data that
# starts with esp.ESP_IMAGE_MAGIC (mostly a problem for encrypted
# images that happen to start with a magic byte
try:
test_image = esp.BOOTLOADER_IMAGE(io.BytesIO(image))
test_image.verify()
except Exception:
print(
"Warning: Image file at 0x%x is not a valid %s image, "
"so not changing any flash settings." % (address, esp.CHIP_NAME)
)
return image
# After the 8-byte header comes the extended header for chips others than ESP8266.
# The 15th byte of the extended header indicates if the image is protected by
# a SHA256 checksum. In that case we recalculate the SHA digest after modifying the header.
sha_appended = args.chip != "esp8266" and image[8 + 15] == 1
if args.flash_mode != "keep":
flash_mode = FLASH_MODES[args.flash_mode]
flash_freq = flash_size_freq & 0x0F
if args.flash_freq != "keep":
flash_freq = esp.parse_flash_freq_arg(args.flash_freq)
flash_size = flash_size_freq & 0xF0
if args.flash_size != "keep":
flash_size = esp.parse_flash_size_arg(args.flash_size)
flash_params = struct.pack(b"BB", flash_mode, flash_size + flash_freq)
if flash_params != image[2:4]:
print("Flash params set to 0x%04x" % struct.unpack(">H", flash_params))
image = image[0:2] + flash_params + image[4:]
# recalculate the SHA digest if it was appended
if sha_appended:
# Since the changes are only made for images located in the bootloader offset,
# we can assume that the image is always a bootloader image.
# For merged binaries, we check the bootloader SHA when parameters are changed.
image_object = esp.BOOTLOADER_IMAGE(io.BytesIO(image))
# get the image header, extended header (if present) and data
image_data_before_sha = image[: image_object.data_length]
# get the image data after the SHA digest (primary for merged binaries)
image_data_after_sha = image[
(image_object.data_length + image_object.SHA256_DIGEST_LEN) :
]
sha_digest_calculated = hashlib.sha256(image_data_before_sha).digest()
image = bytes(
itertools.chain(
image_data_before_sha, sha_digest_calculated, image_data_after_sha
)
)
# get the SHA digest newly stored in the image and compare it to the calculated one
image_stored_sha = image[
image_object.data_length : image_object.data_length
+ image_object.SHA256_DIGEST_LEN
]
if hexify(sha_digest_calculated) == hexify(image_stored_sha):
print("SHA digest in image updated")
else:
print(
"WARNING: SHA recalculation for binary failed!\n"
f"\tExpected calculated SHA: {hexify(sha_digest_calculated)}\n"
f"\tSHA stored in binary: {hexify(image_stored_sha)}"
)
return image
def write_flash(esp, args):
# set args.compress based on default behaviour:
# -> if either --compress or --no-compress is set, honour that
# -> otherwise, set --compress unless --no-stub is set
if args.compress is None and not args.no_compress:
args.compress = not args.no_stub
if not args.force and esp.CHIP_NAME != "ESP8266" and not esp.secure_download_mode:
# Check if secure boot is active
if esp.get_secure_boot_enabled():
for address, _ in args.addr_filename:
if address < 0x8000:
raise FatalError(
"Secure Boot detected, writing to flash regions < 0x8000 "
"is disabled to protect the bootloader. "
"Use --force to override, "
"please use with caution, otherwise it may brick your device!"
)
# Check if chip_id and min_rev in image are valid for the target in use
for _, argfile in args.addr_filename:
try:
image = LoadFirmwareImage(esp.CHIP_NAME, argfile)
except (FatalError, struct.error, RuntimeError):
continue
finally:
argfile.seek(0) # LoadFirmwareImage changes the file handle position
if image.chip_id != esp.IMAGE_CHIP_ID:
raise FatalError(
f"{argfile.name} is not an {esp.CHIP_NAME} image. "
"Use --force to flash anyway."
)
# this logic below decides which min_rev to use, min_rev or min/max_rev_full
if image.max_rev_full == 0: # image does not have max/min_rev_full fields
use_rev_full_fields = False
elif image.max_rev_full == 65535: # image has default value of max_rev_full
use_rev_full_fields = True
if (
image.min_rev_full == 0 and image.min_rev != 0
): # min_rev_full is not set, min_rev is used
use_rev_full_fields = False
else: # max_rev_full set to a version
use_rev_full_fields = True
if use_rev_full_fields:
rev = esp.get_chip_revision()
if rev < image.min_rev_full or rev > image.max_rev_full:
error_str = f"{argfile.name} requires chip revision in range "
error_str += (
f"[v{image.min_rev_full // 100}.{image.min_rev_full % 100} - "
)
if image.max_rev_full == 65535:
error_str += "max rev not set] "
else:
error_str += (
f"v{image.max_rev_full // 100}.{image.max_rev_full % 100}] "
)
error_str += f"(this chip is revision v{rev // 100}.{rev % 100})"
raise FatalError(f"{error_str}. Use --force to flash anyway.")
else:
# In IDF, image.min_rev is set based on Kconfig option.
# For C3 chip, image.min_rev is the Minor revision
# while for the rest chips it is the Major revision.
if esp.CHIP_NAME == "ESP32-C3":
rev = esp.get_minor_chip_version()
else:
rev = esp.get_major_chip_version()
if rev < image.min_rev:
raise FatalError(
f"{argfile.name} requires chip revision "
f"{image.min_rev} or higher (this chip is revision {rev}). "
"Use --force to flash anyway."
)
# In case we have encrypted files to write,
# we first do few sanity checks before actual flash
if args.encrypt or args.encrypt_files is not None:
do_write = True
if not esp.secure_download_mode:
if esp.get_encrypted_download_disabled():
raise FatalError(
"This chip has encrypt functionality "
"in UART download mode disabled. "
"This is the Flash Encryption configuration for Production mode "
"instead of Development mode."
)
crypt_cfg_efuse = esp.get_flash_crypt_config()
if crypt_cfg_efuse is not None and crypt_cfg_efuse != 0xF:
print("Unexpected FLASH_CRYPT_CONFIG value: 0x%x" % (crypt_cfg_efuse))
do_write = False
enc_key_valid = esp.is_flash_encryption_key_valid()
if not enc_key_valid:
print("Flash encryption key is not programmed")
do_write = False
# Determine which files list contain the ones to encrypt
files_to_encrypt = args.addr_filename if args.encrypt else args.encrypt_files
for address, argfile in files_to_encrypt:
if address % esp.FLASH_ENCRYPTED_WRITE_ALIGN:
print(
"File %s address 0x%x is not %d byte aligned, can't flash encrypted"
% (argfile.name, address, esp.FLASH_ENCRYPTED_WRITE_ALIGN)
)
do_write = False
if not do_write and not args.ignore_flash_encryption_efuse_setting:
raise FatalError(
"Can't perform encrypted flash write, "
"consult Flash Encryption documentation for more information"
)
else:
if not args.force and esp.CHIP_NAME != "ESP8266":
# ESP32 does not support `get_security_info()` and `secure_download_mode`
if (
esp.CHIP_NAME != "ESP32"
and esp.secure_download_mode
and bin(esp.get_security_info()["flash_crypt_cnt"]).count("1") & 1 != 0
):
raise FatalError(
"WARNING: Detected flash encryption and "
"secure download mode enabled.\n"
"Flashing plaintext binary may brick your device! "
"Use --force to override the warning."
)
if (
not esp.secure_download_mode
and esp.get_encrypted_download_disabled()
and esp.get_flash_encryption_enabled()
):
raise FatalError(
"WARNING: Detected flash encryption enabled and "
"download manual encrypt disabled.\n"
"Flashing plaintext binary may brick your device! "
"Use --force to override the warning."
)
# verify file sizes fit in flash
flash_end = flash_size_bytes(
detect_flash_size(esp) if args.flash_size == "keep" else args.flash_size
)
if flash_end is not None: # Not in secure download mode
for address, argfile in args.addr_filename:
argfile.seek(0, os.SEEK_END)
if address + argfile.tell() > flash_end:
raise FatalError(
"File %s (length %d) at offset %d "
"will not fit in %d bytes of flash. "
"Use --flash_size argument, or change flashing address."
% (argfile.name, argfile.tell(), address, flash_end)
)
argfile.seek(0)
if args.erase_all:
erase_flash(esp, args)
else:
for address, argfile in args.addr_filename:
argfile.seek(0, os.SEEK_END)
write_end = address + argfile.tell()
argfile.seek(0)
bytes_over = address % esp.FLASH_SECTOR_SIZE
if bytes_over != 0:
print(
"WARNING: Flash address {:#010x} is not aligned "
"to a {:#x} byte flash sector. "
"{:#x} bytes before this address will be erased.".format(
address, esp.FLASH_SECTOR_SIZE, bytes_over
)
)
# Print the address range of to-be-erased flash memory region
print(
"Flash will be erased from {:#010x} to {:#010x}...".format(
address - bytes_over,
div_roundup(write_end, esp.FLASH_SECTOR_SIZE)
* esp.FLASH_SECTOR_SIZE
- 1,
)
)
""" Create a list describing all the files we have to flash.
Each entry holds an "encrypt" flag marking whether the file needs encryption or not.
This list needs to be sorted.
First, append to each entry of our addr_filename list the flag args.encrypt
E.g., if addr_filename is [(0x1000, "partition.bin"), (0x8000, "bootloader")],
all_files will be [
(0x1000, "partition.bin", args.encrypt),
(0x8000, "bootloader", args.encrypt)
],
where, of course, args.encrypt is either True or False
"""
all_files = [
(offs, filename, args.encrypt) for (offs, filename) in args.addr_filename
]
"""
Now do the same with encrypt_files list, if defined.
In this case, the flag is True
"""
if args.encrypt_files is not None:
encrypted_files_flag = [
(offs, filename, True) for (offs, filename) in args.encrypt_files
]
# Concatenate both lists and sort them.
# As both list are already sorted, we could simply do a merge instead,
# but for the sake of simplicity and because the lists are very small,
# let's use sorted.
all_files = sorted(all_files + encrypted_files_flag, key=lambda x: x[0])
for address, argfile, encrypted in all_files:
compress = args.compress
# Check whether we can compress the current file before flashing
if compress and encrypted:
print("\nWARNING: - compress and encrypt options are mutually exclusive ")
print("Will flash %s uncompressed" % argfile.name)
compress = False
if args.no_stub:
print("Erasing flash...")
image = pad_to(
argfile.read(), esp.FLASH_ENCRYPTED_WRITE_ALIGN if encrypted else 4
)
if len(image) == 0:
print("WARNING: File %s is empty" % argfile.name)
continue
if not esp.secure_download_mode and not esp.get_secure_boot_enabled():
image = _update_image_flash_params(esp, address, args, image)
else:
print(
"WARNING: Security features enabled, so not changing any flash settings."
)
calcmd5 = hashlib.md5(image).hexdigest()
uncsize = len(image)
if compress:
uncimage = image
image = zlib.compress(uncimage, 9)
original_image = image # Save the whole image in case retry is needed
# Try again if reconnect was successful
for attempt in range(1, esp.WRITE_FLASH_ATTEMPTS + 1):
try:
if compress:
# Decompress the compressed binary a block at a time,
# to dynamically calculate the timeout based on the real write size
decompress = zlib.decompressobj()
blocks = esp.flash_defl_begin(uncsize, len(image), address)
else:
blocks = esp.flash_begin(
uncsize, address, begin_rom_encrypted=encrypted
)
argfile.seek(0) # in case we need it again
seq = 0
bytes_sent = 0 # bytes sent on wire
bytes_written = 0 # bytes written to flash
t = time.time()
timeout = DEFAULT_TIMEOUT
while len(image) > 0:
print_overwrite(
"Writing at 0x%08x... (%d %%)"
% (address + bytes_written, 100 * (seq + 1) // blocks)
)
sys.stdout.flush()
block = image[0 : esp.FLASH_WRITE_SIZE]
if compress:
# feeding each compressed block into the decompressor lets us
# see block-by-block how much will be written
block_uncompressed = len(decompress.decompress(block))
bytes_written += block_uncompressed
block_timeout = max(
DEFAULT_TIMEOUT,
timeout_per_mb(
ERASE_WRITE_TIMEOUT_PER_MB, block_uncompressed
),
)
if not esp.IS_STUB:
timeout = block_timeout # ROM code writes block to flash before ACKing
esp.flash_defl_block(block, seq, timeout=timeout)
if esp.IS_STUB:
# Stub ACKs when block is received,
# then writes to flash while receiving the block after it
timeout = block_timeout
else:
# Pad the last block
block = block + b"\xff" * (esp.FLASH_WRITE_SIZE - len(block))
if encrypted:
esp.flash_encrypt_block(block, seq)
else:
esp.flash_block(block, seq)
bytes_written += len(block)
bytes_sent += len(block)
image = image[esp.FLASH_WRITE_SIZE :]
seq += 1
break
except SerialException:
if attempt == esp.WRITE_FLASH_ATTEMPTS or encrypted:
# Already retried once or encrypted mode is disabled because of security reasons
raise
print("\nLost connection, retrying...")
esp._port.close()
print("Waiting for the chip to reconnect", end="")
for _ in range(DEFAULT_CONNECT_ATTEMPTS):
try:
time.sleep(1)
esp._port.open()
print() # Print new line which was suppressed by print(".")
esp.connect()
if esp.IS_STUB:
# Hack to bypass the stub overwrite check
esp.IS_STUB = False
# Reflash stub because chip was reset
esp = esp.run_stub()
image = original_image
break
except SerialException:
print(".", end="")
sys.stdout.flush()
else:
raise # Reconnect limit reached
if esp.IS_STUB:
# Stub only writes each block to flash after 'ack'ing the receive,
# so do a final dummy operation which will not be 'ack'ed
# until the last block has actually been written out to flash
esp.read_reg(ESPLoader.CHIP_DETECT_MAGIC_REG_ADDR, timeout=timeout)
t = time.time() - t
speed_msg = ""
if compress:
if t > 0.0:
speed_msg = " (effective %.1f kbit/s)" % (uncsize / t * 8 / 1000)
print_overwrite(
"Wrote %d bytes (%d compressed) at 0x%08x in %.1f seconds%s..."
% (uncsize, bytes_sent, address, t, speed_msg),
last_line=True,
)
else:
if t > 0.0:
speed_msg = " (%.1f kbit/s)" % (bytes_written / t * 8 / 1000)
print_overwrite(
"Wrote %d bytes at 0x%08x in %.1f seconds%s..."
% (bytes_written, address, t, speed_msg),
last_line=True,
)
if not encrypted and not esp.secure_download_mode:
try:
res = esp.flash_md5sum(address, uncsize)
if res != calcmd5:
print("File md5: %s" % calcmd5)
print("Flash md5: %s" % res)
print(
"MD5 of 0xFF is %s"
% (hashlib.md5(b"\xff" * uncsize).hexdigest())
)
raise FatalError("MD5 of file does not match data in flash!")
else:
print("Hash of data verified.")
except NotImplementedInROMError:
pass
print("\nLeaving...")
if esp.IS_STUB:
# skip sending flash_finish to ROM loader here,
# as it causes the loader to exit and run user code
esp.flash_begin(0, 0)
# Get the "encrypted" flag for the last file flashed
# Note: all_files list contains triplets like:
# (address: Integer, filename: String, encrypted: Boolean)
last_file_encrypted = all_files[-1][2]
# Check whether the last file flashed was compressed or not
if args.compress and not last_file_encrypted:
esp.flash_defl_finish(False)
else:
esp.flash_finish(False)
if args.verify:
print("Verifying just-written flash...")
print(
"(This option is deprecated, "
"flash contents are now always read back after flashing.)"
)
# If some encrypted files have been flashed,
# print a warning saying that we won't check them
if args.encrypt or args.encrypt_files is not None:
print("WARNING: - cannot verify encrypted files, they will be ignored")
# Call verify_flash function only if there is at least
# one non-encrypted file flashed
if not args.encrypt:
verify_flash(esp, args)
def image_info(args):
def v2():
def get_key_from_value(dict, val):
"""Get key from value in dictionary"""
for key, value in dict.items():
if value == val:
return key
return None
print()
title = "{} image header".format(args.chip.upper())
print(title)
print("=" * len(title))
print("Image version: {}".format(image.version))
print(
"Entry point: {:#8x}".format(image.entrypoint)
if image.entrypoint != 0
else "Entry point not set"
)
print("Segments: {}".format(len(image.segments)))
# Flash size
flash_s_bits = image.flash_size_freq & 0xF0 # high four bits
flash_s = get_key_from_value(image.ROM_LOADER.FLASH_SIZES, flash_s_bits)
print(
"Flash size: {}".format(flash_s)
if flash_s is not None
else "WARNING: Invalid flash size ({:#02x})".format(flash_s_bits)
)
# Flash frequency
flash_fr_bits = image.flash_size_freq & 0x0F # low four bits
flash_fr = get_key_from_value(image.ROM_LOADER.FLASH_FREQUENCY, flash_fr_bits)
print(
"Flash freq: {}".format(flash_fr)
if flash_fr is not None
else "WARNING: Invalid flash frequency ({:#02x})".format(flash_fr_bits)
)
# Flash mode
flash_mode = get_key_from_value(FLASH_MODES, image.flash_mode)
print(
"Flash mode: {}".format(flash_mode.upper())
if flash_mode is not None
else "WARNING: Invalid flash mode ({})".format(image.flash_mode)
)
# Extended header (ESP32 and later only)
if args.chip != "esp8266":
print()
title = "{} extended image header".format(args.chip.upper())
print(title)
print("=" * len(title))
print(
f"WP pin: {image.wp_pin:#02x}",
*["(disabled)"] if image.wp_pin == image.WP_PIN_DISABLED else [],
)
print(
"Flash pins drive settings: "
"clk_drv: {:#02x}, q_drv: {:#02x}, d_drv: {:#02x}, "
"cs0_drv: {:#02x}, hd_drv: {:#02x}, wp_drv: {:#02x}".format(
image.clk_drv,
image.q_drv,
image.d_drv,
image.cs_drv,
image.hd_drv,
image.wp_drv,
)
)
try:
chip = next(
chip
for chip in CHIP_DEFS.values()
if getattr(chip, "IMAGE_CHIP_ID", None) == image.chip_id
)
print(f"Chip ID: {image.chip_id} ({chip.CHIP_NAME})")
except StopIteration:
print(f"Chip ID: {image.chip_id} (Unknown ID)")
print(
"Minimal chip revision: "
f"v{image.min_rev_full // 100}.{image.min_rev_full % 100}, "
f"(legacy min_rev = {image.min_rev})"
)
print(
"Maximal chip revision: "
f"v{image.max_rev_full // 100}.{image.max_rev_full % 100}"
)
print()
# Segments overview
title = "Segments information"
print(title)
print("=" * len(title))
headers_str = "{:>7} {:>7} {:>10} {:>10} {:10}"
print(
headers_str.format(
"Segment", "Length", "Load addr", "File offs", "Memory types"
)
)
print(
"{} {} {} {} {}".format("-" * 7, "-" * 7, "-" * 10, "-" * 10, "-" * 12)
)
format_str = "{:7} {:#07x} {:#010x} {:#010x} {}"
app_desc = None
bootloader_desc = None
for idx, seg in enumerate(image.segments):
segs = seg.get_memory_type(image)
seg_name = ", ".join(segs)
if "DROM" in segs: # The DROM segment starts with the esp_app_desc_t struct
app_desc = seg.data[:256]
elif "DRAM" in segs:
# The DRAM segment starts with the esp_bootloader_desc_t struct
if len(seg.data) >= 80:
bootloader_desc = seg.data[:80]
print(
format_str.format(idx, len(seg.data), seg.addr, seg.file_offs, seg_name)
)
print()
# Footer
title = f"{args.chip.upper()} image footer"
print(title)
print("=" * len(title))
calc_checksum = image.calculate_checksum()
print(
"Checksum: {:#02x} ({})".format(
image.checksum,
(
"valid"
if image.checksum == calc_checksum
else "invalid - calculated {:02x}".format(calc_checksum)
),
)
)
try:
digest_msg = "Not appended"
if image.append_digest:
is_valid = image.stored_digest == image.calc_digest
digest_msg = "{} ({})".format(
hexify(image.calc_digest, uppercase=False),
"valid" if is_valid else "invalid",
)
print("Validation hash: {}".format(digest_msg))
except AttributeError:
pass # ESP8266 image has no append_digest field
if app_desc:
APP_DESC_STRUCT_FMT = "<II" + "8s" + "32s32s16s16s32s32s" + "80s"
(
magic_word,
secure_version,
reserv1,
version,
project_name,
time,
date,
idf_ver,
app_elf_sha256,
reserv2,
) = struct.unpack(APP_DESC_STRUCT_FMT, app_desc)
if magic_word == 0xABCD5432:
print()
title = "Application information"
print(title)
print("=" * len(title))
print(f'Project name: {project_name.decode("utf-8")}')
print(f'App version: {version.decode("utf-8")}')
print(f'Compile time: {date.decode("utf-8")} {time.decode("utf-8")}')
print(f"ELF file SHA256: {hexify(app_elf_sha256, uppercase=False)}")
print(f'ESP-IDF: {idf_ver.decode("utf-8")}')
print(f"Secure version: {secure_version}")
elif bootloader_desc:
BOOTLOADER_DESC_STRUCT_FMT = "<B" + "3s" + "I32s24s" + "16s"
(
magic_byte,
reserved,
version,
idf_ver,
date_time,
reserved2,
) = struct.unpack(BOOTLOADER_DESC_STRUCT_FMT, bootloader_desc)
if magic_byte == 80:
print()
title = "Bootloader information"
print(title)
print("=" * len(title))
print(f"Bootloader version: {version}")
print(f'ESP-IDF: {idf_ver.decode("utf-8")}')
print(f'Compile time: {date_time.decode("utf-8")}')
print(f"File size: {get_file_size(args.filename)} (bytes)")
with open(args.filename, "rb") as f:
# magic number
try:
common_header = f.read(8)
magic = common_header[0]
except IndexError:
raise FatalError("File is empty")
if magic not in [
ESPLoader.ESP_IMAGE_MAGIC,
ESP8266V2FirmwareImage.IMAGE_V2_MAGIC,
]:
raise FatalError(
"This is not a valid image " "(invalid magic number: {:#x})".format(
magic
)
)
if args.chip == "auto":
try:
extended_header = f.read(16)
# append_digest, either 0 or 1
if extended_header[-1] not in [0, 1]:
raise FatalError("Append digest field not 0 or 1")
chip_id = int.from_bytes(extended_header[4:5], "little")
for rom in [n for n in ROM_LIST if n.CHIP_NAME != "ESP8266"]:
if chip_id == rom.IMAGE_CHIP_ID:
args.chip = rom.CHIP_NAME
break
else:
raise FatalError(f"Unknown image chip ID ({chip_id})")
except FatalError:
args.chip = "esp8266"
print(f"Detected image type: {args.chip.upper()}")
image = LoadFirmwareImage(args.chip, args.filename)
if args.version == "2":
v2()
return
print("Image version: {}".format(image.version))
print(
"Entry point: {:8x}".format(image.entrypoint)
if image.entrypoint != 0
else "Entry point not set"
)
print("{} segments".format(len(image.segments)))
print()
idx = 0
for seg in image.segments:
idx += 1
segs = seg.get_memory_type(image)
seg_name = ",".join(segs)
print("Segment {}: {} [{}]".format(idx, seg, seg_name))
calc_checksum = image.calculate_checksum()
print(
"Checksum: {:02x} ({})".format(
image.checksum,
(
"valid"
if image.checksum == calc_checksum