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 FILE_LICENCE ( GPL2_OR_LATER );
36 FEATURE ( FEATURE_MISC, "PXEXT", DHCP_EB_FEATURE_PXE_EXT, 2 );
41 * @v file_open Pointer to a struct s_PXENV_FILE_OPEN
42 * @v s_PXENV_FILE_OPEN::FileName URL of file to open
43 * @ret #PXENV_EXIT_SUCCESS File was opened
44 * @ret #PXENV_EXIT_FAILURE File was not opened
45 * @ret s_PXENV_FILE_OPEN::Status PXE status code
46 * @ret s_PXENV_FILE_OPEN::FileHandle Handle of opened file
49 PXENV_EXIT_t pxenv_file_open ( struct s_PXENV_FILE_OPEN *file_open ) {
54 DBG ( "PXENV_FILE_OPEN" );
56 /* Copy name from external program, and open it */
57 filename = real_to_user ( file_open->FileName.segment,
58 file_open->FileName.offset );
59 filename_len = strlen_user ( filename, 0 );
61 char uri_string[ filename_len + 1 ];
63 copy_from_user ( uri_string, filename, 0,
64 sizeof ( uri_string ) );
65 DBG ( " %s", uri_string );
66 fd = open ( uri_string );
70 file_open->Status = PXENV_STATUS ( fd );
71 return PXENV_EXIT_FAILURE;
74 DBG ( " as file %d", fd );
76 file_open->FileHandle = fd;
77 file_open->Status = PXENV_STATUS_SUCCESS;
78 return PXENV_EXIT_SUCCESS;
84 * @v file_close Pointer to a struct s_PXENV_FILE_CLOSE
85 * @v s_PXENV_FILE_CLOSE::FileHandle File handle
86 * @ret #PXENV_EXIT_SUCCESS File was closed
87 * @ret #PXENV_EXIT_FAILURE File was not closed
88 * @ret s_PXENV_FILE_CLOSE::Status PXE status code
91 PXENV_EXIT_t pxenv_file_close ( struct s_PXENV_FILE_CLOSE *file_close ) {
93 DBG ( "PXENV_FILE_CLOSE %d", file_close->FileHandle );
95 close ( file_close->FileHandle );
96 file_close->Status = PXENV_STATUS_SUCCESS;
97 return PXENV_EXIT_SUCCESS;
103 * @v file_select Pointer to a struct s_PXENV_FILE_SELECT
104 * @v s_PXENV_FILE_SELECT::FileHandle File handle
105 * @ret #PXENV_EXIT_SUCCESS File has been checked for readiness
106 * @ret #PXENV_EXIT_FAILURE File has not been checked for readiness
107 * @ret s_PXENV_FILE_SELECT::Status PXE status code
108 * @ret s_PXENV_FILE_SELECT::Ready Indication of readiness
111 PXENV_EXIT_t pxenv_file_select ( struct s_PXENV_FILE_SELECT *file_select ) {
115 DBG ( "PXENV_FILE_SELECT %d", file_select->FileHandle );
118 FD_SET ( file_select->FileHandle, &fdset );
119 if ( ( ready = select ( &fdset, 0 ) ) < 0 ) {
120 file_select->Status = PXENV_STATUS ( ready );
121 return PXENV_EXIT_FAILURE;
124 file_select->Ready = ( ready ? RDY_READ : 0 );
125 file_select->Status = PXENV_STATUS_SUCCESS;
126 return PXENV_EXIT_SUCCESS;
132 * @v file_read Pointer to a struct s_PXENV_FILE_READ
133 * @v s_PXENV_FILE_READ::FileHandle File handle
134 * @v s_PXENV_FILE_READ::BufferSize Size of data buffer
135 * @v s_PXENV_FILE_READ::Buffer Data buffer
136 * @ret #PXENV_EXIT_SUCCESS Data has been read from file
137 * @ret #PXENV_EXIT_FAILURE Data has not been read from file
138 * @ret s_PXENV_FILE_READ::Status PXE status code
139 * @ret s_PXENV_FILE_READ::Ready Indication of readiness
140 * @ret s_PXENV_FILE_READ::BufferSize Length of data read
143 PXENV_EXIT_t pxenv_file_read ( struct s_PXENV_FILE_READ *file_read ) {
147 DBG ( "PXENV_FILE_READ %d to %04x:%04x+%04x", file_read->FileHandle,
148 file_read->Buffer.segment, file_read->Buffer.offset,
149 file_read->BufferSize );
151 buffer = real_to_user ( file_read->Buffer.segment,
152 file_read->Buffer.offset );
153 if ( ( len = read_user ( file_read->FileHandle, buffer, 0,
154 file_read->BufferSize ) ) < 0 ) {
155 file_read->Status = PXENV_STATUS ( len );
156 return PXENV_EXIT_FAILURE;
159 DBG ( " read %04zx", ( ( size_t ) len ) );
161 file_read->BufferSize = len;
162 file_read->Status = PXENV_STATUS_SUCCESS;
163 return PXENV_EXIT_SUCCESS;
169 * @v get_file_size Pointer to a struct s_PXENV_GET_FILE_SIZE
170 * @v s_PXENV_GET_FILE_SIZE::FileHandle File handle
171 * @ret #PXENV_EXIT_SUCCESS File size has been determined
172 * @ret #PXENV_EXIT_FAILURE File size has not been determined
173 * @ret s_PXENV_GET_FILE_SIZE::Status PXE status code
174 * @ret s_PXENV_GET_FILE_SIZE::FileSize Size of file
176 PXENV_EXIT_t pxenv_get_file_size ( struct s_PXENV_GET_FILE_SIZE
180 DBG ( "PXENV_GET_FILE_SIZE %d", get_file_size->FileHandle );
182 filesize = fsize ( get_file_size->FileHandle );
183 if ( filesize < 0 ) {
184 get_file_size->Status = PXENV_STATUS ( filesize );
185 return PXENV_EXIT_FAILURE;
188 DBG ( " is %zd", ( ( size_t ) filesize ) );
190 get_file_size->FileSize = filesize;
191 get_file_size->Status = PXENV_STATUS_SUCCESS;
192 return PXENV_EXIT_SUCCESS;
198 * @v file_exec Pointer to a struct s_PXENV_FILE_EXEC
199 * @v s_PXENV_FILE_EXEC::Command Command to execute
200 * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
201 * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
202 * @ret s_PXENV_FILE_EXEC::Status PXE status code
205 PXENV_EXIT_t pxenv_file_exec ( struct s_PXENV_FILE_EXEC *file_exec ) {
210 DBG ( "PXENV_FILE_EXEC" );
212 /* Copy name from external program, and exec it */
213 command = real_to_user ( file_exec->Command.segment,
214 file_exec->Command.offset );
215 command_len = strlen_user ( command, 0 );
217 char command_string[ command_len + 1 ];
219 copy_from_user ( command_string, command, 0,
220 sizeof ( command_string ) );
221 DBG ( " %s", command_string );
223 if ( ( rc = system ( command_string ) ) != 0 ) {
224 file_exec->Status = PXENV_STATUS ( rc );
225 return PXENV_EXIT_FAILURE;
229 file_exec->Status = PXENV_STATUS_SUCCESS;
230 return PXENV_EXIT_SUCCESS;
236 * @v file_exec Pointer to a struct s_PXENV_FILE_API_CHECK
237 * @v s_PXENV_FILE_API_CHECK::Magic Inbound magic number (0x91d447b2)
238 * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
239 * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
240 * @ret s_PXENV_FILE_API_CHECK::Status PXE status code
241 * @ret s_PXENV_FILE_API_CHECK::Magic Outbound magic number (0xe9c17b20)
242 * @ret s_PXENV_FILE_API_CHECK::Provider "gPXE" (0x45585067)
243 * @ret s_PXENV_FILE_API_CHECK::APIMask API function bitmask
244 * @ret s_PXENV_FILE_API_CHECK::Flags Reserved
247 PXENV_EXIT_t pxenv_file_api_check ( struct s_PXENV_FILE_API_CHECK *file_api_check ) {
248 DBG ( "PXENV_FILE_API_CHECK" );
250 if ( file_api_check->Magic != 0x91d447b2 ) {
251 file_api_check->Status = PXENV_STATUS_BAD_FUNC;
252 return PXENV_EXIT_FAILURE;
253 } else if ( file_api_check->Size <
254 sizeof(struct s_PXENV_FILE_API_CHECK) ) {
255 file_api_check->Status = PXENV_STATUS_OUT_OF_RESOURCES;
256 return PXENV_EXIT_FAILURE;
258 file_api_check->Status = PXENV_STATUS_SUCCESS;
259 file_api_check->Size = sizeof(struct s_PXENV_FILE_API_CHECK);
260 file_api_check->Magic = 0xe9c17b20;
261 file_api_check->Provider = 0x45585067; /* "gPXE" */
262 file_api_check->APIMask = 0x0000007f; /* Functions e0-e6 */
263 file_api_check->Flags = 0; /* None defined */
264 return PXENV_EXIT_SUCCESS;