iPXE
pxe_file.c
Go to the documentation of this file.
00001 /** @file
00002  *
00003  * PXE FILE API
00004  *
00005  */
00006 
00007 #include <stdlib.h>
00008 #include <stdio.h>
00009 #include <errno.h>
00010 #include <byteswap.h>
00011 #include <ipxe/uaccess.h>
00012 #include <ipxe/posix_io.h>
00013 #include <ipxe/features.h>
00014 #include <pxe.h>
00015 #include <realmode.h>
00016 
00017 /*
00018  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00019  *
00020  * This program is free software; you can redistribute it and/or
00021  * modify it under the terms of the GNU General Public License as
00022  * published by the Free Software Foundation; either version 2 of the
00023  * License, or any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful, but
00026  * WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00028  * General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00033  * 02110-1301, USA.
00034  *
00035  * You can also choose to distribute this program under the terms of
00036  * the Unmodified Binary Distribution Licence (as given in the file
00037  * COPYING.UBDL), provided that you have satisfied its requirements.
00038  */
00039 
00040 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00041 
00042 FEATURE ( FEATURE_MISC, "PXEXT", DHCP_EB_FEATURE_PXE_EXT, 2 );
00043 
00044 /**
00045  * FILE OPEN
00046  *
00047  * @v file_open                         Pointer to a struct s_PXENV_FILE_OPEN
00048  * @v s_PXENV_FILE_OPEN::FileName       URL of file to open
00049  * @ret #PXENV_EXIT_SUCCESS             File was opened
00050  * @ret #PXENV_EXIT_FAILURE             File was not opened
00051  * @ret s_PXENV_FILE_OPEN::Status       PXE status code
00052  * @ret s_PXENV_FILE_OPEN::FileHandle   Handle of opened file
00053  *
00054  */
00055 static PXENV_EXIT_t pxenv_file_open ( struct s_PXENV_FILE_OPEN *file_open ) {
00056         userptr_t filename;
00057         size_t filename_len;
00058         int fd;
00059 
00060         DBG ( "PXENV_FILE_OPEN" );
00061 
00062         /* Copy name from external program, and open it */
00063         filename = real_to_user ( file_open->FileName.segment,
00064                               file_open->FileName.offset );
00065         filename_len = strlen_user ( filename, 0 );
00066         {
00067                 char uri_string[ filename_len + 1 ];
00068 
00069                 copy_from_user ( uri_string, filename, 0,
00070                                  sizeof ( uri_string ) );
00071                 DBG ( " %s", uri_string );
00072                 fd = open ( uri_string );
00073         }
00074 
00075         if ( fd < 0 ) {
00076                 file_open->Status = PXENV_STATUS ( fd );
00077                 return PXENV_EXIT_FAILURE;
00078         }
00079 
00080         DBG ( " as file %d", fd );
00081 
00082         file_open->FileHandle = fd;
00083         file_open->Status = PXENV_STATUS_SUCCESS;
00084         return PXENV_EXIT_SUCCESS;
00085 }
00086 
00087 /**
00088  * FILE CLOSE
00089  *
00090  * @v file_close                        Pointer to a struct s_PXENV_FILE_CLOSE
00091  * @v s_PXENV_FILE_CLOSE::FileHandle    File handle
00092  * @ret #PXENV_EXIT_SUCCESS             File was closed
00093  * @ret #PXENV_EXIT_FAILURE             File was not closed
00094  * @ret s_PXENV_FILE_CLOSE::Status      PXE status code
00095  *
00096  */
00097 static PXENV_EXIT_t pxenv_file_close ( struct s_PXENV_FILE_CLOSE *file_close ) {
00098 
00099         DBG ( "PXENV_FILE_CLOSE %d", file_close->FileHandle );
00100 
00101         close ( file_close->FileHandle );
00102         file_close->Status = PXENV_STATUS_SUCCESS;
00103         return PXENV_EXIT_SUCCESS;
00104 }
00105 
00106 /**
00107  * FILE SELECT
00108  *
00109  * @v file_select                       Pointer to a struct s_PXENV_FILE_SELECT
00110  * @v s_PXENV_FILE_SELECT::FileHandle   File handle
00111  * @ret #PXENV_EXIT_SUCCESS             File has been checked for readiness
00112  * @ret #PXENV_EXIT_FAILURE             File has not been checked for readiness
00113  * @ret s_PXENV_FILE_SELECT::Status     PXE status code
00114  * @ret s_PXENV_FILE_SELECT::Ready      Indication of readiness
00115  *
00116  */
00117 static PXENV_EXIT_t
00118 pxenv_file_select ( struct s_PXENV_FILE_SELECT *file_select ) {
00119         fd_set fdset;
00120         int ready;
00121 
00122         DBG ( "PXENV_FILE_SELECT %d", file_select->FileHandle );
00123 
00124         FD_ZERO ( &fdset );
00125         FD_SET ( file_select->FileHandle, &fdset );
00126         if ( ( ready = select ( &fdset, 0 ) ) < 0 ) {
00127                 file_select->Status = PXENV_STATUS ( ready );
00128                 return PXENV_EXIT_FAILURE;
00129         }
00130 
00131         file_select->Ready = ( ready ? RDY_READ : 0 );
00132         file_select->Status = PXENV_STATUS_SUCCESS;
00133         return PXENV_EXIT_SUCCESS;
00134 }
00135 
00136 /**
00137  * FILE READ
00138  *
00139  * @v file_read                         Pointer to a struct s_PXENV_FILE_READ
00140  * @v s_PXENV_FILE_READ::FileHandle     File handle
00141  * @v s_PXENV_FILE_READ::BufferSize     Size of data buffer
00142  * @v s_PXENV_FILE_READ::Buffer         Data buffer
00143  * @ret #PXENV_EXIT_SUCCESS             Data has been read from file
00144  * @ret #PXENV_EXIT_FAILURE             Data has not been read from file
00145  * @ret s_PXENV_FILE_READ::Status       PXE status code
00146  * @ret s_PXENV_FILE_READ::Ready        Indication of readiness
00147  * @ret s_PXENV_FILE_READ::BufferSize   Length of data read
00148  *
00149  */
00150 static PXENV_EXIT_t pxenv_file_read ( struct s_PXENV_FILE_READ *file_read ) {
00151         userptr_t buffer;
00152         ssize_t len;
00153 
00154         DBG ( "PXENV_FILE_READ %d to %04x:%04x+%04x", file_read->FileHandle,
00155               file_read->Buffer.segment, file_read->Buffer.offset,
00156               file_read->BufferSize );
00157 
00158         buffer = real_to_user ( file_read->Buffer.segment,
00159                                 file_read->Buffer.offset );
00160         if ( ( len = read_user ( file_read->FileHandle, buffer, 0,
00161                                 file_read->BufferSize ) ) < 0 ) {
00162                 file_read->Status = PXENV_STATUS ( len );
00163                 return PXENV_EXIT_FAILURE;
00164         }
00165 
00166         DBG ( " read %04zx", ( ( size_t ) len ) );
00167 
00168         file_read->BufferSize = len;
00169         file_read->Status = PXENV_STATUS_SUCCESS;
00170         return PXENV_EXIT_SUCCESS;
00171 }
00172 
00173 /**
00174  * GET FILE SIZE
00175  *
00176  * @v get_file_size                     Pointer to a struct s_PXENV_GET_FILE_SIZE
00177  * @v s_PXENV_GET_FILE_SIZE::FileHandle File handle
00178  * @ret #PXENV_EXIT_SUCCESS             File size has been determined
00179  * @ret #PXENV_EXIT_FAILURE             File size has not been determined
00180  * @ret s_PXENV_GET_FILE_SIZE::Status   PXE status code
00181  * @ret s_PXENV_GET_FILE_SIZE::FileSize Size of file
00182  */
00183 static PXENV_EXIT_t
00184 pxenv_get_file_size ( struct s_PXENV_GET_FILE_SIZE *get_file_size ) {
00185         ssize_t filesize;
00186 
00187         DBG ( "PXENV_GET_FILE_SIZE %d", get_file_size->FileHandle );
00188 
00189         filesize = fsize ( get_file_size->FileHandle );
00190         if ( filesize < 0 ) {
00191                 get_file_size->Status = PXENV_STATUS ( filesize );
00192                 return PXENV_EXIT_FAILURE;
00193         }
00194 
00195         DBG ( " is %zd", ( ( size_t ) filesize ) );
00196 
00197         get_file_size->FileSize = filesize;
00198         get_file_size->Status = PXENV_STATUS_SUCCESS;
00199         return PXENV_EXIT_SUCCESS;
00200 }
00201 
00202 /**
00203  * FILE EXEC
00204  *
00205  * @v file_exec                         Pointer to a struct s_PXENV_FILE_EXEC
00206  * @v s_PXENV_FILE_EXEC::Command        Command to execute
00207  * @ret #PXENV_EXIT_SUCCESS             Command was executed successfully
00208  * @ret #PXENV_EXIT_FAILURE             Command was not executed successfully
00209  * @ret s_PXENV_FILE_EXEC::Status       PXE status code
00210  *
00211  */
00212 static PXENV_EXIT_t pxenv_file_exec ( struct s_PXENV_FILE_EXEC *file_exec ) {
00213         userptr_t command;
00214         size_t command_len;
00215         int rc;
00216 
00217         DBG ( "PXENV_FILE_EXEC" );
00218 
00219         /* Copy name from external program, and exec it */
00220         command = real_to_user ( file_exec->Command.segment,
00221                                  file_exec->Command.offset );
00222         command_len = strlen_user ( command, 0 );
00223         {
00224                 char command_string[ command_len + 1 ];
00225 
00226                 copy_from_user ( command_string, command, 0,
00227                                  sizeof ( command_string ) );
00228                 DBG ( " %s", command_string );
00229 
00230                 if ( ( rc = system ( command_string ) ) != 0 ) {
00231                         file_exec->Status = PXENV_STATUS ( rc );
00232                         return PXENV_EXIT_FAILURE;
00233                 }
00234         }
00235 
00236         file_exec->Status = PXENV_STATUS_SUCCESS;
00237         return PXENV_EXIT_SUCCESS;
00238 }
00239 
00240 /**
00241  * FILE CMDLINE
00242  *
00243  * @v file_cmdline                      Pointer to a struct s_PXENV_FILE_CMDLINE
00244  * @v s_PXENV_FILE_CMDLINE::Buffer      Buffer to contain command line
00245  * @v s_PXENV_FILE_CMDLINE::BufferSize  Size of buffer
00246  * @ret #PXENV_EXIT_SUCCESS             Command was executed successfully
00247  * @ret #PXENV_EXIT_FAILURE             Command was not executed successfully
00248  * @ret s_PXENV_FILE_EXEC::Status       PXE status code
00249  * @ret s_PXENV_FILE_EXEC::BufferSize   Length of command line (including NUL)
00250  *
00251  */
00252 static PXENV_EXIT_t
00253 pxenv_file_cmdline ( struct s_PXENV_FILE_CMDLINE *file_cmdline ) {
00254         userptr_t buffer;
00255         size_t max_len;
00256         size_t len;
00257 
00258         DBG ( "PXENV_FILE_CMDLINE to %04x:%04x+%04x \"%s\"\n",
00259               file_cmdline->Buffer.segment, file_cmdline->Buffer.offset,
00260               file_cmdline->BufferSize, pxe_cmdline );
00261 
00262         buffer = real_to_user ( file_cmdline->Buffer.segment,
00263                                 file_cmdline->Buffer.offset );
00264         len = file_cmdline->BufferSize;
00265         max_len = ( pxe_cmdline ?
00266                     ( strlen ( pxe_cmdline ) + 1 /* NUL */ ) : 0 );
00267         if ( len > max_len )
00268                 len = max_len;
00269         copy_to_user ( buffer, 0, pxe_cmdline, len );
00270         file_cmdline->BufferSize = max_len;
00271 
00272         file_cmdline->Status = PXENV_STATUS_SUCCESS;
00273         return PXENV_EXIT_SUCCESS;
00274 }
00275 
00276 /**
00277  * FILE API CHECK
00278  *
00279  * @v file_exec                         Pointer to a struct s_PXENV_FILE_API_CHECK
00280  * @v s_PXENV_FILE_API_CHECK::Magic     Inbound magic number (0x91d447b2)
00281  * @ret #PXENV_EXIT_SUCCESS             Command was executed successfully
00282  * @ret #PXENV_EXIT_FAILURE             Command was not executed successfully
00283  * @ret s_PXENV_FILE_API_CHECK::Status  PXE status code
00284  * @ret s_PXENV_FILE_API_CHECK::Magic   Outbound magic number (0xe9c17b20)
00285  * @ret s_PXENV_FILE_API_CHECK::Provider "iPXE" (0x45585067)
00286  * @ret s_PXENV_FILE_API_CHECK::APIMask API function bitmask
00287  * @ret s_PXENV_FILE_API_CHECK::Flags   Reserved
00288  *
00289  */
00290 static PXENV_EXIT_t
00291 pxenv_file_api_check ( struct s_PXENV_FILE_API_CHECK *file_api_check ) {
00292         struct pxe_api_call *call;
00293         unsigned int mask = 0;
00294         unsigned int offset;
00295 
00296         DBG ( "PXENV_FILE_API_CHECK" );
00297 
00298         /* Check for magic value */
00299         if ( file_api_check->Magic != 0x91d447b2 ) {
00300                 file_api_check->Status = PXENV_STATUS_BAD_FUNC;
00301                 return PXENV_EXIT_FAILURE;
00302         }
00303 
00304         /* Check for required parameter size */
00305         if ( file_api_check->Size < sizeof ( *file_api_check ) ) {
00306                 file_api_check->Status = PXENV_STATUS_OUT_OF_RESOURCES;
00307                 return PXENV_EXIT_FAILURE;
00308         }
00309 
00310         /* Determine supported calls */
00311         for_each_table_entry ( call, PXE_API_CALLS ) {
00312                 offset = ( call->opcode - PXENV_FILE_MIN );
00313                 if ( offset <= ( PXENV_FILE_MAX - PXENV_FILE_MIN ) )
00314                         mask |= ( 1 << offset );
00315         }
00316 
00317         /* Fill in parameters */
00318         file_api_check->Size = sizeof ( *file_api_check );
00319         file_api_check->Magic = 0xe9c17b20;
00320         file_api_check->Provider = 0x45585067; /* "iPXE" */
00321         file_api_check->APIMask = mask;
00322         file_api_check->Flags = 0; /* None defined */
00323 
00324         file_api_check->Status = PXENV_STATUS_SUCCESS;
00325         return PXENV_EXIT_SUCCESS;
00326 }
00327 
00328 /** PXE file API */
00329 struct pxe_api_call pxe_file_api[] __pxe_api_call = {
00330         PXE_API_CALL ( PXENV_FILE_OPEN, pxenv_file_open,
00331                        struct s_PXENV_FILE_OPEN ),
00332         PXE_API_CALL ( PXENV_FILE_CLOSE, pxenv_file_close,
00333                        struct s_PXENV_FILE_CLOSE ),
00334         PXE_API_CALL ( PXENV_FILE_SELECT, pxenv_file_select,
00335                        struct s_PXENV_FILE_SELECT ),
00336         PXE_API_CALL ( PXENV_FILE_READ, pxenv_file_read,
00337                        struct s_PXENV_FILE_READ ),
00338         PXE_API_CALL ( PXENV_GET_FILE_SIZE, pxenv_get_file_size,
00339                        struct s_PXENV_GET_FILE_SIZE ),
00340         PXE_API_CALL ( PXENV_FILE_EXEC, pxenv_file_exec,
00341                        struct s_PXENV_FILE_EXEC ),
00342         PXE_API_CALL ( PXENV_FILE_CMDLINE, pxenv_file_cmdline,
00343                        struct s_PXENV_FILE_CMDLINE ),
00344         PXE_API_CALL ( PXENV_FILE_API_CHECK, pxenv_file_api_check,
00345                        struct s_PXENV_FILE_API_CHECK ),
00346 };