From 6cf44540fb98a22cfa68a98d7abea0db003b2bff Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 13 Oct 2022 09:20:08 +0300 Subject: [PATCH] Code stub --- .gitignore | 9 ++++-- BIOS/Makefile | 22 ++++++++++++++ BIOS/boot.asm | 65 ++++++++++++++++++++++++++++++++++++++++ BIOS/print.inc | 9 ++++++ BIOS/second.asm | 9 ++++++ Makefile | 23 +++++++------- UEFI/Makefile | 44 +++++++++++++++++++++++++++ UEFI/src/FileLoader.c | 8 +++++ UEFI/src/FileLoader.h | 5 ++++ UEFI/src/Lynx.c | 23 ++++++++++++++ UEFI/src/Paging.c | 0 UEFI/src/VirtualMemory.c | 0 12 files changed, 203 insertions(+), 14 deletions(-) create mode 100644 BIOS/Makefile create mode 100644 BIOS/boot.asm create mode 100644 BIOS/print.inc create mode 100644 BIOS/second.asm create mode 100644 UEFI/Makefile create mode 100644 UEFI/src/FileLoader.c create mode 100644 UEFI/src/FileLoader.h create mode 100644 UEFI/src/Lynx.c create mode 100644 UEFI/src/Paging.c create mode 100644 UEFI/src/VirtualMemory.c diff --git a/.gitignore b/.gitignore index 7e2f07d..e42c5ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ -gnu-efi -include +*.o +efi-loader.bin +loader.bin +UEFI/gnu-efi +UEFI/include +UEFI/BOOTX64.EFI +UEFI/BOOTIA32.EFI \ No newline at end of file diff --git a/BIOS/Makefile b/BIOS/Makefile new file mode 100644 index 0000000..2615df1 --- /dev/null +++ b/BIOS/Makefile @@ -0,0 +1,22 @@ +include ../../Makefile.conf + +NAME=loader.bin +NASM = /usr/bin/nasm + +ASM_SOURCES = $(shell find ./ -type f -name '*.asm') +OBJ = $(ASM_SOURCES:.asm=.o) + +prepare: + $(info Nothing to prepare) + +$(NAME): $(OBJ) + cat boot.o second.o > $@ + +build: $(NAME) + +%.o: %.asm + $(info Compiling $<) + $(NASM) $< -f bin -o $@ + +clean: + rm -f $(OBJ) $(NAME) diff --git a/BIOS/boot.asm b/BIOS/boot.asm new file mode 100644 index 0000000..a4d8338 --- /dev/null +++ b/BIOS/boot.asm @@ -0,0 +1,65 @@ +[ORG 0x7C00] +[BITS 16] + +start: + jmp 0x0000:Boot + nop + +times 8-($-$$) db 0 +PrimaryVolumeDescriptor dd 0 +BootFileLocation dd 0 +BootFileLength dd 0 +Checksum dd 0 +Reserved times 40 db 0 +times 90-($-$$) db 0 + +%include "print.inc" + +Boot: + cli + mov [BOOT_DISK], dl + xor ax, ax + mov ds, ax + mov ss, ax + mov sp, 0x9C00 + + mov si, ErrorText + call Print + hlt + jmp $ + + mov si, BootloaderText + call Print + call ReadDisk + jmp EX_ADDRESS + jmp $ + +ReadDisk: + sti + mov ah, 0x02 + mov bx, EX_ADDRESS + mov al, 20 ; max 65 + mov dl, [BOOT_DISK] + mov ch, 0x00 + mov dh, 0x00 + mov cl, 0x02 + int 0x13 + jc DiskError + cli + ret + +DiskError: + cli + mov si, DiskReadingErrorMessage + call Print + jmp $ + +ErrorText db 'BIOS boot not implemented', 0 +BootloaderText db 'Lynx Bootloader', 0 +DiskReadingErrorMessage: db ' Disk Error', 0 +EX_ADDRESS equ 0x8000 +BOOT_DISK: db 0 + +times 510-($-$$) db 0 +db 0x55 +db 0xAA diff --git a/BIOS/print.inc b/BIOS/print.inc new file mode 100644 index 0000000..cfa2ee7 --- /dev/null +++ b/BIOS/print.inc @@ -0,0 +1,9 @@ +Print: + lodsb + or al, al + jz PrintDone + mov ah, 0eh + int 10h + jmp Print +PrintDone: + ret diff --git a/BIOS/second.asm b/BIOS/second.asm new file mode 100644 index 0000000..3229703 --- /dev/null +++ b/BIOS/second.asm @@ -0,0 +1,9 @@ +; TODO +init: + mov si, LoadingText + call Print + jmp $ + +%include "print.inc" + +LoadingText db ' Loading...', 0 diff --git a/Makefile b/Makefile index 308a53d..23d8924 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,14 @@ -GNUEFI_RELEASE_VERSION=3.0.14 +prepare: + make -C BIOS prepare + make -C UEFI prepare -gnuefi: - wget https://archive.org/download/gnu-efi-$(GNUEFI_RELEASE_VERSION).tar/gnu-efi-$(GNUEFI_RELEASE_VERSION).tar.bz2 - tar -xf gnu-efi-$(GNUEFI_RELEASE_VERSION).tar.bz2 - rm gnu-efi-$(GNUEFI_RELEASE_VERSION).tar.bz2 - mv ./gnu-efi-$(GNUEFI_RELEASE_VERSION) ./gnu-efi - mkdir -p include - cp -a ./gnu-efi/inc/. ./include - make -C gnu-efi - -prepare: gnuefi - $(info Nothing to prepare) +build: + make -C BIOS build + make -C UEFI build + cp BIOS/loader.bin . + cp UEFI/efi-loader.bin . clean: + make -C BIOS clean + make -C UEFI clean + rm -f loader.bin efi-loader.bin diff --git a/UEFI/Makefile b/UEFI/Makefile new file mode 100644 index 0000000..8320c82 --- /dev/null +++ b/UEFI/Makefile @@ -0,0 +1,44 @@ +include ../../Makefile.conf + +NAME=efi-loader.bin +CC = gcc +LD = ld +OBJCOPY = objcopy + +C_SOURCES = $(shell find ./src -type f -name '*.c') +OBJ = $(C_SOURCES:.c=.o) + +GNUEFI_RELEASE_VERSION=3.0.14 + +gnuefi: + wget https://archive.org/download/gnu-efi-$(GNUEFI_RELEASE_VERSION).tar/gnu-efi-$(GNUEFI_RELEASE_VERSION).tar.bz2 + tar -xf gnu-efi-$(GNUEFI_RELEASE_VERSION).tar.bz2 + rm gnu-efi-$(GNUEFI_RELEASE_VERSION).tar.bz2 + mv ./gnu-efi-$(GNUEFI_RELEASE_VERSION) ./gnu-efi + mkdir -p include + cp -a ./gnu-efi/inc/. ./include + make -C gnu-efi + +prepare: gnuefi + +build: $(NAME) + +$(NAME): BOOTX64 + dd if=/dev/zero of=$(NAME) bs=512 count=93750 + mformat -i $(NAME) :: + mmd -i $(NAME) ::/EFI + mmd -i $(NAME) ::/EFI/BOOT + mcopy -i $(NAME) BOOTX64.EFI ::/EFI/BOOT + +BOOTX64: $(OBJ) + $(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 $(OBJ) -o tmp.so -lgnuefi -lefi + $(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 tmp.so BOOTX64.EFI + rm tmp.so + +%.o: %.c + $(info Compiling $<) + $(CC) -Ignu-efi/inc -Ignu-efi/inc/x86_64 -Ignu-efi/inc/protocol -fpic -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -maccumulate-outgoing-args -c $< -o $@ + + +clean: + rm -f $(NAME) $(OBJ) BOOTX64.EFI diff --git a/UEFI/src/FileLoader.c b/UEFI/src/FileLoader.c new file mode 100644 index 0000000..bc40996 --- /dev/null +++ b/UEFI/src/FileLoader.c @@ -0,0 +1,8 @@ +#include "FileLoader.h" + +// https://wiki.osdev.org/Loading_files_under_UEFI + +EFI_FILE *LoadFile(EFI_FILE *Directory, CHAR16 *Path, EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) +{ + return NULL; +} diff --git a/UEFI/src/FileLoader.h b/UEFI/src/FileLoader.h new file mode 100644 index 0000000..f7c43d9 --- /dev/null +++ b/UEFI/src/FileLoader.h @@ -0,0 +1,5 @@ +#pragma once +#include +#include + +EFI_FILE *LoadFile(EFI_FILE *Directory, CHAR16 *Path, EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable); diff --git a/UEFI/src/Lynx.c b/UEFI/src/Lynx.c new file mode 100644 index 0000000..91a7e30 --- /dev/null +++ b/UEFI/src/Lynx.c @@ -0,0 +1,23 @@ +#include +#include + +#include "FileLoader.h" + +EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) +{ + InitializeLib(ImageHandle, SystemTable); + SystemTable->BootServices->SetWatchdogTimer(0, 0, 0, NULL); + Print(L"Lynx Bootloader © EnderIce2 2022\n"); + EFI_FILE *Kernel = LoadFile(NULL, L"kernel.fsys", ImageHandle, SystemTable); + + if (Kernel == NULL) + { + Print(L"Kernel not found\n"); + while (1) + asm("hlt"); + } + + while (1) + asm("hlt"); + return EFI_SUCCESS; +} diff --git a/UEFI/src/Paging.c b/UEFI/src/Paging.c new file mode 100644 index 0000000..e69de29 diff --git a/UEFI/src/VirtualMemory.c b/UEFI/src/VirtualMemory.c new file mode 100644 index 0000000..e69de29