~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/skiboot/libfdt/fdt_sw.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * libfdt - Flat Device Tree manipulation
 
3
 * Copyright (C) 2006 David Gibson, IBM Corporation.
 
4
 *
 
5
 * libfdt is dual licensed: you can use it either under the terms of
 
6
 * the GPL, or the BSD license, at your option.
 
7
 *
 
8
 *  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.
 
12
 *
 
13
 *     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.
 
50
 */
 
51
#include "libfdt_env.h"
 
52
 
 
53
#include <fdt.h>
 
54
#include <libfdt.h>
 
55
 
 
56
#include "libfdt_internal.h"
 
57
#include <assert.h>
 
58
#include <mem_region-malloc.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_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
 
113
{
 
114
        struct fdt_reserve_entry *re;
 
115
        int offset;
 
116
 
 
117
        FDT_SW_CHECK_HEADER(fdt);
 
118
 
 
119
        if (fdt_size_dt_struct(fdt))
 
120
                return -FDT_ERR_BADSTATE;
 
121
 
 
122
        offset = fdt_off_dt_struct(fdt);
 
123
        if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
 
124
                return -FDT_ERR_NOSPACE;
 
125
 
 
126
        re = (struct fdt_reserve_entry *)((char *)fdt + offset);
 
127
        re->address = cpu_to_fdt64(addr);
 
128
        re->size = cpu_to_fdt64(size);
 
129
 
 
130
        fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
 
131
 
 
132
        return 0;
 
133
}
 
134
 
 
135
int fdt_finish_reservemap(void *fdt)
 
136
{
 
137
        return fdt_add_reservemap_entry(fdt, 0, 0);
 
138
}
 
139
 
 
140
int fdt_begin_node(void *fdt, const char *name)
 
141
{
 
142
        struct fdt_node_header *nh;
 
143
        int namelen = strlen(name) + 1;
 
144
 
 
145
        FDT_SW_CHECK_HEADER(fdt);
 
146
 
 
147
        nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
 
148
        if (! nh)
 
149
                return -FDT_ERR_NOSPACE;
 
150
 
 
151
        nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
 
152
        memcpy(nh->name, name, namelen);
 
153
        return 0;
 
154
}
 
155
 
 
156
int fdt_end_node(void *fdt)
 
157
{
 
158
        uint32_t *en;
 
159
 
 
160
        FDT_SW_CHECK_HEADER(fdt);
 
161
 
 
162
        en = _fdt_grab_space(fdt, FDT_TAGSIZE);
 
163
        if (! en)
 
164
                return -FDT_ERR_NOSPACE;
 
165
 
 
166
        *en = cpu_to_fdt32(FDT_END_NODE);
 
167
        return 0;
 
168
}
 
169
 
 
170
static int _fdt_find_add_string(void *fdt, const char *s)
 
171
{
 
172
        char *strtab = (char *)fdt + fdt_totalsize(fdt);
 
173
        const char *p;
 
174
        int strtabsize = fdt_size_dt_strings(fdt);
 
175
        int len = strlen(s) + 1;
 
176
        int struct_top, offset;
 
177
 
 
178
        p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
 
179
        if (p)
 
180
                return p - strtab;
 
181
 
 
182
        /* Add it */
 
183
        offset = -strtabsize - len;
 
184
        struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
 
185
        if (fdt_totalsize(fdt) + offset < struct_top)
 
186
                return 0; /* no more room :( */
 
187
 
 
188
        memcpy(strtab + offset, s, len);
 
189
        fdt_set_size_dt_strings(fdt, strtabsize + len);
 
190
        return offset;
 
191
}
 
192
 
 
193
int fdt_property(void *fdt, const char *name, const void *val, int len)
 
194
{
 
195
        struct fdt_property *prop;
 
196
        int nameoff;
 
197
 
 
198
        FDT_SW_CHECK_HEADER(fdt);
 
199
 
 
200
        nameoff = _fdt_find_add_string(fdt, name);
 
201
        if (nameoff == 0)
 
202
                return -FDT_ERR_NOSPACE;
 
203
 
 
204
        prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
 
205
        if (! prop)
 
206
                return -FDT_ERR_NOSPACE;
 
207
 
 
208
        prop->tag = cpu_to_fdt32(FDT_PROP);
 
209
        prop->nameoff = cpu_to_fdt32(nameoff);
 
210
        prop->len = cpu_to_fdt32(len);
 
211
        memcpy(prop->data, val, len);
 
212
        return 0;
 
213
}
 
214
 
 
215
int fdt_property_cells_v(void *fdt, unsigned const char *name, int count,
 
216
                         va_list args)
 
217
{
 
218
        uint32_t *buffer;
 
219
        int i;
 
220
 
 
221
        buffer = (uint32_t*)malloc(sizeof(uint32_t)*count);
 
222
        assert(buffer);
 
223
 
 
224
        for (i = 0; i < count; i++)
 
225
                buffer[i] = cpu_to_fdt32(va_arg(args, uint32_t));
 
226
 
 
227
        return fdt_property(fdt, name, buffer, sizeof(uint32_t)*count);
 
228
}
 
229
 
 
230
int fdt_property_cells(void *fdt, unsigned const char *name, int count, ...)
 
231
{
 
232
        va_list args;
 
233
        int ret;
 
234
 
 
235
        va_start(args, count);
 
236
        ret = fdt_property_cells_v(fdt, name, count, args);
 
237
        va_end(args);
 
238
 
 
239
        return ret;
 
240
}
 
241
 
 
242
int fdt_finish(void *fdt)
 
243
{
 
244
        char *p = (char *)fdt;
 
245
        uint32_t *end;
 
246
        int oldstroffset, newstroffset;
 
247
        uint32_t tag;
 
248
        int offset, nextoffset;
 
249
 
 
250
        FDT_SW_CHECK_HEADER(fdt);
 
251
 
 
252
        /* Add terminator */
 
253
        end = _fdt_grab_space(fdt, sizeof(*end));
 
254
        if (! end)
 
255
                return -FDT_ERR_NOSPACE;
 
256
        *end = cpu_to_fdt32(FDT_END);
 
257
 
 
258
        /* Relocate the string table */
 
259
        oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
 
260
        newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
 
261
        memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
 
262
        fdt_set_off_dt_strings(fdt, newstroffset);
 
263
 
 
264
        /* Walk the structure, correcting string offsets */
 
265
        offset = 0;
 
266
        while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
 
267
                if (tag == FDT_PROP) {
 
268
                        struct fdt_property *prop =
 
269
                                _fdt_offset_ptr_w(fdt, offset);
 
270
                        int nameoff;
 
271
 
 
272
                        nameoff = fdt32_to_cpu(prop->nameoff);
 
273
                        nameoff += fdt_size_dt_strings(fdt);
 
274
                        prop->nameoff = cpu_to_fdt32(nameoff);
 
275
                }
 
276
                offset = nextoffset;
 
277
        }
 
278
        if (nextoffset < 0)
 
279
                return nextoffset;
 
280
 
 
281
        /* Finally, adjust the header */
 
282
        fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
 
283
        fdt_set_magic(fdt, FDT_MAGIC);
 
284
        return 0;
 
285
}