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