boot.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 service[64];
  16. char sys[2*64];
  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. static void acpiirq(void)
  27. {
  28. static char *devs[] = {"#Z", "#$", "#P"};
  29. int i, pid, irqmap;
  30. Waitmsg *w;
  31. for (i = 0; i < nelem(devs); i++){
  32. if(bind(devs[i], "/dev", MAFTER) < 0){
  33. fprint(2, "Can't bind %s: %r", devs[i]);
  34. return;
  35. }
  36. }
  37. irqmap = open("/dev/irqmap", OREAD);
  38. if (irqmap < 0) {
  39. warning("can't open /dev/irqmap");
  40. return;
  41. }
  42. pid = fork();
  43. if (pid < 0){
  44. warning("Can't fork: %r");
  45. close(irqmap);
  46. return;
  47. }
  48. if (pid > 0) {
  49. close(irqmap);
  50. w = wait();
  51. if (w && w->msg && w->msg[0])
  52. warning(w->msg);
  53. return;
  54. }
  55. dup(irqmap, 0);
  56. close(irqmap);
  57. if (execl("/boot/irq", "irq", "-s", nil)) {
  58. print("note: can't start /boot/irq");
  59. }
  60. exits(nil);
  61. }
  62. void
  63. boot(int argc, char *argv[])
  64. {
  65. int fd, afd, srvt;
  66. Method *mp;
  67. char *cmd, cmdbuf[64], *iargv[16];
  68. char rootbuf[64];
  69. int islocal, ishybrid;
  70. char *rp, *rsp;
  71. int iargc, n;
  72. char buf[32];
  73. AuthInfo *ai;
  74. fmtinstall('r', errfmt);
  75. /*
  76. * start /dev/cons
  77. */
  78. startconsole();
  79. /*
  80. * init will reinitialize its namespace.
  81. * #ec gets us plan9.ini settings (*var variables).
  82. */
  83. bind("#ec", "/env", MREPL);
  84. bind("#e", "/env", MBEFORE|MCREATE);
  85. bind("#s", "/srv", MREPL|MCREATE);
  86. bind("#p", "/proc", MREPL|MCREATE);
  87. print("Hello, I am Harvey :-)\n");
  88. #ifdef DEBUG
  89. print("argc=%d\n", argc);
  90. for(fd = 0; fd < argc; fd++)
  91. print("%#p %s ", argv[fd], argv[fd]);
  92. print("\n");
  93. #endif //DEBUG
  94. ARGBEGIN{
  95. case 'k':
  96. kflag = 1;
  97. break;
  98. case 'm':
  99. mflag = 1;
  100. break;
  101. case 'f':
  102. fflag = 1;
  103. break;
  104. }ARGEND
  105. readfile("#e/cputype", cputype, sizeof(cputype));
  106. readfile("#e/service", service, sizeof(service));
  107. /* Do the initial ACPI interrupt setup work.
  108. * If we don't do this we may not get needed
  109. * interfaces. */
  110. if (getenv("acpiirq"))
  111. acpiirq();
  112. /*
  113. * set up usb keyboard, mouse and disk, if any.
  114. */
  115. usbinit();
  116. print("usbinit done\n");
  117. /*
  118. * pick a method and initialize it
  119. */
  120. if(method[0].name == nil)
  121. fatal("no boot methods");
  122. mp = rootserver(argc ? *argv : 0);
  123. (*mp->config)(mp);
  124. islocal = strcmp(mp->name, "local") == 0;
  125. ishybrid = strcmp(mp->name, "hybrid") == 0;
  126. /*
  127. * load keymap if it is there.
  128. */
  129. kbmap();
  130. /*
  131. * authentication agent
  132. */
  133. authentication(cpuflag);
  134. print("connect...");
  135. /*
  136. * connect to the root file system
  137. */
  138. fd = (*mp->connect)();
  139. if(fd < 0)
  140. fatal("can't connect to file server");
  141. if(!islocal && !ishybrid){
  142. if(cfs)
  143. fd = (*cfs)(fd);
  144. }
  145. print("\n");
  146. print("version...");
  147. buf[0] = '\0';
  148. n = fversion(fd, 0, buf, sizeof buf);
  149. if(n < 0)
  150. fatal("can't init 9P");
  151. srvcreate("boot", fd);
  152. /*
  153. * create the name space, mount the root fs
  154. */
  155. if(bind("/", "/", MREPL) < 0)
  156. fatal("bind /");
  157. rp = getenv("rootspec");
  158. if(rp == nil)
  159. rp = "";
  160. afd = fauth(fd, rp);
  161. if(afd >= 0){
  162. ai = auth_proxy(afd, auth_getkey, "proto=p9any role=client");
  163. if(ai == nil)
  164. print("authentication failed (%r), trying mount anyways\n");
  165. }
  166. if(mount(fd, afd, "/root", MREPL|MCREATE, rp, 'M') < 0)
  167. fatal("mount /");
  168. rsp = rp;
  169. rp = getenv("rootdir");
  170. if(rp == nil)
  171. rp = rootdir;
  172. if(bind(rp, "/", MAFTER|MCREATE) < 0){
  173. if(strncmp(rp, "/root", 5) == 0){
  174. fprint(2, "boot: couldn't bind $rootdir=%s to root: %r\n", rp);
  175. fatal("second bind /");
  176. }
  177. snprint(rootbuf, sizeof rootbuf, "/root/%s", rp);
  178. rp = rootbuf;
  179. if(bind(rp, "/", MAFTER|MCREATE) < 0){
  180. fprint(2, "boot: couldn't bind $rootdir=%s to root: %r\n", rp);
  181. if(strcmp(rootbuf, "/root//plan9") == 0){
  182. fprint(2, "**** warning: remove rootdir=/plan9 entry from plan9.ini\n");
  183. rp = "/root";
  184. if(bind(rp, "/", MAFTER|MCREATE) < 0)
  185. fatal("second bind /");
  186. }else
  187. fatal("second bind /");
  188. }
  189. }
  190. close(fd);
  191. setenv("rootdir", rp);
  192. settime(islocal, afd, rsp);
  193. if(afd > 0)
  194. close(afd);
  195. cmd = getenv("init");
  196. srvt = strcmp(service, "terminal");
  197. if(cmd == nil){
  198. if(!srvt) {
  199. sprint(cmdbuf, "/%s/bin/init -%s%s", cputype,
  200. "t", mflag ? "m" : "");
  201. cmd = cmdbuf;
  202. } else {
  203. sprint(cmdbuf, "/%s/bin/init -%s%s", cputype,
  204. "c", mflag ? "m" : "");
  205. cmd = cmdbuf;
  206. }
  207. }
  208. iargc = tokenize(cmd, iargv, nelem(iargv)-1);
  209. cmd = iargv[0];
  210. /* make iargv[0] basename(iargv[0]) */
  211. if(iargv[0] = strrchr(iargv[0], '/'))
  212. iargv[0]++;
  213. else
  214. iargv[0] = cmd;
  215. iargv[iargc] = nil;
  216. exec(cmd, iargv);
  217. fatal(cmd);
  218. }
  219. static Method*
  220. findmethod(char *a)
  221. {
  222. Method *mp;
  223. int i, j;
  224. char *cp;
  225. if((i = strlen(a)) == 0)
  226. return nil;
  227. cp = strchr(a, '!');
  228. if(cp)
  229. i = cp - a;
  230. for(mp = method; mp->name; mp++){
  231. j = strlen(mp->name);
  232. if(j > i)
  233. j = i;
  234. if(strncmp(a, mp->name, j) == 0)
  235. break;
  236. }
  237. if(mp->name)
  238. return mp;
  239. return nil;
  240. }
  241. /*
  242. * ask user from whence cometh the root file system
  243. */
  244. static Method*
  245. rootserver(char *arg)
  246. {
  247. char prompt[256];
  248. int rc;
  249. Method *mp;
  250. char *cp;
  251. char reply[256];
  252. int n;
  253. /* look for required reply */
  254. rc = readfile("#e/nobootprompt", reply, sizeof(reply));
  255. if(rc == 0 && reply[0]){
  256. mp = findmethod(reply);
  257. if(mp)
  258. goto HaveMethod;
  259. print("boot method %s not found\n", reply);
  260. reply[0] = 0;
  261. }
  262. /* make list of methods */
  263. mp = method;
  264. n = sprint(prompt, "root is from (%s", mp->name);
  265. for(mp++; mp->name; mp++)
  266. n += sprint(prompt+n, ", %s", mp->name);
  267. sprint(prompt+n, ")");
  268. /* create default reply */
  269. readfile("#e/bootargs", reply, sizeof(reply));
  270. if(reply[0] == 0 && arg != 0)
  271. strcpy(reply, arg);
  272. if(reply[0]){
  273. mp = findmethod(reply);
  274. if(mp == 0)
  275. reply[0] = 0;
  276. }
  277. if(reply[0] == 0)
  278. strcpy(reply, method->name);
  279. /* parse replies */
  280. do{
  281. outin(prompt, reply, sizeof(reply));
  282. mp = findmethod(reply);
  283. }while(mp == nil);
  284. HaveMethod:
  285. bargc = tokenize(reply, bargv, Nbarg-2);
  286. bargv[bargc] = nil;
  287. cp = strchr(reply, '!');
  288. if(cp)
  289. strcpy(sys, cp+1);
  290. return mp;
  291. }
  292. static void
  293. usbinit(void)
  294. {
  295. static char usbd[] = "/boot/usbd";
  296. static char *argv[] = {"usbd"};
  297. if (access(usbd, AEXIST) < 0) {
  298. print("usbinit: no %s\n", usbd);
  299. return;
  300. }
  301. if(access("#u/usb/ctl", 0) < 0){
  302. print("usbinit: no #u/usb/ctl\n");
  303. return;
  304. }
  305. if (bind("#I", "/dev", MAFTER) < 0) {
  306. print("usbinit: can't bind #I to /dev: %r\n");
  307. return;
  308. }
  309. if (bind("#u", "/dev", MAFTER) < 0) {
  310. print("usbinit: can't bind #u to /dev: %r\n");
  311. return;
  312. }
  313. switch(fork()){
  314. case -1:
  315. print("usbinit: fork failed: %r\n");
  316. case 0:
  317. exec(usbd, argv);
  318. fatal("can't exec usbd");
  319. default:
  320. break;
  321. }
  322. }
  323. static void
  324. kbmap(void)
  325. {
  326. char *f;
  327. int n, in, out;
  328. char buf[1024];
  329. f = getenv("kbmap");
  330. if(f == nil)
  331. return;
  332. if(bind("#κ", "/dev", MAFTER) < 0){
  333. warning("can't bind #κ");
  334. return;
  335. }
  336. in = open(f, OREAD);
  337. if(in < 0){
  338. warning("can't open kbd map");
  339. return;
  340. }
  341. out = open("/dev/kbmap", OWRITE);
  342. if(out < 0) {
  343. warning("can't open /dev/kbmap");
  344. close(in);
  345. return;
  346. }
  347. while((n = read(in, buf, sizeof(buf))) > 0)
  348. if(write(out, buf, n) != n){
  349. warning("write to /dev/kbmap failed");
  350. break;
  351. }
  352. close(in);
  353. close(out);
  354. }