1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2000, 2001 Michael Smith
5 * Copyright (c) 2000 BSDi
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * Print information about system device configuration.
32 */
33
34 #include <sys/types.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include "devinfo.h"
42
43 static int rflag;
44 static int vflag;
45
46 static void print_resource(struct devinfo_res *);
47 static int print_device_matching_resource(struct devinfo_res *, void *);
48 static int print_device_rman_resources(struct devinfo_rman *, void *);
49 static int print_device(struct devinfo_dev *, void *);
50 static int print_rman_resource(struct devinfo_res *, void *);
51 static int print_rman(struct devinfo_rman *, void *);
52
53 struct indent_arg
54 {
55 int indent;
56 void *arg;
57 };
58
59 /*
60 * Print a resource.
61 */
62 void
print_resource(struct devinfo_res * res)63 print_resource(struct devinfo_res *res)
64 {
65 struct devinfo_rman *rman;
66 int hexmode;
67
68 rman = devinfo_handle_to_rman(res->dr_rman);
69 hexmode = (rman->dm_size > 1000) || (rman->dm_size == 0);
70 printf(hexmode ? "0x%jx" : "%ju", res->dr_start);
71 if (res->dr_size > 1)
72 printf(hexmode ? "-0x%jx" : "-%ju",
73 res->dr_start + res->dr_size - 1);
74 }
75
76 /*
77 * Print resource information if this resource matches the
78 * given device.
79 *
80 * If the given indent is 0, return an indicator that a matching
81 * resource exists.
82 */
83 int
print_device_matching_resource(struct devinfo_res * res,void * arg)84 print_device_matching_resource(struct devinfo_res *res, void *arg)
85 {
86 struct indent_arg *ia = (struct indent_arg *)arg;
87 struct devinfo_dev *dev = (struct devinfo_dev *)ia->arg;
88 int i;
89
90 if (devinfo_handle_to_device(res->dr_device) == dev) {
91 /* in 'detect' mode, found a match */
92 if (ia->indent == 0)
93 return(1);
94 for (i = 0; i < ia->indent; i++)
95 printf(" ");
96 print_resource(res);
97 printf("\n");
98 }
99 return(0);
100 }
101
102 /*
103 * Print resource information for this device and resource manager.
104 */
105 int
print_device_rman_resources(struct devinfo_rman * rman,void * arg)106 print_device_rman_resources(struct devinfo_rman *rman, void *arg)
107 {
108 struct indent_arg *ia = (struct indent_arg *)arg;
109 int indent, i;
110
111 indent = ia->indent;
112
113 /* check whether there are any resources matching this device */
114 ia->indent = 0;
115 if (devinfo_foreach_rman_resource(rman,
116 print_device_matching_resource, ia) != 0) {
117
118 /* there are, print header */
119 for (i = 0; i < indent; i++)
120 printf(" ");
121 printf("%s:\n", rman->dm_desc);
122
123 /* print resources */
124 ia->indent = indent + 4;
125 devinfo_foreach_rman_resource(rman,
126 print_device_matching_resource, ia);
127 }
128 ia->indent = indent;
129 return(0);
130 }
131
132 static void
print_dev(struct devinfo_dev * dev)133 print_dev(struct devinfo_dev *dev)
134 {
135
136 printf("%s", dev->dd_name[0] ? dev->dd_name : "unknown");
137 if (vflag && *dev->dd_desc)
138 printf(" <%s>", dev->dd_desc);
139 if (vflag && *dev->dd_pnpinfo)
140 printf(" pnpinfo %s", dev->dd_pnpinfo);
141 if (vflag && *dev->dd_location)
142 printf(" at %s", dev->dd_location);
143 if (!(dev->dd_flags & DF_ENABLED))
144 printf(" (disabled)");
145 else if (dev->dd_flags & DF_SUSPENDED)
146 printf(" (suspended)");
147 }
148
149
150 /*
151 * Print information about a device.
152 */
153 int
print_device(struct devinfo_dev * dev,void * arg)154 print_device(struct devinfo_dev *dev, void *arg)
155 {
156 struct indent_arg ia;
157 int i, indent;
158
159 if (vflag || (dev->dd_name[0] != 0 && dev->dd_state >= DS_ATTACHED)) {
160 indent = (int)(intptr_t)arg;
161 for (i = 0; i < indent; i++)
162 printf(" ");
163 print_dev(dev);
164 printf("\n");
165 if (rflag) {
166 ia.indent = indent + 4;
167 ia.arg = dev;
168 devinfo_foreach_rman(print_device_rman_resources,
169 (void *)&ia);
170 }
171 }
172
173 return(devinfo_foreach_device_child(dev, print_device,
174 (void *)((char *)arg + 2)));
175 }
176
177 /*
178 * Print information about a resource under a resource manager.
179 */
180 int
print_rman_resource(struct devinfo_res * res,void * arg __unused)181 print_rman_resource(struct devinfo_res *res, void *arg __unused)
182 {
183 struct devinfo_dev *dev;
184
185 printf(" ");
186 print_resource(res);
187 dev = devinfo_handle_to_device(res->dr_device);
188 if (dev != NULL) {
189 if (dev->dd_name[0] != 0) {
190 printf(" (%s)", dev->dd_name);
191 } else {
192 printf(" (unknown)");
193 if (vflag && *dev->dd_pnpinfo)
194 printf(" pnpinfo %s", dev->dd_pnpinfo);
195 if (vflag && *dev->dd_location)
196 printf(" at %s", dev->dd_location);
197 }
198 } else {
199 printf(" ----");
200 }
201 printf("\n");
202 return(0);
203 }
204
205 /*
206 * Print information about a resource manager.
207 */
208 int
print_rman(struct devinfo_rman * rman,void * arg __unused)209 print_rman(struct devinfo_rman *rman, void *arg __unused)
210 {
211 printf("%s:\n", rman->dm_desc);
212 devinfo_foreach_rman_resource(rman, print_rman_resource, 0);
213 return(0);
214 }
215
216 static int
print_path(struct devinfo_dev * dev,void * xname)217 print_path(struct devinfo_dev *dev, void *xname)
218 {
219 const char *name = xname;
220 int rv;
221
222 if (strcmp(dev->dd_name, name) == 0) {
223 print_dev(dev);
224 if (vflag)
225 printf("\n");
226 return (1);
227 }
228
229 rv = devinfo_foreach_device_child(dev, print_path, xname);
230 if (rv == 1) {
231 printf(" ");
232 print_dev(dev);
233 if (vflag)
234 printf("\n");
235 }
236 return (rv);
237 }
238
239 static void __dead2
usage(void)240 usage(void)
241 {
242 fprintf(stderr, "%s\n%s\n%s\n",
243 "usage: devinfo [-rv]",
244 " devinfo -u [-v]",
245 " devinfo -p dev [-v]");
246 exit(1);
247 }
248
249 int
main(int argc,char * argv[])250 main(int argc, char *argv[])
251 {
252 struct devinfo_dev *root;
253 int c, uflag, rv;
254 char *path = NULL;
255
256 uflag = 0;
257 while ((c = getopt(argc, argv, "p:ruv")) != -1) {
258 switch(c) {
259 case 'p':
260 path = optarg;
261 break;
262 case 'r':
263 rflag++;
264 break;
265 case 'u':
266 uflag++;
267 break;
268 case 'v':
269 vflag++;
270 break;
271 default:
272 usage();
273 }
274 }
275
276 if (path && (rflag || uflag))
277 usage();
278
279 if ((rv = devinfo_init()) != 0) {
280 errno = rv;
281 err(1, "devinfo_init");
282 }
283
284 if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
285 errx(1, "can't find root device");
286
287 if (path) {
288 if (devinfo_foreach_device_child(root, print_path, (void *)path) == 0)
289 errx(1, "%s: Not found", path);
290 if (!vflag)
291 printf("\n");
292 } else if (uflag) {
293 /* print resource usage? */
294 devinfo_foreach_rman(print_rman, NULL);
295 } else {
296 /* print device hierarchy */
297 devinfo_foreach_device_child(root, print_device, (void *)0);
298 }
299 return(0);
300 }
301