1 /* $NetBSD: fdt_sw.c,v 1.1.1.2 2017/06/08 15:53:12 skrll Exp $ */ 2 3 /* 4 * libfdt - Flat Device Tree manipulation 5 * Copyright (C) 2006 David Gibson, IBM Corporation. 6 * 7 * libfdt is dual licensed: you can use it either under the terms of 8 * the GPL, or the BSD license, at your option. 9 * 10 * a) This library is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License as 12 * published by the Free Software Foundation; either version 2 of the 13 * License, or (at your option) any later version. 14 * 15 * This library is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public 21 * License along with this library; if not, write to the Free 22 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 23 * MA 02110-1301 USA 24 * 25 * Alternatively, 26 * 27 * b) Redistribution and use in source and binary forms, with or 28 * without modification, are permitted provided that the following 29 * conditions are met: 30 * 31 * 1. Redistributions of source code must retain the above 32 * copyright notice, this list of conditions and the following 33 * disclaimer. 34 * 2. Redistributions in binary form must reproduce the above 35 * copyright notice, this list of conditions and the following 36 * disclaimer in the documentation and/or other materials 37 * provided with the distribution. 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 40 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 41 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 44 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 49 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 50 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 51 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 52 */ 53 #include "libfdt_env.h" 54 55 #include <fdt.h> 56 #include <libfdt.h> 57 58 #include "libfdt_internal.h" 59 60 static int _fdt_sw_check_header(void *fdt) 61 { 62 if (fdt_magic(fdt) != FDT_SW_MAGIC) 63 return -FDT_ERR_BADMAGIC; 64 /* FIXME: should check more details about the header state */ 65 return 0; 66 } 67 68 #define FDT_SW_CHECK_HEADER(fdt) \ 69 { \ 70 int err; \ 71 if ((err = _fdt_sw_check_header(fdt)) != 0) \ 72 return err; \ 73 } 74 75 static void *_fdt_grab_space(void *fdt, size_t len) 76 { 77 int offset = fdt_size_dt_struct(fdt); 78 int spaceleft; 79 80 spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt) 81 - fdt_size_dt_strings(fdt); 82 83 if ((offset + len < offset) || (offset + len > spaceleft)) 84 return NULL; 85 86 fdt_set_size_dt_struct(fdt, offset + len); 87 return _fdt_offset_ptr_w(fdt, offset); 88 } 89 90 int fdt_create(void *buf, int bufsize) 91 { 92 void *fdt = buf; 93 94 if (bufsize < sizeof(struct fdt_header)) 95 return -FDT_ERR_NOSPACE; 96 97 memset(buf, 0, bufsize); 98 99 fdt_set_magic(fdt, FDT_SW_MAGIC); 100 fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION); 101 fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION); 102 fdt_set_totalsize(fdt, bufsize); 103 104 fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header), 105 sizeof(struct fdt_reserve_entry))); 106 fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt)); 107 fdt_set_off_dt_strings(fdt, bufsize); 108 109 return 0; 110 } 111 112 int fdt_resize(void *fdt, void *buf, int bufsize) 113 { 114 size_t headsize, tailsize; 115 char *oldtail, *newtail; 116 117 FDT_SW_CHECK_HEADER(fdt); 118 119 headsize = fdt_off_dt_struct(fdt); 120 tailsize = fdt_size_dt_strings(fdt); 121 122 if ((headsize + tailsize) > bufsize) 123 return -FDT_ERR_NOSPACE; 124 125 oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize; 126 newtail = (char *)buf + bufsize - tailsize; 127 128 /* Two cases to avoid clobbering data if the old and new 129 * buffers partially overlap */ 130 if (buf <= fdt) { 131 memmove(buf, fdt, headsize); 132 memmove(newtail, oldtail, tailsize); 133 } else { 134 memmove(newtail, oldtail, tailsize); 135 memmove(buf, fdt, headsize); 136 } 137 138 fdt_set_off_dt_strings(buf, bufsize); 139 fdt_set_totalsize(buf, bufsize); 140 141 return 0; 142 } 143 144 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size) 145 { 146 struct fdt_reserve_entry *re; 147 int offset; 148 149 FDT_SW_CHECK_HEADER(fdt); 150 151 if (fdt_size_dt_struct(fdt)) 152 return -FDT_ERR_BADSTATE; 153 154 offset = fdt_off_dt_struct(fdt); 155 if ((offset + sizeof(*re)) > fdt_totalsize(fdt)) 156 return -FDT_ERR_NOSPACE; 157 158 re = (struct fdt_reserve_entry *)((char *)fdt + offset); 159 re->address = cpu_to_fdt64(addr); 160 re->size = cpu_to_fdt64(size); 161 162 fdt_set_off_dt_struct(fdt, offset + sizeof(*re)); 163 164 return 0; 165 } 166 167 int fdt_finish_reservemap(void *fdt) 168 { 169 return fdt_add_reservemap_entry(fdt, 0, 0); 170 } 171 172 int fdt_begin_node(void *fdt, const char *name) 173 { 174 struct fdt_node_header *nh; 175 int namelen = strlen(name) + 1; 176 177 FDT_SW_CHECK_HEADER(fdt); 178 179 nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen)); 180 if (! nh) 181 return -FDT_ERR_NOSPACE; 182 183 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE); 184 memcpy(nh->name, name, namelen); 185 return 0; 186 } 187 188 int fdt_end_node(void *fdt) 189 { 190 fdt32_t *en; 191 192 FDT_SW_CHECK_HEADER(fdt); 193 194 en = _fdt_grab_space(fdt, FDT_TAGSIZE); 195 if (! en) 196 return -FDT_ERR_NOSPACE; 197 198 *en = cpu_to_fdt32(FDT_END_NODE); 199 return 0; 200 } 201 202 static int _fdt_find_add_string(void *fdt, const char *s) 203 { 204 char *strtab = (char *)fdt + fdt_totalsize(fdt); 205 const char *p; 206 int strtabsize = fdt_size_dt_strings(fdt); 207 int len = strlen(s) + 1; 208 int struct_top, offset; 209 210 p = _fdt_find_string(strtab - strtabsize, strtabsize, s); 211 if (p) 212 return p - strtab; 213 214 /* Add it */ 215 offset = -strtabsize - len; 216 struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); 217 if (fdt_totalsize(fdt) + offset < struct_top) 218 return 0; /* no more room :( */ 219 220 memcpy(strtab + offset, s, len); 221 fdt_set_size_dt_strings(fdt, strtabsize + len); 222 return offset; 223 } 224 225 int fdt_property(void *fdt, const char *name, const void *val, int len) 226 { 227 struct fdt_property *prop; 228 int nameoff; 229 230 FDT_SW_CHECK_HEADER(fdt); 231 232 nameoff = _fdt_find_add_string(fdt, name); 233 if (nameoff == 0) 234 return -FDT_ERR_NOSPACE; 235 236 prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len)); 237 if (! prop) 238 return -FDT_ERR_NOSPACE; 239 240 prop->tag = cpu_to_fdt32(FDT_PROP); 241 prop->nameoff = cpu_to_fdt32(nameoff); 242 prop->len = cpu_to_fdt32(len); 243 memcpy(prop->data, val, len); 244 return 0; 245 } 246 247 int fdt_finish(void *fdt) 248 { 249 char *p = (char *)fdt; 250 fdt32_t *end; 251 int oldstroffset, newstroffset; 252 uint32_t tag; 253 int offset, nextoffset; 254 255 FDT_SW_CHECK_HEADER(fdt); 256 257 /* Add terminator */ 258 end = _fdt_grab_space(fdt, sizeof(*end)); 259 if (! end) 260 return -FDT_ERR_NOSPACE; 261 *end = cpu_to_fdt32(FDT_END); 262 263 /* Relocate the string table */ 264 oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt); 265 newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); 266 memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt)); 267 fdt_set_off_dt_strings(fdt, newstroffset); 268 269 /* Walk the structure, correcting string offsets */ 270 offset = 0; 271 while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) { 272 if (tag == FDT_PROP) { 273 struct fdt_property *prop = 274 _fdt_offset_ptr_w(fdt, offset); 275 int nameoff; 276 277 nameoff = fdt32_to_cpu(prop->nameoff); 278 nameoff += fdt_size_dt_strings(fdt); 279 prop->nameoff = cpu_to_fdt32(nameoff); 280 } 281 offset = nextoffset; 282 } 283 if (nextoffset < 0) 284 return nextoffset; 285 286 /* Finally, adjust the header */ 287 fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt)); 288 fdt_set_magic(fdt, FDT_MAGIC); 289 return 0; 290 } 291