initramfs -> bootloader, kernel can now be loaded, FAT can now be looked

at throug reference implementation
This commit is contained in:
Dario48 2025-07-23 17:24:23 +02:00
parent c7ec508dba
commit e1926aec8a
5 changed files with 495 additions and 316 deletions

View file

@ -1,12 +1,13 @@
const std = @import("std");
const LazyPath = std.Build.LazyPath;
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
const initramfs = addNasmFiles(b, AddNasmFilesOptions{
.filename = "src/initramfs.asm",
.outputname = "initramfs.bin",
const bootloader = addNasmFiles(b, AddNasmFilesOptions{
.filename = "src/bootloader.asm",
.outputname = "bootloader.bin",
.flags = &.{"-f bin"},
});
const kernel = addNasmFiles(b, AddNasmFilesOptions{
@ -15,16 +16,16 @@ pub fn build(b: *std.Build) void {
.flags = &.{"-f bin"},
});
var floppy = createFloppy(b, "main_floppy.img");
floppy.run = formatFloppyFat12(b, floppy);
const floppy = createFloppy(b, "main_floppy.img");
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" },
var installToFloppy = installImgToFloppy(b, floppy, bootloader, &.{
NamedFile{ .file = kernel, .name = "::kernel.bin" },
NamedFile{ .file = b.path("src/reference_implementations/test.txt"), .name = "::test.txt" },
});
const img_install = b.addInstallBinFile(floppy.obj, "main_floppy.img");
img_install.step.dependOn(&installToFloppy);
const img_install = b.addInstallBinFile(floppy, "main_floppy.img");
img_install.step.dependOn(&installToFloppy.step);
b.getInstallStep().dependOn(&img_install.step);
@ -35,8 +36,7 @@ pub fn build(b: *std.Build) void {
.link_libc = true,
});
const run = b.addSystemCommand(&.{ "qemu-system-i386", "-fda" });
run.addFileArg(floppy.obj);
const run = b.addSystemCommand(&.{ "qemu-system-i386", "-fda", "zig-out/bin/main_floppy.img" });
const run_step = b.step("run", "run the os in qemu");
run_step.dependOn(&run.step);
@ -60,66 +60,45 @@ const AddNasmFilesOptions = struct {
flags: []const []const u8 = &.{},
};
const NasmFile = struct {
run: *std.Build.Step.Run,
obj: std.Build.LazyPath,
};
const Floppydisk = NasmFile;
const NamedFile = struct {
file: union(enum) { runFile: NasmFile, simpleFile: std.Build.LazyPath },
file: std.Build.LazyPath,
name: []const u8,
};
fn createFloppy(b: *std.Build, name: []const u8) Floppydisk {
fn createFloppy(b: *std.Build, name: []const u8) LazyPath {
const dd = b.addSystemCommand(&.{ "dd", "if=/dev/zero" });
const Floppy = dd.addPrefixedOutputFileArg("of=", name);
dd.has_side_effects = true;
dd.addArgs(&.{ "bs=512", "count=2880" });
return Floppydisk{
.run = dd,
.obj = Floppy,
};
return Floppy;
}
fn formatFloppyFat12(b: *std.Build, floppy: Floppydisk) *std.Build.Step.Run {
fn formatFloppyFat12(b: *std.Build, floppy: LazyPath) void {
const mkfs = b.addSystemCommand(&.{ "mkfs.fat", "-F12", "-nNBOS" });
mkfs.addFileArg(floppy.obj);
mkfs.step.dependOn(&floppy.run.step);
return mkfs;
mkfs.addFileArg(floppy);
}
fn installImgToFloppy(b: *std.Build, floppy: Floppydisk, bootloader: NasmFile, additional_files: []const NamedFile) std.Build.Step {
fn installImgToFloppy(b: *std.Build, floppy: LazyPath, bootloader: LazyPath, additional_files: []const NamedFile) *std.Build.Step.Run {
const dd = b.addSystemCommand(&.{"dd"});
dd.addPrefixedFileArg("if=", bootloader.obj);
dd.addPrefixedFileArg("of=", floppy.obj);
dd.addPrefixedFileArg("if=", bootloader);
dd.addPrefixedFileArg("of=", floppy);
dd.addArg("conv=notrunc");
dd.step.dependOn(&floppy.run.step);
dd.step.dependOn(&bootloader.run.step);
var mcopy_step = std.Build.Step.init(.{ .name = "mcopy", .owner = b, .id = .custom });
mcopy_step.dependOn(&dd.step);
var last_step = dd;
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.addPrefixedFileArg("-i", floppy);
mcopy.addFileArg(f.file);
mcopy.addArg(f.name);
mcopy_step.dependOn(&mcopy.step);
mcopy.step.dependOn(&last_step.step);
last_step = mcopy;
}
return mcopy_step;
return last_step;
}
// adapted from https://codeberg.org/raddari/zig-nasm-lib.git
fn addNasmFiles(b: *std.Build, options: AddNasmFilesOptions) NasmFile {
fn addNasmFiles(b: *std.Build, options: AddNasmFilesOptions) LazyPath {
std.debug.assert(!std.fs.path.isAbsolute(options.filename));
const src_file = b.path(options.filename);
const output = options.outputname orelse b.fmt("{s}.o", .{std.mem.sliceTo(options.filename, '.')});
@ -129,9 +108,7 @@ fn addNasmFiles(b: *std.Build, options: AddNasmFilesOptions) NasmFile {
nasm.addPrefixedDirectoryArg("-i", b.path("src"));
const obj = nasm.addPrefixedOutputFileArg("-o", output);
nasm.addFileArg(src_file);
nasm.has_side_effects = true;
return .{
.run = nasm,
.obj = obj,
};
return obj;
}