David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 1 | #ifndef _LIBFDT_H |
| 2 | #define _LIBFDT_H |
| 3 | /* |
| 4 | * libfdt - Flat Device Tree manipulation |
| 5 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
| 6 | * |
David Gibson | 9481605 | 2007-06-13 16:30:48 +1000 | [diff] [blame] | 7 | * libfdt is dual licensed: you can use it either under the terms of |
| 8 | * the GPL, or the BSD license, at your option. |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 9 | * |
David Gibson | 9481605 | 2007-06-13 16:30:48 +1000 | [diff] [blame] | 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. |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 14 | * |
David Gibson | 9481605 | 2007-06-13 16:30:48 +1000 | [diff] [blame] | 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. |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 52 | */ |
| 53 | |
David Gibson | ede25de | 2006-12-01 15:02:10 +1100 | [diff] [blame] | 54 | #include <libfdt_env.h> |
David Gibson | 6f8b771 | 2007-10-16 13:50:34 +1000 | [diff] [blame^] | 55 | #include <fdt.h> |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 56 | |
| 57 | #define FDT_FIRST_SUPPORTED_VERSION 0x10 |
David Gibson | fe92f6b | 2006-12-01 16:25:39 +1100 | [diff] [blame] | 58 | #define FDT_LAST_SUPPORTED_VERSION 0x11 |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 59 | |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 60 | /* Error codes: informative error codes */ |
| 61 | #define FDT_ERR_NOTFOUND 1 |
| 62 | #define FDT_ERR_EXISTS 2 |
| 63 | #define FDT_ERR_NOSPACE 3 |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 64 | |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 65 | /* 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 Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 77 | /* "Can't happen" error indicating a bug in libfdt */ |
| 78 | #define FDT_ERR_INTERNAL 12 |
| 79 | |
| 80 | #define FDT_ERR_MAX 12 |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 81 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 82 | #define fdt_get_header(fdt, field) \ |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame] | 83 | (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field)) |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 84 | #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 Gibson | ede25de | 2006-12-01 15:02:10 +1100 | [diff] [blame] | 94 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 95 | #define fdt_set_header(fdt, field, val) \ |
| 96 | ((struct fdt_header *)(fdt))->field = cpu_to_fdt32(val) |
| 97 | |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame] | 98 | const void *fdt_offset_ptr(const void *fdt, int offset, int checklen); |
| 99 | static 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 Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 104 | |
| 105 | #define fdt_offset_ptr_typed(fdt, offset, var) \ |
| 106 | ((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var))))) |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame] | 107 | #define fdt_offset_ptr_typed_w(fdt, offset, var) \ |
| 108 | ((typeof(var))(fdt_offset_ptr_w((fdt), (offset), sizeof(*(var))))) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 109 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 110 | int fdt_move(const void *fdt, void *buf, int bufsize); |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 111 | |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 112 | /* Read-only functions */ |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 113 | char *fdt_string(const void *fdt, int stroffset); |
David Gibson | 3aa4cfd | 2006-11-29 16:34:30 +1100 | [diff] [blame] | 114 | |
David Gibson | fd1bf3a | 2007-10-10 17:12:12 +1000 | [diff] [blame] | 115 | int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size); |
| 116 | int fdt_num_mem_rsv(const void *fdt); |
| 117 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 118 | int fdt_subnode_offset_namelen(const void *fdt, int parentoffset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 119 | const char *name, int namelen); |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 120 | int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 121 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 122 | int fdt_path_offset(const void *fdt, const char *path); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 123 | |
David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 124 | const char *fdt_get_name(const void *fdt, int nodeoffset, int *baselen); |
| 125 | |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame] | 126 | const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset, |
| 127 | const char *name, int *lenp); |
| 128 | |
| 129 | static 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 | |
| 137 | const void *fdt_getprop(const void *fdt, int nodeoffset, |
| 138 | const char *name, int *lenp); |
| 139 | static 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 Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 144 | |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 145 | int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen); |
| 146 | |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 147 | int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, |
| 148 | int supernodedepth, int *nodedepth); |
| 149 | int fdt_node_depth(const void *fdt, int nodeoffset); |
| 150 | int fdt_parent_offset(const void *fdt, int nodeoffset); |
| 151 | |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 152 | int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, |
| 153 | const char *propname, |
| 154 | const void *propval, int proplen); |
| 155 | |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 156 | /* Write-in-place functions */ |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 157 | int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 158 | 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 Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 166 | int fdt_nop_property(void *fdt, int nodeoffset, const char *name); |
| 167 | int fdt_nop_node(void *fdt, int nodeoffset); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 168 | |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 169 | /* Sequential-write functions */ |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 170 | int fdt_create(void *buf, int bufsize); |
| 171 | int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size); |
| 172 | int fdt_finish_reservemap(void *fdt); |
| 173 | int fdt_begin_node(void *fdt, const char *name); |
| 174 | int fdt_property(void *fdt, const char *name, const void *val, int len); |
David Gibson | 063693a | 2006-11-29 16:45:46 +1100 | [diff] [blame] | 175 | #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 Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 182 | int fdt_end_node(void *fdt); |
| 183 | int fdt_finish(void *fdt); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 184 | |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 185 | /* Read-write functions */ |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 186 | int fdt_open_into(void *fdt, void *buf, int bufsize); |
| 187 | int fdt_pack(void *fdt); |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 188 | |
David Gibson | fd1bf3a | 2007-10-10 17:12:12 +1000 | [diff] [blame] | 189 | int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size); |
| 190 | int fdt_del_mem_rsv(void *fdt, int n); |
| 191 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 192 | int fdt_setprop(void *fdt, int nodeoffset, const char *name, |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 193 | 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 Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 201 | int fdt_delprop(void *fdt, int nodeoffset, const char *name); |
| 202 | int fdt_add_subnode_namelen(void *fdt, int parentoffset, |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 203 | const char *name, int namelen); |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 204 | int fdt_add_subnode(void *fdt, int parentoffset, const char *name); |
| 205 | int fdt_del_node(void *fdt, int nodeoffset); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 206 | |
David Gibson | 5b344f9 | 2006-12-21 09:57:08 +1100 | [diff] [blame] | 207 | /* Extra functions */ |
| 208 | const char *fdt_strerror(int errval); |
| 209 | |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 210 | #endif /* _LIBFDT_H */ |