Skip to content

Commit 61cb3b7

Browse files
committed
Initial Commit
0 parents  commit 61cb3b7

40 files changed

+2924
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/kernel-deps
2+
/limine
3+
/ovmf
4+
*.iso
5+
*.hdd

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"files.associations": {
3+
"flanterm.h": "c",
4+
"fb.h": "c"
5+
}
6+
}

GNUmakefile

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
# Nuke built-in rules and variables.
2+
MAKEFLAGS += -rR
3+
.SUFFIXES:
4+
5+
# Convenience macro to reliably declare user overridable variables.
6+
override USER_VARIABLE = $(if $(filter $(origin $(1)),default undefined),$(eval override $(1) := $(2)))
7+
8+
# Target architecture to build for. Default to x86_64.
9+
$(call USER_VARIABLE,KARCH,x86_64)
10+
11+
# Default user QEMU flags. These are appended to the QEMU command calls.
12+
$(call USER_VARIABLE,QEMUFLAGS,-m 2G -no-reboot -no-shutdown -enable-kvm)
13+
14+
override IMAGE_NAME := template-$(KARCH)
15+
16+
.PHONY: all
17+
all: $(IMAGE_NAME).iso
18+
19+
.PHONY: all-hdd
20+
all-hdd: $(IMAGE_NAME).hdd
21+
22+
.PHONY: run
23+
run: run-bios
24+
25+
.PHONY: run-hdd
26+
run-hdd: run-hdd-$(KARCH)
27+
28+
.PHONY: run-x86_64
29+
run-x86_64: ovmf/ovmf-code-$(KARCH).fd ovmf/ovmf-vars-$(KARCH).fd $(IMAGE_NAME).iso
30+
qemu-system-$(KARCH) \
31+
-M q35 \
32+
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-$(KARCH).fd,readonly=on \
33+
-drive if=pflash,unit=1,format=raw,file=ovmf/ovmf-vars-$(KARCH).fd \
34+
-cdrom $(IMAGE_NAME).iso \
35+
$(QEMUFLAGS)
36+
37+
.PHONY: run-hdd-x86_64
38+
run-hdd-x86_64: ovmf/ovmf-code-$(KARCH).fd ovmf/ovmf-vars-$(KARCH).fd $(IMAGE_NAME).hdd
39+
qemu-system-$(KARCH) \
40+
-M q35 \
41+
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-$(KARCH).fd,readonly=on \
42+
-drive if=pflash,unit=1,format=raw,file=ovmf/ovmf-vars-$(KARCH).fd \
43+
-hda $(IMAGE_NAME).hdd \
44+
$(QEMUFLAGS)
45+
46+
.PHONY: run-aarch64
47+
run-aarch64: ovmf/ovmf-code-$(KARCH).fd ovmf/ovmf-vars-$(KARCH).fd $(IMAGE_NAME).iso
48+
qemu-system-$(KARCH) \
49+
-M virt \
50+
-cpu cortex-a72 \
51+
-device ramfb \
52+
-device qemu-xhci \
53+
-device usb-kbd \
54+
-device usb-mouse \
55+
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-$(KARCH).fd,readonly=on \
56+
-drive if=pflash,unit=1,format=raw,file=ovmf/ovmf-vars-$(KARCH).fd \
57+
-cdrom $(IMAGE_NAME).iso \
58+
$(QEMUFLAGS)
59+
60+
.PHONY: run-hdd-aarch64
61+
run-hdd-aarch64: ovmf/ovmf-code-$(KARCH).fd ovmf/ovmf-vars-$(KARCH).fd $(IMAGE_NAME).hdd
62+
qemu-system-$(KARCH) \
63+
-M virt \
64+
-cpu cortex-a72 \
65+
-device ramfb \
66+
-device qemu-xhci \
67+
-device usb-kbd \
68+
-device usb-mouse \
69+
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-$(KARCH).fd,readonly=on \
70+
-drive if=pflash,unit=1,format=raw,file=ovmf/ovmf-vars-$(KARCH).fd \
71+
-hda $(IMAGE_NAME).hdd \
72+
$(QEMUFLAGS)
73+
74+
.PHONY: run-riscv64
75+
run-riscv64: ovmf/ovmf-code-$(KARCH).fd ovmf/ovmf-vars-$(KARCH).fd $(IMAGE_NAME).iso
76+
qemu-system-$(KARCH) \
77+
-M virt \
78+
-cpu rv64 \
79+
-device ramfb \
80+
-device qemu-xhci \
81+
-device usb-kbd \
82+
-device usb-mouse \
83+
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-$(KARCH).fd,readonly=on \
84+
-drive if=pflash,unit=1,format=raw,file=ovmf/ovmf-vars-$(KARCH).fd \
85+
-cdrom $(IMAGE_NAME).iso \
86+
$(QEMUFLAGS)
87+
88+
.PHONY: run-hdd-riscv64
89+
run-hdd-riscv64: ovmf/ovmf-code-$(KARCH).fd ovmf/ovmf-vars-$(KARCH).fd $(IMAGE_NAME).hdd
90+
qemu-system-$(KARCH) \
91+
-M virt \
92+
-cpu rv64 \
93+
-device ramfb \
94+
-device qemu-xhci \
95+
-device usb-kbd \
96+
-device usb-mouse \
97+
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-$(KARCH).fd,readonly=on \
98+
-drive if=pflash,unit=1,format=raw,file=ovmf/ovmf-vars-$(KARCH).fd \
99+
-hda $(IMAGE_NAME).hdd \
100+
$(QEMUFLAGS)
101+
102+
.PHONY: run-loongarch64
103+
run-loongarch64: ovmf/ovmf-code-$(KARCH).fd ovmf/ovmf-vars-$(KARCH).fd $(IMAGE_NAME).iso
104+
qemu-system-$(KARCH) \
105+
-M virt \
106+
-cpu la464 \
107+
-device ramfb \
108+
-device qemu-xhci \
109+
-device usb-kbd \
110+
-device usb-mouse \
111+
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-$(KARCH).fd,readonly=on \
112+
-drive if=pflash,unit=1,format=raw,file=ovmf/ovmf-vars-$(KARCH).fd \
113+
-cdrom $(IMAGE_NAME).iso \
114+
$(QEMUFLAGS)
115+
116+
.PHONY: run-hdd-loongarch64
117+
run-hdd-loongarch64: ovmf/ovmf-code-$(KARCH).fd ovmf/ovmf-vars-$(KARCH).fd $(IMAGE_NAME).hdd
118+
qemu-system-$(KARCH) \
119+
-M virt \
120+
-cpu la464 \
121+
-device ramfb \
122+
-device qemu-xhci \
123+
-device usb-kbd \
124+
-device usb-mouse \
125+
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-$(KARCH).fd,readonly=on \
126+
-drive if=pflash,unit=1,format=raw,file=ovmf/ovmf-vars-$(KARCH).fd \
127+
-hda $(IMAGE_NAME).hdd \
128+
$(QEMUFLAGS)
129+
130+
131+
.PHONY: run-bios
132+
run-bios: $(IMAGE_NAME).iso
133+
qemu-system-$(KARCH) \
134+
-M q35 \
135+
-cdrom $(IMAGE_NAME).iso \
136+
-boot d \
137+
$(QEMUFLAGS)
138+
139+
.PHONY: run-hdd-bios
140+
run-hdd-bios: $(IMAGE_NAME).hdd
141+
qemu-system-$(KARCH) \
142+
-M q35 \
143+
-hda $(IMAGE_NAME).hdd \
144+
$(QEMUFLAGS)
145+
146+
ovmf/ovmf-code-$(KARCH).fd:
147+
mkdir -p ovmf
148+
curl -Lo $@ https://github.com/osdev0/edk2-ovmf-nightly/releases/latest/download/ovmf-code-$(KARCH).fd
149+
case "$(KARCH)" in \
150+
aarch64) dd if=/dev/zero of=$@ bs=1 count=0 seek=67108864 2>/dev/null;; \
151+
loongarch64) dd if=/dev/zero of=$@ bs=1 count=0 seek=5242880 2>/dev/null;; \
152+
riscv64) dd if=/dev/zero of=$@ bs=1 count=0 seek=33554432 2>/dev/null;; \
153+
esac
154+
155+
ovmf/ovmf-vars-$(KARCH).fd:
156+
mkdir -p ovmf
157+
curl -Lo $@ https://github.com/osdev0/edk2-ovmf-nightly/releases/latest/download/ovmf-vars-$(KARCH).fd
158+
case "$(KARCH)" in \
159+
aarch64) dd if=/dev/zero of=$@ bs=1 count=0 seek=67108864 2>/dev/null;; \
160+
loongarch64) dd if=/dev/zero of=$@ bs=1 count=0 seek=5242880 2>/dev/null;; \
161+
riscv64) dd if=/dev/zero of=$@ bs=1 count=0 seek=33554432 2>/dev/null;; \
162+
esac
163+
164+
limine/limine:
165+
rm -rf limine
166+
git clone https://github.com/limine-bootloader/limine.git --branch=v8.x-binary --depth=1
167+
$(MAKE) -C limine
168+
169+
kernel-deps:
170+
./kernel/get-deps
171+
touch kernel-deps
172+
173+
.PHONY: kernel
174+
kernel: kernel-deps
175+
$(MAKE) -C kernel
176+
177+
$(IMAGE_NAME).iso: limine/limine kernel
178+
rm -rf iso_root
179+
mkdir -p iso_root/boot
180+
cp -v kernel/bin-$(KARCH)/kernel iso_root/boot/
181+
mkdir -p iso_root/boot/limine
182+
cp -v limine.conf iso_root/boot/limine/
183+
mkdir -p iso_root/EFI/BOOT
184+
ifeq ($(KARCH),x86_64)
185+
cp -v limine/limine-bios.sys limine/limine-bios-cd.bin limine/limine-uefi-cd.bin iso_root/boot/limine/
186+
cp -v limine/BOOTX64.EFI iso_root/EFI/BOOT/
187+
cp -v limine/BOOTIA32.EFI iso_root/EFI/BOOT/
188+
xorriso -as mkisofs -b boot/limine/limine-bios-cd.bin \
189+
-no-emul-boot -boot-load-size 4 -boot-info-table \
190+
--efi-boot boot/limine/limine-uefi-cd.bin \
191+
-efi-boot-part --efi-boot-image --protective-msdos-label \
192+
iso_root -o $(IMAGE_NAME).iso
193+
./limine/limine bios-install $(IMAGE_NAME).iso
194+
endif
195+
ifeq ($(KARCH),aarch64)
196+
cp -v limine/limine-uefi-cd.bin iso_root/boot/limine/
197+
cp -v limine/BOOTAA64.EFI iso_root/EFI/BOOT/
198+
xorriso -as mkisofs \
199+
--efi-boot boot/limine/limine-uefi-cd.bin \
200+
-efi-boot-part --efi-boot-image --protective-msdos-label \
201+
iso_root -o $(IMAGE_NAME).iso
202+
endif
203+
ifeq ($(KARCH),riscv64)
204+
cp -v limine/limine-uefi-cd.bin iso_root/boot/limine/
205+
cp -v limine/BOOTRISCV64.EFI iso_root/EFI/BOOT/
206+
xorriso -as mkisofs \
207+
--efi-boot boot/limine/limine-uefi-cd.bin \
208+
-efi-boot-part --efi-boot-image --protective-msdos-label \
209+
iso_root -o $(IMAGE_NAME).iso
210+
endif
211+
ifeq ($(KARCH),loongarch64)
212+
cp -v limine/limine-uefi-cd.bin iso_root/boot/limine/
213+
cp -v limine/BOOTLOONGARCH64.EFI iso_root/EFI/BOOT/
214+
xorriso -as mkisofs \
215+
--efi-boot boot/limine/limine-uefi-cd.bin \
216+
-efi-boot-part --efi-boot-image --protective-msdos-label \
217+
iso_root -o $(IMAGE_NAME).iso
218+
endif
219+
rm -rf iso_root
220+
221+
$(IMAGE_NAME).hdd: limine/limine kernel
222+
rm -f $(IMAGE_NAME).hdd
223+
dd if=/dev/zero bs=1M count=0 seek=64 of=$(IMAGE_NAME).hdd
224+
sgdisk $(IMAGE_NAME).hdd -n 1:2048 -t 1:ef00
225+
ifeq ($(KARCH),x86_64)
226+
./limine/limine bios-install $(IMAGE_NAME).hdd
227+
endif
228+
mformat -i $(IMAGE_NAME).hdd@@1M
229+
mmd -i $(IMAGE_NAME).hdd@@1M ::/EFI ::/EFI/BOOT ::/boot ::/boot/limine
230+
mcopy -i $(IMAGE_NAME).hdd@@1M kernel/bin-$(KARCH)/kernel ::/boot
231+
mcopy -i $(IMAGE_NAME).hdd@@1M limine.conf ::/boot/limine
232+
ifeq ($(KARCH),x86_64)
233+
mcopy -i $(IMAGE_NAME).hdd@@1M limine/limine-bios.sys ::/boot/limine
234+
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTX64.EFI ::/EFI/BOOT
235+
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTIA32.EFI ::/EFI/BOOT
236+
endif
237+
ifeq ($(KARCH),aarch64)
238+
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTAA64.EFI ::/EFI/BOOT
239+
endif
240+
ifeq ($(KARCH),riscv64)
241+
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTRISCV64.EFI ::/EFI/BOOT
242+
endif
243+
ifeq ($(KARCH),loongarch64)
244+
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTLOONGARCH64.EFI ::/EFI/BOOT
245+
endif
246+
247+
.PHONY: clean
248+
clean:
249+
if test -f kernel-deps; then $(MAKE) -C kernel clean; fi
250+
rm -rf iso_root $(IMAGE_NAME).iso $(IMAGE_NAME).hdd
251+
252+
.PHONY: distclean
253+
distclean:
254+
if test -f kernel-deps; then $(MAKE) -C kernel distclean; fi
255+
rm -rf iso_root *.iso *.hdd kernel-deps limine ovmf

