diff mbox series

[2/2] Add option to link bootloader libraries statically

Message ID 20230104151924.3797632-2-sbabic@denx.de
State Accepted
Headers show
Series [1/2] Kconfig: Add HAVE_LIBUBOOTENV and HAVE_LIBEBGENV | expand

Commit Message

Stefano Babic Jan. 4, 2023, 3:19 p.m. UTC
Commit 3a4e626b857 introduced dynamic linking for the bootloader
interface. This helps with standard Linux distros, where SWUpdate is
provided as package and it is not built from sources, and SWUpdate
checks at startup if the chosen library can be loaded. However, this
mechanism just works if dynamic linking is supported, and it fails on
system where dlopen() is not available, like uclibc and musl.

Add an option BOOTLOADER_STATIC_LINKED to reintroduce again static
linking.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 Makefile.flags       | 17 +++++++++++++----
 bootloader/Config.in | 10 ++++++++++
 bootloader/ebg.c     | 13 +++++++++++++
 bootloader/uboot.c   | 12 ++++++++++++
 4 files changed, 48 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/Makefile.flags b/Makefile.flags
index 2bec8468..c44412da 100644
--- a/Makefile.flags
+++ b/Makefile.flags
@@ -209,18 +209,27 @@  ifeq ($(CONFIG_UCFWHANDLER),y)
 LDLIBS += gpiod
 endif
 
+
+ifeq ($(CONFIG_BOOTLOADER_STATIC_LINKED),y)
 ifeq ($(CONFIG_UBOOT),y)
+LDLIBS += ubootenv
+endif
+ifeq ($(CONFIG_BOOTLOADER_EBG),y)
+LDLIBS += ebgenv
+endif
+else
+ifeq ($(CONFIG_UBOOT),y)
+LDLIBS += dl
+endif
+ifeq ($(CONFIG_BOOTLOADER_EBG),y)
 LDLIBS += dl
 endif
+endif
 
 ifeq ($(CONFIG_SYSTEMD),y)
 LDLIBS += systemd
 endif
 
-ifeq ($(CONFIG_BOOTLOADER_EBG),y)
-LDLIBS += dl
-endif
-
 ifeq ($(CONFIG_BOOTLOADER_DEFAULT_NONE),y)
 KBUILD_CPPFLAGS += -DBOOTLOADER_DEFAULT="none"
 else ifeq ($(CONFIG_BOOTLOADER_DEFAULT_GRUB),y)
diff --git a/bootloader/Config.in b/bootloader/Config.in
index ad784c51..82e5b6e5 100644
--- a/bootloader/Config.in
+++ b/bootloader/Config.in
@@ -106,6 +106,16 @@  config BOOTLOADER_DEFAULT_NONE
 
 endchoice
 
+config BOOTLOADER_STATIC_LINKED
+	bool "Bootloader interface static linked"
+	help
+	  Link statically the interface to the bootloader. In case of distros,
+	  more interfaces are available at the same time and SWUpdate checks
+	  at runtime and loads the interface via dlopen(). This is available
+	  with glibc and dlopen support. For other system, the interface can be linked
+	  statically.
+
+
 choice
 	prompt "Update Status Storage"
 	help
diff --git a/bootloader/ebg.c b/bootloader/ebg.c
index a456e1ab..edfc9047 100644
--- a/bootloader/ebg.c
+++ b/bootloader/ebg.c
@@ -518,6 +518,18 @@  static bootloader *probe(void)
 		return NULL;
 	}
 
+#if defined(BOOTLOADER_STATIC_LINKED)
+	libebg.beverbose = ebg_beverbose;
+	libebg.env_create_new  = ebg_env_create_new;
+	libebg.env_open_current  = ebg_env_open_current;
+	libebg.env_get  = ebg_env_get;
+	libebg.env_set  =  ebg_env_set;
+	libebg.env_set_ex  = ebg_env_set_ex;
+	libebg.env_getglobalstate = ebg_env_getglobalstate;
+	libebg.env_setglobalstate  = ebg_env_setglobalstate;
+	libebg.env_close  = ebg_env_close;
+	libebg.env_finalize_update  = ebg_env_finalize_update;
+#else
 	void *handle = dlopen("libebgenv.so.0", RTLD_NOW | RTLD_GLOBAL);
 	if (!handle) {
 		return NULL;
@@ -534,6 +546,7 @@  static bootloader *probe(void)
 	load_symbol(handle, &libebg.env_setglobalstate, "ebg_env_setglobalstate");
 	load_symbol(handle, &libebg.env_close, "ebg_env_close");
 	load_symbol(handle, &libebg.env_finalize_update, "ebg_env_finalize_update");
+#endif
 	return &ebg;
 }
 
diff --git a/bootloader/uboot.c b/bootloader/uboot.c
index 2720b1ea..a2f07948 100644
--- a/bootloader/uboot.c
+++ b/bootloader/uboot.c
@@ -123,6 +123,17 @@  static bootloader uboot = {
 
 static bootloader* probe(void)
 {
+#if defined(BOOTLOADER_STATIC_LINKED)
+	libuboot.open = libuboot_open;
+	libuboot.close = libuboot_close;
+	libuboot.exit = libuboot_exit;
+	libuboot.initialize = libuboot_initialize;
+	libuboot.get_env = libuboot_get_env;
+	libuboot.read_config = libuboot_read_config;
+	libuboot.load_file = libuboot_load_file;
+	libuboot.set_env = libuboot_set_env;
+	libuboot.env_store = libuboot_env_store;
+#else
 	void* handle = dlopen("libubootenv.so.0", RTLD_NOW | RTLD_GLOBAL);
 	if (!handle) {
 		return NULL;
@@ -138,6 +149,7 @@  static bootloader* probe(void)
 	load_symbol(handle, &libuboot.load_file, "libuboot_load_file");
 	load_symbol(handle, &libuboot.set_env, "libuboot_set_env");
 	load_symbol(handle, &libuboot.env_store, "libuboot_env_store");
+#endif
 	return &uboot;
 }