Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 1 | /* |
| 2 | * libfdt - Flat Device Tree manipulation |
| 3 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public License |
| 7 | * as published by the Free Software Foundation; either version 2.1 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
Gerald Van Baren | 6679f92 | 2007-04-06 14:17:14 -0400 | [diff] [blame] | 20 | #ifndef _LIBFDT_H |
| 21 | #define _LIBFDT_H |
| 22 | |
Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 23 | #include <fdt.h> |
| 24 | #include <libfdt_env.h> |
| 25 | |
| 26 | #define FDT_FIRST_SUPPORTED_VERSION 0x10 |
| 27 | #define FDT_LAST_SUPPORTED_VERSION 0x11 |
| 28 | |
| 29 | /* Error codes: informative error codes */ |
| 30 | #define FDT_ERR_NOTFOUND 1 |
| 31 | #define FDT_ERR_EXISTS 2 |
| 32 | #define FDT_ERR_NOSPACE 3 |
| 33 | |
| 34 | /* Error codes: codes for bad parameters */ |
| 35 | #define FDT_ERR_BADOFFSET 4 |
| 36 | #define FDT_ERR_BADPATH 5 |
| 37 | #define FDT_ERR_BADSTATE 6 |
| 38 | |
| 39 | /* Error codes: codes for bad device tree blobs */ |
| 40 | #define FDT_ERR_TRUNCATED 7 |
| 41 | #define FDT_ERR_BADMAGIC 8 |
| 42 | #define FDT_ERR_BADVERSION 9 |
| 43 | #define FDT_ERR_BADSTRUCTURE 10 |
| 44 | #define FDT_ERR_BADLAYOUT 11 |
| 45 | |
| 46 | #define FDT_ERR_MAX 11 |
| 47 | |
| 48 | #define fdt_get_header(fdt, field) \ |
| 49 | (fdt32_to_cpu(((struct fdt_header *)(fdt))->field)) |
Wolfgang Denk | 94abd7c | 2007-04-04 01:49:15 +0200 | [diff] [blame] | 50 | #define fdt_magic(fdt) (fdt_get_header(fdt, magic)) |
Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 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)) |
Wolfgang Denk | 94abd7c | 2007-04-04 01:49:15 +0200 | [diff] [blame] | 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)) |
Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 59 | #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct)) |
| 60 | |
| 61 | #define fdt_set_header(fdt, field, val) \ |
| 62 | ((struct fdt_header *)(fdt))->field = cpu_to_fdt32(val) |
| 63 | |
Gerald Van Baren | 6679f92 | 2007-04-06 14:17:14 -0400 | [diff] [blame] | 64 | int fdt_check_header(const void *fdt); |
| 65 | |
Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 66 | void *fdt_offset_ptr(const void *fdt, int offset, int checklen); |
| 67 | |
| 68 | #define fdt_offset_ptr_typed(fdt, offset, var) \ |
| 69 | ((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var))))) |
| 70 | |
| 71 | int fdt_move(const void *fdt, void *buf, int bufsize); |
| 72 | |
| 73 | /* Read-only functions */ |
| 74 | char *fdt_string(const void *fdt, int stroffset); |
| 75 | |
| 76 | int fdt_subnode_offset_namelen(const void *fdt, int parentoffset, |
| 77 | const char *name, int namelen); |
| 78 | int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name); |
| 79 | |
Gerald Van Baren | 1a86116 | 2007-06-06 22:47:58 -0400 | [diff] [blame] | 80 | int fdt_find_node_by_path(const void *fdt, const char *path); |
Gerald Van Baren | 9675ee7 | 2007-05-17 23:54:36 -0400 | [diff] [blame^] | 81 | int fdt_find_node_by_type(const void *fdt, int nodeoffset, const char *type); |
| 82 | |
| 83 | int fdt_node_is_compatible(const void *fdt, int nodeoffset, |
| 84 | const char *compat); |
| 85 | int fdt_find_compatible_node(const void *fdt, int nodeoffset, |
| 86 | const char *type, const char *compat); |
Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 87 | |
| 88 | struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset, |
| 89 | const char *name, int *lenp); |
| 90 | void *fdt_getprop(const void *fdt, int nodeoffset, |
| 91 | const char *name, int *lenp); |
| 92 | |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 93 | uint32_t fdt_next_tag(const void *fdt, int offset, |
Wolfgang Denk | 94abd7c | 2007-04-04 01:49:15 +0200 | [diff] [blame] | 94 | int *nextoffset, char **namep); |
Gerald Van Baren | 3f9f08c | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 95 | int fdt_num_reservemap(void *fdt, int *used, int *total); |
| 96 | int fdt_get_reservemap(void *fdt, int n, struct fdt_reserve_entry *re); |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 97 | |
Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 98 | /* Write-in-place functions */ |
| 99 | int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, |
| 100 | const void *val, int len); |
| 101 | |
| 102 | #define fdt_setprop_inplace_typed(fdt, nodeoffset, name, val) \ |
| 103 | ({ \ |
| 104 | typeof(val) x = val; \ |
| 105 | fdt_setprop_inplace(fdt, nodeoffset, name, &x, sizeof(x)); \ |
| 106 | }) |
| 107 | |
| 108 | int fdt_nop_property(void *fdt, int nodeoffset, const char *name); |
| 109 | int fdt_nop_node(void *fdt, int nodeoffset); |
Gerald Van Baren | 3f9f08c | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 110 | int fdt_insert_reservemap_entry(void *fdt, int n, uint64_t addr, uint64_t size); |
| 111 | |
Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 112 | |
| 113 | /* Sequential-write functions */ |
| 114 | int fdt_create(void *buf, int bufsize); |
| 115 | int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size); |
| 116 | int fdt_finish_reservemap(void *fdt); |
| 117 | int fdt_begin_node(void *fdt, const char *name); |
| 118 | int fdt_property(void *fdt, const char *name, const void *val, int len); |
| 119 | #define fdt_property_typed(fdt, name, val) \ |
| 120 | ({ \ |
| 121 | typeof(val) x = (val); \ |
| 122 | fdt_property((fdt), (name), &x, sizeof(x)); \ |
| 123 | }) |
| 124 | #define fdt_property_string(fdt, name, str) \ |
| 125 | fdt_property(fdt, name, str, strlen(str)+1) |
| 126 | int fdt_end_node(void *fdt); |
| 127 | int fdt_finish(void *fdt); |
Gerald Van Baren | 3f9f08c | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 128 | int fdt_replace_reservemap_entry(void *fdt, int n, uint64_t addr, uint64_t size); |
Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 129 | |
| 130 | /* Read-write functions */ |
| 131 | int fdt_open_into(void *fdt, void *buf, int bufsize); |
| 132 | int fdt_pack(void *fdt); |
| 133 | |
| 134 | int fdt_setprop(void *fdt, int nodeoffset, const char *name, |
| 135 | const void *val, int len); |
| 136 | #define fdt_setprop_typed(fdt, nodeoffset, name, val) \ |
| 137 | ({ \ |
| 138 | typeof(val) x = (val); \ |
| 139 | fdt_setprop((fdt), (nodeoffset), (name), &x, sizeof(x)); \ |
| 140 | }) |
| 141 | #define fdt_setprop_string(fdt, nodeoffset, name, str) \ |
| 142 | fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1) |
| 143 | int fdt_delprop(void *fdt, int nodeoffset, const char *name); |
| 144 | int fdt_add_subnode_namelen(void *fdt, int parentoffset, |
| 145 | const char *name, int namelen); |
| 146 | int fdt_add_subnode(void *fdt, int parentoffset, const char *name); |
| 147 | int fdt_del_node(void *fdt, int nodeoffset); |
| 148 | |
| 149 | /* Extra functions */ |
| 150 | const char *fdt_strerror(int errval); |
| 151 | |
| 152 | #endif /* _LIBFDT_H */ |