asahi-btsync-0.2.1/Cargo.lock0000644000000165350000000000100114060ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "adler32" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" [[package]] name = "ahash" version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" dependencies = [ "getrandom", "once_cell", "version_check", ] [[package]] name = "apple-nvram" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86c1056f6f4803156772f3369ae54d01cdfa36e901a1a271c620c3d445ae7eab" dependencies = [ "adler32", "crc32fast", "nix", ] [[package]] name = "asahi-btsync" version = "0.2.1" dependencies = [ "apple-nvram", "clap", "rust-ini", ] [[package]] name = "atty" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ "hermit-abi", "libc", "winapi", ] [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" version = "3.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ "atty", "bitflags", "clap_lex", "indexmap", "once_cell", "strsim", "termcolor", "textwrap", ] [[package]] name = "clap_lex" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" dependencies = [ "os_str_bytes", ] [[package]] name = "crc32fast" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" dependencies = [ "cfg-if", ] [[package]] name = "dlv-list" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" [[package]] name = "getrandom" version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ "cfg-if", "libc", "wasi", ] [[package]] name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" dependencies = [ "ahash", ] [[package]] name = "hermit-abi" version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ "libc", ] [[package]] name = "indexmap" version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", "hashbrown", ] [[package]] name = "libc" version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "memoffset" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" dependencies = [ "autocfg", ] [[package]] name = "nix" version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a" dependencies = [ "bitflags", "cfg-if", "libc", "memoffset", "pin-utils", "static_assertions", ] [[package]] name = "once_cell" version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "ordered-multimap" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a" dependencies = [ "dlv-list", "hashbrown", ] [[package]] name = "os_str_bytes" version = "6.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d5d9eb14b174ee9aa2ef96dc2b94637a2d4b6e7cb873c7e171f0c20c6cf3eac" [[package]] name = "pin-utils" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "rust-ini" version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df" dependencies = [ "cfg-if", "ordered-multimap", ] [[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "strsim" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "termcolor" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" dependencies = [ "winapi-util", ] [[package]] name = "textwrap" version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ "winapi", ] [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" asahi-btsync-0.2.1/Cargo.toml0000644000000016170000000000100114240ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "asahi-btsync" version = "0.2.1" description = "A tool to sync Bluetooth pairing keys with macos on ARM Macs" homepage = "https://github.com/WhatAmISupposedToPutHere/asahi-nvram" license = "MIT" repository = "https://github.com/WhatAmISupposedToPutHere/asahi-nvram" [dependencies.apple-nvram] version = "0.3" [dependencies.clap] version = "3" features = ["cargo"] [dependencies.rust-ini] version = "0.18.0" asahi-btsync-0.2.1/Cargo.toml.orig000064400000000000000000000010601046102023000150750ustar 00000000000000[package] name = "asahi-btsync" license = "MIT" version = "0.2.1" edition = "2021" description = "A tool to sync Bluetooth pairing keys with macos on ARM Macs" homepage = "https://github.com/WhatAmISupposedToPutHere/asahi-nvram" repository = "https://github.com/WhatAmISupposedToPutHere/asahi-nvram" [dependencies] rust-ini = "0.18.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies.apple-nvram] path = "../apple-nvram" version = "0.3" [dependencies.clap] version = "3" features = ["cargo"] asahi-btsync-0.2.1/LICENSE000064400000000000000000000020741046102023000132210ustar 00000000000000MIT License Copyright (c) 2022 The Asahi Linux Contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.asahi-btsync-0.2.1/src/main.rs000064400000000000000000000154571046102023000143060ustar 00000000000000/* SPDX-License-Identifier: MIT */ use std::{ env, fmt::Debug, fs, fs::OpenOptions, io::{self, stdout, Read, Write}, path::Path, }; use apple_nvram::{nvram_parse, VarType, Variable}; use ini::Ini; #[derive(Debug)] #[allow(dead_code)] enum Error { Parse, SectionTooBig, ApplyError(std::io::Error), VariableNotFound, FileIO, BluezConfigDirNotFound, SliceError, } impl From for Error { fn from(e: apple_nvram::Error) -> Self { match e { apple_nvram::Error::ParseError => Error::Parse, apple_nvram::Error::SectionTooBig => Error::SectionTooBig, apple_nvram::Error::ApplyError(e) => Error::ApplyError(e), } } } impl From for Error { fn from(_e: io::Error) -> Self { Error::FileIO } } impl From for Error { fn from(_e: std::array::TryFromSliceError) -> Self { Error::SliceError } } type Result = std::result::Result; fn main() { real_main().unwrap(); } fn real_main() -> Result<()> { let matches = clap::command!() .arg(clap::arg!(-d --device [DEVICE] "Path to the nvram device.")) .subcommand(clap::Command::new("list").about("Parse shared Bluetooth keys from nvram")) .subcommand( clap::Command::new("sync") .about("Sync Bluetooth device information from nvram") .arg(clap::arg!(-c --config [CONFIG] "Bluez config path.")) .arg(clap::Arg::new("variable").multiple_values(true)), ) .subcommand( clap::Command::new("dump").about("Dump binary Bluetooth device info from nvram"), ) .get_matches(); let default_name = "/dev/mtd0ro".to_owned(); let default_config = "/var/lib/bluetooth".to_owned(); let bt_var = "BluetoothUHEDevices"; let mut file = OpenOptions::new() .read(true) .open(matches.get_one::("device").unwrap_or(&default_name)) .unwrap(); let mut data = Vec::new(); file.read_to_end(&mut data).unwrap(); let mut nv = nvram_parse(&data)?; let active = nv.active_part_mut(); let bt_devs = active .get_variable(bt_var.as_bytes(), VarType::System) .ok_or(Error::VariableNotFound)?; match matches.subcommand() { Some(("list", _args)) => { print_btkeys(bt_devs).expect("Failed to parse bt device info"); } Some(("sync", args)) => { sync_btkeys( bt_devs, args.get_one::("config").unwrap_or(&default_config), ) .expect("Failed to sync bt device info"); } Some(("dump", _args)) => { dump(bt_devs).expect("Failed to dump bt device info"); } _ => { print_btkeys(bt_devs).expect("Failed to parse bt device info"); } } Ok(()) } fn dump(var: &dyn Variable) -> Result<()> { stdout().write_all(&var.value())?; Ok(()) } struct BtDevice { mac: [u8; 6], class: u16, name: String, vendor_id: u16, product_id: u16, pairing_key: [u8; 16], } struct BtInfo { mac: [u8; 6], devices: Vec, } fn read_le_u16(input: &mut &[u8]) -> Result { let (int_bytes, rest) = input.split_at(std::mem::size_of::()); *input = rest; Ok(u16::from_le_bytes(int_bytes.try_into()?)) } fn parse_bt_device(input: &mut &[u8]) -> Result { // parse MAC let (mac_bytes, remain) = input.split_at(6_usize); *input = remain; let mac: [u8; 6] = mac_bytes.try_into().expect("end of bytes"); let class = read_le_u16(input)?; // skip 2 bytes *input = &input[2..]; // parse device name (u16_le length + \0 terminated utf-8 string) let name_len = read_le_u16(input)? as usize; let (name_bytes, remain) = input.split_at(name_len); *input = remain; let name = String::from_utf8_lossy(&name_bytes[..name_len - 1]).to_string(); // parse pairing key let (key_bytes, remain) = input.split_at(16); *input = remain; let key: [u8; 16] = key_bytes.try_into().expect("end of bytes"); // parse product / vendor id let product_id = read_le_u16(input)?; let vendor_id = read_le_u16(input)?; // skip 2 unknown trailing bytes *input = &input[2..]; Ok(BtDevice { mac, class, name, vendor_id, product_id, pairing_key: key, }) } fn parse_bt_info(var: &dyn Variable) -> Result { let data = var.value(); assert!(data.len() >= 8); let adapter_mac: [u8; 6] = data[0..6].try_into()?; let num_devices = data[6]; assert!(data[7] == 0x04); let mut dev_data = &data[8..]; let mut devices: Vec = Vec::new(); for _n in 0..num_devices { devices.push(parse_bt_device(&mut dev_data)?); } Ok(BtInfo { mac: adapter_mac, devices, }) } fn format_mac(mac: &[u8; 6]) -> Result { Ok(mac .iter() .map(|x| format!("{x:02X}")) .collect::>() .join(":")) } fn format_key(key: &[u8; 16]) -> Result { Ok(key.iter().map(|x| format!("{x:02X}")).rev().collect()) } fn print_btkeys(var: &dyn Variable) -> Result<()> { let info = parse_bt_info(var)?; for dev in info.devices { println!( "ID {:04x}:{:04x} {} ({})", dev.vendor_id, dev.product_id, dev.name, format_mac(&dev.mac)? ); } Ok(()) } fn sync_btkeys(var: &dyn Variable, config: &String) -> Result<()> { let config_path = Path::new(config); if !config_path.is_dir() { return Err(Error::BluezConfigDirNotFound); } let info = parse_bt_info(var)?; let adapter_path = config_path.join(format_mac(&info.mac)?); if !adapter_path.is_dir() { fs::create_dir(adapter_path.clone())?; } for dev in info.devices { let dev_path = adapter_path.join(format_mac(&dev.mac)?); if !dev_path.is_dir() { fs::create_dir(dev_path.clone())?; } let info_file = dev_path.as_path().join("info"); if info_file.exists() { continue; } let mut info = Ini::new(); info.with_section(Some("General")) .set("Name", dev.name) .set("Class", format!("{:#08X}", dev.class)) .set("Trusted", "true") .set("Blocked", "false") .set("WakeAllowed", "true"); info.with_section(Some("LinkKey")) .set("Key", format_key(&dev.pairing_key)?); info.with_section(Some("DeviceID")) .set("Vendor", format!("{}", dev.vendor_id)) .set("Product", format!("{}", dev.product_id)); info.write_to_file(info_file)?; println!("{}", format_mac(&dev.mac)?); } Ok(()) }