[Concept,10/15] ulib: Provide a generic init function
Commit Message
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
new file mode 100644
@@ -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
@@ -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
#
new file mode 100644
@@ -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;
+}
@@ -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;