]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - libfdt/fdt.c
Sync'd u-boot-net with mainline
[karo-tx-uboot.git] / libfdt / fdt.c
1 /*
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 "config.h"
20 #if CONFIG_OF_LIBFDT
21
22 #include "libfdt_env.h"
23
24 #include <fdt.h>
25 #include <libfdt.h>
26
27 #include "libfdt_internal.h"
28
29 int fdt_check_header(const void *fdt)
30 {
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
48 void *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
64 const 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
76 int fdt_move(const void *fdt, void *buf, int bufsize)
77 {
78         int err = fdt_check_header(fdt);
79
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 }
89
90 #endif /* CONFIG_OF_LIBFDT */