Gerald Van Baren | 9e61ed8 | 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 | 3eaedea | 2013-07-27 01:12:38 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -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 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 13 | static int _fdt_sw_check_header(void *fdt) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 14 | { |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 15 | if (fdt_magic(fdt) != FDT_SW_MAGIC) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 16 | return -FDT_ERR_BADMAGIC; |
David Gibson | 148a49b | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 17 | /* FIXME: should check more details about the header state */ |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 18 | return 0; |
| 19 | } |
| 20 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 21 | #define FDT_SW_CHECK_HEADER(fdt) \ |
David Gibson | 148a49b | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 22 | { \ |
| 23 | int err; \ |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 24 | if ((err = _fdt_sw_check_header(fdt)) != 0) \ |
David Gibson | 148a49b | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 25 | return err; \ |
| 26 | } |
| 27 | |
Emil Medve | 6b098ae | 2009-02-23 10:43:36 -0600 | [diff] [blame] | 28 | static void *_fdt_grab_space(void *fdt, size_t len) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 29 | { |
| 30 | int offset = fdt_size_dt_struct(fdt); |
| 31 | int spaceleft; |
| 32 | |
| 33 | spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt) |
| 34 | - fdt_size_dt_strings(fdt); |
| 35 | |
| 36 | if ((offset + len < offset) || (offset + len > spaceleft)) |
| 37 | return NULL; |
| 38 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 39 | fdt_set_size_dt_struct(fdt, offset + len); |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 40 | return _fdt_offset_ptr_w(fdt, offset); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | int fdt_create(void *buf, int bufsize) |
| 44 | { |
| 45 | void *fdt = buf; |
| 46 | |
| 47 | if (bufsize < sizeof(struct fdt_header)) |
| 48 | return -FDT_ERR_NOSPACE; |
| 49 | |
| 50 | memset(buf, 0, bufsize); |
| 51 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 52 | fdt_set_magic(fdt, FDT_SW_MAGIC); |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 53 | fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION); |
| 54 | fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION); |
| 55 | fdt_set_totalsize(fdt, bufsize); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 56 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 57 | fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header), |
| 58 | sizeof(struct fdt_reserve_entry))); |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 59 | fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt)); |
| 60 | fdt_set_off_dt_strings(fdt, bufsize); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Simon Glass | 536774d | 2014-07-30 03:59:03 -0600 | [diff] [blame] | 65 | int fdt_resize(void *fdt, void *buf, int bufsize) |
| 66 | { |
| 67 | size_t headsize, tailsize; |
| 68 | char *oldtail, *newtail; |
| 69 | |
| 70 | FDT_SW_CHECK_HEADER(fdt); |
| 71 | |
| 72 | headsize = fdt_off_dt_struct(fdt); |
| 73 | tailsize = fdt_size_dt_strings(fdt); |
| 74 | |
| 75 | if ((headsize + tailsize) > bufsize) |
| 76 | return -FDT_ERR_NOSPACE; |
| 77 | |
| 78 | oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize; |
| 79 | newtail = (char *)buf + bufsize - tailsize; |
| 80 | |
| 81 | /* Two cases to avoid clobbering data if the old and new |
| 82 | * buffers partially overlap */ |
| 83 | if (buf <= fdt) { |
| 84 | memmove(buf, fdt, headsize); |
| 85 | memmove(newtail, oldtail, tailsize); |
| 86 | } else { |
| 87 | memmove(newtail, oldtail, tailsize); |
| 88 | memmove(buf, fdt, headsize); |
| 89 | } |
| 90 | |
| 91 | fdt_set_off_dt_strings(buf, bufsize); |
| 92 | fdt_set_totalsize(buf, bufsize); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 97 | int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size) |
| 98 | { |
| 99 | struct fdt_reserve_entry *re; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 100 | int offset; |
| 101 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 102 | FDT_SW_CHECK_HEADER(fdt); |
David Gibson | 148a49b | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 103 | |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 104 | if (fdt_size_dt_struct(fdt)) |
| 105 | return -FDT_ERR_BADSTATE; |
| 106 | |
| 107 | offset = fdt_off_dt_struct(fdt); |
| 108 | if ((offset + sizeof(*re)) > fdt_totalsize(fdt)) |
| 109 | return -FDT_ERR_NOSPACE; |
| 110 | |
David Gibson | b4141b8 | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 111 | re = (struct fdt_reserve_entry *)((char *)fdt + offset); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 112 | re->address = cpu_to_fdt64(addr); |
| 113 | re->size = cpu_to_fdt64(size); |
| 114 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 115 | fdt_set_off_dt_struct(fdt, offset + sizeof(*re)); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | int fdt_finish_reservemap(void *fdt) |
| 121 | { |
| 122 | return fdt_add_reservemap_entry(fdt, 0, 0); |
| 123 | } |
| 124 | |
| 125 | int fdt_begin_node(void *fdt, const char *name) |
| 126 | { |
| 127 | struct fdt_node_header *nh; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 128 | int namelen = strlen(name) + 1; |
| 129 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 130 | FDT_SW_CHECK_HEADER(fdt); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 131 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 132 | nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen)); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 133 | if (! nh) |
| 134 | return -FDT_ERR_NOSPACE; |
| 135 | |
| 136 | nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE); |
| 137 | memcpy(nh->name, name, namelen); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | int fdt_end_node(void *fdt) |
| 142 | { |
Kim Phillips | 38c3815 | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 143 | fdt32_t *en; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 144 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 145 | FDT_SW_CHECK_HEADER(fdt); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 146 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 147 | en = _fdt_grab_space(fdt, FDT_TAGSIZE); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 148 | if (! en) |
| 149 | return -FDT_ERR_NOSPACE; |
| 150 | |
| 151 | *en = cpu_to_fdt32(FDT_END_NODE); |
| 152 | return 0; |
| 153 | } |
| 154 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 155 | static int _fdt_find_add_string(void *fdt, const char *s) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 156 | { |
| 157 | char *strtab = (char *)fdt + fdt_totalsize(fdt); |
| 158 | const char *p; |
| 159 | int strtabsize = fdt_size_dt_strings(fdt); |
| 160 | int len = strlen(s) + 1; |
| 161 | int struct_top, offset; |
| 162 | |
| 163 | p = _fdt_find_string(strtab - strtabsize, strtabsize, s); |
| 164 | if (p) |
| 165 | return p - strtab; |
| 166 | |
| 167 | /* Add it */ |
| 168 | offset = -strtabsize - len; |
| 169 | struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); |
| 170 | if (fdt_totalsize(fdt) + offset < struct_top) |
| 171 | return 0; /* no more room :( */ |
| 172 | |
| 173 | memcpy(strtab + offset, s, len); |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 174 | fdt_set_size_dt_strings(fdt, strtabsize + len); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 175 | return offset; |
| 176 | } |
| 177 | |
Simon Glass | 4973bf5 | 2016-02-22 22:55:39 -0700 | [diff] [blame] | 178 | int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 179 | { |
| 180 | struct fdt_property *prop; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 181 | int nameoff; |
| 182 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 183 | FDT_SW_CHECK_HEADER(fdt); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 184 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 185 | nameoff = _fdt_find_add_string(fdt, name); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 186 | if (nameoff == 0) |
| 187 | return -FDT_ERR_NOSPACE; |
| 188 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 189 | prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len)); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 190 | if (! prop) |
| 191 | return -FDT_ERR_NOSPACE; |
| 192 | |
| 193 | prop->tag = cpu_to_fdt32(FDT_PROP); |
| 194 | prop->nameoff = cpu_to_fdt32(nameoff); |
| 195 | prop->len = cpu_to_fdt32(len); |
Simon Glass | 4973bf5 | 2016-02-22 22:55:39 -0700 | [diff] [blame] | 196 | *valp = prop->data; |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | int fdt_property(void *fdt, const char *name, const void *val, int len) |
| 201 | { |
| 202 | void *ptr; |
| 203 | int ret; |
| 204 | |
| 205 | ret = fdt_property_placeholder(fdt, name, len, &ptr); |
| 206 | if (ret) |
| 207 | return ret; |
| 208 | memcpy(ptr, val, len); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | int fdt_finish(void *fdt) |
| 213 | { |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 214 | char *p = (char *)fdt; |
Kim Phillips | 38c3815 | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 215 | fdt32_t *end; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 216 | int oldstroffset, newstroffset; |
| 217 | uint32_t tag; |
| 218 | int offset, nextoffset; |
| 219 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 220 | FDT_SW_CHECK_HEADER(fdt); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 221 | |
| 222 | /* Add terminator */ |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 223 | end = _fdt_grab_space(fdt, sizeof(*end)); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 224 | if (! end) |
| 225 | return -FDT_ERR_NOSPACE; |
| 226 | *end = cpu_to_fdt32(FDT_END); |
| 227 | |
| 228 | /* Relocate the string table */ |
| 229 | oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt); |
| 230 | newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); |
| 231 | memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt)); |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 232 | fdt_set_off_dt_strings(fdt, newstroffset); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 233 | |
| 234 | /* Walk the structure, correcting string offsets */ |
| 235 | offset = 0; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 236 | while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) { |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 237 | if (tag == FDT_PROP) { |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 238 | struct fdt_property *prop = |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 239 | _fdt_offset_ptr_w(fdt, offset); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 240 | int nameoff; |
| 241 | |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 242 | nameoff = fdt32_to_cpu(prop->nameoff); |
| 243 | nameoff += fdt_size_dt_strings(fdt); |
| 244 | prop->nameoff = cpu_to_fdt32(nameoff); |
| 245 | } |
| 246 | offset = nextoffset; |
| 247 | } |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 248 | if (nextoffset < 0) |
| 249 | return nextoffset; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 250 | |
| 251 | /* Finally, adjust the header */ |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 252 | fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt)); |
| 253 | fdt_set_magic(fdt, FDT_MAGIC); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 254 | return 0; |
| 255 | } |