Skip to content

Commit e7fa981

Browse files
Marek Matejmarekmatej
authored andcommitted
tools: replace flasher_stub location
Replace esptool hosted flasher_stub by the snapshot of the https://github.com/espressif/esptool-legacy-flasher-stub (sha256: bd267d361412dbbba410cc36c6e7aa58812fc1bd) Signed-off-by: Marek Matej <marek.matej@espressif.com>
1 parent cb0fd49 commit e7fa981

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+33515
-0
lines changed

tools/flasher_stub/Makefile

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# Makefile to compile the flasher stub program
2+
#
3+
# Note that YOU DO NOT NEED TO COMPILE THIS IN ORDER TO JUST USE
4+
# esptool.py - a precompiled version is included in esptool.py,
5+
# so if you don't want to modify the stub code then you are good to go.
6+
#
7+
# See the comments in the top of the Makefile for parameters that
8+
# you probably want to override.
9+
#
10+
# Copyright (c) 2016 Cesanta Software Limited & Angus Gratton
11+
# All rights reserved
12+
#
13+
#
14+
# This program is free software; you can redistribute it and/or modify it under
15+
# the terms of the GNU General Public License as published by the Free Software
16+
# Foundation; either version 2 of the License, or (at your option) any later version.
17+
#
18+
# This program is distributed in the hope that it will be useful, but WITHOUT
19+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20+
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21+
#
22+
# You should have received a copy of the GNU General Public License along with
23+
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
24+
# Street, Fifth Floor, Boston, MA 02110-1301 USA.
25+
26+
# Adapted from Cesanta's original Makefile at
27+
# https://github.com/cesanta/fnc/tree/master/common/platforms/esp8266/stubs
28+
29+
# Override these variables on the command line
30+
# or set them in a local.mk
31+
-include local.mk
32+
33+
# Prefix for each cross compiler (can include a directory path)
34+
# These can be overridden via environment variables or on the make command line
35+
CROSS_8266 ?= xtensa-lx106-elf-
36+
CROSS_32 ?= xtensa-esp32-elf-
37+
CROSS_32S2 ?= xtensa-esp32s2-elf-
38+
CROSS_32S3 ?= xtensa-esp32s3-elf-
39+
CROSS_ESPRISCV32 ?= riscv32-esp-elf-
40+
41+
# extra CFLAGS to be passed on the compiler, in addition to the stock ones
42+
EXTRA_CFLAGS ?=
43+
EXTRA_CFLAGS_ESPRISCV32 ?=
44+
45+
# Python command to invoke wrap_stub.py
46+
WRAP_STUB ?= ./wrap_stub.py
47+
48+
# Pass V=1 to see the commands being executed by make
49+
ifneq ("$(V)","1")
50+
Q = @
51+
endif
52+
53+
STUB = esp
54+
SRCS = stub_flasher.c slip.c stub_commands.c stub_write_flash.c stub_io.c
55+
SRCS_8266 = miniz.c
56+
57+
BUILD_DIR = build
58+
ESPTOOL_STUBS_DIR = ..
59+
60+
STUB_ELF_8266 = $(BUILD_DIR)/$(STUB)8266.elf
61+
STUB_ELF_32 = $(BUILD_DIR)/$(STUB)32.elf
62+
STUB_ELF_32S2 = $(BUILD_DIR)/$(STUB)32s2.elf
63+
STUB_ELF_32S3_BETA_2 = $(BUILD_DIR)/$(STUB)32s3beta2.elf
64+
STUB_ELF_32S3 = $(BUILD_DIR)/$(STUB)32s3.elf
65+
STUB_ELF_32C3 = $(BUILD_DIR)/$(STUB)32c3.elf
66+
STUB_ELF_32C6BETA = $(BUILD_DIR)/$(STUB)32c6beta.elf
67+
STUB_ELF_32H2_BETA_1 = $(BUILD_DIR)/$(STUB)32h2beta1.elf
68+
STUB_ELF_32H2_BETA_2 = $(BUILD_DIR)/$(STUB)32h2beta2.elf
69+
STUB_ELF_32C2 = $(BUILD_DIR)/$(STUB)32c2.elf
70+
STUB_ELF_32C6 = $(BUILD_DIR)/$(STUB)32c6.elf
71+
STUB_ELF_32C61 = $(BUILD_DIR)/$(STUB)32c61.elf
72+
STUB_ELF_32C5 = $(BUILD_DIR)/$(STUB)32c5.elf
73+
STUB_ELF_32C5_BETA_3 = $(BUILD_DIR)/$(STUB)32c5beta3.elf
74+
STUB_ELF_32H2 = $(BUILD_DIR)/$(STUB)32h2.elf
75+
STUB_ELF_32P4 = $(BUILD_DIR)/$(STUB)32p4.elf
76+
77+
STUBS_ELF =
78+
ifneq ($(WITHOUT_ESP8266),1)
79+
STUBS_ELF += \
80+
$(STUB_ELF_8266)
81+
endif
82+
83+
ifneq ($(WITHOUT_ESP32_XTENSA),1)
84+
STUBS_ELF += \
85+
$(STUB_ELF_32) \
86+
$(STUB_ELF_32S2) \
87+
$(STUB_ELF_32S3_BETA_2) \
88+
$(STUB_ELF_32S3)
89+
endif
90+
91+
ifneq ($(WITHOUT_ESP32_RISCV32),1)
92+
STUBS_ELF += \
93+
$(STUB_ELF_32C3) \
94+
$(STUB_ELF_32C6BETA) \
95+
$(STUB_ELF_32H2_BETA_1) \
96+
$(STUB_ELF_32H2_BETA_2) \
97+
$(STUB_ELF_32C2) \
98+
$(STUB_ELF_32C6) \
99+
$(STUB_ELF_32C61) \
100+
$(STUB_ELF_32C5) \
101+
$(STUB_ELF_32C5_BETA_3) \
102+
$(STUB_ELF_32H2) \
103+
$(STUB_ELF_32P4)
104+
endif
105+
106+
.PHONY: all clean install
107+
108+
all: $(STUBS_ELF:.elf=.json)
109+
110+
install: $(patsubst $(BUILD_DIR)/%.elf,$(ESPTOOL_STUBS_DIR)/%.json,$(STUBS_ELF))
111+
112+
$(BUILD_DIR):
113+
$(Q) mkdir $@
114+
115+
$(BUILD_DIR)/%.json: $(BUILD_DIR)/%.elf
116+
@echo " WRAP $^ -> $(BUILD_DIR)"
117+
$(Q) $(WRAP_STUB) $^
118+
119+
$(ESPTOOL_STUBS_DIR)/%.json: $(BUILD_DIR)/%.json
120+
@echo " INSTALL $^ -> $@"
121+
$(Q) cp $^ $@
122+
123+
CFLAGS = -std=c99 -Wall -Werror -Os \
124+
-mtext-section-literals -mlongcalls -nostdlib -fno-builtin -flto \
125+
-Wl,-static -g -ffunction-sections -Wl,--gc-sections -Iinclude -Lld \
126+
$(EXTRA_CFLAGS)
127+
CFLAGS_ESPRISCV32 = -std=c99 -Wall -Werror -Os \
128+
-march=rv32imc -mabi=ilp32 -msmall-data-limit=0 \
129+
-nostdlib -fno-builtin -flto \
130+
-Wl,-static -g -ffunction-sections -Wl,--gc-sections -Iinclude -Lld \
131+
$(EXTRA_CFLAGS_ESPRISCV32)
132+
LDLIBS = -lgcc
133+
134+
$(STUB_ELF_8266): $(SRCS) $(SRCS_8266) $(BUILD_DIR) ld/stub_8266.ld | Makefile
135+
@echo " CC(8266) $^ -> $@"
136+
$(Q) $(CROSS_8266)gcc $(CFLAGS) -DESP8266=1 -Tstub_8266.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
137+
138+
$(STUB_ELF_32): $(SRCS) $(BUILD_DIR) ld/stub_32.ld | Makefile
139+
@echo " CC(32) $^ -> $@"
140+
$(Q) $(CROSS_32)gcc $(CFLAGS) -DESP32=1 -Tstub_32.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
141+
142+
$(STUB_ELF_32S2): $(SRCS) $(BUILD_DIR) ld/stub_32s2.ld
143+
@echo " CC(32S2) $^ -> $@"
144+
$(Q) $(CROSS_32S2)gcc $(CFLAGS) -DESP32S2=1 -Tstub_32s2.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
145+
146+
$(STUB_ELF_32S3_BETA_2): $(SRCS) $(BUILD_DIR) ld/stub_32s3_beta_2.ld
147+
@echo " CC(32S3) $^ -> $@"
148+
$(Q) $(CROSS_32S3)gcc $(CFLAGS) -DESP32S3BETA2=1 -Tstub_32s3_beta_2.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
149+
150+
$(STUB_ELF_32S3): $(SRCS) $(BUILD_DIR) ld/stub_32s3.ld
151+
@echo " CC(32S3) $^ -> $@"
152+
$(Q) $(CROSS_32S3)gcc $(CFLAGS) -DESP32S3=1 -Tstub_32s3.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
153+
154+
$(STUB_ELF_32C3): $(SRCS) $(BUILD_DIR) ld/stub_32c3.ld
155+
@echo " CC(32C3) $^ -> $@"
156+
$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C3=1 -Tstub_32c3.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
157+
158+
$(STUB_ELF_32C6BETA): $(SRCS) $(BUILD_DIR) ld/stub_32c6_beta.ld
159+
@echo " CC(32C6BETA) $^ -> $@"
160+
$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C6BETA=1 -Tstub_32c6_beta.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
161+
162+
$(STUB_ELF_32H2_BETA_1): $(SRCS) $(BUILD_DIR) ld/stub_32h2_beta_1.ld
163+
@echo " CC(32H2BETA1) $^ -> $@"
164+
$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32H2BETA1=1 -Tstub_32h2_beta_1.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
165+
166+
$(STUB_ELF_32H2_BETA_2): $(SRCS) $(BUILD_DIR) ld/stub_32h2_beta_2.ld
167+
@echo " CC(32H2BETA2) $^ -> $@"
168+
$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32H2BETA2=1 -Tstub_32h2_beta_2.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
169+
170+
$(STUB_ELF_32C2): $(SRCS) $(BUILD_DIR) ld/stub_32c2.ld
171+
@echo " CC(32C2) $^ -> $@"
172+
$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C2=1 -Tstub_32c2.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
173+
174+
$(STUB_ELF_32C6): $(SRCS) $(BUILD_DIR) ld/stub_32c6.ld
175+
@echo " CC(32C6) $^ -> $@"
176+
$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C6=1 -Tstub_32c6.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
177+
178+
$(STUB_ELF_32C61): $(SRCS) $(BUILD_DIR) ld/stub_32c61.ld
179+
@echo " CC(32C61) $^ -> $@"
180+
$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C61=1 -Tstub_32c61.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
181+
182+
$(STUB_ELF_32C5): $(SRCS) $(BUILD_DIR) ld/stub_32c5.ld
183+
@echo " CC(32C5) $^ -> $@"
184+
$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C5=1 -Tstub_32c5.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
185+
186+
$(STUB_ELF_32C5_BETA_3): $(SRCS) $(BUILD_DIR) ld/stub_32c5_beta_3.ld
187+
@echo " CC(32C5BETA3) $^ -> $@"
188+
$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C5BETA3=1 -Tstub_32c5_beta_3.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
189+
190+
$(STUB_ELF_32H2): $(SRCS) $(BUILD_DIR) ld/stub_32h2.ld
191+
@echo " CC(32H2) $^ -> $@"
192+
$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32H2=1 -Tstub_32h2.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
193+
194+
$(STUB_ELF_32P4): $(SRCS) $(BUILD_DIR) ld/stub_32p4.ld
195+
@echo " CC(32P4) $^ -> $@"
196+
$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32P4=1 -Tstub_32p4.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
197+
198+
clean:
199+
$(Q) rm -rf $(BUILD_DIR)

