Added builder

This commit is contained in:
2024-12-18 19:23:12 -08:00
parent d42db82558
commit 8b3a1e658a
48 changed files with 1214 additions and 292 deletions

9
builder/.editorconfig Normal file
View File

@ -0,0 +1,9 @@
root = true
[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false

1
builder/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

316
builder/Cargo.lock generated Normal file
View File

@ -0,0 +1,316 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "anstream"
version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
[[package]]
name = "anstyle-parse"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "builder"
version = "0.1.0"
dependencies = [
"anstyle",
"clap",
"gpt",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"
[[package]]
name = "colorchoice"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "crc"
version = "3.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69e6e4d7b33a94f0991c26729976b10ebde1d34c3ee82408fb536164fa10d636"
dependencies = [
"crc-catalog",
]
[[package]]
name = "crc-catalog"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "gpt"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffa5448a0d9d541f1840c0e1b5fe513360861ca83c4b920619f54efe277f9254"
dependencies = [
"bitflags",
"crc",
"simple-bytes",
"uuid",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "libc"
version = "0.2.168"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d"
[[package]]
name = "proc-macro2"
version = "1.0.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "simple-bytes"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c11532d9d241904f095185f35dcdaf930b1427a94d5b01d7002d74ba19b44cc4"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uuid"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

58
builder/Cargo.toml Normal file
View File

@ -0,0 +1,58 @@
[package]
name = "builder"
version = "0.1.0"
edition = "2021"
#
# MARK: lints
#
[workspace.lints.rust]
unused_import_braces = "deny"
unit_bindings = "deny"
single_use_lifetimes = "deny"
non_ascii_idents = "deny"
macro_use_extern_crate = "deny"
elided_lifetimes_in_paths = "deny"
absolute_paths_not_starting_with_crate = "deny"
explicit_outlives_requirements = "warn"
unused_crate_dependencies = "warn"
redundant_lifetimes = "warn"
missing_docs = "allow"
[workspace.lints.clippy]
needless_return = "allow"
new_without_default = "allow"
tabs_in_doc_comments = "allow"
expect_used = "deny"
dbg_macro = "deny"
allow_attributes = "deny"
create_dir = "deny"
filetype_is_file = "deny"
integer_division = "deny"
lossy_float_literal = "deny"
map_err_ignore = "deny"
mutex_atomic = "deny"
needless_raw_strings = "deny"
print_stderr = "deny"
print_stdout = "deny"
str_to_string = "deny"
string_add = "deny"
string_to_string = "deny"
unimplemented = "deny"
use_debug = "deny"
verbose_file_reads = "deny"
large_types_passed_by_value = "deny"
match_on_vec_items = "deny"
wildcard_dependencies = "deny"
negative_feature_names = "deny"
redundant_feature_names = "deny"
#
# MARK: dependencies
#
[dependencies]
anstyle = "1.0.10"
clap = { version = "4.5.23", features = ["derive"] }
gpt = "4.0.0"

1
builder/rustfmt.toml Normal file
View File

@ -0,0 +1 @@
hard_tabs = true

228
builder/src/cmd/disk.rs Normal file
View File

@ -0,0 +1,228 @@
use clap::Subcommand;
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use crate::gpt::{GPTDisk, PartitionType};
use crate::util::parse_bytes_postfix;
#[derive(Subcommand, Debug)]
pub enum DiskCommand {
/// Create an empty file of the given size
Create {
/// The new file's size, in bytes.
/// May be followed by the usual SI prefixes:
/// - K, M, G, T for powers of 10
/// - Kib,Mib,Gib,Tib for powers of 1024
size: String,
},
/// Initialize partition table on the given disk
Init {},
/// Show GPT information.
///
/// By default, this command dumps GPT headers & partitons.
/// Use the flags below to compute other values.
#[command(alias = "i")]
Inspect {
/// Show index of first byte of partition
#[clap(long, group = "inspect-mode", value_name = "PARTITION")]
part_start: Option<u32>,
/// Show index of sector of partition
#[clap(long, group = "inspect-mode", value_name = "PARTITION")]
part_start_sector: Option<u32>,
},
/// Initialize partition table on the given disk
#[command(alias = "addpart")]
AddPartition {
/// The partition's name
name: String,
/// The partition's size, in bytes.
/// May be followed by the usual SI prefixes:
/// - K, M, G, T for powers of 10
/// - Kib,Mib,Gib,Tib for powers of 1024
size: String,
/// The type of this partition
partition_type: PartitionType,
},
/// Write a file to the given partition
#[command(alias = "writepart")]
WritePartition {
/// The index of the partition to write to
part_idx: u32,
/// The data to write
source: PathBuf,
},
/// Write a BIOS bootloader to this disk
SetBootCode {
/// The data to write.
/// This file must contain exactly 440 bytes.
source: PathBuf,
},
}
impl DiskCommand {
pub fn run(&self, file: &Path) {
match self {
Self::Inspect {
part_start,
part_start_sector,
} => {
let file = OpenOptions::new()
.create(false)
.append(false)
.write(false)
.read(true)
.open(file)
.unwrap();
let mut dw = GPTDisk::new(file);
if let Some(part) = part_start {
println!("{}", dw.get_partition(*part).unwrap().start());
} else if let Some(part) = part_start_sector {
assert!(dw.get_partition(*part).unwrap().start() % 512 == 0);
println!("{}", dw.get_partition(*part).unwrap().start() / 512);
} else {
let disk = dw.disk(false);
println!("Disk header: {:#?}", disk.primary_header());
println!("Partition layout: {:#?}", disk.partitions());
}
}
Self::Create { size } => {
let size = match parse_bytes_postfix(&size) {
Some(x) => x,
None => panic!("Bad byte string"),
};
if file.exists() {
panic!("file exists")
}
let mut file = File::create(file).unwrap();
let mut buffer = [0; 1024];
let buffer_len: u64 = buffer.len().try_into().unwrap();
let mut remaining_size = size;
while remaining_size > 0 {
let to_write = remaining_size.min(buffer_len);
let to_write_usize: usize = to_write.try_into().unwrap();
let buffer = &mut buffer[..to_write_usize];
file.write(buffer).unwrap();
remaining_size -= to_write;
}
}
Self::Init {} => {
let file = OpenOptions::new()
.create(false)
.append(false)
.write(true)
.read(true)
.open(file)
.unwrap();
let mut dw = GPTDisk::new(file);
dw.init();
}
Self::AddPartition {
name,
size,
partition_type,
} => {
let file = OpenOptions::new()
.create(false)
.append(false)
.write(true)
.read(true)
.open(file)
.unwrap();
let size = match parse_bytes_postfix(&size) {
Some(x) => x,
None => panic!("Bad byte string"),
};
let mut dw = GPTDisk::new(file);
let mut disk = dw.disk(true);
disk.add_partition(name, size, partition_type.into(), 0, None)
.expect("failed to write disk");
disk.write().unwrap();
}
Self::WritePartition { part_idx, source } => {
let file = OpenOptions::new()
.create(false)
.append(false)
.write(true)
.read(true)
.open(file)
.unwrap();
let mut dw = GPTDisk::new(file);
let mut part = match dw.get_partition(*part_idx) {
Some(part) => part,
None => panic!("no such partition"),
};
let mut source = std::fs::File::open(source).unwrap();
let src_size = source.metadata().unwrap().len();
if src_size > part.len() {
panic!("file too big");
}
let n = std::io::copy(&mut source, &mut part).unwrap();
println!("wrote {n} bytes");
}
Self::SetBootCode { source } => {
let file = OpenOptions::new()
.create(false)
.append(false)
.write(true)
.read(true)
.open(file)
.unwrap();
let mut src = OpenOptions::new()
.create(false)
.append(false)
.write(false)
.read(true)
.open(source)
.unwrap();
if src.metadata().unwrap().len() != 440 {
panic!(
"Bad MBR size {}, expected 440.",
src.metadata().unwrap().len()
)
}
let mut mbr = [0u8; 440];
src.read_exact(&mut mbr).unwrap();
let mut dw = GPTDisk::new(file);
dw.set_boot_code(mbr);
}
}
}
}

25
builder/src/cmd/mod.rs Normal file
View File

@ -0,0 +1,25 @@
mod disk;
use clap::Subcommand;
use std::path::PathBuf;
#[derive(Subcommand, Debug)]
pub enum Command {
/// Disk utilities
#[command(alias = "d")]
Disk {
/// The disk image to work on
file: PathBuf,
#[command(subcommand)]
command: disk::DiskCommand,
},
}
impl Command {
pub fn run(&self) {
match self {
Self::Disk { command, file } => command.run(file),
}
}
}

272
builder/src/gpt.rs Normal file
View File

@ -0,0 +1,272 @@
use gpt::{partition_types, DiskDevice, GptDisk};
use std::io::{Read, Seek, SeekFrom, Write};
use std::str::FromStr;
/// Convenience wrapper.
/// Represents a disk with a GPT partition table
pub struct GPTDisk<D: DiskDevice> {
device: D,
start_offset: u64,
}
impl<D: DiskDevice> GPTDisk<D> {
pub fn new(mut device: D) -> Self {
//TODO:error handling
let start_offset = device.stream_position().unwrap();
return Self {
device,
start_offset,
};
}
/// Re-initilaize this disk's GPT metadata.
/// This destroys data and creates a new, empty disk.
pub fn init(&mut self) {
let disk = gpt::GptConfig::new()
.writable(true)
.create_from_device(&mut self.device, None)
.expect("failed to open disk");
disk.write().unwrap();
}
/// Read this disk's partition table.
pub fn disk(&mut self, writable: bool) -> GptDisk<&mut D> {
self.device
.seek(SeekFrom::Start(self.start_offset))
.unwrap();
let disk = gpt::GptConfig::new()
.writable(writable)
.readonly_backup(!writable)
.open_from_device(&mut self.device)
.expect("failed to open disk");
return disk;
}
/// Get a handle to a partition on this disk
pub fn get_partition<'a>(&'a mut self, partition_idx: u32) -> Option<GPTPartition<'a, D>> {
let disk = self.disk(false);
let part = match disk.partitions().get(&partition_idx) {
Some(part) => part,
None => return None,
};
let lb_size = *disk.logical_block_size();
let start_byte = part.bytes_start(lb_size).unwrap();
let length = part.bytes_len(lb_size).unwrap();
return Some(GPTPartition {
disk: self,
start_byte,
length,
seek: 0,
});
}
/// Set this disk's boot code (first 440 bytes of MBR)
pub fn set_boot_code(&mut self, boot_code: [u8; 440]) {
let disk = self.disk(false);
let lbs = *disk.logical_block_size();
drop(disk);
self.device
.seek(SeekFrom::Start(self.start_offset))
.unwrap();
// Get or make MBR
let mut mbr = gpt::mbr::ProtectiveMBR::from_disk(&mut self.device, lbs).unwrap_or(
gpt::mbr::ProtectiveMBR::with_lb_size(lbs.as_u64().try_into().unwrap_or(0xFF_FF_FF_FF)),
);
mbr.set_bootcode(boot_code);
self.device.write_all(&mbr.to_bytes()).unwrap();
}
}
/// A view into a [`GPTDisk`]'s partition.
pub struct GPTPartition<'a, D: DiskDevice> {
/// The disk this partition is on
disk: &'a mut GPTDisk<D>,
/// The index of the first byte of this partition inside its GPTDisk
start_byte: u64,
/// The length of this partition, in bytes.
length: u64,
/// The position we are reading/writing at within this file
seek: u64,
}
impl<'a, D: DiskDevice> Seek for GPTPartition<'a, D> {
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
match pos {
SeekFrom::Current(x) => {
if x.is_negative() {
let x: u64 = x.abs().try_into().unwrap();
if x > self.seek {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Seek past start",
));
}
self.seek -= x;
} else {
let x: u64 = x.try_into().unwrap();
if self.seek + x > self.length {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Seek past end",
));
}
self.seek += x;
}
}
SeekFrom::Start(x) => {
let x: u64 = x.try_into().unwrap();
if x > self.length {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Seek past end",
));
}
self.seek = x;
}
SeekFrom::End(x) => {
let x: u64 = x.try_into().unwrap();
if x > self.length {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Seek past start",
));
}
self.seek = self.length - x;
}
}
assert!(self.seek <= self.length);
return Ok(self.seek);
}
}
impl<'a, D: DiskDevice> Read for GPTPartition<'a, D> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
// Save parent cursor & seek
// (Not necessary for now, we don't need to save the parent cursor)
// let initial_pos = self.disk.device.stream_position()?;
self.disk.device.seek(SeekFrom::Start(
self.disk.start_offset + self.start_byte + self.seek,
))?;
// Make sure we stay within this partition
let from_back = self.length - self.seek;
let len: u64 = buf.len().try_into().unwrap();
let len = len.min(from_back);
let len: usize = len.try_into().unwrap();
// Do the read
let n = self.disk.device.read(&mut buf[0..len])?;
// Update cursor
let n_u64: u64 = n.try_into().unwrap();
self.seek += n_u64;
assert!(n_u64 <= from_back);
assert!(self.seek <= self.length);
// Fix parent cursor
// self.disk.device.seek(SeekFrom::Start(initial_pos))?;
return Ok(n);
}
}
impl<'a, D: DiskDevice> Write for GPTPartition<'a, D> {
fn flush(&mut self) -> std::io::Result<()> {
self.disk.device.flush()
}
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
// Save parent cursor & seek
// (Not necessary for now, we don't need to save the parent cursor)
// let initial_pos = self.disk.device.stream_position()?;
self.disk.device.seek(SeekFrom::Start(
self.disk.start_offset + self.start_byte + self.seek,
))?;
// Make sure we stay within this partition
let from_back = self.length - self.seek;
let len: u64 = buf.len().try_into().unwrap();
let len = len.min(from_back);
let len: usize = len.try_into().unwrap();
// Do the write
let n = self.disk.device.write(&buf[0..len])?;
// Update cursor
let n_u64: u64 = n.try_into().unwrap();
self.seek += n_u64;
assert!(n_u64 <= from_back);
assert!(self.seek <= self.length);
// Fix parent cursor
// self.disk.device.seek(SeekFrom::Start(initial_pos))?;
return Ok(n);
}
}
impl<'a, D: DiskDevice> GPTPartition<'a, D> {
/// The index of the first byte in this partition
pub fn start(&self) -> u64 {
self.start_byte
}
/// The length of this partition, in bytes
pub fn len(&self) -> u64 {
self.length
}
}
#[derive(Debug, Clone, Copy)]
pub enum PartitionType {
BIOS,
EFI,
Linux,
}
impl FromStr for PartitionType {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"bios" => Self::BIOS,
"efi" => Self::EFI,
"linux" => Self::Linux,
_ => return Err("unknown partition type".into()),
})
}
}
impl From<&PartitionType> for partition_types::Type {
fn from(value: &PartitionType) -> Self {
match value {
PartitionType::BIOS => partition_types::BIOS,
PartitionType::EFI => partition_types::EFI,
PartitionType::Linux => partition_types::LINUX_FS,
}
}
}

