diff options
-rw-r--r-- | common/nvmem.c | 26 | ||||
-rw-r--r-- | test/nvmem.c | 49 |
2 files changed, 57 insertions, 18 deletions
diff --git a/common/nvmem.c b/common/nvmem.c index 1c7d8d69dc..8cf88909c0 100644 --- a/common/nvmem.c +++ b/common/nvmem.c @@ -244,7 +244,7 @@ static int nvmem_find_partition(void) return EC_SUCCESS; if (nvmem_is_unitialized()) { - CPRINTF("NvMem: No Valid Paritions and not fully erased!!\n"); + CPRINTS("NvMem: No Valid Paritions!"); return EC_ERROR_UNKNOWN; } @@ -316,20 +316,18 @@ int nvmem_setup(uint8_t starting_version) return EC_ERROR_TIMEOUT; } - /* Fill in tag info */ + /* Fill entire partition to 0xFFs */ + memset(cache.base_ptr, 0xff, NVMEM_PARTITION_SIZE); + /* Get pointer to start of partition */ p_part = (struct nvmem_partition *)cache.base_ptr; /* Commit function will increment version number */ p_part->tag.version = starting_version + part - 1; + /* Compute sha for the partition */ nvmem_compute_sha(&cache.base_ptr[NVMEM_SHA_SIZE], NVMEM_PARTITION_SIZE - NVMEM_SHA_SIZE, p_part->tag.sha, NVMEM_SHA_SIZE); - /* - * TODO: Should erase parition area prior to this function being - * called, or could write all user buffer data to 0xff here - * before the commit() call. - */ /* Partition is now ready, write it to flash. */ ret = nvmem_commit(); if (ret != EC_SUCCESS) @@ -357,10 +355,16 @@ int nvmem_init(void) ret = nvmem_find_partition(); if (ret != EC_SUCCESS) { - /* Change error state to non-zero */ - nvmem_error_state = EC_ERROR_UNKNOWN; - CPRINTF("%s:%d\n", __func__, __LINE__); - return ret; + /* Write NvMem partitions to 0xff and setup new tags */ + nvmem_setup(0); + /* Should find valid partiion now */ + ret = nvmem_find_partition(); + if (ret) { + /* Change error state to non-zero */ + nvmem_error_state = EC_ERROR_UNKNOWN; + CPRINTF("%s:%d\n", __func__, __LINE__); + return ret; + } } CPRINTS("Active NVram partition set to %d", nvmem_act_partition); diff --git a/test/nvmem.c b/test/nvmem.c index d3bc0b99e1..b9dd76c8cf 100644 --- a/test/nvmem.c +++ b/test/nvmem.c @@ -169,23 +169,58 @@ static int test_corrupt_nvmem(void) { uint32_t offset; int n; + int ret; + struct nvmem_tag *p_part; + uint8_t *p_data; /* * The purpose of this test is to check nvmem_init() in the case when no * vailid partition exists (not fully erased and no valid sha). In this - * case, NvMem can't be initialized and should return an error to the - * calling function. + * case, the initialization function will call setup() to create two new + * valid partitions. */ - /* Overwrite tags of each parition */ - memset(write_buffer, 0, 8); + /* Overwrite each partition will all 0s */ + memset(write_buffer, 0, NVMEM_PARTITION_SIZE); for (n = 0; n < NVMEM_NUM_PARTITIONS; n++) { offset = NVMEM_PARTITION_SIZE * n; - flash_physical_write(CONFIG_FLASH_NVMEM_OFFSET + offset, 8, + flash_physical_write(CONFIG_FLASH_NVMEM_OFFSET + offset, + NVMEM_PARTITION_SIZE, (const char *)write_buffer); } - /* In this case nvmem_init is expected to fail */ - return !nvmem_init(); + /* + * The initialization function will look for a valid partition and if + * none is found, then will call nvmem_setup() which will erase the + * paritions and setup new tags. + */ + ret = nvmem_init(); + if (ret) + return ret; + /* Fill buffer with 0xffs */ + memset(write_buffer, 0xff, NVMEM_PARTITION_SIZE); + /* + * nvmem_setup() will write put version 1 into partition 1 since the + * commit() function toggles the active partition. Check here that + * partition 0 has a version number of 1 and that all of the user buffer + * data has been erased. + */ + p_part = (struct nvmem_tag *)CONFIG_FLASH_NVMEM_BASE; + TEST_ASSERT(p_part->version == 1); + p_data = (uint8_t *)p_part + sizeof(struct nvmem_tag); + /* Verify that partition 0 is fully erased */ + TEST_ASSERT_ARRAY_EQ(write_buffer, p_data, NVMEM_PARTITION_SIZE - + sizeof(struct nvmem_tag)); + + /* Run the same test for partition 1 which should have version 0 */ + p_part = (struct nvmem_tag *)(CONFIG_FLASH_NVMEM_BASE + + NVMEM_PARTITION_SIZE); + TEST_ASSERT(p_part->version == 0); + p_data = (uint8_t *)p_part + sizeof(struct nvmem_tag); + ccprintf("Partition Version = %d\n", p_part->version); + /* Verify that partition 1 is fully erased */ + TEST_ASSERT_ARRAY_EQ(write_buffer, p_data, NVMEM_PARTITION_SIZE - + sizeof(struct nvmem_tag)); + return ret; } static int test_write_read_sequence(void) |