Skip to contentSkip to conversation

efi_loader: provide library for freestanding binaries

PATCH New Heinrich Schuchardt 2 participants 5 messages Jan 20, 2019 – Jan 21, 2019 Kbuild

lore
Share & tools
lore
#0 lore raw

cover efi_loader: provide library for freestanding binaries

GCC requires that freestanding programs provide memcpy(), memmove(), memset(), and memcmp(), cf.
https://gcc.gnu.org/onlinedocs/gcc/Standards.html

Provide the required library functions.
Link them in all *.efi binaries.

Fixes 5be444d14b38 ("efi_loader: consistent build flags for EFI applications")

Reverting said commit would not satisfy the general requirement of the GCC compiler for freestanding programs.

Tested successfully, cf.
https://travis-ci.org/xypron2/u-boot/builds/481823209

Heinrich Schuchardt (3):
efi_loader: provide freestanding library
efi_loader: use freestanding library for efi apps
efi_loader: use library memcpy() in helloworld.efi

 lib/efi_loader/efi_freestanding.c   | 90 +++++++++++++++++++++++++++++
 lib/efi_loader/helloworld.c         | 36 ++----------
 lib/efi_selftest/efi_freestanding.c |  1 +
 scripts/Makefile.lib                |  2 +-
 4 files changed, 98 insertions(+), 31 deletions(-)
 create mode 100644 lib/efi_loader/efi_freestanding.c
 create mode 120000 lib/efi_selftest/efi_freestanding.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/3 efi_loader: provide freestanding library

GCC requires that freestanding programs provide memcpy(), memmove(), memset(), and memcmp().

Provide the required library functions.

---
 lib/efi_loader/efi_freestanding.c | 90 +++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
 create mode 100644 lib/efi_loader/efi_freestanding.c
lib/efi_loader/efi_freestanding.c new file +90 -0
new file mode 100644
index 0000000000..bd9da5bbc8
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Library for freestanding binary
+ *
+ * Copyright 2019, Heinrich Schuchardt <[email protected]>
+ *
+ * GCC requires that freestanding programs provide memcpy(), memmove(),
+ * memset(), and memcmp().
+ */
+
+#include <common.h>
+
+/**
+ * memcmp() - compare memory areas
+ *
+ * @s1:		pointer to first area
+ * @s2:		pointer to second area
+ * @n:		number of bytes to compare
+ * Return:	0 if both memory areas are the same, otherwise the sign of the
+ *		result value is the same as the sign of the difference between
+ *		the first differing pair of bytes taken as u8.
+ */
+int memcmp(const void *s1, const void *s2, size_t n)
+{
+	const u8 *pos1 = s1;
+	const u8 *pos2 = s2;
+
+	for (; n; --n) {
+		if (*pos1 != *pos2)
+			return *pos1 - *pos2;
+		++pos1;
+		++pos2;
+	}
+	return 0;
+}
+
+/**
+ * memcpy() - copy memory area
+ *
+ * @dest:	destination buffer
+ * @src:	source buffer
+ * @n:		number of bytes to copy
+ * Return:	pointer to destination buffer
+ */
+void *memmove(void *dest, const void *src, size_t n)
+{
+	u8 *d = dest;
+	const u8 *s = src;
+
+	if (d >= s) {
+		for (; n; --n)
+			*d++ = *s++;
+	} else {
+		d += n;
+		s += n;
+		for (; n; --n)
+			*--d = *--s;
+	}
+	return dest;
+}
+
+/**
+ * memcpy() - copy memory area
+ *
+ * @dest:	destination buffer
+ * @src:	source buffer
+ * @n:		number of bytes to copy
+ * Return:	pointer to destination buffer
+ */
+void *memcpy(void *dest, const void *src, size_t n)
+{
+	return memmove(dest, src, n);
+}
+
+/**
+ * memset() - fill memory with a constant byte
+ *
+ * @s:		destination buffer
+ * @c:		byte value
+ * @n:		number of bytes to set
+ * Return:	pointer to destination buffer
+ */
+void *memset(void *s, int c, size_t n)
+{
+	u8 *d = s;
+
+	for (; n; --n)
+		*d++ = c;
+	return s;
+}
Signature · 1 line
2.20.1
#2 lore raw

PATCH 2/3 efi_loader: use freestanding library for efi apps

In reply to Heinrich Schuchardt

GCC requires that freestanding programs provide memcpy(), memmove(), memset(), and memcmp().

Add the library functions when building a *.efi files.

The EFI selftests might use other compilation flags. So use a symbolic link to provide lib/efi_selftest/efi_freestanding.c and compile it separately.

Fixes 5be444d14b38 ("efi_loader: consistent build flags for EFI applications")
---
 lib/efi_selftest/efi_freestanding.c | 1 +
 scripts/Makefile.lib                | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
 create mode 120000 lib/efi_selftest/efi_freestanding.c
