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::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 console.
33 // 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("/sys/firmware/devicetree/base/avf/guest/common/log")
36 .unwrap_or_default()
37 {
38 "/dev/hvc2"
39 } else {
40 "/dev/null"
41 };
42 system_properties::write("ro.log.file_logger.path", log_path)?;
43
44 let (adbd_enabled, debuggable) = if system_properties::read_bool("ro.boot.adb.enabled", false)?
45 || get_debug_policy_bool("/sys/firmware/devicetree/base/avf/guest/microdroid/adb")
46 .unwrap_or_default()
47 {
48 // debuggable is required for adb root and bypassing adb authorization.
49 ("1", "1")
50 } else {
51 ("0", "0")
52 };
53 system_properties::write("init_debug_policy.adbd.enabled", adbd_enabled)?;
54 system_properties::write("ro.debuggable", debuggable)?;
55
56 Ok(())
57 }
58