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