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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /**************************************************************************
  128. * initialise() - perform any C-level initialisation
  129. *
  130. * This does not include initialising the NIC, but it does include
  131. * e.g. getting the memory map, relocating to high memory,
  132. * initialising the console, etc.
  133. **************************************************************************
  134. */
  135. void initialise ( void ) {
  136. /* Zero the BSS */
  137. memset ( _bss, 0, _ebss - _bss );
  138. /* Call all registered initialisation functions */
  139. call_init_fns ();
  140. }
  141. /**************************************************************************
  142. MAIN - Kick off routine
  143. **************************************************************************/
  144. int main ( void ) {
  145. int skip = 0;
  146. /* Print out configuration */
  147. print_config();
  148. /*
  149. * Trivial main loop: we need to think about how we want to
  150. * prompt the user etc.
  151. *
  152. */
  153. for ( ; ; disable ( &dev ), call_reset_fns() ) {
  154. /* Get next boot device */
  155. if ( ! find_any_with_driver ( &dev, skip ) ) {
  156. /* Reached end of device list */
  157. printf ( "No more boot devices\n" );
  158. skip = 0;
  159. sleep ( 2 );
  160. continue;
  161. }
  162. /* Skip this device the next time we encounter it */
  163. skip = 1;
  164. /* Print out what we're doing */
  165. printf ( "Booting from %s %s at %s "
  166. "using the %s driver\n",
  167. dev.bus_driver->name ( &dev.bus_dev ),
  168. dev.type_driver->name,
  169. dev.bus_driver->describe ( &dev.bus_dev ),
  170. dev.device_driver->name );
  171. /* Probe boot device */
  172. if ( ! probe ( &dev ) ) {
  173. /* Device found on bus, but probe failed */
  174. printf ( "...probe failed on %s\n" );
  175. continue;
  176. }
  177. printf ( "%s: %s\n", dev.bus_driver->name ( &dev.bus_dev ),
  178. dev.type_driver->describe ( dev.type_dev ) );
  179. }
  180. /* Call registered per-object exit functions */
  181. call_exit_fns ();
  182. return exit_status;
  183. }
  184. void exit(int status)
  185. {
  186. while(!exit_ok)
  187. ;
  188. exit_status = status;
  189. longjmp(restart_etherboot, 255);
  190. }
  191. #if 0
  192. static int main_loop(int state)
  193. {
  194. /* Splitting main into 2 pieces makes the semantics of
  195. * which variables are preserved across a longjmp clean
  196. * and predictable.
  197. */
  198. static unsigned long order;
  199. static unsigned boot_index;
  200. static struct dev * dev = 0;
  201. static struct class_operations *ops;
  202. static int type;
  203. static int i;
  204. if (!initialized) {
  205. initialized = 1;
  206. if (dev && (state >= 1) && (state <= 2)) {
  207. dev->how_probe = PROBE_AWAKE;
  208. dev->how_probe = ops->probe(dev);
  209. if (dev->how_probe == PROBE_FAILED) {
  210. state = -1;
  211. }
  212. }
  213. }
  214. switch(state) {
  215. case 0:
  216. {
  217. static int firsttime = 1;
  218. /* First time through */
  219. if (firsttime) {
  220. cleanup();
  221. firsttime = 0;
  222. }
  223. #ifdef EXIT_IF_NO_OFFER
  224. else {
  225. cleanup();
  226. exit(0);
  227. }
  228. #endif
  229. i = -1;
  230. state = 4;
  231. dev = 0;
  232. /* We just called setjmp ... */
  233. order = ask_boot(&boot_index);
  234. try_floppy_first();
  235. break;
  236. }
  237. case 4:
  238. cleanup();
  239. call_reset_fns();
  240. /* Find a dev entry to probe with */
  241. if (!dev) {
  242. int boot;
  243. int failsafe;
  244. /* Advance to the next device type */
  245. i++;
  246. boot = (order >> (i * BOOT_BITS)) & BOOT_MASK;
  247. type = boot & BOOT_TYPE_MASK;
  248. failsafe = (boot & BOOT_FAILSAFE) != 0;
  249. if (i >= MAX_BOOT_ENTRIES) {
  250. type = BOOT_NOTHING;
  251. }
  252. if ((i == 0) && (type == BOOT_NOTHING)) {
  253. /* Return to caller */
  254. exit(0);
  255. }
  256. if (type >= BOOT_NOTHING) {
  257. interruptible_sleep(2);
  258. state = 0;
  259. break;
  260. }
  261. ops = &operations[type];
  262. dev = ops->dev;
  263. dev->how_probe = PROBE_FIRST;
  264. dev->type = type;
  265. dev->failsafe = failsafe;
  266. dev->type_index = 0;
  267. } else {
  268. /* Advance to the next device of the same type */
  269. dev->how_probe = PROBE_NEXT;
  270. }
  271. state = 3;
  272. break;
  273. case 3:
  274. state = -1;
  275. /* Removed the following line because it was causing
  276. * heap.o to be dragged in unnecessarily. It's also
  277. * slightly puzzling: by resetting heap_base, doesn't
  278. * this mean that we permanently leak memory?
  279. */
  280. /* heap_base = allot(0); */
  281. dev->how_probe = ops->probe(dev);
  282. if (dev->how_probe == PROBE_FAILED) {
  283. dev = 0;
  284. state = 4;
  285. } else if (boot_index && (i == 0) && (boot_index != (unsigned)dev->type_index)) {
  286. printf("Wrong index\n");
  287. state = 4;
  288. }
  289. else {
  290. state = 2;
  291. }
  292. break;
  293. case 2:
  294. state = -1;
  295. if (ops->load_configuration(dev) >= 0) {
  296. state = 1;
  297. }
  298. break;
  299. case 1:
  300. /* Any return from load is a failure */
  301. ops->load(dev);
  302. state = -1;
  303. break;
  304. case 256:
  305. state = 0;
  306. break;
  307. case -3:
  308. i = MAX_BOOT_ENTRIES;
  309. type = BOOT_NOTHING;
  310. /* fall through */
  311. default:
  312. printf("<abort>\n");
  313. state = 4;
  314. /* At the end goto state 0 */
  315. if ((type >= BOOT_NOTHING) || (i >= MAX_BOOT_ENTRIES)) {
  316. state = 0;
  317. }
  318. break;
  319. }
  320. return state;
  321. }
  322. #endif
  323. /**************************************************************************
  324. LOADKERNEL - Try to load kernel image
  325. **************************************************************************/
  326. struct proto {
  327. char *name;
  328. int (*load)(const char *name,
  329. int (*fnc)(unsigned char *, unsigned int, unsigned int, int));
  330. };
  331. static const struct proto protos[] = {
  332. #ifdef DOWNLOAD_PROTO_TFTM
  333. { "x-tftm", url_tftm },
  334. #endif
  335. #ifdef DOWNLOAD_PROTO_SLAM
  336. { "x-slam", url_slam },
  337. #endif
  338. #ifdef DOWNLOAD_PROTO_NFS
  339. { "nfs", nfs },
  340. #endif
  341. #ifdef DOWNLOAD_PROTO_DISK
  342. { "file", url_file },
  343. #endif
  344. #ifdef DOWNLOAD_PROTO_TFTP
  345. { "tftp", tftp },
  346. #endif
  347. #ifdef DOWNLOAD_PROTO_HTTP
  348. { "http", http },
  349. #endif
  350. };
  351. int loadkernel(const char *fname)
  352. {
  353. static const struct proto * const last_proto =
  354. &protos[sizeof(protos)/sizeof(protos[0])];
  355. const struct proto *proto;
  356. in_addr ip;
  357. int len;
  358. const char *name;
  359. #ifdef DNS_RESOLVER
  360. const char *resolvt;
  361. #endif
  362. ip.s_addr = arptable[ARP_SERVER].ipaddr.s_addr;
  363. name = fname;
  364. url_port = -1;
  365. len = 0;
  366. while(fname[len] && fname[len] != ':') {
  367. len++;
  368. }
  369. for(proto = &protos[0]; proto < last_proto; proto++) {
  370. if (memcmp(name, proto->name, len) == 0) {
  371. break;
  372. }
  373. }
  374. if ((proto < last_proto) && (memcmp(fname + len, "://", 3) == 0)) {
  375. name += len + 3;
  376. if (name[0] != '/') {
  377. #ifdef DNS_RESOLVER
  378. resolvt = dns_resolver ( name );
  379. if ( NULL != resolvt ) {
  380. //printf ("Resolved host name [%s] to [%s]\n",
  381. // name, resolvt );
  382. inet_aton(resolvt, &ip);
  383. while ( ( '/' != name[0] ) && ( 0 != name[0]))
  384. ++name;
  385. } else
  386. #endif /* DNS_RESOLVER */
  387. name += inet_aton(name, &ip);
  388. if (name[0] == ':') {
  389. name++;
  390. url_port = strtoul(name, &name, 10);
  391. }
  392. }
  393. if (name[0] == '/') {
  394. arptable[ARP_SERVER].ipaddr.s_addr = ip.s_addr;
  395. printf( "Loading %s ", fname );
  396. return proto->load(name + 1, load_block);
  397. }
  398. }
  399. printf("Loading %@:%s ", arptable[ARP_SERVER].ipaddr, fname);
  400. #ifdef DEFAULT_PROTO_NFS
  401. return nfs(fname, load_block);
  402. #else
  403. return tftp(fname, load_block);
  404. #endif
  405. }
  406. /**************************************************************************
  407. CLEANUP - shut down networking and console so that the OS may be called
  408. **************************************************************************/
  409. void cleanup(void)
  410. {
  411. #ifdef DOWNLOAD_PROTO_NFS
  412. nfs_umountall(ARP_SERVER);
  413. #endif
  414. /* Stop receiving packets */
  415. disable ( &dev );
  416. initialized = 0;
  417. }
  418. /*
  419. * Local variables:
  420. * c-basic-offset: 8
  421. * End:
  422. */