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

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