From 72e143b93caef608d73a61f3661c48a7b725d097 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sun, 18 Mar 2018 16:41:11 -0700 Subject: [PATCH] Bootloader loading kernel, not yet jumping to it --- .gitignore | 1 + Makefile | 28 ++++---- NOTES.md | 10 +++ modules.mk | 2 +- src/arch/x86_64/boot.s | 12 ---- src/arch/x86_64/details.c | 1 + src/arch/x86_64/kernel.ld | 2 +- src/boot/loader.c | 129 +++++++++++++++++++------------------ src/boot/loader.h | 2 +- src/boot/main.c | 22 +++---- src/boot/memory.c | 5 +- src/modules/main/main.c | 4 ++ src/modules/main/module.mk | 2 + 13 files changed, 117 insertions(+), 103 deletions(-) create mode 100644 NOTES.md delete mode 100755 src/arch/x86_64/boot.s create mode 100644 src/arch/x86_64/details.c create mode 100644 src/modules/main/main.c create mode 100644 src/modules/main/module.mk diff --git a/.gitignore b/.gitignore index 3a46e06..391e8b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .lock* build *.bak +tags diff --git a/Makefile b/Makefile index e68c641..fc297f0 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,10 @@ BUILD_D := build ARCH_D := src/arch/$(ARCH) VERSION ?= $(shell git describe --dirty --always) +KERNEL_FILENAME:= popcorn.bin + +MODULES := main + EFI_DIR := external/gnu-efi EFI_DATA := $(EFI_DIR)/gnuefi @@ -33,7 +37,6 @@ BASEFLAGS += -mcpu=$(CPU) endif # Removed Flags:: -Wcast-align -WARNFLAGS := -Wall -Werror -Wextra -Wshadow -Wwrite-strings WARNFLAGS += -Wformat=2 -Winit-self -Wfloat-equal -Winline WARNFLAGS += -Winvalid-pch -Wmissing-format-attribute WARNFLAGS += -Wmissing-include-dirs -Wswitch -Wundef @@ -51,6 +54,7 @@ CFLAGS := $(INCLUDES) $(DEPENDFLAGS) $(BASEFLAGS) $(WARNFLAGS) CFLAGS += -std=c11 -fpic -fshort-wchar CFLAGS += -mno-red-zone -fno-stack-protector CFLAGS += -DGIT_VERSION="L\"$(VERSION)\"" +CFLAGS += -DKERNEL_FILENAME="L\"$(KERNEL_FILENAME)\"" CFLAGS += -DEFI_DEBUG=0 -DEFI_DEBUG_CLEAR_MEMORY=0 CFLAGS += -DGNU_EFI_USE_MS_ABI -DHAVE_USE_MS_ABI #CFLAGS += -DEFI_FUNCTION_WRAPPER @@ -126,12 +130,9 @@ $(BUILD_D)/.version: $(EFI_LIB): make -C external/gnu-efi all -$(BUILD_D)/kernel.elf: $(KOBJS) $(MOD_TARGETS) $(ARCH_D)/kernel.ld - $(LD) $(LDFLAGS) -T $(ARCH_D)/kernel.ld -o $@ $(KOBJS) $(patsubst %,-l%,$(MODULES)) - $(BUILD_D)/boot.elf: $(BOBJS) $(EFI_LIB) $(LD) $(BOOT_LDFLAGS) -T $(EFI_LDS) -o $@ \ - $(EFI_CRT_OBJ) $(BOBJS) $(patsubst %,-l%,$(MODULES)) -lefi -lgnuefi + $(EFI_CRT_OBJ) $(BOBJS) -lefi -lgnuefi $(BUILD_D)/boot.efi: $(BUILD_D)/boot.elf objcopy -j .text -j .sdata -j .data -j .dynamic \ @@ -145,21 +146,24 @@ $(BUILD_D)/boot.debug.efi: $(BUILD_D)/boot.elf -j .debug_aranges -j .debug_line -j .debug_macinfo \ --target=efi-app-$(ARCH) $^ $@ -$(BUILD_D)/%.elf.dump: $(BUILD_D)/%.elf - $(OBJD) -D -S $< > $@ - -$(BUILD_D)/%.bin.dump: $(BUILD_D)/%.bin - $(OBJD) -D $< > $@ - $(BUILD_D)/%.bin: $(BUILD_D)/%.elf $(OBJC) $< -O binary $@ +$(BUILD_D)/boot.dump: $(BUILD_D)/boot.efi + $(OBJD) -D -S $< > $@ + $(BUILD_D)/boot/%.s.o: src/boot/%.s $(INIT_DEP) $(AS) $(ASFLAGS) -o $@ $< $(BUILD_D)/boot/%.c.o: src/boot/%.c $(INIT_DEP) $(CC) $(BOOT_CFLAGS) -c -o $@ $< +$(BUILD_D)/kernel.elf: $(KOBJS) $(MOD_TARGETS) $(ARCH_D)/kernel.ld + $(LD) $(LDFLAGS) -u kernel_main -T $(ARCH_D)/kernel.ld -o $@ $(KOBJS) $(patsubst %,-l%,$(MODULES)) + +$(BUILD_D)/kernel.dump: $(BUILD_D)/kernel.elf + $(OBJD) -D -S $< > $@ + $(BUILD_D)/arch/%.s.o: $(ARCH_D)/%.s $(INIT_DEP) $(AS) $(ASFLAGS) -o $@ $< @@ -177,7 +181,7 @@ $(BUILD_D)/fs.img: $(BUILD_D)/boot.efi $(BUILD_D)/kernel.bin mmd -i $(TEMPFILE) ::/EFI mmd -i $(TEMPFILE) ::/EFI/BOOT mcopy -i $(TEMPFILE) $(BUILD_D)/boot.efi ::/EFI/BOOT/BOOTX64.efi - mcopy -i $(TEMPFILE) $(BUILD_D)/kernel.bin ::/kernel.bin + mcopy -i $(TEMPFILE) $(BUILD_D)/kernel.bin ::$(KERNEL_FILENAME) mlabel -i $(TEMPFILE) ::Popcorn_OS dd if=$(TEMPFILE) of=$@.tmp bs=512 count=91669 seek=2048 conv=notrunc rm $(TEMPFILE) diff --git a/NOTES.md b/NOTES.md new file mode 100644 index 0000000..1a22ebe --- /dev/null +++ b/NOTES.md @@ -0,0 +1,10 @@ +# Design / WIP notes + +## Bootloader / UEFI + +* What is the interface between the UEFI boot portion and the kernel? + * Allocate pages, use UEFI reserved mapping types (0x70000000 - 0x7fffffff) + * Passing memory map: Allocate earliest pages and fill with UEFI's memory map + stuctures? + + diff --git a/modules.mk b/modules.mk index 1d08aad..df0601a 100644 --- a/modules.mk +++ b/modules.mk @@ -4,7 +4,7 @@ endif ifndef SOURCES SOURCES := $(wildcard src/modules/$(MOD_NAME)/*.c) - SOURCES += $(wildcard src/modules/$(MOD_NAME)/*.S) + SOURCES += $(wildcard src/modules/$(MOD_NAME)/*.s) endif ifeq "$(SOURCES)" "" diff --git a/src/arch/x86_64/boot.s b/src/arch/x86_64/boot.s deleted file mode 100755 index a5895f1..0000000 --- a/src/arch/x86_64/boot.s +++ /dev/null @@ -1,12 +0,0 @@ -; boot.s -- Kernel start location. - -[BITS 64] -ALIGN 4 - -SECTION .text -[GLOBAL start] - -start: - ; Load multiboot header location - mov ebx, 0xdeadbeef - jmp $ diff --git a/src/arch/x86_64/details.c b/src/arch/x86_64/details.c new file mode 100644 index 0000000..f1f11fe --- /dev/null +++ b/src/arch/x86_64/details.c @@ -0,0 +1 @@ +const char *KERNEL_PLATFORM = "x86_64"; diff --git a/src/arch/x86_64/kernel.ld b/src/arch/x86_64/kernel.ld index 4913206..587825a 100755 --- a/src/arch/x86_64/kernel.ld +++ b/src/arch/x86_64/kernel.ld @@ -1,4 +1,4 @@ -ENTRY(start) +ENTRY(kernel_main) SECTIONS { . = 0x100000; diff --git a/src/boot/loader.c b/src/boot/loader.c index 25bc13d..cf8944e 100644 --- a/src/boot/loader.c +++ b/src/boot/loader.c @@ -3,73 +3,76 @@ static CHAR16 kernel_name[] = KERNEL_FILENAME; -static EFI_STATUS loader_load_file(EFI_FILE_PROTOCOL *root, void **kernel_image, UINT64 *len) { - EFI_STATUS status; +EFI_STATUS loader_load_kernel(void **kernel_image, uint64_t *length) { + if (kernel_image == 0 || length == 0) + CHECK_EFI_STATUS_OR_RETURN(EFI_INVALID_PARAMETER, "NULL kernel_image or length pointer"); - EFI_FILE_PROTOCOL *handle = 0; - status = root->Open(root, &handle, kernel_name, EFI_FILE_MODE_READ, 0); - CHECK_EFI_STATUS_OR_RETURN(status, "Failed to open kernel file handle"); + EFI_STATUS status; + EFI_GUID guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; + EFI_HANDLE *handles = NULL; + UINTN handleCount = 0; - EFI_FILE_INFO *info = LibFileInfo(handle); - if (info->FileSize == 0) - return EFI_NOT_FOUND; + status = ST->BootServices->LocateHandleBuffer( + ByProtocol, &guid, NULL, &handleCount, &handles); + CHECK_EFI_STATUS_OR_RETURN(status, "LocateHandleBuffer"); - UINTN count = ((info->FileSize - 1) / 0x1000) + 1; - EFI_PHYSICAL_ADDRESS addr = 0x100000; // Try to load the kernel in at 1MiB - EFI_MEMORY_TYPE memType = 0xFFFFFFFF; // Special value to tell the kernel it's here - status = ST->BootServices->AllocatePages(AllocateAddress, memType, count, &addr); - if (status == EFI_NOT_FOUND) { - // couldn't get the address we wanted, try loading the kernel anywhere - status = ST->BootServices->AllocatePages(AllocateAnyPages, memType, count, &addr); + for (unsigned i=0; iBootServices->HandleProtocol( + handles[i], &guid, (void**)&fileSystem); + CHECK_EFI_STATUS_OR_RETURN(status, "HandleProtocol"); + + EFI_FILE_PROTOCOL *root = NULL; + status = fileSystem->OpenVolume(fileSystem, &root); + CHECK_EFI_STATUS_OR_RETURN(status, "OpenVolume"); + + EFI_FILE_PROTOCOL *file = NULL; + status = root->Open( + root, + &file, + kernel_name, + EFI_FILE_MODE_READ, + EFI_FILE_READ_ONLY|EFI_FILE_HIDDEN|EFI_FILE_SYSTEM); + + if (!EFI_ERROR(status)) { + void *buffer = NULL; + EFI_GUID file_info_guid = EFI_FILE_INFO_ID; + UINTN buffer_size = sizeof(EFI_FILE_INFO) + sizeof(kernel_name); + + status = ST->BootServices->AllocatePool(EfiLoaderCode, buffer_size, &buffer); + CHECK_EFI_STATUS_OR_RETURN(status, "Allocating kernel file info memory"); + + status = file->GetInfo(file, &file_info_guid, &buffer_size, buffer); + CHECK_EFI_STATUS_OR_RETURN(status, "Getting kernel file info"); + + buffer_size = ((EFI_FILE_INFO*)buffer)->FileSize; + + status = ST->BootServices->FreePool(buffer); + CHECK_EFI_STATUS_OR_RETURN(status, "Freeing kernel file info memory"); + + UINTN page_count = ((buffer_size - 1) / 0x1000) + 1; + EFI_PHYSICAL_ADDRESS addr = 0x100000; // Try to load the kernel in at 1MiB + EFI_MEMORY_TYPE mem_type = 0xFFFFFFFF; // Special value to tell the kernel it's here + status = ST->BootServices->AllocatePages(AllocateAddress, mem_type, page_count, &addr); + if (status == EFI_NOT_FOUND) { + // couldn't get the address we wanted, try loading the kernel anywhere + status = ST->BootServices->AllocatePages(AllocateAnyPages, mem_type, page_count, &addr); + } + CHECK_EFI_STATUS_OR_RETURN(status, "Allocating kernel pages"); + + buffer = (void*)addr; + status = file->Read(file, &buffer_size, buffer); + CHECK_EFI_STATUS_OR_RETURN(status, "Reading kernel file"); + + status = file->Close(file); + CHECK_EFI_STATUS_OR_RETURN(status, "Closing kernel file handle"); + + *length = buffer_size; + *kernel_image = buffer; + return EFI_SUCCESS; + } } - CHECK_EFI_STATUS_OR_RETURN(status, "Failed to allocate kernel pages"); - - UINTN buffer_size = count * 0x1000; - void *buffer = (void*)addr; - status = handle->Read(handle, &buffer_size, &buffer); - CHECK_EFI_STATUS_OR_RETURN(status, "Failed to read kernel to memory"); - - *len = buffer_size; - *kernel_image = buffer; - return EFI_SUCCESS; -} - -EFI_STATUS loader_load_kernel(void **kernel_image, UINT64 *len) { - if (kernel_image == 0 || len == 0) - CHECK_EFI_STATUS_OR_RETURN(EFI_INVALID_PARAMETER, "NULL kernel_image pointer or size"); - - EFI_STATUS status; - - // First, find all the handles that support the filesystem protocol. Call - UINTN size = 0; - EFI_GUID fs_guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; - status = ST->BootServices->LocateHandle(ByProtocol, &fs_guid, NULL, &size, NULL); - if (status != EFI_BUFFER_TOO_SMALL) { - CHECK_EFI_STATUS_OR_RETURN(status, "Failed to find number of filesystem handles"); - } else if (size == 0) { - CHECK_EFI_STATUS_OR_RETURN(EFI_NO_MEDIA, "Found zero filesystem handles"); - } - - EFI_HANDLE *buffer = 0; - status = ST->BootServices->AllocatePool(EfiLoaderData, size, (void**)&buffer); - CHECK_EFI_STATUS_OR_RETURN(status, "Failed to allocate buffer of filesystem handles"); - - status = ST->BootServices->LocateHandle(ByProtocol, &fs_guid, NULL, &size, buffer); - CHECK_EFI_STATUS_OR_RETURN(status, "Failed to find filesystem handles"); - - unsigned num_fs = size / sizeof(EFI_HANDLE); - EFI_HANDLE *fss = (EFI_HANDLE*)buffer; - - for (unsigned i = 0; i < num_fs; ++i) { - EFI_FILE_HANDLE root = LibOpenRoot(fss[i]); - - status = loader_load_file(root, kernel_image, len); - if (status == EFI_NOT_FOUND) - continue; - - CHECK_EFI_STATUS_OR_RETURN(status, "Failed to load kernel"); - return EFI_SUCCESS; - } return EFI_NOT_FOUND; } diff --git a/src/boot/loader.h b/src/boot/loader.h index 6829d38..800f56e 100644 --- a/src/boot/loader.h +++ b/src/boot/loader.h @@ -5,4 +5,4 @@ #define KERNEL_FILENAME L"kernel.bin" #endif -EFI_STATUS loader_load_kernel(void **kernel_image, UINT64 *len); +EFI_STATUS loader_load_kernel(void **kernel_image, UINT64 *length); diff --git a/src/boot/main.c b/src/boot/main.c index b89b078..96cf513 100644 --- a/src/boot/main.c +++ b/src/boot/main.c @@ -33,22 +33,22 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) CHECK_EFI_STATUS_OR_FAIL(status); con_status_ok(); + // memory_dump_map(); + + con_status_begin(L"Loading kernel into memory..."); + void *kernel_image = NULL; + uint64_t kernel_length = 0; + status = loader_load_kernel(&kernel_image, &kernel_length); + CHECK_EFI_STATUS_OR_FAIL(status); + Print(L" %u bytes at 0x%x", kernel_length, kernel_image); + con_status_ok(); + + /* con_status_begin(L"Virtualizing memory..."); status = memory_virtualize(); CHECK_EFI_STATUS_OR_FAIL(status); con_status_ok(); - con_status_begin(L"Loading kernel into memory..."); - void *kernel_image = NULL; - UINT64 len = 0; - status = loader_load_kernel(&kernel_image, &len); - CHECK_EFI_STATUS_OR_FAIL(status); - Print(L" %u bytes at 0x%x", len, kernel_image); - con_status_ok(); - - /* - dump_memory_map(); - UINTN memmap_size = 0; EFI_MEMORY_DESCRIPTOR *memmap; UINTN memmap_key; diff --git a/src/boot/memory.c b/src/boot/memory.c index 7fc1a57..9769515 100644 --- a/src/boot/memory.c +++ b/src/boot/memory.c @@ -31,7 +31,7 @@ static const CHAR16 *memory_type_name(UINT32 value) { } void EFIAPI memory_update_addresses(EFI_EVENT UNUSED *event, void UNUSED *context) { - ST->RuntimeServices->ConvertPointer(0, (void **)&BS); + //ST->RuntimeServices->ConvertPointer(0, (void **)&BS); ST->RuntimeServices->ConvertPointer(0, (void **)&ST); } @@ -90,7 +90,8 @@ EFI_STATUS memory_dump_map() { Print(L"Type: %s Attr: 0x%x\n", memory_type_name(d->Type), d->Attribute); Print(L"\t Physical %016llx - %016llx\n", d->PhysicalStart, d->PhysicalStart + size_bytes); - Print(L"\t Virtual %016llx - %016llx\n\n", d->VirtualStart, d->VirtualStart + size_bytes); + if (d->VirtualStart != 0) + Print(L"\t Virtual %016llx - %016llx\n\n", d->VirtualStart, d->VirtualStart + size_bytes); d = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)d + desc_size); } diff --git a/src/modules/main/main.c b/src/modules/main/main.c new file mode 100644 index 0000000..e4f428d --- /dev/null +++ b/src/modules/main/main.c @@ -0,0 +1,4 @@ +__attribute__((used)) +int kernel_main() { + return 42; +} diff --git a/src/modules/main/module.mk b/src/modules/main/module.mk new file mode 100644 index 0000000..56bdd2e --- /dev/null +++ b/src/modules/main/module.mk @@ -0,0 +1,2 @@ +MOD_NAME := main +include modules.mk