Skip to content
Snippets Groups Projects
0001-Try-to-autoselect-namespace-based-on-DT-property.patch 1.53 KiB
From 434c75d73a065b93f708cf856fb88c5b5f0dd778 Mon Sep 17 00:00:00 2001
From: Frieder Schrempf <frieder.schrempf@kontron.de>
Date: Wed, 16 Aug 2023 11:11:50 +0200
Subject: [libubootenv][PATCH 1/3] Try to autoselect namespace based on DT
 property

The bootloader might be able to tell us where the currently used
environment is located. Use the string from the
"/chosen/u-boot,env-config" devicetree property as namespace
selector if available.

Signed-off-by: Frieder Schrempf <frieder.schrempf@kontron.de>
---
 src/fw_printenv.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/src/fw_printenv.c b/src/fw_printenv.c
index e475e43..6118324 100644
--- a/src/fw_printenv.c
+++ b/src/fw_printenv.c
@@ -87,6 +87,9 @@ int main (int argc, char **argv) {
 	bool noheader = false;
 	bool default_used = false;
 	struct uboot_version_info *version;
+	char dt_namespace[32];
+	size_t dt_ret;
+	FILE *fp;
 
 	/*
 	 * As old tool, there is just a tool with symbolic link
@@ -144,8 +147,22 @@ int main (int argc, char **argv) {
 		fprintf(stderr, "Cannot initialize environment\n");
 		exit(1);
 	}
+
 	if (namespace)
 		ctx = libuboot_get_namespace(ctx, namespace);
+	else {
+		fp = fopen("/proc/device-tree/chosen/u-boot,env-config", "r");
+		if(fp) {
+			dt_ret = fread(dt_namespace, 1, sizeof(dt_namespace) - 1, fp);
+			if (dt_ret) {
+				dt_namespace[dt_ret] = 0;
+				ctx = libuboot_get_namespace(ctx, dt_namespace);
+			}
+
+			fclose(fp);
+		}
+	}
+
 	if (!ctx) {
 		fprintf(stderr, "Namespace %s not found\n", namespace);
 		exit (1);
-- 
2.41.0