load.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. #include "u.h"
  2. #include "lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "sd.h"
  8. #include "fs.h"
  9. /*
  10. * "cache" must be in this list so that 9load will pass the definition of
  11. * the cache partition into the kernel so that the disk named by the `cfs'
  12. * variable in plan9.ini can be seen in all circumstances before termrc
  13. * sets up all the disk partitions. In particular, if it's on an odd-ball
  14. * disk like sd10 rather than sdC0, this is needed.
  15. */
  16. static char *diskparts[] = {
  17. "dos", "9fat", "fs", "data", "cdboot", "cache", 0
  18. };
  19. static char *etherparts[] = { "*", 0 };
  20. static char *diskinis[] = {
  21. "plan9/plan9.ini",
  22. "plan9.ini",
  23. 0
  24. };
  25. static char *etherinis[] = {
  26. "/cfg/pxe/%E",
  27. 0
  28. };
  29. /* ordering: devbios must be called before devsd calls sdbios */
  30. Type types[] = {
  31. { Tfloppy,
  32. Fini|Ffs,
  33. floppyinit, floppyinitdev,
  34. floppygetfspart, 0, floppyboot,
  35. floppyprintdevs,
  36. diskparts,
  37. diskinis,
  38. },
  39. { Tether,
  40. Fini|Fbootp,
  41. etherinit, etherinitdev,
  42. pxegetfspart, 0, bootpboot,
  43. etherprintdevs,
  44. etherparts,
  45. etherinis,
  46. },
  47. { Tbios,
  48. Fini|Ffs,
  49. biosinit, biosinitdev,
  50. biosgetfspart, nil, biosboot,
  51. biosprintdevs,
  52. diskparts,
  53. diskinis,
  54. },
  55. { Tcd,
  56. Fini|Ffs,
  57. cdinit, sdinitdev,
  58. sdgetfspart, sdaddconf, sdboot,
  59. sdprintdevs,
  60. diskparts,
  61. diskinis,
  62. },
  63. { Tsd,
  64. Fini|Ffs,
  65. sdinit, sdinitdev,
  66. sdgetfspart, sdaddconf, sdboot,
  67. sdprintdevs,
  68. diskparts,
  69. diskinis,
  70. },
  71. { Tnil,
  72. 0,
  73. nil, nil, nil, nil, nil, nil,
  74. nil,
  75. nil,
  76. 0,
  77. nil,
  78. },
  79. };
  80. static char *typenm[] = {
  81. [Tnil] "nil",
  82. [Tfloppy] "floppy",
  83. [Tsd] "sd",
  84. [Tether] "ether",
  85. [Tcd] "cd",
  86. [Tbios] "bios",
  87. };
  88. static char *
  89. typename(int type)
  90. {
  91. if (type < 0 || type >= nelem(typenm) || typenm[type] == nil)
  92. return "**gok**";
  93. return typenm[type];
  94. }
  95. extern SDifc sdataifc;
  96. extern SDifc sdiahciifc;
  97. extern SDifc sdaoeifc;
  98. extern SDifc sdbiosifc;
  99. #ifdef NOSCSI
  100. SDifc* sdifc[] = {
  101. &sdataifc,
  102. &sdiahciifc,
  103. &sdbiosifc,
  104. &sdaoeifc,
  105. nil,
  106. };
  107. #else
  108. extern SDifc sdmylexifc;
  109. extern SDifc sd53c8xxifc;
  110. SDifc* sdifc[] = {
  111. &sdataifc,
  112. &sdiahciifc,
  113. &sdmylexifc,
  114. &sd53c8xxifc,
  115. &sdbiosifc,
  116. &sdaoeifc,
  117. nil,
  118. };
  119. #endif NOSCSI
  120. typedef struct Mode Mode;
  121. enum {
  122. Maxdev = 7,
  123. Dany = -1,
  124. Nmedia = 16,
  125. Nini = 10,
  126. };
  127. enum { /* mode */
  128. Mauto = 0x00,
  129. Mlocal = 0x01,
  130. Manual = 0x02,
  131. NMode = 0x03,
  132. };
  133. typedef struct Medium Medium;
  134. struct Medium {
  135. Type* type;
  136. int flag;
  137. int dev;
  138. char name[NAMELEN];
  139. Fs *inifs;
  140. char *part;
  141. char *ini;
  142. Medium* next;
  143. };
  144. typedef struct Mode {
  145. char* name;
  146. int mode;
  147. } Mode;
  148. static Medium media[Nmedia];
  149. static Medium *curmedium = media;
  150. static Mode modes[NMode+1] = {
  151. [Mauto] { "auto", Mauto, },
  152. [Mlocal] { "local", Mlocal, },
  153. [Manual] { "manual", Manual, },
  154. };
  155. char **ini;
  156. int scsi0port;
  157. char *defaultpartition;
  158. int iniread;
  159. int debugload;
  160. int vga;
  161. static Medium*
  162. parse(char *line, char **file)
  163. {
  164. char *p;
  165. Type *tp;
  166. Medium *mp;
  167. if(p = strchr(line, '!')) {
  168. *p++ = 0;
  169. *file = p;
  170. } else
  171. *file = "";
  172. for(tp = types; tp->type != Tnil; tp++)
  173. for(mp = tp->media; mp; mp = mp->next)
  174. if(strcmp(mp->name, line) == 0)
  175. return mp;
  176. if(p)
  177. *--p = '!';
  178. return nil;
  179. }
  180. static int
  181. boot(Medium *mp, char *file)
  182. {
  183. Type *tp;
  184. Medium *xmp;
  185. static int didaddconf;
  186. Boot b;
  187. memset(&b, 0, sizeof b);
  188. b.state = INITKERNEL;
  189. if(didaddconf == 0) {
  190. didaddconf = 1;
  191. for(tp = types; tp->type != Tnil; tp++)
  192. if(tp->addconf)
  193. for(xmp = tp->media; xmp; xmp = xmp->next)
  194. (*tp->addconf)(xmp->dev);
  195. }
  196. sprint(BOOTLINE, "%s!%s", mp->name, file);
  197. // print("booting %s!%s\n", mp->name, file);
  198. return (*mp->type->boot)(mp->dev, file, &b);
  199. }
  200. static Medium*
  201. allocm(Type *tp)
  202. {
  203. Medium **l;
  204. if(curmedium >= &media[Nmedia])
  205. return 0;
  206. for(l = &tp->media; *l; l = &(*l)->next)
  207. ;
  208. *l = curmedium++;
  209. return *l;
  210. }
  211. Medium*
  212. probe(int type, int flag, int dev)
  213. {
  214. Type *tp;
  215. int i;
  216. Medium *mp;
  217. File f;
  218. Fs *fs;
  219. char **partp;
  220. for(tp = types; tp->type != Tnil; tp++){
  221. if(type != Tany && type != tp->type)
  222. continue;
  223. if(flag != Fnone){
  224. for(mp = tp->media; mp; mp = mp->next){
  225. if((flag & mp->flag) && (dev == Dany || dev == mp->dev))
  226. return mp;
  227. }
  228. }
  229. if (debugload)
  230. print("probing %s...", typename(tp->type));
  231. if((tp->flag & Fprobe) == 0){
  232. tp->flag |= Fprobe;
  233. tp->mask = (*tp->init)();
  234. }
  235. for(i = 0; tp->mask; i++){
  236. if((tp->mask & (1<<i)) == 0)
  237. continue;
  238. tp->mask &= ~(1<<i);
  239. if((mp = allocm(tp)) == 0)
  240. continue;
  241. mp->dev = i;
  242. mp->flag = tp->flag;
  243. mp->type = tp;
  244. (*tp->initdev)(i, mp->name);
  245. if(mp->flag & Fini){
  246. mp->flag &= ~Fini;
  247. for(partp = tp->parts; *partp; partp++){
  248. if((fs = (*tp->getfspart)(i, *partp, 0)) == nil)
  249. continue;
  250. for(ini = tp->inis; *ini; ini++){
  251. if(fswalk(fs, *ini, &f) > 0){
  252. mp->inifs = fs;
  253. mp->part = *partp;
  254. mp->ini = f.path;
  255. mp->flag |= Fini;
  256. goto Break2;
  257. }
  258. }
  259. }
  260. }
  261. Break2:
  262. if((flag & mp->flag) && (dev == Dany || dev == i))
  263. return mp;
  264. }
  265. }
  266. return 0;
  267. }
  268. void
  269. main(void)
  270. {
  271. Medium *mp;
  272. int flag, i, mode, tried;
  273. char def[2*NAMELEN], line[80], *p, *file;
  274. Type *tp;
  275. i8042a20();
  276. memset(m, 0, sizeof(Mach));
  277. trapinit();
  278. clockinit();
  279. alarminit();
  280. meminit(0);
  281. spllo();
  282. /*
  283. * the soekris machines have no video but each has a serial port.
  284. * they must see serial output, if any, before cga output because
  285. * otherwise the soekris bios will translate cga output to serial
  286. * output, which will garble serial console output.
  287. */
  288. pcimatch(nil, 0, 0); /* force scan of pci table */
  289. if (!vga) {
  290. consinit("0", "9600"); /* e.g., for soekris debugging */
  291. print("Plan 9 Bootstrap (serial console)\n");
  292. }
  293. kbdinit();
  294. if((ulong)&end > (KZERO|(640*1024)))
  295. panic("i'm too big");
  296. if (debug)
  297. print("initial probe, for plan9.ini...");
  298. /* find and read plan9.ini, setting configuration variables */
  299. for(tp = types; tp->type != Tnil; tp++){
  300. /* skip bios until we have read plan9.ini */
  301. if(!pxe && tp->type == Tether || tp->type == Tbios)
  302. continue;
  303. if (debug)
  304. print("probing %s...", typename(tp->type));
  305. if((mp = probe(tp->type, Fini, Dany)) && (mp->flag & Fini)){
  306. if (debug)
  307. print("using %s!%s!%s\n",
  308. mp->name, mp->part, mp->ini);
  309. iniread = !dotini(mp->inifs);
  310. break;
  311. }
  312. }
  313. if (debug)
  314. print("\n");
  315. /*
  316. * we should now have read plan9.ini, if any.
  317. */
  318. debugload = getconf("*debugload") != nil;
  319. if((p = getconf("console")) != nil)
  320. consinit(p, getconf("baud"));
  321. prcpuid();
  322. readlsconf();
  323. apminit();
  324. devpccardlink();
  325. devi82365link();
  326. /*
  327. * Even after we find the ini file, we keep probing disks,
  328. * because we have to collect the partition tables and
  329. * have boot devices for parse.
  330. */
  331. probe(Tany, Fnone, Dany);
  332. if (debugload)
  333. print("end disk probe\n");
  334. tried = 0;
  335. mode = Mauto;
  336. p = getconf("bootfile");
  337. if(p != 0) {
  338. mode = Manual;
  339. for(i = 0; i < NMode; i++){
  340. if(strcmp(p, modes[i].name) == 0){
  341. mode = modes[i].mode;
  342. goto done;
  343. }
  344. }
  345. if((mp = parse(p, &file)) == nil) {
  346. print("Unknown boot device: %s\n", p);
  347. goto done;
  348. }
  349. tried = boot(mp, file);
  350. }
  351. done:
  352. if(tried == 0 && mode != Manual){
  353. flag = Fany;
  354. if(mode == Mlocal)
  355. flag &= ~Fbootp;
  356. if((mp = probe(Tany, flag, Dany)) && mp->type->type != Tfloppy)
  357. boot(mp, "");
  358. if (debugload)
  359. print("end auto probe\n");
  360. }
  361. def[0] = 0;
  362. probe(Tany, Fnone, Dany);
  363. if (debugload)
  364. print("end final probe\n");
  365. if(p = getconf("bootdef"))
  366. strcpy(def, p);
  367. flag = 0;
  368. for(tp = types; tp->type != Tnil; tp++){
  369. for(mp = tp->media; mp; mp = mp->next){
  370. if(flag == 0){
  371. flag = 1;
  372. print("Boot devices:");
  373. }
  374. (*tp->printdevs)(mp->dev);
  375. }
  376. }
  377. if(flag)
  378. print("\n");
  379. for(;;){
  380. if(getstr("boot from", line, sizeof(line), def, (mode != Manual)*15) >= 0)
  381. if(mp = parse(line, &file))
  382. boot(mp, file);
  383. def[0] = 0;
  384. }
  385. }
  386. int
  387. getfields(char *lp, char **fields, int n, char sep)
  388. {
  389. int i;
  390. for(i = 0; lp && *lp && i < n; i++){
  391. while(*lp == sep)
  392. *lp++ = 0;
  393. if(*lp == 0)
  394. break;
  395. fields[i] = lp;
  396. while(*lp && *lp != sep){
  397. if(*lp == '\\' && *(lp+1) == '\n')
  398. *lp++ = ' ';
  399. lp++;
  400. }
  401. }
  402. return i;
  403. }
  404. int
  405. cistrcmp(char *a, char *b)
  406. {
  407. int ac, bc;
  408. for(;;){
  409. ac = *a++;
  410. bc = *b++;
  411. if(ac >= 'A' && ac <= 'Z')
  412. ac = 'a' + (ac - 'A');
  413. if(bc >= 'A' && bc <= 'Z')
  414. bc = 'a' + (bc - 'A');
  415. ac -= bc;
  416. if(ac)
  417. return ac;
  418. if(bc == 0)
  419. break;
  420. }
  421. return 0;
  422. }
  423. int
  424. cistrncmp(char *a, char *b, int n)
  425. {
  426. unsigned ac, bc;
  427. while(n > 0){
  428. ac = *a++;
  429. bc = *b++;
  430. n--;
  431. if(ac >= 'A' && ac <= 'Z')
  432. ac = 'a' + (ac - 'A');
  433. if(bc >= 'A' && bc <= 'Z')
  434. bc = 'a' + (bc - 'A');
  435. ac -= bc;
  436. if(ac)
  437. return ac;
  438. if(bc == 0)
  439. break;
  440. }
  441. return 0;
  442. }
  443. #define PSTART ( 8*1024*1024)
  444. #define PEND (16*1024*1024)
  445. ulong palloc = PSTART;
  446. void*
  447. ialloc(ulong n, int align)
  448. {
  449. ulong p;
  450. int a;
  451. p = palloc;
  452. if(align <= 0)
  453. align = 4;
  454. if(a = n % align)
  455. n += align - a;
  456. if(a = p % align)
  457. p += align - a;
  458. palloc = p+n;
  459. if(palloc > PEND)
  460. panic("ialloc(%lud, %d) called from %#p",
  461. n, align, getcallerpc(&n));
  462. return memset((void*)(p|KZERO), 0, n);
  463. }
  464. void*
  465. xspanalloc(ulong size, int align, ulong span)
  466. {
  467. ulong a, v;
  468. if((palloc + (size+align+span)) > PEND)
  469. panic("xspanalloc(%lud, %d, 0x%lux) called from %#p",
  470. size, align, span, getcallerpc(&size));
  471. a = (ulong)ialloc(size+align+span, 0);
  472. if(span > 2)
  473. v = (a + span) & ~(span-1);
  474. else
  475. v = a;
  476. if(align > 1)
  477. v = (v + align) & ~(align-1);
  478. return (void*)v;
  479. }
  480. static Block *allocbp;
  481. Block*
  482. allocb(int size)
  483. {
  484. Block *bp, **lbp;
  485. ulong addr;
  486. lbp = &allocbp;
  487. for(bp = *lbp; bp; bp = bp->next){
  488. if((bp->lim - bp->base) >= size){
  489. *lbp = bp->next;
  490. break;
  491. }
  492. lbp = &bp->next;
  493. }
  494. if(bp == 0){
  495. if((palloc + (sizeof(Block)+size+64)) > PEND)
  496. panic("allocb(%d) called from %#p",
  497. size, getcallerpc(&size));
  498. bp = ialloc(sizeof(Block)+size+64, 0);
  499. addr = (ulong)bp;
  500. addr = ROUNDUP(addr + sizeof(Block), 8);
  501. bp->base = (uchar*)addr;
  502. bp->lim = ((uchar*)bp) + sizeof(Block)+size+64;
  503. }
  504. if(bp->flag)
  505. panic("allocb reuse");
  506. bp->rp = bp->base;
  507. bp->wp = bp->rp;
  508. bp->next = 0;
  509. bp->flag = 1;
  510. return bp;
  511. }
  512. void
  513. freeb(Block* bp)
  514. {
  515. bp->next = allocbp;
  516. allocbp = bp;
  517. bp->flag = 0;
  518. }
  519. enum {
  520. Paddr= 0x70, /* address port */
  521. Pdata= 0x71, /* data port */
  522. };
  523. uchar
  524. nvramread(int offset)
  525. {
  526. outb(Paddr, offset);
  527. return inb(Pdata);
  528. }
  529. void (*etherdetach)(void);
  530. void (*floppydetach)(void);
  531. void (*sddetach)(void);
  532. void
  533. warp9(ulong entry)
  534. {
  535. if(etherdetach)
  536. etherdetach();
  537. if(floppydetach)
  538. floppydetach();
  539. if(sddetach)
  540. sddetach();
  541. consdrain();
  542. splhi();
  543. trapdisable();
  544. /*
  545. * This is where to push things on the stack to
  546. * boot *BSD systems, e.g.
  547. (*(void(*)(void*, void*, void*, void*, ulong, ulong))(PADDR(entry)))(0, 0, 0, 0, 8196, 640);
  548. * will enable NetBSD boot (the real memory size needs to
  549. * go in the 5th argument).
  550. */
  551. (*(void(*)(void))(PADDR(entry)))();
  552. }