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/param.h> 35 36 #include <err.h> 37 #include <errno.h> 38 #include <stdbool.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 #include "devinfo.h" 44 45 static bool rflag; 46 static bool vflag; 47 48 static void print_indent(int); 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 void print_device_props(struct devinfo_dev *); 53 static int print_device(struct devinfo_dev *, void *); 54 static int print_rman_resource(struct devinfo_res *, void *); 55 static int print_rman(struct devinfo_rman *, void *); 56 static int print_device_path(struct devinfo_dev *, void *); 57 static void print_path(struct devinfo_dev *, char *); 58 static void usage(void); 59 60 struct indent_arg 61 { 62 int indent; 63 void *arg; 64 }; 65 66 67 static void 68 print_indent(int n) 69 { 70 static char buffer[1024]; 71 72 if (n < 1) 73 return; 74 n = MIN((size_t)n, sizeof(buffer) - 1); 75 memset(buffer, ' ', n); 76 buffer[n] = '\0'; 77 printf("%s", buffer); 78 } 79 80 /* 81 * Print a resource. 82 */ 83 void 84 print_resource(struct devinfo_res *res) 85 { 86 struct devinfo_rman *rman; 87 bool hexmode; 88 rman_res_t end; 89 90 rman = devinfo_handle_to_rman(res->dr_rman); 91 hexmode = (rman->dm_size > 1000) || (rman->dm_size == 0); 92 end = res->dr_start + res->dr_size - 1; 93 94 if (hexmode) { 95 printf("0x%jx", res->dr_start); 96 if (res->dr_size > 1) 97 printf("-0x%jx", end); 98 } else { 99 printf("%ju", res->dr_start); 100 if (res->dr_size > 1) 101 printf("-%ju", end); 102 } 103 } 104 105 /* 106 * Print resource information if this resource matches the 107 * given device. 108 * 109 * If the given indent is 0, return an indicator that a matching 110 * resource exists. 111 */ 112 int 113 print_device_matching_resource(struct devinfo_res *res, void *arg) 114 { 115 struct indent_arg *ia = (struct indent_arg *)arg; 116 struct devinfo_dev *dev = (struct devinfo_dev *)ia->arg; 117 118 if (devinfo_handle_to_device(res->dr_device) == dev) { 119 /* in 'detect' mode, found a match */ 120 if (ia->indent == 0) 121 return(1); 122 print_indent(ia->indent); 123 print_resource(res); 124 printf("\n"); 125 } 126 return(0); 127 } 128 129 /* 130 * Print resource information for this device and resource manager. 131 */ 132 int 133 print_device_rman_resources(struct devinfo_rman *rman, void *arg) 134 { 135 struct indent_arg *ia = (struct indent_arg *)arg; 136 int indent; 137 138 indent = ia->indent; 139 140 /* check whether there are any resources matching this device */ 141 ia->indent = 0; 142 if (devinfo_foreach_rman_resource(rman, 143 print_device_matching_resource, ia) != 0) { 144 145 /* there are, print header */ 146 print_indent(indent); 147 printf("%s:\n", rman->dm_desc); 148 149 /* print resources */ 150 ia->indent = indent + 4; 151 devinfo_foreach_rman_resource(rman, 152 print_device_matching_resource, ia); 153 } 154 ia->indent = indent; 155 return(0); 156 } 157 158 static void 159 print_device_props(struct devinfo_dev *dev) 160 { 161 if (vflag) { 162 if (*dev->dd_desc) { 163 printf(" <%s>", dev->dd_desc); 164 } 165 if (*dev->dd_pnpinfo) { 166 printf(" pnpinfo %s", dev->dd_pnpinfo); 167 } 168 if (*dev->dd_location) { 169 printf(" at %s", dev->dd_location); 170 } 171 } 172 173 if (!(dev->dd_flags & DF_ENABLED)) 174 printf(" (disabled)"); 175 else if (dev->dd_flags & DF_SUSPENDED) 176 printf(" (suspended)"); 177 } 178 179 /* 180 * Print information about a device. 181 */ 182 static int 183 print_device(struct devinfo_dev *dev, void *arg) 184 { 185 struct indent_arg ia; 186 int indent; 187 bool printit = vflag || (dev->dd_name[0] != 0 && 188 dev->dd_state >= DS_ATTACHED); 189 190 if (printit) { 191 indent = (int)(intptr_t)arg; 192 print_indent(indent); 193 printf("%s", dev->dd_name[0] ? dev->dd_name : "unknown"); 194 print_device_props(dev); 195 printf("\n"); 196 if (rflag) { 197 ia.indent = indent + 4; 198 ia.arg = dev; 199 devinfo_foreach_rman(print_device_rman_resources, 200 (void *)&ia); 201 } 202 } 203 204 return(devinfo_foreach_device_child(dev, print_device, 205 (void *)((char *)arg + 2))); 206 } 207 208 /* 209 * Print information about a resource under a resource manager. 210 */ 211 int 212 print_rman_resource(struct devinfo_res *res, void *arg __unused) 213 { 214 struct devinfo_dev *dev; 215 struct devinfo_rman *rman; 216 rman_res_t end; 217 bool hexmode; 218 219 dev = devinfo_handle_to_device(res->dr_device); 220 rman = devinfo_handle_to_rman(res->dr_rman); 221 hexmode = (rman->dm_size > 1000) || (rman->dm_size == 0); 222 end = res->dr_start + res->dr_size - 1; 223 224 printf(" "); 225 226 if (hexmode) { 227 if (res->dr_size > 1) 228 printf("0x%jx-0x%jx", res->dr_start, end); 229 else 230 printf("0x%jx", res->dr_start); 231 } else { 232 if (res->dr_size > 1) 233 printf("%ju-%ju", res->dr_start, end); 234 else 235 printf("%ju", res->dr_start); 236 } 237 238 dev = devinfo_handle_to_device(res->dr_device); 239 if (dev != NULL) { 240 if (dev->dd_name[0] != 0) { 241 printf(" (%s)", dev->dd_name); 242 } else { 243 printf(" (unknown)"); 244 if (vflag && *dev->dd_pnpinfo) 245 printf(" pnpinfo %s", dev->dd_pnpinfo); 246 if (vflag && *dev->dd_location) 247 printf(" at %s", dev->dd_location); 248 } 249 } else { 250 printf(" ----"); 251 } 252 printf("\n"); 253 return(0); 254 } 255 256 /* 257 * Print information about a resource manager. 258 */ 259 int 260 print_rman(struct devinfo_rman *rman, void *arg __unused) 261 { 262 printf("%s:\n", rman->dm_desc); 263 devinfo_foreach_rman_resource(rman, print_rman_resource, 0); 264 return(0); 265 } 266 267 static void 268 print_device_path_entry(struct devinfo_dev *dev) 269 { 270 const char *devname = dev->dd_name[0] ? dev->dd_name : "unknown"; 271 272 printf("%s", devname); 273 print_device_props(dev); 274 if (vflag) 275 printf("\n"); 276 } 277 278 /* 279 * This assumes the string to be compared in *cp is either space or 280 * nul terminated. 281 */ 282 static bool 283 match_value(const char *cp, const char *name) 284 { 285 const char *end; 286 size_t len; 287 288 end = strchr(cp, ' '); 289 if (end == NULL) 290 return (strcmp(cp, name) == 0); 291 292 if (end == cp) 293 return (false); 294 295 /* NB: strncmp(3) would return zero if name matches a prefix. */ 296 len = end - cp; 297 return (strlen(name) == len && memcmp(name, cp, len) == 0); 298 } 299 300 static bool 301 device_matches_name(struct devinfo_dev *dev, const char *name) 302 { 303 const char *cp, *val; 304 305 if (strcmp(dev->dd_name, name) == 0) 306 return (true); 307 308 if (*dev->dd_location) { 309 /* Permit matches on the ACPI handle stored in location. */ 310 if (name[0] == '\\' && (cp = strstr(dev->dd_location, 311 "handle=\\")) != NULL) { 312 if (match_value(cp + strlen("handle="), name)) 313 return (true); 314 } 315 316 /* Permit matches on the PCI dbsf stored in location. */ 317 if (strncmp(name, "pci", strlen("pci")) == 0 && 318 (cp = strstr(dev->dd_location, "dbsf=pci")) != NULL) { 319 cp += strlen("dbsf=pci"); 320 val = name + strlen("pci"); 321 if (match_value(cp, val)) 322 return (true); 323 324 /* Also match on pci<b>:<s>:<f> for domain 0. */ 325 if (strncmp(cp, "0:", strlen("0:")) == 0 && 326 match_value(cp + strlen("0:"), val)) 327 return (true); 328 } 329 } 330 331 return (false); 332 } 333 334 static int 335 print_device_path(struct devinfo_dev *dev, void *xname) 336 { 337 const char *name = xname; 338 int rv; 339 340 if (device_matches_name(dev, name)) { 341 print_device_path_entry(dev); 342 return (1); 343 } 344 345 rv = devinfo_foreach_device_child(dev, print_device_path, xname); 346 if (rv == 1) { 347 printf(" "); 348 print_device_path_entry(dev); 349 } 350 return (rv); 351 } 352 353 static void 354 print_path(struct devinfo_dev *root, char *path) 355 { 356 if (devinfo_foreach_device_child(root, print_device_path, (void *)path) == 0) 357 errx(1, "%s: Not found", path); 358 if (!vflag) 359 printf("\n"); 360 } 361 362 static void __dead2 363 usage(void) 364 { 365 fprintf(stderr, "%s\n%s\n%s\n", 366 "usage: devinfo [-rv]", 367 " devinfo -u [-v]", 368 " devinfo -p dev [-v]"); 369 exit(1); 370 } 371 372 int 373 main(int argc, char *argv[]) 374 { 375 struct devinfo_dev *root; 376 int c, rv; 377 bool uflag; 378 char *path = NULL; 379 380 uflag = false; 381 while ((c = getopt(argc, argv, "p:ruv")) != -1) { 382 switch(c) { 383 case 'p': 384 path = optarg; 385 break; 386 case 'r': 387 rflag = true; 388 break; 389 case 'u': 390 uflag = true; 391 break; 392 case 'v': 393 vflag = true; 394 break; 395 default: 396 usage(); 397 } 398 } 399 400 if (path && (rflag || uflag)) 401 usage(); 402 403 if ((rv = devinfo_init()) != 0) { 404 errno = rv; 405 err(1, "devinfo_init"); 406 } 407 408 if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL) 409 errx(1, "can't find root device"); 410 411 if (path) { 412 print_path(root, path); 413 } else if (uflag) { 414 /* print resource usage? */ 415 devinfo_foreach_rman(print_rman, NULL); 416 } else { 417 /* print device hierarchy */ 418 devinfo_foreach_device_child(root, print_device, (void *)0); 419 } 420 return(0); 421 } 422