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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. FEATURE ( FEATURE_MISC, "PXEXT", DHCP_EB_FEATURE_PXE_EXT, 2 );
  32. /**
  33. * FILE OPEN
  34. *
  35. * @v file_open Pointer to a struct s_PXENV_FILE_OPEN
  36. * @v s_PXENV_FILE_OPEN::FileName URL of file to open
  37. * @ret #PXENV_EXIT_SUCCESS File was opened
  38. * @ret #PXENV_EXIT_FAILURE File was not opened
  39. * @ret s_PXENV_FILE_OPEN::Status PXE status code
  40. * @ret s_PXENV_FILE_OPEN::FileHandle Handle of opened file
  41. *
  42. */
  43. PXENV_EXIT_t pxenv_file_open ( struct s_PXENV_FILE_OPEN *file_open ) {
  44. userptr_t filename;
  45. size_t filename_len;
  46. int fd;
  47. DBG ( "PXENV_FILE_OPEN" );
  48. /* Copy name from external program, and open it */
  49. filename = real_to_user ( file_open->FileName.segment,
  50. file_open->FileName.offset );
  51. filename_len = strlen_user ( filename, 0 );
  52. {
  53. char uri_string[ filename_len + 1 ];
  54. copy_from_user ( uri_string, filename, 0,
  55. sizeof ( uri_string ) );
  56. DBG ( " %s", uri_string );
  57. fd = open ( uri_string );
  58. }
  59. if ( fd < 0 ) {
  60. file_open->Status = PXENV_STATUS ( fd );
  61. return PXENV_EXIT_FAILURE;
  62. }
  63. DBG ( " as file %d", fd );
  64. file_open->FileHandle = fd;
  65. file_open->Status = PXENV_STATUS_SUCCESS;
  66. return PXENV_EXIT_SUCCESS;
  67. }
  68. /**
  69. * FILE CLOSE
  70. *
  71. * @v file_close Pointer to a struct s_PXENV_FILE_CLOSE
  72. * @v s_PXENV_FILE_CLOSE::FileHandle File handle
  73. * @ret #PXENV_EXIT_SUCCESS File was closed
  74. * @ret #PXENV_EXIT_FAILURE File was not closed
  75. * @ret s_PXENV_FILE_CLOSE::Status PXE status code
  76. *
  77. */
  78. PXENV_EXIT_t pxenv_file_close ( struct s_PXENV_FILE_CLOSE *file_close ) {
  79. DBG ( "PXENV_FILE_CLOSE %d", file_close->FileHandle );
  80. close ( file_close->FileHandle );
  81. file_close->Status = PXENV_STATUS_SUCCESS;
  82. return PXENV_EXIT_SUCCESS;
  83. }
  84. /**
  85. * FILE SELECT
  86. *
  87. * @v file_select Pointer to a struct s_PXENV_FILE_SELECT
  88. * @v s_PXENV_FILE_SELECT::FileHandle File handle
  89. * @ret #PXENV_EXIT_SUCCESS File has been checked for readiness
  90. * @ret #PXENV_EXIT_FAILURE File has not been checked for readiness
  91. * @ret s_PXENV_FILE_SELECT::Status PXE status code
  92. * @ret s_PXENV_FILE_SELECT::Ready Indication of readiness
  93. *
  94. */
  95. PXENV_EXIT_t pxenv_file_select ( struct s_PXENV_FILE_SELECT *file_select ) {
  96. fd_set fdset;
  97. int ready;
  98. DBG ( "PXENV_FILE_SELECT %d", file_select->FileHandle );
  99. FD_ZERO ( &fdset );
  100. FD_SET ( file_select->FileHandle, &fdset );
  101. if ( ( ready = select ( &fdset, 0 ) ) < 0 ) {
  102. file_select->Status = PXENV_STATUS ( ready );
  103. return PXENV_EXIT_FAILURE;
  104. }
  105. file_select->Ready = ( ready ? RDY_READ : 0 );
  106. file_select->Status = PXENV_STATUS_SUCCESS;
  107. return PXENV_EXIT_SUCCESS;
  108. }
  109. /**
  110. * FILE READ
  111. *
  112. * @v file_read Pointer to a struct s_PXENV_FILE_READ
  113. * @v s_PXENV_FILE_READ::FileHandle File handle
  114. * @v s_PXENV_FILE_READ::BufferSize Size of data buffer
  115. * @v s_PXENV_FILE_READ::Buffer Data buffer
  116. * @ret #PXENV_EXIT_SUCCESS Data has been read from file
  117. * @ret #PXENV_EXIT_FAILURE Data has not been read from file
  118. * @ret s_PXENV_FILE_READ::Status PXE status code
  119. * @ret s_PXENV_FILE_READ::Ready Indication of readiness
  120. * @ret s_PXENV_FILE_READ::BufferSize Length of data read
  121. *
  122. */
  123. PXENV_EXIT_t pxenv_file_read ( struct s_PXENV_FILE_READ *file_read ) {
  124. userptr_t buffer;
  125. ssize_t len;
  126. DBG ( "PXENV_FILE_READ %d to %04x:%04x+%04x", file_read->FileHandle,
  127. file_read->Buffer.segment, file_read->Buffer.offset,
  128. file_read->BufferSize );
  129. buffer = real_to_user ( file_read->Buffer.segment,
  130. file_read->Buffer.offset );
  131. if ( ( len = read_user ( file_read->FileHandle, buffer, 0,
  132. file_read->BufferSize ) ) < 0 ) {
  133. file_read->Status = PXENV_STATUS ( len );
  134. return PXENV_EXIT_FAILURE;
  135. }
  136. DBG ( " read %04zx", ( ( size_t ) len ) );
  137. file_read->BufferSize = len;
  138. file_read->Status = PXENV_STATUS_SUCCESS;
  139. return PXENV_EXIT_SUCCESS;
  140. }
  141. /**
  142. * GET FILE SIZE
  143. *
  144. * @v get_file_size Pointer to a struct s_PXENV_GET_FILE_SIZE
  145. * @v s_PXENV_GET_FILE_SIZE::FileHandle File handle
  146. * @ret #PXENV_EXIT_SUCCESS File size has been determined
  147. * @ret #PXENV_EXIT_FAILURE File size has not been determined
  148. * @ret s_PXENV_GET_FILE_SIZE::Status PXE status code
  149. * @ret s_PXENV_GET_FILE_SIZE::FileSize Size of file
  150. */
  151. PXENV_EXIT_t pxenv_get_file_size ( struct s_PXENV_GET_FILE_SIZE
  152. *get_file_size ) {
  153. ssize_t filesize;
  154. DBG ( "PXENV_GET_FILE_SIZE %d", get_file_size->FileHandle );
  155. filesize = fsize ( get_file_size->FileHandle );
  156. if ( filesize < 0 ) {
  157. get_file_size->Status = PXENV_STATUS ( filesize );
  158. return PXENV_EXIT_FAILURE;
  159. }
  160. DBG ( " is %zd", ( ( size_t ) filesize ) );
  161. get_file_size->FileSize = filesize;
  162. get_file_size->Status = PXENV_STATUS_SUCCESS;
  163. return PXENV_EXIT_SUCCESS;
  164. }
  165. /**
  166. * FILE EXEC
  167. *
  168. * @v file_exec Pointer to a struct s_PXENV_FILE_EXEC
  169. * @v s_PXENV_FILE_EXEC::Command Command to execute
  170. * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
  171. * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
  172. * @ret s_PXENV_FILE_EXEC::Status PXE status code
  173. *
  174. */
  175. PXENV_EXIT_t pxenv_file_exec ( struct s_PXENV_FILE_EXEC *file_exec ) {
  176. userptr_t command;
  177. size_t command_len;
  178. int rc;
  179. DBG ( "PXENV_FILE_EXEC" );
  180. /* Copy name from external program, and exec it */
  181. command = real_to_user ( file_exec->Command.segment,
  182. file_exec->Command.offset );
  183. command_len = strlen_user ( command, 0 );
  184. {
  185. char command_string[ command_len + 1 ];
  186. copy_from_user ( command_string, command, 0,
  187. sizeof ( command_string ) );
  188. DBG ( " %s", command_string );
  189. if ( ( rc = system ( command_string ) ) != 0 ) {
  190. file_exec->Status = PXENV_STATUS ( rc );
  191. return PXENV_EXIT_FAILURE;
  192. }
  193. }
  194. file_exec->Status = PXENV_STATUS_SUCCESS;
  195. return PXENV_EXIT_SUCCESS;
  196. }
  197. /**
  198. * FILE API CHECK
  199. *
  200. * @v file_exec Pointer to a struct s_PXENV_FILE_API_CHECK
  201. * @v s_PXENV_FILE_API_CHECK::Magic Inbound magic number (0x91d447b2)
  202. * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
  203. * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
  204. * @ret s_PXENV_FILE_API_CHECK::Status PXE status code
  205. * @ret s_PXENV_FILE_API_CHECK::Magic Outbound magic number (0xe9c17b20)
  206. * @ret s_PXENV_FILE_API_CHECK::Provider "gPXE" (0x45585067)
  207. * @ret s_PXENV_FILE_API_CHECK::APIMask API function bitmask
  208. * @ret s_PXENV_FILE_API_CHECK::Flags Reserved
  209. *
  210. */
  211. PXENV_EXIT_t pxenv_file_api_check ( struct s_PXENV_FILE_API_CHECK *file_api_check ) {
  212. DBG ( "PXENV_FILE_API_CHECK" );
  213. if ( file_api_check->Magic != 0x91d447b2 ) {
  214. file_api_check->Status = PXENV_STATUS_BAD_FUNC;
  215. return PXENV_EXIT_FAILURE;
  216. } else if ( file_api_check->Size <
  217. sizeof(struct s_PXENV_FILE_API_CHECK) ) {
  218. file_api_check->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  219. return PXENV_EXIT_FAILURE;
  220. } else {
  221. file_api_check->Status = PXENV_STATUS_SUCCESS;
  222. file_api_check->Size = sizeof(struct s_PXENV_FILE_API_CHECK);
  223. file_api_check->Magic = 0xe9c17b20;
  224. file_api_check->Provider = 0x45585067; /* "gPXE" */
  225. file_api_check->APIMask = 0x0000007f; /* Functions e0-e6 */
  226. file_api_check->Flags = 0; /* None defined */
  227. return PXENV_EXIT_SUCCESS;
  228. }
  229. }