boot.c 5.9 KB

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