LibXenon
Bare-metal Xbox 360 homebrew library
Loading...
Searching...
No Matches
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
125const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
126static 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
131uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
132
133/**********************************************************************/
134/* Traversal functions */
135/**********************************************************************/
136
137int 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);
169__fdt_set_hdr(boot_cpuid_phys);
170__fdt_set_hdr(size_dt_strings);
171__fdt_set_hdr(size_dt_struct);
172#undef __fdt_set_hdr
173
188int fdt_check_header(const void *fdt);
189
209int fdt_move(const void *fdt, void *buf, int bufsize);
210
211/**********************************************************************/
212/* Read-only functions */
213/**********************************************************************/
214
227const char *fdt_string(const void *fdt, int stroffset);
228
240int fdt_num_mem_rsv(const void *fdt);
241
257int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
258
271int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
272 const char *name, int namelen);
296int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
297
320int fdt_path_offset(const void *fdt, const char *path);
321
343const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
344
363int fdt_first_property_offset(const void *fdt, int nodeoffset);
364
384int fdt_next_property_offset(const void *fdt, int offset);
385
410const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
411 int offset,
412 int *lenp);
413
425const struct fdt_property *fdt_get_property_namelen(const void *fdt,
426 int nodeoffset,
427 const char *name,
428 int namelen, int *lenp);
429
457const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
458 const char *name, int *lenp);
459static 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
498const void *fdt_getprop_by_offset(const void *fdt, int offset,
499 const char **namep, int *lenp);
500
512const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
513 const char *name, int namelen, int *lenp);
514
542const void *fdt_getprop(const void *fdt, int nodeoffset,
543 const char *name, int *lenp);
544static 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
562uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
563
573const char *fdt_get_alias_namelen(const void *fdt,
574 const char *name, int namelen);
575
588const char *fdt_get_alias(const void *fdt, const char *name);
589
615int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
616
647int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
648 int supernodedepth, int *nodedepth);
649
669int fdt_node_depth(const void *fdt, int nodeoffset);
670
692int fdt_parent_offset(const void *fdt, int nodeoffset);
693
732int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
733 const char *propname,
734 const void *propval, int proplen);
735
755int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
756
779int fdt_node_check_compatible(const void *fdt, int nodeoffset,
780 const char *compatible);
781
816int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
817 const char *compatible);
818
819/**********************************************************************/
820/* Write-in-place functions */
821/**********************************************************************/
822
851int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
852 const void *val, int len);
853
882static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
883 const char *name, uint32_t val)
884{
886 return fdt_setprop_inplace(fdt, nodeoffset, name, &val, sizeof(val));
887}
888
913int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
914
937int fdt_nop_node(void *fdt, int nodeoffset);
938
939/**********************************************************************/
940/* Sequential write functions */
941/**********************************************************************/
942
943int fdt_create(void *buf, int bufsize);
944int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
945int fdt_finish_reservemap(void *fdt);
946int fdt_begin_node(void *fdt, const char *name);
947int fdt_property(void *fdt, const char *name, const void *val, int len);
948static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
949{
951 return fdt_property(fdt, name, &val, sizeof(val));
952}
953#define fdt_property_string(fdt, name, str) \
954 fdt_property(fdt, name, str, strlen(str)+1)
955int fdt_end_node(void *fdt);
956int fdt_finish(void *fdt);
957
958/**********************************************************************/
959/* Read-write functions */
960/**********************************************************************/
961
962int fdt_open_into(const void *fdt, void *buf, int bufsize);
963int fdt_pack(void *fdt);
964
988
1011int fdt_del_mem_rsv(void *fdt, int n);
1012
1037int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1038
1067int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1068 const void *val, int len);
1069
1098static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1099 uint32_t val)
1100{
1101 val = cpu_to_fdt32(val);
1102 return fdt_setprop(fdt, nodeoffset, name, &val, sizeof(val));
1103}
1104
1133#define fdt_setprop_string(fdt, nodeoffset, name, str) \
1134 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1135
1158int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1159
1172int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1173 const char *name, int namelen);
1174
1204int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1205
1227int fdt_del_node(void *fdt, int nodeoffset);
1228
1229/**********************************************************************/
1230/* Debugging / informational functions */
1231/**********************************************************************/
1232
1233const char *fdt_strerror(int errval);
1234
1235#endif /* _LIBFDT_H */
uint32_t address
Definition: ata.h:0
static uint32_t val
Definition: io.h:17
u32 size
Definition: iso9660.c:537
__SIZE_TYPE__ uintptr_t
Definition: lib_types.h:64
int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
Definition: fdt_rw.c:348
int fdt_finish_reservemap(void *fdt)
Definition: fdt_sw.c:133
uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
Definition: fdt_ro.c:323
int fdt_move(const void *fdt, void *buf, int bufsize)
Definition: fdt.c:213
const char * fdt_get_alias_namelen(const void *fdt, const char *name, int namelen)
Definition: fdt_ro.c:340
int fdt_path_offset(const void *fdt, const char *path)
Definition: fdt_ro.c:157
int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name)
Definition: fdt_ro.c:151
int fdt_check_header(const void *fdt)
Definition: fdt.c:58
uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset)
Definition: fdt.c:93
int fdt_parent_offset(const void *fdt, int nodeoffset)
Definition: fdt_ro.c:456
int fdt_node_depth(const void *fdt, int nodeoffset)
Definition: fdt_ro.c:445
const void * fdt_getprop_namelen(const void *fdt, int nodeoffset, const char *name, int namelen, int *lenp)
Definition: fdt_ro.c:292
int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
Definition: fdt_rw.c:172
int fdt_del_mem_rsv(void *fdt, int n)
Definition: fdt_rw.c:189
int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
Definition: fdt_ro.c:91
int fdt_create(void *buf, int bufsize)
Definition: fdt_sw.c:88
int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
Definition: fdt_ro.c:493
const char * fdt_get_alias(const void *fdt, const char *name)
Definition: fdt_ro.c:352
int fdt_delprop(void *fdt, int nodeoffset, const char *name)
Definition: fdt_rw.c:292
int fdt_finish(void *fdt)
Definition: fdt_sw.c:213
int fdt_next_node(const void *fdt, int offset, int *depth)
Definition: fdt.c:161
int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, const void *val, int len)
Definition: fdt_wip.c:58
int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, int supernodedepth, int *nodedepth)
Definition: fdt_ro.c:409
const void * fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen)
Definition: fdt.c:77
int fdt_begin_node(void *fdt, const char *name)
Definition: fdt_sw.c:138
const struct fdt_property * fdt_get_property_namelen(const void *fdt, int nodeoffset, const char *name, int namelen, int *lenp)
Definition: fdt_ro.c:260
int fdt_num_mem_rsv(const void *fdt)
Definition: fdt_ro.c:99
int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
Definition: fdt_ro.c:357
const struct fdt_property * fdt_get_property(const void *fdt, int nodeoffset, const char *name, int *lenp)
Definition: fdt_ro.c:284
int fdt_node_offset_by_compatible(const void *fdt, int startoffset, const char *compatible)
Definition: fdt_ro.c:551
const void * fdt_getprop(const void *fdt, int nodeoffset, const char *name, int *lenp)
Definition: fdt_ro.c:317
int fdt_subnode_offset_namelen(const void *fdt, int parentoffset, const char *name, int namelen)
Definition: fdt_ro.c:132
int fdt_pack(void *fdt)
Definition: fdt_rw.c:453
int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, const char *propname, const void *propval, int proplen)
Definition: fdt_ro.c:466
int fdt_add_subnode_namelen(void *fdt, int parentoffset, const char *name, int namelen)
Definition: fdt_rw.c:307
int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
Definition: fdt_sw.c:110
int fdt_open_into(const void *fdt, void *buf, int bufsize)
Definition: fdt_rw.c:389
const struct fdt_property * fdt_get_property_by_offset(const void *fdt, int offset, int *lenp)
Definition: fdt_ro.c:239
const char * fdt_get_name(const void *fdt, int nodeoffset, int *lenp)
Definition: fdt_ro.c:201
const char * fdt_string(const void *fdt, int stroffset)
Definition: fdt_ro.c:78
int fdt_first_property_offset(const void *fdt, int nodeoffset)
Definition: fdt_ro.c:221
int fdt_nop_node(void *fdt, int nodeoffset)
Definition: fdt_wip.c:107
#define __fdt_set_hdr(name)
Definition: libfdt.h:156
int fdt_end_node(void *fdt)
Definition: fdt_sw.c:154
int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
Definition: fdt_wip.c:83
int fdt_next_property_offset(const void *fdt, int offset)
Definition: fdt_ro.c:231
const void * fdt_getprop_by_offset(const void *fdt, int offset, const char **namep, int *lenp)
Definition: fdt_ro.c:304
int fdt_del_node(void *fdt, int nodeoffset)
Definition: fdt_rw.c:353
int fdt_set_name(void *fdt, int nodeoffset, const char *name)
Definition: fdt_rw.c:251
const char * fdt_strerror(int errval)
Definition: fdt_strerror.c:82
int fdt_node_check_compatible(const void *fdt, int nodeoffset, const char *compatible)
Definition: fdt_ro.c:536
int fdt_setprop(void *fdt, int nodeoffset, const char *name, const void *val, int len)
Definition: fdt_rw.c:274
u32 uint32_t
Definition: libfdt_env.h:11
#define cpu_to_fdt32(x)
Definition: libfdt_env.h:24
u64 uint64_t
Definition: libfdt_env.h:12
u32_t magic(void)
uint32_t len
Definition: fdt.h:37
u8 version
Definition: xenos_edid.h:8