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