more refactoring

This commit is contained in:
pantonshire committed 2025-04-06 14:27:55 +01:00
1 parent 909875ec5b
commit 2219fac4e5
30 files changed
+438 -1456

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
use std::{env, error::Error, fmt, path::{Path, PathBuf}, process::ExitCode};
mod mkimg;
fn main() -> ExitCode {
match xtask() {
Ok(()) => ExitCode::SUCCESS,
Err(err) => {
eprintln!("{}", err);
ExitCode::FAILURE
}
}
}
fn xtask() -> Result<(), XtaskError> {
let mut args = env::args_os().skip(1);
let task = args
.next()
.ok_or_else(|| XtaskError::with_message("no task provided".to_owned()))?;
let task = task.to_str().ok_or_else(|| {
XtaskError::with_message(format!("invalid utf-8 \"{}\"", task.to_string_lossy()))
})?;
let manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").ok_or_else(|| {
XtaskError::with_message("CARGO_MANIFEST_DIR not set".to_owned())
})?);
let workspace_dir = manifest_dir.parent().ok_or_else(|| {
XtaskError::with_message("invalid CARGO_MANIFEST_DIR".to_owned())
})?;
let ctx = Context {
workspace: workspace_dir,
};
match task {
"mkimg" => mkimg::mkimg_bios_gpt(ctx),
_ => Err(XtaskError::with_message(format!("unknown task \"{}\"", task))),
}
}
struct Context<'a> {
workspace: &'a Path,
}
#[derive(Debug)]
struct XtaskError {
message: String,
parent: Option<Box<dyn Error>>,
}
impl XtaskError {
fn with_message(message: String) -> Self {
Self {
message,
parent: None,
}
}
fn wrap_with_message<E>(err: E, message: String) -> Self
where
E: Error + 'static,
{
Self {
message,
parent: Some(Box::new(err)),
}
}
}
impl fmt::Display for XtaskError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)?;
if let Some(parent) = &self.parent {
write!(f, ": {}", parent)?;
}
Ok(())
}
}
impl Error for XtaskError {}
+32
View File
@@ -0,0 +1,32 @@
use crate::{Context, XtaskError};
const BLOCK_SIZE: u64 = 512;
const PARTITION_ENTRY_SIZE: u64 = 128;
const ATTR_REQUIRED: u64 = 1 >> 0;
const ATTR_NO_BLOCK_IO_PROTO: u64 = 1 >> 1;
const ATTR_LEGACY_BIOS_BOOTABLE: u64 = 1 >> 2;
// LBA 0: MBR
// LBA 1: partition header
// LBA 2..33: partition table entries
// LBA 34..n: usable blocks
// LBA -33..-2: partition table entries (dup)
// LBA -1: partition header (dup)
struct Partition {
type_guid: [u8; 16],
part_guid: [u8; 16],
lba_start: u64,
lba_end: u64,
attr: u64,
name: String,
}
pub fn mkimg_bios_gpt(ctx: Context) -> Result<(), XtaskError> {
println!("dir={}", ctx.workspace.to_string_lossy());
Ok(())
}