]>
git.ipfire.org Git - people/ms/u-boot.git/blob - libfdt/fdt_rw.c
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libfdt_env.h"
27 #include "libfdt_internal.h"
29 static int rw_check_header(void *fdt
)
33 if ((err
= fdt_check_header(fdt
)))
35 if (fdt_version(fdt
) < 0x11)
36 return -FDT_ERR_BADVERSION
;
37 if (fdt_off_mem_rsvmap(fdt
) < ALIGN(sizeof(struct fdt_header
), 8))
38 return -FDT_ERR_BADLAYOUT
;
39 if (fdt_off_dt_struct(fdt
) <
40 (fdt_off_mem_rsvmap(fdt
) + sizeof(struct fdt_reserve_entry
)))
41 return -FDT_ERR_BADLAYOUT
;
42 if (fdt_off_dt_strings(fdt
) <
43 (fdt_off_dt_struct(fdt
) + fdt_size_dt_struct(fdt
)))
44 return -FDT_ERR_BADLAYOUT
;
45 if (fdt_totalsize(fdt
) <
46 (fdt_off_dt_strings(fdt
) + fdt_size_dt_strings(fdt
)))
47 return -FDT_ERR_BADLAYOUT
;
51 #define RW_CHECK_HEADER(fdt) \
54 if ((err = rw_check_header(fdt)) != 0) \
58 static inline int _blob_data_size(void *fdt
)
60 return fdt_off_dt_strings(fdt
) + fdt_size_dt_strings(fdt
);
63 static int _blob_splice(void *fdt
, void *p
, int oldlen
, int newlen
)
65 void *end
= fdt
+ _blob_data_size(fdt
);
67 if (((p
+ oldlen
) < p
) || ((p
+ oldlen
) > end
))
68 return -FDT_ERR_BADOFFSET
;
69 if ((end
- oldlen
+ newlen
) > (fdt
+ fdt_totalsize(fdt
)))
70 return -FDT_ERR_NOSPACE
;
71 memmove(p
+ newlen
, p
+ oldlen
, end
- p
- oldlen
);
75 static int _blob_splice_struct(void *fdt
, void *p
,
76 int oldlen
, int newlen
)
78 int delta
= newlen
- oldlen
;
81 if ((err
= _blob_splice(fdt
, p
, oldlen
, newlen
)))
84 fdt_set_header(fdt
, size_dt_struct
, fdt_size_dt_struct(fdt
) + delta
);
85 fdt_set_header(fdt
, off_dt_strings
, fdt_off_dt_strings(fdt
) + delta
);
89 static int _blob_splice_string(void *fdt
, int newlen
)
91 void *p
= fdt
+ fdt_off_dt_strings(fdt
) + fdt_size_dt_strings(fdt
);
94 if ((err
= _blob_splice(fdt
, p
, 0, newlen
)))
97 fdt_set_header(fdt
, size_dt_strings
, fdt_size_dt_strings(fdt
) + newlen
);
101 static int _find_add_string(void *fdt
, const char *s
)
103 char *strtab
= (char *)fdt
+ fdt_off_dt_strings(fdt
);
106 int len
= strlen(s
) + 1;
109 p
= _fdt_find_string(strtab
, fdt_size_dt_strings(fdt
), s
);
114 new = strtab
+ fdt_size_dt_strings(fdt
);
115 err
= _blob_splice_string(fdt
, len
);
120 return (new - strtab
);
123 static int _resize_property(void *fdt
, int nodeoffset
, const char *name
, int len
,
124 struct fdt_property
**prop
)
129 *prop
= fdt_get_property(fdt
, nodeoffset
, name
, &oldlen
);
133 if ((err
= _blob_splice_struct(fdt
, (*prop
)->data
,
134 ALIGN(oldlen
, FDT_TAGSIZE
),
135 ALIGN(len
, FDT_TAGSIZE
))))
138 (*prop
)->len
= cpu_to_fdt32(len
);
142 static int _add_property(void *fdt
, int nodeoffset
, const char *name
, int len
,
143 struct fdt_property
**prop
)
151 tag
= fdt_next_tag(fdt
, nodeoffset
, &nextoffset
, NULL
);
152 if (tag
!= FDT_BEGIN_NODE
)
153 return -FDT_ERR_BADOFFSET
;
155 namestroff
= _find_add_string(fdt
, name
);
159 *prop
= _fdt_offset_ptr(fdt
, nextoffset
);
160 proplen
= sizeof(**prop
) + ALIGN(len
, FDT_TAGSIZE
);
162 err
= _blob_splice_struct(fdt
, *prop
, 0, proplen
);
166 (*prop
)->tag
= cpu_to_fdt32(FDT_PROP
);
167 (*prop
)->nameoff
= cpu_to_fdt32(namestroff
);
168 (*prop
)->len
= cpu_to_fdt32(len
);
172 int fdt_setprop(void *fdt
, int nodeoffset
, const char *name
,
173 const void *val
, int len
)
175 struct fdt_property
*prop
;
178 if ((err
= rw_check_header(fdt
)))
181 err
= _resize_property(fdt
, nodeoffset
, name
, len
, &prop
);
182 if (err
== -FDT_ERR_NOTFOUND
)
183 err
= _add_property(fdt
, nodeoffset
, name
, len
, &prop
);
187 memcpy(prop
->data
, val
, len
);
191 int fdt_delprop(void *fdt
, int nodeoffset
, const char *name
)
193 struct fdt_property
*prop
;
196 RW_CHECK_HEADER(fdt
);
198 prop
= fdt_get_property(fdt
, nodeoffset
, name
, &len
);
202 proplen
= sizeof(*prop
) + ALIGN(len
, FDT_TAGSIZE
);
203 return _blob_splice_struct(fdt
, prop
, proplen
, 0);
206 int fdt_add_subnode_namelen(void *fdt
, int parentoffset
,
207 const char *name
, int namelen
)
209 struct fdt_node_header
*nh
;
210 int offset
, nextoffset
;
216 RW_CHECK_HEADER(fdt
);
218 offset
= fdt_subnode_offset_namelen(fdt
, parentoffset
, name
, namelen
);
220 return -FDT_ERR_EXISTS
;
221 else if (offset
!= -FDT_ERR_NOTFOUND
)
224 /* Try to place the new node after the parent's properties */
225 fdt_next_tag(fdt
, parentoffset
, &nextoffset
, NULL
); /* skip the BEGIN_NODE */
228 tag
= fdt_next_tag(fdt
, offset
, &nextoffset
, NULL
);
229 } while (tag
== FDT_PROP
);
231 nh
= _fdt_offset_ptr(fdt
, offset
);
232 nodelen
= sizeof(*nh
) + ALIGN(namelen
+1, FDT_TAGSIZE
) + FDT_TAGSIZE
;
234 err
= _blob_splice_struct(fdt
, nh
, 0, nodelen
);
238 nh
->tag
= cpu_to_fdt32(FDT_BEGIN_NODE
);
239 memset(nh
->name
, 0, ALIGN(namelen
+1, FDT_TAGSIZE
));
240 memcpy(nh
->name
, name
, namelen
);
241 endtag
= (uint32_t *)((void *)nh
+ nodelen
- FDT_TAGSIZE
);
242 *endtag
= cpu_to_fdt32(FDT_END_NODE
);
247 int fdt_add_subnode(void *fdt
, int parentoffset
, const char *name
)
249 return fdt_add_subnode_namelen(fdt
, parentoffset
, name
, strlen(name
));
252 int fdt_del_node(void *fdt
, int nodeoffset
)
256 endoffset
= _fdt_node_end_offset(fdt
, nodeoffset
);
260 return _blob_splice_struct(fdt
, _fdt_offset_ptr(fdt
, nodeoffset
),
261 endoffset
- nodeoffset
, 0);
264 int fdt_open_into(void *fdt
, void *buf
, int bufsize
)
268 err
= fdt_move(fdt
, buf
, bufsize
);
274 fdt_set_header(fdt
, totalsize
, bufsize
);
276 /* FIXME: re-order if necessary */
278 err
= rw_check_header(fdt
);
285 int fdt_pack(void *fdt
)
289 err
= rw_check_header(fdt
);
293 /* FIXME: pack components */
294 fdt_set_header(fdt
, totalsize
, _blob_data_size(fdt
));
298 #endif /* CONFIG_OF_LIBFDT */