1
6
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
53
54
55
59
60
61
62
63
64
65
66
67
68
72
76
80
81
82
83
84
85
86
87
88
89
90
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
113
114
118
119
120
121
122
123
124
125
126
127
128
129
133
137
141
145
149
150
151
154
155
156
160
161
162
163
164
165
166
167
168
169
170
171
172
173
188
189
190
191
192
193
194
195
196
197
198
199
200
201
204
205
206
207
208
209
210
214
215
216
217
218
219
220
221
222
223
224
227
228
229
244
245
246
249
250
251
252
253
254
255
256
257
258
259
260
261
264
265
266
267
268
269
273
274
275
276
277
278
279
280
281
282
283
284
285
288
289
290
291
292
295
296
297
298
299
300
303
304
305
308
309
310
313
314
315
319
320
321
322
323
330
331
332
333
338
339
340
341
345
346
347
348
349
354
355
356
357
358
359
360
361
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
382
383
384
388
389
390
391
392
393
394
395
398
399
400
401
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
422
423
424
425
429
430
431
432
433
434
435
442
443
444
445
446
447
448
449
450
451
452
453
454
457
458
459
460
461
462
463
464
465
466
467
470
471
472
481
482
485
486
487
488
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
514
515
516
517
520
521
522
526
527
528
529
536
539
540
541
542
543
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
573
574
575
579
580
581
585
586
587
591
592
593
600
601
602
603
604
/* ... */
#include <stdint.h>
#include <string.h>
#include "esp_log.h"
#include "nvs_bootloader.h"
#include "nvs_bootloader_private.h"
#include "esp_assert.h"
#include "esp_partition.h"
#include "nvs_constants.h"
#include <esp_rom_crc.h>9 includes
static const char* TAG = "nvs_bootloader";
const bool const_is_encrypted = false;
ESP_STATIC_ASSERT(sizeof(nvs_bootloader_page_header_t) == NVS_CONST_ENTRY_SIZE, "nvs_bootloader_page_header_t size is not 32 bytes");
ESP_STATIC_ASSERT(sizeof(nvs_bootloader_page_entry_states_t) == NVS_CONST_ENTRY_SIZE, "nvs_bootloader_page_entry_states_t size is not 32 bytes");
ESP_STATIC_ASSERT(sizeof(nvs_bootloader_single_entry_t) == NVS_CONST_ENTRY_SIZE, "nvs_bootloader_single_entry_t size is not 32 bytes");
esp_err_t nvs_bootloader_read(const char* partition_name,
const size_t read_list_count,
nvs_bootloader_read_list_t read_list[])
{
/* ... */
ESP_LOGD(TAG, "nvs_bootloader_read called with partition_name: %s, read_list_count: %u", partition_name, (unsigned)read_list_count);
esp_err_t ret = ESP_OK;
ret = nvs_bootloader_check_parameters(partition_name, read_list_count, read_list);
if (ret != ESP_OK) {
ESP_LOGD(TAG, "nvs_bootloader_check_parameters failed");
return ret;
}{...}
const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, partition_name);
if (partition == NULL) {
ESP_LOGV(TAG, "esp_partition_find_first failed");
return ESP_ERR_NVS_PART_NOT_FOUND;
}{...}
ESP_LOGV(TAG, "Partition %s found, size is: %" PRIu32 "", partition->label, partition->size);
nvs_bootloader_page_visitor_param_get_page_states_t page_states_count = {0};
ret = nvs_bootloader_visit_pages(partition, nvs_bootloader_page_visitor_get_page_states, (nvs_bootloader_page_visitor_param_t*)&page_states_count);
if (ret != ESP_OK) {
ESP_LOGV(TAG, "Failed reading page states");
return ret;
}{...}
if (page_states_count.no_freeing_pages > 1) {
ESP_LOGV(TAG, "Multiple pages in the state of FREEING");
return ESP_ERR_INVALID_STATE;
}{...}
if (page_states_count.no_active_pages > 1) {
ESP_LOGV(TAG, "Multiple pages in the state of ACTIVE");
return ESP_ERR_INVALID_STATE;
}{...}
nvs_bootloader_page_visitor_param_read_entries_t read_entries = { .skip_active_page = page_states_count.no_freeing_pages > 0, .read_list_count = read_list_count, .read_list = read_list };
ESP_LOGV(TAG, "nvs_bootloader_read - visiting pages to get namespace indexes");
ret = nvs_bootloader_visit_pages(partition, nvs_bootloader_page_visitor_get_namespaces, (nvs_bootloader_page_visitor_param_t*) &read_entries);
if (ret != ESP_OK) {
ESP_LOGV(TAG, "nvs_bootloader_page_visitor_get_namespaces failed");
return ret;
}{...}
ESP_LOGV(TAG, "nvs_bootloader_read - visiting pages to get key - value pairs");
ret = nvs_bootloader_visit_pages(partition, nvs_bootloader_page_visitor_get_key_value_pairs, (nvs_bootloader_page_visitor_param_t*) &read_entries);
return ret;
}{ ... }
esp_err_t nvs_bootloader_check_parameters(const char* partition_name,
const size_t read_list_count,
nvs_bootloader_read_list_t read_list[])
{
if (partition_name == NULL || strlen(partition_name) > NVS_PART_NAME_MAX_SIZE) {
ESP_LOGD(TAG, "Invalid argument: partition_name is NULL or too long");
return ESP_ERR_NVS_INVALID_NAME;
}{...}
if (read_list == NULL || read_list_count == 0) {
ESP_LOGD(TAG, "Invalid argument: read_list is NULL or read_list_count is 0");
return ESP_ERR_INVALID_ARG;
}{...}
esp_err_t ret = ESP_OK;
for (size_t i = 0; i < read_list_count; i++) {
read_list[i].result_code = ESP_ERR_NVS_NOT_FOUND;
read_list[i].namespace_index = 0;
if (read_list[i].namespace_name == NULL || strlen(read_list[i].namespace_name) > NVS_NS_NAME_MAX_SIZE - 1) {
ESP_LOGD(TAG, "Invalid argument: namespace_name is NULL or too long");
read_list[i].result_code = ESP_ERR_NVS_INVALID_NAME;
}{...}
if (read_list[i].key_name == NULL || strlen(read_list[i].key_name) > NVS_KEY_NAME_MAX_SIZE - 1) {
ESP_LOGD(TAG, "Invalid argument: key_name is NULL or too long");
read_list[i].result_code = ESP_ERR_NVS_KEY_TOO_LONG;
}{...}
if (!NVS_BOOTLOADER_IS_SUPPORTED_TYPE(read_list[i].value_type)) {
ESP_LOGD(TAG, "Invalid argument: value_type is invalid");
read_list[i].result_code = ESP_ERR_INVALID_ARG;
}{...}
if ((read_list[i].value_type) == NVS_TYPE_STR && (read_list[i].value.str_val.buff_len == 0 || read_list[i].value.str_val.buff_len > NVS_CONST_STR_LEN_MAX_SIZE)) {
ESP_LOGD(TAG, "Invalid argument: buffer size provided for NVS_TYPE_STR is invalid");
read_list[i].result_code = ESP_ERR_INVALID_SIZE;
}{...}
if ((read_list[i].value_type) == NVS_TYPE_STR && (read_list[i].value.str_val.buff_ptr == NULL)) {
ESP_LOGD(TAG, "Invalid argument: buffer pointer provided for NVS_TYPE_STR is null");
read_list[i].result_code = ESP_ERR_INVALID_SIZE;
}{...}
if (read_list[i].result_code != ESP_ERR_NVS_NOT_FOUND) {
ret = ESP_ERR_INVALID_ARG;
}{...}
if (NVS_BOOTLOADER_TYPE_FITS_SINGLE_ENTRY(read_list[i].value_type)) {
size_t val_len = sizeof(read_list[i].value);
memset((void*) &read_list[i].value, 0, val_len);
}{...}
}{...}
return ret;
}{ ... }
esp_err_t nvs_bootloader_page_visitor_get_page_states(nvs_bootloader_page_visitor_param_t *visitor_param,
const esp_partition_t *partition,
const size_t page_index,
const nvs_bootloader_page_header_t *page_header)
{
nvs_bootloader_page_visitor_param_get_page_states_t *param = (nvs_bootloader_page_visitor_param_get_page_states_t*) visitor_param;
switch (page_header->page_state) {
case NVS_CONST_PAGE_STATE_ACTIVE:
param->no_active_pages++;
break;...
case NVS_CONST_PAGE_STATE_FREEING:
param->no_freeing_pages++;
break;...
default:
break;...
}{...}
return ESP_OK;
}{ ... }
esp_err_t nvs_bootloader_page_visitor_get_namespaces(nvs_bootloader_page_visitor_param_t *visitor_param,
const esp_partition_t *partition,
const size_t page_index,
const nvs_bootloader_page_header_t *page_header)
{
ESP_LOGV(TAG, "nvs_bootloader_page_visitor_get_namespaces called with page_index: %u", (unsigned)page_index);
nvs_bootloader_page_visitor_param_read_entries_t *param = (nvs_bootloader_page_visitor_param_read_entries_t*) visitor_param;
if (param->skip_active_page && page_header->page_state == NVS_CONST_PAGE_STATE_ACTIVE) {
return ESP_OK;
}{...}
WORD_ALIGNED_ATTR nvs_bootloader_page_entry_states_t page_entry_states = {0};
esp_err_t ret = nvs_bootloader_read_page_entry_states(partition, page_index, &page_entry_states);
if (ret != ESP_OK) {
ESP_LOGV(TAG, "Error reading NVS page entry states");
return ret;
}{...}
uint8_t start_index = 0;
nvs_bootloader_single_entry_t item = {0};
while (ret == ESP_OK) {
ret = nvs_bootloader_read_next_single_entry_item(partition, page_index, &page_entry_states, &start_index, &item);
if (ret != ESP_OK) {
break;
}{...}
if (item.namespace_index == 0 && item.data_type == NVS_TYPE_U8) {
ESP_LOGV(TAG, "Namespace %s record found. NS index is: %d. Trying to match read list...", item.key, item.data.primitive_type.data[0]);
for (size_t i = 0; i < param->read_list_count; i++) {
if (param->read_list[i].namespace_index != 0) {
continue;
}{...}
if (strncmp(param->read_list[i].namespace_name, item.key, NVS_KEY_NAME_MAX_SIZE) == 0) {
param->read_list[i].namespace_index = item.data.primitive_type.data[0];
ESP_LOGV(TAG, "Namespace %s found for read item: %u key: %s NS index is: %d", param->read_list[i].namespace_name,(unsigned)i, param->read_list[i].key_name, param->read_list[i].namespace_index);
}{...}
}{...}
}{...}
}{...}
if (ret == ESP_ERR_NVS_NOT_FOUND) {
ret = ESP_OK;
}{...}
return ret;
}{ ... }
esp_err_t nvs_bootloader_page_visitor_get_key_value_pairs(nvs_bootloader_page_visitor_param_t *visitor_param,
const esp_partition_t *partition,
const size_t page_index,
const nvs_bootloader_page_header_t *page_header)
{
nvs_bootloader_page_visitor_param_read_entries_t *param = (nvs_bootloader_page_visitor_param_read_entries_t*) visitor_param;
if (param->skip_active_page && page_header->page_state == NVS_CONST_PAGE_STATE_ACTIVE) {
return ESP_OK;
}{...}
WORD_ALIGNED_ATTR nvs_bootloader_page_entry_states_t page_entry_states = {0};
esp_err_t ret = nvs_bootloader_read_page_entry_states(partition, page_index, &page_entry_states);
if (ret != ESP_OK) {
ESP_LOGV(TAG, "Error reading NVS page entry states");
return ret;
}{...}
uint8_t next_index = 0;
uint8_t current_index = 0;
nvs_bootloader_single_entry_t item = {0};
while (ret == ESP_OK) {
current_index = next_index;
ret = nvs_bootloader_read_next_single_entry_item(partition, page_index, &page_entry_states, &next_index, &item);
if (ret != ESP_OK) {
break;
}{...}
ESP_LOGV(TAG, "nvs_bootloader_page_visitor_get_key_value_pairs - read item NS index: %d, key: %s, data type: %d, span: %d. Trying to match read list...", item.namespace_index, item.key, item.data_type, item.span);
if (item.namespace_index == 0) {
continue;
}{...}
for (size_t i = 0; i < param->read_list_count; i++) {
if (param->read_list[i].namespace_index != item.namespace_index) {
continue;
}{...}
if (param->read_list[i].result_code != ESP_ERR_NVS_NOT_FOUND) {
continue;
}{...}
if (strncmp(param->read_list[i].key_name, item.key, NVS_KEY_NAME_MAX_SIZE) != 0) {
continue;
}{...}
if (param->read_list[i].value_type != item.data_type) {
param->read_list[i].result_code = ESP_ERR_NVS_TYPE_MISMATCH;
continue;
}{...}
ESP_LOGV(TAG, "nvs_bootloader_page_visitor_get_key_value_pairs - matched item with read_list index: %u", (unsigned)i);
if (NVS_BOOTLOADER_TYPE_FITS_SINGLE_ENTRY(item.data_type)) {
memcpy((void*) ¶m->read_list[i].value, (void*) item.data.primitive_type.data, NVS_BOOTLOADER_GET_TYPE_LEN(item.data_type));
param->read_list[i].result_code = ESP_OK;
break;
}{...}
else if (item.data_type == NVS_TYPE_STR) {
if (param->read_list[i].value.str_val.buff_len < item.data.var_len_type.size) {
param->read_list[i].result_code = ESP_ERR_INVALID_SIZE;
break;
}{...}
esp_err_t ret_str = nvs_bootloader_read_entries_block(partition, page_index, &page_entry_states, current_index + 1, item.data.var_len_type.size, (uint8_t*)param->read_list[i].value.str_val.buff_ptr);
if (ret_str != ESP_OK) {
break;
}{...}
uint32_t calc_crc = esp_rom_crc32_le(0xffffffff, (uint8_t*)(param->read_list[i].value.str_val.buff_ptr), item.data.var_len_type.size);
if (calc_crc != item.data.var_len_type.crc32) {
ESP_LOGV(TAG, "CRC32 of string data failed");
break;
}{...}
param->read_list[i].result_code = ESP_OK;
}{...}
}{...}
}{...}
if (ret == ESP_ERR_NVS_NOT_FOUND) {
ret = ESP_OK;
}{...}
return ret;
}{ ... }
esp_err_t nvs_bootloader_visit_pages(const esp_partition_t *partition,
nvs_bootloader_page_visitor_t visitor,
nvs_bootloader_page_visitor_param_t *visitor_param)
{
esp_err_t ret = ESP_OK;
WORD_ALIGNED_ATTR nvs_bootloader_page_header_t page_header = {0};
for (size_t page_index = 0; page_index < (partition->size / NVS_CONST_PAGE_SIZE); page_index++) {
ret = nvs_bootloader_read_page_header(partition, page_index, &page_header);
if (ret != ESP_OK) {
ESP_LOGV(TAG, "Error %04x reading NVS page header of page %u", ret, (unsigned)page_index);
}{...}
if (ret == ESP_ERR_NVS_INVALID_STATE) {
ret = ESP_OK;
continue;
}{...}
ESP_LOGV(TAG, "nvs_bootloader_visit_pages - page %u, state: %" PRIu32 "", (unsigned)page_index, page_header.page_state);
if (page_header.page_state != NVS_CONST_PAGE_STATE_FULL
&& page_header.page_state != NVS_CONST_PAGE_STATE_ACTIVE
&& page_header.page_state != NVS_CONST_PAGE_STATE_FREEING) {
continue;
}{...}
ret = visitor(visitor_param, partition, page_index, &page_header);
if (ret != ESP_OK) {
ESP_LOGV(TAG, "Error %04x visiting NVS page %u", ret, (unsigned)page_index);
return ret;
}{...}
}{...}
return ret;
}{ ... }
esp_err_t nvs_bootloader_read_page_header(const esp_partition_t *partition,
const size_t page_index,
const nvs_bootloader_page_header_t *page_header)
{
esp_err_t ret = ESP_OK;
ret = esp_partition_read(partition, page_index * NVS_CONST_PAGE_SIZE + NVS_CONST_PAGE_HEADER_OFFSET, (void*)page_header, sizeof(nvs_bootloader_page_header_t));
if (ret != ESP_OK) {
return ret;
}{...}
uint32_t calc_crc = esp_rom_crc32_le(0xffffffff, (uint8_t*)page_header + offsetof(nvs_bootloader_page_header_t, sequence_number), offsetof(nvs_bootloader_page_header_t, crc32) - offsetof(nvs_bootloader_page_header_t, sequence_number));
if (calc_crc != page_header->crc32) {
ESP_LOGV(TAG, "CRC32 of page# %u header failed", (unsigned)page_index);
return ESP_ERR_NVS_INVALID_STATE;
}{...}
return ret;
}{ ... }
esp_err_t nvs_bootloader_read_page_entry_states(const esp_partition_t *partition,
const size_t page_index,
nvs_bootloader_page_entry_states_t *page_entry_states)
{
ESP_LOGV(TAG, "nvs_bootloader_page_visitor_get_namespaces - reading page entry states for page %u", (unsigned)page_index);
return esp_partition_read(partition, page_index * NVS_CONST_PAGE_SIZE + NVS_CONST_PAGE_ENTRY_TABLE_OFFSET, (void*)page_entry_states, sizeof(nvs_bootloader_page_entry_states_t));
}{ ... }
esp_err_t nvs_bootloader_read_next_single_entry_item(const esp_partition_t *partition,
const size_t page_index,
const nvs_bootloader_page_entry_states_t *page_entry_states,
uint8_t *entry_index,
nvs_bootloader_single_entry_t *item)
{
ESP_LOGV(TAG, "nvs_bootloader_read_next_single_entry_item called with page_index: %u, entry_index: %d", (unsigned)page_index, *entry_index);
if (*entry_index >= NVS_CONST_ENTRY_COUNT) {
return ESP_ERR_NVS_NOT_FOUND;
}{...}
esp_err_t ret = ESP_OK;
while ((*entry_index) < NVS_CONST_ENTRY_COUNT) {
if (NVS_BOOTLOADER_GET_ENTRY_STATE(page_entry_states, *entry_index) == NVS_CONST_ENTRY_STATE_WRITTEN) {
ret = esp_partition_read(partition, page_index * NVS_CONST_PAGE_SIZE + NVS_CONST_PAGE_ENTRY_DATA_OFFSET + (*entry_index) * NVS_CONST_ENTRY_SIZE, (void*)item, sizeof(nvs_bootloader_single_entry_t));
if (ret != ESP_OK) {
return ret;
}{...}
if (nvs_bootloader_check_item_header_consistency(item, *entry_index)) {
(*entry_index) += item->span;
if (NVS_BOOTLOADER_IS_SUPPORTED_TYPE(item->data_type)) {
return ESP_OK;
}{...}
}{...}
else {
(*entry_index)++;
}{...}
}{...}
else {
(*entry_index)++;
}{...}
}{...}
return ESP_ERR_NVS_NOT_FOUND;
}{ ... }
esp_err_t nvs_bootloader_read_entries_block(const esp_partition_t *partition,
const size_t page_index,
const nvs_bootloader_page_entry_states_t *entry_states,
const size_t entry_index,
const size_t block_len,
uint8_t *block)
{
ESP_LOGV(TAG, "nvs_bootloader_read_entries_block called with page_index: %u, entry_index: %u, block_len: %u", (unsigned)page_index, (unsigned)entry_index, (unsigned)block_len);
esp_err_t ret = ESP_ERR_INVALID_STATE;
do {
if (entry_index >= NVS_CONST_ENTRY_COUNT) {
ESP_LOGV(TAG, "entry index is beyond the page boundary");
break;
}{...}
size_t number_of_entries = block_len / NVS_CONST_ENTRY_SIZE;
if (block_len % NVS_CONST_ENTRY_SIZE != 0) {
number_of_entries++;
}{...}
if (entry_index + number_of_entries > NVS_CONST_ENTRY_COUNT) {
ESP_LOGV(TAG, "block length exceeds the page boundary");
break;
}{...}
bool all_entries_written = true;
for (size_t i = 0; i < number_of_entries; i++) {
if (NVS_BOOTLOADER_GET_ENTRY_STATE(entry_states, entry_index + i) != NVS_CONST_ENTRY_STATE_WRITTEN) {
ESP_LOGV(TAG, "some entry is not in the state written");
all_entries_written = false;
break;
}{...}
}{...}
if(!all_entries_written) {
break;
}{...}
ret = ESP_OK;
}{...} while (false);
if (ret != ESP_OK) {
return ret;
}{...}
size_t data_offset = page_index * NVS_CONST_PAGE_SIZE + NVS_CONST_PAGE_ENTRY_DATA_OFFSET + entry_index * NVS_CONST_ENTRY_SIZE ;
return esp_partition_read(partition, data_offset, block, block_len);
}{ ... }
bool nvs_bootloader_check_item_header_consistency(const nvs_bootloader_single_entry_t *item,
const uint8_t entry_index)
{
uint32_t calc_crc = esp_rom_crc32_le(0xffffffff, (uint8_t*)item + offsetof(nvs_bootloader_single_entry_t, namespace_index), offsetof(nvs_bootloader_single_entry_t, crc32) - offsetof(nvs_bootloader_single_entry_t, namespace_index));
calc_crc = esp_rom_crc32_le(calc_crc, (uint8_t*)item + offsetof(nvs_bootloader_single_entry_t, key), sizeof(item->key));
calc_crc = esp_rom_crc32_le(calc_crc, (uint8_t*)item + offsetof(nvs_bootloader_single_entry_t, data), sizeof(item->data));
bool ret = false;
do {
if (calc_crc != item->crc32) {
ESP_LOGV(TAG, "Header CRC32 failed for item with key %s", item->key);
break;
}{...}
if (item->span == 0) {
ESP_LOGV(TAG, "Span of key %s is zero", item->key);
break;
}{...}
if (NVS_BOOTLOADER_TYPE_FITS_SINGLE_ENTRY(item->data_type) && item->span != 1) {
ESP_LOGV(TAG, "Span of single entry key %s is not 1", item->key);
break;
}{...}
if ((uint16_t)entry_index + (uint16_t)(item->span) > NVS_CONST_ENTRY_COUNT) {
ESP_LOGV(TAG, "Span of key %s exceeds remaining page space", item->key);
break;
}{...}
if (item->data_type == NVS_TYPE_STR) {
if( ((item->span - 1) * NVS_CONST_ENTRY_SIZE < item->data.var_len_type.size)
|| ((item->span - 1) * NVS_CONST_ENTRY_SIZE > item->data.var_len_type.size + NVS_CONST_ENTRY_SIZE - 1) ) {
ESP_LOGV(TAG, "Span %u of key %s for string type doesn't match indicated string length %u", (unsigned)item->span, item->key, item->data.var_len_type.size);
break;
}{...}
}{...}
ret = true;
}{...} while (false);
return ret;
}{ ... }