128 lines
3.8 KiB
Rust
128 lines
3.8 KiB
Rust
// This file is part of Deja-Vu.
|
|
//
|
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
|
// under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
// See the GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
|
#![allow(clippy::missing_errors_doc)]
|
|
#![allow(clippy::missing_panics_doc)]
|
|
|
|
use super::{disk::PartitionTableType, drivers::Driver};
|
|
use crate::tasks::TaskType;
|
|
use color_eyre::Result;
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
pub enum SafeMode {
|
|
#[default]
|
|
Disable,
|
|
Enable,
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn configure_disk(
|
|
letter_boot: &str,
|
|
letter_os: &str,
|
|
safe_mode: SafeMode,
|
|
system32: &str,
|
|
table_type: &PartitionTableType,
|
|
) -> Vec<TaskType> {
|
|
let mut tasks = Vec::new();
|
|
|
|
// Create
|
|
tasks.push(TaskType::CommandWait(
|
|
PathBuf::from(format!("{system32}/bcdboot.exe")),
|
|
vec![
|
|
format!("{letter_os}:\\Windows"),
|
|
String::from("/s"),
|
|
format!("{letter_boot}:"),
|
|
String::from("/f"),
|
|
String::from(match table_type {
|
|
PartitionTableType::Guid => "UEFI",
|
|
PartitionTableType::Legacy => "BIOS",
|
|
}),
|
|
],
|
|
));
|
|
|
|
// Update boot sector (for legacy setups)
|
|
if *table_type == PartitionTableType::Legacy {
|
|
tasks.push(TaskType::CommandWait(
|
|
PathBuf::from(format!("{system32}/bootsect.exe")),
|
|
vec![
|
|
String::from("/nt60"),
|
|
format!("{letter_boot}:"),
|
|
String::from("/force"),
|
|
String::from("/mbr"),
|
|
],
|
|
));
|
|
}
|
|
|
|
// Lock in safe mode (if needed)
|
|
if safe_mode == SafeMode::Enable {
|
|
tasks.push(
|
|
set_mode(letter_boot, &SafeMode::Enable, system32, table_type)
|
|
.expect("Failed to create set_mode task."),
|
|
);
|
|
}
|
|
|
|
// Done
|
|
tasks
|
|
}
|
|
|
|
pub fn inject_driver(driver: &Driver, letter_os: &str, system32: &str) -> Result<TaskType> {
|
|
//if let Some(driver_path_str) = driver.path.to_str() {
|
|
let driver_path = driver.path.to_str().unwrap();
|
|
Ok(TaskType::CommandWait(
|
|
PathBuf::from(format!("{system32}/dism.exe")),
|
|
vec![
|
|
format!("/image:{letter_os}:\\"),
|
|
String::from("/add-driver"),
|
|
format!("/driver:{driver_path}"),
|
|
String::from("/recurse"),
|
|
],
|
|
))
|
|
}
|
|
|
|
pub fn set_mode(
|
|
letter_boot: &str,
|
|
mode: &SafeMode,
|
|
system32: &str,
|
|
table_type: &PartitionTableType,
|
|
) -> Result<TaskType> {
|
|
let bcd_path = match table_type {
|
|
PartitionTableType::Guid => {
|
|
format!("{letter_boot}:\\EFI\\Microsoft\\Boot\\BCD")
|
|
}
|
|
PartitionTableType::Legacy => {
|
|
format!("{letter_boot}:\\Boot\\BCD")
|
|
}
|
|
};
|
|
|
|
// Build CommandWait
|
|
let mut cmd_args = vec![String::from("/store"), bcd_path];
|
|
match mode {
|
|
SafeMode::Disable => {
|
|
cmd_args.push(String::from("/deletevalue"));
|
|
cmd_args.push(String::from("{default}"));
|
|
cmd_args.push(String::from("safeboot"));
|
|
}
|
|
SafeMode::Enable => {
|
|
cmd_args.push(String::from("/set"));
|
|
cmd_args.push(String::from("{default}"));
|
|
cmd_args.push(String::from("safeboot"));
|
|
cmd_args.push(String::from("minimal"));
|
|
}
|
|
}
|
|
Ok(TaskType::CommandWait(
|
|
PathBuf::from(format!("{system32}/bcdedit.exe")),
|
|
cmd_args,
|
|
))
|
|
}
|