13aae4cf8a6d2a55580c14be40116bebcb2c2985
[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 "timer.h"
24 #include "cpu.h"
25 #include "console.h"
26 #include <gpxe/init.h>
27 #include "image.h"
28 #include <stdarg.h>
29
30 #ifdef CONFIG_FILO
31 #include <lib.h>
32 #endif
33
34 /* Linker symbols */
35 extern char _bss[], _ebss[];
36
37 jmp_buf restart_etherboot;
38 int     url_port;               
39
40 char as_main_program = 1;
41
42 #if 0
43
44 static inline unsigned long ask_boot(unsigned *index)
45 {
46         unsigned long order = DEFAULT_BOOT_ORDER;
47         *index = DEFAULT_BOOT_INDEX;
48 #ifdef LINUXBIOS
49         order = get_boot_order(order, index);
50 #endif
51 #if defined(ASK_BOOT)
52 #if ASK_BOOT >= 0
53         while(1) {
54                 int c = 0;
55                 printf(ASK_PROMPT);
56 #if ASK_BOOT > 0
57                 {
58                         unsigned long time;
59                         for ( time = currticks() + ASK_BOOT*TICKS_PER_SEC;
60                               !c && !iskey(); ) {
61                                 if (currticks() > time) c = ANS_DEFAULT;
62                         }
63                 }
64 #endif /* ASK_BOOT > 0 */
65                 if ( !c ) c = getchar();
66                 if ((c >= 'a') && (c <= 'z')) c &= 0x5F;
67                 if ((c >= ' ') && (c <= '~')) putchar(c);
68                 putchar('\n');
69
70                 switch(c) {
71                 default:
72                         /* Nothing useful try again */
73                         continue;
74                 case ANS_QUIT:
75                         order = BOOT_NOTHING;
76                         *index = 0;
77                         break;
78                 case ANS_DEFAULT:
79                         /* Preserve the default boot order */
80                         break;
81                 case ANS_NETWORK:
82                         order = (BOOT_NIC     << (0*BOOT_BITS)) | 
83                                 (BOOT_NOTHING << (1*BOOT_BITS));
84                         *index = 0;
85                         break;
86                 case ANS_DISK:
87                         order = (BOOT_DISK    << (0*BOOT_BITS)) | 
88                                 (BOOT_NOTHING << (1*BOOT_BITS));
89                         *index = 0;
90                         break;
91                 case ANS_FLOPPY:
92                         order = (BOOT_FLOPPY  << (0*BOOT_BITS)) | 
93                                 (BOOT_NOTHING << (1*BOOT_BITS));
94                         *index = 0;
95                         break;
96                 }
97                 break;
98         }
99         putchar('\n');
100 #endif /* ASK_BOOT >= 0 */
101 #endif /* defined(ASK_BOOT) */
102         return order;
103 }
104
105 static inline void try_floppy_first(void)
106 {
107 #if (TRY_FLOPPY_FIRST > 0)
108         int i;
109         printf("Trying floppy");
110         disk_init();
111         for (i = TRY_FLOPPY_FIRST; i-- > 0; ) {
112                 putchar('.');
113                 if (pcbios_disk_read(0, 0, 0, 0, ((char *)FLOPPY_BOOT_LOCATION)) != 0x8000) {
114                         printf("using floppy\n");
115                         exit(0);
116                 }
117         }
118         printf("no floppy\n");
119 #endif /* TRY_FLOPPY_FIRST */   
120 }
121
122 static struct class_operations {
123         struct dev *dev;
124         int (*probe)(struct dev *dev);
125         int (*load_configuration)(struct dev *dev);
126         int (*load)(struct dev *dev);
127 }
128 operations[] = {
129         { &nic.dev,  eth_probe,  eth_load_configuration,  eth_load  },
130         { &disk.dev, disk_probe, disk_load_configuration, disk_load },
131         { &disk.dev, disk_probe, disk_load_configuration, disk_load },
132 };
133
134 #endif
135
136
137
138 static int main_loop(int state);
139 static int exit_ok;
140 static int exit_status;
141 static int initialized;
142
143
144
145 /**************************************************************************
146 MAIN - Kick off routine
147 **************************************************************************/
148 int main ( void ) {
149         struct image *image;
150         void *image_context;
151         int skip = 0;
152
153         /* Call all registered initialisation functions */
154         call_init_fns ();
155
156         /* Print out configuration */
157         print_config();
158
159         /*
160          * Trivial main loop: we need to think about how we want to
161          * prompt the user etc.
162          *
163          */
164         for ( ; ; disable ( &dev ), call_reset_fns() ) {
165
166                 /* Get next boot device */
167                 if ( ! find_any_with_driver ( &dev, skip ) ) {
168                         /* Reached end of device list */
169                         printf ( "No more boot devices\n" );
170                         skip = 0;
171                         sleep ( 2 );
172                         continue;
173                 }
174
175                 /* Skip this device the next time we encounter it */
176                 skip = 1;
177
178                 /* Print out device information */
179                 printf ( "%s (%s) %s at %s\n",
180                          dev.bus_driver->name_device ( &dev.bus_dev ),
181                          dev.device_driver->name,
182                          dev.type_driver->name,
183                          dev.bus_driver->describe_device ( &dev.bus_dev ) );
184
185                 /* Probe boot device */
186                 if ( ! probe ( &dev ) ) {
187                         /* Device found on bus, but probe failed */
188                         printf ( "...probe failed: %m\n" );
189                         continue;
190                 }
191
192                 /* Print out device information */
193                 printf ( "%s %s has %s\n",
194                          dev.bus_driver->name_device ( &dev.bus_dev ),
195                          dev.type_driver->name,
196                          dev.type_driver->describe_device ( dev.type_dev ) );
197
198                 /* Configure boot device */
199                 if ( ! configure ( &dev ) ) {
200                         /* Configuration (e.g. DHCP) failed */
201                         printf ( "...configuration failed: %m\n" );
202                         continue;
203                 }
204
205                 /* Load boot file from the device */
206                 if ( ! autoload ( &dev, &image, &image_context ) ) {
207                         /* Load (e.g. TFTP) failed */
208                         printf ( "...load failed: %m\n" );
209                         continue;
210                 }
211
212                 /* Print out image information */
213                 printf ( "\nLoaded %s image\n", image->name );
214
215                 /* Disable devices? */
216                 cleanup();
217                 /* arch_on_exit(0); */
218
219                 /* Boot the image */
220                 if ( ! image->boot ( image_context ) ) {
221                         /* Boot failed */
222                         printf ( "...boot failed: %m\n" );
223                         continue;
224                 }
225                 
226                 /* Image returned */
227                 printf ( "...image returned\n" );
228         }
229
230         /* Call registered per-object exit functions */
231         call_exit_fns ();
232
233         return exit_status;
234 }
235
236 void exit(int status)
237 {
238         while(!exit_ok)
239                 ;
240         exit_status = status;
241         longjmp(restart_etherboot, 255);
242 }
243
244
245 #if 0
246
247 static int main_loop(int state)
248 {
249         /* Splitting main into 2 pieces makes the semantics of 
250          * which variables are preserved across a longjmp clean
251          * and predictable.
252          */
253         static unsigned long order;
254         static unsigned boot_index;
255         static struct dev * dev = 0;
256         static struct class_operations *ops;
257         static int type;
258         static int i;
259
260         if (!initialized) {
261                 initialized = 1;
262                 if (dev && (state >= 1) && (state <= 2)) {
263                         dev->how_probe = PROBE_AWAKE;
264                         dev->how_probe = ops->probe(dev);
265                         if (dev->how_probe == PROBE_FAILED) {
266                                 state = -1;
267                         }
268                         if (state == 1) {
269                                 /* The bootp reply might have been changed, re-parse.  */
270                                 decode_rfc1533(bootp_data.bootp_reply.bp_vend, 0,
271 #ifdef  NO_DHCP_SUPPORT
272                                                BOOTP_VENDOR_LEN + MAX_BOOTP_EXTLEN, 
273 #else
274                                                DHCP_OPT_LEN + MAX_BOOTP_EXTLEN, 
275 #endif  /* NO_DHCP_SUPPORT */
276                                                1);
277                         }
278                 }
279         }
280         switch(state) {
281         case 0:
282         {
283                 static int firsttime = 1;
284                 /* First time through */
285                 if (firsttime) {
286                         cleanup();
287                         firsttime = 0;
288                 } 
289 #ifdef  EXIT_IF_NO_OFFER
290                 else {
291                         cleanup();
292                         exit(0);
293                 }
294 #endif
295                 i = -1;
296                 state = 4;
297                 dev = 0;
298
299                 /* We just called setjmp ... */
300                 order = ask_boot(&boot_index);
301                 try_floppy_first();
302                 break;
303         }
304         case 4:
305                 cleanup();
306                 call_reset_fns();
307                 /* Find a dev entry to probe with */
308                 if (!dev) {
309                         int boot;
310                         int failsafe;
311
312                         /* Advance to the next device type */
313                         i++;
314                         boot = (order >> (i * BOOT_BITS)) & BOOT_MASK;
315                         type = boot & BOOT_TYPE_MASK;
316                         failsafe = (boot & BOOT_FAILSAFE) != 0;
317                         if (i >= MAX_BOOT_ENTRIES) {
318                                 type = BOOT_NOTHING;
319                         }
320                         if ((i == 0) && (type == BOOT_NOTHING)) {
321                                 /* Return to caller */
322                                 exit(0);
323                         }
324                         if (type >= BOOT_NOTHING) {
325                                 interruptible_sleep(2);
326                                 state = 0;
327                                 break;
328                         }
329                         ops = &operations[type];
330                         dev = ops->dev;
331                         dev->how_probe = PROBE_FIRST;
332                         dev->type = type;
333                         dev->failsafe = failsafe;
334                         dev->type_index = 0;
335                 } else {
336                         /* Advance to the next device of the same type */
337                         dev->how_probe = PROBE_NEXT;
338                 }
339                 state = 3;
340                 break;
341         case 3:
342                 state = -1;
343                 /* Removed the following line because it was causing
344                  * heap.o to be dragged in unnecessarily.  It's also
345                  * slightly puzzling: by resetting heap_base, doesn't
346                  * this mean that we permanently leak memory?
347                  */
348                 /* heap_base = allot(0); */
349                 dev->how_probe = ops->probe(dev);
350                 if (dev->how_probe == PROBE_FAILED) {
351                         dev = 0;
352                         state = 4;
353                 } else if (boot_index && (i == 0) && (boot_index != (unsigned)dev->type_index)) {
354                         printf("Wrong index\n");
355                         state = 4;
356                 }
357                 else {
358                         state = 2;
359                 }
360                 break;
361         case 2:
362                 state = -1;
363                 if (ops->load_configuration(dev) >= 0) {
364                         state = 1;
365                 }
366                 break;
367         case 1:
368                 /* Any return from load is a failure */
369                 ops->load(dev);
370                 state = -1;
371                 break;
372         case 256:
373                 state = 0;
374                 break;
375         case -3:
376                 i = MAX_BOOT_ENTRIES;
377                 type = BOOT_NOTHING;
378                 /* fall through */
379         default:
380                 printf("<abort>\n");
381                 state = 4;
382                 /* At the end goto state 0 */
383                 if ((type >= BOOT_NOTHING) || (i >= MAX_BOOT_ENTRIES)) {
384                         state = 0;
385                 }
386                 break;
387         }
388         return state;
389 }
390
391
392 #endif
393
394
395 /**************************************************************************
396 LOADKERNEL - Try to load kernel image
397 **************************************************************************/
398 #if 0
399 /* To be split out into individual files */
400 static const struct proto protos[] = {
401         { "x-tftm", url_tftm },
402         { "x-slam", url_slam },
403         { "nfs", nfs },
404         { "file", url_file },
405         { "tftp", tftp },
406         { "http", http },
407 };
408 #endif
409
410
411 /**************************************************************************
412 CLEANUP - shut down networking and console so that the OS may be called 
413 **************************************************************************/
414 void cleanup(void)
415 {
416         /* Stop receiving packets */
417         disable ( &dev );
418         initialized = 0;
419 }
420
421 /*
422  * Local variables:
423  *  c-basic-offset: 8
424  * End:
425  */