|
|
|
@ -19,6 +19,7 @@
@@ -19,6 +19,7 @@
|
|
|
|
|
#include <soc/spi_struct.h> |
|
|
|
|
#include <esp_flash_encrypt.h> |
|
|
|
|
#include <esp_flash_internal.h> |
|
|
|
|
#include <bootloader_flash_priv.h> |
|
|
|
|
|
|
|
|
|
#include <zephyr/kernel.h> |
|
|
|
|
#include <zephyr/device.h> |
|
|
|
@ -69,21 +70,49 @@ static inline void flash_esp32_sem_give(const struct device *dev)
@@ -69,21 +70,49 @@ static inline void flash_esp32_sem_give(const struct device *dev)
|
|
|
|
|
|
|
|
|
|
#endif /* CONFIG_MULTITHREADING */ |
|
|
|
|
|
|
|
|
|
#include <zephyr/kernel.h> |
|
|
|
|
#include <zephyr/logging/log.h> |
|
|
|
|
#include <zephyr/sys/util.h> |
|
|
|
|
#include <stdint.h> |
|
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
|
|
static int flash_esp32_read(const struct device *dev, off_t address, void *buffer, size_t length) |
|
|
|
|
{ |
|
|
|
|
int ret = 0; |
|
|
|
|
|
|
|
|
|
flash_esp32_sem_take(dev); |
|
|
|
|
#ifdef CONFIG_MCUBOOT |
|
|
|
|
/* ensure everything is 4-byte aligned */ |
|
|
|
|
size_t aligned_length = ROUND_UP(length, 4); |
|
|
|
|
off_t aligned_address = ROUND_DOWN(address, 4); |
|
|
|
|
size_t address_offset = address - aligned_address; |
|
|
|
|
uint32_t temp_buf[aligned_length / 4]; |
|
|
|
|
|
|
|
|
|
if (!esp_flash_encryption_enabled()) { |
|
|
|
|
ret = esp_flash_read(NULL, buffer, address, length); |
|
|
|
|
ret = esp_rom_flash_read(aligned_address, temp_buf, aligned_length, false); |
|
|
|
|
} else { |
|
|
|
|
ret = esp_rom_flash_read(aligned_address, temp_buf, aligned_length, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
memcpy((void *)buffer, ((uint8_t *)temp_buf) + address_offset, length); |
|
|
|
|
#else |
|
|
|
|
|
|
|
|
|
flash_esp32_sem_take(dev); |
|
|
|
|
|
|
|
|
|
if (esp_flash_encryption_enabled()) { |
|
|
|
|
ret = esp_flash_read_encrypted(NULL, address, buffer, length); |
|
|
|
|
} else { |
|
|
|
|
ret = esp_flash_read(NULL, buffer, address, length); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
flash_esp32_sem_give(dev); |
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
if (ret != 0) { |
|
|
|
|
LOG_ERR("esp_flash_read failed %d", ret); |
|
|
|
|
LOG_ERR("Flash read error: %d", ret); |
|
|
|
|
return -EIO; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -94,28 +123,55 @@ static int flash_esp32_write(const struct device *dev,
@@ -94,28 +123,55 @@ static int flash_esp32_write(const struct device *dev,
|
|
|
|
|
{ |
|
|
|
|
int ret = 0; |
|
|
|
|
|
|
|
|
|
flash_esp32_sem_take(dev); |
|
|
|
|
#ifdef CONFIG_MCUBOOT |
|
|
|
|
/* ensure everything is 4-byte aligned */ |
|
|
|
|
size_t aligned_length = ROUND_UP(length, 4); |
|
|
|
|
off_t aligned_address = ROUND_DOWN(address, 4); |
|
|
|
|
size_t address_offset = address - aligned_address; |
|
|
|
|
uint32_t temp_buf[aligned_length / 4]; |
|
|
|
|
|
|
|
|
|
if (!esp_flash_encryption_enabled()) { |
|
|
|
|
ret = esp_flash_write(NULL, buffer, address, length); |
|
|
|
|
ret = esp_rom_flash_write(aligned_address, temp_buf, aligned_length, false); |
|
|
|
|
} else { |
|
|
|
|
ret = esp_rom_flash_write(aligned_address, temp_buf, aligned_length, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
memcpy((void *)buffer, ((uint8_t *)temp_buf) + address_offset, length); |
|
|
|
|
#else |
|
|
|
|
|
|
|
|
|
flash_esp32_sem_take(dev); |
|
|
|
|
|
|
|
|
|
if (esp_flash_encryption_enabled()) { |
|
|
|
|
ret = esp_flash_write_encrypted(NULL, address, buffer, length); |
|
|
|
|
} else { |
|
|
|
|
ret = esp_flash_write(NULL, buffer, address, length); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
flash_esp32_sem_give(dev); |
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
if (ret != 0) { |
|
|
|
|
LOG_ERR("esp_flash_write failed %d", ret); |
|
|
|
|
LOG_ERR("Flash write error: %d", ret); |
|
|
|
|
return -EIO; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int flash_esp32_erase(const struct device *dev, off_t start, size_t len) |
|
|
|
|
{ |
|
|
|
|
int ret = 0; |
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_MCUBOOT |
|
|
|
|
ret = esp_rom_flash_erase_range(start, len); |
|
|
|
|
#else |
|
|
|
|
flash_esp32_sem_take(dev); |
|
|
|
|
int ret = esp_flash_erase_region(NULL, start, len); |
|
|
|
|
ret = esp_flash_erase_region(NULL, start, len); |
|
|
|
|
flash_esp32_sem_give(dev); |
|
|
|
|
#endif |
|
|
|
|
if (ret != 0) { |
|
|
|
|
LOG_ERR("esp_flash_erase_region failed %d", ret); |
|
|
|
|
LOG_ERR("Flash erase error: %d", ret); |
|
|
|
|
return -EIO; |
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
@ -146,18 +202,12 @@ flash_esp32_get_parameters(const struct device *dev)
@@ -146,18 +202,12 @@ flash_esp32_get_parameters(const struct device *dev)
|
|
|
|
|
|
|
|
|
|
static int flash_esp32_init(const struct device *dev) |
|
|
|
|
{ |
|
|
|
|
uint32_t ret = 0; |
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_MULTITHREADING |
|
|
|
|
struct flash_esp32_dev_data *const dev_data = dev->data; |
|
|
|
|
|
|
|
|
|
k_sem_init(&dev_data->sem, 1, 1); |
|
|
|
|
#endif /* CONFIG_MULTITHREADING */ |
|
|
|
|
ret = esp_flash_init_default_chip(); |
|
|
|
|
if (ret != 0) { |
|
|
|
|
LOG_ERR("esp_flash_init_default_chip failed %d", ret); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|