asahi-nvram-0.2.2/Cargo.lock0000644000000133770000000000100112310ustar # 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 = "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-nvram" version = "0.2.2" dependencies = [ "apple-nvram", "clap", ] [[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 = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[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 = "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 = "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 = "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-nvram-0.2.2/Cargo.toml0000644000000015320000000000100112420ustar # 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-nvram" version = "0.2.2" description = "A tool to read and write nvram variables 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"] asahi-nvram-0.2.2/Cargo.toml.orig000064400000000000000000000010031046102023000147140ustar 00000000000000[package] name = "asahi-nvram" version = "0.2.2" edition = "2021" license = "MIT" description = "A tool to read and write nvram variables on ARM Macs" homepage = "https://github.com/WhatAmISupposedToPutHere/asahi-nvram" repository = "https://github.com/WhatAmISupposedToPutHere/asahi-nvram" # 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-nvram-0.2.2/LICENSE000064400000000000000000000020741046102023000130430ustar 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-nvram-0.2.2/src/main.rs000064400000000000000000000110221046102023000141100ustar 00000000000000// SPDX-License-Identifier: MIT use std::{borrow::Cow, fs::OpenOptions, io::Read, process::ExitCode}; use apple_nvram::{nvram_parse, VarType}; #[derive(Debug)] #[allow(dead_code)] enum Error { Parse, SectionTooBig, ApplyError(std::io::Error), MissingPartitionName, MissingValue, VariableNotFound, UnknownPartition, InvalidHex, } 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), } } } type Result = std::result::Result; fn main() -> ExitCode { match real_main() { Ok(_) => ExitCode::SUCCESS, Err(e) => { eprintln!("{:?}", e); ExitCode::FAILURE } } } fn real_main() -> Result<()> { let matches = clap::command!() .arg(clap::arg!(-d --device [DEVICE] "Path to the nvram device.")) .subcommand( clap::Command::new("read") .about("Read nvram variables") .arg(clap::Arg::new("variable").multiple_values(true)), ) .subcommand( clap::Command::new("delete") .about("Delete nvram variables") .arg(clap::Arg::new("variable").multiple_values(true)), ) .subcommand( clap::Command::new("write") .about("Write nvram variables") .arg(clap::Arg::new("variable=value").multiple_values(true)), ) .get_matches(); let default_name = "/dev/mtd0".to_owned(); let mut file = OpenOptions::new() .read(true) .write(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)?; match matches.subcommand() { Some(("read", args)) => { let active = nv.active_part_mut(); let vars = args.get_many::("variable"); if let Some(vars) = vars { for var in vars { let (part, name) = var.split_once(':').ok_or(Error::MissingPartitionName)?; let typ = part_by_name(part)?; let v = active .get_variable(name.as_bytes(), typ) .ok_or(Error::VariableNotFound)?; println!("{}", v); } } else { for var in active.variables() { println!("{}", var); } } } Some(("write", args)) => { let vars = args.get_many::("variable=value"); nv.prepare_for_write(); let active = nv.active_part_mut(); for var in vars.unwrap_or_default() { let (key, value) = var.split_once('=').ok_or(Error::MissingValue)?; let (part, name) = key.split_once(':').ok_or(Error::MissingPartitionName)?; let typ = part_by_name(part)?; active.insert_variable(name.as_bytes(), Cow::Owned(read_var(value)?), typ); } nv.apply(&mut file)?; } Some(("delete", args)) => { let vars = args.get_many::("variable"); nv.prepare_for_write(); let active = nv.active_part_mut(); for var in vars.unwrap_or_default() { let (part, name) = var.split_once(':').ok_or(Error::MissingPartitionName)?; let typ = part_by_name(part)?; active.remove_variable(name.as_bytes(), typ); } nv.apply(&mut file)?; } _ => {} } Ok(()) } fn part_by_name(name: &str) -> Result { match name { "common" => Ok(VarType::Common), "system" => Ok(VarType::System), _ => Err(Error::UnknownPartition), } } fn read_var(val: &str) -> Result> { let val = val.as_bytes(); let mut ret = Vec::new(); let mut i = 0; while i < val.len() { if val[i] == b'%' { ret.push( u8::from_str_radix( unsafe { std::str::from_utf8_unchecked(&val[i + 1..i + 3]) }, 16, ) .map_err(|_| Error::InvalidHex)?, ); i += 2; } else { ret.push(val[i]) } i += 1; } Ok(ret) }