Protocols now load data into a buffer; they don't execute it.
[people/lynusvaz/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 "init.h"
27 #include <stdarg.h>
28
29 #ifdef CONFIG_FILO
30 #include <lib.h>
31 #endif
32
33 jmp_buf restart_etherboot;
34 int     url_port;               
35
36 char as_main_program = 1;
37
38 #ifdef  IMAGE_FREEBSD
39 int freebsd_howto = 0;
40 char freebsd_kernel_env[FREEBSD_KERNEL_ENV_SIZE];
41 #endif
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  * initialise() - perform any C-level initialisation
147  *
148  * This does not include initialising the NIC, but it does include
149  * e.g. getting the memory map, relocating to high memory,
150  * initialising the console, etc.
151  **************************************************************************
152  */
153 void initialise ( void ) {
154         /* Zero the BSS */
155         memset ( _bss, 0, _ebss - _bss );
156
157         /* Call all registered initialisation functions */
158         call_init_fns ();
159 }
160
161 /**************************************************************************
162 MAIN - Kick off routine
163 **************************************************************************/
164 int main ( void ) {
165         struct buffer buffer;
166         int skip = 0;
167
168         /* Print out configuration */
169         print_config();
170
171         /*
172          * Trivial main loop: we need to think about how we want to
173          * prompt the user etc.
174          *
175          */
176         for ( ; ; disable ( &dev ), call_reset_fns() ) {
177
178                 /* Get next boot device */
179                 if ( ! find_any_with_driver ( &dev, skip ) ) {
180                         /* Reached end of device list */
181                         printf ( "No more boot devices\n" );
182                         skip = 0;
183                         sleep ( 2 );
184                         continue;
185                 }
186
187                 /* Skip this device the next time we encounter it */
188                 skip = 1;
189
190                 /* Print out device information */
191                 printf ( "%s (%s) %s at %s\n",
192                          dev.bus_driver->name_device ( &dev.bus_dev ),
193                          dev.device_driver->name,
194                          dev.type_driver->name,
195                          dev.bus_driver->describe_device ( &dev.bus_dev ) );
196
197                 /* Probe boot device */
198                 if ( ! probe ( &dev ) ) {
199                         /* Device found on bus, but probe failed */
200                         printf ( "...probe failed\n" );
201                         continue;
202                 }
203
204                 /* Print out device information */
205                 printf ( "%s %s has %s\n",
206                          dev.bus_driver->name_device ( &dev.bus_dev ),
207                          dev.type_driver->name,
208                          dev.type_driver->describe_device ( dev.type_dev ) );
209
210                 /* Configure boot device */
211                 if ( ! configure ( &dev ) ) {
212                         /* Configuration (e.g. DHCP) failed */
213                         printf ( "...configuration failed\n" );
214                         continue;
215                 }
216
217                 /* Load boot file from the device */
218                 init_buffer ( &buffer, 0x7c00, 0x100 );
219                 if ( ! load ( &dev, &buffer ) ) {
220                         /* Load (e.g. TFTP failed) */
221                         printf ( "...load failed\n" );
222                         continue;
223                 }
224
225                 printf ( "Loaded file of size %d\n", buffer.fill );
226
227         }
228
229         /* Call registered per-object exit functions */
230         call_exit_fns ();
231
232         return exit_status;
233 }
234
235 void exit(int status)
236 {
237         while(!exit_ok)
238                 ;
239         exit_status = status;
240         longjmp(restart_etherboot, 255);
241 }
242
243
244 #if 0
245
246 static int main_loop(int state)
247 {
248         /* Splitting main into 2 pieces makes the semantics of 
249          * which variables are preserved across a longjmp clean
250          * and predictable.
251          */
252         static unsigned long order;
253         static unsigned boot_index;
254         static struct dev * dev = 0;
255         static struct class_operations *ops;
256         static int type;
257         static int i;
258
259         if (!initialized) {
260                 initialized = 1;
261                 if (dev && (state >= 1) && (state <= 2)) {
262                         dev->how_probe = PROBE_AWAKE;
263                         dev->how_probe = ops->probe(dev);
264                         if (dev->how_probe == PROBE_FAILED) {
265                                 state = -1;
266                         }
267                 }
268         }
269         switch(state) {
270         case 0:
271         {
272                 static int firsttime = 1;
273                 /* First time through */
274                 if (firsttime) {
275                         cleanup();
276                         firsttime = 0;
277                 } 
278 #ifdef  EXIT_IF_NO_OFFER
279                 else {
280                         cleanup();
281                         exit(0);
282                 }
283 #endif
284                 i = -1;
285                 state = 4;
286                 dev = 0;
287
288                 /* We just called setjmp ... */
289                 order = ask_boot(&boot_index);
290                 try_floppy_first();
291                 break;
292         }
293         case 4:
294                 cleanup();
295                 call_reset_fns();
296                 /* Find a dev entry to probe with */
297                 if (!dev) {
298                         int boot;
299                         int failsafe;
300
301                         /* Advance to the next device type */
302                         i++;
303                         boot = (order >> (i * BOOT_BITS)) & BOOT_MASK;
304                         type = boot & BOOT_TYPE_MASK;
305                         failsafe = (boot & BOOT_FAILSAFE) != 0;
306                         if (i >= MAX_BOOT_ENTRIES) {
307                                 type = BOOT_NOTHING;
308                         }
309                         if ((i == 0) && (type == BOOT_NOTHING)) {
310                                 /* Return to caller */
311                                 exit(0);
312                         }
313                         if (type >= BOOT_NOTHING) {
314                                 interruptible_sleep(2);
315                                 state = 0;
316                                 break;
317                         }
318                         ops = &operations[type];
319                         dev = ops->dev;
320                         dev->how_probe = PROBE_FIRST;
321                         dev->type = type;
322                         dev->failsafe = failsafe;
323                         dev->type_index = 0;
324                 } else {
325                         /* Advance to the next device of the same type */
326                         dev->how_probe = PROBE_NEXT;
327                 }
328                 state = 3;
329                 break;
330         case 3:
331                 state = -1;
332                 /* Removed the following line because it was causing
333                  * heap.o to be dragged in unnecessarily.  It's also
334                  * slightly puzzling: by resetting heap_base, doesn't
335                  * this mean that we permanently leak memory?
336                  */
337                 /* heap_base = allot(0); */
338                 dev->how_probe = ops->probe(dev);
339                 if (dev->how_probe == PROBE_FAILED) {
340                         dev = 0;
341                         state = 4;
342                 } else if (boot_index && (i == 0) && (boot_index != (unsigned)dev->type_index)) {
343                         printf("Wrong index\n");
344                         state = 4;
345                 }
346                 else {
347                         state = 2;
348                 }
349                 break;
350         case 2:
351                 state = -1;
352                 if (ops->load_configuration(dev) >= 0) {
353                         state = 1;
354                 }
355                 break;
356         case 1:
357                 /* Any return from load is a failure */
358                 ops->load(dev);
359                 state = -1;
360                 break;
361         case 256:
362                 state = 0;
363                 break;
364         case -3:
365                 i = MAX_BOOT_ENTRIES;
366                 type = BOOT_NOTHING;
367                 /* fall through */
368         default:
369                 printf("<abort>\n");
370                 state = 4;
371                 /* At the end goto state 0 */
372                 if ((type >= BOOT_NOTHING) || (i >= MAX_BOOT_ENTRIES)) {
373                         state = 0;
374                 }
375                 break;
376         }
377         return state;
378 }
379
380
381 #endif
382
383
384 /**************************************************************************
385 LOADKERNEL - Try to load kernel image
386 **************************************************************************/
387 #if 0
388 /* To be split out into individual files */
389 static const struct proto protos[] = {
390         { "x-tftm", url_tftm },
391         { "x-slam", url_slam },
392         { "nfs", nfs },
393         { "file", url_file },
394         { "tftp", tftp },
395         { "http", http },
396 };
397 #endif
398
399
400 /**************************************************************************
401 CLEANUP - shut down networking and console so that the OS may be called 
402 **************************************************************************/
403 void cleanup(void)
404 {
405         /* Stop receiving packets */
406         disable ( &dev );
407         initialized = 0;
408 }
409
410 /*
411  * Local variables:
412  *  c-basic-offset: 8
413  * End:
414  */