Rev 1894 | Rev 1896 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1894 | Rev 1895 | ||
---|---|---|---|
Line 33... | Line 33... | ||
33 | * @file |
33 | * @file |
34 | * @brief OpenFirmware device tree navigation. |
34 | * @brief OpenFirmware device tree navigation. |
35 | * |
35 | * |
36 | */ |
36 | */ |
37 | 37 | ||
- | 38 | #include <genarch/ofw/ofw_tree.h> |
|
- | 39 | #include <arch/memstr.h> |
|
- | 40 | #include <func.h> |
|
- | 41 | #include <print.h> |
|
- | 42 | #include <panic.h> |
|
- | 43 | ||
- | 44 | #define PATH_MAX_LEN 80 |
|
- | 45 | #define NAME_BUF_LEN 50 |
|
- | 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 | ||
- | 54 | /** Return value of the 'name' property. |
|
- | 55 | * |
|
- | 56 | * @param node Node of interest. |
|
- | 57 | * |
|
- | 58 | * @return Value of the 'name' property belonging to the node. |
|
- | 59 | */ |
|
- | 60 | const char *ofw_tree_node_name(const ofw_tree_node_t *node) |
|
- | 61 | { |
|
- | 62 | int i; |
|
- | 63 | ||
- | 64 | for (i = 0; i < node->properties; i++) { |
|
- | 65 | if (strncmp(node->property[i].name, "name", strlen("name")) == 0) { |
|
- | 66 | if (node->property[i].size < 2) |
|
- | 67 | panic("Invalid name property.\n"); |
|
- | 68 | return node->property[i].value; |
|
- | 69 | } |
|
- | 70 | } |
|
- | 71 | ||
- | 72 | panic("Node without name property.\n"); |
|
- | 73 | } |
|
- | 74 | ||
- | 75 | /** Lookup child of given name. |
|
- | 76 | * |
|
- | 77 | * @param node Node whose child is being looked up. |
|
- | 78 | * @param da_name Disambigued name of the child being looked up. |
|
- | 79 | * |
|
- | 80 | * @return NULL if there is no such child or pointer to the matching child node. |
|
- | 81 | */ |
|
- | 82 | static ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node, const char *da_name) |
|
- | 83 | { |
|
- | 84 | ofw_tree_node_t *cur; |
|
- | 85 | ||
- | 86 | for (cur = node->child; cur; cur = cur->peer) { |
|
- | 87 | if (strncmp(cur->da_name, da_name, strlen(da_name)) == 0) |
|
- | 88 | return cur; |
|
- | 89 | } |
|
- | 90 | ||
- | 91 | return NULL; |
|
- | 92 | } |
|
- | 93 | ||
- | 94 | /** Lookup OpenFirmware node by its path. |
|
- | 95 | * |
|
- | 96 | * @param path Path to the node. |
|
- | 97 | * |
|
- | 98 | * @return NULL if there is no such node or pointer to the leaf node. |
|
- | 99 | */ |
|
- | 100 | ofw_tree_node_t *ofw_tree_lookup(const char *path) |
|
- | 101 | { |
|
- | 102 | char buf[NAME_BUF_LEN+1]; |
|
- | 103 | ofw_tree_node_t *node = ofw_root; |
|
- | 104 | index_t i, j; |
|
- | 105 | ||
- | 106 | if (path[0] != '/') |
|
- | 107 | return NULL; |
|
- | 108 | ||
- | 109 | for (i = 1; i < strlen(path) && node; i = j + 1) { |
|
- | 110 | for (j = i; j < strlen(path) && path[j] != '/'; j++) |
|
- | 111 | ; |
|
- | 112 | if (i == j) /* skip extra slashes */ |
|
- | 113 | continue; |
|
- | 114 | ||
- | 115 | memcpy(buf, &path[i], j - i); |
|
- | 116 | buf[j - i] = '\0'; |
|
- | 117 | node = ofw_tree_find_child(node, buf); |
|
- | 118 | } |
|
- | 119 | ||
- | 120 | return node; |
|
- | 121 | } |
|
- | 122 | ||
- | 123 | /** Recursively print subtree rooted in a node. |
|
- | 124 | * |
|
- | 125 | * @param node Root of the subtree. |
|
- | 126 | * @param path Current path, NULL for the very root of the entire tree. |
|
- | 127 | */ |
|
- | 128 | static void ofw_tree_node_print(const ofw_tree_node_t *node, const char *path) |
|
- | 129 | { |
|
- | 130 | char p[PATH_MAX_LEN]; |
|
- | 131 | ||
- | 132 | if (node->parent) { |
|
- | 133 | snprintf(p, PATH_MAX_LEN, "%s/%s", path, node->da_name); |
|
- | 134 | printf("%s\n", p); |
|
- | 135 | } else { |
|
- | 136 | snprintf(p, PATH_MAX_LEN, "%s", node->da_name); |
|
- | 137 | printf("/\n"); |
|
- | 138 | } |
|
- | 139 | ||
- | 140 | if (node->child) |
|
- | 141 | ofw_tree_node_print(node->child, p); |
|
- | 142 | ||
- | 143 | if (node->peer) |
|
- | 144 | ofw_tree_node_print(node->peer, path); |
|
- | 145 | } |
|
- | 146 | ||
- | 147 | /** Print the structure of the OpenFirmware device tree. */ |
|
- | 148 | void ofw_tree_print(void) |
|
- | 149 | { |
|
- | 150 | ofw_tree_node_print(ofw_root, NULL); |
|
- | 151 | } |
|
- | 152 | ||
38 | /** @} |
153 | /** @} |
39 | */ |
154 | */ |