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.
20 * @file SYSLINUX COMBOOT API
34 #include <gpxe/posix_io.h>
35 #include <gpxe/process.h>
36 #include <gpxe/serial.h>
37 #include <gpxe/init.h>
39 /** The "SYSLINUX" version string */
40 static char __data16_array ( syslinux_version, [] ) = "gPXE " VERSION;
41 #define syslinux_version __use_data16 ( syslinux_version )
43 /** The "SYSLINUX" copyright string */
44 static char __data16_array ( syslinux_copyright, [] ) = "http://etherboot.org";
45 #define syslinux_copyright __use_data16 ( syslinux_copyright )
47 static char __data16_array ( syslinux_configuration_file, [] ) = "";
48 #define syslinux_configuration_file __use_data16 ( syslinux_configuration_file )
51 static uint8_t __data16 ( comboot_feature_flags ) = COMBOOT_FEATURE_IDLE_LOOP;
52 #define comboot_feature_flags __use_data16 ( comboot_feature_flags )
54 static struct segoff __text16 ( int20_vector );
55 #define int20_vector __use_text16 ( int20_vector )
57 static struct segoff __text16 ( int21_vector );
58 #define int21_vector __use_text16 ( int21_vector )
60 static struct segoff __text16 ( int22_vector );
61 #define int22_vector __use_text16 ( int22_vector )
63 extern void int20_wrapper ( void );
64 extern void int21_wrapper ( void );
65 extern void int22_wrapper ( void );
67 /* setjmp/longjmp context buffer used to return after loading an image */
68 jmp_buf comboot_return;
70 /* Command line to execute when returning via comboot_return
71 * with COMBOOT_RETURN_RUN_KERNEL
73 char *comboot_kernel_cmdline;
75 /* Mode flags set by INT 22h AX=0017h */
76 static uint16_t comboot_graphics_mode = 0;
80 * Print a string with a particular terminator
82 static void print_user_string ( unsigned int segment, unsigned int offset, char terminator ) {
85 userptr_t str = real_to_user ( segment, offset );
87 copy_from_user ( &c, str, i, 1 );
88 if ( c == terminator ) break;
96 * Perform a series of memory copies from a list in low memory
98 static void shuffle ( unsigned int list_segment, unsigned int list_offset, unsigned int count )
100 comboot_shuffle_descriptor shuf[COMBOOT_MAX_SHUFFLE_DESCRIPTORS];
103 /* Copy shuffle descriptor list so it doesn't get overwritten */
104 copy_from_user ( shuf, real_to_user ( list_segment, list_offset ), 0,
105 count * sizeof( comboot_shuffle_descriptor ) );
108 for ( i = 0; i < count; i++ ) {
109 userptr_t src_u = phys_to_user ( shuf[ i ].src );
110 userptr_t dest_u = phys_to_user ( shuf[ i ].dest );
112 if ( shuf[ i ].src == 0xFFFFFFFF ) {
113 /* Fill with 0 instead of copying */
114 memset_user ( dest_u, 0, 0, shuf[ i ].len );
115 } else if ( shuf[ i ].dest == 0xFFFFFFFF ) {
116 /* Copy new list of descriptors */
117 count = shuf[ i ].len / sizeof( comboot_shuffle_descriptor );
118 assert ( count <= COMBOOT_MAX_SHUFFLE_DESCRIPTORS );
119 copy_from_user ( shuf, src_u, 0, shuf[ i ].len );
123 memmove_user ( dest_u, 0, src_u, 0, shuf[ i ].len );
130 * Set default text mode
132 void comboot_force_text_mode ( void ) {
133 if ( comboot_graphics_mode & COMBOOT_VIDEO_VESA ) {
134 /* Set VGA mode 3 via VESA VBE mode set */
135 __asm__ __volatile__ (
137 "mov $0x4F02, %%ax\n\t"
138 "mov $0x03, %%bx\n\t"
142 } else if ( comboot_graphics_mode & COMBOOT_VIDEO_GRAPHICS ) {
143 /* Set VGA mode 3 via standard VGA mode set */
144 __asm__ __volatile__ (
146 "mov $0x03, %%ax\n\t"
152 comboot_graphics_mode = 0;
157 * Run the kernel specified in comboot_kernel_cmdline
159 void comboot_run_kernel ( )
163 comboot_force_text_mode ( );
165 DBG ( "COMBOOT: executing image '%s'\n", comboot_kernel_cmdline );
167 /* Find initrd= parameter, if any */
168 if ( ( initrd = strstr ( comboot_kernel_cmdline, "initrd=" ) ) ) {
169 char old_char = '\0';
170 char *initrd_end = strchr( initrd, ' ' );
172 /* Replace space after end of parameter
173 * with a nul terminator if this is not
177 old_char = *initrd_end;
181 /* Replace = with space to get 'initrd filename'
182 * command suitable for system()
186 DBG( "COMBOOT: loading initrd '%s'\n", initrd );
190 /* Restore space after parameter */
192 *initrd_end = old_char;
200 DBG ( "COMBOOT: loading kernel '%s'\n", comboot_kernel_cmdline );
201 system ( comboot_kernel_cmdline );
203 free ( comboot_kernel_cmdline );
208 DBG ( "COMBOOT: back from executing command\n" );
213 * Terminate program interrupt handler
215 static __cdecl void int20 ( struct i386_all_regs *ix86 __unused ) {
216 longjmp ( comboot_return, COMBOOT_RETURN_EXIT );
223 static __cdecl void int21 ( struct i386_all_regs *ix86 ) {
226 switch ( ix86->regs.ah ) {
228 case 0x4C: /* Terminate program */
229 longjmp ( comboot_return, COMBOOT_RETURN_EXIT );
232 case 0x01: /* Get Key with Echo */
233 case 0x08: /* Get Key without Echo */
234 /* TODO: handle extended characters? */
235 ix86->regs.al = getchar( );
238 if ( ix86->regs.al == 0x0A )
239 ix86->regs.al = 0x0D;
241 if ( ix86->regs.ah == 0x01 )
242 putchar ( ix86->regs.al );
247 case 0x02: /* Write Character */
248 putchar ( ix86->regs.dl );
252 case 0x04: /* Write Character to Serial Port */
253 serial_putc ( ix86->regs.dl );
257 case 0x09: /* Write DOS String to Console */
258 print_user_string ( ix86->segs.ds, ix86->regs.dx, '$' );
262 case 0x0B: /* Check Keyboard */
264 ix86->regs.al = 0xFF;
266 ix86->regs.al = 0x00;
271 case 0x30: /* Check DOS Version */
272 /* Bottom halves all 0; top halves spell "SYSLINUX" */
273 ix86->regs.eax = 0x59530000;
274 ix86->regs.ebx = 0x4C530000;
275 ix86->regs.ecx = 0x4E490000;
276 ix86->regs.edx = 0x58550000;
281 DBG ( "COMBOOT unknown int21 function %02x\n", ix86->regs.ah );
290 static __cdecl void int22 ( struct i386_all_regs *ix86 ) {
293 switch ( ix86->regs.ax ) {
294 case 0x0001: /* Get Version */
296 /* Number of INT 22h API functions available */
297 ix86->regs.ax = 0x0018;
299 /* SYSLINUX version number */
300 ix86->regs.ch = 0; /* major */
301 ix86->regs.cl = 0; /* minor */
303 /* SYSLINUX derivative ID */
304 ix86->regs.dl = BZI_LOADER_TYPE_GPXE;
306 /* SYSLINUX version and copyright strings */
307 ix86->segs.es = rm_ds;
308 ix86->regs.si = ( ( unsigned ) __from_data16 ( syslinux_version ) );
309 ix86->regs.di = ( ( unsigned ) __from_data16 ( syslinux_copyright ) );
314 case 0x0002: /* Write String */
315 print_user_string ( ix86->segs.es, ix86->regs.bx, '\0' );
319 case 0x0003: /* Run command */
321 userptr_t cmd_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
322 int len = strlen_user ( cmd_u, 0 );
324 copy_from_user ( cmd, cmd_u, 0, len + 1 );
325 DBG ( "COMBOOT: executing command '%s'\n", cmd );
327 comboot_kernel_cmdline = strdup ( cmd );
329 DBG ( "COMBOOT: returning to run image...\n" );
330 longjmp ( comboot_return, COMBOOT_RETURN_RUN_KERNEL );
334 case 0x0004: /* Run default command */
335 /* FIXME: just exit for now */
336 longjmp ( comboot_return, COMBOOT_RETURN_EXIT );
339 case 0x0005: /* Force text mode */
340 comboot_force_text_mode ( );
344 case 0x0006: /* Open file */
347 userptr_t file_u = real_to_user ( ix86->segs.es, ix86->regs.si );
348 int len = strlen_user ( file_u, 0 );
351 copy_from_user ( file, file_u, 0, len + 1 );
353 if ( file[0] == '\0' ) {
354 DBG ( "COMBOOT: attempted open with empty file name\n" );
358 DBG ( "COMBOOT: opening file '%s'\n", file );
363 DBG ( "COMBOOT: error opening file %s\n", file );
367 /* This relies on the fact that a gPXE POSIX fd will
368 * always fit in 16 bits.
370 #if (POSIX_FD_MAX > 65535)
371 #error POSIX_FD_MAX too large
373 ix86->regs.si = (uint16_t) fd;
375 ix86->regs.cx = COMBOOT_FILE_BLOCKSZ;
376 ix86->regs.eax = fsize ( fd );
381 case 0x0007: /* Read file */
383 int fd = ix86->regs.si;
384 int len = ix86->regs.cx * COMBOOT_FILE_BLOCKSZ;
387 userptr_t buf = real_to_user ( ix86->segs.es, ix86->regs.bx );
389 /* Wait for data ready to read */
395 rc = read_user ( fd, buf, 0, len );
397 DBG ( "COMBOOT: read failed\n" );
407 case 0x0008: /* Close file */
409 int fd = ix86->regs.si;
415 case 0x0009: /* Call PXE Stack */
416 pxe_api_call ( ix86 );
420 case 0x000A: /* Get Derivative-Specific Information */
422 /* gPXE has its own derivative ID, so there is no defined
423 * output here; just return AL for now */
424 ix86->regs.al = BZI_LOADER_TYPE_GPXE;
428 case 0x000B: /* Get Serial Console Configuration */
434 case 0x000E: /* Get configuration file name */
436 ix86->segs.es = rm_ds;
437 ix86->regs.bx = ( ( unsigned ) __from_data16 ( syslinux_configuration_file ) );
441 case 0x000F: /* Get IPAPPEND strings */
449 case 0x0010: /* Resolve hostname */
451 userptr_t hostname_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
452 int len = strlen_user ( hostname_u, 0 );
456 copy_from_user ( hostname, hostname_u, 0, len + 1 );
459 * "If the hostname does not contain a dot (.), the
460 * local domain name is automatically appended."
463 comboot_resolv ( hostname, &addr );
465 ix86->regs.eax = addr.s_addr;
470 case 0x0011: /* Maximum number of shuffle descriptors */
471 ix86->regs.cx = COMBOOT_MAX_SHUFFLE_DESCRIPTORS;
475 case 0x0012: /* Cleanup, shuffle and boot */
476 if ( ix86->regs.cx > COMBOOT_MAX_SHUFFLE_DESCRIPTORS )
479 /* Perform final cleanup */
480 shutdown ( SHUTDOWN_BOOT );
482 /* Perform sequence of copies */
483 shuffle ( ix86->segs.es, ix86->regs.di, ix86->regs.cx );
485 /* Jump to real-mode entry point */
486 __asm__ __volatile__ (
494 : "r" ( ix86->segs.ds ),
495 "r" ( ix86->regs.ebp ),
496 "d" ( ix86->regs.ebx ),
497 "S" ( ix86->regs.esi ) );
499 assert ( 0 ); /* Execution should never reach this point */
503 case 0x0013: /* Idle loop call */
508 case 0x0015: /* Get feature flags */
509 ix86->segs.es = rm_ds;
510 ix86->regs.bx = ( ( unsigned ) __from_data16 ( &comboot_feature_flags ) );
511 ix86->regs.cx = 1; /* Number of feature flag bytes */
515 case 0x0016: /* Run kernel image */
517 userptr_t file_u = real_to_user ( ix86->segs.ds, ix86->regs.si );
518 userptr_t cmd_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
519 int file_len = strlen_user ( file_u, 0 );
520 int cmd_len = strlen_user ( cmd_u, 0 );
521 char file[file_len + 1 + cmd_len + 7 + 1];
522 char cmd[cmd_len + 1];
524 memcpy( file, "kernel ", 7 );
525 copy_from_user ( file + 7, file_u, 0, file_len + 1 );
526 copy_from_user ( cmd, cmd_u, 0, cmd_len + 1 );
527 strcat ( file, " " );
528 strcat ( file, cmd );
530 DBG ( "COMBOOT: run kernel image '%s'\n", file );
532 comboot_kernel_cmdline = strdup ( file );
534 DBG ( "COMBOOT: returning to run image...\n" );
535 longjmp ( comboot_return, COMBOOT_RETURN_RUN_KERNEL );
539 case 0x0017: /* Report video mode change */
540 comboot_graphics_mode = ix86->regs.bx;
544 case 0x0018: /* Query custom font */
553 DBG ( "COMBOOT unknown int22 function %04x\n", ix86->regs.ax );
559 * Hook BIOS interrupts related to COMBOOT API (INT 20h, 21h, 22h)
561 void hook_comboot_interrupts ( ) {
563 __asm__ __volatile__ (
564 TEXT16_CODE ( "\nint20_wrapper:\n\t"
572 hook_bios_interrupt ( 0x20, ( unsigned int ) int20_wrapper,
575 __asm__ __volatile__ (
576 TEXT16_CODE ( "\nint21_wrapper:\n\t"
584 hook_bios_interrupt ( 0x21, ( unsigned int ) int21_wrapper,
587 __asm__ __volatile__ (
588 TEXT16_CODE ( "\nint22_wrapper:\n\t"
596 hook_bios_interrupt ( 0x22, ( unsigned int ) int22_wrapper,