1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/* $OpenBSD: optree.c,v 1.11 2021/10/24 21:24:18 deraadt Exp $ */
/*
* Copyright (c) 2007 Federico G. Schwindt <fgsch@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for
* any purpose with or without fee is hereby granted, provided that
* the above copyright notice and this permission notice appear in all
* copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
* OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/ioctl.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <machine/openpromio.h>
#include "defs.h"
extern char *path_openprom;
static void
op_print(struct opiocdesc *opio, int depth)
{
char *p;
int i, multi, special;
uint32_t cell;
opio->op_name[opio->op_namelen] = '\0';
printf("%*s%s: ", depth * 4, " ", opio->op_name);
if (opio->op_buflen > 0) {
opio->op_buf[opio->op_buflen] = '\0';
multi = special = 0;
/*
* On macppc we have string-values properties that end
* with multiple NUL characters, and the serial number
* has them embedded within the string.
*/
if (opio->op_buf[0] != '\0') {
for (i = 0; i < opio->op_buflen; i++) {
p = &opio->op_buf[i];
if (*p >= ' ' && *p <= '~')
continue;
if (*p == '\0') {
if (i + 1 < opio->op_buflen)
p++;
if (*p >= ' ' && *p <= '~') {
special = multi;
continue;
}
if (*p == '\0') {
multi = 1;
continue;
}
}
special = 1;
break;
}
} else {
if (opio->op_buflen > 1)
special = 1;
}
if (special && strcmp(opio->op_name, "serial-number") != 0) {
for (i = 0; opio->op_buflen - i >= sizeof(int);
i += sizeof(int)) {
if (i)
printf(".");
cell = *(uint32_t *)&opio->op_buf[i];
printf("%08x", betoh32(cell));
}
if (i < opio->op_buflen) {
if (i)
printf(".");
for (; i < opio->op_buflen; i++) {
printf("%02x",
*(u_char *)&opio->op_buf[i]);
}
}
} else {
for (i = 0; i < opio->op_buflen;
i += strlen(&opio->op_buf[i]) + 1) {
if (i && strlen(&opio->op_buf[i]) == 0)
continue;
if (i)
printf(" + ");
printf("'%s'", &opio->op_buf[i]);
}
}
} else if(opio->op_buflen < 0)
printf("too large");
printf("\n");
}
void
op_nodes(int fd, int node, int depth)
{
char op_buf[BUFSIZE * 8];
char op_name[BUFSIZE];
struct opiocdesc opio;
memset(op_name, 0, sizeof(op_name));
opio.op_nodeid = node;
opio.op_buf = op_buf;
opio.op_name = op_name;
if (!node) {
if (ioctl(fd, OPIOCGETNEXT, &opio) == -1)
err(1, "OPIOCGETNEXT");
node = opio.op_nodeid;
} else
printf("\n%*s", depth * 4, " ");
printf("Node 0x%x\n", node);
for (;;) {
opio.op_buflen = sizeof(op_buf);
opio.op_namelen = sizeof(op_name);
/* Get the next property. */
if (ioctl(fd, OPIOCNEXTPROP, &opio) == -1)
err(1, "OPIOCNEXTPROP");
op_buf[opio.op_buflen] = '\0';
(void)strlcpy(op_name, op_buf, sizeof(op_name));
opio.op_namelen = strlen(op_name);
/* If it's the last, punt. */
if (opio.op_namelen == 0)
break;
bzero(op_buf, sizeof(op_buf));
opio.op_buflen = sizeof(op_buf);
/* And its value. */
if (ioctl(fd, OPIOCGET, &opio) == -1) {
if (errno != ENOMEM)
err(1, "OPIOCGET");
opio.op_buflen = -1; /* for op_print */
}
op_print(&opio, depth + 1);
}
/* Get next child. */
if (ioctl(fd, OPIOCGETCHILD, &opio) == -1)
err(1, "OPIOCGETCHILD");
if (opio.op_nodeid)
op_nodes(fd, opio.op_nodeid, depth + 1);
/* Get next node/sibling. */
opio.op_nodeid = node;
if (ioctl(fd, OPIOCGETNEXT, &opio) == -1)
err(1, "OPIOCGETNEXT");
if (opio.op_nodeid)
op_nodes(fd, opio.op_nodeid, depth);
}
void
op_tree(void)
{
int fd;
if ((fd = open(path_openprom, O_RDONLY)) == -1)
err(1, "open: %s", path_openprom);
op_nodes(fd, 0, 0);
(void)close(fd);
}
|