summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJett Rink <jettrink@chromium.org>2018-09-19 17:43:22 -0600
committerchrome-bot <chrome-bot@chromium.org>2018-09-20 21:30:16 -0700
commit4a237232c27c18d5367403d743d523509570e5cd (patch)
tree1f5581bd51e0ca9a0a4194a351c9899ff04df7a6
parent48f662d5ba1928c166fc4befe58165a471ebcc60 (diff)
downloadchrome-ec-4a237232c27c18d5367403d743d523509570e5cd.tar.gz
cbi: allow fields to be resized
Since we now have a dynamically sized CBI field (DRAM), it is more convenient to be able to resize fields. We accomplish this by deleting the old field and adding the field at the end, after copying all of the data forward. BRANCH=none BUG=b:116075074 TEST=Updated various CBI fields and watch them move to the end. tested updating the last field and middle fields Change-Id: Icb9b3cb36445d8e78315f9fd3a74483ff2d52ebf Signed-off-by: Jett Rink <jettrink@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1234747 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
-rw-r--r--common/cbi.c28
1 files changed, 22 insertions, 6 deletions
diff --git a/common/cbi.c b/common/cbi.c
index 3fc199bc67..f37d6950b1 100644
--- a/common/cbi.c
+++ b/common/cbi.c
@@ -161,11 +161,29 @@ int cbi_get_board_info(enum cbi_data_tag tag, uint8_t *buf, uint8_t *size)
return EC_SUCCESS;
}
+static void cbi_remove_tag(void *const cbi, struct cbi_data *const d)
+{
+ struct cbi_header *const h = cbi;
+ const size_t size = sizeof(*d) + d->size;
+ const uint8_t *next = (uint8_t *)d + size;
+ const size_t bytes_after = ((uint8_t *)cbi + h->total_size) - next;
+
+ memmove(d, next, bytes_after);
+ h->total_size -= size;
+}
+
int cbi_set_board_info(enum cbi_data_tag tag, const uint8_t *buf, uint8_t size)
{
struct cbi_data *d;
d = cbi_find_tag(cbi, tag);
+
+ /* If we found the entry, but the size doesn't match, delete it */
+ if (d && d->size != size) {
+ cbi_remove_tag(cbi, d);
+ d = NULL;
+ }
+
if (!d) {
/* Not found. Check if new item would fit */
if (sizeof(cbi) < head->total_size + sizeof(*d) + size)
@@ -173,13 +191,11 @@ int cbi_set_board_info(enum cbi_data_tag tag, const uint8_t *buf, uint8_t size)
/* Append new item */
cbi_set_data(&cbi[head->total_size], tag, buf, size);
head->total_size += (sizeof(*d) + size);
- return EC_SUCCESS;
+ } else {
+ /* Overwrite existing item */
+ memcpy(d->value, buf, d->size);
}
- /* No expand or shrink. Items are tightly packed. */
- if (d->size != size)
- return EC_ERROR_INVAL;
- /* Overwrite existing item */
- memcpy(d->value, buf, d->size);
+
return EC_SUCCESS;
}