Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / openbios / libopenbios / forth_load.c
1 /* tag: forth source loader
2  *
3  * Copyright (C) 2004 Stefan Reinauer
4  *
5  * See the file "COPYING" for further information about
6  * the copyright and warranty status of this work.
7  */
8
9 #include "config.h"
10 #include "kernel/kernel.h"
11 #include "libopenbios/bindings.h"
12 #include "libopenbios/sys_info.h"
13 #include "libc/diskio.h"
14 #include "libopenbios/forth_load.h"
15 #define printk printk
16 #define debug printk
17
18 static int fd;
19 static char *forthtext=NULL;
20
21 int is_forth(char *forth)
22 {
23         return (forth[0] == '\\' && forth[1] == ' ');
24 }
25
26 int forth_load(ihandle_t dev)
27 {
28     char magic[2];
29     unsigned long forthsize;
30     int retval = -1;
31
32     /* Mark the saved-program-state as invalid */
33     feval("0 state-valid !");
34
35     fd = open_ih(dev);
36     if (fd == -1) {
37         goto out;
38     }
39
40     if (read_io(fd, magic, 2) != 2) {
41         debug("Can't read magic header\n");
42         retval = LOADER_NOT_SUPPORT;
43         goto out;
44     }
45
46     if (!is_forth(magic)) {
47         debug("No forth source image\n");
48         retval = LOADER_NOT_SUPPORT;
49         goto out;
50     }
51
52     /* Calculate the file size by seeking to the end of the file */
53     seek_io(fd, -1);
54     forthsize = tell(fd);
55     forthtext = malloc(forthsize+1);
56     seek_io(fd, 0);
57
58     printk("Loading forth source ...");
59     if ((size_t)read_io(fd, forthtext, forthsize) != forthsize) {
60         printk("Can't read forth text\n");
61         goto out;
62     }
63     forthtext[forthsize]=0;
64     printk("ok\n");
65
66     // Initialise saved-program-state
67     PUSH((ucell)forthtext);
68     feval("saved-program-state >sps.entry !");
69     PUSH((ucell)forthsize);
70     feval("saved-program-state >sps.file-size !");
71     feval("forth saved-program-state >sps.file-type !");
72
73     feval("-1 state-valid !");
74
75     retval=0;
76
77 out:
78     //if (forthtext)
79     //  free(forthtext);
80     return retval;
81 }
82
83 void 
84 forth_init_program(void)
85 {
86         // Currently not implemented
87         feval("0 state-valid !");
88 }