diff --git a/README.md b/README.md index d181f3c..a4f4368 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ # esp-serial-flasher -`esp-serial-flasher` is a portable C library for flashing or loading apps to RAM of Espressif SoCs from other host microcontrollers. +`esp-serial-flasher` is a portable C library for flashing or loading apps to RAM of Espressif SoCs from other host that are supported through their dedicated port drivers. +These can be found in the `port` folder. ## Using the library + `esp-serial-flasher` supports a variety of host/target/interface combinations: Supported **host** microcontrollers: diff --git a/examples/common/example_common.c b/examples/common/example_common.c index c6c8aff..2e57eb9 100644 --- a/examples/common/example_common.c +++ b/examples/common/example_common.c @@ -17,11 +17,21 @@ #include #include #include +#if !defined(WIN32) #include +#endif #include "esp_loader_io.h" #include "esp_loader.h" #include "example_common.h" +#ifndef MAX +#define MAX(a, b) ((a) > (b)) ? (a) : (b) +#endif + +#ifndef MIN +#define MIN(a, b) ((a) < (b)) ? (a) : (b) +#endif + #ifndef SINGLE_TARGET_SUPPORT @@ -31,6 +41,7 @@ #define BOOTLOADER_ADDRESS_V1 0x0 #define PARTITION_ADDRESS 0x8000 #define APPLICATION_ADDRESS 0x10000 +#define MAX_BIN_HEADER_SEGMENTS 16 extern const uint8_t ESP32_bootloader_bin[]; extern const uint32_t ESP32_bootloader_bin_size; @@ -336,7 +347,12 @@ esp_loader_error_t load_ram_binary(const uint8_t *bin) printf("Start loading\n"); esp_loader_error_t err; const esp_loader_bin_header_t *header = (const esp_loader_bin_header_t *)bin; - esp_loader_bin_segment_t segments[header->segments]; + if(header->segments > MAX_BIN_HEADER_SEGMENTS) { + printf("Too many segments in binary header\n"); + return ESP_LOADER_ERROR_INVALID_PARAM; + } + + esp_loader_bin_segment_t segments[MAX_BIN_HEADER_SEGMENTS]; // Parse segments uint32_t seg; diff --git a/examples/desktop_esp32_example/CMakeLists.txt b/examples/desktop_esp32_example/CMakeLists.txt new file mode 100644 index 0000000..1e074ec --- /dev/null +++ b/examples/desktop_esp32_example/CMakeLists.txt @@ -0,0 +1,59 @@ + +cmake_minimum_required(VERSION 3.5) +project(esp-serial-flasher) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../submodules) +find_package(WjwwoodSerial REQUIRED) + +set(ESPSERIAL_PATH ${CMAKE_CURRENT_LIST_DIR}/../../) + +# LIB esp_serial + +include(${ESPSERIAL_PATH}examples/common/bin2array.cmake) +create_resources(${ESPSERIAL_PATH}examples/binaries/Hello-world ${CMAKE_BINARY_DIR}/binaries_1.c) +set_property(SOURCE ${CMAKE_BINARY_DIR}/binaries_1.c PROPERTY GENERATED 1) +create_resources(${ESPSERIAL_PATH}examples/binaries/RAM_APP ${CMAKE_BINARY_DIR}/binaries_2.c) +set_property(SOURCE ${CMAKE_BINARY_DIR}/binaries_2.c PROPERTY GENERATED 1) + + +add_library(esp_serial + ${ESPSERIAL_PATH}port/wjwwood_serial_port.cpp + ${ESPSERIAL_PATH}src/esp_loader.c + ${ESPSERIAL_PATH}src/esp_targets.c + ${ESPSERIAL_PATH}src/md5_hash.c + ${ESPSERIAL_PATH}src/slip.c + ${ESPSERIAL_PATH}src/protocol_uart.c + ${ESPSERIAL_PATH}src/protocol_common.c + ${CMAKE_BINARY_DIR}/binaries_1.c + ${CMAKE_BINARY_DIR}/binaries_2.c + ${ESPSERIAL_PATH}examples/common/example_common.c +) + +target_include_directories(esp_serial PUBLIC + ${ESPSERIAL_PATH}include + ${ESPSERIAL_PATH}port + ${ESPSERIAL_PATH}examples/common +) + +target_include_directories(esp_serial PRIVATE + ${ESPSERIAL_PATH}private_include +) + +target_compile_definitions(esp_serial PUBLIC + -DSERIAL_FLASHER_INTERFACE_USB + -DMD5_ENABLED + -DSERIAL_FLASHER_WRITE_BLOCK_RETRIES=4 + -DSERIAL_FLASHER_DEBUG_TRACE +) + +target_link_libraries(esp_serial PUBLIC wjwwood_serial) + +# EXECUTABLE desktop_esp32_example + +add_executable(desktop_esp32_example + main/main.cpp +) + +target_link_libraries(desktop_esp32_example PRIVATE + esp_serial +) \ No newline at end of file diff --git a/examples/desktop_esp32_example/main/main.cpp b/examples/desktop_esp32_example/main/main.cpp new file mode 100644 index 0000000..40d68af --- /dev/null +++ b/examples/desktop_esp32_example/main/main.cpp @@ -0,0 +1,65 @@ +/* Flash multiple partitions example + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +#include "stdio.h" +#include + +#include "wjwwood_serial_port.h" +#include "esp_loader.h" +#include "esp_loader_io.h" +extern "C" { +#include "example_common.h" + +} + +#include "serial/serial.h" + +#define HIGHER_BAUDRATE 230400 + +int main(int argv, char **argc) +{ + if(argv < 2) { + printf("Usage: %s \n", argc[0]); + return 1; + } + + example_binaries_t bin; + + loader_wjwwood_serial_config_t config; + config.portName = argc[1]; + //config.portName = "COM4"; + config.baudrate = 115200; + config.timeout = 600; + + if (loader_port_wjwwood_serial_init((const loader_wjwwood_serial_config_t*)&config) != ESP_LOADER_SUCCESS) { + printf("Serial initialization failed.\n"); + return -1; + } + + printf("Connecting...\n"); + if (connect_to_target(HIGHER_BAUDRATE) == ESP_LOADER_SUCCESS) { + + get_example_binaries(esp_loader_get_target(), &bin); + + printf("Loading bootloader...\n"); + flash_binary(bin.boot.data, bin.boot.size, bin.boot.addr); + printf("Loading partition table...\n"); + flash_binary(bin.part.data, bin.part.size, bin.part.addr); + printf("Loading app...\n"); + flash_binary(bin.app.data, bin.app.size, bin.app.addr); + printf("Done!\n"); + loader_port_wjwwood_serial_deinit(); + } else { + printf("Connect failed\n"); + loader_port_wjwwood_serial_deinit(); + return -1; + } + + return 0; +} diff --git a/port/wjwwood_serial_port.cpp b/port/wjwwood_serial_port.cpp new file mode 100644 index 0000000..e1c3cf2 --- /dev/null +++ b/port/wjwwood_serial_port.cpp @@ -0,0 +1,226 @@ +/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include "serial/serial.h" + +#include "wjwwood_serial_port.h" + +using std::chrono::high_resolution_clock; +using std::chrono::duration_cast; +using std::chrono::duration; +using std::chrono::milliseconds; +using namespace std::chrono_literals; + +loader_wjwwood_serial_config_t serial_config; +std::unique_ptr serial_port; +std::chrono::steady_clock::time_point serial_timer; + +// During the connection process a small delay is needed between write operations. +static bool write_delay = true; + +#ifdef SERIAL_FLASHER_DEBUG_TRACE +static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write) +{ + static bool write_prev = false; + + if (write_prev != write) { + write_prev = write; + printf("\n--- %s ---\n", write ? "WRITE" : "READ"); + } + + for (uint32_t i = 0; i < size; i++) { + printf("%02x ", data[i]); + } +} +#endif + +void setTimeout(uint32_t timeout) +{ + if(timeout == serial_config.timeout) + return; + + if(!serial_port || !serial_port->isOpen()) + { + loader_port_debug_print("Port not open\n"); + return; + } + + serial_port->setTimeout(serial::Timeout::simpleTimeout(timeout)); + serial_config.timeout = timeout; +} + +esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout) +{ + if(!serial_port || !serial_port->isOpen()) + { + loader_port_debug_print("loader_port_write: Port not open\n"); + return ESP_LOADER_ERROR_FAIL; + } + + try { + setTimeout(timeout); + size_t result = serial_port->write(data, size); + if (result != size) { + loader_port_debug_print("Could not write requested data\n"); + return ESP_LOADER_ERROR_TIMEOUT; + } + } catch (std::exception &e){ + loader_port_debug_print(e.what()); + return ESP_LOADER_ERROR_FAIL; + } + + #ifdef SERIAL_FLASHER_DEBUG_TRACE + transfer_debug_print(data, size, true); + #endif + + if(write_delay) + std::this_thread::sleep_for(std::chrono::microseconds(200)); + + return ESP_LOADER_SUCCESS; +} + + +esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout) +{ + if(!serial_port || !serial_port->isOpen()) + { + loader_port_debug_print("loader_port_read: Port not open\n"); + return ESP_LOADER_ERROR_FAIL; + } + + try { + setTimeout(timeout); + size_t result = serial_port->read(data, size); + if (result != size) { + loader_port_debug_print("Could not read requested data\n"); + return ESP_LOADER_ERROR_TIMEOUT; + } + } catch (std::exception &e){ + loader_port_debug_print(e.what()); + return ESP_LOADER_ERROR_FAIL; + } + + #ifdef SERIAL_FLASHER_DEBUG_TRACE + transfer_debug_print(data, size, false); + #endif + + return ESP_LOADER_SUCCESS; +} + +esp_loader_error_t loader_port_wjwwood_serial_init(const loader_wjwwood_serial_config_t *config) +{ + serial_config = *config; + + try { + serial_port = std::make_unique(serial_config.portName, config->baudrate, serial::Timeout::simpleTimeout(config->timeout)); + serial_port->setRTS(false); + serial_port->setDTR(false); + serial_port->setFlowcontrol(serial::flowcontrol_hardware); + if ( serial_port->isOpen() == false ) { + loader_port_debug_print("Port open failed\n"); + return ESP_LOADER_ERROR_FAIL; + } + } catch (std::exception &e){ + loader_port_debug_print(e.what()); + return ESP_LOADER_ERROR_FAIL; + } + + return ESP_LOADER_SUCCESS; +} + +esp_loader_error_t loader_port_wjwwood_serial_deinit() +{ + if(!serial_port || !serial_port->isOpen()) + return ESP_LOADER_ERROR_FAIL; + + try { + serial_port->close(); + serial_port.reset(); + serial_config = {}; + } catch (std::exception &e){ + loader_port_debug_print(e.what()); + return ESP_LOADER_ERROR_FAIL; + } + + return ESP_LOADER_SUCCESS; +} + +void loader_port_enter_bootloader(void) +{ + loader_port_reset_target(); +} + +void loader_port_reset_target(void) +{ + if(!serial_port || !serial_port->isOpen()) + { + loader_port_debug_print("loader_port_reset_target: Port not open\n"); + return; + } + + // This might be the best we can do without GPIO + serial_port->setDTR(true); + serial_port->setRTS(true); + loader_port_delay_ms(50); + serial_port->setDTR(false); + serial_port->setRTS(false); +} + +void loader_port_delay_ms(uint32_t ms) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(ms)); +} + +void loader_port_start_timer(uint32_t ms) +{ + serial_timer = high_resolution_clock::now() + (ms * 1ms); +} + +uint32_t loader_port_remaining_time(void) +{ + auto time_now = high_resolution_clock::now(); + int32_t remaining = (duration_cast(serial_timer - time_now)).count(); + return (remaining > 0) ? (uint32_t)remaining : 0; +} + +void loader_port_debug_print(const char *str) +{ + printf("DEBUG: %s", str); +} + +esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate) +{ + if(!serial_port || !serial_port->isOpen()) + { + loader_port_debug_print("loader_port_change_transmission_rate: Port not open\n"); + return ESP_LOADER_ERROR_FAIL; + } + + try { + serial_port->setBaudrate(baudrate); + write_delay = false; + } catch (std::exception &e){ + loader_port_debug_print(e.what()); + return ESP_LOADER_ERROR_FAIL; + } + + return ESP_LOADER_SUCCESS; +} diff --git a/port/wjwwood_serial_port.h b/port/wjwwood_serial_port.h new file mode 100644 index 0000000..cc2183e --- /dev/null +++ b/port/wjwwood_serial_port.h @@ -0,0 +1,36 @@ +/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include "esp_loader_io.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + char* portName; + int baudrate; + int timeout; +} loader_wjwwood_serial_config_t; + +esp_loader_error_t loader_port_wjwwood_serial_deinit(); +esp_loader_error_t loader_port_wjwwood_serial_init(const loader_wjwwood_serial_config_t *config); + +#ifdef __cplusplus +} +#endif diff --git a/private_include/protocol.h b/private_include/protocol.h index 1d0701b..0bbe3df 100644 --- a/private_include/protocol.h +++ b/private_include/protocol.h @@ -31,7 +31,8 @@ extern "C" { #define MD5_SIZE 32 -typedef enum __attribute__((packed)) +__pragma( pack(push, 1) ) +typedef enum { FLASH_BEGIN = 0x02, FLASH_DATA = 0x03, @@ -52,7 +53,7 @@ typedef enum __attribute__((packed)) SPI_FLASH_MD5 = 0x13, } command_t; -typedef enum __attribute__((packed)) +typedef enum { RESPONSE_OK = 0x00, INVALID_COMMAND = 0x05, // parameters or length field is invalid @@ -64,7 +65,7 @@ typedef enum __attribute__((packed)) DEFLATE_ERROR = 0x0b, // ESP32 compressed uploads only } error_code_t; -typedef struct __attribute__((packed)) +typedef struct { uint8_t direction; uint8_t command; // One of command_t @@ -72,7 +73,7 @@ typedef struct __attribute__((packed)) uint32_t checksum; } command_common_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t erase_size; @@ -82,7 +83,7 @@ typedef struct __attribute__((packed)) uint32_t encrypted; } flash_begin_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t data_size; @@ -91,13 +92,13 @@ typedef struct __attribute__((packed)) uint32_t zero_1; } data_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t stay_in_loader; } flash_end_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t total_size; @@ -106,20 +107,20 @@ typedef struct __attribute__((packed)) uint32_t offset; } mem_begin_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t stay_in_loader; uint32_t entry_point_address; } mem_end_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint8_t sync_sequence[36]; } sync_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t address; @@ -128,27 +129,27 @@ typedef struct __attribute__((packed)) uint32_t delay_us; } write_reg_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t address; } read_reg_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t configuration; uint32_t zero; // ESP32 ROM only } spi_attach_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t new_baudrate; uint32_t old_baudrate; } change_baudrate_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t address; @@ -157,7 +158,7 @@ typedef struct __attribute__((packed)) uint32_t reserved_1; } spi_flash_md5_command_t; -typedef struct __attribute__((packed)) +typedef struct { uint8_t direction; uint8_t command; // One of command_t @@ -165,26 +166,26 @@ typedef struct __attribute__((packed)) uint32_t value; } common_response_t; -typedef struct __attribute__((packed)) +typedef struct { uint8_t failed; uint8_t error; } response_status_t; -typedef struct __attribute__((packed)) +typedef struct { common_response_t common; response_status_t status; } response_t; -typedef struct __attribute__((packed)) +typedef struct { common_response_t common; uint8_t md5[MD5_SIZE]; // ROM only response_status_t status; } rom_md5_response_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t id; @@ -195,6 +196,8 @@ typedef struct __attribute__((packed)) uint32_t status_mask; } write_spi_command_t; +__pragma( pack(pop)) + esp_loader_error_t loader_initialize_conn(esp_loader_connect_args_t *connect_args); #if (defined SERIAL_FLASHER_INTERFACE_UART) || (defined SERIAL_FLASHER_INTERFACE_USB) diff --git a/src/protocol_common.c b/src/protocol_common.c index c925330..61dade0 100644 --- a/src/protocol_common.c +++ b/src/protocol_common.c @@ -295,7 +295,9 @@ esp_loader_error_t loader_spi_parameters(uint32_t total_size) return send_cmd(&spi_cmd, sizeof(spi_cmd), NULL); } +#ifndef _MSC_VER __attribute__ ((weak)) void loader_port_debug_print(const char *str) { (void) str; } +#endif diff --git a/submodules/FindWjwwoodSerial.cmake b/submodules/FindWjwwoodSerial.cmake new file mode 100644 index 0000000..eadb5fd --- /dev/null +++ b/submodules/FindWjwwoodSerial.cmake @@ -0,0 +1,35 @@ +SET(DEPENDENCY_NAME "WjwwoodSerial") +SET(GIT_URL "https://github.com/wjwwood/serial") + +if(NOT EXISTS "${CMAKE_BINARY_DIR}/${DEPENDENCY_NAME}") + # Clone the repository if it's not already present + message(STATUS "Cloning ${DEPENDENCY_NAME} from ${GIT_URL}") + execute_process(COMMAND git clone ${GIT_URL} ${CMAKE_BINARY_DIR}/${DEPENDENCY_NAME}) +else() + message(STATUS "${DEPENDENCY_NAME} already cloned") +endif() + +SET(serial_path ${CMAKE_BINARY_DIR}/${DEPENDENCY_NAME}) +SET(serial_SRCS ${serial_path}/src/serial.cc) + +if(APPLE) + # If OSX + list(APPEND serial_SRCS ${serial_path}/src/impl/unix.cc) + list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_osx.cc) +elseif(UNIX) + # If unix + list(APPEND serial_SRCS ${serial_path}/src/impl/unix.cc) + list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_linux.cc) +else() + # If windows + list(APPEND serial_SRCS ${serial_path}/src/impl/win.cc) + list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_win.cc) +endif() + +add_library(wjwwood_serial + ${serial_SRCS} +) + +target_include_directories(wjwwood_serial PUBLIC + ${serial_path}/include +) diff --git a/submodules/serial b/submodules/serial new file mode 160000 index 0000000..69e0372 --- /dev/null +++ b/submodules/serial @@ -0,0 +1 @@ +Subproject commit 69e0372cf0d3796e84ce9a09aff1d74496f68720