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 <gpxe/uaccess.h>
00012 #include <gpxe/posix_io.h>
00013 #include <gpxe/features.h>
00014 #include <pxe.h>
00015 #include <realmode.h>
00016 
00017 /*
00018  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00019  * Portions (C) 2010 Shao Miller <shao.miller@yrdsb.edu.on.ca>.
00020  *              [PXE exit hook logic]
00021  *
00022  * This program is free software; you can redistribute it and/or
00023  * modify it under the terms of the GNU General Public License as
00024  * published by the Free Software Foundation; either version 2 of the
00025  * License, or any later version.
00026  *
00027  * This program is distributed in the hope that it will be useful, but
00028  * WITHOUT ANY WARRANTY; without even the implied warranty of
00029  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00030  * General Public License for more details.
00031  *
00032  * You should have received a copy of the GNU General Public License
00033  * along with this program; if not, write to the Free Software
00034  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00035  */
00036 
00037 FILE_LICENCE ( GPL2_OR_LATER );
00038 
00039 FEATURE ( FEATURE_MISC, "PXEXT", DHCP_EB_FEATURE_PXE_EXT, 2 );
00040 
00041 /**
00042  * FILE OPEN
00043  *
00044  * @v file_open                         Pointer to a struct s_PXENV_FILE_OPEN
00045  * @v s_PXENV_FILE_OPEN::FileName       URL of file to open
00046  * @ret #PXENV_EXIT_SUCCESS             File was opened
00047  * @ret #PXENV_EXIT_FAILURE             File was not opened
00048  * @ret s_PXENV_FILE_OPEN::Status       PXE status code
00049  * @ret s_PXENV_FILE_OPEN::FileHandle   Handle of opened file
00050  *
00051  */
00052 PXENV_EXIT_t pxenv_file_open ( struct s_PXENV_FILE_OPEN *file_open ) {
00053         userptr_t filename;
00054         size_t filename_len;
00055         int fd;
00056 
00057         DBG ( "PXENV_FILE_OPEN" );
00058 
00059         /* Copy name from external program, and open it */
00060         filename = real_to_user ( file_open->FileName.segment,
00061                               file_open->FileName.offset );
00062         filename_len = strlen_user ( filename, 0 );
00063         {
00064                 char uri_string[ filename_len + 1 ];
00065 
00066                 copy_from_user ( uri_string, filename, 0,
00067                                  sizeof ( uri_string ) );
00068                 DBG ( " %s", uri_string );
00069                 fd = open ( uri_string );
00070         }
00071 
00072         if ( fd < 0 ) {
00073                 file_open->Status = PXENV_STATUS ( fd );
00074                 return PXENV_EXIT_FAILURE;
00075         }
00076 
00077         DBG ( " as file %d", fd );
00078 
00079         file_open->FileHandle = fd;
00080         file_open->Status = PXENV_STATUS_SUCCESS;
00081         return PXENV_EXIT_SUCCESS;
00082 }
00083 
00084 /**
00085  * FILE CLOSE
00086  *
00087  * @v file_close                        Pointer to a struct s_PXENV_FILE_CLOSE
00088  * @v s_PXENV_FILE_CLOSE::FileHandle    File handle
00089  * @ret #PXENV_EXIT_SUCCESS             File was closed
00090  * @ret #PXENV_EXIT_FAILURE             File was not closed
00091  * @ret s_PXENV_FILE_CLOSE::Status      PXE status code
00092  *
00093  */
00094 PXENV_EXIT_t pxenv_file_close ( struct s_PXENV_FILE_CLOSE *file_close ) {
00095 
00096         DBG ( "PXENV_FILE_CLOSE %d", file_close->FileHandle );
00097 
00098         close ( file_close->FileHandle );
00099         file_close->Status = PXENV_STATUS_SUCCESS;
00100         return PXENV_EXIT_SUCCESS;
00101 }
00102 
00103 /**
00104  * FILE SELECT
00105  *
00106  * @v file_select                       Pointer to a struct s_PXENV_FILE_SELECT
00107  * @v s_PXENV_FILE_SELECT::FileHandle   File handle
00108  * @ret #PXENV_EXIT_SUCCESS             File has been checked for readiness
00109  * @ret #PXENV_EXIT_FAILURE             File has not been checked for readiness
00110  * @ret s_PXENV_FILE_SELECT::Status     PXE status code
00111  * @ret s_PXENV_FILE_SELECT::Ready      Indication of readiness
00112  *
00113  */
00114 PXENV_EXIT_t pxenv_file_select ( struct s_PXENV_FILE_SELECT *file_select ) {
00115         fd_set fdset;
00116         int ready;
00117 
00118         DBG ( "PXENV_FILE_SELECT %d", file_select->FileHandle );
00119 
00120         FD_ZERO ( &fdset );
00121         FD_SET ( file_select->FileHandle, &fdset );
00122         if ( ( ready = select ( &fdset, 0 ) ) < 0 ) {
00123                 file_select->Status = PXENV_STATUS ( ready );
00124                 return PXENV_EXIT_FAILURE;
00125         }
00126 
00127         file_select->Ready = ( ready ? RDY_READ : 0 );
00128         file_select->Status = PXENV_STATUS_SUCCESS;
00129         return PXENV_EXIT_SUCCESS;
00130 }
00131 
00132 /**
00133  * FILE READ
00134  *
00135  * @v file_read                         Pointer to a struct s_PXENV_FILE_READ
00136  * @v s_PXENV_FILE_READ::FileHandle     File handle
00137  * @v s_PXENV_FILE_READ::BufferSize     Size of data buffer
00138  * @v s_PXENV_FILE_READ::Buffer         Data buffer
00139  * @ret #PXENV_EXIT_SUCCESS             Data has been read from file
00140  * @ret #PXENV_EXIT_FAILURE             Data has not been read from file
00141  * @ret s_PXENV_FILE_READ::Status       PXE status code
00142  * @ret s_PXENV_FILE_READ::Ready        Indication of readiness
00143  * @ret s_PXENV_FILE_READ::BufferSize   Length of data read
00144  *
00145  */
00146 PXENV_EXIT_t pxenv_file_read ( struct s_PXENV_FILE_READ *file_read ) {
00147         userptr_t buffer;
00148         ssize_t len;
00149 
00150         DBG ( "PXENV_FILE_READ %d to %04x:%04x+%04x", file_read->FileHandle,
00151               file_read->Buffer.segment, file_read->Buffer.offset,
00152               file_read->BufferSize );
00153 
00154         buffer = real_to_user ( file_read->Buffer.segment,
00155                                 file_read->Buffer.offset );
00156         if ( ( len = read_user ( file_read->FileHandle, buffer, 0,
00157                                 file_read->BufferSize ) ) < 0 ) {
00158                 file_read->Status = PXENV_STATUS ( len );
00159                 return PXENV_EXIT_FAILURE;
00160         }
00161 
00162         DBG ( " read %04zx", ( ( size_t ) len ) );
00163 
00164         file_read->BufferSize = len;
00165         file_read->Status = PXENV_STATUS_SUCCESS;
00166         return PXENV_EXIT_SUCCESS;
00167 }
00168 
00169 /**
00170  * GET FILE SIZE
00171  *
00172  * @v get_file_size                     Pointer to a struct s_PXENV_GET_FILE_SIZE
00173  * @v s_PXENV_GET_FILE_SIZE::FileHandle File handle
00174  * @ret #PXENV_EXIT_SUCCESS             File size has been determined
00175  * @ret #PXENV_EXIT_FAILURE             File size has not been determined
00176  * @ret s_PXENV_GET_FILE_SIZE::Status   PXE status code
00177  * @ret s_PXENV_GET_FILE_SIZE::FileSize Size of file
00178  */
00179 PXENV_EXIT_t pxenv_get_file_size ( struct s_PXENV_GET_FILE_SIZE
00180                                    *get_file_size ) {
00181         ssize_t filesize;
00182 
00183         DBG ( "PXENV_GET_FILE_SIZE %d", get_file_size->FileHandle );
00184 
00185         filesize = fsize ( get_file_size->FileHandle );
00186         if ( filesize < 0 ) {
00187                 get_file_size->Status = PXENV_STATUS ( filesize );
00188                 return PXENV_EXIT_FAILURE;
00189         }
00190 
00191         DBG ( " is %zd", ( ( size_t ) filesize ) );
00192 
00193         get_file_size->FileSize = filesize;
00194         get_file_size->Status = PXENV_STATUS_SUCCESS;
00195         return PXENV_EXIT_SUCCESS;
00196 }
00197 
00198 /**
00199  * FILE EXEC
00200  *
00201  * @v file_exec                         Pointer to a struct s_PXENV_FILE_EXEC
00202  * @v s_PXENV_FILE_EXEC::Command        Command to execute
00203  * @ret #PXENV_EXIT_SUCCESS             Command was executed successfully
00204  * @ret #PXENV_EXIT_FAILURE             Command was not executed successfully
00205  * @ret s_PXENV_FILE_EXEC::Status       PXE status code
00206  *
00207  */
00208 PXENV_EXIT_t pxenv_file_exec ( struct s_PXENV_FILE_EXEC *file_exec ) {
00209         userptr_t command;
00210         size_t command_len;
00211         int rc;
00212 
00213         DBG ( "PXENV_FILE_EXEC" );
00214 
00215         /* Copy name from external program, and exec it */
00216         command = real_to_user ( file_exec->Command.segment,
00217                                  file_exec->Command.offset );
00218         command_len = strlen_user ( command, 0 );
00219         {
00220                 char command_string[ command_len + 1 ];
00221 
00222                 copy_from_user ( command_string, command, 0,
00223                                  sizeof ( command_string ) );
00224                 DBG ( " %s", command_string );
00225 
00226                 if ( ( rc = system ( command_string ) ) != 0 ) {
00227                         file_exec->Status = PXENV_STATUS ( rc );
00228                         return PXENV_EXIT_FAILURE;
00229                 }
00230         }
00231 
00232         file_exec->Status = PXENV_STATUS_SUCCESS;
00233         return PXENV_EXIT_SUCCESS;
00234 }
00235 
00236 segoff_t __data16 ( pxe_exit_hook ) = { 0, 0 };
00237 #define pxe_exit_hook __use_data16 ( pxe_exit_hook )
00238 
00239 /**
00240  * FILE API CHECK
00241  *
00242  * @v file_exec                         Pointer to a struct s_PXENV_FILE_API_CHECK
00243  * @v s_PXENV_FILE_API_CHECK::Magic     Inbound magic number (0x91d447b2)
00244  * @ret #PXENV_EXIT_SUCCESS             Command was executed successfully
00245  * @ret #PXENV_EXIT_FAILURE             Command was not executed successfully
00246  * @ret s_PXENV_FILE_API_CHECK::Status  PXE status code
00247  * @ret s_PXENV_FILE_API_CHECK::Magic   Outbound magic number (0xe9c17b20)
00248  * @ret s_PXENV_FILE_API_CHECK::Provider "gPXE" (0x45585067)
00249  * @ret s_PXENV_FILE_API_CHECK::APIMask API function bitmask
00250  * @ret s_PXENV_FILE_API_CHECK::Flags   Reserved
00251  *
00252  */
00253 PXENV_EXIT_t pxenv_file_api_check ( struct s_PXENV_FILE_API_CHECK *file_api_check ) {
00254         DBG ( "PXENV_FILE_API_CHECK" );
00255 
00256         if ( file_api_check->Magic != 0x91d447b2 ) {
00257                 file_api_check->Status = PXENV_STATUS_BAD_FUNC;
00258                 return PXENV_EXIT_FAILURE;
00259         } else if ( file_api_check->Size <
00260                     sizeof(struct s_PXENV_FILE_API_CHECK) ) {
00261                 file_api_check->Status = PXENV_STATUS_OUT_OF_RESOURCES;
00262                 return PXENV_EXIT_FAILURE;
00263         } else {
00264                 file_api_check->Status   = PXENV_STATUS_SUCCESS;
00265                 file_api_check->Size     = sizeof(struct s_PXENV_FILE_API_CHECK);
00266                 file_api_check->Magic    = 0xe9c17b20;
00267                 file_api_check->Provider = 0x45585067; /* "gPXE" */
00268                 file_api_check->APIMask  = 0x0000007f; /* Functions e0-e6 */
00269                 /* Check to see if we have a PXE exit hook */
00270                 if ( pxe_exit_hook.segment | pxe_exit_hook.offset )
00271                         /* Function e7, also */
00272                         file_api_check->APIMask |= 0x00000080;
00273                 file_api_check->Flags    = 0;          /* None defined */
00274                 return PXENV_EXIT_SUCCESS;
00275         }
00276 }
00277 
00278 /**
00279  * FILE EXIT HOOK
00280  *
00281  * @v file_exit_hook                    Pointer to a struct
00282  *                                      s_PXENV_FILE_EXIT_HOOK
00283  * @v s_PXENV_FILE_EXIT_HOOK::Hook      SEG16:OFF16 to jump to
00284  * @ret #PXENV_EXIT_SUCCESS             Successfully set hook
00285  * @ret #PXENV_EXIT_FAILURE             We're not an NBP build
00286  * @ret s_PXENV_FILE_EXIT_HOOK::Status  PXE status code
00287  *
00288  */
00289 PXENV_EXIT_t pxenv_file_exit_hook ( struct s_PXENV_FILE_EXIT_HOOK
00290                                         *file_exit_hook ) {
00291         DBG ( "PXENV_FILE_EXIT_HOOK" );
00292 
00293         /* Check to see if we have a PXE exit hook */
00294         if ( pxe_exit_hook.segment | pxe_exit_hook.offset ) {
00295                 /* We'll jump to the specified SEG16:OFF16 during exit */
00296                 pxe_exit_hook.segment = file_exit_hook->Hook.segment;
00297                 pxe_exit_hook.offset = file_exit_hook->Hook.offset;
00298                 file_exit_hook->Status = PXENV_STATUS_SUCCESS;
00299                 return PXENV_EXIT_SUCCESS;
00300         }
00301 
00302         DBG ( " not NBP" );
00303         file_exit_hook->Status = PXENV_STATUS_UNSUPPORTED;
00304         return PXENV_EXIT_FAILURE;
00305 }
00306 

Generated on Tue Apr 6 20:00:50 2010 for gPXE by  doxygen 1.5.7.1