Skip to content

Commit

Permalink
Improves demo to be standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix "xq" Queißner committed Jun 16, 2024
1 parent 34f1c70 commit 629ec4f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 82 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ jobs:
with:
version: 0.13.0

- name: Basic Build
- name: Build Demo
run: |
zig build
zig build -Dmkfs install demo
- name: Exercise Build options
run: |
Expand Down
10 changes: 7 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ fn bad_config(comptime fmt: []const u8, args: anytype) noreturn {
}

pub fn build(b: *std.Build) void {
// targets:

const demo_step = b.step("demo", "Builds the demo:");

// options:

const target = b.standardTargetOptions(.{});
Expand Down Expand Up @@ -166,16 +170,16 @@ pub fn build(b: *std.Build) void {
// usage demo:

const exe = b.addExecutable(.{
.name = "zfat",
.name = "zfat-demo",
.root_source_file = b.path("demo/main.zig"),
.target = target,
.optimize = optimize,
});

exe.root_module.addImport("zfat", zfat_mod);

// exe.linkLibC();
b.installArtifact(exe);
const demo_exe = b.addInstallArtifact(exe, .{});
demo_step.dependOn(&demo_exe.step);

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
Expand Down
8 changes: 0 additions & 8 deletions demo/README.md

This file was deleted.

Binary file removed demo/blink-fast.uf2
Binary file not shown.
Binary file removed demo/blink-slow.uf2
Binary file not shown.
100 changes: 33 additions & 67 deletions demo/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,31 @@ const fatfs = @import("zfat");
var global_fs: fatfs.FileSystem = undefined;

// requires pointer stability
var image_disk: Disk = .{};
var image_disk: Disk = undefined;

pub fn main() !u8 {
const args = try std.process.argsAlloc(std.heap.page_allocator);
defer std.process.argsFree(std.heap.page_allocator, args);

if (args.len != 2) {
std.log.err("requires source file!", .{});
return 1;
}

var source_file = std.fs.cwd().openFile(args[1], .{}) catch |e| {
std.log.err("failed to open firmware {s}: {s}", .{ args[1], @errorName(e) });
return 1;
};
defer source_file.close();

image_disk.open("/dev/sdb1") catch |e| {
std.log.err("failed to open disk /dev/sdb1: {s}", .{@errorName(e)});
return 1;
image_disk = Disk{
.sectors = try std.heap.page_allocator.alloc([Disk.sector_size]u8, 100_000), // ~40 MB
};
defer image_disk.close();
defer std.heap.page_allocator.free(image_disk.sectors);

fatfs.disks[0] = &image_disk.interface;

var workspace: [4096]u8 = undefined;
try fatfs.mkfs("0:", .{
.filesystem = .fat32,
.sector_align = 1,
.use_partitions = true,
}, &workspace);

try global_fs.mount("0:", true);
defer fatfs.FileSystem.unmount("0:") catch |e| std.log.err("failed to unmount filesystem: {s}", .{@errorName(e)});

{
var fifo = std.fifo.LinearFifo(u8, .{ .Static = 8192 }).init();

var file = try fatfs.File.create("0:/firmware.uf2");
defer file.close();

try fifo.pump(source_file.reader(), file.writer());
try file.writer().writeAll("Hello, World!\r\n");
}

return 0;
Expand All @@ -56,79 +46,55 @@ pub const Disk = struct {
.writeFn = write,
.ioctlFn = ioctl,
},
backing_file: ?std.fs.File = null,

fn open(self: *Disk, path: []const u8) !void {
self.close();
self.backing_file = try std.fs.cwd().openFile(path, .{ .mode = .read_write });
}

fn close(self: *Disk) void {
if (self.backing_file) |*file| {
file.close();
self.backing_file = null;
}
}
sectors: [][sector_size]u8,

pub fn getStatus(interface: *fatfs.Disk) fatfs.Disk.Status {
const self: *Disk = @fieldParentPtr("interface", interface);
_ = self;
return fatfs.Disk.Status{
.initialized = (self.backing_file != null),
.disk_present = (self.backing_file != null),
.initialized = true,
.disk_present = true,
.write_protected = false,
};
}

pub fn initialize(interface: *fatfs.Disk) fatfs.Disk.Error!fatfs.Disk.Status {
const self: *Disk = @fieldParentPtr("interface", interface);
if (self.backing_file != null) {
return fatfs.Disk.Status{
.initialized = true,
.disk_present = true,
.write_protected = false,
};
}
self.backing_file = std.fs.cwd().openFile("disk.img", .{ .mode = .read_write }) catch return error.DiskNotReady;
return fatfs.Disk.Status{
.initialized = (self.backing_file != null),
.disk_present = (self.backing_file != null),
.write_protected = false,
};
return getStatus(&self.interface);
}

pub fn read(interface: *fatfs.Disk, buff: [*]u8, sector: fatfs.LBA, count: c_uint) fatfs.Disk.Error!void {
const self: *Disk = @fieldParentPtr("interface", interface);

std.log.info("read({*}, {}, {})", .{ buff, sector, count });

var file = self.backing_file orelse return error.IoError;
file.seekTo(sector * sector_size) catch return error.IoError;
file.reader().readNoEof(buff[0 .. sector_size * count]) catch return error.IoError;
var sectors = std.io.fixedBufferStream(std.mem.sliceAsBytes(self.sectors));
sectors.seekTo(sector * sector_size) catch return error.IoError;
sectors.reader().readNoEof(buff[0 .. sector_size * count]) catch return error.IoError;
}

pub fn write(interface: *fatfs.Disk, buff: [*]const u8, sector: fatfs.LBA, count: c_uint) fatfs.Disk.Error!void {
const self: *Disk = @fieldParentPtr("interface", interface);

std.log.info("write({*}, {}, {})", .{ buff, sector, count });

var file = self.backing_file orelse return error.IoError;
file.seekTo(sector * sector_size) catch return error.IoError;
file.writer().writeAll(buff[0 .. sector_size * count]) catch return error.IoError;
var sectors = std.io.fixedBufferStream(std.mem.sliceAsBytes(self.sectors));
sectors.seekTo(sector * sector_size) catch return error.IoError;
sectors.writer().writeAll(buff[0 .. sector_size * count]) catch return error.IoError;
}

pub fn ioctl(interface: *fatfs.Disk, cmd: fatfs.IoCtl, buff: [*]u8) fatfs.Disk.Error!void {
const self: *Disk = @fieldParentPtr("interface", interface);
if (self.backing_file) |file| {
_ = buff;
switch (cmd) {
.sync => {
file.sync() catch return error.IoError;
},

else => return error.InvalidParameter,
}
} else {
return error.DiskNotReady;

switch (cmd) {
.sync => {},
.get_sector_count => {
@as(*align(1) fatfs.LBA, @ptrCast(buff)).* = @intCast(self.sectors.len);
},
else => {
std.log.err("invalid ioctl: {}", .{cmd});
return error.InvalidParameter;
},
}
}
};
4 changes: 2 additions & 2 deletions src/fatfs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ fn ErrorSet(comptime options: []const anyerror) type {
},
});

pub fn throw(error_code: c.FRESULT) Error!void {
pub inline fn throw(error_code: c.FRESULT) Error!void {
const mapped_error = if (mapGenericError(error_code)) |_| {
return;
} else |err| err;
Expand All @@ -728,7 +728,7 @@ fn ErrorSet(comptime options: []const anyerror) type {
};
}

pub fn mapGenericError(code: c.FRESULT) GlobalError!void {
pub inline fn mapGenericError(code: c.FRESULT) GlobalError!void {
return switch (code) {
c.FR_OK => {},
c.FR_DISK_ERR => error.DiskErr,
Expand Down

0 comments on commit 629ec4f

Please sign in to comment.