blob: 1ee67ad19cd8ba118bf5d1c85f4c5f2f1ac75b41 [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 */
Gerald Van Baren8096b3b2007-04-20 22:46:53 -040019#include "config.h"
20#if CONFIG_OF_LIBFDT
21
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040022#include "libfdt_env.h"
23
24#include <fdt.h>
25#include <libfdt.h>
26
27#include "libfdt_internal.h"
28
Gerald Van Baren6679f922007-04-06 14:17:14 -040029int fdt_check_header(const void *fdt)
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040030{
31 if (fdt_magic(fdt) == FDT_MAGIC) {
32 /* Complete tree */
33 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
34 return -FDT_ERR_BADVERSION;
35 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
36 return -FDT_ERR_BADVERSION;
37 } else if (fdt_magic(fdt) == SW_MAGIC) {
38 /* Unfinished sequential-write blob */
39 if (fdt_size_dt_struct(fdt) == 0)
40 return -FDT_ERR_BADSTATE;
41 } else {
42 return -FDT_ERR_BADMAGIC;
43 }
44
45 return 0;
46}
47
48void *fdt_offset_ptr(const void *fdt, int offset, int len)
49{
50 void *p;
51
52 if (fdt_version(fdt) >= 0x11)
53 if (((offset + len) < offset)
54 || ((offset + len) > fdt_size_dt_struct(fdt)))
55 return NULL;
56
57 p = _fdt_offset_ptr(fdt, offset);
58
59 if (p + len < p)
60 return NULL;
61 return p;
62}
63
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040064const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
65{
66 int len = strlen(s) + 1;
67 const char *last = strtab + tabsize - len;
68 const char *p;
69
70 for (p = strtab; p <= last; p++)
71 if (memeq(p, s, len))
72 return p;
73 return NULL;
74}
75
76int fdt_move(const void *fdt, void *buf, int bufsize)
77{
Gerald Van Baren6679f922007-04-06 14:17:14 -040078 int err = fdt_check_header(fdt);
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040079
80 if (err)
81 return err;
82
83 if (fdt_totalsize(fdt) > bufsize)
84 return -FDT_ERR_NOSPACE;
85
86 memmove(buf, fdt, fdt_totalsize(fdt));
87 return 0;
88}
Gerald Van Baren8096b3b2007-04-20 22:46:53 -040089
90#endif /* CONFIG_OF_LIBFDT */