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.

guestrpc.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * VMware GuestRPC mechanism
  27. *
  28. */
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <assert.h>
  33. #include <ipxe/vmware.h>
  34. #include <ipxe/guestrpc.h>
  35. /* Disambiguate the various error causes */
  36. #define EPROTO_OPEN __einfo_error ( EINFO_EPROTO_OPEN )
  37. #define EINFO_EPROTO_OPEN \
  38. __einfo_uniqify ( EINFO_EPROTO, 0x00, "GuestRPC open failed" )
  39. #define EPROTO_COMMAND_LEN __einfo_error ( EINFO_EPROTO_COMMAND_LEN )
  40. #define EINFO_EPROTO_COMMAND_LEN \
  41. __einfo_uniqify ( EINFO_EPROTO, 0x01, "GuestRPC command length failed" )
  42. #define EPROTO_COMMAND_DATA __einfo_error ( EINFO_EPROTO_COMMAND_DATA )
  43. #define EINFO_EPROTO_COMMAND_DATA \
  44. __einfo_uniqify ( EINFO_EPROTO, 0x02, "GuestRPC command data failed" )
  45. #define EPROTO_REPLY_LEN __einfo_error ( EINFO_EPROTO_REPLY_LEN )
  46. #define EINFO_EPROTO_REPLY_LEN \
  47. __einfo_uniqify ( EINFO_EPROTO, 0x03, "GuestRPC reply length failed" )
  48. #define EPROTO_REPLY_DATA __einfo_error ( EINFO_EPROTO_REPLY_DATA )
  49. #define EINFO_EPROTO_REPLY_DATA \
  50. __einfo_uniqify ( EINFO_EPROTO, 0x04, "GuestRPC reply data failed" )
  51. #define EPROTO_REPLY_FINISH __einfo_error ( EINFO_EPROTO_REPLY_FINISH )
  52. #define EINFO_EPROTO_REPLY_FINISH \
  53. __einfo_uniqify ( EINFO_EPROTO, 0x05, "GuestRPC reply finish failed" )
  54. #define EPROTO_CLOSE __einfo_error ( EINFO_EPROTO_CLOSE )
  55. #define EINFO_EPROTO_CLOSE \
  56. __einfo_uniqify ( EINFO_EPROTO, 0x06, "GuestRPC close failed" )
  57. /**
  58. * Open GuestRPC channel
  59. *
  60. * @ret channel Channel number, or negative error
  61. */
  62. int guestrpc_open ( void ) {
  63. uint16_t channel;
  64. uint32_t discard_b;
  65. uint32_t status;
  66. /* Issue GuestRPC command */
  67. status = vmware_cmd_guestrpc ( 0, GUESTRPC_OPEN, GUESTRPC_MAGIC,
  68. &channel, &discard_b );
  69. if ( status != GUESTRPC_OPEN_SUCCESS ) {
  70. DBGC ( GUESTRPC_MAGIC, "GuestRPC open failed: status %08x\n",
  71. status );
  72. return -EPROTO_OPEN;
  73. }
  74. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d opened\n", channel );
  75. return channel;
  76. }
  77. /**
  78. * Send GuestRPC command length
  79. *
  80. * @v channel Channel number
  81. * @v len Command length
  82. * @ret rc Return status code
  83. */
  84. static int guestrpc_command_len ( int channel, size_t len ) {
  85. uint16_t discard_d;
  86. uint32_t discard_b;
  87. uint32_t status;
  88. /* Issue GuestRPC command */
  89. status = vmware_cmd_guestrpc ( channel, GUESTRPC_COMMAND_LEN, len,
  90. &discard_d, &discard_b );
  91. if ( status != GUESTRPC_COMMAND_LEN_SUCCESS ) {
  92. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d send command "
  93. "length %zd failed: status %08x\n",
  94. channel, len, status );
  95. return -EPROTO_COMMAND_LEN;
  96. }
  97. return 0;
  98. }
  99. /**
  100. * Send GuestRPC command data
  101. *
  102. * @v channel Channel number
  103. * @v data Command data
  104. * @ret rc Return status code
  105. */
  106. static int guestrpc_command_data ( int channel, uint32_t data ) {
  107. uint16_t discard_d;
  108. uint32_t discard_b;
  109. uint32_t status;
  110. /* Issue GuestRPC command */
  111. status = vmware_cmd_guestrpc ( channel, GUESTRPC_COMMAND_DATA, data,
  112. &discard_d, &discard_b );
  113. if ( status != GUESTRPC_COMMAND_DATA_SUCCESS ) {
  114. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d send command "
  115. "data %08x failed: status %08x\n",
  116. channel, data, status );
  117. return -EPROTO_COMMAND_DATA;
  118. }
  119. return 0;
  120. }
  121. /**
  122. * Receive GuestRPC reply length
  123. *
  124. * @v channel Channel number
  125. * @ret reply_id Reply ID
  126. * @ret len Reply length, or negative error
  127. */
  128. static int guestrpc_reply_len ( int channel, uint16_t *reply_id ) {
  129. uint32_t len;
  130. uint32_t status;
  131. /* Issue GuestRPC command */
  132. status = vmware_cmd_guestrpc ( channel, GUESTRPC_REPLY_LEN, 0,
  133. reply_id, &len );
  134. if ( status != GUESTRPC_REPLY_LEN_SUCCESS ) {
  135. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d receive reply "
  136. "length failed: status %08x\n", channel, status );
  137. return -EPROTO_REPLY_LEN;
  138. }
  139. return len;
  140. }
  141. /**
  142. * Receive GuestRPC reply data
  143. *
  144. * @v channel Channel number
  145. * @v reply_id Reply ID
  146. * @ret data Reply data
  147. * @ret rc Return status code
  148. */
  149. static int guestrpc_reply_data ( int channel, uint16_t reply_id,
  150. uint32_t *data ) {
  151. uint16_t discard_d;
  152. uint32_t status;
  153. /* Issue GuestRPC command */
  154. status = vmware_cmd_guestrpc ( channel, GUESTRPC_REPLY_DATA, reply_id,
  155. &discard_d, data );
  156. if ( status != GUESTRPC_REPLY_DATA_SUCCESS ) {
  157. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d receive reply "
  158. "%d data failed: status %08x\n",
  159. channel, reply_id, status );
  160. return -EPROTO_REPLY_DATA;
  161. }
  162. return 0;
  163. }
  164. /**
  165. * Finish receiving GuestRPC reply
  166. *
  167. * @v channel Channel number
  168. * @v reply_id Reply ID
  169. * @ret rc Return status code
  170. */
  171. static int guestrpc_reply_finish ( int channel, uint16_t reply_id ) {
  172. uint16_t discard_d;
  173. uint32_t discard_b;
  174. uint32_t status;
  175. /* Issue GuestRPC command */
  176. status = vmware_cmd_guestrpc ( channel, GUESTRPC_REPLY_FINISH, reply_id,
  177. &discard_d, &discard_b );
  178. if ( status != GUESTRPC_REPLY_FINISH_SUCCESS ) {
  179. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d finish reply %d "
  180. "failed: status %08x\n", channel, reply_id, status );
  181. return -EPROTO_REPLY_FINISH;
  182. }
  183. return 0;
  184. }
  185. /**
  186. * Close GuestRPC channel
  187. *
  188. * @v channel Channel number
  189. */
  190. void guestrpc_close ( int channel ) {
  191. uint16_t discard_d;
  192. uint32_t discard_b;
  193. uint32_t status;
  194. /* Issue GuestRPC command */
  195. status = vmware_cmd_guestrpc ( channel, GUESTRPC_CLOSE, 0,
  196. &discard_d, &discard_b );
  197. if ( status != GUESTRPC_CLOSE_SUCCESS ) {
  198. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d close failed: "
  199. "status %08x\n", channel, status );
  200. return;
  201. }
  202. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d closed\n", channel );
  203. }
  204. /**
  205. * Issue GuestRPC command
  206. *
  207. * @v channel Channel number
  208. * @v command Command
  209. * @v reply Reply buffer
  210. * @v reply_len Length of reply buffer
  211. * @ret len Length of reply, or negative error
  212. *
  213. * The actual length of the reply will be returned even if the buffer
  214. * was too small.
  215. */
  216. int guestrpc_command ( int channel, const char *command, char *reply,
  217. size_t reply_len ) {
  218. const uint8_t *command_bytes = ( ( const void * ) command );
  219. uint8_t *reply_bytes = ( ( void * ) reply );
  220. size_t command_len = strlen ( command );
  221. int orig_reply_len = reply_len;
  222. uint16_t status;
  223. uint8_t *status_bytes = ( ( void * ) &status );
  224. size_t status_len = sizeof ( status );
  225. uint32_t data;
  226. uint16_t reply_id;
  227. int len;
  228. int remaining;
  229. unsigned int i;
  230. int rc;
  231. DBGC2 ( GUESTRPC_MAGIC, "GuestRPC channel %d issuing command:\n",
  232. channel );
  233. DBGC2_HDA ( GUESTRPC_MAGIC, 0, command, command_len );
  234. /* Sanity check */
  235. assert ( ( reply != NULL ) || ( reply_len == 0 ) );
  236. /* Send command length */
  237. if ( ( rc = guestrpc_command_len ( channel, command_len ) ) < 0 )
  238. return rc;
  239. /* Send command data */
  240. while ( command_len ) {
  241. data = 0;
  242. for ( i = sizeof ( data ) ; i ; i-- ) {
  243. if ( command_len ) {
  244. data = ( ( data & ~0xff ) |
  245. *(command_bytes++) );
  246. command_len--;
  247. }
  248. data = ( ( data << 24 ) | ( data >> 8 ) );
  249. }
  250. if ( ( rc = guestrpc_command_data ( channel, data ) ) < 0 )
  251. return rc;
  252. }
  253. /* Receive reply length */
  254. if ( ( len = guestrpc_reply_len ( channel, &reply_id ) ) < 0 ) {
  255. rc = len;
  256. return rc;
  257. }
  258. /* Receive reply */
  259. for ( remaining = len ; remaining > 0 ; remaining -= sizeof ( data ) ) {
  260. if ( ( rc = guestrpc_reply_data ( channel, reply_id,
  261. &data ) ) < 0 ) {
  262. return rc;
  263. }
  264. for ( i = sizeof ( data ) ; i ; i-- ) {
  265. if ( status_len ) {
  266. *(status_bytes++) = ( data & 0xff );
  267. status_len--;
  268. len--;
  269. } else if ( reply_len ) {
  270. *(reply_bytes++) = ( data & 0xff );
  271. reply_len--;
  272. }
  273. data = ( ( data << 24 ) | ( data >> 8 ) );
  274. }
  275. }
  276. /* Finish receiving RPC reply */
  277. if ( ( rc = guestrpc_reply_finish ( channel, reply_id ) ) < 0 )
  278. return rc;
  279. DBGC2 ( GUESTRPC_MAGIC, "GuestRPC channel %d received reply (id %d, "
  280. "length %d):\n", channel, reply_id, len );
  281. DBGC2_HDA ( GUESTRPC_MAGIC, 0, &status, sizeof ( status ) );
  282. DBGC2_HDA ( GUESTRPC_MAGIC, sizeof ( status ), reply,
  283. ( ( len < orig_reply_len ) ? len : orig_reply_len ) );
  284. /* Check reply status */
  285. if ( status != GUESTRPC_SUCCESS ) {
  286. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d command failed "
  287. "(status %04x, reply id %d, reply length %d):\n",
  288. channel, status, reply_id, len );
  289. DBGC_HDA ( GUESTRPC_MAGIC, 0, command, command_len );
  290. DBGC_HDA ( GUESTRPC_MAGIC, 0, &status, sizeof ( status ) );
  291. DBGC_HDA ( GUESTRPC_MAGIC, sizeof ( status ), reply,
  292. ( ( len < orig_reply_len ) ? len : orig_reply_len ));
  293. return -EIO;
  294. }
  295. return len;
  296. }