2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2011 Free Software Foundation, Inc.
5
* GRUB is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* GRUB is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19
#include <grub/loader.h>
20
#include <grub/memory.h>
21
#include <grub/i386/memory.h>
22
#include <grub/file.h>
26
#include <grub/elfload.h>
27
#include <grub/video.h>
28
#include <grub/relocator.h>
29
#include <grub/i386/relocator.h>
30
#include <grub/command.h>
31
#include <grub/i18n.h>
33
GRUB_MOD_LICENSE ("GPLv3+");
35
static grub_addr_t entry;
36
static struct grub_relocator *relocator = NULL;
39
grub_chain_boot (void)
41
struct grub_relocator32_state state;
43
grub_video_set_mode ("text", 0, 0);
46
return grub_relocator32_boot (relocator, state, 0);
50
grub_chain_unload (void)
52
grub_relocator_unload (relocator);
59
grub_chain_elf32_hook (Elf32_Phdr * phdr, grub_addr_t * addr, int *do_load)
62
grub_relocator_chunk_t ch;
64
if (phdr->p_type != PT_LOAD)
71
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
72
phdr->p_paddr, phdr->p_memsz);
76
*addr = (grub_addr_t) get_virtual_current_address (ch);
83
grub_cmd_chain (grub_command_t cmd __attribute__ ((unused)),
84
int argc, char *argv[])
91
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
95
file = grub_file_open (argv[0]);
99
relocator = grub_relocator_new ();
102
grub_file_close (file);
106
elf = grub_elf_file (file, argv[0]);
109
grub_relocator_unload (relocator);
111
grub_file_close (file);
114
if (!grub_elf_is_elf32 (elf))
116
grub_relocator_unload (relocator);
118
grub_elf_close (elf);
121
entry = elf->ehdr.ehdr32.e_entry & 0xFFFFFF;
123
err = grub_elf32_load (elf, argv[0], grub_chain_elf32_hook, 0, 0);
125
grub_elf_close (elf);
129
grub_loader_set (grub_chain_boot, grub_chain_unload, 0);
130
return GRUB_ERR_NONE;
133
static grub_command_t cmd_chain;
135
GRUB_MOD_INIT (chain)
137
cmd_chain = grub_register_command ("chainloader", grub_cmd_chain,
139
/* TRANSLATORS: "payload" is a term used
140
by coreboot and must be translated in
141
sync with coreboot. If unsure,
142
let it untranslated. */
143
N_("Load another coreboot payload"));
146
GRUB_MOD_FINI (chain)
148
grub_unregister_command (cmd_chain);
149
grub_chain_unload ();