iPXE
Functions | Variables
pxe_file.c File Reference

PXE FILE API. More...

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/uaccess.h>
#include <ipxe/posix_io.h>
#include <ipxe/features.h>
#include <pxe.h>
#include <realmode.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FEATURE (FEATURE_MISC,"PXEXT", DHCP_EB_FEATURE_PXE_EXT, 2)
static PXENV_EXIT_t pxenv_file_open (struct s_PXENV_FILE_OPEN *file_open)
 FILE OPEN.
static PXENV_EXIT_t pxenv_file_close (struct s_PXENV_FILE_CLOSE *file_close)
 FILE CLOSE.
static PXENV_EXIT_t pxenv_file_select (struct s_PXENV_FILE_SELECT *file_select)
 FILE SELECT.
static PXENV_EXIT_t pxenv_file_read (struct s_PXENV_FILE_READ *file_read)
 FILE READ.
static PXENV_EXIT_t pxenv_get_file_size (struct s_PXENV_GET_FILE_SIZE *get_file_size)
 GET FILE SIZE.
static PXENV_EXIT_t pxenv_file_exec (struct s_PXENV_FILE_EXEC *file_exec)
 FILE EXEC.
static PXENV_EXIT_t pxenv_file_cmdline (struct s_PXENV_FILE_CMDLINE *file_cmdline)
 FILE CMDLINE.
static PXENV_EXIT_t pxenv_file_api_check (struct s_PXENV_FILE_API_CHECK *file_api_check)
 FILE API CHECK.

Variables

struct pxe_api_call pxe_file_api[] __pxe_api_call
 PXE file API.

Detailed Description

PXE FILE API.

Definition in file pxe_file.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
FEATURE ( FEATURE_MISC  ,
"PXEXT"  ,
DHCP_EB_FEATURE_PXE_EXT  ,
 
)
static PXENV_EXIT_t pxenv_file_open ( struct s_PXENV_FILE_OPEN file_open) [static]

FILE OPEN.

Parameters:
file_openPointer to a struct s_PXENV_FILE_OPEN
s_PXENV_FILE_OPEN::FileNameURL of file to open
Return values:
PXENV_EXIT_SUCCESSFile was opened
PXENV_EXIT_FAILUREFile was not opened
s_PXENV_FILE_OPEN::StatusPXE status code
s_PXENV_FILE_OPEN::FileHandleHandle of opened file

Definition at line 55 of file pxe_file.c.

References copy_from_user(), DBG, s_PXENV_FILE_OPEN::FileHandle, s_PXENV_FILE_OPEN::FileName, open(), PXENV_EXIT_FAILURE, PXENV_EXIT_SUCCESS, PXENV_STATUS, PXENV_STATUS_SUCCESS, real_to_user(), s_PXENV_FILE_OPEN::Status, and strlen_user().

                                                                            {
        userptr_t filename;
        size_t filename_len;
        int fd;

        DBG ( "PXENV_FILE_OPEN" );

        /* Copy name from external program, and open it */
        filename = real_to_user ( file_open->FileName.segment,
                              file_open->FileName.offset );
        filename_len = strlen_user ( filename, 0 );
        {
                char uri_string[ filename_len + 1 ];

                copy_from_user ( uri_string, filename, 0,
                                 sizeof ( uri_string ) );
                DBG ( " %s", uri_string );
                fd = open ( uri_string );
        }

        if ( fd < 0 ) {
                file_open->Status = PXENV_STATUS ( fd );
                return PXENV_EXIT_FAILURE;
        }

        DBG ( " as file %d", fd );

        file_open->FileHandle = fd;
        file_open->Status = PXENV_STATUS_SUCCESS;
        return PXENV_EXIT_SUCCESS;
}
static PXENV_EXIT_t pxenv_file_close ( struct s_PXENV_FILE_CLOSE file_close) [static]

FILE CLOSE.

Parameters:
file_closePointer to a struct s_PXENV_FILE_CLOSE
s_PXENV_FILE_CLOSE::FileHandleFile handle
Return values:
PXENV_EXIT_SUCCESSFile was closed
PXENV_EXIT_FAILUREFile was not closed
s_PXENV_FILE_CLOSE::StatusPXE status code

