sylveos

Toy Operating System
Log | Files | Refs

commit 2be6a19907a081b3489323b6bc3cb6cee478f169
parent 563bbf8e88826492dedd0b6e728264a214ff8d95
Author: Sylvia Ivory <git@sivory.net>
Date:   Tue, 10 Mar 2026 14:48:40 -0700

Move MBR into separate file

Diffstat:
Mpi/fs/fat.zig | 147-------------------------------------------------------------------------------
Api/fs/mbr.zig | 153+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpi/root.zig | 1+
Mprograms/fs.zig | 2+-
4 files changed, 155 insertions(+), 148 deletions(-)

diff --git a/pi/fs/fat.zig b/pi/fs/fat.zig @@ -10,153 +10,6 @@ pub const Error = pub const Sector = vfs.Sector; pub const Disk = vfs.Disk; -pub const MasterBootRecord = packed struct(Sector) { - // 446 bytes - // Zig doesn't allow arrays in packed - // u8[446] - boot_code: u3568, - pte1: PartitionTableEntry, - pte2: PartitionTableEntry, - pte3: PartitionTableEntry, - pte4: PartitionTableEntry, - // 0xAA55 - signature: u16, - - const Self = @This(); - - pub fn get_partition(self: *const Self, n: u2) *const PartitionTableEntry { - return switch (n) { - 0 => &self.pte1, - 1 => &self.pte2, - 2 => &self.pte3, - 3 => &self.pte4, - }; - } - - pub fn is_valid(self: *const Self) bool { - return self.signature == 0xAA55; - } - - pub fn debug_print(self: *const Self, w: *std.io.Writer) !void { - var boot_code: [446]u8 = undefined; - std.mem.writeInt(u3568, &boot_code, self.boot_code, .little); - const crc32 = std.hash.Crc32.hash(&boot_code); - - try w.print( - \\bootcode crc32 = 0x{X} - \\signature = 0x{X} - \\ - , .{ - crc32, - self.signature, - }); - - // Copy to proper alignment - var pte = self.pte1; - try w.print("Partition 1:\n", .{}); - try pte.debug_print(w); - - pte = self.pte2; - try w.print("Partition 2:\n", .{}); - try pte.debug_print(w); - - pte = self.pte3; - try w.print("Partition 3:\n", .{}); - try pte.debug_print(w); - - pte = self.pte4; - try w.print("Partition 4:\n", .{}); - try pte.debug_print(w); - } -}; - -pub const PartitionTableEntry = packed struct(u128) { - pub const Status = packed struct(u8) { - _reserved: u7, - bootable: bool, - }; - - status: Status, - chs_start: u24, - partition_type: u8, - chs_end: u24, - first_lba: u32, - number_sectors: u32, - - const Self = @This(); - - pub fn is_fat32(self: *const Self) bool { - return self.partition_type == 0x0B or self.partition_type == 0x0C; - } - - pub fn is_empty(self: *const Self) bool { - return std.mem.allEqual(u8, @ptrCast(self), 0x00); - } - - pub fn partition_type_str(self: *const Self) []const u8 { - return switch (self.partition_type) { - 0x00 => "Empty", - 0x01 => "FAT12 - CHS", - 0x04 => "FAT16 - 16-32 MB - CHS", - 0x05 => "Microsoft Extended - CHS", - 0x06 => "FAT16 - 32 MB-2 GB - CHS", - 0x07 => "NTFS", - 0x0B => "FAT32 - CHS", - 0x0C => "FAT32 - LBA", - 0x0E => "FAT16 - 32 MB-2 GB - LBA", - 0x0F => "Microsoft Extended - LBA", - 0x11 => "Hidden FAT12 - CHS", - 0x14 => "Hidden FAT16 - 16-32 MB - CHS", - 0x16 => "Hidden FAT16 - 32 MB-2 GB - CHS", - 0x1b => "Hidden FAT32 - CHS", - 0x1c => "Hidden FAT32 - LBA", - 0x1e => "Hidden FAT16 - 32 MB-2 GB - LBA", - 0x42 => "Microsoft MBR - Dynamic Disk", - 0x82 => "Solaris x86 OR Linux Swap", - 0x83 => "Linux", - 0x84 => "Hibernation", - 0x85 => "Linux Extended", - 0x86 => "NTFS Volume Set", - 0x87 => "NTFS Volume Set", - 0xa0 => "Hibernation", - 0xa1 => "Hibernation", - 0xa5 => "FreeBSD", - 0xa6 => "OpenBSD", - 0xa8 => "Mac OSX", - 0xa9 => "NetBSD", - 0xab => "Mac OSX Boot", - 0xb7 => "BSDI", - 0xb8 => "BSDI swap", - 0xee => "EFI GPT Disk", - 0xef => "EFI System Partition", - 0xfb => "Vmware File System", - 0xfc => "Vmware swap", - else => "Unknown", - }; - } - - pub fn debug_print(self: *const Self, w: *std.io.Writer) !void { - try w.print( - \\ bootable = {any} - \\ chs_start = 0x{X} - \\ partition_type = 0x{X} ({s}) - \\ chs_end = 0X{X} - \\ lba_start = 0x{X} - \\ number sectors = {d} ({B}) - \\ - , .{ - self.status.bootable, - self.chs_start, - self.partition_type, - self.partition_type_str(), - self.chs_end, - self.first_lba, - self.number_sectors, - @as(u64, self.number_sectors) * 512, - }); - } -}; - pub const Fat32BootSector = packed struct(Sector) { // u8[3] asm_code: u24, diff --git a/pi/fs/mbr.zig b/pi/fs/mbr.zig @@ -0,0 +1,153 @@ +const std = @import("std"); + +pub const vfs = @import("./vfs.zig"); + +pub const Sector = vfs.Sector; +pub const Disk = vfs.Disk; + +pub const MasterBootRecord = packed struct(Sector) { + // 446 bytes + // Zig doesn't allow arrays in packed + // u8[446] + boot_code: u3568, + pte1: PartitionTableEntry, + pte2: PartitionTableEntry, + pte3: PartitionTableEntry, + pte4: PartitionTableEntry, + // 0xAA55 + signature: u16, + + const Self = @This(); + + pub fn get_partition(self: *const Self, n: u2) *const PartitionTableEntry { + return switch (n) { + 0 => &self.pte1, + 1 => &self.pte2, + 2 => &self.pte3, + 3 => &self.pte4, + }; + } + + pub fn is_valid(self: *const Self) bool { + return self.signature == 0xAA55; + } + + pub fn debug_print(self: *const Self, w: *std.io.Writer) !void { + var boot_code: [446]u8 = undefined; + std.mem.writeInt(u3568, &boot_code, self.boot_code, .little); + const crc32 = std.hash.Crc32.hash(&boot_code); + + try w.print( + \\bootcode crc32 = 0x{X} + \\signature = 0x{X} + \\ + , .{ + crc32, + self.signature, + }); + + // Copy to proper alignment + var pte = self.pte1; + try w.print("Partition 1:\n", .{}); + try pte.debug_print(w); + + pte = self.pte2; + try w.print("Partition 2:\n", .{}); + try pte.debug_print(w); + + pte = self.pte3; + try w.print("Partition 3:\n", .{}); + try pte.debug_print(w); + + pte = self.pte4; + try w.print("Partition 4:\n", .{}); + try pte.debug_print(w); + } +}; + +pub const PartitionTableEntry = packed struct(u128) { + pub const Status = packed struct(u8) { + _reserved: u7, + bootable: bool, + }; + + status: Status, + chs_start: u24, + partition_type: u8, + chs_end: u24, + first_lba: u32, + number_sectors: u32, + + const Self = @This(); + + pub fn is_fat32(self: *const Self) bool { + return self.partition_type == 0x0B or self.partition_type == 0x0C; + } + + pub fn is_empty(self: *const Self) bool { + return std.mem.allEqual(u8, @ptrCast(self), 0x00); + } + + pub fn partition_type_str(self: *const Self) []const u8 { + return switch (self.partition_type) { + 0x00 => "Empty", + 0x01 => "FAT12 - CHS", + 0x04 => "FAT16 - 16-32 MB - CHS", + 0x05 => "Microsoft Extended - CHS", + 0x06 => "FAT16 - 32 MB-2 GB - CHS", + 0x07 => "NTFS", + 0x0B => "FAT32 - CHS", + 0x0C => "FAT32 - LBA", + 0x0E => "FAT16 - 32 MB-2 GB - LBA", + 0x0F => "Microsoft Extended - LBA", + 0x11 => "Hidden FAT12 - CHS", + 0x14 => "Hidden FAT16 - 16-32 MB - CHS", + 0x16 => "Hidden FAT16 - 32 MB-2 GB - CHS", + 0x1b => "Hidden FAT32 - CHS", + 0x1c => "Hidden FAT32 - LBA", + 0x1e => "Hidden FAT16 - 32 MB-2 GB - LBA", + 0x42 => "Microsoft MBR - Dynamic Disk", + 0x82 => "Solaris x86 OR Linux Swap", + 0x83 => "Linux", + 0x84 => "Hibernation", + 0x85 => "Linux Extended", + 0x86 => "NTFS Volume Set", + 0x87 => "NTFS Volume Set", + 0xa0 => "Hibernation", + 0xa1 => "Hibernation", + 0xa5 => "FreeBSD", + 0xa6 => "OpenBSD", + 0xa8 => "Mac OSX", + 0xa9 => "NetBSD", + 0xab => "Mac OSX Boot", + 0xb7 => "BSDI", + 0xb8 => "BSDI swap", + 0xee => "EFI GPT Disk", + 0xef => "EFI System Partition", + 0xfb => "Vmware File System", + 0xfc => "Vmware swap", + else => "Unknown", + }; + } + + pub fn debug_print(self: *const Self, w: *std.io.Writer) !void { + try w.print( + \\ bootable = {any} + \\ chs_start = 0x{X} + \\ partition_type = 0x{X} ({s}) + \\ chs_end = 0X{X} + \\ lba_start = 0x{X} + \\ number sectors = {d} ({B}) + \\ + , .{ + self.status.bootable, + self.chs_start, + self.partition_type, + self.partition_type_str(), + self.chs_end, + self.first_lba, + self.number_sectors, + @as(u64, self.number_sectors) * 512, + }); + } +}; diff --git a/pi/root.zig b/pi/root.zig @@ -29,6 +29,7 @@ pub const devices = struct { pub const fs = struct { pub const vfs = @import("./fs/vfs.zig"); pub const fat = @import("./fs/fat.zig"); + pub const mbr = @import("./fs/mbr.zig"); pub const FatVFS = @import("./fs/fatvfs.zig"); }; diff --git a/programs/fs.zig b/programs/fs.zig @@ -270,7 +270,7 @@ pub fn main() !void { var vfs_disk = vfs.Disk.impl_by(&sd_card); const vfs_fat = vfs.FileSystem.impl_by(&fat_vfs); - const mbr: pi.fs.fat.MasterBootRecord = @bitCast(try vfs_disk.read_sector(0)); + const mbr: pi.fs.mbr.MasterBootRecord = @bitCast(try vfs_disk.read_sector(0)); try mbr.debug_print(&uart.writer);