xref: /freebsd/usr.sbin/devinfo/devinfo.c (revision 44d4804d1945435745518cd09eb8ae6ab22ecef4)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/types.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include "devinfo.h"
45 
46 static int	rflag;
47 static int	vflag;
48 
49 static void	print_resource(struct devinfo_res *);
50 static int	print_device_matching_resource(struct devinfo_res *, void *);
51 static int	print_device_rman_resources(struct devinfo_rman *, void *);
52 static int	print_device(struct devinfo_dev *, void *);
53 static int	print_rman_resource(struct devinfo_res *, void *);
54 static int	print_rman(struct devinfo_rman *, void *);
55 
56 struct indent_arg
57 {
58 	int	indent;
59 	void	*arg;
60 };
61 
62 /*
63  * Print a resource.
64  */
65 void
66 print_resource(struct devinfo_res *res)
67 {
68 	struct devinfo_rman	*rman;
69 	int			hexmode;
70 
71 	rman = devinfo_handle_to_rman(res->dr_rman);
72 	hexmode =  (rman->dm_size > 1000) || (rman->dm_size == 0);
73 	printf(hexmode ? "0x%jx" : "%ju", res->dr_start);
74 	if (res->dr_size > 1)
75 		printf(hexmode ? "-0x%jx" : "-%ju",
76 		    res->dr_start + res->dr_size - 1);
77 }
78 
79 /*
80  * Print resource information if this resource matches the
81  * given device.
82  *
83  * If the given indent is 0, return an indicator that a matching
84  * resource exists.
85  */
86 int
87 print_device_matching_resource(struct devinfo_res *res, void *arg)
88 {
89 	struct indent_arg	*ia = (struct indent_arg *)arg;
90 	struct devinfo_dev	*dev = (struct devinfo_dev *)ia->arg;
91 	int			i;
92 
93 	if (devinfo_handle_to_device(res->dr_device) == dev) {
94 		/* in 'detect' mode, found a match */
95 		if (ia->indent == 0)
96 			return(1);
97 		for (i = 0; i < ia->indent; i++)
98 			printf(" ");
99 		print_resource(res);
100 		printf("\n");
101 	}
102 	return(0);
103 }
104 
105 /*
106  * Print resource information for this device and resource manager.
107  */
108 int
109 print_device_rman_resources(struct devinfo_rman *rman, void *arg)
110 {
111 	struct indent_arg	*ia = (struct indent_arg *)arg;
112 	int			indent, i;
113 
114 	indent = ia->indent;
115 
116 	/* check whether there are any resources matching this device */
117 	ia->indent = 0;
118 	if (devinfo_foreach_rman_resource(rman,
119 	    print_device_matching_resource, ia) != 0) {
120 
121 		/* there are, print header */
122 		for (i = 0; i < indent; i++)
123 			printf(" ");
124 		printf("%s:\n", rman->dm_desc);
125 
126 		/* print resources */
127 		ia->indent = indent + 4;
128 		devinfo_foreach_rman_resource(rman,
129 		    print_device_matching_resource, ia);
130 	}
131 	ia->indent = indent;
132 	return(0);
133 }
134 
135 static void
136 print_dev(struct devinfo_dev *dev)
137 {
138 
139 	printf("%s", dev->dd_name[0] ? dev->dd_name : "unknown");
140 	if (vflag && *dev->dd_pnpinfo)
141 		printf(" pnpinfo %s", dev->dd_pnpinfo);
142 	if (vflag && *dev->dd_location)
143 		printf(" at %s", dev->dd_location);
144 	if (!(dev->dd_flags & DF_ENABLED))
145 		printf(" (disabled)");
146 	else if (dev->dd_flags & DF_SUSPENDED)
147 		printf(" (suspended)");
148 }
149 
150 
151 /*
152  * Print information about a device.
153  */
154 int
155 print_device(struct devinfo_dev *dev, void *arg)
156 {
157 	struct indent_arg	ia;
158 	int			i, indent;
159 
160 	if (vflag || (dev->dd_name[0] != 0 && dev->dd_state >= DS_ATTACHED)) {
161 		indent = (int)(intptr_t)arg;
162 		for (i = 0; i < indent; i++)
163 			printf(" ");
164 		print_dev(dev);
165 		printf("\n");
166 		if (rflag) {
167 			ia.indent = indent + 4;
168 			ia.arg = dev;
169 			devinfo_foreach_rman(print_device_rman_resources,
170 			    (void *)&ia);
171 		}
172 	}
173 
174 	return(devinfo_foreach_device_child(dev, print_device,
175 	    (void *)((char *)arg + 2)));
176 }
177 
178 /*
179  * Print information about a resource under a resource manager.
180  */
181 int
182 print_rman_resource(struct devinfo_res *res, void *arg __unused)
183 {
184 	struct devinfo_dev	*dev;
185 
186 	printf("    ");
187 	print_resource(res);
188 	dev = devinfo_handle_to_device(res->dr_device);
189 	if ((dev != NULL) && (dev->dd_name[0] != 0)) {
190 		printf(" (%s)", dev->dd_name);
191 	} else {
192 		printf(" ----");
193 	}
194 	printf("\n");
195 	return(0);
196 }
197 
198 /*
199  * Print information about a resource manager.
200  */
201 int
202 print_rman(struct devinfo_rman *rman, void *arg __unused)
203 {
204 	printf("%s:\n", rman->dm_desc);
205 	devinfo_foreach_rman_resource(rman, print_rman_resource, 0);
206 	return(0);
207 }
208 
209 static int
210 print_path(struct devinfo_dev *dev, void *xname)
211 {
212 	const char *name = xname;
213 	int rv;
214 
215 	if (strcmp(dev->dd_name, name) == 0) {
216 		print_dev(dev);
217 		if (vflag)
218 			printf("\n");
219 		return (1);
220 	}
221 
222 	rv = devinfo_foreach_device_child(dev, print_path, xname);
223 	if (rv == 1) {
224 		printf(" ");
225 		print_dev(dev);
226 		if (vflag)
227 			printf("\n");
228 	}
229 	return (rv);
230 }
231 
232 static void __dead2
233 usage(void)
234 {
235 	fprintf(stderr, "%s\n%s\n%s\n",
236 	    "usage: devinfo [-rv]",
237 	    "       devinfo -u",
238 	    "       devinfo -p dev [-v]");
239 	exit(1);
240 }
241 
242 int
243 main(int argc, char *argv[])
244 {
245 	struct devinfo_dev	*root;
246 	int			c, uflag, rv;
247 	char			*path = NULL;
248 
249 	uflag = 0;
250 	while ((c = getopt(argc, argv, "p:ruv")) != -1) {
251 		switch(c) {
252 		case 'p':
253 			path = optarg;
254 			break;
255 		case 'r':
256 			rflag++;
257 			break;
258 		case 'u':
259 			uflag++;
260 			break;
261 		case 'v':
262 			vflag++;
263 			break;
264 		default:
265 			usage();
266 		}
267 	}
268 
269 	if (path && (rflag || uflag))
270 		usage();
271 
272 	if ((rv = devinfo_init()) != 0) {
273 		errno = rv;
274 		err(1, "devinfo_init");
275 	}
276 
277 	if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
278 		errx(1, "can't find root device");
279 
280 	if (path) {
281 		if (devinfo_foreach_device_child(root, print_path, (void *)path) == 0)
282 			errx(1, "%s: Not found", path);
283 		if (!vflag)
284 			printf("\n");
285 	} else if (uflag) {
286 		/* print resource usage? */
287 		devinfo_foreach_rman(print_rman, NULL);
288 	} else {
289 		/* print device hierarchy */
290 		devinfo_foreach_device_child(root, print_device, (void *)0);
291 	}
292 	return(0);
293 }
294