Definition at line 97 of file pxe_file.c.

References close(), DBG, s_PXENV_FILE_CLOSE::FileHandle, PXENV_EXIT_SUCCESS, PXENV_STATUS_SUCCESS, and s_PXENV_FILE_CLOSE::Status.

                                                                               {

        DBG ( "PXENV_FILE_CLOSE %d", file_close->FileHandle );

        close ( file_close->FileHandle );
        file_close->Status = PXENV_STATUS_SUCCESS;
        return PXENV_EXIT_SUCCESS;
}
static PXENV_EXIT_t pxenv_file_select ( struct s_PXENV_FILE_SELECT file_select) [static]

FILE SELECT.

Parameters:
file_selectPointer to a struct s_PXENV_FILE_SELECT
s_PXENV_FILE_SELECT::FileHandleFile handle
Return values:
PXENV_EXIT_SUCCESSFile has been checked for readiness
PXENV_EXIT_FAILUREFile has not been checked for readiness
s_PXENV_FILE_SELECT::StatusPXE status code
s_PXENV_FILE_SELECT::ReadyIndication of readiness

Definition at line 118 of file pxe_file.c.

References DBG, s_PXENV_FILE_SELECT::FileHandle, PXENV_EXIT_FAILURE, PXENV_EXIT_SUCCESS, PXENV_STATUS, PXENV_STATUS_SUCCESS, RDY_READ, s_PXENV_FILE_SELECT::Ready, select(), and s_PXENV_FILE_SELECT::Status.

                                                              {
        fd_set fdset;
        int ready;

        DBG ( "PXENV_FILE_SELECT %d", file_select->FileHandle );

        FD_ZERO ( &fdset );
        FD_SET ( file_select->FileHandle, &fdset );
        if ( ( ready = select ( &fdset, 0 ) ) < 0 ) {
                file_select->Status = PXENV_STATUS ( ready );
                return PXENV_EXIT_FAILURE;
        }

        file_select->Ready = ( ready ? RDY_READ : 0 );
        file_select->Status = PXENV_STATUS_SUCCESS;
        return PXENV_EXIT_SUCCESS;
}
static PXENV_EXIT_t pxenv_file_read ( struct s_PXENV_FILE_READ file_read) [static]

FILE READ.

Parameters:
file_readPointer to a struct s_PXENV_FILE_READ
s_PXENV_FILE_READ::FileHandleFile handle
s_PXENV_FILE_READ::BufferSizeSize of data buffer
s_PXENV_FILE_READ::BufferData buffer
Return values:
PXENV_EXIT_SUCCESSData has been read from file
PXENV_EXIT_FAILUREData has not been read from file
s_PXENV_FILE_READ::StatusPXE status code
s_PXENV_FILE_READ::ReadyIndication of readiness
s_PXENV_FILE_READ::BufferSizeLength of data read

Definition at line 150 of file pxe_file.c.

References buffer, s_PXENV_FILE_READ::Buffer, s_PXENV_FILE_READ::BufferSize, DBG, s_PXENV_FILE_READ::FileHandle, len, PXENV_EXIT_FAILURE, PXENV_EXIT_SUCCESS, PXENV_STATUS, PXENV_STATUS_SUCCESS, read_user(), real_to_user(), and s_PXENV_FILE_READ::Status.

                                                                            {
        userptr_t buffer;
        ssize_t len;

        DBG ( "PXENV_FILE_READ %d to %04x:%04x+%04x", file_read->FileHandle,
              file_read->Buffer.segment, file_read->Buffer.offset,
              file_read->BufferSize );

        buffer = real_to_user ( file_read->Buffer.segment,
                                file_read->Buffer.offset );
        if ( ( len = read_user ( file_read->FileHandle, buffer, 0,
                                file_read->BufferSize ) ) < 0 ) {
                file_read->Status = PXENV_STATUS ( len );
                return PXENV_EXIT_FAILURE;
        }

        DBG ( " read %04zx", ( ( size_t ) len ) );

        file_read->BufferSize = len;
        file_read->Status = PXENV_STATUS_SUCCESS;
        return PXENV_EXIT_SUCCESS;
}
static PXENV_EXIT_t pxenv_get_file_size ( struct s_PXENV_GET_FILE_SIZE get_file_size) [static]

