Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / image / comboot.c
1 /*
2  * Copyright (C) 2008 Daniel Verkamp <daniel@drv.nu>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 /**
21  * @file
22  *
23  * SYSLINUX COMBOOT (16-bit) image format
24  *
25  */
26
27 FILE_LICENCE ( GPL2_OR_LATER );
28
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <errno.h>
34 #include <assert.h>
35 #include <realmode.h>
36 #include <basemem.h>
37 #include <comboot.h>
38 #include <ipxe/uaccess.h>
39 #include <ipxe/image.h>
40 #include <ipxe/segment.h>
41 #include <ipxe/init.h>
42 #include <ipxe/features.h>
43
44 FEATURE ( FEATURE_IMAGE, "COMBOOT", DHCP_EB_FEATURE_COMBOOT, 1 );
45
46 /**
47  * COMBOOT PSP, copied to offset 0 of code segment
48  */
49 struct comboot_psp {
50         /** INT 20 instruction, executed if COMBOOT image returns with RET */
51         uint16_t int20;
52         /** Segment of first non-free paragraph of memory */
53         uint16_t first_non_free_para;
54 };
55
56 /** Offset in PSP of command line */
57 #define COMBOOT_PSP_CMDLINE_OFFSET 0x81
58
59 /** Maximum length of command line in PSP
60  * (127 bytes minus space and CR) */
61 #define COMBOOT_MAX_CMDLINE_LEN    125
62
63
64 /**
65  * Copy command line to PSP
66  * 
67  * @v image             COMBOOT image
68  */
69 static void comboot_copy_cmdline ( struct image * image, userptr_t seg_userptr ) {
70         const char *cmdline = ( image->cmdline ? image->cmdline : "" );
71         int cmdline_len = strlen ( cmdline );
72         if( cmdline_len > COMBOOT_MAX_CMDLINE_LEN )
73                 cmdline_len = COMBOOT_MAX_CMDLINE_LEN;
74         uint8_t len_byte = cmdline_len;
75         char spc = ' ', cr = '\r';
76
77         /* Copy length to byte before command line */
78         copy_to_user ( seg_userptr, COMBOOT_PSP_CMDLINE_OFFSET - 1,
79                        &len_byte, 1 );
80
81         /* Command line starts with space */
82         copy_to_user ( seg_userptr,
83                        COMBOOT_PSP_CMDLINE_OFFSET,
84                        &spc, 1 );
85
86         /* Copy command line */
87         copy_to_user ( seg_userptr,
88                        COMBOOT_PSP_CMDLINE_OFFSET + 1,
89                        cmdline, cmdline_len );
90
91         /* Command line ends with CR */
92         copy_to_user ( seg_userptr,
93                        COMBOOT_PSP_CMDLINE_OFFSET + cmdline_len + 1,
94                        &cr, 1 );
95 }
96
97 /**
98  * Initialize PSP
99  * 
100  * @v image             COMBOOT image
101  * @v seg_userptr       segment to initialize
102  */
103 static void comboot_init_psp ( struct image * image, userptr_t seg_userptr ) {
104         struct comboot_psp psp;
105
106         /* Fill PSP */
107
108         /* INT 20h instruction, byte order reversed */
109         psp.int20 = 0x20CD;
110
111         /* get_fbms() returns BIOS free base memory counter, which is in
112          * kilobytes; x * 1024 / 16 == x * 64 == x << 6 */
113         psp.first_non_free_para = get_fbms() << 6;
114
115         DBGC ( image, "COMBOOT %p: first non-free paragraph = 0x%x\n",
116                image, psp.first_non_free_para );
117
118         /* Copy the PSP to offset 0 of segment.
119          * The rest of the PSP was already zeroed by
120          * comboot_prepare_segment. */
121         copy_to_user ( seg_userptr, 0, &psp, sizeof( psp ) );
122
123         /* Copy the command line to the PSP */
124         comboot_copy_cmdline ( image, seg_userptr );
125 }
126
127 /**
128  * Execute COMBOOT image
129  *
130  * @v image             COMBOOT image
131  * @ret rc              Return status code
132  */
133 static int comboot_exec_loop ( struct image *image ) {
134         userptr_t seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
135         int state;
136
137         state = rmsetjmp ( comboot_return );
138
139         switch ( state ) {
140         case 0: /* First time through; invoke COMBOOT program */
141
142                 /* Initialize PSP */
143                 comboot_init_psp ( image, seg_userptr );
144
145                 /* Hook COMBOOT API interrupts */
146                 hook_comboot_interrupts();
147
148                 DBGC ( image, "executing 16-bit COMBOOT image at %4x:0100\n",
149                        COMBOOT_PSP_SEG );
150
151                 /* Unregister image, so that a "boot" command doesn't
152                  * throw us into an execution loop.  We never
153                  * reregister ourselves; COMBOOT images expect to be
154                  * removed on exit.
155                  */
156                 unregister_image ( image );
157
158                 /* Store stack segment at 0x38 and stack pointer at 0x3A
159                  * in the PSP and jump to the image */
160                 __asm__ __volatile__ (
161                     REAL_CODE ( /* Save return address with segment on old stack */
162                                     "popw %%ax\n\t"
163                                     "pushw %%cs\n\t"
164                                     "pushw %%ax\n\t"
165                                     /* Set DS=ES=segment with image */
166                                     "movw %w0, %%ds\n\t"
167                                     "movw %w0, %%es\n\t"
168                                     /* Set SS:SP to new stack (end of image segment) */
169                                     "movw %w0, %%ss\n\t"
170                                     "xor %%sp, %%sp\n\t"
171                                     "pushw $0\n\t"
172                                     "pushw %w0\n\t"
173                                     "pushw $0x100\n\t"
174                                     /* Zero registers (some COM files assume GP regs are 0) */
175                                     "xorw %%ax, %%ax\n\t"
176                                     "xorw %%bx, %%bx\n\t"
177                                     "xorw %%cx, %%cx\n\t"
178                                     "xorw %%dx, %%dx\n\t"
179                                     "xorw %%si, %%si\n\t"
180                                     "xorw %%di, %%di\n\t"
181                                     "xorw %%bp, %%bp\n\t"
182                                     "lret\n\t" )
183                                          : : "r" ( COMBOOT_PSP_SEG ) : "eax" );
184                 DBGC ( image, "COMBOOT %p: returned\n", image );
185                 break;
186
187         case COMBOOT_EXIT:
188                 DBGC ( image, "COMBOOT %p: exited\n", image );
189                 break;
190
191         case COMBOOT_EXIT_RUN_KERNEL:
192                 assert ( image->replacement );
193                 DBGC ( image, "COMBOOT %p: exited to run kernel %s\n",
194                        image, image->replacement->name );
195                 break;
196
197         case COMBOOT_EXIT_COMMAND:
198                 DBGC ( image, "COMBOOT %p: exited after executing command\n",
199                        image );
200                 break;
201
202         default:
203                 assert ( 0 );
204                 break;
205         }
206
207         unhook_comboot_interrupts();
208         comboot_force_text_mode();
209
210         return 0;
211 }
212
213 /**
214  * Check image name extension
215  * 
216  * @v image             COMBOOT image
217  * @ret rc              Return status code
218  */
219 static int comboot_identify ( struct image *image ) {
220         const char *ext;
221
222         ext = strrchr( image->name, '.' );
223
224         if ( ! ext ) {
225                 DBGC ( image, "COMBOOT %p: no extension\n",
226                        image );
227                 return -ENOEXEC;
228         }
229
230         ++ext;
231
232         if ( strcasecmp( ext, "cbt" ) ) {
233                 DBGC ( image, "COMBOOT %p: unrecognized extension %s\n",
234                        image, ext );
235                 return -ENOEXEC;
236         }
237
238         return 0;
239 }
240
241 /**
242  * Load COMBOOT image into memory, preparing a segment and returning it
243  * @v image             COMBOOT image
244  * @ret rc              Return status code
245  */
246 static int comboot_prepare_segment ( struct image *image )
247 {
248         userptr_t seg_userptr;
249         size_t filesz, memsz;
250         int rc;
251
252         /* Load image in segment */
253         seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
254
255         /* Allow etra 0x100 bytes before image for PSP */
256         filesz = image->len + 0x100; 
257
258         /* Ensure the entire 64k segment is free */
259         memsz = 0xFFFF;
260
261         /* Prepare, verify, and load the real-mode segment */
262         if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
263                 DBGC ( image, "COMBOOT %p: could not prepare segment: %s\n",
264                        image, strerror ( rc ) );
265                 return rc;
266         }
267
268         /* Zero PSP */
269         memset_user ( seg_userptr, 0, 0, 0x100 );
270
271         /* Copy image to segment:0100 */
272         memcpy_user ( seg_userptr, 0x100, image->data, 0, image->len );
273
274         return 0;
275 }
276
277 /**
278  * Probe COMBOOT image
279  *
280  * @v image             COMBOOT image
281  * @ret rc              Return status code
282  */
283 static int comboot_probe ( struct image *image ) {
284         int rc;
285
286         DBGC ( image, "COMBOOT %p: name '%s'\n",
287                image, image->name );
288
289         /* Check if this is a COMBOOT image */
290         if ( ( rc = comboot_identify ( image ) ) != 0 ) {
291                 
292                 return rc;
293         }
294
295         return 0;
296 }
297
298 /**
299  * Execute COMBOOT image
300  *
301  * @v image             COMBOOT image
302  * @ret rc              Return status code
303  */
304 static int comboot_exec ( struct image *image ) {
305         int rc;
306         
307         /* Sanity check for filesize */
308         if( image->len >= 0xFF00 ) {
309                 DBGC( image, "COMBOOT %p: image too large\n",
310                       image );
311                 return -ENOEXEC;
312         }
313
314         /* Prepare segment and load image */
315         if ( ( rc = comboot_prepare_segment ( image ) ) != 0 ) {
316                 return rc;
317         }
318
319         return comboot_exec_loop ( image );
320 }
321
322 /** SYSLINUX COMBOOT (16-bit) image type */
323 struct image_type comboot_image_type __image_type ( PROBE_NORMAL ) = {
324         .name = "COMBOOT",
325         .probe = comboot_probe,
326         .exec = comboot_exec,
327 };