1
/* linux.c - boot Linux zImage or bzImage */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,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/>.
20
#include <grub/loader.h>
21
#include <grub/machine/loader.h>
22
#include <grub/machine/memory.h>
23
#include <grub/file.h>
25
#include <grub/disk.h>
26
#include <grub/misc.h>
27
#include <grub/types.h>
31
#include <grub/term.h>
32
#include <grub/cpu/linux.h>
33
#include <grub/ieee1275/ieee1275.h>
34
#include <grub/command.h>
35
#include <grub/i18n.h>
37
#define GRUB_OFW_LINUX_PARAMS_ADDR 0x90000
38
#define GRUB_OFW_LINUX_KERNEL_ADDR 0x100000
39
#define GRUB_OFW_LINUX_INITRD_ADDR 0x800000
41
#define GRUB_OFW_LINUX_CL_OFFSET 0x1e00
42
#define GRUB_OFW_LINUX_CL_LENGTH 0x100
44
static grub_dl_t my_mod;
46
static grub_size_t kernel_size;
47
static char *kernel_addr, *kernel_cmdline;
48
static grub_size_t initrd_size;
51
grub_linux_unload (void)
53
grub_free (kernel_cmdline);
54
grub_free (kernel_addr);
59
grub_dl_unref (my_mod);
66
grub_ieee1275_debug (void)
70
struct grub_ieee1275_common_hdr common;
74
INIT_IEEE1275_COMMON (&args.common, "enter", 0, 0);
76
if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
84
grub_linux_boot (void)
86
struct linux_kernel_params *params;
87
struct linux_kernel_header *lh;
92
bootpath = grub_env_get ("root");
94
grub_ieee1275_set_property (grub_ieee1275_chosen,
96
grub_strlen (bootpath) + 1,
99
params = (struct linux_kernel_params *) GRUB_OFW_LINUX_PARAMS_ADDR;
100
lh = (struct linux_kernel_header *) params;
102
grub_memset ((char *) params, 0, GRUB_OFW_LINUX_CL_OFFSET);
104
params->alt_mem = grub_mmap_get_upper () >> 10;
105
params->ext_mem = params->alt_mem;
107
lh->cmd_line_ptr = (char *)
108
(GRUB_OFW_LINUX_PARAMS_ADDR + GRUB_OFW_LINUX_CL_OFFSET);
110
params->cl_magic = GRUB_LINUX_CL_MAGIC;
111
params->cl_offset = GRUB_OFW_LINUX_CL_OFFSET;
114
grub_term_output_t term;
116
FOR_ACTIVE_TERM_OUTPUTS(term)
117
if (grub_strcmp (term->name, "ofconsole") == 0)
119
grub_uint16_t pos = grub_term_getxy (term);
120
params->video_cursor_x = pos >> 8;
121
params->video_cursor_y = pos & 0xff;
122
params->video_width = grub_term_width (term);
123
params->video_height = grub_term_height (term);
129
params->video_cursor_x = 0;
130
params->video_cursor_y = 0;
131
params->video_width = 80;
132
params->video_height = 25;
136
params->font_size = 16;
138
params->ofw_signature = GRUB_LINUX_OFW_SIGNATURE;
139
params->ofw_num_items = 1;
140
params->ofw_cif_handler = (grub_uint32_t) grub_ieee1275_entry_fn;
145
lh->type_of_loader = 1;
146
lh->ramdisk_image = GRUB_OFW_LINUX_INITRD_ADDR;
147
lh->ramdisk_size = initrd_size;
151
grub_strcpy (lh->cmd_line_ptr, kernel_cmdline);
153
prot_code = (char *) GRUB_OFW_LINUX_KERNEL_ADDR;
154
grub_memcpy (prot_code, kernel_addr, kernel_size);
156
asm volatile ("movl %0, %%esi" : : "m" (params));
157
asm volatile ("movl %%esi, %%esp" : : );
158
asm volatile ("movl %0, %%ecx" : : "m" (prot_code));
159
asm volatile ("xorl %%ebx, %%ebx" : : );
160
asm volatile ("jmp *%%ecx" : : );
162
return GRUB_ERR_NONE;
166
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
167
int argc, char *argv[])
169
grub_file_t file = 0;
170
struct linux_kernel_header lh;
171
grub_uint8_t setup_sects;
172
grub_size_t real_size, prot_size;
176
grub_dl_ref (my_mod);
180
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
184
file = grub_file_open (argv[0]);
188
if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
190
grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header");
194
if ((lh.boot_flag != grub_cpu_to_le16 (0xaa55)) ||
195
(lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)))
197
grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
201
setup_sects = lh.setup_sects;
203
setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
205
real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
206
prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
208
grub_dprintf ("linux", "Linux-%s, setup=0x%x, size=0x%x\n",
209
"bzImage", real_size, prot_size);
211
grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
215
kernel_cmdline = grub_malloc (GRUB_OFW_LINUX_CL_LENGTH);
216
if (! kernel_cmdline)
219
dest = kernel_cmdline;
222
&& dest + grub_strlen (argv[i]) + 1 < (kernel_cmdline
223
+ GRUB_OFW_LINUX_CL_LENGTH);
227
dest = grub_stpcpy (dest, argv[i]);
230
kernel_addr = grub_malloc (prot_size);
234
kernel_size = prot_size;
235
if (grub_file_read (file, kernel_addr, prot_size) != (int) prot_size)
236
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
238
if (grub_errno == GRUB_ERR_NONE)
239
grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
244
grub_file_close (file);
246
if (grub_errno != GRUB_ERR_NONE)
248
grub_free (kernel_cmdline);
249
grub_free (kernel_addr);
253
grub_dl_unref (my_mod);
260
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
261
int argc, char *argv[])
263
grub_file_t file = 0;
267
grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
273
grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
277
file = grub_file_open (argv[0]);
281
initrd_size = grub_file_size (file);
282
if (grub_file_read (file, (void *) GRUB_OFW_LINUX_INITRD_ADDR,
283
initrd_size) != (int) initrd_size)
285
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
291
grub_file_close (file);
296
static grub_command_t cmd_linux, cmd_initrd;
300
cmd_linux = grub_register_command ("linux", grub_cmd_linux,
301
0, N_("Load Linux."));
302
cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
303
0, N_("Load initrd."));
309
grub_unregister_command (cmd_linux);
310
grub_unregister_command (cmd_initrd);