blob: c2923f0ff94f61c29005e7cf526ba03454c2d898 [file] [log] [blame]
Alan Stokes9e2c5d52021-07-21 11:29:10 +01001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070017//! A tool to start a standalone compsvc server that serves over RPC binder.
Alan Stokes9e2c5d52021-07-21 11:29:10 +010018
Alan Stokes183d7d32021-12-08 16:10:45 +000019mod artifact_signer;
Alan Stokes223a7462022-01-20 14:12:24 +000020mod blob_encryption;
Victor Hsieh51789de2021-08-06 16:50:49 -070021mod compilation;
Alan Stokes9e2c5d52021-07-21 11:29:10 +010022mod compsvc;
Alan Stokes71cc11e2022-01-17 10:39:05 +000023mod dice;
Victor Hsieh9ed27182021-08-25 15:52:42 -070024mod fsverity;
Alan Stokesc33d2922022-01-18 14:17:00 +000025mod signing_key;
Alan Stokes9e2c5d52021-07-21 11:29:10 +010026
Alan Stokesb5c60b42021-09-09 14:44:13 +010027use android_system_virtualmachineservice::{
28 aidl::android::system::virtualmachineservice::IVirtualMachineService::{
29 IVirtualMachineService, VM_BINDER_SERVICE_PORT,
30 },
31 binder::Strong,
32};
33use anyhow::{anyhow, bail, Context, Result};
34use binder::{
Alan Stokescd359bb2021-10-08 18:22:42 +010035 unstable_api::{new_spibinder, AIBinder},
Alan Stokesb5c60b42021-09-09 14:44:13 +010036 FromIBinder,
37};
Alan Stokescd359bb2021-10-08 18:22:42 +010038use binder_common::rpc_server::run_rpc_server;
Alan Stokes17fd36a2021-09-06 17:22:37 +010039use compos_common::COMPOS_VSOCK_PORT;
Alan Stokesb5c60b42021-09-09 14:44:13 +010040use log::{debug, error};
Alan Stokesb5c60b42021-09-09 14:44:13 +010041
42/// The CID representing the host VM
43const VMADDR_CID_HOST: u32 = 2;
Alan Stokes9e2c5d52021-07-21 11:29:10 +010044
Alan Stokese1b6e1c2021-10-01 12:44:49 +010045fn main() {
46 if let Err(e) = try_main() {
47 error!("failed with {:?}", e);
48 std::process::exit(1);
49 }
50}
51
52fn try_main() -> Result<()> {
Alan Stokesf03d81a2021-09-20 17:44:03 +010053 let args = clap::App::new("compsvc")
54 .arg(clap::Arg::with_name("log_to_stderr").long("log_to_stderr"))
55 .get_matches();
56 if args.is_present("log_to_stderr") {
57 env_logger::builder().filter_level(log::LevelFilter::Debug).init();
58 } else {
59 android_logger::init_once(
60 android_logger::Config::default().with_tag("compsvc").with_min_level(log::Level::Debug),
61 );
62 }
Alan Stokes9e2c5d52021-07-21 11:29:10 +010063
Alan Stokescd359bb2021-10-08 18:22:42 +010064 let service = compsvc::new_binder()?.as_binder();
65 let vm_service = get_vm_service()?;
Alan Stokescd359bb2021-10-08 18:22:42 +010066
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070067 debug!("compsvc is starting as a rpc service.");
Alan Stokesb5c60b42021-09-09 14:44:13 +010068
Alan Stokescd359bb2021-10-08 18:22:42 +010069 let retval = run_rpc_server(service, COMPOS_VSOCK_PORT, || {
Inseob Kimc7d28c72021-10-25 14:28:10 +000070 if let Err(e) = vm_service.notifyPayloadReady() {
Alan Stokescd359bb2021-10-08 18:22:42 +010071 error!("Unable to notify ready: {}", e);
72 }
73 });
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070074 if retval {
75 debug!("RPC server has shut down gracefully");
76 Ok(())
Alan Stokes9e2c5d52021-07-21 11:29:10 +010077 } else {
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070078 bail!("Premature termination of RPC server");
Alan Stokes9e2c5d52021-07-21 11:29:10 +010079 }
80}
Alan Stokesb5c60b42021-09-09 14:44:13 +010081
Alan Stokescd359bb2021-10-08 18:22:42 +010082fn get_vm_service() -> Result<Strong<dyn IVirtualMachineService>> {
83 // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership
84 // can be safely taken by new_spibinder.
85 let ibinder = unsafe {
86 new_spibinder(binder_rpc_unstable_bindgen::RpcClient(
87 VMADDR_CID_HOST,
88 VM_BINDER_SERVICE_PORT as u32,
89 ) as *mut AIBinder)
90 }
91 .ok_or_else(|| anyhow!("Failed to connect to IVirtualMachineService"))?;
92
93 FromIBinder::try_from(ibinder).context("Connecting to IVirtualMachineService")
Alan Stokesb5c60b42021-09-09 14:44:13 +010094}