load.c 12 KB

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