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