Skip to content

Commit d194ff1

Browse files
committed
Merge branch 'bugfix/fix_md5_check_with_stub' into 'master'
fix: Add correct MD5 calculation with stub enabled Closes ESF-144 See merge request espressif/esp-serial-flasher!112
2 parents 63dd024 + 88352d9 commit d194ff1

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

private_include/protocol.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@ extern "C" {
2929
#define READ_DIRECTION 1
3030
#define WRITE_DIRECTION 0
3131

32-
#define MD5_SIZE 32
32+
#define MD5_SIZE_ROM 32
33+
#define MD5_SIZE_STUB 16
3334

3435
// Maximum block sized for RAM and Flash writes, respectively.
3536
#define ESP_RAM_BLOCK 0x1800
3637

3738
#ifndef MAX
38-
#define MAX(a, b) ((a) > (b)) ? (a) : (b)
39+
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
3940
#endif
4041

4142
#ifndef MIN
42-
#define MIN(a, b) ((a) < (b)) ? (a) : (b)
43+
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
4344
#endif
4445

4546
#ifndef ROUNDUP
@@ -195,10 +196,17 @@ typedef struct __attribute__((packed))
195196
typedef struct __attribute__((packed))
196197
{
197198
common_response_t common;
198-
uint8_t md5[MD5_SIZE]; // ROM only
199+
uint8_t md5[MD5_SIZE_ROM];
199200
response_status_t status;
200201
} rom_md5_response_t;
201202

203+
typedef struct __attribute__((packed))
204+
{
205+
common_response_t common;
206+
uint8_t md5[MD5_SIZE_STUB];
207+
response_status_t status;
208+
} stub_md5_response_t;
209+
202210
typedef struct __attribute__((packed))
203211
{
204212
command_common_t common;

private_include/protocol_prv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ esp_loader_error_t send_cmd(const void *cmd_data, uint32_t size, uint32_t *reg_v
2828
esp_loader_error_t send_cmd_with_data(const void *cmd_data, size_t cmd_size,
2929
const void *data, size_t data_size);
3030

31-
esp_loader_error_t send_cmd_md5(const void *cmd_data, size_t cmd_size, uint8_t md5_out[MD5_SIZE]);
31+
esp_loader_error_t send_cmd_md5(const void *cmd_data, size_t cmd_size, uint8_t *md5_out);

src/esp_loader.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -462,37 +462,40 @@ static void hexify(const uint8_t raw_md5[16], uint8_t hex_md5_out[32])
462462
}
463463
}
464464

465-
466465
esp_loader_error_t esp_loader_flash_verify(void)
467466
{
468467
if (s_target == ESP8266_CHIP && !esp_stub_get_running()) {
469468
return ESP_LOADER_ERROR_UNSUPPORTED_FUNC;
470469
}
471470

472-
uint8_t raw_md5[16] = {0};
473-
474-
/* Zero termination and new line character require 2 bytes */
475-
uint8_t hex_md5[MD5_SIZE + 2] = {0};
476-
uint8_t received_md5[MD5_SIZE + 2] = {0};
471+
/* Zero termination require 1 byte */
472+
uint8_t received_md5[MAX(MD5_SIZE_ROM, MD5_SIZE_STUB) + 1] = {0};
473+
uint8_t calculated_md5[MAX(MD5_SIZE_ROM, MD5_SIZE_STUB) + 1] = {0};
477474

475+
uint8_t raw_md5[16] = {0};
478476
md5_final(raw_md5);
479-
hexify(raw_md5, hex_md5);
480477

481478
loader_port_start_timer(timeout_per_mb(s_image_size, MD5_TIMEOUT_PER_MB));
482479

483480
RETURN_ON_ERROR( loader_md5_cmd(s_start_address, s_image_size, received_md5) );
484481

485-
bool md5_match = memcmp(hex_md5, received_md5, MD5_SIZE) == 0;
482+
bool md5_match;
483+
if (esp_stub_get_running()) {
484+
md5_match = memcmp(raw_md5, received_md5, MD5_SIZE_STUB) == 0;
485+
memcpy(calculated_md5, raw_md5, MD5_SIZE_STUB);
486+
} else {
487+
hexify(raw_md5, calculated_md5);
488+
md5_match = memcmp(calculated_md5, received_md5, MD5_SIZE_ROM) == 0;
489+
}
486490

487491
if (!md5_match) {
488-
hex_md5[MD5_SIZE] = '\n';
489-
received_md5[MD5_SIZE] = '\n';
490-
491492
loader_port_debug_print("Error: MD5 checksum does not match:\n");
492493
loader_port_debug_print("Expected:\n");
493494
loader_port_debug_print((char *)received_md5);
495+
loader_port_debug_print("\n");
494496
loader_port_debug_print("Actual:\n");
495-
loader_port_debug_print((char *)hex_md5);
497+
loader_port_debug_print((char *)calculated_md5);
498+
loader_port_debug_print("\n");
496499

497500
return ESP_LOADER_ERROR_INVALID_MD5;
498501
}

src/protocol_uart.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,26 @@ esp_loader_error_t send_cmd_with_data(const void *cmd_data, size_t cmd_size,
125125
}
126126

127127

128-
esp_loader_error_t send_cmd_md5(const void *cmd_data, size_t cmd_size, uint8_t md5_out[MD5_SIZE])
128+
esp_loader_error_t send_cmd_md5(const void *cmd_data, size_t cmd_size, uint8_t *md5_out)
129129
{
130-
rom_md5_response_t response;
131130
command_t command = ((const command_common_t *)cmd_data)->command;
132131

133132
RETURN_ON_ERROR( SLIP_send_delimiter() );
134133
RETURN_ON_ERROR( SLIP_send((const uint8_t *)cmd_data, cmd_size) );
135134
RETURN_ON_ERROR( SLIP_send_delimiter() );
136135

137-
RETURN_ON_ERROR( check_response(command, NULL, &response, sizeof(response)) );
138-
139-
memcpy(md5_out, response.md5, MD5_SIZE);
140-
136+
if (esp_stub_get_running()) {
137+
stub_md5_response_t response;
138+
RETURN_ON_ERROR( check_response(command, NULL, &response, sizeof(response)) );
139+
memcpy(md5_out, response.md5, MD5_SIZE_STUB);
140+
} else {
141+
rom_md5_response_t response;
142+
RETURN_ON_ERROR( check_response(command, NULL, &response, sizeof(response)) );
143+
memcpy(md5_out, response.md5, MD5_SIZE_ROM);
144+
}
141145
return ESP_LOADER_SUCCESS;
142146
}
143147

144-
145148
static esp_loader_error_t check_response(command_t cmd, uint32_t *reg_value, void *resp, uint32_t resp_size)
146149
{
147150
esp_loader_error_t err;

0 commit comments

Comments
 (0)