TianoCore EDK2 master
Loading...
Searching...
No Matches
fdt_rw.c
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
58static int
59_fdt_blocks_misordered (
60 const void *fdt,
61 int mem_rsv_size,
62 int struct_size
63 )
64{
65 return (fdt_off_mem_rsvmap (fdt) < FDT_ALIGN (sizeof (struct fdt_header), 8))
66 || (fdt_off_dt_struct (fdt) <
67 (fdt_off_mem_rsvmap (fdt) + mem_rsv_size))
68 || (fdt_off_dt_strings (fdt) <
69 (fdt_off_dt_struct (fdt) + struct_size))
70 || (fdt_totalsize (fdt) <
71 (fdt_off_dt_strings (fdt) + fdt_size_dt_strings (fdt)));
72}
73
74static int
75_fdt_rw_check_header (
76 void *fdt
77 )
78{
79 FDT_CHECK_HEADER (fdt);
80
81 if (fdt_version (fdt) < 17) {
82 return -FDT_ERR_BADVERSION;
83 }
84
85 if (_fdt_blocks_misordered (
86 fdt,
87 sizeof (struct fdt_reserve_entry),
88 fdt_size_dt_struct (fdt)
89 ))
90 {
91 return -FDT_ERR_BADLAYOUT;
92 }
93
94 if (fdt_version (fdt) > 17) {
95 fdt_set_version (fdt, 17);
96 }
97
98 return 0;
99}
100
101#define FDT_RW_CHECK_HEADER(fdt) \
102 { \
103 int __err; \
104 if ((__err = _fdt_rw_check_header(fdt)) != 0) \
105 return __err; \
106 }
107
108static inline int
109_fdt_data_size (
110 void *fdt
111 )
112{
113 return fdt_off_dt_strings (fdt) + fdt_size_dt_strings (fdt);
114}
115
116static int
117_fdt_splice (
118 void *fdt,
119 void *splicepoint,
120 int oldlen,
121 int newlen
122 )
123{
124 char *p = splicepoint;
125 char *end = (char *)fdt + _fdt_data_size (fdt);
126
127 if (((p + oldlen) < p) || ((p + oldlen) > end)) {
128 return -FDT_ERR_BADOFFSET;
129 }
130
131 if ((p < (char *)fdt) || ((end - oldlen + newlen) < (char *)fdt)) {
132 return -FDT_ERR_BADOFFSET;
133 }
134
135 if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize (fdt))) {
136 return -FDT_ERR_NOSPACE;
137 }
138
139 memmove (p + newlen, p + oldlen, end - p - oldlen);
140 return 0;
141}
142
143static int
144_fdt_splice_mem_rsv (
145 void *fdt,
146 struct fdt_reserve_entry *p,
147 int oldn,
148 int newn
149 )
150{
151 int delta = (newn - oldn) * sizeof (*p);
152 int err;
153
154 err = _fdt_splice (fdt, p, oldn * sizeof (*p), newn * sizeof (*p));
155 if (err) {
156 return err;
157 }
158
159 fdt_set_off_dt_struct (fdt, fdt_off_dt_struct (fdt) + delta);
160 fdt_set_off_dt_strings (fdt, fdt_off_dt_strings (fdt) + delta);
161 return 0;
162}
163
164static int
165_fdt_splice_struct (
166 void *fdt,
167 void *p,
168 int oldlen,
169 int newlen
170 )
171{
172 int delta = newlen - oldlen;
173 int err;
174
175 if ((err = _fdt_splice (fdt, p, oldlen, newlen))) {
176 return err;
177 }
178
179 fdt_set_size_dt_struct (fdt, fdt_size_dt_struct (fdt) + delta);
180 fdt_set_off_dt_strings (fdt, fdt_off_dt_strings (fdt) + delta);
181 return 0;
182}
183
184static int
185_fdt_splice_string (
186 void *fdt,
187 int newlen
188 )
189{
190 void *p = (char *)fdt
191 + fdt_off_dt_strings (fdt) + fdt_size_dt_strings (fdt);
192 int err;
193
194 if ((err = _fdt_splice (fdt, p, 0, newlen))) {
195 return err;
196 }
197
198 fdt_set_size_dt_strings (fdt, fdt_size_dt_strings (fdt) + newlen);
199 return 0;
200}
201
202static int
203_fdt_find_add_string (
204 void *fdt,
205 const char *s
206 )
207{
208 char *strtab = (char *)fdt + fdt_off_dt_strings (fdt);
209 const char *p;
210 char *new;
211 int len = strlen (s) + 1;
212 int err;
213
214 p = _fdt_find_string (strtab, fdt_size_dt_strings (fdt), s);
215 if (p) {
216 /* found it */
217 return (p - strtab);
218 }
219
220 new = strtab + fdt_size_dt_strings (fdt);
221 err = _fdt_splice_string (fdt, len);
222 if (err) {
223 return err;
224 }
225
226 memcpy (new, s, len);
227 return (new - strtab);
228}
229
230int
231fdt_add_mem_rsv (
232 void *fdt,
233 uint64_t address,
234 uint64_t size
235 )
236{
237 struct fdt_reserve_entry *re;
238 int err;
239
240 FDT_RW_CHECK_HEADER (fdt);
241
242 re = _fdt_mem_rsv_w (fdt, fdt_num_mem_rsv (fdt));
243 err = _fdt_splice_mem_rsv (fdt, re, 0, 1);
244 if (err) {
245 return err;
246 }
247
248 re->address = cpu_to_fdt64 (address);
249 re->size = cpu_to_fdt64 (size);
250 return 0;
251}
252
253int
254fdt_del_mem_rsv (
255 void *fdt,
256 int n
257 )
258{
259 struct fdt_reserve_entry *re = _fdt_mem_rsv_w (fdt, n);
260
261 FDT_RW_CHECK_HEADER (fdt);
262
263 if (n >= fdt_num_mem_rsv (fdt)) {
264 return -FDT_ERR_NOTFOUND;
265 }
266
267 return _fdt_splice_mem_rsv (fdt, re, 1, 0);
268}
269
270static int
271_fdt_resize_property (
272 void *fdt,
273 int nodeoffset,
274 const char *name,
275 int len,
276 struct fdt_property **prop
277 )
278{
279 int oldlen;
280 int err;
281
282 *prop = fdt_get_property_w (fdt, nodeoffset, name, &oldlen);
283 if (!*prop) {
284 return oldlen;
285 }
286
287 if ((err = _fdt_splice_struct (
288 fdt,
289 (*prop)->data,
290 FDT_TAGALIGN (oldlen),
291 FDT_TAGALIGN (len)
292 )))
293 {
294 return err;
295 }
296
297 (*prop)->len = cpu_to_fdt32 (len);
298 return 0;
299}
300
301static int
302_fdt_add_property (
303 void *fdt,
304 int nodeoffset,
305 const char *name,
306 int len,
307 struct fdt_property **prop
308 )
309{
310 int proplen;
311 int nextoffset;
312 int namestroff;
313 int err;
314
315 if ((nextoffset = _fdt_check_node_offset (fdt, nodeoffset)) < 0) {
316 return nextoffset;
317 }
318
319 namestroff = _fdt_find_add_string (fdt, name);
320 if (namestroff < 0) {
321 return namestroff;
322 }
323
324 *prop = _fdt_offset_ptr_w (fdt, nextoffset);
325 proplen = sizeof (**prop) + FDT_TAGALIGN (len);
326
327 err = _fdt_splice_struct (fdt, *prop, 0, proplen);
328 if (err) {
329 return err;
330 }
331
332 (*prop)->tag = cpu_to_fdt32 (FDT_PROP);
333 (*prop)->nameoff = cpu_to_fdt32 (namestroff);
334 (*prop)->len = cpu_to_fdt32 (len);
335 return 0;
336}
337
338int
339fdt_set_name (
340 void *fdt,
341 int nodeoffset,
342 const char *name
343 )
344{
345 char *namep;
346 int oldlen, newlen;
347 int err;
348
349 FDT_RW_CHECK_HEADER (fdt);
350
351 namep = (char *)(uintptr_t)fdt_get_name (fdt, nodeoffset, &oldlen);
352 if (!namep) {
353 return oldlen;
354 }
355
356 newlen = strlen (name);
357
358 err = _fdt_splice_struct (
359 fdt,
360 namep,
361 FDT_TAGALIGN (oldlen+1),
362 FDT_TAGALIGN (newlen+1)
363 );
364 if (err) {
365 return err;
366 }
367
368 memcpy (namep, name, newlen+1);
369 return 0;
370}
371
372int
373fdt_setprop_placeholder (
374 void *fdt,
375 int nodeoffset,
376 const char *name,
377 int len,
378 void **prop_data
379 )
380{
381 struct fdt_property *prop;
382 int err;
383
384 FDT_RW_CHECK_HEADER (fdt);
385
386 err = _fdt_resize_property (fdt, nodeoffset, name, len, &prop);
387 if (err == -FDT_ERR_NOTFOUND) {
388 err = _fdt_add_property (fdt, nodeoffset, name, len, &prop);
389 }
390
391 if (err) {
392 return err;
393 }
394
395 *prop_data = prop->data;
396 return 0;
397}
398
399int
400fdt_setprop (
401 void *fdt,
402 int nodeoffset,
403 const char *name,
404 const void *val,
405 int len
406 )
407{
408 void *prop_data;
409 int err;
410
411 err = fdt_setprop_placeholder (fdt, nodeoffset, name, len, &prop_data);
412 if (err) {
413 return err;
414 }
415
416 if (len) {
417 memcpy (prop_data, val, len);
418 }
419
420 return 0;
421}
422
423int
424fdt_appendprop (
425 void *fdt,
426 int nodeoffset,
427 const char *name,
428 const void *val,
429 int len
430 )
431{
432 struct fdt_property *prop;
433 int err, oldlen, newlen;
434
435 FDT_RW_CHECK_HEADER (fdt);
436
437 prop = fdt_get_property_w (fdt, nodeoffset, name, &oldlen);
438 if (prop) {
439 newlen = len + oldlen;
440 err = _fdt_splice_struct (
441 fdt,
442 prop->data,
443 FDT_TAGALIGN (oldlen),
444 FDT_TAGALIGN (newlen)
445 );
446 if (err) {
447 return err;
448 }
449
450 prop->len = cpu_to_fdt32 (newlen);
451 memcpy (prop->data + oldlen, val, len);
452 } else {
453 err = _fdt_add_property (fdt, nodeoffset, name, len, &prop);
454 if (err) {
455 return err;
456 }
457
458 memcpy (prop->data, val, len);
459 }
460
461 return 0;
462}
463
464int
465fdt_delprop (
466 void *fdt,
467 int nodeoffset,
468 const char *name
469 )
470{
471 struct fdt_property *prop;
472 int len, proplen;
473
474 FDT_RW_CHECK_HEADER (fdt);
475
476 prop = fdt_get_property_w (fdt, nodeoffset, name, &len);
477 if (!prop) {
478 return len;
479 }
480
481 proplen = sizeof (*prop) + FDT_TAGALIGN (len);
482 return _fdt_splice_struct (fdt, prop, proplen, 0);
483}
484
485int
486fdt_add_subnode_namelen (
487 void *fdt,
488 int parentoffset,
489 const char *name,
490 int namelen
491 )
492{
493 struct fdt_node_header *nh;
494 int offset, nextoffset;
495 int nodelen;
496 int err;
497 uint32_t tag;
498 fdt32_t *endtag;
499
500 FDT_RW_CHECK_HEADER (fdt);
501
502 offset = fdt_subnode_offset_namelen (fdt, parentoffset, name, namelen);
503 if (offset >= 0) {
504 return -FDT_ERR_EXISTS;
505 } else if (offset != -FDT_ERR_NOTFOUND) {
506 return offset;
507 }
508
509 /* Try to place the new node after the parent's properties */
510 fdt_next_tag (fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
511 do {
512 offset = nextoffset;
513 tag = fdt_next_tag (fdt, offset, &nextoffset);
514 } while ((tag == FDT_PROP) || (tag == FDT_NOP));
515
516 nh = _fdt_offset_ptr_w (fdt, offset);
517 nodelen = sizeof (*nh) + FDT_TAGALIGN (namelen+1) + FDT_TAGSIZE;
518
519 err = _fdt_splice_struct (fdt, nh, 0, nodelen);
520 if (err) {
521 return err;
522 }
523
524 nh->tag = cpu_to_fdt32 (FDT_BEGIN_NODE);
525 memset (nh->name, 0, FDT_TAGALIGN (namelen+1));
526 memcpy (nh->name, name, namelen);
527 endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
528 *endtag = cpu_to_fdt32 (FDT_END_NODE);
529
530 return offset;
531}
532
533int
534fdt_add_subnode (
535 void *fdt,
536 int parentoffset,
537 const char *name
538 )
539{
540 return fdt_add_subnode_namelen (fdt, parentoffset, name, strlen (name));
541}
542
543int
544fdt_del_node (
545 void *fdt,
546 int nodeoffset
547 )
548{
549 int endoffset;
550
551 FDT_RW_CHECK_HEADER (fdt);
552
553 endoffset = _fdt_node_end_offset (fdt, nodeoffset);
554 if (endoffset < 0) {
555 return endoffset;
556 }
557
558 return _fdt_splice_struct (
559 fdt,
560 _fdt_offset_ptr_w (fdt, nodeoffset),
561 endoffset - nodeoffset,
562 0
563 );
564}
565
566static void
567_fdt_packblocks (
568 const char *old,
569 char *new,
570 int mem_rsv_size,
571 int struct_size
572 )
573{
574 int mem_rsv_off, struct_off, strings_off;
575
576 mem_rsv_off = FDT_ALIGN (sizeof (struct fdt_header), 8);
577 struct_off = mem_rsv_off + mem_rsv_size;
578 strings_off = struct_off + struct_size;
579
580 memmove (new + mem_rsv_off, old + fdt_off_mem_rsvmap (old), mem_rsv_size);
581 fdt_set_off_mem_rsvmap (new, mem_rsv_off);
582
583 memmove (new + struct_off, old + fdt_off_dt_struct (old), struct_size);
584 fdt_set_off_dt_struct (new, struct_off);
585 fdt_set_size_dt_struct (new, struct_size);
586
587 memmove (
588 new + strings_off,
589 old + fdt_off_dt_strings (old),
590 fdt_size_dt_strings (old)
591 );
592 fdt_set_off_dt_strings (new, strings_off);
593 fdt_set_size_dt_strings (new, fdt_size_dt_strings (old));
594}
595
596int
597fdt_open_into (
598 const void *fdt,
599 void *buf,
600 int bufsize
601 )
602{
603 int err;
604 int mem_rsv_size, struct_size;
605 int newsize;
606 const char *fdtstart = fdt;
607 const char *fdtend = fdtstart + fdt_totalsize (fdt);
608 char *tmp;
609
610 FDT_CHECK_HEADER (fdt);
611
612 mem_rsv_size = (fdt_num_mem_rsv (fdt)+1)
613 * sizeof (struct fdt_reserve_entry);
614
615 if (fdt_version (fdt) >= 17) {
616 struct_size = fdt_size_dt_struct (fdt);
617 } else {
618 struct_size = 0;
619 while (fdt_next_tag (fdt, struct_size, &struct_size) != FDT_END) {
620 }
621
622 if (struct_size < 0) {
623 return struct_size;
624 }
625 }
626
627 if (!_fdt_blocks_misordered (fdt, mem_rsv_size, struct_size)) {
628 /* no further work necessary */
629 err = fdt_move (fdt, buf, bufsize);
630 if (err) {
631 return err;
632 }
633
634 fdt_set_version (buf, 17);
635 fdt_set_size_dt_struct (buf, struct_size);
636 fdt_set_totalsize (buf, bufsize);
637 return 0;
638 }
639
640 /* Need to reorder */
641 newsize = FDT_ALIGN (sizeof (struct fdt_header), 8) + mem_rsv_size
642 + struct_size + fdt_size_dt_strings (fdt);
643
644 if (bufsize < newsize) {
645 return -FDT_ERR_NOSPACE;
646 }
647
648 /* First attempt to build converted tree at beginning of buffer */
649 tmp = buf;
650 /* But if that overlaps with the old tree... */
651 if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
652 /* Try right after the old tree instead */
653 tmp = (char *)(uintptr_t)fdtend;
654 if ((tmp + newsize) > ((char *)buf + bufsize)) {
655 return -FDT_ERR_NOSPACE;
656 }
657 }
658
659 _fdt_packblocks (fdt, tmp, mem_rsv_size, struct_size);
660 memmove (buf, tmp, newsize);
661
662 fdt_set_magic (buf, FDT_MAGIC);
663 fdt_set_totalsize (buf, bufsize);
664 fdt_set_version (buf, 17);
665 fdt_set_last_comp_version (buf, 16);
666 fdt_set_boot_cpuid_phys (buf, fdt_boot_cpuid_phys (fdt));
667
668 return 0;
669}
670
671int
672fdt_pack (
673 void *fdt
674 )
675{
676 int mem_rsv_size;
677
678 FDT_RW_CHECK_HEADER (fdt);
679
680 mem_rsv_size = (fdt_num_mem_rsv (fdt)+1)
681 * sizeof (struct fdt_reserve_entry);
682 _fdt_packblocks (fdt, fdt, mem_rsv_size, fdt_size_dt_struct (fdt));
683 fdt_set_totalsize (fdt, _fdt_data_size (fdt));
684
685 return 0;
686}
Definition: fdt.h:58
Definition: fdt.h:77