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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * VMware GuestRPC mechanism
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <assert.h>
  29. #include <ipxe/vmware.h>
  30. #include <ipxe/guestrpc.h>
  31. /* Disambiguate the various error causes */
  32. #define EPROTO_OPEN __einfo_error ( EINFO_EPROTO_OPEN )
  33. #define EINFO_EPROTO_OPEN \
  34. __einfo_uniqify ( EINFO_EPROTO, 0x00, "GuestRPC open failed" )
  35. #define EPROTO_COMMAND_LEN __einfo_error ( EINFO_EPROTO_COMMAND_LEN )
  36. #define EINFO_EPROTO_COMMAND_LEN \
  37. __einfo_uniqify ( EINFO_EPROTO, 0x01, "GuestRPC command length failed" )
  38. #define EPROTO_COMMAND_DATA __einfo_error ( EINFO_EPROTO_COMMAND_DATA )
  39. #define EINFO_EPROTO_COMMAND_DATA \
  40. __einfo_uniqify ( EINFO_EPROTO, 0x02, "GuestRPC command data failed" )
  41. #define EPROTO_REPLY_LEN __einfo_error ( EINFO_EPROTO_REPLY_LEN )
  42. #define EINFO_EPROTO_REPLY_LEN \
  43. __einfo_uniqify ( EINFO_EPROTO, 0x03, "GuestRPC reply length failed" )
  44. #define EPROTO_REPLY_DATA __einfo_error ( EINFO_EPROTO_REPLY_DATA )
  45. #define EINFO_EPROTO_REPLY_DATA \
  46. __einfo_uniqify ( EINFO_EPROTO, 0x04, "GuestRPC reply data failed" )
  47. #define EPROTO_REPLY_FINISH __einfo_error ( EINFO_EPROTO_REPLY_FINISH )
  48. #define EINFO_EPROTO_REPLY_FINISH \
  49. __einfo_uniqify ( EINFO_EPROTO, 0x05, "GuestRPC reply finish failed" )
  50. #define EPROTO_CLOSE __einfo_error ( EINFO_EPROTO_CLOSE )
  51. #define EINFO_EPROTO_CLOSE \
  52. __einfo_uniqify ( EINFO_EPROTO, 0x06, "GuestRPC close failed" )
  53. /**
  54. * Open GuestRPC channel
  55. *
  56. * @ret channel Channel number, or negative error
  57. */
  58. int guestrpc_open ( void ) {
  59. uint16_t channel;
  60. uint32_t discard_b;
  61. uint32_t status;
  62. /* Issue GuestRPC command */
  63. status = vmware_cmd_guestrpc ( 0, GUESTRPC_OPEN, GUESTRPC_MAGIC,
  64. &channel, &discard_b );
  65. if ( status != GUESTRPC_OPEN_SUCCESS ) {
  66. DBGC ( GUESTRPC_MAGIC, "GuestRPC open failed: status %08x\n",
  67. status );
  68. return -EPROTO_OPEN;
  69. }
  70. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d opened\n", channel );
  71. return channel;
  72. }
  73. /**
  74. * Send GuestRPC command length
  75. *
  76. * @v channel Channel number
  77. * @v len Command length
  78. * @ret rc Return status code
  79. */
  80. static int guestrpc_command_len ( int channel, size_t len ) {
  81. uint16_t discard_d;
  82. uint32_t discard_b;
  83. uint32_t status;
  84. /* Issue GuestRPC command */
  85. status = vmware_cmd_guestrpc ( channel, GUESTRPC_COMMAND_LEN, len,
  86. &discard_d, &discard_b );
  87. if ( status != GUESTRPC_COMMAND_LEN_SUCCESS ) {
  88. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d send command "
  89. "length %zd failed: status %08x\n",
  90. channel, len, status );
  91. return -EPROTO_COMMAND_LEN;
  92. }
  93. return 0;
  94. }
  95. /**
  96. * Send GuestRPC command data
  97. *
  98. * @v channel Channel number
  99. * @v data Command data
  100. * @ret rc Return status code
  101. */
  102. static int guestrpc_command_data ( int channel, uint32_t data ) {
  103. uint16_t discard_d;
  104. uint32_t discard_b;
  105. uint32_t status;
  106. /* Issue GuestRPC command */
  107. status = vmware_cmd_guestrpc ( channel, GUESTRPC_COMMAND_DATA, data,
  108. &discard_d, &discard_b );
  109. if ( status != GUESTRPC_COMMAND_DATA_SUCCESS ) {
  110. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d send command "
  111. "data %08x failed: status %08x\n",
  112. channel, data, status );
  113. return -EPROTO_COMMAND_DATA;
  114. }
  115. return 0;
  116. }
  117. /**
  118. * Receive GuestRPC reply length
  119. *
  120. * @v channel Channel number
  121. * @ret reply_id Reply ID
  122. * @ret len Reply length, or negative error
  123. */
  124. static int guestrpc_reply_len ( int channel, uint16_t *reply_id ) {
  125. uint32_t len;
  126. uint32_t status;
  127. /* Issue GuestRPC command */
  128. status = vmware_cmd_guestrpc ( channel, GUESTRPC_REPLY_LEN, 0,
  129. reply_id, &len );
  130. if ( status != GUESTRPC_REPLY_LEN_SUCCESS ) {
  131. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d receive reply "
  132. "length failed: status %08x\n", channel, status );
  133. return -EPROTO_REPLY_LEN;
  134. }
  135. return len;
  136. }
  137. /**
  138. * Receive GuestRPC reply data
  139. *
  140. * @v channel Channel number
  141. * @v reply_id Reply ID
  142. * @ret data Reply data
  143. * @ret rc Return status code
  144. */
  145. static int guestrpc_reply_data ( int channel, uint16_t reply_id,
  146. uint32_t *data ) {
  147. uint16_t discard_d;
  148. uint32_t status;
  149. /* Issue GuestRPC command */
  150. status = vmware_cmd_guestrpc ( channel, GUESTRPC_REPLY_DATA, reply_id,
  151. &discard_d, data );
  152. if ( status != GUESTRPC_REPLY_DATA_SUCCESS ) {
  153. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d receive reply "
  154. "%d data failed: status %08x\n",
  155. channel, reply_id, status );
  156. return -EPROTO_REPLY_DATA;
  157. }
  158. return 0;
  159. }
  160. /**
  161. * Finish receiving GuestRPC reply
  162. *
  163. * @v channel Channel number
  164. * @v reply_id Reply ID
  165. * @ret rc Return status code
  166. */
  167. static int guestrpc_reply_finish ( int channel, uint16_t reply_id ) {
  168. uint16_t discard_d;
  169. uint32_t discard_b;
  170. uint32_t status;
  171. /* Issue GuestRPC command */
  172. status = vmware_cmd_guestrpc ( channel, GUESTRPC_REPLY_FINISH, reply_id,
  173. &discard_d, &discard_b );
  174. if ( status != GUESTRPC_REPLY_FINISH_SUCCESS ) {
  175. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d finish reply %d "
  176. "failed: status %08x\n", channel, reply_id, status );
  177. return -EPROTO_REPLY_FINISH;
  178. }
  179. return 0;
  180. }
  181. /**
  182. * Close GuestRPC channel
  183. *
  184. * @v channel Channel number
  185. */
  186. void guestrpc_close ( int channel ) {
  187. uint16_t discard_d;
  188. uint32_t discard_b;
  189. uint32_t status;
  190. /* Issue GuestRPC command */
  191. status = vmware_cmd_guestrpc ( channel, GUESTRPC_CLOSE, 0,
  192. &discard_d, &discard_b );
  193. if ( status != GUESTRPC_CLOSE_SUCCESS ) {
  194. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d close failed: "
  195. "status %08x\n", channel, status );
  196. return;
  197. }
  198. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d closed\n", channel );
  199. }
  200. /**
  201. * Issue GuestRPC command
  202. *
  203. * @v channel Channel number
  204. * @v command Command
  205. * @v reply Reply buffer
  206. * @v reply_len Length of reply buffer
  207. * @ret len Length of reply, or negative error
  208. *
  209. * The actual length of the reply will be returned even if the buffer
  210. * was too small.
  211. */
  212. int guestrpc_command ( int channel, const char *command, char *reply,
  213. size_t reply_len ) {
  214. const uint8_t *command_bytes = ( ( const void * ) command );
  215. uint8_t *reply_bytes = ( ( void * ) reply );
  216. size_t command_len = strlen ( command );
  217. int orig_reply_len = reply_len;
  218. uint16_t status;
  219. uint8_t *status_bytes = ( ( void * ) &status );
  220. size_t status_len = sizeof ( status );
  221. uint32_t data;
  222. uint16_t reply_id;
  223. int len;
  224. int remaining;
  225. unsigned int i;
  226. int rc;
  227. DBGC2 ( GUESTRPC_MAGIC, "GuestRPC channel %d issuing command:\n",
  228. channel );
  229. DBGC2_HDA ( GUESTRPC_MAGIC, 0, command, command_len );
  230. /* Sanity check */
  231. assert ( ( reply != NULL ) || ( reply_len == 0 ) );
  232. /* Send command length */
  233. if ( ( rc = guestrpc_command_len ( channel, command_len ) ) < 0 )
  234. return rc;
  235. /* Send command data */
  236. while ( command_len ) {
  237. data = 0;
  238. for ( i = sizeof ( data ) ; i ; i-- ) {
  239. if ( command_len ) {
  240. data = ( ( data & ~0xff ) |
  241. *(command_bytes++) );
  242. command_len--;
  243. }
  244. data = ( ( data << 24 ) | ( data >> 8 ) );
  245. }
  246. if ( ( rc = guestrpc_command_data ( channel, data ) ) < 0 )
  247. return rc;
  248. }
  249. /* Receive reply length */
  250. if ( ( len = guestrpc_reply_len ( channel, &reply_id ) ) < 0 ) {
  251. rc = len;
  252. return rc;
  253. }
  254. /* Receive reply */
  255. for ( remaining = len ; remaining > 0 ; remaining -= sizeof ( data ) ) {
  256. if ( ( rc = guestrpc_reply_data ( channel, reply_id,
  257. &data ) ) < 0 ) {
  258. return rc;
  259. }
  260. for ( i = sizeof ( data ) ; i ; i-- ) {
  261. if ( status_len ) {
  262. *(status_bytes++) = ( data & 0xff );
  263. status_len--;
  264. len--;
  265. } else if ( reply_len ) {
  266. *(reply_bytes++) = ( data & 0xff );
  267. reply_len--;
  268. }
  269. data = ( ( data << 24 ) | ( data >> 8 ) );
  270. }
  271. }
  272. /* Finish receiving RPC reply */
  273. if ( ( rc = guestrpc_reply_finish ( channel, reply_id ) ) < 0 )
  274. return rc;
  275. DBGC2 ( GUESTRPC_MAGIC, "GuestRPC channel %d received reply (id %d, "
  276. "length %d):\n", channel, reply_id, len );
  277. DBGC2_HDA ( GUESTRPC_MAGIC, 0, &status, sizeof ( status ) );
  278. DBGC2_HDA ( GUESTRPC_MAGIC, sizeof ( status ), reply,
  279. ( ( len < orig_reply_len ) ? len : orig_reply_len ) );
  280. /* Check reply status */
  281. if ( status != GUESTRPC_SUCCESS ) {
  282. DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d command failed "
  283. "(status %04x, reply id %d, reply length %d):\n",
  284. channel, status, reply_id, len );
  285. DBGC_HDA ( GUESTRPC_MAGIC, 0, command, command_len );
  286. DBGC_HDA ( GUESTRPC_MAGIC, 0, &status, sizeof ( status ) );
  287. DBGC_HDA ( GUESTRPC_MAGIC, sizeof ( status ), reply,
  288. ( ( len < orig_reply_len ) ? len : orig_reply_len ));
  289. return -EIO;
  290. }
  291. return len;
  292. }