Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / openbios / libopenbios / fcode_load.c
1 /*
2  * FCode boot loader
3  */
4
5 #include "config.h"
6 #include "kernel/kernel.h"
7 #include "libopenbios/bindings.h"
8 #include "libopenbios/fcode_load.h"
9 #include "libopenbios/sys_info.h"
10 #include "libc/diskio.h"
11 #define printf printk
12 #define debug printk
13
14 static int fd;
15
16 int 
17 is_fcode(unsigned char *fcode)
18 {
19         return (fcode[0] == 0xf0        // start0
20                 || fcode[0] == 0xf1     // start1 
21                 || fcode[0] == 0xf2     // start2
22                 || fcode[0] == 0xf3     // start4
23                 || fcode[0] == 0xfd);   // version1
24 }
25
26 int 
27 fcode_load(ihandle_t dev)
28 {
29     int retval = -1;
30     uint8_t fcode_header[8];
31     unsigned long start, size;
32     unsigned int offset;
33
34     /* Mark the saved-program-state as invalid */
35     feval("0 state-valid !");
36
37     fd = open_ih(dev);
38     if (fd == -1) {
39         goto out;
40     }
41
42     for (offset = 0; offset < 16 * 512; offset += 512) {
43         seek_io(fd, offset);
44         if (read_io(fd, &fcode_header, sizeof(fcode_header))
45             != sizeof(fcode_header)) {
46             debug("Can't read FCode header from ihandle " FMT_ucellx "\n", dev);
47             retval = LOADER_NOT_SUPPORT;
48             goto out;
49         }
50
51         if (is_fcode(fcode_header))
52             goto found;
53     }
54
55     debug("Not a bootable FCode image\n");
56     retval = LOADER_NOT_SUPPORT;
57     goto out;
58
59  found:
60     size = (fcode_header[4] << 24) | (fcode_header[5] << 16) |
61         (fcode_header[6] << 8) | fcode_header[7];
62
63     fword("load-base");
64     start = POP();
65
66     printf("\nLoading FCode image...\n");
67
68     seek_io(fd, offset);
69
70     if ((size_t)read_io(fd, (void *)start, size) != size) {
71         printf("Can't read file (size 0x%lx)\n", size);
72         goto out;
73     }
74
75     debug("Loaded %lu bytes\n", size);
76     debug("entry point is %#lx\n", start);
77     
78     // Initialise saved-program-state
79     PUSH(start);
80     feval("saved-program-state >sps.entry !");
81     PUSH(size);
82     feval("saved-program-state >sps.file-size !");
83     feval("fcode saved-program-state >sps.file-type !");
84
85     feval("-1 state-valid !");
86
87 out:
88     close_io(fd);
89     return retval;
90 }
91
92 void 
93 fcode_init_program(void)
94 {
95         /* If the payload is Fcode then we execute it immediately */
96         ucell address;
97
98         fword("load-base");
99         address = POP();
100
101         if (!is_fcode((unsigned char *)address)) {
102                 debug("Not a valid Fcode memory image\n");
103                 return;
104         }
105
106         PUSH(address);
107         PUSH(1);
108         fword("byte-load");
109 }