:: commit 3a44b450291d06b1614f142e21a3c2f4ccc869b4

Yao Zi <ziyao@disroot.org> — 2024-06-08 12:06

parents: e9338aedfa

lib/fdt: add fdt helpers

diff --git a/common/lib/fdt.c b/common/lib/fdt.c
new file mode 100644
index 00000000..c9ae0692
--- /dev/null
+++ b/common/lib/fdt.c
@@ -0,0 +1,35 @@
+#if !defined(__i386__) && !defined(__x86_64__)
+
+#include <stdint.h>
+#include <stddef.h>
+#include <libfdt/libfdt.h>
+
+static int fdt_get_or_add_chosen_node(void *fdt) {
+    int offset = fdt_subnode_offset(fdt, 0, "chosen");
+
+    if (offset == -FDT_ERR_NOTFOUND) {
+        offset = fdt_add_subnode(fdt, 0, "chosen");
+    }
+
+    return offset;
+}
+
+int fdt_set_chosen_string(void *fdt, const char *name, const char *value) {
+    int chosen_offset = fdt_get_or_add_chosen_node(fdt);
+    if (chosen_offset < 0) {
+        return chosen_offset;
+    }
+
+    return fdt_setprop_string(fdt, chosen_offset, name, value);
+}
+
+int fdt_set_chosen_uint64(void *fdt, const char *name, uint64_t value) {
+    int chosen_offset = fdt_get_or_add_chosen_node(fdt);
+    if (chosen_offset < 0) {
+        return chosen_offset;
+    }
+
+    return fdt_setprop_u64(fdt, chosen_offset, name, value);
+}
+
+#endif
diff --git a/common/lib/fdt.h b/common/lib/fdt.h
new file mode 100644
index 00000000..3edcc841
--- /dev/null
+++ b/common/lib/fdt.h
@@ -0,0 +1,14 @@
+#ifndef LIB__FDT_H__
+#define LIB__FDT_H__
+
+#if !defined(__x86_64__) && !defined(__i386__)
+
+#include <stddef.h>
+#include <stdint.h>
+
+int fdt_set_chosen_string(void *fdt, const char *name, const char *value);
+int fdt_set_chosen_uint64(void *fdt, const char *name, uint64_t value);
+
+#endif
+
+#endif // LIB__FDT_H__
tab: 248 wrap: offon