Skip to content

Commit fcabe4e

Browse files
authored
Merge pull request #41 from espressif/fix/various_fixes
Fix: Various fixes
2 parents e0b8c67 + 9e591da commit fcabe4e

File tree

5 files changed

+38
-26
lines changed

5 files changed

+38
-26
lines changed

src/command_handler.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ struct flash_operation_state {
4343
uint32_t erase_remaining;
4444
};
4545

46-
#define ADLER32_CHECKSUM_SIZE 4
47-
4846
#define DIRECTION_REQUEST 0x00
4947
#define DIRECTION_RESPONSE 0x01
5048

@@ -358,9 +356,8 @@ static void s_spi_attach(const uint8_t *buffer, uint16_t size)
358356
}
359357
const uint32_t *params = (const uint32_t *)buffer;
360358
uint32_t ishspi = params[0];
361-
bool legacy = params[1];
362359

363-
stub_lib_flash_attach(ishspi, legacy);
360+
stub_lib_flash_attach(ishspi, 0);
364361
s_send_success_response(ESP_SPI_ATTACH, 0, NULL, 0);
365362
}
366363

@@ -412,7 +409,7 @@ static void s_flash_defl_begin(const uint8_t *buffer, uint16_t size)
412409
}
413410

414411
const uint32_t *params = (const uint32_t *)buffer;
415-
s_flash_state.total_remaining = params[0] + ADLER32_CHECKSUM_SIZE;
412+
s_flash_state.total_remaining = params[0];
416413
s_flash_state.num_blocks = params[1];
417414
s_flash_state.block_size = params[2];
418415
s_flash_state.offset = params[3];
@@ -444,6 +441,8 @@ static void s_flash_defl_begin(const uint8_t *buffer, uint16_t size)
444441

445442
static void s_flash_defl_data(const uint8_t *buffer, uint16_t size, uint32_t packet_checksum)
446443
{
444+
#define ADLER32_CHECKSUM_SIZE 4
445+
447446
if (size < FLASH_DEFL_DATA_HEADER_SIZE) {
448447
s_send_error_response(ESP_FLASH_DEFL_DATA, RESPONSE_BAD_DATA_LEN);
449448
return;
@@ -454,6 +453,13 @@ static void s_flash_defl_data(const uint8_t *buffer, uint16_t size, uint32_t pac
454453
return;
455454
}
456455

456+
// If all expected data has already been decompressed and written,
457+
// only accept the checksum if it is part of the data.
458+
if (s_flash_state.total_remaining == 0 && size > ADLER32_CHECKSUM_SIZE) {
459+
s_send_error_response(ESP_FLASH_DEFL_DATA, RESPONSE_TOO_MUCH_DATA);
460+
return;
461+
}
462+
457463
// When TINFL_LZ_DICT_SIZE buffer size is used, wrapping is handled by miniz library.
458464
static uint8_t decompressed_data[TINFL_LZ_DICT_SIZE];
459465
static uint8_t *decompressed_data_ptr = decompressed_data;
@@ -521,11 +527,12 @@ static void s_flash_defl_data(const uint8_t *buffer, uint16_t size, uint32_t pac
521527
s_send_error_response(ESP_FLASH_DEFL_DATA, RESPONSE_FAILED_SPI_OP);
522528
return;
523529
}
524-
s_flash_state.offset += (uint16_t)(decompressed_data_ptr - decompressed_data);
525-
s_flash_state.total_remaining -= (uint16_t)(decompressed_data_ptr - decompressed_data);
530+
s_flash_state.offset += write_data_size;
531+
s_flash_state.total_remaining -= write_data_size;
526532
decompressed_data_ptr = decompressed_data;
527533
}
528534
}
535+
#undef ADLER32_CHECKSUM_SIZE
529536
}
530537

531538
static void s_flash_defl_end(const uint8_t *buffer, uint16_t size)
@@ -540,7 +547,7 @@ static void s_flash_defl_end(const uint8_t *buffer, uint16_t size)
540547
return;
541548
}
542549

