s/{name,describe}/{name,describe}_device/
[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 what we're doing */
191                 printf ( "Booting from %s %s at %s "
192                          "using the %s driver\n",
193                          dev.bus_driver->name_device ( &dev.bus_dev ),
194                          dev.type_driver->name,
195                          dev.bus_driver->describe_device ( &dev.bus_dev ),
196                          dev.device_driver->name );
197
198                 /* Probe boot device */
199                 if ( ! probe ( &dev ) ) {
200                         /* Device found on bus, but probe failed */
201                         printf ( "...probe failed on %s\n" );
202                         continue;
203                 }
204                 
205                 printf ( "%s: %s\n",
206                          dev.bus_driver->name_device ( &dev.bus_dev ),
207                          dev.type_driver->describe_device ( dev.type_dev ) );
208         }
209
210         /* Call registered per-object exit functions */
211         call_exit_fns ();
212
213         return exit_status;
214 }
215
216 void exit(int status)
217 {
218         while(!exit_ok)
219                 ;
220         exit_status = status;
221         longjmp(restart_etherboot, 255);
222 }
223
224
225 #if 0
226
227 static int main_loop(int state)
228 {
229         /* Splitting main into 2 pieces makes the semantics of 
230          * which variables are preserved across a longjmp clean
231          * and predictable.
232          */
233         static unsigned long order;
234         static unsigned boot_index;
235         static struct dev * dev = 0;
236         static struct class_operations *ops;
237         static int type;
238         static int i;
239
240         if (!initialized) {
241                 initialized = 1;
242                 if (dev && (state >= 1) && (state <= 2)) {
243                         dev->how_probe = PROBE_AWAKE;
244                         dev->how_probe = ops->probe(dev);
245                         if (dev->how_probe == PROBE_FAILED) {
246                                 state = -1;
247                         }
248                 }
249         }
250         switch(state) {
251         case 0:
252         {
253                 static int firsttime = 1;
254                 /* First time through */
255                 if (firsttime) {
256                         cleanup();
257                         firsttime = 0;
258                 } 
259 #ifdef  EXIT_IF_NO_OFFER
260                 else {
261                         cleanup();
262                         exit(0);
263                 }
264 #endif
265                 i = -1;
266                 state = 4;
267                 dev = 0;
268
269                 /* We just called setjmp ... */
270                 order = ask_boot(&boot_index);
271                 try_floppy_first();
272                 break;
273         }
274         case 4:
275                 cleanup();
276                 call_reset_fns();
277                 /* Find a dev entry to probe with */
278                 if (!dev) {
279                         int boot;
280                         int failsafe;
281
282                         /* Advance to the next device type */
283                         i++;
284                         boot = (order >> (i * BOOT_BITS)) & BOOT_MASK;
285                         type = boot & BOOT_TYPE_MASK;
286                         failsafe = (boot & BOOT_FAILSAFE) != 0;
287                         if (i >= MAX_BOOT_ENTRIES) {
288                                 type = BOOT_NOTHING;
289                         }
290                         if ((i == 0) && (type == BOOT_NOTHING)) {
291                                 /* Return to caller */
292                                 exit(0);
293                         }
294                         if (type >= BOOT_NOTHING) {
295                                 interruptible_sleep(2);
296                                 state = 0;
297                                 break;
298                         }
299                         ops = &operations[type];
300                         dev = ops->dev;
301                         dev->how_probe = PROBE_FIRST;
302                         dev->type = type;
303                         dev->failsafe = failsafe;
304                         dev->type_index = 0;
305                 } else {
306                         /* Advance to the next device of the same type */
307                         dev->how_probe = PROBE_NEXT;
308                 }
309                 state = 3;
310                 break;
311         case 3:
312                 state = -1;
313                 /* Removed the following line because it was causing
314                  * heap.o to be dragged in unnecessarily.  It's also
315                  * slightly puzzling: by resetting heap_base, doesn't
316                  * this mean that we permanently leak memory?
317                  */
318                 /* heap_base = allot(0); */
319                 dev->how_probe = ops->probe(dev);
320                 if (dev->how_probe == PROBE_FAILED) {
321                         dev = 0;
322                         state = 4;
323                 } else if (boot_index && (i == 0) && (boot_index != (unsigned)dev->type_index)) {
324                         printf("Wrong index\n");
325                         state = 4;
326                 }
327                 else {
328                         state = 2;
329                 }
330                 break;
331         case 2:
332                 state = -1;
333                 if (ops->load_configuration(dev) >= 0) {
334                         state = 1;
335                 }
336                 break;
337         case 1:
338                 /* Any return from load is a failure */
339                 ops->load(dev);
340                 state = -1;
341                 break;
342         case 256:
343                 state = 0;
344                 break;
345         case -3:
346                 i = MAX_BOOT_ENTRIES;
347                 type = BOOT_NOTHING;
348                 /* fall through */
349         default:
350                 printf("<abort>\n");
351                 state = 4;
352                 /* At the end goto state 0 */
353                 if ((type >= BOOT_NOTHING) || (i >= MAX_BOOT_ENTRIES)) {
354                         state = 0;
355                 }
356                 break;
357         }
358         return state;
359 }
360
361
362 #endif
363
364
365 /**************************************************************************
366 LOADKERNEL - Try to load kernel image
367 **************************************************************************/
368 struct proto {
369         char *name;
370         int (*load)(const char *name,
371                 int (*fnc)(unsigned char *, unsigned int, unsigned int, int));
372 };
373 static const struct proto protos[] = {
374 #ifdef DOWNLOAD_PROTO_TFTM
375         { "x-tftm", url_tftm },
376 #endif
377 #ifdef DOWNLOAD_PROTO_SLAM
378         { "x-slam", url_slam },
379 #endif
380 #ifdef DOWNLOAD_PROTO_NFS
381         { "nfs", nfs },
382 #endif
383 #ifdef DOWNLOAD_PROTO_DISK
384         { "file", url_file },
385 #endif
386 #ifdef DOWNLOAD_PROTO_TFTP
387         { "tftp", tftp },
388 #endif
389 #ifdef DOWNLOAD_PROTO_HTTP
390         { "http", http },
391 #endif
392 };
393
394 int loadkernel(const char *fname)
395 {
396         static const struct proto * const last_proto = 
397                 &protos[sizeof(protos)/sizeof(protos[0])];
398         const struct proto *proto;
399         in_addr ip;
400         int len;
401         const char *name;
402 #ifdef  DNS_RESOLVER
403         const char *resolvt;
404 #endif
405         ip.s_addr = arptable[ARP_SERVER].ipaddr.s_addr;
406         name = fname;
407         url_port = -1;
408         len = 0;
409         while(fname[len] && fname[len] != ':') {
410                 len++;
411         }
412         for(proto = &protos[0]; proto < last_proto; proto++) {
413                 if (memcmp(name, proto->name, len) == 0) {
414                         break;
415                 }
416         }
417         if ((proto < last_proto) && (memcmp(fname + len, "://", 3) == 0)) {
418                 name += len + 3;
419                 if (name[0] != '/') {
420 #ifdef DNS_RESOLVER
421                         resolvt = dns_resolver ( name );
422                         if ( NULL != resolvt ) {
423                                 //printf ("Resolved host name [%s] to [%s]\n",
424                                 //      name, resolvt );
425                                 inet_aton(resolvt, &ip);
426                                 while ( ( '/' != name[0] ) && ( 0 != name[0]))
427                                         ++name;
428                         } else
429 #endif  /* DNS_RESOLVER */
430                         name += inet_aton(name, &ip);
431                         if (name[0] == ':') {
432                                 name++;
433                                 url_port = strtoul(name, &name, 10);
434                         }
435                 }
436                 if (name[0] == '/') {
437                         arptable[ARP_SERVER].ipaddr.s_addr = ip.s_addr;
438                         printf( "Loading %s ", fname );
439                         return proto->load(name + 1, load_block);
440                 }
441         }
442         printf("Loading %@:%s ", arptable[ARP_SERVER].ipaddr, fname);
443 #ifdef  DEFAULT_PROTO_NFS
444         return nfs(fname, load_block);
445 #else
446         return tftp(fname, load_block);
447 #endif
448 }
449
450
451 /**************************************************************************
452 CLEANUP - shut down networking and console so that the OS may be called 
453 **************************************************************************/
454 void cleanup(void)
455 {
456 #ifdef  DOWNLOAD_PROTO_NFS
457         nfs_umountall(ARP_SERVER);
458 #endif
459         /* Stop receiving packets */
460         disable ( &dev );
461         initialized = 0;
462 }
463
464 /*
465  * Local variables:
466  *  c-basic-offset: 8
467  * End:
468  */