main.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #[cfg(target_os = "linux")]
  2. fn main() {
  3. use pico_args::Arguments;
  4. use std::{
  5. fs::{self, Permissions},
  6. os::unix::fs::PermissionsExt,
  7. path::PathBuf,
  8. process::{self, Command},
  9. };
  10. // Find output folder from command line arguments
  11. let mut args = Arguments::from_env();
  12. let name: String = match args.value_from_str("--name") {
  13. Ok(Some(name)) => {
  14. println!("output directory is: dumps/{}", name);
  15. name
  16. }
  17. _ => {
  18. eprintln!(
  19. "usage: acpi-dumper --name [NAME]\n\
  20. the acpi tables will be dumped to the dumps/[name] directory"
  21. );
  22. process::exit(-1);
  23. }
  24. };
  25. // Check lshw is installed
  26. match Command::new("lshw").arg("-version").output() {
  27. Ok(output) => {
  28. println!(
  29. "found lshw version: {}",
  30. String::from_utf8(output.stderr)
  31. .expect("lshw returned invalid utf-8")
  32. .rsplit_terminator(' ')
  33. .next()
  34. .unwrap_or("unknown")
  35. .trim_end()
  36. );
  37. }
  38. Err(e) => {
  39. eprintln!("error: lshw failed to execute ({}) - please check that it is installed", e);
  40. process::exit(-1);
  41. }
  42. };
  43. if unsafe { libc::geteuid() } != 0 {
  44. eprintln!("error: please re-run with root privileges");
  45. process::exit(-1);
  46. }
  47. // Create output folder
  48. let mut out_path = PathBuf::from("./dumps");
  49. out_path.push(name);
  50. fs::create_dir(&out_path).expect("failed to create output directory");
  51. fs::set_permissions(&out_path, Permissions::from_mode(0o777))
  52. .expect("failed to set permissions for output directory");
  53. println!("dumping acpi tables...");
  54. // Loop over the ACPI tables, dump their contents
  55. for file in fs::read_dir("/sys/firmware/acpi/tables")
  56. .expect("failed to access acpi tables, check permissions")
  57. .filter_map(|entry| entry.ok()) // Ignore failures
  58. .filter(|entry| entry.metadata().unwrap().is_file())
  59. {
  60. let table_signature = file.file_name().into_string().expect("invalid utf-8 file name in acpi tables");
  61. // All tables have a 4 letter signature (apart from SSDTs)
  62. if table_signature.len() == 4 || &table_signature[..4] != "SSDT" {
  63. // Read table
  64. let data = fs::read(file.path()).expect("failed to read acpi table");
  65. out_path.push(table_signature + ".bin");
  66. // Dump table to disk
  67. fs::write(&out_path, data).expect("failed to write acpi table");
  68. // Allow anyone to read or write the file
  69. fs::set_permissions(&out_path, Permissions::from_mode(0o666))
  70. .expect("failed to set permissions of dumped acpi table");
  71. out_path.pop();
  72. }
  73. }
  74. println!("done!");
  75. println!("capturing lshw output...");
  76. let lshw_info = Command::new("lshw").output().expect("failed to get hardware information from lshw");
  77. // Write lshw output to disk
  78. let lshw_info = String::from_utf8(lshw_info.stdout).expect("lshw returned invalid utf-8");
  79. out_path.push("lshw.txt");
  80. fs::write(&out_path, lshw_info).expect("failed to write lshw dump");
  81. fs::set_permissions(&out_path, Permissions::from_mode(0o666))
  82. .expect("failed to set permissions of dumped acpi table");
  83. out_path.pop();
  84. println!("done!");
  85. }
  86. #[cfg(not(target_os = "linux"))]
  87. fn main() {
  88. std::compilation_error!("acpi-dumper currently only supports linux");
  89. }