boot.c 6.4 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. int printcol;
  17. int mflag;
  18. int fflag;
  19. int kflag;
  20. char *bargv[Nbarg];
  21. int bargc;
  22. static Method *rootserver(char*);
  23. static void usbinit(void);
  24. static void kbmap(void);
  25. void
  26. boot(int argc, char *argv[])
  27. {
  28. int fd, afd;
  29. Method *mp;
  30. char *cmd, cmdbuf[64], *iargv[16];
  31. char rootbuf[64];
  32. int islocal, ishybrid;
  33. char *rp, *rsp;
  34. int iargc, n;
  35. char buf[32];
  36. AuthInfo *ai;
  37. fmtinstall('r', errfmt);
  38. bind("#c", "/dev", MBEFORE);
  39. /*
  40. * init will reinitialize its namespace.
  41. * #ec gets us plan9.ini settings (*var variables).
  42. */
  43. bind("#ec", "/env", MREPL);
  44. bind("#e", "/env", MBEFORE|MCREATE);
  45. bind("#s", "/srv", MREPL|MCREATE);
  46. bind("#p", "/proc", MREPL|MCREATE);
  47. print("Hello, I am Harvey :-)\n");
  48. #ifdef DEBUG
  49. print("argc=%d\n", argc);
  50. for(fd = 0; fd < argc; fd++)
  51. print("%#p %s ", argv[fd], argv[fd]);
  52. print("\n");
  53. #endif //DEBUG
  54. ARGBEGIN{
  55. case 'k':
  56. kflag = 1;
  57. break;
  58. case 'm':
  59. mflag = 1;
  60. break;
  61. case 'f':
  62. fflag = 1;
  63. break;
  64. }ARGEND
  65. readfile("#e/cputype", cputype, sizeof(cputype));
  66. /*
  67. * set up usb keyboard, mouse and disk, if any.
  68. */
  69. usbinit();
  70. print("usbinit done\n");
  71. /*
  72. * pick a method and initialize it
  73. */
  74. if(method[0].name == nil)
  75. fatal("no boot methods");
  76. mp = rootserver(argc ? *argv : 0);
  77. (*mp->config)(mp);
  78. islocal = strcmp(mp->name, "local") == 0;
  79. ishybrid = strcmp(mp->name, "hybrid") == 0;
  80. /*
  81. * load keymap if it is there.
  82. */
  83. kbmap();
  84. /*
  85. * authentication agent
  86. */
  87. authentication(cpuflag);
  88. print("connect...");
  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("\n");
  102. print("version...");
  103. buf[0] = '\0';
  104. n = fversion(fd, 0, buf, sizeof buf);
  105. if(n < 0)
  106. fatal("can't init 9P");
  107. srvcreate("boot", fd);
  108. /*
  109. * create the name space, mount the root fs
  110. */
  111. if(bind("/", "/", MREPL) < 0)
  112. fatal("bind /");
  113. rp = getenv("rootspec");
  114. if(rp == nil)
  115. rp = "";
  116. afd = fauth(fd, rp);
  117. if(afd >= 0){
  118. ai = auth_proxy(afd, auth_getkey, "proto=p9any role=client");
  119. if(ai == nil)
  120. print("authentication failed (%r), trying mount anyways\n");
  121. }
  122. if(mount(fd, afd, "/root", MREPL|MCREATE, rp, 'M') < 0)
  123. fatal("mount /");
  124. rsp = rp;
  125. rp = getenv("rootdir");
  126. if(rp == nil)
  127. rp = rootdir;
  128. if(bind(rp, "/", MAFTER|MCREATE) < 0){
  129. if(strncmp(rp, "/root", 5) == 0){
  130. fprint(2, "boot: couldn't bind $rootdir=%s to root: %r\n", rp);
  131. fatal("second bind /");
  132. }
  133. snprint(rootbuf, sizeof rootbuf, "/root/%s", rp);
  134. rp = rootbuf;
  135. if(bind(rp, "/", MAFTER|MCREATE) < 0){
  136. fprint(2, "boot: couldn't bind $rootdir=%s to root: %r\n", rp);
  137. if(strcmp(rootbuf, "/root//plan9") == 0){
  138. fprint(2, "**** warning: remove rootdir=/plan9 entry from plan9.ini\n");
  139. rp = "/root";
  140. if(bind(rp, "/", MAFTER|MCREATE) < 0)
  141. fatal("second bind /");
  142. }else
  143. fatal("second bind /");
  144. }
  145. }
  146. close(fd);
  147. setenv("rootdir", rp);
  148. settime(islocal, afd, rsp);
  149. if(afd > 0)
  150. close(afd);
  151. cmd = getenv("init");
  152. if(cmd == nil){
  153. sprint(cmdbuf, "/%s/bin/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. int rc;
  198. Method *mp;
  199. char *cp;
  200. char reply[256];
  201. int n;
  202. /* look for required reply */
  203. rc = readfile("#e/nobootprompt", reply, sizeof(reply));
  204. if(rc == 0 && reply[0]){
  205. mp = findmethod(reply);
  206. if(mp)
  207. goto HaveMethod;
  208. print("boot method %s not found\n", reply);
  209. reply[0] = 0;
  210. }
  211. /* make list of methods */
  212. mp = method;
  213. n = sprint(prompt, "root is from (%s", mp->name);
  214. for(mp++; mp->name; mp++)
  215. n += sprint(prompt+n, ", %s", mp->name);
  216. sprint(prompt+n, ")");
  217. /* create default reply */
  218. readfile("#e/bootargs", reply, sizeof(reply));
  219. if(reply[0] == 0 && arg != 0)
  220. strcpy(reply, arg);
  221. if(reply[0]){
  222. mp = findmethod(reply);
  223. if(mp == 0)
  224. reply[0] = 0;
  225. }
  226. if(reply[0] == 0)
  227. strcpy(reply, method->name);
  228. /* parse replies */
  229. do{
  230. outin(prompt, reply, sizeof(reply));
  231. mp = findmethod(reply);
  232. }while(mp == nil);
  233. HaveMethod:
  234. bargc = tokenize(reply, bargv, Nbarg-2);
  235. bargv[bargc] = nil;
  236. cp = strchr(reply, '!');
  237. if(cp)
  238. strcpy(sys, cp+1);
  239. return mp;
  240. }
  241. int
  242. old9p(int fd)
  243. {
  244. int p[2];
  245. if(pipe(p) < 0)
  246. fatal("pipe");
  247. print("srvold9p...");
  248. switch(fork()) {
  249. case -1:
  250. fatal("rfork srvold9p");
  251. case 0:
  252. dup(fd, 1);
  253. close(fd);
  254. dup(p[0], 0);
  255. close(p[0]);
  256. close(p[1]);
  257. execl("/srvold9p", "srvold9p", "-s", 0);
  258. fatal("exec srvold9p");
  259. default:
  260. close(fd);
  261. close(p[0]);
  262. }
  263. return p[1];
  264. }
  265. static void
  266. usbinit(void)
  267. {
  268. static char usbd[] = "/boot/usbd";
  269. if(access("#u/usb/ctl", 0) >= 0 && bind("#u", "/dev", MAFTER) >= 0 &&
  270. access(usbd, AEXIST) >= 0)
  271. run(usbd, nil);
  272. }
  273. static void
  274. kbmap(void)
  275. {
  276. char *f;
  277. int n, in, out;
  278. char buf[1024];
  279. f = getenv("kbmap");
  280. if(f == nil)
  281. return;
  282. if(bind("#κ", "/dev", MAFTER) < 0){
  283. warning("can't bind #κ");
  284. return;
  285. }
  286. in = open(f, OREAD);
  287. if(in < 0){
  288. warning("can't open kbd map");
  289. return;
  290. }
  291. out = open("/dev/kbmap", OWRITE);
  292. if(out < 0) {
  293. warning("can't open /dev/kbmap");
  294. close(in);
  295. return;
  296. }
  297. while((n = read(in, buf, sizeof(buf))) > 0)
  298. if(write(out, buf, n) != n){
  299. warning("write to /dev/kbmap failed");
  300. break;
  301. }
  302. close(in);
  303. close(out);
  304. }