lib/efi_selftest/efi_freestanding.c new file +1 -0
new file mode 120000
index 0000000000..4b7edd52bd
@@ -0,0 +1 @@
+../efi_loader/efi_freestanding.c
\ No newline at end of file
scripts/Makefile.lib +1 -1
index a5b57fc6b9..4facb76ace 100644
@@ -389,7 +389,7 @@ $(obj)/efi_reloc.o: $(srctree)/arch/$(ARCH)/lib/$(EFI_RELOC:.o=.c) $(recordmcoun
 	$(call cmd,force_checksrc)
 	$(call if_changed_rule,cc_o_c)
 
-$(obj)/%_efi.so: $(obj)/%.o $(obj)/efi_crt0.o $(obj)/efi_reloc.o
+$(obj)/%_efi.so: $(obj)/%.o $(obj)/efi_crt0.o $(obj)/efi_reloc.o $(obj)/efi_freestanding.o
 	$(call cmd,efi_ld)
 
 # ACPI
Signature · 1 line
2.20.1
#3 lore raw

PATCH 2/3 efi_loader: use freestanding library for efi apps

Quoted 24 lines · Heinrich Schuchardt

GCC requires that freestanding programs provide memcpy(), memmove(), memset(), and memcmp().

Add the library functions when building a *.efi files.

The EFI selftests might use other compilation flags. So use a symbolic link to provide lib/efi_selftest/efi_freestanding.c and compile it separately.

Fixes 5be444d14b38 ("efi_loader: consistent build flags for EFI applications")
---
  lib/efi_selftest/efi_freestanding.c | 1 +
  scripts/Makefile.lib                | 2 +-
  2 files changed, 2 insertions(+), 1 deletion(-)

create mode 120000 lib/efi_selftest/efi_freestanding.c

lib/efi_selftest/efi_freestanding.c new file +0 -0
new file mode 120000
index 0000000000..4b7edd52bd

lib/efi_selftest/efi_freestanding.c line 1

Quoted 2 lines
lib/efi_selftest/efi_freestanding.c new file +1 -0
@@ -0,0 +1 @@
+../efi_loader/efi_freestanding.c
\ No newline at end of file

Please try to refrain from using symlinks. I will apply the patch regardless, but if you can think of a better way to model this, I'm all for it.

Thanks,

Alex

#4 lore raw

PATCH 3/3 efi_loader: use library memcpy() in helloworld.efi

In reply to Heinrich Schuchardt

Helloworld does not need its own memcpy() implementation anymore. Use the one provided in efi_freestanding.c.

---
 lib/efi_loader/helloworld.c | 36 ++++++------------------------------
 1 file changed, 6 insertions(+), 30 deletions(-)
lib/efi_loader/helloworld.c +6 -30
index 2905479e65..426f276361 100644
@@ -17,30 +17,6 @@ static const efi_guid_t fdt_guid = EFI_FDT_GUID;
 static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID;
 static const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
 
-/**
- * hw_memcmp() - compare memory areas
- *
- * @buf1:	pointer to first area
- * @buf2:	pointer to second area
- * @length:	number of bytes to compare
- * Return:	0 if both memory areas are the same, otherwise the sign of the
- *		result value is the same as the sign of ghe difference between
- *		the first differing pair of bytes taken as u8.
- */
-static int hw_memcmp(const void *buf1, const void *buf2, size_t length)
-{
-	const u8 *pos1 = buf1;
-	const u8 *pos2 = buf2;
-
-	for (; length; --length) {
-		if (*pos1 != *pos2)
-			return *pos1 - *pos2;
-		++pos1;
-		++pos2;
-	}
-	return 0;
-}
-
 /**
  * efi_main() - entry point of the EFI application.
  *
@@ -88,16 +64,16 @@ efi_status_t EFIAPI efi_main(efi_handle_t handle,
 	}
 	/* Find configuration tables */
 	for (i = 0; i < systable->nr_tables; ++i) {
-		if (!hw_memcmp(&systable->tables[i].guid, &fdt_guid,
-			       sizeof(efi_guid_t)))
+		if (!memcmp(&systable->tables[i].guid, &fdt_guid,
+			    sizeof(efi_guid_t)))
 			con_out->output_string
 					(con_out, L"Have device tree\r\n");
-		if (!hw_memcmp(&systable->tables[i].guid, &acpi_guid,
-			       sizeof(efi_guid_t)))
+		if (!memcmp(&systable->tables[i].guid, &acpi_guid,
+			    sizeof(efi_guid_t)))
 			con_out->output_string
 					(con_out, L"Have ACPI 2.0 table\r\n");
-		if (!hw_memcmp(&systable->tables[i].guid, &smbios_guid,
-			       sizeof(efi_guid_t)))
+		if (!memcmp(&systable->tables[i].guid, &smbios_guid,
+			    sizeof(efi_guid_t)))
 			con_out->output_string
 					(con_out, L"Have SMBIOS table\r\n");
 	}
Signature · 1 line
2.20.1