Mårten Kongstad | 433fab9 | 2023-09-22 11:08:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | |
| 17 | //! `printflags` is a device binary to print feature flags. |
| 18 | |
Dennis Shen | 277e5dc | 2024-01-23 18:01:52 +0000 | [diff] [blame] | 19 | use aconfig_protos::ProtoFlagState as State; |
Mårten Kongstad | 6fdaa11 | 2024-01-30 10:39:04 +0100 | [diff] [blame] | 20 | use aconfig_protos::ProtoParsedFlags; |
Mårten Kongstad | 6e61f84 | 2023-10-10 10:12:29 +0200 | [diff] [blame] | 21 | use anyhow::{bail, Context, Result}; |
Mårten Kongstad | 3bb7988 | 2023-09-26 13:06:22 +0200 | [diff] [blame] | 22 | use regex::Regex; |
Atneya Nair | a3a6bfb | 2023-12-14 00:07:37 +0000 | [diff] [blame] | 23 | use std::collections::BTreeMap; |
Mårten Kongstad | 433fab9 | 2023-09-22 11:08:33 +0200 | [diff] [blame] | 24 | use std::collections::HashMap; |
Mårten Kongstad | 3bb7988 | 2023-09-26 13:06:22 +0200 | [diff] [blame] | 25 | use std::process::Command; |
| 26 | use std::{fs, str}; |
| 27 | |
| 28 | fn parse_device_config(raw: &str) -> HashMap<String, String> { |
| 29 | let mut flags = HashMap::new(); |
| 30 | let regex = Regex::new(r"(?m)^([[[:alnum:]]_]+/[[[:alnum:]]_\.]+)=(true|false)$").unwrap(); |
| 31 | for capture in regex.captures_iter(raw) { |
| 32 | let key = capture.get(1).unwrap().as_str().to_string(); |
| 33 | let value = match capture.get(2).unwrap().as_str() { |
| 34 | "true" => format!("{:?} (device_config)", State::ENABLED), |
| 35 | "false" => format!("{:?} (device_config)", State::DISABLED), |
| 36 | _ => panic!(), |
| 37 | }; |
| 38 | flags.insert(key, value); |
| 39 | } |
| 40 | flags |
| 41 | } |
Mårten Kongstad | 433fab9 | 2023-09-22 11:08:33 +0200 | [diff] [blame] | 42 | |
Mårten Kongstad | 6e61f84 | 2023-10-10 10:12:29 +0200 | [diff] [blame] | 43 | fn xxd(bytes: &[u8]) -> String { |
| 44 | let n = 8.min(bytes.len()); |
| 45 | let mut v = Vec::with_capacity(n); |
| 46 | for byte in bytes.iter().take(n) { |
| 47 | v.push(format!("{:02x}", byte)); |
| 48 | } |
| 49 | let trailer = match bytes.len() { |
| 50 | 0..=8 => "", |
| 51 | _ => " ..", |
| 52 | }; |
| 53 | format!("[{}{}]", v.join(" "), trailer) |
| 54 | } |
| 55 | |
Mårten Kongstad | 433fab9 | 2023-09-22 11:08:33 +0200 | [diff] [blame] | 56 | fn main() -> Result<()> { |
Mårten Kongstad | 3bb7988 | 2023-09-26 13:06:22 +0200 | [diff] [blame] | 57 | // read device_config |
| 58 | let output = Command::new("/system/bin/device_config").arg("list").output()?; |
| 59 | if !output.status.success() { |
| 60 | let reason = match output.status.code() { |
| 61 | Some(code) => format!("exit code {}", code), |
| 62 | None => "terminated by signal".to_string(), |
| 63 | }; |
| 64 | bail!("failed to execute device_config: {}", reason); |
| 65 | } |
| 66 | let dc_stdout = str::from_utf8(&output.stdout)?; |
| 67 | let device_config_flags = parse_device_config(dc_stdout); |
| 68 | |
| 69 | // read aconfig_flags.pb files |
Zhi Dou | 5c912f4 | 2024-03-04 07:23:23 +0000 | [diff] [blame] | 70 | let apex_pattern = Regex::new(r"^/apex/[^@]+\.[^@]+$").unwrap(); |
| 71 | let mut mount_points = vec![ |
| 72 | "system".to_string(), |
| 73 | "system_ext".to_string(), |
| 74 | "product".to_string(), |
| 75 | "vendor".to_string(), |
| 76 | ]; |
| 77 | for apex in fs::read_dir("/apex")? { |
| 78 | let path_name = apex?.path().display().to_string(); |
| 79 | if let Some(canonical_path) = apex_pattern.captures(&path_name) { |
| 80 | mount_points.push(canonical_path.get(0).unwrap().as_str().to_owned()); |
| 81 | } |
| 82 | } |
| 83 | |
Atneya Nair | a3a6bfb | 2023-12-14 00:07:37 +0000 | [diff] [blame] | 84 | let mut flags: BTreeMap<String, Vec<String>> = BTreeMap::new(); |
Zhi Dou | 5c912f4 | 2024-03-04 07:23:23 +0000 | [diff] [blame] | 85 | for mount_point in mount_points { |
| 86 | let path = format!("/{}/etc/aconfig_flags.pb", mount_point); |
Mårten Kongstad | 433fab9 | 2023-09-22 11:08:33 +0200 | [diff] [blame] | 87 | let Ok(bytes) = fs::read(&path) else { |
| 88 | eprintln!("warning: failed to read {}", path); |
| 89 | continue; |
| 90 | }; |
Mårten Kongstad | 6e61f84 | 2023-10-10 10:12:29 +0200 | [diff] [blame] | 91 | let parsed_flags: ProtoParsedFlags = protobuf::Message::parse_from_bytes(&bytes) |
| 92 | .with_context(|| { |
| 93 | format!("failed to parse {} ({}, {} byte(s))", path, xxd(&bytes), bytes.len()) |
| 94 | })?; |
Mårten Kongstad | 433fab9 | 2023-09-22 11:08:33 +0200 | [diff] [blame] | 95 | for flag in parsed_flags.parsed_flag { |
Mårten Kongstad | 3bb7988 | 2023-09-26 13:06:22 +0200 | [diff] [blame] | 96 | let key = format!("{}/{}.{}", flag.namespace(), flag.package(), flag.name()); |
Zhi Dou | 5c912f4 | 2024-03-04 07:23:23 +0000 | [diff] [blame] | 97 | let value = format!("{:?} + {:?} ({})", flag.permission(), flag.state(), mount_point); |
Mårten Kongstad | 433fab9 | 2023-09-22 11:08:33 +0200 | [diff] [blame] | 98 | flags.entry(key).or_default().push(value); |
| 99 | } |
| 100 | } |
Mårten Kongstad | 3bb7988 | 2023-09-26 13:06:22 +0200 | [diff] [blame] | 101 | |
| 102 | // print flags |
| 103 | for (key, mut value) in flags { |
Mårten Kongstad | 3bb7988 | 2023-09-26 13:06:22 +0200 | [diff] [blame] | 104 | if let Some(dc_value) = device_config_flags.get(&key) { |
| 105 | value.push(dc_value.to_string()); |
| 106 | } |
Atneya Nair | a3a6bfb | 2023-12-14 00:07:37 +0000 | [diff] [blame] | 107 | println!("{}: {}", key, value.join(", ")); |
Mårten Kongstad | 433fab9 | 2023-09-22 11:08:33 +0200 | [diff] [blame] | 108 | } |
Mårten Kongstad | 3bb7988 | 2023-09-26 13:06:22 +0200 | [diff] [blame] | 109 | |
Mårten Kongstad | 433fab9 | 2023-09-22 11:08:33 +0200 | [diff] [blame] | 110 | Ok(()) |
| 111 | } |
Mårten Kongstad | 3bb7988 | 2023-09-26 13:06:22 +0200 | [diff] [blame] | 112 | |
| 113 | #[cfg(test)] |
| 114 | mod tests { |
| 115 | use super::*; |
| 116 | |
| 117 | #[test] |
Mårten Kongstad | 6e61f84 | 2023-10-10 10:12:29 +0200 | [diff] [blame] | 118 | fn test_parse_device_config() { |
Mårten Kongstad | 3bb7988 | 2023-09-26 13:06:22 +0200 | [diff] [blame] | 119 | let input = r#" |
| 120 | namespace_one/com.foo.bar.flag_one=true |
| 121 | namespace_one/com.foo.bar.flag_two=false |
| 122 | random_noise; |
| 123 | namespace_two/android.flag_one=true |
| 124 | namespace_two/android.flag_two=nonsense |
| 125 | "#; |
| 126 | let expected = HashMap::from([ |
| 127 | ( |
| 128 | "namespace_one/com.foo.bar.flag_one".to_string(), |
| 129 | "ENABLED (device_config)".to_string(), |
| 130 | ), |
| 131 | ( |
| 132 | "namespace_one/com.foo.bar.flag_two".to_string(), |
| 133 | "DISABLED (device_config)".to_string(), |
| 134 | ), |
| 135 | ("namespace_two/android.flag_one".to_string(), "ENABLED (device_config)".to_string()), |
| 136 | ]); |
| 137 | let actual = parse_device_config(input); |
| 138 | assert_eq!(expected, actual); |
| 139 | } |
Mårten Kongstad | 6e61f84 | 2023-10-10 10:12:29 +0200 | [diff] [blame] | 140 | |
| 141 | #[test] |
| 142 | fn test_xxd() { |
| 143 | let input = [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9]; |
| 144 | assert_eq!("[]", &xxd(&input[0..0])); |
| 145 | assert_eq!("[00]", &xxd(&input[0..1])); |
| 146 | assert_eq!("[00 01]", &xxd(&input[0..2])); |
| 147 | assert_eq!("[00 01 02 03 04 05 06]", &xxd(&input[0..7])); |
| 148 | assert_eq!("[00 01 02 03 04 05 06 07]", &xxd(&input[0..8])); |
| 149 | assert_eq!("[00 01 02 03 04 05 06 07 ..]", &xxd(&input[0..9])); |
| 150 | assert_eq!("[00 01 02 03 04 05 06 07 ..]", &xxd(&input)); |
| 151 | } |
Mårten Kongstad | 3bb7988 | 2023-09-26 13:06:22 +0200 | [diff] [blame] | 152 | } |