diff options
-rw-r--r-- | common/cbi.c | 74 | ||||
-rw-r--r-- | include/cros_board_info.h | 35 | ||||
-rw-r--r-- | util/build.mk | 2 | ||||
-rw-r--r-- | util/cbi-util.c | 38 |
4 files changed, 89 insertions, 60 deletions
diff --git a/common/cbi.c b/common/cbi.c index 935861c2a6..be22642497 100644 --- a/common/cbi.c +++ b/common/cbi.c @@ -13,7 +13,52 @@ #include "host_command.h" #include "i2c.h" #include "timer.h" + +#ifdef HOST_TOOLS_BUILD +#include <string.h> +#else #include "util.h" +#endif + +/* + * Functions and variables defined here shared with host tools (e.g. cbi-util). + * TODO: Move these to common/cbi/cbi.c and common/cbi/utils.c if they grow. + */ +uint8_t cbi_crc8(const struct cbi_header *h) +{ + return crc8((uint8_t *)&h->crc + 1, + h->total_size - sizeof(h->magic) - sizeof(h->crc)); +} + +uint8_t *cbi_set_data(uint8_t *p, enum cbi_data_tag tag, + const void *buf, int size) +{ + struct cbi_data *d = (struct cbi_data *)p; + d->tag = tag; + d->size = size; + memcpy(d->value, buf, size); + p += sizeof(*d) + size; + return p; +} + +struct cbi_data *cbi_find_tag(const void *cbi, enum cbi_data_tag tag) +{ + struct cbi_data *d; + const struct cbi_header *h = cbi; + const uint8_t *p; + for (p = h->data; p + sizeof(*d) < (uint8_t *)cbi + h->total_size;) { + d = (struct cbi_data *)p; + if (d->tag == tag) + return d; + p += sizeof(*d) + d->size; + } + return NULL; +} + +/* + * Functions and variables specific to EC firmware + */ +#ifndef HOST_TOOLS_BUILD #define CPRINTS(format, args...) cprints(CC_SYSTEM, "CBI " format, ## args) @@ -25,12 +70,6 @@ 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; -static uint8_t cbi_crc8(const struct cbi_header *h) -{ - return crc8((uint8_t *)&h->crc + 1, - h->total_size - sizeof(h->magic) - sizeof(h->crc)); -} - static int read_eeprom(uint8_t offset, uint8_t *in, int in_size) { return i2c_xfer(I2C_PORT_EEPROM, I2C_ADDR_EEPROM, @@ -99,19 +138,6 @@ static int read_board_info(void) return cached_read_result; } -static struct cbi_data *find_tag(enum cbi_data_tag tag) -{ - struct cbi_data *d; - uint8_t *p; - for (p = head->data; p + sizeof(*d) < cbi + head->total_size;) { - d = (struct cbi_data *)p; - if (d->tag == tag) - return d; - p += sizeof(*d) + d->size; - } - return NULL; -} - int cbi_get_board_info(enum cbi_data_tag tag, uint8_t *buf, uint8_t *size) { const struct cbi_data *d; @@ -119,7 +145,7 @@ int cbi_get_board_info(enum cbi_data_tag tag, uint8_t *buf, uint8_t *size) if (read_board_info()) return EC_ERROR_UNKNOWN; - d = find_tag(tag); + d = cbi_find_tag(cbi, tag); if (!d) /* Not found */ return EC_ERROR_UNKNOWN; @@ -139,16 +165,13 @@ int cbi_set_board_info(enum cbi_data_tag tag, const uint8_t *buf, uint8_t size) { struct cbi_data *d; - d = find_tag(tag); + d = cbi_find_tag(cbi, tag); if (!d) { /* Not found. Check if new item would fit */ if (sizeof(cbi) < head->total_size + sizeof(*d) + size) return EC_ERROR_OVERFLOW; /* Append new item */ - d = (struct cbi_data *)&cbi[head->total_size]; - d->tag = tag; - d->size = size; - memcpy(d->value, buf, d->size); + cbi_set_data(&cbi[head->total_size], tag, buf, size); head->total_size += (sizeof(*d) + size); return EC_SUCCESS; } @@ -331,3 +354,4 @@ static int cc_cbi(int argc, char **argv) return EC_SUCCESS; } DECLARE_CONSOLE_COMMAND(cbi, cc_cbi, NULL, NULL); +#endif /* !HOST_TOOLS_BUILD */ diff --git a/include/cros_board_info.h b/include/cros_board_info.h index eb239f9696..fb667ef899 100644 --- a/include/cros_board_info.h +++ b/include/cros_board_info.h @@ -62,4 +62,39 @@ int cbi_get_oem_id(uint32_t *oem_id); int cbi_get_board_info(enum cbi_data_tag tag, uint8_t *buf, uint8_t *size); int cbi_set_board_info(enum cbi_data_tag tag, const uint8_t *buf, uint8_t size); +/* + * Utility functions + */ + +/** + * Calculate 8-bit CRC of CBI + * + * @param h Pointer to CBI header + * @return CRC value + */ +uint8_t cbi_crc8(const struct cbi_header *h); + +/** + * Store data in memory in CBI data format + * + * @param p Pointer to the buffer where a new data item will be stored. It + * should be pointing to the data section of CBI. + * @param tag Tag of the data item + * @param buf Pointer to the buffer containing the data being copied. + * @param size Size of the data + * @return Address of the byte following the stored data in the + * destination buffer + */ +uint8_t *cbi_set_data(uint8_t *p, enum cbi_data_tag tag, + const void *buf, int size); + +/** + * Find a data field in CBI + * + * @param cbi Buffer containing CBI struct + * @param tag Tag of the data field to search + * @return Pointer to the data or NULL if not found. + */ +struct cbi_data *cbi_find_tag(const void *cbi, enum cbi_data_tag tag); + #endif /* __CROS_EC_CROS_BOARD_INFO_H */ diff --git a/util/build.mk b/util/build.mk index 7a1ab8b766..1879a0a2a5 100644 --- a/util/build.mk +++ b/util/build.mk @@ -57,7 +57,7 @@ $(out)/util/gen_touchpad_hash: BUILD_CFLAGS += $(OPENSSL_CFLAGS) $(out)/util/gen_touchpad_hash: BUILD_LDFLAGS += $(OPENSSL_LDFLAGS) endif # CONFIG_TOUCHPAD_VIRTUAL_OFF -cbi-util-objs=../common/crc8.o +cbi-util-objs=../common/crc8.o ../common/cbi.o $(out)/util/export_taskinfo.so: $(out)/util/export_taskinfo_ro.o \ $(out)/util/export_taskinfo_rw.o diff --git a/util/cbi-util.c b/util/cbi-util.c index c104e54eae..5f8cddc128 100644 --- a/util/cbi-util.c +++ b/util/cbi-util.c @@ -137,22 +137,6 @@ static uint8_t *read_file(const char *filename, uint32_t *size_ptr) return buf; } -static int cbi_crc8(const struct cbi_header *h) -{ - return crc8((uint8_t *)&h->crc + 1, - h->total_size - sizeof(h->magic) - sizeof(h->crc)); -} - -static uint8_t *set_data(uint8_t *p, enum cbi_data_tag tag, void *buf, int size) -{ - struct cbi_data *d = (struct cbi_data *)p; - d->tag = tag; - d->size = size; - memcpy(d->value, buf, size); - p += sizeof(*d) + size; - return p; -} - /* * Create a CBI blob */ @@ -176,10 +160,10 @@ static int do_create(const char *cbi_filename, uint32_t size, uint8_t erase, h->major_version = CBI_VERSION_MAJOR; h->minor_version = CBI_VERSION_MINOR; p = h->data; - p = set_data(p, CBI_TAG_BOARD_VERSION, + p = cbi_set_data(p, CBI_TAG_BOARD_VERSION, &bi->version, sizeof(bi->version)); - p = set_data(p, CBI_TAG_OEM_ID, &bi->oem_id, sizeof(bi->oem_id)); - p = set_data(p, CBI_TAG_SKU_ID, &bi->sku_id, sizeof(bi->sku_id)); + p = cbi_set_data(p, CBI_TAG_OEM_ID, &bi->oem_id, sizeof(bi->oem_id)); + p = cbi_set_data(p, CBI_TAG_SKU_ID, &bi->sku_id, sizeof(bi->sku_id)); h->total_size = p - cbi; h->crc = cbi_crc8(h); @@ -195,24 +179,10 @@ static int do_create(const char *cbi_filename, uint32_t size, uint8_t erase, return 0; } -static struct cbi_data *find_tag(const uint8_t *cbi, enum cbi_data_tag tag) -{ - struct cbi_data *d; - const struct cbi_header *h = (const struct cbi_header *)cbi; - const uint8_t *p; - for (p = h->data; p + sizeof(*d) < cbi + h->total_size;) { - d = (struct cbi_data *)p; - if (d->tag == tag) - return d; - p += sizeof(*d) + d->size; - } - return NULL; -} - static void print_integer(const uint8_t *buf, enum cbi_data_tag tag) { uint32_t v; - struct cbi_data *d = find_tag(buf, tag); + struct cbi_data *d = cbi_find_tag(buf, tag); const char *name = d->tag < CBI_TAG_COUNT ? field_name[d->tag] : "???"; if (!d) |