Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 33c94bb

Browse files
committed
Setup workspace
1 parent 4cbee4e commit 33c94bb

24 files changed

+408
-73
lines changed

.cargo/config.toml

-5
This file was deleted.

.gitignore

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
/build
2-
/target
1+
/target/
2+
.idea/
3+
build/
4+
qemu.log
5+
.gdb_history
6+
output.txt
7+
rustfmt.toml

.idea/.gitignore

-7
This file was deleted.

.idea/modules.xml

-8
This file was deleted.

.idea/os.iml

-12
This file was deleted.

.idea/vcs.xml

-7
This file was deleted.

Cargo.lock

+18-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
[package]
2-
name = "os"
3-
version = "0.1.0"
4-
edition = "2021"
1+
[workspace]
52

6-
[dependencies]
7-
log = "0.4.22"
8-
uefi = { version = "0.29.0", features = ["logger", "panic_handler"] }
3+
members = [
4+
"chicken-kernel",
5+
"chicken-loader",
6+
"chicken-util",
7+
]
8+
resolver = "2"

Makefile

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
BOOTLOADER_DIR = chicken-loader
2+
KERNEL_DIR = chicken-kernel
3+
UTIL_DIR = chicken-util
4+
5+
TARGET_DIR_BOOTLOADER_DEBUG = target/x86_64-unknown-uefi/debug
6+
TARGET_DIR_BOOTLOADER_RELEASE = target/x86_64-unknown-uefi/release
7+
8+
TARGET_DIR_KERNEL_DEBUG = target/x86_64-unknown-none-chicken/debug
9+
TARGET_DIR_KERNEL_RELEASE = target/x86_64-unknown-none-chicken/release
10+
11+
EFI_FILE = chicken-loader.efi
12+
KERNEL_FILE = chicken-kernel.elf
13+
14+
BUILD_DIR = build
15+
ESP_DIR = $(BUILD_DIR)/esp
16+
BOOT_DIR = $(ESP_DIR)/efi/boot
17+
18+
QEMU_LOG = qemu.log
19+
20+
OVMF_DIR = /usr/share/OVMF/x64
21+
OVMF_CODE = $(OVMF_DIR)/OVMF_CODE.fd
22+
OVMF_VARS = $(OVMF_DIR)/OVMF_VARS.fd
23+
24+
USB_DEVICE = /dev/zero
25+
26+
ifdef release
27+
CARGO_CMD = cargo build --release --target-dir=../target
28+
TARGET_DIR_BOOTLOADER = $(TARGET_DIR_BOOTLOADER_RELEASE)
29+
TARGET_DIR_KERNEL = $(TARGET_DIR_KERNEL_RELEASE)
30+
else
31+
CARGO_CMD = cargo build --target-dir=../target
32+
TARGET_DIR_BOOTLOADER = $(TARGET_DIR_BOOTLOADER_DEBUG)
33+
TARGET_DIR_KERNEL = $(TARGET_DIR_KERNEL_DEBUG)
34+
endif
35+
36+
.PHONY: all
37+
all: bootloader kernel
38+
@echo "Build complete."
39+
40+
.PHONY: bootloader
41+
bootloader:
42+
@echo "Building bootloader..."
43+
@cd $(BOOTLOADER_DIR) && $(CARGO_CMD)
44+
45+
.PHONY: kernel
46+
kernel:
47+
@echo "Building kernel..."
48+
@cd $(KERNEL_DIR) && $(CARGO_CMD)
49+
50+
.PHONY: clippy
51+
clippy:
52+
@echo "Running clippy..."
53+
@cd $(BOOTLOADER_DIR) && cargo clippy --target-dir=../target
54+
@cd $(KERNEL_DIR) && cargo clippy --target-dir=../target
55+
@cd $(UTIL_DIR) && cargo clippy --target-dir=../target
56+
57+
.PHONY: clean
58+
clean:
59+
@echo "Cleaning target directory..."
60+
@cargo clean
61+
@echo "Cleaning build directory..."
62+
@rm -rf $(BUILD_DIR)
63+
@echo "Clean complete."
64+
65+
.PHONY: run
66+
run: all
67+
@echo "Creating build directory..."
68+
@mkdir -p $(BOOT_DIR)
69+
@echo "Copying UEFI file to boot directory..."
70+
@cp $(TARGET_DIR_BOOTLOADER)/$(EFI_FILE) $(BOOT_DIR)/bootx64.efi
71+
@echo "Copying kernel file to boot directory..."
72+
@cp $(TARGET_DIR_KERNEL)/$(KERNEL_FILE) $(ESP_DIR)/kernel.elf
73+
@echo "Running QEMU..."
74+
@qemu-system-x86_64 -enable-kvm \
75+
-drive if=pflash,format=raw,readonly=on,file=$(OVMF_CODE) \
76+
-drive if=pflash,format=raw,readonly=on,file=$(OVMF_VARS) \
77+
-drive format=raw,file=fat:rw:$(ESP_DIR) \
78+
-d int -D $(QEMU_LOG) -no-reboot -serial stdio -m 256M
79+
80+
.PHONY: usb
81+
usb: all
82+
@echo "Formatting USB drive..."
83+
@sudo parted $(USB_DEVICE) -- mklabel gpt
84+
@sudo parted $(USB_DEVICE) -- mkpart ESP fat32 1MiB 100%
85+
@sudo parted $(USB_DEVICE) -- set 1 esp on
86+
@sudo mkfs.fat -F32 $(USB_DEVICE)1
87+
@echo "Mounting USB drive..."
88+
@sudo mount $(USB_DEVICE)1 /mnt
89+
@echo "Creating EFI boot directory on USB drive..."
90+
@sudo mkdir -p /mnt/efi/boot
91+
@echo "Copying UEFI file to USB drive..."
92+
@sudo cp $(TARGET_DIR_BOOTLOADER)/$(EFI_FILE) /mnt/efi/boot/bootx64.efi
93+
@echo "Copying kernel file to USB drive..."
94+
@sudo cp $(TARGET_DIR_KERNEL)/$(KERNEL_FILE) /mnt/kernel.elf
95+
@echo "Unmounting USB drive..."
96+
@sudo umount /mnt
97+
@echo "USB drive is ready to boot."
98+

