readnvram.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <authsrv.h>
  4. static long finddosfile(int, char*);
  5. static int
  6. check(void *x, int len, uchar sum, char *msg)
  7. {
  8. if(nvcsum(x, len) == sum)
  9. return 0;
  10. memset(x, 0, len);
  11. fprint(2, "%s\n", msg);
  12. return 1;
  13. }
  14. /*
  15. * get key info out of nvram. since there isn't room in the PC's nvram use
  16. * a disk partition there.
  17. */
  18. static struct {
  19. char *cputype;
  20. char *file;
  21. int off;
  22. int len;
  23. } nvtab[] = {
  24. "sparc", "#r/nvram", 1024+850, sizeof(Nvrsafe),
  25. "pc", "#S/sdC0/nvram", 0, sizeof(Nvrsafe),
  26. "pc", "#S/sdC0/9fat", -1, sizeof(Nvrsafe),
  27. "pc", "#S/sdC1/nvram", 0, sizeof(Nvrsafe),
  28. "pc", "#S/sdC1/9fat", -1, sizeof(Nvrsafe),
  29. "pc", "#S/sdD0/nvram", 0, sizeof(Nvrsafe),
  30. "pc", "#S/sdD0/9fat", -1, sizeof(Nvrsafe),
  31. "pc", "#S/sdE0/nvram", 0, sizeof(Nvrsafe),
  32. "pc", "#S/sdE0/9fat", -1, sizeof(Nvrsafe),
  33. "pc", "#S/sdF0/nvram", 0, sizeof(Nvrsafe),
  34. "pc", "#S/sdF0/9fat", -1, sizeof(Nvrsafe),
  35. "pc", "#S/sd00/nvram", 0, sizeof(Nvrsafe),
  36. "pc", "#S/sd00/9fat", -1, sizeof(Nvrsafe),
  37. "pc", "#S/sd01/nvram", 0, sizeof(Nvrsafe),
  38. "pc", "#S/sd01/9fat", -1, sizeof(Nvrsafe),
  39. "pc", "#f/fd0disk", -1, 512, /* 512: #f requires whole sector reads */
  40. "pc", "#f/fd1disk", -1, 512,
  41. "mips", "#r/nvram", 1024+900, sizeof(Nvrsafe),
  42. "power", "#F/flash/flash0", 0x440000, sizeof(Nvrsafe),
  43. "power", "#r/nvram", 4352, sizeof(Nvrsafe), /* OK for MTX-604e */
  44. "power", "/nvram", 0, sizeof(Nvrsafe), /* OK for Ucu */
  45. "arm", "#F/flash/flash0", 0x100000, sizeof(Nvrsafe),
  46. "debug", "/tmp/nvram", 0, sizeof(Nvrsafe),
  47. };
  48. static char*
  49. readcons(char *prompt, char *def, int raw, char *buf, int nbuf)
  50. {
  51. int fdin, fdout, ctl, n, m;
  52. char line[10];
  53. fdin = open("/dev/cons", OREAD);
  54. if(fdin < 0)
  55. fdin = 0;
  56. fdout = open("/dev/cons", OWRITE);
  57. if(fdout < 0)
  58. fdout = 1;
  59. if(def != nil)
  60. fprint(fdout, "%s[%s]: ", prompt, def);
  61. else
  62. fprint(fdout, "%s: ", prompt);
  63. if(raw){
  64. ctl = open("/dev/consctl", OWRITE);
  65. if(ctl >= 0)
  66. write(ctl, "rawon", 5);
  67. } else
  68. ctl = -1;
  69. m = 0;
  70. for(;;){
  71. n = read(fdin, line, 1);
  72. if(n == 0){
  73. close(ctl);
  74. werrstr("readcons: EOF");
  75. return nil;
  76. }
  77. if(n < 0){
  78. close(ctl);
  79. werrstr("can't read cons");
  80. return nil;
  81. }
  82. if(line[0] == 0x7f)
  83. exits(0);
  84. if(n == 0 || line[0] == '\n' || line[0] == '\r'){
  85. if(raw){
  86. write(ctl, "rawoff", 6);
  87. write(fdout, "\n", 1);
  88. close(ctl);
  89. }
  90. buf[m] = '\0';
  91. if(buf[0]=='\0' && def)
  92. strcpy(buf, def);
  93. return buf;
  94. }
  95. if(line[0] == '\b'){
  96. if(m > 0)
  97. m--;
  98. }else if(line[0] == 0x15){ /* ^U: line kill */
  99. m = 0;
  100. if(def != nil)
  101. fprint(fdout, "%s[%s]: ", prompt, def);
  102. else
  103. fprint(fdout, "%s: ", prompt);
  104. }else{
  105. if(m >= nbuf-1){
  106. fprint(fdout, "line too long\n");
  107. m = 0;
  108. if(def != nil)
  109. fprint(fdout, "%s[%s]: ", prompt, def);
  110. else
  111. fprint(fdout, "%s: ", prompt);
  112. }else
  113. buf[m++] = line[0];
  114. }
  115. }
  116. }
  117. typedef struct {
  118. int fd;
  119. int safeoff;
  120. int safelen;
  121. } Nvrwhere;
  122. /* returns with *locp filled in and locp->fd open, if possible */
  123. static void
  124. findnvram(Nvrwhere *locp)
  125. {
  126. char *cputype, *nvrfile, *nvrlen, *nvroff, *v[2];
  127. int fd, i, safeoff, safelen;
  128. nvrfile = getenv("nvram");
  129. cputype = getenv("cputype");
  130. if(cputype == nil)
  131. cputype = strdup("mips");
  132. if(strcmp(cputype, "386")==0 || strcmp(cputype, "alpha")==0)
  133. cputype = strdup("pc");
  134. fd = -1;
  135. safeoff = -1;
  136. safelen = -1;
  137. if(nvrfile != nil){
  138. /* accept device and device!file */
  139. i = gettokens(nvrfile, v, nelem(v), "!");
  140. fd = open(v[0], ORDWR);
  141. safelen = sizeof(Nvrsafe);
  142. if(strstr(v[0], "/9fat") == nil)
  143. safeoff = 0;
  144. nvrlen = getenv("nvrlen");
  145. if(nvrlen != nil)
  146. safelen = atoi(nvrlen);
  147. nvroff = getenv("nvroff");
  148. if(nvroff != nil)
  149. if(strcmp(nvroff, "dos") == 0)
  150. safeoff = -1;
  151. else
  152. safeoff = atoi(nvroff);
  153. if(safeoff < 0 && fd >= 0){
  154. safelen = 512;
  155. safeoff = finddosfile(fd, i == 2? v[1]: "plan9.nvr");
  156. if(safeoff < 0){ /* didn't find plan9.nvr? */
  157. close(fd);
  158. fd = -1;
  159. }
  160. }
  161. free(nvroff);
  162. free(nvrlen);
  163. free(nvrfile);
  164. }else
  165. for(i=0; i<nelem(nvtab); i++){
  166. if(strcmp(cputype, nvtab[i].cputype) != 0)
  167. continue;
  168. if((fd = open(nvtab[i].file, ORDWR)) < 0)
  169. continue;
  170. safeoff = nvtab[i].off;
  171. safelen = nvtab[i].len;
  172. if(safeoff == -1){
  173. safeoff = finddosfile(fd, "plan9.nvr");
  174. if(safeoff < 0){ /* didn't find plan9.nvr? */
  175. close(fd);
  176. fd = -1;
  177. continue;
  178. }
  179. }
  180. break;
  181. }
  182. free(cputype);
  183. locp->fd = fd;
  184. locp->safelen = safelen;
  185. locp->safeoff = safeoff;
  186. }
  187. /*
  188. * get key info out of nvram. since there isn't room in the PC's nvram use
  189. * a disk partition there.
  190. */
  191. int
  192. readnvram(Nvrsafe *safep, int flag)
  193. {
  194. int err;
  195. char buf[512], in[128]; /* 512 for floppy i/o */
  196. Nvrsafe *safe;
  197. Nvrwhere loc;
  198. err = 0;
  199. memset(&loc, 0, sizeof loc);
  200. findnvram(&loc);
  201. if (loc.safelen < 0)
  202. loc.safelen = sizeof *safe;
  203. else if (loc.safelen > sizeof buf)
  204. loc.safelen = sizeof buf;
  205. if (loc.safeoff < 0) {
  206. fprint(2, "readnvram: couldn't find nvram\n");
  207. if(!(flag&NVwritemem))
  208. memset(safep, 0, sizeof(*safep));
  209. return -1;
  210. }
  211. safe = (Nvrsafe*)buf;
  212. if(flag&NVwritemem)
  213. safe = safep;
  214. else {
  215. memset(safep, 0, sizeof(*safep));
  216. if(loc.fd < 0
  217. || seek(loc.fd, loc.safeoff, 0) < 0
  218. || read(loc.fd, buf, loc.safelen) != loc.safelen){
  219. err = 1;
  220. if(flag&(NVwrite|NVwriteonerr))
  221. fprint(2, "can't read nvram: %r\n");
  222. /* start from scratch */
  223. memset(safep, 0, sizeof(*safep));
  224. safe = safep;
  225. }else{
  226. *safep = *safe; /* overwrite arg with data read */
  227. safe = safep;
  228. /* verify data read */
  229. err |= check(safe->machkey, DESKEYLEN, safe->machsum,
  230. "bad nvram key");
  231. // err |= check(safe->config, CONFIGLEN, safe->configsum,
  232. // "bad secstore key");
  233. err |= check(safe->authid, ANAMELEN, safe->authidsum,
  234. "bad authentication id");
  235. err |= check(safe->authdom, DOMLEN, safe->authdomsum,
  236. "bad authentication domain");
  237. if(err == 0)
  238. if(safe->authid[0]==0 || safe->authdom[0]==0){
  239. fprint(2, "empty nvram authid or authdom\n");
  240. err = 1;
  241. }
  242. }
  243. }
  244. if((flag&(NVwrite|NVwritemem)) || (err && (flag&NVwriteonerr))){
  245. if (!(flag&NVwritemem)) {
  246. readcons("authid", nil, 0, safe->authid,
  247. sizeof safe->authid);
  248. readcons("authdom", nil, 0, safe->authdom,
  249. sizeof safe->authdom);
  250. readcons("secstore key (or fs config)", nil, 1,
  251. safe->config, sizeof safe->config);
  252. for(;;){
  253. if(readcons("password", nil, 1, in, sizeof in)
  254. == nil)
  255. goto Out;
  256. if(passtokey(safe->machkey, in))
  257. break;
  258. }
  259. }
  260. // safe->authsum = nvcsum(safe->authkey, DESKEYLEN);
  261. safe->machsum = nvcsum(safe->machkey, DESKEYLEN);
  262. safe->configsum = nvcsum(safe->config, CONFIGLEN);
  263. safe->authidsum = nvcsum(safe->authid, sizeof safe->authid);
  264. safe->authdomsum = nvcsum(safe->authdom, sizeof safe->authdom);
  265. *(Nvrsafe*)buf = *safe;
  266. if(loc.fd < 0
  267. || seek(loc.fd, loc.safeoff, 0) < 0
  268. || write(loc.fd, buf, loc.safelen) != loc.safelen){
  269. fprint(2, "can't write key to nvram: %r\n");
  270. err = 1;
  271. }else
  272. err = 0;
  273. }
  274. Out:
  275. if (loc.fd >= 0)
  276. close(loc.fd);
  277. return err? -1: 0;
  278. }
  279. typedef struct Dosboot Dosboot;
  280. struct Dosboot{
  281. uchar magic[3]; /* really an xx86 JMP instruction */
  282. uchar version[8];
  283. uchar sectsize[2];
  284. uchar clustsize;
  285. uchar nresrv[2];
  286. uchar nfats;
  287. uchar rootsize[2];
  288. uchar volsize[2];
  289. uchar mediadesc;
  290. uchar fatsize[2];
  291. uchar trksize[2];
  292. uchar nheads[2];
  293. uchar nhidden[4];
  294. uchar bigvolsize[4];
  295. uchar driveno;
  296. uchar reserved0;
  297. uchar bootsig;
  298. uchar volid[4];
  299. uchar label[11];
  300. uchar type[8];
  301. };
  302. #define GETSHORT(p) (((p)[1]<<8) | (p)[0])
  303. #define GETLONG(p) ((GETSHORT((p)+2) << 16) | GETSHORT((p)))
  304. typedef struct Dosdir Dosdir;
  305. struct Dosdir
  306. {
  307. char name[8];
  308. char ext[3];
  309. uchar attr;
  310. uchar reserved[10];
  311. uchar time[2];
  312. uchar date[2];
  313. uchar start[2];
  314. uchar length[4];
  315. };
  316. static char*
  317. dosparse(char *from, char *to, int len)
  318. {
  319. char c;
  320. memset(to, ' ', len);
  321. if(from == 0)
  322. return 0;
  323. while(len-- > 0){
  324. c = *from++;
  325. if(c == '.')
  326. return from;
  327. if(c == 0)
  328. break;
  329. if(c >= 'a' && c <= 'z')
  330. *to++ = c + 'A' - 'a';
  331. else
  332. *to++ = c;
  333. }
  334. return 0;
  335. }
  336. /*
  337. * return offset of first file block
  338. *
  339. * This is a very simplistic dos file system. It only
  340. * works on floppies, only looks in the root, and only
  341. * returns a pointer to the first block of a file.
  342. *
  343. * This exists for cpu servers that have no hard disk
  344. * or nvram to store the key on.
  345. *
  346. * Please don't make this any smarter: it stays resident
  347. * and I'ld prefer not to waste the space on something that
  348. * runs only at boottime -- presotto.
  349. */
  350. static long
  351. finddosfile(int fd, char *file)
  352. {
  353. uchar secbuf[512];
  354. char name[8];
  355. char ext[3];
  356. Dosboot *b;
  357. Dosdir *root, *dp;
  358. int nroot, sectsize, rootoff, rootsects, n;
  359. /* dos'ize file name */
  360. file = dosparse(file, name, 8);
  361. dosparse(file, ext, 3);
  362. /* read boot block, check for sanity */
  363. b = (Dosboot*)secbuf;
  364. if(read(fd, secbuf, sizeof(secbuf)) != sizeof(secbuf))
  365. return -1;
  366. if(b->magic[0] != 0xEB || b->magic[1] != 0x3C || b->magic[2] != 0x90)
  367. return -1;
  368. sectsize = GETSHORT(b->sectsize);
  369. if(sectsize != 512)
  370. return -1;
  371. rootoff = (GETSHORT(b->nresrv) + b->nfats*GETSHORT(b->fatsize)) * sectsize;
  372. if(seek(fd, rootoff, 0) < 0)
  373. return -1;
  374. nroot = GETSHORT(b->rootsize);
  375. rootsects = (nroot*sizeof(Dosdir)+sectsize-1)/sectsize;
  376. if(rootsects <= 0 || rootsects > 64)
  377. return -1;
  378. /*
  379. * read root. it is contiguous to make stuff like
  380. * this easier
  381. */
  382. root = malloc(rootsects*sectsize);
  383. if(read(fd, root, rootsects*sectsize) != rootsects*sectsize)
  384. return -1;
  385. n = -1;
  386. for(dp = root; dp < &root[nroot]; dp++)
  387. if(memcmp(name, dp->name, 8) == 0 && memcmp(ext, dp->ext, 3) == 0){
  388. n = GETSHORT(dp->start);
  389. break;
  390. }
  391. free(root);
  392. if(n < 0)
  393. return -1;
  394. /*
  395. * dp->start is in cluster units, not sectors. The first
  396. * cluster is cluster 2 which starts immediately after the
  397. * root directory
  398. */
  399. return rootoff + rootsects*sectsize + (n-2)*sectsize*b->clustsize;
  400. }