Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 2 | /* |
| 3 | * libfdt - Flat Device Tree manipulation |
| 4 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 5 | */ |
| 6 | #include "libfdt_env.h" |
| 7 | |
| 8 | #include <fdt.h> |
| 9 | #include <libfdt.h> |
| 10 | |
| 11 | #include "libfdt_internal.h" |
| 12 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 13 | static int fdt_sw_probe_(void *fdt) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 14 | { |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 15 | if (fdt_chk_basic()) { |
| 16 | if (fdt_magic(fdt) == FDT_MAGIC) |
| 17 | return -FDT_ERR_BADSTATE; |
| 18 | else if (fdt_magic(fdt) != FDT_SW_MAGIC) |
| 19 | return -FDT_ERR_BADMAGIC; |
| 20 | } |
| 21 | |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 22 | return 0; |
| 23 | } |
| 24 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 25 | #define FDT_SW_PROBE(fdt) \ |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 26 | { \ |
| 27 | int err; \ |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 28 | if (fdt_chk_basic() && (err = fdt_sw_probe_(fdt)) != 0) \ |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 29 | return err; \ |
| 30 | } |
| 31 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 32 | /* 'memrsv' state: Initial state after fdt_create() |
| 33 | * |
| 34 | * Allowed functions: |
| 35 | * fdt_add_reservmap_entry() |
| 36 | * fdt_finish_reservemap() [moves to 'struct' state] |
| 37 | */ |
| 38 | static int fdt_sw_probe_memrsv_(void *fdt) |
| 39 | { |
| 40 | int err = fdt_sw_probe_(fdt); |
| 41 | if (err) |
| 42 | return err; |
| 43 | |
| 44 | if (fdt_chk_extra() && fdt_off_dt_strings(fdt) != 0) |
| 45 | return -FDT_ERR_BADSTATE; |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | #define FDT_SW_PROBE_MEMRSV(fdt) \ |
| 50 | { \ |
| 51 | int err; \ |
| 52 | if (fdt_chk_extra() && (err = fdt_sw_probe_memrsv_(fdt)) != 0) \ |
| 53 | return err; \ |
| 54 | } |
| 55 | |
| 56 | /* 'struct' state: Enter this state after fdt_finish_reservemap() |
| 57 | * |
| 58 | * Allowed functions: |
| 59 | * fdt_begin_node() |
| 60 | * fdt_end_node() |
| 61 | * fdt_property*() |
| 62 | * fdt_finish() [moves to 'complete' state] |
| 63 | */ |
| 64 | static int fdt_sw_probe_struct_(void *fdt) |
| 65 | { |
| 66 | int err; |
| 67 | |
| 68 | if (!fdt_chk_extra()) |
| 69 | return 0; |
| 70 | err = fdt_sw_probe_(fdt); |
| 71 | if (err) |
| 72 | return err; |
| 73 | |
| 74 | if (fdt_off_dt_strings(fdt) != fdt_totalsize(fdt)) |
| 75 | return -FDT_ERR_BADSTATE; |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | #define FDT_SW_PROBE_STRUCT(fdt) \ |
| 80 | { \ |
| 81 | int err; \ |
| 82 | if (fdt_chk_extra() && (err = fdt_sw_probe_struct_(fdt)) != 0) \ |
| 83 | return err; \ |
| 84 | } |
| 85 | |
| 86 | static inline uint32_t sw_flags(void *fdt) |
| 87 | { |
| 88 | /* assert: (fdt_magic(fdt) == FDT_SW_MAGIC) */ |
| 89 | return fdt_last_comp_version(fdt); |
| 90 | } |
| 91 | |
| 92 | /* 'complete' state: Enter this state after fdt_finish() |
| 93 | * |
| 94 | * Allowed functions: none |
| 95 | */ |
| 96 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 97 | static void *fdt_grab_space_(void *fdt, size_t len) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 98 | { |
Andre Przywara | 832bfad | 2020-10-16 15:42:50 +0100 | [diff] [blame] | 99 | unsigned int offset = fdt_size_dt_struct(fdt); |
| 100 | unsigned int spaceleft; |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 101 | |
| 102 | spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt) |
| 103 | - fdt_size_dt_strings(fdt); |
| 104 | |
| 105 | if ((offset + len < offset) || (offset + len > spaceleft)) |
| 106 | return NULL; |
| 107 | |
| 108 | fdt_set_size_dt_struct(fdt, offset + len); |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 109 | return fdt_offset_ptr_w_(fdt, offset); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 112 | int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 113 | { |
Andre Przywara | 832bfad | 2020-10-16 15:42:50 +0100 | [diff] [blame] | 114 | const int hdrsize = FDT_ALIGN(sizeof(struct fdt_header), |
| 115 | sizeof(struct fdt_reserve_entry)); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 116 | void *fdt = buf; |
| 117 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 118 | if (bufsize < hdrsize) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 119 | return -FDT_ERR_NOSPACE; |
| 120 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 121 | if (flags & ~FDT_CREATE_FLAGS_ALL) |
| 122 | return -FDT_ERR_BADFLAGS; |
| 123 | |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 124 | memset(buf, 0, bufsize); |
| 125 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 126 | /* |
| 127 | * magic and last_comp_version keep intermediate state during the fdt |
| 128 | * creation process, which is replaced with the proper FDT format by |
| 129 | * fdt_finish(). |
| 130 | * |
| 131 | * flags should be accessed with sw_flags(). |
| 132 | */ |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 133 | fdt_set_magic(fdt, FDT_SW_MAGIC); |
| 134 | fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION); |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 135 | fdt_set_last_comp_version(fdt, flags); |
| 136 | |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 137 | fdt_set_totalsize(fdt, bufsize); |
| 138 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 139 | fdt_set_off_mem_rsvmap(fdt, hdrsize); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 140 | fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt)); |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 141 | fdt_set_off_dt_strings(fdt, 0); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 146 | int fdt_create(void *buf, int bufsize) |
| 147 | { |
| 148 | return fdt_create_with_flags(buf, bufsize, 0); |
| 149 | } |
| 150 | |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 151 | int fdt_resize(void *fdt, void *buf, int bufsize) |
| 152 | { |
| 153 | size_t headsize, tailsize; |
| 154 | char *oldtail, *newtail; |
| 155 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 156 | FDT_SW_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 157 | |
Andre Przywara | 832bfad | 2020-10-16 15:42:50 +0100 | [diff] [blame] | 158 | if (bufsize < 0) |
| 159 | return -FDT_ERR_NOSPACE; |
| 160 | |
Simon Glass | 89d6690 | 2019-07-20 12:23:59 -0600 | [diff] [blame] | 161 | headsize = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 162 | tailsize = fdt_size_dt_strings(fdt); |
| 163 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 164 | if (fdt_chk_extra() && (headsize + tailsize) > fdt_totalsize(fdt)) |
| 165 | return -FDT_ERR_INTERNAL; |
| 166 | |
Andre Przywara | 832bfad | 2020-10-16 15:42:50 +0100 | [diff] [blame] | 167 | if ((headsize + tailsize) > (unsigned)bufsize) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 168 | return -FDT_ERR_NOSPACE; |
| 169 | |
| 170 | oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize; |
| 171 | newtail = (char *)buf + bufsize - tailsize; |
| 172 | |
| 173 | /* Two cases to avoid clobbering data if the old and new |
| 174 | * buffers partially overlap */ |
| 175 | if (buf <= fdt) { |
| 176 | memmove(buf, fdt, headsize); |
| 177 | memmove(newtail, oldtail, tailsize); |
| 178 | } else { |
| 179 | memmove(newtail, oldtail, tailsize); |
| 180 | memmove(buf, fdt, headsize); |
| 181 | } |
| 182 | |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 183 | fdt_set_totalsize(buf, bufsize); |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 184 | if (fdt_off_dt_strings(buf)) |
| 185 | fdt_set_off_dt_strings(buf, bufsize); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size) |
| 191 | { |
| 192 | struct fdt_reserve_entry *re; |
| 193 | int offset; |
| 194 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 195 | FDT_SW_PROBE_MEMRSV(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 196 | |
| 197 | offset = fdt_off_dt_struct(fdt); |
| 198 | if ((offset + sizeof(*re)) > fdt_totalsize(fdt)) |
| 199 | return -FDT_ERR_NOSPACE; |
| 200 | |
| 201 | re = (struct fdt_reserve_entry *)((char *)fdt + offset); |
| 202 | re->address = cpu_to_fdt64(addr); |
| 203 | re->size = cpu_to_fdt64(size); |
| 204 | |
| 205 | fdt_set_off_dt_struct(fdt, offset + sizeof(*re)); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | int fdt_finish_reservemap(void *fdt) |
| 211 | { |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 212 | int err = fdt_add_reservemap_entry(fdt, 0, 0); |
| 213 | |
| 214 | if (err) |
| 215 | return err; |
| 216 | |
| 217 | fdt_set_off_dt_strings(fdt, fdt_totalsize(fdt)); |
| 218 | return 0; |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | int fdt_begin_node(void *fdt, const char *name) |
| 222 | { |
| 223 | struct fdt_node_header *nh; |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 224 | int namelen; |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 225 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 226 | FDT_SW_PROBE_STRUCT(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 227 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 228 | namelen = strlen(name) + 1; |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 229 | nh = fdt_grab_space_(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen)); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 230 | if (! nh) |
| 231 | return -FDT_ERR_NOSPACE; |
| 232 | |
| 233 | nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE); |
| 234 | memcpy(nh->name, name, namelen); |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | int fdt_end_node(void *fdt) |
| 239 | { |
| 240 | fdt32_t *en; |
| 241 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 242 | FDT_SW_PROBE_STRUCT(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 243 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 244 | en = fdt_grab_space_(fdt, FDT_TAGSIZE); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 245 | if (! en) |
| 246 | return -FDT_ERR_NOSPACE; |
| 247 | |
| 248 | *en = cpu_to_fdt32(FDT_END_NODE); |
| 249 | return 0; |
| 250 | } |
| 251 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 252 | static int fdt_add_string_(void *fdt, const char *s) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 253 | { |
| 254 | char *strtab = (char *)fdt + fdt_totalsize(fdt); |
Andre Przywara | 832bfad | 2020-10-16 15:42:50 +0100 | [diff] [blame] | 255 | unsigned int strtabsize = fdt_size_dt_strings(fdt); |
| 256 | unsigned int len = strlen(s) + 1; |
| 257 | unsigned int struct_top, offset; |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 258 | |
Andre Przywara | 832bfad | 2020-10-16 15:42:50 +0100 | [diff] [blame] | 259 | offset = strtabsize + len; |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 260 | struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); |
Andre Przywara | 832bfad | 2020-10-16 15:42:50 +0100 | [diff] [blame] | 261 | if (fdt_totalsize(fdt) - offset < struct_top) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 262 | return 0; /* no more room :( */ |
| 263 | |
Andre Przywara | 832bfad | 2020-10-16 15:42:50 +0100 | [diff] [blame] | 264 | memcpy(strtab - offset, s, len); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 265 | fdt_set_size_dt_strings(fdt, strtabsize + len); |
Andre Przywara | 832bfad | 2020-10-16 15:42:50 +0100 | [diff] [blame] | 266 | return -offset; |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 267 | } |
| 268 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 269 | /* Must only be used to roll back in case of error */ |
| 270 | static void fdt_del_last_string_(void *fdt, const char *s) |
| 271 | { |
| 272 | int strtabsize = fdt_size_dt_strings(fdt); |
| 273 | int len = strlen(s) + 1; |
| 274 | |
| 275 | fdt_set_size_dt_strings(fdt, strtabsize - len); |
| 276 | } |
| 277 | |
| 278 | static int fdt_find_add_string_(void *fdt, const char *s, int *allocated) |
| 279 | { |
| 280 | char *strtab = (char *)fdt + fdt_totalsize(fdt); |
| 281 | int strtabsize = fdt_size_dt_strings(fdt); |
| 282 | const char *p; |
| 283 | |
| 284 | *allocated = 0; |
| 285 | |
| 286 | p = fdt_find_string_(strtab - strtabsize, strtabsize, s); |
| 287 | if (p) |
| 288 | return p - strtab; |
| 289 | |
| 290 | *allocated = 1; |
| 291 | |
| 292 | return fdt_add_string_(fdt, s); |
| 293 | } |
| 294 | |
Tom Rini | 2d4c225 | 2017-09-23 17:31:59 -0400 | [diff] [blame] | 295 | int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 296 | { |
| 297 | struct fdt_property *prop; |
| 298 | int nameoff; |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 299 | int allocated; |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 300 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 301 | FDT_SW_PROBE_STRUCT(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 302 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 303 | /* String de-duplication can be slow, _NO_NAME_DEDUP skips it */ |
| 304 | if (sw_flags(fdt) & FDT_CREATE_FLAG_NO_NAME_DEDUP) { |
| 305 | allocated = 1; |
| 306 | nameoff = fdt_add_string_(fdt, name); |
| 307 | } else { |
| 308 | nameoff = fdt_find_add_string_(fdt, name, &allocated); |
| 309 | } |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 310 | if (nameoff == 0) |
| 311 | return -FDT_ERR_NOSPACE; |
| 312 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 313 | prop = fdt_grab_space_(fdt, sizeof(*prop) + FDT_TAGALIGN(len)); |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 314 | if (! prop) { |
| 315 | if (allocated) |
| 316 | fdt_del_last_string_(fdt, name); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 317 | return -FDT_ERR_NOSPACE; |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 318 | } |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 319 | |
| 320 | prop->tag = cpu_to_fdt32(FDT_PROP); |
| 321 | prop->nameoff = cpu_to_fdt32(nameoff); |
| 322 | prop->len = cpu_to_fdt32(len); |
Tom Rini | 2d4c225 | 2017-09-23 17:31:59 -0400 | [diff] [blame] | 323 | *valp = prop->data; |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | int fdt_property(void *fdt, const char *name, const void *val, int len) |
| 328 | { |
| 329 | void *ptr; |
| 330 | int ret; |
| 331 | |
| 332 | ret = fdt_property_placeholder(fdt, name, len, &ptr); |
| 333 | if (ret) |
| 334 | return ret; |
| 335 | memcpy(ptr, val, len); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | int fdt_finish(void *fdt) |
| 340 | { |
| 341 | char *p = (char *)fdt; |
| 342 | fdt32_t *end; |
| 343 | int oldstroffset, newstroffset; |
| 344 | uint32_t tag; |
| 345 | int offset, nextoffset; |
| 346 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 347 | FDT_SW_PROBE_STRUCT(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 348 | |
| 349 | /* Add terminator */ |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 350 | end = fdt_grab_space_(fdt, sizeof(*end)); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 351 | if (! end) |
| 352 | return -FDT_ERR_NOSPACE; |
| 353 | *end = cpu_to_fdt32(FDT_END); |
| 354 | |
| 355 | /* Relocate the string table */ |
| 356 | oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt); |
| 357 | newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); |
| 358 | memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt)); |
| 359 | fdt_set_off_dt_strings(fdt, newstroffset); |
| 360 | |
| 361 | /* Walk the structure, correcting string offsets */ |
| 362 | offset = 0; |
| 363 | while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) { |
| 364 | if (tag == FDT_PROP) { |
| 365 | struct fdt_property *prop = |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 366 | fdt_offset_ptr_w_(fdt, offset); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 367 | int nameoff; |
| 368 | |
| 369 | nameoff = fdt32_to_cpu(prop->nameoff); |
| 370 | nameoff += fdt_size_dt_strings(fdt); |
| 371 | prop->nameoff = cpu_to_fdt32(nameoff); |
| 372 | } |
| 373 | offset = nextoffset; |
| 374 | } |
| 375 | if (nextoffset < 0) |
| 376 | return nextoffset; |
| 377 | |
| 378 | /* Finally, adjust the header */ |
| 379 | fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt)); |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 380 | |
| 381 | /* And fix up fields that were keeping intermediate state. */ |
| 382 | fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 383 | fdt_set_magic(fdt, FDT_MAGIC); |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 384 | |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 385 | return 0; |
| 386 | } |