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