Skip to content

Commit

Permalink
add --no-stub option
Browse files Browse the repository at this point in the history
  • Loading branch information
higaski committed Mar 27, 2024
1 parent f954955 commit 61deebf
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmake/gen_flasher_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"#pragma once\n"
"\n"
"#include <stdint.h>\n"
"#include <stdbool.h>\n"
'#include "esp_loader.h"\n'
"\n"
"#ifdef __cplusplus\n"
Expand All @@ -53,6 +54,7 @@
"} esp_stub_t;\n"
"\n"
"#if STUB_ENABLED\n"
"extern bool esp_no_stub;\n"
"extern const esp_stub_t esp_stub[ESP_MAX_CHIP];\n"
"#endif\n"
"\n"
Expand Down Expand Up @@ -84,6 +86,8 @@
+ ', "Stub order matches target_chip_t enumeration");\n'
"#endif\n"
"\n"
"bool esp_no_stub = false;\n"
"\n"
"const esp_stub_t esp_stub[ESP_MAX_CHIP] = {\n"
"\n"
)
Expand Down
15 changes: 15 additions & 0 deletions include/esp_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,21 @@ esp_loader_error_t esp_loader_change_transmission_rate(uint32_t transmission_rat
#if MD5_ENABLED
esp_loader_error_t esp_loader_flash_verify(void);
#endif

/**
* @brief Disable launching the flasher stub, only talk to ROM bootloader.
*
* @note This function is only available if STUB_ENABLED is set.
*
* @param no_stub[in] Disable launching the flasher stub if true.
*
* @return
* - ESP_LOADER_SUCCESS Success
*/
#if STUB_ENABLED
esp_loader_error_t esp_loader_no_stub(bool no_stub);
#endif

/**
* @brief Toggles reset pin.
*/
Expand Down
2 changes: 2 additions & 0 deletions private_include/esp_stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once

#include <stdint.h>
#include <stdbool.h>
#include "esp_loader.h"

#ifdef __cplusplus
Expand All @@ -15,6 +16,7 @@ typedef struct {
} esp_stub_t;

#if STUB_ENABLED
extern bool esp_no_stub;
extern const esp_stub_t esp_stub[ESP_MAX_CHIP];
#endif

Expand Down
13 changes: 12 additions & 1 deletion src/esp_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "protocol.h"
#include "esp_loader_io.h"
#include "esp_loader.h"
#include "esp_stubs.h"
#include "esp_targets.h"
#include "md5_hash.h"
#include <string.h>
Expand Down Expand Up @@ -74,7 +75,9 @@ esp_loader_error_t esp_loader_connect(esp_loader_connect_args_t *connect_args)
RETURN_ON_ERROR(loader_detect_chip(&s_target, &s_reg));

#if STUB_ENABLED
RETURN_ON_ERROR(loader_run_stub(s_target));
if (!esp_no_stub) {
RETURN_ON_ERROR(loader_run_stub(s_target));
}
#endif

#if (defined SERIAL_FLASHER_INTERFACE_UART) || (defined SERIAL_FLASHER_INTERFACE_USB)
Expand Down Expand Up @@ -456,6 +459,14 @@ esp_loader_error_t esp_loader_flash_verify(void)

#endif

#if STUB_ENABLED

esp_loader_error_t esp_loader_no_stub(bool no_stub) {
return loader_no_stub(no_stub);
}

#endif

void esp_loader_reset_target(void)
{
loader_port_reset_target();
Expand Down
2 changes: 2 additions & 0 deletions src/esp_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ _Static_assert(ESP32C6_CHIP == 8, "Stub order matches target_chip_t enumeration"
_Static_assert(ESP_MAX_CHIP == 9, "Stub order matches target_chip_t enumeration");
#endif

bool esp_no_stub = false;

const esp_stub_t esp_stub[ESP_MAX_CHIP] = {

// stub_flasher_8266.json
Expand Down
21 changes: 20 additions & 1 deletion src/protocol_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ esp_loader_error_t loader_flash_begin_cmd(uint32_t offset,
uint32_t blocks_to_write,
bool encryption)
{
// stub does not support encryption
#if STUB_ENABLED
if (!esp_no_stub) {
encryption = false;
}
#endif

flash_begin_command_t flash_begin_cmd = {
.common = {
.direction = WRITE_DIRECTION,
Expand Down Expand Up @@ -240,7 +247,14 @@ esp_loader_error_t loader_spi_attach_cmd(uint32_t config)
.zero = 0
};

return send_cmd(&attach_cmd, sizeof(attach_cmd), NULL);
// On ESP32 ROM loader only, there is an additional 4 bytes in the data payload of this command.
#if STUB_ENABLED
const uint32_t attach_cmd_size = esp_no_stub ? sizeof(attach_cmd) : sizeof(attach_cmd) - sizeof(attach_cmd.zero);
#else
const uint32_t attach_cmd_size = sizeof(attach_cmd);
#endif

return send_cmd(&attach_cmd, attach_cmd_size, NULL);
}

esp_loader_error_t loader_change_baudrate_cmd(uint32_t baudrate)
Expand Down Expand Up @@ -300,11 +314,16 @@ esp_loader_error_t loader_spi_parameters(uint32_t total_size)
#if STUB_ENABLED

esp_loader_error_t loader_no_stub(bool no_stub) {
esp_no_stub = no_stub;
return ESP_LOADER_SUCCESS;
}

esp_loader_error_t loader_run_stub(target_chip_t target)
{
if (esp_no_stub) {
return ESP_LOADER_ERROR_FAIL;
}

esp_loader_error_t err;
const esp_stub_t* stub = &esp_stub[target];

Expand Down

0 comments on commit 61deebf

Please sign in to comment.