tools/flasher_stub/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
This is the source of the software flasher stub.
2+
3+
esptool.py loads the flasher stub into memory and executes it to:
4+
5+
* Add features that the Espressif chips bootloader ROMs do not have.
6+
7+
* Add features to the ESP8266 bootloader ROM which are only in the ROM of newer chips.
8+
9+
* Improve flashing performance over the ROM bootloaders.
10+
11+
* Work around bugs in the ESP8266 ROM bootloader.
12+
13+
Thanks to [Cesanta](http://cesanta.com/) who provided the original ESP8266 stub loader upon which this loader is based.
14+
15+
# To Use
16+
17+
The stub loader is already automatically integrated into esptool.py. You don't need to do anything special to use it.
18+
19+
# To Build
20+
21+
If you want to build the stub to test modifications or updates, here's how:
22+
23+
* You will need an ESP8266 gcc toolchain (xtensa-lx106-elf-) and toolchains for ESP32 and later chips (xtensa-esp32-elf-, riscv32-esp-elf-) on your PATH. If you are developing the stub flasher and plan to send a pull request, please use the latest toolchains available.
24+
25+
* Set the environment variables SDK_PATH to the path to an ESP8266 IoT NON-OS SDK directory (last stub was built with SDK v1.5.1).
26+
27+
* Set the environment variable IDF_PATH to the path to an ESP-IDF directory.
28+
29+
* Set any other environment variables you'd like to override in the Makefile.
30+
31+
* To build type `make`. To build only for the ESP32 family, type `make WITHOUT_ESP8266=1` (this negates the need of an ESP8266 toolchain).
32+
33+
Activating an ESP-IDF environment takes care of most of these steps (only the ESP8266 gcc toolchain has to be manually added to PATH).
34+
35+
# To Test
36+
37+
To test the built stub, you can run `make install` (or `make install WITHOUT_ESP8266=1`), which will update the stubs in `esptool.py` to the newly compiled ones. Or there are some convenience wrappers to make testing quicker to iterate on:
38+
39+
* Running `esptool_test_stub.py` is the same as running `esptool.py`, only it uses the just-compiled stubs from the build directory.
40+
41+
* Running `run_tests_with_stub.sh` is the same as running `pytest test/test_esptool.py`, only it uses the just-compiled stubs from the build directory. See the [Automated Integration Tests](https://docs.espressif.com/projects/esptool/en/latest/contributing.html#automated-integration-tests) docs for more information.

tools/flasher_stub/compare_stubs.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
#
3+
# SPDX-FileCopyrightText: 2014-2022 Fredrik Ahlberg, Angus Gratton,
4+
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
5+
#
6+
# SPDX-License-Identifier: GPL-2.0-or-later
7+
8+
import os
9+
import sys
10+
11+
import esptool
12+
13+
# Compare the esptool stub loaders to freshly built ones in the build directory
14+
#
15+
# (Used by CI to verify the stubs are up to date.)
16+
17+
THIS_SCRIPT_DIR = os.path.dirname(__file__)
18+
STUB_DIR = "../esptool/targets/stub_flasher/"
19+
BUILD_DIR = "build/"
20+
JSON_NAME = "stub_flasher_{}.json"
21+
22+
23+
def diff(path_to_new, path_to_old):
24+
output = ""
25+
new = esptool.loader.StubFlasher(path_to_new)
26+
old = esptool.loader.StubFlasher(path_to_old)
27+
28+
if new.data_start != old.data_start:
29+
output += " Data start: New {:#x}, old {:#x} \n".format(
30+
new.data_start, old.data_start
31+
)
32+
if new.text_start != old.text_start:
33+
output += " Text start: New {:#x}, old {:#x} \n".format(
34+
new.text_start, old.text_start
35+
)
36+
if new.entry != old.entry:
37+
output += " Entrypoint: New {:#x}, old {:#x} \n".format(new.entry, old.entry)
38+
39+
# data
40+
if new.data != old.data:
41+
if len(new.data) == len(old.data):
42+
for i, (new_b, old_b) in enumerate(zip(new.data, old.data)):
43+
if new_b != old_b:
44+
output += " Data byte {:#x}: new {:#04x} old {:#04x} \n".format(
45+
i, new_b, old_b
46+
)
47+
else:
48+
output += " Data length: New {} bytes, old {} bytes \n".format(
49+
len(new.data), len(old.data)
50+
)
51+
52+
# text
53+
if new.text != old.text:
54+
if len(new.text) == len(old.text):
55+
for i, (new_b, old_b) in enumerate(zip(new.text, old.text)):
56+
if new_b != old_b:
57+
output += " Text byte {:#x}: new {:#04x} old {:#04x} \n".format(
58+
i, new_b, old_b
59+
)
60+
else:
61+
output += " Text length: New {} bytes, old {} bytes \n".format(
62+
len(new.text), len(old.text)
63+
)
64+
return output
65+
66+
67+
if __name__ == "__main__":
68+
same = True
69+
for chip in esptool.CHIP_LIST:
70+
print("Comparing {} stub: ".format(chip), end="")
71+
# TODO: [ESP32C5] ESPTOOL-825 remove when supported stub flasher
72+
# TODO: [ESP32C61] IDF-9241 remove when supported stub flasher
73+
if chip in ["esp32c5", "esp32c61"]:
74+
print(f"{chip} has not supported stub yet, skipping...")
75+
continue
76+
77+
chip = chip.replace("esp", "")
78+
old = os.path.join(THIS_SCRIPT_DIR, STUB_DIR, JSON_NAME.format(chip))
79+
new = os.path.join(THIS_SCRIPT_DIR, BUILD_DIR, JSON_NAME.format(chip))
80+
81+
output = diff(new, old)
82+
if output != "":
83+
same = False
84+
print("FAIL")
85+
print(
86+
" Mismatch: {} json file in esptool/targets/stub_flasher/ differs "
87+
"from the just-built stub".format("esp" + chip)
88+
)
89+
print(output)
90+
else:
91+
print("OK")
92+
93+
if not same:
94+
sys.exit(1)
95+
else:
96+
print("Stub flasher json files are the same")
97+
sys.exit(0)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
#
3+
# SPDX-FileCopyrightText: 2014-2022 Fredrik Ahlberg, Angus Gratton,
4+
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
5+
#
6+
# SPDX-License-Identifier: GPL-2.0-or-later
7+
#
8+
# Trivial wrapper program to run esptool.py using the just-compiled
9+
# flasher stub in the build/ subdirectory
10+
#
11+
# For use when developing new flasher_stubs, not important otherwise.
12+
13+
import os.path
14+
import sys
15+
16+
THIS_DIR = os.path.dirname(__file__)
17+
STUBS_BUILD_DIR = os.path.join(THIS_DIR, "build/")
18+
19+
sys.path.append("..")
20+
import esptool # noqa: E402
21+
22+
# Python hackiness: change the path to stub json files in the context of the esptool
23+
# module, so it edits the esptool's global variables
24+
exec(
25+
"loader.STUBS_DIR = '{}'".format(STUBS_BUILD_DIR),
26+
esptool.__dict__,
27+
esptool.__dict__,
28+
)
29+
30+
31+
if __name__ == "__main__":
32+
try:
33+
esptool.main()
34+
except esptool.FatalError as e:
35+
print("\nA fatal error occurred: %s" % e)
36+
sys.exit(2)

0 commit comments

Comments
 (0)