-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.zig
167 lines (142 loc) · 5.57 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const std = @import("std");
const Builder = std.build.Builder;
const builtin = std.builtin;
const assert = std.debug.assert;
const sabaton = @import("boot/Sabaton/build.zig");
const flork = @import("subprojects/flork/build.zig");
// zig fmt: off
fn qemu_run_aarch64_sabaton(b: *Builder, board_name: []const u8, desc: []const u8) !void {
const sabaton_blob = sabaton.aarch64VirtBlob(b);
const sabaton_blob_path = b.getInstallPath(sabaton_blob.dest_dir, sabaton_blob.dest_filename);
const kernel_step = try flork.buildKernel(.{
.builder = b,
.arch = .aarch64,
});
const kernel_path = b.getInstallPath(kernel_step.install_step.?.dest_dir, kernel_step.out_filename);
const command_step = b.step(board_name, desc);
const params = &[_][]const u8 {
"qemu-system-aarch64",
"-M", board_name,
"-cpu", "cortex-a57",
"-drive", b.fmt("if=pflash,format=raw,file={s},readonly=on", .{sabaton_blob_path}),
"-fw_cfg", b.fmt("opt/Sabaton/kernel,file={s}", .{kernel_path}),
"-m", "4G",
"-serial", "stdio",
//"-S", "-s",
"-smp", "4",
"-device", "virtio-gpu-pci",
"-device", "ramfb",
};
const run_step = b.addSystemCommand(params);
run_step.step.dependOn(&sabaton_blob.step);
run_step.step.dependOn(&kernel_step.install_step.?.step);
command_step.dependOn(&run_step.step);
}
fn qemu_run_riscv_sabaton(b: *Builder, board_name: []const u8, desc: []const u8, dep: *std.build.LibExeObjStep) void {
const command_step = b.step(board_name, desc);
const kernel_path = b.getInstallPath(dep.install_step.?.dest_dir, dep.out_filename);
const params = &[_][]const u8{
"qemu-system-riscv64",
"-M", board_name,
"-cpu", "rv64",
"-drive", b.fmt("if=pflash,format=raw,file=Sabaton/out/riscv64_{s}.bin,readonly=on", .{board_name}),
"-drive", b.fmt("if=pflash,format=raw,file={s},readonly=on", .{kernel_path}),
"-m", "4G",
"-serial", "stdio",
//"-S", "-s",
"-d", "int",
"-smp", "4",
"-device", "virtio-gpu-pci",
};
const pad_step = b.addSystemCommand(
&[_][]const u8{
"truncate", "-s", "64M", kernel_path,
},
);
const run_step = b.addSystemCommand(params);
pad_step.step.dependOn(&dep.install_step.?.step);
run_step.step.dependOn(&pad_step.step);
command_step.dependOn(&run_step.step);
}
fn qemu_run_image_x86_64(b: *Builder, image_path: []const u8) *std.build.RunStep {
const run_params = &[_][]const u8{
"qemu-system-x86_64",
"-drive", b.fmt("format=raw,file={s}", .{image_path}),
"-debugcon", "stdio",
"-vga", "virtio",
//"-serial", "stdio",
"-m", "4G",
"-no-reboot",
"-no-shutdown",
"-machine", "q35,accel=kvm:whpx:tcg",
"-device", "qemu-xhci",
"-netdev", "user,id=mynet0",
"-device", "e1000,netdev=mynet0",
"-smp", "8",
//"-d", "int",
//"-s", "-S",
//"-trace", "ahci_*",
};
return b.addSystemCommand(run_params);
}
fn universal_x86_64_image(b: *Builder, image_path: []const u8, kernel_path: []const u8) *std.build.RunStep {
const image_dir = b.fmt("./{s}/universal_image/", .{b.cache_root});
const image_params = &[_][]const u8{
"/bin/sh", "-c",
std.mem.concat(b.allocator, u8, &[_][]const u8{
"make -C boot/limine-bin && ",
"mkdir -p ", image_dir, " && ",
"cp boot/stivale2_image/limine.cfg ",
"boot/limine-bin/limine.sys ", "boot/limine-bin/limine-cd.bin ",
"boot/limine-bin/limine-eltorito-efi.bin ",
image_dir, " && ",
"cp ", kernel_path, " ", image_dir, "/flork.elf && ",
"xorriso -as mkisofs -b limine-cd.bin ",
"-no-emul-boot -boot-load-size 4 -boot-info-table ",
"--efi-boot limine-eltorito-efi.bin ",
"-efi-boot-part --efi-boot-image --protective-msdos-label ",
image_dir, " -o ", image_path,
"&&",
"boot/limine-bin/limine-install ", image_path,
}) catch unreachable,
};
return b.addSystemCommand(image_params);
}
fn limine_target(b: *Builder, command: []const u8, desc: []const u8, image_path: []const u8, dep: *std.build.LibExeObjStep) void {
assert(dep.target.cpu_arch.? == .x86_64);
const command_step = b.step(command, desc);
const run_step = qemu_run_image_x86_64(b, image_path);
const kernel_path = b.getInstallPath(dep.install_step.?.dest_dir, dep.out_filename);
const image_step = universal_x86_64_image(b, image_path, kernel_path);
image_step.step.dependOn(&dep.install_step.?.step);
run_step.step.dependOn(&image_step.step);
command_step.dependOn(&run_step.step);
}
// zig fmt: on
pub fn build(b: *Builder) !void {
// try qemu_run_aarch64_sabaton(
// b,
// "raspi3",
// "(WIP) Run aarch64 kernel with Sabaton stivale2 on the raspi3 board",
// );
try qemu_run_aarch64_sabaton(
b,
"virt",
"Run aarch64 kernel with Sabaton stivale2 on the virt board",
);
// qemu_run_riscv_sabaton(b,
// "riscv-virt",
// "(WIP) Run risc-v kernel with Sabaton stivale2 on the virt board",
// build_kernel(b, builtin.Arch.riscv64, "stivale2"),
// );
limine_target(
b,
"x86_64-stivale2",
"Run x86_64 kernel with limine stivale2",
b.fmt("{s}/stivale2.img", .{b.cache_root}),
try flork.buildKernel(.{
.builder = b,
.arch = .x86_64,
}),
);
}