blob: b58ae0fdebf7ac3022ba4b3d9d27886aedbfc8ce [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>
23
24#define FDT_FIRST_SUPPORTED_VERSION 0x10
25#define FDT_LAST_SUPPORTED_VERSION 0x10
26
27/* Errors */
28#define FDT_ERR_OK 0
29#define FDT_ERR_BADMAGIC 1
30#define FDT_ERR_BADVERSION 2
31#define FDT_ERR_BADPOINTER 3
32#define FDT_ERR_BADHEADER 4
33#define FDT_ERR_BADSTRUCTURE 5
34#define FDT_ERR_BADOFFSET 6
35#define FDT_ERR_NOTFOUND 7
36#define FDT_ERR_BADPATH 8
37#define FDT_ERR_TRUNCATED 9
38#define FDT_ERR_NOSPACE 10
39#define FDT_ERR_BADSTATE 11
40#define FDT_ERR_SIZE_MISMATCH 12
41#define FDT_ERR_INTERNAL 13
42
43#define FDT_ERR_MAX 13
44
45/* Offset handling functions */
46void *fdt_offset_ptr(const struct fdt_header *fdt, int offset, int checklen);
47
48#define fdt_offset_ptr_typed(fdt, offset, var) \
49 ((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var)))))
50
51#define fdt_offset_error(offset) \
52 ( (offset) < 0 ? -(offset) : 0 )
53
54#define fdt_ptr_error(ptr) \
55 ( (((long)(ptr) < 0) && ((long)(ptr) >= -FDT_ERR_MAX)) ? -(long)(ptr) : 0 )
56
David Gibson3da0f9a2006-11-27 16:21:28 +110057/* Read-only functions */
David Gibson3aa4cfd2006-11-29 16:34:30 +110058char *fdt_string(const struct fdt_header *fdt, int stroffset);
59
David Gibson3da0f9a2006-11-27 16:21:28 +110060int fdt_property_offset(const struct fdt_header *fdt, int nodeoffset,
61 const char *name);
62int fdt_subnode_offset_namelen(const struct fdt_header *fdt, int parentoffset,
63 const char *name, int namelen);
64int fdt_subnode_offset(const struct fdt_header *fdt, int parentoffset,
65 const char *name);
66
67int fdt_path_offset(const struct fdt_header *fdt, const char *path);
68
69void *fdt_getprop(const struct fdt_header *fdt, int nodeoffset,
70 const char *name, int *lenp);
71
72/* Write-in-place functions */
73int fdt_setprop_inplace(struct fdt_header *fdt, int nodeoffset, const char *name,
74 const void *val, int len);
75
76#define fdt_setprop_inplace_typed(fdt, nodeoffset, name, val) \
77 ({ \
78 typeof(val) x = val; \
79 fdt_setprop_inplace(fdt, nodeoffset, name, &x, sizeof(x)); \
80 })
81
82int fdt_nop_property(struct fdt_header *fdt, int nodeoffset, const char *name);
83int fdt_nop_node(struct fdt_header *fdt, int nodeoffset);
84
David Gibson3da0f9a2006-11-27 16:21:28 +110085/* Sequential-write functions */
86struct fdt_header *fdt_create(void *buf, int bufsize);
87int fdt_add_reservemap_entry(struct fdt_header *fdt, uint64_t addr, uint64_t size);
David Gibson063693a2006-11-29 16:45:46 +110088int fdt_finish_reservemap(struct fdt_header *fdt);
David Gibson3da0f9a2006-11-27 16:21:28 +110089int fdt_begin_node(struct fdt_header *fdt, const char *name);
90int fdt_property(struct fdt_header *fdt, const char *name, const void *val, int len);
David Gibson063693a2006-11-29 16:45:46 +110091#define fdt_property_typed(fdt, name, val) \
92 ({ \
93 typeof(val) x = (val); \
94 fdt_property((fdt), (name), &x, sizeof(x)); \
95 })
96#define fdt_property_string(fdt, name, str) \
97 fdt_property(fdt, name, str, strlen(str)+1)
David Gibson3da0f9a2006-11-27 16:21:28 +110098int fdt_end_node(struct fdt_header *fdt);
David Gibson063693a2006-11-29 16:45:46 +110099int fdt_finish(struct fdt_header *fdt);
David Gibson3da0f9a2006-11-27 16:21:28 +1100100
David Gibson063693a2006-11-29 16:45:46 +1100101#if 0
David Gibson3da0f9a2006-11-27 16:21:28 +1100102/* Read-write functions */
103struct fdt_header *fdt_open(struct fdt_header *fdt, int bufsize);
104int fdt_add_subnode(struct fdt_header *fdtx, void *node, const char *name);
105int fdt_set_property(struct fdt_header *fdtx, void *node, const char *name,
106 const void *val, int len);
107int fdt_del_property(struct fdt_header *fdtx, void *node, const char *name);
108
109/* Misc functions */
110struct fdt_header *fdt_move(struct fdt_header *fdt, void *buf, int bufsize);
111#endif
112
113#endif /* _LIBFDT_H */