From cbdea61a6198edd83adc5c896273c57f9adcf6c5 Mon Sep 17 00:00:00 2001 From: Ivan Tan Date: Sat, 18 Nov 2023 02:50:06 +0000 Subject: [PATCH] add option for variable size of EFI system partition --- src/bin/installer_tui.rs | 10 ++++++++-- src/config/general.rs | 1 + src/lib.rs | 30 ++++++++++++++++++++++-------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/bin/installer_tui.rs b/src/bin/installer_tui.rs index 38c3140744..74067a542e 100644 --- a/src/bin/installer_tui.rs +++ b/src/bin/installer_tui.rs @@ -11,7 +11,7 @@ extern crate toml; use pkgar::{PackageHead, ext::EntryExt}; use pkgar_core::PackageSrc; use pkgar_keys::PublicKeyFile; -use redox_installer::{Config, with_whole_disk}; +use redox_installer::{Config, with_whole_disk, DiskOption}; use std::{ ffi::OsStr, fs, @@ -302,7 +302,13 @@ fn main() { } }; - let res = with_whole_disk(&disk_path, &bootloader_bios, &bootloader_efi, password_opt.as_ref().map(|x| x.as_bytes()), |mount_path| -> Result<(), failure::Error> { + let disk_option = DiskOption { + bootloader_bios: &bootloader_bios, + bootloader_efi: &bootloader_efi, + password_opt: password_opt.as_ref().map(|x| x.as_bytes()), + efi_partition_size: None, + }; + let res = with_whole_disk(&disk_path, &disk_option, |mount_path| -> Result<(), failure::Error> { let mut config: Config = { let path = root_path.join("filesystem.toml"); match fs::read_to_string(&path) { diff --git a/src/config/general.rs b/src/config/general.rs index 4df0f529ec..59099bbcdb 100644 --- a/src/config/general.rs +++ b/src/config/general.rs @@ -3,4 +3,5 @@ pub struct GeneralConfig { pub prompt: bool, // Allow config to specify cookbook recipe or binary package as default pub repo_binary: Option, + pub efi_partition_size: Option, //MiB } diff --git a/src/lib.rs b/src/lib.rs index b2136edb13..2f50c08b34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,13 @@ use std::{ pub(crate) type Result = std::result::Result; +pub struct DiskOption<'a> { + pub bootloader_bios: &'a [u8], + pub bootloader_efi: &'a [u8], + pub password_opt: Option<&'a [u8]>, + pub efi_partition_size: Option, //MiB +} + const REMOTE: &'static str = "https://static.redox-os.org/pkg"; fn get_target() -> String { @@ -353,7 +360,7 @@ pub fn fetch_bootloaders>(config: &Config, cookbook: Option, li } //TODO: make bootloaders use Option, dynamically create BIOS and EFI partitions -pub fn with_whole_disk(disk_path: P, bootloader_bios: &[u8], bootloader_efi: &[u8], password_opt: Option<&[u8]>, callback: F) +pub fn with_whole_disk(disk_path: P, disk_option: &DiskOption, callback: F) -> Result where P: AsRef, F: FnOnce(&Path) -> Result @@ -393,7 +400,8 @@ pub fn with_whole_disk(disk_path: P, bootloader_bios: &[u8], bootloader // Second megabyte of the disk is reserved for EFI partition let efi_start = bios_end + 1; - let efi_end = efi_start + (mibi / block_size) - 1; + let efi_size = if let Some(size) = disk_option.efi_partition_size { size as u64 } else { 1 }; + let efi_end = efi_start + (efi_size * mibi / block_size) - 1; // The rest of the disk is RedoxFS, reserving the GPT table mirror at the end of disk let redoxfs_start = efi_end + 1; @@ -402,9 +410,9 @@ pub fn with_whole_disk(disk_path: P, bootloader_bios: &[u8], bootloader // Format and install BIOS partition { // Write BIOS bootloader to disk - eprintln!("Write bootloader with size {:#x}", bootloader_bios.len()); + eprintln!("Write bootloader with size {:#x}", disk_option.bootloader_bios.len()); disk_file.seek(SeekFrom::Start(0))?; - disk_file.write_all(&bootloader_bios)?; + disk_file.write_all(&disk_option.bootloader_bios)?; // Replace MBR tables with protective MBR let mbr_blocks = ((disk_size + block_size - 1) / block_size) - 1; @@ -487,11 +495,11 @@ pub fn with_whole_disk(disk_path: P, bootloader_bios: &[u8], bootloader let efi_dir = root_dir.open_dir("EFI")?; efi_dir.create_dir("BOOT")?; - eprintln!("Writing EFI/BOOT/{} file with size {:#x}", bootloader_efi_name, bootloader_efi.len()); + eprintln!("Writing EFI/BOOT/{} file with size {:#x}", bootloader_efi_name, disk_option.bootloader_efi.len()); let boot_dir = efi_dir.open_dir("BOOT")?; let mut file = boot_dir.create_file(bootloader_efi_name)?; file.truncate()?; - file.write_all(&bootloader_efi)?; + file.write_all(&disk_option.bootloader_efi)?; } // Format and install RedoxFS partition @@ -503,7 +511,7 @@ pub fn with_whole_disk(disk_path: P, bootloader_bios: &[u8], bootloader )?); with_redoxfs( disk_redoxfs, - password_opt, + disk_option.password_opt, callback ) } @@ -519,7 +527,13 @@ pub fn install(config: Config, output: P, cookbook: Option, live: bool) install_dir(config, output, cookbook) } else { let (bootloader_bios, bootloader_efi) = fetch_bootloaders(&config, cookbook.as_ref(), live)?; - with_whole_disk(output, &bootloader_bios, &bootloader_efi, None, + let disk_option = DiskOption { + bootloader_bios: &bootloader_bios, + bootloader_efi: &bootloader_efi, + password_opt: None, + efi_partition_size: config.general.efi_partition_size, + }; + with_whole_disk(output, &disk_option, move |mount_path| { install_dir(config, mount_path, cookbook) }