11 #include <gpxe/uaccess.h>
12 #include <gpxe/posix_io.h>
13 #include <gpxe/features.h>
17 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License as
21 * published by the Free Software Foundation; either version 2 of the
22 * License, or any later version.
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 FEATURE ( FEATURE_MISC, "PXEXT", DHCP_EB_FEATURE_PXE_EXT, 2 );
39 * @v file_open Pointer to a struct s_PXENV_FILE_OPEN
40 * @v s_PXENV_FILE_OPEN::FileName URL of file to open
41 * @ret #PXENV_EXIT_SUCCESS File was opened
42 * @ret #PXENV_EXIT_FAILURE File was not opened
43 * @ret s_PXENV_FILE_OPEN::Status PXE status code
44 * @ret s_PXENV_FILE_OPEN::FileHandle Handle of opened file
47 PXENV_EXIT_t pxenv_file_open ( struct s_PXENV_FILE_OPEN *file_open ) {
52 DBG ( "PXENV_FILE_OPEN" );
54 /* Copy name from external program, and open it */
55 filename = real_to_user ( file_open->FileName.segment,
56 file_open->FileName.offset );
57 filename_len = strlen_user ( filename, 0 );
59 char uri_string[ filename_len + 1 ];
61 copy_from_user ( uri_string, filename, 0,
62 sizeof ( uri_string ) );
63 DBG ( " %s", uri_string );
64 fd = open ( uri_string );
68 file_open->Status = PXENV_STATUS ( fd );
69 return PXENV_EXIT_FAILURE;
72 DBG ( " as file %d", fd );
74 file_open->FileHandle = fd;
75 file_open->Status = PXENV_STATUS_SUCCESS;
76 return PXENV_EXIT_SUCCESS;
82 * @v file_close Pointer to a struct s_PXENV_FILE_CLOSE
83 * @v s_PXENV_FILE_CLOSE::FileHandle File handle
84 * @ret #PXENV_EXIT_SUCCESS File was closed
85 * @ret #PXENV_EXIT_FAILURE File was not closed
86 * @ret s_PXENV_FILE_CLOSE::Status PXE status code
89 PXENV_EXIT_t pxenv_file_close ( struct s_PXENV_FILE_CLOSE *file_close ) {
91 DBG ( "PXENV_FILE_CLOSE %d", file_close->FileHandle );
93 close ( file_close->FileHandle );
94 file_close->Status = PXENV_STATUS_SUCCESS;
95 return PXENV_EXIT_SUCCESS;
101 * @v file_select Pointer to a struct s_PXENV_FILE_SELECT
102 * @v s_PXENV_FILE_SELECT::FileHandle File handle
103 * @ret #PXENV_EXIT_SUCCESS File has been checked for readiness
104 * @ret #PXENV_EXIT_FAILURE File has not been checked for readiness
105 * @ret s_PXENV_FILE_SELECT::Status PXE status code
106 * @ret s_PXENV_FILE_SELECT::Ready Indication of readiness
109 PXENV_EXIT_t pxenv_file_select ( struct s_PXENV_FILE_SELECT *file_select ) {
113 DBG ( "PXENV_FILE_SELECT %d", file_select->FileHandle );
116 FD_SET ( file_select->FileHandle, &fdset );
117 if ( ( ready = select ( &fdset, 0 ) ) < 0 ) {
118 file_select->Status = PXENV_STATUS ( ready );
119 return PXENV_EXIT_FAILURE;
122 file_select->Ready = ( ready ? RDY_READ : 0 );
123 file_select->Status = PXENV_STATUS_SUCCESS;
124 return PXENV_EXIT_SUCCESS;
130 * @v file_read Pointer to a struct s_PXENV_FILE_READ
131 * @v s_PXENV_FILE_READ::FileHandle File handle
132 * @v s_PXENV_FILE_READ::BufferSize Size of data buffer
133 * @v s_PXENV_FILE_READ::Buffer Data buffer
134 * @ret #PXENV_EXIT_SUCCESS Data has been read from file
135 * @ret #PXENV_EXIT_FAILURE Data has not been read from file
136 * @ret s_PXENV_FILE_READ::Status PXE status code
137 * @ret s_PXENV_FILE_READ::Ready Indication of readiness
138 * @ret s_PXENV_FILE_READ::BufferSize Length of data read
141 PXENV_EXIT_t pxenv_file_read ( struct s_PXENV_FILE_READ *file_read ) {
145 DBG ( "PXENV_FILE_READ %d to %04x:%04x+%04x", file_read->FileHandle,
146 file_read->Buffer.segment, file_read->Buffer.offset,
147 file_read->BufferSize );
149 buffer = real_to_user ( file_read->Buffer.segment,
150 file_read->Buffer.offset );
151 if ( ( len = read_user ( file_read->FileHandle, buffer, 0,
152 file_read->BufferSize ) ) < 0 ) {
153 file_read->Status = PXENV_STATUS ( len );
154 return PXENV_EXIT_FAILURE;
157 DBG ( " read %04zx", ( ( size_t ) len ) );
159 file_read->BufferSize = len;
160 file_read->Status = PXENV_STATUS_SUCCESS;
161 return PXENV_EXIT_SUCCESS;
167 * @v get_file_size Pointer to a struct s_PXENV_GET_FILE_SIZE
168 * @v s_PXENV_GET_FILE_SIZE::FileHandle File handle
169 * @ret #PXENV_EXIT_SUCCESS File size has been determined
170 * @ret #PXENV_EXIT_FAILURE File size has not been determined
171 * @ret s_PXENV_GET_FILE_SIZE::Status PXE status code
172 * @ret s_PXENV_GET_FILE_SIZE::FileSize Size of file
174 PXENV_EXIT_t pxenv_get_file_size ( struct s_PXENV_GET_FILE_SIZE
178 DBG ( "PXENV_GET_FILE_SIZE %d", get_file_size->FileHandle );
180 filesize = fsize ( get_file_size->FileHandle );
181 if ( filesize < 0 ) {
182 get_file_size->Status = PXENV_STATUS ( filesize );
183 return PXENV_EXIT_FAILURE;
186 DBG ( " is %zd", ( ( size_t ) filesize ) );
188 get_file_size->FileSize = filesize;
189 get_file_size->Status = PXENV_STATUS_SUCCESS;
190 return PXENV_EXIT_SUCCESS;
196 * @v file_exec Pointer to a struct s_PXENV_FILE_EXEC
197 * @v s_PXENV_FILE_EXEC::Command Command to execute
198 * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
199 * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
200 * @ret s_PXENV_FILE_EXEC::Status PXE status code
203 PXENV_EXIT_t pxenv_file_exec ( struct s_PXENV_FILE_EXEC *file_exec ) {
208 DBG ( "PXENV_FILE_EXEC" );
210 /* Copy name from external program, and exec it */
211 command = real_to_user ( file_exec->Command.segment,
212 file_exec->Command.offset );
213 command_len = strlen_user ( command, 0 );
215 char command_string[ command_len + 1 ];
217 copy_from_user ( command_string, command, 0,
218 sizeof ( command_string ) );
219 DBG ( " %s", command_string );
221 if ( ( rc = system ( command_string ) ) != 0 ) {
222 file_exec->Status = PXENV_STATUS ( rc );
223 return PXENV_EXIT_FAILURE;
227 file_exec->Status = PXENV_STATUS_SUCCESS;
228 return PXENV_EXIT_SUCCESS;