iPXE
Defines | Functions | Variables
runtime.c File Reference

Command line and initrd passed to iPXE at runtime. More...

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/init.h>
#include <ipxe/image.h>
#include <ipxe/script.h>
#include <ipxe/umalloc.h>
#include <realmode.h>

Go to the source code of this file.

Defines

#define cmdline_phys   __use_data16 ( cmdline_phys )
#define initrd_phys   __use_data16 ( initrd_phys )
#define initrd_len   __use_data16 ( initrd_len )
#define colour   &cmdline_image
 Colour for debug messages.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
uint32_t __bss16 (cmdline_phys)
 Command line physical address.
uint32_t __bss16 (initrd_phys)
 initrd physical address
uint32_t __bss16 (initrd_len)
 initrd length
static void cmdline_image_free (struct refcnt *refcnt)
 Free command line image.
static void cmdline_strip (char *cmdline, const char *cruft)
 Strip unwanted cruft from command line.
static int cmdline_init (void)
 Initialise command line.
static int initrd_init (void)
 Initialise initrd.
static void runtime_init (void)
 Initialise command line and initrd.
struct startup_fn
runtime_startup_fn 
__startup_fn (STARTUP_NORMAL)
 Command line and initrd initialisation function.

Variables

static char * cmdline_copy
 Internal copy of the command line.
static struct image cmdline_image
 Embedded script representing the command line.

Detailed Description

Command line and initrd passed to iPXE at runtime.

Definition in file runtime.c.


Define Documentation

Definition at line 49 of file runtime.c.

Referenced by cmdline_init().

Definition at line 56 of file runtime.c.

Referenced by initrd_init().

Definition at line 63 of file runtime.c.

Referenced by initrd_init().

#define colour   &cmdline_image

Colour for debug messages.

Definition at line 84 of file runtime.c.

Referenced by cmdline_init(), cmdline_strip(), and initrd_init().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

Command line physical address.

This can be set by the prefix.

initrd physical address

This can be set by the prefix.

initrd length

This can be set by the prefix.

static void cmdline_image_free ( struct refcnt refcnt) [static]

Free command line image.

Definition at line 69 of file runtime.c.

References cmdline_copy, container_of, DBGC, and free.

                                                         {
        struct image *image = container_of ( refcnt, struct image, refcnt );

        DBGC ( image, "RUNTIME freeing command line\n" );
        free ( cmdline_copy );
}
static void cmdline_strip ( char *  cmdline,
const char *  cruft 
) [static]

Strip unwanted cruft from command line.

Parameters:
cmdlineCommand line
cruftInitial substring of cruft to strip

Definition at line 92 of file runtime.c.

References colour, DBGC, strchr(), strcpy(), and strstr().

Referenced by cmdline_init().

                                                               {
        char *strip;
        char *strip_end;

        /* Find unwanted cruft, if present */
        if ( ! ( strip = strstr ( cmdline, cruft ) ) )
                return;

        /* Strip unwanted cruft */
        strip_end = strchr ( strip, ' ' );
        if ( strip_end ) {
                *strip_end = '\0';
                DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
                strcpy ( strip, ( strip_end + 1 ) );
        } else {
                DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
                *strip = '\0';
        }
}
static int cmdline_init ( void  ) [static]

Initialise command line.

Return values:
rcReturn status code

Definition at line 117 of file runtime.c.

References cmdline, cmdline_copy, cmdline_phys, cmdline_strip(), colour, copy_from_user(), image::data, DBGC, ENOMEM, image_put(), isspace(), image::len, len, malloc(), phys_to_user(), rc, register_image(), strerror(), strlen(), strlen_user(), and virt_to_user().

