config.c 18 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. #include "all.h"
  2. #include "io.h"
  3. static void dowormcopy(void);
  4. /*
  5. * This is needed for IP configuration.
  6. */
  7. #include "../ip/ip.h"
  8. struct
  9. {
  10. char* icharp;
  11. char* charp;
  12. int error;
  13. int newconf; /* clear befor start */
  14. int modconf; /* write back when done */
  15. int nextiter;
  16. int lastiter;
  17. int diriter;
  18. int ipauthset;
  19. Device* lastcw;
  20. Device* devlist;
  21. } f;
  22. static Device* confdev;
  23. static int copyworm = 0;
  24. int
  25. devcmpr(Device *d1, Device *d2)
  26. {
  27. loop:
  28. if(d1 == d2)
  29. return 0;
  30. if(d1 == 0 || d2 == 0 || d1->type != d2->type)
  31. return 1;
  32. switch(d1->type) {
  33. default:
  34. print("can't compare dev: %Z\n", d1);
  35. panic("devcmp");
  36. break;
  37. case Devmcat:
  38. case Devmlev:
  39. case Devmirr:
  40. d1 = d1->cat.first;
  41. d2 = d2->cat.first;
  42. while(d1 && d2) {
  43. if(devcmpr(d1, d2))
  44. return 1;
  45. d1 = d1->link;
  46. d2 = d2->link;
  47. }
  48. goto loop;
  49. case Devnone:
  50. return 0;
  51. case Devro:
  52. d1 = d1->ro.parent;
  53. d2 = d2->ro.parent;
  54. goto loop;
  55. case Devjuke:
  56. case Devcw:
  57. if(devcmpr(d1->cw.c, d2->cw.c))
  58. break;
  59. d1 = d1->cw.w;
  60. d2 = d2->cw.w;
  61. goto loop;
  62. case Devfworm:
  63. d1 = d1->fw.fw;
  64. d2 = d2->fw.fw;
  65. goto loop;
  66. case Devwren:
  67. case Devworm:
  68. case Devlworm:
  69. case Devide:
  70. if(d1->wren.ctrl == d2->wren.ctrl)
  71. if(d1->wren.targ == d2->wren.targ)
  72. if(d1->wren.lun == d2->wren.lun)
  73. return 0;
  74. break;
  75. case Devpart:
  76. if(d1->part.base == d2->part.base)
  77. if(d1->part.size == d2->part.size) {
  78. d1 = d1->part.d;
  79. d2 = d2->part.d;
  80. goto loop;
  81. }
  82. break;
  83. }
  84. return 1;
  85. }
  86. void
  87. cdiag(char *s, int c1)
  88. {
  89. f.charp--;
  90. if(f.error == 0) {
  91. print("config diag: %s -- <%c>\n", s, c1);
  92. f.error = 1;
  93. }
  94. }
  95. int
  96. cnumb(void)
  97. {
  98. int c, n;
  99. c = *f.charp++;
  100. if(c == '<') {
  101. n = f.nextiter;
  102. if(n >= 0) {
  103. f.nextiter = n+f.diriter;
  104. if(n == f.lastiter) {
  105. f.nextiter = -1;
  106. f.lastiter = -1;
  107. }
  108. for(;;) {
  109. c = *f.charp++;
  110. if(c == '>')
  111. break;
  112. }
  113. return n;
  114. }
  115. n = cnumb();
  116. if(*f.charp++ != '-') {
  117. cdiag("- expected", f.charp[-1]);
  118. return 0;
  119. }
  120. c = cnumb();
  121. if(*f.charp++ != '>') {
  122. cdiag("> expected", f.charp[-1]);
  123. return 0;
  124. }
  125. f.lastiter = c;
  126. f.diriter = 1;
  127. if(n > c)
  128. f.diriter = -1;
  129. f.nextiter = n+f.diriter;
  130. return n;
  131. }
  132. if(c < '0' || c > '9') {
  133. cdiag("number expected", c);
  134. return 0;
  135. }
  136. n = 0;
  137. while(c >= '0' && c <= '9') {
  138. n = n*10 + (c-'0');
  139. c = *f.charp++;
  140. }
  141. f.charp--;
  142. return n;
  143. }
  144. Device*
  145. config1(int c)
  146. {
  147. Device *d, *t;
  148. int m;
  149. d = ialloc(sizeof(Device), 0);
  150. for(;;) {
  151. t = config();
  152. if(d->cat.first == 0)
  153. d->cat.first = t;
  154. else
  155. d->cat.last->link = t;
  156. d->cat.last = t;
  157. if(f.error)
  158. goto bad;
  159. m = *f.charp;
  160. if(c == '(' && m == ')') {
  161. d->type = Devmcat;
  162. break;
  163. }
  164. if(c == '[' && m == ']') {
  165. d->type = Devmlev;
  166. break;
  167. }
  168. if(c == '{' && m == '}') {
  169. d->type = Devmirr;
  170. break;
  171. }
  172. }
  173. f.charp++;
  174. if(d->cat.first == d->cat.last)
  175. d = d->cat.first;
  176. return d;
  177. bad:
  178. return devnone;
  179. }
  180. Device*
  181. config(void)
  182. {
  183. int c, m;
  184. Device *d;
  185. char *icp;
  186. if(f.error)
  187. goto bad;
  188. d = ialloc(sizeof(Device), 0);
  189. c = *f.charp++;
  190. switch(c) {
  191. default:
  192. cdiag("unknown type", c);
  193. goto bad;
  194. case '(': /* (d+) one or multiple cat */
  195. case '[': /* [d+] one or multiple interleave */
  196. case '{': /* {d+} a mirrored device and optional mirrors */
  197. return config1(c);
  198. case 'f': /* fd fake worm */
  199. d->type = Devfworm;
  200. d->fw.fw = config();
  201. break;
  202. case 'n':
  203. d->type = Devnone;
  204. break;
  205. case 'w': /* w[#.]#[.#] wren [ctrl] unit [lun] */
  206. case 'h': /* h[#.]# ide [ctlr] unit */
  207. case 'r': /* r# worm side */
  208. case 'l': /* l# labelled-worm side */
  209. icp = f.charp;
  210. if(c == 'h')
  211. d->type = Devide;
  212. else
  213. d->type = Devwren;
  214. d->wren.ctrl = 0;
  215. d->wren.targ = cnumb();
  216. d->wren.lun = 0;
  217. m = *f.charp;
  218. if(m == '.') {
  219. f.charp++;
  220. d->wren.lun = cnumb();
  221. m = *f.charp;
  222. if(m == '.') {
  223. f.charp++;
  224. d->wren.ctrl = d->wren.targ;
  225. d->wren.targ = d->wren.lun;
  226. d->wren.lun = cnumb();
  227. }
  228. }
  229. if(f.nextiter >= 0)
  230. f.charp = icp-1;
  231. if(c == 'r') { /* worms are virtual and not uniqued */
  232. d->type = Devworm;
  233. break;
  234. }
  235. if(c == 'l') {
  236. d->type = Devlworm;
  237. break;
  238. }
  239. break;
  240. case 'o': /* o ro part of last cw */
  241. if(f.lastcw == 0) {
  242. cdiag("no cw to match", c);
  243. goto bad;
  244. }
  245. return f.lastcw->cw.ro;
  246. case 'j': /* DD jukebox */
  247. d->type = Devjuke;
  248. d->j.j = config();
  249. d->j.m = config();
  250. break;
  251. case 'c': /* cache/worm */
  252. d->type = Devcw;
  253. d->cw.c = config();
  254. d->cw.w = config();
  255. d->cw.ro = ialloc(sizeof(Device), 0);
  256. d->cw.ro->type = Devro;
  257. d->cw.ro->ro.parent = d;
  258. f.lastcw = d;
  259. break;
  260. case 'p': /* pd#.# partition base% size% */
  261. d->type = Devpart;
  262. d->part.d = config();
  263. d->part.base = cnumb();
  264. c = *f.charp++;
  265. if(c != '.')
  266. cdiag("dot expected", c);
  267. d->part.size = cnumb();
  268. break;
  269. case 'x': /* xD swab a device */
  270. d->type = Devswab;
  271. d->swab.d = config();
  272. break;
  273. }
  274. d->dlink = f.devlist;
  275. f.devlist = d;
  276. return d;
  277. bad:
  278. return devnone;
  279. }
  280. char*
  281. strdup(char *s)
  282. {
  283. int n;
  284. char *s1;
  285. n = strlen(s);
  286. s1 = ialloc(n+1, 0);
  287. strcpy(s1, s);
  288. return s1;
  289. }
  290. Device*
  291. iconfig(char *s)
  292. {
  293. Device *d;
  294. f.nextiter = -1;
  295. f.lastiter = -1;
  296. f.error = 0;
  297. f.icharp = s;
  298. f.charp = f.icharp;
  299. d = config();
  300. if(*f.charp) {
  301. cdiag("junk on end", *f.charp);
  302. f.error = 1;
  303. }
  304. return d;
  305. }
  306. int
  307. testconfig(char *s)
  308. {
  309. iconfig(s);
  310. return f.error;
  311. }
  312. int
  313. astrcmp(char *a, char *b)
  314. {
  315. int n, c;
  316. n = strlen(b);
  317. if(memcmp(a, b, n))
  318. return 1;
  319. c = a[n];
  320. if(c == 0) {
  321. aindex = 0;
  322. return 0;
  323. }
  324. if(a[n+1])
  325. return 1;
  326. if(c >= '0' && c <= '9') {
  327. aindex = c - '0';
  328. return 0;
  329. }
  330. return 1;
  331. }
  332. void
  333. mergeconf(Iobuf *p)
  334. {
  335. char word[100];
  336. char *cp;
  337. Filsys *fs;
  338. cp = p->iobuf;
  339. goto line;
  340. loop:
  341. if(*cp != '\n')
  342. goto bad;
  343. cp++;
  344. line:
  345. cp = getwd(word, cp);
  346. if(strcmp(word, "") == 0)
  347. return;
  348. if(strcmp(word, "service") == 0) {
  349. cp = getwd(word, cp);
  350. if(service[0] == 0)
  351. strcpy(service, word);
  352. goto loop;
  353. }
  354. if(strcmp(word, "ipauth") == 0) {
  355. cp = getwd(word, cp);
  356. if(!f.ipauthset)
  357. if(chartoip(authip, word))
  358. goto bad;
  359. goto loop;
  360. }
  361. if(astrcmp(word, "ip") == 0) {
  362. cp = getwd(word, cp);
  363. if(!isvalidip(ipaddr[aindex].sysip))
  364. if(chartoip(ipaddr[aindex].sysip, word))
  365. goto bad;
  366. goto loop;
  367. }
  368. if(astrcmp(word, "ipgw") == 0) {
  369. cp = getwd(word, cp);
  370. if(!isvalidip(ipaddr[aindex].defgwip))
  371. if(chartoip(ipaddr[aindex].defgwip, word))
  372. goto bad;
  373. goto loop;
  374. }
  375. if(astrcmp(word, "ipsntp") == 0) {
  376. cp = getwd(word, cp);
  377. if (!isvalidip(sntpip))
  378. if (chartoip(sntpip, word))
  379. goto bad;
  380. goto loop;
  381. }
  382. if(astrcmp(word, "ipmask") == 0) {
  383. cp = getwd(word, cp);
  384. if(!isvalidip(ipaddr[aindex].defmask))
  385. if(chartoip(ipaddr[aindex].defmask, word))
  386. goto bad;
  387. goto loop;
  388. }
  389. if(strcmp(word, "filsys") == 0) {
  390. cp = getwd(word, cp);
  391. for(fs=filsys; fs->name; fs++)
  392. if(strcmp(fs->name, word) == 0) {
  393. if(fs->flags & FEDIT) {
  394. cp = getwd(word, cp);
  395. goto loop;
  396. }
  397. break;
  398. }
  399. fs->name = strdup(word);
  400. cp = getwd(word, cp);
  401. fs->conf = strdup(word);
  402. goto loop;
  403. }
  404. goto bad;
  405. bad:
  406. putbuf(p);
  407. panic("unknown word in config block: %s", word);
  408. }
  409. void
  410. cmd_printconf(int, char *[])
  411. {
  412. char *p, *s;
  413. Iobuf *iob;
  414. iob = getbuf(confdev, 0, Bread);
  415. if(iob == nil)
  416. return;
  417. if(checktag(iob, Tconfig, 0)){
  418. putbuf(iob);
  419. return;
  420. }
  421. print("config %s\n", nvrgetconfig());
  422. s = p = iob->iobuf;
  423. while(*p != 0 && p < iob->iobuf+BUFSIZE){
  424. if(*p++ != '\n')
  425. continue;
  426. print("%.*s", (int)(p-s), s);
  427. s = p;
  428. }
  429. if(p != s)
  430. print("%.*s", (int)(p-s), s);
  431. print("end\n");
  432. putbuf(iob);
  433. }
  434. extern void floppyhalt(void);
  435. void
  436. sysinit(void)
  437. {
  438. Filsys *fs;
  439. int error, i;
  440. Device *d;
  441. Iobuf *p;
  442. char *cp;
  443. dofilter(u->time+0, C0a, C0b, 1);
  444. dofilter(u->time+1, C1a, C1b, 1);
  445. dofilter(u->time+2, C2a, C2b, 1);
  446. dofilter(cons.work+0, C0a, C0b, 1);
  447. dofilter(cons.work+1, C1a, C1b, 1);
  448. dofilter(cons.work+2, C2a, C2b, 1);
  449. dofilter(cons.rate+0, C0a, C0b, 1000);
  450. dofilter(cons.rate+1, C1a, C1b, 1000);
  451. dofilter(cons.rate+2, C2a, C2b, 1000);
  452. dofilter(cons.bhit+0, C0a, C0b, 1);
  453. dofilter(cons.bhit+1, C1a, C1b, 1);
  454. dofilter(cons.bhit+2, C2a, C2b, 1);
  455. dofilter(cons.bread+0, C0a, C0b, 1);
  456. dofilter(cons.bread+1, C1a, C1b, 1);
  457. dofilter(cons.bread+2, C2a, C2b, 1);
  458. dofilter(cons.brahead+0, C0a, C0b, 1);
  459. dofilter(cons.brahead+1, C1a, C1b, 1);
  460. dofilter(cons.brahead+2, C2a, C2b, 1);
  461. dofilter(cons.binit+0, C0a, C0b, 1);
  462. dofilter(cons.binit+1, C1a, C1b, 1);
  463. dofilter(cons.binit+2, C2a, C2b, 1);
  464. cons.chan = chaninit(Devcon, 1, 0);
  465. start:
  466. /*
  467. * part 1 -- read the config file
  468. */
  469. devnone = iconfig("n");
  470. cp = nvrgetconfig();
  471. print("config %s\n", cp);
  472. confdev = d = iconfig(cp);
  473. devinit(d);
  474. if(f.newconf) {
  475. p = getbuf(d, 0, Bmod);
  476. memset(p->iobuf, 0, RBUFSIZE);
  477. settag(p, Tconfig, 0);
  478. } else
  479. p = getbuf(d, 0, Bread|Bmod);
  480. if(!p || checktag(p, Tconfig, 0))
  481. panic("config io");
  482. mergeconf(p);
  483. if(f.modconf) {
  484. memset(p->iobuf, 0, BUFSIZE);
  485. p->flags |= Bmod|Bimm;
  486. if(service[0])
  487. sprint(strchr(p->iobuf, 0), "service %s\n", service);
  488. for(fs=filsys; fs->name; fs++)
  489. if(fs->conf)
  490. sprint(strchr(p->iobuf, 0),
  491. "filsys %s %s\n", fs->name, fs->conf);
  492. sprint(strchr(p->iobuf, 0), "ipauth %I\n", authip);
  493. sprint(strchr(p->iobuf, 0), "ipsntp %I\n", sntpip);
  494. for(i=0; i<10; i++) {
  495. if(isvalidip(ipaddr[i].sysip))
  496. sprint(strchr(p->iobuf, 0),
  497. "ip%d %I\n", i, ipaddr[i].sysip);
  498. if(isvalidip(ipaddr[i].defgwip))
  499. sprint(strchr(p->iobuf, 0),
  500. "ipgw%d %I\n", i, ipaddr[i].defgwip);
  501. if(isvalidip(ipaddr[i].defmask))
  502. sprint(strchr(p->iobuf, 0),
  503. "ipmask%d %I\n", i, ipaddr[i].defmask);
  504. }
  505. putbuf(p);
  506. f.modconf = 0;
  507. f.newconf = 0;
  508. print("config block written\n");
  509. goto start;
  510. }
  511. putbuf(p);
  512. print("service %s\n", service);
  513. print("ipauth %I\n", authip);
  514. print("ipsntp %I\n", sntpip);
  515. for(i=0; i<10; i++) {
  516. if(isvalidip(ipaddr[i].sysip)) {
  517. print("ip%d %I\n", i, ipaddr[i].sysip);
  518. print("ipgw%d %I\n", i, ipaddr[i].defgwip);
  519. print("ipmask%d %I\n", i, ipaddr[i].defmask);
  520. }
  521. }
  522. loop:
  523. /*
  524. * part 2 -- squeeze out the deleted filesystems
  525. */
  526. for(fs=filsys; fs->name; fs++)
  527. if(fs->conf == 0) {
  528. for(; fs->name; fs++)
  529. *fs = *(fs+1);
  530. goto loop;
  531. }
  532. if(filsys[0].name == 0)
  533. panic("no filsys");
  534. /*
  535. * part 3 -- compile the device expression
  536. */
  537. error = 0;
  538. for(fs=filsys; fs->name; fs++) {
  539. print("filsys %s %s\n", fs->name, fs->conf);
  540. fs->dev = iconfig(fs->conf);
  541. if(f.error) {
  542. error = 1;
  543. continue;
  544. }
  545. }
  546. if(error)
  547. panic("fs config");
  548. /*
  549. * part 4 -- initialize the devices
  550. */
  551. for(fs=filsys; fs->name; fs++) {
  552. delay(3000);
  553. print("sysinit: %s\n", fs->name);
  554. if(fs->flags & FREAM)
  555. devream(fs->dev, 1);
  556. if(fs->flags & FRECOVER)
  557. devrecover(fs->dev);
  558. devinit(fs->dev);
  559. }
  560. floppyhalt(); /* don't wear out the floppy */
  561. if (copyworm) {
  562. dowormcopy(); /* can return if user quits early */
  563. panic("copyworm bailed out!");
  564. }
  565. }
  566. /* an unfinished idea. a non-blocking rawchar() would help. */
  567. static int
  568. userabort(char *msg)
  569. {
  570. #ifdef IdeaIsFinished
  571. if (consgetcifany() == 'q') {
  572. print("aborting %s\n", msg);
  573. return 1;
  574. }
  575. #else
  576. USED(msg);
  577. #endif /* IdeaIsFinished */
  578. return 0;
  579. }
  580. static int
  581. blockok(Device *d, long a)
  582. {
  583. Iobuf *p = getbuf(d, a, Bread);
  584. if (p == 0) {
  585. print("i/o error reading %Z block %ld\n", d, a);
  586. return 0;
  587. }
  588. putbuf(p);
  589. return 1;
  590. }
  591. /*
  592. * special case for fake worms only:
  593. * we need to size the inner cw's worm device.
  594. * in particular, we want to avoid copying the fake-worm bitmap
  595. * at the end of the device.
  596. *
  597. * N.B.: for real worms (e.g. cw jukes), we need to compute devsize(cw(juke)),
  598. * *NOT* devsize(juke).
  599. */
  600. static Device *
  601. wormof(Device *dev)
  602. {
  603. Device *worm = dev, *cw;
  604. if (dev->type == Devfworm) {
  605. cw = dev->fw.fw;
  606. if (cw != nil && cw->type == Devcw)
  607. worm = cw->cw.w;
  608. }
  609. // print("wormof(%Z)=%Z\n", dev, worm);
  610. return worm;
  611. }
  612. /*
  613. * return the number of the highest-numbered block actually written, plus 1.
  614. * 0 indicates an error.
  615. */
  616. static long
  617. writtensize(Device *worm)
  618. {
  619. long lim = devsize(worm);
  620. Iobuf *p;
  621. print("devsize(%Z) = %ld\n", worm, lim);
  622. if (!blockok(worm, 0) || !blockok(worm, lim-1))
  623. return 0;
  624. delay(5*1000);
  625. if (userabort("sanity checks"))
  626. return 0;
  627. /* find worm's last valid block in case "worm" is an (f)worm */
  628. while (lim > 0) {
  629. if (userabort("sizing")) {
  630. lim = 0; /* you lose */
  631. break;
  632. }
  633. --lim;
  634. p = getbuf(worm, lim, Bread);
  635. if (p != 0) { /* actually read one okay? */
  636. putbuf(p);
  637. break;
  638. }
  639. }
  640. print("limit(%Z) = %ld\n", worm, lim);
  641. return lim <= 0? 0: lim + 1;
  642. }
  643. extern int devatadebug, devataidedebug;
  644. /* copy worm fs from "main"'s inner worm to "output" */
  645. static void
  646. dowormcopy(void)
  647. {
  648. Filsys *f1, *f2;
  649. Device *fdev, *from, *to = nil;
  650. Iobuf *p;
  651. long a, lim;
  652. /*
  653. * convert file system names into Filsyss and Devices.
  654. */
  655. f1 = fsstr("main");
  656. if(f1 == nil)
  657. panic("main file system missing");
  658. fdev = f1->dev;
  659. from = wormof(fdev); /* fake worm special */
  660. f2 = fsstr("output");
  661. if(f2 == nil) {
  662. print("no output file system - check only\n\n");
  663. print("reading worm from %Z (worm %Z)\n", fdev, from);
  664. } else {
  665. to = f2->dev;
  666. print("\ncopying worm from %Z (worm %Z) to %Z, starting in 8 seconds\n",
  667. fdev, from, to);
  668. delay(8000);
  669. }
  670. if (userabort("preparing to copy"))
  671. return;
  672. /*
  673. * initialise devices, size them, more sanity checking.
  674. */
  675. devinit(from);
  676. if (0 && fdev != from) {
  677. devinit(fdev);
  678. print("debugging, sizing %Z first\n", fdev);
  679. writtensize(fdev);
  680. }
  681. lim = writtensize(from);
  682. if(lim == 0)
  683. panic("no blocks to copy on %Z", from);
  684. if (to) {
  685. print("reaming %Z in 8 seconds\n", to);
  686. delay(8000);
  687. if (userabort("preparing to ream & copy"))
  688. return;
  689. devream(to, 0);
  690. devinit(to);
  691. print("copying worm: %ld blocks from %Z to %Z\n",
  692. lim, from, to);
  693. }
  694. /* can't read to's blocks in case to is a real WORM device */
  695. /*
  696. * Copy written fs blocks, a block at a time (or just read
  697. * if no "output" fs).
  698. */
  699. // devatadebug = 1; devataidedebug = 1;
  700. for (a = 0; a < lim; a++) {
  701. if (userabort("copy"))
  702. break;
  703. p = getbuf(from, a, Bread);
  704. /*
  705. * if from is a real WORM device, we'll get errors trying to
  706. * read unwritten blocks, but the unwritten blocks need not
  707. * be contiguous.
  708. */
  709. if (p == 0) {
  710. print("%ld not written yet; can't read\n", a);
  711. continue;
  712. }
  713. if (to != 0 && devwrite(to, p->addr, p->iobuf) != 0) {
  714. print("out block %ld: write error; bailing", a);
  715. break;
  716. }
  717. putbuf(p);
  718. if(a % 20000 == 0)
  719. print("block %ld %T\n", a, time());
  720. }
  721. /*
  722. * wrap up: sync target, loop
  723. */
  724. print("copied %ld blocks from %Z to %Z\n", a, from, to);
  725. sync("wormcopy");
  726. delay(2000);
  727. print("looping; reset the machine at any time.\n");
  728. for (; ; )
  729. continue; /* await reset */
  730. }
  731. void
  732. getline(char *line)
  733. {
  734. char *p;
  735. int c;
  736. p = line;
  737. for(;;) {
  738. c = rawchar(0);
  739. if(c == 0 || c == '\n') {
  740. *p = 0;
  741. return;
  742. }
  743. if(c == '\b') {
  744. p--;
  745. continue;
  746. }
  747. *p++ = c;
  748. }
  749. }
  750. void
  751. arginit(void)
  752. {
  753. int verb, c;
  754. char line[300], word[300], *cp;
  755. uchar localip[Pasize];
  756. Filsys *fs;
  757. if(nvrcheck() == 0){
  758. print("for config mode hit a key within 5 seconds\n");
  759. c = rawchar(5);
  760. if(c == 0) {
  761. print(" no config\n");
  762. return;
  763. }
  764. }
  765. loop:
  766. print("config: ");
  767. getline(line);
  768. cp = getwd(word, line);
  769. if(strcmp(word, "end") == 0)
  770. return;
  771. if(strcmp(word, "halt") == 0)
  772. exit();
  773. if(strcmp(word, "allow") == 0) {
  774. wstatallow = 1;
  775. writeallow = 1;
  776. goto loop;
  777. }
  778. if(strcmp(word, "copyworm") == 0) {
  779. copyworm = 1;
  780. goto loop;
  781. }
  782. if(strcmp(word, "noauth") == 0) {
  783. noauth = !noauth;
  784. goto loop;
  785. }
  786. if(strcmp(word, "noattach") == 0) {
  787. noattach = !noattach;
  788. goto loop;
  789. }
  790. if(strcmp(word, "readonly") == 0) {
  791. readonly = 1;
  792. goto loop;
  793. }
  794. if(strcmp(word, "ream") == 0) {
  795. verb = FREAM;
  796. goto gfsname;
  797. }
  798. if(strcmp(word, "recover") == 0) {
  799. verb = FRECOVER;
  800. goto gfsname;
  801. }
  802. if(strcmp(word, "filsys") == 0) {
  803. verb = FEDIT;
  804. goto gfsname;
  805. }
  806. if(strcmp(word, "nvram") == 0) {
  807. getwd(word, cp);
  808. if(testconfig(word))
  809. goto loop;
  810. if(nvrsetconfig(word) != 0)
  811. goto loop;
  812. goto loop;
  813. }
  814. if(strcmp(word, "config") == 0) {
  815. getwd(word, cp);
  816. if(testconfig(word))
  817. goto loop;
  818. if(nvrsetconfig(word) != 0)
  819. goto loop;
  820. f.newconf = 1;
  821. goto loop;
  822. }
  823. if(strcmp(word, "service") == 0) {
  824. getwd(word, cp);
  825. strcpy(service, word);
  826. f.modconf = 1;
  827. goto loop;
  828. }
  829. if(strcmp(word, "ipauth") == 0) {
  830. f.ipauthset = 1;
  831. verb = 2;
  832. goto ipname;
  833. }
  834. if(astrcmp(word, "ip") == 0) {
  835. verb = 0;
  836. goto ipname;
  837. }
  838. if(astrcmp(word, "ipgw") == 0) {
  839. verb = 1;
  840. goto ipname;
  841. }
  842. if(astrcmp(word, "ipmask") == 0) {
  843. verb = 3;
  844. goto ipname;
  845. }
  846. if(astrcmp(word, "ipsntp") == 0) {
  847. verb = 4;
  848. goto ipname;
  849. }
  850. print("unknown config command\n");
  851. print(" type end to get out\n");
  852. goto loop;
  853. ipname:
  854. getwd(word, cp);
  855. if(chartoip(localip, word)) {
  856. print("bad ip address\n");
  857. goto loop;
  858. }
  859. switch(verb) {
  860. case 0:
  861. memmove(ipaddr[aindex].sysip, localip,
  862. sizeof(ipaddr[aindex].sysip));
  863. break;
  864. case 1:
  865. memmove(ipaddr[aindex].defgwip, localip,
  866. sizeof(ipaddr[aindex].defgwip));
  867. break;
  868. case 2:
  869. memmove(authip, localip,
  870. sizeof(authip));
  871. break;
  872. case 3:
  873. memmove(ipaddr[aindex].defmask, localip,
  874. sizeof(ipaddr[aindex].defmask));
  875. break;
  876. case 4:
  877. memmove(sntpip, localip,
  878. sizeof(sntpip));
  879. break;
  880. }
  881. f.modconf = 1;
  882. goto loop;
  883. gfsname:
  884. cp = getwd(word, cp);
  885. for(fs=filsys; fs->name; fs++)
  886. if(strcmp(word, fs->name) == 0)
  887. goto found;
  888. memset(fs, 0, sizeof(*fs));
  889. fs->name = strdup(word);
  890. found:
  891. switch(verb) {
  892. case FREAM:
  893. if(strcmp(fs->name, "main") == 0)
  894. wstatallow = 1; /* only set, never reset */
  895. case FRECOVER:
  896. fs->flags |= verb;
  897. goto loop;
  898. case FEDIT:
  899. f.modconf = 1;
  900. getwd(word, cp);
  901. fs->flags |= verb;
  902. if(word[0] == 0) {
  903. fs->conf = 0;
  904. goto loop;
  905. }
  906. if(testconfig(word))
  907. goto loop;
  908. fs->conf = strdup(word);
  909. goto loop;
  910. }
  911. }