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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. FILE_LICENCE ( GPL2_OR_LATER );
  34. FEATURE ( FEATURE_MISC, "PXEXT", DHCP_EB_FEATURE_PXE_EXT, 2 );
  35. /**
  36. * FILE OPEN
  37. *
  38. * @v file_open Pointer to a struct s_PXENV_FILE_OPEN
  39. * @v s_PXENV_FILE_OPEN::FileName URL of file to open
  40. * @ret #PXENV_EXIT_SUCCESS File was opened
  41. * @ret #PXENV_EXIT_FAILURE File was not opened
  42. * @ret s_PXENV_FILE_OPEN::Status PXE status code
  43. * @ret s_PXENV_FILE_OPEN::FileHandle Handle of opened file
  44. *
  45. */
  46. static PXENV_EXIT_t pxenv_file_open ( struct s_PXENV_FILE_OPEN *file_open ) {
  47. userptr_t filename;
  48. size_t filename_len;
  49. int fd;
  50. DBG ( "PXENV_FILE_OPEN" );
  51. /* Copy name from external program, and open it */
  52. filename = real_to_user ( file_open->FileName.segment,
  53. file_open->FileName.offset );
  54. filename_len = strlen_user ( filename, 0 );
  55. {
  56. char uri_string[ filename_len + 1 ];
  57. copy_from_user ( uri_string, filename, 0,
  58. sizeof ( uri_string ) );
  59. DBG ( " %s", uri_string );
  60. fd = open ( uri_string );
  61. }
  62. if ( fd < 0 ) {
  63. file_open->Status = PXENV_STATUS ( fd );
  64. return PXENV_EXIT_FAILURE;
  65. }
  66. DBG ( " as file %d", fd );
  67. file_open->FileHandle = fd;
  68. file_open->Status = PXENV_STATUS_SUCCESS;
  69. return PXENV_EXIT_SUCCESS;
  70. }
  71. /**
  72. * FILE CLOSE
  73. *
  74. * @v file_close Pointer to a struct s_PXENV_FILE_CLOSE
  75. * @v s_PXENV_FILE_CLOSE::FileHandle File handle
  76. * @ret #PXENV_EXIT_SUCCESS File was closed
  77. * @ret #PXENV_EXIT_FAILURE File was not closed
  78. * @ret s_PXENV_FILE_CLOSE::Status PXE status code
  79. *
  80. */
  81. static PXENV_EXIT_t pxenv_file_close ( struct s_PXENV_FILE_CLOSE *file_close ) {
  82. DBG ( "PXENV_FILE_CLOSE %d", file_close->FileHandle );
  83. close ( file_close->FileHandle );
  84. file_close->Status = PXENV_STATUS_SUCCESS;
  85. return PXENV_EXIT_SUCCESS;
  86. }
  87. /**
  88. * FILE SELECT
  89. *
  90. * @v file_select Pointer to a struct s_PXENV_FILE_SELECT
  91. * @v s_PXENV_FILE_SELECT::FileHandle File handle
  92. * @ret #PXENV_EXIT_SUCCESS File has been checked for readiness
  93. * @ret #PXENV_EXIT_FAILURE File has not been checked for readiness
  94. * @ret s_PXENV_FILE_SELECT::Status PXE status code
  95. * @ret s_PXENV_FILE_SELECT::Ready Indication of readiness
  96. *
  97. */
  98. static PXENV_EXIT_t
  99. pxenv_file_select ( struct s_PXENV_FILE_SELECT *file_select ) {
  100. fd_set fdset;
  101. int ready;
  102. DBG ( "PXENV_FILE_SELECT %d", file_select->FileHandle );
  103. FD_ZERO ( &fdset );
  104. FD_SET ( file_select->FileHandle, &fdset );
  105. if ( ( ready = select ( &fdset, 0 ) ) < 0 ) {
  106. file_select->Status = PXENV_STATUS ( ready );
  107. return PXENV_EXIT_FAILURE;
  108. }
  109. file_select->Ready = ( ready ? RDY_READ : 0 );
  110. file_select->Status = PXENV_STATUS_SUCCESS;
  111. return PXENV_EXIT_SUCCESS;
  112. }
  113. /**
  114. * FILE READ
  115. *
  116. * @v file_read Pointer to a struct s_PXENV_FILE_READ
  117. * @v s_PXENV_FILE_READ::FileHandle File handle
  118. * @v s_PXENV_FILE_READ::BufferSize Size of data buffer
  119. * @v s_PXENV_FILE_READ::Buffer Data buffer
  120. * @ret #PXENV_EXIT_SUCCESS Data has been read from file
  121. * @ret #PXENV_EXIT_FAILURE Data has not been read from file
  122. * @ret s_PXENV_FILE_READ::Status PXE status code
  123. * @ret s_PXENV_FILE_READ::Ready Indication of readiness
  124. * @ret s_PXENV_FILE_READ::BufferSize Length of data read
  125. *
  126. */
  127. static PXENV_EXIT_t pxenv_file_read ( struct s_PXENV_FILE_READ *file_read ) {
  128. userptr_t buffer;
  129. ssize_t len;
  130. DBG ( "PXENV_FILE_READ %d to %04x:%04x+%04x", file_read->FileHandle,
  131. file_read->Buffer.segment, file_read->Buffer.offset,
  132. file_read->BufferSize );
  133. buffer = real_to_user ( file_read->Buffer.segment,
  134. file_read->Buffer.offset );
  135. if ( ( len = read_user ( file_read->FileHandle, buffer, 0,
  136. file_read->BufferSize ) ) < 0 ) {
  137. file_read->Status = PXENV_STATUS ( len );
  138. return PXENV_EXIT_FAILURE;
  139. }
  140. DBG ( " read %04zx", ( ( size_t ) len ) );
  141. file_read->BufferSize = len;
  142. file_read->Status = PXENV_STATUS_SUCCESS;
  143. return PXENV_EXIT_SUCCESS;
  144. }
  145. /**
  146. * GET FILE SIZE
  147. *
  148. * @v get_file_size Pointer to a struct s_PXENV_GET_FILE_SIZE
  149. * @v s_PXENV_GET_FILE_SIZE::FileHandle File handle
  150. * @ret #PXENV_EXIT_SUCCESS File size has been determined
  151. * @ret #PXENV_EXIT_FAILURE File size has not been determined
  152. * @ret s_PXENV_GET_FILE_SIZE::Status PXE status code
  153. * @ret s_PXENV_GET_FILE_SIZE::FileSize Size of file
  154. */
  155. static PXENV_EXIT_t
  156. pxenv_get_file_size ( struct s_PXENV_GET_FILE_SIZE *get_file_size ) {
  157. ssize_t filesize;
  158. DBG ( "PXENV_GET_FILE_SIZE %d", get_file_size->FileHandle );
  159. filesize = fsize ( get_file_size->FileHandle );
  160. if ( filesize < 0 ) {
  161. get_file_size->Status = PXENV_STATUS ( filesize );
  162. return PXENV_EXIT_FAILURE;
  163. }
  164. DBG ( " is %zd", ( ( size_t ) filesize ) );
  165. get_file_size->FileSize = filesize;
  166. get_file_size->Status = PXENV_STATUS_SUCCESS;
  167. return PXENV_EXIT_SUCCESS;
  168. }
  169. /**
  170. * FILE EXEC
  171. *
  172. * @v file_exec Pointer to a struct s_PXENV_FILE_EXEC
  173. * @v s_PXENV_FILE_EXEC::Command Command to execute
  174. * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
  175. * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
  176. * @ret s_PXENV_FILE_EXEC::Status PXE status code
  177. *
  178. */
  179. static PXENV_EXIT_t pxenv_file_exec ( struct s_PXENV_FILE_EXEC *file_exec ) {
  180. userptr_t command;
  181. size_t command_len;
  182. int rc;
  183. DBG ( "PXENV_FILE_EXEC" );
  184. /* Copy name from external program, and exec it */
  185. command = real_to_user ( file_exec->Command.segment,
  186. file_exec->Command.offset );
  187. command_len = strlen_user ( command, 0 );
  188. {
  189. char command_string[ command_len + 1 ];
  190. copy_from_user ( command_string, command, 0,
  191. sizeof ( command_string ) );
  192. DBG ( " %s", command_string );
  193. if ( ( rc = system ( command_string ) ) != 0 ) {
  194. file_exec->Status = PXENV_STATUS ( rc );
  195. return PXENV_EXIT_FAILURE;
  196. }
  197. }
  198. file_exec->Status = PXENV_STATUS_SUCCESS;
  199. return PXENV_EXIT_SUCCESS;
  200. }
  201. /**
  202. * FILE CMDLINE
  203. *
  204. * @v file_cmdline Pointer to a struct s_PXENV_FILE_CMDLINE
  205. * @v s_PXENV_FILE_CMDLINE::Buffer Buffer to contain command line
  206. * @v s_PXENV_FILE_CMDLINE::BufferSize Size of buffer
  207. * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
  208. * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
  209. * @ret s_PXENV_FILE_EXEC::Status PXE status code
  210. * @ret s_PXENV_FILE_EXEC::BufferSize Length of command line (including NUL)
  211. *
  212. */
  213. static PXENV_EXIT_t
  214. pxenv_file_cmdline ( struct s_PXENV_FILE_CMDLINE *file_cmdline ) {
  215. userptr_t buffer;
  216. size_t max_len;
  217. size_t len;
  218. DBG ( "PXENV_FILE_CMDLINE to %04x:%04x+%04x \"%s\"\n",
  219. file_cmdline->Buffer.segment, file_cmdline->Buffer.offset,
  220. file_cmdline->BufferSize, pxe_cmdline );
  221. buffer = real_to_user ( file_cmdline->Buffer.segment,
  222. file_cmdline->Buffer.offset );
  223. len = file_cmdline->BufferSize;
  224. max_len = ( pxe_cmdline ?
  225. ( strlen ( pxe_cmdline ) + 1 /* NUL */ ) : 0 );
  226. if ( len > max_len )
  227. len = max_len;
  228. copy_to_user ( buffer, 0, pxe_cmdline, len );
  229. file_cmdline->BufferSize = max_len;
  230. file_cmdline->Status = PXENV_STATUS_SUCCESS;
  231. return PXENV_EXIT_SUCCESS;
  232. }
  233. /**
  234. * FILE API CHECK
  235. *
  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 "iPXE" (0x45585067)
  243. * @ret s_PXENV_FILE_API_CHECK::APIMask API function bitmask
  244. * @ret s_PXENV_FILE_API_CHECK::Flags Reserved
  245. *
  246. */
  247. static PXENV_EXIT_t
  248. pxenv_file_api_check ( struct s_PXENV_FILE_API_CHECK *file_api_check ) {
  249. struct pxe_api_call *call;
  250. unsigned int mask = 0;
  251. unsigned int offset;
  252. DBG ( "PXENV_FILE_API_CHECK" );
  253. /* Check for magic value */
  254. if ( file_api_check->Magic != 0x91d447b2 ) {
  255. file_api_check->Status = PXENV_STATUS_BAD_FUNC;
  256. return PXENV_EXIT_FAILURE;
  257. }
  258. /* Check for required parameter size */
  259. if ( file_api_check->Size < sizeof ( *file_api_check ) ) {
  260. file_api_check->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  261. return PXENV_EXIT_FAILURE;
  262. }
  263. /* Determine supported calls */
  264. for_each_table_entry ( call, PXE_API_CALLS ) {
  265. offset = ( call->opcode - PXENV_FILE_MIN );
  266. if ( offset <= ( PXENV_FILE_MAX - PXENV_FILE_MIN ) )
  267. mask |= ( 1 << offset );
  268. }
  269. /* Fill in parameters */
  270. file_api_check->Size = sizeof ( *file_api_check );
  271. file_api_check->Magic = 0xe9c17b20;
  272. file_api_check->Provider = 0x45585067; /* "iPXE" */
  273. file_api_check->APIMask = mask;
  274. file_api_check->Flags = 0; /* None defined */
  275. file_api_check->Status = PXENV_STATUS_SUCCESS;
  276. return PXENV_EXIT_SUCCESS;
  277. }
  278. /** PXE file API */
  279. struct pxe_api_call pxe_file_api[] __pxe_api_call = {
  280. PXE_API_CALL ( PXENV_FILE_OPEN, pxenv_file_open,
  281. struct s_PXENV_FILE_OPEN ),
  282. PXE_API_CALL ( PXENV_FILE_CLOSE, pxenv_file_close,
  283. struct s_PXENV_FILE_CLOSE ),
  284. PXE_API_CALL ( PXENV_FILE_SELECT, pxenv_file_select,
  285. struct s_PXENV_FILE_SELECT ),
  286. PXE_API_CALL ( PXENV_FILE_READ, pxenv_file_read,
  287. struct s_PXENV_FILE_READ ),
  288. PXE_API_CALL ( PXENV_GET_FILE_SIZE, pxenv_get_file_size,
  289. struct s_PXENV_GET_FILE_SIZE ),
  290. PXE_API_CALL ( PXENV_FILE_EXEC, pxenv_file_exec,
  291. struct s_PXENV_FILE_EXEC ),
  292. PXE_API_CALL ( PXENV_FILE_CMDLINE, pxenv_file_cmdline,
  293. struct s_PXENV_FILE_CMDLINE ),
  294. PXE_API_CALL ( PXENV_FILE_API_CHECK, pxenv_file_api_check,
  295. struct s_PXENV_FILE_API_CHECK ),
  296. };