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/file.h>
24
#include <grub/device.h>
25
#include <grub/disk.h>
26
#include <grub/misc.h>
27
#include <grub/types.h>
28
#include <grub/memory.h>
30
#include <grub/cpu/linux.h>
31
#include <grub/command.h>
32
#include <grub/i18n.h>
34
#include <grub/cpu/relocator.h>
35
#include <grub/video.h>
36
#include <grub/i386/floppy.h>
38
#define GRUB_LINUX_CL_OFFSET 0x9000
39
#define GRUB_LINUX_CL_END_OFFSET 0x90FF
41
static grub_dl_t my_mod;
43
static grub_size_t linux_mem_size;
45
static struct grub_relocator *relocator = NULL;
46
static grub_addr_t grub_linux_real_target;
47
static char *grub_linux_real_chunk;
48
static grub_size_t grub_linux16_prot_size;
51
grub_linux16_boot (void)
53
grub_uint16_t segment;
54
struct grub_relocator16_state state;
56
segment = grub_linux_real_target >> 4;
57
state.gs = state.fs = state.es = state.ds = state.ss = segment;
58
state.sp = GRUB_LINUX_SETUP_STACK;
59
state.cs = segment + 0x20;
62
grub_video_set_mode ("text", 0, 0);
66
return grub_relocator16_boot (relocator, state);
70
grub_linux_unload (void)
72
grub_dl_unref (my_mod);
74
grub_relocator_unload (relocator);
80
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
81
int argc, char *argv[])
84
struct linux_kernel_header lh;
85
grub_uint8_t setup_sects;
86
grub_size_t real_size;
90
char *grub_linux_prot_chunk;
91
int grub_linux_is_bzimage;
92
grub_addr_t grub_linux_prot_target;
99
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
103
file = grub_file_open (argv[0]);
107
if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
109
grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header");
113
if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
115
grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
119
if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
121
grub_error (GRUB_ERR_BAD_OS, "too many setup sectors");
125
grub_linux_is_bzimage = 0;
126
setup_sects = lh.setup_sects;
129
if (lh.header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
130
&& grub_le_to_cpu16 (lh.version) >= 0x0200)
132
grub_linux_is_bzimage = (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL);
133
lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
135
/* Put the real mode part at as a high location as possible. */
136
grub_linux_real_target = grub_mmap_get_lower ()
137
- GRUB_LINUX_SETUP_MOVE_SIZE;
138
/* But it must not exceed the traditional area. */
139
if (grub_linux_real_target > GRUB_LINUX_OLD_REAL_MODE_ADDR)
140
grub_linux_real_target = GRUB_LINUX_OLD_REAL_MODE_ADDR;
142
if (grub_le_to_cpu16 (lh.version) >= 0x0201)
144
lh.heap_end_ptr = grub_cpu_to_le16 (GRUB_LINUX_HEAP_END_OFFSET);
145
lh.loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
148
if (grub_le_to_cpu16 (lh.version) >= 0x0202)
149
lh.cmd_line_ptr = grub_linux_real_target + GRUB_LINUX_CL_OFFSET;
152
lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC);
153
lh.cl_offset = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET);
154
lh.setup_move_size = grub_cpu_to_le16 (GRUB_LINUX_SETUP_MOVE_SIZE);
159
/* Your kernel is quite old... */
160
lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC);
161
lh.cl_offset = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET);
163
setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
165
grub_linux_real_target = GRUB_LINUX_OLD_REAL_MODE_ADDR;
168
/* If SETUP_SECTS is not set, set it to the default (4). */
170
setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
172
real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
173
grub_linux16_prot_size = grub_file_size (file)
174
- real_size - GRUB_DISK_SECTOR_SIZE;
176
if (! grub_linux_is_bzimage
177
&& GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size
178
> grub_linux_real_target)
180
grub_error (GRUB_ERR_BAD_OS, "too big zImage (0x%x > 0x%x), use bzImage instead",
181
(char *) GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size,
182
(grub_size_t) grub_linux_real_target);
186
if (grub_linux_real_target + GRUB_LINUX_SETUP_MOVE_SIZE
187
> grub_mmap_get_lower ())
189
grub_error (GRUB_ERR_OUT_OF_RANGE,
190
"too small lower memory (0x%x > 0x%x)",
191
grub_linux_real_target + GRUB_LINUX_SETUP_MOVE_SIZE,
192
(int) grub_mmap_get_lower ());
196
grub_printf (" [Linux-%s, setup=0x%x, size=0x%x]\n",
197
grub_linux_is_bzimage ? "bzImage" : "zImage", real_size,
198
grub_linux16_prot_size);
200
relocator = grub_relocator_new ();
204
for (i = 1; i < argc; i++)
205
if (grub_memcmp (argv[i], "vga=", 4) == 0)
207
/* Video mode selection support. */
208
grub_uint16_t vid_mode;
209
char *val = argv[i] + 4;
211
if (grub_strcmp (val, "normal") == 0)
212
vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
213
else if (grub_strcmp (val, "ext") == 0)
214
vid_mode = GRUB_LINUX_VID_MODE_EXTENDED;
215
else if (grub_strcmp (val, "ask") == 0)
216
vid_mode = GRUB_LINUX_VID_MODE_ASK;
218
vid_mode = (grub_uint16_t) grub_strtoul (val, 0, 0);
223
lh.vid_mode = grub_cpu_to_le16 (vid_mode);
225
else if (grub_memcmp (argv[i], "mem=", 4) == 0)
227
char *val = argv[i] + 4;
229
linux_mem_size = grub_strtoul (val, &val, 0);
233
grub_errno = GRUB_ERR_NONE;
240
switch (grub_tolower (val[0]))
252
/* Check an overflow. */
253
if (linux_mem_size > (~0UL >> shift))
256
linux_mem_size <<= shift;
261
grub_relocator_chunk_t ch;
262
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
263
grub_linux_real_target,
264
GRUB_LINUX_SETUP_MOVE_SIZE);
267
grub_linux_real_chunk = get_virtual_current_address (ch);
270
/* Put the real mode code at the temporary address. */
271
grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));
273
len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
274
if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len)
276
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
280
if (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
281
|| grub_le_to_cpu16 (lh.version) < 0x0200)
282
/* Clear the heap space. */
283
grub_memset (grub_linux_real_chunk
284
+ ((setup_sects + 1) << GRUB_DISK_SECTOR_BITS),
286
((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1)
287
<< GRUB_DISK_SECTOR_BITS));
289
/* Specify the boot file. */
290
dest = grub_stpcpy (grub_linux_real_chunk + GRUB_LINUX_CL_OFFSET,
292
dest = grub_stpcpy (dest, argv[0]);
294
/* Copy kernel parameters. */
297
&& dest + grub_strlen (argv[i]) + 1 < (grub_linux_real_chunk
298
+ GRUB_LINUX_CL_END_OFFSET);
302
dest = grub_stpcpy (dest, argv[i]);
305
if (grub_linux_is_bzimage)
306
grub_linux_prot_target = GRUB_LINUX_BZIMAGE_ADDR;
308
grub_linux_prot_target = GRUB_LINUX_ZIMAGE_ADDR;
310
grub_relocator_chunk_t ch;
311
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
312
grub_linux_prot_target,
313
grub_linux16_prot_size);
316
grub_linux_prot_chunk = get_virtual_current_address (ch);
319
len = grub_linux16_prot_size;
320
if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size)
321
!= (grub_ssize_t) grub_linux16_prot_size)
322
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
324
if (grub_errno == GRUB_ERR_NONE)
326
grub_loader_set (grub_linux16_boot, grub_linux_unload, 0);
333
grub_file_close (file);
335
if (grub_errno != GRUB_ERR_NONE)
337
grub_dl_unref (my_mod);
339
grub_relocator_unload (relocator);
346
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
347
int argc, char *argv[])
349
grub_file_t file = 0;
351
grub_addr_t addr_max, addr_min;
352
struct linux_kernel_header *lh;
353
grub_uint8_t *initrd_chunk;
354
grub_addr_t initrd_addr;
359
grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
365
grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
369
lh = (struct linux_kernel_header *) grub_linux_real_chunk;
371
if (!(lh->header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
372
&& grub_le_to_cpu16 (lh->version) >= 0x0200))
374
grub_error (GRUB_ERR_BAD_OS, "the kernel is too old for initrd");
378
/* Get the highest address available for the initrd. */
379
if (grub_le_to_cpu16 (lh->version) >= 0x0203)
381
addr_max = grub_cpu_to_le32 (lh->initrd_addr_max);
383
/* XXX in reality, Linux specifies a bogus value, so
384
it is necessary to make sure that ADDR_MAX does not exceed
386
if (addr_max > GRUB_LINUX_INITRD_MAX_ADDRESS)
387
addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
390
addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
392
if (linux_mem_size != 0 && linux_mem_size < addr_max)
393
addr_max = linux_mem_size;
395
/* Linux 2.3.xx has a bug in the memory range check, so avoid
397
Linux 2.2.xx has a bug in the memory range check, which is
398
worse than that of Linux 2.3.xx, so avoid the last 64kb. */
401
addr_min = GRUB_LINUX_BZIMAGE_ADDR + grub_linux16_prot_size;
403
grub_file_filter_disable_compression ();
404
file = grub_file_open (argv[0]);
408
size = grub_file_size (file);
411
grub_relocator_chunk_t ch;
412
err = grub_relocator_alloc_chunk_align (relocator, &ch,
413
addr_min, addr_max - size,
415
GRUB_RELOCATOR_PREFERENCE_HIGH);
418
initrd_chunk = get_virtual_current_address (ch);
419
initrd_addr = get_physical_target_address (ch);
422
if (grub_file_read (file, initrd_chunk, size) != size)
424
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
428
lh->ramdisk_image = initrd_addr;
429
lh->ramdisk_size = size;
433
grub_file_close (file);
438
static grub_command_t cmd_linux, cmd_initrd;
440
GRUB_MOD_INIT(linux16)
443
grub_register_command ("linux16", grub_cmd_linux,
444
0, N_("Load Linux."));
446
grub_register_command ("initrd16", grub_cmd_initrd,
447
0, N_("Load initrd."));
451
GRUB_MOD_FINI(linux16)
453
grub_unregister_command (cmd_linux);
454
grub_unregister_command (cmd_initrd);