diff --git a/Makefile b/Makefile index 5d2192b..b0d65d3 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,38 @@ -all: - mkdir -p build - gcc -Ignu-efi/inc -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -maccumulate-outgoing-args -std=c17 -c boot.c -o build/boot.o - g++ -Iinclude -Ignu-efi/inc -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -maccumulate-outgoing-args -std=c++17 -c kernel/main.cpp -o build/kernel.o - ld -shared -Bsymbolic -Lgnu-efi/x86_64/lib -Lgnu-efi/x86_64/gnuefi -Tgnu-efi/gnuefi/elf_x86_64_efi.lds gnu-efi/x86_64/gnuefi/crt0-efi-x86_64.o build/boot.o build/kernel.o -o build/boot.so -lgnuefi -lefi --no-undefined - objcopy -j .text -j .sdata -j .data -j .rodata -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --output-target efi-app-x86_64 --subsystem=10 build/boot.so build/BOOTX64.EFI +CFLAGS = -Iinclude -Ignu-efi/inc -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -std=c11 +CXXFLAGS = -Iinclude -Ignu-efi/inc -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -maccumulate-outgoing-args -std=c++17 +LDFLAGS = -shared -Bsymbolic -Lgnu-efi/x86_64/lib -Lgnu-efi/x86_64/gnuefi -Tgnu-efi/gnuefi/elf_x86_64_efi.lds +LDLIBS = -lgnuefi -lefi --no-undefined + +SRC_C = $(wildcard *.c) +SRC_CPP = $(wildcard */*.cpp) +OBJ = $(SRC_C:%.c=build/%.o) $(SRC_CPP:%.cpp=build/%.o) + +_bd: + @mkdir -p build/graphics build/kernel + +all: _bd $(OBJ) + @echo "* Linking EFI..." + @ld $(LDFLAGS) gnu-efi/x86_64/gnuefi/crt0-efi-x86_64.o build/boot.o build/graphics/draw.o build/kernel/main.o -o build/boot.so $(LDLIBS) + @objcopy -j .text -j .sdata -j .data -j .rodata -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --output-target efi-app-x86_64 --subsystem=10 build/boot.so build/BOOTX64.EFI + +build/%.o: %.c + @echo "Compile C File $< to $@" + @gcc $(CFLAGS) -c $< -o $@ + +build/%.o: %.cpp + @echo "Compile CPP File $< to $@" + @g++ $(CXXFLAGS) -c $< -o $@ vdir: all - mkdir -p vdir - mkdir -p vdir/EFI/BOOT - cp build/BOOTX64.EFI vdir/EFI/BOOT + @mkdir -p vdir/EFI/BOOT + @cp build/BOOTX64.EFI vdir/EFI/BOOT run: vdir - qemu-system-x86_64 -bios /usr/share/ovmf/OVMF.fd -net none -drive file=fat:rw:vdir,index=0,format=vvfat -serial file:serial.log -serial stdio -s -S + @echo "Launching QEMU" + @qemu-system-x86_64 -bios /usr/share/ovmf/OVMF.fd -net none -drive file=fat:rw:vdir,index=0,format=vvfat -serial file:serial.log -monitor stdio -s -S clean: - rm -rf build vdir + @echo "Cleaning old files" + @rm -rf build vdir -.PHONY: all vdir run clean \ No newline at end of file +.PHONY: all vdir run clean _bd \ No newline at end of file diff --git a/boot.c b/boot.c index b1a82a7..fe91196 100644 --- a/boot.c +++ b/boot.c @@ -3,16 +3,18 @@ extern void kernel_main(); -EFI_STATUS EFIAPI efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *ST) { - InitializeLib(ImageHandle, ST); +EFI_STATUS EFIAPI efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { + InitializeLib(ImageHandle, SystemTable); - uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); - uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, 1); - uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"Welcome to SYLVA OS!\n"); - uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, 2); - uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"* The system has booted successfully.\n"); - uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, 3); - uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"* Jumping to kernel...\n"); + uefi_call_wrapper(SystemTable->ConOut->ClearScreen, 1, SystemTable->ConOut); + uefi_call_wrapper(SystemTable->ConOut->SetCursorPosition, 3, SystemTable->ConOut, 0, 1); + uefi_call_wrapper(SystemTable->ConOut->OutputString, 2, SystemTable->ConOut, L"Welcome to SYLVA OS!\n"); + uefi_call_wrapper(SystemTable->ConOut->SetCursorPosition, 3, SystemTable->ConOut, 0, 2); + uefi_call_wrapper(SystemTable->ConOut->OutputString, 2, SystemTable->ConOut, L"* The system has booted successfully.\n"); + + + uefi_call_wrapper(SystemTable->ConOut->SetCursorPosition, 3, SystemTable->ConOut, 0, 3); + uefi_call_wrapper(SystemTable->ConOut->OutputString, 2, SystemTable->ConOut, L"* Jumping to kernel...\n"); kernel_main(); diff --git a/graphics/draw.cpp b/graphics/draw.cpp new file mode 100644 index 0000000..fbab475 --- /dev/null +++ b/graphics/draw.cpp @@ -0,0 +1,11 @@ +#include + +void draw_pixel(unsigned int x, unsigned int y, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color, + EFI_GRAPHICS_OUTPUT_PROTOCOL *GOP, unsigned int hr, EFI_GRAPHICS_OUTPUT_BLT_PIXEL *base) { + EFI_GRAPHICS_OUTPUT_BLT_PIXEL *p = base + (hr * y) + x; + + p->Blue = color.Blue; + p->Green = color.Green; + p->Red = color.Red; + p->Reserved = color.Reserved; +} diff --git a/include/.gitkeep b/include/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/include/graphics/draw.h b/include/graphics/draw.h new file mode 100644 index 0000000..784e0a8 --- /dev/null +++ b/include/graphics/draw.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +void draw_pixel(unsigned int x, unsigned int y, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color, + EFI_GRAPHICS_OUTPUT_PROTOCOL *GOP, unsigned int hr, EFI_GRAPHICS_OUTPUT_BLT_PIXEL *base); \ No newline at end of file diff --git a/kernel/main.cpp b/kernel/main.cpp index 4ccae86..4c254b8 100644 --- a/kernel/main.cpp +++ b/kernel/main.cpp @@ -1,12 +1,26 @@ #include #include +#include -extern "C" void kernel_main(); +extern "C" void kernel_main() { + EFI_GUID gop_guid = {0x9042a9de, 0x23dc, 0x4a38, {0x96, 0xfb, 0x7a, 0xde, 0xd0, 0x80, 0x51, 0x6a}}; + + EFI_GRAPHICS_OUTPUT_PROTOCOL *GOP; + uefi_call_wrapper((void*)ST->BootServices->SetWatchdogTimer, 4, 0, 0, 0, NULL); + uefi_call_wrapper((void*)ST->BootServices->LocateProtocol, 3, &gop_guid, NULL, (void **)&GOP); + + unsigned int hr = GOP->Mode->Info->HorizontalResolution; + EFI_GRAPHICS_OUTPUT_BLT_PIXEL *base = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) GOP->Mode->FrameBufferBase; -void kernel_main() { - // uefi_call_wrapper((void*)ST->ConOut->ClearScreen, 1, ST->ConOut); uefi_call_wrapper((void*)ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, 5); uefi_call_wrapper((void*)ST->ConOut->OutputString, 2, ST->ConOut, L"Kernel is running!\n"); - + + for (unsigned int i = 1; i <= 100; i++) { + // uefi_call_wrapper((void*)ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, i + 6); + // uefi_call_wrapper((void*)ST->ConOut->OutputString, 2, ST->ConOut, L"hello!"); + for (unsigned int j = 1; j <= 100; j++) { + draw_pixel(i, j, {255, 255, 255, 255}, GOP, hr, base); + } + } while (1); } \ No newline at end of file