boot.c 6.3 KB

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