2 * This file does the necessary interface mapping between the bootwrapper
3 * device tree operations and the interface provided by shared source
4 * files flatdevicetree.[ch].
6 * Copyright 2007 David Gibson, IBM Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
31 #define BAD_ERROR(err) (((err) < 0) \
32 && ((err) != -FDT_ERR_NOTFOUND) \
33 && ((err) != -FDT_ERR_EXISTS))
35 #define check_err(err) \
37 if (BAD_ERROR(err) || ((err < 0) && DEBUG)) \
38 printf("%s():%d %s\n\r", __func__, __LINE__, \
45 #define offset_devp(off) \
47 int _offset = (off); \
48 check_err(_offset) ? NULL : (void *)(_offset+1); \
51 #define devp_offset_find(devp) (((int)(devp))-1)
52 #define devp_offset(devp) (devp ? ((int)(devp))-1 : 0)
55 static void *buf; /* = NULL */
57 #define EXPAND_GRANULARITY 1024
59 static void expand_buf(int minexpand)
61 int size = fdt_totalsize(fdt);
64 size = _ALIGN(size + minexpand, EXPAND_GRANULARITY);
65 buf = platform_ops.realloc(buf, size);
67 fatal("Couldn't find %d bytes to expand device tree\n\r", size);
68 rc = fdt_open_into(fdt, buf, size);
70 fatal("Couldn't expand fdt into new buffer: %s\n\r",
76 static void *fdt_wrapper_finddevice(const char *path)
78 return offset_devp(fdt_path_offset(fdt, path));
81 static int fdt_wrapper_getprop(const void *devp, const char *name,
82 void *buf, const int buflen)
87 p = fdt_getprop(fdt, devp_offset(devp), name, &len);
89 return check_err(len);
90 memcpy(buf, p, min(len, buflen));
94 static int fdt_wrapper_setprop(const void *devp, const char *name,
95 const void *buf, const int len)
99 rc = fdt_setprop(fdt, devp_offset(devp), name, buf, len);
100 if (rc == -FDT_ERR_NOSPACE) {
101 expand_buf(len + 16);
102 rc = fdt_setprop(fdt, devp_offset(devp), name, buf, len);
105 return check_err(rc);
108 static void *fdt_wrapper_get_parent(const void *devp)
110 return offset_devp(fdt_parent_offset(fdt, devp_offset(devp)));
113 static void *fdt_wrapper_create_node(const void *devp, const char *name)
117 offset = fdt_add_subnode(fdt, devp_offset(devp), name);
118 if (offset == -FDT_ERR_NOSPACE) {
119 expand_buf(strlen(name) + 16);
120 offset = fdt_add_subnode(fdt, devp_offset(devp), name);
123 return offset_devp(offset);
126 static void *fdt_wrapper_find_node_by_prop_value(const void *prev,
131 int offset = fdt_node_offset_by_prop_value(fdt, devp_offset_find(prev),
133 return offset_devp(offset);
136 static void *fdt_wrapper_find_node_by_compatible(const void *prev,
139 int offset = fdt_node_offset_by_compatible(fdt, devp_offset_find(prev),
141 return offset_devp(offset);
144 static char *fdt_wrapper_get_path(const void *devp, char *buf, int len)
148 rc = fdt_get_path(fdt, devp_offset(devp), buf, len);
154 static unsigned long fdt_wrapper_finalize(void)
160 fatal("Couldn't pack flat tree: %s\n\r",
162 return (unsigned long)fdt;
165 void fdt_init(void *blob)
169 dt_ops.finddevice = fdt_wrapper_finddevice;
170 dt_ops.getprop = fdt_wrapper_getprop;
171 dt_ops.setprop = fdt_wrapper_setprop;
172 dt_ops.get_parent = fdt_wrapper_get_parent;
173 dt_ops.create_node = fdt_wrapper_create_node;
174 dt_ops.find_node_by_prop_value = fdt_wrapper_find_node_by_prop_value;
175 dt_ops.find_node_by_compatible = fdt_wrapper_find_node_by_compatible;
176 dt_ops.get_path = fdt_wrapper_get_path;
177 dt_ops.finalize = fdt_wrapper_finalize;
179 /* Make sure the dt blob is the right version and so forth */
181 err = fdt_open_into(fdt, fdt, fdt_totalsize(blob));
182 if (err == -FDT_ERR_NOSPACE) {
183 int bufsize = fdt_totalsize(fdt) + 4;
184 buf = malloc(bufsize);
185 err = fdt_open_into(fdt, buf, bufsize);
189 fatal("fdt_init(): %s\n\r", fdt_strerror(err));