boot.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <auth.h>
  4. #include <fcall.h>
  5. #include "../boot/boot.h"
  6. char cputype[64];
  7. char sys[2*64];
  8. char reply[256];
  9. int printcol;
  10. int mflag;
  11. int fflag;
  12. int kflag;
  13. char *bargv[Nbarg];
  14. int bargc;
  15. static void swapproc(void);
  16. static Method *rootserver(char*);
  17. static void kbmap(void);
  18. void
  19. boot(int argc, char *argv[])
  20. {
  21. int fd, afd;
  22. Method *mp;
  23. char *cmd, cmdbuf[64], *iargv[16];
  24. char rootbuf[64];
  25. int islocal, ishybrid;
  26. char *rp, *rsp;
  27. int iargc, n;
  28. char buf[32];
  29. AuthInfo *ai;
  30. fmtinstall('r', errfmt);
  31. bind("#c", "/dev", MBEFORE);
  32. open("/dev/cons", OREAD);
  33. open("/dev/cons", OWRITE);
  34. open("/dev/cons", OWRITE);
  35. /*
  36. * init will reinitialize its namespace.
  37. * #ec gets us plan9.ini settings (*var variables).
  38. */
  39. bind("#ec", "/env", MREPL);
  40. bind("#e", "/env", MBEFORE|MCREATE);
  41. bind("#s", "/srv", MREPL|MCREATE);
  42. #ifdef DEBUG
  43. print("argc=%d\n", argc);
  44. for(fd = 0; fd < argc; fd++)
  45. print("%lux %s ", argv[fd], argv[fd]);
  46. print("\n");
  47. #endif DEBUG
  48. ARGBEGIN{
  49. case 'k':
  50. kflag = 1;
  51. break;
  52. case 'm':
  53. mflag = 1;
  54. break;
  55. case 'f':
  56. fflag = 1;
  57. break;
  58. }ARGEND
  59. readfile("#e/cputype", cputype, sizeof(cputype));
  60. /*
  61. * pick a method and initialize it
  62. */
  63. mp = rootserver(argc ? *argv : 0);
  64. (*mp->config)(mp);
  65. islocal = strcmp(mp->name, "local") == 0;
  66. ishybrid = strcmp(mp->name, "hybrid") == 0;
  67. /*
  68. * load keymap if its there
  69. */
  70. kbmap();
  71. /*
  72. * authentication agent
  73. */
  74. authentication(cpuflag);
  75. /*
  76. * connect to the root file system
  77. */
  78. fd = (*mp->connect)();
  79. if(fd < 0)
  80. fatal("can't connect to file server");
  81. if(getenv("srvold9p"))
  82. fd = old9p(fd);
  83. if(!islocal && !ishybrid){
  84. if(cfs)
  85. fd = (*cfs)(fd);
  86. }
  87. print("version...");
  88. buf[0] = '\0';
  89. n = fversion(fd, 0, buf, sizeof buf);
  90. if(n < 0)
  91. fatal("can't init 9P");
  92. srvcreate("boot", fd);
  93. /*
  94. * create the name space, mount the root fs
  95. */
  96. if(bind("/", "/", MREPL) < 0)
  97. fatal("bind /");
  98. rp = getenv("rootspec");
  99. if(rp == nil)
  100. rp = "";
  101. afd = fauth(fd, rp);
  102. if(afd >= 0){
  103. ai = auth_proxy(afd, auth_getkey, "proto=p9any role=client");
  104. if(ai == nil)
  105. print("authentication failed (%r), trying mount anyways\n");
  106. }
  107. if(mount(fd, afd, "/root", MREPL|MCREATE, rp) < 0)
  108. fatal("mount /");
  109. rsp = rp;
  110. rp = getenv("rootdir");
  111. if(rp == nil)
  112. rp = rootdir;
  113. if(bind(rp, "/", MAFTER|MCREATE) < 0){
  114. if(strncmp(rp, "/root", 5) == 0){
  115. fprint(2, "boot: couldn't bind $rootdir=%s to root: %r\n", rp);
  116. fatal("second bind /");
  117. }
  118. snprint(rootbuf, sizeof rootbuf, "/root/%s", rp);
  119. rp = rootbuf;
  120. if(bind(rp, "/", MAFTER|MCREATE) < 0){
  121. fprint(2, "boot: couldn't bind $rootdir=%s to root: %r\n", rp);
  122. if(strcmp(rootbuf, "/root//plan9") == 0){
  123. fprint(2, "**** warning: remove rootdir=/plan9 entry from plan9.ini\n");
  124. rp = "/root";
  125. if(bind(rp, "/", MAFTER|MCREATE) < 0)
  126. fatal("second bind /");
  127. }else
  128. fatal("second bind /");
  129. }
  130. }
  131. close(fd);
  132. setenv("rootdir", rp);
  133. settime(islocal, afd, rsp);
  134. if(afd > 0)
  135. close(afd);
  136. swapproc();
  137. cmd = getenv("init");
  138. if(cmd == nil){
  139. sprint(cmdbuf, "/%s/init -%s%s", cputype,
  140. cpuflag ? "c" : "t", mflag ? "m" : "");
  141. cmd = cmdbuf;
  142. }
  143. iargc = tokenize(cmd, iargv, nelem(iargv)-1);
  144. cmd = iargv[0];
  145. /* make iargv[0] basename(iargv[0]) */
  146. if(iargv[0] = strrchr(iargv[0], '/'))
  147. iargv[0]++;
  148. else
  149. iargv[0] = cmd;
  150. iargv[iargc] = nil;
  151. exec(cmd, iargv);
  152. fatal(cmd);
  153. }
  154. Method*
  155. findmethod(char *a)
  156. {
  157. Method *mp;
  158. int i, j;
  159. char *cp;
  160. i = strlen(a);
  161. cp = strchr(a, '!');
  162. if(cp)
  163. i = cp - a;
  164. for(mp = method; mp->name; mp++){
  165. j = strlen(mp->name);
  166. if(j > i)
  167. j = i;
  168. if(strncmp(a, mp->name, j) == 0)
  169. break;
  170. }
  171. if(mp->name)
  172. return mp;
  173. return 0;
  174. }
  175. /*
  176. * ask user from whence cometh the root file system
  177. */
  178. static Method*
  179. rootserver(char *arg)
  180. {
  181. char prompt[256];
  182. Method *mp;
  183. char *cp;
  184. int n;
  185. /* look for required reply */
  186. readfile("#e/nobootprompt", reply, sizeof(reply));
  187. if(reply[0]){
  188. mp = findmethod(reply);
  189. if(mp)
  190. goto HaveMethod;
  191. print("boot method %s not found\n", reply);
  192. reply[0] = 0;
  193. }
  194. /* make list of methods */
  195. mp = method;
  196. n = sprint(prompt, "root is from (%s", mp->name);
  197. for(mp++; mp->name; mp++)
  198. n += sprint(prompt+n, ", %s", mp->name);
  199. sprint(prompt+n, ")");
  200. /* create default reply */
  201. readfile("#e/bootargs", reply, sizeof(reply));
  202. if(reply[0] == 0 && arg != 0)
  203. strcpy(reply, arg);
  204. if(reply[0]){
  205. mp = findmethod(reply);
  206. if(mp == 0)
  207. reply[0] = 0;
  208. }
  209. if(reply[0] == 0)
  210. strcpy(reply, method->name);
  211. /* parse replies */
  212. for(;;){
  213. outin(prompt, reply, sizeof(reply));
  214. if(strlen(reply) == 0)
  215. continue;
  216. mp = findmethod(reply);
  217. if(mp){
  218. HaveMethod:
  219. bargc = tokenize(reply, bargv, Nbarg-2);
  220. bargv[bargc] = nil;
  221. cp = strchr(reply, '!');
  222. if(cp)
  223. strcpy(sys, cp+1);
  224. return mp;
  225. }
  226. }
  227. return 0; /* not reached */
  228. }
  229. static void
  230. swapproc(void)
  231. {
  232. int fd;
  233. fd = open("#c/swap", OWRITE);
  234. if(fd < 0){
  235. warning("opening #c/swap");
  236. return;
  237. }
  238. if(write(fd, "start", 5) <= 0)
  239. warning("starting swap kproc");
  240. close(fd);
  241. }
  242. int
  243. old9p(int fd)
  244. {
  245. int p[2];
  246. if(pipe(p) < 0)
  247. fatal("pipe");
  248. print("srvold9p...");
  249. switch(fork()) {
  250. case -1:
  251. fatal("rfork srvold9p");
  252. case 0:
  253. dup(fd, 1);
  254. close(fd);
  255. dup(p[0], 0);
  256. close(p[0]);
  257. close(p[1]);
  258. execl("/srvold9p", "srvold9p", "-s", 0);
  259. fatal("exec srvold9p");
  260. default:
  261. close(fd);
  262. close(p[0]);
  263. }
  264. return p[1];
  265. }
  266. static void
  267. kbmap(void)
  268. {
  269. char *f;
  270. int in, out;
  271. int n;
  272. char buf[1024];
  273. f = getenv("kbmap");
  274. if(f == nil)
  275. return;
  276. in = open(f, OREAD);
  277. if(in < 0){
  278. warning("can't open kbd map");
  279. return;
  280. }
  281. if(bind("#κ", "/dev", MAFTER) < 0){
  282. warning("can't bind #κ");
  283. close(in);
  284. return;
  285. }
  286. out = open("/dev/kbmap", OWRITE);
  287. if(out < 0){
  288. warning("can't open /dev/kbmap");
  289. close(out);
  290. return;
  291. }
  292. while((n = read(in, buf, sizeof(buf))) > 0)
  293. if(write(out, buf, n) != n){
  294. warning("write to /dev/kbmap failed");
  295. break;
  296. }
  297. close(in);
  298. close(out);
  299. }