]> git.proxmox.com Git - grub2.git/blame - grub-core/loader/i386/linux.c
i386: make struct linux_kernel_header architecture specific
[grub2.git] / grub-core / loader / i386 / linux.c
CommitLineData
c9baafe7 1/*
2 * GRUB -- GRand Unified Bootloader
1a064917 3 * Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, Inc.
c9baafe7 4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <grub/loader.h>
c8142599 20#include <grub/memory.h>
6c67de15 21#include <grub/normal.h>
c9baafe7 22#include <grub/file.h>
23#include <grub/disk.h>
24#include <grub/err.h>
25#include <grub/misc.h>
26#include <grub/types.h>
c9baafe7 27#include <grub/dl.h>
28#include <grub/mm.h>
29#include <grub/term.h>
30#include <grub/cpu/linux.h>
14aad807 31#include <grub/video.h>
d3539132 32#include <grub/video_fb.h>
b1b797cb 33#include <grub/command.h>
55b40bc6 34#include <grub/i386/relocator.h>
809bbfeb 35#include <grub/i18n.h>
25953e10 36#include <grub/lib/cmdline.h>
92750e4c 37#include <grub/linux.h>
c9baafe7 38
e745cf0c
VS
39GRUB_MOD_LICENSE ("GPLv3+");
40
e31bb619
VS
41#ifdef GRUB_MACHINE_PCBIOS
42#include <grub/i386/pc/vesa_modes_table.h>
43#endif
44
c8142599
VS
45#ifdef GRUB_MACHINE_EFI
46#include <grub/efi/efi.h>
47#define HAS_VGA_TEXT 0
bf26bcc4 48#define DEFAULT_VIDEO_MODE "auto"
b1257f65 49#define ACCEPTS_PURE_TEXT 0
8071fb79
VS
50#elif defined (GRUB_MACHINE_IEEE1275)
51#include <grub/ieee1275/ieee1275.h>
52#define HAS_VGA_TEXT 0
53#define DEFAULT_VIDEO_MODE "text"
b1257f65 54#define ACCEPTS_PURE_TEXT 1
c8142599
VS
55#else
56#include <grub/i386/pc/vbe.h>
57#include <grub/i386/pc/console.h>
58#define HAS_VGA_TEXT 1
59#define DEFAULT_VIDEO_MODE "text"
b1257f65 60#define ACCEPTS_PURE_TEXT 1
c8142599
VS
61#endif
62
c9baafe7 63static grub_dl_t my_mod;
64
65static grub_size_t linux_mem_size;
66static int loaded;
c9baafe7 67static void *prot_mode_mem;
55b40bc6 68static grub_addr_t prot_mode_target;
c9baafe7 69static void *initrd_mem;
55b40bc6 70static grub_addr_t initrd_mem_target;
807fb77c 71static grub_size_t prot_init_space;
55b40bc6 72static struct grub_relocator *relocator = NULL;
c8142599 73static void *efi_mmap_buf;
a06eb03a 74static grub_size_t maximal_cmdline_size;
a4e5ca80
VS
75static struct linux_kernel_params linux_params;
76static char *linux_cmdline;
d3fbca98
VS
77#ifdef GRUB_MACHINE_EFI
78static grub_efi_uintn_t efi_mmap_size;
79#else
80static const grub_size_t efi_mmap_size = 0;
81#endif
c9baafe7 82
55b40bc6
VS
83/* FIXME */
84#if 0
c9baafe7 85struct idt_descriptor
86{
87 grub_uint16_t limit;
88 void *base;
7e47e27b 89} GRUB_PACKED;
c9baafe7 90
91static struct idt_descriptor idt_desc =
92 {
93 0,
94 0
95 };
55b40bc6 96#endif
c9baafe7 97
98static inline grub_size_t
99page_align (grub_size_t size)
100{
101 return (size + (1 << 12) - 1) & (~((1 << 12) - 1));
102}
103
c8142599
VS
104#ifdef GRUB_MACHINE_EFI
105/* Find the optimal number of pages for the memory map. Is it better to
106 move this code to efi/mm.c? */
107static grub_efi_uintn_t
108find_efi_mmap_size (void)
109{
110 static grub_efi_uintn_t mmap_size = 0;
111
112 if (mmap_size != 0)
113 return mmap_size;
114
115 mmap_size = (1 << 12);
116 while (1)
117 {
118 int ret;
119 grub_efi_memory_descriptor_t *mmap;
120 grub_efi_uintn_t desc_size;
35692881 121 grub_efi_uintn_t cur_mmap_size = mmap_size;
c8142599 122
35692881 123 mmap = grub_malloc (cur_mmap_size);
c8142599
VS
124 if (! mmap)
125 return 0;
126
35692881 127 ret = grub_efi_get_memory_map (&cur_mmap_size, mmap, 0, &desc_size, 0);
c8142599
VS
128 grub_free (mmap);
129
130 if (ret < 0)
119d11c8
VS
131 {
132 grub_error (GRUB_ERR_IO, "cannot get memory map");
133 return 0;
134 }
c8142599
VS
135 else if (ret > 0)
136 break;
137
35692881
VS
138 if (mmap_size < cur_mmap_size)
139 mmap_size = cur_mmap_size;
c8142599
VS
140 mmap_size += (1 << 12);
141 }
142
143 /* Increase the size a bit for safety, because GRUB allocates more on
144 later, and EFI itself may allocate more. */
f923ad22 145 mmap_size += (3 << 12);
c8142599 146
be1a7ce0
CW
147 mmap_size = page_align (mmap_size);
148 return mmap_size;
c8142599
VS
149}
150
151#endif
152
d0d4b8a0
CW
153/* Helper for find_mmap_size. */
154static int
155count_hook (grub_uint64_t addr __attribute__ ((unused)),
156 grub_uint64_t size __attribute__ ((unused)),
157 grub_memory_type_t type __attribute__ ((unused)), void *data)
158{
159 grub_size_t *count = data;
160
161 (*count)++;
162 return 0;
163}
164
c9baafe7 165/* Find the optimal number of pages for the memory map. */
166static grub_size_t
167find_mmap_size (void)
168{
169 grub_size_t count = 0, mmap_size;
170
d0d4b8a0 171 grub_mmap_iterate (count_hook, &count);
b39f9d20 172
c9baafe7 173 mmap_size = count * sizeof (struct grub_e820_mmap);
174
175 /* Increase the size a bit for safety, because GRUB allocates more on
176 later. */
177 mmap_size += (1 << 12);
b39f9d20 178
c9baafe7 179 return page_align (mmap_size);
180}
181
182static void
183free_pages (void)
184{
55b40bc6
VS
185 grub_relocator_unload (relocator);
186 relocator = NULL;
a4e5ca80
VS
187 prot_mode_mem = initrd_mem = 0;
188 prot_mode_target = initrd_mem_target = 0;
c9baafe7 189}
190
191/* Allocate pages for the real mode code and the protected mode code
192 for linux as well as a memory map buffer. */
55b40bc6 193static grub_err_t
9be4c45d
VS
194allocate_pages (grub_size_t prot_size, grub_size_t *align,
195 grub_size_t min_align, int relocatable,
39525c22 196 grub_uint64_t preferred_address)
c9baafe7 197{
55b40bc6 198 grub_err_t err;
c9baafe7 199
81afc5cc
VS
200 if (prot_size == 0)
201 prot_size = 1;
202
c9baafe7 203 prot_size = page_align (prot_size);
b39f9d20 204
c9baafe7 205 /* Initialize the memory pointers with NULL for convenience. */
1007d1f5 206 free_pages ();
b39f9d20 207
55b40bc6
VS
208 relocator = grub_relocator_new ();
209 if (!relocator)
210 {
211 err = grub_errno;
212 goto fail;
213 }
214
aa9f3bff 215 /* FIXME: Should request low memory from the heap when this feature is
216 implemented. */
2f2a3442 217
4b2ec20b
VS
218 {
219 grub_relocator_chunk_t ch;
9be4c45d
VS
220 if (relocatable)
221 {
222 err = grub_relocator_alloc_chunk_align (relocator, &ch,
39525c22
CW
223 preferred_address,
224 preferred_address,
9be4c45d
VS
225 prot_size, 1,
226 GRUB_RELOCATOR_PREFERENCE_LOW,
227 1);
1a11761f 228 for (; err && *align + 1 > min_align; (*align)--)
9be4c45d
VS
229 {
230 grub_errno = GRUB_ERR_NONE;
231 err = grub_relocator_alloc_chunk_align (relocator, &ch,
2282da4a
VS
232 0x1000000,
233 0xffffffff & ~prot_size,
9be4c45d
VS
234 prot_size, 1 << *align,
235 GRUB_RELOCATOR_PREFERENCE_LOW,
236 1);
237 }
238 if (err)
239 goto fail;
240 }
241 else
242 err = grub_relocator_alloc_chunk_addr (relocator, &ch,
39525c22 243 preferred_address,
9be4c45d 244 prot_size);
4b2ec20b
VS
245 if (err)
246 goto fail;
247 prot_mode_mem = get_virtual_current_address (ch);
9be4c45d 248 prot_mode_target = get_physical_target_address (ch);
4b2ec20b 249 }
c9baafe7 250
24ca71c9
VS
251 grub_dprintf ("linux", "prot_mode_mem = %p, prot_mode_target = %lx, prot_size = %x\n",
252 prot_mode_mem, (unsigned long) prot_mode_target,
a4e5ca80 253 (unsigned) prot_size);
55b40bc6 254 return GRUB_ERR_NONE;
c9baafe7 255
256 fail:
257 free_pages ();
55b40bc6 258 return err;
c9baafe7 259}
260
119d11c8 261static grub_err_t
c9baafe7 262grub_e820_add_region (struct grub_e820_mmap *e820_map, int *e820_num,
263 grub_uint64_t start, grub_uint64_t size,
264 grub_uint32_t type)
265{
266 int n = *e820_num;
267
c9baafe7 268 if ((n > 0) && (e820_map[n - 1].addr + e820_map[n - 1].size == start) &&
269 (e820_map[n - 1].type == type))
270 e820_map[n - 1].size += size;
271 else
272 {
273 e820_map[n].addr = start;
274 e820_map[n].size = size;
275 e820_map[n].type = type;
276 (*e820_num)++;
277 }
119d11c8 278 return GRUB_ERR_NONE;
c9baafe7 279}
280
c8142599 281static grub_err_t
14aad807 282grub_linux_setup_video (struct linux_kernel_params *params)
283{
284 struct grub_video_mode_info mode_info;
d3539132 285 void *framebuffer;
c8142599 286 grub_err_t err;
3395fe52 287 grub_video_driver_id_t driver_id;
d35d0d37 288 const char *gfxlfbvar = grub_env_get ("gfxpayloadforcelfb");
3395fe52
VS
289
290 driver_id = grub_video_get_driver_id ();
291
292 if (driver_id == GRUB_VIDEO_DRIVER_NONE)
293 return 1;
c8142599 294
c8142599 295 err = grub_video_get_info_and_fini (&mode_info, &framebuffer);
14aad807 296
c8142599 297 if (err)
74e31b5c
VS
298 {
299 grub_errno = GRUB_ERR_NONE;
300 return 1;
301 }
14aad807 302
303 params->lfb_width = mode_info.width;
304 params->lfb_height = mode_info.height;
305 params->lfb_depth = mode_info.bpp;
306 params->lfb_line_len = mode_info.pitch;
307
d3539132 308 params->lfb_base = (grub_size_t) framebuffer;
7cae4377 309 params->lfb_size = ALIGN_UP (params->lfb_line_len * params->lfb_height, 65536);
14aad807 310
7a6bf9f2 311 params->red_mask_size = mode_info.red_mask_size;
312 params->red_field_pos = mode_info.red_field_pos;
313 params->green_mask_size = mode_info.green_mask_size;
314 params->green_field_pos = mode_info.green_field_pos;
315 params->blue_mask_size = mode_info.blue_mask_size;
316 params->blue_field_pos = mode_info.blue_field_pos;
317 params->reserved_mask_size = mode_info.reserved_mask_size;
318 params->reserved_field_pos = mode_info.reserved_field_pos;
14aad807 319
3395fe52
VS
320 if (gfxlfbvar && (gfxlfbvar[0] == '1' || gfxlfbvar[0] == 'y'))
321 params->have_vga = GRUB_VIDEO_LINUX_TYPE_SIMPLE;
322 else
323 {
324 switch (driver_id)
325 {
326 case GRUB_VIDEO_DRIVER_VBE:
327 params->lfb_size >>= 16;
328 params->have_vga = GRUB_VIDEO_LINUX_TYPE_VESA;
329 break;
330
331 case GRUB_VIDEO_DRIVER_EFI_UGA:
332 case GRUB_VIDEO_DRIVER_EFI_GOP:
333 params->have_vga = GRUB_VIDEO_LINUX_TYPE_EFIFB;
334 break;
335
336 /* FIXME: check if better id is available. */
337 case GRUB_VIDEO_DRIVER_SM712:
67b1e5c9 338 case GRUB_VIDEO_DRIVER_SIS315PRO:
3395fe52
VS
339 case GRUB_VIDEO_DRIVER_VGA:
340 case GRUB_VIDEO_DRIVER_CIRRUS:
341 case GRUB_VIDEO_DRIVER_BOCHS:
fe8d4a7b 342 case GRUB_VIDEO_DRIVER_RADEON_FULOONG2E:
184c61dd 343 case GRUB_VIDEO_DRIVER_RADEON_YEELOONG3A:
45fbd9a2
VS
344 case GRUB_VIDEO_DRIVER_IEEE1275:
345 case GRUB_VIDEO_DRIVER_COREBOOT:
3395fe52 346 /* Make gcc happy. */
71669c3b 347 case GRUB_VIDEO_DRIVER_XEN:
3395fe52
VS
348 case GRUB_VIDEO_DRIVER_SDL:
349 case GRUB_VIDEO_DRIVER_NONE:
095accd1 350 case GRUB_VIDEO_ADAPTER_CAPTURE:
3395fe52
VS
351 params->have_vga = GRUB_VIDEO_LINUX_TYPE_SIMPLE;
352 break;
353 }
354 }
61229557 355
356#ifdef GRUB_MACHINE_PCBIOS
357 /* VESA packed modes may come with zeroed mask sizes, which need
358 to be set here according to DAC Palette width. If we don't,
359 this results in Linux displaying a black screen. */
3395fe52 360 if (driver_id == GRUB_VIDEO_DRIVER_VBE && mode_info.bpp <= 8)
61229557 361 {
362 struct grub_vbe_info_block controller_info;
363 int status;
364 int width = 8;
365
366 status = grub_vbe_bios_get_controller_info (&controller_info);
367
368 if (status == GRUB_VBE_STATUS_OK &&
369 (controller_info.capabilities & GRUB_VBE_CAPABILITY_DACWIDTH))
370 status = grub_vbe_bios_set_dac_palette_width (&width);
371
372 if (status != GRUB_VBE_STATUS_OK)
373 /* 6 is default after mode reset. */
374 width = 6;
375
376 params->red_mask_size = params->green_mask_size
377 = params->blue_mask_size = width;
378 params->reserved_mask_size = 0;
379 }
380#endif
381
c8142599 382 return GRUB_ERR_NONE;
14aad807 383}
384
d0d4b8a0
CW
385/* Context for grub_linux_boot. */
386struct grub_linux_boot_ctx
387{
388 grub_addr_t real_mode_target;
389 grub_size_t real_size;
390 struct linux_kernel_params *params;
391 int e820_num;
392};
393
394/* Helper for grub_linux_boot. */
395static int
396grub_linux_boot_mmap_find (grub_uint64_t addr, grub_uint64_t size,
397 grub_memory_type_t type, void *data)
398{
399 struct grub_linux_boot_ctx *ctx = data;
400
401 /* We must put real mode code in the traditional space. */
402 if (type != GRUB_MEMORY_AVAILABLE || addr > 0x90000)
403 return 0;
404
405 if (addr + size < 0x10000)
406 return 0;
407
408 if (addr < 0x10000)
409 {
410 size += addr - 0x10000;
411 addr = 0x10000;
412 }
413
414 if (addr + size > 0x90000)
415 size = 0x90000 - addr;
416
417 if (ctx->real_size + efi_mmap_size > size)
418 return 0;
419
420 grub_dprintf ("linux", "addr = %lx, size = %x, need_size = %x\n",
421 (unsigned long) addr,
422 (unsigned) size,
423 (unsigned) (ctx->real_size + efi_mmap_size));
424 ctx->real_mode_target = ((addr + size) - (ctx->real_size + efi_mmap_size));
425 return 1;
426}
427
6de9ee86 428/* GRUB types conveniently match E820 types. */
d0d4b8a0
CW
429static int
430grub_linux_boot_mmap_fill (grub_uint64_t addr, grub_uint64_t size,
431 grub_memory_type_t type, void *data)
432{
433 struct grub_linux_boot_ctx *ctx = data;
434
d0d4b8a0 435 if (grub_e820_add_region (ctx->params->e820_map, &ctx->e820_num,
6de9ee86 436 addr, size, type))
d0d4b8a0
CW
437 return 1;
438
439 return 0;
440}
441
c9baafe7 442static grub_err_t
a9368fd3 443grub_linux_boot (void)
c9baafe7 444{
ad760f81 445 grub_err_t err = 0;
d35d0d37
VS
446 const char *modevar;
447 char *tmp;
55b40bc6 448 struct grub_relocator32_state state;
a4e5ca80 449 void *real_mode_mem;
d0d4b8a0
CW
450 struct grub_linux_boot_ctx ctx = {
451 .real_mode_target = 0
452 };
453 grub_size_t mmap_size;
a4e5ca80
VS
454 grub_size_t cl_offset;
455
67caf9eb
VS
456#ifdef GRUB_MACHINE_IEEE1275
457 {
458 const char *bootpath;
459 grub_ssize_t len;
460
461 bootpath = grub_env_get ("root");
462 if (bootpath)
463 grub_ieee1275_set_property (grub_ieee1275_chosen,
464 "bootpath", bootpath,
465 grub_strlen (bootpath) + 1,
466 &len);
467 linux_params.ofw_signature = GRUB_LINUX_OFW_SIGNATURE;
468 linux_params.ofw_num_items = 1;
469 linux_params.ofw_cif_handler = (grub_uint32_t) grub_ieee1275_entry_fn;
470 linux_params.ofw_idt = 0;
471 }
472#endif
473
474 modevar = grub_env_get ("gfxpayload");
475
476 /* Now all graphical modes are acceptable.
477 May change in future if we have modes without framebuffer. */
478 if (modevar && *modevar != 0)
479 {
480 tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
481 if (! tmp)
482 return grub_errno;
483#if ACCEPTS_PURE_TEXT
484 err = grub_video_set_mode (tmp, 0, 0);
485#else
486 err = grub_video_set_mode (tmp, GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
487#endif
488 grub_free (tmp);
489 }
1a40f80c
VS
490 else /* We can't go back to text mode from coreboot fb. */
491#ifdef GRUB_MACHINE_COREBOOT
492 if (grub_video_get_driver_id () == GRUB_VIDEO_DRIVER_COREBOOT)
493 err = GRUB_ERR_NONE;
494 else
495#endif
496 {
67caf9eb 497#if ACCEPTS_PURE_TEXT
1a40f80c 498 err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0);
67caf9eb 499#else
1a40f80c 500 err = grub_video_set_mode (DEFAULT_VIDEO_MODE,
67caf9eb
VS
501 GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
502#endif
1a40f80c 503 }
67caf9eb
VS
504
505 if (err)
506 {
507 grub_print_error ();
508 grub_puts_ (N_("Booting in blind mode"));
509 grub_errno = GRUB_ERR_NONE;
510 }
511
512 if (grub_linux_setup_video (&linux_params))
513 {
514#if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
515 linux_params.have_vga = GRUB_VIDEO_LINUX_TYPE_TEXT;
516 linux_params.video_mode = 0x3;
517#else
518 linux_params.have_vga = 0;
519 linux_params.video_mode = 0;
520 linux_params.video_width = 0;
521 linux_params.video_height = 0;
522#endif
523 }
524
525
526#ifndef GRUB_MACHINE_IEEE1275
527 if (linux_params.have_vga == GRUB_VIDEO_LINUX_TYPE_TEXT)
528#endif
529 {
530 grub_term_output_t term;
531 int found = 0;
532 FOR_ACTIVE_TERM_OUTPUTS(term)
533 if (grub_strcmp (term->name, "vga_text") == 0
534 || grub_strcmp (term->name, "console") == 0
535 || grub_strcmp (term->name, "ofconsole") == 0)
536 {
e89c2d48
VS
537 struct grub_term_coordinate pos = grub_term_getxy (term);
538 linux_params.video_cursor_x = pos.x;
539 linux_params.video_cursor_y = pos.y;
67caf9eb
VS
540 linux_params.video_width = grub_term_width (term);
541 linux_params.video_height = grub_term_height (term);
542 found = 1;
543 break;
544 }
545 if (!found)
546 {
547 linux_params.video_cursor_x = 0;
548 linux_params.video_cursor_y = 0;
549 linux_params.video_width = 80;
550 linux_params.video_height = 25;
551 }
552 }
553
a4e5ca80
VS
554 mmap_size = find_mmap_size ();
555 /* Make sure that each size is aligned to a page boundary. */
67caf9eb
VS
556 cl_offset = ALIGN_UP (mmap_size + sizeof (linux_params), 4096);
557 if (cl_offset < ((grub_size_t) linux_params.setup_sects << GRUB_DISK_SECTOR_BITS))
558 cl_offset = ALIGN_UP ((grub_size_t) (linux_params.setup_sects
f19cb95e 559 << GRUB_DISK_SECTOR_BITS), 4096);
d0d4b8a0 560 ctx.real_size = ALIGN_UP (cl_offset + maximal_cmdline_size, 4096);
a4e5ca80
VS
561
562#ifdef GRUB_MACHINE_EFI
563 efi_mmap_size = find_efi_mmap_size ();
564 if (efi_mmap_size == 0)
565 return grub_errno;
566#endif
567
568 grub_dprintf ("linux", "real_size = %x, mmap_size = %x\n",
d0d4b8a0 569 (unsigned) ctx.real_size, (unsigned) mmap_size);
a4e5ca80 570
a4e5ca80 571#ifdef GRUB_MACHINE_EFI
d0d4b8a0
CW
572 grub_efi_mmap_iterate (grub_linux_boot_mmap_find, &ctx, 1);
573 if (! ctx.real_mode_target)
574 grub_efi_mmap_iterate (grub_linux_boot_mmap_find, &ctx, 0);
a4e5ca80 575#else
d0d4b8a0 576 grub_mmap_iterate (grub_linux_boot_mmap_find, &ctx);
a4e5ca80 577#endif
f19cb95e 578 grub_dprintf ("linux", "real_mode_target = %lx, real_size = %x, efi_mmap_size = %x\n",
d0d4b8a0
CW
579 (unsigned long) ctx.real_mode_target,
580 (unsigned) ctx.real_size,
f19cb95e
VS
581 (unsigned) efi_mmap_size);
582
d0d4b8a0 583 if (! ctx.real_mode_target)
a4e5ca80
VS
584 return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages");
585
586 {
587 grub_relocator_chunk_t ch;
588 err = grub_relocator_alloc_chunk_addr (relocator, &ch,
d0d4b8a0
CW
589 ctx.real_mode_target,
590 (ctx.real_size + efi_mmap_size));
a4e5ca80
VS
591 if (err)
592 return err;
593 real_mode_mem = get_virtual_current_address (ch);
594 }
d0d4b8a0 595 efi_mmap_buf = (grub_uint8_t *) real_mode_mem + ctx.real_size;
a4e5ca80 596
24ca71c9
VS
597 grub_dprintf ("linux", "real_mode_mem = %p\n",
598 real_mode_mem);
67caf9eb 599
d0d4b8a0 600 ctx.params = real_mode_mem;
c9baafe7 601
d0d4b8a0
CW
602 *ctx.params = linux_params;
603 ctx.params->cmd_line_ptr = ctx.real_mode_target + cl_offset;
604 grub_memcpy ((char *) ctx.params + cl_offset, linux_cmdline,
a4e5ca80
VS
605 maximal_cmdline_size);
606
55b40bc6 607 grub_dprintf ("linux", "code32_start = %x\n",
d0d4b8a0 608 (unsigned) ctx.params->code32_start);
c9baafe7 609
d0d4b8a0
CW
610 ctx.e820_num = 0;
611 if (grub_mmap_iterate (grub_linux_boot_mmap_fill, &ctx))
119d11c8 612 return grub_errno;
d0d4b8a0 613 ctx.params->mmap_size = ctx.e820_num;
c9baafe7 614
c8142599
VS
615#ifdef GRUB_MACHINE_EFI
616 {
91b58e6b 617 grub_efi_uintn_t efi_desc_size;
3c62402d 618 grub_size_t efi_mmap_target;
c8142599 619 grub_efi_uint32_t efi_desc_version;
91b58e6b
VS
620 err = grub_efi_finish_boot_services (&efi_mmap_size, efi_mmap_buf, NULL,
621 &efi_desc_size, &efi_desc_version);
622 if (err)
623 return err;
c8142599 624
c8142599 625 /* Note that no boot services are available from here. */
d0d4b8a0 626 efi_mmap_target = ctx.real_mode_target
3c62402d 627 + ((grub_uint8_t *) efi_mmap_buf - (grub_uint8_t *) real_mode_mem);
c8142599 628 /* Pass EFI parameters. */
d0d4b8a0 629 if (grub_le_to_cpu16 (ctx.params->version) >= 0x0208)
bcc75fb3 630 {
d0d4b8a0
CW
631 ctx.params->v0208.efi_mem_desc_size = efi_desc_size;
632 ctx.params->v0208.efi_mem_desc_version = efi_desc_version;
633 ctx.params->v0208.efi_mmap = efi_mmap_target;
634 ctx.params->v0208.efi_mmap_size = efi_mmap_size;
bcc75fb3
PJ
635
636#ifdef __x86_64__
d0d4b8a0 637 ctx.params->v0208.efi_mmap_hi = (efi_mmap_target >> 32);
bcc75fb3
PJ
638#endif
639 }
d0d4b8a0 640 else if (grub_le_to_cpu16 (ctx.params->version) >= 0x0206)
c8142599 641 {
d0d4b8a0
CW
642 ctx.params->v0206.efi_mem_desc_size = efi_desc_size;
643 ctx.params->v0206.efi_mem_desc_version = efi_desc_version;
644 ctx.params->v0206.efi_mmap = efi_mmap_target;
645 ctx.params->v0206.efi_mmap_size = efi_mmap_size;
c8142599 646 }
d0d4b8a0 647 else if (grub_le_to_cpu16 (ctx.params->version) >= 0x0204)
c8142599 648 {
d0d4b8a0
CW
649 ctx.params->v0204.efi_mem_desc_size = efi_desc_size;
650 ctx.params->v0204.efi_mem_desc_version = efi_desc_version;
651 ctx.params->v0204.efi_mmap = efi_mmap_target;
652 ctx.params->v0204.efi_mmap_size = efi_mmap_size;
c8142599
VS
653 }
654 }
655#endif
656
55b40bc6
VS
657 /* FIXME. */
658 /* asm volatile ("lidt %0" : : "m" (idt_desc)); */
9056cbf3 659 state.ebp = state.edi = state.ebx = 0;
d0d4b8a0
CW
660 state.esi = ctx.real_mode_target;
661 state.esp = ctx.real_mode_target;
662 state.eip = ctx.params->code32_start;
9be4c45d 663 return grub_relocator32_boot (relocator, state, 0);
c9baafe7 664}
665
666static grub_err_t
667grub_linux_unload (void)
668{
c9baafe7 669 grub_dl_unref (my_mod);
670 loaded = 0;
a4e5ca80
VS
671 grub_free (linux_cmdline);
672 linux_cmdline = 0;
c9baafe7 673 return GRUB_ERR_NONE;
674}
675
b1b797cb 676static grub_err_t
677grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
678 int argc, char *argv[])
c9baafe7 679{
680 grub_file_t file = 0;
7d36709d 681 struct linux_i386_kernel_header lh;
c9baafe7 682 grub_uint8_t setup_sects;
7c8d0ce7 683 grub_size_t real_size, prot_size, prot_file_size;
c9baafe7 684 grub_ssize_t len;
685 int i;
9be4c45d
VS
686 grub_size_t align, min_align;
687 int relocatable;
39525c22 688 grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
c9baafe7 689
690 grub_dl_ref (my_mod);
b39f9d20 691
c9baafe7 692 if (argc == 0)
693 {
9c4b5c13 694 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
c9baafe7 695 goto fail;
696 }
697
698 file = grub_file_open (argv[0]);
699 if (! file)
87a4623b 700 goto fail;
c9baafe7 701
5c5215d5 702 if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
c9baafe7 703 {
7a45a539 704 if (!grub_errno)
9c4b5c13 705 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
7a45a539 706 argv[0]);
c9baafe7 707 goto fail;
708 }
709
ec824e0f 710 if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
c9baafe7 711 {
712 grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
713 goto fail;
714 }
715
716 if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
717 {
718 grub_error (GRUB_ERR_BAD_OS, "too many setup sectors");
719 goto fail;
720 }
721
9bab65c4
VS
722 /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
723 still not support 32-bit boot. */
3245f02d 724 if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
9bab65c4 725 || grub_le_to_cpu16 (lh.version) < 0x0203)
c9baafe7 726 {
9bab65c4 727 grub_error (GRUB_ERR_BAD_OS, "version too old for 32-bit boot"
92f33540 728#ifdef GRUB_MACHINE_PCBIOS
729 " (try with `linux16')"
730#endif
731 );
c9baafe7 732 goto fail;
733 }
734
9bab65c4 735 if (! (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL))
c9baafe7 736 {
9bab65c4 737 grub_error (GRUB_ERR_BAD_OS, "zImage doesn't support 32-bit boot"
92f33540 738#ifdef GRUB_MACHINE_PCBIOS
739 " (try with `linux16')"
740#endif
741 );
c9baafe7 742 goto fail;
743 }
744
a06eb03a
VS
745 if (grub_le_to_cpu16 (lh.version) >= 0x0206)
746 maximal_cmdline_size = grub_le_to_cpu32 (lh.cmdline_size) + 1;
747 else
748 maximal_cmdline_size = 256;
749
750 if (maximal_cmdline_size < 128)
751 maximal_cmdline_size = 128;
752
c9baafe7 753 setup_sects = lh.setup_sects;
b39f9d20 754
c9baafe7 755 /* If SETUP_SECTS is not set, set it to the default (4). */
756 if (! setup_sects)
757 setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
758
759 real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
7c8d0ce7 760 prot_file_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
b39f9d20 761
9be4c45d
VS
762 if (grub_le_to_cpu16 (lh.version) >= 0x205
763 && lh.kernel_alignment != 0
764 && ((lh.kernel_alignment - 1) & lh.kernel_alignment) == 0)
765 {
766 for (align = 0; align < 32; align++)
767 if (grub_le_to_cpu32 (lh.kernel_alignment) & (1 << align))
768 break;
769 relocatable = grub_le_to_cpu32 (lh.relocatable);
770 }
771 else
772 {
773 align = 0;
774 relocatable = 0;
775 }
776
777 if (grub_le_to_cpu16 (lh.version) >= 0x020a)
778 {
779 min_align = lh.min_alignment;
780 prot_size = grub_le_to_cpu32 (lh.init_size);
807fb77c 781 prot_init_space = page_align (prot_size);
2282da4a 782 if (relocatable)
39525c22 783 preferred_address = grub_le_to_cpu64 (lh.pref_address);
2282da4a 784 else
39525c22 785 preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
9be4c45d
VS
786 }
787 else
788 {
807fb77c 789 min_align = align;
7c8d0ce7 790 prot_size = prot_file_size;
39525c22 791 preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
807fb77c
VS
792 /* Usually, the compression ratio is about 50%. */
793 prot_init_space = page_align (prot_size) * 3;
9be4c45d
VS
794 }
795
796 if (allocate_pages (prot_size, &align,
797 min_align, relocatable,
39525c22 798 preferred_address))
c9baafe7 799 goto fail;
b39f9d20 800
cc2fa5ec
VS
801 grub_memset (&linux_params, 0, sizeof (linux_params));
802 grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
c9baafe7 803
cc2fa5ec
VS
804 linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
805 linux_params.kernel_alignment = (1 << align);
806 linux_params.ps_mouse = linux_params.padding10 = 0;
c9baafe7 807
cc2fa5ec
VS
808 len = sizeof (linux_params) - sizeof (lh);
809 if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
c9baafe7 810 {
7a45a539 811 if (!grub_errno)
9c4b5c13 812 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
7a45a539 813 argv[0]);
c9baafe7 814 goto fail;
815 }
816
cc2fa5ec 817 linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
c9baafe7 818
b09db61d 819 /* These two are used (instead of cmd_line_ptr) by older versions of Linux,
820 and otherwise ignored. */
cc2fa5ec
VS
821 linux_params.cl_magic = GRUB_LINUX_CL_MAGIC;
822 linux_params.cl_offset = 0x1000;
b09db61d 823
cc2fa5ec
VS
824 linux_params.ramdisk_image = 0;
825 linux_params.ramdisk_size = 0;
c9baafe7 826
cc2fa5ec
VS
827 linux_params.heap_end_ptr = GRUB_LINUX_HEAP_END_OFFSET;
828 linux_params.loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
c9baafe7 829
830 /* These are not needed to be precise, because Linux uses these values
831 only to raise an error when the decompression code cannot find good
832 space. */
cc2fa5ec
VS
833 linux_params.ext_mem = ((32 * 0x100000) >> 10);
834 linux_params.alt_mem = ((32 * 0x100000) >> 10);
b39f9d20 835
b09db61d 836 /* Ignored by Linux. */
cc2fa5ec 837 linux_params.video_page = 0;
b09db61d 838
b09db61d 839 /* Only used when `video_mode == 0x7', otherwise ignored. */
cc2fa5ec 840 linux_params.video_ega_bx = 0;
b09db61d 841
cc2fa5ec 842 linux_params.font_size = 16; /* XXX */
c9baafe7 843
c8142599 844#ifdef GRUB_MACHINE_EFI
bcc75fb3 845#ifdef __x86_64__
cc2fa5ec 846 if (grub_le_to_cpu16 (linux_params.version) < 0x0208 &&
bcc75fb3
PJ
847 ((grub_addr_t) grub_efi_system_table >> 32) != 0)
848 return grub_error(GRUB_ERR_BAD_OS,
849 "kernel does not support 64-bit addressing");
850#endif
851
cc2fa5ec 852 if (grub_le_to_cpu16 (linux_params.version) >= 0x0208)
c8142599 853 {
cc2fa5ec 854 linux_params.v0208.efi_signature = GRUB_LINUX_EFI_SIGNATURE;
111bf5db 855 linux_params.v0208.efi_system_table = (grub_uint32_t) (grub_addr_t) grub_efi_system_table;
c8142599 856#ifdef __x86_64__
cc2fa5ec 857 linux_params.v0208.efi_system_table_hi = (grub_uint32_t) ((grub_uint64_t) grub_efi_system_table >> 32);
c8142599
VS
858#endif
859 }
cc2fa5ec 860 else if (grub_le_to_cpu16 (linux_params.version) >= 0x0206)
bcc75fb3 861 {
cc2fa5ec 862 linux_params.v0206.efi_signature = GRUB_LINUX_EFI_SIGNATURE;
111bf5db 863 linux_params.v0206.efi_system_table = (grub_uint32_t) (grub_addr_t) grub_efi_system_table;
bcc75fb3 864 }
cc2fa5ec 865 else if (grub_le_to_cpu16 (linux_params.version) >= 0x0204)
c8142599 866 {
cc2fa5ec 867 linux_params.v0204.efi_signature = GRUB_LINUX_EFI_SIGNATURE_0204;
111bf5db 868 linux_params.v0204.efi_system_table = (grub_uint32_t) (grub_addr_t) grub_efi_system_table;
c8142599
VS
869 }
870#endif
871
c9baafe7 872 /* The other parameters are filled when booting. */
873
874 grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
875
1a064917
RM
876 grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
877 (unsigned) real_size, (unsigned) prot_size);
c9baafe7 878
1007d1f5 879 /* Look for memory size and video mode specified on the command line. */
c9baafe7 880 linux_mem_size = 0;
c9baafe7 881 for (i = 1; i < argc; i++)
b01f0548 882#ifdef GRUB_MACHINE_PCBIOS
9c323f09 883 if (grub_memcmp (argv[i], "vga=", 4) == 0)
884 {
885 /* Video mode selection support. */
886 char *val = argv[i] + 4;
3eb5ed4e 887 unsigned vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
e31bb619 888 struct grub_vesa_mode_table_entry *linux_mode;
3eb5ed4e 889 grub_err_t err;
890 char *buf;
9c323f09 891
2b36fbf4
VS
892 grub_dl_load ("vbe");
893
9c323f09 894 if (grub_strcmp (val, "normal") == 0)
895 vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
896 else if (grub_strcmp (val, "ext") == 0)
897 vid_mode = GRUB_LINUX_VID_MODE_EXTENDED;
1007d1f5 898 else if (grub_strcmp (val, "ask") == 0)
cba416eb 899 {
10f0117b 900 grub_puts_ (N_("Legacy `ask' parameter no longer supported."));
6c67de15 901
902 /* We usually would never do this in a loader, but "vga=ask" means user
903 requested interaction, so it can't hurt to request keyboard input. */
904 grub_wait_after_message ();
905
cba416eb 906 goto fail;
907 }
9c323f09 908 else
909 vid_mode = (grub_uint16_t) grub_strtoul (val, 0, 0);
910
112972a9 911 switch (vid_mode)
912 {
913 case 0:
3eb5ed4e 914 case GRUB_LINUX_VID_MODE_NORMAL:
915 grub_env_set ("gfxpayload", "text");
10f0117b 916 grub_printf_ (N_("%s is deprecated. "
67093bc0 917 "Use set gfxpayload=%s before "
972765fe
AB
918 "linux command instead.\n"),
919 argv[i], "text");
112972a9 920 break;
3eb5ed4e 921
112972a9 922 case 1:
3eb5ed4e 923 case GRUB_LINUX_VID_MODE_EXTENDED:
924 /* FIXME: support 80x50 text. */
925 grub_env_set ("gfxpayload", "text");
10f0117b 926 grub_printf_ (N_("%s is deprecated. "
67093bc0 927 "Use set gfxpayload=%s before "
972765fe
AB
928 "linux command instead.\n"),
929 argv[i], "text");
112972a9 930 break;
931 default:
932 /* Ignore invalid values. */
e31bb619
VS
933 if (vid_mode < GRUB_VESA_MODE_TABLE_START ||
934 vid_mode > GRUB_VESA_MODE_TABLE_END)
3eb5ed4e 935 {
936 grub_env_set ("gfxpayload", "text");
6e69da9c
VS
937 /* TRANSLATORS: "x" has to be entered in, like an identifier,
938 so please don't use better Unicode codepoints. */
8822a8a0 939 grub_printf_ (N_("%s is deprecated. VGA mode %d isn't recognized. "
10f0117b
VS
940 "Use set gfxpayload=WIDTHxHEIGHT[xDEPTH] "
941 "before linux command instead.\n"),
b39f9d20 942 argv[i], vid_mode);
3eb5ed4e 943 break;
944 }
945
e31bb619
VS
946 linux_mode = &grub_vesa_mode_table[vid_mode
947 - GRUB_VESA_MODE_TABLE_START];
b39f9d20 948
61eb45ee 949 buf = grub_xasprintf ("%ux%ux%u,%ux%u",
e31bb619 950 linux_mode->width, linux_mode->height,
8b442f3f 951 linux_mode->depth,
e31bb619 952 linux_mode->width, linux_mode->height);
8b442f3f
VS
953 if (! buf)
954 goto fail;
955
10f0117b
VS
956 grub_printf_ (N_("%s is deprecated. "
957 "Use set gfxpayload=%s before "
958 "linux command instead.\n"),
b39f9d20 959 argv[i], buf);
3eb5ed4e 960 err = grub_env_set ("gfxpayload", buf);
961 grub_free (buf);
962 if (err)
963 goto fail;
112972a9 964 }
9c323f09 965 }
7c1d00cd 966 else
b01f0548 967#endif /* GRUB_MACHINE_PCBIOS */
7c1d00cd 968 if (grub_memcmp (argv[i], "mem=", 4) == 0)
c9baafe7 969 {
970 char *val = argv[i] + 4;
b39f9d20 971
c9baafe7 972 linux_mem_size = grub_strtoul (val, &val, 0);
b39f9d20 973
c9baafe7 974 if (grub_errno)
975 {
976 grub_errno = GRUB_ERR_NONE;
977 linux_mem_size = 0;
978 }
979 else
980 {
981 int shift = 0;
b39f9d20 982
c9baafe7 983 switch (grub_tolower (val[0]))
984 {
985 case 'g':
986 shift += 10;
4bd4a887 987 /* FALLTHROUGH */
c9baafe7 988 case 'm':
989 shift += 10;
4bd4a887 990 /* FALLTHROUGH */
c9baafe7 991 case 'k':
992 shift += 10;
4bd4a887 993 /* FALLTHROUGH */
c9baafe7 994 default:
995 break;
996 }
997
998 /* Check an overflow. */
999 if (linux_mem_size > (~0UL >> shift))
1000 linux_mem_size = 0;
1001 else
1002 linux_mem_size <<= shift;
1003 }
1004 }
28333ad0 1005 else if (grub_memcmp (argv[i], "quiet", sizeof ("quiet") - 1) == 0)
1006 {
cc2fa5ec 1007 linux_params.loadflags |= GRUB_LINUX_FLAG_QUIET;
28333ad0 1008 }
1009
25953e10 1010 /* Create kernel command line. */
a4e5ca80
VS
1011 linux_cmdline = grub_zalloc (maximal_cmdline_size + 1);
1012 if (!linux_cmdline)
1013 goto fail;
1014 grub_memcpy (linux_cmdline, LINUX_IMAGE, sizeof (LINUX_IMAGE));
25953e10 1015 grub_create_loader_cmdline (argc, argv,
a4e5ca80 1016 linux_cmdline
25953e10 1017 + sizeof (LINUX_IMAGE) - 1,
05caa461 1018 maximal_cmdline_size
25953e10 1019 - (sizeof (LINUX_IMAGE) - 1));
c9baafe7 1020
7c8d0ce7 1021 len = prot_file_size;
7a45a539 1022 if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
9c4b5c13 1023 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
7a45a539 1024 argv[0]);
c9baafe7 1025
1026 if (grub_errno == GRUB_ERR_NONE)
1027 {
a9368fd3 1028 grub_loader_set (grub_linux_boot, grub_linux_unload,
14aad807 1029 0 /* set noreturn=0 in order to avoid grub_console_fini() */);
c9baafe7 1030 loaded = 1;
1031 }
1032
1033 fail:
b39f9d20 1034
c9baafe7 1035 if (file)
1036 grub_file_close (file);
1037
1038 if (grub_errno != GRUB_ERR_NONE)
1039 {
1040 grub_dl_unref (my_mod);
1041 loaded = 0;
1042 }
b1b797cb 1043
1044 return grub_errno;
c9baafe7 1045}
1046
b1b797cb 1047static grub_err_t
1048grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
1049 int argc, char *argv[])
c9baafe7 1050{
e5b4ba8c 1051 grub_size_t size = 0, aligned_size = 0;
c9baafe7 1052 grub_addr_t addr_min, addr_max;
1053 grub_addr_t addr;
55b40bc6 1054 grub_err_t err;
9a67e1ac 1055 struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
b39f9d20 1056
c9baafe7 1057 if (argc == 0)
1058 {
9c4b5c13 1059 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
c9baafe7 1060 goto fail;
1061 }
b39f9d20 1062
c9baafe7 1063 if (! loaded)
1064 {
9c4b5c13 1065 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
c9baafe7 1066 goto fail;
1067 }
1068
92750e4c 1069 if (grub_initrd_init (argc, argv, &initrd_ctx))
c9baafe7 1070 goto fail;
1071
92750e4c 1072 size = grub_get_initrd_size (&initrd_ctx);
e5b4ba8c 1073 aligned_size = ALIGN_UP (size, 4096);
c9baafe7 1074
92907110 1075 /* Get the highest address available for the initrd. */
8645f72c 1076 if (grub_le_to_cpu16 (linux_params.version) >= 0x0203)
92907110 1077 {
8645f72c 1078 addr_max = grub_cpu_to_le32 (linux_params.initrd_addr_max);
92907110 1079
1080 /* XXX in reality, Linux specifies a bogus value, so
1081 it is necessary to make sure that ADDR_MAX does not exceed
1082 0x3fffffff. */
1083 if (addr_max > GRUB_LINUX_INITRD_MAX_ADDRESS)
1084 addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
1085 }
1086 else
1087 addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
b39f9d20 1088
c9baafe7 1089 if (linux_mem_size != 0 && linux_mem_size < addr_max)
1090 addr_max = linux_mem_size;
b39f9d20 1091
c9baafe7 1092 /* Linux 2.3.xx has a bug in the memory range check, so avoid
1093 the last page.
1094 Linux 2.2.xx has a bug in the memory range check, which is
1095 worse than that of Linux 2.3.xx, so avoid the last 64kb. */
1096 addr_max -= 0x10000;
1097
6a0debbd 1098 addr_min = (grub_addr_t) prot_mode_target + prot_init_space;
b39f9d20 1099
92907110 1100 /* Put the initrd as high as possible, 4KiB aligned. */
e5b4ba8c 1101 addr = (addr_max - aligned_size) & ~0xFFF;
c9baafe7 1102
92907110 1103 if (addr < addr_min)
c9baafe7 1104 {
7fd0baee 1105 grub_error (GRUB_ERR_OUT_OF_RANGE, "the initrd is too big");
c9baafe7 1106 goto fail;
1107 }
b39f9d20 1108
4b2ec20b
VS
1109 {
1110 grub_relocator_chunk_t ch;
1111 err = grub_relocator_alloc_chunk_align (relocator, &ch,
e5b4ba8c
VS
1112 addr_min, addr, aligned_size,
1113 0x1000,
9be4c45d
VS
1114 GRUB_RELOCATOR_PREFERENCE_HIGH,
1115 1);
4b2ec20b
VS
1116 if (err)
1117 return err;
1118 initrd_mem = get_virtual_current_address (ch);
1119 initrd_mem_target = get_physical_target_address (ch);
1120 }
b39f9d20 1121
92750e4c
VS
1122 if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
1123 goto fail;
c9baafe7 1124
1a064917
RM
1125 grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n",
1126 (unsigned) addr, (unsigned) size);
b39f9d20 1127
8645f72c
VS
1128 linux_params.ramdisk_image = initrd_mem_target;
1129 linux_params.ramdisk_size = size;
1130 linux_params.root_dev = 0x0100; /* XXX */
b39f9d20 1131
c9baafe7 1132 fail:
92750e4c 1133 grub_initrd_close (&initrd_ctx);
b1b797cb 1134
1135 return grub_errno;
c9baafe7 1136}
1137
b1b797cb 1138static grub_command_t cmd_linux, cmd_initrd;
c9baafe7 1139
1140GRUB_MOD_INIT(linux)
1141{
b1b797cb 1142 cmd_linux = grub_register_command ("linux", grub_cmd_linux,
809bbfeb 1143 0, N_("Load Linux."));
b1b797cb 1144 cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
809bbfeb 1145 0, N_("Load initrd."));
c9baafe7 1146 my_mod = mod;
1147}
1148
1149GRUB_MOD_FINI(linux)
1150{
b1b797cb 1151 grub_unregister_command (cmd_linux);
1152 grub_unregister_command (cmd_initrd);
c9baafe7 1153}