You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pxe_file.c 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /** @file
  2. *
  3. * PXE FILE API
  4. *
  5. */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <errno.h>
  9. #include <byteswap.h>
  10. #include <gpxe/uaccess.h>
  11. #include <gpxe/posix_io.h>
  12. #include <gpxe/features.h>
  13. #include <pxe.h>
  14. /*
  15. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of the
  20. * License, or any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful, but
  23. * WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30. */
  31. FILE_LICENCE ( GPL2_OR_LATER );
  32. FEATURE ( FEATURE_MISC, "PXEXT", DHCP_EB_FEATURE_PXE_EXT, 2 );
  33. /**
  34. * FILE OPEN
  35. *
  36. * @v file_open Pointer to a struct s_PXENV_FILE_OPEN
  37. * @v s_PXENV_FILE_OPEN::FileName URL of file to open
  38. * @ret #PXENV_EXIT_SUCCESS File was opened
  39. * @ret #PXENV_EXIT_FAILURE File was not opened
  40. * @ret s_PXENV_FILE_OPEN::Status PXE status code
  41. * @ret s_PXENV_FILE_OPEN::FileHandle Handle of opened file
  42. *
  43. */
  44. PXENV_EXIT_t pxenv_file_open ( struct s_PXENV_FILE_OPEN *file_open ) {
  45. userptr_t filename;
  46. size_t filename_len;
  47. int fd;
  48. DBG ( "PXENV_FILE_OPEN" );
  49. /* Copy name from external program, and open it */
  50. filename = real_to_user ( file_open->FileName.segment,
  51. file_open->FileName.offset );
  52. filename_len = strlen_user ( filename, 0 );
  53. {
  54. char uri_string[ filename_len + 1 ];
  55. copy_from_user ( uri_string, filename, 0,
  56. sizeof ( uri_string ) );
  57. DBG ( " %s", uri_string );
  58. fd = open ( uri_string );
  59. }
  60. if ( fd < 0 ) {
  61. file_open->Status = PXENV_STATUS ( fd );
  62. return PXENV_EXIT_FAILURE;
  63. }
  64. DBG ( " as file %d", fd );
  65. file_open->FileHandle = fd;
  66. file_open->Status = PXENV_STATUS_SUCCESS;
  67. return PXENV_EXIT_SUCCESS;
  68. }
  69. /**
  70. * FILE CLOSE
  71. *
  72. * @v file_close Pointer to a struct s_PXENV_FILE_CLOSE
  73. * @v s_PXENV_FILE_CLOSE::FileHandle File handle
  74. * @ret #PXENV_EXIT_SUCCESS File was closed
  75. * @ret #PXENV_EXIT_FAILURE File was not closed
  76. * @ret s_PXENV_FILE_CLOSE::Status PXE status code
  77. *
  78. */
  79. PXENV_EXIT_t pxenv_file_close ( struct s_PXENV_FILE_CLOSE *file_close ) {
  80. DBG ( "PXENV_FILE_CLOSE %d", file_close->FileHandle );
  81. close ( file_close->FileHandle );
  82. file_close->Status = PXENV_STATUS_SUCCESS;
  83. return PXENV_EXIT_SUCCESS;
  84. }
  85. /**
  86. * FILE SELECT
  87. *
  88. * @v file_select Pointer to a struct s_PXENV_FILE_SELECT
  89. * @v s_PXENV_FILE_SELECT::FileHandle File handle
  90. * @ret #PXENV_EXIT_SUCCESS File has been checked for readiness
  91. * @ret #PXENV_EXIT_FAILURE File has not been checked for readiness
  92. * @ret s_PXENV_FILE_SELECT::Status PXE status code
  93. * @ret s_PXENV_FILE_SELECT::Ready Indication of readiness
  94. *
  95. */
  96. PXENV_EXIT_t pxenv_file_select ( struct s_PXENV_FILE_SELECT *file_select ) {
  97. fd_set fdset;
  98. int ready;
  99. DBG ( "PXENV_FILE_SELECT %d", file_select->FileHandle );
  100. FD_ZERO ( &fdset );
  101. FD_SET ( file_select->FileHandle, &fdset );
  102. if ( ( ready = select ( &fdset, 0 ) ) < 0 ) {
  103. file_select->Status = PXENV_STATUS ( ready );
  104. return PXENV_EXIT_FAILURE;
  105. }
  106. file_select->Ready = ( ready ? RDY_READ : 0 );
  107. file_select->Status = PXENV_STATUS_SUCCESS;
  108. return PXENV_EXIT_SUCCESS;
  109. }
  110. /**
  111. * FILE READ
  112. *
  113. * @v file_read Pointer to a struct s_PXENV_FILE_READ
  114. * @v s_PXENV_FILE_READ::FileHandle File handle
  115. * @v s_PXENV_FILE_READ::BufferSize Size of data buffer
  116. * @v s_PXENV_FILE_READ::Buffer Data buffer
  117. * @ret #PXENV_EXIT_SUCCESS Data has been read from file
  118. * @ret #PXENV_EXIT_FAILURE Data has not been read from file
  119. * @ret s_PXENV_FILE_READ::Status PXE status code
  120. * @ret s_PXENV_FILE_READ::Ready Indication of readiness
  121. * @ret s_PXENV_FILE_READ::BufferSize Length of data read
  122. *
  123. */
  124. PXENV_EXIT_t pxenv_file_read ( struct s_PXENV_FILE_READ *file_read ) {
  125. userptr_t buffer;
  126. ssize_t len;
  127. DBG ( "PXENV_FILE_READ %d to %04x:%04x+%04x", file_read->FileHandle,
  128. file_read->Buffer.segment, file_read->Buffer.offset,
  129. file_read->BufferSize );
  130. buffer = real_to_user ( file_read->Buffer.segment,
  131. file_read->Buffer.offset );
  132. if ( ( len = read_user ( file_read->FileHandle, buffer, 0,
  133. file_read->BufferSize ) ) < 0 ) {
  134. file_read->Status = PXENV_STATUS ( len );
  135. return PXENV_EXIT_FAILURE;
  136. }
  137. DBG ( " read %04zx", ( ( size_t ) len ) );
  138. file_read->BufferSize = len;
  139. file_read->Status = PXENV_STATUS_SUCCESS;
  140. return PXENV_EXIT_SUCCESS;
  141. }
  142. /**
  143. * GET FILE SIZE
  144. *
  145. * @v get_file_size Pointer to a struct s_PXENV_GET_FILE_SIZE
  146. * @v s_PXENV_GET_FILE_SIZE::FileHandle File handle
  147. * @ret #PXENV_EXIT_SUCCESS File size has been determined
  148. * @ret #PXENV_EXIT_FAILURE File size has not been determined
  149. * @ret s_PXENV_GET_FILE_SIZE::Status PXE status code
  150. * @ret s_PXENV_GET_FILE_SIZE::FileSize Size of file
  151. */
  152. PXENV_EXIT_t pxenv_get_file_size ( struct s_PXENV_GET_FILE_SIZE
  153. *get_file_size ) {
  154. ssize_t filesize;
  155. DBG ( "PXENV_GET_FILE_SIZE %d", get_file_size->FileHandle );
  156. filesize = fsize ( get_file_size->FileHandle );
  157. if ( filesize < 0 ) {
  158. get_file_size->Status = PXENV_STATUS ( filesize );
  159. return PXENV_EXIT_FAILURE;
  160. }
  161. DBG ( " is %zd", ( ( size_t ) filesize ) );
  162. get_file_size->FileSize = filesize;
  163. get_file_size->Status = PXENV_STATUS_SUCCESS;
  164. return PXENV_EXIT_SUCCESS;
  165. }
  166. /**
  167. * FILE EXEC
  168. *
  169. * @v file_exec Pointer to a struct s_PXENV_FILE_EXEC
  170. * @v s_PXENV_FILE_EXEC::Command Command to execute
  171. * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
  172. * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
  173. * @ret s_PXENV_FILE_EXEC::Status PXE status code
  174. *
  175. */
  176. PXENV_EXIT_t pxenv_file_exec ( struct s_PXENV_FILE_EXEC *file_exec ) {
  177. userptr_t command;
  178. size_t command_len;
  179. int rc;
  180. DBG ( "PXENV_FILE_EXEC" );
  181. /* Copy name from external program, and exec it */
  182. command = real_to_user ( file_exec->Command.segment,
  183. file_exec->Command.offset );
  184. command_len = strlen_user ( command, 0 );
  185. {
  186. char command_string[ command_len + 1 ];
  187. copy_from_user ( command_string, command, 0,
  188. sizeof ( command_string ) );
  189. DBG ( " %s", command_string );
  190. if ( ( rc = system ( command_string ) ) != 0 ) {
  191. file_exec->Status = PXENV_STATUS ( rc );
  192. return PXENV_EXIT_FAILURE;
  193. }
  194. }
  195. file_exec->Status = PXENV_STATUS_SUCCESS;
  196. return PXENV_EXIT_SUCCESS;
  197. }
  198. /**
  199. * FILE API CHECK
  200. *
  201. * @v file_exec Pointer to a struct s_PXENV_FILE_API_CHECK
  202. * @v s_PXENV_FILE_API_CHECK::Magic Inbound magic number (0x91d447b2)
  203. * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
  204. * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
  205. * @ret s_PXENV_FILE_API_CHECK::Status PXE status code
  206. * @ret s_PXENV_FILE_API_CHECK::Magic Outbound magic number (0xe9c17b20)
  207. * @ret s_PXENV_FILE_API_CHECK::Provider "gPXE" (0x45585067)
  208. * @ret s_PXENV_FILE_API_CHECK::APIMask API function bitmask
  209. * @ret s_PXENV_FILE_API_CHECK::Flags Reserved
  210. *
  211. */
  212. PXENV_EXIT_t pxenv_file_api_check ( struct s_PXENV_FILE_API_CHECK *file_api_check ) {
  213. DBG ( "PXENV_FILE_API_CHECK" );
  214. if ( file_api_check->Magic != 0x91d447b2 ) {
  215. file_api_check->Status = PXENV_STATUS_BAD_FUNC;
  216. return PXENV_EXIT_FAILURE;
  217. } else if ( file_api_check->Size <
  218. sizeof(struct s_PXENV_FILE_API_CHECK) ) {
  219. file_api_check->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  220. return PXENV_EXIT_FAILURE;
  221. } else {
  222. file_api_check->Status = PXENV_STATUS_SUCCESS;
  223. file_api_check->Size = sizeof(struct s_PXENV_FILE_API_CHECK);
  224. file_api_check->Magic = 0xe9c17b20;
  225. file_api_check->Provider = 0x45585067; /* "gPXE" */
  226. file_api_check->APIMask = 0x0000007f; /* Functions e0-e6 */
  227. file_api_check->Flags = 0; /* None defined */
  228. return PXENV_EXIT_SUCCESS;
  229. }
  230. }