GET FILE SIZE.

Parameters:
get_file_sizePointer to a struct s_PXENV_GET_FILE_SIZE
s_PXENV_GET_FILE_SIZE::FileHandleFile handle
Return values:
PXENV_EXIT_SUCCESSFile size has been determined
PXENV_EXIT_FAILUREFile size has not been determined
s_PXENV_GET_FILE_SIZE::StatusPXE status code
s_PXENV_GET_FILE_SIZE::FileSizeSize of file

Definition at line 184 of file pxe_file.c.

References DBG, s_PXENV_GET_FILE_SIZE::FileHandle, s_PXENV_GET_FILE_SIZE::FileSize, fsize(), PXENV_EXIT_FAILURE, PXENV_EXIT_SUCCESS, PXENV_STATUS, PXENV_STATUS_SUCCESS, and s_PXENV_GET_FILE_SIZE::Status.

                                                                    {
        ssize_t filesize;

        DBG ( "PXENV_GET_FILE_SIZE %d", get_file_size->FileHandle );

        filesize = fsize ( get_file_size->FileHandle );
        if ( filesize < 0 ) {
                get_file_size->Status = PXENV_STATUS ( filesize );
                return PXENV_EXIT_FAILURE;
        }

        DBG ( " is %zd", ( ( size_t ) filesize ) );

        get_file_size->FileSize = filesize;
        get_file_size->Status = PXENV_STATUS_SUCCESS;
        return PXENV_EXIT_SUCCESS;
}
static PXENV_EXIT_t pxenv_file_exec ( struct s_PXENV_FILE_EXEC file_exec) [static]

FILE EXEC.

Parameters:
file_execPointer to a struct s_PXENV_FILE_EXEC
s_PXENV_FILE_EXEC::CommandCommand to execute
Return values:
PXENV_EXIT_SUCCESSCommand was executed successfully
PXENV_EXIT_FAILURECommand was not executed successfully
s_PXENV_FILE_EXEC::StatusPXE status code

Definition at line 212 of file pxe_file.c.

References command, s_PXENV_FILE_EXEC::Command, copy_from_user(), DBG, PXENV_EXIT_FAILURE, PXENV_EXIT_SUCCESS, PXENV_STATUS, PXENV_STATUS_SUCCESS, rc, real_to_user(), s_PXENV_FILE_EXEC::Status, strlen_user(), and system.

                                                                            {
        userptr_t command;
        size_t command_len;
        int rc;

        DBG ( "PXENV_FILE_EXEC" );

        /* Copy name from external program, and exec it */
        command = real_to_user ( file_exec->Command.segment,
                                 file_exec->Command.offset );
        command_len = strlen_user ( command, 0 );
        {
                char command_string[ command_len + 1 ];

                copy_from_user ( command_string, command, 0,
                                 sizeof ( command_string ) );
                DBG ( " %s", command_string );

                if ( ( rc = system ( command_string ) ) != 0 ) {
                        file_exec->Status = PXENV_STATUS ( rc );
                        return PXENV_EXIT_FAILURE;
                }
        }

        file_exec->Status = PXENV_STATUS_SUCCESS;
        return PXENV_EXIT_SUCCESS;
}
static PXENV_EXIT_t pxenv_file_cmdline ( struct s_PXENV_FILE_CMDLINE file_cmdline) [static]

FILE CMDLINE.

Parameters:
file_cmdlinePointer to a struct s_PXENV_FILE_CMDLINE
s_PXENV_FILE_CMDLINE::BufferBuffer to contain command line
s_PXENV_FILE_CMDLINE::BufferSizeSize of buffer
Return values:
PXENV_EXIT_SUCCESSCommand was executed successfully
PXENV_EXIT_FAILURECommand was not executed successfully
s_PXENV_FILE_EXEC::StatusPXE status code
s_PXENV_FILE_EXEC::BufferSizeLength of command line (including NUL)

Definition at line 253 of file pxe_file.c.