chicken-kernel/.cargo/config.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[unstable]
2+
build-std = ["core", "compiler_builtins"]
3+
build-std-features = ["compiler-builtins-mem"]
4+
5+
[build]
6+
target = "./x86_64-unknown-none-chicken.json"

chicken-kernel/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "chicken-kernel"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]

chicken-kernel/boot.s

Whitespace-only changes.

chicken-kernel/build.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::{env, path::PathBuf, process::Command};
2+
3+
fn main() {
4+
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
5+
6+
let out_dir = manifest_dir.join("../target");
7+
println!("cargo:rustc-link-search={}", out_dir.display());
8+
9+
// boot.s -> boot.o -> libboot.a
10+
let out_boot_asm = {
11+
let mut path = out_dir.clone();
12+
path.push("boot.o");
13+
path
14+
};
15+
Command::new("nasm")
16+
.args(["-f", "elf64", "-o", out_boot_asm.to_str().unwrap(), "boot.s"])
17+
.status()
18+
.unwrap();
19+
Command::new("ar")
20+
.args(["crUs", "libboot.a", "boot.o"])
21+
.current_dir(&out_dir)
22+
.status()
23+
.unwrap();
24+
println!("cargo:rustc-link-lib=static=boot");
25+
println!("cargo:rustc-link-search={}", out_dir.display());
26+
}

chicken-kernel/linker.ld

Whitespace-only changes.

chicken-kernel/src/main.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use core::panic::PanicInfo;
5+
6+
fn kernel_main() -> ! {
7+
loop {}
8+
}
9+
10+
#[panic_handler]
11+
fn panic(_info: &PanicInfo) -> ! {
12+
loop {}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"arch": "x86_64",
3+
"code-model": "kernel",
4+
"cpu": "x86-64",
5+
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
6+
"disable-redzone": true,
7+
"executables": true,
8+
"exe-suffix": ".elf",
9+
"features": "-mmx,-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-3dnow,-3dnowa,-avx,-avx2,+soft-float",
10+
"linker-flavor": "ld.lld",
11+
"llvm-target": "x86_64-unknown-none-elf",
12+
"max-atomic-width": 64,
13+
"os": "none",
14+
"panic-strategy": "abort",
15+
"relro-level": "off",
16+
"position-independent-executables": false,
17+
"pre-link-args": {
18+
"ld.lld": [
19+
"-T./chicken-kernel/linker.ld"
20+
]
21+
},
22+
"post-link-args": {
23+
"ld.lld": [
24+
"--static"
25+
]
26+
},
27+
"target-pointer-width": "64"
28+
}

chicken-loader/.cargo/config.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[unstable]
2+
build-std = ["core", "compiler_builtins"]
3+
build-std-features = ["compiler-builtins-mem"]
4+
5+
[build]
6+
target = "x86_64-unknown-uefi"

0 commit comments

Comments
 (0)