49
builder/src/main.rs Normal file
View File

@ -0,0 +1,49 @@
use anstyle::{AnsiColor, Color, Style};
use clap::Parser;
mod cmd;
mod gpt;
mod util;
fn get_styles() -> clap::builder::Styles {
clap::builder::Styles::styled()
.usage(
Style::new()
.bold()
.fg_color(Some(Color::Ansi(AnsiColor::BrightBlue))),
)
.header(
Style::new()
.bold()
.fg_color(Some(Color::Ansi(AnsiColor::Green))),
)
.literal(Style::new().fg_color(Some(Color::Ansi(AnsiColor::BrightBlack))))
.invalid(Style::new().fg_color(Some(Color::Ansi(AnsiColor::Red))))
.error(
Style::new()
.bold()
.fg_color(Some(Color::Ansi(AnsiColor::Red))),
)
.valid(
Style::new()
.bold()
.underline()
.fg_color(Some(Color::Ansi(AnsiColor::Green))),
)
.placeholder(Style::new().fg_color(Some(Color::Ansi(AnsiColor::White))))
}
/// Redox disk utility
#[derive(Parser, Debug)]
#[command(version, about, long_about = None, styles=get_styles())]
struct Cli {
#[command(subcommand)]
command: cmd::Command,
}
/// Demonstrates how to create a new partition table without anything pre-existing
fn main() {
let cli = Cli::parse();
cli.command.run();
}

