[3abe07f5] | 1 | /*
|
---|
| 2 | * Copyright (C) 2006 Jakub Jermar
|
---|
| 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 | #include <ofw_tree.h>
|
---|
| 30 | #include <ofw.h>
|
---|
| 31 | #include <types.h>
|
---|
| 32 | #include <string.h>
|
---|
| 33 |
|
---|
| 34 | static ofw_tree_node_t *ofw_tree_node_alloc(void)
|
---|
| 35 | {
|
---|
| 36 | return NULL;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | static ofw_tree_property_t *ofw_tree_properties_alloc(unsigned count)
|
---|
| 40 | {
|
---|
| 41 | return NULL;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | static void * ofw_tree_space_alloc(size_t size)
|
---|
| 45 | {
|
---|
| 46 | return NULL;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /** Transfer information from one OpenFirmware node into its memory representation.
|
---|
| 50 | *
|
---|
| 51 | * Transfer entire information from the OpenFirmware device tree 'current' node to
|
---|
| 52 | * its memory representation in 'current_node'. This function recursively processes
|
---|
| 53 | * all node's peers and children.
|
---|
| 54 | *
|
---|
| 55 | * @param current_node Pointer to uninitialized ofw_tree_node structure that will
|
---|
| 56 | * become the memory represenation of 'current'.
|
---|
| 57 | * @param parent_node Parent ofw_tree_node structure or NULL in case of root node.
|
---|
| 58 | * @param current OpenFirmware phandle to the current device tree node.
|
---|
| 59 | */
|
---|
| 60 | static void ofw_tree_node_process(ofw_tree_node_t *current_node,
|
---|
| 61 | ofw_tree_node_t *parent_node, phandle current)
|
---|
| 62 | {
|
---|
| 63 | phandle peer;
|
---|
| 64 | phandle child;
|
---|
| 65 | unsigned properties = 0;
|
---|
| 66 | char name[OFW_TREE_PROPERTY_MAX_NAMELEN];
|
---|
| 67 |
|
---|
| 68 | /*
|
---|
| 69 | * Initialize node.
|
---|
| 70 | */
|
---|
| 71 | current_node->parent = parent_node;
|
---|
| 72 | current_node->peer = NULL;
|
---|
| 73 | current_node->child = NULL;
|
---|
| 74 | current_node->properties = 0;
|
---|
| 75 | current_node->property = NULL;
|
---|
| 76 |
|
---|
| 77 | /*
|
---|
| 78 | * Recursively process the potential peer node.
|
---|
| 79 | */
|
---|
| 80 | peer = ofw_get_peer_node(current);
|
---|
| 81 | if (peer != 0 && peer != -1) {
|
---|
| 82 | ofw_tree_node_t *peer_node;
|
---|
| 83 |
|
---|
| 84 | peer_node = ofw_tree_node_alloc();
|
---|
| 85 | if (peer_node) {
|
---|
| 86 | ofw_tree_node_process(peer_node, current_node, peer);
|
---|
| 87 | current_node->peer = peer_node;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /*
|
---|
| 92 | * Recursively process the potential child node.
|
---|
| 93 | */
|
---|
| 94 | child = ofw_get_child_node(current);
|
---|
| 95 | if (child != 0 && child != -1) {
|
---|
| 96 | ofw_tree_node_t *child_node;
|
---|
| 97 |
|
---|
| 98 | child_node = ofw_tree_node_alloc();
|
---|
| 99 | if (child_node) {
|
---|
| 100 | ofw_tree_node_process(child_node, current_node, child);
|
---|
| 101 | current_node->child = child_node;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /*
|
---|
| 106 | * Count properties.
|
---|
| 107 | */
|
---|
| 108 | name[0] = '\0';
|
---|
| 109 | while (ofw_next_property(current, name, name) == 1)
|
---|
| 110 | current_node->properties++;
|
---|
| 111 |
|
---|
| 112 | if (!current_node->properties)
|
---|
| 113 | return;
|
---|
| 114 |
|
---|
| 115 | /*
|
---|
| 116 | * Copy properties.
|
---|
| 117 | */
|
---|
| 118 | current_node->property = ofw_tree_properties_alloc(current_node->properties);
|
---|
| 119 | if (!current_node->property)
|
---|
| 120 | return;
|
---|
| 121 |
|
---|
| 122 | int i = 0;
|
---|
| 123 |
|
---|
| 124 | name[0] = '\0';
|
---|
| 125 | for (i = 0; ofw_next_property(current, name, name) == 1; i++) {
|
---|
| 126 | size_t size;
|
---|
| 127 |
|
---|
| 128 | if (i == current_node->properties)
|
---|
| 129 | break;
|
---|
| 130 |
|
---|
| 131 | strncpy(current_node->property[i].name, name, sizeof(name));
|
---|
| 132 |
|
---|
| 133 | size = ofw_get_proplen(current, name);
|
---|
| 134 | current_node->property[i].size = size;
|
---|
| 135 | if (size) {
|
---|
| 136 | void *buf;
|
---|
| 137 |
|
---|
| 138 | buf = ofw_tree_space_alloc(size);
|
---|
| 139 | if (current_node->property[i].value = buf) {
|
---|
| 140 | /*
|
---|
| 141 | * Copy property value to memory node.
|
---|
| 142 | */
|
---|
| 143 | (void) ofw_get_property(current, name, buf, size);
|
---|
| 144 | }
|
---|
| 145 | } else {
|
---|
| 146 | current_node->property[i].value = NULL;
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | current_node->properties = i; /* Just in case we ran out of memory. */
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /** Construct memory representation of OpenFirmware device tree.
|
---|
| 154 | *
|
---|
| 155 | * @return NULL on failure or pointer to the root node.
|
---|
| 156 | */
|
---|
| 157 | ofw_tree_node_t *ofw_tree_build(void)
|
---|
| 158 | {
|
---|
| 159 | ofw_tree_node_t *root;
|
---|
| 160 |
|
---|
| 161 | root = ofw_tree_node_alloc();
|
---|
| 162 | if (root)
|
---|
| 163 | ofw_tree_node_process(root, NULL, ofw_root);
|
---|
| 164 |
|
---|
| 165 | return root;
|
---|
| 166 | }
|
---|