readnvram.c 12 KB

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