blob: 55ebebf1eb20e8c7f9200881f99ecf03632db9a3 [file] [log] [blame]
David Gibson063693a2006-11-29 16:45:46 +11001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
David Gibson94816052007-06-13 16:30:48 +10005 * libfdt is dual licensed: you can use it either under the terms of
6 * the GPL, or the BSD license, at your option.
David Gibson063693a2006-11-29 16:45:46 +11007 *
David Gibson94816052007-06-13 16:30:48 +10008 * 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.
David Gibson063693a2006-11-29 16:45:46 +110012 *
David Gibson94816052007-06-13 16:30:48 +100013 * 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.
David Gibson063693a2006-11-29 16:45:46 +110050 */
51#include "libfdt_env.h"
52
53#include <fdt.h>
54#include <libfdt.h>
55
56#include "libfdt_internal.h"
57
David Gibsonb6d80a22008-07-09 14:10:24 +100058static int _fdt_sw_check_header(void *fdt)
David Gibson063693a2006-11-29 16:45:46 +110059{
David Gibsonb6d80a22008-07-09 14:10:24 +100060 if (fdt_magic(fdt) != FDT_SW_MAGIC)
David Gibson9a9fdf52006-12-15 15:12:51 +110061 return -FDT_ERR_BADMAGIC;
David Gibsonaa1baab2008-05-20 17:19:11 +100062 /* FIXME: should check more details about the header state */
David Gibson063693a2006-11-29 16:45:46 +110063 return 0;
64}
65
David Gibsonb6d80a22008-07-09 14:10:24 +100066#define FDT_SW_CHECK_HEADER(fdt) \
David Gibsonaa1baab2008-05-20 17:19:11 +100067 { \
68 int err; \
David Gibsonb6d80a22008-07-09 14:10:24 +100069 if ((err = _fdt_sw_check_header(fdt)) != 0) \
David Gibsonaa1baab2008-05-20 17:19:11 +100070 return err; \
71 }
72
Emil Medve804fed82009-02-23 10:43:36 -060073static void *_fdt_grab_space(void *fdt, size_t len)
David Gibson063693a2006-11-29 16:45:46 +110074{
David Gibsonfe92f6b2006-12-01 16:25:39 +110075 int offset = fdt_size_dt_struct(fdt);
David Gibson063693a2006-11-29 16:45:46 +110076 int spaceleft;
77
David Gibsonede25de2006-12-01 15:02:10 +110078 spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
79 - fdt_size_dt_strings(fdt);
David Gibson063693a2006-11-29 16:45:46 +110080
81 if ((offset + len < offset) || (offset + len > spaceleft))
82 return NULL;
83
David Gibson9b911342007-10-25 14:29:38 +100084 fdt_set_size_dt_struct(fdt, offset + len);
David Gibson1a020e42009-02-06 14:03:24 +110085 return _fdt_offset_ptr_w(fdt, offset);
David Gibson063693a2006-11-29 16:45:46 +110086}
87
David Gibson73d60922006-12-15 15:12:47 +110088int fdt_create(void *buf, int bufsize)
David Gibson063693a2006-11-29 16:45:46 +110089{
David Gibson73d60922006-12-15 15:12:47 +110090 void *fdt = buf;
David Gibson063693a2006-11-29 16:45:46 +110091
92 if (bufsize < sizeof(struct fdt_header))
David Gibson9a9fdf52006-12-15 15:12:51 +110093 return -FDT_ERR_NOSPACE;
David Gibson063693a2006-11-29 16:45:46 +110094
95 memset(buf, 0, bufsize);
96
David Gibsonb6d80a22008-07-09 14:10:24 +100097 fdt_set_magic(fdt, FDT_SW_MAGIC);
David Gibson9b911342007-10-25 14:29:38 +100098 fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
99 fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
100 fdt_set_totalsize(fdt, bufsize);
David Gibson063693a2006-11-29 16:45:46 +1100101
David Gibsonb6d80a22008-07-09 14:10:24 +1000102 fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header),
103 sizeof(struct fdt_reserve_entry)));
David Gibson9b911342007-10-25 14:29:38 +1000104 fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
105 fdt_set_off_dt_strings(fdt, bufsize);
David Gibson063693a2006-11-29 16:45:46 +1100106
David Gibson9a9fdf52006-12-15 15:12:51 +1100107 return 0;
David Gibson063693a2006-11-29 16:45:46 +1100108}
109
David Gibson73d60922006-12-15 15:12:47 +1100110int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
David Gibson063693a2006-11-29 16:45:46 +1100111{
112 struct fdt_reserve_entry *re;
David Gibson063693a2006-11-29 16:45:46 +1100113 int offset;
114
David Gibsonb6d80a22008-07-09 14:10:24 +1000115 FDT_SW_CHECK_HEADER(fdt);
David Gibsonaa1baab2008-05-20 17:19:11 +1000116
David Gibsonfe92f6b2006-12-01 16:25:39 +1100117 if (fdt_size_dt_struct(fdt))
David Gibson9a9fdf52006-12-15 15:12:51 +1100118 return -FDT_ERR_BADSTATE;
David Gibson063693a2006-11-29 16:45:46 +1100119
David Gibsonede25de2006-12-01 15:02:10 +1100120 offset = fdt_off_dt_struct(fdt);
121 if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
David Gibson9a9fdf52006-12-15 15:12:51 +1100122 return -FDT_ERR_NOSPACE;
David Gibson063693a2006-11-29 16:45:46 +1100123
David Gibson36786db2008-07-07 10:10:48 +1000124 re = (struct fdt_reserve_entry *)((char *)fdt + offset);
David Gibson063693a2006-11-29 16:45:46 +1100125 re->address = cpu_to_fdt64(addr);
126 re->size = cpu_to_fdt64(size);
127
David Gibson9b911342007-10-25 14:29:38 +1000128 fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
David Gibson063693a2006-11-29 16:45:46 +1100129
130 return 0;
131}
132
David Gibson73d60922006-12-15 15:12:47 +1100133int fdt_finish_reservemap(void *fdt)
David Gibson063693a2006-11-29 16:45:46 +1100134{
135 return fdt_add_reservemap_entry(fdt, 0, 0);
136}
137
David Gibson73d60922006-12-15 15:12:47 +1100138int fdt_begin_node(void *fdt, const char *name)
David Gibson063693a2006-11-29 16:45:46 +1100139{
140 struct fdt_node_header *nh;
David Gibson063693a2006-11-29 16:45:46 +1100141 int namelen = strlen(name) + 1;
142
David Gibsonb6d80a22008-07-09 14:10:24 +1000143 FDT_SW_CHECK_HEADER(fdt);
David Gibson063693a2006-11-29 16:45:46 +1100144
David Gibsonb6d80a22008-07-09 14:10:24 +1000145 nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
David Gibson063693a2006-11-29 16:45:46 +1100146 if (! nh)
David Gibson9a9fdf52006-12-15 15:12:51 +1100147 return -FDT_ERR_NOSPACE;
David Gibson063693a2006-11-29 16:45:46 +1100148
149 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
150 memcpy(nh->name, name, namelen);
151 return 0;
152}
153
David Gibson73d60922006-12-15 15:12:47 +1100154int fdt_end_node(void *fdt)
David Gibson063693a2006-11-29 16:45:46 +1100155{
156 uint32_t *en;
David Gibson063693a2006-11-29 16:45:46 +1100157
David Gibsonb6d80a22008-07-09 14:10:24 +1000158 FDT_SW_CHECK_HEADER(fdt);
David Gibson063693a2006-11-29 16:45:46 +1100159
David Gibsonb6d80a22008-07-09 14:10:24 +1000160 en = _fdt_grab_space(fdt, FDT_TAGSIZE);
David Gibson063693a2006-11-29 16:45:46 +1100161 if (! en)
David Gibson9a9fdf52006-12-15 15:12:51 +1100162 return -FDT_ERR_NOSPACE;
David Gibson063693a2006-11-29 16:45:46 +1100163
164 *en = cpu_to_fdt32(FDT_END_NODE);
165 return 0;
166}
167
David Gibsonb6d80a22008-07-09 14:10:24 +1000168static int _fdt_find_add_string(void *fdt, const char *s)
David Gibson063693a2006-11-29 16:45:46 +1100169{
David Gibsonede25de2006-12-01 15:02:10 +1100170 char *strtab = (char *)fdt + fdt_totalsize(fdt);
David Gibsonaeddfe22006-12-01 15:11:58 +1100171 const char *p;
David Gibsonede25de2006-12-01 15:02:10 +1100172 int strtabsize = fdt_size_dt_strings(fdt);
David Gibson063693a2006-11-29 16:45:46 +1100173 int len = strlen(s) + 1;
174 int struct_top, offset;
175
David Gibsonaeddfe22006-12-01 15:11:58 +1100176 p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
177 if (p)
178 return p - strtab;
David Gibson063693a2006-11-29 16:45:46 +1100179
180 /* Add it */
181 offset = -strtabsize - len;
David Gibsonfe92f6b2006-12-01 16:25:39 +1100182 struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
David Gibsonede25de2006-12-01 15:02:10 +1100183 if (fdt_totalsize(fdt) + offset < struct_top)
David Gibson063693a2006-11-29 16:45:46 +1100184 return 0; /* no more room :( */
185
186 memcpy(strtab + offset, s, len);
David Gibson9b911342007-10-25 14:29:38 +1000187 fdt_set_size_dt_strings(fdt, strtabsize + len);
David Gibson063693a2006-11-29 16:45:46 +1100188 return offset;
189}
190
David Gibson73d60922006-12-15 15:12:47 +1100191int fdt_property(void *fdt, const char *name, const void *val, int len)
David Gibson063693a2006-11-29 16:45:46 +1100192{
193 struct fdt_property *prop;
David Gibson063693a2006-11-29 16:45:46 +1100194 int nameoff;
195
David Gibsonb6d80a22008-07-09 14:10:24 +1000196 FDT_SW_CHECK_HEADER(fdt);
David Gibson063693a2006-11-29 16:45:46 +1100197
David Gibsonb6d80a22008-07-09 14:10:24 +1000198 nameoff = _fdt_find_add_string(fdt, name);
David Gibson063693a2006-11-29 16:45:46 +1100199 if (nameoff == 0)
David Gibson9a9fdf52006-12-15 15:12:51 +1100200 return -FDT_ERR_NOSPACE;
David Gibson063693a2006-11-29 16:45:46 +1100201
David Gibsonb6d80a22008-07-09 14:10:24 +1000202 prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
David Gibson063693a2006-11-29 16:45:46 +1100203 if (! prop)
David Gibson9a9fdf52006-12-15 15:12:51 +1100204 return -FDT_ERR_NOSPACE;
David Gibson063693a2006-11-29 16:45:46 +1100205
206 prop->tag = cpu_to_fdt32(FDT_PROP);
207 prop->nameoff = cpu_to_fdt32(nameoff);
208 prop->len = cpu_to_fdt32(len);
209 memcpy(prop->data, val, len);
210 return 0;
211}
212
David Gibson73d60922006-12-15 15:12:47 +1100213int fdt_finish(void *fdt)
David Gibson063693a2006-11-29 16:45:46 +1100214{
David Gibson063693a2006-11-29 16:45:46 +1100215 char *p = (char *)fdt;
216 uint32_t *end;
David Gibsonede25de2006-12-01 15:02:10 +1100217 int oldstroffset, newstroffset;
David Gibson063693a2006-11-29 16:45:46 +1100218 uint32_t tag;
219 int offset, nextoffset;
220
David Gibsonb6d80a22008-07-09 14:10:24 +1000221 FDT_SW_CHECK_HEADER(fdt);
David Gibson063693a2006-11-29 16:45:46 +1100222
223 /* Add terminator */
David Gibsonb6d80a22008-07-09 14:10:24 +1000224 end = _fdt_grab_space(fdt, sizeof(*end));
David Gibson063693a2006-11-29 16:45:46 +1100225 if (! end)
David Gibson9a9fdf52006-12-15 15:12:51 +1100226 return -FDT_ERR_NOSPACE;
David Gibson063693a2006-11-29 16:45:46 +1100227 *end = cpu_to_fdt32(FDT_END);
228
229 /* Relocate the string table */
David Gibsonede25de2006-12-01 15:02:10 +1100230 oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
David Gibsonfe92f6b2006-12-01 16:25:39 +1100231 newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
David Gibsonede25de2006-12-01 15:02:10 +1100232 memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
David Gibson9b911342007-10-25 14:29:38 +1000233 fdt_set_off_dt_strings(fdt, newstroffset);
David Gibson063693a2006-11-29 16:45:46 +1100234
235 /* Walk the structure, correcting string offsets */
236 offset = 0;
David Gibson3c44c872007-10-24 11:06:09 +1000237 while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
David Gibson063693a2006-11-29 16:45:46 +1100238 if (tag == FDT_PROP) {
David Gibsona6c76f92007-06-13 14:18:10 +1000239 struct fdt_property *prop =
David Gibson1a020e42009-02-06 14:03:24 +1100240 _fdt_offset_ptr_w(fdt, offset);
David Gibson063693a2006-11-29 16:45:46 +1100241 int nameoff;
242
David Gibson063693a2006-11-29 16:45:46 +1100243 nameoff = fdt32_to_cpu(prop->nameoff);
David Gibsonede25de2006-12-01 15:02:10 +1100244 nameoff += fdt_size_dt_strings(fdt);
David Gibson063693a2006-11-29 16:45:46 +1100245 prop->nameoff = cpu_to_fdt32(nameoff);
246 }
247 offset = nextoffset;
248 }
David Gibson1a020e42009-02-06 14:03:24 +1100249 if (nextoffset < 0)
250 return nextoffset;
David Gibson063693a2006-11-29 16:45:46 +1100251
252 /* Finally, adjust the header */
David Gibson9b911342007-10-25 14:29:38 +1000253 fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
254 fdt_set_magic(fdt, FDT_MAGIC);
David Gibson063693a2006-11-29 16:45:46 +1100255 return 0;
256}