blob: 3572ef823afeaf8fbfb4662533feeeb2242d50dc [file] [log] [blame]
David Gibson3da0f9a2006-11-27 16:21:28 +11001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include "libfdt_env.h"
20
21#include <fdt.h>
22#include <libfdt.h>
23
24#include "libfdt_internal.h"
25
David Gibson73d60922006-12-15 15:12:47 +110026int _fdt_check_header(const void *fdt)
David Gibson42369762006-12-01 15:07:19 +110027{
28 if (fdt_magic(fdt) == FDT_MAGIC) {
29 /* Complete tree */
30 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
David Gibson9a9fdf52006-12-15 15:12:51 +110031 return -FDT_ERR_BADVERSION;
David Gibson42369762006-12-01 15:07:19 +110032 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
David Gibson9a9fdf52006-12-15 15:12:51 +110033 return -FDT_ERR_BADVERSION;
David Gibson42369762006-12-01 15:07:19 +110034 } else if (fdt_magic(fdt) == SW_MAGIC) {
35 /* Unfinished sequential-write blob */
David Gibsonfe92f6b2006-12-01 16:25:39 +110036 if (fdt_size_dt_struct(fdt) == 0)
David Gibson9a9fdf52006-12-15 15:12:51 +110037 return -FDT_ERR_BADSTATE;
David Gibson42369762006-12-01 15:07:19 +110038 } else {
David Gibson9a9fdf52006-12-15 15:12:51 +110039 return -FDT_ERR_BADMAGIC;
David Gibson42369762006-12-01 15:07:19 +110040 }
41
42 return 0;
43}
44
David Gibsona6c76f92007-06-13 14:18:10 +100045const void *fdt_offset_ptr(const void *fdt, int offset, int len)
David Gibson3da0f9a2006-11-27 16:21:28 +110046{
David Gibsona6c76f92007-06-13 14:18:10 +100047 const void *p;
David Gibson3da0f9a2006-11-27 16:21:28 +110048
David Gibsonfe92f6b2006-12-01 16:25:39 +110049 if (fdt_version(fdt) >= 0x11)
50 if (((offset + len) < offset)
51 || ((offset + len) > fdt_size_dt_struct(fdt)))
52 return NULL;
53
David Gibson568b5692006-12-12 15:46:14 +110054 p = _fdt_offset_ptr(fdt, offset);
David Gibson3da0f9a2006-11-27 16:21:28 +110055
56 if (p + len < p)
57 return NULL;
58 return p;
59}
60
David Gibson73d60922006-12-15 15:12:47 +110061uint32_t _fdt_next_tag(const void *fdt, int offset, int *nextoffset)
David Gibson3da0f9a2006-11-27 16:21:28 +110062{
63 const uint32_t *tagp, *lenp;
64 uint32_t tag;
65 const char *p;
66
67 if (offset % FDT_TAGSIZE)
68 return -1;
69
70 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
71 if (! tagp)
72 return FDT_END; /* premature end */
73 tag = fdt32_to_cpu(*tagp);
74 offset += FDT_TAGSIZE;
75
76 switch (tag) {
77 case FDT_BEGIN_NODE:
78 /* skip name */
79 do {
80 p = fdt_offset_ptr(fdt, offset++, 1);
81 } while (p && (*p != '\0'));
82 if (! p)
83 return FDT_END;
84 break;
85 case FDT_PROP:
David Gibson07a12a02007-02-23 14:40:14 +110086 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
David Gibson3da0f9a2006-11-27 16:21:28 +110087 if (! lenp)
88 return FDT_END;
89 /* skip name offset, length and value */
90 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
91 break;
92 }
93
94 if (nextoffset)
95 *nextoffset = ALIGN(offset, FDT_TAGSIZE);
96
97 return tag;
98}
David Gibson42369762006-12-01 15:07:19 +110099
David Gibsonaeddfe22006-12-01 15:11:58 +1100100const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
101{
102 int len = strlen(s) + 1;
103 const char *last = strtab + tabsize - len;
104 const char *p;
105
106 for (p = strtab; p <= last; p++)
107 if (memeq(p, s, len))
108 return p;
109 return NULL;
110}
111
David Gibson73d60922006-12-15 15:12:47 +1100112int fdt_move(const void *fdt, void *buf, int bufsize)
David Gibson42369762006-12-01 15:07:19 +1100113{
114 int err = _fdt_check_header(fdt);
115
116 if (err)
David Gibson73d60922006-12-15 15:12:47 +1100117 return err;
David Gibson42369762006-12-01 15:07:19 +1100118
119 if (fdt_totalsize(fdt) > bufsize)
David Gibson9a9fdf52006-12-15 15:12:51 +1100120 return -FDT_ERR_NOSPACE;
David Gibson42369762006-12-01 15:07:19 +1100121
122 memmove(buf, fdt, fdt_totalsize(fdt));
David Gibson9a9fdf52006-12-15 15:12:51 +1100123 return 0;
David Gibson42369762006-12-01 15:07:19 +1100124}