您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

main.c 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. 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. static int main_loop(int state);
  122. static int exit_ok;
  123. static int exit_status;
  124. static int initialized;
  125. /**************************************************************************
  126. MAIN - Kick off routine
  127. **************************************************************************/
  128. int main ( void ) {
  129. int state;
  130. print_config();
  131. cleanup();
  132. /* -1: timeout or ESC
  133. -2: error return from loader
  134. -3: finish the current run.
  135. 0: retry booting bootp and tftp
  136. 1: retry tftp with possibly modified bootp reply
  137. 2: retry bootp and tftp
  138. 3: retry probe bootp and tftp
  139. 4: start with the next device and retry from there...
  140. 255: exit Etherboot
  141. 256: retry after relocation
  142. */
  143. state = setjmp(restart_etherboot);
  144. exit_ok = 1;
  145. for(;state != 255;) {
  146. state = main_loop(state);
  147. }
  148. /* arch_on_exit(exit_status) */
  149. return exit_status;
  150. }
  151. void exit(int status)
  152. {
  153. while(!exit_ok)
  154. ;
  155. exit_status = status;
  156. longjmp(restart_etherboot, 255);
  157. }
  158. static int main_loop(int state)
  159. {
  160. /* Splitting main into 2 pieces makes the semantics of
  161. * which variables are preserved across a longjmp clean
  162. * and predictable.
  163. */
  164. static unsigned long order;
  165. static unsigned boot_index;
  166. static struct dev * dev = 0;
  167. static struct class_operations *ops;
  168. static int type;
  169. static int i;
  170. if (!initialized) {
  171. initialized = 1;
  172. if (dev && (state >= 1) && (state <= 2)) {
  173. dev->how_probe = PROBE_AWAKE;
  174. dev->how_probe = ops->probe(dev);
  175. if (dev->how_probe == PROBE_FAILED) {
  176. state = -1;
  177. }
  178. }
  179. }
  180. switch(state) {
  181. case 0:
  182. {
  183. static int firsttime = 1;
  184. /* First time through */
  185. if (firsttime) {
  186. /* relocate(); */
  187. cleanup();
  188. firsttime = 0;
  189. }
  190. #ifdef EXIT_IF_NO_OFFER
  191. else {
  192. cleanup();
  193. exit(0);
  194. }
  195. #endif
  196. i = -1;
  197. state = 4;
  198. dev = 0;
  199. /* We just called setjmp ... */
  200. order = ask_boot(&boot_index);
  201. try_floppy_first();
  202. break;
  203. }
  204. case 4:
  205. cleanup();
  206. call_reset_fns();
  207. /* Find a dev entry to probe with */
  208. if (!dev) {
  209. int boot;
  210. int failsafe;
  211. /* Advance to the next device type */
  212. i++;
  213. boot = (order >> (i * BOOT_BITS)) & BOOT_MASK;
  214. type = boot & BOOT_TYPE_MASK;
  215. failsafe = (boot & BOOT_FAILSAFE) != 0;
  216. if (i >= MAX_BOOT_ENTRIES) {
  217. type = BOOT_NOTHING;
  218. }
  219. if ((i == 0) && (type == BOOT_NOTHING)) {
  220. /* Return to caller */
  221. exit(0);
  222. }
  223. if (type >= BOOT_NOTHING) {
  224. interruptible_sleep(2);
  225. state = 0;
  226. break;
  227. }
  228. ops = &operations[type];
  229. dev = ops->dev;
  230. dev->how_probe = PROBE_FIRST;
  231. dev->type = type;
  232. dev->failsafe = failsafe;
  233. dev->type_index = 0;
  234. } else {
  235. /* Advance to the next device of the same type */
  236. dev->how_probe = PROBE_NEXT;
  237. }
  238. state = 3;
  239. break;
  240. case 3:
  241. state = -1;
  242. /* Removed the following line because it was causing
  243. * heap.o to be dragged in unnecessarily. It's also
  244. * slightly puzzling: by resetting heap_base, doesn't
  245. * this mean that we permanently leak memory?
  246. */
  247. /* heap_base = allot(0); */
  248. dev->how_probe = ops->probe(dev);
  249. if (dev->how_probe == PROBE_FAILED) {
  250. dev = 0;
  251. state = 4;
  252. } else if (boot_index && (i == 0) && (boot_index != (unsigned)dev->type_index)) {
  253. printf("Wrong index\n");
  254. state = 4;
  255. }
  256. else {
  257. state = 2;
  258. }
  259. break;
  260. case 2:
  261. state = -1;
  262. if (ops->load_configuration(dev) >= 0) {
  263. state = 1;
  264. }
  265. break;
  266. case 1:
  267. /* Any return from load is a failure */
  268. ops->load(dev);
  269. state = -1;
  270. break;
  271. case 256:
  272. state = 0;
  273. break;
  274. case -3:
  275. i = MAX_BOOT_ENTRIES;
  276. type = BOOT_NOTHING;
  277. /* fall through */
  278. default:
  279. printf("<abort>\n");
  280. state = 4;
  281. /* At the end goto state 0 */
  282. if ((type >= BOOT_NOTHING) || (i >= MAX_BOOT_ENTRIES)) {
  283. state = 0;
  284. }
  285. break;
  286. }
  287. return state;
  288. }
  289. /**************************************************************************
  290. LOADKERNEL - Try to load kernel image
  291. **************************************************************************/
  292. struct proto {
  293. char *name;
  294. int (*load)(const char *name,
  295. int (*fnc)(unsigned char *, unsigned int, unsigned int, int));
  296. };
  297. static const struct proto protos[] = {
  298. #ifdef DOWNLOAD_PROTO_TFTM
  299. { "x-tftm", url_tftm },
  300. #endif
  301. #ifdef DOWNLOAD_PROTO_SLAM
  302. { "x-slam", url_slam },
  303. #endif
  304. #ifdef DOWNLOAD_PROTO_NFS
  305. { "nfs", nfs },
  306. #endif
  307. #ifdef DOWNLOAD_PROTO_DISK
  308. { "file", url_file },
  309. #endif
  310. #ifdef DOWNLOAD_PROTO_TFTP
  311. { "tftp", tftp },
  312. #endif
  313. #ifdef DOWNLOAD_PROTO_HTTP
  314. { "http", http },
  315. #endif
  316. };
  317. int loadkernel(const char *fname)
  318. {
  319. static const struct proto * const last_proto =
  320. &protos[sizeof(protos)/sizeof(protos[0])];
  321. const struct proto *proto;
  322. in_addr ip;
  323. int len;
  324. const char *name;
  325. #ifdef DNS_RESOLVER
  326. const char *resolvt;
  327. #endif
  328. ip.s_addr = arptable[ARP_SERVER].ipaddr.s_addr;
  329. name = fname;
  330. url_port = -1;
  331. len = 0;
  332. while(fname[len] && fname[len] != ':') {
  333. len++;
  334. }
  335. for(proto = &protos[0]; proto < last_proto; proto++) {
  336. if (memcmp(name, proto->name, len) == 0) {
  337. break;
  338. }
  339. }
  340. if ((proto < last_proto) && (memcmp(fname + len, "://", 3) == 0)) {
  341. name += len + 3;
  342. if (name[0] != '/') {
  343. #ifdef DNS_RESOLVER
  344. resolvt = dns_resolver ( name );
  345. if ( NULL != resolvt ) {
  346. //printf ("Resolved host name [%s] to [%s]\n",
  347. // name, resolvt );
  348. inet_aton(resolvt, &ip);
  349. while ( ( '/' != name[0] ) && ( 0 != name[0]))
  350. ++name;
  351. } else
  352. #endif /* DNS_RESOLVER */
  353. name += inet_aton(name, &ip);
  354. if (name[0] == ':') {
  355. name++;
  356. url_port = strtoul(name, &name, 10);
  357. }
  358. }
  359. if (name[0] == '/') {
  360. arptable[ARP_SERVER].ipaddr.s_addr = ip.s_addr;
  361. printf( "Loading %s ", fname );
  362. return proto->load(name + 1, load_block);
  363. }
  364. }
  365. printf("Loading %@:%s ", arptable[ARP_SERVER].ipaddr, fname);
  366. #ifdef DEFAULT_PROTO_NFS
  367. return nfs(fname, load_block);
  368. #else
  369. return tftp(fname, load_block);
  370. #endif
  371. }
  372. /**************************************************************************
  373. CLEANUP - shut down networking and console so that the OS may be called
  374. **************************************************************************/
  375. void cleanup(void)
  376. {
  377. #ifdef DOWNLOAD_PROTO_NFS
  378. nfs_umountall(ARP_SERVER);
  379. #endif
  380. /* Stop receiving packets */
  381. eth_disable();
  382. disk_disable();
  383. initialized = 0;
  384. }
  385. /*
  386. * Local variables:
  387. * c-basic-offset: 8
  388. * End:
  389. */