diff options
65 files changed, 171 insertions, 302 deletions
diff --git a/hikari/boot/params.zig b/hikari/boot/params.zig index ebc7c67..1574008 100644 --- a/hikari/boot/params.zig +++ b/hikari/boot/params.zig @@ -1,10 +1,8 @@ //! Hikari Boot Parameters -//! -//! This structure is passed to the Mirai kernel at boot time. const paging = @import("../paging/paging.zig"); -pub const boot_params_magic: u64 = 0x494152494D424B41; // "AKBMIRAI" +pub const boot_params_magic: u64 = 0x494152494D424B41; pub const boot_params_version: u32 = 1; pub const BootParams = extern struct { diff --git a/hikari/disk/gpt/constants.zig b/hikari/disk/gpt/constants.zig index 5bc2c91..afb86f7 100644 --- a/hikari/disk/gpt/constants.zig +++ b/hikari/disk/gpt/constants.zig @@ -1,6 +1,6 @@ //! Hikari GPT Constants -pub const signature: u64 = 0x5452415020494645; // "EFI PART" +pub const signature: u64 = 0x5452415020494645; pub const revision_1_0: u32 = 0x00010000; pub const header_size_minimum: u32 = 92; diff --git a/hikari/efi/constants/tables.zig b/hikari/efi/constants/tables.zig index 592113e..c2ade51 100644 --- a/hikari/efi/constants/tables.zig +++ b/hikari/efi/constants/tables.zig @@ -1,8 +1,8 @@ //! Hikari EFI Table Signature Constants -pub const system_table_signature: u64 = 0x5453595320494249; // "IBI SYST" -pub const boot_services_signature: u64 = 0x56524553544f4f42; // "BOOTSERV" -pub const runtime_services_signature: u64 = 0x56524553544e5552; // "RUNTSERV" +pub const system_table_signature: u64 = 0x5453595320494249; +pub const boot_services_signature: u64 = 0x56524553544f4f42; +pub const runtime_services_signature: u64 = 0x56524553544e5552; pub const system_table_revision_2_100: u32 = (2 << 16) | 100; pub const system_table_revision_2_90: u32 = (2 << 16) | 90; diff --git a/hikari/fs/afs/afs.zig b/hikari/fs/afs/afs.zig index efad50f..575e4c6 100644 --- a/hikari/fs/afs/afs.zig +++ b/hikari/fs/afs/afs.zig @@ -2,12 +2,10 @@ const shared_afs = @import("shared").afs; -// Adapters (EFI-specific) pub const btree = @import("btree.zig"); pub const reader = @import("reader.zig"); pub const block_io = @import("block.zig"); -// Re-export shared types pub const constants = shared_afs.constants; pub const types = shared_afs.types; @@ -29,7 +27,6 @@ pub const JournalInfoCell = shared_afs.types.JournalInfoCell; pub const JournalHeader = shared_afs.types.JournalHeader; pub const Timestamp = shared_afs.types.Timestamp; -// Adapter types pub const BTree = btree.BTree; pub const BTreeError = btree.BTreeError; diff --git a/hikari/fs/afs/block.zig b/hikari/fs/afs/block.zig index ae45f7b..f13edf2 100644 --- a/hikari/fs/afs/block.zig +++ b/hikari/fs/afs/block.zig @@ -6,7 +6,6 @@ const shared_afs = @import("shared").afs; const BlockReader = shared_afs.BlockReader; const BlockError = shared_afs.BlockError; -/// EFI Block I/O context pub const EfiBlockContext = struct { block_io: *efi.protocols.BlockIoProtocol, boot_services: *efi.services.BootServices, @@ -15,7 +14,6 @@ pub const EfiBlockContext = struct { cell_size: u32, }; -/// EFI block read function - implements shared BlockReader interface pub fn efi_read_cell(context: *anyopaque, cell: u64, buffer: []u8) BlockError!void { const ctx: *EfiBlockContext = @ptrCast(@alignCast(context)); @@ -34,7 +32,6 @@ pub fn efi_read_cell(context: *anyopaque, cell: u64, buffer: []u8) BlockError!vo } } -/// Create a BlockReader from EFI context pub fn create_block_reader(ctx: *EfiBlockContext, total_cells: u64) BlockReader { return BlockReader{ .context = @ptrCast(ctx), diff --git a/hikari/fs/afs/btree.zig b/hikari/fs/afs/btree.zig index d5de00c..4ce4c2a 100644 --- a/hikari/fs/afs/btree.zig +++ b/hikari/fs/afs/btree.zig @@ -6,7 +6,6 @@ const block_io = @import("block.zig"); const EfiBlockContext = block_io.EfiBlockContext; -// Import shared types const BTreeNodeDescriptor = shared_afs.BTreeNodeDescriptor; const BTreeHeaderRecord = shared_afs.BTreeHeaderRecord; const IndexKey = shared_afs.types.IndexKey; @@ -15,7 +14,6 @@ const UnitRecord = shared_afs.UnitRecord; const ThreadRecord = shared_afs.types.ThreadRecord; const SpanDescriptor = shared_afs.SpanDescriptor; -// Import shared B-tree operations const btree_ops = shared_afs.btree; const constants = shared_afs.constants; diff --git a/hikari/fs/afs/reader.zig b/hikari/fs/afs/reader.zig index 9a4e98c..db6445c 100644 --- a/hikari/fs/afs/reader.zig +++ b/hikari/fs/afs/reader.zig @@ -4,7 +4,6 @@ const efi = @import("../../efi/efi.zig"); const shared_afs = @import("shared").afs; const btree_adapter = @import("btree.zig"); -// Import shared types const VolumeHeader = shared_afs.VolumeHeader; const SpanDescriptor = shared_afs.SpanDescriptor; const ChannelInfo = shared_afs.ChannelInfo; @@ -163,7 +162,6 @@ pub const Reader = struct { }; if (unit_record) |unit| { - // Check if this is the last component if (iter.next() == null) { last_unit = unit.*; break; diff --git a/hikari/fs/fat32/fat32.zig b/hikari/fs/fat32/fat32.zig index 41d2d91..f1612e6 100644 --- a/hikari/fs/fat32/fat32.zig +++ b/hikari/fs/fat32/fat32.zig @@ -3,7 +3,6 @@ const efi = @import("../../efi/efi.zig"); const shared_fat32 = @import("shared").fat32; -// Re-export shared types pub const constants = shared_fat32.constants; pub const types = shared_fat32.types; @@ -14,7 +13,6 @@ pub const LongIdentityEntry = shared_fat32.LongIdentityEntry; pub const TimeFormat = shared_fat32.TimeFormat; pub const DateFormat = shared_fat32.DateFormat; -// Reader adapter pub const reader = @import("reader.zig"); pub const Reader = reader.Reader; pub const ReadError = reader.ReadError; diff --git a/hikari/fs/fat32/reader.zig b/hikari/fs/fat32/reader.zig index 89a449a..fcaa7da 100644 --- a/hikari/fs/fat32/reader.zig +++ b/hikari/fs/fat32/reader.zig @@ -3,7 +3,6 @@ const efi = @import("../../efi/efi.zig"); const shared_fat32 = @import("shared").fat32; -// Import shared types const BootSector = shared_fat32.BootSector; const FsInfo = shared_fat32.FsInfo; const StackEntry = shared_fat32.StackEntry; diff --git a/hikari/paging/types.zig b/hikari/paging/types.zig index 0cb512e..812f14e 100644 --- a/hikari/paging/types.zig +++ b/hikari/paging/types.zig @@ -62,11 +62,10 @@ pub const PageTable = struct { } }; -// Page table levels (L4 = top, L1 = bottom) -pub const TableL4 = PageTable; // PML4 -pub const TableL3 = PageTable; // PDPT -pub const TableL2 = PageTable; // PD -pub const TableL1 = PageTable; // PT +pub const TableL4 = PageTable; +pub const TableL3 = PageTable; +pub const TableL2 = PageTable; +pub const TableL1 = PageTable; pub fn get_l4_index(address: u64) usize { return @truncate((address >> constants.pml4_shift) & 0x1FF); diff --git a/mirai/crimson/panic/halt.zig b/mirai/crimson/panic/halt.zig index 55d7966..058d528 100644 --- a/mirai/crimson/panic/halt.zig +++ b/mirai/crimson/panic/halt.zig @@ -12,10 +12,6 @@ pub fn halt_current() noreturn { asm_cpu.halt_loop(); } -pub fn send_halt_ipi() void { - // TODO: Send IPI to all other cores to halt them -} +pub fn send_halt_ipi() void {} -pub fn wait_for_other_cpus() void { - // TODO: Wait for all other CPUs to acknowledge halt -} +pub fn wait_for_other_cpus() void {} diff --git a/mirai/crimson/ports/constants/constants.zig b/mirai/crimson/ports/constants/constants.zig new file mode 100644 index 0000000..11bc58f --- /dev/null +++ b/mirai/crimson/ports/constants/constants.zig @@ -0,0 +1,3 @@ +//! Exception Port Constants + +pub const masks = @import("masks.zig"); diff --git a/mirai/crimson/ports/masks.zig b/mirai/crimson/ports/constants/masks.zig index fb7453f..43295dd 100644 --- a/mirai/crimson/ports/masks.zig +++ b/mirai/crimson/ports/constants/masks.zig @@ -1,6 +1,6 @@ //! Exception Masks -const constants = @import("../constants/constants.zig"); +const constants = @import("../../constants/constants.zig"); const ExceptionType = constants.ExceptionType; diff --git a/mirai/crimson/ports/ports.zig b/mirai/crimson/ports/ports.zig index c145491..c4757b6 100644 --- a/mirai/crimson/ports/ports.zig +++ b/mirai/crimson/ports/ports.zig @@ -2,7 +2,7 @@ pub const port = @import("port.zig"); pub const array = @import("array.zig"); -pub const masks = @import("masks.zig"); +pub const masks = @import("constants/masks.zig"); pub const host = @import("host.zig"); pub const kata = @import("kata.zig"); pub const thread = @import("thread.zig"); diff --git a/mirai/crimson/render/banner.zig b/mirai/crimson/render/banner.zig index 802c804..aa40abe 100644 --- a/mirai/crimson/render/banner.zig +++ b/mirai/crimson/render/banner.zig @@ -1,23 +1,24 @@ //! Collapse Banner const serial = @import("../../drivers/serial/serial.zig"); +const messages = @import("strings/strings.zig").messages; pub fn render() void { serial.printf("\n", .{}); - serial.printf("================================================================================\n", .{}); - serial.printf(" AKIBA HAS COLLAPSED \n", .{}); - serial.printf("================================================================================\n", .{}); + serial.printf(messages.separator, .{}); + serial.printf(messages.collapse_header, .{}); + serial.printf(messages.separator, .{}); serial.printf("\n", .{}); } pub fn render_message(message: []const u8) void { - serial.printf("Reason: %s\n", .{message}); + serial.printf(messages.reason, .{message}); serial.printf("\n", .{}); } pub fn render_halt() void { serial.printf("\n", .{}); - serial.printf("================================================================================\n", .{}); - serial.printf("System halted. Please restart your computer.\n", .{}); - serial.printf("================================================================================\n", .{}); + serial.printf(messages.separator, .{}); + serial.printf(messages.system_halted, .{}); + serial.printf(messages.separator, .{}); } diff --git a/mirai/crimson/render/context.zig b/mirai/crimson/render/context.zig index 944f2cc..c18a9a7 100644 --- a/mirai/crimson/render/context.zig +++ b/mirai/crimson/render/context.zig @@ -2,20 +2,21 @@ const serial = @import("../../drivers/serial/serial.zig"); const types = @import("../types/types.zig"); +const messages = @import("strings/strings.zig").messages; const Context = types.Context; pub fn render(context: *const Context) void { - serial.printf("CPU Context:\n", .{}); - serial.printf(" RAX: %x RBX: %x\n", .{ context.rax, context.rbx }); - serial.printf(" RCX: %x RDX: %x\n", .{ context.rcx, context.rdx }); - serial.printf(" RSI: %x RDI: %x\n", .{ context.rsi, context.rdi }); - serial.printf(" RBP: %x RSP: %x\n", .{ context.rbp, context.rsp }); - serial.printf(" R8: %x R9: %x\n", .{ context.r8, context.r9 }); - serial.printf(" R10: %x R11: %x\n", .{ context.r10, context.r11 }); - serial.printf(" R12: %x R13: %x\n", .{ context.r12, context.r13 }); - serial.printf(" R14: %x R15: %x\n", .{ context.r14, context.r15 }); - serial.printf(" RIP: %x RFLAGS: %x\n", .{ context.rip, context.rflags }); + serial.printf(messages.cpu_context_header, .{}); + serial.printf(messages.reg_rax_rbx, .{ context.rax, context.rbx }); + serial.printf(messages.reg_rcx_rdx, .{ context.rcx, context.rdx }); + serial.printf(messages.reg_rsi_rdi, .{ context.rsi, context.rdi }); + serial.printf(messages.reg_rbp_rsp, .{ context.rbp, context.rsp }); + serial.printf(messages.reg_r8_r9, .{ context.r8, context.r9 }); + serial.printf(messages.reg_r10_r11, .{ context.r10, context.r11 }); + serial.printf(messages.reg_r12_r13, .{ context.r12, context.r13 }); + serial.printf(messages.reg_r14_r15, .{ context.r14, context.r15 }); + serial.printf(messages.reg_rip_rflags, .{ context.rip, context.rflags }); serial.printf("\n", .{}); render_control_registers(context); @@ -23,15 +24,15 @@ pub fn render(context: *const Context) void { } fn render_control_registers(context: *const Context) void { - serial.printf("Control Registers:\n", .{}); - serial.printf(" CR0: %x CR2: %x\n", .{ context.cr0, context.cr2 }); - serial.printf(" CR3: %x CR4: %x\n", .{ context.cr3, context.cr4 }); + serial.printf(messages.control_registers_header, .{}); + serial.printf(messages.reg_cr0_cr2, .{ context.cr0, context.cr2 }); + serial.printf(messages.reg_cr3_cr4, .{ context.cr3, context.cr4 }); serial.printf("\n", .{}); } fn render_segment_registers(context: *const Context) void { - serial.printf("Segment Registers:\n", .{}); - serial.printf(" CS: %x DS: %x ES: %x\n", .{ context.cs, context.ds, context.es }); - serial.printf(" FS: %x GS: %x SS: %x\n", .{ context.fs, context.gs, context.ss }); + serial.printf(messages.segment_registers_header, .{}); + serial.printf(messages.reg_cs_ds_es, .{ context.cs, context.ds, context.es }); + serial.printf(messages.reg_fs_gs_ss, .{ context.fs, context.gs, context.ss }); serial.printf("\n", .{}); } diff --git a/mirai/crimson/render/exception.zig b/mirai/crimson/render/exception.zig index edc8dd3..17c5e0c 100644 --- a/mirai/crimson/render/exception.zig +++ b/mirai/crimson/render/exception.zig @@ -3,34 +3,35 @@ const serial = @import("../../drivers/serial/serial.zig"); const types = @import("../types/types.zig"); const classify = @import("../classify/classify.zig"); +const messages = @import("strings/strings.zig").messages; const Exception = types.Exception; const PageFaultError = classify.PageFaultError; pub fn render(exception: *const Exception) void { - serial.printf("Exception: %s (%s)\n", .{ + serial.printf(messages.exception_line, .{ exception.exception_type.name(), classify.get_vector_name(exception.vector), }); - serial.printf(" Vector: %d\n", .{exception.vector}); - serial.printf(" Code: %x\n", .{exception.code}); - serial.printf(" Subcode: %x\n", .{exception.subcode}); + serial.printf(messages.vector, .{exception.vector}); + serial.printf(messages.code, .{exception.code}); + serial.printf(messages.subcode, .{exception.subcode}); if (exception.address != 0) { - serial.printf(" Fault Address: %x\n", .{exception.address}); + serial.printf(messages.fault_address, .{exception.address}); } if (exception.vector == 14) { render_page_fault_details(exception.code); } - serial.printf(" Location: %s mode\n", .{ - if (exception.context.is_kernel_mode()) "kernel" else "user", + serial.printf(messages.location, .{ + if (exception.context.is_kernel_mode()) messages.location_kernel else messages.location_user, }); if (exception.kata_id != 0) { - serial.printf(" Kata: %d, Thread: %d\n", .{ exception.kata_id, exception.thread_id }); + serial.printf(messages.kata_thread, .{ exception.kata_id, exception.thread_id }); } serial.printf("\n", .{}); @@ -39,21 +40,21 @@ pub fn render(exception: *const Exception) void { fn render_page_fault_details(error_code: u64) void { const pf_error = PageFaultError.from_error_code(error_code); - serial.printf(" Access: %s\n", .{pf_error.description()}); + serial.printf(messages.access, .{pf_error.description()}); if (pf_error.user) { - serial.printf(" Mode: User\n", .{}); + serial.printf(messages.mode_user, .{}); } else { - serial.printf(" Mode: Kernel\n", .{}); + serial.printf(messages.mode_kernel, .{}); } } pub fn render_faulting_instruction(rip: u64) void { - serial.printf("Faulting Instruction:\n", .{}); - serial.printf(" Address: %x\n", .{rip}); + serial.printf(messages.faulting_instruction_header, .{}); + serial.printf(messages.address, .{rip}); const code_ptr: [*]const u8 = @ptrFromInt(rip); - serial.printf(" Bytes: ", .{}); + serial.printf(messages.bytes_label, .{}); for (0..8) |i| { serial.printf("%x ", .{code_ptr[i]}); } diff --git a/mirai/crimson/render/memory.zig b/mirai/crimson/render/memory.zig index efef69c..44938e5 100644 --- a/mirai/crimson/render/memory.zig +++ b/mirai/crimson/render/memory.zig @@ -1,6 +1,7 @@ //! Render Memory Around Fault const serial = @import("../../drivers/serial/serial.zig"); +const messages = @import("strings/strings.zig").messages; pub fn render_around_address(address: u64, bytes_before: usize, bytes_after: usize) void { if (address == 0) return; @@ -8,7 +9,7 @@ pub fn render_around_address(address: u64, bytes_before: usize, bytes_after: usi const start = if (address >= bytes_before) address - bytes_before else 0; const total_bytes = bytes_before + bytes_after; - serial.printf("Memory around %x:\n", .{address}); + serial.printf(messages.memory_around, .{address}); const mem_ptr: [*]const u8 = @ptrFromInt(start); @@ -44,7 +45,7 @@ pub fn render_around_address(address: u64, bytes_before: usize, bytes_after: usi } pub fn render_instruction_bytes(rip: u64, count: usize) void { - serial.printf("Instruction bytes at %x:\n ", .{rip}); + serial.printf(messages.instruction_bytes, .{rip}); const code_ptr: [*]const u8 = @ptrFromInt(rip); diff --git a/mirai/crimson/render/modules.zig b/mirai/crimson/render/modules.zig index 8520fb1..697cc32 100644 --- a/mirai/crimson/render/modules.zig +++ b/mirai/crimson/render/modules.zig @@ -1,6 +1,7 @@ //! Render Loaded Modules const serial = @import("../../drivers/serial/serial.zig"); +const messages = @import("strings/strings.zig").messages; pub const ModuleInfo = struct { name: [64]u8, @@ -35,15 +36,15 @@ pub fn register_module(name: []const u8, base_address: u64, size: u64) bool { pub fn render() void { if (module_count == 0) { - serial.printf("Loaded Modules: (none registered)\n\n", .{}); + serial.printf(messages.modules_none, .{}); return; } - serial.printf("Loaded Modules:\n", .{}); + serial.printf(messages.modules_header, .{}); for (0..module_count) |i| { const module = &loaded_modules[i]; - serial.printf(" %s: %x - %x (%d bytes)\n", .{ + serial.printf(messages.module_entry, .{ module.name[0..module.name_len], module.base_address, module.base_address + module.size, diff --git a/mirai/crimson/render/stack.zig b/mirai/crimson/render/stack.zig index a00f0c7..e9f443d 100644 --- a/mirai/crimson/render/stack.zig +++ b/mirai/crimson/render/stack.zig @@ -2,11 +2,12 @@ const serial = @import("../../drivers/serial/serial.zig"); const types = @import("../types/types.zig"); +const messages = @import("strings/strings.zig").messages; const Context = types.Context; pub fn render(context: *const Context) void { - serial.printf("Stack Trace:\n", .{}); + serial.printf(messages.stack_trace_header, .{}); var rbp = context.rbp; var depth: usize = 0; @@ -28,14 +29,14 @@ pub fn render(context: *const Context) void { } if (depth == 0) { - serial.printf(" (no stack frames available)\n", .{}); + serial.printf(messages.no_stack_frames, .{}); } serial.printf("\n", .{}); } pub fn render_raw_stack(rsp: u64, count: usize) void { - serial.printf("Raw Stack (from %x):\n", .{rsp}); + serial.printf(messages.raw_stack, .{rsp}); const stack_ptr: [*]const u64 = @ptrFromInt(rsp); diff --git a/mirai/crimson/render/strings/messages.zig b/mirai/crimson/render/strings/messages.zig new file mode 100644 index 0000000..f64a805 --- /dev/null +++ b/mirai/crimson/render/strings/messages.zig @@ -0,0 +1,50 @@ +//! Crimson Render Messages + +pub const separator = "================================================================================\n"; +pub const collapse_header = " AKIBA HAS COLLAPSED \n"; +pub const reason = "Reason: %s\n"; +pub const system_halted = "System halted. Please restart your computer.\n"; + +pub const cpu_context_header = "CPU Context:\n"; +pub const reg_rax_rbx = " RAX: %x RBX: %x\n"; +pub const reg_rcx_rdx = " RCX: %x RDX: %x\n"; +pub const reg_rsi_rdi = " RSI: %x RDI: %x\n"; +pub const reg_rbp_rsp = " RBP: %x RSP: %x\n"; +pub const reg_r8_r9 = " R8: %x R9: %x\n"; +pub const reg_r10_r11 = " R10: %x R11: %x\n"; +pub const reg_r12_r13 = " R12: %x R13: %x\n"; +pub const reg_r14_r15 = " R14: %x R15: %x\n"; +pub const reg_rip_rflags = " RIP: %x RFLAGS: %x\n"; +pub const control_registers_header = "Control Registers:\n"; +pub const reg_cr0_cr2 = " CR0: %x CR2: %x\n"; +pub const reg_cr3_cr4 = " CR3: %x CR4: %x\n"; +pub const segment_registers_header = "Segment Registers:\n"; +pub const reg_cs_ds_es = " CS: %x DS: %x ES: %x\n"; +pub const reg_fs_gs_ss = " FS: %x GS: %x SS: %x\n"; + +pub const exception_line = "Exception: %s (%s)\n"; +pub const vector = " Vector: %d\n"; +pub const code = " Code: %x\n"; +pub const subcode = " Subcode: %x\n"; +pub const fault_address = " Fault Address: %x\n"; +pub const location = " Location: %s mode\n"; +pub const location_kernel = "kernel"; +pub const location_user = "user"; +pub const kata_thread = " Kata: %d, Thread: %d\n"; +pub const access = " Access: %s\n"; +pub const mode_user = " Mode: User\n"; +pub const mode_kernel = " Mode: Kernel\n"; +pub const faulting_instruction_header = "Faulting Instruction:\n"; +pub const address = " Address: %x\n"; +pub const bytes_label = " Bytes: "; + +pub const memory_around = "Memory around %x:\n"; +pub const instruction_bytes = "Instruction bytes at %x:\n "; + +pub const stack_trace_header = "Stack Trace:\n"; +pub const no_stack_frames = " (no stack frames available)\n"; +pub const raw_stack = "Raw Stack (from %x):\n"; + +pub const modules_none = "Loaded Modules: (none registered)\n\n"; +pub const modules_header = "Loaded Modules:\n"; +pub const module_entry = " %s: %x - %x (%d bytes)\n"; diff --git a/mirai/crimson/render/strings/strings.zig b/mirai/crimson/render/strings/strings.zig new file mode 100644 index 0000000..506643f --- /dev/null +++ b/mirai/crimson/render/strings/strings.zig @@ -0,0 +1,3 @@ +//! Crimson Render Strings + +pub const messages = @import("messages.zig"); diff --git a/mirai/drivers/pit/constants/constants.zig b/mirai/drivers/pit/constants/constants.zig new file mode 100644 index 0000000..d61651a --- /dev/null +++ b/mirai/drivers/pit/constants/constants.zig @@ -0,0 +1,17 @@ +//! PIT Constants + +pub const limits = @import("limits.zig"); + +pub const channel0_data = limits.channel0_data; +pub const channel1_data = limits.channel1_data; +pub const channel2_data = limits.channel2_data; +pub const command = limits.command; + +pub const base_frequency = limits.base_frequency; +pub const target_frequency = limits.target_frequency; + +pub const mode_square_wave = limits.mode_square_wave; +pub const mode_rate_generator = limits.mode_rate_generator; + +pub const irq = limits.irq; +pub const vector = limits.vector; diff --git a/mirai/drivers/pit/constants.zig b/mirai/drivers/pit/constants/limits.zig index 9aa8dec..9aa8dec 100644 --- a/mirai/drivers/pit/constants.zig +++ b/mirai/drivers/pit/constants/limits.zig diff --git a/mirai/drivers/pit/handler.zig b/mirai/drivers/pit/handler.zig index 4d3d3b0..f1e676d 100644 --- a/mirai/drivers/pit/handler.zig +++ b/mirai/drivers/pit/handler.zig @@ -1,6 +1,6 @@ //! PIT IRQ Handler -const constants = @import("constants.zig"); +const constants = @import("constants/constants.zig"); const pic = @import("../../interrupts/pic/pic.zig"); const idt = @import("../../interrupts/idt.zig"); diff --git a/mirai/drivers/pit/init.zig b/mirai/drivers/pit/init.zig index f31e47f..fe6c1d1 100644 --- a/mirai/drivers/pit/init.zig +++ b/mirai/drivers/pit/init.zig @@ -1,6 +1,6 @@ //! PIT Initialization -const constants = @import("constants.zig"); +const constants = @import("constants/constants.zig"); const asm_io = @import("../../asm/io/io.zig"); pub fn init(frequency: u32) void { diff --git a/mirai/drivers/pit/pit.zig b/mirai/drivers/pit/pit.zig index db0f12a..5832dd0 100644 --- a/mirai/drivers/pit/pit.zig +++ b/mirai/drivers/pit/pit.zig @@ -1,6 +1,6 @@ //! PIT - Programmable Interval Timer -pub const constants = @import("constants.zig"); +pub const constants = @import("constants/constants.zig"); pub const init = @import("init.zig"); pub const handler = @import("handler.zig"); diff --git a/mirai/interrupts/pic/constants/constants.zig b/mirai/interrupts/pic/constants/constants.zig new file mode 100644 index 0000000..c68b936 --- /dev/null +++ b/mirai/interrupts/pic/constants/constants.zig @@ -0,0 +1,3 @@ +//! PIC Constants + +pub const ports = @import("ports.zig"); diff --git a/mirai/interrupts/pic/ports.zig b/mirai/interrupts/pic/constants/ports.zig index aba363f..aba363f 100644 --- a/mirai/interrupts/pic/ports.zig +++ b/mirai/interrupts/pic/constants/ports.zig diff --git a/mirai/interrupts/pic/eoi.zig b/mirai/interrupts/pic/eoi.zig index 810ea37..ff71b78 100644 --- a/mirai/interrupts/pic/eoi.zig +++ b/mirai/interrupts/pic/eoi.zig @@ -1,6 +1,6 @@ //! PIC End-of-Interrupt -const ports = @import("ports.zig"); +const ports = @import("constants/ports.zig"); const asm_io = @import("../../asm/io/io.zig"); pub fn send(irq: u4) void { diff --git a/mirai/interrupts/pic/init.zig b/mirai/interrupts/pic/init.zig index 2f84b8a..d6dcbff 100644 --- a/mirai/interrupts/pic/init.zig +++ b/mirai/interrupts/pic/init.zig @@ -1,6 +1,6 @@ //! PIC Initialization -const ports = @import("ports.zig"); +const ports = @import("constants/ports.zig"); const asm_io = @import("../../asm/io/io.zig"); pub fn remap() void { diff --git a/mirai/interrupts/pic/mask.zig b/mirai/interrupts/pic/mask.zig index 3b98e28..4170cd0 100644 --- a/mirai/interrupts/pic/mask.zig +++ b/mirai/interrupts/pic/mask.zig @@ -1,6 +1,6 @@ //! PIC IRQ Masking -const ports = @import("ports.zig"); +const ports = @import("constants/ports.zig"); const asm_io = @import("../../asm/io/io.zig"); pub fn enable_irq(irq: u4) void { diff --git a/mirai/interrupts/pic/pic.zig b/mirai/interrupts/pic/pic.zig index ee34508..bec6a1f 100644 --- a/mirai/interrupts/pic/pic.zig +++ b/mirai/interrupts/pic/pic.zig @@ -1,6 +1,6 @@ //! PIC - 8259A Programmable Interrupt Controller -pub const ports = @import("ports.zig"); +pub const ports = @import("constants/ports.zig"); pub const init = @import("init.zig"); pub const mask = @import("mask.zig"); pub const eoi = @import("eoi.zig"); diff --git a/mirai/kernel/boot.zig b/mirai/kernel/boot.zig index fb8997b..f70aac6 100644 --- a/mirai/kernel/boot.zig +++ b/mirai/kernel/boot.zig @@ -1,8 +1,6 @@ //! Boot Parameters -//! -//! This structure matches what Hikari bootloader passes to the kernel. -pub const boot_params_magic: u64 = 0x494152494D424B41; // "AKBMIRAI" +pub const boot_params_magic: u64 = 0x494152494D424B41; pub const boot_params_version: u32 = 1; pub const BootParams = extern struct { diff --git a/shared/fs/afs/afs.zig b/shared/fs/afs/afs.zig index 42990ee..9c494af 100644 --- a/shared/fs/afs/afs.zig +++ b/shared/fs/afs/afs.zig @@ -7,7 +7,6 @@ pub const btree = @import("btree/btree.zig"); pub const read = @import("read/read.zig"); pub const write = @import("write/write.zig"); -// Re-export commonly used types pub const VolumeHeader = types.VolumeHeader; pub const SpanDescriptor = types.SpanDescriptor; pub const ChannelInfo = types.ChannelInfo; @@ -19,13 +18,11 @@ pub const BTreeHeaderRecord = types.BTreeHeaderRecord; pub const IndexKey = types.IndexKey; pub const Permissions = types.Permissions; -// Re-export I/O interfaces pub const BlockReader = io.BlockReader; pub const BlockWriter = io.BlockWriter; pub const BlockDevice = io.BlockDevice; pub const BlockError = io.BlockError; -// Re-export errors pub const BTreeError = btree.BTreeError; pub const ReadError = read.ReadError; pub const WriteError = write.WriteError; diff --git a/shared/fs/afs/btree/btree.zig b/shared/fs/afs/btree/btree.zig index ba72f99..cac9e36 100644 --- a/shared/fs/afs/btree/btree.zig +++ b/shared/fs/afs/btree/btree.zig @@ -3,7 +3,6 @@ pub const node = @import("node.zig"); pub const search = @import("search.zig"); -// Node operations pub const get_node_cell = node.get_node_cell; pub const get_node_offset_in_cell = node.get_node_offset_in_cell; pub const get_record_offset = node.get_record_offset; @@ -11,7 +10,6 @@ pub const get_record_ptr = node.get_record_ptr; pub const get_record_ptr_const = node.get_record_ptr_const; pub const NodeError = node.NodeError; -// Search operations pub const compare_keys = search.compare_keys; pub const search_index_node = search.search_index_node; pub const search_leaf_for_unit = search.search_leaf_for_unit; diff --git a/shared/fs/afs/btree/node.zig b/shared/fs/afs/btree/node.zig index d2ccc73..5fe161f 100644 --- a/shared/fs/afs/btree/node.zig +++ b/shared/fs/afs/btree/node.zig @@ -13,32 +13,27 @@ pub const NodeError = error{ OutOfBounds, }; -/// Calculate the cell containing a node pub fn get_node_cell(node_number: u32, cell_size: u32, node_size: u32) u64 { const nodes_per_cell = cell_size / node_size; return node_number / nodes_per_cell; } -/// Calculate offset of a node within a cell pub fn get_node_offset_in_cell(node_number: u32, cell_size: u32, node_size: u32) u32 { const nodes_per_cell = cell_size / node_size; return (node_number % nodes_per_cell) * node_size; } -/// Get record offset from the offset table at the end of the node pub fn get_record_offset(node_buffer: [*]const u8, node_size: u32, record_index: u16) u16 { const offset_table_start = node_size - (@as(u32, record_index) + 1) * 2; const offset_ptr: *align(1) const u16 = @ptrCast(node_buffer + offset_table_start); return offset_ptr.*; } -/// Get pointer to a record in the node buffer pub fn get_record_ptr(node_buffer: [*]u8, node_size: u32, record_index: u16) [*]u8 { const offset = get_record_offset(node_buffer, node_size, record_index); return node_buffer + offset; } -/// Get const pointer to a record in the node buffer pub fn get_record_ptr_const(node_buffer: [*]const u8, node_size: u32, record_index: u16) [*]const u8 { const offset = get_record_offset(node_buffer, node_size, record_index); return node_buffer + offset; diff --git a/shared/fs/afs/btree/search.zig b/shared/fs/afs/btree/search.zig index 85f7c4b..93614d1 100644 --- a/shared/fs/afs/btree/search.zig +++ b/shared/fs/afs/btree/search.zig @@ -10,8 +10,6 @@ const StackRecord = types.StackRecord; const UnitRecord = types.UnitRecord; const ThreadRecord = types.ThreadRecord; -/// Compare search key against a B-tree key -/// Returns: -1 if search < key, 0 if equal, 1 if search > key pub fn compare_keys(parent_node_id: u32, identity: []const u16, key: *align(1) const IndexKey) i32 { if (parent_node_id < key.parent_node_id) { return -1; @@ -45,7 +43,6 @@ pub fn compare_keys(parent_node_id: u32, identity: []const u16, key: *align(1) c return 0; } -/// Search an index node for the child pointer to follow pub fn search_index_node( node_buffer: [*]const u8, node_size: u32, @@ -65,7 +62,6 @@ pub fn search_index_node( } } - // Return last child pointer if search key is greater than all keys if (record_count > 0) { const last_record = node_ops.get_record_ptr_const(node_buffer, node_size, record_count - 1); const last_key: *align(1) const IndexKey = @ptrCast(last_record); @@ -76,7 +72,6 @@ pub fn search_index_node( return null; } -/// Search a leaf node for a unit record pub fn search_leaf_for_unit( node_buffer: [*]const u8, node_size: u32, @@ -104,7 +99,6 @@ pub fn search_leaf_for_unit( return null; } -/// Search a leaf node for a stack record pub fn search_leaf_for_stack( node_buffer: [*]const u8, node_size: u32, @@ -132,7 +126,6 @@ pub fn search_leaf_for_stack( return null; } -/// Search a leaf node for a thread record pub fn search_leaf_for_thread( node_buffer: [*]const u8, node_size: u32, diff --git a/shared/fs/afs/constants/constants.zig b/shared/fs/afs/constants/constants.zig index 2478a86..6be89a4 100644 --- a/shared/fs/afs/constants/constants.zig +++ b/shared/fs/afs/constants/constants.zig @@ -7,7 +7,6 @@ pub const records = @import("records.zig"); pub const btree = @import("btree.zig"); pub const flags = @import("flags.zig"); -// Re-export commonly used constants pub const signature = magic.signature; pub const version = magic.version; pub const journal_signature = magic.journal_signature; diff --git a/shared/fs/afs/constants/flags.zig b/shared/fs/afs/constants/flags.zig index 3ac1143..32daaa9 100644 --- a/shared/fs/afs/constants/flags.zig +++ b/shared/fs/afs/constants/flags.zig @@ -1,6 +1,5 @@ //! AFS Flags -// Unit flags pub const unit_locked: u16 = 0x0001; pub const unit_has_thread: u16 = 0x0002; pub const unit_has_alias: u16 = 0x0004; @@ -8,21 +7,17 @@ pub const unit_has_security: u16 = 0x0008; pub const unit_has_twins: u16 = 0x0010; pub const unit_has_resource_channel: u16 = 0x0020; -// Channel types pub const channel_data: u8 = 0x00; pub const channel_resource: u8 = 0xFF; -// Compression types pub const compression_none: u32 = 0; pub const compression_zlib: u32 = 1; pub const compression_lz4: u32 = 2; pub const compression_zstd: u32 = 3; -// Encryption types pub const encryption_none: u32 = 0; pub const encryption_aes_128_xts: u32 = 1; pub const encryption_aes_256_xts: u32 = 2; -// Journal flags pub const journal_on_other_device: u32 = 0x00000001; pub const journal_needs_init: u32 = 0x00000002; diff --git a/shared/fs/afs/constants/magic.zig b/shared/fs/afs/constants/magic.zig index 84f46db..f268615 100644 --- a/shared/fs/afs/constants/magic.zig +++ b/shared/fs/afs/constants/magic.zig @@ -1,15 +1,11 @@ //! AFS Magic Numbers and Signatures -/// "AKIBAFS!" signature pub const signature: u64 = 0x2153464142494B41; -/// AFS version pub const version: u16 = 0x0001; -/// "JNRL" journal signature pub const journal_signature: u32 = 0x4A4E524C; -/// AFS partition type GUID: 414B4942-4146-5300-0000-000000000001 pub const partition_type_guid = [16]u8{ 0x42, 0x49, 0x4B, 0x41, 0x46, 0x41, 0x00, 0x53, diff --git a/shared/fs/afs/io/block.zig b/shared/fs/afs/io/block.zig index 8abd71c..8475562 100644 --- a/shared/fs/afs/io/block.zig +++ b/shared/fs/afs/io/block.zig @@ -9,7 +9,6 @@ pub const BlockError = error{ NotSupported, }; -/// Block reader interface - implemented by each consumer pub const BlockReader = struct { context: *anyopaque, read_fn: *const fn (context: *anyopaque, cell: u64, buffer: []u8) BlockError!void, @@ -40,7 +39,6 @@ pub const BlockReader = struct { } }; -/// Block writer interface - implemented by each consumer pub const BlockWriter = struct { context: *anyopaque, write_fn: *const fn (context: *anyopaque, cell: u64, data: []const u8) BlockError!void, @@ -71,7 +69,6 @@ pub const BlockWriter = struct { } }; -/// Combined reader/writer for read-write operations pub const BlockDevice = struct { reader: BlockReader, writer: BlockWriter, diff --git a/shared/fs/afs/read/location.zig b/shared/fs/afs/read/location.zig index ef145fc..f76b9d4 100644 --- a/shared/fs/afs/read/location.zig +++ b/shared/fs/afs/read/location.zig @@ -13,14 +13,12 @@ pub const LocationError = error{ BTreeError, }; -/// Result of looking up a location pub const LookupResult = union(enum) { unit: UnitRecord, stack: StackRecord, not_found: void, }; -/// Convert ASCII location component to UTF-16 identity pub fn component_to_identity(component: []const u8, identity_buffer: []u16) usize { var len: usize = 0; for (component) |byte| { @@ -31,14 +29,12 @@ pub fn component_to_identity(component: []const u8, identity_buffer: []u16) usiz return len; } -/// Iterator for location components pub const LocationIterator = struct { location: []const u8, position: usize, pub fn init(location: []const u8) LocationIterator { var start: usize = 0; - // Skip leading separator if (location.len > 0 and (location[0] == '/' or location[0] == '\\')) { start = 1; } @@ -49,7 +45,6 @@ pub const LocationIterator = struct { } pub fn next(self: *LocationIterator) ?[]const u8 { - // Skip empty components while (self.position < self.location.len and (self.location[self.position] == '/' or self.location[self.position] == '\\')) { diff --git a/shared/fs/afs/read/read.zig b/shared/fs/afs/read/read.zig index 7b232db..6adb4be 100644 --- a/shared/fs/afs/read/read.zig +++ b/shared/fs/afs/read/read.zig @@ -3,13 +3,11 @@ pub const unit = @import("unit.zig"); pub const location = @import("location.zig"); -// Unit operations pub const read_span = unit.read_span; pub const read_unit_inline_spans = unit.read_unit_inline_spans; pub const read_unit = unit.read_unit; pub const ReadError = unit.ReadError; -// Location operations pub const component_to_identity = location.component_to_identity; pub const LocationIterator = location.LocationIterator; pub const LocationError = location.LocationError; diff --git a/shared/fs/afs/read/unit.zig b/shared/fs/afs/read/unit.zig index cecd5f7..46a7b82 100644 --- a/shared/fs/afs/read/unit.zig +++ b/shared/fs/afs/read/unit.zig @@ -17,7 +17,6 @@ pub const ReadError = error{ BufferTooSmall, }; -/// Read data from a span into a buffer pub fn read_span( reader: *const BlockReader, span: *const SpanDescriptor, @@ -50,7 +49,6 @@ pub fn read_span( return bytes_read; } -/// Read unit data using inline spans pub fn read_unit_inline_spans( reader: *const BlockReader, channel: *const ChannelInfo, @@ -86,7 +84,6 @@ pub fn read_unit_inline_spans( return bytes_read; } -/// Read entire unit data (inline spans only, no overflow support yet) pub fn read_unit( reader: *const BlockReader, unit: *const UnitRecord, diff --git a/shared/fs/afs/types/types.zig b/shared/fs/afs/types/types.zig index 151435e..21d5ad6 100644 --- a/shared/fs/afs/types/types.zig +++ b/shared/fs/afs/types/types.zig @@ -8,17 +8,14 @@ pub const attributes = @import("attributes.zig"); pub const journal = @import("journal.zig"); pub const timestamp = @import("timestamp.zig"); -// Volume types pub const VolumeHeader = volume.VolumeHeader; pub const SpanDescriptor = volume.SpanDescriptor; pub const ChannelInfo = volume.ChannelInfo; -// B-tree types pub const BTreeNodeDescriptor = btree.NodeDescriptor; pub const BTreeHeaderRecord = btree.HeaderRecord; pub const IndexKey = btree.IndexKey; -// Catalog types pub const StackRecord = catalog.StackRecord; pub const UnitRecord = catalog.UnitRecord; pub const ThreadRecord = catalog.ThreadRecord; @@ -28,20 +25,16 @@ pub const SpecialInfo = catalog.SpecialInfo; pub const AliasInfo = catalog.AliasInfo; pub const TwinInfo = catalog.TwinInfo; -// Extent types pub const SpanKey = extents.SpanKey; pub const SpanRecord = extents.SpanRecord; -// Attribute types pub const AttributeKey = attributes.AttributeKey; pub const AttributeInlineRecord = attributes.AttributeInlineRecord; pub const AttributeChannelRecord = attributes.AttributeChannelRecord; -// Journal types pub const JournalInfoCell = journal.JournalInfoCell; pub const JournalHeader = journal.JournalHeader; pub const JournalCellList = journal.JournalCellList; pub const JournalCellInfo = journal.JournalCellInfo; -// Timestamp pub const Timestamp = timestamp.Timestamp; diff --git a/shared/fs/afs/write/allocate.zig b/shared/fs/afs/write/allocate.zig index 2377f4f..9573a88 100644 --- a/shared/fs/afs/write/allocate.zig +++ b/shared/fs/afs/write/allocate.zig @@ -5,7 +5,6 @@ pub const AllocationError = error{ InvalidCell, }; -/// Allocation bitmap operations pub const AllocationMap = struct { bitmap: []u8, total_cells: u32, @@ -19,7 +18,6 @@ pub const AllocationMap = struct { }; } - /// Mark a cell as allocated pub fn mark_allocated(self: *AllocationMap, cell: u32) void { if (cell >= self.total_cells) return; const byte_index = cell / 8; @@ -27,7 +25,6 @@ pub const AllocationMap = struct { self.bitmap[byte_index] |= @as(u8, 1) << bit_index; } - /// Mark a cell as free pub fn mark_free(self: *AllocationMap, cell: u32) void { if (cell >= self.total_cells) return; const byte_index = cell / 8; @@ -35,7 +32,6 @@ pub const AllocationMap = struct { self.bitmap[byte_index] &= ~(@as(u8, 1) << bit_index); } - /// Check if a cell is allocated pub fn is_allocated(self: *const AllocationMap, cell: u32) bool { if (cell >= self.total_cells) return true; const byte_index = cell / 8; @@ -43,7 +39,6 @@ pub const AllocationMap = struct { return (self.bitmap[byte_index] & (@as(u8, 1) << bit_index)) != 0; } - /// Allocate a contiguous range of cells pub fn allocate_cells(self: *AllocationMap, count: u32) AllocationError!u32 { if (count == 0) return self.next_free; @@ -61,7 +56,6 @@ pub const AllocationMap = struct { return start; } - /// Mark a range of cells as allocated (for reserved areas) pub fn reserve_range(self: *AllocationMap, start: u32, count: u32) void { var i: u32 = 0; while (i < count) : (i += 1) { @@ -69,7 +63,6 @@ pub const AllocationMap = struct { } } - /// Get count of free cells pub fn free_count(self: *const AllocationMap) u32 { var count: u32 = 0; var i: u32 = 0; @@ -82,7 +75,6 @@ pub const AllocationMap = struct { } }; -/// Calculate bitmap size needed for a given number of cells pub fn bitmap_size(total_cells: u32) u32 { return (total_cells + 7) / 8; } diff --git a/shared/fs/afs/write/stack.zig b/shared/fs/afs/write/stack.zig index 69afd53..0507bae 100644 --- a/shared/fs/afs/write/stack.zig +++ b/shared/fs/afs/write/stack.zig @@ -10,7 +10,6 @@ const IndexKey = types.IndexKey; const Permissions = types.Permissions; const ChannelInfo = types.ChannelInfo; -/// Create a stack record pub fn create_stack_record( node_id: u32, timestamp: u64, @@ -40,7 +39,6 @@ pub fn create_stack_record( }; } -/// Create a unit record pub fn create_unit_record( node_id: u32, timestamp: u64, @@ -73,7 +71,6 @@ pub fn create_unit_record( }; } -/// Create an index key for a catalog entry pub fn create_index_key( parent_node_id: u32, identity: []const u8, @@ -91,7 +88,6 @@ pub fn create_index_key( return key; } -/// Get the size of an index key in bytes pub fn index_key_size(identity_len: usize) usize { return 8 + identity_len * 2; } diff --git a/shared/fs/afs/write/unit.zig b/shared/fs/afs/write/unit.zig index a20da4e..0162219 100644 --- a/shared/fs/afs/write/unit.zig +++ b/shared/fs/afs/write/unit.zig @@ -15,7 +15,6 @@ pub const WriteError = error{ InvalidCell, }; -/// Write data to a span pub fn write_span( writer: *const BlockWriter, span: *const SpanDescriptor, @@ -29,7 +28,6 @@ pub fn write_span( var current_cell = span.start_cell; while (bytes_written < bytes_to_write) { - // Clear cell buffer for (cell_buffer) |*b| { b.* = 0; } @@ -37,7 +35,6 @@ pub fn write_span( const bytes_remaining = bytes_to_write - bytes_written; const bytes_to_copy = if (bytes_remaining < writer.cell_size) bytes_remaining else writer.cell_size; - // Copy data to cell buffer var i: u64 = 0; while (i < bytes_to_copy) : (i += 1) { cell_buffer[@intCast(i)] = data[@intCast(bytes_written + i)]; @@ -54,13 +51,11 @@ pub fn write_span( return bytes_written; } -/// Calculate number of cells needed for a given size pub fn cells_needed(size: u64, cell_size: u32) u32 { if (size == 0) return 0; return @intCast((size + cell_size - 1) / cell_size); } -/// Create a channel info for inline data pub fn create_channel_info( logical_size: u64, start_cell: u64, diff --git a/shared/fs/afs/write/write.zig b/shared/fs/afs/write/write.zig index b33ed3b..c4c1727 100644 --- a/shared/fs/afs/write/write.zig +++ b/shared/fs/afs/write/write.zig @@ -4,18 +4,15 @@ pub const unit = @import("unit.zig"); pub const allocate = @import("allocate.zig"); pub const stack = @import("stack.zig"); -// Unit operations pub const write_span = unit.write_span; pub const cells_needed = unit.cells_needed; pub const create_channel_info = unit.create_channel_info; pub const WriteError = unit.WriteError; -// Allocation operations pub const AllocationMap = allocate.AllocationMap; pub const AllocationError = allocate.AllocationError; pub const bitmap_size = allocate.bitmap_size; -// Stack operations pub const create_stack_record = stack.create_stack_record; pub const create_unit_record = stack.create_unit_record; pub const create_index_key = stack.create_index_key; diff --git a/shared/fs/fat32/constants/constants.zig b/shared/fs/fat32/constants/constants.zig index 2034699..80b8d08 100644 --- a/shared/fs/fat32/constants/constants.zig +++ b/shared/fs/fat32/constants/constants.zig @@ -1,63 +1,52 @@ //! FAT32 Constants -/// Boot sector signature pub const boot_signature: u16 = 0xAA55; -/// FAT32 filesystem type string pub const fs_type_fat32: [8]u8 = .{ 'F', 'A', 'T', '3', '2', ' ', ' ', ' ' }; -// Entry attributes pub const attr_read_only: u8 = 0x01; pub const attr_hidden: u8 = 0x02; pub const attr_system: u8 = 0x04; pub const attr_volume_id: u8 = 0x08; -pub const attr_stack: u8 = 0x10; // FAT32 calls this "directory" +pub const attr_stack: u8 = 0x10; pub const attr_archive: u8 = 0x20; pub const attr_long_identity: u8 = attr_read_only | attr_hidden | attr_system | attr_volume_id; pub const attr_long_identity_mask: u8 = attr_read_only | attr_hidden | attr_system | attr_volume_id | attr_stack | attr_archive; -// FAT32 spec aliases pub const attr_directory = attr_stack; pub const attr_long_name = attr_long_identity; pub const attr_long_name_mask = attr_long_identity_mask; -// Entry markers pub const entry_free: u8 = 0xE5; pub const entry_end: u8 = 0x00; pub const entry_kanji_lead: u8 = 0x05; -// Cluster values pub const cluster_free: u32 = 0x00000000; pub const cluster_reserved_start: u32 = 0x00000001; pub const cluster_reserved_end: u32 = 0x00000001; pub const cluster_data_start: u32 = 0x00000002; pub const cluster_bad: u32 = 0x0FFFFFF7; -pub const cluster_eoc_start: u32 = 0x0FFFFFF8; // End of chain start -pub const cluster_eoc: u32 = 0x0FFFFFFF; // End of chain marker +pub const cluster_eoc_start: u32 = 0x0FFFFFF8; +pub const cluster_eoc: u32 = 0x0FFFFFFF; pub const cluster_mask: u32 = 0x0FFFFFFF; -// Long identity constants pub const lfn_sequence_mask: u8 = 0x1F; pub const lfn_last_entry: u8 = 0x40; pub const lfn_chars_per_entry: usize = 13; -// Short identity lengths pub const short_identity_length: usize = 8; pub const short_ext_length: usize = 3; pub const short_total_length: usize = short_identity_length + short_ext_length; -// Sector sizes pub const sector_size_min: u16 = 512; pub const sector_size_max: u16 = 4096; -// FSInfo signatures pub const fsinfo_sig1: u32 = 0x41615252; pub const fsinfo_sig2: u32 = 0x61417272; pub const fsinfo_sig3: u32 = 0xAA550000; pub const fsinfo_unknown: u32 = 0xFFFFFFFF; -// Default values for creation pub const default_oem_name: [8]u8 = .{ 'M', 'S', 'W', 'I', 'N', '4', '.', '1' }; -pub const default_media_type: u8 = 0xF8; // Fixed disk +pub const default_media_type: u8 = 0xF8; pub const default_drive_number: u8 = 0x80; pub const default_boot_sig: u8 = 0x29; diff --git a/shared/fs/fat32/fat32.zig b/shared/fs/fat32/fat32.zig index 3e3cfdc..48454d4 100644 --- a/shared/fs/fat32/fat32.zig +++ b/shared/fs/fat32/fat32.zig @@ -5,7 +5,6 @@ pub const types = @import("types/types.zig"); pub const read = @import("read/read.zig"); pub const write = @import("write/write.zig"); -// Re-export commonly used types (Akiba terminology) pub const BootSector = types.BootSector; pub const FsInfo = types.FsInfo; pub const StackEntry = types.StackEntry; @@ -13,10 +12,8 @@ pub const LongIdentityEntry = types.LongIdentityEntry; pub const TimeFormat = types.TimeFormat; pub const DateFormat = types.DateFormat; -// FAT32 spec aliases (for compatibility) pub const DirEntry = types.DirEntry; pub const LongNameEntry = types.LongNameEntry; -// Re-export errors pub const ClusterError = read.ClusterError; pub const LocationError = read.LocationError; diff --git a/shared/fs/fat32/read/cluster.zig b/shared/fs/fat32/read/cluster.zig index 7b4161d..3ad196a 100644 --- a/shared/fs/fat32/read/cluster.zig +++ b/shared/fs/fat32/read/cluster.zig @@ -11,19 +11,16 @@ pub const ClusterError = error{ ReadFailed, }; -/// Check if cluster number is valid data cluster pub fn is_valid_cluster(cluster: u32) bool { return cluster >= constants.cluster_data_start and cluster < constants.cluster_eoc_start and cluster != constants.cluster_bad; } -/// Check if cluster marks end of chain pub fn is_end_of_chain(cluster: u32) bool { return (cluster & constants.cluster_mask) >= constants.cluster_eoc_start; } -/// Calculate FAT sector and offset for a cluster pub fn get_fat_position(cluster: u32, bytes_per_sector: u32) struct { sector: u32, offset: u32 } { const fat_offset = cluster * 4; return .{ @@ -32,7 +29,6 @@ pub fn get_fat_position(cluster: u32, bytes_per_sector: u32) struct { sector: u3 }; } -/// Calculate LBA for a cluster's data pub fn cluster_to_lba( cluster: u32, data_start_lba: u64, @@ -41,7 +37,6 @@ pub fn cluster_to_lba( return data_start_lba + (@as(u64, cluster - 2) * sectors_per_cluster); } -/// Read next cluster from FAT entry pub fn parse_fat_entry(entry: u32) ClusterError!?u32 { const next = entry & constants.cluster_mask; diff --git a/shared/fs/fat32/read/read.zig b/shared/fs/fat32/read/read.zig index 7fca337..2bd7411 100644 --- a/shared/fs/fat32/read/read.zig +++ b/shared/fs/fat32/read/read.zig @@ -3,7 +3,6 @@ pub const cluster = @import("cluster.zig"); pub const stack = @import("stack.zig"); -// Cluster operations pub const ClusterError = cluster.ClusterError; pub const is_valid_cluster = cluster.is_valid_cluster; pub const is_end_of_chain = cluster.is_end_of_chain; @@ -11,7 +10,6 @@ pub const get_fat_position = cluster.get_fat_position; pub const cluster_to_lba = cluster.cluster_to_lba; pub const parse_fat_entry = cluster.parse_fat_entry; -// Stack operations pub const LocationError = stack.LocationError; pub const identities_equal = stack.identities_equal; pub const LocationIterator = stack.LocationIterator; diff --git a/shared/fs/fat32/read/stack.zig b/shared/fs/fat32/read/stack.zig index ded95eb..8df3ba4 100644 --- a/shared/fs/fat32/read/stack.zig +++ b/shared/fs/fat32/read/stack.zig @@ -10,7 +10,6 @@ pub const LocationError = error{ InvalidLocation, }; -/// Case-insensitive comparison for identities pub fn identities_equal(a: []const u8, b: []const u8) bool { if (a.len != b.len) { return false; @@ -25,14 +24,12 @@ pub fn identities_equal(a: []const u8, b: []const u8) bool { return true; } -/// Location component iterator pub const LocationIterator = struct { location: []const u8, position: usize, pub fn init(location: []const u8) LocationIterator { var start: usize = 0; - // Skip leading separator if (location.len > 0 and (location[0] == '/' or location[0] == '\\')) { start = 1; } @@ -43,7 +40,6 @@ pub const LocationIterator = struct { } pub fn next(self: *LocationIterator) ?[]const u8 { - // Skip empty components while (self.position < self.location.len and (self.location[self.position] == '/' or self.location[self.position] == '\\')) { @@ -70,7 +66,6 @@ pub const LocationIterator = struct { } }; -/// Check if entry matches an identity (case-insensitive) pub fn entry_matches_identity(entry: *const StackEntry, identity: []const u8) bool { var short_identity_buf: [12]u8 = undefined; const short_identity_len = entry.get_short_identity(&short_identity_buf); diff --git a/shared/fs/fat32/types/boot.zig b/shared/fs/fat32/types/boot.zig index 2735404..68fc786 100644 --- a/shared/fs/fat32/types/boot.zig +++ b/shared/fs/fat32/types/boot.zig @@ -9,15 +9,14 @@ pub const BootSector = extern struct { sectors_per_cluster: u8, reserved_sectors: u16 align(1), fat_count: u8, - root_entry_count: u16 align(1), // Must be 0 for FAT32 - total_sectors_16: u16 align(1), // Must be 0 for FAT32 + root_entry_count: u16 align(1), + total_sectors_16: u16 align(1), media_type: u8, - fat_size_16: u16 align(1), // Must be 0 for FAT32 + fat_size_16: u16 align(1), sectors_per_track: u16 align(1), head_count: u16 align(1), hidden_sectors: u32 align(1), total_sectors_32: u32 align(1), - // FAT32 extended BPB fat_size_32: u32 align(1), ext_flags: u16 align(1), fs_version: u16 align(1), diff --git a/shared/fs/fat32/types/entry.zig b/shared/fs/fat32/types/entry.zig index a2668ff..daa949a 100644 --- a/shared/fs/fat32/types/entry.zig +++ b/shared/fs/fat32/types/entry.zig @@ -2,7 +2,6 @@ const constants = @import("../constants/constants.zig"); -/// Standard 32-byte entry - Stack or Unit pub const StackEntry = extern struct { identity: [8]u8, extension: [3]u8, @@ -47,7 +46,6 @@ pub const StackEntry = extern struct { self.first_cluster_low = @intCast(cluster & 0xFFFF); } - /// Extract short identity (8.3 format) into buffer, returns length pub fn get_short_identity(self: *const StackEntry, buffer: *[12]u8) usize { var length: usize = 0; @@ -56,7 +54,6 @@ pub const StackEntry = extern struct { first_byte = constants.entry_free; } - // Copy identity part (up to 8 chars, stop at space) var i: usize = 0; while (i < 8 and self.identity[i] != ' ') : (i += 1) { if (i == 0) { @@ -67,7 +64,6 @@ pub const StackEntry = extern struct { length += 1; } - // Add extension if present if (self.extension[0] != ' ') { buffer[length] = '.'; length += 1; @@ -83,16 +79,15 @@ pub const StackEntry = extern struct { } }; -/// Long identity entry (LFN) pub const LongIdentityEntry = extern struct { sequence: u8, - identity_1: [10]u8, // 5 UTF-16 characters + identity_1: [10]u8, attributes: u8, entry_type: u8, checksum: u8, - identity_2: [12]u8, // 6 UTF-16 characters - first_cluster: u16 align(1), // Always 0 - identity_3: [4]u8, // 2 UTF-16 characters + identity_2: [12]u8, + first_cluster: u16 align(1), + identity_3: [4]u8, pub fn is_last(self: *const LongIdentityEntry) bool { return (self.sequence & constants.lfn_last_entry) != 0; @@ -102,25 +97,21 @@ pub const LongIdentityEntry = extern struct { return self.sequence & constants.lfn_sequence_mask; } - /// Extract 13 UTF-16 characters from this entry pub fn extract_chars(self: *const LongIdentityEntry, buffer: *[13]u16) void { var index: usize = 0; - // Extract from identity_1 (5 chars) var i: usize = 0; while (i < 10) : (i += 2) { buffer[index] = @as(u16, self.identity_1[i]) | (@as(u16, self.identity_1[i + 1]) << 8); index += 1; } - // Extract from identity_2 (6 chars) i = 0; while (i < 12) : (i += 2) { buffer[index] = @as(u16, self.identity_2[i]) | (@as(u16, self.identity_2[i + 1]) << 8); index += 1; } - // Extract from identity_3 (2 chars) i = 0; while (i < 4) : (i += 2) { buffer[index] = @as(u16, self.identity_3[i]) | (@as(u16, self.identity_3[i + 1]) << 8); @@ -129,20 +120,17 @@ pub const LongIdentityEntry = extern struct { } }; -/// Time format (packed into 16 bits) pub const TimeFormat = packed struct(u16) { second_div_2: u5, minute: u6, hour: u5, }; -/// Date format (packed into 16 bits) pub const DateFormat = packed struct(u16) { day: u5, month: u4, year_from_1980: u7, }; -// FAT32 spec aliases (for external compatibility) pub const DirEntry = StackEntry; pub const LongNameEntry = LongIdentityEntry; diff --git a/shared/fs/fat32/types/types.zig b/shared/fs/fat32/types/types.zig index 7be282e..5799b3c 100644 --- a/shared/fs/fat32/types/types.zig +++ b/shared/fs/fat32/types/types.zig @@ -3,16 +3,13 @@ pub const boot = @import("boot.zig"); pub const entry = @import("entry.zig"); -// Boot sector types pub const BootSector = boot.BootSector; pub const FsInfo = boot.FsInfo; -// Entry types (Akiba terminology) pub const StackEntry = entry.StackEntry; pub const LongIdentityEntry = entry.LongIdentityEntry; pub const TimeFormat = entry.TimeFormat; pub const DateFormat = entry.DateFormat; -// FAT32 spec aliases pub const DirEntry = entry.DirEntry; pub const LongNameEntry = entry.LongNameEntry; diff --git a/shared/fs/fat32/write/boot.zig b/shared/fs/fat32/write/boot.zig index b7bc459..3f64c2e 100644 --- a/shared/fs/fat32/write/boot.zig +++ b/shared/fs/fat32/write/boot.zig @@ -7,7 +7,6 @@ const types = @import("../types/types.zig"); const BootSector = types.BootSector; const FsInfo = types.FsInfo; -/// Parameters for creating a FAT32 filesystem pub const CreateParams = struct { total_sectors: u32, hidden_sectors: u32 = 0, @@ -19,16 +18,13 @@ pub const CreateParams = struct { volume_label: [11]u8 = .{ 'N', 'O', ' ', 'N', 'A', 'M', 'E', ' ', ' ', ' ', ' ' }, }; -/// Calculate FAT size in sectors pub fn calculate_fat_size(params: CreateParams) u32 { const data_sectors = params.total_sectors - params.reserved_sectors; const cluster_count = data_sectors / params.sectors_per_cluster; - // Each FAT entry is 4 bytes const fat_bytes = (cluster_count + 2) * 4; return (fat_bytes + params.bytes_per_sector - 1) / params.bytes_per_sector; } -/// Create a boot sector structure pub fn create_boot_sector(params: CreateParams) BootSector { const fat_size = calculate_fat_size(params); @@ -67,7 +63,6 @@ pub fn create_boot_sector(params: CreateParams) BootSector { return boot; } -/// Create FSInfo structure pub fn create_fsinfo(free_clusters: u32, next_free: u32) FsInfo { return FsInfo{ .signature_1 = constants.fsinfo_sig1, @@ -80,20 +75,14 @@ pub fn create_fsinfo(free_clusters: u32, next_free: u32) FsInfo { }; } -/// Initialize FAT table with required entries pub fn init_fat_table(fat: []u8) void { - // Clear @memset(fat, 0); - // Entry 0: Media type std.mem.writeInt(u32, fat[0..4], 0x0FFFFFF8, .little); - // Entry 1: End of chain marker std.mem.writeInt(u32, fat[4..8], 0x0FFFFFFF, .little); - // Entry 2: Origin stack EOC std.mem.writeInt(u32, fat[8..12], 0x0FFFFFFF, .little); } -/// Allocate a cluster in the FAT pub fn allocate_cluster(fat: []u8, cluster: u32) void { const offset = cluster * 4; if (offset + 4 <= fat.len) { @@ -101,7 +90,6 @@ pub fn allocate_cluster(fat: []u8, cluster: u32) void { } } -/// Link two clusters in the FAT pub fn link_clusters(fat: []u8, from: u32, to: u32) void { const offset = from * 4; if (offset + 4 <= fat.len) { diff --git a/shared/fs/fat32/write/entry.zig b/shared/fs/fat32/write/entry.zig index 31f672e..3a26823 100644 --- a/shared/fs/fat32/write/entry.zig +++ b/shared/fs/fat32/write/entry.zig @@ -6,7 +6,6 @@ const types = @import("../types/types.zig"); const StackEntry = types.StackEntry; -/// Create a short (8.3) entry pub fn create_entry( identity: []const u8, extension: []const u8, @@ -30,13 +29,11 @@ pub fn create_entry( .unit_size = unit_size, }; - // Copy identity (uppercase) for (identity, 0..) |c, i| { if (i >= 8) break; entry.identity[i] = to_upper(c); } - // Copy extension (uppercase) for (extension, 0..) |c, i| { if (i >= 3) break; entry.extension[i] = to_upper(c); @@ -45,7 +42,6 @@ pub fn create_entry( return entry; } -/// Create a stack entry pub fn create_stack_entry( identity: []const u8, first_cluster: u32, @@ -53,17 +49,14 @@ pub fn create_stack_entry( return create_entry(identity, "", constants.attr_stack, first_cluster, 0); } -/// Create a "." entry for current stack pub fn create_dot_entry(cluster: u32) StackEntry { return create_entry(".", "", constants.attr_stack, cluster, 0); } -/// Create a ".." entry for parent stack pub fn create_dotdot_entry(parent_cluster: u32) StackEntry { return create_entry("..", "", constants.attr_stack, parent_cluster, 0); } -/// Create a unit entry pub fn create_unit_entry( identity: []const u8, extension: []const u8, @@ -73,7 +66,6 @@ pub fn create_unit_entry( return create_entry(identity, extension, constants.attr_archive, first_cluster, unit_size); } -/// Convert character to uppercase fn to_upper(c: u8) u8 { if (c >= 'a' and c <= 'z') { return c - 32; @@ -81,9 +73,7 @@ fn to_upper(c: u8) u8 { return c; } -/// Parse identity into name and extension parts pub fn parse_identity(identity: []const u8) struct { name: []const u8, ext: []const u8 } { - // Find last dot var dot_pos: ?usize = null; var i: usize = identity.len; while (i > 0) { diff --git a/shared/fs/fat32/write/write.zig b/shared/fs/fat32/write/write.zig index 711293e..b6a596e 100644 --- a/shared/fs/fat32/write/write.zig +++ b/shared/fs/fat32/write/write.zig @@ -3,7 +3,6 @@ pub const boot = @import("boot.zig"); pub const entry = @import("entry.zig"); -// Boot sector creation pub const CreateParams = boot.CreateParams; pub const calculate_fat_size = boot.calculate_fat_size; pub const create_boot_sector = boot.create_boot_sector; @@ -12,7 +11,6 @@ pub const init_fat_table = boot.init_fat_table; pub const allocate_cluster = boot.allocate_cluster; pub const link_clusters = boot.link_clusters; -// Entry creation pub const create_entry = entry.create_entry; pub const create_stack_entry = entry.create_stack_entry; pub const create_dot_entry = entry.create_dot_entry; diff --git a/shared/shared.zig b/shared/shared.zig index 323ea4b..22dfed1 100644 --- a/shared/shared.zig +++ b/shared/shared.zig @@ -2,6 +2,5 @@ pub const fs = @import("fs/fs.zig"); -// Convenience re-exports pub const afs = fs.afs; pub const fat32 = fs.fat32; diff --git a/toolchain/mkafsdisk/afs/afs.zig b/toolchain/mkafsdisk/afs/afs.zig index 8819f05..eebded8 100644 --- a/toolchain/mkafsdisk/afs/afs.zig +++ b/toolchain/mkafsdisk/afs/afs.zig @@ -1,12 +1,9 @@ //! mkafsdisk AFS Adapter -//! -//! Uses shared/afs for types and constants, provides host file I/O writer. const shared_afs = @import("shared").afs; pub const writer = @import("writer.zig"); -// Re-export shared types and constants pub const constants = shared_afs.constants; pub const types = shared_afs.types; @@ -22,5 +19,4 @@ pub const Permissions = shared_afs.Permissions; pub const JournalInfoCell = shared_afs.types.JournalInfoCell; pub const JournalHeader = shared_afs.types.JournalHeader; -// Writer using shared types pub const Writer = writer.Writer; diff --git a/toolchain/mkafsdisk/afs/writer.zig b/toolchain/mkafsdisk/afs/writer.zig index 9910e14..2315f32 100644 --- a/toolchain/mkafsdisk/afs/writer.zig +++ b/toolchain/mkafsdisk/afs/writer.zig @@ -1,11 +1,9 @@ //! AFS Writer for mkafsdisk -//! Creates AFS filesystem structures on disk using shared/afs types. const std = @import("std"); const strings = @import("../strings/strings.zig"); const shared_afs = @import("shared").afs; -// Import shared types and constants const constants = shared_afs.constants; const types = shared_afs.types; const write_ops = shared_afs.write; @@ -55,15 +53,6 @@ pub const Writer = struct { const cell_size = constants.default_cell_size; const total_cells: u32 = @intCast(partition_size_bytes / cell_size); - // Layout: - // Cell 0: Volume Header - // Cell 1: Alternate Volume Header - // Cell 2: Journal Info - // Cells 3-10: Journal (8 cells = 32KB) - // Cells 11-14: Allocation Map (4 cells for up to 128K cells) - // Cells 15-30: Index B-tree (16 cells) - // Cells 31+: Data - const allocation_map_start: u32 = 11; const allocation_map_cells: u32 = 4; const index_start: u32 = allocation_map_start + allocation_map_cells; diff --git a/toolchain/mkafsdisk/main.zig b/toolchain/mkafsdisk/main.zig index 681ec78..2b95881 100644 --- a/toolchain/mkafsdisk/main.zig +++ b/toolchain/mkafsdisk/main.zig @@ -1,18 +1,13 @@ //! mkafsdisk - Creates bootable Akiba disk image -//! -//! Creates a GPT disk with: -//! - Partition 1: ESP (FAT32) with /EFI/BOOT/BOOTX64.EFI -//! - Partition 2: AFS with /system/akiba/mirai.kernel const std = @import("std"); const strings = @import("strings/strings.zig"); const afs = @import("afs/afs.zig"); const SECTOR_SIZE: u32 = 512; -const ESP_SIZE_SECTORS: u32 = 69632; // 34MB so cluster count stays above the FAT32 minimum of 65525 +const ESP_SIZE_SECTORS: u32 = 69632; const FAT32_MINIMUM_CLUSTERS: u32 = 65525; -// ESP partition type GUID: C12A7328-F81F-11D2-BA4B-00A0C93EC93B const ESP_TYPE_GUID = [16]u8{ 0x28, 0x73, 0x2A, 0xC1, 0x1F, 0xF8, 0xD2, 0x11, @@ -55,14 +50,6 @@ fn create_disk_image( const total_bytes: u64 = @as(u64, size_megabytes) * 1024 * 1024; const total_sectors: u32 = @intCast(total_bytes / SECTOR_SIZE); - // Partition layout: - // Sector 0: Protective MBR - // Sector 1: GPT Header - // Sectors 2-33: GPT Partition Entries - // Sectors 2048-67583: ESP (FAT32) - // Sectors 67584+: AFS - // Last 33 sectors: Backup GPT - const esp_start_sector: u32 = 2048; const esp_end_sector: u32 = esp_start_sector + ESP_SIZE_SECTORS - 1; const afs_start_sector: u32 = esp_end_sector + 1; @@ -85,12 +72,10 @@ fn create_disk_image( fn write_protective_mbr(file: std.fs.File, total_sectors: u32) !void { var mbr: [512]u8 = [_]u8{0} ** 512; - // Protective MBR partition entry at offset 0x1BE - mbr[0x1BE + 4] = 0xEE; // GPT protective type + mbr[0x1BE + 4] = 0xEE; std.mem.writeInt(u32, mbr[0x1BE + 8 ..][0..4], 1, .little); std.mem.writeInt(u32, mbr[0x1BE + 12 ..][0..4], total_sectors - 1, .little); - // Boot signature mbr[510] = 0x55; mbr[511] = 0xAA; @@ -123,30 +108,23 @@ fn write_gpt( afs_end: u32, total_sectors: u32, ) !void { - // Partition entries (128 entries * 128 bytes = 16KB) var partition_entries: [16384]u8 = [_]u8{0} ** 16384; - // ESP partition entry @memcpy(partition_entries[0..16], &ESP_TYPE_GUID); - // Unique partition GUID const esp_unique_guid = [16]u8{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 }; @memcpy(partition_entries[16..32], &esp_unique_guid); std.mem.writeInt(u64, partition_entries[32..40], esp_start, .little); std.mem.writeInt(u64, partition_entries[40..48], esp_end, .little); - // Name: strings.names.esp_partition_name const esp_name = strings.names.esp_partition_name; for (esp_name, 0..) |char, index| { partition_entries[56 + index * 2] = char; } - // AFS partition entry @memcpy(partition_entries[128..144], &afs.constants.partition_type_guid); - // Unique partition GUID const afs_unique_guid = [16]u8{ 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20 }; @memcpy(partition_entries[144..160], &afs_unique_guid); std.mem.writeInt(u64, partition_entries[160..168], afs_start, .little); std.mem.writeInt(u64, partition_entries[168..176], afs_end, .little); - // Name: strings.names.afs_partition_name const afs_name = strings.names.afs_partition_name; for (afs_name, 0..) |char, index| { partition_entries[184 + index * 2] = char; @@ -154,39 +132,33 @@ fn write_gpt( const entries_crc = calculate_crc32(&partition_entries); - // GPT Header var gpt_header: [512]u8 = [_]u8{0} ** 512; @memcpy(gpt_header[0..8], strings.names.gpt_signature); - std.mem.writeInt(u32, gpt_header[8..12], 0x00010000, .little); // Revision - std.mem.writeInt(u32, gpt_header[12..16], 92, .little); // Header size - // CRC at offset 16 - filled in later - std.mem.writeInt(u64, gpt_header[24..32], 1, .little); // Current LBA - std.mem.writeInt(u64, gpt_header[32..40], @as(u64, total_sectors) - 1, .little); // Backup LBA - std.mem.writeInt(u64, gpt_header[40..48], 34, .little); // First usable LBA - std.mem.writeInt(u64, gpt_header[48..56], @as(u64, total_sectors) - 34, .little); // Last usable LBA - // Disk GUID + std.mem.writeInt(u32, gpt_header[8..12], 0x00010000, .little); + std.mem.writeInt(u32, gpt_header[12..16], 92, .little); + std.mem.writeInt(u64, gpt_header[24..32], 1, .little); + std.mem.writeInt(u64, gpt_header[32..40], @as(u64, total_sectors) - 1, .little); + std.mem.writeInt(u64, gpt_header[40..48], 34, .little); + std.mem.writeInt(u64, gpt_header[48..56], @as(u64, total_sectors) - 34, .little); const disk_guid = [16]u8{ 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99 }; @memcpy(gpt_header[56..72], &disk_guid); - std.mem.writeInt(u64, gpt_header[72..80], 2, .little); // Partition entries LBA - std.mem.writeInt(u32, gpt_header[80..84], 128, .little); // Number of entries - std.mem.writeInt(u32, gpt_header[84..88], 128, .little); // Entry size - std.mem.writeInt(u32, gpt_header[88..92], entries_crc, .little); // Entries CRC + std.mem.writeInt(u64, gpt_header[72..80], 2, .little); + std.mem.writeInt(u32, gpt_header[80..84], 128, .little); + std.mem.writeInt(u32, gpt_header[84..88], 128, .little); + std.mem.writeInt(u32, gpt_header[88..92], entries_crc, .little); const header_crc = calculate_crc32(gpt_header[0..92]); std.mem.writeInt(u32, gpt_header[16..20], header_crc, .little); - // Write primary GPT try file.seekTo(SECTOR_SIZE); try file.writeAll(&gpt_header); try file.seekTo(2 * SECTOR_SIZE); try file.writeAll(&partition_entries); - // Write backup GPT (entries before header at end of disk) const backup_entries_lba = total_sectors - 33; try file.seekTo(@as(u64, backup_entries_lba) * SECTOR_SIZE); try file.writeAll(&partition_entries); - // Backup GPT header var backup_header = gpt_header; std.mem.writeInt(u64, backup_header[24..32], @as(u64, total_sectors) - 1, .little); std.mem.writeInt(u64, backup_header[32..40], 1, .little); @@ -220,7 +192,6 @@ fn create_esp( const data_sectors = available_sectors - (fat_size_sectors * fat_count); const total_clusters = data_sectors / sectors_per_cluster; - // FAT32 Boot Sector var boot_sector: [512]u8 = [_]u8{0} ** 512; boot_sector[0] = 0xEB; boot_sector[1] = 0x58; @@ -255,7 +226,6 @@ fn create_esp( try file.seekTo(esp_start_byte); try file.writeAll(&boot_sector); - // FSInfo var fsinfo: [512]u8 = [_]u8{0} ** 512; std.mem.writeInt(u32, fsinfo[0..4], 0x41615252, .little); std.mem.writeInt(u32, fsinfo[484..488], 0x61417272, .little); @@ -266,11 +236,9 @@ fn create_esp( try file.seekTo(esp_start_byte + SECTOR_SIZE); try file.writeAll(&fsinfo); - // Backup boot sector try file.seekTo(esp_start_byte + 6 * SECTOR_SIZE); try file.writeAll(&boot_sector); - // FAT table const fat_bytes = fat_size_sectors * SECTOR_SIZE; var fat_table = try allocator.alloc(u8, fat_bytes); defer allocator.free(fat_table); @@ -284,24 +252,19 @@ fn create_esp( const fat2_offset = fat1_offset + fat_bytes; const data_start = fat2_offset + fat_bytes; - // Copy EFI/BOOT/BOOTX64.EFI from source var current_cluster: u32 = 3; - // Origin stack (root) var origin_stack: [512]u8 = [_]u8{0} ** 512; - // EFI stack entry in origin @memcpy(origin_stack[0..11], strings.names.dir_entry_efi); origin_stack[11] = 0x10; std.mem.writeInt(u16, origin_stack[26..28], @intCast(current_cluster), .little); const efi_cluster = current_cluster; current_cluster += 1; - // Write origin stack at cluster 2 try file.seekTo(data_start); try file.writeAll(&origin_stack); - // EFI stack var efi_stack: [512]u8 = [_]u8{0} ** 512; @memcpy(efi_stack[0..11], strings.names.dir_entry_current); efi_stack[11] = 0x10; @@ -318,7 +281,6 @@ fn create_esp( try file.writeAll(&efi_stack); std.mem.writeInt(u32, fat_table[efi_cluster * 4 ..][0..4], 0x0FFFFFFF, .little); - // BOOT stack var boot_stack: [512]u8 = [_]u8{0} ** 512; @memcpy(boot_stack[0..11], strings.names.dir_entry_current); boot_stack[11] = 0x10; @@ -327,7 +289,6 @@ fn create_esp( boot_stack[43] = 0x10; std.mem.writeInt(u16, boot_stack[58..60], @intCast(efi_cluster), .little); - // Try to find and copy BOOTX64.EFI const bootloader_location = try std.fs.path.join(allocator, &.{ source_location, strings.names.stack_efi, strings.names.stack_boot, strings.names.unit_bootloader }); defer allocator.free(bootloader_location); @@ -349,7 +310,6 @@ fn create_esp( std.debug.print(strings.messages.bootloader_adding, .{ bootloader_size, bootloader_clusters }); - // Add BOOTX64.EFI entry @memcpy(boot_stack[64..75], strings.names.dir_entry_bootloader); boot_stack[75] = 0x20; std.mem.writeInt(u16, boot_stack[90..92], @intCast(current_cluster), .little); @@ -360,7 +320,6 @@ fn create_esp( try file.writeAll(&boot_stack); std.mem.writeInt(u32, fat_table[boot_cluster * 4 ..][0..4], 0x0FFFFFFF, .little); - // Copy bootloader data var cluster_index: u32 = 0; while (cluster_index < bootloader_clusters) : (cluster_index += 1) { var buffer: [512]u8 = [_]u8{0} ** 512; @@ -375,7 +334,6 @@ fn create_esp( std.mem.writeInt(u32, fat_table[cluster_number * 4 ..][0..4], next_cluster, .little); } - // Write FAT tables try file.seekTo(fat1_offset); try file.writeAll(fat_table); try file.seekTo(fat2_offset); |
