srvinit.b 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. implement Init;
  2. #
  3. # init program for standalone wm using TK
  4. #
  5. include "sys.m";
  6. sys: Sys;
  7. FD, Connection, sprint, Dir: import sys;
  8. print, fprint, open, bind, mount, dial, sleep, read: import sys;
  9. include "security.m";
  10. auth: Auth;
  11. include "draw.m";
  12. draw: Draw;
  13. Context: import draw;
  14. include "keyring.m";
  15. kr: Keyring;
  16. Init: module
  17. {
  18. init: fn();
  19. };
  20. Command: module
  21. {
  22. init: fn(ctxt: ref Context, argv: list of string);
  23. };
  24. rootfs(server: string): int
  25. {
  26. ok, n: int;
  27. c: Connection;
  28. (ok, c) = dial("tcp!" + server + "!6666", nil);
  29. if(ok < 0) {
  30. sys->print("cannot connected to %s: %r\n", server);
  31. return -1;
  32. }
  33. sys->print("Connected ...");
  34. if(kr != nil && auth != nil){
  35. err: string;
  36. sys->print("Authenticate ...");
  37. ai := kr->readauthinfo("/nvfs/default");
  38. if(ai == nil){
  39. sys->print("readauthinfo /nvfs/default failed: %r\n");
  40. sys->print("trying mount as `nobody'\n");
  41. }
  42. (c.dfd, err) = auth->client("none", ai, c.dfd);
  43. if(c.dfd == nil){
  44. sys->print("authentication failed: %s\n", err);
  45. return -1;
  46. }
  47. }
  48. sys->print("mount ...");
  49. c.cfd = nil;
  50. n = mount(c.dfd, "/", sys->MREPL, "");
  51. if(n > 0)
  52. return 0;
  53. return -1;
  54. }
  55. Bootpreadlen: con 128;
  56. init()
  57. {
  58. spec: string;
  59. sys = load Sys Sys->PATH;
  60. kr = load Keyring Keyring->PATH;
  61. auth = load Auth Auth->PATH;
  62. if(auth != nil)
  63. auth->init();
  64. sys->print("**\n** Inferno\n** Vita Nuova\n**\n");
  65. sys->print("Setup boot net services ...\n");
  66. #
  67. # Setup what we need to call a server and
  68. # Authenticate
  69. #
  70. bind("#l", "/net", sys->MREPL);
  71. bind("#I", "/net", sys->MAFTER);
  72. bind("#c", "/dev", sys->MAFTER);
  73. bind("#H", "/dev", sys->MAFTER);
  74. nvramfd := sys->open("#H/hd0nvram", sys->ORDWR);
  75. if(nvramfd != nil){
  76. spec = "#Fhd0nvram";
  77. if(bind(spec, "/nvfs", sys->MAFTER) < 0)
  78. print("init: bind %s: %r\n", spec);
  79. }
  80. setsysname();
  81. sys->print("bootp...");
  82. fd := open("/net/ipifc/clone", sys->OWRITE);
  83. if(fd == nil) {
  84. print("init: open /net/ipifc/clone: %r\n");
  85. exit;
  86. }
  87. cfg := array of byte "bind ether ether0";
  88. if(sys->write(fd, cfg, len cfg) != len cfg) {
  89. sys->print("could not bind interface: %r\n");
  90. exit;
  91. }
  92. cfg = array of byte "bootp";
  93. if(sys->write(fd, cfg, len cfg) != len cfg) {
  94. sys->print("could not bootp: %r\n");
  95. exit;
  96. }
  97. fd = open("/net/bootp", sys->OREAD);
  98. if(fd == nil) {
  99. print("init: open /net/bootp: %r");
  100. exit;
  101. }
  102. buf := array[Bootpreadlen] of byte;
  103. nr := read(fd, buf, len buf);
  104. fd = nil;
  105. if(nr <= 0) {
  106. print("init: read /net/bootp: %r");
  107. exit;
  108. }
  109. (ntok, ls) := sys->tokenize(string buf, " \t\n");
  110. while(ls != nil) {
  111. if(hd ls == "fsip"){
  112. ls = tl ls;
  113. break;
  114. }
  115. ls = tl ls;
  116. }
  117. if(ls == nil) {
  118. print("init: server address not in bootp read");
  119. exit;
  120. }
  121. srv := hd ls;
  122. sys->print("server %s\nConnect ...\n", srv);
  123. retrycount := 0;
  124. while(rootfs(srv) < 0 && retrycount++ < 5)
  125. sleep(1000);
  126. sys->print("done\n");
  127. #
  128. # default namespace
  129. #
  130. bind("#c", "/dev", sys->MREPL); # console
  131. bind("#l", "/net", sys->MREPL); # ethernet
  132. bind("#I", "/net", sys->MBEFORE); # TCP/IP
  133. bind("#p", "/prog", sys->MREPL); # prog device
  134. sys->bind("#d", "/fd", Sys->MREPL);
  135. sys->print("clock...\n");
  136. setclock();
  137. sys->print("sh...\n");
  138. sh := load Command "/dis/sh.dis";
  139. if (sh == nil)
  140. print("cannot load /dis/sh.dis: %r\n");
  141. sh->init(nil, "sh" :: "-i" :: nil);
  142. }
  143. setclock()
  144. {
  145. (ok, dir) := sys->stat("/");
  146. if (ok < 0) {
  147. print("init: stat /: %r");
  148. return;
  149. }
  150. fd := sys->open("/dev/time", sys->OWRITE);
  151. if (fd == nil) {
  152. print("init: open /dev/time: %r");
  153. return;
  154. }
  155. # Time is kept as microsecs, atime is in secs
  156. b := array of byte sprint("%d000000", dir.atime);
  157. if (sys->write(fd, b, len b) != len b)
  158. print("init: write /dev/time: %r");
  159. }
  160. #
  161. # Set system name from nvram
  162. #
  163. setsysname()
  164. {
  165. fd := open("/nvfs/ID", sys->OREAD);
  166. if(fd == nil)
  167. return;
  168. fds := open("/dev/sysname", sys->OWRITE);
  169. if(fds == nil)
  170. return;
  171. buf := array[128] of byte;
  172. nr := sys->read(fd, buf, len buf);
  173. if(nr <= 0)
  174. return;
  175. sys->write(fds, buf, nr);
  176. }