1fcf3ce44SJohn Forte /* 2fcf3ce44SJohn Forte * CDDL HEADER START 3fcf3ce44SJohn Forte * 4fcf3ce44SJohn Forte * The contents of this file are subject to the terms of the 5fcf3ce44SJohn Forte * Common Development and Distribution License (the "License"). 6fcf3ce44SJohn Forte * You may not use this file except in compliance with the License. 7fcf3ce44SJohn Forte * 8fcf3ce44SJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fcf3ce44SJohn Forte * or http://www.opensolaris.org/os/licensing. 10fcf3ce44SJohn Forte * See the License for the specific language governing permissions 11fcf3ce44SJohn Forte * and limitations under the License. 12fcf3ce44SJohn Forte * 13fcf3ce44SJohn Forte * When distributing Covered Code, include this CDDL HEADER in each 14fcf3ce44SJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fcf3ce44SJohn Forte * If applicable, add the following below this CDDL HEADER, with the 16fcf3ce44SJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying 17fcf3ce44SJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner] 18fcf3ce44SJohn Forte * 19fcf3ce44SJohn Forte * CDDL HEADER END 20fcf3ce44SJohn Forte */ 21*570de38fSSurya Prakki 22fcf3ce44SJohn Forte /* 23*570de38fSSurya Prakki * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24fcf3ce44SJohn Forte * Use is subject to license terms. 25fcf3ce44SJohn Forte */ 26fcf3ce44SJohn Forte 27fcf3ce44SJohn Forte #include <sys/types.h> 28fcf3ce44SJohn Forte #include <sys/stat.h> 29fcf3ce44SJohn Forte #include <strings.h> 30fcf3ce44SJohn Forte #include <stdlib.h> 31fcf3ce44SJohn Forte #include <unistd.h> 32fcf3ce44SJohn Forte #include <errno.h> 33fcf3ce44SJohn Forte #include <stdio.h> 34fcf3ce44SJohn Forte #include <locale.h> 35fcf3ce44SJohn Forte 36fcf3ce44SJohn Forte #include <nsctl.h> 37fcf3ce44SJohn Forte #define __NSC_GEN__ 38fcf3ce44SJohn Forte #include <sys/nsctl/nsc_gen.h> 39fcf3ce44SJohn Forte #include <sys/nsctl/nsc_mem.h> 40fcf3ce44SJohn Forte 41fcf3ce44SJohn Forte 42fcf3ce44SJohn Forte /* 43fcf3ce44SJohn Forte * Private functions from libsd. 44fcf3ce44SJohn Forte */ 45fcf3ce44SJohn Forte extern int nsc_nvclean(int); 46fcf3ce44SJohn Forte extern int nsc_gmem_data(char *); 47fcf3ce44SJohn Forte extern int nsc_gmem_sizes(int *); 48fcf3ce44SJohn Forte 49fcf3ce44SJohn Forte /* 50fcf3ce44SJohn Forte * Local functions. 51fcf3ce44SJohn Forte */ 52fcf3ce44SJohn Forte static int _nsc_gmem(void); 53fcf3ce44SJohn Forte static void show_maps(char *, int); 54fcf3ce44SJohn Forte 55fcf3ce44SJohn Forte 56fcf3ce44SJohn Forte static void 57fcf3ce44SJohn Forte usage(void) 58fcf3ce44SJohn Forte { 59*570de38fSSurya Prakki (void) fprintf(stderr, gettext("usage: nscadm [-h] command\n")); 60*570de38fSSurya Prakki (void) fprintf(stderr, gettext("valid commands:\n")); 61*570de38fSSurya Prakki (void) fprintf(stderr, gettext(" freeze <device>\n")); 62*570de38fSSurya Prakki (void) fprintf(stderr, gettext(" unfreeze <device>\n")); 63*570de38fSSurya Prakki (void) fprintf(stderr, gettext(" isfrozen <device>\n")); 64fcf3ce44SJohn Forte } 65fcf3ce44SJohn Forte 66fcf3ce44SJohn Forte static void 67fcf3ce44SJohn Forte is_chr_dev(char *dev, char *op) 68fcf3ce44SJohn Forte { 69fcf3ce44SJohn Forte struct stat sbuf; 70fcf3ce44SJohn Forte if (stat(dev, &sbuf) < 0) { 71*570de38fSSurya Prakki (void) fprintf(stderr, gettext("nscadm: ")); 72fcf3ce44SJohn Forte perror(op); 73fcf3ce44SJohn Forte exit(255); 74fcf3ce44SJohn Forte } 75fcf3ce44SJohn Forte if (!S_ISCHR(sbuf.st_mode)) { 76*570de38fSSurya Prakki (void) fprintf(stderr, gettext("nscadm: %s: not a valid device " 77fcf3ce44SJohn Forte "<%s>\n"), op, dev); 78fcf3ce44SJohn Forte exit(255); 79fcf3ce44SJohn Forte } 80fcf3ce44SJohn Forte } 81fcf3ce44SJohn Forte 82fcf3ce44SJohn Forte int 83fcf3ce44SJohn Forte main(int argc, char *argv[]) 84fcf3ce44SJohn Forte { 85fcf3ce44SJohn Forte extern int optind, opterr; 86fcf3ce44SJohn Forte int opt, rc; 87fcf3ce44SJohn Forte 88fcf3ce44SJohn Forte (void) setlocale(LC_ALL, ""); 89fcf3ce44SJohn Forte (void) textdomain("nscadm"); 90fcf3ce44SJohn Forte 91fcf3ce44SJohn Forte rc = 0; 92fcf3ce44SJohn Forte opterr = 0; 93fcf3ce44SJohn Forte 94fcf3ce44SJohn Forte while ((opt = getopt(argc, argv, "h")) != -1) { 95fcf3ce44SJohn Forte switch (opt) { 96fcf3ce44SJohn Forte case 'h': 97fcf3ce44SJohn Forte usage(); 98fcf3ce44SJohn Forte exit(0); 99fcf3ce44SJohn Forte break; 100fcf3ce44SJohn Forte default: 101fcf3ce44SJohn Forte usage(); 102fcf3ce44SJohn Forte exit(255); 103fcf3ce44SJohn Forte break; 104fcf3ce44SJohn Forte } 105fcf3ce44SJohn Forte } 106fcf3ce44SJohn Forte 107fcf3ce44SJohn Forte if (optind == argc) { 108fcf3ce44SJohn Forte usage(); 109fcf3ce44SJohn Forte exit(255); 110fcf3ce44SJohn Forte } 111fcf3ce44SJohn Forte 112fcf3ce44SJohn Forte if (strcoll(argv[optind], gettext("freeze")) == 0 || 113fcf3ce44SJohn Forte strcmp(argv[optind], "freeze") == 0) { 114fcf3ce44SJohn Forte if (argc - optind != 2) { 115fcf3ce44SJohn Forte usage(); 116fcf3ce44SJohn Forte exit(255); 117fcf3ce44SJohn Forte } 118fcf3ce44SJohn Forte 119fcf3ce44SJohn Forte is_chr_dev(argv[optind+1], "freeze"); 120fcf3ce44SJohn Forte rc = nsc_isfrozen(argv[optind+1]); 121fcf3ce44SJohn Forte if (rc < 0) { 122fcf3ce44SJohn Forte perror(gettext("nscadm: freeze")); 123fcf3ce44SJohn Forte exit(255); 124fcf3ce44SJohn Forte } else if (rc != 0) { 125fcf3ce44SJohn Forte rc = nsc_freeze(argv[optind+1]); 126fcf3ce44SJohn Forte if (rc < 0) { 127fcf3ce44SJohn Forte perror(gettext("nscadm: freeze")); 128fcf3ce44SJohn Forte exit(255); 129fcf3ce44SJohn Forte } 130fcf3ce44SJohn Forte } else { 131*570de38fSSurya Prakki (void) fprintf(stderr, gettext("nscadm: device <%s> is " 132fcf3ce44SJohn Forte "already frozen\n"), argv[optind+1]); 133fcf3ce44SJohn Forte exit(255); 134fcf3ce44SJohn Forte } 135fcf3ce44SJohn Forte 136*570de38fSSurya Prakki (void) printf(gettext("nscadm: device <%s> frozen\n"), 137*570de38fSSurya Prakki argv[optind+1]); 138fcf3ce44SJohn Forte } else if (strcoll(argv[optind], gettext("unfreeze")) == 0 || 139fcf3ce44SJohn Forte strcmp(argv[optind], "unfreeze") == 0) { 140fcf3ce44SJohn Forte if (argc - optind != 2) { 141fcf3ce44SJohn Forte usage(); 142fcf3ce44SJohn Forte exit(255); 143fcf3ce44SJohn Forte } 144fcf3ce44SJohn Forte 145fcf3ce44SJohn Forte is_chr_dev(argv[optind+1], "unfreeze"); 146fcf3ce44SJohn Forte rc = nsc_isfrozen(argv[optind+1]); 147fcf3ce44SJohn Forte if (rc < 0) { 148fcf3ce44SJohn Forte perror(gettext("nscadm: unfreeze")); 149fcf3ce44SJohn Forte exit(255); 150fcf3ce44SJohn Forte } else if (rc == 0) { 151fcf3ce44SJohn Forte rc = nsc_unfreeze(argv[optind+1]); 152fcf3ce44SJohn Forte if (rc < 0) { 153fcf3ce44SJohn Forte perror(gettext("nscadm: unfreeze")); 154fcf3ce44SJohn Forte exit(255); 155fcf3ce44SJohn Forte } 156fcf3ce44SJohn Forte } else { 157*570de38fSSurya Prakki (void) fprintf(stderr, 158*570de38fSSurya Prakki gettext("nscadm: device <%s> is not " 159fcf3ce44SJohn Forte "frozen\n"), argv[optind+1]); 160fcf3ce44SJohn Forte exit(255); 161fcf3ce44SJohn Forte } 162fcf3ce44SJohn Forte 163*570de38fSSurya Prakki (void) printf(gettext("nscadm: device <%s> unfrozen\n"), 164fcf3ce44SJohn Forte argv[optind+1]); 165fcf3ce44SJohn Forte } else if (strcoll(argv[optind], gettext("isfrozen")) == 0 || 166fcf3ce44SJohn Forte strcmp(argv[optind], "isfrozen") == 0) { 167fcf3ce44SJohn Forte if (argc - optind != 2) { 168fcf3ce44SJohn Forte usage(); 169fcf3ce44SJohn Forte exit(255); 170fcf3ce44SJohn Forte } 171fcf3ce44SJohn Forte 172fcf3ce44SJohn Forte is_chr_dev(argv[optind+1], "isfrozen"); 173fcf3ce44SJohn Forte rc = nsc_isfrozen(argv[optind+1]); 174fcf3ce44SJohn Forte if (rc < 0) { 175fcf3ce44SJohn Forte perror(gettext("nscadm: isfrozen")); 176fcf3ce44SJohn Forte exit(255); 177fcf3ce44SJohn Forte } 178fcf3ce44SJohn Forte 179*570de38fSSurya Prakki (void) printf(gettext("nscadm: device <%s> is %sfrozen\n"), 180fcf3ce44SJohn Forte argv[optind+1], rc ? gettext("not ") : ""); 181fcf3ce44SJohn Forte #ifdef DEBUG 182fcf3ce44SJohn Forte } else if (strcoll(argv[optind], gettext("nvclean")) == 0 || 183fcf3ce44SJohn Forte strcmp(argv[optind], "nvclean") == 0) { 184fcf3ce44SJohn Forte rc = nsc_nvclean(0); 185fcf3ce44SJohn Forte if (rc < 0) { 186fcf3ce44SJohn Forte perror(gettext("nscadm: nvclean")); 187fcf3ce44SJohn Forte exit(255); 188fcf3ce44SJohn Forte } 189fcf3ce44SJohn Forte } else if (strcoll(argv[optind], gettext("nvclean_force")) == 0 || 190fcf3ce44SJohn Forte strcmp(argv[optind], "nvclean_force") == 0) { 191fcf3ce44SJohn Forte rc = nsc_nvclean(1); 192fcf3ce44SJohn Forte if (rc < 0) { 193fcf3ce44SJohn Forte perror(gettext("nscadm: nvclean_force")); 194fcf3ce44SJohn Forte exit(255); 195fcf3ce44SJohn Forte } 196fcf3ce44SJohn Forte #endif /* DEBUG */ 197fcf3ce44SJohn Forte } else if (strcoll(argv[optind], gettext("gmem")) == 0 || 198fcf3ce44SJohn Forte strcmp(argv[optind], "gmem") == 0) { 199fcf3ce44SJohn Forte rc = _nsc_gmem(); 200fcf3ce44SJohn Forte if (rc < 0) { 201fcf3ce44SJohn Forte perror(gettext("nscadm: gmem")); 202fcf3ce44SJohn Forte exit(255); 203fcf3ce44SJohn Forte } 204fcf3ce44SJohn Forte } else { 205fcf3ce44SJohn Forte usage(); 206fcf3ce44SJohn Forte exit(255); 207fcf3ce44SJohn Forte } 208fcf3ce44SJohn Forte 209fcf3ce44SJohn Forte return (rc); 210fcf3ce44SJohn Forte } 211fcf3ce44SJohn Forte 212fcf3ce44SJohn Forte 213fcf3ce44SJohn Forte static int 214fcf3ce44SJohn Forte _nsc_gmem(void) 215fcf3ce44SJohn Forte { 216fcf3ce44SJohn Forte char *addr; 217fcf3ce44SJohn Forte int size; 218fcf3ce44SJohn Forte int rc = 0; 219fcf3ce44SJohn Forte 220fcf3ce44SJohn Forte rc = nsc_gmem_sizes(&size); 221fcf3ce44SJohn Forte 222fcf3ce44SJohn Forte if (rc < 0) 223fcf3ce44SJohn Forte return (rc); 224fcf3ce44SJohn Forte 225*570de38fSSurya Prakki (void) printf(gettext("size %d\n"), size); 226fcf3ce44SJohn Forte 227fcf3ce44SJohn Forte if ((addr = (char *)malloc(size * 2)) == NULL) { 228fcf3ce44SJohn Forte errno = ENOMEM; 229fcf3ce44SJohn Forte return (-1); 230fcf3ce44SJohn Forte } 231fcf3ce44SJohn Forte 232fcf3ce44SJohn Forte rc = nsc_gmem_data(addr); 233fcf3ce44SJohn Forte 234fcf3ce44SJohn Forte if (rc < 0) { 235fcf3ce44SJohn Forte free(addr); 236fcf3ce44SJohn Forte return (rc); 237fcf3ce44SJohn Forte } 238fcf3ce44SJohn Forte 239*570de38fSSurya Prakki (void) printf(gettext("Global map entries:\n")); 240fcf3ce44SJohn Forte show_maps(addr, size); 241fcf3ce44SJohn Forte 242*570de38fSSurya Prakki (void) printf(gettext("\nGlobal NVMEM map entries:\n")); 243fcf3ce44SJohn Forte show_maps(addr + size, size); 244fcf3ce44SJohn Forte 245fcf3ce44SJohn Forte free(addr); 246fcf3ce44SJohn Forte return (0); 247fcf3ce44SJohn Forte } 248fcf3ce44SJohn Forte 249fcf3ce44SJohn Forte 250fcf3ce44SJohn Forte static void 251fcf3ce44SJohn Forte show_maps(char *addr, int len) 252fcf3ce44SJohn Forte { 253fcf3ce44SJohn Forte /* LINTED alignment of cast ok */ 254fcf3ce44SJohn Forte nsc_rmhdr_t *rhp = (nsc_rmhdr_t *)addr; 255fcf3ce44SJohn Forte nsc_rmmap_t *rmap; 256fcf3ce44SJohn Forte char tname[_NSC_MAXNAME + 1]; 257fcf3ce44SJohn Forte int i; 258fcf3ce44SJohn Forte 259*570de38fSSurya Prakki (void) printf( 260*570de38fSSurya Prakki gettext("magic 0x%x ver %d size %d dirty (nvmem systems): %d\n"), 261fcf3ce44SJohn Forte rhp->magic, rhp->ver, rhp->size, rhp->rh_dirty); 262fcf3ce44SJohn Forte 263fcf3ce44SJohn Forte for (i = 0, rmap = rhp->map; 264fcf3ce44SJohn Forte /* LINTED alignment of cast ok */ 265fcf3ce44SJohn Forte rmap < (nsc_rmmap_t *)(addr + len); ++i, ++rmap) { 266fcf3ce44SJohn Forte if (!rmap->name[0]) 267fcf3ce44SJohn Forte continue; 268*570de38fSSurya Prakki (void) strncpy(tname, rmap->name, _NSC_MAXNAME); 269*570de38fSSurya Prakki (void) strcpy(&tname[strlen(tname)], " "); 270fcf3ce44SJohn Forte tname[_NSC_MAXNAME] = '\0'; 271*570de38fSSurya Prakki (void) printf(gettext( 272fcf3ce44SJohn Forte "%d:\tname %s\toffset 0x%x size 0x%x inuse 0x%x\n"), 273fcf3ce44SJohn Forte i, tname, rmap->offset, rmap->size, rmap->inuse); 274fcf3ce44SJohn Forte } 275fcf3ce44SJohn Forte } 276