blob: dda2de34b2e0a968463cf808b669b37e226620df [file] [log] [blame]
Gerald Van Baren35748172007-03-31 12:00:56 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
Kumar Gala8d04f022007-10-24 11:04:22 -05005 * libfdt is dual licensed: you can use it either under the terms of
6 * the GPL, or the BSD license, at your option.
Gerald Van Baren35748172007-03-31 12:00:56 -04007 *
Kumar Gala8d04f022007-10-24 11:04:22 -05008 * a) This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
Gerald Van Baren35748172007-03-31 12:00:56 -040012 *
Kumar Gala8d04f022007-10-24 11:04:22 -050013 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 * MA 02110-1301 USA
22 *
23 * Alternatively,
24 *
25 * b) Redistribution and use in source and binary forms, with or
26 * without modification, are permitted provided that the following
27 * conditions are met:
28 *
29 * 1. Redistributions of source code must retain the above
30 * copyright notice, this list of conditions and the following
31 * disclaimer.
32 * 2. Redistributions in binary form must reproduce the above
33 * copyright notice, this list of conditions and the following
34 * disclaimer in the documentation and/or other materials
35 * provided with the distribution.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Gerald Van Baren35748172007-03-31 12:00:56 -040050 */
51#include "libfdt_env.h"
52
53#include <fdt.h>
54#include <libfdt.h>
55
56#include "libfdt_internal.h"
57
58static int check_header_sw(void *fdt)
59{
60 if (fdt_magic(fdt) != SW_MAGIC)
61 return -FDT_ERR_BADMAGIC;
62 return 0;
63}
64
65static void *grab_space(void *fdt, int len)
66{
67 int offset = fdt_size_dt_struct(fdt);
68 int spaceleft;
69
70 spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
71 - fdt_size_dt_strings(fdt);
72
73 if ((offset + len < offset) || (offset + len > spaceleft))
74 return NULL;
75
Kumar Gala8d04f022007-10-24 11:04:22 -050076 fdt_set_size_dt_struct(fdt, offset + len);
77 return fdt_offset_ptr_w(fdt, offset, len);
Gerald Van Baren35748172007-03-31 12:00:56 -040078}
79
80int fdt_create(void *buf, int bufsize)
81{
82 void *fdt = buf;
83
84 if (bufsize < sizeof(struct fdt_header))
85 return -FDT_ERR_NOSPACE;
86
87 memset(buf, 0, bufsize);
88
Kumar Gala8d04f022007-10-24 11:04:22 -050089 fdt_set_magic(fdt, SW_MAGIC);
90 fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
91 fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
92 fdt_set_totalsize(fdt, bufsize);
Gerald Van Baren35748172007-03-31 12:00:56 -040093
Kumar Gala8d04f022007-10-24 11:04:22 -050094 fdt_set_off_mem_rsvmap(fdt, ALIGN(sizeof(struct fdt_header),
95 sizeof(struct fdt_reserve_entry)));
96 fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
97 fdt_set_off_dt_strings(fdt, bufsize);
Gerald Van Baren35748172007-03-31 12:00:56 -040098
99 return 0;
100}
101
102int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
103{
104 struct fdt_reserve_entry *re;
105 int err = check_header_sw(fdt);
106 int offset;
107
108 if (err)
109 return err;
110 if (fdt_size_dt_struct(fdt))
111 return -FDT_ERR_BADSTATE;
112
113 offset = fdt_off_dt_struct(fdt);
114 if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
115 return -FDT_ERR_NOSPACE;
116
Kumar Gala8d04f022007-10-24 11:04:22 -0500117 re = (struct fdt_reserve_entry *)(fdt + offset);
Gerald Van Baren35748172007-03-31 12:00:56 -0400118 re->address = cpu_to_fdt64(addr);
119 re->size = cpu_to_fdt64(size);
120
Kumar Gala8d04f022007-10-24 11:04:22 -0500121 fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
Gerald Van Baren35748172007-03-31 12:00:56 -0400122
123 return 0;
124}
125
126int fdt_finish_reservemap(void *fdt)
127{
128 return fdt_add_reservemap_entry(fdt, 0, 0);
129}
130
131int fdt_begin_node(void *fdt, const char *name)
132{
133 struct fdt_node_header *nh;
134 int err = check_header_sw(fdt);
135 int namelen = strlen(name) + 1;
136
137 if (err)
138 return err;
139
140 nh = grab_space(fdt, sizeof(*nh) + ALIGN(namelen, FDT_TAGSIZE));
141 if (! nh)
142 return -FDT_ERR_NOSPACE;
143
144 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
145 memcpy(nh->name, name, namelen);
146 return 0;
147}
148
149int fdt_end_node(void *fdt)
150{
151 uint32_t *en;
152 int err = check_header_sw(fdt);
153
154 if (err)
155 return err;
156
157 en = grab_space(fdt, FDT_TAGSIZE);
158 if (! en)
159 return -FDT_ERR_NOSPACE;
160
161 *en = cpu_to_fdt32(FDT_END_NODE);
162 return 0;
163}
164
165static int find_add_string(void *fdt, const char *s)
166{
167 char *strtab = (char *)fdt + fdt_totalsize(fdt);
168 const char *p;
169 int strtabsize = fdt_size_dt_strings(fdt);
170 int len = strlen(s) + 1;
171 int struct_top, offset;
172
173 p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
174 if (p)
175 return p - strtab;
176
177 /* Add it */
178 offset = -strtabsize - len;
179 struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
180 if (fdt_totalsize(fdt) + offset < struct_top)
181 return 0; /* no more room :( */
182
183 memcpy(strtab + offset, s, len);
Kumar Gala8d04f022007-10-24 11:04:22 -0500184 fdt_set_size_dt_strings(fdt, strtabsize + len);
Gerald Van Baren35748172007-03-31 12:00:56 -0400185 return offset;
186}
187
188int fdt_property(void *fdt, const char *name, const void *val, int len)
189{
190 struct fdt_property *prop;
191 int err = check_header_sw(fdt);
192 int nameoff;
193
194 if (err)
195 return err;
196
197 nameoff = find_add_string(fdt, name);
198 if (nameoff == 0)
199 return -FDT_ERR_NOSPACE;
200
201 prop = grab_space(fdt, sizeof(*prop) + ALIGN(len, FDT_TAGSIZE));
202 if (! prop)
203 return -FDT_ERR_NOSPACE;
204
205 prop->tag = cpu_to_fdt32(FDT_PROP);
206 prop->nameoff = cpu_to_fdt32(nameoff);
207 prop->len = cpu_to_fdt32(len);
208 memcpy(prop->data, val, len);
209 return 0;
210}
211
212int fdt_finish(void *fdt)
213{
214 int err = check_header_sw(fdt);
215 char *p = (char *)fdt;
216 uint32_t *end;
217 int oldstroffset, newstroffset;
218 uint32_t tag;
219 int offset, nextoffset;
220
221 if (err)
222 return err;
223
224 /* Add terminator */
225 end = grab_space(fdt, sizeof(*end));
226 if (! end)
227 return -FDT_ERR_NOSPACE;
228 *end = cpu_to_fdt32(FDT_END);
229
230 /* Relocate the string table */
231 oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
232 newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
233 memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
Kumar Gala8d04f022007-10-24 11:04:22 -0500234 fdt_set_off_dt_strings(fdt, newstroffset);
Gerald Van Baren35748172007-03-31 12:00:56 -0400235
236 /* Walk the structure, correcting string offsets */
237 offset = 0;
Kumar Gala8d04f022007-10-24 11:04:22 -0500238 while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
Gerald Van Baren35748172007-03-31 12:00:56 -0400239 if (tag == FDT_PROP) {
Kumar Gala8d04f022007-10-24 11:04:22 -0500240 struct fdt_property *prop =
241 fdt_offset_ptr_w(fdt, offset, sizeof(*prop));
Gerald Van Baren35748172007-03-31 12:00:56 -0400242 int nameoff;
243
244 if (! prop)
245 return -FDT_ERR_BADSTRUCTURE;
246
247 nameoff = fdt32_to_cpu(prop->nameoff);
248 nameoff += fdt_size_dt_strings(fdt);
249 prop->nameoff = cpu_to_fdt32(nameoff);
250 }
251 offset = nextoffset;
252 }
253
254 /* Finally, adjust the header */
Kumar Gala8d04f022007-10-24 11:04:22 -0500255 fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
256 fdt_set_magic(fdt, FDT_MAGIC);
Gerald Van Baren35748172007-03-31 12:00:56 -0400257 return 0;
258}