Added back in the actual call to load().
[people/xl0/gpxe.git] / src / core / main.c
1 /**************************************************************************
2 Etherboot -  Network Bootstrap Program
3
4 Literature dealing with the network protocols:
5         ARP - RFC826
6         RARP - RFC903
7         UDP - RFC768
8         BOOTP - RFC951, RFC2132 (vendor extensions)
9         DHCP - RFC2131, RFC2132 (options)
10         TFTP - RFC1350, RFC2347 (options), RFC2348 (blocksize), RFC2349 (tsize)
11         RPC - RFC1831, RFC1832 (XDR), RFC1833 (rpcbind/portmapper)
12         NFS - RFC1094, RFC1813 (v3, useful for clarifications, not implemented)
13         IGMP - RFC1112
14
15 **************************************************************************/
16
17 /* #define MDEBUG */
18
19 #include "etherboot.h"
20 #include "dev.h"
21 #include "nic.h"
22 #include "disk.h"
23 #include "http.h"
24 #include "timer.h"
25 #include "cpu.h"
26 #include "console.h"
27 #include "init.h"
28 #include <stdarg.h>
29
30 #ifdef CONFIG_FILO
31 #include <lib.h>
32 #endif
33
34 jmp_buf restart_etherboot;
35 int     url_port;               
36
37 char as_main_program = 1;
38
39 #ifdef  IMAGE_FREEBSD
40 int freebsd_howto = 0;
41 char freebsd_kernel_env[FREEBSD_KERNEL_ENV_SIZE];
42 #endif
43
44 #if 0
45
46 static inline unsigned long ask_boot(unsigned *index)
47 {
48         unsigned long order = DEFAULT_BOOT_ORDER;
49         *index = DEFAULT_BOOT_INDEX;
50 #ifdef LINUXBIOS
51         order = get_boot_order(order, index);
52 #endif
53 #if defined(ASK_BOOT)
54 #if ASK_BOOT >= 0
55         while(1) {
56                 int c = 0;
57                 printf(ASK_PROMPT);
58 #if ASK_BOOT > 0
59                 {
60                         unsigned long time;
61                         for ( time = currticks() + ASK_BOOT*TICKS_PER_SEC;
62                               !c && !iskey(); ) {
63                                 if (currticks() > time) c = ANS_DEFAULT;
64                         }
65                 }
66 #endif /* ASK_BOOT > 0 */
67                 if ( !c ) c = getchar();
68                 if ((c >= 'a') && (c <= 'z')) c &= 0x5F;
69                 if ((c >= ' ') && (c <= '~')) putchar(c);
70                 putchar('\n');
71
72                 switch(c) {
73                 default:
74                         /* Nothing useful try again */
75                         continue;
76                 case ANS_QUIT:
77                         order = BOOT_NOTHING;
78                         *index = 0;
79                         break;
80                 case ANS_DEFAULT:
81                         /* Preserve the default boot order */
82                         break;
83                 case ANS_NETWORK:
84                         order = (BOOT_NIC     << (0*BOOT_BITS)) | 
85                                 (BOOT_NOTHING << (1*BOOT_BITS));
86                         *index = 0;
87                         break;
88                 case ANS_DISK:
89                         order = (BOOT_DISK    << (0*BOOT_BITS)) | 
90                                 (BOOT_NOTHING << (1*BOOT_BITS));
91                         *index = 0;
92                         break;
93                 case ANS_FLOPPY:
94                         order = (BOOT_FLOPPY  << (0*BOOT_BITS)) | 
95                                 (BOOT_NOTHING << (1*BOOT_BITS));
96                         *index = 0;
97                         break;
98                 }
99                 break;
100         }
101         putchar('\n');
102 #endif /* ASK_BOOT >= 0 */
103 #endif /* defined(ASK_BOOT) */
104         return order;
105 }
106
107 static inline void try_floppy_first(void)
108 {
109 #if (TRY_FLOPPY_FIRST > 0)
110         int i;
111         printf("Trying floppy");
112         disk_init();
113         for (i = TRY_FLOPPY_FIRST; i-- > 0; ) {
114                 putchar('.');
115                 if (pcbios_disk_read(0, 0, 0, 0, ((char *)FLOPPY_BOOT_LOCATION)) != 0x8000) {
116                         printf("using floppy\n");
117                         exit(0);
118                 }
119         }
120         printf("no floppy\n");
121 #endif /* TRY_FLOPPY_FIRST */   
122 }
123
124 static struct class_operations {
125         struct dev *dev;
126         int (*probe)(struct dev *dev);
127         int (*load_configuration)(struct dev *dev);
128         int (*load)(struct dev *dev);
129 }
130 operations[] = {
131         { &nic.dev,  eth_probe,  eth_load_configuration,  eth_load  },
132         { &disk.dev, disk_probe, disk_load_configuration, disk_load },
133         { &disk.dev, disk_probe, disk_load_configuration, disk_load },
134 };
135
136 #endif
137
138
139
140 static int main_loop(int state);
141 static int exit_ok;
142 static int exit_status;
143 static int initialized;
144
145
146 /**************************************************************************
147  * initialise() - perform any C-level initialisation
148  *
149  * This does not include initialising the NIC, but it does include
150  * e.g. getting the memory map, relocating to high memory,
151  * initialising the console, etc.
152  **************************************************************************
153  */
154 void initialise ( void ) {
155         /* Zero the BSS */
156         memset ( _bss, 0, _ebss - _bss );
157
158         /* Call all registered initialisation functions */
159         call_init_fns ();
160 }
161
162 /**************************************************************************
163 MAIN - Kick off routine
164 **************************************************************************/
165 int main ( void ) {
166         int skip = 0;
167
168         /* Print out configuration */
169         print_config();
170
171         /*
172          * Trivial main loop: we need to think about how we want to
173          * prompt the user etc.
174          *
175          */
176         for ( ; ; disable ( &dev ), call_reset_fns() ) {
177
178                 /* Get next boot device */
179                 if ( ! find_any_with_driver ( &dev, skip ) ) {
180                         /* Reached end of device list */
181                         printf ( "No more boot devices\n" );
182                         skip = 0;
183                         sleep ( 2 );
184                         continue;
185                 }
186
187                 /* Skip this device the next time we encounter it */
188                 skip = 1;
189
190                 /* Print out device information */
191                 printf ( "%s (%s) %s at %s\n",
192                          dev.bus_driver->name_device ( &dev.bus_dev ),
193                          dev.device_driver->name,
194                          dev.type_driver->name,
195                          dev.bus_driver->describe_device ( &dev.bus_dev ) );
196
197                 /* Probe boot device */
198                 if ( ! probe ( &dev ) ) {
199                         /* Device found on bus, but probe failed */
200                         printf ( "...probe failed\n" );
201                         continue;
202                 }
203
204                 /* Print out device information */
205                 printf ( "%s %s has %s\n",
206                          dev.bus_driver->name_device ( &dev.bus_dev ),
207                          dev.type_driver->name,
208                          dev.type_driver->describe_device ( dev.type_dev ) );
209
210                 /* Configure boot device */
211                 if ( ! configure ( &dev ) ) {
212                         /* Configuration (e.g. DHCP) failed */
213                         printf ( "...configuration failed\n" );
214                         continue;
215                 }
216
217                 /* Boot from the device */
218                 load ( &dev, load_block );
219
220         }
221
222         /* Call registered per-object exit functions */
223         call_exit_fns ();
224
225         return exit_status;
226 }
227
228 void exit(int status)
229 {
230         while(!exit_ok)
231                 ;
232         exit_status = status;
233         longjmp(restart_etherboot, 255);
234 }
235
236
237 #if 0
238
239 static int main_loop(int state)
240 {
241         /* Splitting main into 2 pieces makes the semantics of 
242          * which variables are preserved across a longjmp clean
243          * and predictable.
244          */
245         static unsigned long order;
246         static unsigned boot_index;
247         static struct dev * dev = 0;
248         static struct class_operations *ops;
249         static int type;
250         static int i;
251
252         if (!initialized) {
253                 initialized = 1;
254                 if (dev && (state >= 1) && (state <= 2)) {
255                         dev->how_probe = PROBE_AWAKE;
256                         dev->how_probe = ops->probe(dev);
257                         if (dev->how_probe == PROBE_FAILED) {
258                                 state = -1;
259                         }
260                 }
261         }
262         switch(state) {
263         case 0:
264         {
265                 static int firsttime = 1;
266                 /* First time through */
267                 if (firsttime) {
268                         cleanup();
269                         firsttime = 0;
270                 } 
271 #ifdef  EXIT_IF_NO_OFFER
272                 else {
273                         cleanup();
274                         exit(0);
275                 }
276 #endif
277                 i = -1;
278                 state = 4;
279                 dev = 0;
280
281                 /* We just called setjmp ... */
282                 order = ask_boot(&boot_index);
283                 try_floppy_first();
284                 break;
285         }
286         case 4:
287                 cleanup();
288                 call_reset_fns();
289                 /* Find a dev entry to probe with */
290                 if (!dev) {
291                         int boot;
292                         int failsafe;
293
294                         /* Advance to the next device type */
295                         i++;
296                         boot = (order >> (i * BOOT_BITS)) & BOOT_MASK;
297                         type = boot & BOOT_TYPE_MASK;
298                         failsafe = (boot & BOOT_FAILSAFE) != 0;
299                         if (i >= MAX_BOOT_ENTRIES) {
300                                 type = BOOT_NOTHING;
301                         }
302                         if ((i == 0) && (type == BOOT_NOTHING)) {
303                                 /* Return to caller */
304                                 exit(0);
305                         }
306                         if (type >= BOOT_NOTHING) {
307                                 interruptible_sleep(2);
308                                 state = 0;
309                                 break;
310                         }
311                         ops = &operations[type];
312                         dev = ops->dev;
313                         dev->how_probe = PROBE_FIRST;
314                         dev->type = type;
315                         dev->failsafe = failsafe;
316                         dev->type_index = 0;
317                 } else {
318                         /* Advance to the next device of the same type */
319                         dev->how_probe = PROBE_NEXT;
320                 }
321                 state = 3;
322                 break;
323         case 3:
324                 state = -1;
325                 /* Removed the following line because it was causing
326                  * heap.o to be dragged in unnecessarily.  It's also
327                  * slightly puzzling: by resetting heap_base, doesn't
328                  * this mean that we permanently leak memory?
329                  */
330                 /* heap_base = allot(0); */
331                 dev->how_probe = ops->probe(dev);
332                 if (dev->how_probe == PROBE_FAILED) {
333                         dev = 0;
334                         state = 4;
335                 } else if (boot_index && (i == 0) && (boot_index != (unsigned)dev->type_index)) {
336                         printf("Wrong index\n");
337                         state = 4;
338                 }
339                 else {
340                         state = 2;
341                 }
342                 break;
343         case 2:
344                 state = -1;
345                 if (ops->load_configuration(dev) >= 0) {
346                         state = 1;
347                 }
348                 break;
349         case 1:
350                 /* Any return from load is a failure */
351                 ops->load(dev);
352                 state = -1;
353                 break;
354         case 256:
355                 state = 0;
356                 break;
357         case -3:
358                 i = MAX_BOOT_ENTRIES;
359                 type = BOOT_NOTHING;
360                 /* fall through */
361         default:
362                 printf("<abort>\n");
363                 state = 4;
364                 /* At the end goto state 0 */
365                 if ((type >= BOOT_NOTHING) || (i >= MAX_BOOT_ENTRIES)) {
366                         state = 0;
367                 }
368                 break;
369         }
370         return state;
371 }
372
373
374 #endif
375
376
377 /**************************************************************************
378 LOADKERNEL - Try to load kernel image
379 **************************************************************************/
380 struct proto {
381         char *name;
382         int (*load)(const char *name,
383                 int (*fnc)(unsigned char *, unsigned int, unsigned int, int));
384 };
385 static const struct proto protos[] = {
386 #ifdef DOWNLOAD_PROTO_TFTM
387         { "x-tftm", url_tftm },
388 #endif
389 #ifdef DOWNLOAD_PROTO_SLAM
390         { "x-slam", url_slam },
391 #endif
392 #ifdef DOWNLOAD_PROTO_NFS
393         { "nfs", nfs },
394 #endif
395 #ifdef DOWNLOAD_PROTO_DISK
396         { "file", url_file },
397 #endif
398 #ifdef DOWNLOAD_PROTO_TFTP
399         { "tftp", tftp },
400 #endif
401 #ifdef DOWNLOAD_PROTO_HTTP
402         { "http", http },
403 #endif
404 };
405
406 int loadkernel ( const char *fname,
407                  int ( * load_block ) ( unsigned char *data,
408                                         unsigned int blocknum,
409                                         unsigned int len, int eof ) ) {
410         static const struct proto * const last_proto = 
411                 &protos[sizeof(protos)/sizeof(protos[0])];
412         const struct proto *proto;
413         in_addr ip;
414         int len;
415         const char *name;
416 #ifdef  DNS_RESOLVER
417         const char *resolvt;
418 #endif
419         ip.s_addr = arptable[ARP_SERVER].ipaddr.s_addr;
420         name = fname;
421         url_port = -1;
422         len = 0;
423         while(fname[len] && fname[len] != ':') {
424                 len++;
425         }
426         for(proto = &protos[0]; proto < last_proto; proto++) {
427                 if (memcmp(name, proto->name, len) == 0) {
428                         break;
429                 }
430         }
431         if ((proto < last_proto) && (memcmp(fname + len, "://", 3) == 0)) {
432                 name += len + 3;
433                 if (name[0] != '/') {
434 #ifdef DNS_RESOLVER
435                         resolvt = dns_resolver ( name );
436                         if ( NULL != resolvt ) {
437                                 //printf ("Resolved host name [%s] to [%s]\n",
438                                 //      name, resolvt );
439                                 inet_aton(resolvt, &ip);
440                                 while ( ( '/' != name[0] ) && ( 0 != name[0]))
441                                         ++name;
442                         } else
443 #endif  /* DNS_RESOLVER */
444                         name += inet_aton(name, &ip);
445                         if (name[0] == ':') {
446                                 name++;
447                                 url_port = strtoul(name, &name, 10);
448                         }
449                 }
450                 if (name[0] == '/') {
451                         arptable[ARP_SERVER].ipaddr.s_addr = ip.s_addr;
452                         printf( "Loading %s ", fname );
453                         return proto->load(name + 1, load_block);
454                 }
455         }
456         printf("Loading %@:%s ", arptable[ARP_SERVER].ipaddr, fname);
457 #ifdef  DEFAULT_PROTO_NFS
458         return nfs(fname, load_block);
459 #else
460         return tftp(fname, load_block);
461 #endif
462 }
463
464
465 /**************************************************************************
466 CLEANUP - shut down networking and console so that the OS may be called 
467 **************************************************************************/
468 void cleanup(void)
469 {
470 #ifdef  DOWNLOAD_PROTO_NFS
471         nfs_umountall(ARP_SERVER);
472 #endif
473         /* Stop receiving packets */
474         disable ( &dev );
475         initialized = 0;
476 }
477
478 /*
479  * Local variables:
480  *  c-basic-offset: 8
481  * End:
482  */