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/file.h>
23 #include <grub/device.h>
24 #include <grub/disk.h>
25 #include <grub/misc.h>
26 #include <grub/types.h>
27 #include <grub/memory.h>
29 #include <grub/cpu/linux.h>
30 #include <grub/command.h>
31 #include <grub/i18n.h>
33 #include <grub/cpu/relocator.h>
34 #include <grub/video.h>
35 #include <grub/i386/floppy.h>
36 #include <grub/lib/cmdline.h>
37 #include <grub/linux.h>
39 GRUB_MOD_LICENSE ("GPLv3+");
41 #define GRUB_LINUX_CL_OFFSET 0x9000
43 static grub_dl_t my_mod;
45 static grub_size_t linux_mem_size;
47 static struct grub_relocator *relocator = NULL;
48 static grub_addr_t grub_linux_real_target;
49 static char *grub_linux_real_chunk;
50 static grub_size_t grub_linux16_prot_size;
51 static grub_size_t maximal_cmdline_size;
54 grub_linux16_boot (void)
56 grub_uint16_t segment;
57 struct grub_relocator16_state state;
59 segment = grub_linux_real_target >> 4;
60 state.gs = state.fs = state.es = state.ds = state.ss = segment;
61 state.sp = GRUB_LINUX_SETUP_STACK;
62 state.cs = segment + 0x20;
66 grub_video_set_mode ("text", 0, 0);
70 return grub_relocator16_boot (relocator, state);
74 grub_linux_unload (void)
76 grub_dl_unref (my_mod);
78 grub_relocator_unload (relocator);
84 target_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
87 grub_uint64_t *result = data;
88 grub_uint64_t candidate;
90 if (type != GRUB_MEMORY_AVAILABLE)
94 if (addr + size >= 0xa0000)
95 size = 0xa0000 - addr;
97 /* Put the real mode part at as a high location as possible. */
98 candidate = addr + size - (GRUB_LINUX_CL_OFFSET + maximal_cmdline_size);
99 /* But it must not exceed the traditional area. */
100 if (candidate > GRUB_LINUX_OLD_REAL_MODE_ADDR)
101 candidate = GRUB_LINUX_OLD_REAL_MODE_ADDR;
102 if (candidate < addr)
105 if (candidate > *result || *result == (grub_uint64_t) -1)
111 grub_find_real_target (void)
113 grub_uint64_t result = (grub_uint64_t) -1;
115 grub_mmap_iterate (target_hook, &result);
120 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
121 int argc, char *argv[])
123 grub_file_t file = 0;
124 struct linux_i386_kernel_header lh;
125 grub_uint8_t setup_sects;
126 grub_size_t real_size;
129 char *grub_linux_prot_chunk;
130 int grub_linux_is_bzimage;
131 grub_addr_t grub_linux_prot_target;
134 grub_dl_ref (my_mod);
138 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
142 file = grub_file_open (argv[0]);
146 if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
149 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
154 if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
156 grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
160 if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
162 grub_error (GRUB_ERR_BAD_OS, "too many setup sectors");
166 grub_linux_is_bzimage = 0;
167 setup_sects = lh.setup_sects;
170 maximal_cmdline_size = 256;
172 if (lh.header == grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
173 && grub_le_to_cpu16 (lh.version) >= 0x0200)
175 grub_linux_is_bzimage = (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL);
176 lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
178 if (grub_le_to_cpu16 (lh.version) >= 0x0206)
179 maximal_cmdline_size = grub_le_to_cpu32 (lh.cmdline_size) + 1;
181 grub_linux_real_target = grub_find_real_target ();
182 if (grub_linux_real_target == (grub_addr_t)-1)
184 grub_error (GRUB_ERR_OUT_OF_RANGE,
185 "no appropriate low memory found");
189 if (grub_le_to_cpu16 (lh.version) >= 0x0201)
191 lh.heap_end_ptr = grub_cpu_to_le16_compile_time (GRUB_LINUX_HEAP_END_OFFSET);
192 lh.loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
195 if (grub_le_to_cpu16 (lh.version) >= 0x0202)
196 lh.cmd_line_ptr = grub_linux_real_target + GRUB_LINUX_CL_OFFSET;
199 lh.cl_magic = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_MAGIC);
200 lh.cl_offset = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_OFFSET);
201 lh.setup_move_size = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_OFFSET
202 + maximal_cmdline_size);
207 /* Your kernel is quite old... */
208 lh.cl_magic = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_MAGIC);
209 lh.cl_offset = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_OFFSET);
211 setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
213 grub_linux_real_target = GRUB_LINUX_OLD_REAL_MODE_ADDR;
216 /* If SETUP_SECTS is not set, set it to the default (4). */
218 setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
220 real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
221 grub_linux16_prot_size = grub_file_size (file)
222 - real_size - GRUB_DISK_SECTOR_SIZE;
224 if (! grub_linux_is_bzimage
225 && GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size
226 > grub_linux_real_target)
228 grub_error (GRUB_ERR_BAD_OS, "too big zImage (0x%x > 0x%x), use bzImage instead",
229 (char *) GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size,
230 (grub_size_t) grub_linux_real_target);
234 grub_dprintf ("linux", "[Linux-%s, setup=0x%x, size=0x%x]\n",
235 grub_linux_is_bzimage ? "bzImage" : "zImage",
236 (unsigned) real_size,
237 (unsigned) grub_linux16_prot_size);
239 relocator = grub_relocator_new ();
243 for (i = 1; i < argc; i++)
244 if (grub_memcmp (argv[i], "vga=", 4) == 0)
246 /* Video mode selection support. */
247 grub_uint16_t vid_mode;
248 char *val = argv[i] + 4;
250 if (grub_strcmp (val, "normal") == 0)
251 vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
252 else if (grub_strcmp (val, "ext") == 0)
253 vid_mode = GRUB_LINUX_VID_MODE_EXTENDED;
254 else if (grub_strcmp (val, "ask") == 0)
255 vid_mode = GRUB_LINUX_VID_MODE_ASK;
257 vid_mode = (grub_uint16_t) grub_strtoul (val, 0, 0);
262 lh.vid_mode = grub_cpu_to_le16 (vid_mode);
264 else if (grub_memcmp (argv[i], "mem=", 4) == 0)
266 char *val = argv[i] + 4;
268 linux_mem_size = grub_strtoul (val, &val, 0);
272 grub_errno = GRUB_ERR_NONE;
279 switch (grub_tolower (val[0]))
294 /* Check an overflow. */
295 if (linux_mem_size > (~0UL >> shift))
298 linux_mem_size <<= shift;
303 grub_relocator_chunk_t ch;
304 err = grub_relocator_alloc_chunk_addr (relocator, &ch,
305 grub_linux_real_target,
307 + maximal_cmdline_size);
310 grub_linux_real_chunk = get_virtual_current_address (ch);
313 /* Put the real mode code at the temporary address. */
314 grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));
316 len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
317 if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len)
320 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
325 if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
326 || grub_le_to_cpu16 (lh.version) < 0x0200)
327 /* Clear the heap space. */
328 grub_memset (grub_linux_real_chunk
329 + ((setup_sects + 1) << GRUB_DISK_SECTOR_BITS),
331 ((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1)
332 << GRUB_DISK_SECTOR_BITS));
334 /* Create kernel command line. */
335 grub_memcpy ((char *)grub_linux_real_chunk + GRUB_LINUX_CL_OFFSET,
336 LINUX_IMAGE, sizeof (LINUX_IMAGE));
337 grub_create_loader_cmdline (argc, argv,
338 (char *)grub_linux_real_chunk
339 + GRUB_LINUX_CL_OFFSET + sizeof (LINUX_IMAGE) - 1,
341 - (sizeof (LINUX_IMAGE) - 1));
343 if (grub_linux_is_bzimage)
344 grub_linux_prot_target = GRUB_LINUX_BZIMAGE_ADDR;
346 grub_linux_prot_target = GRUB_LINUX_ZIMAGE_ADDR;
348 grub_relocator_chunk_t ch;
349 err = grub_relocator_alloc_chunk_addr (relocator, &ch,
350 grub_linux_prot_target,
351 grub_linux16_prot_size);
354 grub_linux_prot_chunk = get_virtual_current_address (ch);
357 len = grub_linux16_prot_size;
358 if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size)
359 != (grub_ssize_t) grub_linux16_prot_size && !grub_errno)
360 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
363 if (grub_errno == GRUB_ERR_NONE)
365 grub_loader_set (grub_linux16_boot, grub_linux_unload, 0);
372 grub_file_close (file);
374 if (grub_errno != GRUB_ERR_NONE)
376 grub_dl_unref (my_mod);
378 grub_relocator_unload (relocator);
385 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
386 int argc, char *argv[])
388 grub_size_t size = 0;
389 grub_addr_t addr_max, addr_min;
390 struct linux_i386_kernel_header *lh;
391 grub_uint8_t *initrd_chunk;
392 grub_addr_t initrd_addr;
394 struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
398 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
404 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
408 lh = (struct linux_i386_kernel_header *) grub_linux_real_chunk;
410 if (!(lh->header == grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
411 && grub_le_to_cpu16 (lh->version) >= 0x0200))
413 grub_error (GRUB_ERR_BAD_OS, "the kernel is too old for initrd");
417 /* Get the highest address available for the initrd. */
418 if (grub_le_to_cpu16 (lh->version) >= 0x0203)
420 addr_max = grub_cpu_to_le32 (lh->initrd_addr_max);
422 /* XXX in reality, Linux specifies a bogus value, so
423 it is necessary to make sure that ADDR_MAX does not exceed
425 if (addr_max > GRUB_LINUX_INITRD_MAX_ADDRESS)
426 addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
429 addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
431 if (linux_mem_size != 0 && linux_mem_size < addr_max)
432 addr_max = linux_mem_size;
434 /* Linux 2.3.xx has a bug in the memory range check, so avoid
436 Linux 2.2.xx has a bug in the memory range check, which is
437 worse than that of Linux 2.3.xx, so avoid the last 64kb. */
440 addr_min = GRUB_LINUX_BZIMAGE_ADDR + grub_linux16_prot_size;
442 if (grub_initrd_init (argc, argv, &initrd_ctx))
445 size = grub_get_initrd_size (&initrd_ctx);
448 grub_relocator_chunk_t ch;
449 err = grub_relocator_alloc_chunk_align (relocator, &ch,
450 addr_min, addr_max - size,
452 GRUB_RELOCATOR_PREFERENCE_HIGH, 0);
455 initrd_chunk = get_virtual_current_address (ch);
456 initrd_addr = get_physical_target_address (ch);
459 if (grub_initrd_load (&initrd_ctx, argv, initrd_chunk))
462 lh->ramdisk_image = initrd_addr;
463 lh->ramdisk_size = size;
466 grub_initrd_close (&initrd_ctx);
471 static grub_command_t cmd_linux, cmd_initrd;
473 GRUB_MOD_INIT(linux16)
476 grub_register_command ("linux16", grub_cmd_linux,
477 0, N_("Load Linux."));
479 grub_register_command ("initrd16", grub_cmd_initrd,
480 0, N_("Load initrd."));
484 GRUB_MOD_FINI(linux16)
486 grub_unregister_command (cmd_linux);
487 grub_unregister_command (cmd_initrd);