source: trunk/athena/bin/machtype/machtype_solaris.c @ 15698

Revision 15698, 3.8 KB checked in by ghudson, 23 years ago (diff)
From kolya: remove kmem dependencies for 32/64-bit independence.
Line 
1/*
2 *  Machtype: determine machine type & display type
3 *
4 * RCS Info
5 *    $Id: machtype_solaris.c,v 1.3 2001-02-28 20:39:54 ghudson Exp $
6 */
7
8#define volatile
9#include <stdio.h>
10#include <string.h>
11#include <kvm.h>
12#include <nlist.h>
13#include <fcntl.h>
14#include <unistd.h>
15#include <sys/types.h>
16#include <sys/file.h>
17#include <sys/cpu.h>
18#include <ctype.h>
19#include <dirent.h>
20#include <sys/vtoc.h>
21/* OpenPROM stuff */
22#include <sys/promif.h>
23#include <sys/ddi.h>
24#include <sys/sunddi.h>
25#include <sys/ddi_impldefs.h>
26#include <sys/systeminfo.h>
27/* Frame buffer stuff */
28#include <sys/fbio.h>
29#include <errno.h>
30#include "machtype.h"
31
32void do_machtype(void)
33{
34    puts("sun4");
35}
36
37void do_cpu(int verbose)
38{
39  unsigned long   ptop;
40  struct dev_info top;
41  char            buf[BUFSIZ];
42
43  char*           cpustr;
44
45  /* read device name of the top node of the OpenPROM */
46
47  if (sysinfo(SI_PLATFORM, buf, BUFSIZ) < 0) {
48    fprintf(stderr, "Can't get CPU information\n");
49    exit(2);
50  }
51  buf[BUFSIZ-1] = '\0';
52
53  /* now, return a string identifying the CPU */
54
55  if (verbose) {
56    /* "verbose" returns the kernel information directly */
57    puts(buf);
58
59  } else {
60
61    /* skip the initial "SUNW," */
62    cpustr = strchr(buf, ',');
63    if (cpustr)
64      cpustr++;
65    else
66      cpustr = buf;
67
68    /* reformat the result to look like "SPARC/Classic" or "SPARC/5" */
69    if (! strncmp(cpustr, "SPARC", sizeof("SPARC")-1)) {
70      cpustr += sizeof("SPARC")-1;
71
72      if (! strncmp(cpustr, "station-", sizeof("station-")-1))
73        cpustr += sizeof("station-")-1;
74      else
75        if (! strcmp(cpustr, "classic")) /* backwards compat - cap classic */
76          (*cpustr) = toupper(*cpustr);
77
78      printf("SPARC/%s\n", cpustr);
79
80    } else {
81      /* if it didn't start with "SPARC", just leave it be... */
82      puts(cpustr);
83    }
84  }
85
86  return;
87}
88
89void do_dpy(int verbose)
90{
91  int count;
92  char buf[1024], *p;
93
94  count = readlink("/dev/fb", buf, sizeof(buf) - 1);
95  if (count == -1) {
96    puts(verbose ? "unknown frame buffer" : "unknown");
97    return;
98  }
99  buf[count] = 0;
100  p = buf + count;
101  while (p > buf && isdigit((unsigned char)*(p - 1)))
102    *--p = 0;
103  p = strrchr(buf, ':');
104  if (!p) {
105    puts(verbose ? "unknown frame buffer" : "unknown");
106    return;
107  }
108  printf("%s%s\n", p + 1, verbose ? " frame buffer" : "");
109}
110
111void do_disk(int verbose)
112{
113  DIR *dp;
114  struct dirent *de;
115  char path[MAXPATHLEN];
116  const char *devdir = "/dev/rdsk";
117  char *cp;
118  int fd;
119  int devlen;                   /* Length of device name, w/o partition */
120  struct vtoc vtoc;
121 
122  dp = opendir(devdir);
123  if (dp == NULL)
124    {
125      fprintf(stderr, "Cannot open %s: %s\n", devdir, strerror(errno));
126      exit(1);
127    }
128
129  while ((de = readdir(dp)) != NULL)
130    {
131      if ((!strcmp(de->d_name, ".")) || (!strcmp(de->d_name, "..")))
132        continue;
133
134      /* By convention partition (slice) 2 is the whole disk. */
135      cp = strrchr(de->d_name, 's');
136      if ((cp == NULL) || (strcmp(cp, "s2") != 0))
137        continue;
138      devlen = cp - de->d_name;         /* Get name length w/o partition */
139      sprintf(path, "%s/%s", devdir, de->d_name);
140      fd = open(path, O_RDONLY);
141      if (fd == -1)
142        continue;
143
144      if ((read_vtoc(fd, &vtoc) < 0) || (vtoc.v_sanity != VTOC_SANE))
145        {
146          close(fd);
147          continue;
148        }
149      close(fd);
150
151      if (!verbose)
152        {
153          /* Strip geometry info from the label text. */
154          cp = strchr(vtoc.v_asciilabel, ' ');
155          if (cp)
156            *cp = '\0';
157        }
158
159      printf("%.*s: %.*s\n",
160             devlen, de->d_name,
161             LEN_DKL_ASCII, vtoc.v_asciilabel);
162    }
163
164  closedir(dp);
165  return;
166}
167
168#define MEG (1024*1024)
169
170void do_memory(int verbose)
171{
172   kvm_t *kernel;
173   int mem, nbpp;
174
175   nbpp = getpagesize() / 1024;
176   mem = sysconf(_SC_PHYS_PAGES);
177   if(verbose)
178      printf("%d (%d M) total\n", mem * nbpp, (mem * nbpp + 916) / 1024);
179   else
180      printf("%d\n", mem * nbpp + 916);
181   return;
182}
183
Note: See TracBrowser for help on using the repository browser.