References buffer, s_PXENV_FILE_CMDLINE::Buffer, s_PXENV_FILE_CMDLINE::BufferSize, copy_to_user(), DBG, len, max_len, pxe_cmdline, PXENV_EXIT_SUCCESS, PXENV_STATUS_SUCCESS, real_to_user(), s_PXENV_FILE_CMDLINE::Status, and strlen().

                                                                 {
        userptr_t buffer;
        size_t max_len;
        size_t len;

        DBG ( "PXENV_FILE_CMDLINE to %04x:%04x+%04x \"%s\"\n",
              file_cmdline->Buffer.segment, file_cmdline->Buffer.offset,
              file_cmdline->BufferSize, pxe_cmdline );

        buffer = real_to_user ( file_cmdline->Buffer.segment,
                                file_cmdline->Buffer.offset );
        len = file_cmdline->BufferSize;
        max_len = ( pxe_cmdline ?
                    ( strlen ( pxe_cmdline ) + 1 /* NUL */ ) : 0 );
        if ( len > max_len )
                len = max_len;
        copy_to_user ( buffer, 0, pxe_cmdline, len );
        file_cmdline->BufferSize = max_len;

        file_cmdline->Status = PXENV_STATUS_SUCCESS;
        return PXENV_EXIT_SUCCESS;
}
static PXENV_EXIT_t pxenv_file_api_check ( struct s_PXENV_FILE_API_CHECK file_api_check) [static]

FILE API CHECK.

Parameters:
file_execPointer to a struct s_PXENV_FILE_API_CHECK
s_PXENV_FILE_API_CHECK::MagicInbound magic number (0x91d447b2)
Return values:
PXENV_EXIT_SUCCESSCommand was executed successfully
PXENV_EXIT_FAILURECommand was not executed successfully
s_PXENV_FILE_API_CHECK::StatusPXE status code
s_PXENV_FILE_API_CHECK::MagicOutbound magic number (0xe9c17b20)
s_PXENV_FILE_API_CHECK::Provider"iPXE" (0x45585067)
s_PXENV_FILE_API_CHECK::APIMaskAPI function bitmask
s_PXENV_FILE_API_CHECK::FlagsReserved

Definition at line 291 of file pxe_file.c.

References s_PXENV_FILE_API_CHECK::APIMask, DBG, s_PXENV_FILE_API_CHECK::Flags, for_each_table_entry, s_PXENV_FILE_API_CHECK::Magic, offset, pxe_api_call::opcode, s_PXENV_FILE_API_CHECK::Provider, PXE_API_CALLS, PXENV_EXIT_FAILURE, PXENV_EXIT_SUCCESS, PXENV_FILE_MAX, PXENV_FILE_MIN, PXENV_STATUS_BAD_FUNC, PXENV_STATUS_OUT_OF_RESOURCES, PXENV_STATUS_SUCCESS, s_PXENV_FILE_API_CHECK::Size, and s_PXENV_FILE_API_CHECK::Status.

                                                                       {
        struct pxe_api_call *call;
        unsigned int mask = 0;
        unsigned int offset;

        DBG ( "PXENV_FILE_API_CHECK" );

        /* Check for magic value */
        if ( file_api_check->Magic != 0x91d447b2 ) {
                file_api_check->Status = PXENV_STATUS_BAD_FUNC;
                return PXENV_EXIT_FAILURE;
        }

        /* Check for required parameter size */
        if ( file_api_check->Size < sizeof ( *file_api_check ) ) {
                file_api_check->Status = PXENV_STATUS_OUT_OF_RESOURCES;
                return PXENV_EXIT_FAILURE;
        }

        /* Determine supported calls */
        for_each_table_entry ( call, PXE_API_CALLS ) {
                offset = ( call->opcode - PXENV_FILE_MIN );
                if ( offset <= ( PXENV_FILE_MAX - PXENV_FILE_MIN ) )
                        mask |= ( 1 << offset );
        }

        /* Fill in parameters */
        file_api_check->Size = sizeof ( *file_api_check );
        file_api_check->Magic = 0xe9c17b20;
        file_api_check->Provider = 0x45585067; /* "iPXE" */
        file_api_check->APIMask = mask;
        file_api_check->Flags = 0; /* None defined */

        file_api_check->Status = PXENV_STATUS_SUCCESS;
        return PXENV_EXIT_SUCCESS;
}

Variable Documentation

struct pxe_api_call pxe_file_api [] __pxe_api_call