summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--build.bat29
-rw-r--r--dbgcmd4
-rw-r--r--linker.ld9
-rw-r--r--src/boot.s81
-rw-r--r--src/kernel.c3
5 files changed, 126 insertions, 0 deletions
diff --git a/build.bat b/build.bat
new file mode 100644
index 0000000..da1fbba
--- /dev/null
+++ b/build.bat
@@ -0,0 +1,29 @@
+IF EXIST build (
+ rmdir /s /q build
+)
+
+mkdir build
+
+:: bootloader
+nasm -g -O0 -f bin -o build/boot.bin src/boot.s
+
+:: kernel
+clang --target=x86_64-unknown-linux-gnu -c src/kernel.c -o build/kernel.o -std=c23 -ffreestanding -Wall -Wextra
+ld.lld -T linker.ld build/kernel.o -o build/kernel.bin
+
+:: create img
+copy ref.img build/disk.img
+:: qemu-img create -f raw build/disk.img 8M
+:: qemu-img dd if=build/boot.bin of=build/disk.img
+:: qemu-img dd count=2 skip=1 if=build/kernel.o of=build/disk.img
+
+:: -drive is equivalent to -hda
+:: bootloader
+::start qemu-system-x86_64 -drive file=build/boot.bin,index=0,media=disk,format=raw -s -S
+:: img with bootload
+start qemu-system-x86_64 -drive file=ref.img,index=0,media=disk,format=raw -s -S
+:: start qemu-system-x86_64 -drive file=fat:build/boot.bin,index=0,media=disk,format=raw -s -S
+
+:: start lldb --arch x86_64 -s dbgcmd
+
+:: start windbgx -v -kx exdi:PathToSrvCfgFiles=exdiConfigData.xml,CLSID={29f9906e-9dbe-4d4b-b0fb-6acf7fb6d014},Kd=Guess,DataBreaks=Exdi
diff --git a/dbgcmd b/dbgcmd
new file mode 100644
index 0000000..6054b7f
--- /dev/null
+++ b/dbgcmd
@@ -0,0 +1,4 @@
+settings set target.x86-disassembly-flavor intel
+gdb-remote localhost:1234
+b 0x7c00
+c
diff --git a/linker.ld b/linker.ld
new file mode 100644
index 0000000..03d3967
--- /dev/null
+++ b/linker.ld
@@ -0,0 +1,9 @@
+ENTRY(k_main)
+
+SECTIONS {
+ . = 2M;
+
+}
+
+OUTPUT_FORMAT(elf64-x86-64)
+OUTPUT(kernel.bin)
diff --git a/src/boot.s b/src/boot.s
new file mode 100644
index 0000000..28da384
--- /dev/null
+++ b/src/boot.s
@@ -0,0 +1,81 @@
+ mov ax, 0x7c0
+ mov ds, ax
+ mov ax, 0xb800 ; protected mode video memory segment (addr / 16)
+ mov es, ax
+ mov si, msg
+
+ cld
+
+ call print
+
+ cli
+
+; print using BIOS functions
+; print_loop:
+; lodsb ; load byte from DS:SI into al
+; or al, al
+; jz hang
+; mov ah, 0x0E
+; int 0x10
+; jmp print_loop
+
+; print without using BIOS functions
+print:
+ lodsb
+ cmp al, 0
+ jz exit_print
+
+ mov ah, 0x0F
+ mov cx, ax
+
+ movzx bx, byte [xpos]
+ shl bx, 1
+ movzx ax, byte [ypos]
+ mov dx, 160
+ mul dx
+
+ xor di, di
+ mov di, bx
+ add di, ax
+
+ mov ax, cx
+ stosw
+ add byte[xpos], 1
+ jmp print
+
+exit_print:
+ add byte[ypos], 1
+ mov byte[xpos], 0
+ ret
+
+hang:
+ jmp hang
+
+
+ ; vars
+ msg db 'Hello world', 13, 10, 0 ; Hello world CR LF \0
+ xpos db 0
+ ypos db 0
+
+
+ ; fill
+ times 0x1b8 -($-$$) db 0 ; fill MBR boostrap
+ dd 0 ; fill unique disk ID
+ dw 0 ; reserved
+ ; write first partition table entry CHS for start and end are set to 0
+ db 0x80 ; mark the partition as bootable
+ db 0x0 ; partition start head
+ dw 0x0 ; partition start sector and cylinder (sector = 1, cylinder = 0)
+ db 0x06 ; system ID (partition type, 07 = exFAT)
+ db 0x0 ; partition end head
+ dw 0x0 ; partition end sector and cylinder (sector = 5, cylinder = 455)
+ dd 0x1 ; partition start LBA
+ dd 0x3fff ; partition end LBA
+
+ times 0x10 db 0 ; fill second partition table entry
+ times 0x10 db 0 ; fill third partition table entry
+ times 0x10 db 0 ; fill fourth partition table entry
+ dw 0xAA55 ; valid boot sector signature
+
+ ; db 'TEST'
+ ; times 0x800000 - ($-$$) db 0
diff --git a/src/kernel.c b/src/kernel.c
new file mode 100644
index 0000000..23d2c96
--- /dev/null
+++ b/src/kernel.c
@@ -0,0 +1,3 @@
+int k_main(void) {
+ return 0;
+}