xref: /illumos-gate/usr/src/cmd/prtconf/prtconf.c (revision 1c7f36ec)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c3a9724dSVikram Hegde  * Common Development and Distribution License (the "License").
6c3a9724dSVikram Hegde  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2123a1cceaSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
2387ea2c5cSAnil udupa  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24a104dbc0SJerry Jelinek  * Copyright 2011, Joyent, Inc. All rights reserved.
2549ca4dd9SPeter Tribble  * Copyright (c) 2019 Peter Tribble.
26f73c681dSSachidananda Urs  * Copyright (c) 2022 Sachidananda Urs <sacchi@gmail.com>
27*1c7f36ecSRobert Mustacchi  * Copyright 2022 Oxide Computer Company
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
317c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdarg.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <strings.h>
387c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
41*1c7f36ecSRobert Mustacchi #include <err.h>
427c478bd9Sstevel@tonic-gate #include "prtconf.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate struct prt_opts	opts;
457c478bd9Sstevel@tonic-gate struct prt_dbg	dbg;
467c478bd9Sstevel@tonic-gate static char	new_path[MAXPATHLEN];
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	INDENT_LENGTH	4
497c478bd9Sstevel@tonic-gate 
50524b24f9SJudy Chen static const char *usage =
5149ca4dd9SPeter Tribble 	"%s [ -F | -m | -V | -x | -abcdvpPD ] [ <device_path > ]\n";
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static void
setpname(const char * name)5423a1cceaSRoger A. Faulkner setpname(const char *name)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	char *p;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	if (name == NULL)
597c478bd9Sstevel@tonic-gate 		opts.o_progname = "prtconf";
602545779bSRobert Mustacchi 	else if ((p = strrchr(name, '/')) != NULL)
617c478bd9Sstevel@tonic-gate 		opts.o_progname = (const char *) p + 1;
627c478bd9Sstevel@tonic-gate 	else
637c478bd9Sstevel@tonic-gate 		opts.o_progname = name;
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
677c478bd9Sstevel@tonic-gate void
dprintf(const char * fmt,...)687c478bd9Sstevel@tonic-gate dprintf(const char *fmt, ...)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	if (dbg.d_debug) {
717c478bd9Sstevel@tonic-gate 		va_list ap;
727c478bd9Sstevel@tonic-gate 		va_start(ap, fmt);
737c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
747c478bd9Sstevel@tonic-gate 		va_end(ap);
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate void
indent_to_level(int ilev)797c478bd9Sstevel@tonic-gate indent_to_level(int ilev)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	(void) printf("%*s", INDENT_LENGTH * ilev, "");
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * debug version has two more flags:
867c478bd9Sstevel@tonic-gate  *	-L force load driver
877c478bd9Sstevel@tonic-gate  *	-M: print per driver list
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #ifdef	DEBUG
9149ca4dd9SPeter Tribble static const char *optstring = "abcdDvVxmpPFf:M:LuC";
927c478bd9Sstevel@tonic-gate #else
93a104dbc0SJerry Jelinek static const char *optstring = "abcdDvVxmpPFf:uC";
947c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])977c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	long pagesize, npages;
1007c478bd9Sstevel@tonic-gate 	int c, ret;
1017c478bd9Sstevel@tonic-gate 	char hw_provider[SYS_NMLN];
1027c478bd9Sstevel@tonic-gate 
10323a1cceaSRoger A. Faulkner 	setpname(argv[0]);
1047c478bd9Sstevel@tonic-gate 	opts.o_promdev = "/dev/openprom";
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, optstring)) != -1)  {
1077c478bd9Sstevel@tonic-gate 		switch (c)  {
1087c478bd9Sstevel@tonic-gate 		case 'a':
1097c478bd9Sstevel@tonic-gate 			++opts.o_ancestors;
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		case 'b':
1127c478bd9Sstevel@tonic-gate 			++opts.o_productinfo;
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 		case 'c':
1157c478bd9Sstevel@tonic-gate 			++opts.o_children;
1167c478bd9Sstevel@tonic-gate 			break;
117524b24f9SJudy Chen 		case 'd':
118524b24f9SJudy Chen 			++opts.o_pciid;
119524b24f9SJudy Chen 			break;
1207c478bd9Sstevel@tonic-gate 		case 'D':
1217c478bd9Sstevel@tonic-gate 			++opts.o_drv_name;
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 		case 'v':
1247c478bd9Sstevel@tonic-gate 			++opts.o_verbose;
1257c478bd9Sstevel@tonic-gate 			break;
126a104dbc0SJerry Jelinek 		case 'm':
127a104dbc0SJerry Jelinek 			++opts.o_memory;
128a104dbc0SJerry Jelinek 			break;
1297c478bd9Sstevel@tonic-gate 		case 'p':
1307c478bd9Sstevel@tonic-gate 			++opts.o_prominfo;
1317c478bd9Sstevel@tonic-gate 			break;
1327c478bd9Sstevel@tonic-gate 		case 'f':
1337c478bd9Sstevel@tonic-gate 			opts.o_promdev = optarg;
1347c478bd9Sstevel@tonic-gate 			break;
1357c478bd9Sstevel@tonic-gate 		case 'V':
1367c478bd9Sstevel@tonic-gate 			++opts.o_promversion;
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 		case 'x':
1397c478bd9Sstevel@tonic-gate 			++opts.o_prom_ready64;
1407c478bd9Sstevel@tonic-gate 			break;
1417c478bd9Sstevel@tonic-gate 		case 'F':
1427c478bd9Sstevel@tonic-gate 			++opts.o_fbname;
1437c478bd9Sstevel@tonic-gate 			++opts.o_noheader;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		case 'P':
1467c478bd9Sstevel@tonic-gate 			++opts.o_pseudodevs;
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		case 'C':
1497c478bd9Sstevel@tonic-gate 			++opts.o_forcecache;
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1527c478bd9Sstevel@tonic-gate 		case 'M':
1537c478bd9Sstevel@tonic-gate 			dbg.d_drivername = optarg;
1547c478bd9Sstevel@tonic-gate 			++dbg.d_bydriver;
1557c478bd9Sstevel@tonic-gate 			break;
1567c478bd9Sstevel@tonic-gate 		case 'L':
1577c478bd9Sstevel@tonic-gate 			++dbg.d_forceload;
1587c478bd9Sstevel@tonic-gate 			break;
1597c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		default:
1627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, opts.o_progname);
1637c478bd9Sstevel@tonic-gate 			return (1);
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	(void) uname(&opts.o_uts);
1687c478bd9Sstevel@tonic-gate 
169*1c7f36ecSRobert Mustacchi 	if (opts.o_verbose || opts.o_pciid) {
170*1c7f36ecSRobert Mustacchi 		opts.o_pcidb = pcidb_open(PCIDB_VERSION);
171*1c7f36ecSRobert Mustacchi 		if (opts.o_pcidb == NULL) {
172*1c7f36ecSRobert Mustacchi 			warn("pcidb facility not available, PCI names will "
173*1c7f36ecSRobert Mustacchi 			    "not be translated");
174*1c7f36ecSRobert Mustacchi 		}
175*1c7f36ecSRobert Mustacchi 	}
176*1c7f36ecSRobert Mustacchi 
1777c478bd9Sstevel@tonic-gate 	if (opts.o_fbname)
1787c478bd9Sstevel@tonic-gate 		return (do_fbname());
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (opts.o_promversion)
1817c478bd9Sstevel@tonic-gate 		return (do_promversion());
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (opts.o_prom_ready64)
18449ca4dd9SPeter Tribble 		return (0);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (opts.o_productinfo)
1877c478bd9Sstevel@tonic-gate 		return (do_productinfo());
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	opts.o_devices_path = NULL;
1907c478bd9Sstevel@tonic-gate 	opts.o_devt = DDI_DEV_T_NONE;
1917c478bd9Sstevel@tonic-gate 	opts.o_target = 0;
1927c478bd9Sstevel@tonic-gate 	if (optind < argc) {
1937c478bd9Sstevel@tonic-gate 		struct stat	sinfo;
1947c478bd9Sstevel@tonic-gate 		char		*path = argv[optind];
1957c478bd9Sstevel@tonic-gate 		int		error;
1967c478bd9Sstevel@tonic-gate 
197c3a9724dSVikram Hegde 		if (opts.o_prominfo) {
198c3a9724dSVikram Hegde 			/* PROM tree cannot be used with path */
199c3a9724dSVikram Hegde 			(void) fprintf(stderr, "%s: path and -p option are "
200c3a9724dSVikram Hegde 			    "mutually exclusive\n", opts.o_progname);
201c3a9724dSVikram Hegde 			return (1);
202c3a9724dSVikram Hegde 		}
203c3a9724dSVikram Hegde 
2047c478bd9Sstevel@tonic-gate 		if (strlen(path) >= MAXPATHLEN) {
2057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: "
2067c478bd9Sstevel@tonic-gate 			    "path specified is too long\n", opts.o_progname);
2077c478bd9Sstevel@tonic-gate 			return (1);
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 
2102545779bSRobert Mustacchi 		if ((error = stat(path, &sinfo)) != 0) {
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 			/* an invalid path was specified */
2137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: invalid path specified\n",
214c3a9724dSVikram Hegde 			    opts.o_progname);
2157c478bd9Sstevel@tonic-gate 			return (1);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		} else if (((sinfo.st_mode & S_IFMT) == S_IFCHR) ||
218c3a9724dSVikram Hegde 		    ((sinfo.st_mode & S_IFMT) == S_IFBLK)) {
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 			opts.o_devt = sinfo.st_rdev;
2217c478bd9Sstevel@tonic-gate 			error = 0;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		} else if ((sinfo.st_mode & S_IFMT) == S_IFDIR) {
2247c478bd9Sstevel@tonic-gate 			size_t	len, plen;
2257c478bd9Sstevel@tonic-gate 
226f73c681dSSachidananda Urs 			if (realpath(path, new_path) == NULL) {
227f73c681dSSachidananda Urs 				(void) fprintf(stderr, "%s: invalid device"
228f73c681dSSachidananda Urs 				    " path specified\n",
229f73c681dSSachidananda Urs 				    opts.o_progname);
230f73c681dSSachidananda Urs 				return (1);
231f73c681dSSachidananda Urs 			}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 			len = strlen(new_path);
2347c478bd9Sstevel@tonic-gate 			plen = strlen("/devices");
2357c478bd9Sstevel@tonic-gate 			if (len < plen) {
2367c478bd9Sstevel@tonic-gate 				/* This is not a valid /devices path */
2377c478bd9Sstevel@tonic-gate 				error = 1;
2387c478bd9Sstevel@tonic-gate 			} else if ((len == plen) &&
2397c478bd9Sstevel@tonic-gate 			    (strcmp(new_path, "/devices") == 0)) {
2407c478bd9Sstevel@tonic-gate 				/* /devices is the root nexus */
2417c478bd9Sstevel@tonic-gate 				opts.o_devices_path = "/";
2427c478bd9Sstevel@tonic-gate 				error = 0;
2437c478bd9Sstevel@tonic-gate 			} else if (strncmp(new_path, "/devices/", plen + 1)) {
2447c478bd9Sstevel@tonic-gate 				/* This is not a valid /devices path */
2457c478bd9Sstevel@tonic-gate 				error = 1;
2467c478bd9Sstevel@tonic-gate 			} else {
2477c478bd9Sstevel@tonic-gate 				/* a /devices/ path was specified */
2487c478bd9Sstevel@tonic-gate 				opts.o_devices_path = new_path + plen;
2497c478bd9Sstevel@tonic-gate 				error = 0;
2507c478bd9Sstevel@tonic-gate 			}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		} else {
2537c478bd9Sstevel@tonic-gate 			/* an invalid device path was specified */
2547c478bd9Sstevel@tonic-gate 			error = 1;
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		if (error) {
2587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: "
2597c478bd9Sstevel@tonic-gate 			    "invalid device path specified\n",
2607c478bd9Sstevel@tonic-gate 			    opts.o_progname);
2617c478bd9Sstevel@tonic-gate 			return (1);
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		opts.o_target = 1;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if ((opts.o_ancestors || opts.o_children) && (!opts.o_target)) {
2687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: options require a device path\n",
2697c478bd9Sstevel@tonic-gate 		    opts.o_progname);
2707c478bd9Sstevel@tonic-gate 		return (1);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (opts.o_target) {
2747c478bd9Sstevel@tonic-gate 		prtconf_devinfo();
2757c478bd9Sstevel@tonic-gate 		return (0);
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
278a104dbc0SJerry Jelinek 	if (!opts.o_memory) {
279a104dbc0SJerry Jelinek 		ret = sysinfo(SI_HW_PROVIDER, hw_provider,
2807c478bd9Sstevel@tonic-gate 		    sizeof (hw_provider));
281a104dbc0SJerry Jelinek 		/*
282a104dbc0SJerry Jelinek 		 * If 0 bytes are returned (the system returns '1', for the \0),
283a104dbc0SJerry Jelinek 		 * we're probably on x86, default to "Unknown Hardware Vendor".
284a104dbc0SJerry Jelinek 		 */
285a104dbc0SJerry Jelinek 		if (ret <= 1) {
286a104dbc0SJerry Jelinek 			(void) strncpy(hw_provider, "Unknown Hardware Vendor",
287a104dbc0SJerry Jelinek 			    sizeof (hw_provider));
288a104dbc0SJerry Jelinek 		}
289a104dbc0SJerry Jelinek 		(void) printf("System Configuration:  %s  %s\n", hw_provider,
290a104dbc0SJerry Jelinek 		    opts.o_uts.machine);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	pagesize = sysconf(_SC_PAGESIZE);
2947c478bd9Sstevel@tonic-gate 	npages = sysconf(_SC_PHYS_PAGES);
295a104dbc0SJerry Jelinek 	if (pagesize == -1 || npages == -1) {
296a104dbc0SJerry Jelinek 		if (opts.o_memory) {
297a104dbc0SJerry Jelinek 			(void) printf("0\n");
298a104dbc0SJerry Jelinek 			return (1);
299a104dbc0SJerry Jelinek 		} else {
300a104dbc0SJerry Jelinek 			(void) printf("Memory size: unable to determine\n");
301a104dbc0SJerry Jelinek 		}
302a104dbc0SJerry Jelinek 	} else {
3037c478bd9Sstevel@tonic-gate 		const int64_t mbyte = 1024 * 1024;
3047c478bd9Sstevel@tonic-gate 		int64_t ii = (int64_t)pagesize * npages;
3057c478bd9Sstevel@tonic-gate 
306a104dbc0SJerry Jelinek 		if (opts.o_memory) {
307a104dbc0SJerry Jelinek 			(void) printf("%ld\n", (long)((ii+mbyte-1) / mbyte));
308a104dbc0SJerry Jelinek 			return (0);
309a104dbc0SJerry Jelinek 		} else {
310a104dbc0SJerry Jelinek 			(void) printf("Memory size: %ld Megabytes\n",
311c3a9724dSVikram Hegde 			    (long)((ii+mbyte-1) / mbyte));
312a104dbc0SJerry Jelinek 		}
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (opts.o_prominfo) {
3167c478bd9Sstevel@tonic-gate 		(void) printf("System Peripherals (PROM Nodes):\n\n");
3177c478bd9Sstevel@tonic-gate 		if (do_prominfo() == 0)
3187c478bd9Sstevel@tonic-gate 			return (0);
3197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: Defaulting to non-PROM mode...\n",
3207c478bd9Sstevel@tonic-gate 		    opts.o_progname);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	(void) printf("System Peripherals (Software Nodes):\n\n");
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	(void) prtconf_devinfo();
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	return (0);
3287c478bd9Sstevel@tonic-gate }
329