Skip to content

Commit 9774b35

Browse files
add code
1 parent 406838f commit 9774b35

23 files changed

+1546
-1
lines changed

.bochsrc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Configuration file for Bochs
2+
3+
# CPU configuration
4+
cpu {
5+
type = "i486"
6+
mode = "protected"
7+
# Other CPU options...
8+
}
9+
10+
# Memory configuration
11+
memory {
12+
size = 32M
13+
# Other memory options...
14+
}
15+
16+
# Display configuration (VGA)
17+
display {
18+
dynamic_vga = true
19+
resolution = "1024x768"
20+
# Other display options...
21+
}
22+
23+
# CD-ROM drive configuration
24+
disk {
25+
type = "cdrom0"
26+
device = 0x1d00
27+
file = "[REPLACE_WITH_YOUR_ISO_PATH]"
28+
# Other disk options...
29+
}
30+
31+
# BIOS configuration (optional)
32+
bios {
33+
boot_device = "none" # Set to cdrom or harddisk if needed
34+
}
35+

.gitignore

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

GNUmakefile

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Nuke built-in rules and variables.
2+
MAKEFLAGS += -rR
3+
.SUFFIXES:
4+
5+
# Default user QEMU flags. These are appended to the QEMU command calls.
6+
QEMUFLAGS := -m 2G
7+
8+
override IMAGE_NAME := template
9+
10+
# Toolchain for building the 'limine' executable for the host.
11+
HOST_CC := cc
12+
HOST_CFLAGS := -g -O2 -pipe
13+
HOST_CPPFLAGS :=
14+
HOST_LDFLAGS :=
15+
HOST_LIBS :=
16+
17+
.PHONY: all
18+
all: $(IMAGE_NAME).iso
19+
20+
.PHONY: all-hdd
21+
all-hdd: $(IMAGE_NAME).hdd
22+
23+
.PHONY: run
24+
run: $(IMAGE_NAME).iso
25+
qemu-system-x86_64 \
26+
-M q35 \
27+
-cdrom $(IMAGE_NAME).iso \
28+
-boot d \
29+
-serial stdio \
30+
$(QEMUFLAGS)
31+
32+
.PHONY: run-monitor
33+
run-monitor: $(IMAGE_NAME).iso
34+
qemu-system-x86_64 \
35+
-M q35 \
36+
-cdrom $(IMAGE_NAME).iso \
37+
-boot d \
38+
-serial stdio \
39+
$(QEMUFLAGS)
40+
41+
.PHONY: debug
42+
debug: $(IMAGE_NAME).iso
43+
qemu-system-x86_64 \
44+
-M q35 \
45+
-cdrom $(IMAGE_NAME).iso \
46+
-boot d \
47+
-serial stdio \
48+
-gdb tcp::1234 \
49+
$(QEMUFLAGS)
50+
51+
.PHONY: run-uefi
52+
run-uefi: ovmf/ovmf-code-x86_64.fd $(IMAGE_NAME).iso
53+
qemu-system-x86_64 \
54+
-M q35 \
55+
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-x86_64.fd,readonly=on \
56+
-cdrom $(IMAGE_NAME).iso \
57+
-boot d \
58+
-serial stdio \
59+
$(QEMUFLAGS)
60+
61+
.PHONY: debug-uefi
62+
debug-uefi: ovmf/ovmf-code-x86_64.fd $(IMAGE_NAME).iso
63+
qemu-system-x86_64 \
64+
-M q35 \
65+
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-x86_64.fd,readonly=on \
66+
-cdrom $(IMAGE_NAME).iso \
67+
-boot d \
68+
-gdb tcp::1234 \
69+
-serial stdio \
70+
$(QEMUFLAGS)
71+
72+
.PHONY: run-hdd
73+
run-hdd: $(IMAGE_NAME).hdd
74+
qemu-system-x86_64 \
75+
-M q35 \
76+
-hda $(IMAGE_NAME).hdd \
77+
$(QEMUFLAGS)
78+
79+
.PHONY: run-hdd-uefi
80+
run-hdd-uefi: ovmf/ovmf-code-x86_64.fd $(IMAGE_NAME).hdd
81+
qemu-system-x86_64 \
82+
-M q35 \
83+
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-x86_64.fd,readonly=on \
84+
-hda $(IMAGE_NAME).hdd \
85+
$(QEMUFLAGS)
86+
87+
ovmf/ovmf-code-x86_64.fd:
88+
mkdir -p ovmf
89+
curl -Lo $@ https://github.com/osdev0/edk2-ovmf-nightly/releases/latest/download/ovmf-code-x86_64.fd
90+
91+
limine/limine:
92+
rm -rf limine
93+
git clone https://github.com/limine-bootloader/limine.git --branch=v9.x-binary --depth=1
94+
$(MAKE) -C limine \
95+
CC="$(HOST_CC)" \
96+
CFLAGS="$(HOST_CFLAGS)" \
97+
CPPFLAGS="$(HOST_CPPFLAGS)" \
98+
LDFLAGS="$(HOST_LDFLAGS)" \
99+
LIBS="$(HOST_LIBS)"
100+
101+
kernel-deps:
102+
./kernel/get-deps
103+
touch kernel-deps
104+
105+
.PHONY: kernel
106+
kernel: kernel-deps
107+
$(MAKE) -C kernel
108+
109+
$(IMAGE_NAME).iso: limine/limine kernel
110+
rm -rf iso_root
111+
mkdir -p iso_root/boot
112+
cp -v kernel/bin/kernel iso_root/boot/
113+
mkdir -p iso_root/boot/limine
114+
cp -v limine.conf limine/limine-bios.sys limine/limine-bios-cd.bin limine/limine-uefi-cd.bin iso_root/boot/limine/
115+
mkdir -p iso_root/EFI/BOOT
116+
cp -v limine/BOOTX64.EFI iso_root/EFI/BOOT/
117+
cp -v limine/BOOTIA32.EFI iso_root/EFI/BOOT/
118+
xorriso -as mkisofs -R -r -J -b boot/limine/limine-bios-cd.bin \
119+
-no-emul-boot -boot-load-size 4 -boot-info-table -hfsplus \
120+
-apm-block-size 2048 --efi-boot boot/limine/limine-uefi-cd.bin \
121+
-efi-boot-part --efi-boot-image --protective-msdos-label \
122+
iso_root -o $(IMAGE_NAME).iso
123+
./limine/limine bios-install $(IMAGE_NAME).iso
124+
rm -rf iso_root
125+
126+
$(IMAGE_NAME).hdd: limine/limine kernel
127+
rm -f $(IMAGE_NAME).hdd
128+
dd if=/dev/zero bs=1M count=0 seek=64 of=$(IMAGE_NAME).hdd
129+
PATH=$$PATH:/usr/sbin:/sbin sgdisk $(IMAGE_NAME).hdd -n 1:2048 -t 1:ef00 -m 1
130+
./limine/limine bios-install $(IMAGE_NAME).hdd
131+
mformat -i $(IMAGE_NAME).hdd@@1M
132+
mmd -i $(IMAGE_NAME).hdd@@1M ::/EFI ::/EFI/BOOT ::/boot ::/boot/limine
133+
mcopy -i $(IMAGE_NAME).hdd@@1M kernel/bin/kernel ::/boot
134+
mcopy -i $(IMAGE_NAME).hdd@@1M limine.conf limine/limine-bios.sys ::/boot/limine
135+
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTX64.EFI ::/EFI/BOOT
136+
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTIA32.EFI ::/EFI/BOOT
137+
138+
.PHONY: clean
139+
clean:
140+
$(MAKE) -C kernel clean
141+
rm -rf iso_root $(IMAGE_NAME).iso $(IMAGE_NAME).hdd
142+
143+
.PHONY: distclean
144+
distclean: clean
145+
$(MAKE) -C kernel distclean
146+
rm -rf 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) 2022-2025 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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,3 @@ Running `make run` will build the kernel and a bootable ISO (equivalent to make
2525
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).
2626

