summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFurquan Shaikh <furquan@google.com>2019-03-06 14:44:37 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-03-08 21:20:36 -0800
commitc67a566dbcca552b3d7cea57832685c2a83033d6 (patch)
treedc358e43acf0bc4e9ccf7b1df220f6897caf406f
parent03e1bfd9a375d52236323978e151ed6fdc68a59a (diff)
downloadchrome-ec-c67a566dbcca552b3d7cea57832685c2a83033d6.tar.gz
cbi: Add a new API for cbi_set_string
This change introduces cbi_set_string that can be used by callers to add string data to CBI. Internally, this function ensures that the data is not NULL and call cbi_set_data for the string with a size of strlen + 1. It is added so that callers don't have to repeat the same operations for string data. BUG=b:124282048 BRANCH=None TEST=None Change-Id: Iad9da206c5e87735eac3adee6fce151fd1f0a53a Signed-off-by: Furquan Shaikh <furquan@google.com> Reviewed-on: https://chromium-review.googlesource.com/1506440 Commit-Ready: Furquan Shaikh <furquan@chromium.org> Tested-by: Furquan Shaikh <furquan@chromium.org> Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Jett Rink <jettrink@chromium.org>
-rw-r--r--common/cbi.c8
-rw-r--r--include/cros_board_info.h16
-rw-r--r--util/cbi-util.c10
3 files changed, 26 insertions, 8 deletions
diff --git a/common/cbi.c b/common/cbi.c
index 8517506eec..cf4605288a 100644
--- a/common/cbi.c
+++ b/common/cbi.c
@@ -49,6 +49,14 @@ uint8_t *cbi_set_data(uint8_t *p, enum cbi_data_tag tag,
return p;
}
+uint8_t *cbi_set_string(uint8_t *p, enum cbi_data_tag tag, const char *str)
+{
+ if (str == NULL)
+ return p;
+
+ 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 *d;
diff --git a/include/cros_board_info.h b/include/cros_board_info.h
index 8ed8737731..e7a1f1bb48 100644
--- a/include/cros_board_info.h
+++ b/include/cros_board_info.h
@@ -90,6 +90,22 @@ uint8_t *cbi_set_data(uint8_t *p, enum cbi_data_tag tag,
const void *buf, int size);
/**
+ * Store string 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 str Pointer to the string data being copied. If pointer is NULL,
+ * this function will ignore adding the tag as well. Else, the
+ * string data will be added to CBI using size of strlen + 1. This
+ * string is assumed to be NUL-terminated and NUL gets stored in
+ * CBI along with the string data.
+ * @return Address of the byte following the stored data in the destination
+ * buffer.
+ */
+uint8_t *cbi_set_string(uint8_t *p, enum cbi_data_tag tag, const char *str);
+
+/**
* Find a data field in CBI
*
* @param cbi Buffer containing CBI struct
diff --git a/util/cbi-util.c b/util/cbi-util.c
index 2ea86b14cf..f8abb4e64a 100644
--- a/util/cbi-util.c
+++ b/util/cbi-util.c
@@ -340,14 +340,8 @@ static int cmd_create(int argc, char **argv)
p = cbi_set_data(p, CBI_TAG_OEM_ID, &bi.oem.val, bi.oem.size);
p = cbi_set_data(p, CBI_TAG_SKU_ID, &bi.sku.val, bi.sku.size);
p = cbi_set_data(p, CBI_TAG_MODEL_ID, &bi.model.val, bi.model.size);
- if (bi.dram_part_num != NULL) {
- p = cbi_set_data(p, CBI_TAG_DRAM_PART_NUM, bi.dram_part_num,
- strlen(bi.dram_part_num) + 1);
- }
- if (bi.oem_name != NULL) {
- p = cbi_set_data(p, CBI_TAG_OEM_NAME, bi.oem_name,
- strlen(bi.oem_name) + 1);
- }
+ p = cbi_set_string(p, CBI_TAG_DRAM_PART_NUM, bi.dram_part_num);
+ p = cbi_set_string(p, CBI_TAG_OEM_NAME, bi.oem_name);
h->total_size = p - cbi;
h->crc = cbi_crc8(h);