-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds initial support for the flasher stub. The stubs get pulled from the original esptool repository and converted to .h/.c files using a Python script. This script can be run using the custom CMake target gen_esp_stubs. Additions to the public API: - esp_loader_connect_to_stub Upload stub loader, then connect to it instead of the internal ROM loader. - esp_loader_change_transmission_rate_stub Like esp_loader_change_transmission_rate but has a second parameter for the stub version of the CHANGE_BAUDRATE command. Additions to private API: - loader_run_stub Upload and run the stub loader. Closes #103
- Loading branch information
Showing
11 changed files
with
620 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.vscode | ||
build | ||
sdkconfig | ||
sdkconfig.old | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
macro(add_gen_esp_stubs_target) | ||
set(ONE_VALUE_KEYWORDS VERSION) | ||
cmake_parse_arguments(ESPTOOL "" "${ONE_VALUE_KEYWORDS}" "" "${ARGN}") | ||
|
||
# Don't make this mandatory, some users might not have Python installed | ||
find_package(Python COMPONENTS Interpreter) | ||
if(NOT Python_Interpreter_FOUND) | ||
message(WARNING "Python not found, gen_esp_stubs target not created") | ||
return() | ||
endif() | ||
|
||
# Run python script to generate esp_stubs.h and esp_stubs.c | ||
add_custom_command( | ||
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/stub.c | ||
COMMAND | ||
${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/gen_esp_stubs.py | ||
${ESPTOOL_VERSION} ${CMAKE_CURRENT_SOURCE_DIR} | ||
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/cmake/gen_esp_stubs.py) | ||
add_custom_target(gen_esp_stubs | ||
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/stub.c) | ||
endmacro() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import base64 | ||
import json | ||
import os | ||
import sys | ||
import urllib.request | ||
|
||
""" | ||
Generate flasher stubs from https://github.com/espressif/esptool/tree/master/esptool/targets/stub_flasher | ||
""" | ||
|
||
# Paths | ||
esptool_version = sys.argv[1] | ||
root_path = sys.argv[2] | ||
priv_inc_path = os.path.join(root_path, "private_include") | ||
src_path = os.path.join(root_path, "src") | ||
hfile_path = os.path.join(priv_inc_path, "esp_stubs.h") | ||
cfile_path = os.path.join(src_path, "esp_stubs.c") | ||
|
||
# Matches order of target_chip_t enumeration | ||
files_to_download = [ | ||
"stub_flasher_8266.json", # ESP8266_CHIP | ||
"stub_flasher_32.json", # ESP32_CHIP | ||
"stub_flasher_32s2.json", # ESP32S2_CHIP | ||
"stub_flasher_32c3.json", # ESP32C3_CHIP | ||
"stub_flasher_32s3.json", # ESP32S3_CHIP | ||
"stub_flasher_32c2.json", # ESP32C2_CHIP | ||
None, # ESP32_RESERVED0_CHIP | ||
"stub_flasher_32h2.json", # ESP32H2_CHIP | ||
"stub_flasher_32c6.json", # ESP32C6_CHIP | ||
] | ||
|
||
# .h and .c file to generate | ||
hfile = open(hfile_path, "w") | ||
cfile = open(cfile_path, "w") | ||
codegen_notice = "// auto-generated stubs from esptool v" + esptool_version + "\n" | ||
|
||
# Write .h file | ||
hfile.write(codegen_notice) | ||
hfile.write( | ||
"\n" | ||
"#pragma once\n" | ||
"\n" | ||
"#include <stdint.h>\n" | ||
"#include <stdbool.h>\n" | ||
'#include "esp_loader.h"\n' | ||
"\n" | ||
"#ifdef __cplusplus\n" | ||
'extern "C" {\n' | ||
"#endif\n" | ||
"\n" | ||
"extern bool esp_stub_running;\n" | ||
"\n" | ||
"#if (defined SERIAL_FLASHER_INTERFACE_UART) || (defined SERIAL_FLASHER_INTERFACE_USB)\n" | ||
"\n" | ||
"typedef struct {\n" | ||
" esp_loader_bin_header_t header;\n" | ||
" esp_loader_bin_segment_t segments[2];\n" | ||
"} esp_stub_t;\n" | ||
"\n" | ||
"extern const esp_stub_t esp_stub[ESP_MAX_CHIP];\n" | ||
"\n" | ||
"#endif\n" | ||
"\n" | ||
"#ifdef __cplusplus\n" | ||
"}\n" | ||
"#endif\n" | ||
) | ||
|
||
# Write .c file | ||
cfile.write(codegen_notice) | ||
cfile.write( | ||
"\n" | ||
'#include "esp_stubs.h"\n' | ||
"\n" | ||
"bool esp_stub_running = false;\n" | ||
"\n" | ||
"#if (defined SERIAL_FLASHER_INTERFACE_UART) || (defined SERIAL_FLASHER_INTERFACE_USB)\n" | ||
"\n" | ||
"#if __STDC_VERSION__ >= 201112L\n" | ||
'_Static_assert(ESP8266_CHIP == 0, "Stub order matches target_chip_t enumeration");\n' | ||
'_Static_assert(ESP32_CHIP == 1, "Stub order matches target_chip_t enumeration");\n' | ||
'_Static_assert(ESP32S2_CHIP == 2, "Stub order matches target_chip_t enumeration");\n' | ||
'_Static_assert(ESP32C3_CHIP == 3, "Stub order matches target_chip_t enumeration");\n' | ||
'_Static_assert(ESP32S3_CHIP == 4, "Stub order matches target_chip_t enumeration");\n' | ||
'_Static_assert(ESP32C2_CHIP == 5, "Stub order matches target_chip_t enumeration");\n' | ||
'_Static_assert(ESP32_RESERVED0_CHIP == 6, "Stub order matches target_chip_t enumeration");\n' | ||
'_Static_assert(ESP32H2_CHIP == 7, "Stub order matches target_chip_t enumeration");\n' | ||
'_Static_assert(ESP32C6_CHIP == 8, "Stub order matches target_chip_t enumeration");\n' | ||
"_Static_assert(ESP_MAX_CHIP == " | ||
+ str(len(files_to_download)) | ||
+ ', "Stub order matches target_chip_t enumeration");\n' | ||
"#endif\n" | ||
"\n" | ||
"const esp_stub_t esp_stub[ESP_MAX_CHIP] = {\n" | ||
"\n" | ||
) | ||
|
||
for file_to_download in files_to_download: | ||
if file_to_download is None: | ||
cfile.write(" // placeholder\n" " {},\n" "\n") | ||
else: | ||
with urllib.request.urlopen( | ||
"https://raw.githubusercontent.com/espressif/esptool/v" | ||
+ esptool_version | ||
+ "/esptool/targets/stub_flasher/" | ||
+ file_to_download | ||
) as url: | ||
# Read stub_flasher*.json | ||
stub = json.load(url) | ||
entry = stub["entry"] | ||
text = base64.b64decode(stub["text"]) | ||
text_start = stub["text_start"] | ||
try: | ||
data = base64.b64decode(stub["data"]) | ||
data_start = stub["data_start"] | ||
except KeyError: | ||
data = None | ||
data_start = None | ||
bss_start = stub["bss_start"] | ||
|
||
# According to esptool source those data could potentially be None | ||
text_str = ",".join([hex(b) for b in text]) | ||
data_str = "" if data is None else ",".join([hex(b) for b in data]) | ||
data_size_str = str(0) if data is None else str(len(data)) | ||
data_start_str = str(0) if data_start is None else str(data_start) | ||
|
||
cfile.write( | ||
" // " + file_to_download + "\n" | ||
" {\n" | ||
" .header = {\n" | ||
" .entrypoint = " + str(entry) + ",\n" | ||
" },\n" | ||
" .segments = {\n" | ||
" {\n" | ||
" .addr = " + str(text_start) + ",\n" | ||
" .size = " + str(len(text)) + ",\n" | ||
" .data = (uint8_t[]){" + text_str + "},\n" | ||
" },\n" | ||
" {\n" | ||
" .addr = " + data_start_str + ",\n" | ||
" .size = " + data_size_str + ",\n" | ||
" .data = (uint8_t[]){" + data_str + "},\n" | ||
" },\n" | ||
" },\n" | ||
" },\n" | ||
"\n" | ||
) | ||
|
||
cfile.write("};\n" "\n" "#endif\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// auto-generated stubs from esptool v4.7.0 | ||
|
||
#pragma once | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
#include "esp_loader.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
extern bool esp_stub_running; | ||
|
||
#if (defined SERIAL_FLASHER_INTERFACE_UART) || (defined SERIAL_FLASHER_INTERFACE_USB) | ||
|
||
typedef struct { | ||
esp_loader_bin_header_t header; | ||
esp_loader_bin_segment_t segments[2]; | ||
} esp_stub_t; | ||
|
||
extern const esp_stub_t esp_stub[ESP_MAX_CHIP]; | ||
|
||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.