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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. cleanup();
  187. firsttime = 0;
  188. }
  189. #ifdef EXIT_IF_NO_OFFER
  190. else {
  191. cleanup();
  192. exit(0);
  193. }
  194. #endif
  195. i = -1;
  196. state = 4;
  197. dev = 0;
  198. /* We just called setjmp ... */
  199. order = ask_boot(&boot_index);
  200. try_floppy_first();
  201. break;
  202. }
  203. case 4:
  204. cleanup();
  205. call_reset_fns();
  206. /* Find a dev entry to probe with */
  207. if (!dev) {
  208. int boot;
  209. int failsafe;
  210. /* Advance to the next device type */
  211. i++;
  212. boot = (order >> (i * BOOT_BITS)) & BOOT_MASK;
  213. type = boot & BOOT_TYPE_MASK;
  214. failsafe = (boot & BOOT_FAILSAFE) != 0;
  215. if (i >= MAX_BOOT_ENTRIES) {
  216. type = BOOT_NOTHING;
  217. }
  218. if ((i == 0) && (type == BOOT_NOTHING)) {
  219. /* Return to caller */
  220. exit(0);
  221. }
  222. if (type >= BOOT_NOTHING) {
  223. interruptible_sleep(2);
  224. state = 0;
  225. break;
  226. }
  227. ops = &operations[type];
  228. dev = ops->dev;
  229. dev->how_probe = PROBE_FIRST;
  230. dev->type = type;
  231. dev->failsafe = failsafe;
  232. dev->type_index = 0;
  233. } else {
  234. /* Advance to the next device of the same type */
  235. dev->how_probe = PROBE_NEXT;
  236. }
  237. state = 3;
  238. break;
  239. case 3:
  240. state = -1;
  241. /* Removed the following line because it was causing
  242. * heap.o to be dragged in unnecessarily. It's also
  243. * slightly puzzling: by resetting heap_base, doesn't
  244. * this mean that we permanently leak memory?
  245. */
  246. /* heap_base = allot(0); */
  247. dev->how_probe = ops->probe(dev);
  248. if (dev->how_probe == PROBE_FAILED) {
  249. dev = 0;
  250. state = 4;
  251. } else if (boot_index && (i == 0) && (boot_index != (unsigned)dev->type_index)) {
  252. printf("Wrong index\n");
  253. state = 4;
  254. }
  255. else {
  256. state = 2;
  257. }
  258. break;
  259. case 2:
  260. state = -1;
  261. if (ops->load_configuration(dev) >= 0) {
  262. state = 1;
  263. }
  264. break;
  265. case 1:
  266. /* Any return from load is a failure */
  267. ops->load(dev);
  268. state = -1;
  269. break;
  270. case 256:
  271. state = 0;
  272. break;
  273. case -3:
  274. i = MAX_BOOT_ENTRIES;
  275. type = BOOT_NOTHING;
  276. /* fall through */
  277. default:
  278. printf("<abort>\n");
  279. state = 4;
  280. /* At the end goto state 0 */
  281. if ((type >= BOOT_NOTHING) || (i >= MAX_BOOT_ENTRIES)) {
  282. state = 0;
  283. }
  284. break;
  285. }
  286. return state;
  287. }
  288. /**************************************************************************
  289. LOADKERNEL - Try to load kernel image
  290. **************************************************************************/
  291. struct proto {
  292. char *name;
  293. int (*load)(const char *name,
  294. int (*fnc)(unsigned char *, unsigned int, unsigned int, int));
  295. };
  296. static const struct proto protos[] = {
  297. #ifdef DOWNLOAD_PROTO_TFTM
  298. { "x-tftm", url_tftm },
  299. #endif
  300. #ifdef DOWNLOAD_PROTO_SLAM
  301. { "x-slam", url_slam },
  302. #endif
  303. #ifdef DOWNLOAD_PROTO_NFS
  304. { "nfs", nfs },
  305. #endif
  306. #ifdef DOWNLOAD_PROTO_DISK
  307. { "file", url_file },
  308. #endif
  309. #ifdef DOWNLOAD_PROTO_TFTP
  310. { "tftp", tftp },
  311. #endif
  312. #ifdef DOWNLOAD_PROTO_HTTP
  313. { "http", http },
  314. #endif
  315. };
  316. int loadkernel(const char *fname)
  317. {
  318. static const struct proto * const last_proto =
  319. &protos[sizeof(protos)/sizeof(protos[0])];
  320. const struct proto *proto;
  321. in_addr ip;
  322. int len;
  323. const char *name;
  324. #ifdef DNS_RESOLVER
  325. const char *resolvt;
  326. #endif
  327. ip.s_addr = arptable[ARP_SERVER].ipaddr.s_addr;
  328. name = fname;
  329. url_port = -1;
  330. len = 0;
  331. while(fname[len] && fname[len] != ':') {
  332. len++;
  333. }
  334. for(proto = &protos[0]; proto < last_proto; proto++) {
  335. if (memcmp(name, proto->name, len) == 0) {
  336. break;
  337. }
  338. }
  339. if ((proto < last_proto) && (memcmp(fname + len, "://", 3) == 0)) {
  340. name += len + 3;
  341. if (name[0] != '/') {
  342. #ifdef DNS_RESOLVER
  343. resolvt = dns_resolver ( name );
  344. if ( NULL != resolvt ) {
  345. //printf ("Resolved host name [%s] to [%s]\n",
  346. // name, resolvt );
  347. inet_aton(resolvt, &ip);
  348. while ( ( '/' != name[0] ) && ( 0 != name[0]))
  349. ++name;
  350. } else
  351. #endif /* DNS_RESOLVER */
  352. name += inet_aton(name, &ip);
  353. if (name[0] == ':') {
  354. name++;
  355. url_port = strtoul(name, &name, 10);
  356. }
  357. }
  358. if (name[0] == '/') {
  359. arptable[ARP_SERVER].ipaddr.s_addr = ip.s_addr;
  360. printf( "Loading %s ", fname );
  361. return proto->load(name + 1, load_block);
  362. }
  363. }
  364. printf("Loading %@:%s ", arptable[ARP_SERVER].ipaddr, fname);
  365. #ifdef DEFAULT_PROTO_NFS
  366. return nfs(fname, load_block);
  367. #else
  368. return tftp(fname, load_block);
  369. #endif
  370. }
  371. /**************************************************************************
  372. CLEANUP - shut down networking and console so that the OS may be called
  373. **************************************************************************/
  374. void cleanup(void)
  375. {
  376. #ifdef DOWNLOAD_PROTO_NFS
  377. nfs_umountall(ARP_SERVER);
  378. #endif
  379. /* Stop receiving packets */
  380. eth_disable();
  381. disk_disable();
  382. initialized = 0;
  383. }
  384. /*
  385. * Local variables:
  386. * c-basic-offset: 8
  387. * End:
  388. */