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 static int 279 print_device_path(struct devinfo_dev *dev, void *xname) 280 { 281 const char *name = xname; 282 int rv; 283 284 if (strcmp(dev->dd_name, name) == 0) { 285 print_device_path_entry(dev); 286 return (1); 287 } 288 289 rv = devinfo_foreach_device_child(dev, print_device_path, xname); 290 if (rv == 1) { 291 printf(" "); 292 print_device_path_entry(dev); 293 } 294 return (rv); 295 } 296 297 static void 298 print_path(struct devinfo_dev *root, char *path) 299 { 300 if (devinfo_foreach_device_child(root, print_device_path, (void *)path) == 0) 301 errx(1, "%s: Not found", path); 302 if (!vflag) 303 printf("\n"); 304 } 305 306 static void __dead2 307 usage(void) 308 { 309 fprintf(stderr, "%s\n%s\n%s\n", 310 "usage: devinfo [-rv]", 311 " devinfo -u [-v]", 312 " devinfo -p dev [-v]"); 313 exit(1); 314 } 315 316 int 317 main(int argc, char *argv[]) 318 { 319 struct devinfo_dev *root; 320 int c, rv; 321 bool uflag; 322 char *path = NULL; 323 324 uflag = false; 325 while ((c = getopt(argc, argv, "p:ruv")) != -1) { 326 switch(c) { 327 case 'p': 328 path = optarg; 329 break; 330 case 'r': 331 rflag = true; 332 break; 333 case 'u': 334 uflag = true; 335 break; 336 case 'v': 337 vflag = true; 338 break; 339 default: 340 usage(); 341 } 342 } 343 344 if (path && (rflag || uflag)) 345 usage(); 346 347 if ((rv = devinfo_init()) != 0) { 348 errno = rv; 349 err(1, "devinfo_init"); 350 } 351 352 if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL) 353 errx(1, "can't find root device"); 354 355 if (path) { 356 print_path(root, path); 357 } else if (uflag) { 358 /* print resource usage? */ 359 devinfo_foreach_rman(print_rman, NULL); 360 } else { 361 /* print device hierarchy */ 362 devinfo_foreach_device_child(root, print_device, (void *)0); 363 } 364 return(0); 365 } 366