load.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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", "plan9", "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. /*
  294. * find and read plan9.ini, setting configuration variables.
  295. */
  296. if (debug)
  297. print("plan9.ini probe...");
  298. biosload = 1; /* initially be optimistic */
  299. for(tp = types; tp->type != Tnil; tp++){
  300. /*
  301. * we don't know which ether interface to use nor
  302. * whether bios loading is disabled until we have read
  303. * plan9.ini. make an exception for 9pxeload: probe
  304. * ethers anyway. see if we can live with always
  305. * probing the bios.
  306. */
  307. if(!pxe && tp->type == Tether /*|| !vga && tp->type == Tbios */)
  308. continue;
  309. if (debug)
  310. print("probing %s...", typename(tp->type));
  311. if((mp = probe(tp->type, Fini, Dany)) && (mp->flag & Fini)){
  312. if (debug)
  313. print("using %s!%s!%s\n",
  314. mp->name, mp->part, mp->ini);
  315. iniread = !dotini(mp->inifs);
  316. break;
  317. }
  318. }
  319. if (debug)
  320. print("\n");
  321. /*
  322. * we should now have read plan9.ini, if any.
  323. */
  324. if (!iniread)
  325. print("no plan9.ini\n");
  326. persist = getconf("*bootppersist");
  327. debugload = getconf("*debugload") != nil;
  328. /* hack for soekris-like machines */
  329. if(!vga || getconf("*nobiosload") != nil)
  330. biosload = 0;
  331. if((p = getconf("console")) != nil)
  332. consinit(p, getconf("baud"));
  333. prcpuid();
  334. readlsconf();
  335. apminit();
  336. devpccardlink();
  337. devi82365link();
  338. /*
  339. * Even after we find the ini file, we keep probing disks,
  340. * because we have to collect the partition tables and
  341. * have boot devices for parse.
  342. */
  343. probe(Tany, Fnone, Dany);
  344. if (debugload)
  345. print("end disk probe\n");
  346. tried = 0;
  347. mode = Mauto;
  348. p = getconf("bootfile");
  349. if(p != 0) {
  350. mode = Manual;
  351. for(i = 0; i < NMode; i++){
  352. if(strcmp(p, modes[i].name) == 0){
  353. mode = modes[i].mode;
  354. goto done;
  355. }
  356. }
  357. if((mp = parse(p, &file)) == nil) {
  358. print("Unknown boot device: %s\n", p);
  359. goto done;
  360. }
  361. tried = boot(mp, file);
  362. }
  363. done:
  364. if(tried == 0 && mode != Manual){
  365. flag = Fany;
  366. if(mode == Mlocal)
  367. flag &= ~Fbootp;
  368. if((mp = probe(Tany, flag, Dany)) && mp->type->type != Tfloppy)
  369. boot(mp, "");
  370. if (debugload)
  371. print("end auto-boot probe\n");
  372. }
  373. def[0] = 0;
  374. probe(Tany, Fnone, Dany);
  375. if (debugload)
  376. print("end final probe\n");
  377. if(p = getconf("bootdef"))
  378. strcpy(def, p);
  379. /* print possible boot methods */
  380. flag = 0;
  381. for(tp = types; tp->type != Tnil; tp++){
  382. for(mp = tp->media; mp; mp = mp->next){
  383. if(flag == 0){
  384. flag = 1;
  385. print("Boot devices:");
  386. }
  387. (*tp->printdevs)(mp->dev);
  388. }
  389. }
  390. if(flag)
  391. print("\n");
  392. /*
  393. * e.g., *bootppersist=ether0
  394. *
  395. * previously, we looped in bootpopen if we were pxeload or if
  396. * *bootppersist was set. that doesn't work well for pxeload
  397. * in configurations where bootp will never succeed on the first
  398. * interface but only on another interface.
  399. */
  400. if (mode == Mauto && persist != nil &&
  401. (mp = parse(persist, &file)) != nil) {
  402. boot(mp, file);
  403. print("pausing before retry...");
  404. delay(30*1000);
  405. print("\n");
  406. }
  407. for(;;){
  408. if(getstr("boot from", line, sizeof(line), def, (mode != Manual)*15) >= 0)
  409. if(mp = parse(line, &file))
  410. boot(mp, file);
  411. def[0] = 0;
  412. }
  413. }
  414. int
  415. getfields(char *lp, char **fields, int n, char sep)
  416. {
  417. int i;
  418. for(i = 0; lp && *lp && i < n; i++){
  419. while(*lp == sep)
  420. *lp++ = 0;
  421. if(*lp == 0)
  422. break;
  423. fields[i] = lp;
  424. while(*lp && *lp != sep){
  425. if(*lp == '\\' && *(lp+1) == '\n')
  426. *lp++ = ' ';
  427. lp++;
  428. }
  429. }
  430. return i;
  431. }
  432. int
  433. cistrcmp(char *a, char *b)
  434. {
  435. int ac, bc;
  436. for(;;){
  437. ac = *a++;
  438. bc = *b++;
  439. if(ac >= 'A' && ac <= 'Z')
  440. ac = 'a' + (ac - 'A');
  441. if(bc >= 'A' && bc <= 'Z')
  442. bc = 'a' + (bc - 'A');
  443. ac -= bc;
  444. if(ac)
  445. return ac;
  446. if(bc == 0)
  447. break;
  448. }
  449. return 0;
  450. }
  451. int
  452. cistrncmp(char *a, char *b, int n)
  453. {
  454. unsigned ac, bc;
  455. while(n > 0){
  456. ac = *a++;
  457. bc = *b++;
  458. n--;
  459. if(ac >= 'A' && ac <= 'Z')
  460. ac = 'a' + (ac - 'A');
  461. if(bc >= 'A' && bc <= 'Z')
  462. bc = 'a' + (bc - 'A');
  463. ac -= bc;
  464. if(ac)
  465. return ac;
  466. if(bc == 0)
  467. break;
  468. }
  469. return 0;
  470. }
  471. #define PSTART ( 8*1024*1024)
  472. #define PEND (16*1024*1024)
  473. ulong palloc = PSTART;
  474. void*
  475. ialloc(ulong n, int align)
  476. {
  477. ulong p;
  478. int a;
  479. p = palloc;
  480. if(align <= 0)
  481. align = 4;
  482. if(a = n % align)
  483. n += align - a;
  484. if(a = p % align)
  485. p += align - a;
  486. palloc = p+n;
  487. if(palloc > PEND)
  488. panic("ialloc(%lud, %d) called from %#p",
  489. n, align, getcallerpc(&n));
  490. return memset((void*)(p|KZERO), 0, n);
  491. }
  492. void*
  493. xspanalloc(ulong size, int align, ulong span)
  494. {
  495. ulong a, v;
  496. if((palloc + (size+align+span)) > PEND)
  497. panic("xspanalloc(%lud, %d, 0x%lux) called from %#p",
  498. size, align, span, getcallerpc(&size));
  499. a = (ulong)ialloc(size+align+span, 0);
  500. if(span > 2)
  501. v = (a + span) & ~(span-1);
  502. else
  503. v = a;
  504. if(align > 1)
  505. v = (v + align) & ~(align-1);
  506. return (void*)v;
  507. }
  508. static Block *allocbp;
  509. Block*
  510. allocb(int size)
  511. {
  512. Block *bp, **lbp;
  513. ulong addr;
  514. lbp = &allocbp;
  515. for(bp = *lbp; bp; bp = bp->next){
  516. if((bp->lim - bp->base) >= size){
  517. *lbp = bp->next;
  518. break;
  519. }
  520. lbp = &bp->next;
  521. }
  522. if(bp == 0){
  523. if((palloc + (sizeof(Block)+size+64)) > PEND)
  524. panic("allocb(%d) called from %#p",
  525. size, getcallerpc(&size));
  526. bp = ialloc(sizeof(Block)+size+64, 0);
  527. addr = (ulong)bp;
  528. addr = ROUNDUP(addr + sizeof(Block), 8);
  529. bp->base = (uchar*)addr;
  530. bp->lim = ((uchar*)bp) + sizeof(Block)+size+64;
  531. }
  532. if(bp->flag)
  533. panic("allocb reuse");
  534. bp->rp = bp->base;
  535. bp->wp = bp->rp;
  536. bp->next = 0;
  537. bp->flag = 1;
  538. return bp;
  539. }
  540. void
  541. freeb(Block* bp)
  542. {
  543. bp->next = allocbp;
  544. allocbp = bp;
  545. bp->flag = 0;
  546. }
  547. enum {
  548. Paddr= 0x70, /* address port */
  549. Pdata= 0x71, /* data port */
  550. };
  551. uchar
  552. nvramread(int offset)
  553. {
  554. outb(Paddr, offset);
  555. return inb(Pdata);
  556. }
  557. void (*etherdetach)(void);
  558. void (*floppydetach)(void);
  559. void (*sddetach)(void);
  560. void
  561. warp9(ulong entry)
  562. {
  563. if(etherdetach)
  564. etherdetach();
  565. if(floppydetach)
  566. floppydetach();
  567. if(sddetach)
  568. sddetach();
  569. consdrain();
  570. splhi();
  571. trapdisable();
  572. /*
  573. * This is where to push things on the stack to
  574. * boot *BSD systems, e.g.
  575. (*(void(*)(void*, void*, void*, void*, ulong, ulong))(PADDR(entry)))(0, 0, 0, 0, 8196, 640);
  576. * will enable NetBSD boot (the real memory size needs to
  577. * go in the 5th argument).
  578. */
  579. (*(void(*)(void))(PADDR(entry)))();
  580. }