source: trunk/athena/bin/machtype/machtype_sun4.c @ 12029

Revision 12029, 6.2 KB checked in by ghudson, 26 years ago (diff)
From svalente: factor out platform-independent functions.
Line 
1/*
2 *  Machtype: determine machine type & display type
3 *
4 * RCS Info
5 *    $Id: machtype_sun4.c,v 1.24 1998-09-30 21:07:12 ghudson Exp $
6 *    $Locker:  $
7 */
8
9#define volatile
10#include <stdio.h>
11#include <string.h>
12#include <kvm.h>
13#include <nlist.h>
14#include <fcntl.h>
15#include <unistd.h>
16#include <sys/types.h>
17#include <sys/file.h>
18#include <sys/cpu.h>
19#include <ctype.h>
20#include <dirent.h>
21#include <sys/vtoc.h>
22/* OpenPROM stuff */
23#include <sys/promif.h>
24#include <sys/ddi.h>
25#include <sys/sunddi.h>
26#include <sys/ddi_impldefs.h>
27/* Frame buffer stuff */
28#include <sys/fbio.h>
29#include <errno.h>
30#include "machtype.h"
31
32struct nlist nl[] = {
33#define X_cpu 0
34      { "cputype" },
35#define X_maxmem 1
36      { "maxmem" },
37#define X_physmem 2
38      { "physmem" },
39#define X_topnode 3
40      { "top_devinfo" },
41      { "" }
42};
43
44kvm_t *do_init(void)
45{
46    kvm_t *kernel;
47
48    kernel = kvm_open(NULL,NULL,NULL,O_RDONLY,NULL);
49    if (!kernel) {
50        fprintf(stderr,"unable to examine the kernel\n");
51        exit(2);
52    }
53    if (kvm_nlist(kernel, &nl) < 0) {
54        fprintf(stderr,"can't get namelist\n");
55        exit(2);
56    }
57    return kernel;
58}
59
60void do_cleanup(kvm_t *kernel)
61{
62    kvm_close(kernel);
63}
64
65void do_machtype(void)
66{
67    puts("sun4");
68}
69
70void do_cpu_prom(kvm_t *kernel, int verbose)
71{
72  unsigned long   ptop;
73  struct dev_info top;
74  char            buf[BUFSIZ];
75
76  char*           cpustr;
77
78  /* read device name of the top node of the OpenPROM */
79
80  if (   (! nl[X_topnode].n_value)
81      || (kvm_read(kernel, (unsigned long) nl[X_topnode].n_value,
82                            (char*) &ptop, sizeof(ptop)) != sizeof(ptop))
83      || (! ptop)
84      || (kvm_read(kernel, (unsigned long) ptop,
85                            (char*) &top, sizeof(top)) != sizeof(top))
86      || (! top.devi_name)
87      || (kvm_read(kernel, (unsigned long) top.devi_name,
88                            (char*) &buf, sizeof(buf)) != sizeof(buf))
89      || (! buf[0]) ) {
90    fprintf(stderr, "Can't get CPU information from the kernel\n");
91    exit(2);
92  }
93  buf[BUFSIZ-1] = '\0';
94
95  /* now, return a string identifying the CPU */
96
97  if (verbose) {
98    /* "verbose" returns the kernel information directly */
99    puts(buf);
100
101  } else {
102
103    /* skip the initial "SUNW," */
104    if (cpustr = strchr(buf, ','))
105      cpustr++;
106    else
107      cpustr = buf;
108
109    /* reformat the result to look like "SPARC/Classic" or "SPARC/5" */
110    if (! strncmp(cpustr, "SPARC", sizeof("SPARC")-1)) {
111      cpustr += sizeof("SPARC")-1;
112
113      if (! strncmp(cpustr, "station-", sizeof("station-")-1))
114        cpustr += sizeof("station-")-1;
115      else
116        if (! strcmp(cpustr, "classic")) /* backwards compat - cap classic */
117          (*cpustr) = toupper(*cpustr);
118
119      printf("SPARC/%s\n", cpustr);
120
121    } else {
122      /* if it didn't start with "SPARC", just leave it be... */
123      puts(cpustr);
124    }
125  }
126
127  return;
128}
129
130void do_cpu(int verbose)
131{
132    kvm_t *kernel;
133    short cpu, cpu_type;
134
135    kernel = do_init();
136    cpu_type = kvm_read(kernel,nl[X_cpu].n_value,&cpu, sizeof(cpu));
137
138        switch(cpu) {
139          case CPU_SUN4C_60:
140            puts(verbose ? "SPARCstation 1": "SPARC/1");
141            break;
142          case CPU_SUN4C_40:
143            puts(verbose ? "SPARCstation IPC" : "SPARC/IPC");
144            break;
145          case CPU_SUN4C_65:
146            puts(verbose ? "SPARCstation 1+" : "SPARC/1+");
147            break;
148          case CPU_SUN4C_20:
149            puts(verbose ? "SPARCstation SLC" : "SPARC/SLC");
150            break;
151          case CPU_SUN4C_75:
152            puts(verbose ? "SPARCstation 2" : "SPARC/2");
153            break;
154          case CPU_SUN4C_25:
155            puts(verbose ? "SPARCstation ELC" : "SPARC/ELC");
156            break;
157          case CPU_SUN4C_50:
158            puts(verbose ? "SPARCstation IPX" : "SPARC/IPX");
159            break;
160          case CPU_SUN4M_50:    /* 114... Sparc20 */
161          case OBP_ARCH:        /* 128 */
162            do_cpu_prom(kernel, verbose);
163                break;
164
165         default:
166           if(verbose)
167                printf("Unknown SUN type %d\n", cpu);
168           else
169              puts("SUN???");
170         }
171
172    do_cleanup(kernel);
173    return;
174}
175
176void do_dpy(int verbose)
177{
178  int count;
179  char buf[1024], *p;
180
181  count = readlink("/dev/fb", buf, sizeof(buf) - 1);
182  if (count == -1) {
183    puts(verbose ? "unknown frame buffer" : "unknown");
184    return;
185  }
186  buf[count] = 0;
187  p = buf + count;
188  while (p > buf && isdigit(*(p - 1)))
189    *--p = 0;
190  p = strrchr(buf, ':');
191  if (!p) {
192    puts(verbose ? "unknown frame buffer" : "unknown");
193    return;
194  }
195  printf("%s%s\n", p + 1, verbose ? " frame buffer" : "");
196}
197
198void do_disk(int verbose)
199{
200  DIR *dp;
201  struct dirent *de;
202  char path[MAXPATHLEN];
203  const char *devdir = "/dev/rdsk";
204  char *cp;
205  int fd;
206  int devlen;                   /* Length of device name, w/o partition */
207  struct vtoc vtoc;
208 
209  dp = opendir(devdir);
210  if (dp == NULL)
211    {
212      fprintf(stderr, "Cannot open %s: %s\n", devdir, strerror(errno));
213      exit(1);
214    }
215
216  while ((de = readdir(dp)) != NULL)
217    {
218      if ((!strcmp(de->d_name, ".")) || (!strcmp(de->d_name, "..")))
219        continue;
220
221      /* By convention partition (slice) 2 is the whole disk. */
222      cp = strrchr(de->d_name, 's');
223      if ((cp == NULL) || (strcmp(cp, "s2") != 0))
224        continue;
225      devlen = cp - de->d_name;         /* Get name length w/o partition */
226      sprintf(path, "%s/%s", devdir, de->d_name);
227      fd = open(path, O_RDONLY);
228      if (fd == -1)
229        continue;
230
231      if ((read_vtoc(fd, &vtoc) < 0) || (vtoc.v_sanity != VTOC_SANE))
232        {
233          close(fd);
234          continue;
235        }
236      close(fd);
237
238      if (!verbose)
239        {
240          /* Strip geometry info from the label text. */
241          cp = strchr(vtoc.v_asciilabel, ' ');
242          if (cp)
243            *cp = '\0';
244        }
245
246      printf("%.*s: %.*s\n",
247             devlen, de->d_name,
248             LEN_DKL_ASCII, vtoc.v_asciilabel);
249    }
250
251  closedir(dp);
252  return;
253}
254
255#define MEG (1024*1024)
256
257void do_memory(int verbose)
258{
259   kvm_t *kernel;
260   int pos, mem, nbpp;
261
262   kernel = do_init();
263   nbpp = getpagesize() / 1024;
264   kvm_read(kernel, nl[X_maxmem].n_value, &mem, sizeof(mem));
265   if(verbose)
266      printf("%d user, ", mem * nbpp);
267   kvm_read(kernel, nl[X_physmem].n_value, &mem, sizeof(mem));
268   if(verbose)
269      printf("%d (%d M) total\n", mem * nbpp, (mem * nbpp + 916) / 1024);
270   else
271      printf("%d\n", mem * nbpp + 916);
272   do_cleanup(kernel);
273   return;
274}
275
Note: See TracBrowser for help on using the repository browser.