Skip to content

Commit a696e22

Browse files
committed
Merge branch 'feature/calc_checksum_from_flash' into 'master'
feat: calc checksum from flash See merge request sdk/ESP8266_RTOS_SDK!382
2 parents 00e0ca0 + 269f646 commit a696e22

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

components/bootloader_support/src/esp_image_format.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ static void debug_log_hash(const uint8_t *image_hash, const char *label)
586586
#ifdef CONFIG_TARGET_PLATFORM_ESP8266
587587

588588
#include <string.h>
589+
#include <stdlib.h>
589590
#include <sys/param.h>
590591

591592
#include <esp_image_format.h>
@@ -600,6 +601,8 @@ static const char *TAG = "esp_image";
600601

601602
#define HASH_LEN 32 /* SHA-256 digest length */
602603

604+
#define MAX_CHECKSUM_READ_SIZE SPI_FLASH_SEC_SIZE
605+
603606
#define SIXTEEN_MB 0x1000000
604607
#define ESP_ROM_CHECKSUM_INITIAL 0xEF
605608

@@ -923,6 +926,7 @@ static esp_err_t process_segment(int index, uint32_t flash_addr, esp_image_segme
923926

924927
static esp_err_t process_segment_data(intptr_t load_addr, uint32_t data_addr, uint32_t data_len, bool do_load, bootloader_sha256_handle_t sha_handle, uint32_t *checksum)
925928
{
929+
#ifdef BOOTLOADER_BUILD
926930
const uint32_t *data = (const uint32_t *)bootloader_mmap(data_addr, data_len);
927931
if(!data) {
928932
ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed",
@@ -965,6 +969,55 @@ static esp_err_t process_segment_data(intptr_t load_addr, uint32_t data_addr, ui
965969
bootloader_munmap(data);
966970

967971
return ESP_OK;
972+
#endif
973+
974+
#ifndef BOOTLOADER_BUILD
975+
uint32_t had_read_size = 0, to_read_size = 0;
976+
uint32_t* data = 0;
977+
978+
data = (uint32_t*)malloc(MAX_CHECKSUM_READ_SIZE);
979+
if(data == NULL) {
980+
return ESP_FAIL;
981+
}
982+
983+
const uint32_t *src = data;
984+
for (; had_read_size != data_len; ) {
985+
to_read_size = ((data_len - had_read_size) < MAX_CHECKSUM_READ_SIZE) ? (data_len - had_read_size) : MAX_CHECKSUM_READ_SIZE;
986+
int ret = ESP_OK;
987+
ret = spi_flash_read(data_addr + had_read_size, data, to_read_size);
988+
if (ret) {
989+
ESP_LOGE(TAG, "SPI flash read result %d\n", ret);
990+
free(data);
991+
return ESP_FAIL;
992+
}
993+
994+
had_read_size += to_read_size;
995+
996+
for (int i = 0; i < to_read_size; i += 4) {
997+
int w_i = i/4; // Word index
998+
uint32_t w = src[w_i];
999+
*checksum ^= w;
1000+
#ifdef BOOTLOADER_BUILD
1001+
if (do_load) {
1002+
// dest[w_i] = w ^ ((w_i & 1) ? ram_obfs_value[0] : ram_obfs_value[1]);
1003+
}
1004+
#endif
1005+
// SHA_CHUNK determined experimentally as the optimum size
1006+
// to call bootloader_sha256_data() with. This is a bit
1007+
// counter-intuitive, but it's ~3ms better than using the
1008+
// SHA256 block size.
1009+
const size_t SHA_CHUNK = 1024;
1010+
if (sha_handle != NULL && i % SHA_CHUNK == 0) {
1011+
// bootloader_sha256_data(sha_handle, &src[w_i],
1012+
// MIN(SHA_CHUNK, data_len - i));
1013+
}
1014+
}
1015+
1016+
1017+
} // end for
1018+
free(data);
1019+
return ESP_OK;
1020+
#endif
9681021
}
9691022

9701023
static esp_err_t verify_segment_header(int index, const esp_image_segment_header_t *segment, uint32_t segment_data_offs, bool silent)

0 commit comments

Comments
 (0)