git: d55ddce0c8e9 - stable/14 - kboot: kbootfdt: fix error handling

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 16 Apr 2024 20:13:36 UTC
The branch stable/14 has been updated by imp:

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

commit d55ddce0c8e957c5285d28a5bfdbf3774c04bc56
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2024-03-11 20:15:44 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-04-16 19:54:29 +0000

    kboot: kbootfdt: fix error handling
    
    If we are able to open /sys/firmware/fdt, but aren't able to read it,
    fall back to /proc/device-tree. Remove comment that's not really true,
    it turns out.
    
    Sponsored by:           Netflix
    
    (cherry picked from commit 462af7676b3ee8a8bd9ee9b55a35c0cf815a351f)
---
 stand/kboot/kboot/kbootfdt.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/stand/kboot/kboot/kbootfdt.c b/stand/kboot/kboot/kbootfdt.c
index 70844820d345..e4c378423585 100644
--- a/stand/kboot/kboot/kbootfdt.c
+++ b/stand/kboot/kboot/kbootfdt.c
@@ -91,22 +91,21 @@ fdt_platform_load_dtb(void)
 {
 	void *buffer;
 	size_t buflen = 409600;
-	int fd;
+	int fd, err = 0;
 
 	/*
 	 * 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...
+	 * PS3 boot, we'll need the latter due to that kernel's age.
 	 */
 	buffer = malloc(buflen);
 	fd = host_open("/sys/firmware/fdt", O_RDONLY, 0);
-	if (fd != -1) {
-		buflen = host_read(fd, buffer, buflen);
+	if (fd >= 0) {
+		err = host_read(fd, buffer, buflen);
 		close(fd);
-	} else {
+	}
+	if (fd < 0 || err < 0) {
 		fdt_create_empty_tree(buffer, buflen);
 		add_node_to_fdt(buffer, "/proc/device-tree",
 		    fdt_path_offset(buffer, "/"));