543-
if (s_flash_state.total_remaining != ADLER32_CHECKSUM_SIZE) {
550+
if (s_flash_state.total_remaining != 0) {
544551
s_send_error_response(ESP_FLASH_DEFL_END, RESPONSE_BAD_DATA_LEN);
545552
return;
546553
}
@@ -651,9 +658,8 @@ static void s_read_flash(const uint8_t *buffer, uint16_t size)
651658
uint32_t packet_size = params[2];
652659
// Packet contains max in-flight packets, which esptool and other tools set to 64 or higher,
653660
// but old stub interpreted this always as 1 due to a bug. When interpreted correctly, esptool
654-
// cannot handle the data flow due to the implementation. Setting it to 2 to avoid possible issues,
655-
// while still slightly benefitting from the increased throughput.
656-
uint32_t max_unacked_packets = 2;
661+
// cannot handle the data flow due to the implementation. Setting it to 1 to avoid possible issues.
662+
uint32_t max_unacked_packets = 1;
657663

658664
uint8_t data[4102] __attribute__((aligned(4)));
659665
uint32_t read_size_remaining = read_size;
@@ -725,6 +731,9 @@ static void s_erase_flash(void)
725731

726732
static void s_erase_region(const uint8_t *buffer, uint16_t size)
727733
{
734+
// Timeout values for flash operations, inspired by esptool
735+
#define ERASE_PER_SECTOR_TIMEOUT_US 120000U
736+
728737
if (size != ERASE_REGION_SIZE) {
729738
s_send_error_response(ESP_ERASE_REGION, RESPONSE_BAD_DATA_LEN);
730739
return;
@@ -734,16 +743,24 @@ static void s_erase_region(const uint8_t *buffer, uint16_t size)
734743
uint32_t addr = params[0];
735744
uint32_t erase_size = params[1];
736745

746+
uint64_t timeout_us = (erase_size + STUB_FLASH_SECTOR_SIZE - 1) / STUB_FLASH_SECTOR_SIZE * ERASE_PER_SECTOR_TIMEOUT_US;
747+
737748
if (addr % STUB_FLASH_SECTOR_SIZE || erase_size % STUB_FLASH_SECTOR_SIZE) {
738749
s_send_error_response(ESP_ERASE_REGION, RESPONSE_BAD_DATA_LEN);
739750
return;
740751
}
741752

742-
while (erase_size > 0) {
753+
while (erase_size > 0 && timeout_us > 0) {
743754
stub_lib_flash_start_next_erase(&addr, &erase_size);
755+
stub_lib_delay_us(1);
756+
--timeout_us;
757+
}
758+
if (stub_lib_flash_wait_ready(timeout_us) != STUB_LIB_OK) {
759+
s_send_error_response(ESP_ERASE_REGION, RESPONSE_FAILED_SPI_OP);
760+
return;
744761
}
745-
746762
s_send_success_response(ESP_ERASE_REGION, 0, NULL, 0);
763+
#undef ERASE_PER_SECTOR_TIMEOUT_US
747764
}
748765

749766
static void s_send_response_packet(uint8_t command, uint32_t value, uint8_t *data, uint16_t data_size,

src/ld/esp32.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
of available RAM, to reduce change of colliding with anything
33
else... */
44
MEMORY {
5-
iram : org = 0x400BE000, len = 0x1500
5+
iram : org = 0x400A8000, len = 0x2000
66
dram : org = 0x3ffcc000, len = 0x14000
77
}
88

src/slip.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ typedef enum {
2626

2727
typedef struct {
2828
uint8_t buffer[MAX_COMMAND_SIZE] __attribute__((aligned(4)));
29-
size_t frame_length;
30-
bool frame_complete;
31-
bool frame_error;
29+
volatile size_t frame_length;
30+
volatile bool frame_complete;
31+
volatile bool frame_error;
3232
} slip_buffer_t;
3333

3434
static slip_buffer_t s_buffers[SLIP_NUM_BUFFERS];
35-
static uint8_t s_receiving_buffer = 0;
36-
static uint8_t s_processing_buffer = 0;
37-
static slip_state_t s_state = STATE_NO_FRAME;
35+
static volatile uint8_t s_receiving_buffer = 0;
36+
static volatile uint8_t s_processing_buffer = 0;
37+
static volatile slip_state_t s_state = STATE_NO_FRAME;
3838

3939
/* TX function pointer set by transport init (defaults to UART) */
4040
static uint8_t (*s_tx_one_char)(uint8_t) = stub_lib_uart_tx_one_char;

src/transport.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ void uart_rx_interrupt_handler()
2727
for (uint32_t i = 0; i < count; ++i) {
2828
uint8_t byte = stub_lib_uart_read_rxfifo_byte(UART_NUM_0);
2929
slip_recv_byte(byte);
30-
31-
// Cannot process more bytes until frame is processed
32-
if (slip_is_frame_complete() || slip_is_frame_error()) {
33-
break;
34-
}
3530
}
3631
}
3732
}

0 commit comments

Comments
 (0)