boot.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. void
  27. boot(int argc, char *argv[])
  28. {
  29. int fd, afd, srvt;
  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. /*
  41. * init will reinitialize its namespace.
  42. * #ec gets us plan9.ini settings (*var variables).
  43. */
  44. bind("#ec", "/env", MREPL);
  45. bind("#e", "/env", MBEFORE|MCREATE);
  46. bind("#s", "/srv", MREPL|MCREATE);
  47. bind("#p", "/proc", MREPL|MCREATE);
  48. print("Hello, I am Harvey :-)\n");
  49. #ifdef DEBUG
  50. print("argc=%d\n", argc);
  51. for(fd = 0; fd < argc; fd++)
  52. print("%#p %s ", argv[fd], argv[fd]);
  53. print("\n");
  54. #endif //DEBUG
  55. ARGBEGIN{
  56. case 'k':
  57. kflag = 1;
  58. break;
  59. case 'm':
  60. mflag = 1;
  61. break;
  62. case 'f':
  63. fflag = 1;
  64. break;
  65. }ARGEND
  66. readfile("#e/cputype", cputype, sizeof(cputype));
  67. readfile("#e/service", service, sizeof(service));
  68. /*
  69. * set up usb keyboard, mouse and disk, if any.
  70. */
  71. usbinit();
  72. print("usbinit done\n");
  73. /*
  74. * pick a method and initialize it
  75. */
  76. if(method[0].name == nil)
  77. fatal("no boot methods");
  78. mp = rootserver(argc ? *argv : 0);
  79. (*mp->config)(mp);
  80. islocal = strcmp(mp->name, "local") == 0;
  81. ishybrid = strcmp(mp->name, "hybrid") == 0;
  82. /*
  83. * load keymap if it is there.
  84. */
  85. kbmap();
  86. /*
  87. * authentication agent
  88. */
  89. authentication(cpuflag);
  90. print("connect...");
  91. /*
  92. * connect to the root file system
  93. */
  94. fd = (*mp->connect)();
  95. if(fd < 0)
  96. fatal("can't connect to file server");
  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. srvt = strcmp(service, "terminal");
  153. if(cmd == nil){
  154. if(!srvt) {
  155. sprint(cmdbuf, "/%s/bin/init -%s%s", cputype,
  156. "t", mflag ? "m" : "");
  157. cmd = cmdbuf;
  158. } else {
  159. sprint(cmdbuf, "/%s/bin/init -%s%s", cputype,
  160. "c", mflag ? "m" : "");
  161. cmd = cmdbuf;
  162. }
  163. }
  164. iargc = tokenize(cmd, iargv, nelem(iargv)-1);
  165. cmd = iargv[0];
  166. /* make iargv[0] basename(iargv[0]) */
  167. if(iargv[0] = strrchr(iargv[0], '/'))
  168. iargv[0]++;
  169. else
  170. iargv[0] = cmd;
  171. iargv[iargc] = nil;
  172. exec(cmd, iargv);
  173. fatal(cmd);
  174. }
  175. static Method*
  176. findmethod(char *a)
  177. {
  178. Method *mp;
  179. int i, j;
  180. char *cp;
  181. if((i = strlen(a)) == 0)
  182. return nil;
  183. cp = strchr(a, '!');
  184. if(cp)
  185. i = cp - a;
  186. for(mp = method; mp->name; mp++){
  187. j = strlen(mp->name);
  188. if(j > i)
  189. j = i;
  190. if(strncmp(a, mp->name, j) == 0)
  191. break;
  192. }
  193. if(mp->name)
  194. return mp;
  195. return nil;
  196. }
  197. /*
  198. * ask user from whence cometh the root file system
  199. */
  200. static Method*
  201. rootserver(char *arg)
  202. {
  203. char prompt[256];
  204. int rc;
  205. Method *mp;
  206. char *cp;
  207. char reply[256];
  208. int n;
  209. /* look for required reply */
  210. rc = readfile("#e/nobootprompt", reply, sizeof(reply));
  211. if(rc == 0 && reply[0]){
  212. mp = findmethod(reply);
  213. if(mp)
  214. goto HaveMethod;
  215. print("boot method %s not found\n", reply);
  216. reply[0] = 0;
  217. }
  218. /* make list of methods */
  219. mp = method;
  220. n = sprint(prompt, "root is from (%s", mp->name);
  221. for(mp++; mp->name; mp++)
  222. n += sprint(prompt+n, ", %s", mp->name);
  223. sprint(prompt+n, ")");
  224. /* create default reply */
  225. readfile("#e/bootargs", reply, sizeof(reply));
  226. if(reply[0] == 0 && arg != 0)
  227. strcpy(reply, arg);
  228. if(reply[0]){
  229. mp = findmethod(reply);
  230. if(mp == 0)
  231. reply[0] = 0;
  232. }
  233. if(reply[0] == 0)
  234. strcpy(reply, method->name);
  235. /* parse replies */
  236. do{
  237. outin(prompt, reply, sizeof(reply));
  238. mp = findmethod(reply);
  239. }while(mp == nil);
  240. HaveMethod:
  241. bargc = tokenize(reply, bargv, Nbarg-2);
  242. bargv[bargc] = nil;
  243. cp = strchr(reply, '!');
  244. if(cp)
  245. strcpy(sys, cp+1);
  246. return mp;
  247. }
  248. static void
  249. usbinit(void)
  250. {
  251. static char usbd[] = "/boot/usbd";
  252. static char *argv[] = {"usbd"};
  253. if (access(usbd, AEXIST) < 0) {
  254. print("usbinit: no %s\n", usbd);
  255. return;
  256. }
  257. if(access("#u/usb/ctl", 0) < 0){
  258. print("usbinit: no #u/usb/ctl\n");
  259. return;
  260. }
  261. if (bind("#I", "/dev", MAFTER) < 0) {
  262. print("usbinit: can't bind #I to /dev: %r\n");
  263. return;
  264. }
  265. if (bind("#u", "/dev", MAFTER) < 0) {
  266. print("usbinit: can't bind #u to /dev: %r\n");
  267. return;
  268. }
  269. switch(fork()){
  270. case -1:
  271. print("usbinit: fork failed: %r\n");
  272. case 0:
  273. exec(usbd, argv);
  274. fatal("can't exec usbd");
  275. default:
  276. break;
  277. }
  278. }
  279. static void
  280. kbmap(void)
  281. {
  282. char *f;
  283. int n, in, out;
  284. char buf[1024];
  285. f = getenv("kbmap");
  286. if(f == nil)
  287. return;
  288. if(bind("#κ", "/dev", MAFTER) < 0){
  289. warning("can't bind #κ");
  290. return;
  291. }
  292. in = open(f, OREAD);
  293. if(in < 0){
  294. warning("can't open kbd map");
  295. return;
  296. }
  297. out = open("/dev/kbmap", OWRITE);
  298. if(out < 0) {
  299. warning("can't open /dev/kbmap");
  300. close(in);
  301. return;
  302. }
  303. while((n = read(in, buf, sizeof(buf))) > 0)
  304. if(write(out, buf, n) != n){
  305. warning("write to /dev/kbmap failed");
  306. break;
  307. }
  308. close(in);
  309. close(out);
  310. }