summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaisuke Nojiri <dnojiri@chromium.org>2018-02-15 11:28:47 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-02-20 18:16:35 -0800
commitf483d46b01992dfbaf274aaef4e382ca0091e407 (patch)
tree164aa8a3caa77e151b19836cba9032888512f8d4
parentd173cf4d6cfb01e5c559e2df8e978744ec7cb028 (diff)
downloadchrome-ec-f483d46b01992dfbaf274aaef4e382ca0091e407.tar.gz
CBI: Update cbi-util printing format
This patch makes cbi-util print the tag and the size of each field. It also fixes help message and adds indendation for readability. BUG=b:70294260 BRANCH=none TEST=Run the command as follows: $ cbi-util --show /tmp/cbi.bin CBI blob: /tmp/cbi2.bin TOTAL_SIZE: 18 CBI_VERSION: 0 Data Field: name: value (hex, tag, size) BOARD_VERSION: 514 (0x202, 0, 2) OEM_ID: 2 (0x2, 1, 1) SKU_ID: 3 (0x3, 2, 1) Data validated successfully Change-Id: I5f0fde4690c29c0ee58c798e8cc35bac3ed1b6f8 Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/926781 Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r--util/cbi-util.c55
1 files changed, 41 insertions, 14 deletions
diff --git a/util/cbi-util.c b/util/cbi-util.c
index 33f6203f56..c104e54eae 100644
--- a/util/cbi-util.c
+++ b/util/cbi-util.c
@@ -5,6 +5,7 @@
* Cros Board Info utility
*/
+#include <compile_time_macros.h>
#include <errno.h>
#include <dirent.h>
#include <getopt.h>
@@ -65,6 +66,14 @@ static const struct option long_opts[] = {
{NULL, 0, 0, 0}
};
+static const char *field_name[] = {
+ /* Same order as enum cbi_data_tag */
+ "BOARD_VERSION",
+ "OEM_ID",
+ "SKU_ID",
+};
+BUILD_ASSERT(ARRAY_SIZE(field_name) == CBI_TAG_COUNT);
+
static int write_file(const char *filename, const char *buf, int size)
{
FILE *f;
@@ -200,12 +209,38 @@ static struct cbi_data *find_tag(const uint8_t *cbi, enum cbi_data_tag tag)
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);
+ const char *name = d->tag < CBI_TAG_COUNT ? field_name[d->tag] : "???";
+
+ if (!d)
+ return;
+
+ switch (d->size) {
+ case 1:
+ v = *(uint8_t *)d->value;
+ break;
+ case 2:
+ v = *(uint16_t *)d->value;
+ break;
+ case 4:
+ v = *(uint32_t *)d->value;
+ break;
+ default:
+ printf(" %s: Integer of size %d not supported\n",
+ name, d->size);
+ return;
+ }
+ printf(" %s: %u (0x%x, %u, %u)\n", name, v, v, d->tag, d->size);
+}
+
static int do_show(const char *cbi_filename, int show_all)
{
uint8_t *buf;
uint32_t size;
struct cbi_header *h;
- struct cbi_data *d;
if (!cbi_filename) {
fprintf(stderr, "Missing arguments\n");
@@ -233,18 +268,10 @@ static int do_show(const char *cbi_filename, int show_all)
printf(" TOTAL_SIZE: %u\n", h->total_size);
printf(" CBI_VERSION: %u\n", h->version);
- d = find_tag(buf, CBI_TAG_BOARD_VERSION);
- if (d)
- printf(" BOARD_VERSION: %u (0x%x)\n",
- *(uint16_t *)d->value, *(uint16_t *)d->value);
- d = find_tag(buf, CBI_TAG_OEM_ID);
- if (d)
- printf(" OEM_ID: %u (0x%x)\n",
- *(uint8_t *)d->value, *(uint8_t *)d->value);
- d = find_tag(buf, CBI_TAG_SKU_ID);
- if (d)
- printf(" SKU_ID: %u (0x%x)\n",
- *(uint8_t *)d->value, *(uint8_t *)d->value);
+ printf(" Data Field: name: value (hex, tag, size)\n");
+ print_integer(buf, CBI_TAG_BOARD_VERSION);
+ print_integer(buf, CBI_TAG_OEM_ID);
+ print_integer(buf, CBI_TAG_SKU_ID);
printf("Data validated successfully\n");
return 0;
@@ -253,7 +280,7 @@ static int do_show(const char *cbi_filename, int show_all)
/* Print help and return error */
static void print_help(int argc, char *argv[])
{
- printf("\nUsage: cbi %s <--create|--show>\n"
+ printf("\nUsage: %s <--create|--show>\n"
"\n"
"Utility for managing Cros Board Info (CBIs).\n"
"\n"