blob: 4b1c8abf95e90c4ec6fc45a44e81b357e9b80fb5 [file] [log] [blame]
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001/*
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
26int _fdt_check_header(const void *fdt)
27{
28 if (fdt_magic(fdt) == FDT_MAGIC) {
29 /* Complete tree */
30 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
31 return -FDT_ERR_BADVERSION;
32 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
33 return -FDT_ERR_BADVERSION;
34 } else if (fdt_magic(fdt) == SW_MAGIC) {
35 /* Unfinished sequential-write blob */
36 if (fdt_size_dt_struct(fdt) == 0)
37 return -FDT_ERR_BADSTATE;
38 } else {
39 return -FDT_ERR_BADMAGIC;
40 }
41
42 return 0;
43}
44
45void *fdt_offset_ptr(const void *fdt, int offset, int len)
46{
47 void *p;
48
49 if (fdt_version(fdt) >= 0x11)
50 if (((offset + len) < offset)
51 || ((offset + len) > fdt_size_dt_struct(fdt)))
52 return NULL;
53
54 p = _fdt_offset_ptr(fdt, offset);
55
56 if (p + len < p)
57 return NULL;
58 return p;
59}
60
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040061const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
62{
63 int len = strlen(s) + 1;
64 const char *last = strtab + tabsize - len;
65 const char *p;
66
67 for (p = strtab; p <= last; p++)
68 if (memeq(p, s, len))
69 return p;
70 return NULL;
71}
72
73int fdt_move(const void *fdt, void *buf, int bufsize)
74{
75 int err = _fdt_check_header(fdt);
76
77 if (err)
78 return err;
79
80 if (fdt_totalsize(fdt) > bufsize)
81 return -FDT_ERR_NOSPACE;
82
83 memmove(buf, fdt, fdt_totalsize(fdt));
84 return 0;
85}