45
builder/src/util.rs Normal file
View File

@ -0,0 +1,45 @@
// Parse a byte string like `5M` or `8 Tib` into a
// quantity of bytes. Returns `None` if the input
// could not be parsed.
pub fn parse_bytes_postfix(string: &str) -> Option<u64> {
let mut lower = string.trim().to_lowercase();
// Turn Mb & Mib into M and Mi
if lower.ends_with("b") {
lower.pop();
}
let mut string = &lower[..];
let mut multiplier = 1;
let len = string.len();
if string.ends_with("k") {
string = &string[0..len - 1];
multiplier = 1000;
} else if lower.ends_with("m") {
string = &string[0..len - 1];
multiplier = 1000 * 1000;
} else if lower.ends_with("g") {
string = &string[0..len - 1];
multiplier = 1000 * 1000 * 1000;
} else if lower.ends_with("t") {
string = &string[0..len - 1];
multiplier = 1000 * 1000 * 1000 * 1000;
} else if lower.ends_with("ki") {
string = &string[0..len - 2];
multiplier = 1024;
} else if lower.ends_with("mi") {
string = &string[0..len - 2];
multiplier = 1024 * 1024;
} else if lower.ends_with("gi") {
string = &string[0..len - 2];
multiplier = 1024 * 1024 * 1024;
} else if lower.ends_with("ti") {
string = &string[0..len - 2];
multiplier = 1024 * 1024 * 1024 * 1024;
}
return match string.trim().parse::<u64>() {
Ok(x) => Some(multiplier * x),
Err(_) => None,
};
}