Received: from list by lists.gnu.org with archive (Exim 4.71) id 1cb85F-0006cJ-Vp for mharc-grub-devel@gnu.org; Tue, 07 Feb 2017 10:53:34 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43771) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cb85A-0006XG-2F for grub-devel@gnu.org; Tue, 07 Feb 2017 10:53:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cb854-0003Gl-48 for grub-devel@gnu.org; Tue, 07 Feb 2017 10:53:28 -0500 Received: from smtpbgsg2.qq.com ([54.254.200.128]:60840) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cb853-000395-CA for grub-devel@gnu.org; Tue, 07 Feb 2017 10:53:22 -0500 X-QQ-mid: bizesmtp16t1486482788thk23o7u Received: from heiher-pc.localdomain (unknown [222.92.8.142]) by esmtp4.qq.com (ESMTP) with id ; Tue, 07 Feb 2017 23:53:04 +0800 (CST) X-QQ-SSF: 01100000006000F0FF31B00A0000000 X-QQ-FEAT: PML9g4LxpBpHUszyw8btjyLAhmfvX/LQjrbDJonpvGgwU53ZYiP/VZmbGhSH4 E4XQCJh+3CYqsqi1K1ZYfs3aeYt5oUV6g5s/4e8v+/dH5X7bZO23Ncc2AJ4WVGMgXR719Uv 8x5XO1BqquNH8QFCDUMpBtq4it1TGgYOM9f8Ip/4OvU4SYF+ZQ29DHs3u9TazNQ7jvTMS31 reS5zamwoVKS+QlBoLL79V62RzmJFKOP7d9SbfJkks/FcJ/v4QyQfFD08H+8Xaw43AX4Oay BcIboJ6QXfnFfrj2tmCmQuP8w= X-QQ-GoodBg: 0 From: wangr@lemote.com To: grub-devel@gnu.org Cc: Heiher Subject: [PATCH 3/4] mips64: Implement linux loader. Date: Tue, 7 Feb 2017 23:52:07 +0800 Message-Id: <20170207155208.4905-3-wangr@lemote.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170207155208.4905-1-wangr@lemote.com> References: <20170207155208.4905-1-wangr@lemote.com> X-QQ-SENDSIZE: 520 X-QQ-Bgrelay: 1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 54.254.200.128 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Feb 2017 15:53:32 -0000 From: Heiher --- grub-core/Makefile.core.def | 1 + grub-core/kern/efi/mm.c | 4 + grub-core/loader/mips64/linux.c | 335 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 340 insertions(+) create mode 100644 grub-core/loader/mips64/linux.c diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index e1dc440ec..58009672b 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1680,6 +1680,7 @@ module = { xen = loader/i386/xen.c; i386_pc = lib/i386/pc/vesa_modes_table.c; mips = loader/mips/linux.c; + mips64 = loader/mips64/linux.c; powerpc_ieee1275 = loader/powerpc/ieee1275/linux.c; sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c; ia64_efi = loader/ia64/efi/linux.c; diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c index 20a47aaf5..aa7b05fdf 100644 --- a/grub-core/kern/efi/mm.c +++ b/grub-core/kern/efi/mm.c @@ -494,7 +494,11 @@ grub_efi_mm_init (void) /* By default, request a quarter of the available memory. */ total_pages = get_total_pages (filtered_memory_map, desc_size, filtered_memory_map_end); +#if defined (__mips__) && (_MIPS_SIM == _ABI64) + required_pages = (total_pages >> 1); +#else required_pages = (total_pages >> 2); +#endif if (required_pages < BYTES_TO_PAGES (MIN_HEAP_SIZE)) required_pages = BYTES_TO_PAGES (MIN_HEAP_SIZE); else if (required_pages > BYTES_TO_PAGES (MAX_HEAP_SIZE)) diff --git a/grub-core/loader/mips64/linux.c b/grub-core/loader/mips64/linux.c new file mode 100644 index 000000000..84f9f2f81 --- /dev/null +++ b/grub-core/loader/mips64/linux.c @@ -0,0 +1,335 @@ +/* linux.c - boot Linux */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2003,2004,2005,2007,2009,2010,2017 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +#pragma GCC diagnostic ignored "-Wcast-align" + +static grub_dl_t my_mod; + +static int loaded; + +static grub_size_t linux_size; + +static struct grub_relocator *relocator; +static grub_addr_t target_addr, entry_addr; +static int linux_argc; +static grub_uint8_t *linux_args_addr; +static grub_off_t rd_addr_arg_off, rd_size_arg_off; +static int initrd_loaded = 0; + +static grub_err_t +grub_linux_boot (void) +{ + struct grub_relocator64_state state; + + grub_memset (&state, 0, sizeof (state)); + + /* Boot the kernel. */ + state.gpr[1] = entry_addr; + + state.gpr[4] = linux_argc; + state.gpr[5] = (grub_addr_t) linux_args_addr; + state.jumpreg = 1; + grub_relocator64_boot (relocator, state); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_linux_unload (void) +{ + grub_relocator_unload (relocator); + grub_dl_unref (my_mod); + + loaded = 0; + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_linux_load32 (grub_elf_t elf, const char *filename) +{ + Elf32_Addr base; + grub_err_t err; + grub_uint8_t *playground; + + /* Linux's entry point incorrectly contains a virtual address. */ + entry_addr = elf->ehdr.ehdr32.e_entry; + + linux_size = grub_elf32_size (elf, &base, 0); + if (linux_size == 0) + return grub_errno; + target_addr = base; + linux_size = ALIGN_UP (base + linux_size - base, 8); + + relocator = grub_relocator_new (); + if (!relocator) + return grub_errno; + + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + grub_vtop ((void *) target_addr), + linux_size); + if (err) + return err; + playground = get_virtual_current_address (ch); + } + + /* Now load the segments into the area we claimed. */ + return grub_elf32_load (elf, filename, playground - base, GRUB_ELF_LOAD_FLAGS_NONE, 0, 0); +} + +static grub_err_t +grub_linux_load64 (grub_elf_t elf, const char *filename) +{ + Elf64_Addr base; + grub_err_t err; + grub_uint8_t *playground; + + /* Linux's entry point incorrectly contains a virtual address. */ + entry_addr = elf->ehdr.ehdr64.e_entry; + + linux_size = grub_elf64_size (elf, &base, 0); + if (linux_size == 0) + return grub_errno; + target_addr = base; + linux_size = ALIGN_UP (base + linux_size - base, 8); + + relocator = grub_relocator_new (); + if (!relocator) + return grub_errno; + + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + grub_vtop ((void *) target_addr), + linux_size); + if (err) + return err; + playground = get_virtual_current_address (ch); + } + + /* Now load the segments into the area we claimed. */ + return grub_elf64_load (elf, filename, playground - base, GRUB_ELF_LOAD_FLAGS_NONE, 0, 0); +} + +static grub_err_t +grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), + int argc, char *argv[]) +{ + grub_elf_t elf = 0; + int size; + int i; + grub_uint32_t *linux_argv; + char *linux_args; + grub_err_t err; + + if (argc == 0) + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); + + elf = grub_elf_open (argv[0]); + if (! elf) + return grub_errno; + + if (elf->ehdr.ehdr32.e_type != ET_EXEC) + { + grub_elf_close (elf); + return grub_error (GRUB_ERR_UNKNOWN_OS, + N_("this ELF file is not of the right type")); + } + + /* Release the previously used memory. */ + grub_loader_unset (); + loaded = 0; + + /* For arguments. */ + linux_argc = argc; + /* Main arguments. */ + size = (linux_argc) * sizeof (grub_uint32_t); + /* Initrd address and size. */ + size += 2 * sizeof (grub_uint32_t); + /* NULL terminator. */ + size += sizeof (grub_uint32_t); + + /* First argument is always "a0". */ + size += ALIGN_UP (sizeof ("a0"), 4); + /* Normal arguments. */ + for (i = 1; i < argc; i++) + size += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); + + /* rd arguments. */ + size += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); + size += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); + + size = ALIGN_UP (size, 8); + + if (grub_elf_is_elf32 (elf)) + err = grub_linux_load32 (elf, argv[0]); + else + if (grub_elf_is_elf64 (elf)) + err = grub_linux_load64 (elf, argv[0]); + else + err = grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic")); + + grub_elf_close (elf); + + if (err) + return err; + + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_align (relocator, &ch, + 0, (0xffffffff - size) + 1, + size, 8, + GRUB_RELOCATOR_PREFERENCE_HIGH, 0); + if (err) + return err; + linux_args_addr = get_virtual_current_address (ch); + } + + linux_argv = (grub_uint32_t *) linux_args_addr; + linux_args = (char *) (linux_argv + (linux_argc + 1 + 2)); + + grub_memcpy (linux_args, "a0", sizeof ("a0")); + *linux_argv = (grub_uint32_t) (grub_addr_t) linux_args; + linux_argv++; + linux_args += ALIGN_UP (sizeof ("a0"), 4); + + for (i = 1; i < argc; i++) + { + grub_memcpy (linux_args, argv[i], grub_strlen (argv[i]) + 1); + *linux_argv = (grub_uint32_t) (grub_addr_t) linux_args; + linux_argv++; + linux_args += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); + } + + /* Reserve space for rd arguments. */ + rd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; + linux_args += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); + *linux_argv = 0; + linux_argv++; + + rd_size_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; + linux_args += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); + *linux_argv = 0; + linux_argv++; + + *linux_argv = 0; + + grub_loader_set (grub_linux_boot, grub_linux_unload, GRUB_LOADER_FLAG_NORETURN); + initrd_loaded = 0; + loaded = 1; + grub_dl_ref (my_mod); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), + int argc, char *argv[]) +{ + grub_size_t size = 0; + void *initrd_dest; + grub_err_t err; + struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 }; + + if (argc == 0) + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); + + if (!loaded) + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first")); + + if (initrd_loaded) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "only one initrd command can be issued."); + + if (grub_initrd_init (argc, argv, &initrd_ctx)) + goto fail; + + size = grub_get_initrd_size (&initrd_ctx); + + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_align (relocator, &ch, + 0, (0xffffffff - size) + 1, + size, 0x10000, + GRUB_RELOCATOR_PREFERENCE_HIGH, 0); + + if (err) + goto fail; + initrd_dest = get_virtual_current_address (ch); + } + + if (grub_initrd_load (&initrd_ctx, argv, initrd_dest)) + goto fail; + + grub_snprintf ((char *) linux_args_addr + rd_addr_arg_off, + sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), "rd_start=0x%lx", + (grub_uint64_t) initrd_dest); + ((grub_uint32_t *) linux_args_addr)[linux_argc] + = (grub_uint32_t) ((grub_addr_t) linux_args_addr + rd_addr_arg_off); + linux_argc++; + + grub_snprintf ((char *) linux_args_addr + rd_size_arg_off, + sizeof ("rd_size=0xXXXXXXXXXXXXXXXXX"), "rd_size=0x%lx", + (grub_uint64_t) size); + ((grub_uint32_t *) linux_args_addr)[linux_argc] + = (grub_uint32_t) ((grub_addr_t) linux_args_addr + rd_size_arg_off); + linux_argc++; + + initrd_loaded = 1; + + fail: + grub_initrd_close (&initrd_ctx); + + return grub_errno; +} + +static grub_command_t cmd_linux, cmd_initrd; + +GRUB_MOD_INIT(linux) +{ + cmd_linux = grub_register_command ("linux", grub_cmd_linux, + 0, N_("Load Linux.")); + cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, + 0, N_("Load initrd.")); + my_mod = mod; +} + +GRUB_MOD_FINI(linux) +{ + grub_unregister_command (cmd_linux); + grub_unregister_command (cmd_initrd); +} -- 2.11.1