Skip to content

Commit 01be2bf

Browse files
committed
contrib/loaders: replace old espressif stub with the new one
1 parent 0be9251 commit 01be2bf

File tree

471 files changed

+3325
-38366
lines changed

Some content is hidden

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

471 files changed

+3325
-38366
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
[submodule "src/jtag/drivers/libjaylink"]
66
path = src/jtag/drivers/libjaylink
77
url = https://gitlab.zapb.de/libjaylink/libjaylink.git
8+
[submodule "contrib/loaders/flash/espressif/esp-stub-lib"]
9+
path = contrib/loaders/flash/espressif/esp-stub-lib
10+
url = https://github.com/espressif/esp-stub-lib.git

CMakeLists.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ install(FILES contrib/60-openocd.rules DESTINATION ${pkgdatadir}/contrib)
3131
install(DIRECTORY contrib/libdcc DESTINATION ${pkgdatadir}/contrib)
3232
install(FILES tools/esp_detect_config.py DESTINATION ${pkgdatadir}/espressif/tools)
3333
install(FILES tools/esp_problems_hints.yml DESTINATION ${pkgdatadir}/espressif/tools)
34-
install(FILES contrib/loaders/flash/espressif/esp32c6/inc/stub_flash_idf_binary_code.inc DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32c6)
35-
install(FILES contrib/loaders/flash/espressif/esp32c6/inc/stub_flash_idf_binary_data.inc DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32c6)
36-
install(FILES contrib/loaders/flash/espressif/esp32c6/stub_flash_idf_image.h DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32c6)
37-
install(FILES contrib/loaders/flash/espressif/esp32h2/inc/stub_flash_idf_binary_code.inc DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32h2)
38-
install(FILES contrib/loaders/flash/espressif/esp32h2/inc/stub_flash_idf_binary_data.inc DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32h2)
39-
install(FILES contrib/loaders/flash/espressif/esp32h2/stub_flash_idf_image.h DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32h2)
34+
35+
#TODO: enable when the stub code is ready
36+
#install(FILES contrib/loaders/flash/espressif/esp32c6/inc/stub_flash_idf_binary_code.inc DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32c6)
37+
#install(FILES contrib/loaders/flash/espressif/esp32c6/inc/stub_flash_idf_binary_data.inc DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32c6)
38+
#install(FILES contrib/loaders/flash/espressif/esp32c6/stub_flash_idf_image.h DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32c6)
39+
#install(FILES contrib/loaders/flash/espressif/esp32h2/inc/stub_flash_idf_binary_code.inc DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32h2)
40+
#install(FILES contrib/loaders/flash/espressif/esp32h2/inc/stub_flash_idf_binary_data.inc DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32h2)
41+
#install(FILES contrib/loaders/flash/espressif/esp32h2/stub_flash_idf_image.h DESTINATION ${pkgdatadir}/espressif/stub_bins/esp32h2)
4042
get_property(ulink_firmware TARGET ocdjtagdrivers PROPERTY ULINK_FIRMWARE)
4143
if(BUILD_JLINK)
4244
install(FILES ${ulink_firmware} DESTINATION ${pkgdatadir}/OpenULINK)

Makefile.am

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ dist_esptools_DATA = \
2424
tools/esp_detect_config.py \
2525
tools/esp_problems_hints.yml
2626

