From b29d0808e22d30c3e9ce85dd31457ab8ff28dd2c Mon Sep 17 00:00:00 2001 From: Daisuke Nojiri Date: Tue, 3 Oct 2017 18:28:57 -0700 Subject: CBI: Add unit test This patch adds unit tests for Cros Board Info APIs. BUG=b:163038871 BRANCH=none TEST=buildall Signed-off-by: Daisuke Nojiri Change-Id: I7b2fdb2c4f13da12f8c0dc2ab526332cbd46d849 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2339393 --- common/cbi.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'common/cbi.c') diff --git a/common/cbi.c b/common/cbi.c index 5263407441..6918ad50dd 100644 --- a/common/cbi.c +++ b/common/cbi.c @@ -57,12 +57,12 @@ uint8_t *cbi_set_string(uint8_t *p, enum cbi_data_tag tag, const char *str) return cbi_set_data(p, tag, str, strlen(str) + 1); } -struct cbi_data *cbi_find_tag(const void *cbi, enum cbi_data_tag tag) +struct cbi_data *cbi_find_tag(const void *buf, enum cbi_data_tag tag) { struct cbi_data *d; - const struct cbi_header *h = cbi; + const struct cbi_header *h = buf; const uint8_t *p; - for (p = h->data; p + sizeof(*d) < (uint8_t *)cbi + h->total_size;) { + for (p = h->data; p + sizeof(*d) < (uint8_t *)buf + h->total_size;) { d = (struct cbi_data *)p; if (d->tag == tag) return d; @@ -92,6 +92,26 @@ static int cached_read_result = EC_ERROR_CBI_CACHE_INVALID; static uint8_t cbi[CBI_EEPROM_SIZE]; static struct cbi_header * const head = (struct cbi_header *)cbi; +int cbi_create(void) +{ + struct cbi_header * const h = (struct cbi_header *)cbi; + + memset(cbi, 0, sizeof(cbi)); + memcpy(h->magic, cbi_magic, sizeof(cbi_magic)); + h->total_size = sizeof(*h); + h->major_version = CBI_VERSION_MAJOR; + h->minor_version = CBI_VERSION_MINOR; + h->crc = cbi_crc8(h); + cached_read_result = EC_SUCCESS; + + return EC_SUCCESS; +} + +void cbi_invalidate_cache(void) +{ + cached_read_result = EC_ERROR_CBI_CACHE_INVALID; +} + static int read_eeprom(uint8_t offset, uint8_t *in, int in_size) { return i2c_read_block(I2C_PORT_EEPROM, I2C_ADDR_EEPROM_FLAGS, @@ -266,6 +286,11 @@ static int write_board_info(void) return EC_SUCCESS; } +int cbi_write(void) +{ + return write_board_info(); +} + int cbi_get_board_version(uint32_t *ver) { uint8_t size = sizeof(*ver); -- cgit v1.2.1