1
/* linux.c - boot Linux */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2003,2004,2005,2007,2009,2010 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/command.h>
27
#include <grub/mips/relocator.h>
28
#include <grub/memory.h>
29
#include <grub/i18n.h>
31
/* For frequencies. */
33
#include <grub/machine/time.h>
35
#ifdef GRUB_MACHINE_MIPS_YEELOONG
36
/* This can be detected on runtime from PMON, but:
37
a) it wouldn't work when GRUB is the firmware
39
b) for now we only support Yeeloong anyway. */
40
#define LOONGSON_MACHTYPE "machtype=lemote-yeeloong-2f-8.9inches"
43
static grub_dl_t my_mod;
47
static grub_size_t linux_size;
49
static struct grub_relocator *relocator;
50
static grub_uint8_t *playground;
51
static grub_addr_t target_addr, entry_addr;
52
static int linux_argc;
53
static grub_off_t argv_off, envp_off;
54
static grub_off_t rd_addr_arg_off, rd_size_arg_off;
55
static int initrd_loaded = 0;
58
grub_linux_boot (void)
60
struct grub_relocator32_state state;
62
/* Boot the kernel. */
63
state.gpr[1] = entry_addr;
64
state.gpr[4] = linux_argc;
65
state.gpr[5] = target_addr + argv_off;
66
state.gpr[6] = target_addr + envp_off;
68
grub_relocator32_boot (relocator, state);
74
grub_linux_unload (void)
76
grub_relocator_unload (relocator);
77
grub_dl_unref (my_mod);
85
grub_linux_load32 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size)
91
/* Linux's entry point incorrectly contains a virtual address. */
92
entry_addr = elf->ehdr.ehdr32.e_entry;
94
linux_size = grub_elf32_size (elf, &base, 0);
98
/* Pad it; the kernel scribbles over memory beyond its load address. */
99
linux_size += 0x100000;
100
linux_size = ALIGN_UP (base + linux_size, 4) - base;
101
extraoff = linux_size;
102
linux_size += extra_size;
104
relocator = grub_relocator_new ();
109
grub_relocator_chunk_t ch;
110
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
111
target_addr & 0x1fffffff,
115
playground = get_virtual_current_address (ch);
118
*extra_mem = playground + extraoff;
120
/* Now load the segments into the area we claimed. */
121
auto grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load);
122
grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load)
124
if (phdr->p_type != PT_LOAD)
131
/* Linux's program headers incorrectly contain virtual addresses.
132
* Translate those to physical, and offset to the area we claimed. */
133
*addr = (grub_addr_t) (phdr->p_paddr - base + playground);
136
return grub_elf32_load (elf, offset_phdr, 0, 0);
140
grub_linux_load64 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size)
146
/* Linux's entry point incorrectly contains a virtual address. */
147
entry_addr = elf->ehdr.ehdr64.e_entry;
149
linux_size = grub_elf64_size (elf, &base, 0);
153
/* Pad it; the kernel scribbles over memory beyond its load address. */
154
linux_size += 0x100000;
155
linux_size = ALIGN_UP (base + linux_size, 4) - base;
156
extraoff = linux_size;
157
linux_size += extra_size;
159
relocator = grub_relocator_new ();
164
grub_relocator_chunk_t ch;
165
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
166
target_addr & 0x1fffffff,
170
playground = get_virtual_current_address (ch);
173
*extra_mem = playground + extraoff;
175
/* Now load the segments into the area we claimed. */
176
auto grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load);
177
grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load)
179
if (phdr->p_type != PT_LOAD)
185
/* Linux's program headers incorrectly contain virtual addresses.
186
* Translate those to physical, and offset to the area we claimed. */
187
*addr = (grub_addr_t) (phdr->p_paddr - base + playground);
190
return grub_elf64_load (elf, offset_phdr, 0, 0);
194
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
195
int argc, char *argv[])
201
grub_uint32_t *linux_argv, *linux_envp;
202
char *linux_args, *linux_envs;
206
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
208
elf = grub_elf_open (argv[0]);
212
if (elf->ehdr.ehdr32.e_type != ET_EXEC)
214
grub_elf_close (elf);
215
return grub_error (GRUB_ERR_UNKNOWN_OS,
216
"this ELF file is not of the right type\n");
219
/* Release the previously used memory. */
220
grub_loader_unset ();
225
#ifdef LOONGSON_MACHTYPE
228
/* Main arguments. */
229
size = (linux_argc) * sizeof (grub_uint32_t);
230
/* Initrd address and size. */
231
size += 2 * sizeof (grub_uint32_t);
232
/* NULL terminator. */
233
size += sizeof (grub_uint32_t);
235
/* First argument is always "a0". */
236
size += ALIGN_UP (sizeof ("a0"), 4);
237
/* Normal arguments. */
238
for (i = 1; i < argc; i++)
239
size += ALIGN_UP (grub_strlen (argv[i]) + 1, 4);
240
#ifdef LOONGSON_MACHTYPE
241
size += ALIGN_UP (sizeof (LOONGSON_MACHTYPE), 4);
245
size += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4);
246
size += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4);
248
/* For the environment. */
249
size += sizeof (grub_uint32_t);
250
size += 4 * sizeof (grub_uint32_t);
251
size += ALIGN_UP (sizeof ("memsize=XXXXXXXXXXXXXXXXXXXX"), 4)
252
+ ALIGN_UP (sizeof ("highmemsize=XXXXXXXXXXXXXXXXXXXX"), 4)
253
+ ALIGN_UP (sizeof ("busclock=XXXXXXXXXX"), 4)
254
+ ALIGN_UP (sizeof ("cpuclock=XXXXXXXXXX"), 4);
256
if (grub_elf_is_elf32 (elf))
257
err = grub_linux_load32 (elf, &extra, size);
259
if (grub_elf_is_elf64 (elf))
260
err = grub_linux_load64 (elf, &extra, size);
262
err = grub_error (GRUB_ERR_BAD_FILE_TYPE, "unknown ELF class");
264
grub_elf_close (elf);
270
argv_off = (grub_uint8_t *) linux_argv - (grub_uint8_t *) playground;
271
extra = linux_argv + (linux_argc + 1 + 2);
274
grub_memcpy (linux_args, "a0", sizeof ("a0"));
275
*linux_argv = (grub_uint8_t *) linux_args - (grub_uint8_t *) playground
278
linux_args += ALIGN_UP (sizeof ("a0"), 4);
280
#ifdef LOONGSON_MACHTYPE
281
/* In Loongson platform, it is the responsibility of the bootloader/firmware
282
to supply the OS kernel with machine type information. */
283
grub_memcpy (linux_args, LOONGSON_MACHTYPE, sizeof (LOONGSON_MACHTYPE));
284
*linux_argv = (grub_uint8_t *) linux_args - (grub_uint8_t *) playground
287
linux_args += ALIGN_UP (sizeof (LOONGSON_MACHTYPE), 4);
290
for (i = 1; i < argc; i++)
292
grub_memcpy (linux_args, argv[i], grub_strlen (argv[i]) + 1);
293
*linux_argv = (grub_uint8_t *) linux_args - (grub_uint8_t *) playground
296
linux_args += ALIGN_UP (grub_strlen (argv[i]) + 1, 4);
299
/* Reserve space for rd arguments. */
300
rd_addr_arg_off = (grub_uint8_t *) linux_args - (grub_uint8_t *) playground;
301
linux_args += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4);
305
rd_size_arg_off = (grub_uint8_t *) linux_args - (grub_uint8_t *) playground;
306
linux_args += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4);
315
envp_off = (grub_uint8_t *) linux_envp - (grub_uint8_t *) playground;
316
linux_envs = (char *) (linux_envp + 5);
317
grub_snprintf (linux_envs, sizeof ("memsize=XXXXXXXXXXXXXXXXXXXX"),
319
(unsigned long long) grub_mmap_get_lower () >> 20);
320
linux_envp[0] = (grub_uint8_t *) linux_envs - (grub_uint8_t *) playground
322
linux_envs += ALIGN_UP (grub_strlen (linux_envs) + 1, 4);
323
grub_snprintf (linux_envs, sizeof ("highmemsize=XXXXXXXXXXXXXXXXXXXX"),
325
(unsigned long long) grub_mmap_get_upper () >> 20);
326
linux_envp[1] = (grub_uint8_t *) linux_envs - (grub_uint8_t *) playground
328
linux_envs += ALIGN_UP (grub_strlen (linux_envs) + 1, 4);
330
grub_snprintf (linux_envs, sizeof ("busclock=XXXXXXXXXX"),
331
"busclock=%d", grub_arch_busclock);
332
linux_envp[2] = (grub_uint8_t *) linux_envs - (grub_uint8_t *) playground
334
linux_envs += ALIGN_UP (grub_strlen (linux_envs) + 1, 4);
335
grub_snprintf (linux_envs, sizeof ("cpuclock=XXXXXXXXXX"),
336
"cpuclock=%d", grub_arch_cpuclock);
337
linux_envp[3] = (grub_uint8_t *) linux_envs - (grub_uint8_t *) playground
339
linux_envs += ALIGN_UP (grub_strlen (linux_envs) + 1, 4);
344
grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
347
grub_dl_ref (my_mod);
349
return GRUB_ERR_NONE;
353
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
354
int argc, char *argv[])
356
grub_file_t file = 0;
359
grub_addr_t initrd_dest;
363
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no initrd specified");
366
return grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load Linux first.");
369
return grub_error (GRUB_ERR_BAD_ARGUMENT, "only one initrd can be loaded.");
371
grub_file_filter_disable_compression ();
372
file = grub_file_open (argv[0]);
376
size = grub_file_size (file);
379
grub_relocator_chunk_t ch;
381
err = grub_relocator_alloc_chunk_align (relocator, &ch,
382
target_addr + linux_size + 0x10000,
383
(0xffffffff - size) + 1,
385
GRUB_RELOCATOR_PREFERENCE_NONE);
389
grub_file_close (file);
392
initrd_src = get_virtual_current_address (ch);
393
initrd_dest = get_physical_target_address (ch) | 0x80000000;
396
if (grub_file_read (file, initrd_src, size) != size)
398
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
399
grub_file_close (file);
404
grub_snprintf ((char *) playground + rd_addr_arg_off,
405
sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), "rd_start=0x%llx",
406
(unsigned long long) initrd_dest);
407
((grub_uint32_t *) (playground + argv_off))[linux_argc]
408
= target_addr + rd_addr_arg_off;
411
grub_snprintf ((char *) playground + rd_size_arg_off,
412
sizeof ("rd_size=0xXXXXXXXXXXXXXXXXX"), "rd_size=0x%llx",
413
(unsigned long long) size);
414
((grub_uint32_t *) (playground + argv_off))[linux_argc]
415
= target_addr + rd_size_arg_off;
420
grub_file_close (file);
422
return GRUB_ERR_NONE;
425
static grub_command_t cmd_linux, cmd_initrd;
429
cmd_linux = grub_register_command ("linux", grub_cmd_linux,
430
0, N_("Load Linux."));
431
cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
432
0, N_("Load initrd."));
438
grub_unregister_command (cmd_linux);
439
grub_unregister_command (cmd_initrd);