sylveos

Toy Operating System
Log | Files | Refs

commit 20467d27baa757ed36617d2778d53fedd54d042d
parent e90acc16640527926b45080708ac11997f88b049
Author: Sylvia Ivory <git@sivory.net>
Date:   Sun,  8 Mar 2026 21:57:16 -0700

Fix SD card code

Diffstat:
Minclude/emmc.c | 2+-
Mpi/devices/sd.zig | 17+++++++++++++----
Mpi/fs/vfs.zig | 4++++
Mpi/root.zig | 16++++++++++++++++
Mprograms/fs.zig | 39++++++++++++++++++++++-----------------
5 files changed, 56 insertions(+), 22 deletions(-)

diff --git a/include/emmc.c b/include/emmc.c @@ -29,7 +29,7 @@ static u32 get_clock_divider(u32 base_clock) { } int div = -1; - for (int fb = 31; fb >= 0; fb--) { + for (int fb = 30; fb >= 0; fb--) { u32 bt = (1 << fb); if (target_div & bt) { diff --git a/pi/devices/sd.zig b/pi/devices/sd.zig @@ -10,14 +10,17 @@ const Self = @This(); alloc: std.mem.Allocator, -pub fn init(alloc: std.mem.Allocator) Self { +pub fn init(alloc: std.mem.Allocator) vfs.Error!Self { + if (pi_sd.pi_sd_init() == -1) { + return vfs.Error.InitFailed; + } return .{ .alloc = alloc }; } pub fn read_sector(self: *const Self, sector: usize) vfs.Error!vfs.Sector { _ = self; - var buffer: [512]u8 = .{0} ** 512; + var buffer: [512]u8 align(512) = .{0} ** 512; if (pi_sd.pi_sd_read(&buffer, sector, 1) == -1) { return vfs.Error.ReadFailed; } @@ -26,8 +29,14 @@ pub fn read_sector(self: *const Self, sector: usize) vfs.Error!vfs.Sector { } pub fn read_sectors(self: *const Self, sector: usize, count: usize) vfs.Error![]vfs.Sector { - var buffer = try self.alloc.alloc(u8, count * 512); - if (pi_sd.pi_sd_read(@ptrCast(&buffer), sector, 1) == -1) { + const buffer = try self.alloc.allocWithOptions( + u8, + count * 512, + std.mem.Alignment.fromByteUnits(512), + null, + ); + + if (pi_sd.pi_sd_read(buffer.ptr, sector, count) == -1) { return vfs.Error.ReadFailed; } diff --git a/pi/fs/vfs.zig b/pi/fs/vfs.zig @@ -1,5 +1,7 @@ const std = @import("std"); +const uart = @import("../devices/mini-uart.zig"); + // TODO; future expansion // - File locking // - Writing @@ -16,6 +18,8 @@ pub const Error = error{ NotInitialized, InvalidFileName, + + InitFailed, ReadFailed, } || std.mem.Allocator.Error; diff --git a/pi/root.zig b/pi/root.zig @@ -101,3 +101,19 @@ export fn delay_s(s: u32) void { export fn gpio_set_function(pin: u8, mode: u8) void { devices.gpio.fn_sel(pin, @enumFromInt(@as(u3, @truncate(mode)))) catch {}; } +export fn printk_0(a0: *u8) void { + const msg: [:0]u8 = @ptrCast(a0); + devices.mini_uart.print("{s}\n", .{msg}); +} +export fn printk_1(a0: *u8, a1: i32) void { + const msg: [:0]u8 = @ptrCast(a0); + devices.mini_uart.print("{s} {d}\n", .{ msg, a1 }); +} +export fn printk_2(a0: *u8, a1: i32, a2: i32) void { + const msg: [:0]u8 = @ptrCast(a0); + devices.mini_uart.print("{s} {d} {d}\n", .{ msg, a1, a2 }); +} +export fn printk_3(a0: *u8, a1: i32, a2: i32, a3: i32) void { + const msg: [:0]u8 = @ptrCast(a0); + devices.mini_uart.print("{s} {d} {d} {d}\n", .{ msg, a1, a2, a3 }); +} diff --git a/programs/fs.zig b/programs/fs.zig @@ -7,7 +7,6 @@ const uart = pi.devices.mini_uart; const Explorer = struct { alloc: std.mem.Allocator, - io_r: *std.Io.Reader, io_w: *std.Io.Writer, fs: vfs.FileSystem, @@ -16,7 +15,7 @@ const Explorer = struct { const Self = @This(); - pub fn init(alloc: std.mem.Allocator, fs: vfs.FileSystem, io_r: *std.Io.Reader, io_w: *std.Io.Writer) !Self { + pub fn init(alloc: std.mem.Allocator, fs: vfs.FileSystem, io_w: *std.Io.Writer) !Self { var path: std.ArrayList([]const u8) = try .initCapacity(alloc, 1); try path.append(alloc, "/"); @@ -25,25 +24,27 @@ const Explorer = struct { .fs = fs, .dir = try fs.open_root(), .path = path, - .io_r = io_r, .io_w = io_w, }; } - pub fn start(self: *Self) !void { - { - const path = try std.mem.concat(self.alloc, u8, self.path.items); - defer self.alloc.free(path); - try self.io_w.print("[{s}] $ ", .{path}); - } + pub fn read_line(self: *Self) !void { + const path = try std.mem.concat(self.alloc, u8, self.path.items); + defer self.alloc.free(path); + try self.io_w.print("[{s}] $ ", .{path}); + + var line: std.ArrayList(u8) = try .initCapacity(self.alloc, 16); + defer line.deinit(self.alloc); - while (try self.io_r.takeDelimiter('\n')) |line| { - try self.parse_line(line); + // Wait until \n + while (true) { + const byte = uart.read_byte_sync(); + if (byte == '\n') break; - const path = try std.mem.concat(self.alloc, u8, self.path.items); - defer self.alloc.free(path); - try self.io_w.print("[{s}] $ ", .{path}); + try line.append(self.alloc, byte); } + + try self.parse_line(line.items); } // I would like to apologize for this truly bad parser @@ -65,6 +66,8 @@ const Explorer = struct { try self.cat(entry); } else if (std.mem.eql(u8, cmd, "help")) { try self.io_w.print("Commands: ls, tree, cd, stat, cat, help\n", .{}); + } else if (std.mem.eql(u8, cmd, "exit")) { + pi.reboot(); } else { try self.io_w.print("\"{s}\": Command not found\n", .{cmd}); } @@ -261,7 +264,7 @@ pub fn main() !void { var fba: std.heap.FixedBufferAllocator = .init(&buffer); const alloc = fba.allocator(); - var sd_card = sd.init(alloc); + var sd_card = try sd.init(alloc); var fat_vfs: pi.fs.FatVFS = .init(); var vfs_disk = vfs.Disk.impl_by(&sd_card); @@ -274,7 +277,9 @@ pub fn main() !void { try vfs_fat.open(alloc, &vfs_disk, mbr.pte1.first_lba); defer vfs_fat.close(); - var explorer = try Explorer.init(alloc, vfs_fat, &uart.reader, &uart.writer); + var explorer = try Explorer.init(alloc, vfs_fat, &uart.writer); - try explorer.start(); + while (true) { + try explorer.read_line(); + } }