2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libfdt_env.h"
27 #include "libfdt_internal.h"
29 static int check_header_sw(void *fdt)
31 if (fdt_magic(fdt) != SW_MAGIC)
32 return -FDT_ERR_BADMAGIC;
36 static void *grab_space(void *fdt, int len)
38 int offset = fdt_size_dt_struct(fdt);
41 spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
42 - fdt_size_dt_strings(fdt);
44 if ((offset + len < offset) || (offset + len > spaceleft))
47 fdt_set_header(fdt, size_dt_struct, offset + len);
48 return fdt_offset_ptr(fdt, offset, len);
51 int fdt_create(void *buf, int bufsize)
55 if (bufsize < sizeof(struct fdt_header))
56 return -FDT_ERR_NOSPACE;
58 memset(buf, 0, bufsize);
60 fdt_set_header(fdt, magic, SW_MAGIC);
61 fdt_set_header(fdt, version, FDT_LAST_SUPPORTED_VERSION);
62 fdt_set_header(fdt, last_comp_version, FDT_FIRST_SUPPORTED_VERSION);
63 fdt_set_header(fdt, totalsize, bufsize);
65 fdt_set_header(fdt, off_mem_rsvmap, ALIGN(sizeof(struct fdt_header),
66 sizeof(struct fdt_reserve_entry)));
67 fdt_set_header(fdt, off_dt_struct, fdt_off_mem_rsvmap(fdt));
68 fdt_set_header(fdt, off_dt_strings, bufsize);
73 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
75 struct fdt_reserve_entry *re;
76 int err = check_header_sw(fdt);
81 if (fdt_size_dt_struct(fdt))
82 return -FDT_ERR_BADSTATE;
84 offset = fdt_off_dt_struct(fdt);
85 if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
86 return -FDT_ERR_NOSPACE;
88 re = (struct fdt_reserve_entry *)((void *)fdt + offset);
89 re->address = cpu_to_fdt64(addr);
90 re->size = cpu_to_fdt64(size);
92 fdt_set_header(fdt, off_dt_struct, offset + sizeof(*re));
97 int fdt_finish_reservemap(void *fdt)
99 return fdt_add_reservemap_entry(fdt, 0, 0);
102 int fdt_begin_node(void *fdt, const char *name)
104 struct fdt_node_header *nh;
105 int err = check_header_sw(fdt);
106 int namelen = strlen(name) + 1;
111 nh = grab_space(fdt, sizeof(*nh) + ALIGN(namelen, FDT_TAGSIZE));
113 return -FDT_ERR_NOSPACE;
115 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
116 memcpy(nh->name, name, namelen);
120 int fdt_end_node(void *fdt)
123 int err = check_header_sw(fdt);
128 en = grab_space(fdt, FDT_TAGSIZE);
130 return -FDT_ERR_NOSPACE;
132 *en = cpu_to_fdt32(FDT_END_NODE);
136 static int find_add_string(void *fdt, const char *s)
138 char *strtab = (char *)fdt + fdt_totalsize(fdt);
140 int strtabsize = fdt_size_dt_strings(fdt);
141 int len = strlen(s) + 1;
142 int struct_top, offset;
144 p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
149 offset = -strtabsize - len;
150 struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
151 if (fdt_totalsize(fdt) + offset < struct_top)
152 return 0; /* no more room :( */
154 memcpy(strtab + offset, s, len);
155 fdt_set_header(fdt, size_dt_strings, strtabsize + len);
159 int fdt_property(void *fdt, const char *name, const void *val, int len)
161 struct fdt_property *prop;
162 int err = check_header_sw(fdt);
168 nameoff = find_add_string(fdt, name);
170 return -FDT_ERR_NOSPACE;
172 prop = grab_space(fdt, sizeof(*prop) + ALIGN(len, FDT_TAGSIZE));
174 return -FDT_ERR_NOSPACE;
176 prop->tag = cpu_to_fdt32(FDT_PROP);
177 prop->nameoff = cpu_to_fdt32(nameoff);
178 prop->len = cpu_to_fdt32(len);
179 memcpy(prop->data, val, len);
183 int fdt_finish(void *fdt)
185 int err = check_header_sw(fdt);
186 char *p = (char *)fdt;
188 int oldstroffset, newstroffset;
190 int offset, nextoffset;
196 end = grab_space(fdt, sizeof(*end));
198 return -FDT_ERR_NOSPACE;
199 *end = cpu_to_fdt32(FDT_END);
201 /* Relocate the string table */
202 oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
203 newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
204 memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
205 fdt_set_header(fdt, off_dt_strings, newstroffset);
207 /* Walk the structure, correcting string offsets */
209 while ((tag = fdt_next_tag(fdt, offset, &nextoffset, NULL)) != FDT_END) {
210 if (tag == FDT_PROP) {
211 struct fdt_property *prop = fdt_offset_ptr(fdt, offset,
216 return -FDT_ERR_BADSTRUCTURE;
218 nameoff = fdt32_to_cpu(prop->nameoff);
219 nameoff += fdt_size_dt_strings(fdt);
220 prop->nameoff = cpu_to_fdt32(nameoff);
225 /* Finally, adjust the header */
226 fdt_set_header(fdt, totalsize, newstroffset + fdt_size_dt_strings(fdt));
227 fdt_set_header(fdt, magic, FDT_MAGIC);
231 #endif /* CONFIG_OF_LIBFDT */