:: commit c60c33ee4826a632b8d8c39d39406d4eba4cc272

48cf <32851089+48cf@users.noreply.github.com> — 2023-11-27 14:23

parents: 016dd8d713

build: Add libfdt

diff --git a/.gitignore b/.gitignore
index 0bb5453e..68457a92 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,7 @@
 /freestanding-headers
 /libgcc-binaries
 /common/flanterm
+/common/libfdt
 /common/stb/stb_image.h
 /decompressor/tinf
 /ovmf*
diff --git a/GNUmakefile.in b/GNUmakefile.in
index bf2f1550..b8a87218 100644
--- a/GNUmakefile.in
+++ b/GNUmakefile.in
@@ -328,7 +328,7 @@ distclean: clean
 
 .PHONY: maintainer-clean
 maintainer-clean: distclean
-	cd '$(call SHESCAPE,$(SRCDIR))' && rm -rf common/flanterm common/stb/stb_image.h decompressor/tinf freestanding-headers libgcc-binaries limine-efi freestanding-toolchain configure INSTALL build-aux *'~' autom4te.cache aclocal.m4 *.tar.xz *.tar.gz
+	cd '$(call SHESCAPE,$(SRCDIR))' && rm -rf common/flanterm common/libfdt common/stb/stb_image.h decompressor/tinf freestanding-headers libgcc-binaries limine-efi freestanding-toolchain configure INSTALL build-aux *'~' autom4te.cache aclocal.m4 *.tar.xz *.tar.gz
 
 .PHONY: common-uefi-x86-64
 common-uefi-x86-64:
