Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libfdt.h
Go to the documentation of this file.
1 #ifndef _LIBFDT_H
2 #define _LIBFDT_H
3 /*
4  * libfdt - Flat Device Tree manipulation
5  * Copyright (C) 2006 David Gibson, IBM Corporation.
6  *
7  * libfdt is dual licensed: you can use it either under the terms of
8  * the GPL, or the BSD license, at your option.
9  *
10  * a) This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of the
13  * License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this library; if not, write to the Free
22  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23  * MA 02110-1301 USA
24  *
25  * Alternatively,
26  *
27  * b) Redistribution and use in source and binary forms, with or
28  * without modification, are permitted provided that the following
29  * conditions are met:
30  *
31  * 1. Redistributions of source code must retain the above
32  * copyright notice, this list of conditions and the following
33  * disclaimer.
34  * 2. Redistributions in binary form must reproduce the above
35  * copyright notice, this list of conditions and the following
36  * disclaimer in the documentation and/or other materials
37  * provided with the distribution.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
40  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
41  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
44  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
50  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
51  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52  */
53 
54 #include <libfdt_env.h>
55 #include <fdt.h>
56 
57 #define FDT_FIRST_SUPPORTED_VERSION 0x10
58 #define FDT_LAST_SUPPORTED_VERSION 0x11
59 
60 /* Error codes: informative error codes */
61 #define FDT_ERR_NOTFOUND 1
62  /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
63 #define FDT_ERR_EXISTS 2
64  /* FDT_ERR_EXISTS: Attemped to create a node or property which
65  * already exists */
66 #define FDT_ERR_NOSPACE 3
67  /* FDT_ERR_NOSPACE: Operation needed to expand the device
68  * tree, but its buffer did not have sufficient space to
69  * contain the expanded tree. Use fdt_open_into() to move the
70  * device tree to a buffer with more space. */
71 
72 /* Error codes: codes for bad parameters */
73 #define FDT_ERR_BADOFFSET 4
74  /* FDT_ERR_BADOFFSET: Function was passed a structure block
75  * offset which is out-of-bounds, or which points to an
76  * unsuitable part of the structure for the operation. */
77 #define FDT_ERR_BADPATH 5
78  /* FDT_ERR_BADPATH: Function was passed a badly formatted path
79  * (e.g. missing a leading / for a function which requires an
80  * absolute path) */
81 #define FDT_ERR_BADPHANDLE 6
82  /* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle
83  * value. phandle values of 0 and -1 are not permitted. */
84 #define FDT_ERR_BADSTATE 7
85  /* FDT_ERR_BADSTATE: Function was passed an incomplete device
86  * tree created by the sequential-write functions, which is
87  * not sufficiently complete for the requested operation. */
88 
89 /* Error codes: codes for bad device tree blobs */
90 #define FDT_ERR_TRUNCATED 8
91  /* FDT_ERR_TRUNCATED: Structure block of the given device tree
92  * ends without an FDT_END tag. */
93 #define FDT_ERR_BADMAGIC 9
94  /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
95  * device tree at all - it is missing the flattened device
96  * tree magic number. */
97 #define FDT_ERR_BADVERSION 10
98  /* FDT_ERR_BADVERSION: Given device tree has a version which
99  * can't be handled by the requested operation. For
100  * read-write functions, this may mean that fdt_open_into() is
101  * required to convert the tree to the expected version. */
102 #define FDT_ERR_BADSTRUCTURE 11
103  /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
104  * structure block or other serious error (e.g. misnested
105  * nodes, or subnodes preceding properties). */
106 #define FDT_ERR_BADLAYOUT 12
107  /* FDT_ERR_BADLAYOUT: For read-write functions, the given
108  * device tree has it's sub-blocks in an order that the
109  * function can't handle (memory reserve map, then structure,
110  * then strings). Use fdt_open_into() to reorganize the tree
111  * into a form suitable for the read-write operations. */
112 
113 /* "Can't happen" error indicating a bug in libfdt */
114 #define FDT_ERR_INTERNAL 13
115  /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
116  * Should never be returned, if it is, it indicates a bug in
117  * libfdt itself. */
118 
119 #define FDT_ERR_MAX 13
120 
121 /**********************************************************************/
122 /* Low-level functions (you probably don't need these) */
123 /**********************************************************************/
124 
125 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
126 static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
127 {
128  return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
129 }
130 
131 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
132 
133 /**********************************************************************/
134 /* Traversal functions */
135 /**********************************************************************/
136 
137 int fdt_next_node(const void *fdt, int offset, int *depth);
138 
139 /**********************************************************************/
140 /* General functions */
141 /**********************************************************************/
142 
143 #define fdt_get_header(fdt, field) \
144  (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
145 #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
146 #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
147 #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
148 #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
149 #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
150 #define fdt_version(fdt) (fdt_get_header(fdt, version))
151 #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
152 #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
153 #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
154 #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
155 
156 #define __fdt_set_hdr(name) \
157  static inline void fdt_set_##name(void *fdt, uint32_t val) \
158  { \
159  struct fdt_header *fdth = (struct fdt_header*)fdt; \
160  fdth->name = cpu_to_fdt32(val); \
161  }
163 __fdt_set_hdr(totalsize);
164 __fdt_set_hdr(off_dt_struct);
165 __fdt_set_hdr(off_dt_strings);
166 __fdt_set_hdr(off_mem_rsvmap);
168 __fdt_set_hdr(last_comp_version);
170 __fdt_set_hdr(size_dt_strings);
171 __fdt_set_hdr(size_dt_struct);
172 #undef __fdt_set_hdr
173 
188 int fdt_check_header(const void *fdt);
189 
209 int fdt_move(const void *fdt, void *buf, int bufsize);
210 
211 /**********************************************************************/
212 /* Read-only functions */
213 /**********************************************************************/
214 
227 const char *fdt_string(const void *fdt, int stroffset);
228 
240 int fdt_num_mem_rsv(const void *fdt);
241 
257 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
258 
271 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
272  const char *name, int namelen);
296 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
297 
320 int fdt_path_offset(const void *fdt, const char *path);
321 
343 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
344 
363 int fdt_first_property_offset(const void *fdt, int nodeoffset);
364 
384 int fdt_next_property_offset(const void *fdt, int offset);
385 
410 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
411  int offset,
412  int *lenp);
413 
425 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
426  int nodeoffset,
427  const char *name,
428  int namelen, int *lenp);
429 
457 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
458  const char *name, int *lenp);
459 static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
460  const char *name,
461  int *lenp)
462 {
463  return (struct fdt_property *)(uintptr_t)
464  fdt_get_property(fdt, nodeoffset, name, lenp);
465 }
466 
498 const void *fdt_getprop_by_offset(const void *fdt, int offset,
499  const char **namep, int *lenp);
500 
512 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
513  const char *name, int namelen, int *lenp);
514 
542 const void *fdt_getprop(const void *fdt, int nodeoffset,
543  const char *name, int *lenp);
544 static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
545  const char *name, int *lenp)
546 {
547  return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
548 }
549 
562 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
563 
573 const char *fdt_get_alias_namelen(const void *fdt,
574  const char *name, int namelen);
575 
588 const char *fdt_get_alias(const void *fdt, const char *name);
589 
615 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
616 
647 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
648  int supernodedepth, int *nodedepth);
649 
669 int fdt_node_depth(const void *fdt, int nodeoffset);
670 
692 int fdt_parent_offset(const void *fdt, int nodeoffset);
693 
732 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
733  const char *propname,
734  const void *propval, int proplen);
735 
755 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
756 
779 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
780  const char *compatible);
781 
816 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
817  const char *compatible);
818 
819 /**********************************************************************/
820 /* Write-in-place functions */
821 /**********************************************************************/
822 
851 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
852  const void *val, int len);
853 
882 static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
883  const char *name, uint32_t val)
884 {
885  val = cpu_to_fdt32(val);
886  return fdt_setprop_inplace(fdt, nodeoffset, name, &val, sizeof(val));
887 }
888 
917 static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
918  const char *name, uint64_t val)
919 {
920  val = cpu_to_fdt64(val);
921  return fdt_setprop_inplace(fdt, nodeoffset, name, &val, sizeof(val));
922 }
923 
929 static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
930  const char *name, uint32_t val)
931 {
932  return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
933 }
934 
959 int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
960 
983 int fdt_nop_node(void *fdt, int nodeoffset);
984 
985 /**********************************************************************/
986 /* Sequential write functions */
987 /**********************************************************************/
988 
989 int fdt_create(void *buf, int bufsize);
991 int fdt_finish_reservemap(void *fdt);
992 int fdt_begin_node(void *fdt, const char *name);
993 int fdt_property(void *fdt, const char *name, const void *val, int len);
994 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
995 {
996  val = cpu_to_fdt32(val);
997  return fdt_property(fdt, name, &val, sizeof(val));
998 }
999 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1000 {
1001  val = cpu_to_fdt64(val);
1002  return fdt_property(fdt, name, &val, sizeof(val));
1003 }
1004 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1005 {
1006  return fdt_property_u32(fdt, name, val);
1007 }
1008 #define fdt_property_string(fdt, name, str) \
1009  fdt_property(fdt, name, str, strlen(str)+1)
1010 int fdt_end_node(void *fdt);
1011 int fdt_finish(void *fdt);
1012 
1013 /**********************************************************************/
1014 /* Read-write functions */
1015 /**********************************************************************/
1016 
1017 int fdt_create_empty_tree(void *buf, int bufsize);
1018 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1019 int fdt_pack(void *fdt);
1020 
1043 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1044 
1067 int fdt_del_mem_rsv(void *fdt, int n);
1068 
1093 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1094 
1123 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1124  const void *val, int len);
1125 
1154 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1155  uint32_t val)
1156 {
1157  val = cpu_to_fdt32(val);
1158  return fdt_setprop(fdt, nodeoffset, name, &val, sizeof(val));
1159 }
1160 
1189 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1190  uint64_t val)
1191 {
1192  val = cpu_to_fdt64(val);
1193  return fdt_setprop(fdt, nodeoffset, name, &val, sizeof(val));
1194 }
1195 
1201 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1202  uint32_t val)
1203 {
1204  return fdt_setprop_u32(fdt, nodeoffset, name, val);
1205 }
1206 
1235 #define fdt_setprop_string(fdt, nodeoffset, name, str) \
1236  fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1237 
1265 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1266  const void *val, int len);
1267 
1296 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1297  const char *name, uint32_t val)
1298 {
1299  val = cpu_to_fdt32(val);
1300  return fdt_appendprop(fdt, nodeoffset, name, &val, sizeof(val));
1301 }
1302 
1331 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1332  const char *name, uint64_t val)
1333 {
1334  val = cpu_to_fdt64(val);
1335  return fdt_appendprop(fdt, nodeoffset, name, &val, sizeof(val));
1336 }
1337 
1343 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1344  const char *name, uint32_t val)
1345 {
1346  return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1347 }
1348 
1376 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1377  fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1378 
1401 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1402 
1415 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1416  const char *name, int namelen);
1417 
1447 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1448 
1470 int fdt_del_node(void *fdt, int nodeoffset);
1471 
1472 /**********************************************************************/
1473 /* Debugging / informational functions */
1474 /**********************************************************************/
1475 
1476 const char *fdt_strerror(int errval);
1477 
1478 #endif /* _LIBFDT_H */