summaryrefslogtreecommitdiff
path: root/common/nvmem.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/nvmem.c')
-rw-r--r--common/nvmem.c29
1 files changed, 16 insertions, 13 deletions
diff --git a/common/nvmem.c b/common/nvmem.c
index 3358ad5b3a..3251ed8e94 100644
--- a/common/nvmem.c
+++ b/common/nvmem.c
@@ -48,6 +48,18 @@ static int nvmem_error_state;
/* Flag to track if an Nv write/move is not completed */
static int nvmem_write_error;
+/*
+ * Given the nvmem tag address calculate the sha value of the nvmem buffer and
+ * save it in the provided space. The caller is expected to provide enough
+ * space to store CIPHER_SALT_SIZE bytes.
+ */
+static void nvmem_compute_sha(struct nvmem_tag *tag, void *sha_buf)
+{
+ app_compute_hash(&tag->generation,
+ NVMEM_PARTITION_SIZE - NVMEM_SHA_SIZE,
+ sha_buf, sizeof(tag->sha));
+}
+
static int nvmem_save(uint8_t tag_generation, size_t partition)
{
struct nvmem_tag *tag;
@@ -67,11 +79,7 @@ static int nvmem_save(uint8_t tag_generation, size_t partition)
tag->generation = tag_generation;
/* Calculate sha of the whole thing. */
- nvmem_compute_sha(&tag->generation,
- NVMEM_PARTITION_SIZE -
- offsetof(struct nvmem_tag, generation),
- tag->sha,
- sizeof(tag->sha));
+ nvmem_compute_sha(tag, tag->sha);
/* Write partition */
if (flash_physical_write(nvmem_offset,
@@ -90,9 +98,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.generation,
- (NVMEM_PARTITION_SIZE - NVMEM_SHA_SIZE),
- sha_comp, sizeof(sha_comp));
+ nvmem_compute_sha(&p_part->tag, sha_comp);
/* Check if computed value matches stored value. */
return !memcmp(p_part->tag.sha, sha_comp, NVMEM_SHA_SIZE);
@@ -342,11 +348,8 @@ int nvmem_setup(uint8_t starting_generation)
/* 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 -
- NVMEM_SHA_SIZE,
- p_part->tag.sha,
- NVMEM_SHA_SIZE);
+ nvmem_compute_sha(&p_part->tag, p_part->tag.sha);
+
/* Partition is now ready, write it to flash. */
ret = nvmem_commit();
if (ret != EC_SUCCESS)