2 * Copyright (C) 2008 Daniel Verkamp <daniel@drv.nu>.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * SYSLINUX COMBOOT (16-bit) image format
35 #include <gpxe/uaccess.h>
36 #include <gpxe/image.h>
37 #include <gpxe/segment.h>
38 #include <gpxe/init.h>
39 #include <gpxe/features.h>
41 FEATURE ( FEATURE_IMAGE, "COMBOOT", DHCP_EB_FEATURE_COMBOOT, 1 );
43 struct image_type comboot_image_type __image_type ( PROBE_NORMAL );
46 * COMBOOT PSP, copied to offset 0 of code segment
49 /** INT 20 instruction, executed if COMBOOT image returns with RET */
51 /** Segment of first non-free paragraph of memory */
52 uint16_t first_non_free_para;
55 /** Offset in PSP of command line */
56 #define COMBOOT_PSP_CMDLINE_OFFSET 0x81
58 /** Maximum length of command line in PSP
59 * (127 bytes minus space and CR) */
60 #define COMBOOT_MAX_CMDLINE_LEN 125
64 * Copy command line to PSP
66 * @v image COMBOOT image
68 static void comboot_copy_cmdline ( struct image * image, userptr_t seg_userptr ) {
69 const char *cmdline = ( image->cmdline ? image->cmdline : "" );
70 int cmdline_len = strlen ( cmdline );
71 if( cmdline_len > COMBOOT_MAX_CMDLINE_LEN )
72 cmdline_len = COMBOOT_MAX_CMDLINE_LEN;
73 uint8_t len_byte = cmdline_len;
74 char spc = ' ', cr = '\r';
76 /* Copy length to byte before command line */
77 copy_to_user ( seg_userptr, COMBOOT_PSP_CMDLINE_OFFSET - 1,
80 /* Command line starts with space */
81 copy_to_user ( seg_userptr,
82 COMBOOT_PSP_CMDLINE_OFFSET,
85 /* Copy command line */
86 copy_to_user ( seg_userptr,
87 COMBOOT_PSP_CMDLINE_OFFSET + 1,
88 cmdline, cmdline_len );
90 /* Command line ends with CR */
91 copy_to_user ( seg_userptr,
92 COMBOOT_PSP_CMDLINE_OFFSET + cmdline_len + 1,
99 * @v image COMBOOT image
100 * @v seg_userptr segment to initialize
102 static void comboot_init_psp ( struct image * image, userptr_t seg_userptr ) {
103 struct comboot_psp psp;
107 /* INT 20h instruction, byte order reversed */
110 /* get_fbms() returns BIOS free base memory counter, which is in
111 * kilobytes; x * 1024 / 16 == x * 64 == x << 6 */
112 psp.first_non_free_para = get_fbms() << 6;
114 DBGC ( image, "COMBOOT %p: first non-free paragraph = 0x%x\n",
115 image, psp.first_non_free_para );
117 /* Copy the PSP to offset 0 of segment.
118 * The rest of the PSP was already zeroed by
119 * comboot_prepare_segment. */
120 copy_to_user ( seg_userptr, 0, &psp, sizeof( psp ) );
122 /* Copy the command line to the PSP */
123 comboot_copy_cmdline ( image, seg_userptr );
127 * Execute COMBOOT image
129 * @v image COMBOOT image
130 * @ret rc Return status code
132 static int comboot_exec ( struct image *image ) {
133 userptr_t seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
136 state = rmsetjmp ( comboot_return );
139 case 0: /* First time through; invoke COMBOOT program */
142 comboot_init_psp ( image, seg_userptr );
144 /* Hook COMBOOT API interrupts */
145 hook_comboot_interrupts();
147 DBGC ( image, "executing 16-bit COMBOOT image at %4x:0100\n",
150 /* Unregister image, so that a "boot" command doesn't
151 * throw us into an execution loop. We never
152 * reregister ourselves; COMBOOT images expect to be
155 unregister_image ( image );
157 /* Store stack segment at 0x38 and stack pointer at 0x3A
158 * in the PSP and jump to the image */
159 __asm__ __volatile__ (
160 REAL_CODE ( /* Save return address with segment on old stack */
164 /* Set DS=ES=segment with image */
167 /* Set SS:SP to new stack (end of image segment) */
173 /* Zero registers (some COM files assume GP regs are 0) */
174 "xorw %%ax, %%ax\n\t"
175 "xorw %%bx, %%bx\n\t"
176 "xorw %%cx, %%cx\n\t"
177 "xorw %%dx, %%dx\n\t"
178 "xorw %%si, %%si\n\t"
179 "xorw %%di, %%di\n\t"
180 "xorw %%bp, %%bp\n\t"
182 : : "r" ( COMBOOT_PSP_SEG ) : "eax" );
183 DBGC ( image, "COMBOOT %p: returned\n", image );
187 DBGC ( image, "COMBOOT %p: exited\n", image );
190 case COMBOOT_EXIT_RUN_KERNEL:
191 DBGC ( image, "COMBOOT %p: exited to run kernel %p\n",
192 image, comboot_replacement_image );
193 image->replacement = comboot_replacement_image;
194 comboot_replacement_image = NULL;
195 image_autoload ( image->replacement );
198 case COMBOOT_EXIT_COMMAND:
199 DBGC ( image, "COMBOOT %p: exited after executing command\n",
208 unhook_comboot_interrupts();
209 comboot_force_text_mode();
215 * Check image name extension
217 * @v image COMBOOT image
218 * @ret rc Return status code
220 static int comboot_identify ( struct image *image ) {
223 ext = strrchr( image->name, '.' );
226 DBGC ( image, "COMBOOT %p: no extension\n",
233 if ( strcasecmp( ext, "com" ) && strcasecmp( ext, "cbt" ) ) {
234 DBGC ( image, "COMBOOT %p: unrecognized extension %s\n",
243 * Load COMBOOT image into memory, preparing a segment and returning it
244 * @v image COMBOOT image
245 * @ret rc Return status code
247 static int comboot_prepare_segment ( struct image *image )
249 userptr_t seg_userptr;
250 size_t filesz, memsz;
253 /* Load image in segment */
254 seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
256 /* Allow etra 0x100 bytes before image for PSP */
257 filesz = image->len + 0x100;
259 /* Ensure the entire 64k segment is free */
262 /* Prepare, verify, and load the real-mode segment */
263 if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
264 DBGC ( image, "COMBOOT %p: could not prepare segment: %s\n",
265 image, strerror ( rc ) );
270 memset_user ( seg_userptr, 0, 0, 0x100 );
272 /* Copy image to segment:0100 */
273 memcpy_user ( seg_userptr, 0x100, image->data, 0, image->len );
279 * Load COMBOOT image into memory
281 * @v image COMBOOT image
282 * @ret rc Return status code
284 static int comboot_load ( struct image *image ) {
287 DBGC ( image, "COMBOOT %p: name '%s'\n",
288 image, image->name );
290 /* Check if this is a COMBOOT image */
291 if ( ( rc = comboot_identify ( image ) ) != 0 ) {
296 /* This is a 16-bit COMBOOT image, valid or otherwise */
298 image->type = &comboot_image_type;
300 /* Sanity check for filesize */
301 if( image->len >= 0xFF00 ) {
302 DBGC( image, "COMBOOT %p: image too large\n",
307 /* Prepare segment and load image */
308 if ( ( rc = comboot_prepare_segment ( image ) ) != 0 ) {
315 /** SYSLINUX COMBOOT (16-bit) image type */
316 struct image_type comboot_image_type __image_type ( PROBE_NORMAL ) = {
318 .load = comboot_load,
319 .exec = comboot_exec,