boot.c 6.3 KB

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