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. |
Roger Meier | 3508476 | 2013-07-27 01:12:38 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 5 | */ |
Robert P. J. Day | 6feed2a | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 6 | #include <libfdt_env.h> |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 7 | |
Bartlomiej Sieka | 8cf3080 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 8 | #ifndef USE_HOSTCC |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 9 | #include <fdt.h> |
| 10 | #include <libfdt.h> |
Bartlomiej Sieka | 8cf3080 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 11 | #else |
| 12 | #include "fdt_host.h" |
| 13 | #endif |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 14 | |
| 15 | #include "libfdt_internal.h" |
| 16 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 17 | static int _fdt_blocks_misordered(const void *fdt, |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 18 | int mem_rsv_size, int struct_size) |
| 19 | { |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 20 | return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8)) |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 21 | || (fdt_off_dt_struct(fdt) < |
| 22 | (fdt_off_mem_rsvmap(fdt) + mem_rsv_size)) |
| 23 | || (fdt_off_dt_strings(fdt) < |
| 24 | (fdt_off_dt_struct(fdt) + struct_size)) |
| 25 | || (fdt_totalsize(fdt) < |
| 26 | (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt))); |
| 27 | } |
| 28 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 29 | static int _fdt_rw_check_header(void *fdt) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 30 | { |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 31 | FDT_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 32 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 33 | if (fdt_version(fdt) < 17) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 34 | return -FDT_ERR_BADVERSION; |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 35 | if (_fdt_blocks_misordered(fdt, sizeof(struct fdt_reserve_entry), |
| 36 | fdt_size_dt_struct(fdt))) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 37 | return -FDT_ERR_BADLAYOUT; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 38 | if (fdt_version(fdt) > 17) |
| 39 | fdt_set_version(fdt, 17); |
| 40 | |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 41 | return 0; |
| 42 | } |
| 43 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 44 | #define FDT_RW_CHECK_HEADER(fdt) \ |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 45 | { \ |
Simon Glass | 0596d35 | 2014-07-30 03:59:03 -0600 | [diff] [blame] | 46 | int __err; \ |
| 47 | if ((__err = _fdt_rw_check_header(fdt)) != 0) \ |
| 48 | return __err; \ |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 49 | } |
| 50 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 51 | static inline int _fdt_data_size(void *fdt) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 52 | { |
| 53 | return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt); |
| 54 | } |
| 55 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 56 | static int _fdt_splice(void *fdt, void *splicepoint, int oldlen, int newlen) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 57 | { |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 58 | char *p = splicepoint; |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 59 | char *end = (char *)fdt + _fdt_data_size(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 60 | |
| 61 | if (((p + oldlen) < p) || ((p + oldlen) > end)) |
| 62 | return -FDT_ERR_BADOFFSET; |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 63 | if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt))) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 64 | return -FDT_ERR_NOSPACE; |
| 65 | memmove(p + newlen, p + oldlen, end - p - oldlen); |
| 66 | return 0; |
| 67 | } |
| 68 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 69 | static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p, |
| 70 | int oldn, int newn) |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 71 | { |
| 72 | int delta = (newn - oldn) * sizeof(*p); |
| 73 | int err; |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 74 | err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p)); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 75 | if (err) |
| 76 | return err; |
| 77 | fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta); |
| 78 | fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta); |
| 79 | return 0; |
| 80 | } |
| 81 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 82 | static int _fdt_splice_struct(void *fdt, void *p, |
| 83 | int oldlen, int newlen) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 84 | { |
| 85 | int delta = newlen - oldlen; |
| 86 | int err; |
| 87 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 88 | if ((err = _fdt_splice(fdt, p, oldlen, newlen))) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 89 | return err; |
| 90 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 91 | fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta); |
| 92 | fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 96 | static int _fdt_splice_string(void *fdt, int newlen) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 97 | { |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 98 | void *p = (char *)fdt |
| 99 | + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 100 | int err; |
| 101 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 102 | if ((err = _fdt_splice(fdt, p, 0, newlen))) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 103 | return err; |
| 104 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 105 | fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 109 | static int _fdt_find_add_string(void *fdt, const char *s) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 110 | { |
| 111 | char *strtab = (char *)fdt + fdt_off_dt_strings(fdt); |
| 112 | const char *p; |
| 113 | char *new; |
| 114 | int len = strlen(s) + 1; |
| 115 | int err; |
| 116 | |
| 117 | p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s); |
| 118 | if (p) |
| 119 | /* found it */ |
| 120 | return (p - strtab); |
| 121 | |
| 122 | new = strtab + fdt_size_dt_strings(fdt); |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 123 | err = _fdt_splice_string(fdt, len); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 124 | if (err) |
| 125 | return err; |
| 126 | |
| 127 | memcpy(new, s, len); |
| 128 | return (new - strtab); |
| 129 | } |
| 130 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 131 | int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size) |
| 132 | { |
| 133 | struct fdt_reserve_entry *re; |
| 134 | int err; |
| 135 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 136 | FDT_RW_CHECK_HEADER(fdt); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 137 | |
| 138 | re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt)); |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 139 | err = _fdt_splice_mem_rsv(fdt, re, 0, 1); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 140 | if (err) |
| 141 | return err; |
| 142 | |
| 143 | re->address = cpu_to_fdt64(address); |
| 144 | re->size = cpu_to_fdt64(size); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | int fdt_del_mem_rsv(void *fdt, int n) |
| 149 | { |
| 150 | struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 151 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 152 | FDT_RW_CHECK_HEADER(fdt); |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 153 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 154 | if (n >= fdt_num_mem_rsv(fdt)) |
| 155 | return -FDT_ERR_NOTFOUND; |
| 156 | |
Masahiro Yamada | 7dc0789 | 2016-09-06 22:17:37 +0900 | [diff] [blame^] | 157 | return _fdt_splice_mem_rsv(fdt, re, 1, 0); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 158 | } |
| 159 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 160 | static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name, |
| 161 | int len, struct fdt_property **prop) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 162 | { |
| 163 | int oldlen; |
| 164 | int err; |
| 165 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 166 | *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen); |
Robert P. J. Day | 6feed2a | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 167 | if (!(*prop)) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 168 | return oldlen; |
| 169 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 170 | if ((err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen), |
| 171 | FDT_TAGALIGN(len)))) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 172 | return err; |
| 173 | |
| 174 | (*prop)->len = cpu_to_fdt32(len); |
| 175 | return 0; |
| 176 | } |
| 177 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 178 | static int _fdt_add_property(void *fdt, int nodeoffset, const char *name, |
| 179 | int len, struct fdt_property **prop) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 180 | { |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 181 | int proplen; |
| 182 | int nextoffset; |
| 183 | int namestroff; |
| 184 | int err; |
| 185 | |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 186 | if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0) |
| 187 | return nextoffset; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 188 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 189 | namestroff = _fdt_find_add_string(fdt, name); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 190 | if (namestroff < 0) |
| 191 | return namestroff; |
| 192 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 193 | *prop = _fdt_offset_ptr_w(fdt, nextoffset); |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 194 | proplen = sizeof(**prop) + FDT_TAGALIGN(len); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 195 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 196 | err = _fdt_splice_struct(fdt, *prop, 0, proplen); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 197 | if (err) |
| 198 | return err; |
| 199 | |
| 200 | (*prop)->tag = cpu_to_fdt32(FDT_PROP); |
| 201 | (*prop)->nameoff = cpu_to_fdt32(namestroff); |
| 202 | (*prop)->len = cpu_to_fdt32(len); |
| 203 | return 0; |
| 204 | } |
| 205 | |
David Gibson | 9eaeb07 | 2008-01-11 14:55:05 +1100 | [diff] [blame] | 206 | int fdt_set_name(void *fdt, int nodeoffset, const char *name) |
| 207 | { |
| 208 | char *namep; |
| 209 | int oldlen, newlen; |
| 210 | int err; |
| 211 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 212 | FDT_RW_CHECK_HEADER(fdt); |
David Gibson | 9eaeb07 | 2008-01-11 14:55:05 +1100 | [diff] [blame] | 213 | |
David Gibson | c668302 | 2008-07-07 10:14:15 +1000 | [diff] [blame] | 214 | namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen); |
David Gibson | 9eaeb07 | 2008-01-11 14:55:05 +1100 | [diff] [blame] | 215 | if (!namep) |
| 216 | return oldlen; |
| 217 | |
| 218 | newlen = strlen(name); |
| 219 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 220 | err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1), |
| 221 | FDT_TAGALIGN(newlen+1)); |
David Gibson | 9eaeb07 | 2008-01-11 14:55:05 +1100 | [diff] [blame] | 222 | if (err) |
| 223 | return err; |
| 224 | |
| 225 | memcpy(namep, name, newlen+1); |
| 226 | return 0; |
| 227 | } |
| 228 | |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 229 | int fdt_setprop(void *fdt, int nodeoffset, const char *name, |
| 230 | const void *val, int len) |
| 231 | { |
| 232 | struct fdt_property *prop; |
| 233 | int err; |
| 234 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 235 | FDT_RW_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 236 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 237 | err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 238 | if (err == -FDT_ERR_NOTFOUND) |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 239 | err = _fdt_add_property(fdt, nodeoffset, name, len, &prop); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 240 | if (err) |
| 241 | return err; |
| 242 | |
| 243 | memcpy(prop->data, val, len); |
| 244 | return 0; |
| 245 | } |
| 246 | |
Minghuan Lian | 36ad18a | 2012-08-27 19:38:59 -0500 | [diff] [blame] | 247 | int fdt_appendprop(void *fdt, int nodeoffset, const char *name, |
| 248 | const void *val, int len) |
| 249 | { |
| 250 | struct fdt_property *prop; |
| 251 | int err, oldlen, newlen; |
| 252 | |
| 253 | FDT_RW_CHECK_HEADER(fdt); |
| 254 | |
| 255 | prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen); |
| 256 | if (prop) { |
| 257 | newlen = len + oldlen; |
| 258 | err = _fdt_splice_struct(fdt, prop->data, |
| 259 | FDT_TAGALIGN(oldlen), |
| 260 | FDT_TAGALIGN(newlen)); |
| 261 | if (err) |
| 262 | return err; |
| 263 | prop->len = cpu_to_fdt32(newlen); |
| 264 | memcpy(prop->data + oldlen, val, len); |
| 265 | } else { |
| 266 | err = _fdt_add_property(fdt, nodeoffset, name, len, &prop); |
| 267 | if (err) |
| 268 | return err; |
| 269 | memcpy(prop->data, val, len); |
| 270 | } |
| 271 | return 0; |
| 272 | } |
| 273 | |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 274 | int fdt_delprop(void *fdt, int nodeoffset, const char *name) |
| 275 | { |
| 276 | struct fdt_property *prop; |
| 277 | int len, proplen; |
| 278 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 279 | FDT_RW_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 280 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 281 | prop = fdt_get_property_w(fdt, nodeoffset, name, &len); |
Robert P. J. Day | 6feed2a | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 282 | if (!prop) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 283 | return len; |
| 284 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 285 | proplen = sizeof(*prop) + FDT_TAGALIGN(len); |
| 286 | return _fdt_splice_struct(fdt, prop, proplen, 0); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | int fdt_add_subnode_namelen(void *fdt, int parentoffset, |
| 290 | const char *name, int namelen) |
| 291 | { |
| 292 | struct fdt_node_header *nh; |
| 293 | int offset, nextoffset; |
| 294 | int nodelen; |
| 295 | int err; |
| 296 | uint32_t tag; |
Kim Phillips | b2ba62a | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 297 | fdt32_t *endtag; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 298 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 299 | FDT_RW_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 300 | |
| 301 | offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen); |
| 302 | if (offset >= 0) |
| 303 | return -FDT_ERR_EXISTS; |
| 304 | else if (offset != -FDT_ERR_NOTFOUND) |
| 305 | return offset; |
| 306 | |
| 307 | /* Try to place the new node after the parent's properties */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 308 | fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */ |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 309 | do { |
| 310 | offset = nextoffset; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 311 | tag = fdt_next_tag(fdt, offset, &nextoffset); |
David Gibson | f84d65f | 2008-02-14 16:50:34 +1100 | [diff] [blame] | 312 | } while ((tag == FDT_PROP) || (tag == FDT_NOP)); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 313 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 314 | nh = _fdt_offset_ptr_w(fdt, offset); |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 315 | nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 316 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 317 | err = _fdt_splice_struct(fdt, nh, 0, nodelen); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 318 | if (err) |
| 319 | return err; |
| 320 | |
| 321 | nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE); |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 322 | memset(nh->name, 0, FDT_TAGALIGN(namelen+1)); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 323 | memcpy(nh->name, name, namelen); |
Kim Phillips | b2ba62a | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 324 | endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 325 | *endtag = cpu_to_fdt32(FDT_END_NODE); |
| 326 | |
| 327 | return offset; |
| 328 | } |
| 329 | |
| 330 | int fdt_add_subnode(void *fdt, int parentoffset, const char *name) |
| 331 | { |
| 332 | return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name)); |
| 333 | } |
| 334 | |
| 335 | int fdt_del_node(void *fdt, int nodeoffset) |
| 336 | { |
| 337 | int endoffset; |
| 338 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 339 | FDT_RW_CHECK_HEADER(fdt); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 340 | |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 341 | endoffset = _fdt_node_end_offset(fdt, nodeoffset); |
| 342 | if (endoffset < 0) |
| 343 | return endoffset; |
| 344 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 345 | return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset), |
| 346 | endoffset - nodeoffset, 0); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 347 | } |
| 348 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 349 | static void _fdt_packblocks(const char *old, char *new, |
| 350 | int mem_rsv_size, int struct_size) |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 351 | { |
| 352 | int mem_rsv_off, struct_off, strings_off; |
| 353 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 354 | mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 355 | struct_off = mem_rsv_off + mem_rsv_size; |
| 356 | strings_off = struct_off + struct_size; |
| 357 | |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 358 | memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size); |
| 359 | fdt_set_off_mem_rsvmap(new, mem_rsv_off); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 360 | |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 361 | memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size); |
| 362 | fdt_set_off_dt_struct(new, struct_off); |
| 363 | fdt_set_size_dt_struct(new, struct_size); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 364 | |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 365 | memmove(new + strings_off, old + fdt_off_dt_strings(old), |
| 366 | fdt_size_dt_strings(old)); |
| 367 | fdt_set_off_dt_strings(new, strings_off); |
| 368 | fdt_set_size_dt_strings(new, fdt_size_dt_strings(old)); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | int fdt_open_into(const void *fdt, void *buf, int bufsize) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 372 | { |
| 373 | int err; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 374 | int mem_rsv_size, struct_size; |
| 375 | int newsize; |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 376 | const char *fdtstart = fdt; |
| 377 | const char *fdtend = fdtstart + fdt_totalsize(fdt); |
| 378 | char *tmp; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 379 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 380 | FDT_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 381 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 382 | mem_rsv_size = (fdt_num_mem_rsv(fdt)+1) |
| 383 | * sizeof(struct fdt_reserve_entry); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 384 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 385 | if (fdt_version(fdt) >= 17) { |
| 386 | struct_size = fdt_size_dt_struct(fdt); |
| 387 | } else { |
| 388 | struct_size = 0; |
| 389 | while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END) |
| 390 | ; |
David Gibson | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 391 | if (struct_size < 0) |
| 392 | return struct_size; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 393 | } |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 394 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 395 | if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) { |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 396 | /* no further work necessary */ |
| 397 | err = fdt_move(fdt, buf, bufsize); |
| 398 | if (err) |
| 399 | return err; |
| 400 | fdt_set_version(buf, 17); |
| 401 | fdt_set_size_dt_struct(buf, struct_size); |
| 402 | fdt_set_totalsize(buf, bufsize); |
| 403 | return 0; |
| 404 | } |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 405 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 406 | /* Need to reorder */ |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 407 | newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 408 | + struct_size + fdt_size_dt_strings(fdt); |
| 409 | |
| 410 | if (bufsize < newsize) |
| 411 | return -FDT_ERR_NOSPACE; |
| 412 | |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 413 | /* First attempt to build converted tree at beginning of buffer */ |
| 414 | tmp = buf; |
| 415 | /* But if that overlaps with the old tree... */ |
| 416 | if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) { |
| 417 | /* Try right after the old tree instead */ |
David Gibson | c668302 | 2008-07-07 10:14:15 +1000 | [diff] [blame] | 418 | tmp = (char *)(uintptr_t)fdtend; |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 419 | if ((tmp + newsize) > ((char *)buf + bufsize)) |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 420 | return -FDT_ERR_NOSPACE; |
| 421 | } |
| 422 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 423 | _fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 424 | memmove(buf, tmp, newsize); |
| 425 | |
| 426 | fdt_set_magic(buf, FDT_MAGIC); |
| 427 | fdt_set_totalsize(buf, bufsize); |
| 428 | fdt_set_version(buf, 17); |
| 429 | fdt_set_last_comp_version(buf, 16); |
| 430 | fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt)); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | int fdt_pack(void *fdt) |
| 436 | { |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 437 | int mem_rsv_size; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 438 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 439 | FDT_RW_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 440 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 441 | mem_rsv_size = (fdt_num_mem_rsv(fdt)+1) |
| 442 | * sizeof(struct fdt_reserve_entry); |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 443 | _fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt)); |
| 444 | fdt_set_totalsize(fdt, _fdt_data_size(fdt)); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 445 | |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 446 | return 0; |
| 447 | } |
Simon Glass | 8f6e2e1 | 2015-06-23 15:38:26 -0600 | [diff] [blame] | 448 | |
| 449 | int fdt_remove_unused_strings(const void *old, void *new) |
| 450 | { |
| 451 | const struct fdt_property *old_prop; |
| 452 | struct fdt_property *new_prop; |
| 453 | int size = fdt_totalsize(old); |
| 454 | int next_offset, offset; |
| 455 | const char *str; |
| 456 | int ret; |
| 457 | int tag = FDT_PROP; |
| 458 | |
| 459 | /* Make a copy and remove the strings */ |
| 460 | memcpy(new, old, size); |
| 461 | fdt_set_size_dt_strings(new, 0); |
| 462 | |
| 463 | /* Add every property name back into the new string table */ |
| 464 | for (offset = 0; tag != FDT_END; offset = next_offset) { |
| 465 | tag = fdt_next_tag(old, offset, &next_offset); |
| 466 | if (tag != FDT_PROP) |
| 467 | continue; |
| 468 | old_prop = fdt_get_property_by_offset(old, offset, NULL); |
| 469 | new_prop = (struct fdt_property *)(unsigned long) |
| 470 | fdt_get_property_by_offset(new, offset, NULL); |
| 471 | str = fdt_string(old, fdt32_to_cpu(old_prop->nameoff)); |
| 472 | ret = _fdt_find_add_string(new, str); |
| 473 | if (ret < 0) |
| 474 | return ret; |
| 475 | new_prop->nameoff = cpu_to_fdt32(ret); |
| 476 | } |
| 477 | |
| 478 | return 0; |
| 479 | } |