[Concept,10/15] ulib: Provide a generic init function

Message ID 20250905170132.182249-11-sjg@u-boot.org
State New
Headers
Series ulib: Provide test programs and documentation |

Commit Message

Simon Glass Sept. 5, 2025, 5:01 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Provide an arch-neutral init function that can be used by including a
single header file.

This declares a static global_data which is used for the initial startup
process. Once board_init_r() is called, the global_data is moved into
a new place and the static version is not needed.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 include/u-boot-lib.h  | 34 ++++++++++++++++++++++++++++++++++
 lib/Makefile          |  2 ++
 lib/ulib.c            | 36 ++++++++++++++++++++++++++++++++++++
 test/ulib/ulib_test.c |  8 ++------
 4 files changed, 74 insertions(+), 6 deletions(-)
 create mode 100644 include/u-boot-lib.h
 create mode 100644 lib/ulib.c
  

Patch

diff --git a/include/u-boot-lib.h b/include/u-boot-lib.h
new file mode 100644
index 00000000000..7157ef6ba60
--- /dev/null
+++ b/include/u-boot-lib.h
@@ -0,0 +1,34 @@ 
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * U-Boot library interface
+ *
+ * This provides basic access to setup of the U-Boot library.
+ *
+ * Library functions must be individually accessed via their respective headers.
+ *
+ * Copyright 2025 Canonical
+ * Written by Simon Glass <simon.glass@canonical.com>
+ */
+
+#ifndef __U_BOOT_LIB_H
+#define __U_BOOT_LIB_H
+
+struct global_data;
+
+/**
+ * ulib_init() - set up the U-Boot library
+ *
+ * @progname: Program name to use (must be a writeable string, typically argv[0])
+ * @data: Global data (must remain valid until the program exits)
+ * Return: 0 if OK, -ve error code on error
+ */
+int ulib_init(char *progname);
+
+/**
+ * ulib_uninit() shut down the U-Boot librrary
+ *
+ * Call this when your program has finished using the library, before it exits
+ */
+void ulib_uninit(void);
+
+#endif
diff --git a/lib/Makefile b/lib/Makefile
index 1db1e3c9000..eb6da6d63c9 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -166,6 +166,8 @@  obj-$(CONFIG_LIB_ELF) += elf.o
 
 obj-$(CONFIG_$(PHASE_)SEMIHOSTING) += semihosting.o
 
+obj-$(CONFIG_ULIB) += ulib.o
+
 #
 # Build a fast OID lookup registry from include/linux/oid_registry.h
 #
diff --git a/lib/ulib.c b/lib/ulib.c
new file mode 100644
index 00000000000..03acbe93fd1
--- /dev/null
+++ b/lib/ulib.c
@@ -0,0 +1,36 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Simplified U-Boot library interface implementation
+ *
+ * Copyright 2025 Canonical
+ * Written by Simon Glass <simon.glass@canonical.com>
+ */
+
+#include <string.h>
+#include <version.h>
+#include <asm/global_data.h>
+#include <u-boot-lib.h>
+
+/* Static storage for global data when using simplified API */
+static struct global_data static_gd;
+
+int ulib_init(char *progname)
+{
+	int ret;
+
+	/* Initialize the U-Boot library with our static global data */
+	ret = ulib_init_with_data(progname, &static_gd);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+void ulib_uninit(void)
+{
+}
+
+const char *ulib_get_version(void)
+{
+	return PLAIN_VERSION;
+}
diff --git a/test/ulib/ulib_test.c b/test/ulib/ulib_test.c
index e1e863712a8..a68290ff484 100644
--- a/test/ulib/ulib_test.c
+++ b/test/ulib/ulib_test.c
@@ -11,19 +11,15 @@ 
 /* Use system headers, not U-Boot headers */
 #include <stdio.h>
 #include <string.h>
-#include <init.h>
-#include <asm/global_data.h>
-
-DECLARE_GLOBAL_DATA_PTR;
+#include <u-boot-lib.h>
 
 int main(int argc, char *argv[])
 {
-	struct global_data data;
 	int ret;
 
 	printf("Calling U-Boot initialization via shared library...\n");
 
-	ret = ulib_init_with_data(argv[0], &data);
+	ret = ulib_init(argv[0]);
 	if (ret)
 		return 1;