2727
The `run-uefi` and `run-hdd-uefi` targets are equivalent to their non `-uefi` counterparts except that they boot `qemu` using a UEFI-compatible firmware.
28-
# osdev

kernel/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/compile_commands.json
2+
/.cache
3+
/freestnd-c-hdrs
4+
/src/cc-runtime
5+
/src/limine.h
6+
/bin
7+
/obj

kernel/GNUmakefile

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# Nuke built-in rules and variables.
2+
MAKEFLAGS += -rR
3+
.SUFFIXES:
4+
5+
# This is the name that our final executable will have.
6+
# Change as needed.
7+
override OUTPUT := kernel
8+
9+
# Install prefix; /usr/local is a good, standard default pick.
10+
PREFIX := /usr/local
11+
12+
# User controllable C compiler command.
13+
CC := cc
14+
15+
# User controllable C flags.
16+
CFLAGS := -g -O2 -pipe
17+
18+
# User controllable C preprocessor flags. We set none by default.
19+
CPPFLAGS :=
20+
21+
# User controllable nasm flags.
22+
NASMFLAGS := -F dwarf -g
23+
24+
# User controllable linker flags. We set none by default.
25+
LDFLAGS := -g
26+
27+
# Ensure the dependencies have been obtained.
28+
ifneq ($(shell ( test '$(MAKECMDGOALS)' = clean || test '$(MAKECMDGOALS)' = distclean ); echo $$?),0)
29+
ifeq ($(shell ( ! test -d freestnd-c-hdrs || ! test -d src/cc-runtime || ! test -f src/limine.h ); echo $$?),0)
30+
$(error Please run the ./get-deps script first)
31+
endif
32+
endif
33+
34+
# Check if CC is Clang.
35+
override CC_IS_CLANG := $(shell ! $(CC) --version 2>/dev/null | grep 'clang' >/dev/null 2>&1; echo $$?)
36+
37+
# If the C compiler is Clang, set the target as needed.
38+
ifeq ($(CC_IS_CLANG),1)
39+
override CC += \
40+
-target x86_64-unknown-none
41+
endif
42+
43+
# Internal C flags that should not be changed by the user.
44+
override CFLAGS += \
45+
-Wall \
46+
-Wextra \
47+
-std=gnu11 \
48+
-nostdinc \
49+
-ffreestanding \
50+
-fno-stack-protector \
51+
-fno-stack-check \
52+
-fno-PIC \
53+
-ffunction-sections \
54+
-fdata-sections \
55+
-m64 \
56+
-march=x86-64 \
57+
-mno-80387 \
58+
-mno-mmx \
59+
-mno-sse \
60+
-mno-sse2 \
61+
-mno-red-zone \
62+
-mcmodel=kernel
63+
64+
# Internal C preprocessor flags that should not be changed by the user.
65+
override CPPFLAGS := \
66+
-I src \
67+
-isystem freestnd-c-hdrs \
68+
$(CPPFLAGS) \
69+
-DLIMINE_API_REVISION=3 \
70+
-MMD \
71+
-MP
72+
73+
# Internal nasm flags that should not be changed by the user.
74+
override NASMFLAGS += \
75+
-Wall \
76+
-f elf64
77+
78+
# Internal linker flags that should not be changed by the user.
79+
override LDFLAGS += \
80+
-Wl,-m,elf_x86_64 \
81+
-Wl,--build-id=none \
82+
-nostdlib \
83+
-static \
84+
-z max-page-size=0x1000 \
85+
-Wl,--gc-sections \
86+
-T linker.ld
87+
88+
# Use "find" to glob all *.c, *.S, and *.asm files in the tree and obtain the
89+
# object and header dependency file names.
90+
override SRCFILES := $(shell cd src && find -L * -type f | LC_ALL=C sort)
91+
override CFILES := $(filter %.c,$(SRCFILES))
92+
override ASFILES := $(filter %.S,$(SRCFILES))
93+
override NASMFILES := $(filter %.asm,$(SRCFILES))
94+
override OBJ := $(addprefix obj/,$(CFILES:.c=.c.o) $(ASFILES:.S=.S.o) $(NASMFILES:.asm=.asm.o))
95+
override HEADER_DEPS := $(addprefix obj/,$(CFILES:.c=.c.d) $(ASFILES:.S=.S.d))
96+
97+
# Default target. This must come first, before header dependencies.
98+
.PHONY: all
99+
all: bin/$(OUTPUT)
100+
101+
# Include header dependencies.
102+
-include $(HEADER_DEPS)
103+
104+
# Link rules for the final executable.
105+
bin/$(OUTPUT): GNUmakefile linker.ld $(OBJ)
106+
mkdir -p "$$(dirname $@)"
107+
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $@
108+
109+
# Compilation rules for *.c files.
110+
obj/%.c.o: src/%.c GNUmakefile
111+
mkdir -p "$$(dirname $@)"
112+
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
113+
114+
# Compilation rules for *.S files.
115+
obj/%.S.o: src/%.S GNUmakefile
116+
mkdir -p "$$(dirname $@)"
117+
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
118+
119+
# Compilation rules for *.asm (nasm) files.
120+
obj/%.asm.o: src/%.asm GNUmakefile
121+
mkdir -p "$$(dirname $@)"
122+
nasm $(NASMFLAGS) $< -o $@
123+
124+
# Remove object files and the final executable.
125+
.PHONY: clean
126+
clean:
127+
rm -rf bin obj
128+
129+
# Remove everything built and generated including downloaded dependencies.
130+
.PHONY: distclean
131+
distclean: clean
132+
rm -rf freestnd-c-hdrs src/cc-runtime src/limine.h
133+
134+
# Install the final built executable to its final on-root location.
135+
.PHONY: install
136+
install: all
137+
install -d "$(DESTDIR)$(PREFIX)/share/$(OUTPUT)"
138+
install -m 644 bin/$(OUTPUT) "$(DESTDIR)$(PREFIX)/share/$(OUTPUT)/"
139+
140+
# Try to undo whatever the "install" target did.
141+
.PHONY: uninstall
142+
uninstall:
143+
rm -f "$(DESTDIR)$(PREFIX)/share/$(OUTPUT)/$(OUTPUT)"
144+
-rmdir "$(DESTDIR)$(PREFIX)/share/$(OUTPUT)"

0 commit comments

Comments
 (0)