From 06d05938cc828901703112b99740c2e90a392b88 Mon Sep 17 00:00:00 2001 From: Alexey Gavrilov Date: Tue, 8 Aug 2023 18:38:07 +0700 Subject: First commit --- .gitignore | 5 +++++ README.md | 41 +++++++++++++++++++++++++++++++++++++++++ iso/boot/grub/grub.cfg | 3 +++ kernel/boot.s | 28 ++++++++++++++++++++++++++++ kernel/kernel.c | 4 ++++ kernel/linker.ld | 28 ++++++++++++++++++++++++++++ makefile | 30 ++++++++++++++++++++++++++++++ 7 files changed, 139 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 iso/boot/grub/grub.cfg create mode 100644 kernel/boot.s create mode 100644 kernel/kernel.c create mode 100644 kernel/linker.ld create mode 100644 makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5fdcd7a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Directory where project build +build + +# Operarting system's files needed to boot +iso/os/* diff --git a/README.md b/README.md new file mode 100644 index 0000000..2e7ba67 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +## Object Operating System + +---- + +## Install dependencies + +#### Ubuntu + +'''sh +sudo apt update -y +sudo apt -y install build-essential binutils nasm xorriso grub-common qemu-system-i386 mtools +''' + +## Build and run + +#### Ubuntu + +'''sh +# Build and create iso image +make install +make run +'''' + +## Docs + +- [1][Link on collection of articles "How startup app without OS"] +- [2][The GNU GRUB manual] +- [3][Executable and linkable format ELF] +- [4][NASM documentation] +- [5][Article where creating a simple OS] +- [6][Multiboot specification] +- [7][GCC compiler documentation] +- [8][Using NASM in a Hello World kernel] + +[1]:https://habr.com/ru/companies/neobit/articles/173263/ +[2]:https://www.gnu.org/software/grub/manual/grub/grub.pdf +[3]:https://www.cs.cmu.edu/afs/cs/academic/class/15213-f00/docs/elf.pdf +[4]:https://www.nasm.us/xdoc/2.16.01/nasmdoc.pdf +[5]:https://wiki.osdev.org/Bare_Bones#Writing_a_kernel_in_C.2B.2B +[6]:https://www.gnu.org/software/grub/manual/multiboot/multiboot.pdf +[7]:https://gcc.gnu.org/onlinedocs/gcc.pdf diff --git a/iso/boot/grub/grub.cfg b/iso/boot/grub/grub.cfg new file mode 100644 index 0000000..fd0f925 --- /dev/null +++ b/iso/boot/grub/grub.cfg @@ -0,0 +1,3 @@ +menuentry "myos" { + multiboot /os/kernel.bin +} diff --git a/kernel/boot.s b/kernel/boot.s new file mode 100644 index 0000000..2fd1c55 --- /dev/null +++ b/kernel/boot.s @@ -0,0 +1,28 @@ +MBALIGN equ 1 << 0 +MEMINFO equ 1 << 1 +MBFLAGS equ MBALIGN | MEMINFO +MAGIC equ 0x1BADB002 +CHECKSUM equ -(MAGIC + MBFLAGS) + +section .multiboot +align 4 + dd MAGIC + dd MBFLAGS + dd CHECKSUM + +section .bss +align 16 +stack_bottom: +resb 16384 +stack_top: + +section .text +global _start:function (_start.end - _start) +_start: + mov esp, stack_top + extern main + call main + cli +.hang: hlt + jmp .hang +.end: diff --git a/kernel/kernel.c b/kernel/kernel.c new file mode 100644 index 0000000..1d734c9 --- /dev/null +++ b/kernel/kernel.c @@ -0,0 +1,4 @@ +int main() +{ + return 0; +}; diff --git a/kernel/linker.ld b/kernel/linker.ld new file mode 100644 index 0000000..e490e6a --- /dev/null +++ b/kernel/linker.ld @@ -0,0 +1,28 @@ +ENTRY(_start) + +SECTIONS +{ + . = 1M; + + .text BLOCK(4K) : ALIGN(4K) + { + *.(.multiboot) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4k) + { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) + { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) + { + *(COMMON) + *(.bss) + } +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..1e07743 --- /dev/null +++ b/makefile @@ -0,0 +1,30 @@ +CC = gcc# compiler +LD = ld# linker +ASM = nasm# assembler +# what is the flag -O2 +CFLAGS = -c -Wall -ffreestanding -nostdinc -nostdlib -lgcc# c compiler flags +# flag elf32 allow decrease object file size +AFLAGS = -f elf64# asm compiler flags +BDIR = build# build directory + +.PHONY: install + +install: build + @echo "Creating an iso image of OS" + cp ./$(BDIR)/kernel.bin ./iso/os/ + grub-mkrescue -o ./$(BDIR)/kernel.iso ./iso + +build: + mkdir $(BDIR) + @echo "Compile source files..." + $(CC) $(CFLAGS) -o ./$(BDIR)/kernel.o ./kernel/kernel.c + $(ASM) $(AFLAGS) -o ./$(BDIR)/boot.o ./kernel/boot.s + @echo "Link object files..." + $(LD) -m elf_x86_64 -o ./$(BDIR)/kernel.bin -T ./kernel/linker.ld ./$(BDIR)/*.o + @echo "Project was built" + +clean: + rm -rf ./build + +run: + qemu-system-i386 -cdrom ./$(BDIR)/kernel.iso -- cgit v1.2.3