iPXE
Data Structures | Defines | Functions
comboot.c File Reference

SYSLINUX COMBOOT (16-bit) image format. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <assert.h>
#include <realmode.h>
#include <basemem.h>
#include <comboot.h>
#include <ipxe/uaccess.h>
#include <ipxe/image.h>
#include <ipxe/segment.h>
#include <ipxe/init.h>
#include <ipxe/features.h>
#include <ipxe/console.h>

Go to the source code of this file.

Data Structures

struct  comboot_psp
 COMBOOT PSP, copied to offset 0 of code segment. More...

Defines

#define COMBOOT_PSP_CMDLINE_OFFSET   0x81
 Offset in PSP of command line.
#define COMBOOT_MAX_CMDLINE_LEN   125
 Maximum length of command line in PSP (127 bytes minus space and CR)

Functions

 FILE_LICENCE (GPL2_OR_LATER)
 FEATURE (FEATURE_IMAGE,"COMBOOT", DHCP_EB_FEATURE_COMBOOT, 1)
static void comboot_copy_cmdline (struct image *image, userptr_t seg_userptr)
 Copy command line to PSP.
static void comboot_init_psp (struct image *image, userptr_t seg_userptr)
 Initialize PSP.
static int comboot_exec_loop (struct image *image)
 Execute COMBOOT image.
static int comboot_identify (struct image *image)
 Check image name extension.
static int comboot_prepare_segment (struct image *image)
 Load COMBOOT image into memory, preparing a segment and returning it.
static int comboot_probe (struct image *image)
 Probe COMBOOT image.
static int comboot_exec (struct image *image)
 Execute COMBOOT image.
struct image_type
comboot_image_type 
__image_type (PROBE_NORMAL)
 SYSLINUX COMBOOT (16-bit) image type.

Detailed Description

SYSLINUX COMBOOT (16-bit) image format.

Definition in file comboot.c.


Define Documentation

#define COMBOOT_PSP_CMDLINE_OFFSET   0x81

Offset in PSP of command line.

Definition at line 58 of file comboot.c.

Referenced by comboot_copy_cmdline().

#define COMBOOT_MAX_CMDLINE_LEN   125

Maximum length of command line in PSP (127 bytes minus space and CR)

Definition at line 62 of file comboot.c.

Referenced by comboot_copy_cmdline().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER  )
FEATURE ( FEATURE_IMAGE  ,
"COMBOOT"  ,
DHCP_EB_FEATURE_COMBOOT  ,
 
)
static void comboot_copy_cmdline ( struct image image,
userptr_t  seg_userptr 
) [static]

Copy command line to PSP.

Parameters:
imageCOMBOOT image

Definition at line 70 of file comboot.c.

References image::cmdline, cmdline, COMBOOT_MAX_CMDLINE_LEN, COMBOOT_PSP_CMDLINE_OFFSET, copy_to_user(), cr, and strlen().

Referenced by comboot_init_psp().

                                                                                 {
        const char *cmdline = ( image->cmdline ? image->cmdline : "" );
        int cmdline_len = strlen ( cmdline );
        if( cmdline_len > COMBOOT_MAX_CMDLINE_LEN )
                cmdline_len = COMBOOT_MAX_CMDLINE_LEN;
        uint8_t len_byte = cmdline_len;
        char spc = ' ', cr = '\r';

        /* Copy length to byte before command line */
        copy_to_user ( seg_userptr, COMBOOT_PSP_CMDLINE_OFFSET - 1,
                       &len_byte, 1 );

        /* Command line starts with space */
        copy_to_user ( seg_userptr,
                       COMBOOT_PSP_CMDLINE_OFFSET,
                       &spc, 1 );

        /* Copy command line */
        copy_to_user ( seg_userptr,
                       COMBOOT_PSP_CMDLINE_OFFSET + 1,
                       cmdline, cmdline_len );

        /* Command line ends with CR */
        copy_to_user ( seg_userptr,
                       COMBOOT_PSP_CMDLINE_OFFSET + cmdline_len + 1,
                       &cr, 1 );
}
static void comboot_init_psp ( struct image image,
userptr_t  seg_userptr 
) [static]

