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/sdD0/nvram", 0, sizeof(Nvrsafe),
33 "pc", "#S/sdD0/9fat", -1, sizeof(Nvrsafe),
34 "pc", "#S/sdE0/nvram", 0, sizeof(Nvrsafe),
35 "pc", "#S/sdE0/9fat", -1, sizeof(Nvrsafe),
36 "pc", "#S/sdF0/nvram", 0, sizeof(Nvrsafe),
37 "pc", "#S/sdF0/9fat", -1, sizeof(Nvrsafe),
38 "pc", "#S/sd00/nvram", 0, sizeof(Nvrsafe),
39 "pc", "#S/sd00/9fat", -1, sizeof(Nvrsafe),
40 "pc", "#S/sd01/nvram", 0, sizeof(Nvrsafe),
41 "pc", "#S/sd01/9fat", -1, sizeof(Nvrsafe),
42 "pc", "#S/sd10/nvram", 0, sizeof(Nvrsafe),
43 "pc", "#S/sd10/9fat", -1, sizeof(Nvrsafe),
44 "pc", "#f/fd0disk", -1, 512, /* 512: #f requires whole sector reads */
45 "pc", "#f/fd1disk", -1, 512,
46 "mips", "#r/nvram", 1024+900, sizeof(Nvrsafe),
47 "power", "#F/flash/flash0", 0x440000, sizeof(Nvrsafe),
48 "power", "#F/flash/flash", 0x440000, sizeof(Nvrsafe),
49 "power", "#r/nvram", 4352, sizeof(Nvrsafe), /* OK for MTX-604e */
50 "power", "/nvram", 0, sizeof(Nvrsafe), /* OK for Ucu */
51 "arm", "#F/flash/flash0", 0x100000, sizeof(Nvrsafe),
52 "arm", "#F/flash/flash", 0x100000, sizeof(Nvrsafe),
53 "debug", "/tmp/nvram", 0, sizeof(Nvrsafe),
57 readcons(char *prompt, char *def, int raw, char *buf, int nbuf)
59 int fdin, fdout, ctl, n, m;
62 fdin = open("/dev/cons", OREAD);
65 fdout = open("/dev/cons", OWRITE);
69 fprint(fdout, "%s[%s]: ", prompt, def);
71 fprint(fdout, "%s: ", prompt);
73 ctl = open("/dev/consctl", OWRITE);
75 write(ctl, "rawon", 5);
81 n = read(fdin, line, 1);
84 werrstr("readcons: EOF");
89 werrstr("can't read cons");
94 if(n == 0 || line[0] == '\n' || line[0] == '\r'){
96 write(ctl, "rawoff", 6);
97 write(fdout, "\n", 1);
101 if(buf[0]=='\0' && def)
108 }else if(line[0] == 0x15){ /* ^U: line kill */
111 fprint(fdout, "%s[%s]: ", prompt, def);
113 fprint(fdout, "%s: ", prompt);
116 fprint(fdout, "line too long\n");
119 fprint(fdout, "%s[%s]: ", prompt, def);
121 fprint(fdout, "%s: ", prompt);
134 static char *nvrfile = nil, *cputype = nil;
136 /* returns with *locp filled in and locp->fd open, if possible */
138 findnvram(Nvrwhere *locp)
140 char *nvrlen, *nvroff, *v[2];
145 nvrfile = getenv("nvram");
147 cputype = getenv("cputype");
149 cputype = strdup("mips");
150 if(strcmp(cputype, "386")==0 || strcmp(cputype, "amd64")==0 || strcmp(cputype, "alpha")==0) {
152 cputype = strdup("pc");
158 if(nvrfile != nil && *nvrfile != '\0'){
159 /* accept device and device!file */
160 i = gettokens(nvrfile, v, nelem(v), "!");
166 fd = open(v[0], ORDWR);
168 fd = open(v[0], OREAD);
169 safelen = sizeof(Nvrsafe);
170 if(strstr(v[0], "/9fat") == nil)
172 nvrlen = getenv("nvrlen");
174 safelen = strtol(nvrlen, 0, 0);
175 nvroff = getenv("nvroff");
177 if(strcmp(nvroff, "dos") == 0)
180 safeoff = strtoll(nvroff, 0, 0);
181 if(safeoff < 0 && fd >= 0){
183 safeoff = finddosfile(fd, i == 2? v[1]: "plan9.nvr");
184 if(safeoff < 0){ /* didn't find plan9.nvr? */
192 for(i=0; i<nelem(nvtab); i++){
193 if(strcmp(cputype, nvtab[i].cputype) != 0)
195 if((fd = open(nvtab[i].file, ORDWR)) < 0)
197 safeoff = nvtab[i].off;
198 safelen = nvtab[i].len;
200 safeoff = finddosfile(fd, "plan9.nvr");
201 if(safeoff < 0){ /* didn't find plan9.nvr? */
210 locp->safelen = safelen;
211 locp->safeoff = safeoff;
215 * get key info out of nvram. since there isn't room in the PC's nvram use
216 * a disk partition there.
219 readnvram(Nvrsafe *safep, int flag)
222 char buf[512], in[128]; /* 512 for floppy i/o */
227 safe = (Nvrsafe*)buf;
228 memset(&loc, 0, sizeof loc);
231 loc.safelen = sizeof *safe;
232 else if (loc.safelen > sizeof buf)
233 loc.safelen = sizeof buf;
234 if (loc.safeoff < 0) {
235 fprint(2, "readnvram: couldn't find nvram\n");
236 if(!(flag&NVwritemem))
237 memset(safep, 0, sizeof(*safep));
240 * allow user to type the data for authentication,
241 * even if there's no nvram to store it in.
248 memset(safep, 0, sizeof(*safep));
250 || seek(loc.fd, loc.safeoff, 0) < 0
251 || read(loc.fd, buf, loc.safelen) != loc.safelen){
253 if(flag&(NVwrite|NVwriteonerr))
255 fprint(2, "can't open %s: %r\n", nvrfile);
256 else if (seek(loc.fd, loc.safeoff, 0) < 0)
257 fprint(2, "can't seek %s to %lld: %r\n",
258 nvrfile, loc.safeoff);
260 fprint(2, "can't read %d bytes from %s: %r\n",
261 loc.safelen, nvrfile);
262 /* start from scratch */
263 memset(safep, 0, sizeof(*safep));
266 *safep = *safe; /* overwrite arg with data read */
269 /* verify data read */
270 err |= check(safe->machkey, DESKEYLEN, safe->machsum,
272 // err |= check(safe->config, CONFIGLEN, safe->configsum,
273 // "bad secstore key");
274 err |= check(safe->authid, ANAMELEN, safe->authidsum,
275 "bad authentication id");
276 err |= check(safe->authdom, DOMLEN, safe->authdomsum,
277 "bad authentication domain");
279 if(safe->authid[0]==0 || safe->authdom[0]==0){
280 fprint(2, "empty nvram authid or authdom\n");
286 if((flag&(NVwrite|NVwritemem)) || (err && (flag&NVwriteonerr))){
287 if (!(flag&NVwritemem)) {
288 readcons("authid", nil, 0, safe->authid,
289 sizeof safe->authid);
290 readcons("authdom", nil, 0, safe->authdom,
291 sizeof safe->authdom);
292 readcons("secstore key", nil, 1, safe->config,
293 sizeof safe->config);
295 if(readcons("password", nil, 1, in, sizeof in)
298 if(passtokey(safe->machkey, in))
303 // safe->authsum = nvcsum(safe->authkey, DESKEYLEN);
304 safe->machsum = nvcsum(safe->machkey, DESKEYLEN);
305 safe->configsum = nvcsum(safe->config, CONFIGLEN);
306 safe->authidsum = nvcsum(safe->authid, sizeof safe->authid);
307 safe->authdomsum = nvcsum(safe->authdom, sizeof safe->authdom);
309 *(Nvrsafe*)buf = *safe;
311 || seek(loc.fd, loc.safeoff, 0) < 0
312 || write(loc.fd, buf, loc.safelen) != loc.safelen){
313 fprint(2, "can't write key to nvram: %r\n");
324 typedef struct Dosboot Dosboot;
326 uchar magic[3]; /* really an xx86 JMP instruction */
347 #define GETSHORT(p) (((p)[1]<<8) | (p)[0])
348 #define GETLONG(p) ((GETSHORT((p)+2) << 16) | GETSHORT((p)))
350 typedef struct Dosdir Dosdir;
364 dosparse(char *from, char *to, int len)
368 memset(to, ' ', len);
377 if(c >= 'a' && c <= 'z')
378 *to++ = c + 'A' - 'a';
386 * return offset of first file block
388 * This is a very simplistic dos file system. It only
389 * works on floppies, only looks in the root, and only
390 * returns a pointer to the first block of a file.
392 * This exists for cpu servers that have no hard disk
393 * or nvram to store the key on.
395 * Please don't make this any smarter: it stays resident
396 * and I'ld prefer not to waste the space on something that
397 * runs only at boottime -- presotto.
400 finddosfile(int fd, char *file)
407 int nroot, sectsize, rootoff, rootsects, n;
409 /* dos'ize file name */
410 file = dosparse(file, name, 8);
411 dosparse(file, ext, 3);
413 /* read boot block, check for sanity */
414 b = (Dosboot*)secbuf;
415 if(read(fd, secbuf, sizeof(secbuf)) != sizeof(secbuf))
417 if(b->magic[0] != 0xEB || b->magic[1] != 0x3C || b->magic[2] != 0x90)
419 sectsize = GETSHORT(b->sectsize);
422 rootoff = (GETSHORT(b->nresrv) + b->nfats*GETSHORT(b->fatsize)) * sectsize;
423 if(seek(fd, rootoff, 0) < 0)
425 nroot = GETSHORT(b->rootsize);
426 rootsects = (nroot*sizeof(Dosdir)+sectsize-1)/sectsize;
427 if(rootsects <= 0 || rootsects > 64)
431 * read root. it is contiguous to make stuff like
434 root = malloc(rootsects*sectsize);
435 if(read(fd, root, rootsects*sectsize) != rootsects*sectsize)
438 for(dp = root; dp < &root[nroot]; dp++)
439 if(memcmp(name, dp->name, 8) == 0 && memcmp(ext, dp->ext, 3) == 0){
440 n = GETSHORT(dp->start);
449 * dp->start is in cluster units, not sectors. The first
450 * cluster is cluster 2 which starts immediately after the
453 return rootoff + rootsects*sectsize + (n-2)*sectsize*b->clustsize;