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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. * Portions (C) 2010 Shao Miller <shao.miller@yrdsb.edu.on.ca>.
  18. * [PXE exit hook logic]
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License as
  22. * published by the Free Software Foundation; either version 2 of the
  23. * License, or any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful, but
  26. * WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  28. * General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33. */
  34. FILE_LICENCE ( GPL2_OR_LATER );
  35. FEATURE ( FEATURE_MISC, "PXEXT", DHCP_EB_FEATURE_PXE_EXT, 2 );
  36. /**
  37. * FILE OPEN
  38. *
  39. * @v file_open Pointer to a struct s_PXENV_FILE_OPEN
  40. * @v s_PXENV_FILE_OPEN::FileName URL of file to open
  41. * @ret #PXENV_EXIT_SUCCESS File was opened
  42. * @ret #PXENV_EXIT_FAILURE File was not opened
  43. * @ret s_PXENV_FILE_OPEN::Status PXE status code
  44. * @ret s_PXENV_FILE_OPEN::FileHandle Handle of opened file
  45. *
  46. */
  47. PXENV_EXIT_t pxenv_file_open ( struct s_PXENV_FILE_OPEN *file_open ) {
  48. userptr_t filename;
  49. size_t filename_len;
  50. int fd;
  51. DBG ( "PXENV_FILE_OPEN" );
  52. /* Copy name from external program, and open it */
  53. filename = real_to_user ( file_open->FileName.segment,
  54. file_open->FileName.offset );
  55. filename_len = strlen_user ( filename, 0 );
  56. {
  57. char uri_string[ filename_len + 1 ];
  58. copy_from_user ( uri_string, filename, 0,
  59. sizeof ( uri_string ) );
  60. DBG ( " %s", uri_string );
  61. fd = open ( uri_string );
  62. }
  63. if ( fd < 0 ) {
  64. file_open->Status = PXENV_STATUS ( fd );
  65. return PXENV_EXIT_FAILURE;
  66. }
  67. DBG ( " as file %d", fd );
  68. file_open->FileHandle = fd;
  69. file_open->Status = PXENV_STATUS_SUCCESS;
  70. return PXENV_EXIT_SUCCESS;
  71. }
  72. /**
  73. * FILE CLOSE
  74. *
  75. * @v file_close Pointer to a struct s_PXENV_FILE_CLOSE
  76. * @v s_PXENV_FILE_CLOSE::FileHandle File handle
  77. * @ret #PXENV_EXIT_SUCCESS File was closed
  78. * @ret #PXENV_EXIT_FAILURE File was not closed
  79. * @ret s_PXENV_FILE_CLOSE::Status PXE status code
  80. *
  81. */
  82. PXENV_EXIT_t pxenv_file_close ( struct s_PXENV_FILE_CLOSE *file_close ) {
  83. DBG ( "PXENV_FILE_CLOSE %d", file_close->FileHandle );
  84. close ( file_close->FileHandle );
  85. file_close->Status = PXENV_STATUS_SUCCESS;
  86. return PXENV_EXIT_SUCCESS;
  87. }
  88. /**
  89. * FILE SELECT
  90. *
  91. * @v file_select Pointer to a struct s_PXENV_FILE_SELECT
  92. * @v s_PXENV_FILE_SELECT::FileHandle File handle
  93. * @ret #PXENV_EXIT_SUCCESS File has been checked for readiness
  94. * @ret #PXENV_EXIT_FAILURE File has not been checked for readiness
  95. * @ret s_PXENV_FILE_SELECT::Status PXE status code
  96. * @ret s_PXENV_FILE_SELECT::Ready Indication of readiness
  97. *
  98. */
  99. PXENV_EXIT_t 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. 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. PXENV_EXIT_t pxenv_get_file_size ( struct s_PXENV_GET_FILE_SIZE
  156. *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. 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. segoff_t __data16 ( pxe_exit_hook ) = { 0, 0 };
  202. #define pxe_exit_hook __use_data16 ( pxe_exit_hook )
  203. /**
  204. * FILE API CHECK
  205. *
  206. * @v file_exec Pointer to a struct s_PXENV_FILE_API_CHECK
  207. * @v s_PXENV_FILE_API_CHECK::Magic Inbound magic number (0x91d447b2)
  208. * @ret #PXENV_EXIT_SUCCESS Command was executed successfully
  209. * @ret #PXENV_EXIT_FAILURE Command was not executed successfully
  210. * @ret s_PXENV_FILE_API_CHECK::Status PXE status code
  211. * @ret s_PXENV_FILE_API_CHECK::Magic Outbound magic number (0xe9c17b20)
  212. * @ret s_PXENV_FILE_API_CHECK::Provider "iPXE" (0x45585067)
  213. * @ret s_PXENV_FILE_API_CHECK::APIMask API function bitmask
  214. * @ret s_PXENV_FILE_API_CHECK::Flags Reserved
  215. *
  216. */
  217. PXENV_EXIT_t pxenv_file_api_check ( struct s_PXENV_FILE_API_CHECK *file_api_check ) {
  218. DBG ( "PXENV_FILE_API_CHECK" );
  219. if ( file_api_check->Magic != 0x91d447b2 ) {
  220. file_api_check->Status = PXENV_STATUS_BAD_FUNC;
  221. return PXENV_EXIT_FAILURE;
  222. } else if ( file_api_check->Size <
  223. sizeof(struct s_PXENV_FILE_API_CHECK) ) {
  224. file_api_check->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  225. return PXENV_EXIT_FAILURE;
  226. } else {
  227. file_api_check->Status = PXENV_STATUS_SUCCESS;
  228. file_api_check->Size = sizeof(struct s_PXENV_FILE_API_CHECK);
  229. file_api_check->Magic = 0xe9c17b20;
  230. file_api_check->Provider = 0x45585067; /* "iPXE" */
  231. file_api_check->APIMask = 0x0000007f; /* Functions e0-e6 */
  232. /* Check to see if we have a PXE exit hook */
  233. if ( pxe_exit_hook.segment | pxe_exit_hook.offset )
  234. /* Function e7, also */
  235. file_api_check->APIMask |= 0x00000080;
  236. file_api_check->Flags = 0; /* None defined */
  237. return PXENV_EXIT_SUCCESS;
  238. }
  239. }
  240. /**
  241. * FILE EXIT HOOK
  242. *
  243. * @v file_exit_hook Pointer to a struct
  244. * s_PXENV_FILE_EXIT_HOOK
  245. * @v s_PXENV_FILE_EXIT_HOOK::Hook SEG16:OFF16 to jump to
  246. * @ret #PXENV_EXIT_SUCCESS Successfully set hook
  247. * @ret #PXENV_EXIT_FAILURE We're not an NBP build
  248. * @ret s_PXENV_FILE_EXIT_HOOK::Status PXE status code
  249. *
  250. */
  251. PXENV_EXIT_t pxenv_file_exit_hook ( struct s_PXENV_FILE_EXIT_HOOK
  252. *file_exit_hook ) {
  253. DBG ( "PXENV_FILE_EXIT_HOOK" );
  254. /* Check to see if we have a PXE exit hook */
  255. if ( pxe_exit_hook.segment | pxe_exit_hook.offset ) {
  256. /* We'll jump to the specified SEG16:OFF16 during exit */
  257. pxe_exit_hook.segment = file_exit_hook->Hook.segment;
  258. pxe_exit_hook.offset = file_exit_hook->Hook.offset;
  259. file_exit_hook->Status = PXENV_STATUS_SUCCESS;
  260. return PXENV_EXIT_SUCCESS;
  261. }
  262. DBG ( " not NBP" );
  263. file_exit_hook->Status = PXENV_STATUS_UNSUPPORTED;
  264. return PXENV_EXIT_FAILURE;
  265. }