2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 1999 by Sun Microsystems, Inc.
2N/A * All rights reserved.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <sys/stat.h>
2N/A#include <fcntl.h>
2N/A#include <unistd.h>
2N/A#include <stdlib.h>
2N/A#include <stdio.h>
2N/A#include <strings.h>
2N/A#include <fcntl.h>
2N/A
2N/A#include <fcode/private.h>
2N/A#include <fcode/log.h>
2N/A
2N/A#include <fcdriver/fcdriver.h>
2N/A
2N/Astatic fc_phandle_t
2N/Afc_nodeop(common_data_t *cdp, fc_phandle_t node, char *svc)
2N/A{
2N/A fc_cell_t hcell;
2N/A int error;
2N/A
2N/A error = fc_run_priv(cdp, svc, 1, 1, fc_phandle2cell(node), &hcell);
2N/A if (error)
2N/A return (NULL);
2N/A return (fc_cell2phandle(hcell));
2N/A}
2N/A
2N/Avoid
2N/Arecurse_tree(fcode_env_t *env, device_t *d,
2N/A void (*fn)(fcode_env_t *, device_t *))
2N/A{
2N/A if (d != NULL) {
2N/A device_t *p;
2N/A
2N/A fn(env, d);
2N/A recurse_tree(env, d->child, fn);
2N/A recurse_tree(env, d->peer, fn);
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Aget_prom_nodeid(fcode_env_t *env, device_t *d)
2N/A{
2N/A common_data_t *cdp = env->private;
2N/A private_data_t *pd = d->private;
2N/A char *name;
2N/A int namelen;
2N/A char *namebuf;
2N/A
2N/A if ((pd != NULL) && (pd->node)) {
2N/A if (os_get_prop_common(cdp, pd->node, "name",
2N/A 0, &namebuf, &namelen))
2N/A namebuf = "<unknown>";
2N/A debug_msg(DEBUG_UPLOAD, "Populated: %s = %p\n", namebuf,
2N/A pd->node);
2N/A return;
2N/A }
2N/A
2N/A name = get_package_name(env, d);
2N/A debug_msg(DEBUG_UPLOAD, "Node %s: %p (%p)\n", name, d, pd);
2N/A if (d->parent) {
2N/A private_data_t *ppd = (private_data_t *) d->parent->private;
2N/A fc_phandle_t thisnode;
2N/A
2N/A if (os_get_prop_common(cdp, ppd->node, "name",
2N/A 0, &namebuf, &namelen))
2N/A namebuf = "<unknown>";
2N/A debug_msg(DEBUG_UPLOAD, "Parent: %p (%p) %s = %p\n", d->parent,
2N/A ppd, namebuf, ppd->node);
2N/A for (thisnode = fc_nodeop(cdp, ppd->node, FC_CHILD_FCODE);
2N/A thisnode != NULL;
2N/A thisnode = fc_nodeop(cdp, thisnode, FC_PEER_FCODE)) {
2N/A int status;
2N/A
2N/A namebuf = "";
2N/A namelen = 0;
2N/A status = os_get_prop_common(cdp, thisnode, "name",
2N/A 0, &namebuf, &namelen);
2N/A debug_msg(DEBUG_UPLOAD, "Lookup: %p name '%s'\n"
2N/A " status: %d", thisnode, namebuf, status);
2N/A if (status == 0 && strcmp(name, namebuf) == 0)
2N/A break;
2N/A }
2N/A if (thisnode) {
2N/A pd = MALLOC(sizeof (private_data_t));
2N/A pd->common = cdp;
2N/A pd->node = thisnode;
2N/A pd->upload = 0;
2N/A d->private = pd;
2N/A add_my_handle(env, pd->node, d);
2N/A install_property_vectors(env, d);
2N/A debug_msg(DEBUG_UPLOAD, "Found: %p\n", thisnode);
2N/A }
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Aupdate_nodeids(fcode_env_t *env)
2N/A{
2N/A /*
2N/A * We scan through the tree looking for nodes that don't have
2N/A * one of my structures attached, and for each of those nodes
2N/A * I attempt to match it with a real firmware node
2N/A */
2N/A recurse_tree(env, env->root_node, get_prom_nodeid);
2N/A}
2N/A
2N/Astatic void
2N/Abuild_nodes(fcode_env_t *env, common_data_t *cdp, fc_phandle_t h)
2N/A{
2N/A char *name;
2N/A int len;
2N/A int n, allocd, depth;
2N/A fc_phandle_t p;
2N/A device_t *current, *attach;
2N/A private_data_t *pd;
2N/A private_data_t **node_array;
2N/A
2N/A /*
2N/A * This is not nice; new_device calls the allocate_phandle
2N/A * routine without exception, we need to disable the allocation
2N/A * while we are building the tree to the attachment point
2N/A * which is why the init_done variable exists.
2N/A */
2N/A cdp->init_done = 0;
2N/A node_array = NULL;
2N/A depth = 0;
2N/A allocd = sizeof (private_data_t *);
2N/A do {
2N/A node_array = REALLOC(node_array, allocd*(depth+1));
2N/A pd = MALLOC(sizeof (private_data_t));
2N/A pd->node = h;
2N/A node_array[depth] = pd;
2N/A name = NULL;
2N/A (void) os_get_prop_common(cdp, pd->node, "name", 0, &name,
2N/A &len);
2N/A if (name)
2N/A debug_msg(DEBUG_UPLOAD, "Node: %p name: '%s'\n", h,
2N/A name);
2N/A else
2N/A log_message(MSG_ERROR, "Node: %p Unnamed node!!\n", h);
2N/A depth++;
2N/A h = fc_nodeop(cdp, h, FC_PARENT);
2N/A } while (h);
2N/A
2N/A for (n = 0; n < (depth-1); n++) {
2N/A new_device(env);
2N/A }
2N/A
2N/A env->attachment_pt = current = attach = env->current_device;
2N/A
2N/A for (n = 0; n < depth; n++) {
2N/A pd = node_array[n];
2N/A pd->common = cdp;
2N/A current->private = pd;
2N/A add_my_handle(env, pd->node, current);
2N/A install_property_vectors(env, current);
2N/A current = current->parent;
2N/A }
2N/A
2N/A for (current = attach; current != NULL; current = current->parent) {
2N/A install_node_data(env, current);
2N/A if (current->parent)
2N/A finish_device(env);
2N/A }
2N/A
2N/A FREE(node_array);
2N/A cdp->init_done = 2;
2N/A update_nodeids(env);
2N/A cdp->init_done = 1;
2N/A cdp->first_node = 1;
2N/A}
2N/A
2N/Avoid
2N/Abuild_tree(fcode_env_t *env)
2N/A{
2N/A common_data_t *cdp = env->private;
2N/A instance_t *ih;
2N/A
2N/A root_node(env);
2N/A ih = open_instance_chain(env, env->current_device, 0);
2N/A MYSELF = ih;
2N/A build_nodes(env, cdp, cdp->attach);
2N/A close_instance_chain(env, ih, 0);
2N/A MYSELF = 0;
2N/A device_end(env);
2N/A}
2N/A
2N/A/*
2N/A * Installs /openprom and /packages nodes and sub-nodes.
2N/A */
2N/Avoid
2N/Ainstall_builtin_nodes(fcode_env_t *env)
2N/A{
2N/A common_data_t *cdp = env->private;
2N/A int saved_first_node;
2N/A int saved_init_done;
2N/A
2N/A if (cdp) {
2N/A saved_first_node = cdp->first_node;
2N/A saved_init_done = cdp->init_done;
2N/A cdp->first_node = 0;
2N/A cdp->init_done = 2;
2N/A install_openprom_nodes(env);
2N/A install_package_nodes(env);
2N/A cdp->first_node = saved_first_node;
2N/A cdp->init_done = saved_init_done;
2N/A }
2N/A}
2N/A
2N/A
2N/A#pragma init(_init)
2N/A
2N/Astatic void
2N/A_init(void)
2N/A{
2N/A fcode_env_t *env = initial_env;
2N/A
2N/A ASSERT(env);
2N/A NOTICE;
2N/A
2N/A FORTH(0, "update-nodes", update_nodeids);
2N/A FORTH(0, "build-tree", build_tree);
2N/A}