aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--.gitmodules3
-rw-r--r--Makefile7
-rw-r--r--TODO.txt8
-rwxr-xr-xarch/riscv/conf/mkimage.sh3
-rw-r--r--arch/riscv/init/init.c2
-rw-r--r--arch/riscv/sbi.c27
-rw-r--r--arch/riscv/source.mk6
-rw-r--r--common/bytes.c46
-rw-r--r--common/string.c308
m---------dtc0
-rw-r--r--include/apos/attrs.h (renamed from include/apos/compiler_attributes.h)1
-rw-r--r--include/apos/init.h2
-rw-r--r--include/apos/main.h2
-rw-r--r--include/apos/mem.h4
-rw-r--r--include/apos/string.h78
-rw-r--r--include/apos/types.h208
-rw-r--r--lib/fdt.c2
-rw-r--r--lib/fdt_addresses.c2
-rw-r--r--lib/fdt_empty_tree.c2
-rw-r--r--lib/fdt_ro.c2
-rw-r--r--lib/fdt_rw.c2
-rw-r--r--lib/fdt_strerror.c2
-rw-r--r--lib/fdt_sw.c2
-rw-r--r--lib/fdt_wip.c2
-rw-r--r--lib/libfdt_env.h17
26 files changed, 702 insertions, 37 deletions
diff --git a/.gitignore b/.gitignore
index feb9659..d0074c4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
*.o
+*.d
*.bin
*.elf
*.ld
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..dcf638f
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "dtc"]
+ path = dtc
+ url = git@github.com:dgibson/dtc.git
diff --git a/Makefile b/Makefile
index cc9f7b2..d7f02c1 100644
--- a/Makefile
+++ b/Makefile
@@ -15,14 +15,15 @@ CPP := cpp
OBJCOPY ?= objcopy
OBJCOPY_FLAGS ?= -Obinary
-KERNEL_SOURCES :=
-INIT_SOURCES :=
+COMMON_SOURCES != echo common/*.c
+KERNEL_SOURCES := $(COMMON_SOURCES)
+INIT_SOURCES := $(COMMON_SOURCES)
CLEANUP := build deps.mk kernel.* init.* apos.bin
CLEANUP_CMD :=
include arch/$(ARCH)/source.mk
-INCLUDE_FLAGS := -Iinclude -Iarch/$(ARCH)/include
+INCLUDE_FLAGS := -Iinclude -Iarch/$(ARCH)/include $(USE_FDT:y=-Idtc/libfdt)
COMPILE = $(CROSS_COMPILE)$(CC) $(CFLAGS) $(DEPFLAGS) $(INCLUDE_FLAGS)
GENLINK = $(CROSS_COMPILE)$(CPP) $(DEPFLAGS) $(INCLUDE_FLAGS)
diff --git a/TODO.txt b/TODO.txt
index b5a4900..c2ad2bb 100644
--- a/TODO.txt
+++ b/TODO.txt
@@ -1,4 +1,8 @@
-+ Add in sensible defines (uint64_t etc.)
++ Add in sensible defines (uint64_t etc.) x
+ Improve build process (Kbuild? nah, regular config.h files)
-+ Automate booting and starting qemu etc. (make run)
++ Automate booting and starting qemu etc. (make run) (sort of)
+ Add in pmem/vmem routines (both .init and .text!)
+
++ Test string.c
++ Write the kernel lol
++ Test libfdt and see if it can be used to read memory geometry etc.
diff --git a/arch/riscv/conf/mkimage.sh b/arch/riscv/conf/mkimage.sh
index 31abb8e..5a22d01 100755
--- a/arch/riscv/conf/mkimage.sh
+++ b/arch/riscv/conf/mkimage.sh
@@ -13,6 +13,9 @@ SSIZE=$(fdisk -l rootfs.img | awk '$1=="Units:" {print $8}')
OFFSET=$(fdisk -l rootfs.img | awk '$1=="rootfs.img1" {print $2}')
mount -o loop,offset=$((${SSIZE}*${OFFSET})) rootfs.img fs
+# not entirely pleased with this solution, although eventually I should probably
+# move `run` out of the kernel repo and into some `aposos` repo with a runtime
+# and proper initrd etc. so this is good enough for now
../u-boot-apos/tools/mkimage -f arch/riscv/conf/apos.its fs/apos.itb
../u-boot-apos/tools/mkimage -A riscv -O apos -T script \
-d arch/riscv/conf/boot.cmd fs/boot.scr
diff --git a/arch/riscv/init/init.c b/arch/riscv/init/init.c
index 69dbc0a..30c60ac 100644
--- a/arch/riscv/init/init.c
+++ b/arch/riscv/init/init.c
@@ -1,6 +1,6 @@
-#include <stddef.h>
#include <apos/init.h>
#include <apos/sizes.h>
+#include <apos/types.h>
#include <pages.h>
#include <csr.h>
#include <vmap.h>
diff --git a/arch/riscv/sbi.c b/arch/riscv/sbi.c
deleted file mode 100644
index e255ecf..0000000
--- a/arch/riscv/sbi.c
+++ /dev/null
@@ -1,27 +0,0 @@
-#include <sbi.h>
-
-struct sbiret sbi_ecall(int ext, int fid,
- unsigned long arg0, unsigned long arg1, unsigned long arg2,
- unsigned long arg3, unsigned long arg4, unsigned long arg5)
-{
- struct sbiret ret;
-
- register unsigned long a0 asm("a0") = arg0;
- register unsigned long a1 asm("a1") = arg1;
- register unsigned long a2 asm("a2") = arg2;
- register unsigned long a3 asm("a3") = arg3;
- register unsigned long a4 asm("a4") = arg4;
- register unsigned long a5 asm("a5") = arg5;
- register unsigned long a6 asm("a6") = fid;
- register unsigned long a7 asm("a7") = ext;
-
- asm volatile ("ecall"
- : "+r" (a0), "+r" (a1)
- : "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7)
- : "memory");
-
- ret.error = a0;
- ret.value = a1;
-
- return ret;
-}
diff --git a/arch/riscv/source.mk b/arch/riscv/source.mk
index fb645f7..cf8edd7 100644
--- a/arch/riscv/source.mk
+++ b/arch/riscv/source.mk
@@ -2,8 +2,12 @@ KERNEL_LOCAL != echo arch/riscv/*.[cS]
KERNEL_SOURCES += $(KERNEL_LOCAL)
INIT_LOCAL != echo arch/riscv/init/*.[cS]
-INIT_SOURCES += $(INIT_LOCAL)
+INIT_FDT != echo lib/fdt*.c
+INIT_SOURCES += $(INIT_LOCAL) $(INIT_FDT)
CLEANUP_CMD := ./arch/riscv/conf/rmimage.sh
+
+USE_FDT := y
+
run:
./arch/riscv/conf/mkimage.sh
diff --git a/common/bytes.c b/common/bytes.c
new file mode 100644
index 0000000..1196989
--- /dev/null
+++ b/common/bytes.c
@@ -0,0 +1,46 @@
+#include <apos/types.h>
+#include <apos/attrs.h>
+
+/* Functions for swapping endianness.
+ *
+ * Typically you wouldn't call these directly, instead using macros such as
+ * le16_to_cpu, cpu_to_be32, etc. that take the system endianness into account.
+ *
+ * The macros cpu_to_be64 etc. use GCC's and Clang's __builtin_bswap16/32/64, but in
+ * the case that the arch compiler doesn't provide the __builtin functions they're replaced
+ * with __bswaphi2/si2/di2 respectively, and it is up to the developer to
+ * provide these. The naming scheme uses GCC's internal naming scheme, where
+ *
+ * HI ~= 16 bits
+ * SI ~= 32 bits
+ * DI ~= 64 bits
+ *
+ * Here we assume this to be true in all cases, and it probably is, but in the
+ * case that some architecture is different (SI ~= 64 bits or something) we define
+ * these functions as __weak, allowing any arch to redefine them.
+ */
+
+__weak uint16_t __bswaphi2(uint16_t u)
+{
+ return (u & 0xff00) >> 8 | (u & 0x00ff) << 8;
+}
+
+__weak uint32_t __bswapsi2(uint32_t u)
+{
+ return (u & 0xff000000) >> 24 |
+ (u & 0x00ff0000) >> 8 |
+ (u & 0x0000ff00) << 8 |
+ (u & 0x000000ff) << 24;
+}
+
+__weak uint64_t __bswapdi2(uint64_t u)
+{
+ return (u & 0xff00000000000000ULL) >> 56 |
+ (u & 0x00ff000000000000ULL) >> 40 |
+ (u & 0x0000ff0000000000ULL) >> 24 |
+ (u & 0x000000ff00000000ULL) >> 8 |
+ (u & 0x00000000ff000000ULL) << 8 |
+ (u & 0x0000000000ff0000ULL) << 24 |
+ (u & 0x000000000000ff00ULL) << 40 |
+ (u & 0x00000000000000ffULL) << 56;
+}
diff --git a/common/string.c b/common/string.c
new file mode 100644
index 0000000..ab65f2a
--- /dev/null
+++ b/common/string.c
@@ -0,0 +1,308 @@
+#include <apos/string.h>
+#include <apos/types.h>
+#include <apos/attrs.h>
+
+/* we need to undef the macros in string.h, otherwise the names get mangled */
+#undef strcpy
+__weak char *strcpy(char *dst, const char *src)
+{
+ const char *s1 = src;
+ char *s2 = dst;
+
+ while (*s1)
+ *(s2++) = *(s1++);
+
+ return dst;
+}
+
+#undef strncpy
+__weak char *strncpy(char *dst, const char *src, size_t num)
+{
+ const char *s1 = src;
+ char *s2 = dst;
+
+ /* copy s1 into s2 */
+ while (num-- && *s1)
+ *(s2++) = *(s1++);
+
+ /* the previous loop always overshoots by one */
+ num++;
+
+ /* pad with zeroes if num is not yet zero */
+ while (num--)
+ *(s2++) = 0;
+
+ return dst;
+}
+
+#undef strcat
+__weak char *strcat(char *dst, const char *src)
+{
+ const char *s1 = src;
+ size_t l1 = strlen(s1);
+ char *s2 = dst + l1;
+
+ while (*s1)
+ *(s2++) = *(s1++);
+
+ /* append null character */
+ *s2 = 0;
+
+ return dst;
+}
+
+#undef strncat
+__weak char *strncat(char *dst, const char *src, size_t num)
+{
+ const char *s1 = src;
+ size_t l1 = strlen(s1);
+ char *s2 = dst + l1;
+
+ while (num-- && *s1)
+ *(s2++) = *(s1++);
+
+ /* append null character */
+ *s2 = 0;
+
+ return dst;
+}
+
+#undef strcmp
+__weak int strcmp(const char *str1, const char *str2)
+{
+ const char *s1 = (const char *)str1;
+ const char *s2 = (const char *)str2;
+
+ while ((*(s1++) == *(s2++)) && *s1 && *s2) ;
+
+ return (int)(s1[-1] - s2[-1]);
+}
+
+#undef strncmp
+__weak int strncmp(const char *str1, const char *str2, size_t num)
+{
+ const char *s1 = (const char *)str1;
+ const char *s2 = (const char *)str2;
+
+ while ((*(s1++) == *(s2++)) && *s1 && *s2 && num--) ;
+
+ return (int)(s1[-1] - s2[-1]);
+}
+
+#undef strchr
+__weak char *strchr(const char *str, int chr)
+{
+ const char *s1 = str;
+ size_t num = strlen(s1);
+
+ while (num-- && *(s1--) != chr) ;
+
+ if (!num)
+ return 0;
+
+ return (char *)s1;
+}
+
+#undef strtok
+__weak char *strtok(char *str, const char *delims)
+{
+ static char *cont = 0;
+ const char *s1 = str;
+
+ if (!s1)
+ s1 = cont;
+
+ if (!s1)
+ return 0;
+
+ s1 = strpbrk(s1, delims);
+
+ if (!s1)
+ cont = 0;
+ else
+ cont = (char *)s1 + 1;
+
+ return (char *)s1;
+}
+
+/* should probably test out these functions somehwere, blergh */
+#undef strstr
+__weak char *strstr(const char *str1, const char *str2)
+{
+ /* boyer-moore-horspool */
+ char table[256] = { 0 };
+ size_t sl = strlen(str1);
+ size_t pl = strlen(str2);
+
+ const char *s1 = str1;
+
+ for (size_t i = 0; i < 256; ++i)
+ table[i] = pl;
+
+ /* generate deltas */
+ for (size_t i = 0; i < pl - 1; ++i)
+ table[str2[i]] = pl - i - 1;
+
+ size_t skip = 0;
+ while (sl - skip >= pl) {
+ s1 = &str1[skip];
+
+ if (!memcmp(s1, s2, pl))
+ return (char *)s1;
+
+ skip += table[str1[skip + pl - 1]];
+ }
+
+ return 0;
+}
+
+#undef strrchr
+__weak char *strrchr(const char *str, int chr)
+{
+ size_t num = strlen(str);
+ const char *s1 = str + num;
+
+ while (num-- && *(s1--) != chr) ;
+
+ if (!num)
+ return 0;
+
+ return (char *)s1;
+}
+
+#undef strpbrk
+__weak char *strpbrk(const char *str1, const char *str2)
+{
+ size_t i = strcspn(str1, str2);
+
+ if (!i)
+ return 0;
+
+ return (char *)(str1 + i);
+}
+
+#undef strcspn
+__weak size_t strcspn(const char *str1, const char *str2)
+{
+ char table[256] = { 0 };
+ const char *s1 = str1;
+ const char *s2 = s1;
+ const char *t1 = str2;
+
+ /* populate table */
+ while (*(t1++))
+ table[*t1] = 1;
+
+ for (;;) {
+ if (table[*(s2++)])
+ break;
+ }
+
+ /* the for loop overshoots by one */
+ return (size_t)(s2 - s1) - 1;
+}
+
+#undef strspn
+__weak size_t strspn(const char *str1, const char *str2)
+{
+ char table[256] = { 0 };
+ const char *s1 = str1;
+ const char *s2 = s1;
+ const char *t1 = str2;
+
+ /* populate table */
+ while (*(t1++))
+ table[*t1] = 1;
+
+ for (;;) {
+ if (!table[*(s2++)])
+ break;
+ }
+
+ /* the for loop overshoots by one */
+ return (size_t)(s2 - s1) - 1;
+}
+
+#undef strlen
+__weak size_t strlen(const char *str)
+{
+ const char *s1 = str;
+ while (*(s1++)) ;
+
+ /* the loop overshoots by one */
+ return (size_t)(s1 - str) - 1;
+}
+
+/* not a macro */
+__weak size_t strnlen(const char *str, size_t num)
+{
+ const char *s1 = str;
+ while (num-- && *(s1++)) ;
+
+ return (size_t)(s1 - str) - 1;
+}
+
+#undef memset
+__weak void *memset(void *ptr, int value, size_t num)
+{
+ char *p = ptr;
+ char c = value;
+
+ while (num--)
+ *(p--) = c;
+
+ return ptr;
+}
+
+#undef memchr
+__weak void *memchr(const void *ptr, int val, size_t num)
+{
+ const char *p1 = (char *)ptr;
+ char c = (char)val;
+
+ while (num-- && *(p1--) != c) ;
+
+ if (!num)
+ return 0;
+
+ return (void *)p1;
+}
+
+#undef memcpy
+__weak void *memcpy(void *dst, const void *src, size_t num)
+{
+ const char *m1 = (const char *)src;
+ char *m2 = (char *)dst;
+
+ while (num--)
+ *(m2++) = *(m1++);
+
+ return dst;
+}
+
+#undef memmove
+__weak void *memmove(void *dst, const void *src, size_t num)
+{
+ const char *m1 = (const char *)src;
+ char *m2 = (char *)dst;
+
+ m1 += num;
+ m2 += num;
+
+ while (num--)
+ *(--m2) = *(--m1);
+
+ return dst;
+}
+
+#undef memcmp
+__weak int memcmp(const void *ptr1, const void *ptr2, size_t num)
+{
+ const char *p1 = (const char *)ptr1;
+ const char *p2 = (const char *)ptr2;
+
+ while ((*(p1++) == *(p2++)) && num--) ;
+
+ return (int)(p1[-1] - p2[-1]);
+}
+
diff --git a/dtc b/dtc
new file mode 160000
+Subproject b6910bec11614980a21e46fbccc35934b671bd8
diff --git a/include/apos/compiler_attributes.h b/include/apos/attrs.h
index 45b7ac3..00dcf82 100644
--- a/include/apos/compiler_attributes.h
+++ b/include/apos/attrs.h
@@ -2,5 +2,6 @@
#define __section(section) __attribute__((__section__(section)))
#define __noinline __attribute__((noinline))
+#define __weak __attribute((weak))
#endif /* APOS_COMPILER_ATTRIBUTES_H */
diff --git a/include/apos/init.h b/include/apos/init.h
index 82f1774..1bf6a9e 100644
--- a/include/apos/init.h
+++ b/include/apos/init.h
@@ -1,7 +1,7 @@
#ifndef APOS_INIT_H
#define APOS_INIT_H
-#include <apos/compiler_attributes.h>
+#include <apos/attrs.h>
#define __init
#define __initdata
diff --git a/include/apos/main.h b/include/apos/main.h
index 1cea533..5292df7 100644
--- a/include/apos/main.h
+++ b/include/apos/main.h
@@ -1,7 +1,7 @@
#ifndef APOS_MAIN_H
#define APOS_MAIN_H
-#include <apos/compiler_attributes.h>
+#include <apos/attrs.h>
#define __main __section(".text.start") __noinline
diff --git a/include/apos/mem.h b/include/apos/mem.h
new file mode 100644
index 0000000..6b209c5
--- /dev/null
+++ b/include/apos/mem.h
@@ -0,0 +1,4 @@
+#ifndef APOS_MEM_H
+#define APOS_MEM_H
+
+#endif /* APOS_MEM_H */
diff --git a/include/apos/string.h b/include/apos/string.h
new file mode 100644
index 0000000..787f630
--- /dev/null
+++ b/include/apos/string.h
@@ -0,0 +1,78 @@
+#ifndef APOS_STRING_H
+#define APOS_STRING_H
+
+#include <apos/types.h>
+
+/* follow C library functions, drop location and error functions */
+
+char *strcpy(char *dst, const char *src);
+char *strncpy(char *dst, const char *src, size_t num);
+
+char *strcat(char *dst, const char *src);
+char *strncat(char *dst, const char *src, size_t num);
+
+int strcmp(const char *str1, const char *str2);
+int strncmp(const char *str1, const char *str2, size_t num);
+
+char *strchr(const char *str, int chr);
+char *strtok(char *str, const char *delims);
+char *strstr(const char *str1, const char *str2);
+
+char *strrchr(const char *str, int chr);
+char *strpbrk(const char *str1, const char *str2);
+
+size_t strspn(const char *str1, const char *str2);
+size_t strcspn(const char *str1, const char *str2);
+
+size_t strlen(const char *str);
+/* only addition on top of libc */
+size_t strnlen(const char *str, size_t num);
+
+void *memset(void *ptr, int value, size_t num);
+void *memchr(const void *ptr, int val, size_t num);
+void *memcpy(void *dst, const void *src, size_t num);
+void *memmove(void *dst, const void *src, size_t num);
+
+int memcmp(const void *ptr1, const void *ptr2, size_t num);
+
+/* Honorable mentions:
+ *
+ * char *strerror(int err);
+ * int strcoll(const char *str1, const char *str2);
+ * int strxfrm(char *dest, const char *src, size_t num);
+ *
+ */
+
+/* provide macros for builtin functions. The compiler is always allowed to
+ * replace a __builtin_* with a regular call to the function, which is why we
+ * still need to define the functions.
+ *
+ * Does mean we can't take the address of any of these, but I suppose that's not
+ * an issue.
+ */
+
+#define memcpy(dst, src, num) __builtin_memcpy(dst, src, num)
+#define memmove(dst, src, num) __builtin_memmove(dst, src, num)
+#define strcpy(dst, src) __builtin_strcpy(dst, src)
+#define strncpy(dst, src, num) __builtin_strncpy(dst, src, num)
+
+#define strcat(dst, src) __builtin_strcat(dst, src)
+#define strncat(dst, src, num) __builtin_strncat(dst, src, num)
+
+#define memcmp(p1, p2, num) __builtin_memcmp(p1, p2, num)
+#define strcmp(s1, s2) __builtin_strcmp(s1, s2)
+#define strncmp(s1, s2, num) __builtin_strncmp(s1, s2, num)
+
+#define memchr(ptr, val, num) __builtin_memchr(ptr, val, num)
+#define strchr(str, chr) __builtin_strchr(str, chr)
+#define strcspn(s1, s2) __builtin_strcspn(s1, s2)
+#define strpbrk(s1, s2) __builtin_strpbrk(s1, s2)
+#define strrchr(s1, s2) __builtin_strrchr(s1, s2)
+#define strspn(s1, s2) __builtin_strspn(s1, s2)
+#define strstr(s1, s2) __builtin_strstr(s1, s2)
+#define strtok(s1, s2) __builtin_strtok(s1, s2)
+
+#define memset(p, v, n) __builtin_memset(p, v, n)
+#define strlen(s) __builtin_strlen(s)
+
+#endif /* APOS_STRING_H */
diff --git a/include/apos/types.h b/include/apos/types.h
index 2e30080..04afd4c 100644
--- a/include/apos/types.h
+++ b/include/apos/types.h
@@ -1,4 +1,210 @@
-#ifdef APOS_TYPES_H
+#ifndef APOS_TYPES_H
#define APOS_TYPES_H
+typedef _Bool bool;
+#define true 1
+#define false 0
+
+typedef __SIZE_TYPE__ size_t;
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
+typedef __WCHAR_TYPE__ wchar_t;
+typedef __WINT_TYPE__ wint_t;
+typedef __INTMAX_TYPE__ intmax_t;
+typedef __UINTMAX_TYPE__ uintmax_t;
+typedef __SIG_ATOMIC_TYPE__ sig_atomic_t;
+typedef __INT8_TYPE__ int8_t;
+typedef __INT16_TYPE__ int16_t;
+typedef __INT32_TYPE__ int32_t;
+typedef __INT64_TYPE__ int64_t;
+typedef __UINT8_TYPE__ uint8_t;
+typedef __UINT16_TYPE__ uint16_t;
+typedef __UINT32_TYPE__ uint32_t;
+typedef __UINT64_TYPE__ uint64_t;
+typedef __INT_LEAST8_TYPE__ int_least8_t;
+typedef __INT_LEAST16_TYPE__ int_least16_t;
+typedef __INT_LEAST32_TYPE__ int_least32_t;
+typedef __INT_LEAST64_TYPE__ int_least64_t;
+typedef __UINT_LEAST8_TYPE__ uint_least8_t;
+typedef __UINT_LEAST16_TYPE__ uint_least16_t;
+typedef __UINT_LEAST32_TYPE__ uint_least32_t;
+typedef __UINT_LEAST64_TYPE__ uint_least64_t;
+typedef __INT_FAST8_TYPE__ int_fast8_t;
+typedef __INT_FAST16_TYPE__ int_fast16_t;
+typedef __INT_FAST32_TYPE__ int_fast32_t;
+typedef __INT_FAST64_TYPE__ int_fast64_t;
+typedef __UINT_FAST8_TYPE__ uint_fast8_t;
+typedef __UINT_FAST16_TYPE__ uint_fast16_t;
+typedef __UINT_FAST32_TYPE__ uint_fast32_t;
+typedef __UINT_FAST64_TYPE__ uint_fast64_t;
+typedef __INTPTR_TYPE__ intptr_t;
+typedef __UINTPTR_TYPE__ uintptr_t;
+
+#define INT8_C __INT8_C
+#define INT16_C __INT16_C
+#define INT32_C __INT32_C
+#define INT64_C __INT64_C
+#define UINT8_C __UINT8_C
+#define UINT16_C __UINT16_C
+#define UINT32_C __UINT32_C
+#define UINT64_C __UINT64_C
+#define INTMAX_C __INTMAX_C
+#define UINTMAX_C __UINTMAX_C
+
+#define CHAR_BIT __CHAR_BIT__
+
+#define SCHAR_MAX __SCHAR_MAX__
+#define SCHAR_MIN (-__SCHAR_MAX - 1)
+#define UCHAR_MAX (2 * __SCHAR_MAX__ - 1)
+
+#ifdef __CHAR_UNSIGNED__
+#define CHAR_MIN 0
+#define CHAR_MAX UCHAR_MAX
+#else
+#define CHAR_MIN SCHAR_MIN
+#define CHAR_MAX SCHAR_MAX
+#endif
+
+/* probably not necessary? */
+#define MB_LEN_MAX 16
+
+#define SHRT_MAX __SHRT_MAX__
+#define SHRT_MIN (-__SHRT_MAX - 1)
+#define USHRT_MAX (2 * __SHRT_MAX + 1)
+
+#define INT_MAX __INT_MAX__
+#define INT_MIN (-__INT_MAX__ - 1)
+#define UINT_MAX (2 * __INT_MAX__ + 1)
+
+#define LONG_MAX __LONG__MAX__
+#define LONG_MIN (-__LONG_MAX__ - 1)
+#define ULONG_MAX (2 * __INT_MAX__ + 1)
+
+#define LLONG_MAX __LONG_LONG_MAX__
+#define LLONG_MIN (-__LONG_LONG_MAX__ - 1)
+#define ULLONG_MAX (2 * __LONG_LONG_MAX__ + 1)
+
+#define INT8_MAX __INT8_MAX__
+#define INT8_MIN (-__INT8_MAX__ - 1)
+#define UINT8_MAX __UINT8_MAX__
+
+#define INT16_MAX __INT16_MAX__
+#define INT16_MIN (-__INT16_MAX__ - 1)
+#define UINT16_MAX __UINT16_MAX__
+
+#define INT32_MAX __INT32_MAX__
+#define INT32_MIN (-__INT32_MAX__ - 1)
+#define UINT32_MAX __UINT32_MAX__
+
+#define INT64_MAX __INT64_MAX__
+#define INT64_MIN (-__INT64_MAX__ - 1)
+#define UINT64_MAX __UINT64_MAX__
+
+#define INT_LEAST8_MAX __INT_LEAST8_MAX__
+#define INT_LEAST8_MIN (-__INT_LEAST8_MAX__ - 1)
+#define UINT_LEAST8_MAX __UINT_LEAST8_MAX__
+
+#define INT_LEAST16_MAX __INT_LEAST16_MAX__
+#define INT_LEAST16_MIN (-__INT_LEAST16_MAX__ - 1)
+#define UINT_LEAST16_MAX __UINT_LEAST16_MAX__
+
+#define INT_LEAST32_MAX __INT_LEAST32_MAX__
+#define INT_LEAST32_MIN (-__INT_LEAST32_MAX__ - 1)
+#define UINT_LEAST32_MAX __UINT_LEAST32_MAX__
+
+#define INT_LEAST64_MAX __INT_LEAST64_MAX__
+#define INT_LEAST64_MIN (-__INT_LEAST64_MAX__ - 1)
+#define UINT_LEAST64_MAX __UINT_LEAST64_MAX__
+
+#define INT_FAST8_MAX __INT_FAST8_MAX__
+#define INT_FAST8_MIN (-__INT_FAST8_MAX__ - 1)
+#define UINT_FAST8_MAX __UINT_FAST8_MAX__
+
+#define INT_FAST16_MAX __INT_FAST16_MAX__
+#define INT_FAST16_MIN (-__INT_FAST16_MAX__ - 1)
+#define UINT_FAST16_MAX __UINT_FAST16_MAX__
+
+#define INT_FAST32_MAX __INT_FAST32_MAX__
+#define INT_FAST32_MIN (-__INT_FAST32_MAX__ - 1)
+#define UINT_FAST32_MAX __UINT_FAST32_MAX__
+
+#define INT_FAST64_MAX __INT_FAST64_MAX__
+#define INT_FAST64_MIN (-__INT_FAST64_MAX__ - 1)
+#define UINT_FAST64_MAX __UINT_FAST64_MAX__
+
+#define INTPTR_MAX __INTPTR_MAX__
+#define INTPTR_MIN (-__INTPTR_MAX__ - 1)
+#define UINTPTR_MAX __UINTPTR_MAX__
+
+#define INTMAX_MAX __INTMAX_MAX__
+#define INTMAX_MIN (-__INTMAX_MAX__ - 1)
+#define UINTMAX_MAX __UINTMAX_MAX__
+
+#define INT8_WIDTH 8
+#define INT16_WIDTH 16
+#define INT32_WIDTH 32
+#define INT64_WIDTH 64
+#define UINT8_WIDTH 8
+#define UINT16_WIDTH 16
+#define UINT32_WIDTH 32
+#define UINT64_WIDTH 64
+
+#define INT_FAST8_WIDTH __INT_FAST8_WIDTH__
+#define INT_FAST16_WIDTH __INT_FAST16_WIDTH__
+#define INT_FAST32_WIDTH __INT_FAST32_WIDTH__
+#define INT_FAST64_WIDTH __INT_FAST64_WIDTH__
+#define UINT_FAST8_WIDTH __INT_FAST8_WIDTH__
+#define UINT_FAST16_WIDTH __INT_FAST16_WIDTH__
+#define UINT_FAST32_WIDTH __INT_FAST32_WIDTH__
+#define UINT_FAST64_WIDTH __INT_FAST64_WIDTH__
+
+#define INT_LEAST8_WIDTH __INT_LEAST8_WIDTH__
+#define INT_LEAST16_WIDTH __INT_LEAST16_WIDTH__
+#define INT_LEAST32_WIDTH __INT_LEAST32_WIDTH__
+#define INT_LEAST64_WIDTH __INT_LEAST64_WIDTH__
+#define UINT_LEAST8_WIDTH __INT_LEAST8_WIDTH__
+#define UINT_LEAST16_WIDTH __INT_LEAST16_WIDTH__
+#define UINT_LEAST32_WIDTH __INT_LEAST32_WIDTH__
+#define UINT_LEAST64_WIDTH __INT_LEAST64_WIDTH__
+
+#define INTPTR_WIDTH __INTPTR_WIDTH__
+#define INTMAX_WIDTH __INTMAX_WIDTH__
+#define UINTPTR_WIDTH __INTPTR_WIDTH__
+#define UINTMAX_WIDTH __INTMAX_WIDTH__
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define be16_to_cpu(x) __builtin_bswap16(x)
+#define be32_to_cpu(x) __builtin_bswap32(x)
+#define be64_to_cpu(x) __builtin_bswap64(x)
+
+#define cpu_to_be16(x) __builtin_bswap16(x)
+#define cpu_to_be32(x) __builtin_bswap32(x)
+#define cpu_to_be64(x) __builtin_bswap64(x)
+
+#define le16_to_cpu(x) (x)
+#define le32_to_cpu(x) (x)
+#define le64_to_cpu(x) (x)
+
+#define cpu_to_le16(x) (x)
+#define cpu_to_le32(x) (x)
+#define cpu_to_le64(x) (x)
+#else
+#define be16_to_cpu(x) (x)
+#define be32_to_cpu(x) (x)
+#define be64_to_cpu(x) (x)
+
+#define cpu_to_be16(x) (x)
+#define cpu_to_be32(x) (x)
+#define cpu_to_be64(x) (x)
+
+#define le16_to_cpu(x) __builtin_bswap16(x)
+#define le32_to_cpu(x) __builtin_bswap32(x)
+#define le64_to_cpu(x) __builtin_bswap64(x)
+
+#define cpu_to_le16(x) __builtin_bswap16(x)
+#define cpu_to_le32(x) __builtin_bswap32(x)
+#define cpu_to_le64(x) __builtin_bswap64(x)
+#endif
+
+#define NULL 0
+
#endif /* APOS_TYPES_H */
diff --git a/lib/fdt.c b/lib/fdt.c
new file mode 100644
index 0000000..2d910c0
--- /dev/null
+++ b/lib/fdt.c
@@ -0,0 +1,2 @@
+#include "libfdt_env.h"
+#include "../dtc/libfdt/fdt.c"
diff --git a/lib/fdt_addresses.c b/lib/fdt_addresses.c
new file mode 100644
index 0000000..f7ac8ae
--- /dev/null
+++ b/lib/fdt_addresses.c
@@ -0,0 +1,2 @@
+#include "libfdt_env.h"
+#include "../dtc/libfdt/fdt_addresses.c"
diff --git a/lib/fdt_empty_tree.c b/lib/fdt_empty_tree.c
new file mode 100644
index 0000000..a057358
--- /dev/null
+++ b/lib/fdt_empty_tree.c
@@ -0,0 +1,2 @@
+#include "libfdt_env.h"
+#include "../dtc/libfdt/fdt_empty_tree.c"
diff --git a/lib/fdt_ro.c b/lib/fdt_ro.c
new file mode 100644
index 0000000..b60a1a8
--- /dev/null
+++ b/lib/fdt_ro.c
@@ -0,0 +1,2 @@
+#include "libfdt_env.h"
+#include "../dtc/libfdt/fdt_ro.c"
diff --git a/lib/fdt_rw.c b/lib/fdt_rw.c
new file mode 100644
index 0000000..1bb8ed4
--- /dev/null
+++ b/lib/fdt_rw.c
@@ -0,0 +1,2 @@
+#include "libfdt_env.h"
+#include "../dtc/libfdt/fdt_rw.c"
diff --git a/lib/fdt_strerror.c b/lib/fdt_strerror.c
new file mode 100644
index 0000000..f442010
--- /dev/null
+++ b/lib/fdt_strerror.c
@@ -0,0 +1,2 @@
+#include "libfdt_env.h"
+#include "../dtc/libfdt/fdt_strerror.c"
diff --git a/lib/fdt_sw.c b/lib/fdt_sw.c
new file mode 100644
index 0000000..b5b1257
--- /dev/null
+++ b/lib/fdt_sw.c
@@ -0,0 +1,2 @@
+#include "libfdt_env.h"
+#include "../dtc/libfdt/fdt_sw.c"
diff --git a/lib/fdt_wip.c b/lib/fdt_wip.c
new file mode 100644
index 0000000..ee3b4d3
--- /dev/null
+++ b/lib/fdt_wip.c
@@ -0,0 +1,2 @@
+#include "libfdt_env.h"
+#include "../dtc/libfdt/fdt_wip.c"
diff --git a/lib/libfdt_env.h b/lib/libfdt_env.h
new file mode 100644
index 0000000..166b558
--- /dev/null
+++ b/lib/libfdt_env.h
@@ -0,0 +1,17 @@
+#ifndef LIBFDT_ENV_H
+/* take over libfdt */
+#define LIBFDT_ENV_H
+
+#include <apos/types.h>
+#include <apos/string.h>
+
+typedef int16_t fdt16_t;
+typedef int32_t fdt32_t;
+typedef int64_t fdt64_t;
+
+#define fdt32_to_cpu(x) be32_to_cpu(x)
+#define cpu_to_fdt32(x) cpu_to_be32(x)
+#define fdt64_to_cpu(x) be64_to_cpu(x)
+#define cpu_to_fdt64(x) cpu_to_be64(x)
+
+#endif /* LIBFDT_ENV_H */