-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
111 lines (98 loc) · 3.1 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = std.zig.CrossTarget{
.cpu_arch = .x86,
.os_tag = .freestanding,
.abi = .none,
.cpu_model = .{ .explicit = &std.Target.x86.cpu.i386 },
};
const create_image = b.addSystemCommand(&[_][]const u8{
"dd",
"if=/dev/zero",
"of=./disk.img",
"bs=1M",
"count=20",
"status=none",
});
const format_disk = b.addSystemCommand(&[_][]const u8{
"mkfs.fat",
"-F",
"12",
"-n",
"BOOTDISK",
"./disk.img",
});
format_disk.step.dependOn(&create_image.step);
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "bootloader",
.root_source_file = .{ .path = "src/bootloader.zig" },
.target = target,
.optimize = optimize,
});
exe.setLinkerScriptPath(.{ .path = "linker.ld" });
exe.code_model = .small;
exe.red_zone = false;
exe.pie = false;
exe.force_pic = false;
const install_exe = b.addInstallArtifact(exe, .{});
// Convert the ELF to binary
const objcopy_cmd = b.addSystemCommand(&[_][]const u8{
"llvm-objcopy",
"-O",
"binary",
"--set-section-flags",
".bss=alloc,load,contents",
"--set-section-flags",
".data=alloc,load,contents",
"zig-out/bin/bootloader",
"zig-out/bin/bootloader.bin",
});
objcopy_cmd.step.dependOn(&install_exe.step);
// Pad the bootloader to exactly 512 bytes
const pad_bootloader = b.addSystemCommand(&[_][]const u8{
"dd",
"if=zig-out/bin/bootloader.bin",
"of=zig-out/bin/bootloader.img",
"bs=512",
"count=1",
"conv=sync",
"status=none",
});
pad_bootloader.step.dependOn(&objcopy_cmd.step);
// Write the padded bootloader to the disk image
const write_bootloader = b.addSystemCommand(&[_][]const u8{
"dd",
"if=zig-out/bin/bootloader.img",
"of=./disk.img",
"bs=512",
"count=1",
"conv=notrunc",
"status=none",
});
write_bootloader.step.dependOn(&pad_bootloader.step);
write_bootloader.step.dependOn(&create_image.step);
write_bootloader.step.dependOn(&format_disk.step);
// Make the disk image readable
const chmod_cmd = b.addSystemCommand(&[_][]const u8{
"chmod",
"644",
"./disk.img",
});
chmod_cmd.step.dependOn(&write_bootloader.step);
const build_disk = b.step("disk", "Create bootable disk image");
build_disk.dependOn(&chmod_cmd.step);
// Add a run step that specifies raw format explicitly
const run_qemu = b.addSystemCommand(&[_][]const u8{
"qemu-system-i386",
"-drive",
"file=disk.img,format=raw,if=floppy",
"-drive",
"file=fat:rw:./test_files,format=raw,if=ide", // This will mount a directory as a disk
"-monitor",
"stdio",
});
run_qemu.step.dependOn(&chmod_cmd.step);
const run_step = b.step("run", "Run QEMU");
run_step.dependOn(&run_qemu.step);
}