]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/kboot/kbootfdt.c
zfs: merge openzfs/zfs@10e36e176
[FreeBSD/FreeBSD.git] / stand / kboot / kbootfdt.c
1 /*-
2  * Copyright (C) 2014 Nathan Whitehorn
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/types.h>
30 #include <fdt_platform.h>
31 #include <libfdt.h>
32 #include "bootstrap.h"
33 #include "host_syscall.h"
34 #include "kboot.h"
35
36 static void
37 add_node_to_fdt(void *buffer, const char *path, int fdt_offset)
38 {
39         int child_offset, fd, pfd, error, dentsize;
40         char subpath[512];
41         void *propbuf;
42         ssize_t proplen;
43
44         char dents[2048];
45         struct host_dirent64 *dent;
46         int d_type;
47
48         fd = host_open(path, O_RDONLY, 0);
49         while (1) {
50                 dentsize = host_getdents64(fd, dents, sizeof(dents));
51                 if (dentsize <= 0)
52                         break;
53                 for (dent = (struct host_dirent64 *)dents;
54                      (char *)dent < dents + dentsize;
55                      dent = (struct host_dirent64 *)((void *)dent + dent->d_reclen)) {
56                         sprintf(subpath, "%s/%s", path, dent->d_name);
57                         if (strcmp(dent->d_name, ".") == 0 ||
58                             strcmp(dent->d_name, "..") == 0)
59                                 continue;
60                         d_type = dent->d_type;
61                         if (d_type == HOST_DT_DIR) {
62                                 child_offset = fdt_add_subnode(buffer, fdt_offset,
63                                     dent->d_name);
64                                 if (child_offset < 0) {
65                                         printf("Error %d adding node %s/%s, skipping\n",
66                                             child_offset, path, dent->d_name);
67                                         continue;
68                                 }
69                                 add_node_to_fdt(buffer, subpath, child_offset);
70                         } else {
71                                 propbuf = malloc(1024);
72                                 proplen = 0;
73                                 pfd = host_open(subpath, O_RDONLY, 0);
74                                 if (pfd > 0) {
75                                         proplen = host_read(pfd, propbuf, 1024);
76                                         host_close(pfd);
77                                 }
78                                 error = fdt_setprop(buffer, fdt_offset, dent->d_name,
79                                     propbuf, proplen);
80                                 free(propbuf);
81                                 if (error)
82                                         printf("Error %d adding property %s to "
83                                             "node %d\n", error, dent->d_name,
84                                             fdt_offset);
85                         }
86                 }
87         }
88
89         host_close(fd);
90 }
91
92 int
93 fdt_platform_load_dtb(void)
94 {
95         void *buffer;
96         size_t buflen = 409600;
97         int fd;
98
99         /*
100          * Should load /sys/firmware/fdt if it exists, otherwise we walk the
101          * tree from /proc/device-tree. The former is much easier than the
102          * latter and also the newer interface. But as long as we support the
103          * PS3 boot, we'll need the latter due to that kernel's age. It likely
104          * would be better to script the decision between the two, but that
105          * turns out to be tricky...
106          */
107         buffer = malloc(buflen);
108         fd = host_open("/sys/firmware/fdt", O_RDONLY, 0);
109         if (fd != -1) {
110                 buflen = host_read(fd, buffer, buflen);
111                 close(fd);
112         } else {
113                 fdt_create_empty_tree(buffer, buflen);
114                 add_node_to_fdt(buffer, "/proc/device-tree",
115                     fdt_path_offset(buffer, "/"));
116         }
117         fdt_arch_fixups(buffer);
118
119         fdt_pack(buffer);
120
121         fdt_load_dtb_addr(buffer);
122         free(buffer);
123         
124         return (0);
125 }
126
127 void
128 fdt_platform_load_overlays(void)
129 {
130         fdt_load_dtb_overlays(NULL);
131 }
132
133 void
134 fdt_platform_fixups(void)
135 {
136         fdt_apply_overlays();
137 }