[PATCH 2/2] env: export getenv_ullx() helper
Ahmad Fatoum
a.fatoum at pengutronix.de
Wed Feb 28 09:51:28 PST 2024
getenv_ull() will parse values according to prefix:
- If it starts with 0x, it's hexadecimal
- If it's just 0, it's octal
- otherwise, it's decimal
Some variables like i.MX8M soc0.soc_uid are hexadecimal without leading
0x. Therefore add a getenv_ullx helper, so code that used to do:
uid = imx8m_uid();
can be replaced with
getenv_ullx("soc0.soc_uid", &uid);
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
common/env.c | 15 +++++++++++++--
include/environment.h | 6 ++++++
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/common/env.c b/common/env.c
index d673b061ab7a..b68e998e46b1 100644
--- a/common/env.c
+++ b/common/env.c
@@ -343,17 +343,28 @@ const char *getenv_nonempty(const char *var)
}
EXPORT_SYMBOL(getenv_nonempty);
-int getenv_ull(const char *var , unsigned long long *val)
+static int getenv_ull_base(const char *var, int base, unsigned long long *val)
{
const char *valstr = getenv(var);
if (!valstr || !*valstr)
return -EINVAL;
- *val = simple_strtoull(valstr, NULL, 0);
+ *val = simple_strtoull(valstr, NULL, base);
return 0;
}
+
+int getenv_ull(const char *var , unsigned long long *val)
+{
+ return getenv_ull_base(var, 0, val);
+}
+EXPORT_SYMBOL(getenv_ull);
+
+int getenv_ullx(const char *var , unsigned long long *val)
+{
+ return getenv_ull_base(var, 16, val);
+}
EXPORT_SYMBOL(getenv_ull);
int getenv_ul(const char *var , unsigned long *val)
diff --git a/include/environment.h b/include/environment.h
index 1557c3a1d730..8b143c16b7ad 100644
--- a/include/environment.h
+++ b/include/environment.h
@@ -35,6 +35,7 @@ int setenv(const char *, const char *);
int pr_setenv(const char *, const char *fmt, ...) __attribute__ ((format(__printf__, 2, 3)));
void export_env_ull(const char *name, unsigned long long val);
int getenv_ull(const char *name, unsigned long long *val);
+int getenv_ullx(const char *name, unsigned long long *val);
int getenv_ul(const char *name, unsigned long *val);
int getenv_uint(const char *name, unsigned int *val);
int getenv_bool(const char *var, int *val);
@@ -63,6 +64,11 @@ static inline int getenv_ull(const char *name, unsigned long long *val)
return -EINVAL;
}
+static inline int getenv_ullx(const char *name, unsigned long long *val)
+{
+ return -EINVAL;
+}
+
static inline int getenv_ul(const char *name, unsigned long *val)
{
return -EINVAL;
--
2.39.2
More information about the barebox
mailing list