2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
24 * VMware GuestRPC mechanism
32 #include <ipxe/vmware.h>
33 #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" )
59 * Open GuestRPC channel
61 * @ret channel Channel number, or negative error
63 int guestrpc_open ( void ) {
68 /* Issue GuestRPC command */
69 status = vmware_cmd_guestrpc ( 0, GUESTRPC_OPEN, GUESTRPC_MAGIC,
70 &channel, &discard_b );
71 if ( status != GUESTRPC_OPEN_SUCCESS ) {
72 DBGC ( GUESTRPC_MAGIC, "GuestRPC open failed: status %08x\n",
77 DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d opened\n", channel );
82 * Send GuestRPC command length
84 * @v channel Channel number
85 * @v len Command length
86 * @ret rc Return status code
88 static int guestrpc_command_len ( int channel, size_t len ) {
93 /* Issue GuestRPC command */
94 status = vmware_cmd_guestrpc ( channel, GUESTRPC_COMMAND_LEN, len,
95 &discard_d, &discard_b );
96 if ( status != GUESTRPC_COMMAND_LEN_SUCCESS ) {
97 DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d send command "
98 "length %zd failed: status %08x\n",
99 channel, len, status );
100 return -EPROTO_COMMAND_LEN;
107 * Send GuestRPC command data
109 * @v channel Channel number
110 * @v data Command data
111 * @ret rc Return status code
113 static int guestrpc_command_data ( int channel, uint32_t data ) {
118 /* Issue GuestRPC command */
119 status = vmware_cmd_guestrpc ( channel, GUESTRPC_COMMAND_DATA, data,
120 &discard_d, &discard_b );
121 if ( status != GUESTRPC_COMMAND_DATA_SUCCESS ) {
122 DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d send command "
123 "data %08x failed: status %08x\n",
124 channel, data, status );
125 return -EPROTO_COMMAND_DATA;
132 * Receive GuestRPC reply length
134 * @v channel Channel number
135 * @ret reply_id Reply ID
136 * @ret len Reply length, or negative error
138 static int guestrpc_reply_len ( int channel, uint16_t *reply_id ) {
142 /* Issue GuestRPC command */
143 status = vmware_cmd_guestrpc ( channel, GUESTRPC_REPLY_LEN, 0,
145 if ( status != GUESTRPC_REPLY_LEN_SUCCESS ) {
146 DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d receive reply "
147 "length failed: status %08x\n", channel, status );
148 return -EPROTO_REPLY_LEN;
155 * Receive GuestRPC reply data
157 * @v channel Channel number
158 * @v reply_id Reply ID
159 * @ret data Reply data
160 * @ret rc Return status code
162 static int guestrpc_reply_data ( int channel, uint16_t reply_id,
167 /* Issue GuestRPC command */
168 status = vmware_cmd_guestrpc ( channel, GUESTRPC_REPLY_DATA, reply_id,
170 if ( status != GUESTRPC_REPLY_DATA_SUCCESS ) {
171 DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d receive reply "
172 "%d data failed: status %08x\n",
173 channel, reply_id, status );
174 return -EPROTO_REPLY_DATA;
181 * Finish receiving GuestRPC reply
183 * @v channel Channel number
184 * @v reply_id Reply ID
185 * @ret rc Return status code
187 static int guestrpc_reply_finish ( int channel, uint16_t reply_id ) {
192 /* Issue GuestRPC command */
193 status = vmware_cmd_guestrpc ( channel, GUESTRPC_REPLY_FINISH, reply_id,
194 &discard_d, &discard_b );
195 if ( status != GUESTRPC_REPLY_FINISH_SUCCESS ) {
196 DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d finish reply %d "
197 "failed: status %08x\n", channel, reply_id, status );
198 return -EPROTO_REPLY_FINISH;
205 * Close GuestRPC channel
207 * @v channel Channel number
209 void guestrpc_close ( int channel ) {
214 /* Issue GuestRPC command */
215 status = vmware_cmd_guestrpc ( channel, GUESTRPC_CLOSE, 0,
216 &discard_d, &discard_b );
217 if ( status != GUESTRPC_CLOSE_SUCCESS ) {
218 DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d close failed: "
219 "status %08x\n", channel, status );
223 DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d closed\n", channel );
227 * Issue GuestRPC command
229 * @v channel Channel number
231 * @v reply Reply buffer
232 * @v reply_len Length of reply buffer
233 * @ret len Length of reply, or negative error
235 * The actual length of the reply will be returned even if the buffer
238 int guestrpc_command ( int channel, const char *command, char *reply,
240 const uint8_t *command_bytes = ( ( const void * ) command );
241 uint8_t *reply_bytes = ( ( void * ) reply );
242 size_t command_len = strlen ( command );
243 int orig_reply_len = reply_len;
245 uint8_t *status_bytes = ( ( void * ) &status );
246 size_t status_len = sizeof ( status );
254 DBGC2 ( GUESTRPC_MAGIC, "GuestRPC channel %d issuing command:\n",
256 DBGC2_HDA ( GUESTRPC_MAGIC, 0, command, command_len );
259 assert ( ( reply != NULL ) || ( reply_len == 0 ) );
261 /* Send command length */
262 if ( ( rc = guestrpc_command_len ( channel, command_len ) ) < 0 )
265 /* Send command data */
266 while ( command_len ) {
268 for ( i = sizeof ( data ) ; i ; i-- ) {
270 data = ( ( data & ~0xff ) |
271 *(command_bytes++) );
274 data = ( ( data << 24 ) | ( data >> 8 ) );
276 if ( ( rc = guestrpc_command_data ( channel, data ) ) < 0 )
280 /* Receive reply length */
281 if ( ( len = guestrpc_reply_len ( channel, &reply_id ) ) < 0 ) {
287 for ( remaining = len ; remaining > 0 ; remaining -= sizeof ( data ) ) {
288 if ( ( rc = guestrpc_reply_data ( channel, reply_id,
292 for ( i = sizeof ( data ) ; i ; i-- ) {
294 *(status_bytes++) = ( data & 0xff );
297 } else if ( reply_len ) {
298 *(reply_bytes++) = ( data & 0xff );
301 data = ( ( data << 24 ) | ( data >> 8 ) );
305 /* Finish receiving RPC reply */
306 if ( ( rc = guestrpc_reply_finish ( channel, reply_id ) ) < 0 )
309 DBGC2 ( GUESTRPC_MAGIC, "GuestRPC channel %d received reply (id %d, "
310 "length %d):\n", channel, reply_id, len );
311 DBGC2_HDA ( GUESTRPC_MAGIC, 0, &status, sizeof ( status ) );
312 DBGC2_HDA ( GUESTRPC_MAGIC, sizeof ( status ), reply,
313 ( ( len < orig_reply_len ) ? len : orig_reply_len ) );
315 /* Check reply status */
316 if ( status != GUESTRPC_SUCCESS ) {
317 DBGC ( GUESTRPC_MAGIC, "GuestRPC channel %d command failed "
318 "(status %04x, reply id %d, reply length %d):\n",
319 channel, status, reply_id, len );
320 DBGC_HDA ( GUESTRPC_MAGIC, 0, command, command_len );
321 DBGC_HDA ( GUESTRPC_MAGIC, 0, &status, sizeof ( status ) );
322 DBGC_HDA ( GUESTRPC_MAGIC, sizeof ( status ), reply,
323 ( ( len < orig_reply_len ) ? len : orig_reply_len ));