Initialize PSP.

Parameters:
imageCOMBOOT image
seg_userptrsegment to initialize

Definition at line 104 of file comboot.c.

References comboot_copy_cmdline(), copy_to_user(), DBGC, comboot_psp::first_non_free_para, get_fbms(), and comboot_psp::int20.

Referenced by comboot_exec_loop().

                                                                             {
        struct comboot_psp psp;

        /* Fill PSP */

        /* INT 20h instruction, byte order reversed */
        psp.int20 = 0x20CD;

        /* get_fbms() returns BIOS free base memory counter, which is in
         * kilobytes; x * 1024 / 16 == x * 64 == x << 6 */
        psp.first_non_free_para = get_fbms() << 6;

        DBGC ( image, "COMBOOT %p: first non-free paragraph = 0x%x\n",
               image, psp.first_non_free_para );

        /* Copy the PSP to offset 0 of segment.
         * The rest of the PSP was already zeroed by
         * comboot_prepare_segment. */
        copy_to_user ( seg_userptr, 0, &psp, sizeof( psp ) );

        /* Copy the command line to the PSP */
        comboot_copy_cmdline ( image, seg_userptr );
}
static int comboot_exec_loop ( struct image image) [static]

Execute COMBOOT image.

Parameters:
imageCOMBOOT image
Return values:
rcReturn status code

Definition at line 134 of file comboot.c.

References __asm__(), assert, COMBOOT_EXIT, COMBOOT_EXIT_COMMAND, COMBOOT_EXIT_RUN_KERNEL, comboot_force_text_mode(), comboot_init_psp(), COMBOOT_PSP_SEG, comboot_return, DBGC, hook_comboot_interrupts(), image::name, REAL_CODE, real_to_user(), image::replacement, rmsetjmp, state, unhook_comboot_interrupts(), and unregister_image().

Referenced by comboot_exec().

                                                     {
        userptr_t seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
        int state;

        state = rmsetjmp ( comboot_return );

        switch ( state ) {
        case 0: /* First time through; invoke COMBOOT program */

                /* Initialize PSP */
                comboot_init_psp ( image, seg_userptr );

                /* Hook COMBOOT API interrupts */
                hook_comboot_interrupts();

                DBGC ( image, "executing 16-bit COMBOOT image at %4x:0100\n",
                       COMBOOT_PSP_SEG );

                /* Unregister image, so that a "boot" command doesn't
                 * throw us into an execution loop.  We never
                 * reregister ourselves; COMBOOT images expect to be
                 * removed on exit.
                 */
                unregister_image ( image );

                /* Store stack segment at 0x38 and stack pointer at 0x3A
                 * in the PSP and jump to the image */
                __asm__ __volatile__ (
                    REAL_CODE ( /* Save return address with segment on old stack */
                                    "popw %%ax\n\t"
                                    "pushw %%cs\n\t"
                                    "pushw %%ax\n\t"
                                    /* Set DS=ES=segment with image */
                                    "movw %w0, %%ds\n\t"
                                    "movw %w0, %%es\n\t"
                                    /* Set SS:SP to new stack (end of image segment) */
                                    "movw %w0, %%ss\n\t"
                                    "xor %%sp, %%sp\n\t"
                                    "pushw $0\n\t"
                                    "pushw %w0\n\t"
                                    "pushw $0x100\n\t"
                                    /* Zero registers (some COM files assume GP regs are 0) */
                                    "xorw %%ax, %%ax\n\t"
                                    "xorw %%bx, %%bx\n\t"
                                    "xorw %%cx, %%cx\n\t"
                                    "xorw %%dx, %%dx\n\t"
                                    "xorw %%si, %%si\n\t"
                                    "xorw %%di, %%di\n\t"
                                    "xorw %%bp, %%bp\n\t"
                                    "lret\n\t" )
                                         : : "r" ( COMBOOT_PSP_SEG ) : "eax" );
                DBGC ( image, "COMBOOT %p: returned\n", image );
                break;

        case COMBOOT_EXIT:
                DBGC ( image, "COMBOOT %p: exited\n", image );
                break;

        case COMBOOT_EXIT_RUN_KERNEL:
                assert ( image->replacement );
                DBGC ( image, "COMBOOT %p: exited to run kernel %s\n",
                       image, image->replacement->name );
                break;

        case COMBOOT_EXIT_COMMAND:
                DBGC ( image, "COMBOOT %p: exited after executing command\n",
                       image );
                break;

        default:
                assert ( 0 );
                break;
        }

        unhook_comboot_interrupts();
        comboot_force_text_mode();

        return 0;
}
static int comboot_identify ( struct image image) [static]

