[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 |
|
---|
[6404aca] | 29 | /** @addtogroup kernel_genarch
|
---|
[61e90dd] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[e731b0d] | 34 | * @brief OpenFirmware device tree navigation.
|
---|
[61e90dd] | 35 | *
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[16529d5] | 38 | #include <genarch/ofw/ofw_tree.h>
|
---|
[aafed15] | 39 | #include <stdlib.h>
|
---|
[de3db94a] | 40 | #include <sysinfo/sysinfo.h>
|
---|
[b169619] | 41 | #include <memw.h>
|
---|
[19f857a] | 42 | #include <str.h>
|
---|
[16529d5] | 43 | #include <panic.h>
|
---|
[bab75df6] | 44 | #include <stdio.h>
|
---|
[16529d5] | 45 |
|
---|
[e731b0d] | 46 | #define PATH_MAX_LEN 256
|
---|
| 47 | #define NAME_BUF_LEN 50
|
---|
[16529d5] | 48 |
|
---|
| 49 | static ofw_tree_node_t *ofw_root;
|
---|
| 50 |
|
---|
| 51 | void ofw_tree_init(ofw_tree_node_t *root)
|
---|
| 52 | {
|
---|
| 53 | ofw_root = root;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[28ecadb] | 56 | /** Get OpenFirmware node property.
|
---|
| 57 | *
|
---|
[e731b0d] | 58 | * @param node Node in which to lookup the property.
|
---|
| 59 | * @param name Name of the property.
|
---|
| 60 | *
|
---|
| 61 | * @return Pointer to the property structure or NULL if no such
|
---|
| 62 | * property.
|
---|
[28ecadb] | 63 | *
|
---|
| 64 | */
|
---|
[e731b0d] | 65 | ofw_tree_property_t *ofw_tree_getprop(const ofw_tree_node_t *node,
|
---|
| 66 | const char *name)
|
---|
[28ecadb] | 67 | {
|
---|
[8b9a443] | 68 | for (size_t i = 0; i < node->properties; i++) {
|
---|
[b60c582] | 69 | if (str_cmp(node->property[i].name, name) == 0)
|
---|
[28ecadb] | 70 | return &node->property[i];
|
---|
| 71 | }
|
---|
[a35b458] | 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;
|
---|
[a35b458] | 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 | {
|
---|
[28ecadb] | 105 | /*
|
---|
| 106 | * Try to find the disambigued name.
|
---|
| 107 | */
|
---|
[8ebe212] | 108 | for (ofw_tree_node_t *cur = node->child; cur; cur = cur->peer) {
|
---|
[b60c582] | 109 | if (str_cmp(cur->da_name, name) == 0)
|
---|
[16529d5] | 110 | return cur;
|
---|
| 111 | }
|
---|
[a35b458] | 112 |
|
---|
[28ecadb] | 113 | /*
|
---|
| 114 | * Disambigued name not found.
|
---|
| 115 | * Lets try our luck with possibly ambiguous "name" property.
|
---|
| 116 | *
|
---|
| 117 | * We need to do this because paths stored in "/aliases"
|
---|
| 118 | * are not always fully-qualified.
|
---|
| 119 | */
|
---|
[8ebe212] | 120 | for (ofw_tree_node_t *cur = node->child; cur; cur = cur->peer) {
|
---|
[b60c582] | 121 | if (str_cmp(ofw_tree_node_name(cur), name) == 0)
|
---|
[28ecadb] | 122 | return cur;
|
---|
| 123 | }
|
---|
[a35b458] | 124 |
|
---|
[16529d5] | 125 | return NULL;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[45b26dad] | 128 | /** Lookup first child of given device type.
|
---|
| 129 | *
|
---|
[e731b0d] | 130 | * @param node Node whose child is being looked up.
|
---|
| 131 | * @param dtype Device type of the child being looked up.
|
---|
| 132 | *
|
---|
| 133 | * @return NULL if there is no such child or pointer to the
|
---|
| 134 | * matching child node.
|
---|
[45b26dad] | 135 | *
|
---|
| 136 | */
|
---|
[e731b0d] | 137 | ofw_tree_node_t *ofw_tree_find_child_by_device_type(ofw_tree_node_t *node,
|
---|
| 138 | const char *dtype)
|
---|
[45b26dad] | 139 | {
|
---|
[8ebe212] | 140 | for (ofw_tree_node_t *cur = node->child; cur; cur = cur->peer) {
|
---|
[e731b0d] | 141 | ofw_tree_property_t *prop =
|
---|
| 142 | ofw_tree_getprop(cur, "device_type");
|
---|
[a35b458] | 143 |
|
---|
[e731b0d] | 144 | if ((!prop) || (!prop->value))
|
---|
[45b26dad] | 145 | continue;
|
---|
[a35b458] | 146 |
|
---|
[e731b0d] | 147 | if (str_cmp(prop->value, dtype) == 0)
|
---|
[45b26dad] | 148 | return cur;
|
---|
| 149 | }
|
---|
[a35b458] | 150 |
|
---|
[45b26dad] | 151 | return NULL;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[36db5ac] | 154 | /** Lookup node with matching node_handle.
|
---|
[2f8f480] | 155 | *
|
---|
| 156 | * Child nodes are looked up recursively contrary to peer nodes that
|
---|
| 157 | * are looked up iteratively to avoid stack overflow.
|
---|
[36db5ac] | 158 | *
|
---|
[e731b0d] | 159 | * @param root Root of the searched subtree.
|
---|
| 160 | * @param handle OpenFirmware handle.
|
---|
| 161 | *
|
---|
| 162 | * @return NULL if there is no such node or pointer to the matching
|
---|
| 163 | * node.
|
---|
[36db5ac] | 164 | *
|
---|
| 165 | */
|
---|
[e731b0d] | 166 | ofw_tree_node_t *ofw_tree_find_node_by_handle(ofw_tree_node_t *root,
|
---|
[4872160] | 167 | phandle handle)
|
---|
[36db5ac] | 168 | {
|
---|
[8ebe212] | 169 | for (ofw_tree_node_t *cur = root; cur; cur = cur->peer) {
|
---|
[2f8f480] | 170 | if (cur->node_handle == handle)
|
---|
| 171 | return cur;
|
---|
[a35b458] | 172 |
|
---|
[2f8f480] | 173 | if (cur->child) {
|
---|
[8b9a443] | 174 | ofw_tree_node_t *node =
|
---|
| 175 | ofw_tree_find_node_by_handle(cur->child, handle);
|
---|
[2f8f480] | 176 | if (node)
|
---|
| 177 | return node;
|
---|
| 178 | }
|
---|
[36db5ac] | 179 | }
|
---|
[a35b458] | 180 |
|
---|
[e731b0d] | 181 | return NULL;
|
---|
[36db5ac] | 182 | }
|
---|
| 183 |
|
---|
[45b26dad] | 184 | /** Lookup first peer of given device type.
|
---|
| 185 | *
|
---|
[e731b0d] | 186 | * @param node Node whose peer is being looked up.
|
---|
| 187 | * @param dtype Device type of the child being looked up.
|
---|
| 188 | *
|
---|
| 189 | * @return NULL if there is no such child or pointer to the
|
---|
| 190 | * matching child node.
|
---|
[45b26dad] | 191 | *
|
---|
| 192 | */
|
---|
[e731b0d] | 193 | ofw_tree_node_t *ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node,
|
---|
| 194 | const char *dtype)
|
---|
[45b26dad] | 195 | {
|
---|
[8ebe212] | 196 | for (ofw_tree_node_t *cur = node->peer; cur; cur = cur->peer) {
|
---|
[e731b0d] | 197 | ofw_tree_property_t *prop =
|
---|
| 198 | ofw_tree_getprop(cur, "device_type");
|
---|
[a35b458] | 199 |
|
---|
[e731b0d] | 200 | if ((!prop) || (!prop->value))
|
---|
[45b26dad] | 201 | continue;
|
---|
[a35b458] | 202 |
|
---|
[e731b0d] | 203 | if (str_cmp(prop->value, dtype) == 0)
|
---|
[45b26dad] | 204 | return cur;
|
---|
| 205 | }
|
---|
[a35b458] | 206 |
|
---|
[45b26dad] | 207 | return NULL;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[965dc18] | 210 | /** Lookup first peer of given name.
|
---|
| 211 | *
|
---|
[e731b0d] | 212 | * @param node Node whose peer is being looked up.
|
---|
| 213 | * @param name Name of the child being looked up.
|
---|
| 214 | *
|
---|
| 215 | * @return NULL if there is no such peer or pointer to the matching
|
---|
| 216 | * peer node.
|
---|
[965dc18] | 217 | *
|
---|
| 218 | */
|
---|
[e731b0d] | 219 | ofw_tree_node_t *ofw_tree_find_peer_by_name(ofw_tree_node_t *node,
|
---|
| 220 | const char *name)
|
---|
[965dc18] | 221 | {
|
---|
[8ebe212] | 222 | for (ofw_tree_node_t *cur = node->peer; cur; cur = cur->peer) {
|
---|
[8b9a443] | 223 | ofw_tree_property_t *prop =
|
---|
| 224 | ofw_tree_getprop(cur, "name");
|
---|
[a35b458] | 225 |
|
---|
[e731b0d] | 226 | if ((!prop) || (!prop->value))
|
---|
[965dc18] | 227 | continue;
|
---|
[a35b458] | 228 |
|
---|
[b60c582] | 229 | if (str_cmp(prop->value, name) == 0)
|
---|
[965dc18] | 230 | return cur;
|
---|
| 231 | }
|
---|
[a35b458] | 232 |
|
---|
[965dc18] | 233 | return NULL;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[16529d5] | 236 | /** Lookup OpenFirmware node by its path.
|
---|
| 237 | *
|
---|
[e731b0d] | 238 | * @param path Path to the node.
|
---|
| 239 | *
|
---|
| 240 | * @return NULL if there is no such node or pointer to the leaf
|
---|
| 241 | * node.
|
---|
[16529d5] | 242 | *
|
---|
| 243 | */
|
---|
| 244 | ofw_tree_node_t *ofw_tree_lookup(const char *path)
|
---|
| 245 | {
|
---|
[e731b0d] | 246 | if (path[0] != '/')
|
---|
| 247 | return NULL;
|
---|
[a35b458] | 248 |
|
---|
[16529d5] | 249 | ofw_tree_node_t *node = ofw_root;
|
---|
[98000fb] | 250 | size_t j;
|
---|
[a35b458] | 251 |
|
---|
[8b9a443] | 252 | for (size_t i = 1; (i < str_size(path)) && (node); i = j + 1) {
|
---|
[84239b1] | 253 | j = i;
|
---|
| 254 | while (j < str_size(path) && path[j] != '/')
|
---|
| 255 | j++;
|
---|
[a35b458] | 256 |
|
---|
[b60c582] | 257 | /* Skip extra slashes */
|
---|
| 258 | if (i == j)
|
---|
[16529d5] | 259 | continue;
|
---|
[a35b458] | 260 |
|
---|
[e731b0d] | 261 | char buf[NAME_BUF_LEN + 1];
|
---|
[16529d5] | 262 | memcpy(buf, &path[i], j - i);
|
---|
[b60c582] | 263 | buf[j - i] = 0;
|
---|
[16529d5] | 264 | node = ofw_tree_find_child(node, buf);
|
---|
| 265 | }
|
---|
[a35b458] | 266 |
|
---|
[16529d5] | 267 | return node;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[e731b0d] | 270 | /** Walk the OpenFirmware device subtree rooted in a node.
|
---|
[2f8f480] | 271 | *
|
---|
| 272 | * Child nodes are processed recursively and peer nodes are processed
|
---|
| 273 | * iteratively in order to avoid stack overflow.
|
---|
[16529d5] | 274 | *
|
---|
[e731b0d] | 275 | * @param node Root of the subtree.
|
---|
| 276 | * @param dtype Device type to look for.
|
---|
| 277 | * @param walker Routine to be invoked on found device.
|
---|
| 278 | * @param arg User argument to the walker.
|
---|
| 279 | *
|
---|
| 280 | * @return True if the walk should continue.
|
---|
| 281 | *
|
---|
[16529d5] | 282 | */
|
---|
[e731b0d] | 283 | static bool ofw_tree_walk_by_device_type_internal(ofw_tree_node_t *node,
|
---|
| 284 | const char *dtype, ofw_tree_walker_t walker, void *arg)
|
---|
[16529d5] | 285 | {
|
---|
[8ebe212] | 286 | for (ofw_tree_node_t *cur = node; cur; cur = cur->peer) {
|
---|
[e731b0d] | 287 | ofw_tree_property_t *prop =
|
---|
| 288 | ofw_tree_getprop(cur, "device_type");
|
---|
[a35b458] | 289 |
|
---|
[e731b0d] | 290 | if ((prop) && (prop->value) && (str_cmp(prop->value, dtype) == 0)) {
|
---|
| 291 | bool ret = walker(cur, arg);
|
---|
| 292 | if (!ret)
|
---|
| 293 | return false;
|
---|
| 294 | }
|
---|
[a35b458] | 295 |
|
---|
[e731b0d] | 296 | if (cur->child) {
|
---|
| 297 | bool ret =
|
---|
| 298 | ofw_tree_walk_by_device_type_internal(cur->child, dtype, walker, arg);
|
---|
| 299 | if (!ret)
|
---|
| 300 | return false;
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
[a35b458] | 303 |
|
---|
[e731b0d] | 304 | return true;
|
---|
| 305 | }
|
---|
[1b43a04] | 306 |
|
---|
[e731b0d] | 307 | /** Walk the OpenFirmware device tree and find devices by type.
|
---|
| 308 | *
|
---|
| 309 | * Walk the whole OpenFirmware device tree and if any node has
|
---|
| 310 | * the property "device_type" equal to dtype, run a walker on it.
|
---|
| 311 | * If the walker returns false, the walk does not continue.
|
---|
| 312 | *
|
---|
| 313 | * @param dtype Device type to look for.
|
---|
| 314 | * @param walker Routine to be invoked on found device.
|
---|
| 315 | * @param arg User argument to the walker.
|
---|
| 316 | *
|
---|
| 317 | */
|
---|
| 318 | void ofw_tree_walk_by_device_type(const char *dtype, ofw_tree_walker_t walker,
|
---|
| 319 | void *arg)
|
---|
| 320 | {
|
---|
| 321 | (void) ofw_tree_walk_by_device_type_internal(ofw_root, dtype, walker, arg);
|
---|
| 322 | }
|
---|
[1b43a04] | 323 |
|
---|
[3113d47] | 324 | /** Get OpenFirmware node properties.
|
---|
| 325 | *
|
---|
| 326 | * @param item Sysinfo item (unused).
|
---|
| 327 | * @param size Size of the returned data.
|
---|
| 328 | * @param dry_run Do not get the data, just calculate the size.
|
---|
| 329 | * @param data OpenFirmware node.
|
---|
| 330 | *
|
---|
| 331 | * @return Data containing a serialized dump of all node
|
---|
| 332 | * properties. If the return value is not NULL, it
|
---|
| 333 | * should be freed in the context of the sysinfo request.
|
---|
| 334 | *
|
---|
| 335 | */
|
---|
| 336 | static void *ofw_sysinfo_properties(struct sysinfo_item *item, size_t *size,
|
---|
| 337 | bool dry_run, void *data)
|
---|
| 338 | {
|
---|
| 339 | ofw_tree_node_t *node = (ofw_tree_node_t *) data;
|
---|
[a35b458] | 340 |
|
---|
[3113d47] | 341 | /* Compute serialized data size */
|
---|
| 342 | *size = 0;
|
---|
| 343 | for (size_t i = 0; i < node->properties; i++)
|
---|
| 344 | *size += str_size(node->property[i].name) + 1 +
|
---|
| 345 | sizeof(node->property[i].size) + node->property[i].size;
|
---|
[a35b458] | 346 |
|
---|
[3113d47] | 347 | if (dry_run)
|
---|
| 348 | return NULL;
|
---|
[a35b458] | 349 |
|
---|
[11b285d] | 350 | void *dump = malloc(*size);
|
---|
[3113d47] | 351 | if (dump == NULL) {
|
---|
| 352 | *size = 0;
|
---|
| 353 | return NULL;
|
---|
| 354 | }
|
---|
[a35b458] | 355 |
|
---|
[3113d47] | 356 | /* Serialize the data */
|
---|
| 357 | size_t pos = 0;
|
---|
| 358 | for (size_t i = 0; i < node->properties; i++) {
|
---|
| 359 | /* Property name */
|
---|
| 360 | str_cpy(dump + pos, *size - pos, node->property[i].name);
|
---|
| 361 | pos += str_size(node->property[i].name) + 1;
|
---|
[a35b458] | 362 |
|
---|
[3113d47] | 363 | /* Value size */
|
---|
| 364 | memcpy(dump + pos, &node->property[i].size,
|
---|
| 365 | sizeof(node->property[i].size));
|
---|
| 366 | pos += sizeof(node->property[i].size);
|
---|
[a35b458] | 367 |
|
---|
[3113d47] | 368 | /* Value */
|
---|
| 369 | memcpy(dump + pos, node->property[i].value,
|
---|
| 370 | node->property[i].size);
|
---|
| 371 | pos += node->property[i].size;
|
---|
| 372 | }
|
---|
[a35b458] | 373 |
|
---|
[3113d47] | 374 | return ((void *) dump);
|
---|
| 375 | }
|
---|
| 376 |
|
---|
[de3db94a] | 377 | /** Map OpenFirmware device subtree rooted in a node into sysinfo.
|
---|
| 378 | *
|
---|
| 379 | * Child nodes are processed recursively and peer nodes are processed
|
---|
| 380 | * iteratively in order to avoid stack overflow.
|
---|
| 381 | *
|
---|
| 382 | * @param node Root of the subtree.
|
---|
| 383 | * @param path Current path, NULL for the very root of the entire tree.
|
---|
| 384 | *
|
---|
| 385 | */
|
---|
| 386 | static void ofw_tree_node_sysinfo(ofw_tree_node_t *node, const char *path)
|
---|
| 387 | {
|
---|
[4f3aa76] | 388 | char *cur_path = malloc(PATH_MAX_LEN);
|
---|
| 389 | if (!cur_path)
|
---|
| 390 | panic("Not enough memory to process OFW tree.");
|
---|
[a35b458] | 391 |
|
---|
[8ebe212] | 392 | for (ofw_tree_node_t *cur = node; cur; cur = cur->peer) {
|
---|
[de3db94a] | 393 | if ((cur->parent) && (path))
|
---|
| 394 | snprintf(cur_path, PATH_MAX_LEN, "%s.%s", path, cur->da_name);
|
---|
[063a3647] | 395 | else if (!str_size(cur->da_name))
|
---|
| 396 | snprintf(cur_path, PATH_MAX_LEN, "firmware.ofw");
|
---|
[de3db94a] | 397 | else
|
---|
| 398 | snprintf(cur_path, PATH_MAX_LEN, "firmware.%s", cur->da_name);
|
---|
[a35b458] | 399 |
|
---|
[3113d47] | 400 | sysinfo_set_item_gen_data(cur_path, NULL, ofw_sysinfo_properties,
|
---|
| 401 | (void *) cur);
|
---|
[a35b458] | 402 |
|
---|
[de3db94a] | 403 | if (cur->child)
|
---|
| 404 | ofw_tree_node_sysinfo(cur->child, cur_path);
|
---|
| 405 | }
|
---|
[a35b458] | 406 |
|
---|
[de3db94a] | 407 | free(cur_path);
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | /** Map the OpenFirmware device tree into sysinfo. */
|
---|
| 411 | void ofw_sysinfo_map(void)
|
---|
| 412 | {
|
---|
| 413 | ofw_tree_node_sysinfo(ofw_root, NULL);
|
---|
| 414 | }
|
---|
| 415 |
|
---|
[61e90dd] | 416 | /** @}
|
---|
| 417 | */
|
---|