Until we have a functioning UDP stack and PXE API again, there's not much
[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 extern struct net_device static_single_netdev;
145
146
147 /**************************************************************************
148 MAIN - Kick off routine
149 **************************************************************************/
150 int main ( void ) {
151         struct image *image;
152         void *image_context;
153         int skip = 0;
154
155         /* Call all registered initialisation functions */
156         init_heap();
157         call_init_fns ();
158         probe_devices();
159
160         test_aoeboot ( &static_single_netdev );
161         printf ( "Press any key to exit\n" );
162         getchar();
163
164         remove_devices();
165         call_exit_fns ();
166
167         return exit_status;
168 }
169
170 #if 0
171
172 void exit(int status)
173 {
174         while(!exit_ok)
175                 ;
176         exit_status = status;
177         longjmp(restart_etherboot, 255);
178 }
179
180
181
182 static int main_loop(int state)
183 {
184         /* Splitting main into 2 pieces makes the semantics of 
185          * which variables are preserved across a longjmp clean
186          * and predictable.
187          */
188         static unsigned long order;
189         static unsigned boot_index;
190         static struct dev * dev = 0;
191         static struct class_operations *ops;
192         static int type;
193         static int i;
194
195         if (!initialized) {
196                 initialized = 1;
197                 if (dev && (state >= 1) && (state <= 2)) {
198                         dev->how_probe = PROBE_AWAKE;
199                         dev->how_probe = ops->probe(dev);
200                         if (dev->how_probe == PROBE_FAILED) {
201                                 state = -1;
202                         }
203                         if (state == 1) {
204                                 /* The bootp reply might have been changed, re-parse.  */
205                                 decode_rfc1533(bootp_data.bootp_reply.bp_vend, 0,
206 #ifdef  NO_DHCP_SUPPORT
207                                                BOOTP_VENDOR_LEN + MAX_BOOTP_EXTLEN, 
208 #else
209                                                DHCP_OPT_LEN + MAX_BOOTP_EXTLEN, 
210 #endif  /* NO_DHCP_SUPPORT */
211                                                1);
212                         }
213                 }
214         }
215         switch(state) {
216         case 0:
217         {
218                 static int firsttime = 1;
219                 /* First time through */
220                 if (firsttime) {
221                         cleanup();
222                         firsttime = 0;
223                 } 
224 #ifdef  EXIT_IF_NO_OFFER
225                 else {
226                         cleanup();
227                         exit(0);
228                 }
229 #endif
230                 i = -1;
231                 state = 4;
232                 dev = 0;
233
234                 /* We just called setjmp ... */
235                 order = ask_boot(&boot_index);
236                 try_floppy_first();
237                 break;
238         }
239         case 4:
240                 cleanup();
241                 call_reset_fns();
242                 /* Find a dev entry to probe with */
243                 if (!dev) {
244                         int boot;
245                         int failsafe;
246
247                         /* Advance to the next device type */
248                         i++;
249                         boot = (order >> (i * BOOT_BITS)) & BOOT_MASK;
250                         type = boot & BOOT_TYPE_MASK;
251                         failsafe = (boot & BOOT_FAILSAFE) != 0;
252                         if (i >= MAX_BOOT_ENTRIES) {
253                                 type = BOOT_NOTHING;
254                         }
255                         if ((i == 0) && (type == BOOT_NOTHING)) {
256                                 /* Return to caller */
257                                 exit(0);
258                         }
259                         if (type >= BOOT_NOTHING) {
260                                 interruptible_sleep(2);
261                                 state = 0;
262                                 break;
263                         }
264                         ops = &operations[type];
265                         dev = ops->dev;
266                         dev->how_probe = PROBE_FIRST;
267                         dev->type = type;
268                         dev->failsafe = failsafe;
269                         dev->type_index = 0;
270                 } else {
271                         /* Advance to the next device of the same type */
272                         dev->how_probe = PROBE_NEXT;
273                 }
274                 state = 3;
275                 break;
276         case 3:
277                 state = -1;
278                 /* Removed the following line because it was causing
279                  * heap.o to be dragged in unnecessarily.  It's also
280                  * slightly puzzling: by resetting heap_base, doesn't
281                  * this mean that we permanently leak memory?
282                  */
283                 /* heap_base = allot(0); */
284                 dev->how_probe = ops->probe(dev);
285                 if (dev->how_probe == PROBE_FAILED) {
286                         dev = 0;
287                         state = 4;
288                 } else if (boot_index && (i == 0) && (boot_index != (unsigned)dev->type_index)) {
289                         printf("Wrong index\n");
290                         state = 4;
291                 }
292                 else {
293                         state = 2;
294                 }
295                 break;
296         case 2:
297                 state = -1;
298                 if (ops->load_configuration(dev) >= 0) {
299                         state = 1;
300                 }
301                 break;
302         case 1:
303                 /* Any return from load is a failure */
304                 ops->load(dev);
305                 state = -1;
306                 break;
307         case 256:
308                 state = 0;
309                 break;
310         case -3:
311                 i = MAX_BOOT_ENTRIES;
312                 type = BOOT_NOTHING;
313                 /* fall through */
314         default:
315                 printf("<abort>\n");
316                 state = 4;
317                 /* At the end goto state 0 */
318                 if ((type >= BOOT_NOTHING) || (i >= MAX_BOOT_ENTRIES)) {
319                         state = 0;
320                 }
321                 break;
322         }
323         return state;
324 }
325
326
327 #endif
328
329
330 /**************************************************************************
331 LOADKERNEL - Try to load kernel image
332 **************************************************************************/
333 #if 0
334 /* To be split out into individual files */
335 static const struct proto protos[] = {
336         { "x-tftm", url_tftm },
337         { "x-slam", url_slam },
338         { "nfs", nfs },
339         { "file", url_file },
340         { "tftp", tftp },
341         { "http", http },
342 };
343 #endif
344
345
346 /**************************************************************************
347 CLEANUP - shut down networking and console so that the OS may be called 
348 **************************************************************************/
349 void cleanup(void)
350 {
351         /* Stop receiving packets */
352         disable ( &dev );
353         initialized = 0;
354 }
355
356 /*
357  * Local variables:
358  *  c-basic-offset: 8
359  * End:
360  */