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