David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [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 | #include "libfdt_env.h" |
| 20 | |
| 21 | #include <fdt.h> |
| 22 | #include <libfdt.h> |
| 23 | |
| 24 | #include "libfdt_internal.h" |
| 25 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 26 | #define CHECK_HEADER(fdt) \ |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 27 | { \ |
| 28 | int err; \ |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 29 | if ((err = _fdt_check_header(fdt)) != 0) \ |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 30 | return err; \ |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 31 | } |
| 32 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 33 | static int offset_streq(const void *fdt, int offset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 34 | const char *s, int len) |
| 35 | { |
| 36 | const char *p = fdt_offset_ptr(fdt, offset, len+1); |
| 37 | |
| 38 | if (! p) |
| 39 | /* short match */ |
| 40 | return 0; |
| 41 | |
| 42 | if (memcmp(p, s, len) != 0) |
| 43 | return 0; |
| 44 | |
| 45 | if (p[len] != '\0') |
| 46 | return 0; |
| 47 | |
| 48 | return 1; |
| 49 | } |
| 50 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 51 | char *fdt_string(const void *fdt, int stroffset) |
David Gibson | 3aa4cfd | 2006-11-29 16:34:30 +1100 | [diff] [blame] | 52 | { |
David Gibson | ede25de | 2006-12-01 15:02:10 +1100 | [diff] [blame] | 53 | return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset; |
David Gibson | 3aa4cfd | 2006-11-29 16:34:30 +1100 | [diff] [blame] | 54 | } |
| 55 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 56 | int fdt_subnode_offset_namelen(const void *fdt, int parentoffset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 57 | const char *name, int namelen) |
| 58 | { |
| 59 | int level = 0; |
| 60 | uint32_t tag; |
| 61 | int offset, nextoffset; |
| 62 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 63 | CHECK_HEADER(fdt); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 64 | |
| 65 | tag = _fdt_next_tag(fdt, parentoffset, &nextoffset); |
| 66 | if (tag != FDT_BEGIN_NODE) |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 67 | return -FDT_ERR_BADOFFSET; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 68 | |
| 69 | do { |
| 70 | offset = nextoffset; |
| 71 | tag = _fdt_next_tag(fdt, offset, &nextoffset); |
| 72 | |
| 73 | switch (tag) { |
| 74 | case FDT_END: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 75 | return -FDT_ERR_TRUNCATED; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 76 | |
| 77 | case FDT_BEGIN_NODE: |
| 78 | level++; |
| 79 | if (level != 1) |
| 80 | continue; |
| 81 | if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen)) |
| 82 | /* Found it! */ |
| 83 | return offset; |
| 84 | break; |
| 85 | |
| 86 | case FDT_END_NODE: |
| 87 | level--; |
| 88 | break; |
| 89 | |
| 90 | case FDT_PROP: |
| 91 | case FDT_NOP: |
| 92 | break; |
| 93 | |
| 94 | default: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 95 | return -FDT_ERR_BADSTRUCTURE; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 96 | } |
| 97 | } while (level >= 0); |
| 98 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 99 | return -FDT_ERR_NOTFOUND; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 100 | } |
| 101 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 102 | int fdt_subnode_offset(const void *fdt, int parentoffset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 103 | const char *name) |
| 104 | { |
| 105 | return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name)); |
| 106 | } |
| 107 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 108 | int fdt_path_offset(const void *fdt, const char *path) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 109 | { |
| 110 | const char *end = path + strlen(path); |
| 111 | const char *p = path; |
| 112 | int offset = 0; |
| 113 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 114 | CHECK_HEADER(fdt); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 115 | |
| 116 | if (*path != '/') |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 117 | return -FDT_ERR_BADPATH; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 118 | |
| 119 | while (*p) { |
| 120 | const char *q; |
| 121 | |
| 122 | while (*p == '/') |
| 123 | p++; |
| 124 | if (! *p) |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 125 | return -FDT_ERR_BADPATH; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 126 | q = strchr(p, '/'); |
| 127 | if (! q) |
| 128 | q = end; |
| 129 | |
| 130 | offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 131 | if (offset < 0) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 132 | return offset; |
| 133 | |
| 134 | p = q; |
| 135 | } |
| 136 | |
| 137 | return offset; |
| 138 | } |
| 139 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 140 | struct fdt_property *fdt_get_property(const void *fdt, |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 141 | int nodeoffset, |
| 142 | const char *name, int *lenp) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 143 | { |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 144 | int level = 0; |
| 145 | uint32_t tag; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 146 | struct fdt_property *prop; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 147 | int namestroff; |
| 148 | int offset, nextoffset; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 149 | int err; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 150 | |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 151 | if ((err = _fdt_check_header(fdt)) != 0) |
| 152 | goto fail; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 153 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 154 | err = -FDT_ERR_BADOFFSET; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 155 | if (nodeoffset % FDT_TAGSIZE) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 156 | goto fail; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 157 | |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 158 | tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset); |
| 159 | if (tag != FDT_BEGIN_NODE) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 160 | goto fail; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 161 | |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 162 | do { |
| 163 | offset = nextoffset; |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 164 | err = -FDT_ERR_INTERNAL; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 165 | if (offset % FDT_TAGSIZE) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 166 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 167 | |
| 168 | tag = _fdt_next_tag(fdt, offset, &nextoffset); |
| 169 | switch (tag) { |
| 170 | case FDT_END: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 171 | err = -FDT_ERR_TRUNCATED; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 172 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 173 | |
| 174 | case FDT_BEGIN_NODE: |
| 175 | level++; |
| 176 | break; |
| 177 | |
| 178 | case FDT_END_NODE: |
| 179 | level--; |
| 180 | break; |
| 181 | |
| 182 | case FDT_PROP: |
| 183 | if (level != 0) |
| 184 | continue; |
| 185 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 186 | err = -FDT_ERR_BADSTRUCTURE; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 187 | prop = fdt_offset_ptr_typed(fdt, offset, prop); |
| 188 | if (! prop) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 189 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 190 | namestroff = fdt32_to_cpu(prop->nameoff); |
| 191 | if (streq(fdt_string(fdt, namestroff), name)) { |
| 192 | /* Found it! */ |
| 193 | int len = fdt32_to_cpu(prop->len); |
| 194 | prop = fdt_offset_ptr(fdt, offset, |
David Gibson | 9825f82 | 2006-12-14 15:29:25 +1100 | [diff] [blame] | 195 | sizeof(*prop)+len); |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 196 | if (! prop) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 197 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 198 | |
| 199 | if (lenp) |
| 200 | *lenp = len; |
| 201 | |
| 202 | return prop; |
| 203 | } |
| 204 | break; |
| 205 | |
| 206 | case FDT_NOP: |
| 207 | break; |
| 208 | |
| 209 | default: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 210 | err = -FDT_ERR_BADSTRUCTURE; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 211 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 212 | } |
| 213 | } while (level >= 0); |
| 214 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 215 | err = -FDT_ERR_NOTFOUND; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 216 | fail: |
| 217 | if (lenp) |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame^] | 218 | *lenp = err; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 219 | return NULL; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 220 | } |
| 221 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 222 | void *fdt_getprop(const void *fdt, int nodeoffset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 223 | const char *name, int *lenp) |
| 224 | { |
| 225 | const struct fdt_property *prop; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 226 | |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 227 | prop = fdt_get_property(fdt, nodeoffset, name, lenp); |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 228 | if (! prop) |
| 229 | return NULL; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 230 | |
| 231 | return prop->data; |
| 232 | } |