diff --git a/common/nvmem.c b/common/nvmem.c index 6a11b64fa7..3358ad5b3a 100644 --- a/common/nvmem.c +++ b/common/nvmem.c @@ -48,7 +48,7 @@ static int nvmem_error_state; /* Flag to track if an Nv write/move is not completed */ static int nvmem_write_error; -static int nvmem_save(uint8_t tag_version, size_t partition) +static int nvmem_save(uint8_t tag_generation, size_t partition) { struct nvmem_tag *tag; size_t nvmem_offset; @@ -64,12 +64,12 @@ static int nvmem_save(uint8_t tag_version, size_t partition) } tag = (struct nvmem_tag *)cache.base_ptr; - tag->version = tag_version; + tag->generation = tag_generation; /* Calculate sha of the whole thing. */ - nvmem_compute_sha(&tag->version, + nvmem_compute_sha(&tag->generation, NVMEM_PARTITION_SIZE - - offsetof(struct nvmem_tag, version), + offsetof(struct nvmem_tag, generation), tag->sha, sizeof(tag->sha)); @@ -90,7 +90,7 @@ static int nvmem_partition_sha_match(int index) struct nvmem_partition *p_part; p_part = (struct nvmem_partition *)nvmem_base_addr[index]; - nvmem_compute_sha(&p_part->tag.version, + nvmem_compute_sha(&p_part->tag.generation, (NVMEM_PARTITION_SIZE - NVMEM_SHA_SIZE), sha_comp, sizeof(sha_comp)); @@ -197,7 +197,7 @@ static int nvmem_reinitialize(void) memset(cache.base_ptr, 0xff, NVMEM_PARTITION_SIZE); - /* Start with version zero in the current active partition. */ + /* Start with generation zero in the current active partition. */ ret = nvmem_save(0, nvmem_act_partition); nvmem_release_cache(); if (ret) { @@ -207,25 +207,26 @@ static int nvmem_reinitialize(void) return EC_SUCCESS; } -static int nvmem_compare_version(void) +static int nvmem_compare_generation(void) { struct nvmem_partition *p_part; uint16_t ver0, ver1; uint32_t delta; p_part = (struct nvmem_partition *)nvmem_base_addr[0]; - ver0 = p_part->tag.version; + ver0 = p_part->tag.generation; p_part = (struct nvmem_partition *)nvmem_base_addr[1]; - ver1 = p_part->tag.version; + ver1 = p_part->tag.generation; - /* Compute version difference accounting for wrap condition */ - delta = (ver0 - ver1 + (1<tag.version = starting_version + part - 1; + /* Commit function will increment generation number */ + p_part->tag.generation = starting_generation + part - 1; /* Compute sha for the partition */ nvmem_compute_sha(&cache.base_ptr[NVMEM_SHA_SIZE], NVMEM_PARTITION_SIZE - @@ -547,7 +549,7 @@ void nvmem_disable_commits(void) int nvmem_commit(void) { int new_active_partition; - uint16_t version; + uint16_t generation; struct nvmem_partition *p_part; if (!commits_enabled) { @@ -570,22 +572,22 @@ int nvmem_commit(void) * entries must be reset along with the index itself. */ - /* Update version number */ + /* Update generation number */ if (cache.base_ptr == NULL) { CPRINTF("%s:%d\n", __func__, __LINE__); return EC_ERROR_UNKNOWN; } p_part = (struct nvmem_partition *)cache.base_ptr; - version = p_part->tag.version + 1; - /* Check for restricted version number */ - if (version == NVMEM_VERSION_MASK) - version = 0; + generation = p_part->tag.generation + 1; + /* Check for restricted generation number */ + if (generation == NVMEM_GENERATION_MASK) + generation = 0; /* Toggle parition being used (always write to current spare) */ new_active_partition = nvmem_act_partition ^ 1; /* Write active partition to NvMem */ - if (nvmem_save(version, new_active_partition) != EC_SUCCESS) { + if (nvmem_save(generation, new_active_partition) != EC_SUCCESS) { /* Free up scratch buffers */ nvmem_release_cache(); return EC_ERROR_UNKNOWN; diff --git a/include/nvmem.h b/include/nvmem.h index 730870345a..9bbf851a75 100644 --- a/include/nvmem.h +++ b/include/nvmem.h @@ -17,12 +17,12 @@ * --------------------------------------------------------------------- * * Physical Block Tag details - * --------------------------------------------------------------------- - * | sha | version | reserved | - * --------------------------------------------------------------------- - * sha -> 4 bytes of sha1 digest - * version -> 1 byte version number (0 - 0xfe) - * reserved -> 3 bytes + * -------------------------------------------------------------------- + * | sha | generation | reserved | + * -------------------------------------------------------------------- + * sha -> 4 bytes of sha1 digest + * generation -> 1 byte generation number (0 - 0xfe) + * reserved -> 3 bytes * * At initialization time, each partition is scanned to see if it has a good sha * entry. One of the two partitions being valid is a supported condition. If @@ -36,8 +36,8 @@ * must be equal in total size. A table is used by the NvMem module to get the * correct base address and offset for each partition. * - * A version number is used to distinguish between two valid partitions with - * the newsest version number (in a circular sense) marking the correct + * A generation number is used to distinguish between two valid partitions with + * the newsest generation number (in a circular sense) marking the correct * partition to use. The parition number 0/1 is tracked via a static * variable. When the NvMem contents need to be updated, the flash erase/write * of the updated partition will use the inactive partition space in NvMem. This @@ -65,13 +65,13 @@ extern uint32_t nvmem_user_sizes[NVMEM_NUM_USERS]; #define NVMEM_NUM_PARTITIONS 2 #define NVMEM_SHA_SIZE 4 -#define NVMEM_VERSION_BITS 8 -#define NVMEM_VERSION_MASK ((1 << NVMEM_VERSION_BITS) - 1) +#define NVMEM_GENERATION_BITS 8 +#define NVMEM_GENERATION_MASK ((1 << NVMEM_GENERATION_BITS) - 1) /* Struct for NV block tag */ struct nvmem_tag { uint8_t sha[NVMEM_SHA_SIZE]; - uint8_t version; + uint8_t generation; uint8_t reserved[3]; }; @@ -159,12 +159,12 @@ int nvmem_commit(void); /** * One time initialization of NvMem partitions - * @param version: Starting version number of partition 0 + * @param generation: Starting generation number of partition 0 * * @return EC_SUCCESS if flash operations are successful. * EC_ERROR_UNKNOWN otherwise. */ -int nvmem_setup(uint8_t version); +int nvmem_setup(uint8_t generation); /** * Compute sha1 (lower 4 bytes or equivalent checksum) for NvMem tag diff --git a/test/nvmem.c b/test/nvmem.c index 9f007f0191..5ed33f3445 100644 --- a/test/nvmem.c +++ b/test/nvmem.c @@ -154,7 +154,7 @@ static int test_configured_nvmem(void) * partitions are configured and valid. */ - /* Configure all NvMem partitions with starting version number 0 */ + /* Configure all NvMem partitions with starting generation number 0 */ nvmem_setup(0); /* Call NvMem initialization */ return nvmem_init(); @@ -162,6 +162,7 @@ static int test_configured_nvmem(void) static int test_corrupt_nvmem(void) { + uint8_t invalid_value = 0x55; int ret; struct nvmem_tag *p_part; uint8_t *p_data; @@ -174,7 +175,7 @@ static int test_corrupt_nvmem(void) */ /* Overwrite each partition will all 0s */ - memset(write_buffer, 0, NVMEM_PARTITION_SIZE); + memset(write_buffer, invalid_value, NVMEM_PARTITION_SIZE); flash_physical_write(CONFIG_FLASH_NVMEM_OFFSET_A, NVMEM_PARTITION_SIZE, (const char *)write_buffer); @@ -192,27 +193,23 @@ static int test_corrupt_nvmem(void) /* 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. + * nvmem_init() will create generation 0 in partition 0 and commit it. + * Check here that partition 0 has a generation number of 0 and that + * all of the user buffer data has been erased. */ p_part = (struct nvmem_tag *)CONFIG_FLASH_NVMEM_BASE_A; - TEST_ASSERT(p_part->version == 1); + TEST_ASSERT(p_part->generation == 0); 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_B; - 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; + /* Verify that partition 1 still has invalid values in it. */ + p_data = (uint8_t *)CONFIG_FLASH_NVMEM_BASE_B; + memset(write_buffer, invalid_value, NVMEM_PARTITION_SIZE); + TEST_ASSERT_ARRAY_EQ(write_buffer, p_data, NVMEM_PARTITION_SIZE); + + return EC_SUCCESS; } static int test_write_read_sequence(void)