1 // Copyright 2023, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Applies debug policies when booting microdroid
16 
17 use rustutils::system_properties;
18 use rustutils::system_properties::error::PropertyWatcherError;
19 use std::fs::File;
20 use std::io::Read;
21 
22 /// Get debug policy value in bool. It's true iff the value is explicitly set to <1>.
get_debug_policy_bool(path: &'static str) -> Option<bool>23 fn get_debug_policy_bool(path: &'static str) -> Option<bool> {
24     let mut file = File::open(path).ok()?;
25     let mut log: [u8; 4] = Default::default();
26     file.read_exact(&mut log).ok()?;
27     // DT spec uses big endian although Android is always little endian.
28     Some(u32::from_be_bytes(log) == 1)
29 }
30 
main() -> Result<(), PropertyWatcherError>31 fn main() -> Result<(), PropertyWatcherError> {
32     // If VM is debuggable or debug policy says so, send logs to outside ot the VM via the serial
33     // console. Otherwise logs are internally consumed at /dev/null
34     let log_path = if system_properties::read_bool("ro.boot.microdroid.debuggable", false)?
35         || get_debug_policy_bool("/proc/device-tree/avf/guest/common/log").unwrap_or_default()
36     {
37         "/dev/hvc2"
38     } else {
39         "/dev/null"
40     };
41     system_properties::write("ro.log.file_logger.path", log_path)?;
42 
43     let (adbd_enabled, debuggable) = if system_properties::read_bool("ro.boot.adb.enabled", false)?
44         || get_debug_policy_bool("/proc/device-tree/avf/guest/microdroid/adb").unwrap_or_default()
45     {
46         // debuggable is required for adb root and bypassing adb authorization.
47         ("1", "1")
48     } else {
49         ("0", "0")
50     };
51     system_properties::write("init_debug_policy.adbd.enabled", adbd_enabled)?;
52     system_properties::write("ro.debuggable", debuggable)?;
53 
54     Ok(())
55 }
56