#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/systeminfo.h>
#include <sys/utsname.h>
#include <sys/openpromio.h>
#include <libdevinfo.h>
#include "pdevinfo.h"
#include "pdevinfo_sun4u.h"
#include "display.h"
#include "display_sun4u.h"
#include "libprtdiag.h"
int do_devinfo(int syserrlog, char *pgname, int log_flag,
int prt_flag);
static void dump_di_node(Prom_node *pnode, di_node_t di_node);
static Prom_node *walk_di_tree(Sys_tree *tree, Prom_node *root,
di_node_t di_node);
static int match_compatible_name(char *, int, char *);
di_prom_handle_t ph;
extern char *progname;
static Prom_node*
walk_di_tree(Sys_tree *tree, Prom_node *root, di_node_t di_node)
{
di_node_t curnode;
Prom_node *pnode;
char *name, *type, *model, *compatible_array;
int board_node = 0;
int *int_val;
int is_schizo = 0, n_names;
#ifdef DEBUG
int portid;
#endif
if ((pnode = (Prom_node *) malloc(sizeof (struct prom_node))) ==
NULL) {
perror("malloc");
exit(2);
}
pnode->parent = root;
pnode->sibling = NULL;
pnode->child = NULL;
dump_di_node(pnode, di_node);
name = get_node_name(pnode);
type = get_node_type(pnode);
if (type == NULL)
type = "";
model = (char *)get_prop_val(find_prop(pnode, "model"));
if (model == NULL)
model = "";
if ((n_names = di_compatible_names(di_node, &compatible_array)) > 0) {
if (match_compatible_name(compatible_array, n_names,
"pci108e,8001"))
is_schizo = 1;
}
#ifdef DEBUG
if (int_val = (int *)get_prop_val(find_prop(pnode, "portid")))
portid = *int_val;
else if ((strcmp(type, "cpu") == 0) &&
(int_val = (int *)get_prop_val(find_prop(pnode->parent, "portid"))))
portid = *int_val;
else
portid = -1;
if (name != NULL)
printf("name=%s\n", name);
if (type != NULL)
printf("type=%s\n", type);
if (model != NULL)
printf("model=%s\n", model);
printf("portid=%d\n", portid);
#endif
if (name != NULL) {
if (has_board_num(pnode)) {
add_node(tree, pnode);
board_node = 1;
D_PRINTF("\n---\nnodename = %s [ %s ] \n",
di_node_name(di_node), di_devfs_path(di_node));
D_PRINTF("ADDED BOARD name=%s type=%s model=%s "
"portid =%d\n", name, type, model, portid);
} else if ((strcmp(name, FFB_NAME) == 0) ||
(strcmp(type, "cpu") == 0) ||
((strcmp(type, "memory-controller") == 0) &&
(strcmp(name, "ac") != 0)) ||
((strcmp(name, "pci") == 0) &&
(strcmp(model, "SUNW,psycho") == 0)) ||
((strcmp(name, "pci") == 0) &&
(strcmp(model, "SUNW,sabre") == 0)) ||
((strcmp(name, "pci") == 0) && (is_schizo)) ||
(strcmp(name, "counter-timer") == 0) ||
(strcmp(name, "sbus") == 0)) {
add_node(tree, pnode);
board_node = 1;
D_PRINTF("\n---\nnodename = %s [ %s ] \n",
di_node_name(di_node), di_devfs_path(di_node));
D_PRINTF("ADDED BOARD name=%s type=%s model=%s\n",
name, type, model);
}
} else {
D_PRINTF("node not added: type=%s portid =%d\n", type, portid);
}
if (curnode = di_child_node(di_node)) {
pnode->child = walk_di_tree(tree, pnode, curnode);
}
if (curnode = di_sibling_node(di_node)) {
if (board_node) {
return (walk_di_tree(tree, root, curnode));
} else {
pnode->sibling = walk_di_tree(tree, root, curnode);
}
}
if (board_node)
return (NULL);
return (pnode);
}
static void
dump_di_node(Prom_node *pnode, di_node_t di_node)
{
Prop *prop = NULL;
Prop *temp;
di_prop_t di_prop;
di_prom_prop_t p_prop;
int retval = 0;
int i;
pnode->props = NULL;
D_PRINTF("\n\n ------- Dumping devinfo properties for node ------\n");
for (di_prop = di_prop_next(di_node, DI_PROP_NIL);
di_prop != DI_PROP_NIL;
di_prop = di_prop_next(di_node, di_prop)) {
char *di_name;
void *di_data;
int di_ptype;
di_name = di_prop_name(di_prop);
if (di_name == (char *)NULL)
continue;
di_ptype = di_prop_type(di_prop);
D_PRINTF("DEVINFO Properties %s: ", di_name);
switch (di_ptype) {
case DI_PROP_TYPE_INT:
retval = di_prop_lookup_ints(DDI_DEV_T_ANY,
di_node, di_name, (int **)&di_data);
if (retval > 0) {
D_PRINTF("0x%x\n", *(int *)di_data);
}
break;
case DI_PROP_TYPE_STRING:
retval = di_prop_lookup_strings(DDI_DEV_T_ANY,
di_node, di_name, (char **)&di_data);
if (retval > 0) {
D_PRINTF("%s\n", (char *)di_data);
}
break;
case DI_PROP_TYPE_BYTE:
retval = di_prop_lookup_bytes(DDI_DEV_T_ANY,
di_node, di_name, (uchar_t **)&di_data);
if (retval > 0) {
D_PRINTF("%s\n", (char *)di_data);
}
break;
case DI_PROP_TYPE_UNKNOWN:
retval = di_prop_lookup_bytes(DDI_DEV_T_ANY,
di_node, di_name, (uchar_t **)&di_data);
if (retval > 0) {
D_PRINTF("%s\n", (char *)di_data);
}
break;
case DI_PROP_TYPE_BOOLEAN:
di_data = NULL;
retval = 1;
break;
default:
D_PRINTF(" Skipping property\n");
retval = -1;
}
if (retval <= 0)
continue;
if ((temp = (Prop *) malloc(sizeof (Prop))) == NULL) {
perror("malloc");
exit(1);
}
temp->name.val_ptr = (void *)di_name;
temp->name.opp.oprom_array = temp->name.val_ptr;
temp->name.opp.holds_array = 1;
temp->value.val_ptr = (void *)di_data;
if ((di_data != NULL) && (*((char *)di_data) == '\0')) {
for (i = 0; i < OPROM_NODE_SIZE; i++)
temp->value.opp.oprom_node[i] =
*((int *)di_data+i);
temp->value.opp.holds_array = 0;
} else {
temp->value.opp.oprom_array = temp->value.val_ptr;
if (di_ptype == DI_PROP_TYPE_BOOLEAN)
temp->value.opp.holds_array = 0;
else
temp->value.opp.holds_array = 1;
}
temp->size = retval;
if (pnode->props == NULL)
pnode->props = temp;
else if (prop != NULL)
prop->next = temp;
prop = temp;
prop->next = NULL;
}
for (p_prop = di_prom_prop_next(ph, di_node, DI_PROM_PROP_NIL);
p_prop != DI_PROM_PROP_NIL;
p_prop = di_prom_prop_next(ph, di_node, p_prop)) {
char *p_name;
unsigned char *p_data;
p_name = di_prom_prop_name(p_prop);
if (p_name == (char *)NULL)
retval = -1;
else
retval = di_prom_prop_data(p_prop, &p_data);
if (retval <= 0)
continue;
if ((temp = (Prop *) malloc(sizeof (Prop))) == NULL) {
perror("malloc");
exit(1);
}
temp->name.val_ptr = (void *)p_name;
temp->name.opp.oprom_array = temp->name.val_ptr;
temp->name.opp.holds_array = 1;
temp->value.val_ptr = (void *)p_data;
if ((p_data != NULL) && (*p_data == '\0')) {
for (i = 0; i < OPROM_NODE_SIZE; i++)
temp->value.opp.oprom_node[i] =
*((int *)p_data+i);
temp->value.opp.holds_array = 0;
} else {
temp->value.opp.oprom_array = temp->value.val_ptr;
temp->value.opp.holds_array = 1;
}
temp->size = retval;
if (pnode->props == NULL) {
pnode->props = temp;
} else if (prop != NULL) {
prop->next = temp;
}
prop = temp;
prop->next = NULL;
}
}
int
do_devinfo(int syserrlog, char *pgname, int log_flag, int prt_flag)
{
Sys_tree sys_tree;
Prom_node *root_node;
di_node_t di_root_node;
struct system_kstat_data sys_kstat;
int retval = -1;
progname = pgname;
logging = log_flag;
print_flag = prt_flag;
sys_tree.sys_mem = NULL;
sys_tree.boards = NULL;
sys_tree.bd_list = NULL;
sys_tree.board_cnt = 0;
if ((di_root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
exit(_error("di_init() failed"));
}
if ((ph = di_prom_init()) == NULL) {
exit(_error("di_prom_init() failed"));
}
root_node = walk_di_tree(&sys_tree, NULL, di_root_node);
resolve_board_types(&sys_tree);
read_sun4u_kstats(&sys_tree, &sys_kstat);
retval = display(&sys_tree, root_node, &sys_kstat, syserrlog);
di_fini(di_root_node);
di_prom_fini(ph);
return (retval);
}
static int
match_compatible_name(char *compatible_array, int n_names, char *name)
{
int i, ret = 0;
for (i = 0; i < n_names; i++) {
if (strcmp(compatible_array, name) == 0) {
ret = 1;
break;
}
compatible_array += strlen(compatible_array) + 1;
}
return (ret);
}