Referenced by runtime_init().

                                 {
        userptr_t cmdline_user;
        char *cmdline;
        size_t len;
        int rc;

        /* Do nothing if no command line was specified */
        if ( ! cmdline_phys ) {
                DBGC ( colour, "RUNTIME found no command line\n" );
                return 0;
        }
        cmdline_user = phys_to_user ( cmdline_phys );
        len = ( strlen_user ( cmdline_user, 0 ) + 1 /* NUL */ );

        /* Allocate and copy command line */
        cmdline_copy = malloc ( len );
        if ( ! cmdline_copy ) {
                DBGC ( colour, "RUNTIME could not allocate %zd bytes for "
                       "command line\n", len );
                rc = -ENOMEM;
                goto err_alloc_cmdline_copy;
        }
        cmdline = cmdline_copy;
        copy_from_user ( cmdline, cmdline_user, 0, len );
        DBGC ( colour, "RUNTIME found command line \"%s\" at %08x\n",
               cmdline, cmdline_phys );

        /* Mark command line as consumed */
        cmdline_phys = 0;

        /* Strip unwanted cruft from the command line */
        cmdline_strip ( cmdline, "BOOT_IMAGE=" );
        cmdline_strip ( cmdline, "initrd=" );
        while ( isspace ( *cmdline ) )
                cmdline++;
        DBGC ( colour, "RUNTIME using command line \"%s\"\n", cmdline );

        /* Prepare and register image */
        cmdline_image.data = virt_to_user ( cmdline );
        cmdline_image.len = strlen ( cmdline );
        if ( cmdline_image.len ) {
                if ( ( rc = register_image ( &cmdline_image ) ) != 0 ) {
                        DBGC ( colour, "RUNTIME could not register command "
                               "line: %s\n", strerror ( rc ) );
                        goto err_register_image;
                }
        }

        /* Drop our reference to the image */
        image_put ( &cmdline_image );

        return 0;

 err_register_image:
        image_put ( &cmdline_image );
 err_alloc_cmdline_copy:
        return rc;
}
static int initrd_init ( void  ) [static]

Initialise initrd.

Return values:
rcReturn status code

Definition at line 181 of file runtime.c.

References alloc_image(), colour, image::data, DBGC, ENOMEM, image_put(), image_set_name(), initrd_len, initrd_phys, image::len, memcpy_user(), NULL, phys_to_user(), rc, register_image(), strerror(), and umalloc().

Referenced by runtime_init().

                                {
        struct image *image;
        int rc;

        /* Do nothing if no initrd was specified */
        if ( ! initrd_phys ) {
                DBGC ( colour, "RUNTIME found no initrd\n" );
                return 0;
        }
        if ( ! initrd_len ) {
                DBGC ( colour, "RUNTIME found empty initrd\n" );
                return 0;
        }
        DBGC ( colour, "RUNTIME found initrd at [%x,%x)\n",
               initrd_phys, ( initrd_phys + initrd_len ) );

        /* Allocate image */
        image = alloc_image ( NULL );
        if ( ! image ) {
                DBGC ( colour, "RUNTIME could not allocate image for "
                       "initrd\n" );
                rc = -ENOMEM;
                goto err_alloc_image;
        }
        if ( ( rc = image_set_name ( image, "<INITRD>" ) ) != 0 ) {
                DBGC ( colour, "RUNTIME could not set image name: %s\n",
                       strerror ( rc ) );
                goto err_set_name;
        }

        /* Allocate and copy initrd content */
        image->data = umalloc ( initrd_len );
        if ( ! image->data ) {
                DBGC ( colour, "RUNTIME could not allocate %d bytes for "
                       "initrd\n", initrd_len );
                rc = -ENOMEM;
                goto err_umalloc;
        }
        image->len = initrd_len;
        memcpy_user ( image->data, 0, phys_to_user ( initrd_phys ), 0,
                      initrd_len );

        /* Mark initrd as consumed */
        initrd_phys = 0;

        /* Register image */
        if ( ( rc = register_image ( image ) ) != 0 ) {
                DBGC ( colour, "RUNTIME could not register initrd: %s\n",
                       strerror ( rc ) );
                goto err_register_image;
        }

        /* Drop our reference to the image */
        image_put ( image );

        return 0;

 err_register_image:
 err_umalloc:
 err_set_name:
        image_put ( image );
 err_alloc_image:
        return rc;
}
static void runtime_init ( void  ) [static]

Initialise command line and initrd.

Definition at line 250 of file runtime.c.

References cmdline_init(), initrd_init(), and rc.

                                  {
        int rc;

        /* Initialise command line */
        if ( ( rc = cmdline_init() ) != 0 ) {
                /* No way to report failure */
                return;
        }

        /* Initialise initrd */
        if ( ( rc = initrd_init() ) != 0 ) {
                /* No way to report failure */
                return;
        }
}
struct startup_fn runtime_startup_fn __startup_fn ( STARTUP_NORMAL  ) [read]

Command line and initrd initialisation function.


Variable Documentation

char* cmdline_copy [static]

Internal copy of the command line.

Definition at line 66 of file runtime.c.

Referenced by cmdline_image_free(), and cmdline_init().

struct image cmdline_image [static]
Initial value:
 {
        .refcnt = REF_INIT ( cmdline_image_free ),
        .name = "<CMDLINE>",
        .type = &script_image_type,
}

Embedded script representing the command line.

Definition at line 77 of file runtime.c.