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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 "timer.h"
  20. #include "cpu.h"
  21. #include "console.h"
  22. #include <gpxe/init.h>
  23. #include "image.h"
  24. #include <stdarg.h>
  25. #include <gpxe/device.h>
  26. #include <gpxe/heap.h>
  27. #include <gpxe/netdevice.h>
  28. #ifdef CONFIG_FILO
  29. #include <lib.h>
  30. #endif
  31. /* Linker symbols */
  32. extern char _bss[], _ebss[];
  33. jmp_buf restart_etherboot;
  34. int url_port;
  35. char as_main_program = 1;
  36. #if 0
  37. static inline unsigned long ask_boot(unsigned *index)
  38. {
  39. unsigned long order = DEFAULT_BOOT_ORDER;
  40. *index = DEFAULT_BOOT_INDEX;
  41. #ifdef LINUXBIOS
  42. order = get_boot_order(order, index);
  43. #endif
  44. #if defined(ASK_BOOT)
  45. #if ASK_BOOT >= 0
  46. while(1) {
  47. int c = 0;
  48. printf(ASK_PROMPT);
  49. #if ASK_BOOT > 0
  50. {
  51. unsigned long time;
  52. for ( time = currticks() + ASK_BOOT*TICKS_PER_SEC;
  53. !c && !iskey(); ) {
  54. if (currticks() > time) c = ANS_DEFAULT;
  55. }
  56. }
  57. #endif /* ASK_BOOT > 0 */
  58. if ( !c ) c = getchar();
  59. if ((c >= 'a') && (c <= 'z')) c &= 0x5F;
  60. if ((c >= ' ') && (c <= '~')) putchar(c);
  61. putchar('\n');
  62. switch(c) {
  63. default:
  64. /* Nothing useful try again */
  65. continue;
  66. case ANS_QUIT:
  67. order = BOOT_NOTHING;
  68. *index = 0;
  69. break;
  70. case ANS_DEFAULT:
  71. /* Preserve the default boot order */
  72. break;
  73. case ANS_NETWORK:
  74. order = (BOOT_NIC << (0*BOOT_BITS)) |
  75. (BOOT_NOTHING << (1*BOOT_BITS));
  76. *index = 0;
  77. break;
  78. case ANS_DISK:
  79. order = (BOOT_DISK << (0*BOOT_BITS)) |
  80. (BOOT_NOTHING << (1*BOOT_BITS));
  81. *index = 0;
  82. break;
  83. case ANS_FLOPPY:
  84. order = (BOOT_FLOPPY << (0*BOOT_BITS)) |
  85. (BOOT_NOTHING << (1*BOOT_BITS));
  86. *index = 0;
  87. break;
  88. }
  89. break;
  90. }
  91. putchar('\n');
  92. #endif /* ASK_BOOT >= 0 */
  93. #endif /* defined(ASK_BOOT) */
  94. return order;
  95. }
  96. static inline void try_floppy_first(void)
  97. {
  98. #if (TRY_FLOPPY_FIRST > 0)
  99. int i;
  100. printf("Trying floppy");
  101. disk_init();
  102. for (i = TRY_FLOPPY_FIRST; i-- > 0; ) {
  103. putchar('.');
  104. if (pcbios_disk_read(0, 0, 0, 0, ((char *)FLOPPY_BOOT_LOCATION)) != 0x8000) {
  105. printf("using floppy\n");
  106. exit(0);
  107. }
  108. }
  109. printf("no floppy\n");
  110. #endif /* TRY_FLOPPY_FIRST */
  111. }
  112. static struct class_operations {
  113. struct dev *dev;
  114. int (*probe)(struct dev *dev);
  115. int (*load_configuration)(struct dev *dev);
  116. int (*load)(struct dev *dev);
  117. }
  118. operations[] = {
  119. { &nic.dev, eth_probe, eth_load_configuration, eth_load },
  120. { &disk.dev, disk_probe, disk_load_configuration, disk_load },
  121. { &disk.dev, disk_probe, disk_load_configuration, disk_load },
  122. };
  123. #endif
  124. static int main_loop(int state);
  125. static int exit_ok;
  126. static int exit_status;
  127. static int initialized;
  128. /**************************************************************************
  129. MAIN - Kick off routine
  130. **************************************************************************/
  131. int main ( void ) {
  132. struct net_device *netdev;
  133. /* Call all registered initialisation functions */
  134. init_heap();
  135. call_init_fns ();
  136. probe_devices();
  137. netdev = next_netdev ();
  138. if ( netdev ) {
  139. test_aoeboot ( netdev );
  140. } else {
  141. printf ( "No network device found\n" );
  142. }
  143. printf ( "Press any key to exit\n" );
  144. getchar();
  145. remove_devices();
  146. call_exit_fns ();
  147. return exit_status;
  148. }
  149. #if 0
  150. void exit(int status)
  151. {
  152. while(!exit_ok)
  153. ;
  154. exit_status = status;
  155. longjmp(restart_etherboot, 255);
  156. }
  157. static int main_loop(int state)
  158. {
  159. /* Splitting main into 2 pieces makes the semantics of
  160. * which variables are preserved across a longjmp clean
  161. * and predictable.
  162. */
  163. static unsigned long order;
  164. static unsigned boot_index;
  165. static struct dev * dev = 0;
  166. static struct class_operations *ops;
  167. static int type;
  168. static int i;
  169. if (!initialized) {
  170. initialized = 1;
  171. if (dev && (state >= 1) && (state <= 2)) {
  172. dev->how_probe = PROBE_AWAKE;
  173. dev->how_probe = ops->probe(dev);
  174. if (dev->how_probe == PROBE_FAILED) {
  175. state = -1;
  176. }
  177. if (state == 1) {
  178. /* The bootp reply might have been changed, re-parse. */
  179. decode_rfc1533(bootp_data.bootp_reply.bp_vend, 0,
  180. #ifdef NO_DHCP_SUPPORT
  181. BOOTP_VENDOR_LEN + MAX_BOOTP_EXTLEN,
  182. #else
  183. DHCP_OPT_LEN + MAX_BOOTP_EXTLEN,
  184. #endif /* NO_DHCP_SUPPORT */
  185. 1);
  186. }
  187. }
  188. }
  189. switch(state) {
  190. case 0:
  191. {
  192. static int firsttime = 1;
  193. /* First time through */
  194. if (firsttime) {
  195. cleanup();
  196. firsttime = 0;
  197. }
  198. #ifdef EXIT_IF_NO_OFFER
  199. else {
  200. cleanup();
  201. exit(0);
  202. }
  203. #endif
  204. i = -1;
  205. state = 4;
  206. dev = 0;
  207. /* We just called setjmp ... */
  208. order = ask_boot(&boot_index);
  209. try_floppy_first();
  210. break;
  211. }
  212. case 4:
  213. cleanup();
  214. call_reset_fns();
  215. /* Find a dev entry to probe with */
  216. if (!dev) {
  217. int boot;
  218. int failsafe;
  219. /* Advance to the next device type */
  220. i++;
  221. boot = (order >> (i * BOOT_BITS)) & BOOT_MASK;
  222. type = boot & BOOT_TYPE_MASK;
  223. failsafe = (boot & BOOT_FAILSAFE) != 0;
  224. if (i >= MAX_BOOT_ENTRIES) {
  225. type = BOOT_NOTHING;
  226. }
  227. if ((i == 0) && (type == BOOT_NOTHING)) {
  228. /* Return to caller */
  229. exit(0);
  230. }
  231. if (type >= BOOT_NOTHING) {
  232. interruptible_sleep(2);
  233. state = 0;
  234. break;
  235. }
  236. ops = &operations[type];
  237. dev = ops->dev;
  238. dev->how_probe = PROBE_FIRST;
  239. dev->type = type;
  240. dev->failsafe = failsafe;
  241. dev->type_index = 0;
  242. } else {
  243. /* Advance to the next device of the same type */
  244. dev->how_probe = PROBE_NEXT;
  245. }
  246. state = 3;
  247. break;
  248. case 3:
  249. state = -1;
  250. /* Removed the following line because it was causing
  251. * heap.o to be dragged in unnecessarily. It's also
  252. * slightly puzzling: by resetting heap_base, doesn't
  253. * this mean that we permanently leak memory?
  254. */
  255. /* heap_base = allot(0); */
  256. dev->how_probe = ops->probe(dev);
  257. if (dev->how_probe == PROBE_FAILED) {
  258. dev = 0;
  259. state = 4;
  260. } else if (boot_index && (i == 0) && (boot_index != (unsigned)dev->type_index)) {
  261. printf("Wrong index\n");
  262. state = 4;
  263. }
  264. else {
  265. state = 2;
  266. }
  267. break;
  268. case 2:
  269. state = -1;
  270. if (ops->load_configuration(dev) >= 0) {
  271. state = 1;
  272. }
  273. break;
  274. case 1:
  275. /* Any return from load is a failure */
  276. ops->load(dev);
  277. state = -1;
  278. break;
  279. case 256:
  280. state = 0;
  281. break;
  282. case -3:
  283. i = MAX_BOOT_ENTRIES;
  284. type = BOOT_NOTHING;
  285. /* fall through */
  286. default:
  287. printf("<abort>\n");
  288. state = 4;
  289. /* At the end goto state 0 */
  290. if ((type >= BOOT_NOTHING) || (i >= MAX_BOOT_ENTRIES)) {
  291. state = 0;
  292. }
  293. break;
  294. }
  295. return state;
  296. }
  297. #endif
  298. /**************************************************************************
  299. LOADKERNEL - Try to load kernel image
  300. **************************************************************************/
  301. #if 0
  302. /* To be split out into individual files */
  303. static const struct proto protos[] = {
  304. { "x-tftm", url_tftm },
  305. { "x-slam", url_slam },
  306. { "nfs", nfs },
  307. { "file", url_file },
  308. { "tftp", tftp },
  309. { "http", http },
  310. };
  311. #endif
  312. /**************************************************************************
  313. CLEANUP - shut down networking and console so that the OS may be called
  314. **************************************************************************/
  315. void cleanup(void)
  316. {
  317. /* Stop receiving packets */
  318. disable ( &dev );
  319. initialized = 0;
  320. }
  321. /*
  322. * Local variables:
  323. * c-basic-offset: 8
  324. * End:
  325. */