:: commit 4cdda6a2722b824d5f83ee241ccde21427cbd8c7

Marvin Friedrich <contact@marvinf.com> — 2024-10-18 18:01

parents: 6068e094d9

protos/limine: Add device tree blob override config option (#419)

* protos/limine: Add dtb_path config option

* test: Add a device tree and dtb_path to the config

* docs: Add dtb_path configuration information

* protos/limine: Fix missing argument for dtb_path for x86

* protos/limine: Correct panic message argument

* protos/limine: Use freadall instead of freadall_mode
diff --git a/.gitignore b/.gitignore
index 72811293..a21e0c69 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,6 +19,7 @@
 *.hdd
 *.iso
 *.sys
+*.dtb
 *~
 
 /bin
diff --git a/CONFIG.md b/CONFIG.md
index 43b158b3..2e350471 100644
--- a/CONFIG.md
+++ b/CONFIG.md
@@ -132,6 +132,7 @@ Editor control options:
     - riscv64: `sv39`, `sv48`, `sv57`.
     - loongarch64: `4level`.
   * `paging_mode` - Equivalent to setting both `max_paging_mode` and `min_paging_mode` to the same value.
+  * `dtb_path` - A device tree blob to pass instead of the one provided by the firmware.
 
 * multiboot1 and multiboot2 protocols:
   * `kernel_path` - The path of the kernel.
diff --git a/common/menu.c b/common/menu.c
index eb8fd942..b3bd70fc 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -117,6 +117,7 @@ static const char *VALID_KEYS[] = {
     "GPT_GUID",
     "GPT_UUID",
     "IMAGE_PATH",
+	"DTB_PATH",
     NULL
 };
 
diff --git a/common/protos/limine.c b/common/protos/limine.c
index 54a216e4..600b1b31 100644
--- a/common/protos/limine.c
+++ b/common/protos/limine.c
@@ -998,8 +998,23 @@ FEAT_START
         break; // next feature
     }
 
+    void *dtb = NULL;
+    char *dtb_path = config_get_value(config, 0, "DTB_PATH");
+
+    if (dtb_path) {
+        struct file_handle *dtb_file;
+        if ((dtb_file = uri_open(dtb_path)) == NULL)
+            panic(true, "limine: Failed to open device tree blob with path `%#`. Is the path correct?", dtb_path);
+
+        dtb = freadall(dtb_file, MEMMAP_BOOTLOADER_RECLAIMABLE);
+        fclose(dtb_file);
+    } else {
 #if defined (UEFI)
-    void *dtb = get_device_tree_blob(0);
+        dtb = get_device_tree_blob(0);
+#else
+        break;
+#endif
+    }
 
     if (dtb) {
         struct limine_dtb_response *dtb_response =
@@ -1007,7 +1022,6 @@ FEAT_START
         dtb_response->dtb_ptr = reported_addr(dtb);
         dtb_request->response = reported_addr(dtb_response);
     }
-#endif
 
 FEAT_END
 
diff --git a/test/device_tree.dts b/test/device_tree.dts
new file mode 100644
index 00000000..268de0ff
--- /dev/null
+++ b/test/device_tree.dts
@@ -0,0 +1,12 @@
+// Example device tree.
+
+/dts-v1/;
+
+/ {
+    soc {
+        limine_node: limine-node@deadbeef {
+            reg = <0xdeadbeef 0x1000>;
+            label = "KANKER";
+        };
+    };
+};
diff --git a/test/limine.c b/test/limine.c
index 1fe0a018..54a79b72 100644
--- a/test/limine.c
+++ b/test/limine.c
@@ -518,6 +518,8 @@ FEAT_START
     struct limine_dtb_response *dtb_response = _dtb_request.response;
     e9_printf("Device tree blob feature, revision %d", dtb_response->revision);
     e9_printf("Device tree blob pointer: %x", dtb_response->dtb_ptr);
+	uint32_t dtb_magic = *(uint32_t*)dtb_response->dtb_ptr;
+	e9_printf("Device tree header magic: %x", dtb_magic);
 FEAT_END
 
 FEAT_START
diff --git a/test/limine.conf b/test/limine.conf
index ffa819ad..0815a9d0 100644
--- a/test/limine.conf
+++ b/test/limine.conf
@@ -20,6 +20,7 @@ backdrop: 008080
     module_cmdline: This is the first module.
 
     module_path: boot():/boot/bg.jpg
+    dtb_path: boot():/boot/device_tree.dtb
 
 /Multiboot2 Test
     comment: Test of the multiboot2 boot protocol.
diff --git a/test/test.mk b/test/test.mk
index d80ceb9c..58a7a010 100644
--- a/test/test.mk
+++ b/test/test.mk
@@ -108,9 +108,9 @@ override CFLAGS_MB := \
     -I../common/protos
 
 ifneq ($(findstring 86,$(shell $(CC_FOR_TARGET) -dumpmachine)),)
-all: test.elf multiboot2.elf multiboot.elf
+all: test.elf multiboot2.elf multiboot.elf device_tree.dtb
 else
-all: test.elf
+all: test.elf device_tree.dtb
 endif
 
 flanterm:
@@ -141,8 +141,11 @@ multiboot.elf: multiboot_trampoline.o
 %.o: %.asm
 	nasm -felf32 -F dwarf -g $< -o $@
 
+%.dtb: %.dts
+	dtc $< -o $@
+
 clean:
 	rm -rf test.elf limine.o e9print.o memory.o
 	rm -rf multiboot2.o multiboot2.elf multiboot2_trampoline.o
 	rm -rf multiboot.o multiboot_trampoline.o multiboot.elf
-	rm -rf flanterm limine.h
+	rm -rf flanterm limine.h device_tree.dtb
tab: 248 wrap: offon