1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * This file does the necessary interface mapping between the bootwrapper
4 * device tree operations and the interface provided by shared source
5 * files flatdevicetree.[ch].
7 * Copyright 2007 David Gibson, IBM Corporation.
17 #define BAD_ERROR(err) (((err) < 0) \
18 && ((err) != -FDT_ERR_NOTFOUND) \
19 && ((err) != -FDT_ERR_EXISTS))
21 #define check_err(err) \
23 if (BAD_ERROR(err) || ((err < 0) && DEBUG)) \
24 printf("%s():%d %s\n\r", __func__, __LINE__, \
31 #define offset_devp(off) \
33 unsigned long _offset = (off); \
34 check_err(_offset) ? NULL : (void *)(_offset+1); \
37 #define devp_offset_find(devp) (((unsigned long)(devp))-1)
38 #define devp_offset(devp) (devp ? ((unsigned long)(devp))-1 : 0)
41 static void *buf
; /* = NULL */
43 #define EXPAND_GRANULARITY 1024
45 static void expand_buf(int minexpand
)
47 int size
= fdt_totalsize(fdt
);
50 size
= _ALIGN(size
+ minexpand
, EXPAND_GRANULARITY
);
51 buf
= platform_ops
.realloc(buf
, size
);
53 fatal("Couldn't find %d bytes to expand device tree\n\r", size
);
54 rc
= fdt_open_into(fdt
, buf
, size
);
56 fatal("Couldn't expand fdt into new buffer: %s\n\r",
62 static void *fdt_wrapper_finddevice(const char *path
)
64 return offset_devp(fdt_path_offset(fdt
, path
));
67 static int fdt_wrapper_getprop(const void *devp
, const char *name
,
68 void *buf
, const int buflen
)
73 p
= fdt_getprop(fdt
, devp_offset(devp
), name
, &len
);
75 return check_err(len
);
76 memcpy(buf
, p
, min(len
, buflen
));
80 static int fdt_wrapper_setprop(const void *devp
, const char *name
,
81 const void *buf
, const int len
)
85 rc
= fdt_setprop(fdt
, devp_offset(devp
), name
, buf
, len
);
86 if (rc
== -FDT_ERR_NOSPACE
) {
88 rc
= fdt_setprop(fdt
, devp_offset(devp
), name
, buf
, len
);
94 static int fdt_wrapper_del_node(const void *devp
)
96 return fdt_del_node(fdt
, devp_offset(devp
));
99 static void *fdt_wrapper_get_parent(const void *devp
)
101 return offset_devp(fdt_parent_offset(fdt
, devp_offset(devp
)));
104 static void *fdt_wrapper_create_node(const void *devp
, const char *name
)
108 offset
= fdt_add_subnode(fdt
, devp_offset(devp
), name
);
109 if (offset
== -FDT_ERR_NOSPACE
) {
110 expand_buf(strlen(name
) + 16);
111 offset
= fdt_add_subnode(fdt
, devp_offset(devp
), name
);
114 return offset_devp(offset
);
117 static void *fdt_wrapper_find_node_by_prop_value(const void *prev
,
122 int offset
= fdt_node_offset_by_prop_value(fdt
, devp_offset_find(prev
),
124 return offset_devp(offset
);
127 static void *fdt_wrapper_find_node_by_compatible(const void *prev
,
130 int offset
= fdt_node_offset_by_compatible(fdt
, devp_offset_find(prev
),
132 return offset_devp(offset
);
135 static char *fdt_wrapper_get_path(const void *devp
, char *buf
, int len
)
139 rc
= fdt_get_path(fdt
, devp_offset(devp
), buf
, len
);
145 static unsigned long fdt_wrapper_finalize(void)
151 fatal("Couldn't pack flat tree: %s\n\r",
153 return (unsigned long)fdt
;
156 void fdt_init(void *blob
)
161 dt_ops
.finddevice
= fdt_wrapper_finddevice
;
162 dt_ops
.getprop
= fdt_wrapper_getprop
;
163 dt_ops
.setprop
= fdt_wrapper_setprop
;
164 dt_ops
.get_parent
= fdt_wrapper_get_parent
;
165 dt_ops
.create_node
= fdt_wrapper_create_node
;
166 dt_ops
.find_node_by_prop_value
= fdt_wrapper_find_node_by_prop_value
;
167 dt_ops
.find_node_by_compatible
= fdt_wrapper_find_node_by_compatible
;
168 dt_ops
.del_node
= fdt_wrapper_del_node
;
169 dt_ops
.get_path
= fdt_wrapper_get_path
;
170 dt_ops
.finalize
= fdt_wrapper_finalize
;
172 /* Make sure the dt blob is the right version and so forth */
174 bufsize
= fdt_totalsize(fdt
) + EXPAND_GRANULARITY
;
175 buf
= malloc(bufsize
);
177 fatal("malloc failed. can't relocate the device tree\n\r");
179 err
= fdt_open_into(fdt
, buf
, bufsize
);
182 fatal("fdt_init(): %s\n\r", fdt_strerror(err
));