LICENSE

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Copyright (C) 2023-2024 mintsuki and contributors.
2+
3+
Permission to use, copy, modify, and/or distribute this software for any
4+
purpose with or without fee is hereby granted.
5+
6+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
7+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
8+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
9+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
10+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
11+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
12+
PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Limine C Template
2+
3+
This repository will demonstrate how to set up a basic kernel in C using Limine.
4+
5+
## How to use this?
6+
7+
### Dependencies
8+
9+
Any `make` command depends on GNU make (`gmake`) and is expected to be run using it. This usually means using `make` on most GNU/Linux distros, or `gmake` on other non-GNU systems.
10+
11+
It is recommended to build this project using a standard UNIX-like system, using a Clang/LLVM toolchain capable of cross compilation (the default, unless `KCC` and/or `KLD` are explicitly set).
12+
13+
Additionally, building an ISO with `make all` requires `xorriso`, and building a HDD/USB image with `make all-hdd` requires `sgdisk` (usually from `gdisk` or `gptfdisk` packages) and `mtools`.
14+
15+
### Architectural targets
16+
17+
The `KARCH` make variable determines the target architecture to build the kernel and image for.
18+
19+
The default `KARCH` is `x86_64`. Other options include: `aarch64`, `loongarch64`, and `riscv64`.
20+
21+
### Makefile targets
22+
23+
Running `make all` will compile the kernel (from the `kernel/` directory) and then generate a bootable ISO image.
24+
25+
Running `make all-hdd` will compile the kernel and then generate a raw image suitable to be flashed onto a USB stick or hard drive/SSD.
26+
27+
Running `make run` will build the kernel and a bootable ISO (equivalent to make all) and then run it using `qemu` (if installed).
28+
29+
Running `make run-hdd` will build the kernel and a raw HDD image (equivalent to make all-hdd) and then run it using `qemu` (if installed).
30+
31+
For x86_64, the `run-bios` and `run-hdd-bios` targets are equivalent to their non `-bios` counterparts except that they boot `qemu` using the default SeaBIOS firmware instead of OVMF.

kernel/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/freestanding-headers
2+
/src/cc-runtime.c
3+
/src/limine.h
4+
/bin-*
5+
/obj-*

0 commit comments

Comments
 (0)