Skip to content

Commit

Permalink
fix CRC and fill sparse area similar to original firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
g3gg0 committed Jul 26, 2024
1 parent c0ecf18 commit 844ffa3
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,7 @@ static error_t process_nvs_item(FsFile *file, size_t offset, size_t part_offset,
TRACE_INFO(" Namespace %s\r\n", (*namespaces)[item->nsIndex]);
}

uint32_t crc_header_calc = crc32_header(item);
TRACE_INFO(" Key %s\r\n", item->key);
TRACE_INFO(" Header CRC %08" PRIX32 " (calc %08" PRIX32 ")\r\n", item->crc32, crc_header_calc);
if (item->crc32 != crc_header_calc)
{
TRACE_INFO(" * Updating CRC\r\n");
item->crc32 = crc_header_calc;
error = ERROR_BAD_CRC;
}

switch (item->datatype & 0xF0)
{
Expand Down Expand Up @@ -626,7 +618,7 @@ static error_t process_nvs_item(FsFile *file, size_t offset, size_t part_offset,
uint32_t crc_data_calc = crc32(buffer, length);
TRACE_INFO(" Size %" PRIu32 "\r\n", item->var_length.size);
TRACE_INFO(" Data %s\r\n", buffer_hex);
TRACE_INFO(" Data CRC %08" PRIX32 " (calc %08" PRIX32 ")\r\n", item->var_length.data_crc32, crc32(buffer, length));
TRACE_INFO(" Data CRC %08" PRIX32 " (calc %08" PRIX32 ")\r\n", item->var_length.data_crc32, crc_data_calc);
if (item->var_length.data_crc32 != crc_data_calc)
{
TRACE_INFO(" * Updating CRC\r\n");
Expand All @@ -638,6 +630,15 @@ static error_t process_nvs_item(FsFile *file, size_t offset, size_t part_offset,
break;
}
}

uint32_t crc_header_calc = crc32_header(item);
TRACE_INFO(" Header CRC %08" PRIX32 " (calc %08" PRIX32 ")\r\n", item->crc32, crc_header_calc);
if (item->crc32 != crc_header_calc)
{
TRACE_INFO(" * Updating CRC\r\n");
item->crc32 = crc_header_calc;
error = ERROR_BAD_CRC;
}
}

return error;
Expand Down Expand Up @@ -835,12 +836,14 @@ error_t esp32_nvs_del(FsFile *file, size_t offset, size_t length, const char *na
struct ESP32_nvs_item *esp32_nvs_create_uint8(const char *name, uint8_t value)
{
struct ESP32_nvs_item *item = osAllocMem(sizeof(struct ESP32_nvs_item));
osMemset(item, 0x00, sizeof(struct ESP32_nvs_item));
osMemset(item, 0xFF, sizeof(struct ESP32_nvs_item));

item[0].datatype = NVS_TYPE_U8;
item[0].nsIndex = 0;
item[0].span = 1;
item[0].chunkIndex = 0xFF;
item[0].uint8 = value;
osMemset(item[0].key, 0x00, sizeof(item[0].key));
osStrcpy(item[0].key, name);

return item;
Expand All @@ -850,12 +853,14 @@ struct ESP32_nvs_item *esp32_nvs_create_string(const char *name, const char *val
{
int span = 2 + osStrlen(value) / sizeof(struct ESP32_nvs_item);
struct ESP32_nvs_item *item = osAllocMem(sizeof(struct ESP32_nvs_item) * span);
osMemset(item, 0x00, sizeof(struct ESP32_nvs_item) * span);
osMemset(item, 0xFF, sizeof(struct ESP32_nvs_item) * span);

item[0].datatype = NVS_TYPE_STR;
item[0].nsIndex = 0;
item[0].span = span;
item[0].chunkIndex = 0xFF;
item[0].uint16 = osStrlen(value);
osMemset(item[0].key, 0x00, sizeof(item[0].key));
osStrcpy(item[0].key, name);
osStrcpy((char *)&item[1], value);

Expand All @@ -866,19 +871,22 @@ struct ESP32_nvs_item *esp32_nvs_create_blob(const char *name, const char *value
{
int span = 2 + length / sizeof(struct ESP32_nvs_item);
struct ESP32_nvs_item *item = osAllocMem(sizeof(struct ESP32_nvs_item) * (span + 1));
osMemset(item, 0x00, sizeof(struct ESP32_nvs_item) * (span + 1));
osMemset(item, 0xFF, sizeof(struct ESP32_nvs_item) * (span + 1));

item[0].datatype = NVS_TYPE_BLOB;
item[0].nsIndex = 0;
item[0].span = span;
item[0].chunkIndex = 0;
item[0].var_length.size = length;
osMemset(item[0].key, 0x00, sizeof(item[0].key));
osStrcpy(item[0].key, name);
osMemcpy((char *)&item[1], value, length);

int idx = 2 + length / sizeof(struct ESP32_nvs_item);
item[idx].datatype = NVS_TYPE_BLOB_IDX;
item[idx].nsIndex = 0;
item[idx].span = 1;
item[idx].chunkIndex = 0xFF;
osMemset(item[idx].key, 0x00, sizeof(item[idx].key));
osStrcpy(item[idx].key, name);
item[idx].blob_index.size = length;
item[idx].blob_index.chunk_count = 1;
Expand Down

0 comments on commit 844ffa3

Please sign in to comment.