27-
esp32c6stubsdir = $(pkgdatadir)/espressif/stub_bins/esp32c6
28-
dist_esp32c6stubs_DATA = \
29-
contrib/loaders/flash/espressif/esp32c6/inc/stub_flash_idf_binary_code.inc \
30-
contrib/loaders/flash/espressif/esp32c6/inc/stub_flash_idf_binary_data.inc \
31-
contrib/loaders/flash/espressif/esp32c6/stub_flash_idf_image.h
32-
33-
esp32h2stubsdir = $(pkgdatadir)/espressif/stub_bins/esp32h2
34-
dist_esp32h2stubs_DATA = \
35-
contrib/loaders/flash/espressif/esp32h2/inc/stub_flash_idf_binary_code.inc \
36-
contrib/loaders/flash/espressif/esp32h2/inc/stub_flash_idf_binary_data.inc \
37-
contrib/loaders/flash/espressif/esp32h2/stub_flash_idf_image.h
27+
#TODO: enable when the stub code is ready
28+
#esp32c6stubsdir = $(pkgdatadir)/espressif/stub_bins/esp32c6
29+
#dist_esp32c6stubs_DATA = \
30+
# contrib/loaders/flash/espressif/esp32c6/inc/stub_flash_idf_binary_code.inc \
31+
# contrib/loaders/flash/espressif/esp32c6/inc/stub_flash_idf_binary_data.inc \
32+
# contrib/loaders/flash/espressif/esp32c6/stub_flash_idf_image.h
33+
34+
#esp32h2stubsdir = $(pkgdatadir)/espressif/stub_bins/esp32h2
35+
#dist_esp32h2stubs_DATA = \
36+
# contrib/loaders/flash/espressif/esp32h2/inc/stub_flash_idf_binary_code.inc \
37+
# contrib/loaders/flash/espressif/esp32h2/inc/stub_flash_idf_binary_data.inc \
38+
# contrib/loaders/flash/espressif/esp32h2/stub_flash_idf_image.h
3839

