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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #include <gpxe/shell.h>
  30. #include <gpxe/shell_banner.h>
  31. #include <gpxe/autoboot.h>
  32. /* Linker symbols */
  33. extern char _bss[], _ebss[];
  34. jmp_buf restart_etherboot;
  35. int url_port;
  36. char as_main_program = 1;
  37. #if 0
  38. static inline unsigned long ask_boot(unsigned *index)
  39. {
  40. unsigned long order = DEFAULT_BOOT_ORDER;
  41. *index = DEFAULT_BOOT_INDEX;
  42. #ifdef LINUXBIOS
  43. order = get_boot_order(order, index);
  44. #endif
  45. #if defined(ASK_BOOT)
  46. #if ASK_BOOT >= 0
  47. while(1) {
  48. int c = 0;
  49. printf(ASK_PROMPT);
  50. #if ASK_BOOT > 0
  51. {
  52. unsigned long time;
  53. for ( time = currticks() + ASK_BOOT*TICKS_PER_SEC;
  54. !c && !iskey(); ) {
  55. if (currticks() > time) c = ANS_DEFAULT;
  56. }
  57. }
  58. #endif /* ASK_BOOT > 0 */
  59. if ( !c ) c = getchar();
  60. if ((c >= 'a') && (c <= 'z')) c &= 0x5F;
  61. if ((c >= ' ') && (c <= '~')) putchar(c);
  62. putchar('\n');
  63. switch(c) {
  64. default:
  65. /* Nothing useful try again */
  66. continue;
  67. case ANS_QUIT:
  68. order = BOOT_NOTHING;
  69. *index = 0;
  70. break;
  71. case ANS_DEFAULT:
  72. /* Preserve the default boot order */
  73. break;
  74. case ANS_NETWORK:
  75. order = (BOOT_NIC << (0*BOOT_BITS)) |
  76. (BOOT_NOTHING << (1*BOOT_BITS));
  77. *index = 0;
  78. break;
  79. case ANS_DISK:
  80. order = (BOOT_DISK << (0*BOOT_BITS)) |
  81. (BOOT_NOTHING << (1*BOOT_BITS));
  82. *index = 0;
  83. break;
  84. case ANS_FLOPPY:
  85. order = (BOOT_FLOPPY << (0*BOOT_BITS)) |
  86. (BOOT_NOTHING << (1*BOOT_BITS));
  87. *index = 0;
  88. break;
  89. }
  90. break;
  91. }
  92. putchar('\n');
  93. #endif /* ASK_BOOT >= 0 */
  94. #endif /* defined(ASK_BOOT) */
  95. return order;
  96. }
  97. static inline void try_floppy_first(void)
  98. {
  99. #if (TRY_FLOPPY_FIRST > 0)
  100. int i;
  101. printf("Trying floppy");
  102. disk_init();
  103. for (i = TRY_FLOPPY_FIRST; i-- > 0; ) {
  104. putchar('.');
  105. if (pcbios_disk_read(0, 0, 0, 0, ((char *)FLOPPY_BOOT_LOCATION)) != 0x8000) {
  106. printf("using floppy\n");
  107. exit(0);
  108. }
  109. }
  110. printf("no floppy\n");
  111. #endif /* TRY_FLOPPY_FIRST */
  112. }
  113. static struct class_operations {
  114. struct dev *dev;
  115. int (*probe)(struct dev *dev);
  116. int (*load_configuration)(struct dev *dev);
  117. int (*load)(struct dev *dev);
  118. }
  119. operations[] = {
  120. { &nic.dev, eth_probe, eth_load_configuration, eth_load },
  121. { &disk.dev, disk_probe, disk_load_configuration, disk_load },
  122. { &disk.dev, disk_probe, disk_load_configuration, disk_load },
  123. };
  124. #endif
  125. #if 0
  126. static int main_loop(int state);
  127. static int exit_ok;
  128. static int initialized;
  129. #endif
  130. static int exit_status;
  131. /**************************************************************************
  132. MAIN - Kick off routine
  133. **************************************************************************/
  134. int main ( void ) {
  135. /* Call all registered initialisation functions */
  136. init_heap();
  137. call_init_fns ();
  138. probe_devices();
  139. /* Try autobooting if we're not going straight to the shell */
  140. if ( ! shell_banner() ) {
  141. autoboot();
  142. }
  143. /* Autobooting failed or the user wanted the shell */
  144. shell();
  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. #if 0
  316. void cleanup(void)
  317. {
  318. /* Stop receiving packets */
  319. disable ( &dev );
  320. initialized = 0;
  321. }
  322. #endif
  323. /*
  324. * Local variables:
  325. * c-basic-offset: 8
  326. * End:
  327. */