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 xreadcons(char *prompt, char *def, int secret, char *buf, int nbuf)
49 p = readcons(prompt, def, secret);
52 strecpy(buf, buf+nbuf, p);
53 memset(p, 0, strlen(p));
59 * get key info out of nvram. since there isn't room in the PC's nvram use
60 * a disk partition there.
63 readnvram(Nvrsafe *safep, int flag)
65 char buf[1024], in[128], *cputype, *nvrfile, *nvrlen, *nvroff, *v[2];
66 int fd, err, i, safeoff, safelen;
70 memset(safep, 0, sizeof(*safep));
72 nvrfile = getenv("nvram");
73 cputype = getenv("cputype");
76 if(strcmp(cputype, "386")==0 || strcmp(cputype, "alpha")==0)
83 /* accept device and device!file */
84 i = gettokens(nvrfile, v, nelem(v), "!");
85 fd = open(v[0], ORDWR);
86 safelen = sizeof(Nvrsafe);
87 if(strstr(v[0], "/9fat") == nil)
89 nvrlen = getenv("nvrlen");
91 safelen = atoi(nvrlen);
92 nvroff = getenv("nvroff");
94 if(strcmp(nvroff, "dos") == 0)
97 safeoff = atoi(nvroff);
99 if(safeoff < 0 && fd >= 0){
101 safeoff = finddosfile(fd, i == 2 ? v[1] : "plan9.nvr");
113 for(i=0; i<nelem(nvtab); i++){
114 if(strcmp(cputype, nvtab[i].cputype) != 0)
116 if((fd = open(nvtab[i].file, ORDWR)) < 0)
118 safeoff = nvtab[i].off;
119 safelen = nvtab[i].len;
121 safeoff = finddosfile(fd, "plan9.nvr");
133 || seek(fd, safeoff, 0) < 0
134 || read(fd, buf, safelen) != safelen){
136 if(flag&(NVwrite|NVwriteonerr))
137 fprint(2, "can't read nvram: %r\n");
138 memset(safep, 0, sizeof(*safep));
141 memmove(safep, buf, sizeof *safep);
144 err |= check(safe->machkey, DESKEYLEN, safe->machsum, "bad nvram key");
145 /* err |= check(safe->config, CONFIGLEN, safe->configsum, "bad secstore key"); */
146 err |= check(safe->authid, ANAMELEN, safe->authidsum, "bad authentication id");
147 err |= check(safe->authdom, DOMLEN, safe->authdomsum, "bad authentication domain");
150 if((flag&NVwrite) || (err && (flag&NVwriteonerr))){
151 xreadcons("authid", nil, 0, safe->authid, sizeof(safe->authid));
152 xreadcons("authdom", nil, 0, safe->authdom, sizeof(safe->authdom));
153 xreadcons("secstore key", nil, 1, safe->config, sizeof(safe->config));
155 if(xreadcons("password", nil, 1, in, sizeof in) == nil)
157 if(passtokey(safe->machkey, in))
160 safe->machsum = nvcsum(safe->machkey, DESKEYLEN);
161 safe->configsum = nvcsum(safe->config, CONFIGLEN);
162 safe->authidsum = nvcsum(safe->authid, sizeof(safe->authid));
163 safe->authdomsum = nvcsum(safe->authdom, sizeof(safe->authdom));
164 memmove(buf, safe, sizeof *safe);
165 if(seek(fd, safeoff, 0) < 0
166 || write(fd, buf, safelen) != safelen){
167 fprint(2, "can't write key to nvram: %r\n");
177 typedef struct Dosboot Dosboot;
179 uchar magic[3]; /* really an xx86 JMP instruction */
200 #define GETSHORT(p) (((p)[1]<<8) | (p)[0])
201 #define GETLONG(p) ((GETSHORT((p)+2) << 16) | GETSHORT((p)))
203 typedef struct Dosdir Dosdir;
217 dosparse(char *from, char *to, int len)
221 memset(to, ' ', len);
230 if(c >= 'a' && c <= 'z')
231 *to++ = c + 'A' - 'a';
239 * return offset of first file block
241 * This is a very simplistic dos file system. It only
242 * works on floppies, only looks in the root, and only
243 * returns a pointer to the first block of a file.
245 * This exists for cpu servers that have no hard disk
246 * or nvram to store the key on.
248 * Please don't make this any smarter: it stays resident
249 * and I'ld prefer not to waste the space on something that
250 * runs only at boottime -- presotto.
253 finddosfile(int fd, char *file)
260 int nroot, sectsize, rootoff, rootsects, n;
262 /* dos'ize file name */
263 file = dosparse(file, name, 8);
264 dosparse(file, ext, 3);
266 /* read boot block, check for sanity */
267 b = (Dosboot*)secbuf;
268 if(read(fd, secbuf, sizeof(secbuf)) != sizeof(secbuf))
270 if(b->magic[0] != 0xEB || b->magic[1] != 0x3C || b->magic[2] != 0x90)
272 sectsize = GETSHORT(b->sectsize);
275 rootoff = (GETSHORT(b->nresrv) + b->nfats*GETSHORT(b->fatsize)) * sectsize;
276 if(seek(fd, rootoff, 0) < 0)
278 nroot = GETSHORT(b->rootsize);
279 rootsects = (nroot*sizeof(Dosdir)+sectsize-1)/sectsize;
280 if(rootsects <= 0 || rootsects > 64)
284 * read root. it is contiguous to make stuff like
287 root = malloc(rootsects*sectsize);
288 if(read(fd, root, rootsects*sectsize) != rootsects*sectsize)
291 for(dp = root; dp < &root[nroot]; dp++)
292 if(memcmp(name, dp->name, 8) == 0 && memcmp(ext, dp->ext, 3) == 0){
293 n = GETSHORT(dp->start);
302 * dp->start is in cluster units, not sectors. The first
303 * cluster is cluster 2 which starts immediately after the
306 return rootoff + rootsects*sectsize + (n-2)*sectsize*b->clustsize;