# HG changeset patch # User taiki # Date 1359438300 -32400 # Node ID ac5d699b9787239bd28225f94683f2d51696f2d3 # Parent 30ba02c504a05060f412989523fcc47f186af55a# Parent 09ced7d8f64a2f3fd1fa39181f900d59f9e8f11e add config and open. diff -r 30ba02c504a0 -r ac5d699b9787 boot/Makefile --- a/boot/Makefile Mon Jan 21 23:41:36 2013 +0900 +++ b/boot/Makefile Tue Jan 29 14:45:00 2013 +0900 @@ -1,21 +1,23 @@ ARCH = $(shell uname -m | sed s,i[3456789]86,ia32,) -EFI_INCLUDE = /usr/local/include/efi +EFI_INCLUDE = /usr/include/efi EFI_INCLUDES = -I$(EFI_INCLUDE) -I$(EFI_INCLUDE)/$(ARCH) -I$(EFI_INCLUDE)/protocol -EFI_PATH = /usr/local/lib +EFI_PATH = /usr/lib64/gnuefi +EFI_LIB_PATH = /usr/lib64 LIB_GCC = $(shell $(CC) -print-libgcc-file-name) EFI_LIBS = -lefi -lgnuefi $(LIB_GCC) EFI_CRT_OBJS = $(EFI_PATH)/crt0-efi-$(ARCH).o EFI_LDS = $(EFI_PATH)/elf_$(ARCH)_efi.lds -CFLAGS = -O2 -fno-stack-protector -fno-strict-aliasing -fpic -fshort-wchar -Wall -Werror $(EFI_INCLUDES) +CFLAGS = -O2 -g -mno-red-zone -fno-stack-protector -fno-strict-aliasing -fpic -fshort-wchar -fno-merge-constants -Wall -Werror $(EFI_INCLUDES) ifeq ($(ARCH),x86_64) CFLAGS += -DEFI_FUNCTION_WRAPPER endif -LDFLAGS = -nostdlib -znocombreloc -T $(EFI_LDS) -shared -Bsymbolic -L$(EFI_PATH) $(EFI_CRT_OBJS) +LDFLAGS = -nostdlib -znocombreloc -T $(EFI_LDS) -shared -Bsymbolic -L$(EFI_LIB_PATH) $(EFI_CRT_OBJS) TARGET = bootx64.efi OBJS = bootx64.o + all: $(TARGET) bootx64.efi: $(OBJS) diff -r 30ba02c504a0 -r ac5d699b9787 boot/bootx64.c --- a/boot/bootx64.c Mon Jan 21 23:41:36 2013 +0900 +++ b/boot/bootx64.c Tue Jan 29 14:45:00 2013 +0900 @@ -1,33 +1,164 @@ -#include -#include -#include "mach-o/loader.h" +/* + * This program is EFI boot loader. + * load Mach-O kernel, and locate on memory, and jump kernel. + * + * Author: Taiki TAIRA. + */ + +#include "bootx64.h" + +INTN +efi_error(CHAR16* error_massage, EFI_STATUS status) +{ + Print(L"%s", error_massage); + if (status < 0) return ERROR; + return SUCCESS; +} + +EFI_STATUS +load_mach_o() +{ + Print(L"kernel load ... \n"); + return EFI_SUCCESS; +} + +EFI_STATUS +open(CHAR16 *name, UINTN *fd, fs_t *fs) +{ + EFI_STATUS status; + EFI_FILE_HANDLE fh; + + if (name == NULL || fd == NULL) return EFI_INVALID_PARAMETER; + + status = uefi_call_wrapper(fs->volume->Open, 5, fs->volume, &fh, name, EFI_FILE_MODE_READ, (UINT64)0); + if (status == EFI_SUCCESS) { + *fd = (UINTN)fh; + } + return status; +} + +EFI_STATUS +config_fs_one(EFI_HANDLE dev, VOID *fs) +{ + EFI_STATUS status; + EFI_FILE_IO_INTERFACE *volume; + EFI_FILE_HANDLE volume_fh; + EFI_GUID LocalFsProtocol = LOCALFS_PROTOCOL; + + status = uefi_call_wrapper(BS->HandleProtocol, 3, dev, &FileSystemProtocol, (VOID **)&volume); + if (EFI_ERROR(status)) return EFI_INVALID_PARAMETER; + + /* alloc */ + + status = uefi_call_wrapper(volume->OpenVolume, 2, volume, &volume_fh); + if (EFI_ERROR(status)) { + Print(L"Can not open volume.\n"); + return status; + } + Print(L"Open volume.\n"); + + fs_t *fs_tmp = (fs_t *)fs; + + SetMem(fs, sizeof(fs_t), 0); + + fs_tmp->dev = dev; + fs_tmp->volume = volume_fh; + + status = LibInstallProtocolInterfaces(&dev, &LocalFsProtocol, fs, NULL); + if (EFI_ERROR(status)) return status; + /* free */ + + return EFI_SUCCESS; +} + +EFI_STATUS +config_fs(EFI_HANDLE boot_handle, fs_t *fs, dev_tab_t *boot_dev) +{ + UINTN size = 0; + UINTN i; + EFI_GUID *proto = NULL; + + Print(L"configure filesystems for all volume. \n"); + + uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &FileSystemProtocol, NULL, &size, NULL); + if (size == 0) return EFI_UNSUPPORTED; + + /* alloc */ + + dev_tab_t *dev_tab = NULL; // all devices + EFI_STATUS status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &FileSystemProtocol, NULL, &size, (VOID **)dev_tab); + if (status != EFI_SUCCESS) { + efi_error(L"can't get handler.\n", status); + /* free */ + return status; + } + + UINTN ndev = size / sizeof(EFI_HANDLE); + + for (i = 0; i < ndev; i++) { + VOID *fs = NULL; + config_fs_one(dev_tab[i].dev, &fs); + dev_tab[i].fs = fs; + } + + status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, proto, NULL, &size, NULL); + if (EFI_ERROR(status)) { + Print(L"No useable filesystem found.\n"); + return status; + } + + SetMem(proto, sizeof(EFI_HANDLE), 0); + + uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &proto, NULL, &size, NULL); + + EFI_HANDLE *tab = NULL; + + SetMem(tab, size, 0); + + status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, proto, NULL, &size, tab); + if (status != EFI_SUCCESS) { + Print(L"faild to get handles\n"); + } + + size /= sizeof(EFI_HANDLE); + + UINTN idx = 0; + + for (i=0; iSetWatchdogTimer, 4, 0, 0x0, 0, NULL); + Print(L"Set watchdog timer.\n"); - status = uefi_call_wrapper( - system_table->BootServices->HandleProtocol, - 3, - image, - &loaded_image_protocol, - (void **) &loaded_image); - - if (EFI_ERROR(status)) { - Print(L"handleprotocol: %r\n", status); + EFI_STATUS status = uefi_call_wrapper(BS->HandleProtocol, 3, image, &LoadedImageProtocol, (VOID **) &info); + if (efi_error(L"Load error.\n", status)) { + return EFI_LOAD_ERROR; } - Print(L"Image base: %lx\n", loaded_image->ImageBase); - Print(L"Image size: %lx\n", loaded_image->ImageSize); - Print(L"Image file: %s\n", DevicePathToStr(loaded_image->FilePath)); -*/ - Print(L"Hello, World\n"); - return EFI_SUCCESS; + fs_t fs; + dev_tab_t boot_dev; + status = config_fs(info->DeviceHandle, &fs, &boot_dev); + + UINTN fd; + open(kname, &fd, &fs); + + status = EFI_SUCCESS; + return status; } diff -r 30ba02c504a0 -r ac5d699b9787 boot/bootx64.efi Binary file boot/bootx64.efi has changed