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];
141 int fd, i, safeoff, safelen;
144 nvrfile = getenv("nvram");
146 cputype = getenv("cputype");
148 cputype = strdup("mips");
149 if(strcmp(cputype, "386")==0 || strcmp(cputype, "amd64")==0 || strcmp(cputype, "alpha")==0) {
151 cputype = strdup("pc");
157 if(nvrfile != nil && *nvrfile != '\0'){
158 /* accept device and device!file */
159 i = gettokens(nvrfile, v, nelem(v), "!");
165 fd = open(v[0], ORDWR);
167 fd = open(v[0], OREAD);
168 safelen = sizeof(Nvrsafe);
169 if(strstr(v[0], "/9fat") == nil)
171 nvrlen = getenv("nvrlen");
173 safelen = atoi(nvrlen);
174 nvroff = getenv("nvroff");
176 if(strcmp(nvroff, "dos") == 0)
179 safeoff = atoi(nvroff);
180 if(safeoff < 0 && fd >= 0){
182 safeoff = finddosfile(fd, i == 2? v[1]: "plan9.nvr");
183 if(safeoff < 0){ /* didn't find plan9.nvr? */
191 for(i=0; i<nelem(nvtab); i++){
192 if(strcmp(cputype, nvtab[i].cputype) != 0)
194 if((fd = open(nvtab[i].file, ORDWR)) < 0)
196 safeoff = nvtab[i].off;
197 safelen = nvtab[i].len;
199 safeoff = finddosfile(fd, "plan9.nvr");
200 if(safeoff < 0){ /* didn't find plan9.nvr? */
209 locp->safelen = safelen;
210 locp->safeoff = safeoff;
214 * get key info out of nvram. since there isn't room in the PC's nvram use
215 * a disk partition there.
218 readnvram(Nvrsafe *safep, int flag)
221 char buf[512], in[128]; /* 512 for floppy i/o */
226 safe = (Nvrsafe*)buf;
227 memset(&loc, 0, sizeof loc);
230 loc.safelen = sizeof *safe;
231 else if (loc.safelen > sizeof buf)
232 loc.safelen = sizeof buf;
233 if (loc.safeoff < 0) {
234 fprint(2, "readnvram: couldn't find nvram\n");
235 if(!(flag&NVwritemem))
236 memset(safep, 0, sizeof(*safep));
239 * allow user to type the data for authentication,
240 * even if there's no nvram to store it in.
247 memset(safep, 0, sizeof(*safep));
249 || seek(loc.fd, loc.safeoff, 0) < 0
250 || read(loc.fd, buf, loc.safelen) != loc.safelen){
252 if(flag&(NVwrite|NVwriteonerr))
254 fprint(2, "can't open %s: %r\n", nvrfile);
255 else if (seek(loc.fd, loc.safeoff, 0) < 0)
256 fprint(2, "can't seek %s to %d: %r\n",
257 nvrfile, loc.safeoff);
259 fprint(2, "can't read %d bytes from %s: %r\n",
260 loc.safelen, nvrfile);
261 /* start from scratch */
262 memset(safep, 0, sizeof(*safep));
265 *safep = *safe; /* overwrite arg with data read */
268 /* verify data read */
269 err |= check(safe->machkey, DESKEYLEN, safe->machsum,
271 // err |= check(safe->config, CONFIGLEN, safe->configsum,
272 // "bad secstore key");
273 err |= check(safe->authid, ANAMELEN, safe->authidsum,
274 "bad authentication id");
275 err |= check(safe->authdom, DOMLEN, safe->authdomsum,
276 "bad authentication domain");
278 if(safe->authid[0]==0 || safe->authdom[0]==0){
279 fprint(2, "empty nvram authid or authdom\n");
285 if((flag&(NVwrite|NVwritemem)) || (err && (flag&NVwriteonerr))){
286 if (!(flag&NVwritemem)) {
287 readcons("authid", nil, 0, safe->authid,
288 sizeof safe->authid);
289 readcons("authdom", nil, 0, safe->authdom,
290 sizeof safe->authdom);
291 readcons("secstore key", nil, 1, safe->config,
292 sizeof safe->config);
294 if(readcons("password", nil, 1, in, sizeof in)
297 if(passtokey(safe->machkey, in))
302 // safe->authsum = nvcsum(safe->authkey, DESKEYLEN);
303 safe->machsum = nvcsum(safe->machkey, DESKEYLEN);
304 safe->configsum = nvcsum(safe->config, CONFIGLEN);
305 safe->authidsum = nvcsum(safe->authid, sizeof safe->authid);
306 safe->authdomsum = nvcsum(safe->authdom, sizeof safe->authdom);
308 *(Nvrsafe*)buf = *safe;
310 || seek(loc.fd, loc.safeoff, 0) < 0
311 || write(loc.fd, buf, loc.safelen) != loc.safelen){
312 fprint(2, "can't write key to nvram: %r\n");
323 typedef struct Dosboot Dosboot;
325 uchar magic[3]; /* really an xx86 JMP instruction */
346 #define GETSHORT(p) (((p)[1]<<8) | (p)[0])
347 #define GETLONG(p) ((GETSHORT((p)+2) << 16) | GETSHORT((p)))
349 typedef struct Dosdir Dosdir;
363 dosparse(char *from, char *to, int len)
367 memset(to, ' ', len);
376 if(c >= 'a' && c <= 'z')
377 *to++ = c + 'A' - 'a';
385 * return offset of first file block
387 * This is a very simplistic dos file system. It only
388 * works on floppies, only looks in the root, and only
389 * returns a pointer to the first block of a file.
391 * This exists for cpu servers that have no hard disk
392 * or nvram to store the key on.
394 * Please don't make this any smarter: it stays resident
395 * and I'ld prefer not to waste the space on something that
396 * runs only at boottime -- presotto.
399 finddosfile(int fd, char *file)
406 int nroot, sectsize, rootoff, rootsects, n;
408 /* dos'ize file name */
409 file = dosparse(file, name, 8);
410 dosparse(file, ext, 3);
412 /* read boot block, check for sanity */
413 b = (Dosboot*)secbuf;
414 if(read(fd, secbuf, sizeof(secbuf)) != sizeof(secbuf))
416 if(b->magic[0] != 0xEB || b->magic[1] != 0x3C || b->magic[2] != 0x90)
418 sectsize = GETSHORT(b->sectsize);
421 rootoff = (GETSHORT(b->nresrv) + b->nfats*GETSHORT(b->fatsize)) * sectsize;
422 if(seek(fd, rootoff, 0) < 0)
424 nroot = GETSHORT(b->rootsize);
425 rootsects = (nroot*sizeof(Dosdir)+sectsize-1)/sectsize;
426 if(rootsects <= 0 || rootsects > 64)
430 * read root. it is contiguous to make stuff like
433 root = malloc(rootsects*sectsize);
434 if(read(fd, root, rootsects*sectsize) != rootsects*sectsize)
437 for(dp = root; dp < &root[nroot]; dp++)
438 if(memcmp(name, dp->name, 8) == 0 && memcmp(ext, dp->ext, 3) == 0){
439 n = GETSHORT(dp->start);
448 * dp->start is in cluster units, not sectors. The first
449 * cluster is cluster 2 which starts immediately after the
452 return rootoff + rootsects*sectsize + (n-2)*sectsize*b->clustsize;