git: 111610316e49 - main - kboot: Allow loading fdt from different sources

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 09 Dec 2022 05:10:26 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=111610316e49dd6dc6b0b74455c1e4c81960faa8

commit 111610316e49dd6dc6b0b74455c1e4c81960faa8
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-12-09 05:07:52 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2022-12-09 05:07:52 +0000

    kboot: Allow loading fdt from different sources
    
    Linux has /sys/firmware/fdt and /proc/device-tree to publish the dtb for
    the system. The former has it all in one file, while the latter breaks
    it out. Prefer the former since it's the more modern interface, but
    retain both since I don't have a PS3 to test to see if its kernel is new
    enough for /sys/firmware or not.
    
    In addition, do the proper fixup.
    
    Sponsored by:           Netflix
---
 stand/kboot/kbootfdt.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/stand/kboot/kbootfdt.c b/stand/kboot/kbootfdt.c
index aafa436daf9f..3791b0c92ddd 100644
--- a/stand/kboot/kbootfdt.c
+++ b/stand/kboot/kbootfdt.c
@@ -94,11 +94,26 @@ fdt_platform_load_dtb(void)
 {
 	void *buffer;
 	size_t buflen = 409600;
+	int fd;
 
+	/*
+	 * Should load /sys/firmware/fdt if it exists, otherwise we walk the
+	 * tree from /proc/device-tree. The former is much easier than the
+	 * latter and also the newer interface. But as long as we support the
+	 * PS3 boot, we'll need the latter due to that kernel's age. It likely
+	 * would be better to script the decision between the two, but that
+	 * turns out to be tricky...
+	 */
 	buffer = malloc(buflen);
-	fdt_create_empty_tree(buffer, buflen);
-	add_node_to_fdt(buffer, "/proc/device-tree",
-	    fdt_path_offset(buffer, "/"));
+	fd = host_open("/sys/firmware/fdt", O_RDONLY, 0);
+	if (fd != -1) {
+		buflen = host_read(fd, buffer, buflen);
+		close(fd);
+	} else {
+		fdt_create_empty_tree(buffer, buflen);
+		add_node_to_fdt(buffer, "/proc/device-tree",
+		    fdt_path_offset(buffer, "/"));
+	}
 	fdt_arch_fixups(buffer);
 
 	fdt_pack(buffer);
@@ -112,12 +127,11 @@ fdt_platform_load_dtb(void)
 void
 fdt_platform_load_overlays(void)
 {
-
+	fdt_load_dtb_overlays(NULL);
 }
 
 void
 fdt_platform_fixups(void)
 {
-
+	fdt_apply_overlays();
 }
-