diff mbox series

[05/12] kbuild: introduce blob framework

Message ID 20250217-kunit-kselftests-v1-5-42b4524c3b0a@linutronix.de (mailing list archive)
State New
Headers show
Series kunit: Introduce UAPI testing framework | expand

Commit Message

Thomas Weißschuh Feb. 17, 2025, 10:59 a.m. UTC
Various subsystems embed non-code build artifacts into the kernel,
for example the initramfs, /proc/config.gz, vDSO image, etc.
Currently each user has their own implementation for that.

Add a common "blob" framework to provide this functionality.
It provides standard kbuild and C APIs to embed and later access non-code build
artifacts into the kernel image or modules.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>

---
This is currently lacking a MAINTAINERS entry.
Due to its closeness to kbuild I would tend to add it there.
But I can also maintain it on its own.
---
 include/linux/blob.h   | 21 +++++++++++++++++++++
 scripts/Makefile.blobs | 19 +++++++++++++++++++
 scripts/Makefile.build |  6 ++++++
 scripts/Makefile.clean |  2 +-
 scripts/blob-wrap.c    | 27 +++++++++++++++++++++++++++
 5 files changed, 74 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/include/linux/blob.h b/include/linux/blob.h
new file mode 100644
index 0000000000000000000000000000000000000000..5749c364f7e9c93eb32a6d8870621e3d6df0683c
--- /dev/null
+++ b/include/linux/blob.h
@@ -0,0 +1,21 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_BLOB_H
+#define _LINUX_BLOB_H
+
+#include <linux/compiler_types.h>
+#include <linux/types.h>
+
+struct blob {
+	const char *const path;
+	const u8 *data;
+	const u8 __private *end;
+};
+
+#define DECLARE_BLOB(_symbol)	extern const struct blob _symbol
+
+static inline size_t blob_size(const struct blob *blob)
+{
+	return ACCESS_PRIVATE(blob, end) - blob->data;
+}
+
+#endif /* _LINUX_BLOB_H */
diff --git a/scripts/Makefile.blobs b/scripts/Makefile.blobs
new file mode 100644
index 0000000000000000000000000000000000000000..fd20ebb41c1d6509750debf7896a08a143d28759
--- /dev/null
+++ b/scripts/Makefile.blobs
@@ -0,0 +1,19 @@ 
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Build linkable blobs
+#
+
+blobs	:= $(addprefix $(obj)/, $(blobs))
+
+blob-stem = $(subst -,_,$(subst .blob,,$(basename $(patsubst $(obj)/%,%,$@))))
+blob-symbol = $(or $($(target-stem)-symbol),$(blob-stem))
+
+blob-flags = -DBLOB_SYMBOL="$(blob-symbol)" -DBLOB_INPUT=$<
+
+quiet_cmd_blob = BLOB    $@
+      cmd_blob = $(CC) $(c_flags) $(blob-flags) -c -o $@ $(srctree)/scripts/blob-wrap.c
+
+$(blobs): $(obj)/%.blob.o: $(obj)/% $(srctree)/scripts/blob-wrap.c FORCE
+	$(call if_changed_dep,blob)
+
+targets += $(blobs)
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 993708d1187459f3678d68ec81ef005e6f51d470..def9791523d718552617ef6ef752c1852d40d996 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -440,6 +440,12 @@  ifneq ($(need-dtbslist)$(dtb-y)$(dtb-)$(filter %.dtb %.dtb.o %.dtbo.o,$(targets)
 include $(srctree)/scripts/Makefile.dtbs
 endif
 
+# $(sort ...) is used here to remove duplicated words and excessive spaces.
+blobs := $(sort $(blobs))
+ifneq ($(blobs),)
+include $(srctree)/scripts/Makefile.blobs
+endif
+
 # Build
 # ---------------------------------------------------------------------------
 
diff --git a/scripts/Makefile.clean b/scripts/Makefile.clean
index 6ead00ec7313b3e4330a8de5f1342f2da1d6eb84..536972b0a528d117e17296da9936825c3903af6e 100644
--- a/scripts/Makefile.clean
+++ b/scripts/Makefile.clean
@@ -25,7 +25,7 @@  subdir-ymn	:= $(addprefix $(obj)/,$(subdir-ymn))
 # directory
 
 __clean-files	:= \
-	$(clean-files) $(targets) $(hostprogs) $(userprogs) \
+	$(clean-files) $(targets) $(hostprogs) $(userprogs) $(blobs) \
 	$(extra-y) $(extra-m) $(extra-) \
 	$(always-y) $(always-m) $(always-) \
 	$(hostprogs-always-y) $(hostprogs-always-m) $(hostprogs-always-) \
diff --git a/scripts/blob-wrap.c b/scripts/blob-wrap.c
new file mode 100644
index 0000000000000000000000000000000000000000..715d6981fa94bee309c693c2b937899c982e27b4
--- /dev/null
+++ b/scripts/blob-wrap.c
@@ -0,0 +1,27 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <linux/args.h>
+#include <linux/blob.h>
+#include <linux/stringify.h>
+
+#define BLOB_SYMBOL_DATA	CONCATENATE(_blob_data_, BLOB_SYMBOL)
+#define BLOB_SYMBOL_END		CONCATENATE(_blob_end_, BLOB_SYMBOL)
+
+asm (
+"	.pushsection .rodata, \"a\"			\n"
+"	.global " __stringify(BLOB_SYMBOL_DATA) "	\n"
+ __stringify(BLOB_SYMBOL_DATA) ":			\n"
+"	.incbin \"" __stringify(BLOB_INPUT) "\"		\n"
+"	.global " __stringify(BLOB_SYMBOL_END) "	\n"
+ __stringify(BLOB_SYMBOL_END) ":			\n"
+"	.popsection					\n"
+);
+
+extern const u8 BLOB_SYMBOL_DATA;
+extern const u8 BLOB_SYMBOL_END;
+
+const struct blob BLOB_SYMBOL = {
+	.path	= __stringify(BLOB_INPUT),
+	.data	= &BLOB_SYMBOL_DATA,
+	.end	= &BLOB_SYMBOL_END,
+};