adding reference impementation for reading files from fat

This commit is contained in:
Dario48 2025-07-17 04:37:30 +02:00
parent 2c44bea9b1
commit da1a2345a9
3 changed files with 167 additions and 10 deletions

View file

@ -18,16 +18,40 @@ pub fn build(b: *std.Build) void {
var floppy = createFloppy(b, "main_floppy.img");
floppy.run = formatFloppyFat12(b, floppy);
var installToFloppy = installImgToFloppy(b, floppy, initramfs, &.{
NamedFile{ .file = .{ .runFile = kernel }, .name = "::kernel.bin" },
NamedFile{ .file = .{ .simpleFile = b.path("src/reference_implementations/test.txt") }, .name = "::test.txt" },
});
const img_install = b.addInstallBinFile(floppy.obj, "main_floppy.img");
img_install.step.dependOn(&installImgToFloppy(b, floppy, initramfs, kernel).step);
img_install.step.dependOn(&installToFloppy);
b.getInstallStep().dependOn(&img_install.step);
const target = b.standardTargetOptions(.{});
const fat_reference_mod = b.createModule(.{
.root_source_file = b.path("src/reference_implementations/fat.zig"),
.target = target,
.link_libc = true,
});
const run = b.addSystemCommand(&.{ "qemu-system-i386", "-fda" });
run.addFileArg(floppy.obj);
const run_step = b.step("run", "run the os in qemu");
run_step.dependOn(&run.step);
const fat_reference = b.addExecutable(.{
.root_module = fat_reference_mod,
.name = "fat_reference_implementation",
.use_lld = false,
.use_llvm = false,
});
const fat_install = b.addInstallArtifact(fat_reference, .{});
const build_references = b.step("references", "build the reference implementations");
build_references.dependOn(&fat_install.step);
}
const AddNasmFilesOptions = struct {
@ -43,6 +67,11 @@ const NasmFile = struct {
const Floppydisk = NasmFile;
const NamedFile = struct {
file: union(enum) { runFile: NasmFile, simpleFile: std.Build.LazyPath },
name: []const u8,
};
fn createFloppy(b: *std.Build, name: []const u8) Floppydisk {
const dd = b.addSystemCommand(&.{ "dd", "if=/dev/zero" });
const Floppy = dd.addPrefixedOutputFileArg("of=", name);
@ -63,7 +92,7 @@ fn formatFloppyFat12(b: *std.Build, floppy: Floppydisk) *std.Build.Step.Run {
return mkfs;
}
fn installImgToFloppy(b: *std.Build, floppy: Floppydisk, bootloader: NasmFile, kernel: NasmFile) *std.Build.Step.Run {
fn installImgToFloppy(b: *std.Build, floppy: Floppydisk, bootloader: NasmFile, additional_files: []const NamedFile) std.Build.Step {
const dd = b.addSystemCommand(&.{"dd"});
dd.addPrefixedFileArg("if=", bootloader.obj);
dd.addPrefixedFileArg("of=", floppy.obj);
@ -71,14 +100,22 @@ fn installImgToFloppy(b: *std.Build, floppy: Floppydisk, bootloader: NasmFile, k
dd.step.dependOn(&floppy.run.step);
dd.step.dependOn(&bootloader.run.step);
const mcopy = b.addSystemCommand(&.{"mcopy"});
mcopy.addPrefixedFileArg("-i", floppy.obj);
mcopy.addFileArg(kernel.obj);
mcopy.addArg("::kernel.bin");
mcopy.step.dependOn(&dd.step);
mcopy.step.dependOn(&kernel.run.step);
return mcopy;
var mcopy_step = std.Build.Step.init(.{ .name = "mcopy", .owner = b, .id = .custom });
mcopy_step.dependOn(&dd.step);
for (additional_files) |f| {
const mcopy = b.addSystemCommand(&.{"mcopy"});
mcopy.addPrefixedFileArg("-i", floppy.obj);
switch (f.file) {
.runFile => |file| {
mcopy.addFileArg(file.obj);
mcopy_step.dependOn(&file.run.step);
},
.simpleFile => |file| mcopy.addFileArg(file),
}
mcopy.addArg(f.name);
mcopy_step.dependOn(&mcopy.step);
}
return mcopy_step;
}
// adapted from https://codeberg.org/raddari/zig-nasm-lib.git