Check image name extension.

Parameters:
imageCOMBOOT image
Return values:
rcReturn status code

Definition at line 220 of file comboot.c.

References DBGC, ENOEXEC, ext, image::name, strcasecmp(), and strrchr().

Referenced by comboot_probe().

                                                    {
        const char *ext;

        ext = strrchr( image->name, '.' );

        if ( ! ext ) {
                DBGC ( image, "COMBOOT %p: no extension\n",
                       image );
                return -ENOEXEC;
        }

        ++ext;

        if ( strcasecmp( ext, "cbt" ) ) {
                DBGC ( image, "COMBOOT %p: unrecognized extension %s\n",
                       image, ext );
                return -ENOEXEC;
        }

        return 0;
}
static int comboot_prepare_segment ( struct image image) [static]

Load COMBOOT image into memory, preparing a segment and returning it.

Parameters:
imageCOMBOOT image
Return values:
rcReturn status code

Definition at line 247 of file comboot.c.

References COMBOOT_PSP_SEG, image::data, DBGC, image::len, memcpy_user(), memset_user(), prep_segment(), rc, real_to_user(), and strerror().

Referenced by comboot_exec().

{
        userptr_t seg_userptr;
        size_t filesz, memsz;
        int rc;

        /* Load image in segment */
        seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );

        /* Allow etra 0x100 bytes before image for PSP */
        filesz = image->len + 0x100;

        /* Ensure the entire 64k segment is free */
        memsz = 0xFFFF;

        /* Prepare, verify, and load the real-mode segment */
        if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
                DBGC ( image, "COMBOOT %p: could not prepare segment: %s\n",
                       image, strerror ( rc ) );
                return rc;
        }

        /* Zero PSP */
        memset_user ( seg_userptr, 0, 0, 0x100 );

        /* Copy image to segment:0100 */
        memcpy_user ( seg_userptr, 0x100, image->data, 0, image->len );

        return 0;
}
static int comboot_probe ( struct image image) [static]

Probe COMBOOT image.

Parameters:
imageCOMBOOT image
Return values:
rcReturn status code

Definition at line 284 of file comboot.c.

References comboot_identify(), DBGC, image::name, and rc.

                                                 {
        int rc;

        DBGC ( image, "COMBOOT %p: name '%s'\n",
               image, image->name );

        /* Check if this is a COMBOOT image */
        if ( ( rc = comboot_identify ( image ) ) != 0 ) {

                return rc;
        }

        return 0;
}
static int comboot_exec ( struct image image) [static]

Execute COMBOOT image.

Parameters:
imageCOMBOOT image
Return values:
rcReturn status code

Definition at line 305 of file comboot.c.

References comboot_exec_loop(), comboot_prepare_segment(), console_reset(), DBGC, ENOEXEC, image::len, and rc.

                                                {
        int rc;

        /* Sanity check for filesize */
        if( image->len >= 0xFF00 ) {
                DBGC( image, "COMBOOT %p: image too large\n",
                      image );
                return -ENOEXEC;
        }

        /* Prepare segment and load image */
        if ( ( rc = comboot_prepare_segment ( image ) ) != 0 ) {
                return rc;
        }

        /* Reset console */
        console_reset();

        return comboot_exec_loop ( image );
}
struct image_type comboot_image_type __image_type ( PROBE_NORMAL  ) [read]

SYSLINUX COMBOOT (16-bit) image type.