5 static long finddosfile(int, char*);
8 check(void *x, int len, uchar sum, char *msg)
10 if(nvcsum(x, len) == sum)
13 fprint(2, "%s\n", msg);
18 * get key info out of nvram. since there isn't room in the PC's nvram use
19 * a disk partition there.
27 "sparc", "#r/nvram", 1024+850, sizeof(Nvrsafe),
28 "pc", "#S/sdC0/nvram", 0, sizeof(Nvrsafe),
29 "pc", "#S/sdC0/9fat", -1, sizeof(Nvrsafe),
30 "pc", "#S/sdC1/nvram", 0, sizeof(Nvrsafe),
31 "pc", "#S/sdC1/9fat", -1, sizeof(Nvrsafe),
32 "pc", "#S/sd00/nvram", 0, sizeof(Nvrsafe),
33 "pc", "#S/sd00/9fat", -1, sizeof(Nvrsafe),
34 "pc", "#S/sd01/nvram", 0, sizeof(Nvrsafe),
35 "pc", "#S/sd01/9fat", -1, sizeof(Nvrsafe),
36 "pc", "#f/fd0disk", -1, 512, /* 512: #f requires whole sector reads */
37 "pc", "#f/fd1disk", -1, 512,
38 "mips", "#r/nvram", 1024+900, sizeof(Nvrsafe),
39 "power", "#F/flash/flash0", 0x440000, sizeof(Nvrsafe),
40 "power", "#r/nvram", 4352, sizeof(Nvrsafe), /* OK for MTX-604e */
41 "debug", "/tmp/nvram", 0, sizeof(Nvrsafe),
45 readcons(char *prompt, char *def, int raw, char *buf, int nbuf)
47 int fdin, fdout, ctl, n, m;
50 fdin = open("/dev/cons", OREAD);
53 fdout = open("/dev/cons", OWRITE);
57 fprint(fdout, "%s[%s]: ", prompt, def);
59 fprint(fdout, "%s: ", prompt);
61 ctl = open("/dev/consctl", OWRITE);
63 write(ctl, "rawon", 5);
69 n = read(fdin, line, 1);
72 werrstr("readcons: EOF");
77 werrstr("can't read cons");
82 if(n == 0 || line[0] == '\n' || line[0] == '\r'){
84 write(ctl, "rawoff", 6);
85 write(fdout, "\n", 1);
89 if(buf[0]=='\0' && def)
96 }else if(line[0] == 0x15){ /* ^U: line kill */
99 fprint(fdout, "%s[%s]: ", prompt, def);
101 fprint(fdout, "%s: ", prompt);
104 fprint(fdout, "line too long\n");
107 fprint(fdout, "%s[%s]: ", prompt, def);
109 fprint(fdout, "%s: ", prompt);
114 return buf; /* how does this happen */
119 * get key info out of nvram. since there isn't room in the PC's nvram use
120 * a disk partition there.
123 readnvram(Nvrsafe *safep, int flag)
125 char buf[1024], in[128], *cputype, *nvrfile, *nvrlen, *nvroff, *v[2];
126 int fd, err, i, safeoff, safelen;
130 memset(safep, 0, sizeof(*safep));
132 nvrfile = getenv("nvram");
133 cputype = getenv("cputype");
136 if(strcmp(cputype, "386")==0 || strcmp(cputype, "alpha")==0)
139 safe = (Nvrsafe*)buf;
145 /* accept device and device!file */
146 i = gettokens(nvrfile, v, nelem(v), "!");
147 fd = open(v[0], ORDWR);
148 safelen = sizeof(Nvrsafe);
149 if(strstr(v[0], "/9fat") == nil)
151 nvrlen = getenv("nvrlen");
153 safelen = atoi(nvrlen);
154 nvroff = getenv("nvroff");
156 if(strcmp(nvroff, "dos") == 0)
159 safeoff = atoi(nvroff);
161 if(safeoff < 0 && fd >= 0){
163 safeoff = finddosfile(fd, i == 2 ? v[1] : "plan9.nvr");
175 for(i=0; i<nelem(nvtab); i++){
176 if(strcmp(cputype, nvtab[i].cputype) != 0)
178 if((fd = open(nvtab[i].file, ORDWR)) < 0)
180 safeoff = nvtab[i].off;
181 safelen = nvtab[i].len;
183 safeoff = finddosfile(fd, "plan9.nvr");
195 || seek(fd, safeoff, 0) < 0
196 || read(fd, buf, safelen) != safelen){
198 if(flag&(NVwrite|NVwriteonerr))
199 fprint(2, "can't read nvram: %r\n");
200 memset(safep, 0, sizeof(*safep));
206 err |= check(safe->machkey, DESKEYLEN, safe->machsum, "bad nvram key");
207 // err |= check(safe->config, CONFIGLEN, safe->configsum, "bad secstore key");
208 err |= check(safe->authid, ANAMELEN, safe->authidsum, "bad authentication id");
209 err |= check(safe->authdom, DOMLEN, safe->authdomsum, "bad authentication domain");
212 if((flag&NVwrite) || (err && (flag&NVwriteonerr))){
213 readcons("authid", nil, 0, safe->authid, sizeof(safe->authid));
214 readcons("authdom", nil, 0, safe->authdom, sizeof(safe->authdom));
215 readcons("secstore key", nil, 1, safe->config, sizeof(safe->config));
217 if(readcons("password", nil, 1, in, sizeof in) == nil)
219 if(passtokey(safe->machkey, in))
222 safe->machsum = nvcsum(safe->machkey, DESKEYLEN);
223 safe->configsum = nvcsum(safe->config, CONFIGLEN);
224 safe->authidsum = nvcsum(safe->authid, sizeof(safe->authid));
225 safe->authdomsum = nvcsum(safe->authdom, sizeof(safe->authdom));
226 *(Nvrsafe*)buf = *safe;
227 if(seek(fd, safeoff, 0) < 0
228 || write(fd, buf, safelen) != safelen){
229 fprint(2, "can't write key to nvram: %r\n");
239 typedef struct Dosboot Dosboot;
241 uchar magic[3]; /* really an xx86 JMP instruction */
262 #define GETSHORT(p) (((p)[1]<<8) | (p)[0])
263 #define GETLONG(p) ((GETSHORT((p)+2) << 16) | GETSHORT((p)))
265 typedef struct Dosdir Dosdir;
279 dosparse(char *from, char *to, int len)
283 memset(to, ' ', len);
292 if(c >= 'a' && c <= 'z')
293 *to++ = c + 'A' - 'a';
301 * return offset of first file block
303 * This is a very simplistic dos file system. It only
304 * works on floppies, only looks in the root, and only
305 * returns a pointer to the first block of a file.
307 * This exists for cpu servers that have no hard disk
308 * or nvram to store the key on.
310 * Please don't make this any smarter: it stays resident
311 * and I'ld prefer not to waste the space on something that
312 * runs only at boottime -- presotto.
315 finddosfile(int fd, char *file)
322 int nroot, sectsize, rootoff, rootsects, n;
324 /* dos'ize file name */
325 file = dosparse(file, name, 8);
326 dosparse(file, ext, 3);
328 /* read boot block, check for sanity */
329 b = (Dosboot*)secbuf;
330 if(read(fd, secbuf, sizeof(secbuf)) != sizeof(secbuf))
332 if(b->magic[0] != 0xEB || b->magic[1] != 0x3C || b->magic[2] != 0x90)
334 sectsize = GETSHORT(b->sectsize);
337 rootoff = (GETSHORT(b->nresrv) + b->nfats*GETSHORT(b->fatsize)) * sectsize;
338 if(seek(fd, rootoff, 0) < 0)
340 nroot = GETSHORT(b->rootsize);
341 rootsects = (nroot*sizeof(Dosdir)+sectsize-1)/sectsize;
342 if(rootsects <= 0 || rootsects > 64)
346 * read root. it is contiguous to make stuff like
349 root = malloc(rootsects*sectsize);
350 if(read(fd, root, rootsects*sectsize) != rootsects*sectsize)
353 for(dp = root; dp < &root[nroot]; dp++)
354 if(memcmp(name, dp->name, 8) == 0 && memcmp(ext, dp->ext, 3) == 0){
355 n = GETSHORT(dp->start);
364 * dp->start is in cluster units, not sectors. The first
365 * cluster is cluster 2 which starts immediately after the
368 return rootoff + rootsects*sectsize + (n-2)*sectsize*b->clustsize;