1
/* linux.c - boot Linux */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2003,2004,2005,2007,2009 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21
#include <grub/elfload.h>
22
#include <grub/loader.h>
25
#include <grub/misc.h>
26
#include <grub/ieee1275/ieee1275.h>
27
#include <grub/command.h>
28
#include <grub/i18n.h>
29
#include <grub/memory.h>
31
#define ELF32_LOADMASK (0xc0000000UL)
32
#define ELF64_LOADMASK (0xc000000000000000ULL)
34
static grub_dl_t my_mod;
38
static grub_addr_t initrd_addr;
39
static grub_size_t initrd_size;
41
static grub_addr_t linux_addr;
42
static grub_size_t linux_size;
44
static char *linux_args;
46
typedef void (*kernel_entry_t) (void *, unsigned long, int (void *),
47
unsigned long, unsigned long);
50
grub_linux_claimmap_iterate (grub_addr_t target, grub_size_t size,
53
grub_addr_t found_addr = (grub_addr_t) -1;
55
auto int NESTED_FUNC_ATTR alloc_mem (grub_uint64_t addr, grub_uint64_t len,
56
grub_memory_type_t type);
57
int NESTED_FUNC_ATTR alloc_mem (grub_uint64_t addr, grub_uint64_t len,
58
grub_memory_type_t type)
60
grub_uint64_t end = addr + len;
61
addr = ALIGN_UP (addr, align);
62
target = ALIGN_UP (target, align);
64
/* Target above the memory chunk. */
65
if (type != GRUB_MEMORY_AVAILABLE || target > end)
68
/* Target inside the memory chunk. */
69
if (target >= addr && target < end && size <= end - target)
71
if (grub_claimmap (target, size) == GRUB_ERR_NONE)
77
/* Target below the memory chunk. */
78
if (target < addr && addr + size <= end)
80
if (grub_claimmap (addr, size) == GRUB_ERR_NONE)
89
grub_machine_mmap_iterate (alloc_mem);
95
grub_linux_boot (void)
97
kernel_entry_t linuxmain;
100
/* Set the command line arguments. */
101
grub_ieee1275_set_property (grub_ieee1275_chosen, "bootargs", linux_args,
102
grub_strlen (linux_args) + 1, &actual);
104
grub_dprintf ("loader", "Entry point: 0x%x\n", linux_addr);
105
grub_dprintf ("loader", "Initrd at: 0x%x, size 0x%x\n", initrd_addr,
107
grub_dprintf ("loader", "Boot arguments: %s\n", linux_args);
108
grub_dprintf ("loader", "Jumping to Linux...\n");
110
/* Boot the kernel. */
111
linuxmain = (kernel_entry_t) linux_addr;
112
linuxmain ((void *) initrd_addr, initrd_size, grub_ieee1275_entry_fn, 0, 0);
114
return GRUB_ERR_NONE;
118
grub_linux_release_mem (void)
120
grub_free (linux_args);
123
if (linux_addr && grub_ieee1275_release (linux_addr, linux_size))
124
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot release memory");
126
if (initrd_addr && grub_ieee1275_release (initrd_addr, initrd_size))
127
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot release memory");
132
return GRUB_ERR_NONE;
136
grub_linux_unload (void)
140
err = grub_linux_release_mem ();
141
grub_dl_unref (my_mod);
149
grub_linux_load32 (grub_elf_t elf)
151
Elf32_Addr base_addr;
152
grub_addr_t seg_addr;
156
linux_size = grub_elf32_size (elf, &base_addr, &align);
159
/* Pad it; the kernel scribbles over memory beyond its load address. */
160
linux_size += 0x100000;
162
offset = elf->ehdr.ehdr32.e_entry - base_addr;
163
/* Linux's incorrectly contains a virtual address. */
164
base_addr &= ~ELF32_LOADMASK;
166
/* On some systems, firmware occupies the memory we're trying to use.
167
* Happily, Linux can be loaded anywhere (it relocates itself). Iterate
168
* until we find an open area. */
169
seg_addr = grub_linux_claimmap_iterate (base_addr & ~ELF32_LOADMASK, linux_size, align);
170
if (seg_addr == (grub_addr_t) -1)
171
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory");
173
linux_addr = seg_addr + offset;
175
/* Now load the segments into the area we claimed. */
176
auto grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load);
177
grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load)
179
if (phdr->p_type != PT_LOAD)
186
*addr = (phdr->p_paddr - base_addr) + seg_addr;
189
return grub_elf32_load (elf, offset_phdr, 0, 0);
193
grub_linux_load64 (grub_elf_t elf)
195
Elf64_Addr base_addr;
196
grub_addr_t seg_addr;
200
linux_size = grub_elf64_size (elf, &base_addr, &align);
203
/* Pad it; the kernel scribbles over memory beyond its load address. */
204
linux_size += 0x100000;
206
offset = elf->ehdr.ehdr64.e_entry - base_addr;
207
/* Linux's incorrectly contains a virtual address. */
208
base_addr &= ~ELF64_LOADMASK;
210
/* On some systems, firmware occupies the memory we're trying to use.
211
* Happily, Linux can be loaded anywhere (it relocates itself). Iterate
212
* until we find an open area. */
213
seg_addr = grub_linux_claimmap_iterate (base_addr & ~ELF64_LOADMASK, linux_size, align);
214
if (seg_addr == (grub_addr_t) -1)
215
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory");
217
linux_addr = seg_addr + offset;
219
/* Now load the segments into the area we claimed. */
220
auto grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load);
221
grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load)
223
if (phdr->p_type != PT_LOAD)
230
*addr = (phdr->p_paddr - base_addr) + seg_addr;
233
return grub_elf64_load (elf, offset_phdr, 0, 0);
237
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
238
int argc, char *argv[])
245
grub_dl_ref (my_mod);
249
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
253
elf = grub_elf_open (argv[0]);
257
if (elf->ehdr.ehdr32.e_type != ET_EXEC && elf->ehdr.ehdr32.e_type != ET_DYN)
259
grub_error (GRUB_ERR_UNKNOWN_OS,
260
"this ELF file is not of the right type");
264
/* Release the previously used memory. */
265
grub_loader_unset ();
267
if (grub_elf_is_elf32 (elf))
268
grub_linux_load32 (elf);
270
if (grub_elf_is_elf64 (elf))
271
grub_linux_load64 (elf);
274
grub_error (GRUB_ERR_BAD_FILE_TYPE, "unknown ELF class");
278
size = sizeof ("BOOT_IMAGE=") + grub_strlen (argv[0]);
279
for (i = 0; i < argc; i++)
280
size += grub_strlen (argv[i]) + 1;
282
linux_args = grub_malloc (size);
286
/* Specify the boot file. */
287
dest = grub_stpcpy (linux_args, "BOOT_IMAGE=");
288
dest = grub_stpcpy (dest, argv[0]);
290
for (i = 1; i < argc; i++)
293
dest = grub_stpcpy (dest, argv[i]);
299
grub_elf_close (elf);
301
if (grub_errno != GRUB_ERR_NONE)
303
grub_linux_release_mem ();
304
grub_dl_unref (my_mod);
309
grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
318
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
319
int argc, char *argv[])
321
grub_file_t file = 0;
323
grub_addr_t first_addr;
328
grub_error (GRUB_ERR_BAD_ARGUMENT, "no initrd specified");
334
grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
338
grub_file_filter_disable_compression ();
339
file = grub_file_open (argv[0]);
343
first_addr = linux_addr + linux_size;
344
size = grub_file_size (file);
346
/* Attempt to claim at a series of addresses until successful in
347
the same way that grub_rescue_cmd_linux does. */
348
addr = grub_linux_claimmap_iterate (first_addr, size, 0x100000);
349
if (addr == (grub_addr_t) -1)
352
grub_dprintf ("loader", "Loading initrd at 0x%x, size 0x%x\n", addr, size);
354
if (grub_file_read (file, (void *) addr, size) != size)
356
grub_ieee1275_release (addr, size);
357
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
366
grub_file_close (file);
371
static grub_command_t cmd_linux, cmd_initrd;
375
cmd_linux = grub_register_command ("linux", grub_cmd_linux,
376
0, N_("Load Linux."));
377
cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
378
0, N_("Load initrd."));
384
grub_unregister_command (cmd_linux);
385
grub_unregister_command (cmd_initrd);