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