blob: 8b7d501498f88566a7b4fc6b6202f953e04a38e3 [file] [log] [blame]
David Gibson3da0f9a2006-11-27 16:21:28 +11001#ifndef _LIBFDT_H
2#define _LIBFDT_H
3/*
4 * libfdt - Flat Device Tree manipulation
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 *
David Gibson94816052007-06-13 16:30:48 +10007 * libfdt is dual licensed: you can use it either under the terms of
8 * the GPL, or the BSD license, at your option.
David Gibson3da0f9a2006-11-27 16:21:28 +11009 *
David Gibson94816052007-06-13 16:30:48 +100010 * 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.
David Gibson3da0f9a2006-11-27 16:21:28 +110014 *
David Gibson94816052007-06-13 16:30:48 +100015 * 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.
David Gibson3da0f9a2006-11-27 16:21:28 +110052 */
53
David Gibsonede25de2006-12-01 15:02:10 +110054#include <libfdt_env.h>
David Gibson6f8b7712007-10-16 13:50:34 +100055#include <fdt.h>
David Gibson3da0f9a2006-11-27 16:21:28 +110056
57#define FDT_FIRST_SUPPORTED_VERSION 0x10
David Gibsonfe92f6b2006-12-01 16:25:39 +110058#define FDT_LAST_SUPPORTED_VERSION 0x11
David Gibson3da0f9a2006-11-27 16:21:28 +110059
David Gibson3aea8282006-12-15 15:12:52 +110060/* Error codes: informative error codes */
61#define FDT_ERR_NOTFOUND 1
62#define FDT_ERR_EXISTS 2
63#define FDT_ERR_NOSPACE 3
David Gibson3da0f9a2006-11-27 16:21:28 +110064
David Gibson3aea8282006-12-15 15:12:52 +110065/* Error codes: codes for bad parameters */
66#define FDT_ERR_BADOFFSET 4
67#define FDT_ERR_BADPATH 5
68#define FDT_ERR_BADSTATE 6
69
70/* Error codes: codes for bad device tree blobs */
71#define FDT_ERR_TRUNCATED 7
72#define FDT_ERR_BADMAGIC 8
73#define FDT_ERR_BADVERSION 9
74#define FDT_ERR_BADSTRUCTURE 10
75#define FDT_ERR_BADLAYOUT 11
76
David Gibson12482372007-08-30 14:54:04 +100077/* "Can't happen" error indicating a bug in libfdt */
78#define FDT_ERR_INTERNAL 12
79
80#define FDT_ERR_MAX 12
David Gibson3da0f9a2006-11-27 16:21:28 +110081
David Gibson73d60922006-12-15 15:12:47 +110082#define fdt_get_header(fdt, field) \
David Gibsona6c76f92007-06-13 14:18:10 +100083 (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
David Gibson73d60922006-12-15 15:12:47 +110084#define fdt_magic(fdt) (fdt_get_header(fdt, magic))
85#define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
86#define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
87#define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
88#define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
89#define fdt_version(fdt) (fdt_get_header(fdt, version))
90#define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
91#define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
92#define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
93#define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
David Gibsonede25de2006-12-01 15:02:10 +110094
David Gibson73d60922006-12-15 15:12:47 +110095#define fdt_set_header(fdt, field, val) \
96 ((struct fdt_header *)(fdt))->field = cpu_to_fdt32(val)
97
David Gibsona6c76f92007-06-13 14:18:10 +100098const void *fdt_offset_ptr(const void *fdt, int offset, int checklen);
99static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
100{
101 return (void *)fdt_offset_ptr(fdt, offset, checklen);
102}
103
David Gibson3da0f9a2006-11-27 16:21:28 +1100104
105#define fdt_offset_ptr_typed(fdt, offset, var) \
106 ((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var)))))
David Gibsona6c76f92007-06-13 14:18:10 +1000107#define fdt_offset_ptr_typed_w(fdt, offset, var) \
108 ((typeof(var))(fdt_offset_ptr_w((fdt), (offset), sizeof(*(var)))))
David Gibson3da0f9a2006-11-27 16:21:28 +1100109
David Gibson73d60922006-12-15 15:12:47 +1100110int fdt_move(const void *fdt, void *buf, int bufsize);
David Gibson42369762006-12-01 15:07:19 +1100111
David Gibson3da0f9a2006-11-27 16:21:28 +1100112/* Read-only functions */
David Gibson73d60922006-12-15 15:12:47 +1100113char *fdt_string(const void *fdt, int stroffset);
David Gibson3aa4cfd2006-11-29 16:34:30 +1100114
David Gibsonfd1bf3a2007-10-10 17:12:12 +1000115int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
116int fdt_num_mem_rsv(const void *fdt);
117
David Gibson73d60922006-12-15 15:12:47 +1100118int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
David Gibson3da0f9a2006-11-27 16:21:28 +1100119 const char *name, int namelen);
David Gibson73d60922006-12-15 15:12:47 +1100120int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
David Gibson3da0f9a2006-11-27 16:21:28 +1100121
David Gibson73d60922006-12-15 15:12:47 +1100122int fdt_path_offset(const void *fdt, const char *path);
David Gibson3da0f9a2006-11-27 16:21:28 +1100123
David Gibson9d26eab2007-08-30 14:54:04 +1000124const char *fdt_get_name(const void *fdt, int nodeoffset, int *baselen);
125
David Gibsona6c76f92007-06-13 14:18:10 +1000126const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
127 const char *name, int *lenp);
128
129static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
130 const char *name,
131 int *lenp)
132{
133 return (struct fdt_property *)fdt_get_property(fdt, nodeoffset,
134 name, lenp);
135}
136
137const void *fdt_getprop(const void *fdt, int nodeoffset,
138 const char *name, int *lenp);
139static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
140 const char *name, int *lenp)
141{
142 return (void *)fdt_getprop(fdt, nodeoffset, name, lenp);
143}
David Gibson3da0f9a2006-11-27 16:21:28 +1100144
David Gibson037db262007-08-30 14:54:04 +1000145int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
146
David Gibson12482372007-08-30 14:54:04 +1000147int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
148 int supernodedepth, int *nodedepth);
149int fdt_node_depth(const void *fdt, int nodeoffset);
150int fdt_parent_offset(const void *fdt, int nodeoffset);
151
David Gibsonae1454b2007-09-17 14:28:34 +1000152int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
153 const char *propname,
154 const void *propval, int proplen);
155
David Gibson3da0f9a2006-11-27 16:21:28 +1100156/* Write-in-place functions */
David Gibson73d60922006-12-15 15:12:47 +1100157int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
David Gibson3da0f9a2006-11-27 16:21:28 +1100158 const void *val, int len);
159
160#define fdt_setprop_inplace_typed(fdt, nodeoffset, name, val) \
161 ({ \
162 typeof(val) x = val; \
163 fdt_setprop_inplace(fdt, nodeoffset, name, &x, sizeof(x)); \
164 })
165
David Gibson73d60922006-12-15 15:12:47 +1100166int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
167int fdt_nop_node(void *fdt, int nodeoffset);
David Gibson3da0f9a2006-11-27 16:21:28 +1100168
David Gibson3da0f9a2006-11-27 16:21:28 +1100169/* Sequential-write functions */
David Gibson73d60922006-12-15 15:12:47 +1100170int fdt_create(void *buf, int bufsize);
171int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
172int fdt_finish_reservemap(void *fdt);
173int fdt_begin_node(void *fdt, const char *name);
174int fdt_property(void *fdt, const char *name, const void *val, int len);
David Gibson063693a2006-11-29 16:45:46 +1100175#define fdt_property_typed(fdt, name, val) \
176 ({ \
177 typeof(val) x = (val); \
178 fdt_property((fdt), (name), &x, sizeof(x)); \
179 })
180#define fdt_property_string(fdt, name, str) \
181 fdt_property(fdt, name, str, strlen(str)+1)
David Gibson73d60922006-12-15 15:12:47 +1100182int fdt_end_node(void *fdt);
183int fdt_finish(void *fdt);
David Gibson3da0f9a2006-11-27 16:21:28 +1100184
David Gibson3da0f9a2006-11-27 16:21:28 +1100185/* Read-write functions */
David Gibson73d60922006-12-15 15:12:47 +1100186int fdt_open_into(void *fdt, void *buf, int bufsize);
187int fdt_pack(void *fdt);
David Gibson7ba551f2006-12-01 16:59:43 +1100188
David Gibsonfd1bf3a2007-10-10 17:12:12 +1000189int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
190int fdt_del_mem_rsv(void *fdt, int n);
191
David Gibson73d60922006-12-15 15:12:47 +1100192int fdt_setprop(void *fdt, int nodeoffset, const char *name,
David Gibson7ba551f2006-12-01 16:59:43 +1100193 const void *val, int len);
194#define fdt_setprop_typed(fdt, nodeoffset, name, val) \
195 ({ \
196 typeof(val) x = (val); \
197 fdt_setprop((fdt), (nodeoffset), (name), &x, sizeof(x)); \
198 })
199#define fdt_setprop_string(fdt, nodeoffset, name, str) \
200 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
David Gibson73d60922006-12-15 15:12:47 +1100201int fdt_delprop(void *fdt, int nodeoffset, const char *name);
202int fdt_add_subnode_namelen(void *fdt, int parentoffset,
David Gibson7ba551f2006-12-01 16:59:43 +1100203 const char *name, int namelen);
David Gibson73d60922006-12-15 15:12:47 +1100204int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
205int fdt_del_node(void *fdt, int nodeoffset);
David Gibson3da0f9a2006-11-27 16:21:28 +1100206
David Gibson5b344f92006-12-21 09:57:08 +1100207/* Extra functions */
208const char *fdt_strerror(int errval);
209
David Gibson3da0f9a2006-11-27 16:21:28 +1100210#endif /* _LIBFDT_H */