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/cpu/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/machine/init.h>
29
#include <grub/machine/memory.h>
31
#include <grub/cpu/linux.h>
32
#include <grub/command.h>
33
#include <grub/i18n.h>
35
#include <grub/video.h>
37
#define GRUB_LINUX_CL_OFFSET 0x9000
38
#define GRUB_LINUX_CL_END_OFFSET 0x90FF
40
extern grub_uint32_t grub_linux_prot_size;
41
extern char *grub_linux_tmp_addr;
42
extern char *grub_linux_real_addr;
43
extern grub_int32_t grub_linux_is_bzimage;
44
extern char grub_linux16_boot_end[];
45
grub_err_t grub_linux16_real_boot (void);
47
static grub_dl_t my_mod;
49
static grub_size_t linux_mem_size;
53
grub_linux_unload (void)
55
grub_dl_unref (my_mod);
61
grub_linux16_boot (void)
63
grub_video_set_mode ("text", 0, 0);
64
grub_linux16_real_boot ();
71
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
72
int argc, char *argv[])
75
struct linux_kernel_header lh;
76
grub_uint8_t setup_sects;
77
grub_size_t real_size, prot_size;
86
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
90
file = grub_file_open (argv[0]);
94
if ((grub_size_t) grub_file_size (file) > grub_os_area_size)
96
grub_error (GRUB_ERR_OUT_OF_RANGE, "too big kernel (0x%x > 0x%x)",
97
(grub_size_t) grub_file_size (file),
102
if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
104
grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header");
108
if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
110
grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
114
if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
116
grub_error (GRUB_ERR_BAD_OS, "too many setup sectors");
120
grub_linux_is_bzimage = 0;
121
setup_sects = lh.setup_sects;
124
if (lh.header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
125
&& grub_le_to_cpu16 (lh.version) >= 0x0200)
127
grub_linux_is_bzimage = (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL);
128
lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
130
/* Put the real mode part at as a high location as possible. */
132
= (char *) UINT_TO_PTR (grub_mmap_get_lower ()
133
- GRUB_LINUX_SETUP_MOVE_SIZE);
134
/* But it must not exceed the traditional area. */
135
if (grub_linux_real_addr > (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR)
136
grub_linux_real_addr = (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR;
138
/* Ensure that it doesn't overlap with helper. */
139
if (((char *) grub_linux16_boot <= grub_linux_real_addr
140
&& grub_linux16_boot_end >= grub_linux_real_addr)
141
|| ((char *) grub_linux16_boot
142
<= grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE
143
&& grub_linux16_boot_end
144
>= grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE))
145
grub_linux_real_addr = ((char *) grub_linux16_boot)
146
- GRUB_LINUX_SETUP_MOVE_SIZE;
148
if (grub_le_to_cpu16 (lh.version) >= 0x0201)
150
lh.heap_end_ptr = grub_cpu_to_le16 (GRUB_LINUX_HEAP_END_OFFSET);
151
lh.loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
154
if (grub_le_to_cpu16 (lh.version) >= 0x0202)
155
lh.cmd_line_ptr = grub_linux_real_addr + GRUB_LINUX_CL_OFFSET;
158
lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC);
159
lh.cl_offset = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET);
160
lh.setup_move_size = grub_cpu_to_le16 (GRUB_LINUX_SETUP_MOVE_SIZE);
165
/* Your kernel is quite old... */
166
lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC);
167
lh.cl_offset = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET);
169
setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
171
grub_linux_real_addr = (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR;
173
/* Ensure that it doesn't overlap with helper. */
174
if (((char *) grub_linux16_boot <= grub_linux_real_addr
175
&& grub_linux16_boot_end >= grub_linux_real_addr)
176
|| ((char *) grub_linux16_boot
177
<= grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE
178
&& grub_linux16_boot_end
179
>= grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE))
181
grub_error (GRUB_ERR_OUT_OF_RANGE, "Too much low memory allocated and"
182
" your kernel is too old.");
187
/* If SETUP_SECTS is not set, set it to the default (4). */
189
setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
191
real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
192
prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
194
grub_linux_tmp_addr = (char *) GRUB_LINUX_BZIMAGE_ADDR + prot_size;
196
if (! grub_linux_is_bzimage
197
&& ((char *) GRUB_LINUX_ZIMAGE_ADDR + prot_size > grub_linux_real_addr))
199
grub_error (GRUB_ERR_BAD_OS, "too big zImage (0x%x > 0x%x), use bzImage instead",
200
(char *) GRUB_LINUX_ZIMAGE_ADDR + prot_size,
201
(grub_size_t) grub_linux_real_addr);
205
if (grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE
206
> (char *) UINT_TO_PTR (grub_mmap_get_lower ()))
208
grub_error (GRUB_ERR_OUT_OF_RANGE,
209
"too small lower memory (0x%x > 0x%x)",
210
grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE,
211
(int) grub_mmap_get_lower ());
215
grub_printf (" [Linux-%s, setup=0x%x, size=0x%x]\n",
216
grub_linux_is_bzimage ? "bzImage" : "zImage", real_size, prot_size);
218
for (i = 1; i < argc; i++)
219
if (grub_memcmp (argv[i], "vga=", 4) == 0)
221
/* Video mode selection support. */
222
grub_uint16_t vid_mode;
223
char *val = argv[i] + 4;
225
if (grub_strcmp (val, "normal") == 0)
226
vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
227
else if (grub_strcmp (val, "ext") == 0)
228
vid_mode = GRUB_LINUX_VID_MODE_EXTENDED;
229
else if (grub_strcmp (val, "ask") == 0)
230
vid_mode = GRUB_LINUX_VID_MODE_ASK;
232
vid_mode = (grub_uint16_t) grub_strtoul (val, 0, 0);
237
lh.vid_mode = grub_cpu_to_le16 (vid_mode);
239
else if (grub_memcmp (argv[i], "mem=", 4) == 0)
241
char *val = argv[i] + 4;
243
linux_mem_size = grub_strtoul (val, &val, 0);
247
grub_errno = GRUB_ERR_NONE;
254
switch (grub_tolower (val[0]))
266
/* Check an overflow. */
267
if (linux_mem_size > (~0UL >> shift))
270
linux_mem_size <<= shift;
274
/* Put the real mode code at the temporary address. */
275
grub_memmove (grub_linux_tmp_addr, &lh, sizeof (lh));
277
len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
278
if (grub_file_read (file, grub_linux_tmp_addr + sizeof (lh), len) != len)
280
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
284
if (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
285
|| grub_le_to_cpu16 (lh.version) < 0x0200)
286
/* Clear the heap space. */
287
grub_memset (grub_linux_tmp_addr
288
+ ((setup_sects + 1) << GRUB_DISK_SECTOR_BITS),
290
((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1)
291
<< GRUB_DISK_SECTOR_BITS));
293
/* Specify the boot file. */
294
dest = grub_stpcpy (grub_linux_tmp_addr + GRUB_LINUX_CL_OFFSET,
296
dest = grub_stpcpy (dest, argv[0]);
298
/* Copy kernel parameters. */
301
&& dest + grub_strlen (argv[i]) + 1 < (grub_linux_tmp_addr
302
+ GRUB_LINUX_CL_END_OFFSET);
306
dest = grub_stpcpy (dest, argv[i]);
310
if (grub_file_read (file, (void *) GRUB_LINUX_BZIMAGE_ADDR, len) != len)
311
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
313
if (grub_errno == GRUB_ERR_NONE)
315
grub_linux_prot_size = prot_size;
316
grub_loader_set (grub_linux16_boot, grub_linux_unload, 1);
323
grub_file_close (file);
325
if (grub_errno != GRUB_ERR_NONE)
327
grub_dl_unref (my_mod);
335
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
336
int argc, char *argv[])
338
grub_file_t file = 0;
340
grub_addr_t addr_max, addr_min, addr;
341
struct linux_kernel_header *lh;
345
grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
351
grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
355
lh = (struct linux_kernel_header *) grub_linux_tmp_addr;
357
if (!(lh->header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
358
&& grub_le_to_cpu16 (lh->version) >= 0x0200))
360
grub_error (GRUB_ERR_BAD_OS, "the kernel is too old for initrd");
364
/* Get the highest address available for the initrd. */
365
if (grub_le_to_cpu16 (lh->version) >= 0x0203)
367
addr_max = grub_cpu_to_le32 (lh->initrd_addr_max);
369
/* XXX in reality, Linux specifies a bogus value, so
370
it is necessary to make sure that ADDR_MAX does not exceed
372
if (addr_max > GRUB_LINUX_INITRD_MAX_ADDRESS)
373
addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
376
addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
378
if (linux_mem_size != 0 && linux_mem_size < addr_max)
379
addr_max = linux_mem_size;
381
/* Linux 2.3.xx has a bug in the memory range check, so avoid
383
Linux 2.2.xx has a bug in the memory range check, which is
384
worse than that of Linux 2.3.xx, so avoid the last 64kb. */
387
if (addr_max > grub_os_area_addr + grub_os_area_size)
388
addr_max = grub_os_area_addr + grub_os_area_size;
390
addr_min = (grub_addr_t) grub_linux_tmp_addr + GRUB_LINUX_CL_END_OFFSET;
392
file = grub_file_open (argv[0]);
396
size = grub_file_size (file);
398
/* Put the initrd as high as possible, 4KiB aligned. */
399
addr = (addr_max - size) & ~0xFFF;
403
grub_error (GRUB_ERR_OUT_OF_RANGE, "the initrd is too big");
407
if (grub_file_read (file, (void *) addr, size) != size)
409
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
413
lh->ramdisk_image = addr;
414
lh->ramdisk_size = size;
418
grub_file_close (file);
423
static grub_command_t cmd_linux, cmd_initrd;
425
GRUB_MOD_INIT(linux16)
428
grub_register_command ("linux16", grub_cmd_linux,
429
0, N_("Load Linux."));
431
grub_register_command ("initrd16", grub_cmd_initrd,
432
0, N_("Load initrd."));
436
GRUB_MOD_FINI(linux16)
438
grub_unregister_command (cmd_linux);
439
grub_unregister_command (cmd_initrd);