[61e90dd] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[61e90dd] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup ofw
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[e731b0d] | 34 | * @brief OpenFirmware device tree navigation.
|
---|
[61e90dd] | 35 | *
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[16529d5] | 38 | #include <genarch/ofw/ofw_tree.h>
|
---|
[1b43a04] | 39 | #include <mm/slab.h>
|
---|
[19f857a] | 40 | #include <str.h>
|
---|
[16529d5] | 41 | #include <panic.h>
|
---|
[e731b0d] | 42 | #include <print.h>
|
---|
[16529d5] | 43 |
|
---|
[e731b0d] | 44 | #define PATH_MAX_LEN 256
|
---|
| 45 | #define NAME_BUF_LEN 50
|
---|
[16529d5] | 46 |
|
---|
| 47 | static ofw_tree_node_t *ofw_root;
|
---|
| 48 |
|
---|
| 49 | void ofw_tree_init(ofw_tree_node_t *root)
|
---|
| 50 | {
|
---|
| 51 | ofw_root = root;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[28ecadb] | 54 | /** Get OpenFirmware node property.
|
---|
| 55 | *
|
---|
[e731b0d] | 56 | * @param node Node in which to lookup the property.
|
---|
| 57 | * @param name Name of the property.
|
---|
| 58 | *
|
---|
| 59 | * @return Pointer to the property structure or NULL if no such
|
---|
| 60 | * property.
|
---|
[28ecadb] | 61 | *
|
---|
| 62 | */
|
---|
[e731b0d] | 63 | ofw_tree_property_t *ofw_tree_getprop(const ofw_tree_node_t *node,
|
---|
| 64 | const char *name)
|
---|
[28ecadb] | 65 | {
|
---|
[4872160] | 66 | size_t i;
|
---|
[28ecadb] | 67 |
|
---|
| 68 | for (i = 0; i < node->properties; i++) {
|
---|
[b60c582] | 69 | if (str_cmp(node->property[i].name, name) == 0)
|
---|
[28ecadb] | 70 | return &node->property[i];
|
---|
| 71 | }
|
---|
[e731b0d] | 72 |
|
---|
[28ecadb] | 73 | return NULL;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[16529d5] | 76 | /** Return value of the 'name' property.
|
---|
| 77 | *
|
---|
[e731b0d] | 78 | * @param node Node of interest.
|
---|
| 79 | *
|
---|
| 80 | * @return Value of the 'name' property belonging to the node
|
---|
| 81 | * or NULL if the property is invalid.
|
---|
[16529d5] | 82 | *
|
---|
| 83 | */
|
---|
| 84 | const char *ofw_tree_node_name(const ofw_tree_node_t *node)
|
---|
| 85 | {
|
---|
[e731b0d] | 86 | ofw_tree_property_t *prop = ofw_tree_getprop(node, "name");
|
---|
| 87 | if ((!prop) || (prop->size < 2))
|
---|
| 88 | return NULL;
|
---|
[16529d5] | 89 |
|
---|
[28ecadb] | 90 | return prop->value;
|
---|
[16529d5] | 91 | }
|
---|
| 92 |
|
---|
| 93 | /** Lookup child of given name.
|
---|
| 94 | *
|
---|
[e731b0d] | 95 | * @param node Node whose child is being looked up.
|
---|
| 96 | * @param name Name of the child being looked up.
|
---|
| 97 | *
|
---|
| 98 | * @return NULL if there is no such child or pointer to the
|
---|
| 99 | * matching child node.
|
---|
[16529d5] | 100 | *
|
---|
| 101 | */
|
---|
[e731b0d] | 102 | ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node,
|
---|
| 103 | const char *name)
|
---|
[16529d5] | 104 | {
|
---|
| 105 | ofw_tree_node_t *cur;
|
---|
| 106 |
|
---|
[28ecadb] | 107 | /*
|
---|
| 108 | * Try to find the disambigued name.
|
---|
| 109 | */
|
---|
[16529d5] | 110 | for (cur = node->child; cur; cur = cur->peer) {
|
---|
[b60c582] | 111 | if (str_cmp(cur->da_name, name) == 0)
|
---|
[16529d5] | 112 | return cur;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[28ecadb] | 115 | /*
|
---|
| 116 | * Disambigued name not found.
|
---|
| 117 | * Lets try our luck with possibly ambiguous "name" property.
|
---|
| 118 | *
|
---|
| 119 | * We need to do this because paths stored in "/aliases"
|
---|
| 120 | * are not always fully-qualified.
|
---|
| 121 | */
|
---|
| 122 | for (cur = node->child; cur; cur = cur->peer) {
|
---|
[b60c582] | 123 | if (str_cmp(ofw_tree_node_name(cur), name) == 0)
|
---|
[28ecadb] | 124 | return cur;
|
---|
| 125 | }
|
---|
[e731b0d] | 126 |
|
---|
[16529d5] | 127 | return NULL;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[45b26dad] | 130 | /** Lookup first child of given device type.
|
---|
| 131 | *
|
---|
[e731b0d] | 132 | * @param node Node whose child is being looked up.
|
---|
| 133 | * @param dtype Device type of the child being looked up.
|
---|
| 134 | *
|
---|
| 135 | * @return NULL if there is no such child or pointer to the
|
---|
| 136 | * matching child node.
|
---|
[45b26dad] | 137 | *
|
---|
| 138 | */
|
---|
[e731b0d] | 139 | ofw_tree_node_t *ofw_tree_find_child_by_device_type(ofw_tree_node_t *node,
|
---|
| 140 | const char *dtype)
|
---|
[45b26dad] | 141 | {
|
---|
| 142 | ofw_tree_node_t *cur;
|
---|
| 143 |
|
---|
| 144 | for (cur = node->child; cur; cur = cur->peer) {
|
---|
[e731b0d] | 145 | ofw_tree_property_t *prop =
|
---|
| 146 | ofw_tree_getprop(cur, "device_type");
|
---|
| 147 |
|
---|
| 148 | if ((!prop) || (!prop->value))
|
---|
[45b26dad] | 149 | continue;
|
---|
[e731b0d] | 150 |
|
---|
| 151 | if (str_cmp(prop->value, dtype) == 0)
|
---|
[45b26dad] | 152 | return cur;
|
---|
| 153 | }
|
---|
[e731b0d] | 154 |
|
---|
[45b26dad] | 155 | return NULL;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[36db5ac] | 158 | /** Lookup node with matching node_handle.
|
---|
[2f8f480] | 159 | *
|
---|
| 160 | * Child nodes are looked up recursively contrary to peer nodes that
|
---|
| 161 | * are looked up iteratively to avoid stack overflow.
|
---|
[36db5ac] | 162 | *
|
---|
[e731b0d] | 163 | * @param root Root of the searched subtree.
|
---|
| 164 | * @param handle OpenFirmware handle.
|
---|
| 165 | *
|
---|
| 166 | * @return NULL if there is no such node or pointer to the matching
|
---|
| 167 | * node.
|
---|
[36db5ac] | 168 | *
|
---|
| 169 | */
|
---|
[e731b0d] | 170 | ofw_tree_node_t *ofw_tree_find_node_by_handle(ofw_tree_node_t *root,
|
---|
[4872160] | 171 | phandle handle)
|
---|
[36db5ac] | 172 | {
|
---|
[2f8f480] | 173 | ofw_tree_node_t *cur;
|
---|
[e731b0d] | 174 |
|
---|
| 175 | for (cur = root; cur; cur = cur->peer) {
|
---|
[2f8f480] | 176 | if (cur->node_handle == handle)
|
---|
| 177 | return cur;
|
---|
[e731b0d] | 178 |
|
---|
[2f8f480] | 179 | if (cur->child) {
|
---|
[e731b0d] | 180 | ofw_tree_node_t *node
|
---|
| 181 | = ofw_tree_find_node_by_handle(cur->child, handle);
|
---|
[2f8f480] | 182 | if (node)
|
---|
| 183 | return node;
|
---|
| 184 | }
|
---|
[36db5ac] | 185 | }
|
---|
| 186 |
|
---|
[e731b0d] | 187 | return NULL;
|
---|
[36db5ac] | 188 | }
|
---|
| 189 |
|
---|
[45b26dad] | 190 | /** Lookup first peer of given device type.
|
---|
| 191 | *
|
---|
[e731b0d] | 192 | * @param node Node whose peer is being looked up.
|
---|
| 193 | * @param dtype Device type of the child being looked up.
|
---|
| 194 | *
|
---|
| 195 | * @return NULL if there is no such child or pointer to the
|
---|
| 196 | * matching child node.
|
---|
[45b26dad] | 197 | *
|
---|
| 198 | */
|
---|
[e731b0d] | 199 | ofw_tree_node_t *ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node,
|
---|
| 200 | const char *dtype)
|
---|
[45b26dad] | 201 | {
|
---|
| 202 | ofw_tree_node_t *cur;
|
---|
| 203 |
|
---|
| 204 | for (cur = node->peer; cur; cur = cur->peer) {
|
---|
[e731b0d] | 205 | ofw_tree_property_t *prop =
|
---|
| 206 | ofw_tree_getprop(cur, "device_type");
|
---|
| 207 |
|
---|
| 208 | if ((!prop) || (!prop->value))
|
---|
[45b26dad] | 209 | continue;
|
---|
[e731b0d] | 210 |
|
---|
| 211 | if (str_cmp(prop->value, dtype) == 0)
|
---|
[45b26dad] | 212 | return cur;
|
---|
| 213 | }
|
---|
[e731b0d] | 214 |
|
---|
[45b26dad] | 215 | return NULL;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[965dc18] | 218 | /** Lookup first peer of given name.
|
---|
| 219 | *
|
---|
[e731b0d] | 220 | * @param node Node whose peer is being looked up.
|
---|
| 221 | * @param name Name of the child being looked up.
|
---|
| 222 | *
|
---|
| 223 | * @return NULL if there is no such peer or pointer to the matching
|
---|
| 224 | * peer node.
|
---|
[965dc18] | 225 | *
|
---|
| 226 | */
|
---|
[e731b0d] | 227 | ofw_tree_node_t *ofw_tree_find_peer_by_name(ofw_tree_node_t *node,
|
---|
| 228 | const char *name)
|
---|
[965dc18] | 229 | {
|
---|
| 230 | ofw_tree_node_t *cur;
|
---|
| 231 |
|
---|
| 232 | for (cur = node->peer; cur; cur = cur->peer) {
|
---|
[e731b0d] | 233 | ofw_tree_property_t *prop
|
---|
| 234 | = ofw_tree_getprop(cur, "name");
|
---|
| 235 |
|
---|
| 236 | if ((!prop) || (!prop->value))
|
---|
[965dc18] | 237 | continue;
|
---|
[e731b0d] | 238 |
|
---|
[b60c582] | 239 | if (str_cmp(prop->value, name) == 0)
|
---|
[965dc18] | 240 | return cur;
|
---|
| 241 | }
|
---|
[e731b0d] | 242 |
|
---|
[965dc18] | 243 | return NULL;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[16529d5] | 246 | /** Lookup OpenFirmware node by its path.
|
---|
| 247 | *
|
---|
[e731b0d] | 248 | * @param path Path to the node.
|
---|
| 249 | *
|
---|
| 250 | * @return NULL if there is no such node or pointer to the leaf
|
---|
| 251 | * node.
|
---|
[16529d5] | 252 | *
|
---|
| 253 | */
|
---|
| 254 | ofw_tree_node_t *ofw_tree_lookup(const char *path)
|
---|
| 255 | {
|
---|
[e731b0d] | 256 | if (path[0] != '/')
|
---|
| 257 | return NULL;
|
---|
| 258 |
|
---|
[16529d5] | 259 | ofw_tree_node_t *node = ofw_root;
|
---|
[98000fb] | 260 | size_t i;
|
---|
| 261 | size_t j;
|
---|
[16529d5] | 262 |
|
---|
[b60c582] | 263 | for (i = 1; (i < str_size(path)) && (node); i = j + 1) {
|
---|
| 264 | for (j = i; (j < str_size(path)) && (path[j] != '/'); j++);
|
---|
| 265 |
|
---|
| 266 | /* Skip extra slashes */
|
---|
| 267 | if (i == j)
|
---|
[16529d5] | 268 | continue;
|
---|
[b60c582] | 269 |
|
---|
[e731b0d] | 270 | char buf[NAME_BUF_LEN + 1];
|
---|
[16529d5] | 271 | memcpy(buf, &path[i], j - i);
|
---|
[b60c582] | 272 | buf[j - i] = 0;
|
---|
[16529d5] | 273 | node = ofw_tree_find_child(node, buf);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | return node;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[e731b0d] | 279 | /** Walk the OpenFirmware device subtree rooted in a node.
|
---|
[2f8f480] | 280 | *
|
---|
| 281 | * Child nodes are processed recursively and peer nodes are processed
|
---|
| 282 | * iteratively in order to avoid stack overflow.
|
---|
[16529d5] | 283 | *
|
---|
[e731b0d] | 284 | * @param node Root of the subtree.
|
---|
| 285 | * @param dtype Device type to look for.
|
---|
| 286 | * @param walker Routine to be invoked on found device.
|
---|
| 287 | * @param arg User argument to the walker.
|
---|
| 288 | *
|
---|
| 289 | * @return True if the walk should continue.
|
---|
| 290 | *
|
---|
[16529d5] | 291 | */
|
---|
[e731b0d] | 292 | static bool ofw_tree_walk_by_device_type_internal(ofw_tree_node_t *node,
|
---|
| 293 | const char *dtype, ofw_tree_walker_t walker, void *arg)
|
---|
[16529d5] | 294 | {
|
---|
[e731b0d] | 295 | ofw_tree_node_t *cur;
|
---|
| 296 |
|
---|
| 297 | for (cur = node; cur; cur = cur->peer) {
|
---|
| 298 | ofw_tree_property_t *prop =
|
---|
| 299 | ofw_tree_getprop(cur, "device_type");
|
---|
| 300 |
|
---|
| 301 | if ((prop) && (prop->value) && (str_cmp(prop->value, dtype) == 0)) {
|
---|
| 302 | bool ret = walker(cur, arg);
|
---|
| 303 | if (!ret)
|
---|
| 304 | return false;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | if (cur->child) {
|
---|
| 308 | bool ret =
|
---|
| 309 | ofw_tree_walk_by_device_type_internal(cur->child, dtype, walker, arg);
|
---|
| 310 | if (!ret)
|
---|
| 311 | return false;
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | return true;
|
---|
| 316 | }
|
---|
[1b43a04] | 317 |
|
---|
[e731b0d] | 318 | /** Walk the OpenFirmware device tree and find devices by type.
|
---|
| 319 | *
|
---|
| 320 | * Walk the whole OpenFirmware device tree and if any node has
|
---|
| 321 | * the property "device_type" equal to dtype, run a walker on it.
|
---|
| 322 | * If the walker returns false, the walk does not continue.
|
---|
| 323 | *
|
---|
| 324 | * @param dtype Device type to look for.
|
---|
| 325 | * @param walker Routine to be invoked on found device.
|
---|
| 326 | * @param arg User argument to the walker.
|
---|
| 327 | *
|
---|
| 328 | */
|
---|
| 329 | void ofw_tree_walk_by_device_type(const char *dtype, ofw_tree_walker_t walker,
|
---|
| 330 | void *arg)
|
---|
| 331 | {
|
---|
| 332 | (void) ofw_tree_walk_by_device_type_internal(ofw_root, dtype, walker, arg);
|
---|
| 333 | }
|
---|
[1b43a04] | 334 |
|
---|
[e731b0d] | 335 | /** Print OpenFirmware device subtree rooted in a node.
|
---|
| 336 | *
|
---|
| 337 | * Child nodes are processed recursively and peer nodes are processed
|
---|
| 338 | * iteratively in order to avoid stack overflow.
|
---|
| 339 | *
|
---|
| 340 | * @param node Root of the subtree.
|
---|
| 341 | * @param path Current path, NULL for the very root of the entire tree.
|
---|
| 342 | *
|
---|
| 343 | */
|
---|
| 344 | static void ofw_tree_node_print(ofw_tree_node_t *node, const char *path)
|
---|
| 345 | {
|
---|
| 346 | char *cur_path = (char *) malloc(PATH_MAX_LEN, 0);
|
---|
| 347 | ofw_tree_node_t *cur;
|
---|
| 348 |
|
---|
[2f8f480] | 349 | for (cur = node; cur; cur = cur->peer) {
|
---|
[e731b0d] | 350 | if ((cur->parent) && (path)) {
|
---|
| 351 | snprintf(cur_path, PATH_MAX_LEN, "%s/%s", path, cur->da_name);
|
---|
| 352 | printf("%s\n", cur_path);
|
---|
[2f8f480] | 353 | } else {
|
---|
[e731b0d] | 354 | snprintf(cur_path, PATH_MAX_LEN, "%s", cur->da_name);
|
---|
[2f8f480] | 355 | printf("/\n");
|
---|
| 356 | }
|
---|
[e731b0d] | 357 |
|
---|
[2f8f480] | 358 | if (cur->child)
|
---|
[e731b0d] | 359 | ofw_tree_node_print(cur->child, cur_path);
|
---|
[2f8f480] | 360 | }
|
---|
[e731b0d] | 361 |
|
---|
| 362 | free(cur_path);
|
---|
[16529d5] | 363 | }
|
---|
| 364 |
|
---|
| 365 | /** Print the structure of the OpenFirmware device tree. */
|
---|
| 366 | void ofw_tree_print(void)
|
---|
| 367 | {
|
---|
| 368 | ofw_tree_node_print(ofw_root, NULL);
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[61e90dd] | 371 | /** @}
|
---|
| 372 | */
|
---|