You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /**************************************************************************
  2. Etherboot - Network Bootstrap Program
  3. Literature dealing with the network protocols:
  4. ARP - RFC826
  5. RARP - RFC903
  6. UDP - RFC768
  7. BOOTP - RFC951, RFC2132 (vendor extensions)
  8. DHCP - RFC2131, RFC2132 (options)
  9. TFTP - RFC1350, RFC2347 (options), RFC2348 (blocksize), RFC2349 (tsize)
  10. RPC - RFC1831, RFC1832 (XDR), RFC1833 (rpcbind/portmapper)
  11. NFS - RFC1094, RFC1813 (v3, useful for clarifications, not implemented)
  12. IGMP - RFC1112
  13. **************************************************************************/
  14. /* #define MDEBUG */
  15. #include "etherboot.h"
  16. #include "dev.h"
  17. #include "nic.h"
  18. #include "disk.h"
  19. #include "http.h"
  20. #include "timer.h"
  21. #include "cpu.h"
  22. #include "console.h"
  23. #include "init.h"
  24. #include <stdarg.h>
  25. #ifdef CONFIG_FILO
  26. #include <lib.h>
  27. #endif
  28. jmp_buf restart_etherboot;
  29. int url_port;
  30. char as_main_program = 1;
  31. #ifdef IMAGE_FREEBSD
  32. int freebsd_howto = 0;
  33. char freebsd_kernel_env[FREEBSD_KERNEL_ENV_SIZE];
  34. #endif
  35. #if 0
  36. static inline unsigned long ask_boot(unsigned *index)
  37. {
  38. unsigned long order = DEFAULT_BOOT_ORDER;
  39. *index = DEFAULT_BOOT_INDEX;
  40. #ifdef LINUXBIOS
  41. order = get_boot_order(order, index);
  42. #endif
  43. #if defined(ASK_BOOT)
  44. #if ASK_BOOT >= 0
  45. while(1) {
  46. int c = 0;
  47. printf(ASK_PROMPT);
  48. #if ASK_BOOT > 0
  49. {
  50. unsigned long time;
  51. for ( time = currticks() + ASK_BOOT*TICKS_PER_SEC;
  52. !c && !iskey(); ) {
  53. if (currticks() > time) c = ANS_DEFAULT;
  54. }
  55. }
  56. #endif /* ASK_BOOT > 0 */
  57. if ( !c ) c = getchar();
  58. if ((c >= 'a') && (c <= 'z')) c &= 0x5F;
  59. if ((c >= ' ') && (c <= '~')) putchar(c);
  60. putchar('\n');
  61. switch(c) {
  62. default:
  63. /* Nothing useful try again */
  64. continue;
  65. case ANS_QUIT:
  66. order = BOOT_NOTHING;
  67. *index = 0;
  68. break;
  69. case ANS_DEFAULT:
  70. /* Preserve the default boot order */
  71. break;
  72. case ANS_NETWORK:
  73. order = (BOOT_NIC << (0*BOOT_BITS)) |
  74. (BOOT_NOTHING << (1*BOOT_BITS));
  75. *index = 0;
  76. break;
  77. case ANS_DISK:
  78. order = (BOOT_DISK << (0*BOOT_BITS)) |
  79. (BOOT_NOTHING << (1*BOOT_BITS));
  80. *index = 0;
  81. break;
  82. case ANS_FLOPPY:
  83. order = (BOOT_FLOPPY << (0*BOOT_BITS)) |
  84. (BOOT_NOTHING << (1*BOOT_BITS));
  85. *index = 0;
  86. break;
  87. }
  88. break;
  89. }
  90. putchar('\n');
  91. #endif /* ASK_BOOT >= 0 */
  92. #endif /* defined(ASK_BOOT) */
  93. return order;
  94. }
  95. static inline void try_floppy_first(void)
  96. {
  97. #if (TRY_FLOPPY_FIRST > 0)
  98. int i;
  99. printf("Trying floppy");
  100. disk_init();
  101. for (i = TRY_FLOPPY_FIRST; i-- > 0; ) {
  102. putchar('.');
  103. if (pcbios_disk_read(0, 0, 0, 0, ((char *)FLOPPY_BOOT_LOCATION)) != 0x8000) {
  104. printf("using floppy\n");
  105. exit(0);
  106. }
  107. }
  108. printf("no floppy\n");
  109. #endif /* TRY_FLOPPY_FIRST */
  110. }
  111. static struct class_operations {
  112. struct dev *dev;
  113. int (*probe)(struct dev *dev);
  114. int (*load_configuration)(struct dev *dev);
  115. int (*load)(struct dev *dev);
  116. }
  117. operations[] = {
  118. { &nic.dev, eth_probe, eth_load_configuration, eth_load },
  119. { &disk.dev, disk_probe, disk_load_configuration, disk_load },
  120. { &disk.dev, disk_probe, disk_load_configuration, disk_load },
  121. };
  122. #endif
  123. static int main_loop(int state);
  124. static int exit_ok;
  125. static int exit_status;
  126. static int initialized;
  127. /* Global instance of the current boot device */
  128. struct dev dev;
  129. /**************************************************************************
  130. * initialise() - perform any C-level initialisation
  131. *
  132. * This does not include initialising the NIC, but it does include
  133. * e.g. getting the memory map, relocating to high memory,
  134. * initialising the console, etc.
  135. **************************************************************************
  136. */
  137. void initialise ( void ) {
  138. /* Zero the BSS */
  139. memset ( _bss, 0, _ebss - _bss );
  140. /* Call all registered initialisation functions */
  141. call_init_fns ();
  142. }
  143. /**************************************************************************
  144. MAIN - Kick off routine
  145. **************************************************************************/
  146. int main ( void ) {
  147. /* Print out configuration */
  148. print_config();
  149. /*
  150. * Trivial main loop: we need to think about how we want to
  151. * prompt the user etc.
  152. *
  153. */
  154. for ( ; ; disable ( &dev ), call_reset_fns() ) {
  155. /* Get next boot device */
  156. if ( ! probe ( &dev ) ) {
  157. /* Reached end of device list */
  158. continue;
  159. }
  160. /* Print device info */
  161. print_info ( &dev );
  162. /* Load configuration (e.g. DHCP) */
  163. if ( ! load_configuration ( &dev ) ) {
  164. /* DHCP failed */
  165. continue;
  166. }
  167. /* Load image */
  168. if ( ! load ( &dev ) )
  169. /* Load failed */
  170. continue;
  171. }
  172. /* Call registered per-object exit functions */
  173. call_exit_fns ();
  174. return exit_status;
  175. }
  176. void exit(int status)
  177. {
  178. while(!exit_ok)
  179. ;
  180. exit_status = status;
  181. longjmp(restart_etherboot, 255);
  182. }
  183. /*
  184. * Set PCI device to use.
  185. *
  186. * This routine can be called by e.g. the ROM prefix to specify that
  187. * the first device to be tried should be the device on which the ROM
  188. * was physically located.
  189. *
  190. * Note that this is deliberately in main.c rather than pci.c, because
  191. * this function should generalise to other bus types (e.g. ISAPnP),
  192. * and we don't want to end up dragging in pci.o unnecessarily.
  193. */
  194. void set_pci_device ( uint16_t busdevfn ) {
  195. dev.devid.bus_type = PCI_BUS_TYPE;
  196. dev.pci.busdevfn = busdevfn;
  197. dev.pci.already_tried = 0;
  198. }
  199. #if 0
  200. static int main_loop(int state)
  201. {
  202. /* Splitting main into 2 pieces makes the semantics of
  203. * which variables are preserved across a longjmp clean
  204. * and predictable.
  205. */
  206. static unsigned long order;
  207. static unsigned boot_index;
  208. static struct dev * dev = 0;
  209. static struct class_operations *ops;
  210. static int type;
  211. static int i;
  212. if (!initialized) {
  213. initialized = 1;
  214. if (dev && (state >= 1) && (state <= 2)) {
  215. dev->how_probe = PROBE_AWAKE;
  216. dev->how_probe = ops->probe(dev);
  217. if (dev->how_probe == PROBE_FAILED) {
  218. state = -1;
  219. }
  220. }
  221. }
  222. switch(state) {
  223. case 0:
  224. {
  225. static int firsttime = 1;
  226. /* First time through */
  227. if (firsttime) {
  228. cleanup();
  229. firsttime = 0;
  230. }
  231. #ifdef EXIT_IF_NO_OFFER
  232. else {
  233. cleanup();
  234. exit(0);
  235. }
  236. #endif
  237. i = -1;
  238. state = 4;
  239. dev = 0;
  240. /* We just called setjmp ... */
  241. order = ask_boot(&boot_index);
  242. try_floppy_first();
  243. break;
  244. }
  245. case 4:
  246. cleanup();
  247. call_reset_fns();
  248. /* Find a dev entry to probe with */
  249. if (!dev) {
  250. int boot;
  251. int failsafe;
  252. /* Advance to the next device type */
  253. i++;
  254. boot = (order >> (i * BOOT_BITS)) & BOOT_MASK;
  255. type = boot & BOOT_TYPE_MASK;
  256. failsafe = (boot & BOOT_FAILSAFE) != 0;
  257. if (i >= MAX_BOOT_ENTRIES) {
  258. type = BOOT_NOTHING;
  259. }
  260. if ((i == 0) && (type == BOOT_NOTHING)) {
  261. /* Return to caller */
  262. exit(0);
  263. }
  264. if (type >= BOOT_NOTHING) {
  265. interruptible_sleep(2);
  266. state = 0;
  267. break;
  268. }
  269. ops = &operations[type];
  270. dev = ops->dev;
  271. dev->how_probe = PROBE_FIRST;
  272. dev->type = type;
  273. dev->failsafe = failsafe;
  274. dev->type_index = 0;
  275. } else {
  276. /* Advance to the next device of the same type */
  277. dev->how_probe = PROBE_NEXT;
  278. }
  279. state = 3;
  280. break;
  281. case 3:
  282. state = -1;
  283. /* Removed the following line because it was causing
  284. * heap.o to be dragged in unnecessarily. It's also
  285. * slightly puzzling: by resetting heap_base, doesn't
  286. * this mean that we permanently leak memory?
  287. */
  288. /* heap_base = allot(0); */
  289. dev->how_probe = ops->probe(dev);
  290. if (dev->how_probe == PROBE_FAILED) {
  291. dev = 0;
  292. state = 4;
  293. } else if (boot_index && (i == 0) && (boot_index != (unsigned)dev->type_index)) {
  294. printf("Wrong index\n");
  295. state = 4;
  296. }
  297. else {
  298. state = 2;
  299. }
  300. break;
  301. case 2:
  302. state = -1;
  303. if (ops->load_configuration(dev) >= 0) {
  304. state = 1;
  305. }
  306. break;
  307. case 1:
  308. /* Any return from load is a failure */
  309. ops->load(dev);
  310. state = -1;
  311. break;
  312. case 256:
  313. state = 0;
  314. break;
  315. case -3:
  316. i = MAX_BOOT_ENTRIES;
  317. type = BOOT_NOTHING;
  318. /* fall through */
  319. default:
  320. printf("<abort>\n");
  321. state = 4;
  322. /* At the end goto state 0 */
  323. if ((type >= BOOT_NOTHING) || (i >= MAX_BOOT_ENTRIES)) {
  324. state = 0;
  325. }
  326. break;
  327. }
  328. return state;
  329. }
  330. #endif
  331. /**************************************************************************
  332. LOADKERNEL - Try to load kernel image
  333. **************************************************************************/
  334. struct proto {
  335. char *name;
  336. int (*load)(const char *name,
  337. int (*fnc)(unsigned char *, unsigned int, unsigned int, int));
  338. };
  339. static const struct proto protos[] = {
  340. #ifdef DOWNLOAD_PROTO_TFTM
  341. { "x-tftm", url_tftm },
  342. #endif
  343. #ifdef DOWNLOAD_PROTO_SLAM
  344. { "x-slam", url_slam },
  345. #endif
  346. #ifdef DOWNLOAD_PROTO_NFS
  347. { "nfs", nfs },
  348. #endif
  349. #ifdef DOWNLOAD_PROTO_DISK
  350. { "file", url_file },
  351. #endif
  352. #ifdef DOWNLOAD_PROTO_TFTP
  353. { "tftp", tftp },
  354. #endif
  355. #ifdef DOWNLOAD_PROTO_HTTP
  356. { "http", http },
  357. #endif
  358. };
  359. int loadkernel(const char *fname)
  360. {
  361. static const struct proto * const last_proto =
  362. &protos[sizeof(protos)/sizeof(protos[0])];
  363. const struct proto *proto;
  364. in_addr ip;
  365. int len;
  366. const char *name;
  367. #ifdef DNS_RESOLVER
  368. const char *resolvt;
  369. #endif
  370. ip.s_addr = arptable[ARP_SERVER].ipaddr.s_addr;
  371. name = fname;
  372. url_port = -1;
  373. len = 0;
  374. while(fname[len] && fname[len] != ':') {
  375. len++;
  376. }
  377. for(proto = &protos[0]; proto < last_proto; proto++) {
  378. if (memcmp(name, proto->name, len) == 0) {
  379. break;
  380. }
  381. }
  382. if ((proto < last_proto) && (memcmp(fname + len, "://", 3) == 0)) {
  383. name += len + 3;
  384. if (name[0] != '/') {
  385. #ifdef DNS_RESOLVER
  386. resolvt = dns_resolver ( name );
  387. if ( NULL != resolvt ) {
  388. //printf ("Resolved host name [%s] to [%s]\n",
  389. // name, resolvt );
  390. inet_aton(resolvt, &ip);
  391. while ( ( '/' != name[0] ) && ( 0 != name[0]))
  392. ++name;
  393. } else
  394. #endif /* DNS_RESOLVER */
  395. name += inet_aton(name, &ip);
  396. if (name[0] == ':') {
  397. name++;
  398. url_port = strtoul(name, &name, 10);
  399. }
  400. }
  401. if (name[0] == '/') {
  402. arptable[ARP_SERVER].ipaddr.s_addr = ip.s_addr;
  403. printf( "Loading %s ", fname );
  404. return proto->load(name + 1, load_block);
  405. }
  406. }
  407. printf("Loading %@:%s ", arptable[ARP_SERVER].ipaddr, fname);
  408. #ifdef DEFAULT_PROTO_NFS
  409. return nfs(fname, load_block);
  410. #else
  411. return tftp(fname, load_block);
  412. #endif
  413. }
  414. /**************************************************************************
  415. CLEANUP - shut down networking and console so that the OS may be called
  416. **************************************************************************/
  417. void cleanup(void)
  418. {
  419. #ifdef DOWNLOAD_PROTO_NFS
  420. nfs_umountall(ARP_SERVER);
  421. #endif
  422. /* Stop receiving packets */
  423. eth_disable();
  424. disk_disable();
  425. initialized = 0;
  426. }
  427. /*
  428. * Local variables:
  429. * c-basic-offset: 8
  430. * End:
  431. */