1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <stdio.h> 34*0Sstevel@tonic-gate #include <stdarg.h> 35*0Sstevel@tonic-gate #include <stdlib.h> 36*0Sstevel@tonic-gate #include <unistd.h> 37*0Sstevel@tonic-gate #include <strings.h> 38*0Sstevel@tonic-gate #include <sys/systeminfo.h> 39*0Sstevel@tonic-gate #include <sys/types.h> 40*0Sstevel@tonic-gate #include <sys/stat.h> 41*0Sstevel@tonic-gate #include "prtconf.h" 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate struct prt_opts opts; 44*0Sstevel@tonic-gate struct prt_dbg dbg; 45*0Sstevel@tonic-gate static char new_path[MAXPATHLEN]; 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate #define INDENT_LENGTH 4 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate #ifdef __x86 50*0Sstevel@tonic-gate static const char *usage = "%s [ -V | -x | -abcvpPD ] [ <device_path > ]\n"; 51*0Sstevel@tonic-gate #else 52*0Sstevel@tonic-gate static const char *usage = "%s [ -F | -V | -x | -abcvpPD ][ <device_path > ]\n"; 53*0Sstevel@tonic-gate #endif /* __x86 */ 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate static void 56*0Sstevel@tonic-gate setprogname(const char *name) 57*0Sstevel@tonic-gate { 58*0Sstevel@tonic-gate char *p; 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate if (name == NULL) 61*0Sstevel@tonic-gate opts.o_progname = "prtconf"; 62*0Sstevel@tonic-gate else if (p = strrchr(name, '/')) 63*0Sstevel@tonic-gate opts.o_progname = (const char *) p + 1; 64*0Sstevel@tonic-gate else 65*0Sstevel@tonic-gate opts.o_progname = name; 66*0Sstevel@tonic-gate } 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate /*PRINTFLIKE1*/ 69*0Sstevel@tonic-gate void 70*0Sstevel@tonic-gate dprintf(const char *fmt, ...) 71*0Sstevel@tonic-gate { 72*0Sstevel@tonic-gate if (dbg.d_debug) { 73*0Sstevel@tonic-gate va_list ap; 74*0Sstevel@tonic-gate va_start(ap, fmt); 75*0Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 76*0Sstevel@tonic-gate va_end(ap); 77*0Sstevel@tonic-gate } 78*0Sstevel@tonic-gate } 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate void 81*0Sstevel@tonic-gate indent_to_level(int ilev) 82*0Sstevel@tonic-gate { 83*0Sstevel@tonic-gate (void) printf("%*s", INDENT_LENGTH * ilev, ""); 84*0Sstevel@tonic-gate } 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate static void 87*0Sstevel@tonic-gate cleanup_path(const char *input_path, char *path) 88*0Sstevel@tonic-gate { 89*0Sstevel@tonic-gate char *ptr, *ptr2; 90*0Sstevel@tonic-gate size_t len; 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate if ((input_path == NULL) || (path == NULL)) 93*0Sstevel@tonic-gate return; 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate (void) strcpy(path, input_path); 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate /*LINTED*/ 98*0Sstevel@tonic-gate while (1) { 99*0Sstevel@tonic-gate len = strlen(path); 100*0Sstevel@tonic-gate if (len == 0) 101*0Sstevel@tonic-gate break; 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate /* change substring "//" into "/" */ 104*0Sstevel@tonic-gate if (ptr = strstr(path, "//")) { 105*0Sstevel@tonic-gate len = strlen(ptr + 1); 106*0Sstevel@tonic-gate (void) memmove(ptr, ptr + 1, len + 1); 107*0Sstevel@tonic-gate continue; 108*0Sstevel@tonic-gate } 109*0Sstevel@tonic-gate /* change substring "/./" into "/" */ 110*0Sstevel@tonic-gate if (ptr = strstr(path, "/./")) { 111*0Sstevel@tonic-gate len = strlen(ptr + 2); 112*0Sstevel@tonic-gate (void) memmove(ptr, ptr + 2, len + 1); 113*0Sstevel@tonic-gate continue; 114*0Sstevel@tonic-gate } 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate /* change substring "/<foo>/../" into "/" */ 117*0Sstevel@tonic-gate if (ptr = strstr(path, "/../")) { 118*0Sstevel@tonic-gate len = strlen(ptr + 3); 119*0Sstevel@tonic-gate *ptr = '\0'; 120*0Sstevel@tonic-gate ptr2 = strrchr(path, (int)'/'); 121*0Sstevel@tonic-gate if (ptr2 == NULL) { 122*0Sstevel@tonic-gate /* path had a leading "/../" */ 123*0Sstevel@tonic-gate ptr2 = path; 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate (void) memmove(ptr2, ptr + 3, len + 1); 126*0Sstevel@tonic-gate continue; 127*0Sstevel@tonic-gate } 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate /* change trailing "/<foo>/.." into "/" */ 130*0Sstevel@tonic-gate if ((len >= 3) && 131*0Sstevel@tonic-gate (path[len - 3] == '/') && 132*0Sstevel@tonic-gate (path[len - 2] == '.') && 133*0Sstevel@tonic-gate (path[len - 1] == '.')) { 134*0Sstevel@tonic-gate path[len - 3] = '\0'; 135*0Sstevel@tonic-gate ptr2 = strrchr(path, (int)'/'); 136*0Sstevel@tonic-gate if (ptr2 != NULL) { 137*0Sstevel@tonic-gate ptr2[1] = '\0'; 138*0Sstevel@tonic-gate } else { 139*0Sstevel@tonic-gate /* path was "/.." */ 140*0Sstevel@tonic-gate path[0] = '/'; 141*0Sstevel@tonic-gate path[1] = '\0'; 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate continue; 144*0Sstevel@tonic-gate } 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate /* change trailing "/." into "/" */ 147*0Sstevel@tonic-gate if ((len >= 2) && 148*0Sstevel@tonic-gate (path[len - 2] == '/') && 149*0Sstevel@tonic-gate (path[len - 1] == '.')) { 150*0Sstevel@tonic-gate path[len - 1] = '\0'; 151*0Sstevel@tonic-gate continue; 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate /* remove trailing "/" unless it's the root */ 155*0Sstevel@tonic-gate if ((len > 1) && (path[len - 1] == '/')) { 156*0Sstevel@tonic-gate path[len - 1] = '\0'; 157*0Sstevel@tonic-gate continue; 158*0Sstevel@tonic-gate } 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate break; 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate /* 166*0Sstevel@tonic-gate * debug version has two more flags: 167*0Sstevel@tonic-gate * -L force load driver 168*0Sstevel@tonic-gate * -M: print per driver list 169*0Sstevel@tonic-gate */ 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate #ifdef DEBUG 172*0Sstevel@tonic-gate static const char *optstring = "abcDvVxpPFf:M:dLuC"; 173*0Sstevel@tonic-gate #else 174*0Sstevel@tonic-gate static const char *optstring = "abcDvVxpPFf:uC"; 175*0Sstevel@tonic-gate #endif /* DEBUG */ 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate int 178*0Sstevel@tonic-gate main(int argc, char *argv[]) 179*0Sstevel@tonic-gate { 180*0Sstevel@tonic-gate long pagesize, npages; 181*0Sstevel@tonic-gate int c, ret; 182*0Sstevel@tonic-gate char hw_provider[SYS_NMLN]; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate setprogname(argv[0]); 185*0Sstevel@tonic-gate opts.o_promdev = "/dev/openprom"; 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gate while ((c = getopt(argc, argv, optstring)) != -1) { 188*0Sstevel@tonic-gate switch (c) { 189*0Sstevel@tonic-gate case 'a': 190*0Sstevel@tonic-gate ++opts.o_ancestors; 191*0Sstevel@tonic-gate break; 192*0Sstevel@tonic-gate case 'b': 193*0Sstevel@tonic-gate ++opts.o_productinfo; 194*0Sstevel@tonic-gate break; 195*0Sstevel@tonic-gate case 'c': 196*0Sstevel@tonic-gate ++opts.o_children; 197*0Sstevel@tonic-gate break; 198*0Sstevel@tonic-gate case 'D': 199*0Sstevel@tonic-gate ++opts.o_drv_name; 200*0Sstevel@tonic-gate break; 201*0Sstevel@tonic-gate case 'v': 202*0Sstevel@tonic-gate ++opts.o_verbose; 203*0Sstevel@tonic-gate break; 204*0Sstevel@tonic-gate case 'p': 205*0Sstevel@tonic-gate ++opts.o_prominfo; 206*0Sstevel@tonic-gate break; 207*0Sstevel@tonic-gate case 'f': 208*0Sstevel@tonic-gate opts.o_promdev = optarg; 209*0Sstevel@tonic-gate break; 210*0Sstevel@tonic-gate case 'V': 211*0Sstevel@tonic-gate ++opts.o_promversion; 212*0Sstevel@tonic-gate break; 213*0Sstevel@tonic-gate case 'x': 214*0Sstevel@tonic-gate ++opts.o_prom_ready64; 215*0Sstevel@tonic-gate break; 216*0Sstevel@tonic-gate case 'F': 217*0Sstevel@tonic-gate ++opts.o_fbname; 218*0Sstevel@tonic-gate ++opts.o_noheader; 219*0Sstevel@tonic-gate break; 220*0Sstevel@tonic-gate case 'P': 221*0Sstevel@tonic-gate ++opts.o_pseudodevs; 222*0Sstevel@tonic-gate break; 223*0Sstevel@tonic-gate case 'C': 224*0Sstevel@tonic-gate ++opts.o_forcecache; 225*0Sstevel@tonic-gate break; 226*0Sstevel@tonic-gate #ifdef DEBUG 227*0Sstevel@tonic-gate case 'M': 228*0Sstevel@tonic-gate dbg.d_drivername = optarg; 229*0Sstevel@tonic-gate ++dbg.d_bydriver; 230*0Sstevel@tonic-gate break; 231*0Sstevel@tonic-gate case 'L': 232*0Sstevel@tonic-gate ++dbg.d_forceload; 233*0Sstevel@tonic-gate break; 234*0Sstevel@tonic-gate #endif /* DEBUG */ 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate default: 237*0Sstevel@tonic-gate (void) fprintf(stderr, usage, opts.o_progname); 238*0Sstevel@tonic-gate return (1); 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate } 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate (void) uname(&opts.o_uts); 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate if (opts.o_fbname) 245*0Sstevel@tonic-gate return (do_fbname()); 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate if (opts.o_promversion) 248*0Sstevel@tonic-gate return (do_promversion()); 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate if (opts.o_prom_ready64) 251*0Sstevel@tonic-gate return (do_prom_version64()); 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate if (opts.o_productinfo) 254*0Sstevel@tonic-gate return (do_productinfo()); 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate opts.o_devices_path = NULL; 257*0Sstevel@tonic-gate opts.o_devt = DDI_DEV_T_NONE; 258*0Sstevel@tonic-gate opts.o_target = 0; 259*0Sstevel@tonic-gate if (optind < argc) { 260*0Sstevel@tonic-gate struct stat sinfo; 261*0Sstevel@tonic-gate char *path = argv[optind]; 262*0Sstevel@tonic-gate int error; 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate if (strlen(path) >= MAXPATHLEN) { 265*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: " 266*0Sstevel@tonic-gate "path specified is too long\n", opts.o_progname); 267*0Sstevel@tonic-gate return (1); 268*0Sstevel@tonic-gate } 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gate if (error = stat(path, &sinfo)) { 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate /* an invalid path was specified */ 273*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: invalid path specified\n", 274*0Sstevel@tonic-gate opts.o_progname); 275*0Sstevel@tonic-gate return (1); 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate } else if (((sinfo.st_mode & S_IFMT) == S_IFCHR) || 278*0Sstevel@tonic-gate ((sinfo.st_mode & S_IFMT) == S_IFBLK)) { 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate opts.o_devt = sinfo.st_rdev; 281*0Sstevel@tonic-gate error = 0; 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate } else if ((sinfo.st_mode & S_IFMT) == S_IFDIR) { 284*0Sstevel@tonic-gate size_t len, plen; 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate /* clean up the path */ 287*0Sstevel@tonic-gate cleanup_path(path, new_path); 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate len = strlen(new_path); 290*0Sstevel@tonic-gate plen = strlen("/devices"); 291*0Sstevel@tonic-gate if (len < plen) { 292*0Sstevel@tonic-gate /* This is not a valid /devices path */ 293*0Sstevel@tonic-gate error = 1; 294*0Sstevel@tonic-gate } else if ((len == plen) && 295*0Sstevel@tonic-gate (strcmp(new_path, "/devices") == 0)) { 296*0Sstevel@tonic-gate /* /devices is the root nexus */ 297*0Sstevel@tonic-gate opts.o_devices_path = "/"; 298*0Sstevel@tonic-gate error = 0; 299*0Sstevel@tonic-gate } else if (strncmp(new_path, "/devices/", plen + 1)) { 300*0Sstevel@tonic-gate /* This is not a valid /devices path */ 301*0Sstevel@tonic-gate error = 1; 302*0Sstevel@tonic-gate } else { 303*0Sstevel@tonic-gate /* a /devices/ path was specified */ 304*0Sstevel@tonic-gate opts.o_devices_path = new_path + plen; 305*0Sstevel@tonic-gate error = 0; 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gate } else { 309*0Sstevel@tonic-gate /* an invalid device path was specified */ 310*0Sstevel@tonic-gate error = 1; 311*0Sstevel@tonic-gate } 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate if (error) { 314*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: " 315*0Sstevel@tonic-gate "invalid device path specified\n", 316*0Sstevel@tonic-gate opts.o_progname); 317*0Sstevel@tonic-gate return (1); 318*0Sstevel@tonic-gate } 319*0Sstevel@tonic-gate 320*0Sstevel@tonic-gate opts.o_target = 1; 321*0Sstevel@tonic-gate } 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate if ((opts.o_ancestors || opts.o_children) && (!opts.o_target)) { 324*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: options require a device path\n", 325*0Sstevel@tonic-gate opts.o_progname); 326*0Sstevel@tonic-gate return (1); 327*0Sstevel@tonic-gate } 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gate if (opts.o_target) { 330*0Sstevel@tonic-gate prtconf_devinfo(); 331*0Sstevel@tonic-gate return (0); 332*0Sstevel@tonic-gate } 333*0Sstevel@tonic-gate 334*0Sstevel@tonic-gate ret = sysinfo(SI_HW_PROVIDER, hw_provider, sizeof (hw_provider)); 335*0Sstevel@tonic-gate /* 336*0Sstevel@tonic-gate * If 0 bytes are returned (the system returns '1', for the \0), 337*0Sstevel@tonic-gate * we're probably on x86, and there has been no si-hw-provider 338*0Sstevel@tonic-gate * set in /etc/bootrc, so just default to Sun. 339*0Sstevel@tonic-gate */ 340*0Sstevel@tonic-gate if (ret <= 1) { 341*0Sstevel@tonic-gate (void) strncpy(hw_provider, "Sun Microsystems", 342*0Sstevel@tonic-gate sizeof (hw_provider)); 343*0Sstevel@tonic-gate } else { 344*0Sstevel@tonic-gate /* 345*0Sstevel@tonic-gate * Provide backward compatibility by stripping out the _. 346*0Sstevel@tonic-gate */ 347*0Sstevel@tonic-gate if (strcmp(hw_provider, "Sun_Microsystems") == 0) 348*0Sstevel@tonic-gate hw_provider[3] = ' '; 349*0Sstevel@tonic-gate } 350*0Sstevel@tonic-gate (void) printf("System Configuration: %s %s\n", hw_provider, 351*0Sstevel@tonic-gate opts.o_uts.machine); 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate pagesize = sysconf(_SC_PAGESIZE); 354*0Sstevel@tonic-gate npages = sysconf(_SC_PHYS_PAGES); 355*0Sstevel@tonic-gate (void) printf("Memory size: "); 356*0Sstevel@tonic-gate if (pagesize == -1 || npages == -1) 357*0Sstevel@tonic-gate (void) printf("unable to determine\n"); 358*0Sstevel@tonic-gate else { 359*0Sstevel@tonic-gate const int64_t kbyte = 1024; 360*0Sstevel@tonic-gate const int64_t mbyte = 1024 * 1024; 361*0Sstevel@tonic-gate int64_t ii = (int64_t)pagesize * npages; 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gate if (ii >= mbyte) 364*0Sstevel@tonic-gate (void) printf("%ld Megabytes\n", 365*0Sstevel@tonic-gate (long)((ii+mbyte-1) / mbyte)); 366*0Sstevel@tonic-gate else 367*0Sstevel@tonic-gate (void) printf("%ld Kilobytes\n", 368*0Sstevel@tonic-gate (long)((ii+kbyte-1) / kbyte)); 369*0Sstevel@tonic-gate } 370*0Sstevel@tonic-gate 371*0Sstevel@tonic-gate if (opts.o_prominfo) { 372*0Sstevel@tonic-gate (void) printf("System Peripherals (PROM Nodes):\n\n"); 373*0Sstevel@tonic-gate if (do_prominfo() == 0) 374*0Sstevel@tonic-gate return (0); 375*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: Defaulting to non-PROM mode...\n", 376*0Sstevel@tonic-gate opts.o_progname); 377*0Sstevel@tonic-gate } 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate (void) printf("System Peripherals (Software Nodes):\n\n"); 380*0Sstevel@tonic-gate 381*0Sstevel@tonic-gate (void) prtconf_devinfo(); 382*0Sstevel@tonic-gate 383*0Sstevel@tonic-gate return (0); 384*0Sstevel@tonic-gate } 385