From d200ef17705e888e6476cd9bbf0a1360f56f2518 Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Fri, 25 Jan 2019 11:45:24 +0800 Subject: [PATCH] fix(ota): Fix OTA copy buffer is not align Using globle macro "ROM_FLASH_BUF_DECLARE" to declare a block of buffer not "uint8_t". --- .../esp8266/include/esp8266/rom_functions.h | 2 ++ components/spi_flash/port/port.c | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/components/esp8266/include/esp8266/rom_functions.h b/components/esp8266/include/esp8266/rom_functions.h index 80d8a593..246622a8 100644 --- a/components/esp8266/include/esp8266/rom_functions.h +++ b/components/esp8266/include/esp8266/rom_functions.h @@ -4,6 +4,8 @@ #include #include +#define ROM_FLASH_BUF_DECLARE(__name, __size) uint8_t __name[__size] __attribute__((aligned(4))) + typedef struct esp_spi_flash_chip { uint32_t deviceId; uint32_t chip_size; // chip size in byte diff --git a/components/spi_flash/port/port.c b/components/spi_flash/port/port.c index 32e3ca3c..bb41d4b3 100644 --- a/components/spi_flash/port/port.c +++ b/components/spi_flash/port/port.c @@ -59,12 +59,12 @@ typedef union s_boot_param { uint8_t enhance_boot_flag : 1; } boot_base; - uint8_t data[4096]; + ROM_FLASH_BUF_DECLARE(__data, 32); } boot_param_t; static const char *TAG = "partition_port"; static uint32_t s_partition_offset; -static uint8_t s_cache_buf[SPI_FLASH_SEC_SIZE]; +static ROM_FLASH_BUF_DECLARE(s_cache_buf, SPI_FLASH_SEC_SIZE); static sys_param_t s_sys_param; static boot_param_t s_boot_param; static esp_spi_flash_chip_t s_flash_chip = { @@ -96,6 +96,11 @@ static inline int spi_flash_read_data(uint32_t addr, void *buf, size_t n) { int ret; + if (addr & 3 || (uint32_t)buf & 3 || n & 3) { + ESP_LOGE(TAG, "flash read parameters is not align, value is %p %x %x", buf, n ,addr); + return -1; + } + ESP_LOGD(TAG, "read buffer %p total %d from 0x%x", buf, n ,addr); ret = SPI_read_data(&s_flash_chip, addr, buf, n); @@ -107,6 +112,11 @@ static inline int spi_flash_write_data(uint32_t addr, const void *buf, uint32_t { int ret; + if (addr & 3 || (uint32_t)buf & 3 || n & 3) { + ESP_LOGE(TAG, "flash write parameters is not align, value is %p %x %x", buf, n ,addr); + return -1; + } + ESP_LOGD(TAG, "write buffer %p total %d to 0x%x", buf, n ,addr); ret = SPIWrite(addr, (void *)buf, n); @@ -182,7 +192,7 @@ static inline uint32_t esp_get_updated_partition_table_addr(void) static inline int spi_flash_write_data_safe(uint32_t addr, const void *buf, size_t n) { int ret; - static uint8_t check_buf[SPI_FLASH_SEC_SIZE]; + static ROM_FLASH_BUF_DECLARE(check_buf, SPI_FLASH_SEC_SIZE); ret = spi_flash_erase(addr); if (ret) {