blob: ed834bdedbc71144511e52b7ffaa831f8c1990f7 [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 *
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>
David Gibsonede25de2006-12-01 15:02:10 +110023#include <libfdt_env.h>
David Gibson3da0f9a2006-11-27 16:21:28 +110024
25#define FDT_FIRST_SUPPORTED_VERSION 0x10
David Gibsonfe92f6b2006-12-01 16:25:39 +110026#define FDT_LAST_SUPPORTED_VERSION 0x11
David Gibson3da0f9a2006-11-27 16:21:28 +110027
28/* Errors */
29#define FDT_ERR_OK 0
30#define FDT_ERR_BADMAGIC 1
31#define FDT_ERR_BADVERSION 2
32#define FDT_ERR_BADPOINTER 3
33#define FDT_ERR_BADHEADER 4
34#define FDT_ERR_BADSTRUCTURE 5
35#define FDT_ERR_BADOFFSET 6
36#define FDT_ERR_NOTFOUND 7
37#define FDT_ERR_BADPATH 8
38#define FDT_ERR_TRUNCATED 9
39#define FDT_ERR_NOSPACE 10
40#define FDT_ERR_BADSTATE 11
41#define FDT_ERR_SIZE_MISMATCH 12
42#define FDT_ERR_INTERNAL 13
David Gibson7ba551f2006-12-01 16:59:43 +110043#define FDT_ERR_BADLAYOUT 14
44#define FDT_ERR_EXISTS 15
David Gibson3da0f9a2006-11-27 16:21:28 +110045
David Gibson7ba551f2006-12-01 16:59:43 +110046#define FDT_ERR_MAX 14
David Gibson3da0f9a2006-11-27 16:21:28 +110047
David Gibson73d60922006-12-15 15:12:47 +110048#define fdt_get_header(fdt, field) \
49 (fdt32_to_cpu(((struct fdt_header *)(fdt))->field))
50#define fdt_magic(fdt) (fdt_get_header(fdt, magic))
51#define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
52#define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
53#define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
54#define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
55#define fdt_version(fdt) (fdt_get_header(fdt, version))
56#define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
57#define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
58#define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
59#define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
David Gibsonede25de2006-12-01 15:02:10 +110060
David Gibson73d60922006-12-15 15:12:47 +110061#define fdt_set_header(fdt, field, val) \
62 ((struct fdt_header *)(fdt))->field = cpu_to_fdt32(val)
63
64void *fdt_offset_ptr(const void *fdt, int offset, int checklen);
David Gibson3da0f9a2006-11-27 16:21:28 +110065
66#define fdt_offset_ptr_typed(fdt, offset, var) \
67 ((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var)))))
68
69#define fdt_offset_error(offset) \
70 ( (offset) < 0 ? -(offset) : 0 )
71
72#define fdt_ptr_error(ptr) \
73 ( (((long)(ptr) < 0) && ((long)(ptr) >= -FDT_ERR_MAX)) ? -(long)(ptr) : 0 )
74
David Gibson73d60922006-12-15 15:12:47 +110075int fdt_move(const void *fdt, void *buf, int bufsize);
David Gibson42369762006-12-01 15:07:19 +110076
David Gibson3da0f9a2006-11-27 16:21:28 +110077/* Read-only functions */
David Gibson73d60922006-12-15 15:12:47 +110078char *fdt_string(const void *fdt, int stroffset);
David Gibson3aa4cfd2006-11-29 16:34:30 +110079
David Gibson73d60922006-12-15 15:12:47 +110080int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
David Gibson3da0f9a2006-11-27 16:21:28 +110081 const char *name, int namelen);
David Gibson73d60922006-12-15 15:12:47 +110082int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
David Gibson3da0f9a2006-11-27 16:21:28 +110083
David Gibson73d60922006-12-15 15:12:47 +110084int fdt_path_offset(const void *fdt, const char *path);
David Gibson3da0f9a2006-11-27 16:21:28 +110085
David Gibson73d60922006-12-15 15:12:47 +110086struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
David Gibson94993f42006-12-11 16:15:34 +110087 const char *name, int *lenp);
David Gibson73d60922006-12-15 15:12:47 +110088void *fdt_getprop(const void *fdt, int nodeoffset,
David Gibson3da0f9a2006-11-27 16:21:28 +110089 const char *name, int *lenp);
90
91/* Write-in-place functions */
David Gibson73d60922006-12-15 15:12:47 +110092int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
David Gibson3da0f9a2006-11-27 16:21:28 +110093 const void *val, int len);
94
95#define fdt_setprop_inplace_typed(fdt, nodeoffset, name, val) \
96 ({ \
97 typeof(val) x = val; \
98 fdt_setprop_inplace(fdt, nodeoffset, name, &x, sizeof(x)); \
99 })
100
David Gibson73d60922006-12-15 15:12:47 +1100101int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
102int fdt_nop_node(void *fdt, int nodeoffset);
David Gibson3da0f9a2006-11-27 16:21:28 +1100103
David Gibson3da0f9a2006-11-27 16:21:28 +1100104/* Sequential-write functions */
David Gibson73d60922006-12-15 15:12:47 +1100105int fdt_create(void *buf, int bufsize);
106int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
107int fdt_finish_reservemap(void *fdt);
108int fdt_begin_node(void *fdt, const char *name);
109int fdt_property(void *fdt, const char *name, const void *val, int len);
David Gibson063693a2006-11-29 16:45:46 +1100110#define fdt_property_typed(fdt, name, val) \
111 ({ \
112 typeof(val) x = (val); \
113 fdt_property((fdt), (name), &x, sizeof(x)); \
114 })
115#define fdt_property_string(fdt, name, str) \
116 fdt_property(fdt, name, str, strlen(str)+1)
David Gibson73d60922006-12-15 15:12:47 +1100117int fdt_end_node(void *fdt);
118int fdt_finish(void *fdt);
David Gibson3da0f9a2006-11-27 16:21:28 +1100119
David Gibson3da0f9a2006-11-27 16:21:28 +1100120/* Read-write functions */
David Gibson73d60922006-12-15 15:12:47 +1100121int fdt_open_into(void *fdt, void *buf, int bufsize);
122int fdt_pack(void *fdt);
David Gibson7ba551f2006-12-01 16:59:43 +1100123
David Gibson73d60922006-12-15 15:12:47 +1100124int fdt_setprop(void *fdt, int nodeoffset, const char *name,
David Gibson7ba551f2006-12-01 16:59:43 +1100125 const void *val, int len);
126#define fdt_setprop_typed(fdt, nodeoffset, name, val) \
127 ({ \
128 typeof(val) x = (val); \
129 fdt_setprop((fdt), (nodeoffset), (name), &x, sizeof(x)); \
130 })
131#define fdt_setprop_string(fdt, nodeoffset, name, str) \
132 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
David Gibson73d60922006-12-15 15:12:47 +1100133int fdt_delprop(void *fdt, int nodeoffset, const char *name);
134int fdt_add_subnode_namelen(void *fdt, int parentoffset,
David Gibson7ba551f2006-12-01 16:59:43 +1100135 const char *name, int namelen);
David Gibson73d60922006-12-15 15:12:47 +1100136int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
137int fdt_del_node(void *fdt, int nodeoffset);
David Gibson3da0f9a2006-11-27 16:21:28 +1100138
139#endif /* _LIBFDT_H */