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