blob: a0b4d55034155e5e99ff5013c65040bbd7c42b3e [file] [log] [blame]
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001#ifndef _LIBFDT_H
2#define _LIBFDT_H
3/*
4 * libfdt - Flat Device Tree manipulation
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <fdt.h>
23#include <libfdt_env.h>
24
25#define FDT_FIRST_SUPPORTED_VERSION 0x10
26#define FDT_LAST_SUPPORTED_VERSION 0x11
27
28/* Error codes: informative error codes */
29#define FDT_ERR_NOTFOUND 1
30#define FDT_ERR_EXISTS 2
31#define FDT_ERR_NOSPACE 3
32
33/* Error codes: codes for bad parameters */
34#define FDT_ERR_BADOFFSET 4
35#define FDT_ERR_BADPATH 5
36#define FDT_ERR_BADSTATE 6
37
38/* Error codes: codes for bad device tree blobs */
39#define FDT_ERR_TRUNCATED 7
40#define FDT_ERR_BADMAGIC 8
41#define FDT_ERR_BADVERSION 9
42#define FDT_ERR_BADSTRUCTURE 10
43#define FDT_ERR_BADLAYOUT 11
44
45#define FDT_ERR_MAX 11
46
47#define fdt_get_header(fdt, field) \
48 (fdt32_to_cpu(((struct fdt_header *)(fdt))->field))
Wolfgang Denk94abd7c2007-04-04 01:49:15 +020049#define fdt_magic(fdt) (fdt_get_header(fdt, magic))
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040050#define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
51#define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
52#define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
53#define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
54#define fdt_version(fdt) (fdt_get_header(fdt, version))
Wolfgang Denk94abd7c2007-04-04 01:49:15 +020055#define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
56#define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
57#define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040058#define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
59
60#define fdt_set_header(fdt, field, val) \
61 ((struct fdt_header *)(fdt))->field = cpu_to_fdt32(val)
62
63void *fdt_offset_ptr(const void *fdt, int offset, int checklen);
64
65#define fdt_offset_ptr_typed(fdt, offset, var) \
66 ((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var)))))
67
68int fdt_move(const void *fdt, void *buf, int bufsize);
69
70/* Read-only functions */
71char *fdt_string(const void *fdt, int stroffset);
72
73int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
74 const char *name, int namelen);
75int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
76
77int fdt_path_offset(const void *fdt, const char *path);
78
79struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
80 const char *name, int *lenp);
81void *fdt_getprop(const void *fdt, int nodeoffset,
82 const char *name, int *lenp);
83
Gerald Van Baren3af0d582007-03-31 12:13:43 -040084uint32_t fdt_next_tag(const void *fdt, int offset,
Wolfgang Denk94abd7c2007-04-04 01:49:15 +020085 int *nextoffset, char **namep);
Gerald Van Baren3af0d582007-03-31 12:13:43 -040086
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040087/* Write-in-place functions */
88int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
89 const void *val, int len);
90
91#define fdt_setprop_inplace_typed(fdt, nodeoffset, name, val) \
92 ({ \
93 typeof(val) x = val; \
94 fdt_setprop_inplace(fdt, nodeoffset, name, &x, sizeof(x)); \
95 })
96
97int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
98int fdt_nop_node(void *fdt, int nodeoffset);
99
100/* Sequential-write functions */
101int fdt_create(void *buf, int bufsize);
102int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
103int fdt_finish_reservemap(void *fdt);
104int fdt_begin_node(void *fdt, const char *name);
105int fdt_property(void *fdt, const char *name, const void *val, int len);
106#define fdt_property_typed(fdt, name, val) \
107 ({ \
108 typeof(val) x = (val); \
109 fdt_property((fdt), (name), &x, sizeof(x)); \
110 })
111#define fdt_property_string(fdt, name, str) \
112 fdt_property(fdt, name, str, strlen(str)+1)
113int fdt_end_node(void *fdt);
114int fdt_finish(void *fdt);
115
116/* Read-write functions */
117int fdt_open_into(void *fdt, void *buf, int bufsize);
118int fdt_pack(void *fdt);
119
120int fdt_setprop(void *fdt, int nodeoffset, const char *name,
121 const void *val, int len);
122#define fdt_setprop_typed(fdt, nodeoffset, name, val) \
123 ({ \
124 typeof(val) x = (val); \
125 fdt_setprop((fdt), (nodeoffset), (name), &x, sizeof(x)); \
126 })
127#define fdt_setprop_string(fdt, nodeoffset, name, str) \
128 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
129int fdt_delprop(void *fdt, int nodeoffset, const char *name);
130int fdt_add_subnode_namelen(void *fdt, int parentoffset,
131 const char *name, int namelen);
132int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
133int fdt_del_node(void *fdt, int nodeoffset);
134
135/* Extra functions */
136const char *fdt_strerror(int errval);
137
138#endif /* _LIBFDT_H */