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 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 62 | /* FDT_ERR_NOTFOUND: The requested node or property does not exist */ |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 63 | #define FDT_ERR_EXISTS 2 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 64 | /* FDT_ERR_EXISTS: Attemped to create a node or property which |
| 65 | * already exists */ |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 66 | #define FDT_ERR_NOSPACE 3 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 67 | /* FDT_ERR_NOSPACE: Operation needed to expand the device |
| 68 | * tree, but its buffer did not have sufficient space to |
| 69 | * contain the expanded tree. Use fdt_open_into() to move the |
| 70 | * device tree to a buffer with more space. */ |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 71 | |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 72 | /* Error codes: codes for bad parameters */ |
| 73 | #define FDT_ERR_BADOFFSET 4 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 74 | /* FDT_ERR_BADOFFSET: Function was passed a structure block |
| 75 | * offset which is out-of-bounds, or which points to an |
| 76 | * unsuitable part of the structure for the operation. */ |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 77 | #define FDT_ERR_BADPATH 5 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 78 | /* FDT_ERR_BADPATH: Function was passed a badly formatted path |
| 79 | * (e.g. missing a leading / for a function which requires an |
| 80 | * absolute path) */ |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 81 | #define FDT_ERR_BADSTATE 6 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 82 | /* FDT_ERR_BADSTATE: Function was passed an incomplete device |
| 83 | * tree created by the sequential-write functions, which is |
| 84 | * not sufficiently complete for the requested operation. */ |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 85 | |
| 86 | /* Error codes: codes for bad device tree blobs */ |
| 87 | #define FDT_ERR_TRUNCATED 7 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 88 | /* FDT_ERR_TRUNCATED: Structure block of the given device tree |
| 89 | * ends without an FDT_END tag. */ |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 90 | #define FDT_ERR_BADMAGIC 8 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 91 | /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a |
| 92 | * device tree at all - it is missing the flattened device |
| 93 | * tree magic number. */ |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 94 | #define FDT_ERR_BADVERSION 9 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 95 | /* FDT_ERR_BADVERSION: Given device tree has a version which |
| 96 | * can't be handled by the requested operation. For |
| 97 | * read-write functions, this may mean that fdt_open_into() is |
| 98 | * required to convert the tree to the expected version. */ |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 99 | #define FDT_ERR_BADSTRUCTURE 10 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 100 | /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt |
| 101 | * structure block or other serious error (e.g. misnested |
| 102 | * nodes, or subnodes preceding properties). */ |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 103 | #define FDT_ERR_BADLAYOUT 11 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 104 | /* FDT_ERR_BADLAYOUT: For read-write functions, the given |
| 105 | * device tree has it's sub-blocks in an order that the |
| 106 | * function can't handle (memory reserve map, then structure, |
| 107 | * then strings). Use fdt_open_into() to reorganize the tree |
| 108 | * into a form suitable for the read-write operations. */ |
David Gibson | 3aea828 | 2006-12-15 15:12:52 +1100 | [diff] [blame] | 109 | |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 110 | /* "Can't happen" error indicating a bug in libfdt */ |
| 111 | #define FDT_ERR_INTERNAL 12 |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 112 | /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion. |
| 113 | * Indicates a bug in libfdt itself. */ |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 114 | |
| 115 | #define FDT_ERR_MAX 12 |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 116 | |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 117 | /**********************************************************************/ |
| 118 | /* Low-level functions (you probably don't need these) */ |
| 119 | /**********************************************************************/ |
David Gibson | 96b5fad | 2007-10-24 10:28:52 +1000 | [diff] [blame] | 120 | |
| 121 | const void *fdt_offset_ptr(const void *fdt, int offset, int checklen); |
| 122 | static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) |
| 123 | { |
| 124 | return (void *)fdt_offset_ptr(fdt, offset, checklen); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | #define fdt_offset_ptr_typed(fdt, offset, var) \ |
| 129 | ((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var))))) |
| 130 | #define fdt_offset_ptr_typed_w(fdt, offset, var) \ |
| 131 | ((typeof(var))(fdt_offset_ptr_w((fdt), (offset), sizeof(*(var))))) |
| 132 | |
David Gibson | 3c44c87 | 2007-10-24 11:06:09 +1000 | [diff] [blame] | 133 | uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset); |
David Gibson | 96b5fad | 2007-10-24 10:28:52 +1000 | [diff] [blame] | 134 | |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 135 | /**********************************************************************/ |
| 136 | /* General functions */ |
| 137 | /**********************************************************************/ |
| 138 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 139 | #define fdt_get_header(fdt, field) \ |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame] | 140 | (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field)) |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 141 | #define fdt_magic(fdt) (fdt_get_header(fdt, magic)) |
| 142 | #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize)) |
| 143 | #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct)) |
| 144 | #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings)) |
| 145 | #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap)) |
| 146 | #define fdt_version(fdt) (fdt_get_header(fdt, version)) |
| 147 | #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version)) |
| 148 | #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys)) |
| 149 | #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings)) |
| 150 | #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] | 151 | |
David Gibson | 9b91134 | 2007-10-25 14:29:38 +1000 | [diff] [blame] | 152 | #define __fdt_set_hdr(name) \ |
| 153 | static inline void fdt_set_##name(void *fdt, uint32_t val) \ |
| 154 | { \ |
| 155 | struct fdt_header *fdth = fdt; \ |
| 156 | fdth->name = cpu_to_fdt32(val); \ |
| 157 | } |
| 158 | __fdt_set_hdr(magic); |
| 159 | __fdt_set_hdr(totalsize); |
| 160 | __fdt_set_hdr(off_dt_struct); |
| 161 | __fdt_set_hdr(off_dt_strings); |
| 162 | __fdt_set_hdr(off_mem_rsvmap); |
| 163 | __fdt_set_hdr(version); |
| 164 | __fdt_set_hdr(last_comp_version); |
| 165 | __fdt_set_hdr(boot_cpuid_phys); |
| 166 | __fdt_set_hdr(size_dt_strings); |
| 167 | __fdt_set_hdr(size_dt_struct); |
| 168 | #undef __fdt_set_hdr |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 169 | |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 170 | /** |
| 171 | * fdt_check_header - sanity check a device tree or possible device tree |
| 172 | * @fdt: pointer to data which might be a flattened device tree |
| 173 | * |
| 174 | * fdt_check_header() checks that the given buffer contains what |
| 175 | * appears to be a flattened device tree with sane information in its |
| 176 | * header. |
| 177 | * |
| 178 | * returns: |
| 179 | * 0, if the buffer appears to contain a valid device tree |
| 180 | * -FDT_ERR_BADMAGIC, |
| 181 | * -FDT_ERR_BADVERSION, |
| 182 | * -FDT_ERR_BADSTATE, standard meanings, as above |
| 183 | */ |
David Gibson | 96b5fad | 2007-10-24 10:28:52 +1000 | [diff] [blame] | 184 | int fdt_check_header(const void *fdt); |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * fdt_move - move a device tree around in memory |
| 188 | * @fdt: pointer to the device tree to move |
| 189 | * @buf: pointer to memory where the device is to be moved |
| 190 | * @bufsize: size of the memory space at buf |
| 191 | * |
| 192 | * fdt_move() relocates, if possible, the device tree blob located at |
| 193 | * fdt to the buffer at buf of size bufsize. The buffer may overlap |
| 194 | * with the existing device tree blob at fdt. Therefore, |
| 195 | * fdt_move(fdt, fdt, fdt_totalsize(fdt)) |
| 196 | * should always succeed. |
| 197 | * |
| 198 | * returns: |
| 199 | * 0, on success |
| 200 | * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree |
| 201 | * -FDT_ERR_BADMAGIC, |
| 202 | * -FDT_ERR_BADVERSION, |
| 203 | * -FDT_ERR_BADSTATE, standard meanings |
| 204 | */ |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 205 | int fdt_move(const void *fdt, void *buf, int bufsize); |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 206 | |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 207 | /**********************************************************************/ |
| 208 | /* Read-only functions */ |
| 209 | /**********************************************************************/ |
| 210 | |
| 211 | /** |
| 212 | * fdt_string - retreive a string from the strings block of a device tree |
| 213 | * @fdt: pointer to the device tree blob |
| 214 | * @stroffset: offset of the string within the strings block (native endian) |
| 215 | * |
| 216 | * fdt_string() retrieves a pointer to a single string from the |
| 217 | * strings block of the device tree blob at fdt. |
| 218 | * |
| 219 | * returns: |
| 220 | * a pointer to the string, on success |
| 221 | * NULL, if stroffset is out of bounds |
| 222 | */ |
David Gibson | 11d5302 | 2007-10-18 12:10:42 +1000 | [diff] [blame] | 223 | const char *fdt_string(const void *fdt, int stroffset); |
David Gibson | 3aa4cfd | 2006-11-29 16:34:30 +1100 | [diff] [blame] | 224 | |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 225 | /** |
| 226 | * fdt_num_mem_rsv - retreive the number of memory reserve map entries |
| 227 | * @fdt: pointer to the device tree blob |
| 228 | * |
| 229 | * Returns the number of entries in the device tree blob's memory |
| 230 | * reservation map. This does not include the terminating 0,0 entry |
| 231 | * or any other (0,0) entries reserved for expansion. |
| 232 | * |
| 233 | * returns: |
| 234 | * the number of entries |
| 235 | */ |
David Gibson | fd1bf3a | 2007-10-10 17:12:12 +1000 | [diff] [blame] | 236 | int fdt_num_mem_rsv(const void *fdt); |
| 237 | |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 238 | /** |
| 239 | * fdt_get_mem_rsv - retreive one memory reserve map entry |
| 240 | * @fdt: pointer to the device tree blob |
| 241 | * @address, @size: pointers to 64-bit variables |
| 242 | * |
| 243 | * On success, *address and *size will contain the address and size of |
| 244 | * the n-th reserve map entry from the device tree blob, in |
| 245 | * native-endian format. |
| 246 | * |
| 247 | * returns: |
| 248 | * 0, on success |
| 249 | * -FDT_ERR_BADMAGIC, |
| 250 | * -FDT_ERR_BADVERSION, |
| 251 | * -FDT_ERR_BADSTATE, standard meanings |
| 252 | */ |
| 253 | int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size); |
| 254 | |
David Gibson | 57f99b7 | 2007-10-25 11:27:31 +1000 | [diff] [blame] | 255 | /** |
| 256 | * fdt_subnode_offset_namelen - find a subnode based on substring |
| 257 | * @fdt: pointer to the device tree blob |
| 258 | * @parentoffset: structure block offset of a node |
| 259 | * @name: name of the subnode to locate |
| 260 | * @namelen: number of characters of name to consider |
| 261 | * |
| 262 | * Identical to fdt_subnode_offset(), but only examine the first |
| 263 | * namelen characters of name for matching the subnode name. This is |
| 264 | * useful for finding subnodes based on a portion of a larger string, |
| 265 | * such as a full path. |
| 266 | */ |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 267 | int fdt_subnode_offset_namelen(const void *fdt, int parentoffset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 268 | const char *name, int namelen); |
David Gibson | 57f99b7 | 2007-10-25 11:27:31 +1000 | [diff] [blame] | 269 | /** |
| 270 | * fdt_subnode_offset - find a subnode of a given node |
| 271 | * @fdt: pointer to the device tree blob |
| 272 | * @parentoffset: structure block offset of a node |
| 273 | * @name: name of the subnode to locate |
| 274 | * |
| 275 | * fdt_subnode_offset() finds a subnode of the node at structure block |
| 276 | * offset parentoffset with the given name. name may include a unit |
| 277 | * address, in which case fdt_subnode_offset() will find the subnode |
| 278 | * with that unit address, or the unit address may be omitted, in |
| 279 | * which case fdt_subnode_offset() will find an arbitrary subnode |
| 280 | * whose name excluding unit address matches the given name. |
| 281 | * |
| 282 | * returns: |
| 283 | * structure block offset of the requested subnode (>=0), on success |
| 284 | * -FDT_ERR_NOTFOUND, if the requested subnode does not exist |
| 285 | * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag |
| 286 | * -FDT_ERR_BADMAGIC, |
| 287 | * -FDT_ERR_BADVERSION, |
| 288 | * -FDT_ERR_BADSTATE, |
| 289 | * -FDT_ERR_BADSTRUCTURE, |
| 290 | * -FDT_ERR_TRUNCATED, standard meanings. |
| 291 | */ |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 292 | int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 293 | |
David Gibson | 57f99b7 | 2007-10-25 11:27:31 +1000 | [diff] [blame] | 294 | /** |
| 295 | * fdt_path_offset - find a tree node by its full path |
| 296 | * @fdt: pointer to the device tree blob |
| 297 | * @path: full path of the node to locate |
| 298 | * |
| 299 | * fdt_path_offset() finds a node of a given path in the device tree. |
| 300 | * Each path component may omit the unit address portion, but the |
| 301 | * results of this are undefined if any such path component is |
| 302 | * ambiguous (that is if there are multiple nodes at the relevant |
| 303 | * level matching the given component, differentiated only by unit |
| 304 | * address). |
| 305 | * |
| 306 | * returns: |
| 307 | * structure block offset of the node with the requested path (>=0), on success |
| 308 | * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid |
| 309 | * -FDT_ERR_NOTFOUND, if the requested node does not exist |
| 310 | * -FDT_ERR_BADMAGIC, |
| 311 | * -FDT_ERR_BADVERSION, |
| 312 | * -FDT_ERR_BADSTATE, |
| 313 | * -FDT_ERR_BADSTRUCTURE, |
| 314 | * -FDT_ERR_TRUNCATED, standard meanings. |
| 315 | */ |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 316 | int fdt_path_offset(const void *fdt, const char *path); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 317 | |
David Gibson | 57f99b7 | 2007-10-25 11:27:31 +1000 | [diff] [blame] | 318 | /** |
| 319 | * fdt_get_name - retreive the name of a given node |
| 320 | * @fdt: pointer to the device tree blob |
| 321 | * @nodeoffset: structure block offset of the starting node |
| 322 | * @len: pointer to an intger variable (will be overwritten) or NULL |
| 323 | * |
| 324 | * fdt_get_name() retrieves the name (including unit address) of the |
| 325 | * device tree node at structure block offset nodeoffset. If len is |
| 326 | * non-NULL, the length of this name is also returned, in the integer |
| 327 | * pointed to by len. |
| 328 | * |
| 329 | * returns: |
| 330 | * pointer to the node's name, on success |
| 331 | * *len contains the length of that name (>=0) |
| 332 | * NULL, on error |
| 333 | * *len contains an error code (<0): |
| 334 | * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag |
| 335 | * -FDT_ERR_BADMAGIC, |
| 336 | * -FDT_ERR_BADVERSION, |
| 337 | * -FDT_ERR_BADSTATE, standard meanings |
| 338 | */ |
| 339 | const char *fdt_get_name(const void *fdt, int nodeoffset, int *len); |
David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 340 | |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame] | 341 | const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset, |
| 342 | const char *name, int *lenp); |
| 343 | |
| 344 | static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset, |
| 345 | const char *name, |
| 346 | int *lenp) |
| 347 | { |
| 348 | return (struct fdt_property *)fdt_get_property(fdt, nodeoffset, |
| 349 | name, lenp); |
| 350 | } |
| 351 | |
| 352 | const void *fdt_getprop(const void *fdt, int nodeoffset, |
| 353 | const char *name, int *lenp); |
| 354 | static inline void *fdt_getprop_w(void *fdt, int nodeoffset, |
| 355 | const char *name, int *lenp) |
| 356 | { |
| 357 | return (void *)fdt_getprop(fdt, nodeoffset, name, lenp); |
| 358 | } |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 359 | |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 360 | int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen); |
| 361 | |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 362 | int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, |
| 363 | int supernodedepth, int *nodedepth); |
| 364 | int fdt_node_depth(const void *fdt, int nodeoffset); |
| 365 | int fdt_parent_offset(const void *fdt, int nodeoffset); |
| 366 | |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 367 | int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, |
| 368 | const char *propname, |
| 369 | const void *propval, int proplen); |
| 370 | |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 371 | int fdt_node_check_compatible(const void *fdt, int nodeoffset, |
| 372 | const char *compatible); |
| 373 | int fdt_node_offset_by_compatible(const void *fdt, int startoffset, |
| 374 | const char *compatible); |
| 375 | |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 376 | /**********************************************************************/ |
| 377 | /* Write-in-place functions */ |
| 378 | /**********************************************************************/ |
| 379 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 380 | int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 381 | const void *val, int len); |
| 382 | |
| 383 | #define fdt_setprop_inplace_typed(fdt, nodeoffset, name, val) \ |
| 384 | ({ \ |
| 385 | typeof(val) x = val; \ |
| 386 | fdt_setprop_inplace(fdt, nodeoffset, name, &x, sizeof(x)); \ |
| 387 | }) |
| 388 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 389 | int fdt_nop_property(void *fdt, int nodeoffset, const char *name); |
| 390 | int fdt_nop_node(void *fdt, int nodeoffset); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 391 | |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 392 | /**********************************************************************/ |
| 393 | /* Sequential write functions */ |
| 394 | /**********************************************************************/ |
| 395 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 396 | int fdt_create(void *buf, int bufsize); |
| 397 | int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size); |
| 398 | int fdt_finish_reservemap(void *fdt); |
| 399 | int fdt_begin_node(void *fdt, const char *name); |
| 400 | 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] | 401 | #define fdt_property_typed(fdt, name, val) \ |
| 402 | ({ \ |
| 403 | typeof(val) x = (val); \ |
| 404 | fdt_property((fdt), (name), &x, sizeof(x)); \ |
| 405 | }) |
| 406 | #define fdt_property_string(fdt, name, str) \ |
| 407 | fdt_property(fdt, name, str, strlen(str)+1) |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 408 | int fdt_end_node(void *fdt); |
| 409 | int fdt_finish(void *fdt); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 410 | |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 411 | /**********************************************************************/ |
| 412 | /* Read-write functions */ |
| 413 | /**********************************************************************/ |
| 414 | |
David Gibson | a041dcd | 2007-11-01 11:37:31 +1100 | [diff] [blame^] | 415 | int fdt_open_into(const void *fdt, void *buf, int bufsize); |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 416 | int fdt_pack(void *fdt); |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 417 | |
David Gibson | fd1bf3a | 2007-10-10 17:12:12 +1000 | [diff] [blame] | 418 | int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size); |
| 419 | int fdt_del_mem_rsv(void *fdt, int n); |
| 420 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 421 | int fdt_setprop(void *fdt, int nodeoffset, const char *name, |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 422 | const void *val, int len); |
| 423 | #define fdt_setprop_typed(fdt, nodeoffset, name, val) \ |
| 424 | ({ \ |
| 425 | typeof(val) x = (val); \ |
| 426 | fdt_setprop((fdt), (nodeoffset), (name), &x, sizeof(x)); \ |
| 427 | }) |
| 428 | #define fdt_setprop_string(fdt, nodeoffset, name, str) \ |
| 429 | fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1) |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 430 | int fdt_delprop(void *fdt, int nodeoffset, const char *name); |
| 431 | int fdt_add_subnode_namelen(void *fdt, int parentoffset, |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 432 | const char *name, int namelen); |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 433 | int fdt_add_subnode(void *fdt, int parentoffset, const char *name); |
| 434 | int fdt_del_node(void *fdt, int nodeoffset); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 435 | |
David Gibson | cec0c38 | 2007-10-24 17:10:58 +1000 | [diff] [blame] | 436 | /**********************************************************************/ |
| 437 | /* Debugging / informational functions */ |
| 438 | /**********************************************************************/ |
| 439 | |
David Gibson | 5b344f9 | 2006-12-21 09:57:08 +1100 | [diff] [blame] | 440 | const char *fdt_strerror(int errval); |
| 441 | |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 442 | #endif /* _LIBFDT_H */ |