MCPcopy Create free account
hub / github.com/cloud-hypervisor/cloud-hypervisor / run_control_cmd

Method run_control_cmd

tpm/src/emulator.rs:215–284  ·  view source on GitHub ↗

# Arguments `cmd` - Control Command to run `msg` - Optional msg to be sent along with Control Command `msg_len_in` - len of 'msg' in bytes, if passed `msg_len_out` - length of expected output from Control Command in bytes

(
        &mut self,
        cmd: Commands,
        msg: &mut dyn Ptm,
        msg_len_in: usize,
        msg_len_out: usize,
    )

Source from the content-addressed store, hash-verified

213 /// * `msg_len_out` - length of expected output from Control Command in bytes
214 ///
215 fn run_control_cmd(
216 &mut self,
217 cmd: Commands,
218 msg: &mut dyn Ptm,
219 msg_len_in: usize,
220 msg_len_out: usize,
221 ) -> Result<()> {
222 debug!("Control Cmd to send : {cmd:02X?}");
223
224 let cmd_no = (cmd as u32).to_be_bytes();
225 let n = mem::size_of::<u32>() + msg_len_in;
226
227 let converted_req = msg.ptm_to_request();
228 debug!("converted request: {converted_req:02X?}");
229
230 let mut buf = Vec::<u8>::with_capacity(n);
231
232 buf.extend(cmd_no);
233 buf.extend(converted_req);
234 debug!("full Control request {buf:02X?}");
235
236 let written = self.control_socket.write(&buf).map_err(|e| {
237 Error::RunControlCmd(anyhow!(
238 "Failed while running {cmd:02X?} Control Cmd. Error: {e:?}"
239 ))
240 })?;
241
242 if written < buf.len() {
243 return Err(Error::RunControlCmd(anyhow!(
244 "Truncated write while running {cmd:02X?} Control Cmd",
245 )));
246 }
247
248 // The largest response is 16 bytes so far.
249 if msg_len_out > 16 {
250 return Err(Error::RunControlCmd(anyhow!(
251 "Response size is too large for Cmd {cmd:02X?}, max 16 wanted {msg_len_out}"
252 )));
253 }
254
255 let mut output = [0u8; 16];
256
257 // Every Control Cmd gets at least a result code in response. Read it
258 let read_size = self.control_socket.read(&mut output).map_err(|e| {
259 Error::RunControlCmd(anyhow!(
260 "Failed while reading response for Control Cmd: {cmd:02X?}. Error: {e:?}"
261 ))
262 })?;
263
264 if msg_len_out != 0 {
265 msg.update_ptm_with_response(&output[0..read_size])
266 .map_err(|e| {
267 Error::RunControlCmd(anyhow!(
268 "Failed while converting response of Control Cmd: {cmd:02X?} to PTM. Error: {e:?}"
269 ))
270 })?;
271 } else {
272 // No response expected, only handle return code

Callers 7

prepare_data_fdMethod · 0.80
probe_capsMethod · 0.80
get_established_flagMethod · 0.80
cancel_cmdMethod · 0.80
set_buffer_sizeMethod · 0.80
startup_tpmMethod · 0.80
stop_tpmMethod · 0.80

Calls 8

ptm_to_requestMethod · 0.80
set_member_typeMethod · 0.80
get_result_codeMethod · 0.80
extendMethod · 0.45
writeMethod · 0.45
lenMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected