Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * libfdt - Flat Device Tree manipulation |
| 3 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | */ |
| 19 | #include "libfdt_env.h" |
| 20 | |
| 21 | #include <fdt.h> |
| 22 | #include <libfdt.h> |
| 23 | |
| 24 | #include "libfdt_internal.h" |
| 25 | |
| 26 | static int rw_check_header(void *fdt) |
| 27 | { |
| 28 | int err; |
| 29 | |
Gerald Van Baren | 6679f92 | 2007-04-06 14:17:14 -0400 | [diff] [blame] | 30 | if ((err = fdt_check_header(fdt))) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 31 | return err; |
| 32 | if (fdt_version(fdt) < 0x11) |
| 33 | return -FDT_ERR_BADVERSION; |
| 34 | if (fdt_off_mem_rsvmap(fdt) < ALIGN(sizeof(struct fdt_header), 8)) |
| 35 | return -FDT_ERR_BADLAYOUT; |
| 36 | if (fdt_off_dt_struct(fdt) < |
| 37 | (fdt_off_mem_rsvmap(fdt) + sizeof(struct fdt_reserve_entry))) |
| 38 | return -FDT_ERR_BADLAYOUT; |
| 39 | if (fdt_off_dt_strings(fdt) < |
| 40 | (fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt))) |
| 41 | return -FDT_ERR_BADLAYOUT; |
| 42 | if (fdt_totalsize(fdt) < |
| 43 | (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt))) |
| 44 | return -FDT_ERR_BADLAYOUT; |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | #define RW_CHECK_HEADER(fdt) \ |
| 49 | { \ |
| 50 | int err; \ |
| 51 | if ((err = rw_check_header(fdt)) != 0) \ |
| 52 | return err; \ |
| 53 | } |
| 54 | |
| 55 | static inline int _blob_data_size(void *fdt) |
| 56 | { |
| 57 | return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt); |
| 58 | } |
| 59 | |
| 60 | static int _blob_splice(void *fdt, void *p, int oldlen, int newlen) |
| 61 | { |
| 62 | void *end = fdt + _blob_data_size(fdt); |
| 63 | |
| 64 | if (((p + oldlen) < p) || ((p + oldlen) > end)) |
| 65 | return -FDT_ERR_BADOFFSET; |
| 66 | if ((end - oldlen + newlen) > (fdt + fdt_totalsize(fdt))) |
| 67 | return -FDT_ERR_NOSPACE; |
| 68 | memmove(p + newlen, p + oldlen, end - p - oldlen); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int _blob_splice_struct(void *fdt, void *p, |
| 73 | int oldlen, int newlen) |
| 74 | { |
| 75 | int delta = newlen - oldlen; |
| 76 | int err; |
| 77 | |
| 78 | if ((err = _blob_splice(fdt, p, oldlen, newlen))) |
| 79 | return err; |
| 80 | |
| 81 | fdt_set_header(fdt, size_dt_struct, fdt_size_dt_struct(fdt) + delta); |
| 82 | fdt_set_header(fdt, off_dt_strings, fdt_off_dt_strings(fdt) + delta); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int _blob_splice_string(void *fdt, int newlen) |
| 87 | { |
| 88 | void *p = fdt + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt); |
| 89 | int err; |
| 90 | |
| 91 | if ((err = _blob_splice(fdt, p, 0, newlen))) |
| 92 | return err; |
| 93 | |
| 94 | fdt_set_header(fdt, size_dt_strings, fdt_size_dt_strings(fdt) + newlen); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int _find_add_string(void *fdt, const char *s) |
| 99 | { |
| 100 | char *strtab = (char *)fdt + fdt_off_dt_strings(fdt); |
| 101 | const char *p; |
| 102 | char *new; |
| 103 | int len = strlen(s) + 1; |
| 104 | int err; |
| 105 | |
| 106 | p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s); |
| 107 | if (p) |
| 108 | /* found it */ |
| 109 | return (p - strtab); |
| 110 | |
| 111 | new = strtab + fdt_size_dt_strings(fdt); |
| 112 | err = _blob_splice_string(fdt, len); |
| 113 | if (err) |
| 114 | return err; |
| 115 | |
| 116 | memcpy(new, s, len); |
| 117 | return (new - strtab); |
| 118 | } |
| 119 | |
| 120 | static int _resize_property(void *fdt, int nodeoffset, const char *name, int len, |
| 121 | struct fdt_property **prop) |
| 122 | { |
| 123 | int oldlen; |
| 124 | int err; |
| 125 | |
| 126 | *prop = fdt_get_property(fdt, nodeoffset, name, &oldlen); |
| 127 | if (! (*prop)) |
| 128 | return oldlen; |
| 129 | |
| 130 | if ((err = _blob_splice_struct(fdt, (*prop)->data, |
| 131 | ALIGN(oldlen, FDT_TAGSIZE), |
| 132 | ALIGN(len, FDT_TAGSIZE)))) |
| 133 | return err; |
| 134 | |
| 135 | (*prop)->len = cpu_to_fdt32(len); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int _add_property(void *fdt, int nodeoffset, const char *name, int len, |
| 140 | struct fdt_property **prop) |
| 141 | { |
| 142 | uint32_t tag; |
| 143 | int proplen; |
| 144 | int nextoffset; |
| 145 | int namestroff; |
| 146 | int err; |
| 147 | |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 148 | tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 149 | if (tag != FDT_BEGIN_NODE) |
| 150 | return -FDT_ERR_BADOFFSET; |
| 151 | |
| 152 | namestroff = _find_add_string(fdt, name); |
| 153 | if (namestroff < 0) |
| 154 | return namestroff; |
| 155 | |
| 156 | *prop = _fdt_offset_ptr(fdt, nextoffset); |
| 157 | proplen = sizeof(**prop) + ALIGN(len, FDT_TAGSIZE); |
| 158 | |
| 159 | err = _blob_splice_struct(fdt, *prop, 0, proplen); |
| 160 | if (err) |
| 161 | return err; |
| 162 | |
| 163 | (*prop)->tag = cpu_to_fdt32(FDT_PROP); |
| 164 | (*prop)->nameoff = cpu_to_fdt32(namestroff); |
| 165 | (*prop)->len = cpu_to_fdt32(len); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | int fdt_setprop(void *fdt, int nodeoffset, const char *name, |
| 170 | const void *val, int len) |
| 171 | { |
| 172 | struct fdt_property *prop; |
| 173 | int err; |
| 174 | |
| 175 | if ((err = rw_check_header(fdt))) |
| 176 | return err; |
| 177 | |
| 178 | err = _resize_property(fdt, nodeoffset, name, len, &prop); |
| 179 | if (err == -FDT_ERR_NOTFOUND) |
| 180 | err = _add_property(fdt, nodeoffset, name, len, &prop); |
| 181 | if (err) |
| 182 | return err; |
| 183 | |
| 184 | memcpy(prop->data, val, len); |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | int fdt_delprop(void *fdt, int nodeoffset, const char *name) |
| 189 | { |
| 190 | struct fdt_property *prop; |
| 191 | int len, proplen; |
| 192 | |
| 193 | RW_CHECK_HEADER(fdt); |
| 194 | |
| 195 | prop = fdt_get_property(fdt, nodeoffset, name, &len); |
| 196 | if (! prop) |
| 197 | return len; |
| 198 | |
| 199 | proplen = sizeof(*prop) + ALIGN(len, FDT_TAGSIZE); |
| 200 | return _blob_splice_struct(fdt, prop, proplen, 0); |
| 201 | } |
| 202 | |
| 203 | int fdt_add_subnode_namelen(void *fdt, int parentoffset, |
| 204 | const char *name, int namelen) |
| 205 | { |
| 206 | struct fdt_node_header *nh; |
| 207 | int offset, nextoffset; |
| 208 | int nodelen; |
| 209 | int err; |
| 210 | uint32_t tag; |
| 211 | uint32_t *endtag; |
| 212 | |
| 213 | RW_CHECK_HEADER(fdt); |
| 214 | |
| 215 | offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen); |
| 216 | if (offset >= 0) |
| 217 | return -FDT_ERR_EXISTS; |
| 218 | else if (offset != -FDT_ERR_NOTFOUND) |
| 219 | return offset; |
| 220 | |
| 221 | /* Try to place the new node after the parent's properties */ |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 222 | fdt_next_tag(fdt, parentoffset, &nextoffset, NULL); /* skip the BEGIN_NODE */ |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 223 | do { |
| 224 | offset = nextoffset; |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 225 | tag = fdt_next_tag(fdt, offset, &nextoffset, NULL); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 226 | } while (tag == FDT_PROP); |
| 227 | |
| 228 | nh = _fdt_offset_ptr(fdt, offset); |
| 229 | nodelen = sizeof(*nh) + ALIGN(namelen+1, FDT_TAGSIZE) + FDT_TAGSIZE; |
| 230 | |
| 231 | err = _blob_splice_struct(fdt, nh, 0, nodelen); |
| 232 | if (err) |
| 233 | return err; |
| 234 | |
| 235 | nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE); |
| 236 | memset(nh->name, 0, ALIGN(namelen+1, FDT_TAGSIZE)); |
| 237 | memcpy(nh->name, name, namelen); |
| 238 | endtag = (uint32_t *)((void *)nh + nodelen - FDT_TAGSIZE); |
| 239 | *endtag = cpu_to_fdt32(FDT_END_NODE); |
| 240 | |
| 241 | return offset; |
| 242 | } |
| 243 | |
| 244 | int fdt_add_subnode(void *fdt, int parentoffset, const char *name) |
| 245 | { |
| 246 | return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name)); |
| 247 | } |
| 248 | |
| 249 | int fdt_del_node(void *fdt, int nodeoffset) |
| 250 | { |
| 251 | int endoffset; |
| 252 | |
| 253 | endoffset = _fdt_node_end_offset(fdt, nodeoffset); |
| 254 | if (endoffset < 0) |
| 255 | return endoffset; |
| 256 | |
| 257 | return _blob_splice_struct(fdt, _fdt_offset_ptr(fdt, nodeoffset), |
| 258 | endoffset - nodeoffset, 0); |
| 259 | } |
| 260 | |
| 261 | int fdt_open_into(void *fdt, void *buf, int bufsize) |
| 262 | { |
| 263 | int err; |
| 264 | |
| 265 | err = fdt_move(fdt, buf, bufsize); |
| 266 | if (err) |
| 267 | return err; |
| 268 | |
| 269 | fdt = buf; |
| 270 | |
| 271 | fdt_set_header(fdt, totalsize, bufsize); |
| 272 | |
| 273 | /* FIXME: re-order if necessary */ |
| 274 | |
| 275 | err = rw_check_header(fdt); |
| 276 | if (err) |
| 277 | return err; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | int fdt_pack(void *fdt) |
| 283 | { |
| 284 | int err; |
| 285 | |
| 286 | err = rw_check_header(fdt); |
| 287 | if (err) |
| 288 | return err; |
| 289 | |
| 290 | /* FIXME: pack components */ |
| 291 | fdt_set_header(fdt, totalsize, _blob_data_size(fdt)); |
| 292 | return 0; |
| 293 | } |