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

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