From c00ffef47c342cc65a5fed11ba2adc0d28da7129 Mon Sep 17 00:00:00 2001 From: Nico Huber Date: Sun, 27 Sep 2020 16:21:22 +0200 Subject: vpd: Add vpd_get_int() function Change-Id: I1c1b5710a5236fe4a3bdda1fc978393e636e9817 Signed-off-by: Nico Huber Reviewed-on: https://review.coreboot.org/c/coreboot/+/45773 Reviewed-by: Angel Pons Reviewed-by: Julius Werner Tested-by: build bot (Jenkins) --- src/drivers/vpd/vpd.c | 23 +++++++++++++++++++++++ src/drivers/vpd/vpd.h | 9 +++++++++ 2 files changed, 32 insertions(+) (limited to 'src') diff --git a/src/drivers/vpd/vpd.c b/src/drivers/vpd/vpd.c index c332a6e94c..d3ff37019d 100644 --- a/src/drivers/vpd/vpd.c +++ b/src/drivers/vpd/vpd.c @@ -3,10 +3,12 @@ #include #include #include +#include #include #include #include #include +#include #include "vpd.h" #include "vpd_decode.h" @@ -274,4 +276,25 @@ bool vpd_get_bool(const char *key, enum vpd_region region, uint8_t *val) return false; } +/* + * Find value of integer type by vpd key. + * + * Expects to find a decimal string, trailing chars are ignored. + * Returns true if the key is found and the value is not too long and + * starts with a decimal digit. Leaves `val` untouched if unsuccessful. + */ +bool vpd_get_int(const char *const key, const enum vpd_region region, int *const val) +{ + char value[11]; + + if (!vpd_gets(key, value, sizeof(value), region)) + return false; + + if (!isdigit(*value)) + return false; + + *val = (int)atol(value); + return true; +} + ROMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd) diff --git a/src/drivers/vpd/vpd.h b/src/drivers/vpd/vpd.h index 25e0aed4ee..817867aba4 100644 --- a/src/drivers/vpd/vpd.h +++ b/src/drivers/vpd/vpd.h @@ -50,4 +50,13 @@ const void *vpd_find(const char *key, int *size, enum vpd_region region); bool vpd_get_bool(const char *key, enum vpd_region region, uint8_t *val); +/* + * Find value of integer type by vpd key. + * + * Expects to find a decimal string, trailing chars are ignored. + * Returns true if the key is found and the value is not too long and + * starts with a decimal digit. + */ +bool vpd_get_int(const char *key, enum vpd_region region, int *val); + #endif /* __VPD_H__ */ -- cgit v1.2.3