[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>
|
---|
| 39 | #include <arch/memstr.h>
|
---|
[1b43a04] | 40 | #include <mm/slab.h>
|
---|
[16da5f8e] | 41 | #include <string.h>
|
---|
[16529d5] | 42 | #include <panic.h>
|
---|
[e731b0d] | 43 | #include <print.h>
|
---|
[16529d5] | 44 |
|
---|
[e731b0d] | 45 | #define PATH_MAX_LEN 256
|
---|
| 46 | #define NAME_BUF_LEN 50
|
---|
[16529d5] | 47 |
|
---|
| 48 | static ofw_tree_node_t *ofw_root;
|
---|
| 49 |
|
---|
| 50 | void ofw_tree_init(ofw_tree_node_t *root)
|
---|
| 51 | {
|
---|
| 52 | ofw_root = root;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[28ecadb] | 55 | /** Get OpenFirmware node property.
|
---|
| 56 | *
|
---|
[e731b0d] | 57 | * @param node Node in which to lookup the property.
|
---|
| 58 | * @param name Name of the property.
|
---|
| 59 | *
|
---|
| 60 | * @return Pointer to the property structure or NULL if no such
|
---|
| 61 | * property.
|
---|
[28ecadb] | 62 | *
|
---|
| 63 | */
|
---|
[e731b0d] | 64 | ofw_tree_property_t *ofw_tree_getprop(const ofw_tree_node_t *node,
|
---|
| 65 | const char *name)
|
---|
[28ecadb] | 66 | {
|
---|
[6c441cf8] | 67 | unsigned int i;
|
---|
[28ecadb] | 68 |
|
---|
| 69 | for (i = 0; i < node->properties; i++) {
|
---|
[b60c582] | 70 | if (str_cmp(node->property[i].name, name) == 0)
|
---|
[28ecadb] | 71 | return &node->property[i];
|
---|
| 72 | }
|
---|
[e731b0d] | 73 |
|
---|
[28ecadb] | 74 | return NULL;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[16529d5] | 77 | /** Return value of the 'name' property.
|
---|
| 78 | *
|
---|
[e731b0d] | 79 | * @param node Node of interest.
|
---|
| 80 | *
|
---|
| 81 | * @return Value of the 'name' property belonging to the node
|
---|
| 82 | * or NULL if the property is invalid.
|
---|
[16529d5] | 83 | *
|
---|
| 84 | */
|
---|
| 85 | const char *ofw_tree_node_name(const ofw_tree_node_t *node)
|
---|
| 86 | {
|
---|
[e731b0d] | 87 | ofw_tree_property_t *prop = ofw_tree_getprop(node, "name");
|
---|
| 88 | if ((!prop) || (prop->size < 2))
|
---|
| 89 | return NULL;
|
---|
[16529d5] | 90 |
|
---|
[28ecadb] | 91 | return prop->value;
|
---|
[16529d5] | 92 | }
|
---|
| 93 |
|
---|
| 94 | /** Lookup child of given name.
|
---|
| 95 | *
|
---|
[e731b0d] | 96 | * @param node Node whose child is being looked up.
|
---|
| 97 | * @param name Name of the child being looked up.
|
---|
| 98 | *
|
---|
| 99 | * @return NULL if there is no such child or pointer to the
|
---|
| 100 | * matching child node.
|
---|
[16529d5] | 101 | *
|
---|
| 102 | */
|
---|
[e731b0d] | 103 | ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node,
|
---|
| 104 | const char *name)
|
---|
[16529d5] | 105 | {
|
---|
| 106 | ofw_tree_node_t *cur;
|
---|
| 107 |
|
---|
[28ecadb] | 108 | /*
|
---|
| 109 | * Try to find the disambigued name.
|
---|
| 110 | */
|
---|
[16529d5] | 111 | for (cur = node->child; cur; cur = cur->peer) {
|
---|
[b60c582] | 112 | if (str_cmp(cur->da_name, name) == 0)
|
---|
[16529d5] | 113 | return cur;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[28ecadb] | 116 | /*
|
---|
| 117 | * Disambigued name not found.
|
---|
| 118 | * Lets try our luck with possibly ambiguous "name" property.
|
---|
| 119 | *
|
---|
| 120 | * We need to do this because paths stored in "/aliases"
|
---|
| 121 | * are not always fully-qualified.
|
---|
| 122 | */
|
---|
| 123 | for (cur = node->child; cur; cur = cur->peer) {
|
---|
[b60c582] | 124 | if (str_cmp(ofw_tree_node_name(cur), name) == 0)
|
---|
[28ecadb] | 125 | return cur;
|
---|
| 126 | }
|
---|
[e731b0d] | 127 |
|
---|
[16529d5] | 128 | return NULL;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[45b26dad] | 131 | /** Lookup first child of given device type.
|
---|
| 132 | *
|
---|
[e731b0d] | 133 | * @param node Node whose child is being looked up.
|
---|
| 134 | * @param dtype Device type of the child being looked up.
|
---|
| 135 | *
|
---|
| 136 | * @return NULL if there is no such child or pointer to the
|
---|
| 137 | * matching child node.
|
---|
[45b26dad] | 138 | *
|
---|
| 139 | */
|
---|
[e731b0d] | 140 | ofw_tree_node_t *ofw_tree_find_child_by_device_type(ofw_tree_node_t *node,
|
---|
| 141 | const char *dtype)
|
---|
[45b26dad] | 142 | {
|
---|
| 143 | ofw_tree_node_t *cur;
|
---|
| 144 |
|
---|
| 145 | for (cur = node->child; cur; cur = cur->peer) {
|
---|
[e731b0d] | 146 | ofw_tree_property_t *prop =
|
---|
| 147 | ofw_tree_getprop(cur, "device_type");
|
---|
| 148 |
|
---|
| 149 | if ((!prop) || (!prop->value))
|
---|
[45b26dad] | 150 | continue;
|
---|
[e731b0d] | 151 |
|
---|
| 152 | if (str_cmp(prop->value, dtype) == 0)
|
---|
[45b26dad] | 153 | return cur;
|
---|
| 154 | }
|
---|
[e731b0d] | 155 |
|
---|
[45b26dad] | 156 | return NULL;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[36db5ac] | 159 | /** Lookup node with matching node_handle.
|
---|
[2f8f480] | 160 | *
|
---|
| 161 | * Child nodes are looked up recursively contrary to peer nodes that
|
---|
| 162 | * are looked up iteratively to avoid stack overflow.
|
---|
[36db5ac] | 163 | *
|
---|
[e731b0d] | 164 | * @param root Root of the searched subtree.
|
---|
| 165 | * @param handle OpenFirmware handle.
|
---|
| 166 | *
|
---|
| 167 | * @return NULL if there is no such node or pointer to the matching
|
---|
| 168 | * node.
|
---|
[36db5ac] | 169 | *
|
---|
| 170 | */
|
---|
[e731b0d] | 171 | ofw_tree_node_t *ofw_tree_find_node_by_handle(ofw_tree_node_t *root,
|
---|
| 172 | uint32_t handle)
|
---|
[36db5ac] | 173 | {
|
---|
[2f8f480] | 174 | ofw_tree_node_t *cur;
|
---|
[e731b0d] | 175 |
|
---|
| 176 | for (cur = root; cur; cur = cur->peer) {
|
---|
[2f8f480] | 177 | if (cur->node_handle == handle)
|
---|
| 178 | return cur;
|
---|
[e731b0d] | 179 |
|
---|
[2f8f480] | 180 | if (cur->child) {
|
---|
[e731b0d] | 181 | ofw_tree_node_t *node
|
---|
| 182 | = ofw_tree_find_node_by_handle(cur->child, handle);
|
---|
[2f8f480] | 183 | if (node)
|
---|
| 184 | return node;
|
---|
| 185 | }
|
---|
[36db5ac] | 186 | }
|
---|
| 187 |
|
---|
[e731b0d] | 188 | return NULL;
|
---|
[36db5ac] | 189 | }
|
---|
| 190 |
|
---|
[45b26dad] | 191 | /** Lookup first peer of given device type.
|
---|
| 192 | *
|
---|
[e731b0d] | 193 | * @param node Node whose peer is being looked up.
|
---|
| 194 | * @param dtype Device type of the child being looked up.
|
---|
| 195 | *
|
---|
| 196 | * @return NULL if there is no such child or pointer to the
|
---|
| 197 | * matching child node.
|
---|
[45b26dad] | 198 | *
|
---|
| 199 | */
|
---|
[e731b0d] | 200 | ofw_tree_node_t *ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node,
|
---|
| 201 | const char *dtype)
|
---|
[45b26dad] | 202 | {
|
---|
| 203 | ofw_tree_node_t *cur;
|
---|
| 204 |
|
---|
| 205 | for (cur = node->peer; cur; cur = cur->peer) {
|
---|
[e731b0d] | 206 | ofw_tree_property_t *prop =
|
---|
| 207 | ofw_tree_getprop(cur, "device_type");
|
---|
| 208 |
|
---|
| 209 | if ((!prop) || (!prop->value))
|
---|
[45b26dad] | 210 | continue;
|
---|
[e731b0d] | 211 |
|
---|
| 212 | if (str_cmp(prop->value, dtype) == 0)
|
---|
[45b26dad] | 213 | return cur;
|
---|
| 214 | }
|
---|
[e731b0d] | 215 |
|
---|
[45b26dad] | 216 | return NULL;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[965dc18] | 219 | /** Lookup first peer of given name.
|
---|
| 220 | *
|
---|
[e731b0d] | 221 | * @param node Node whose peer is being looked up.
|
---|
| 222 | * @param name Name of the child being looked up.
|
---|
| 223 | *
|
---|
| 224 | * @return NULL if there is no such peer or pointer to the matching
|
---|
| 225 | * peer node.
|
---|
[965dc18] | 226 | *
|
---|
| 227 | */
|
---|
[e731b0d] | 228 | ofw_tree_node_t *ofw_tree_find_peer_by_name(ofw_tree_node_t *node,
|
---|
| 229 | const char *name)
|
---|
[965dc18] | 230 | {
|
---|
| 231 | ofw_tree_node_t *cur;
|
---|
| 232 |
|
---|
| 233 | for (cur = node->peer; cur; cur = cur->peer) {
|
---|
[e731b0d] | 234 | ofw_tree_property_t *prop
|
---|
| 235 | = ofw_tree_getprop(cur, "name");
|
---|
| 236 |
|
---|
| 237 | if ((!prop) || (!prop->value))
|
---|
[965dc18] | 238 | continue;
|
---|
[e731b0d] | 239 |
|
---|
[b60c582] | 240 | if (str_cmp(prop->value, name) == 0)
|
---|
[965dc18] | 241 | return cur;
|
---|
| 242 | }
|
---|
[e731b0d] | 243 |
|
---|
[965dc18] | 244 | return NULL;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[16529d5] | 247 | /** Lookup OpenFirmware node by its path.
|
---|
| 248 | *
|
---|
[e731b0d] | 249 | * @param path Path to the node.
|
---|
| 250 | *
|
---|
| 251 | * @return NULL if there is no such node or pointer to the leaf
|
---|
| 252 | * node.
|
---|
[16529d5] | 253 | *
|
---|
| 254 | */
|
---|
| 255 | ofw_tree_node_t *ofw_tree_lookup(const char *path)
|
---|
| 256 | {
|
---|
[e731b0d] | 257 | if (path[0] != '/')
|
---|
| 258 | return NULL;
|
---|
| 259 |
|
---|
[16529d5] | 260 | ofw_tree_node_t *node = ofw_root;
|
---|
[98000fb] | 261 | size_t i;
|
---|
| 262 | size_t j;
|
---|
[16529d5] | 263 |
|
---|
[b60c582] | 264 | for (i = 1; (i < str_size(path)) && (node); i = j + 1) {
|
---|
| 265 | for (j = i; (j < str_size(path)) && (path[j] != '/'); j++);
|
---|
| 266 |
|
---|
| 267 | /* Skip extra slashes */
|
---|
| 268 | if (i == j)
|
---|
[16529d5] | 269 | continue;
|
---|
[b60c582] | 270 |
|
---|
[e731b0d] | 271 | char buf[NAME_BUF_LEN + 1];
|
---|
[16529d5] | 272 | memcpy(buf, &path[i], j - i);
|
---|
[b60c582] | 273 | buf[j - i] = 0;
|
---|
[16529d5] | 274 | node = ofw_tree_find_child(node, buf);
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | return node;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[e731b0d] | 280 | /** Walk the OpenFirmware device subtree rooted in a node.
|
---|
[2f8f480] | 281 | *
|
---|
| 282 | * Child nodes are processed recursively and peer nodes are processed
|
---|
| 283 | * iteratively in order to avoid stack overflow.
|
---|
[16529d5] | 284 | *
|
---|
[e731b0d] | 285 | * @param node Root of the subtree.
|
---|
| 286 | * @param dtype Device type to look for.
|
---|
| 287 | * @param walker Routine to be invoked on found device.
|
---|
| 288 | * @param arg User argument to the walker.
|
---|
| 289 | *
|
---|
| 290 | * @return True if the walk should continue.
|
---|
| 291 | *
|
---|
[16529d5] | 292 | */
|
---|
[e731b0d] | 293 | static bool ofw_tree_walk_by_device_type_internal(ofw_tree_node_t *node,
|
---|
| 294 | const char *dtype, ofw_tree_walker_t walker, void *arg)
|
---|
[16529d5] | 295 | {
|
---|
[e731b0d] | 296 | ofw_tree_node_t *cur;
|
---|
| 297 |
|
---|
| 298 | for (cur = node; cur; cur = cur->peer) {
|
---|
| 299 | ofw_tree_property_t *prop =
|
---|
| 300 | ofw_tree_getprop(cur, "device_type");
|
---|
| 301 |
|
---|
| 302 | if ((prop) && (prop->value) && (str_cmp(prop->value, dtype) == 0)) {
|
---|
| 303 | bool ret = walker(cur, arg);
|
---|
| 304 | if (!ret)
|
---|
| 305 | return false;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | if (cur->child) {
|
---|
| 309 | bool ret =
|
---|
| 310 | ofw_tree_walk_by_device_type_internal(cur->child, dtype, walker, arg);
|
---|
| 311 | if (!ret)
|
---|
| 312 | return false;
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | return true;
|
---|
| 317 | }
|
---|
[1b43a04] | 318 |
|
---|
[e731b0d] | 319 | /** Walk the OpenFirmware device tree and find devices by type.
|
---|
| 320 | *
|
---|
| 321 | * Walk the whole OpenFirmware device tree and if any node has
|
---|
| 322 | * the property "device_type" equal to dtype, run a walker on it.
|
---|
| 323 | * If the walker returns false, the walk does not continue.
|
---|
| 324 | *
|
---|
| 325 | * @param dtype Device type to look for.
|
---|
| 326 | * @param walker Routine to be invoked on found device.
|
---|
| 327 | * @param arg User argument to the walker.
|
---|
| 328 | *
|
---|
| 329 | */
|
---|
| 330 | void ofw_tree_walk_by_device_type(const char *dtype, ofw_tree_walker_t walker,
|
---|
| 331 | void *arg)
|
---|
| 332 | {
|
---|
| 333 | (void) ofw_tree_walk_by_device_type_internal(ofw_root, dtype, walker, arg);
|
---|
| 334 | }
|
---|
[1b43a04] | 335 |
|
---|
[e731b0d] | 336 | /** Print OpenFirmware device subtree rooted in a node.
|
---|
| 337 | *
|
---|
| 338 | * Child nodes are processed recursively and peer nodes are processed
|
---|
| 339 | * iteratively in order to avoid stack overflow.
|
---|
| 340 | *
|
---|
| 341 | * @param node Root of the subtree.
|
---|
| 342 | * @param path Current path, NULL for the very root of the entire tree.
|
---|
| 343 | *
|
---|
| 344 | */
|
---|
| 345 | static void ofw_tree_node_print(ofw_tree_node_t *node, const char *path)
|
---|
| 346 | {
|
---|
| 347 | char *cur_path = (char *) malloc(PATH_MAX_LEN, 0);
|
---|
| 348 | ofw_tree_node_t *cur;
|
---|
| 349 |
|
---|
[2f8f480] | 350 | for (cur = node; cur; cur = cur->peer) {
|
---|
[e731b0d] | 351 | if ((cur->parent) && (path)) {
|
---|
| 352 | snprintf(cur_path, PATH_MAX_LEN, "%s/%s", path, cur->da_name);
|
---|
| 353 | printf("%s\n", cur_path);
|
---|
[2f8f480] | 354 | } else {
|
---|
[e731b0d] | 355 | snprintf(cur_path, PATH_MAX_LEN, "%s", cur->da_name);
|
---|
[2f8f480] | 356 | printf("/\n");
|
---|
| 357 | }
|
---|
[e731b0d] | 358 |
|
---|
[2f8f480] | 359 | if (cur->child)
|
---|
[e731b0d] | 360 | ofw_tree_node_print(cur->child, cur_path);
|
---|
[2f8f480] | 361 | }
|
---|
[e731b0d] | 362 |
|
---|
| 363 | free(cur_path);
|
---|
[16529d5] | 364 | }
|
---|
| 365 |
|
---|
| 366 | /** Print the structure of the OpenFirmware device tree. */
|
---|
| 367 | void ofw_tree_print(void)
|
---|
| 368 | {
|
---|
| 369 | ofw_tree_node_print(ofw_root, NULL);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[61e90dd] | 372 | /** @}
|
---|
| 373 | */
|
---|