From 6e845dad42466014edf06ecfe07153ac2880d711 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Fri, 6 Jul 2018 09:17:20 +0900 Subject: [PATCH] init efi boot work --- .gitignore | 1 + Makefile | 27 +++++- boot/efi.c | 208 +++++++++++++++++++++++++++++++++++++++++++ boot/jmp.s | 10 +++ boot/moremultiboot.h | 11 +++ 5 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 boot/efi.c create mode 100644 boot/jmp.s diff --git a/.gitignore b/.gitignore index de4ca1bf..91aefb75 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ cdrom/netinit cdrom/mod/* cdrom/ramdisk.img cdrom/boot/boot.sys +cdrom/boot/boot.efi util/tarballs util/build util/local diff --git a/Makefile b/Makefile index 9880e767..9fc5a863 100644 --- a/Makefile +++ b/Makefile @@ -174,11 +174,31 @@ cdrom/ramdisk.img: ${APPS_X} ${LIBS_X} base/lib/ld.so base/lib/libm.so $(shell f # CD image image.iso: cdrom/ramdisk.img cdrom/boot/boot.sys cdrom/kernel cdrom/netinit ${MODULES} - xorriso -as mkisofs -R -J -c boot/bootcat -b boot/boot.sys -no-emul-boot -boot-load-size 20 -o image.iso cdrom + xorriso -as mkisofs -R -J -c boot/bootcat \ + -b boot/boot.sys -no-emul-boot -boot-load-size 20 \ + -eltorito-alt-boot -e boot/boot.efi -no-emul-boot \ + -isohybrid-gpt-basdat \ + -o image.iso cdrom # Boot loader -cdrom/boot/boot.sys: boot/boot.o boot/cstuff.o boot/link.ld | cdrom/boot +cdrom/boot/boot.efi: boot/efi.c boot/jmp.o + $(CC) -c -fno-stack-protector -fpic -fshort-wchar -I /usr/include/efi -I /usr/include/efi/ia32 -DEFI_FUNCTION_WRAPPER -o boot/efi.o $< + $(LD) boot/efi.o boot/jmp.o /usr/lib32/crt0-efi-ia32.o -nostdlib -znocombreloc -T /usr/lib32/elf_ia32_efi.lds -shared -Bsymbolic -L /usr/lib32 -l:libgnuefi.a -l:libefi.a -o boot/efi.so + objcopy \ + -j .text \ + -j .sdata \ + -j .data \ + -j .dynamic \ + -j .dynsym \ + -j .rel \ + -j .rela \ + -j .reloc \ + --target=efi-app-ia32 \ + boot/efi.so \ + cdrom/boot/boot.efi + +cdrom/boot/boot.sys: boot/boot.o boot/cstuff.o boot/link.ld cdrom/boot/boot.efi | cdrom/boot ${KLD} -T boot/link.ld -o $@ boot/boot.o boot/cstuff.o boot/cstuff.o: boot/cstuff.c boot/*.h @@ -187,6 +207,9 @@ boot/cstuff.o: boot/cstuff.c boot/*.h boot/boot.o: boot/boot.s yasm -f elf -o $@ $< +boot/jmp.o: boot/jmp.s + yasm -f elf -o $@ $< + .PHONY: clean clean: rm -f base/lib/*.so diff --git a/boot/efi.c b/boot/efi.c new file mode 100644 index 00000000..d62e67f8 --- /dev/null +++ b/boot/efi.c @@ -0,0 +1,208 @@ +#include +#include + +#include "ata.h" +#include "text.h" +#include "util.h" +#include "atapi_imp.h" +#include "iso9660.h" +#include "elf.h" +#include "multiboot.h" +#include "kbd.h" +#include "options.h" + +/* Basic text strings */ +#define VERSION_TEXT "ToaruOS-NIH Bootloader v1.3" +#define HELP_TEXT "Press or select a menu option with \030/\031/\032/\033." +#define COPYRIGHT_TEXT "ToaruOS is free software under the NCSA license." +#define LINK_TEXT "https://toaruos.org - https://gitlab.com/toaruos" + +/* Boot command line strings */ +#define DEFAULT_ROOT_CMDLINE "root=/dev/ram0,nocache " +#define DEFAULT_GRAPHICAL_CMDLINE "start=live-session " +#define DEFAULT_SINGLE_CMDLINE "start=terminal " +#define DEFAULT_TEXT_CMDLINE "start=--vga " +#define DEFAULT_VID_CMDLINE "vid=auto,1440,900 " +#define DEFAULT_NETINIT_CMDLINE "init=/dev/ram0 _" +#define MIGRATE_CMDLINE "start=--migrate _" +#define DEBUG_LOG_CMDLINE "logtoserial=3 " +#define DEBUG_SERIAL_CMDLINE "kdebug " + +char * module_dir = "MOD"; +char * kernel_path = "KERNEL."; +char * ramdisk_path = "RAMDISK.IMG"; + +/* Where to dump kernel data while loading */ +#define KERNEL_LOAD_START 0x300000 + +/* Module file names - need to be ordered. */ +static char * modules[] = { + "ZERO.KO", // 0 + "RANDOM.KO", // 1 + "SERIAL.KO", // 2 + "DEBUG_SH.KO", // 3 + "PROCFS.KO", // 4 + "TMPFS.KO", // 5 + "ATA.KO", // 6 + "EXT2.KO", // 7 + "ISO9660.KO", // 8 + "PS2KBD.KO", // 9 + "PS2MOUSE.KO", // 10 + "LFBVIDEO.KO", // 11 + "VBOXGUES.KO", // 12 + "VMWARE.KO", // 13 + "VIDSET.KO", // 14 + "PACKETFS.KO", // 15 + "SND.KO", // 16 + "AC97.KO", // 17 + "NET.KO", // 18 + "PCNET.KO", // 19 + "RTL.KO", // 20 + "E1000.KO", // 21 + "PCSPKR.KO", // 22 + 0 +}; + +/* Names of the available boot modes. */ +static char * boot_mode_names[] = { + "Normal Boot", + "VGA Text Mode", + "Single-User Graphical Terminal", +}; + +/* More bootloader implementation that depends on the module config */ +#include "moremultiboot.h" + +EFI_STATUS + EFIAPI +efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) +{ + InitializeLib(ImageHandle, SystemTable); + Print(L"Hello, world!\n"); + BOOT_OPTION(_debug, 0, "Debug output", + "Enable debug output in the bootloader and enable the", + "serial debug log in the operating system itself."); + + BOOT_OPTION(_legacy_ata, 0, "Legacy ATA driver", + "Enable the legacy ATA driver, which does not support", + "ATAPI or use DMA. May be necessary in some virtual machines."); + + BOOT_OPTION(_normal_ata, 1, "DMA ATA driver", + "Enable the normal, DMA-capable ATA driver. This is the default.", + NULL); + + BOOT_OPTION(_debug_shell, 1, "Debug shell", + "Enable the kernel debug shell. This can be accessed using", + "the `kdebug` application."); + + BOOT_OPTION(_video, 1, "Video modules", + "Enable the video modules. These are needed to modeset", + "and provide a framebuffer for the UI."); + + BOOT_OPTION(_vbox, 1, "VirtualBox Guest Additions", + "Enable integration with VirtualBox, including", + "automatic mode setting and absolute mouse pointer."); + + BOOT_OPTION(_vmware, 1, "VMWare mouse driver", + "Enable the VMware / QEMU absolute mouse pointer.", + NULL); + + BOOT_OPTION(_sound, 1, "Audio drivers", + "Enable the audio subsystem and AC'97 drivers.", + NULL); + + BOOT_OPTION(_net, 1, "Network drivers", + "Enable the IPv4 network subsystem and various", + "network interface drivers."); + + BOOT_OPTION(_migrate, 1, "Writable root", + "Migrates the ramdisk from ext2 to an in-memory", + "temporary filesystem at boot."); + + BOOT_OPTION(_serialshell, 0, "Debug on serial", + "Start a kernel debug shell on the first", + "serial port."); + + BOOT_OPTION(_netinit, 0, "Netinit", + "Downloads a userspace filesystem from a remote", + "server and extracts it at boot."); + + Print(L"Boot options loaded.\n"); + + /* Loop over rendering the menu */ + show_menu(); + + /* Build our command line. */ + if (_netinit) { + strcat(cmdline, DEFAULT_NETINIT_CMDLINE); + ramdisk_path = "NETINIT."; + } else { + strcat(cmdline, DEFAULT_ROOT_CMDLINE); + + if (_migrate) { + strcat(cmdline, MIGRATE_CMDLINE); + } + } + + if (boot_mode == 0) { + strcat(cmdline, DEFAULT_GRAPHICAL_CMDLINE); + strcat(cmdline, DEFAULT_VID_CMDLINE); + } else if (boot_mode == 1) { + strcat(cmdline, DEFAULT_TEXT_CMDLINE); + } else if (boot_mode == 2) { + strcat(cmdline, DEFAULT_SINGLE_CMDLINE); + strcat(cmdline, DEFAULT_VID_CMDLINE); + } + + if (_debug) { + strcat(cmdline, DEBUG_LOG_CMDLINE); + } + + if (_serialshell) { + strcat(cmdline, DEBUG_SERIAL_CMDLINE); + } + + /* Configure modules */ + if (!_normal_ata) { + modules[6] = "NONE"; + } + + if (_legacy_ata) { + modules[6] = "ATAOLD.KO"; + } + + if (!_debug_shell) { + modules[3] = "NONE"; + modules[14] = "NONE"; + } + + if (!_video) { + modules[11] = "NONE"; + modules[12] = "NONE"; + modules[13] = "NONE"; + modules[14] = "NONE"; + } + + if (!_vmware) { + modules[13] = "NONE"; + } + + if (!_vbox) { + modules[12] = "NONE"; + } + + if (!_sound) { + modules[16] = "NONE"; + modules[17] = "NONE"; + } + + if (!_net) { + modules[18] = "NONE"; + modules[19] = "NONE"; + modules[20] = "NONE"; + modules[21] = "NONE"; + } + + boot(); + return EFI_SUCCESS; +} diff --git a/boot/jmp.s b/boot/jmp.s new file mode 100644 index 00000000..da131e1f --- /dev/null +++ b/boot/jmp.s @@ -0,0 +1,10 @@ +[bits 32] +global jump_to_main +jump_to_main: + extern _eax + extern _ebx + extern _xmain + mov eax, [_eax] + mov ebx, [_ebx] + jmp [_xmain] + diff --git a/boot/moremultiboot.h b/boot/moremultiboot.h index aaa72605..44465b75 100644 --- a/boot/moremultiboot.h +++ b/boot/moremultiboot.h @@ -222,6 +222,7 @@ done: static int boot_mode = 0; +#ifndef EFI_FUNCTION_WRAPPER void show_menu(void) { /* Determine number of options */ sel_max = 0; @@ -353,3 +354,13 @@ static void boot(void) { while (1); } +#else + +void show_menu(void) { + Print(L"lol menu goes here\n"); +} + +static void boot(void) { + Print(L"Boop.\n"); +} +#endif