misc: Introduce libc-compat headers to avoid the ugly libfdt patch
diff --git a/bootstrap b/bootstrap
index cf40f252..b4dcee8b 100755
--- a/bootstrap
+++ b/bootstrap
@@ -121,8 +121,6 @@ if ! test -f version; then
rm -rf common/libfdt
cp -rp dtc/libfdt common/
find common/libfdt/ -type f -not -name '*.c' -not -name '*.h' -delete
- patch -p1 < common/libfdt.patch
- rm -f common/libfdt/*.orig
fi
# Create timestamps file
diff --git a/common/common.mk b/common/common.mk
index 55b8ae9c..7bd3c34d 100644
--- a/common/common.mk
+++ b/common/common.mk
@@ -49,9 +49,11 @@ override CFLAGS_FOR_TARGET += \
-fno-lto
override CPPFLAGS_FOR_TARGET := \
+ -I libc-compat \
-isystem ../freestnd-c-hdrs-0bsd \
-I'$(call SHESCAPE,$(BUILDDIR))/..' \
-I. \
+ -I libfdt \
$(CPPFLAGS_FOR_TARGET) \
-DCOM_OUTPUT=$(COM_OUTPUT) \
-DE9_OUTPUT=$(E9_OUTPUT) \
diff --git a/common/lib/libc.c b/common/lib/libc.c
index 25b6323f..85831676 100644
--- a/common/lib/libc.c
+++ b/common/lib/libc.c
@@ -1,6 +1,80 @@
#include <stddef.h>
#include <stdint.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
#include <lib/libc.h>
+#include <lib/misc.h>
+
+// Slightly adapted strtoul() implementation from FreeBSD.
+// https://github.com/freebsd/freebsd-src/blob/de1aa3dab23c06fec962a14da3e7b4755c5880cf/lib/libc/stdlib/strtoul.c
+unsigned long strtoul(const char *nptr, char **endptr, int base) {
+ const char *s;
+ unsigned long acc;
+ char c;
+ unsigned long cutoff;
+ int neg, any, cutlim;
+
+ s = nptr;
+ do {
+ c = *s++;
+ } while (isspace((unsigned char)c));
+ if (c == '-') {
+ neg = 1;
+ c = *s++;
+ } else {
+ neg = 0;
+ if (c == '+')
+ c = *s++;
+ }
+ if ((base == 0 || base == 16) &&
+ c == '0' && (*s == 'x' || *s == 'X') &&
+ ((s[1] >= '0' && s[1] <= '9') ||
+ (s[1] >= 'A' && s[1] <= 'F') ||
+ (s[1] >= 'a' && s[1] <= 'f'))) {
+ c = s[1];
+ s += 2;
+ base = 16;
+ }
+ if (base == 0)
+ base = c == '0' ? 8 : 10;
+ acc = any = 0;
+ if (base < 2 || base > 36)
+ goto noconv;
+
+ cutoff = ULONG_MAX / base;
+ cutlim = ULONG_MAX % base;
+ for ( ; ; c = *s++) {
+ if (c >= '0' && c <= '9')
+ c -= '0';
+ else if (c >= 'A' && c <= 'Z')
+ c -= 'A' - 10;
+ else if (c >= 'a' && c <= 'z')
+ c -= 'a' - 10;
+ else
+ break;
+ if (c >= base)
+ break;
+ if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
+ any = -1;
+ else {
+ any = 1;
+ acc *= base;
+ acc += c;
+ }
+ }
+ if (any < 0) {
+ acc = ULONG_MAX;
+ //errno = ERANGE;
+ } else if (!any) {
+noconv:
+ ;//errno = EINVAL;
+ } else if (neg)
+ acc = -acc;
+ if (endptr != NULL)
+ *endptr = (char *)(any ? s - 1 : nptr);
+ return (acc);
+}
size_t strnlen(const char *str, size_t maxlen) {
size_t len;
diff --git a/common/libc-compat/stdlib.h b/common/libc-compat/stdlib.h
new file mode 100644
index 00000000..8e01eb27
--- /dev/null
+++ b/common/libc-compat/stdlib.h
@@ -0,0 +1,6 @@
+#ifndef LIBC_COMPAT__STDLIB_H__
+#define LIBC_COMPAT__STDLIB_H__
+
+unsigned long strtoul(const char *str, char **end, int base);
+
+#endif
diff --git a/common/libc-compat/string.h b/common/libc-compat/string.h
new file mode 100644
index 00000000..4f597da7
--- /dev/null
+++ b/common/libc-compat/string.h
@@ -0,0 +1,23 @@
+#ifndef LIBC_COMPAT__STRING_H__
+#define LIBC_COMPAT__STRING_H__
+
+#include <stddef.h>
+
+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);
+int strncasecmp(const char *, const char *, size_t);
+
+#endif
diff --git a/common/libfdt.patch b/common/libfdt.patch
deleted file mode 100644
index 1f754e7f..00000000
--- a/common/libfdt.patch
+++ /dev/null
@@ -1,177 +0,0 @@
---- 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))
\ No newline at end of file
