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