feat(nvs_flash): Modify for ESP8266

This commit is contained in:
Dong Heng
2021-07-23 17:21:22 +08:00
parent f09d86123d
commit f25a7ad205
16 changed files with 42 additions and 50 deletions

View File

@@ -68,4 +68,10 @@
#define _COUNTER_STRINGIFY(COUNTER) #COUNTER
#ifdef IDF_CI_BUILD
#define IDF_DEPRECATED(REASON) __attribute__((deprecated(REASON)))
#else
#define IDF_DEPRECATED(REASON)
#endif
#endif /* __ESP_ATTR_H__ */

View File

@@ -204,15 +204,15 @@ static const char* PHY_NAMESPACE = "phy";
static const char* PHY_CAL_DATA_KEY = "cal_data";
static const char* PHY_RX_GAIN_DC_TABLE_KEY = "dc_table";
static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
static esp_err_t load_cal_data_from_nvs_handle(nvs_handle_t handle,
esp_phy_calibration_data_t* out_cal_data);
static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
static esp_err_t store_cal_data_to_nvs_handle(nvs_handle_t handle,
const esp_phy_calibration_data_t* cal_data);
esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data)
{
nvs_handle handle;
nvs_handle_t handle;
esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
@@ -230,7 +230,7 @@ esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_dat
esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)
{
nvs_handle handle;
nvs_handle_t handle;
esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
if (err != ESP_OK) {
@@ -243,7 +243,7 @@ esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_da
}
}
static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
static esp_err_t load_cal_data_from_nvs_handle(nvs_handle_t handle,
esp_phy_calibration_data_t* out_cal_data)
{
esp_err_t err;
@@ -278,7 +278,7 @@ static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
return ESP_OK;
}
static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
static esp_err_t store_cal_data_to_nvs_handle(nvs_handle_t handle,
const esp_phy_calibration_data_t* cal_data)
{
esp_err_t err;

View File

@@ -136,7 +136,7 @@ static const char *BACKUP_MAC_DATA_KEY = "backup_mac_data";
static esp_err_t load_backup_mac_data(uint8_t *mac)
{
esp_err_t err;
nvs_handle handle;
nvs_handle_t handle;
uint32_t efuse[4];
uint8_t efuse_crc = 0;
uint8_t calc_crc = 0;
@@ -221,7 +221,7 @@ static esp_err_t load_backup_mac_data(uint8_t *mac)
static esp_err_t store_backup_mac_data()
{
esp_err_t err;
nvs_handle handle;
nvs_handle_t handle;
uint32_t efuse[4];
efuse[0] = REG_READ(EFUSE_DATA0_REG);
efuse[1] = REG_READ(EFUSE_DATA1_REG);

View File

@@ -31,7 +31,6 @@ static const char *interface_key[] = {"IF_STA", "IF_AP", "IF_ETH", "IF_TEST"};
_Static_assert(sizeof(interface_key) / sizeof(char*) == TCPIP_ADAPTER_IF_MAX,
"Number interface keys differs from number of interfaces");
typedef nvs_handle nvs_handle_t;
bool dhcp_ip_addr_restore(void *netif)
{
nvs_handle_t nvs;

View File

@@ -1,12 +0,0 @@
menu "NVS"
config NVS_ENCRYPTION
bool "Enable NVS encryption"
default y
depends on SECURE_FLASH_ENC_ENABLED
help
This option enables encryption for NVS. When enabled, AES-XTS is used to encrypt
the complete NVS data, except the page headers. It requires XTS encryption keys
to be stored in an encrypted partition. This means enabling flash encryption is
a pre-requisite for this feature.
endmenu

View File

@@ -52,7 +52,7 @@ public:
esp_err_t read_raw(size_t src_offset, void* dst, size_t size) override
{
return esp_partition_read_raw(&partition, src_offset, dst, size);
return esp_partition_read(&partition, src_offset, dst, size);
}
esp_err_t read(size_t src_offset, void* dst, size_t size) override
@@ -62,7 +62,7 @@ public:
esp_err_t write_raw(size_t dst_offset, const void* src, size_t size) override
{
return esp_partition_write_raw(&partition, dst_offset, src, size);
return esp_partition_write(&partition, dst_offset, src, size);
}
esp_err_t write(size_t dst_offset, const void* src, size_t size) override

View File

@@ -52,7 +52,7 @@ static const unsigned int crc32_le_table[256] = {
extern "C" uint32_t esp_rom_crc32_le(unsigned int crc, unsigned char const * buf,unsigned int len)
extern "C" uint32_t crc32_le(unsigned int crc, unsigned char const * buf,unsigned int len)
{
unsigned int i;
crc = ~crc;

View File

@@ -24,7 +24,7 @@ extern "C" {
* Mock function to replace ESP ROM function used in IDF with a Linux implementation.
* Note: the name MUST have the prefix esp_rom_* since tools/ci/check_rom_apis.sh checks and complains otherwise.
*/
uint32_t esp_rom_crc32_le(uint32_t crc, const uint8_t* buf, size_t len);
uint32_t crc32_le(uint32_t crc, const uint8_t* buf, size_t len);
#ifdef __cplusplus
}

View File

@@ -27,7 +27,7 @@
#include "crc.h"
#define ESP_LOGD(...)
#else // LINUX_TARGET
#include <esp32/rom/crc.h>
#include <esp_crc.h>
// Uncomment this line to force output from this module
// #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
@@ -586,13 +586,13 @@ extern "C" esp_err_t nvs_flash_generate_keys(const esp_partition_t* partition, n
* But the read is decrypted through flash encryption engine. This allows unique NVS encryption configuration,
* as flash encryption key is randomly generated per device.
*/
err = esp_partition_write_raw(partition, 0, cfg->eky, NVS_KEY_SIZE);
err = esp_partition_write(partition, 0, cfg->eky, NVS_KEY_SIZE);
if(err != ESP_OK) {
return err;
}
/* Write without encryption, see note above */
err = esp_partition_write_raw(partition, NVS_KEY_SIZE, cfg->tky, NVS_KEY_SIZE);
err = esp_partition_write(partition, NVS_KEY_SIZE, cfg->tky, NVS_KEY_SIZE);
if(err != ESP_OK) {
return err;
}
@@ -638,17 +638,17 @@ extern "C" esp_err_t nvs_flash_read_security_cfg(const esp_partition_t* partitio
return true;
};
auto err = esp_partition_read_raw(partition, 0, eky_raw, NVS_KEY_SIZE);
auto err = esp_partition_read(partition, 0, eky_raw, NVS_KEY_SIZE);
if(err != ESP_OK) {
return err;
}
err = esp_partition_read_raw(partition, NVS_KEY_SIZE, tky_raw, NVS_KEY_SIZE);
err = esp_partition_read(partition, NVS_KEY_SIZE, tky_raw, NVS_KEY_SIZE);
if(err != ESP_OK) {
return err;
}
err = esp_partition_read_raw(partition, 2 * NVS_KEY_SIZE, &crc_raw, 4);
err = esp_partition_read(partition, 2 * NVS_KEY_SIZE, &crc_raw, 4);
if(err != ESP_OK) {
return err;
}

View File

@@ -15,7 +15,7 @@
#if defined(LINUX_TARGET)
#include "crc.h"
#else
#include <esp_rom_crc.h>
#include <esp_crc.h>
#endif
#include <cstdio>
#include <cstring>
@@ -27,7 +27,7 @@ Page::Page() : mPartition(nullptr) { }
uint32_t Page::Header::calculateCrc32()
{
return esp_rom_crc32_le(0xffffffff,
return crc32_le(0xffffffff,
reinterpret_cast<uint8_t*>(this) + offsetof(Header, mSeqNumber),
offsetof(Header, mCrc32) - offsetof(Header, mSeqNumber));
}

View File

@@ -33,7 +33,7 @@ const char *NVSPartition::get_partition_name()
esp_err_t NVSPartition::read_raw(size_t src_offset, void* dst, size_t size)
{
return esp_partition_read_raw(mESPPartition, src_offset, dst, size);
return esp_partition_read(mESPPartition, src_offset, dst, size);
}
esp_err_t NVSPartition::read(size_t src_offset, void* dst, size_t size)
@@ -47,7 +47,7 @@ esp_err_t NVSPartition::read(size_t src_offset, void* dst, size_t size)
esp_err_t NVSPartition::write_raw(size_t dst_offset, const void* src, size_t size)
{
return esp_partition_write_raw(mESPPartition, dst_offset, src, size);
return esp_partition_write(mESPPartition, dst_offset, src, size);
}
esp_err_t NVSPartition::write(size_t dst_offset, const void* src, size_t size)

View File

@@ -50,7 +50,7 @@ public:
const char *get_partition_name() override;
/**
* Look into \c esp_partition_read_raw for more details.
* Look into \c esp_partition_read for more details.
*
* @return
* - ESP_OK on success
@@ -69,7 +69,7 @@ public:
esp_err_t read(size_t src_offset, void* dst, size_t size) override;
/**
* Look into \c esp_partition_write_raw for more details.
* Look into \c esp_partition_write for more details.
*
* @return
* - ESP_OK on success

View File

@@ -16,7 +16,7 @@
#if defined(LINUX_TARGET)
#include "crc.h"
#else
#include <esp_rom_crc.h>
#include <esp_crc.h>
#endif
namespace nvs
@@ -25,10 +25,10 @@ uint32_t Item::calculateCrc32() const
{
uint32_t result = 0xffffffff;
const uint8_t* p = reinterpret_cast<const uint8_t*>(this);
result = esp_rom_crc32_le(result, p + offsetof(Item, nsIndex),
result = crc32_le(result, p + offsetof(Item, nsIndex),
offsetof(Item, crc32) - offsetof(Item, nsIndex));
result = esp_rom_crc32_le(result, p + offsetof(Item, key), sizeof(key));
result = esp_rom_crc32_le(result, p + offsetof(Item, data), sizeof(data));
result = crc32_le(result, p + offsetof(Item, key), sizeof(key));
result = crc32_le(result, p + offsetof(Item, data), sizeof(data));
return result;
}
@@ -36,17 +36,17 @@ uint32_t Item::calculateCrc32WithoutValue() const
{
uint32_t result = 0xffffffff;
const uint8_t* p = reinterpret_cast<const uint8_t*>(this);
result = esp_rom_crc32_le(result, p + offsetof(Item, nsIndex),
result = crc32_le(result, p + offsetof(Item, nsIndex),
offsetof(Item, datatype) - offsetof(Item, nsIndex));
result = esp_rom_crc32_le(result, p + offsetof(Item, key), sizeof(key));
result = esp_rom_crc32_le(result, p + offsetof(Item, chunkIndex), sizeof(chunkIndex));
result = crc32_le(result, p + offsetof(Item, key), sizeof(key));
result = crc32_le(result, p + offsetof(Item, chunkIndex), sizeof(chunkIndex));
return result;
}
uint32_t Item::calculateCrc32(const uint8_t* data, size_t size)
{
uint32_t result = 0xffffffff;
result = esp_rom_crc32_le(result, data, size);
result = crc32_le(result, data, size);
return result;
}

View File

@@ -7,7 +7,6 @@
#include "nvs.h"
#include "nvs_flash.h"
#include "esp_partition.h"
#include "esp_flash_encrypt.h"
#include "esp_log.h"
#include <string.h>
#include "esp_system.h"

View File

@@ -62,7 +62,7 @@ esp_err_t esp_partition_read(const esp_partition_t* partition,
return ESP_OK;
}
esp_err_t esp_partition_read_raw(const esp_partition_t* partition,
esp_err_t esp_partition_read(const esp_partition_t* partition,
size_t src_offset, void* dst, size_t size)
{
if (!s_emulator) {
@@ -90,7 +90,7 @@ esp_err_t esp_partition_write(const esp_partition_t* partition,
return ESP_OK;
}
esp_err_t esp_partition_write_raw(const esp_partition_t* partition,
esp_err_t esp_partition_write(const esp_partition_t* partition,
size_t dst_offset, const void* src, size_t size)
{
if (!s_emulator) {

View File

@@ -95,7 +95,7 @@ TEST_CASE("EMU raw read function works", "[spi_flash_emu]")
uint32_t read_value = 0;
CHECK(esp_partition_write(&f.esp_part, 0, &value, sizeof(value)) == ESP_OK);
CHECK(esp_partition_read_raw(&f.esp_part, 0, &read_value, sizeof(&read_value)) == ESP_OK);
CHECK(esp_partition_read(&f.esp_part, 0, &read_value, sizeof(&read_value)) == ESP_OK);
CHECK(read_value == 0xdeadbeef);
}
@@ -105,7 +105,7 @@ TEST_CASE("EMU raw write function works", "[spi_flash_emu]")
FlashEmuFixture f(4);
uint32_t value = 0xdeadbeef;
uint32_t read_value = 0;
CHECK(esp_partition_write_raw(&f.esp_part, 0, &value, sizeof(value)) == ESP_OK);
CHECK(esp_partition_write(&f.esp_part, 0, &value, sizeof(value)) == ESP_OK);
CHECK(esp_partition_read(&f.esp_part, 0, &read_value, sizeof(&read_value)) == ESP_OK);