diff --git a/README.md b/README.md
index 2574961e..4576d826 100644
--- a/README.md
+++ b/README.md
@@ -246,6 +246,8 @@ Limine uses a stripped-down version of [tinf](https://github.com/jibsen/tinf) fo
 
 Limine relies on [stb_image](https://github.com/nothings/stb/blob/dev/stb_image.h) for runtime GZIP decompression and image loading.
 
+Limine uses a patched version of libfdt (can be found in Linux's source tree) for manipulating FDTs.
+
 ## Discord server
 We have a [Discord server](https://discord.gg/QEeZMz4) if you need support,
 info, or you just want to hang out with us.
diff --git a/bootstrap b/bootstrap
index 7cfc838c..710142e2 100755
--- a/bootstrap
+++ b/bootstrap
@@ -12,6 +12,15 @@ if [ -z "$BOOTSTRAP_NO_SHALLOW_CLONES" ]; then
 fi
 
 [ -d common/flanterm ] || git clone https://github.com/mintsuki/flanterm.git common/flanterm $SHALLOW_CLONE_FLAG
+[ -d common/libfdt ] || (
+    set -e
+    curl -Lo dtc-1.7.0.tar.xz https://mirrors.edge.kernel.org/pub/software/utils/dtc/dtc-1.7.0.tar.xz
+    tar -xf dtc-1.7.0.tar.xz
+    mv dtc-1.7.0/libfdt/ common/libfdt
+    rm -rf dtc-1.7.0 dtc-1.7.0.tar.xz
+    find common/libfdt/ -type f -not -name '*.c' -not -name '*.h' -delete
+    patch -p1 < common/libfdt.patch
+)
 [ -f common/stb/stb_image.h ] || ( curl -Lo common/stb/stb_image.h https://github.com/nothings/stb/raw/dev/stb_image.h && patch -p0 < common/stb_image.patch )
 [ -d decompressor/tinf ] || (
     set -e
diff --git a/common/lib/libc.c b/common/lib/libc.c
new file mode 100644
index 00000000..1a1effda
--- /dev/null
+++ b/common/lib/libc.c
@@ -0,0 +1,45 @@
+#include <stddef.h>
+#include <stdint.h>
+#include <lib/libc.h>
+
+void *memchr(const void *ptr, int ch, size_t n) {
+    uint8_t *p = (uint8_t *)ptr;
+
+    for (size_t i = 0; i < n; i++) {
+        if (p[i] == ch) {
+            return (void *)ptr + i;
+        }
+    }
+
+    return NULL;
+}
+
+char *strchr(const char *str, int ch) {
+    for (size_t i = 0; str[i]; i++) {
+        if (str[i] == ch) {
+            return (char *)str + i;
+        }
+    }
+
+    return NULL;
+}
+
+char *strrchr(const char *str, int ch) {
+    char *p = NULL;
+
+    for (size_t i = 0; str[i]; i++) {
+        if (str[i] == ch) {
+            p = (char *)str + i;
+        }
+    }
+
+    return p;
+}
+
+size_t strnlen(const char *str, size_t maxlen) {
+    size_t len;
+
+    for (len = 0; len < maxlen && str[len]; len++);
+
+    return len;
+}
diff --git a/common/lib/libc.h b/common/lib/libc.h
index 551d91a1..7e60d141 100644
--- a/common/lib/libc.h
+++ b/common/lib/libc.h
@@ -18,10 +18,14 @@ void *memset(void *, int, size_t);
 void *memcpy(void *, const void *, size_t);
 int memcmp(const void *, const void *, size_t);
 void *memmove(void *, const void *, size_t);
+void *memchr(const void *, int, size_t);
 
 char *strcpy(char *, const char *);
 char *strncpy(char *, const char *, size_t);
+char *strchr(const char *, int);
+char *strrchr(const char *, int);
 size_t strlen(const char *);
+size_t strnlen(const char *, size_t);
 int strcmp(const char *, const char *);
 int strcasecmp(const char *, const char *);
 int strncmp(const char *, const char *, size_t);
diff --git a/common/libfdt.patch b/common/libfdt.patch
new file mode 100644
index 00000000..94011261
--- /dev/null
+++ b/common/libfdt.patch
@@ -0,0 +1,177 @@
+--- a/common/libfdt/fdt.c	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/fdt.c	2023-11-27 14:45:52.372043771 +0100
+@@ -5,8 +5,8 @@
+  */
+ #include "libfdt_env.h"
+
+-#include <fdt.h>
+-#include <libfdt.h>
++#include "fdt.h"
++#include "libfdt.h"
+
+ #include "libfdt_internal.h"
+
+--- a/common/libfdt/fdt_addresses.c	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/fdt_addresses.c	2023-11-27 14:45:52.372043771 +0100
+@@ -6,8 +6,8 @@
+  */
+ #include "libfdt_env.h"
+
+-#include <fdt.h>
+-#include <libfdt.h>
++#include "fdt.h"
++#include "libfdt.h"
+
+ #include "libfdt_internal.h"
+
+--- a/common/libfdt/fdt_check.c	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/fdt_check.c	2023-11-27 14:45:52.372043771 +0100
+@@ -5,8 +5,8 @@
+  */
+ #include "libfdt_env.h"
+
+-#include <fdt.h>
+-#include <libfdt.h>
++#include "fdt.h"
++#include "libfdt.h"
+
+ #include "libfdt_internal.h"
+
+--- a/common/libfdt/fdt_empty_tree.c	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/fdt_empty_tree.c	2023-11-27 14:45:52.372043771 +0100
+@@ -5,8 +5,8 @@
+  */
+ #include "libfdt_env.h"
+
+-#include <fdt.h>
+-#include <libfdt.h>
++#include "fdt.h"
++#include "libfdt.h"
+
+ #include "libfdt_internal.h"
+
+--- a/common/libfdt/fdt_overlay.c	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/fdt_overlay.c	2023-11-27 14:45:52.372043771 +0100
+@@ -6,8 +6,8 @@
+  */
+ #include "libfdt_env.h"
+
+-#include <fdt.h>
+-#include <libfdt.h>
++#include "fdt.h"
++#include "libfdt.h"
+
+ #include "libfdt_internal.h"
+
+@@ -461,7 +461,7 @@
+ 		if (!name_len)
+ 			return -FDT_ERR_BADOVERLAY;
+
+-		poffset = strtoul(sep + 1, &endptr, 10);
++		poffset = strtoui(sep + 1, (const char **)&endptr, 10);
+ 		if ((*endptr != '\0') || (endptr <= (sep + 1)))
+ 			return -FDT_ERR_BADOVERLAY;
+
+--- a/common/libfdt/fdt_ro.c	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/fdt_ro.c	2023-11-27 14:45:52.372043771 +0100
+@@ -5,8 +5,8 @@
+  */
+ #include "libfdt_env.h"
+
+-#include <fdt.h>
+-#include <libfdt.h>
++#include "fdt.h"
++#include "libfdt.h"
+
+ #include "libfdt_internal.h"
+
+--- a/common/libfdt/fdt_rw.c	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/fdt_rw.c	2023-11-27 14:45:52.372043771 +0100
+@@ -5,8 +5,8 @@
+  */
+ #include "libfdt_env.h"
+
+-#include <fdt.h>
+-#include <libfdt.h>
++#include "fdt.h"
++#include "libfdt.h"
+
+ #include "libfdt_internal.h"
+
+--- a/common/libfdt/fdt_strerror.c	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/fdt_strerror.c	2023-11-27 14:45:52.372043771 +0100
+@@ -6,8 +6,8 @@
+  */
+ #include "libfdt_env.h"
+
+-#include <fdt.h>
+-#include <libfdt.h>
++#include "fdt.h"
++#include "libfdt.h"
+
+ #include "libfdt_internal.h"
+
+--- a/common/libfdt/fdt_sw.c	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/fdt_sw.c	2023-11-27 14:45:52.372043771 +0100
+@@ -5,8 +5,8 @@
+  */
+ #include "libfdt_env.h"
+
+-#include <fdt.h>
+-#include <libfdt.h>
++#include "fdt.h"
++#include "libfdt.h"
+
+ #include "libfdt_internal.h"
+
+--- a/common/libfdt/fdt_wip.c	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/fdt_wip.c	2023-11-27 14:45:52.375377052 +0100
+@@ -5,8 +5,8 @@
+  */
+ #include "libfdt_env.h"
+
+-#include <fdt.h>
+-#include <libfdt.h>
++#include "fdt.h"
++#include "libfdt.h"
+
+ #include "libfdt_internal.h"
+
+--- a/common/libfdt/libfdt.h	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/libfdt.h	2023-11-27 14:45:52.375377052 +0100
+@@ -6,8 +6,8 @@
+  * Copyright (C) 2006 David Gibson, IBM Corporation.
+  */
+
+-#include <libfdt_env.h>
+-#include <fdt.h>
++#include "libfdt_env.h"
++#include "fdt.h"
+
+ #ifdef __cplusplus
+ extern "C" {
+--- a/common/libfdt/libfdt_env.h	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/libfdt_env.h	2023-11-27 14:45:52.375377052 +0100
+@@ -10,9 +10,9 @@
+ #include <stdbool.h>
+ #include <stddef.h>
+ #include <stdint.h>
+-#include <stdlib.h>
+-#include <string.h>
+ #include <limits.h>
++#include <lib/libc.h>
++#include <lib/misc.h>
+
+ #ifdef __CHECKER__
+ #define FDT_FORCE __attribute__((force))
+--- a/common/libfdt/libfdt_internal.h	2023-02-09 11:01:35.000000000 +0100
++++ b/common/libfdt/libfdt_internal.h	2023-11-27 14:45:52.375377052 +0100
+@@ -5,7 +5,7 @@
+  * libfdt - Flat Device Tree manipulation
+  * Copyright (C) 2006 David Gibson, IBM Corporation.
+  */
+-#include <fdt.h>
++#include "fdt.h"
+
+ #define FDT_ALIGN(x, a)		(((x) + (a) - 1) & ~((a) - 1))
+ #define FDT_TAGALIGN(x)		(FDT_ALIGN((x), FDT_TAGSIZE))
tab: 248 wrap: offon