Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

pxe_file.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 <ipxe/uaccess.h>
  11. #include <ipxe/posix_io.h>
  12. #include <ipxe/features.h>
  13. #include <pxe.h>
  14. #include <realmode.h>
  15. /*
  16. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License as
  20. * published by the Free Software Foundation; either version 2 of the
  21. * License, or any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful, but
  24. * WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  31. * 02110-1301, USA.
  32. *
  33. * You can also choose to distribute this program under the terms of
  34. * the Unmodified Binary Distribution Licence (as given in the file
  35. * COPYING.UBDL), provided that you have satisfied its requirements.
  36. */
  37. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  38. FEATURE ( FEATURE_MISC, "PXEXT", DHCP_EB_FEATURE_PXE_EXT, 2 );
  39. /**
  40. * FILE OPEN
  41. *
  42. * @v file_open Pointer to a struct s_PXENV_FILE_OPEN
  43. * @v s_PXENV_FILE_OPEN::FileName URL of file to open
  44. * @ret #PXENV_EXIT_SUCCESS File was opened
  45. * @ret #PXENV_EXIT_FAILURE File was not opened
  46. * @ret s_PXENV_FILE_OPEN::Status PXE status code
  47. * @ret s_PXENV_FILE_OPEN::FileHandle Handle of opened file
  48. *
  49. */
  50. static PXENV_EXIT_t pxenv_file_open ( struct s_PXENV_FILE_OPEN *file_open ) {
  51. userptr_t filename;
  52. size_t filename_len;
  53. int fd;
  54. DBG ( "PXENV_FILE_OPEN" );
  55. /* Copy name from external program, and open it */
  56. filename = real_to_user ( file_open->FileName.segment,
  57. file_open->FileName.offset );
  58. filename_len = strlen_user ( filename, 0 );
  59. {
  60. 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 );
  65. }
  66. if ( fd < 0 ) {
  67. file_open->Status = PXENV_STATUS ( fd );
  68. return PXENV_EXIT_FAILURE;
  69. }
  70. DBG ( " as file %d", fd );
  71. file_open->FileHandle = fd;
  72. file_open->Status = PXENV_STATUS_SUCCESS;
  73. return PXENV_EXIT_SUCCESS;
  74. }
  75. /**
  76. * FILE CLOSE
  77. *
  78. * @v file_close Pointer to a struct s_PXENV_FILE_CLOSE
  79. * @v s_PXENV_FILE_CLOSE::FileHandle File handle
  80. * @ret #PXENV_EXIT_SUCCESS File was closed
  81. * @ret #PXENV_EXIT_FAILURE File was not closed
  82. * @ret s_PXENV_FILE_CLOSE::Status PXE status code
  83. *
  84. */
  85. static PXENV_EXIT_t pxenv_file_close ( struct s_PXENV_FILE_CLOSE *file_close ) {
  86. DBG ( "PXENV_FILE_CLOSE %d", file_close->FileHandle );
  87. close ( file_close->FileHandle );
  88. file_close->Status = PXENV_STATUS_SUCCESS;
  89. return PXENV_EXIT_SUCCESS;
  90. }
  91. /**
  92. * FILE SELECT
  93. *
  94. * @v file_select Pointer to a struct s_PXENV_FILE_SELECT
  95. * @v s_PXENV_FILE_SELECT::FileHandle File handle
  96. * @ret #PXENV_EXIT_SUCCESS File has been checked for readiness
  97. * @ret #PXENV_EXIT_FAILURE File has not been checked for readiness
  98. * @ret s_PXENV_FILE_SELECT::Status PXE status code
  99. * @ret s_PXENV_FILE_SELECT::Ready Indication of readiness
  100. *
  101. */
  102. static PXENV_EXIT_t
  103. pxenv_file_select ( struct s_PXENV_FILE_SELECT *file_select ) {
  104. fd_set fdset;
  105. int ready;
  106. DBG ( "PXENV_FILE_SELECT %d", file_select->FileHandle );
  107. FD_ZERO ( &fdset );
  108. FD_SET ( file_select->FileHandle, &fdset );
  109. if ( ( ready = select ( &fdset, 0 ) ) < 0 ) {
  110. file_select->Status = PXENV_STATUS ( ready );
  111. return PXENV_EXIT_FAILURE;
  112. }
  113. file_select->Ready = ( ready ? RDY_READ : 0 );
  114. file_select->Status = PXENV_STATUS_SUCCESS;
  115. return PXENV_EXIT_SUCCESS;
  116. }
  117. /**
  118. * FILE READ
  119. *
  120. * @v file_read Pointer to a struct s_PXENV_FILE_READ
  121. * @v s_PXENV_FILE_READ::FileHandle File handle
  122. * @v s_PXENV_FILE_READ::BufferSize Size of data buffer
  123. * @v s_PXENV_FILE_READ::Buffer Data buffer
  124. * @ret #PXENV_EXIT_SUCCESS Data has been read from file
  125. * @ret #PXENV_EXIT_FAILURE Data has not been read from file
  126. * @ret s_PXENV_FILE_READ::Status PXE status code
  127. * @ret s_PXENV_FILE_READ::Ready Indication of readiness
  128. * @ret s_PXENV_FILE_READ::BufferSize Length of data read
  129. *
  130. */
  131. static PXENV_EXIT_t pxenv_file_read ( struct s_PXENV_FILE_READ *file_read ) {
  132. userptr_t buffer;
  133. ssize_t len;
  134. DBG ( "PXENV_FILE_READ %d to %04x:%04x+%04x", file_read->FileHandle,
  135. file_read->Buffer.segment, file_read->Buffer.offset,
  136. file_read->BufferSize );
  137. buffer = real_to_user ( file_read->Buffer.segment,
  138. file_read->Buffer.offset );
  139. if ( ( len = read_user ( file_read->FileHandle, buffer, 0,
  140. file_read->BufferSize ) ) < 0 ) {
  141. file_read->Status = PXENV_STATUS ( len );
  142. return PXENV_EXIT_FAILURE;
  143. }
  144. DBG ( " read %04zx", ( ( size_t ) len ) );
  145. file_read->BufferSize = len;
  146. file_read->Status = PXENV_STATUS_SUCCESS;
  147. return PXENV_EXIT_SUCCESS;
  148. }
  149. /**
  150. * GET FILE SIZE
  151. *
  152. * @v get_file_size Pointer to a struct s_PXENV_GET_FILE_SIZE
  153. * @v s_PXENV_GET_FILE_SIZE::FileHandle File handle
  154. * @ret #PXENV_EXIT_SUCCESS File size has been determined
  155. * @ret #PXENV_EXIT_FAILURE File size has not been determined
  156. * @ret s_PXENV_GET_FILE_SIZE::Status PXE status code
  157. * @ret s_PXENV_GET_FILE_SIZE::FileSize Size of file
  158. */
  159. static PXENV_EXIT_t
  160. pxenv_get_file_size ( struct s_PXENV_GET_FILE_SIZE *get_file_size ) {
  161. ssize_t filesize;
  162. DBG ( "PXENV_GET_FILE_SIZE %d", get_file_size->FileHandle );
  163. filesize = fsize ( get_file_size->FileHandle );
  164. if ( filesize < 0 ) {
  165. get_file_size->Status = PXENV_STATUS ( filesize );
  166. return PXENV_EXIT_FAILURE;
  167. }
  168. DBG ( " is %zd", ( ( size_t ) filesize ) );
  169. get_file_size->FileSize = filesize;
  170. get_file_size->Status = PXENV_STATUS_SUCCESS;
  171. return PXENV_EXIT_SUCCESS;
  172. }
  173. /**
  174. * FILE EXEC
  175. *
  176. * @v file_exec Pointer to a struct s_PXENV_FILE_EXEC
  177. * @v s_PXENV_FILE_EXEC::Command Command to execute
  178. * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
  179. * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
  180. * @ret s_PXENV_FILE_EXEC::Status PXE status code
  181. *
  182. */
  183. static PXENV_EXIT_t pxenv_file_exec ( struct s_PXENV_FILE_EXEC *file_exec ) {
  184. userptr_t command;
  185. size_t command_len;
  186. int rc;
  187. DBG ( "PXENV_FILE_EXEC" );
  188. /* Copy name from external program, and exec it */
  189. command = real_to_user ( file_exec->Command.segment,
  190. file_exec->Command.offset );
  191. command_len = strlen_user ( command, 0 );
  192. {
  193. char command_string[ command_len + 1 ];
  194. copy_from_user ( command_string, command, 0,
  195. sizeof ( command_string ) );
  196. DBG ( " %s", command_string );
  197. if ( ( rc = system ( command_string ) ) != 0 ) {
  198. file_exec->Status = PXENV_STATUS ( rc );
  199. return PXENV_EXIT_FAILURE;
  200. }
  201. }
  202. file_exec->Status = PXENV_STATUS_SUCCESS;
  203. return PXENV_EXIT_SUCCESS;
  204. }
  205. /**
  206. * FILE CMDLINE
  207. *
  208. * @v file_cmdline Pointer to a struct s_PXENV_FILE_CMDLINE
  209. * @v s_PXENV_FILE_CMDLINE::Buffer Buffer to contain command line
  210. * @v s_PXENV_FILE_CMDLINE::BufferSize Size of buffer
  211. * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
  212. * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
  213. * @ret s_PXENV_FILE_EXEC::Status PXE status code
  214. * @ret s_PXENV_FILE_EXEC::BufferSize Length of command line (including NUL)
  215. *
  216. */
  217. static PXENV_EXIT_t
  218. pxenv_file_cmdline ( struct s_PXENV_FILE_CMDLINE *file_cmdline ) {
  219. userptr_t buffer;
  220. size_t max_len;
  221. size_t len;
  222. DBG ( "PXENV_FILE_CMDLINE to %04x:%04x+%04x \"%s\"\n",
  223. file_cmdline->Buffer.segment, file_cmdline->Buffer.offset,
  224. file_cmdline->BufferSize, pxe_cmdline );
  225. buffer = real_to_user ( file_cmdline->Buffer.segment,
  226. file_cmdline->Buffer.offset );
  227. len = file_cmdline->BufferSize;
  228. max_len = ( pxe_cmdline ?
  229. ( strlen ( pxe_cmdline ) + 1 /* NUL */ ) : 0 );
  230. if ( len > max_len )
  231. len = max_len;
  232. copy_to_user ( buffer, 0, pxe_cmdline, len );
  233. file_cmdline->BufferSize = max_len;
  234. file_cmdline->Status = PXENV_STATUS_SUCCESS;
  235. return PXENV_EXIT_SUCCESS;
  236. }
  237. /**
  238. * FILE API CHECK
  239. *
  240. * @v file_exec Pointer to a struct s_PXENV_FILE_API_CHECK
  241. * @v s_PXENV_FILE_API_CHECK::Magic Inbound magic number (0x91d447b2)
  242. * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
  243. * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
  244. * @ret s_PXENV_FILE_API_CHECK::Status PXE status code
  245. * @ret s_PXENV_FILE_API_CHECK::Magic Outbound magic number (0xe9c17b20)
  246. * @ret s_PXENV_FILE_API_CHECK::Provider "iPXE" (0x45585067)
  247. * @ret s_PXENV_FILE_API_CHECK::APIMask API function bitmask
  248. * @ret s_PXENV_FILE_API_CHECK::Flags Reserved
  249. *
  250. */
  251. static PXENV_EXIT_t
  252. pxenv_file_api_check ( struct s_PXENV_FILE_API_CHECK *file_api_check ) {
  253. struct pxe_api_call *call;
  254. unsigned int mask = 0;
  255. unsigned int offset;
  256. DBG ( "PXENV_FILE_API_CHECK" );
  257. /* Check for magic value */
  258. if ( file_api_check->Magic != 0x91d447b2 ) {
  259. file_api_check->Status = PXENV_STATUS_BAD_FUNC;
  260. return PXENV_EXIT_FAILURE;
  261. }
  262. /* Check for required parameter size */
  263. if ( file_api_check->Size < sizeof ( *file_api_check ) ) {
  264. file_api_check->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  265. return PXENV_EXIT_FAILURE;
  266. }
  267. /* Determine supported calls */
  268. for_each_table_entry ( call, PXE_API_CALLS ) {
  269. offset = ( call->opcode - PXENV_FILE_MIN );
  270. if ( offset <= ( PXENV_FILE_MAX - PXENV_FILE_MIN ) )
  271. mask |= ( 1 << offset );
  272. }
  273. /* Fill in parameters */
  274. file_api_check->Size = sizeof ( *file_api_check );
  275. file_api_check->Magic = 0xe9c17b20;
  276. file_api_check->Provider = 0x45585067; /* "iPXE" */
  277. file_api_check->APIMask = mask;
  278. file_api_check->Flags = 0; /* None defined */
  279. file_api_check->Status = PXENV_STATUS_SUCCESS;
  280. return PXENV_EXIT_SUCCESS;
  281. }
  282. /** PXE file API */
  283. struct pxe_api_call pxe_file_api[] __pxe_api_call = {
  284. PXE_API_CALL ( PXENV_FILE_OPEN, pxenv_file_open,
  285. struct s_PXENV_FILE_OPEN ),
  286. PXE_API_CALL ( PXENV_FILE_CLOSE, pxenv_file_close,
  287. struct s_PXENV_FILE_CLOSE ),
  288. PXE_API_CALL ( PXENV_FILE_SELECT, pxenv_file_select,
  289. struct s_PXENV_FILE_SELECT ),
  290. PXE_API_CALL ( PXENV_FILE_READ, pxenv_file_read,
  291. struct s_PXENV_FILE_READ ),
  292. PXE_API_CALL ( PXENV_GET_FILE_SIZE, pxenv_get_file_size,
  293. struct s_PXENV_GET_FILE_SIZE ),
  294. PXE_API_CALL ( PXENV_FILE_EXEC, pxenv_file_exec,
  295. struct s_PXENV_FILE_EXEC ),
  296. PXE_API_CALL ( PXENV_FILE_CMDLINE, pxenv_file_cmdline,
  297. struct s_PXENV_FILE_CMDLINE ),
  298. PXE_API_CALL ( PXENV_FILE_API_CHECK, pxenv_file_api_check,
  299. struct s_PXENV_FILE_API_CHECK ),
  300. };