|
@@ -1,4 +1,4 @@
|
|
|
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
|
|
|
+/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
|
|
*
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -13,9 +13,9 @@
|
|
|
* limitations under the License.
|
|
* limitations under the License.
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
-#include "serial_comm_prv.h"
|
|
|
|
|
-#include "serial_comm.h"
|
|
|
|
|
-#include "serial_io.h"
|
|
|
|
|
|
|
+#include "protocol.h"
|
|
|
|
|
+#include "esp_loader_io.h"
|
|
|
|
|
+#include "slip.h"
|
|
|
#include <stddef.h>
|
|
#include <stddef.h>
|
|
|
#include <string.h>
|
|
#include <string.h>
|
|
|
|
|
|
|
@@ -23,23 +23,8 @@
|
|
|
|
|
|
|
|
static uint32_t s_sequence_number = 0;
|
|
static uint32_t s_sequence_number = 0;
|
|
|
|
|
|
|
|
-static const uint8_t DELIMITER = 0xC0;
|
|
|
|
|
-static const uint8_t C0_REPLACEMENT[2] = {0xDB, 0xDC};
|
|
|
|
|
-static const uint8_t DB_REPLACEMENT[2] = {0xDB, 0xDD};
|
|
|
|
|
-
|
|
|
|
|
static esp_loader_error_t check_response(command_t cmd, uint32_t *reg_value, void* resp, uint32_t resp_size);
|
|
static esp_loader_error_t check_response(command_t cmd, uint32_t *reg_value, void* resp, uint32_t resp_size);
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-static inline esp_loader_error_t serial_read(uint8_t *buff, size_t size)
|
|
|
|
|
-{
|
|
|
|
|
- return loader_port_serial_read(buff, size, loader_port_remaining_time());
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-static inline esp_loader_error_t serial_write(const uint8_t *buff, size_t size)
|
|
|
|
|
-{
|
|
|
|
|
- return loader_port_serial_write(buff, size, loader_port_remaining_time());
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
static uint8_t compute_checksum(const uint8_t *data, uint32_t size)
|
|
static uint8_t compute_checksum(const uint8_t *data, uint32_t size)
|
|
|
{
|
|
{
|
|
|
uint8_t checksum = 0xEF;
|
|
uint8_t checksum = 0xEF;
|
|
@@ -51,101 +36,6 @@ static uint8_t compute_checksum(const uint8_t *data, uint32_t size)
|
|
|
return checksum;
|
|
return checksum;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static esp_loader_error_t SLIP_receive_data(uint8_t *buff, uint32_t size)
|
|
|
|
|
-{
|
|
|
|
|
- uint8_t ch;
|
|
|
|
|
-
|
|
|
|
|
- for (uint32_t i = 0; i < size; i++) {
|
|
|
|
|
- RETURN_ON_ERROR( serial_read(&ch, 1) );
|
|
|
|
|
-
|
|
|
|
|
- if (ch == 0xDB) {
|
|
|
|
|
- RETURN_ON_ERROR( serial_read(&ch, 1) );
|
|
|
|
|
- if (ch == 0xDC) {
|
|
|
|
|
- buff[i] = 0xC0;
|
|
|
|
|
- } else if (ch == 0xDD) {
|
|
|
|
|
- buff[i] = 0xDB;
|
|
|
|
|
- } else {
|
|
|
|
|
- return ESP_LOADER_ERROR_INVALID_RESPONSE;
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- buff[i] = ch;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return ESP_LOADER_SUCCESS;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-static esp_loader_error_t SLIP_receive_packet(uint8_t *buff, uint32_t size)
|
|
|
|
|
-{
|
|
|
|
|
- uint8_t ch;
|
|
|
|
|
-
|
|
|
|
|
- // Wait for delimiter
|
|
|
|
|
- do {
|
|
|
|
|
- RETURN_ON_ERROR( serial_read(&ch, 1) );
|
|
|
|
|
- } while (ch != DELIMITER);
|
|
|
|
|
-
|
|
|
|
|
- // Workaround: bootloader sends two dummy(0xC0) bytes after response when baud rate is changed.
|
|
|
|
|
- do {
|
|
|
|
|
- RETURN_ON_ERROR( serial_read(&ch, 1) );
|
|
|
|
|
- } while (ch == DELIMITER);
|
|
|
|
|
-
|
|
|
|
|
- buff[0] = ch;
|
|
|
|
|
-
|
|
|
|
|
- RETURN_ON_ERROR( SLIP_receive_data(&buff[1], size - 1) );
|
|
|
|
|
-
|
|
|
|
|
- // Wait for delimiter
|
|
|
|
|
- do {
|
|
|
|
|
- RETURN_ON_ERROR( serial_read(&ch, 1) );
|
|
|
|
|
- } while (ch != DELIMITER);
|
|
|
|
|
-
|
|
|
|
|
- return ESP_LOADER_SUCCESS;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-static esp_loader_error_t SLIP_send(const uint8_t *data, uint32_t size)
|
|
|
|
|
-{
|
|
|
|
|
- uint32_t to_write = 0; // Bytes ready to write as they are
|
|
|
|
|
- uint32_t written = 0; // Bytes already written
|
|
|
|
|
-
|
|
|
|
|
- for (uint32_t i = 0; i < size; i++) {
|
|
|
|
|
- if (data[i] != 0xC0 && data[i] != 0xDB) {
|
|
|
|
|
- to_write++; // Queue this byte for writing
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // We have a byte that needs encoding, write the queue first
|
|
|
|
|
- if (to_write > 0) {
|
|
|
|
|
- RETURN_ON_ERROR( serial_write(&data[written], to_write) );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Write the encoded byte
|
|
|
|
|
- if (data[i] == 0xC0) {
|
|
|
|
|
- RETURN_ON_ERROR( serial_write(C0_REPLACEMENT, 2) );
|
|
|
|
|
- } else {
|
|
|
|
|
- RETURN_ON_ERROR( serial_write(DB_REPLACEMENT, 2) );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Update to start again after the encoded byte
|
|
|
|
|
- written = i + 1;
|
|
|
|
|
- to_write = 0;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Write the rest of the bytes that didn't need encoding
|
|
|
|
|
- if (to_write > 0) {
|
|
|
|
|
- RETURN_ON_ERROR( serial_write(&data[written], to_write) );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return ESP_LOADER_SUCCESS;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-static esp_loader_error_t SLIP_send_delimiter(void)
|
|
|
|
|
-{
|
|
|
|
|
- return serial_write(&DELIMITER, 1);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
static esp_loader_error_t send_cmd(const void *cmd_data, uint32_t size, uint32_t *reg_value)
|
|
static esp_loader_error_t send_cmd(const void *cmd_data, uint32_t size, uint32_t *reg_value)
|
|
|
{
|
|
{
|
|
|
response_t response;
|
|
response_t response;
|
|
@@ -205,7 +95,7 @@ static void log_loader_internal_error(error_code_t error)
|
|
|
case DEFLATE_ERROR: loader_port_debug_print("DEFLATE_ERROR"); break;
|
|
case DEFLATE_ERROR: loader_port_debug_print("DEFLATE_ERROR"); break;
|
|
|
default: loader_port_debug_print("UNKNOWN ERROR"); break;
|
|
default: loader_port_debug_print("UNKNOWN ERROR"); break;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
loader_port_debug_print("\n");
|
|
loader_port_debug_print("\n");
|
|
|
}
|
|
}
|
|
|
|
|
|