17836SJohn.Forte@Sun.COM /*
27836SJohn.Forte@Sun.COM * CDDL HEADER START
37836SJohn.Forte@Sun.COM *
47836SJohn.Forte@Sun.COM * The contents of this file are subject to the terms of the
57836SJohn.Forte@Sun.COM * Common Development and Distribution License (the "License").
67836SJohn.Forte@Sun.COM * You may not use this file except in compliance with the License.
77836SJohn.Forte@Sun.COM *
87836SJohn.Forte@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97836SJohn.Forte@Sun.COM * or http://www.opensolaris.org/os/licensing.
107836SJohn.Forte@Sun.COM * See the License for the specific language governing permissions
117836SJohn.Forte@Sun.COM * and limitations under the License.
127836SJohn.Forte@Sun.COM *
137836SJohn.Forte@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
147836SJohn.Forte@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157836SJohn.Forte@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
167836SJohn.Forte@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
177836SJohn.Forte@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
187836SJohn.Forte@Sun.COM *
197836SJohn.Forte@Sun.COM * CDDL HEADER END
207836SJohn.Forte@Sun.COM */
21*11576SSurya.Prakki@Sun.COM
227836SJohn.Forte@Sun.COM /*
23*11576SSurya.Prakki@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
247836SJohn.Forte@Sun.COM * Use is subject to license terms.
257836SJohn.Forte@Sun.COM */
267836SJohn.Forte@Sun.COM
277836SJohn.Forte@Sun.COM #include <sys/types.h>
287836SJohn.Forte@Sun.COM #include <sys/stat.h>
297836SJohn.Forte@Sun.COM #include <strings.h>
307836SJohn.Forte@Sun.COM #include <stdlib.h>
317836SJohn.Forte@Sun.COM #include <unistd.h>
327836SJohn.Forte@Sun.COM #include <errno.h>
337836SJohn.Forte@Sun.COM #include <stdio.h>
347836SJohn.Forte@Sun.COM #include <locale.h>
357836SJohn.Forte@Sun.COM
367836SJohn.Forte@Sun.COM #include <nsctl.h>
377836SJohn.Forte@Sun.COM #define __NSC_GEN__
387836SJohn.Forte@Sun.COM #include <sys/nsctl/nsc_gen.h>
397836SJohn.Forte@Sun.COM #include <sys/nsctl/nsc_mem.h>
407836SJohn.Forte@Sun.COM
417836SJohn.Forte@Sun.COM
427836SJohn.Forte@Sun.COM /*
437836SJohn.Forte@Sun.COM * Private functions from libsd.
447836SJohn.Forte@Sun.COM */
457836SJohn.Forte@Sun.COM extern int nsc_nvclean(int);
467836SJohn.Forte@Sun.COM extern int nsc_gmem_data(char *);
477836SJohn.Forte@Sun.COM extern int nsc_gmem_sizes(int *);
487836SJohn.Forte@Sun.COM
497836SJohn.Forte@Sun.COM /*
507836SJohn.Forte@Sun.COM * Local functions.
517836SJohn.Forte@Sun.COM */
527836SJohn.Forte@Sun.COM static int _nsc_gmem(void);
537836SJohn.Forte@Sun.COM static void show_maps(char *, int);
547836SJohn.Forte@Sun.COM
557836SJohn.Forte@Sun.COM
567836SJohn.Forte@Sun.COM static void
usage(void)577836SJohn.Forte@Sun.COM usage(void)
587836SJohn.Forte@Sun.COM {
59*11576SSurya.Prakki@Sun.COM (void) fprintf(stderr, gettext("usage: nscadm [-h] command\n"));
60*11576SSurya.Prakki@Sun.COM (void) fprintf(stderr, gettext("valid commands:\n"));
61*11576SSurya.Prakki@Sun.COM (void) fprintf(stderr, gettext(" freeze <device>\n"));
62*11576SSurya.Prakki@Sun.COM (void) fprintf(stderr, gettext(" unfreeze <device>\n"));
63*11576SSurya.Prakki@Sun.COM (void) fprintf(stderr, gettext(" isfrozen <device>\n"));
647836SJohn.Forte@Sun.COM }
657836SJohn.Forte@Sun.COM
667836SJohn.Forte@Sun.COM static void
is_chr_dev(char * dev,char * op)677836SJohn.Forte@Sun.COM is_chr_dev(char *dev, char *op)
687836SJohn.Forte@Sun.COM {
697836SJohn.Forte@Sun.COM struct stat sbuf;
707836SJohn.Forte@Sun.COM if (stat(dev, &sbuf) < 0) {
71*11576SSurya.Prakki@Sun.COM (void) fprintf(stderr, gettext("nscadm: "));
727836SJohn.Forte@Sun.COM perror(op);
737836SJohn.Forte@Sun.COM exit(255);
747836SJohn.Forte@Sun.COM }
757836SJohn.Forte@Sun.COM if (!S_ISCHR(sbuf.st_mode)) {
76*11576SSurya.Prakki@Sun.COM (void) fprintf(stderr, gettext("nscadm: %s: not a valid device "
777836SJohn.Forte@Sun.COM "<%s>\n"), op, dev);
787836SJohn.Forte@Sun.COM exit(255);
797836SJohn.Forte@Sun.COM }
807836SJohn.Forte@Sun.COM }
817836SJohn.Forte@Sun.COM
827836SJohn.Forte@Sun.COM int
main(int argc,char * argv[])837836SJohn.Forte@Sun.COM main(int argc, char *argv[])
847836SJohn.Forte@Sun.COM {
857836SJohn.Forte@Sun.COM extern int optind, opterr;
867836SJohn.Forte@Sun.COM int opt, rc;
877836SJohn.Forte@Sun.COM
887836SJohn.Forte@Sun.COM (void) setlocale(LC_ALL, "");
897836SJohn.Forte@Sun.COM (void) textdomain("nscadm");
907836SJohn.Forte@Sun.COM
917836SJohn.Forte@Sun.COM rc = 0;
927836SJohn.Forte@Sun.COM opterr = 0;
937836SJohn.Forte@Sun.COM
947836SJohn.Forte@Sun.COM while ((opt = getopt(argc, argv, "h")) != -1) {
957836SJohn.Forte@Sun.COM switch (opt) {
967836SJohn.Forte@Sun.COM case 'h':
977836SJohn.Forte@Sun.COM usage();
987836SJohn.Forte@Sun.COM exit(0);
997836SJohn.Forte@Sun.COM break;
1007836SJohn.Forte@Sun.COM default:
1017836SJohn.Forte@Sun.COM usage();
1027836SJohn.Forte@Sun.COM exit(255);
1037836SJohn.Forte@Sun.COM break;
1047836SJohn.Forte@Sun.COM }
1057836SJohn.Forte@Sun.COM }
1067836SJohn.Forte@Sun.COM
1077836SJohn.Forte@Sun.COM if (optind == argc) {
1087836SJohn.Forte@Sun.COM usage();
1097836SJohn.Forte@Sun.COM exit(255);
1107836SJohn.Forte@Sun.COM }
1117836SJohn.Forte@Sun.COM
1127836SJohn.Forte@Sun.COM if (strcoll(argv[optind], gettext("freeze")) == 0 ||
113*11576SSurya.Prakki@Sun.COM strcmp(argv[optind], "freeze") == 0) {
1147836SJohn.Forte@Sun.COM if (argc - optind != 2) {
1157836SJohn.Forte@Sun.COM usage();
1167836SJohn.Forte@Sun.COM exit(255);
1177836SJohn.Forte@Sun.COM }
1187836SJohn.Forte@Sun.COM
1197836SJohn.Forte@Sun.COM is_chr_dev(argv[optind+1], "freeze");
1207836SJohn.Forte@Sun.COM rc = nsc_isfrozen(argv[optind+1]);
1217836SJohn.Forte@Sun.COM if (rc < 0) {
1227836SJohn.Forte@Sun.COM perror(gettext("nscadm: freeze"));
1237836SJohn.Forte@Sun.COM exit(255);
1247836SJohn.Forte@Sun.COM } else if (rc != 0) {
1257836SJohn.Forte@Sun.COM rc = nsc_freeze(argv[optind+1]);
1267836SJohn.Forte@Sun.COM if (rc < 0) {
1277836SJohn.Forte@Sun.COM perror(gettext("nscadm: freeze"));
1287836SJohn.Forte@Sun.COM exit(255);
1297836SJohn.Forte@Sun.COM }
1307836SJohn.Forte@Sun.COM } else {
131*11576SSurya.Prakki@Sun.COM (void) fprintf(stderr, gettext("nscadm: device <%s> is "
1327836SJohn.Forte@Sun.COM "already frozen\n"), argv[optind+1]);
1337836SJohn.Forte@Sun.COM exit(255);
1347836SJohn.Forte@Sun.COM }
1357836SJohn.Forte@Sun.COM
136*11576SSurya.Prakki@Sun.COM (void) printf(gettext("nscadm: device <%s> frozen\n"),
137*11576SSurya.Prakki@Sun.COM argv[optind+1]);
1387836SJohn.Forte@Sun.COM } else if (strcoll(argv[optind], gettext("unfreeze")) == 0 ||
139*11576SSurya.Prakki@Sun.COM strcmp(argv[optind], "unfreeze") == 0) {
1407836SJohn.Forte@Sun.COM if (argc - optind != 2) {
1417836SJohn.Forte@Sun.COM usage();
1427836SJohn.Forte@Sun.COM exit(255);
1437836SJohn.Forte@Sun.COM }
1447836SJohn.Forte@Sun.COM
1457836SJohn.Forte@Sun.COM is_chr_dev(argv[optind+1], "unfreeze");
1467836SJohn.Forte@Sun.COM rc = nsc_isfrozen(argv[optind+1]);
1477836SJohn.Forte@Sun.COM if (rc < 0) {
1487836SJohn.Forte@Sun.COM perror(gettext("nscadm: unfreeze"));
1497836SJohn.Forte@Sun.COM exit(255);
1507836SJohn.Forte@Sun.COM } else if (rc == 0) {
1517836SJohn.Forte@Sun.COM rc = nsc_unfreeze(argv[optind+1]);
1527836SJohn.Forte@Sun.COM if (rc < 0) {
1537836SJohn.Forte@Sun.COM perror(gettext("nscadm: unfreeze"));
1547836SJohn.Forte@Sun.COM exit(255);
1557836SJohn.Forte@Sun.COM }
1567836SJohn.Forte@Sun.COM } else {
157*11576SSurya.Prakki@Sun.COM (void) fprintf(stderr,
158*11576SSurya.Prakki@Sun.COM gettext("nscadm: device <%s> is not "
1597836SJohn.Forte@Sun.COM "frozen\n"), argv[optind+1]);
1607836SJohn.Forte@Sun.COM exit(255);
1617836SJohn.Forte@Sun.COM }
1627836SJohn.Forte@Sun.COM
163*11576SSurya.Prakki@Sun.COM (void) printf(gettext("nscadm: device <%s> unfrozen\n"),
164*11576SSurya.Prakki@Sun.COM argv[optind+1]);
1657836SJohn.Forte@Sun.COM } else if (strcoll(argv[optind], gettext("isfrozen")) == 0 ||
166*11576SSurya.Prakki@Sun.COM strcmp(argv[optind], "isfrozen") == 0) {
1677836SJohn.Forte@Sun.COM if (argc - optind != 2) {
1687836SJohn.Forte@Sun.COM usage();
1697836SJohn.Forte@Sun.COM exit(255);
1707836SJohn.Forte@Sun.COM }
1717836SJohn.Forte@Sun.COM
1727836SJohn.Forte@Sun.COM is_chr_dev(argv[optind+1], "isfrozen");
1737836SJohn.Forte@Sun.COM rc = nsc_isfrozen(argv[optind+1]);
1747836SJohn.Forte@Sun.COM if (rc < 0) {
1757836SJohn.Forte@Sun.COM perror(gettext("nscadm: isfrozen"));
1767836SJohn.Forte@Sun.COM exit(255);
1777836SJohn.Forte@Sun.COM }
1787836SJohn.Forte@Sun.COM
179*11576SSurya.Prakki@Sun.COM (void) printf(gettext("nscadm: device <%s> is %sfrozen\n"),
180*11576SSurya.Prakki@Sun.COM argv[optind+1], rc ? gettext("not ") : "");
1817836SJohn.Forte@Sun.COM #ifdef DEBUG
1827836SJohn.Forte@Sun.COM } else if (strcoll(argv[optind], gettext("nvclean")) == 0 ||
183*11576SSurya.Prakki@Sun.COM strcmp(argv[optind], "nvclean") == 0) {
1847836SJohn.Forte@Sun.COM rc = nsc_nvclean(0);
1857836SJohn.Forte@Sun.COM if (rc < 0) {
1867836SJohn.Forte@Sun.COM perror(gettext("nscadm: nvclean"));
1877836SJohn.Forte@Sun.COM exit(255);
1887836SJohn.Forte@Sun.COM }
1897836SJohn.Forte@Sun.COM } else if (strcoll(argv[optind], gettext("nvclean_force")) == 0 ||
190*11576SSurya.Prakki@Sun.COM strcmp(argv[optind], "nvclean_force") == 0) {
1917836SJohn.Forte@Sun.COM rc = nsc_nvclean(1);
1927836SJohn.Forte@Sun.COM if (rc < 0) {
1937836SJohn.Forte@Sun.COM perror(gettext("nscadm: nvclean_force"));
1947836SJohn.Forte@Sun.COM exit(255);
1957836SJohn.Forte@Sun.COM }
1967836SJohn.Forte@Sun.COM #endif /* DEBUG */
1977836SJohn.Forte@Sun.COM } else if (strcoll(argv[optind], gettext("gmem")) == 0 ||
198*11576SSurya.Prakki@Sun.COM strcmp(argv[optind], "gmem") == 0) {
1997836SJohn.Forte@Sun.COM rc = _nsc_gmem();
2007836SJohn.Forte@Sun.COM if (rc < 0) {
2017836SJohn.Forte@Sun.COM perror(gettext("nscadm: gmem"));
2027836SJohn.Forte@Sun.COM exit(255);
2037836SJohn.Forte@Sun.COM }
2047836SJohn.Forte@Sun.COM } else {
2057836SJohn.Forte@Sun.COM usage();
2067836SJohn.Forte@Sun.COM exit(255);
2077836SJohn.Forte@Sun.COM }
2087836SJohn.Forte@Sun.COM
2097836SJohn.Forte@Sun.COM return (rc);
2107836SJohn.Forte@Sun.COM }
2117836SJohn.Forte@Sun.COM
2127836SJohn.Forte@Sun.COM
2137836SJohn.Forte@Sun.COM static int
_nsc_gmem(void)2147836SJohn.Forte@Sun.COM _nsc_gmem(void)
2157836SJohn.Forte@Sun.COM {
2167836SJohn.Forte@Sun.COM char *addr;
2177836SJohn.Forte@Sun.COM int size;
2187836SJohn.Forte@Sun.COM int rc = 0;
2197836SJohn.Forte@Sun.COM
2207836SJohn.Forte@Sun.COM rc = nsc_gmem_sizes(&size);
2217836SJohn.Forte@Sun.COM
2227836SJohn.Forte@Sun.COM if (rc < 0)
2237836SJohn.Forte@Sun.COM return (rc);
2247836SJohn.Forte@Sun.COM
225*11576SSurya.Prakki@Sun.COM (void) printf(gettext("size %d\n"), size);
2267836SJohn.Forte@Sun.COM
2277836SJohn.Forte@Sun.COM if ((addr = (char *)malloc(size * 2)) == NULL) {
2287836SJohn.Forte@Sun.COM errno = ENOMEM;
2297836SJohn.Forte@Sun.COM return (-1);
2307836SJohn.Forte@Sun.COM }
2317836SJohn.Forte@Sun.COM
2327836SJohn.Forte@Sun.COM rc = nsc_gmem_data(addr);
2337836SJohn.Forte@Sun.COM
2347836SJohn.Forte@Sun.COM if (rc < 0) {
2357836SJohn.Forte@Sun.COM free(addr);
2367836SJohn.Forte@Sun.COM return (rc);
2377836SJohn.Forte@Sun.COM }
2387836SJohn.Forte@Sun.COM
239*11576SSurya.Prakki@Sun.COM (void) printf(gettext("Global map entries:\n"));
2407836SJohn.Forte@Sun.COM show_maps(addr, size);
2417836SJohn.Forte@Sun.COM
242*11576SSurya.Prakki@Sun.COM (void) printf(gettext("\nGlobal NVMEM map entries:\n"));
2437836SJohn.Forte@Sun.COM show_maps(addr + size, size);
2447836SJohn.Forte@Sun.COM
2457836SJohn.Forte@Sun.COM free(addr);
2467836SJohn.Forte@Sun.COM return (0);
2477836SJohn.Forte@Sun.COM }
2487836SJohn.Forte@Sun.COM
2497836SJohn.Forte@Sun.COM
2507836SJohn.Forte@Sun.COM static void
show_maps(char * addr,int len)2517836SJohn.Forte@Sun.COM show_maps(char *addr, int len)
2527836SJohn.Forte@Sun.COM {
2537836SJohn.Forte@Sun.COM /* LINTED alignment of cast ok */
2547836SJohn.Forte@Sun.COM nsc_rmhdr_t *rhp = (nsc_rmhdr_t *)addr;
2557836SJohn.Forte@Sun.COM nsc_rmmap_t *rmap;
2567836SJohn.Forte@Sun.COM char tname[_NSC_MAXNAME + 1];
2577836SJohn.Forte@Sun.COM int i;
2587836SJohn.Forte@Sun.COM
259*11576SSurya.Prakki@Sun.COM (void) printf(
260*11576SSurya.Prakki@Sun.COM gettext("magic 0x%x ver %d size %d dirty (nvmem systems): %d\n"),
2617836SJohn.Forte@Sun.COM rhp->magic, rhp->ver, rhp->size, rhp->rh_dirty);
2627836SJohn.Forte@Sun.COM
2637836SJohn.Forte@Sun.COM for (i = 0, rmap = rhp->map;
2647836SJohn.Forte@Sun.COM /* LINTED alignment of cast ok */
2657836SJohn.Forte@Sun.COM rmap < (nsc_rmmap_t *)(addr + len); ++i, ++rmap) {
2667836SJohn.Forte@Sun.COM if (!rmap->name[0])
2677836SJohn.Forte@Sun.COM continue;
268*11576SSurya.Prakki@Sun.COM (void) strncpy(tname, rmap->name, _NSC_MAXNAME);
269*11576SSurya.Prakki@Sun.COM (void) strcpy(&tname[strlen(tname)], " ");
2707836SJohn.Forte@Sun.COM tname[_NSC_MAXNAME] = '\0';
271*11576SSurya.Prakki@Sun.COM (void) printf(gettext(
2727836SJohn.Forte@Sun.COM "%d:\tname %s\toffset 0x%x size 0x%x inuse 0x%x\n"),
2737836SJohn.Forte@Sun.COM i, tname, rmap->offset, rmap->size, rmap->inuse);
2747836SJohn.Forte@Sun.COM }
2757836SJohn.Forte@Sun.COM }
276