blob: 70fd026550736d16877512dd685b69380b6b2922 [file] [log] [blame]
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
Roger Meier3eaedea2013-07-27 01:12:38 +02004 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04005 */
Robert P. J. Day55019c52016-05-23 05:40:55 -04006#include <libfdt_env.h>
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04007#include <fdt.h>
8#include <libfdt.h>
9
10#include "libfdt_internal.h"
11
David Gibson9457d9d2008-07-09 14:10:24 +100012static int _fdt_sw_check_header(void *fdt)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040013{
David Gibson9457d9d2008-07-09 14:10:24 +100014 if (fdt_magic(fdt) != FDT_SW_MAGIC)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040015 return -FDT_ERR_BADMAGIC;
David Gibson148a49b2008-05-20 17:19:11 +100016 /* FIXME: should check more details about the header state */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040017 return 0;
18}
19
David Gibson9457d9d2008-07-09 14:10:24 +100020#define FDT_SW_CHECK_HEADER(fdt) \
David Gibson148a49b2008-05-20 17:19:11 +100021 { \
22 int err; \
David Gibson9457d9d2008-07-09 14:10:24 +100023 if ((err = _fdt_sw_check_header(fdt)) != 0) \
David Gibson148a49b2008-05-20 17:19:11 +100024 return err; \
25 }
26
Emil Medve6b098ae2009-02-23 10:43:36 -060027static void *_fdt_grab_space(void *fdt, size_t len)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040028{
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 Galac8ab7052007-10-24 11:04:22 -050038 fdt_set_size_dt_struct(fdt, offset + len);
David Gibson82c10232009-02-06 14:03:24 +110039 return _fdt_offset_ptr_w(fdt, offset);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040040}
41
42int 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 Gibson9457d9d2008-07-09 14:10:24 +100051 fdt_set_magic(fdt, FDT_SW_MAGIC);
Kumar Galac8ab7052007-10-24 11:04:22 -050052 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 Baren9e61ed82007-03-31 12:00:56 -040055
David Gibson9457d9d2008-07-09 14:10:24 +100056 fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header),
57 sizeof(struct fdt_reserve_entry)));
Kumar Galac8ab7052007-10-24 11:04:22 -050058 fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
59 fdt_set_off_dt_strings(fdt, bufsize);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040060
61 return 0;
62}
63
Simon Glass536774d2014-07-30 03:59:03 -060064int 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 Baren9e61ed82007-03-31 12:00:56 -040096int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
97{
98 struct fdt_reserve_entry *re;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040099 int offset;
100
David Gibson9457d9d2008-07-09 14:10:24 +1000101 FDT_SW_CHECK_HEADER(fdt);
David Gibson148a49b2008-05-20 17:19:11 +1000102
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400103 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 Gibsonb4141b82008-07-07 10:10:48 +1000110 re = (struct fdt_reserve_entry *)((char *)fdt + offset);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400111 re->address = cpu_to_fdt64(addr);
112 re->size = cpu_to_fdt64(size);
113
Kumar Galac8ab7052007-10-24 11:04:22 -0500114 fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400115
116 return 0;
117}
118
119int fdt_finish_reservemap(void *fdt)
120{
121 return fdt_add_reservemap_entry(fdt, 0, 0);
122}
123
124int fdt_begin_node(void *fdt, const char *name)
125{
126 struct fdt_node_header *nh;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400127 int namelen = strlen(name) + 1;
128
David Gibson9457d9d2008-07-09 14:10:24 +1000129 FDT_SW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400130
David Gibson9457d9d2008-07-09 14:10:24 +1000131 nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400132 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
140int fdt_end_node(void *fdt)
141{
Kim Phillips38c38152013-01-16 13:59:50 +0000142 fdt32_t *en;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400143
David Gibson9457d9d2008-07-09 14:10:24 +1000144 FDT_SW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400145
David Gibson9457d9d2008-07-09 14:10:24 +1000146 en = _fdt_grab_space(fdt, FDT_TAGSIZE);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400147 if (! en)
148 return -FDT_ERR_NOSPACE;
149
150 *en = cpu_to_fdt32(FDT_END_NODE);
151 return 0;
152}
153
David Gibson9457d9d2008-07-09 14:10:24 +1000154static int _fdt_find_add_string(void *fdt, const char *s)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400155{
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 Galac8ab7052007-10-24 11:04:22 -0500173 fdt_set_size_dt_strings(fdt, strtabsize + len);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400174 return offset;
175}
176
Simon Glass4973bf52016-02-22 22:55:39 -0700177int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400178{
179 struct fdt_property *prop;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400180 int nameoff;
181
David Gibson9457d9d2008-07-09 14:10:24 +1000182 FDT_SW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400183
David Gibson9457d9d2008-07-09 14:10:24 +1000184 nameoff = _fdt_find_add_string(fdt, name);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400185 if (nameoff == 0)
186 return -FDT_ERR_NOSPACE;
187
David Gibson9457d9d2008-07-09 14:10:24 +1000188 prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400189 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 Glass4973bf52016-02-22 22:55:39 -0700195 *valp = prop->data;
196 return 0;
197}
198
199int 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 Baren9e61ed82007-03-31 12:00:56 -0400208 return 0;
209}
210
211int fdt_finish(void *fdt)
212{
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400213 char *p = (char *)fdt;
Kim Phillips38c38152013-01-16 13:59:50 +0000214 fdt32_t *end;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400215 int oldstroffset, newstroffset;
216 uint32_t tag;
217 int offset, nextoffset;
218
David Gibson9457d9d2008-07-09 14:10:24 +1000219 FDT_SW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400220
221 /* Add terminator */
David Gibson9457d9d2008-07-09 14:10:24 +1000222 end = _fdt_grab_space(fdt, sizeof(*end));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400223 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 Galac8ab7052007-10-24 11:04:22 -0500231 fdt_set_off_dt_strings(fdt, newstroffset);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400232
233 /* Walk the structure, correcting string offsets */
234 offset = 0;
Kumar Galac8ab7052007-10-24 11:04:22 -0500235 while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400236 if (tag == FDT_PROP) {
Kumar Galac8ab7052007-10-24 11:04:22 -0500237 struct fdt_property *prop =
David Gibson82c10232009-02-06 14:03:24 +1100238 _fdt_offset_ptr_w(fdt, offset);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400239 int nameoff;
240
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400241 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 Gibson82c10232009-02-06 14:03:24 +1100247 if (nextoffset < 0)
248 return nextoffset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400249
250 /* Finally, adjust the header */
Kumar Galac8ab7052007-10-24 11:04:22 -0500251 fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
252 fdt_set_magic(fdt, FDT_MAGIC);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400253 return 0;
254}