[3abe07f5] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[3abe07f5] | 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>
|
---|
[e731b0d] | 31 | #include <ofwarch.h>
|
---|
[3abe07f5] | 32 | #include <types.h>
|
---|
| 33 | #include <string.h>
|
---|
[61e90dd] | 34 | #include <balloc.h>
|
---|
[16529d5] | 35 | #include <asm.h>
|
---|
[e731b0d] | 36 | #include <memstr.h>
|
---|
[16529d5] | 37 |
|
---|
[3abe07f5] | 38 | static ofw_tree_node_t *ofw_tree_node_alloc(void)
|
---|
| 39 | {
|
---|
[61e90dd] | 40 | return balloc(sizeof(ofw_tree_node_t), sizeof(ofw_tree_node_t));
|
---|
[3abe07f5] | 41 | }
|
---|
| 42 |
|
---|
| 43 | static ofw_tree_property_t *ofw_tree_properties_alloc(unsigned count)
|
---|
| 44 | {
|
---|
[8ea0308] | 45 | return balloc(count * sizeof(ofw_tree_property_t),
|
---|
| 46 | sizeof(ofw_tree_property_t));
|
---|
[3abe07f5] | 47 | }
|
---|
| 48 |
|
---|
[8ea0308] | 49 | static void *ofw_tree_space_alloc(size_t size)
|
---|
[3abe07f5] | 50 | {
|
---|
[28ecadb] | 51 | /*
|
---|
| 52 | * What we do here is a nasty hack :-)
|
---|
| 53 | * Problem: string property values that are allocated via this
|
---|
| 54 | * function typically do not contain the trailing '\0'. This
|
---|
| 55 | * is very uncomfortable for kernel, which is supposed to deal
|
---|
| 56 | * with the properties.
|
---|
| 57 | * Solution: when allocating space via this function, we always
|
---|
| 58 | * allocate space for the extra '\0' character that we store
|
---|
| 59 | * behind the requested memory.
|
---|
| 60 | */
|
---|
[e731b0d] | 61 | char *addr = balloc(size + 1, size);
|
---|
[28ecadb] | 62 | if (addr)
|
---|
| 63 | addr[size] = '\0';
|
---|
[e731b0d] | 64 |
|
---|
[28ecadb] | 65 | return addr;
|
---|
[3abe07f5] | 66 | }
|
---|
| 67 |
|
---|
[8ea0308] | 68 | /** Transfer information from one OpenFirmware node into its memory
|
---|
| 69 | * representation.
|
---|
[3abe07f5] | 70 | *
|
---|
[8ea0308] | 71 | * Transfer entire information from the OpenFirmware device tree 'current' node
|
---|
| 72 | * to its memory representation in 'current_node'. This function recursively
|
---|
| 73 | * processes all node's children. Node's peers are processed iteratively in
|
---|
| 74 | * order to prevent stack from overflowing.
|
---|
[3abe07f5] | 75 | *
|
---|
[e731b0d] | 76 | * @param current_node Pointer to uninitialized ofw_tree_node structure that
|
---|
| 77 | * will become the memory represenation of 'current'.
|
---|
| 78 | * @param parent_node Parent ofw_tree_node structure or NULL in case of root
|
---|
| 79 | * node.
|
---|
| 80 | * @param current OpenFirmware phandle to the current device tree node.
|
---|
| 81 | *
|
---|
[3abe07f5] | 82 | */
|
---|
| 83 | static void ofw_tree_node_process(ofw_tree_node_t *current_node,
|
---|
[8ea0308] | 84 | ofw_tree_node_t *parent_node, phandle current)
|
---|
[3abe07f5] | 85 | {
|
---|
[b63a7cc] | 86 | while (current_node) {
|
---|
| 87 | /*
|
---|
| 88 | * Initialize node.
|
---|
| 89 | */
|
---|
[e731b0d] | 90 | current_node->parent = (ofw_tree_node_t *) balloc_rebase(parent_node);
|
---|
[b63a7cc] | 91 | current_node->peer = NULL;
|
---|
| 92 | current_node->child = NULL;
|
---|
| 93 | current_node->node_handle = current;
|
---|
| 94 | current_node->properties = 0;
|
---|
| 95 | current_node->property = NULL;
|
---|
| 96 | current_node->device = NULL;
|
---|
[e731b0d] | 97 |
|
---|
[b63a7cc] | 98 | /*
|
---|
| 99 | * Get the disambigued name.
|
---|
| 100 | */
|
---|
[e0565005] | 101 | static char path[OFW_TREE_PATH_MAX_LEN + 1];
|
---|
| 102 | size_t len = ofw_package_to_path(current, path, OFW_TREE_PATH_MAX_LEN);
|
---|
[b63a7cc] | 103 | if (len == -1)
|
---|
| 104 | return;
|
---|
[e731b0d] | 105 |
|
---|
[b63a7cc] | 106 | path[len] = '\0';
|
---|
[e731b0d] | 107 |
|
---|
| 108 | /* Find last slash */
|
---|
| 109 | int i;
|
---|
| 110 | for (i = len - 1; (i >= 0) && (path[i] != '/'); i--);
|
---|
| 111 |
|
---|
| 112 | /* Do not include the slash */
|
---|
| 113 | i++;
|
---|
[b63a7cc] | 114 | len -= i;
|
---|
[e731b0d] | 115 |
|
---|
| 116 | /* Add space for trailing '\0' */
|
---|
| 117 | char *da_name = ofw_tree_space_alloc(len + 1);
|
---|
| 118 | if (!da_name)
|
---|
[b63a7cc] | 119 | return;
|
---|
[e731b0d] | 120 |
|
---|
| 121 | memcpy(da_name, &path[i], len);
|
---|
| 122 | da_name[len] = '\0';
|
---|
| 123 | current_node->da_name = (char *) balloc_rebase(da_name);
|
---|
| 124 |
|
---|
[b63a7cc] | 125 | /*
|
---|
| 126 | * Recursively process the potential child node.
|
---|
| 127 | */
|
---|
[e731b0d] | 128 | phandle child = ofw_get_child_node(current);
|
---|
| 129 | if ((child != 0) && (child != -1)) {
|
---|
| 130 | ofw_tree_node_t *child_node = ofw_tree_node_alloc();
|
---|
[b63a7cc] | 131 | if (child_node) {
|
---|
[8ea0308] | 132 | ofw_tree_node_process(child_node, current_node,
|
---|
| 133 | child);
|
---|
[e731b0d] | 134 | current_node->child =
|
---|
| 135 | (ofw_tree_node_t *) balloc_rebase(child_node);
|
---|
[b63a7cc] | 136 | }
|
---|
[3abe07f5] | 137 | }
|
---|
[e731b0d] | 138 |
|
---|
[b63a7cc] | 139 | /*
|
---|
| 140 | * Count properties.
|
---|
| 141 | */
|
---|
[e731b0d] | 142 | static char name[OFW_TREE_PROPERTY_MAX_NAMELEN];
|
---|
| 143 | static char name2[OFW_TREE_PROPERTY_MAX_NAMELEN];
|
---|
[b63a7cc] | 144 | name[0] = '\0';
|
---|
[0832b4d] | 145 | while (ofw_next_property(current, name, name2) == 1) {
|
---|
[b63a7cc] | 146 | current_node->properties++;
|
---|
[0832b4d] | 147 | memcpy(name, name2, OFW_TREE_PROPERTY_MAX_NAMELEN);
|
---|
| 148 | }
|
---|
[e731b0d] | 149 |
|
---|
[b63a7cc] | 150 | if (!current_node->properties)
|
---|
| 151 | return;
|
---|
[e731b0d] | 152 |
|
---|
[b63a7cc] | 153 | /*
|
---|
| 154 | * Copy properties.
|
---|
| 155 | */
|
---|
[e731b0d] | 156 | ofw_tree_property_t *property =
|
---|
[8ea0308] | 157 | ofw_tree_properties_alloc(current_node->properties);
|
---|
[e731b0d] | 158 | if (!property)
|
---|
[b63a7cc] | 159 | return;
|
---|
[3abe07f5] | 160 |
|
---|
[b63a7cc] | 161 | name[0] = '\0';
|
---|
[0832b4d] | 162 | for (i = 0; ofw_next_property(current, name, name2) == 1; i++) {
|
---|
[b63a7cc] | 163 | if (i == current_node->properties)
|
---|
| 164 | break;
|
---|
[e731b0d] | 165 |
|
---|
[0832b4d] | 166 | memcpy(name, name2, OFW_TREE_PROPERTY_MAX_NAMELEN);
|
---|
[e731b0d] | 167 | memcpy(property[i].name, name, OFW_TREE_PROPERTY_MAX_NAMELEN);
|
---|
[e0565005] | 168 | property[i].name[OFW_TREE_PROPERTY_MAX_NAMELEN - 1] = '\0';
|
---|
[e731b0d] | 169 |
|
---|
| 170 | size_t size = ofw_get_proplen(current, name);
|
---|
| 171 | property[i].size = size;
|
---|
[3abe07f5] | 172 |
|
---|
[e731b0d] | 173 | if (size) {
|
---|
| 174 | void *buf = ofw_tree_space_alloc(size);
|
---|
| 175 | if (buf) {
|
---|
[b63a7cc] | 176 | /*
|
---|
| 177 | * Copy property value to memory node.
|
---|
| 178 | */
|
---|
[e731b0d] | 179 | (void) ofw_get_property(current, name, buf, size);
|
---|
| 180 | property[i].value = balloc_rebase(buf);
|
---|
[b63a7cc] | 181 | }
|
---|
[e731b0d] | 182 | } else
|
---|
| 183 | property[i].value = NULL;
|
---|
[b63a7cc] | 184 | }
|
---|
[e731b0d] | 185 |
|
---|
[8ea0308] | 186 | /* Just in case we ran out of memory. */
|
---|
| 187 | current_node->properties = i;
|
---|
[e731b0d] | 188 | current_node->property = (ofw_tree_property_t *) balloc_rebase(property);
|
---|
| 189 |
|
---|
| 190 |
|
---|
[b63a7cc] | 191 | /*
|
---|
| 192 | * Iteratively process the next peer node.
|
---|
| 193 | * Note that recursion is a bad idea here.
|
---|
| 194 | * Due to the topology of the OpenFirmware device tree,
|
---|
| 195 | * the nesting of peer nodes could be to wide and the
|
---|
| 196 | * risk of overflowing the stack is too real.
|
---|
| 197 | */
|
---|
[e731b0d] | 198 | phandle peer = ofw_get_peer_node(current);
|
---|
| 199 | if ((peer != 0) && (peer != -1)) {
|
---|
| 200 | ofw_tree_node_t *peer_node = ofw_tree_node_alloc();
|
---|
| 201 | if (peer_node) {
|
---|
| 202 | current_node->peer = (ofw_tree_node_t *) balloc_rebase(peer_node);
|
---|
[b63a7cc] | 203 | current_node = peer_node;
|
---|
| 204 | current = peer;
|
---|
[3abe07f5] | 205 | /*
|
---|
[b63a7cc] | 206 | * Process the peer in next iteration.
|
---|
[3abe07f5] | 207 | */
|
---|
[b63a7cc] | 208 | continue;
|
---|
[3abe07f5] | 209 | }
|
---|
| 210 | }
|
---|
[e731b0d] | 211 |
|
---|
[b63a7cc] | 212 | /*
|
---|
| 213 | * No more peers on this level.
|
---|
| 214 | */
|
---|
| 215 | break;
|
---|
[3abe07f5] | 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /** Construct memory representation of OpenFirmware device tree.
|
---|
| 220 | *
|
---|
[e731b0d] | 221 | * @return NULL on failure or kernel pointer to the root node.
|
---|
| 222 | *
|
---|
[3abe07f5] | 223 | */
|
---|
| 224 | ofw_tree_node_t *ofw_tree_build(void)
|
---|
| 225 | {
|
---|
[e731b0d] | 226 | ofw_tree_node_t *root = ofw_tree_node_alloc();
|
---|
[3abe07f5] | 227 | if (root)
|
---|
| 228 | ofw_tree_node_process(root, NULL, ofw_root);
|
---|
[e731b0d] | 229 |
|
---|
[965dc18] | 230 | /*
|
---|
| 231 | * The firmware client interface does not automatically include the
|
---|
| 232 | * "ssm" node in the list of children of "/". A nasty yet working
|
---|
| 233 | * solution is to explicitly stick "ssm" to the OFW tree.
|
---|
| 234 | */
|
---|
[e731b0d] | 235 | phandle ssm_node = ofw_find_device("/ssm@0,0");
|
---|
[965dc18] | 236 | if (ssm_node != -1) {
|
---|
[e731b0d] | 237 | ofw_tree_node_t *ssm = ofw_tree_node_alloc();
|
---|
[965dc18] | 238 | if (ssm) {
|
---|
[8ea0308] | 239 | ofw_tree_node_process(ssm, root,
|
---|
| 240 | ofw_find_device("/ssm@0,0"));
|
---|
[965dc18] | 241 | ssm->peer = root->child;
|
---|
[e731b0d] | 242 | root->child = (ofw_tree_node_t *) balloc_rebase(ssm);
|
---|
[965dc18] | 243 | }
|
---|
| 244 | }
|
---|
[3abe07f5] | 245 |
|
---|
[e731b0d] | 246 | return (ofw_tree_node_t *) balloc_rebase(root);
|
---|
[3abe07f5] | 247 | }
|
---|