3940
SUBDIRS =
4041
DIST_SUBDIRS =
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
cmake_minimum_required(VERSION 3.27)
2+
3+
include(${CMAKE_CURRENT_LIST_DIR}/cmake/util.cmake)
4+
5+
validate_esp_target(${ESP_TARGET})
6+
7+
include(${CMAKE_CURRENT_LIST_DIR}/cmake/build-flags.cmake)
8+
9+
setup_toolchain(${ESP_TARGET} "${XTENSA_TARGETS}")
10+
11+
project(openocd_stub LANGUAGES C CXX ASM)
12+
13+
check_toolchain_version(${CMAKE_C_COMPILER} "esp-14.2.0_20241119")
14+
15+
# Add common flags and definitions to all targets
16+
add_compile_options(${TARGET_COMPILER_FLAGS})
17+
add_link_options(${COMMON_LINKER_FLAGS})
18+
add_compile_definitions(${COMMON_COMPILE_DEFS})
19+
20+
set(SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/src")
21+
set(LINKER_SCRIPTS
22+
${CMAKE_CURRENT_LIST_DIR}/ld/${ESP_TARGET}.ld
23+
${CMAKE_CURRENT_LIST_DIR}/ld/common.ld
24+
)
25+
file(GLOB SRC_FILES "${SRC_DIR}/*.c" "${SRC_DIR}/*.S")
26+
27+
# Define command names. Should be the same order as the commands in the esp_stub.h file
28+
set(COMMANDS
29+
"cmd_test1"
30+
"cmd_test2"
31+
"cmd_test_all"
32+
)
33+
34+
# Define enabled macros for each command
35+
define_property(GLOBAL PROPERTY COMMAND_MACROS)
36+
set_property(GLOBAL PROPERTY COMMAND_MACROS:cmd_test1 STUB_CMD_TEST1_ENABLED)
37+
set_property(GLOBAL PROPERTY COMMAND_MACROS:cmd_test2 STUB_CMD_TEST2_ENABLED)
38+
set_property(GLOBAL PROPERTY COMMAND_MACROS:cmd_test_all
39+
STUB_CMD_TEST1_ENABLED
40+
STUB_CMD_TEST2_ENABLED
41+
STUB_LOG_ENABLED
42+
)
43+
44+
add_subdirectory(esp-stub-lib)
45+
46+
# Create a list to hold all target names
47+
set(ALL_TARGETS)
48+
49+
# Iterate over each command and create a separate target
50+
foreach(COMMAND ${COMMANDS})
51+
string(TOUPPER ${COMMAND} COMMAND_UPPER)
52+
set(TARGET_NAME stub_${ESP_TARGET}_${COMMAND})
53+
set(TARGET_ELF ${TARGET_NAME}.elf)
54+
set(ESP_STUB_LIB esp-stub-lib-${COMMAND})
55+
set(ESP_TARGET_LIB ${ESP_TARGET}-${COMMAND})
56+
57+
# Project setup for each command
58+
add_executable(${TARGET_ELF} ${SRC_FILES})
59+
target_include_directories(${TARGET_ELF}
60+
PRIVATE include
61+
PRIVATE include/private
62+
)
63+
foreach(script ${LINKER_SCRIPTS})
64+
target_link_options(${TARGET_ELF} PRIVATE -T${script})
65+
endforeach()
66+
67+
if(${ESP_TARGET} STREQUAL "esp8266")
68+
target_link_options(${TARGET_ELF} PRIVATE -Wl,--entry=stub_main_esp8266)
69+
endif()
70+
71+
get_property(COMMAND_DEFINES GLOBAL PROPERTY COMMAND_MACROS:${COMMAND})
72+
73+
set(STUB_COMPILE_DEFS "${COMMAND_DEFINES}")
74+
75+
add_subdirectory(esp-stub-lib ${ESP_STUB_LIB})
76+
77+
target_compile_definitions(${TARGET_ELF} PRIVATE ${STUB_COMPILE_DEFS})
78+
79+
set(MAP_FILE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.map)
80+
target_link_options(${TARGET_ELF} PRIVATE -Wl,-Map=${MAP_FILE})
81+
set_directory_properties(PROPERTIES ADDITIONAL_CLEAN_FILES ${MAP_FILE})
82+
83+
add_custom_command(TARGET ${TARGET_ELF} POST_BUILD
84+
COMMAND ${CMAKE_SIZE} ${TARGET_ELF}
85+
COMMAND ${CMAKE_OBJDUMP} -d $<TARGET_FILE:${TARGET_ELF}> -M no-aliases > ${CMAKE_BINARY_DIR}/${TARGET_NAME}.asm
86+
)
87+
88+
# Link each target with the built esp-stub-lib
89+
target_link_libraries(${TARGET_ELF} PRIVATE ${ESP_STUB_LIB})
90+
91+
list(APPEND ALL_TARGETS ${TARGET_ELF})
92+
93+
endforeach()
94+
95+
string(REPLACE ";" "," COMMANDS_LIST "${COMMANDS}")
96+
add_custom_target(generate_image_headers ALL
97+
DEPENDS ${ALL_TARGETS}
98+
COMMAND ${CMAKE_COMMAND}
99+
-DESP_TARGET=${ESP_TARGET}
100+
-DCOMMANDS="${COMMANDS_LIST}"
101+
-DCMAKE_OBJCOPY=${CMAKE_OBJCOPY}
102+
-DCMAKE_READELF=${CMAKE_READELF}
103+
-P ${CMAKE_CURRENT_LIST_DIR}/cmake/generate_header.cmake
104+
COMMENT "Generating stub image headers"
105+
)
Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
1-
# ESP32 Stubs
1+
## Building Stub for OpenOCD
22

3-
These are small bits of code that run on the ESP32 to facilitate OpenOCD operation on the chip. The generated `.inc` files in these directories are included in the final `openocd` binary, but these `.inc` files need to be generated separately.
3+
### Using build script
44

5-
## Building Stubs
5+
The simplest way to build is using the provided build script:
66

7-
The stubs use a specific build of ESP-IDF. Look at the `stub_flasher_image.h` file for the `ESP32*_STUB_BUILD_IDF_REV` comment to find the ESP-IDF SHA that you need to use to build the stub. The process for each chip (e.g. esp32, esp32s3, etc) is roughly the same, though the stubs may use different versions of IDF. The process is:
8-
9-
1. Download the git repo for the ESP-IDF version.
107
```bash
11-
git clone https://github.com/espressif/esp-idf.git
12-
```
8+
# Build for all supported targets
9+
./build.sh all
1310

14-
2. Checkout the right version of ESP-IDF based on the comment in `stub_flasher_image.h` for your target chip (e.g. `036bd3eb26` from `esp32s3/stub_flasher_image.h`)
15-
```bash
16-
cd esp-idf
17-
git checkout 036bd3eb26 # or whatever the SHA is
18-
git submodule update --init --recursive
11+
# Or specify a different target
12+
./build.sh esp32s2
13+
14+
# Clean all built files and directories
15+
./build.sh clean
1916
```
2017

21-
3. Set up the ESP-IDF environment
18+
### Manual Build
19+
20+
1. Create and enter build directory:
2221
```bash
23-
./install.sh
24-
source export.sh
22+
mkdir -p build && cd build
2523
```
2624

27-
4. Go back to your `openocd-esp32` directory and build the stub for your target chip
25+
2. Configure CMake with your ESP target:
2826
```bash
29-
cd ../openocd-esp32/contrib/loaders/flash/espressif/esp32s3
27+
mkdir esp32 && cd esp32
28+
cmake -DESP_TARGET=esp32 ../.. # Replace esp32 with your target (esp32s2, etc.)
3029
```
3130

32-
5. Build the stub for all makefile targets. Currently, one target with disabled logs, and the other with `-DSTUB_LOG_ENABLE=1` macro. Latter will help to transfer the stub logs to the OpenOCD screen.
31+
3. Build the project:
3332
```bash
34-
make all
33+
cmake --build .
34+
35+
# Or
36+
make -j4
3537
```
3638

37-
6. Now you can go through the normal OpenOCD build process, and the stub images will be included in the final binary.
39+
## Build Outputs
40+
41+
After a successful build, you'll find:
42+
- `build/<target>/stub_<target>.elf` - The compiled binary
43+
- `build/<target>/stub_<target>.map` - Memory map file
44+
- `build/<target>/stub_<target>.asm` - Disassembly output
45+
46+
## Notes
47+
48+
- Make sure you have exported the esp-idf environment with the correct toolchain for your target ESP chip. https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html
49+
- The ESP8266 toolchain is older than the others. `./build.sh esp8266` will handle installing it.
50+
- The example assumes the parent directory contains the ESP stub library
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
TARGETS="esp32 esp32s2 esp32s3 esp32c2 esp32c3 esp32c5 esp32c6 esp32c61 esp32h2 esp32p4"
5+
6+
ESP8266_LINUX_TOOLCHAIN_URL="https://dl.espressif.com/dl/xtensa-lx106-elf-gcc8_4_0-esp-2020r3-linux-amd64.tar.gz"
7+
ESP8266_MACOS_TOOLCHAIN_URL="https://dl.espressif.com/dl/xtensa-lx106-elf-gcc8_4_0-esp-2020r3-macos.tar.gz"
8+
9+
download_esp8266_toolchain() {
10+
mkdir -p toolchain
11+
cd toolchain
12+
13+
filename=$(basename "$ESP8266_TOOLCHAIN_URL")
14+
if [ ! -f "$filename" ]; then
15+
echo "Downloading ESP8266 toolchain... $ESP8266_TOOLCHAIN_URL"
16+
wget "$ESP8266_TOOLCHAIN_URL" --no-verbose
17+
tar -xzf "$filename"
18+
rm "$filename"
19+
else
20+
echo "ESP8266 toolchain already exists. Skipping download."
21+
fi
22+
23+
cd ..
24+
}
25+
26+
build_target() {
27+
local target=$1
28+
echo "Building for $target..."
29+
30+
if [ "$target" = "esp8266" ]; then
31+
if [[ "$OSTYPE" == "windows"* ]]; then
32+
echo "ESP8266 build is only supported on unix-like systems. Skipping..."
33+
return
34+
elif [[ "$OSTYPE" == "darwin"* ]]; then
35+
ESP8266_TOOLCHAIN_URL=$ESP8266_MACOS_TOOLCHAIN_URL
36+
else
37+
ESP8266_TOOLCHAIN_URL=$ESP8266_LINUX_TOOLCHAIN_URL
38+
fi
39+
40+
# Download and setup ESP8266 toolchain if needed
41+
if [ ! -d "toolchain/xtensa-lx106-elf" ]; then
42+
download_esp8266_toolchain
43+
fi
44+
export PATH="$PWD/toolchain/xtensa-lx106-elf/bin:$PATH"
45+
fi
46+
47+
mkdir -p build/$target
48+
cd build/$target
49+
cmake -DESP_TARGET=$target ../..
50+
make
51+
cd ../..
52+
}
53+
54+
if [ "$1" = "all" ]; then
55+
# Build all targets
56+
for target in $TARGETS; do
57+
build_target $target
58+
done
59+
elif [ "$1" = "clean" ]; then
60+
rm -rf build
61+
rm -rf images
62+
else
63+
# Build specific target
64+
if [[ ! " $TARGETS " =~ " $1 " ]]; then
65+
echo "Usage: $0 <target|all|clean>"
66+
echo "Available targets: $TARGETS"
67+
exit 1
68+
fi
69+
build_target $1
70+
fi
71+
72+
# usage: ./build.sh all
73+
# usage: ./build.sh esp32
74+
# usage: ./build.sh clean
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
set(COMMON_COMPILER_FLAGS
2+
-Wall
3+
-Werror
4+
-Wextra
5+
-Wshadow
6+
-Wundef
7+
-Wconversion
8+
-Os
9+
-nostdlib
10+
-fno-builtin
11+
-fno-common
12+
-g
13+
-ffunction-sections
14+
-fdata-sections
15+
-std=gnu17
16+
)
17+
18+
set(XTENSA_COMPILER_FLAGS
19+
${COMMON_COMPILER_FLAGS}
20+
-mlongcalls
21+
-mtext-section-literals
22+
-flto
23+
)
24+
25+
set(ESP8266_COMPILER_FLAGS
26+
${COMMON_COMPILER_FLAGS}
27+
-mlongcalls
28+
-mtext-section-literals
29+
-DESP8266
30+
)
31+
32+
set(RISCV_COMPILER_FLAGS
33+
${COMMON_COMPILER_FLAGS}
34+
-flto
35+
)
36+
37+
set(COMMON_LINKER_FLAGS
38+
"-nodefaultlibs"
39+
"-Wl,-static"
40+
"-Wl,--gc-sections"
41+
"-Wl,--start-group"
42+
"-lgcc"
43+
"-lc"
44+
"-Wl,--end-group"
45+
"-Wl,--undefined=s_esp_stub_desc"
46+
)
47+
48+
set(COMMON_COMPILE_DEFS "asm=__asm__")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set(CMAKE_SYSTEM_NAME Generic)
2+
set(CMAKE_SYSTEM_VERSION 1)
3+
set(CMAKE_SYSTEM_PROCESSOR XTENSA)
4+
5+
set(CMAKE_TOOLCHAIN_PREFIX xtensa-lx106-elf)
6+
set(CMAKE_C_COMPILER ${CMAKE_TOOLCHAIN_PREFIX}-gcc)
7+
set(CMAKE_CXX_COMPILER ${CMAKE_TOOLCHAIN_PREFIX}-g++)
8+
set(CMAKE_ASM_COMPILER ${CMAKE_TOOLCHAIN_PREFIX}-gcc)
9+
set(CMAKE_OBJCOPY ${CMAKE_TOOLCHAIN_PREFIX}-objcopy)
10+
set(CMAKE_OBJDUMP ${CMAKE_TOOLCHAIN_PREFIX}-objdump)
11+
set(CMAKE_READELF ${CMAKE_TOOLCHAIN_PREFIX}-readelf)
12+
set(CMAKE_SIZE ${CMAKE_TOOLCHAIN_PREFIX}-size)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy