Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.c 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 "init.h"
  23. #include <stdarg.h>
  24. #ifdef CONFIG_FILO
  25. #include <lib.h>
  26. #endif
  27. jmp_buf restart_etherboot;
  28. int url_port;
  29. char as_main_program = 1;
  30. #ifdef IMAGE_FREEBSD
  31. int freebsd_howto = 0;
  32. char freebsd_kernel_env[FREEBSD_KERNEL_ENV_SIZE];
  33. #endif
  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. static int main_loop(int state);
  123. static int exit_ok;
  124. static int exit_status;
  125. static int initialized;
  126. /**************************************************************************
  127. * initialise() - perform any C-level initialisation
  128. *
  129. * This does not include initialising the NIC, but it does include
  130. * e.g. getting the memory map, relocating to high memory,
  131. * initialising the console, etc.
  132. **************************************************************************
  133. */
  134. void initialise ( void ) {
  135. /* Zero the BSS */
  136. memset ( _bss, 0, _ebss - _bss );
  137. /* Call all registered initialisation functions */
  138. call_init_fns ();
  139. }
  140. /**************************************************************************
  141. MAIN - Kick off routine
  142. **************************************************************************/
  143. int main ( void ) {
  144. int skip = 0;
  145. /* Print out configuration */
  146. print_config();
  147. /*
  148. * Trivial main loop: we need to think about how we want to
  149. * prompt the user etc.
  150. *
  151. */
  152. for ( ; ; disable ( &dev ), call_reset_fns() ) {
  153. /* Get next boot device */
  154. if ( ! find_any_with_driver ( &dev, skip ) ) {
  155. /* Reached end of device list */
  156. printf ( "No more boot devices\n" );
  157. skip = 0;
  158. sleep ( 2 );
  159. continue;
  160. }
  161. /* Skip this device the next time we encounter it */
  162. skip = 1;
  163. /* Print out device information */
  164. printf ( "%s (%s) %s at %s\n",
  165. dev.bus_driver->name_device ( &dev.bus_dev ),
  166. dev.device_driver->name,
  167. dev.type_driver->name,
  168. dev.bus_driver->describe_device ( &dev.bus_dev ) );
  169. /* Probe boot device */
  170. if ( ! probe ( &dev ) ) {
  171. /* Device found on bus, but probe failed */
  172. printf ( "...probe failed\n" );
  173. continue;
  174. }
  175. /* Print out device information */
  176. printf ( "%s %s has %s\n",
  177. dev.bus_driver->name_device ( &dev.bus_dev ),
  178. dev.type_driver->name,
  179. dev.type_driver->describe_device ( dev.type_dev ) );
  180. /* Configure boot device */
  181. if ( ! configure ( &dev ) ) {
  182. /* Configuration (e.g. DHCP) failed */
  183. printf ( "...configuration failed\n" );
  184. continue;
  185. }
  186. /* Load boot file from the device */
  187. init_buffer ( &load_buffer );
  188. if ( ! load ( &dev, &load_buffer ) ) {
  189. /* Load (e.g. TFTP) failed */
  190. printf ( "...load failed\n" );
  191. continue;
  192. }
  193. /* Boot the loaded image */
  194. if ( ! boot_image ( &load_buffer ) ) {
  195. /* Boot failed (e.g. invalid image) */
  196. printf ( "...boot failed\n" );
  197. continue;
  198. }
  199. /* Image returned */
  200. }
  201. /* Call registered per-object exit functions */
  202. call_exit_fns ();
  203. return exit_status;
  204. }
  205. void exit(int status)
  206. {
  207. while(!exit_ok)
  208. ;
  209. exit_status = status;
  210. longjmp(restart_etherboot, 255);
  211. }
  212. #if 0
  213. static int main_loop(int state)
  214. {
  215. /* Splitting main into 2 pieces makes the semantics of
  216. * which variables are preserved across a longjmp clean
  217. * and predictable.
  218. */
  219. static unsigned long order;
  220. static unsigned boot_index;
  221. static struct dev * dev = 0;
  222. static struct class_operations *ops;
  223. static int type;
  224. static int i;
  225. if (!initialized) {
  226. initialized = 1;
  227. if (dev && (state >= 1) && (state <= 2)) {
  228. dev->how_probe = PROBE_AWAKE;
  229. dev->how_probe = ops->probe(dev);
  230. if (dev->how_probe == PROBE_FAILED) {
  231. state = -1;
  232. }
  233. }
  234. }
  235. switch(state) {
  236. case 0:
  237. {
  238. static int firsttime = 1;
  239. /* First time through */
  240. if (firsttime) {
  241. cleanup();
  242. firsttime = 0;
  243. }
  244. #ifdef EXIT_IF_NO_OFFER
  245. else {
  246. cleanup();
  247. exit(0);
  248. }
  249. #endif
  250. i = -1;
  251. state = 4;
  252. dev = 0;
  253. /* We just called setjmp ... */
  254. order = ask_boot(&boot_index);
  255. try_floppy_first();
  256. break;
  257. }
  258. case 4:
  259. cleanup();
  260. call_reset_fns();
  261. /* Find a dev entry to probe with */
  262. if (!dev) {
  263. int boot;
  264. int failsafe;
  265. /* Advance to the next device type */
  266. i++;
  267. boot = (order >> (i * BOOT_BITS)) & BOOT_MASK;
  268. type = boot & BOOT_TYPE_MASK;
  269. failsafe = (boot & BOOT_FAILSAFE) != 0;
  270. if (i >= MAX_BOOT_ENTRIES) {
  271. type = BOOT_NOTHING;
  272. }
  273. if ((i == 0) && (type == BOOT_NOTHING)) {
  274. /* Return to caller */
  275. exit(0);
  276. }
  277. if (type >= BOOT_NOTHING) {
  278. interruptible_sleep(2);
  279. state = 0;
  280. break;
  281. }
  282. ops = &operations[type];
  283. dev = ops->dev;
  284. dev->how_probe = PROBE_FIRST;
  285. dev->type = type;
  286. dev->failsafe = failsafe;
  287. dev->type_index = 0;
  288. } else {
  289. /* Advance to the next device of the same type */
  290. dev->how_probe = PROBE_NEXT;
  291. }
  292. state = 3;
  293. break;
  294. case 3:
  295. state = -1;
  296. /* Removed the following line because it was causing
  297. * heap.o to be dragged in unnecessarily. It's also
  298. * slightly puzzling: by resetting heap_base, doesn't
  299. * this mean that we permanently leak memory?
  300. */
  301. /* heap_base = allot(0); */
  302. dev->how_probe = ops->probe(dev);
  303. if (dev->how_probe == PROBE_FAILED) {
  304. dev = 0;
  305. state = 4;
  306. } else if (boot_index && (i == 0) && (boot_index != (unsigned)dev->type_index)) {
  307. printf("Wrong index\n");
  308. state = 4;
  309. }
  310. else {
  311. state = 2;
  312. }
  313. break;
  314. case 2:
  315. state = -1;
  316. if (ops->load_configuration(dev) >= 0) {
  317. state = 1;
  318. }
  319. break;
  320. case 1:
  321. /* Any return from load is a failure */
  322. ops->load(dev);
  323. state = -1;
  324. break;
  325. case 256:
  326. state = 0;
  327. break;
  328. case -3:
  329. i = MAX_BOOT_ENTRIES;
  330. type = BOOT_NOTHING;
  331. /* fall through */
  332. default:
  333. printf("<abort>\n");
  334. state = 4;
  335. /* At the end goto state 0 */
  336. if ((type >= BOOT_NOTHING) || (i >= MAX_BOOT_ENTRIES)) {
  337. state = 0;
  338. }
  339. break;
  340. }
  341. return state;
  342. }
  343. #endif
  344. /**************************************************************************
  345. LOADKERNEL - Try to load kernel image
  346. **************************************************************************/
  347. #if 0
  348. /* To be split out into individual files */
  349. static const struct proto protos[] = {
  350. { "x-tftm", url_tftm },
  351. { "x-slam", url_slam },
  352. { "nfs", nfs },
  353. { "file", url_file },
  354. { "tftp", tftp },
  355. { "http", http },
  356. };
  357. #endif
  358. /**************************************************************************
  359. CLEANUP - shut down networking and console so that the OS may be called
  360. **************************************************************************/
  361. void cleanup(void)
  362. {
  363. /* Stop receiving packets */
  364. disable ( &dev );
  365. initialized = 0;
  366. }
  367. /*
  368. * Local variables:
  369. * c-basic-offset: 8
  370. * End:
  371. */