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