]> git.proxmox.com Git - grub2.git/blob - 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
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, Inc.
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>
20 #include <grub/memory.h>
21 #include <grub/normal.h>
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>
27 #include <grub/dl.h>
28 #include <grub/mm.h>
29 #include <grub/term.h>
30 #include <grub/cpu/linux.h>
31 #include <grub/video.h>
32 #include <grub/video_fb.h>
33 #include <grub/command.h>
34 #include <grub/i386/relocator.h>
35 #include <grub/i18n.h>
36 #include <grub/lib/cmdline.h>
37 #include <grub/linux.h>
38
39 GRUB_MOD_LICENSE ("GPLv3+");
40
41 #ifdef GRUB_MACHINE_PCBIOS
42 #include <grub/i386/pc/vesa_modes_table.h>
43 #endif
44
45 #ifdef GRUB_MACHINE_EFI
46 #include <grub/efi/efi.h>
47 #define HAS_VGA_TEXT 0
48 #define DEFAULT_VIDEO_MODE "auto"
49 #define ACCEPTS_PURE_TEXT 0
50 #elif defined (GRUB_MACHINE_IEEE1275)
51 #include <grub/ieee1275/ieee1275.h>
52 #define HAS_VGA_TEXT 0
53 #define DEFAULT_VIDEO_MODE "text"
54 #define ACCEPTS_PURE_TEXT 1
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"
60 #define ACCEPTS_PURE_TEXT 1
61 #endif
62
63 static grub_dl_t my_mod;
64
65 static grub_size_t linux_mem_size;
66 static int loaded;
67 static void *prot_mode_mem;
68 static grub_addr_t prot_mode_target;
69 static void *initrd_mem;
70 static grub_addr_t initrd_mem_target;
71 static grub_size_t prot_init_space;
72 static grub_uint32_t initrd_pages;
73 static struct grub_relocator *relocator = NULL;
74 static void *efi_mmap_buf;
75 static grub_size_t maximal_cmdline_size;
76 static struct linux_kernel_params linux_params;
77 static char *linux_cmdline;
78 #ifdef GRUB_MACHINE_EFI
79 static grub_efi_uintn_t efi_mmap_size;
80 #else
81 static const grub_size_t efi_mmap_size = 0;
82 #endif
83
84 /* FIXME */
85 #if 0
86 struct idt_descriptor
87 {
88 grub_uint16_t limit;
89 void *base;
90 } GRUB_PACKED;
91
92 static struct idt_descriptor idt_desc =
93 {
94 0,
95 0
96 };
97 #endif
98
99 static inline grub_size_t
100 page_align (grub_size_t size)
101 {
102 return (size + (1 << 12) - 1) & (~((1 << 12) - 1));
103 }
104
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? */
108 static grub_efi_uintn_t
109 find_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;
122 grub_efi_uintn_t cur_mmap_size = mmap_size;
123
124 mmap = grub_malloc (cur_mmap_size);
125 if (! mmap)
126 return 0;
127
128 ret = grub_efi_get_memory_map (&cur_mmap_size, mmap, 0, &desc_size, 0);
129 grub_free (mmap);
130
131 if (ret < 0)
132 {
133 grub_error (GRUB_ERR_IO, "cannot get memory map");
134 return 0;
135 }
136 else if (ret > 0)
137 break;
138
139 if (mmap_size < cur_mmap_size)
140 mmap_size = cur_mmap_size;
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. */
146 mmap_size += (3 << 12);
147
148 mmap_size = page_align (mmap_size);
149 return mmap_size;
150 }
151
152 #endif
153
154 /* Helper for find_mmap_size. */
155 static int
156 count_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
166 /* Find the optimal number of pages for the memory map. */
167 static grub_size_t
168 find_mmap_size (void)
169 {
170 grub_size_t count = 0, mmap_size;
171
172 grub_mmap_iterate (count_hook, &count);
173
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);
179
180 return page_align (mmap_size);
181 }
182
183 static void
184 free_pages (void)
185 {
186 grub_relocator_unload (relocator);
187 relocator = NULL;
188 prot_mode_mem = initrd_mem = 0;
189 prot_mode_target = initrd_mem_target = 0;
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. */
194 static grub_err_t
195 allocate_pages (grub_size_t prot_size, grub_size_t *align,
196 grub_size_t min_align, int relocatable,
197 grub_uint64_t preferred_address)
198 {
199 grub_err_t err;
200
201 if (prot_size == 0)
202 prot_size = 1;
203
204 prot_size = page_align (prot_size);
205
206 /* Initialize the memory pointers with NULL for convenience. */
207 free_pages ();
208
209 relocator = grub_relocator_new ();
210 if (!relocator)
211 {
212 err = grub_errno;
213 goto fail;
214 }
215
216 /* FIXME: Should request low memory from the heap when this feature is
217 implemented. */
218
219 {
220 grub_relocator_chunk_t ch;
221 if (relocatable)
222 {
223 err = grub_relocator_alloc_chunk_align (relocator, &ch,
224 preferred_address,
225 preferred_address,
226 prot_size, 1,
227 GRUB_RELOCATOR_PREFERENCE_LOW,
228 1);
229 for (; err && *align + 1 > min_align; (*align)--)
230 {
231 grub_errno = GRUB_ERR_NONE;
232 err = grub_relocator_alloc_chunk_align (relocator, &ch,
233 0x1000000,
234 0xffffffff & ~prot_size,
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,
244 preferred_address,
245 prot_size);
246 if (err)
247 goto fail;
248 prot_mode_mem = get_virtual_current_address (ch);
249 prot_mode_target = get_physical_target_address (ch);
250 }
251
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,
254 (unsigned) prot_size);
255 return GRUB_ERR_NONE;
256
257 fail:
258 free_pages ();
259 return err;
260 }
261
262 static grub_err_t
263 grub_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
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 }
279 return GRUB_ERR_NONE;
280 }
281
282 static grub_err_t
283 grub_linux_setup_video (struct linux_kernel_params *params)
284 {
285 struct grub_video_mode_info mode_info;
286 void *framebuffer;
287 grub_err_t err;
288 grub_video_driver_id_t driver_id;
289 const char *gfxlfbvar = grub_env_get ("gfxpayloadforcelfb");
290
291 driver_id = grub_video_get_driver_id ();
292
293 if (driver_id == GRUB_VIDEO_DRIVER_NONE)
294 return 1;
295
296 err = grub_video_get_info_and_fini (&mode_info, &framebuffer);
297
298 if (err)
299 {
300 grub_errno = GRUB_ERR_NONE;
301 return 1;
302 }
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
309 params->lfb_base = (grub_size_t) framebuffer;
310 params->lfb_size = ALIGN_UP (params->lfb_line_len * params->lfb_height, 65536);
311
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;
320
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:
339 case GRUB_VIDEO_DRIVER_SIS315PRO:
340 case GRUB_VIDEO_DRIVER_VGA:
341 case GRUB_VIDEO_DRIVER_CIRRUS:
342 case GRUB_VIDEO_DRIVER_BOCHS:
343 case GRUB_VIDEO_DRIVER_RADEON_FULOONG2E:
344 case GRUB_VIDEO_DRIVER_RADEON_YEELOONG3A:
345 case GRUB_VIDEO_DRIVER_IEEE1275:
346 case GRUB_VIDEO_DRIVER_COREBOOT:
347 /* Make gcc happy. */
348 case GRUB_VIDEO_DRIVER_XEN:
349 case GRUB_VIDEO_DRIVER_SDL:
350 case GRUB_VIDEO_DRIVER_NONE:
351 case GRUB_VIDEO_ADAPTER_CAPTURE:
352 params->have_vga = GRUB_VIDEO_LINUX_TYPE_SIMPLE;
353 break;
354 }
355 }
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. */
361 if (driver_id == GRUB_VIDEO_DRIVER_VBE && mode_info.bpp <= 8)
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
383 return GRUB_ERR_NONE;
384 }
385
386 /* Context for grub_linux_boot. */
387 struct 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. */
396 static int
397 grub_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
429 /* GRUB types conveniently match E820 types. */
430 static int
431 grub_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
436 if (grub_e820_add_region (ctx->params->e820_map, &ctx->e820_num,
437 addr, size, type))
438 return 1;
439
440 return 0;
441 }
442
443 static grub_err_t
444 grub_linux_boot (void)
445 {
446 grub_err_t err = 0;
447 const char *modevar;
448 char *tmp;
449 struct grub_relocator32_state state;
450 void *real_mode_mem;
451 struct grub_linux_boot_ctx ctx = {
452 .real_mode_target = 0
453 };
454 grub_size_t mmap_size;
455 grub_size_t cl_offset;
456
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 }
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 {
498 #if ACCEPTS_PURE_TEXT
499 err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0);
500 #else
501 err = grub_video_set_mode (DEFAULT_VIDEO_MODE,
502 GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
503 #endif
504 }
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 {
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;
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
555 mmap_size = find_mmap_size ();
556 /* Make sure that each size is aligned to a page boundary. */
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
560 << GRUB_DISK_SECTOR_BITS), 4096);
561 ctx.real_size = ALIGN_UP (cl_offset + maximal_cmdline_size, 4096);
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",
570 (unsigned) ctx.real_size, (unsigned) mmap_size);
571
572 #ifdef GRUB_MACHINE_EFI
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);
576 #else
577 grub_mmap_iterate (grub_linux_boot_mmap_find, &ctx);
578 #endif
579 grub_dprintf ("linux", "real_mode_target = %lx, real_size = %x, efi_mmap_size = %x\n",
580 (unsigned long) ctx.real_mode_target,
581 (unsigned) ctx.real_size,
582 (unsigned) efi_mmap_size);
583
584 if (! ctx.real_mode_target)
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,
590 ctx.real_mode_target,
591 (ctx.real_size + efi_mmap_size));
592 if (err)
593 return err;
594 real_mode_mem = get_virtual_current_address (ch);
595 }
596 efi_mmap_buf = (grub_uint8_t *) real_mode_mem + ctx.real_size;
597
598 grub_dprintf ("linux", "real_mode_mem = %p\n",
599 real_mode_mem);
600
601 ctx.params = real_mode_mem;
602
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,
606 maximal_cmdline_size);
607
608 grub_dprintf ("linux", "code32_start = %x\n",
609 (unsigned) ctx.params->code32_start);
610
611 ctx.e820_num = 0;
612 if (grub_mmap_iterate (grub_linux_boot_mmap_fill, &ctx))
613 return grub_errno;
614 ctx.params->mmap_size = ctx.e820_num;
615
616 #ifdef GRUB_MACHINE_EFI
617 {
618 grub_efi_uintn_t efi_desc_size;
619 grub_size_t efi_mmap_target;
620 grub_efi_uint32_t efi_desc_version;
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;
625
626 /* Note that no boot services are available from here. */
627 efi_mmap_target = ctx.real_mode_target
628 + ((grub_uint8_t *) efi_mmap_buf - (grub_uint8_t *) real_mode_mem);
629 /* Pass EFI parameters. */
630 if (grub_le_to_cpu16 (ctx.params->version) >= 0x0208)
631 {
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;
636
637 #ifdef __x86_64__
638 ctx.params->v0208.efi_mmap_hi = (efi_mmap_target >> 32);
639 #endif
640 }
641 else if (grub_le_to_cpu16 (ctx.params->version) >= 0x0206)
642 {
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;
647 }
648 else if (grub_le_to_cpu16 (ctx.params->version) >= 0x0204)
649 {
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;
654 }
655 }
656 #endif
657
658 /* FIXME. */
659 /* asm volatile ("lidt %0" : : "m" (idt_desc)); */
660 state.ebp = state.edi = state.ebx = 0;
661 state.esi = ctx.real_mode_target;
662 state.esp = ctx.real_mode_target;
663 state.eip = ctx.params->code32_start;
664 return grub_relocator32_boot (relocator, state, 0);
665 }
666
667 static grub_err_t
668 grub_linux_unload (void)
669 {
670 grub_dl_unref (my_mod);
671 loaded = 0;
672 grub_free (linux_cmdline);
673 linux_cmdline = 0;
674 return GRUB_ERR_NONE;
675 }
676
677 static grub_err_t
678 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
679 int argc, char *argv[])
680 {
681 grub_file_t file = 0;
682 struct linux_kernel_header lh;
683 grub_uint8_t setup_sects;
684 grub_size_t real_size, prot_size, prot_file_size;
685 grub_ssize_t len;
686 int i;
687 grub_size_t align, min_align;
688 int relocatable;
689 grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
690
691 grub_dl_ref (my_mod);
692
693 if (argc == 0)
694 {
695 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
696 goto fail;
697 }
698
699 file = grub_file_open (argv[0]);
700 if (! file)
701 goto fail;
702
703 if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
704 {
705 if (!grub_errno)
706 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
707 argv[0]);
708 goto fail;
709 }
710
711 if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
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
723 /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
724 still not support 32-bit boot. */
725 if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE)
726 || grub_le_to_cpu16 (lh.version) < 0x0203)
727 {
728 grub_error (GRUB_ERR_BAD_OS, "version too old for 32-bit boot"
729 #ifdef GRUB_MACHINE_PCBIOS
730 " (try with `linux16')"
731 #endif
732 );
733 goto fail;
734 }
735
736 if (! (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL))
737 {
738 grub_error (GRUB_ERR_BAD_OS, "zImage doesn't support 32-bit boot"
739 #ifdef GRUB_MACHINE_PCBIOS
740 " (try with `linux16')"
741 #endif
742 );
743 goto fail;
744 }
745
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
754 setup_sects = lh.setup_sects;
755
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;
761 prot_file_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
762
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);
782 prot_init_space = page_align (prot_size);
783 if (relocatable)
784 preferred_address = grub_le_to_cpu64 (lh.pref_address);
785 else
786 preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
787 }
788 else
789 {
790 min_align = align;
791 prot_size = prot_file_size;
792 preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
793 /* Usually, the compression ratio is about 50%. */
794 prot_init_space = page_align (prot_size) * 3;
795 }
796
797 if (allocate_pages (prot_size, &align,
798 min_align, relocatable,
799 preferred_address))
800 goto fail;
801
802 grub_memset (&linux_params, 0, sizeof (linux_params));
803 grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
804
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;
808
809 len = sizeof (linux_params) - sizeof (lh);
810 if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
811 {
812 if (!grub_errno)
813 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
814 argv[0]);
815 goto fail;
816 }
817
818 linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
819
820 /* These two are used (instead of cmd_line_ptr) by older versions of Linux,
821 and otherwise ignored. */
822 linux_params.cl_magic = GRUB_LINUX_CL_MAGIC;
823 linux_params.cl_offset = 0x1000;
824
825 linux_params.ramdisk_image = 0;
826 linux_params.ramdisk_size = 0;
827
828 linux_params.heap_end_ptr = GRUB_LINUX_HEAP_END_OFFSET;
829 linux_params.loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
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. */
834 linux_params.ext_mem = ((32 * 0x100000) >> 10);
835 linux_params.alt_mem = ((32 * 0x100000) >> 10);
836
837 /* Ignored by Linux. */
838 linux_params.video_page = 0;
839
840 /* Only used when `video_mode == 0x7', otherwise ignored. */
841 linux_params.video_ega_bx = 0;
842
843 linux_params.font_size = 16; /* XXX */
844
845 #ifdef GRUB_MACHINE_EFI
846 #ifdef __x86_64__
847 if (grub_le_to_cpu16 (linux_params.version) < 0x0208 &&
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
853 if (grub_le_to_cpu16 (linux_params.version) >= 0x0208)
854 {
855 linux_params.v0208.efi_signature = GRUB_LINUX_EFI_SIGNATURE;
856 linux_params.v0208.efi_system_table = (grub_uint32_t) (grub_addr_t) grub_efi_system_table;
857 #ifdef __x86_64__
858 linux_params.v0208.efi_system_table_hi = (grub_uint32_t) ((grub_uint64_t) grub_efi_system_table >> 32);
859 #endif
860 }
861 else if (grub_le_to_cpu16 (linux_params.version) >= 0x0206)
862 {
863 linux_params.v0206.efi_signature = GRUB_LINUX_EFI_SIGNATURE;
864 linux_params.v0206.efi_system_table = (grub_uint32_t) (grub_addr_t) grub_efi_system_table;
865 }
866 else if (grub_le_to_cpu16 (linux_params.version) >= 0x0204)
867 {
868 linux_params.v0204.efi_signature = GRUB_LINUX_EFI_SIGNATURE_0204;
869 linux_params.v0204.efi_system_table = (grub_uint32_t) (grub_addr_t) grub_efi_system_table;
870 }
871 #endif
872
873 /* The other parameters are filled when booting. */
874
875 grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
876
877 grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
878 (unsigned) real_size, (unsigned) prot_size);
879
880 /* Look for memory size and video mode specified on the command line. */
881 linux_mem_size = 0;
882 for (i = 1; i < argc; i++)
883 #ifdef GRUB_MACHINE_PCBIOS
884 if (grub_memcmp (argv[i], "vga=", 4) == 0)
885 {
886 /* Video mode selection support. */
887 char *val = argv[i] + 4;
888 unsigned vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
889 struct grub_vesa_mode_table_entry *linux_mode;
890 grub_err_t err;
891 char *buf;
892
893 grub_dl_load ("vbe");
894
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;
899 else if (grub_strcmp (val, "ask") == 0)
900 {
901 grub_puts_ (N_("Legacy `ask' parameter no longer supported."));
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
907 goto fail;
908 }
909 else
910 vid_mode = (grub_uint16_t) grub_strtoul (val, 0, 0);
911
912 switch (vid_mode)
913 {
914 case 0:
915 case GRUB_LINUX_VID_MODE_NORMAL:
916 grub_env_set ("gfxpayload", "text");
917 grub_printf_ (N_("%s is deprecated. "
918 "Use set gfxpayload=%s before "
919 "linux command instead.\n"), "text",
920 argv[i]);
921 break;
922
923 case 1:
924 case GRUB_LINUX_VID_MODE_EXTENDED:
925 /* FIXME: support 80x50 text. */
926 grub_env_set ("gfxpayload", "text");
927 grub_printf_ (N_("%s is deprecated. "
928 "Use set gfxpayload=%s before "
929 "linux command instead.\n"), "text",
930 argv[i]);
931 break;
932 default:
933 /* Ignore invalid values. */
934 if (vid_mode < GRUB_VESA_MODE_TABLE_START ||
935 vid_mode > GRUB_VESA_MODE_TABLE_END)
936 {
937 grub_env_set ("gfxpayload", "text");
938 /* TRANSLATORS: "x" has to be entered in, like an identifier,
939 so please don't use better Unicode codepoints. */
940 grub_printf_ (N_("%s is deprecated. VGA mode %d isn't recognized. "
941 "Use set gfxpayload=WIDTHxHEIGHT[xDEPTH] "
942 "before linux command instead.\n"),
943 argv[i], vid_mode);
944 break;
945 }
946
947 linux_mode = &grub_vesa_mode_table[vid_mode
948 - GRUB_VESA_MODE_TABLE_START];
949
950 buf = grub_xasprintf ("%ux%ux%u,%ux%u",
951 linux_mode->width, linux_mode->height,
952 linux_mode->depth,
953 linux_mode->width, linux_mode->height);
954 if (! buf)
955 goto fail;
956
957 grub_printf_ (N_("%s is deprecated. "
958 "Use set gfxpayload=%s before "
959 "linux command instead.\n"),
960 argv[i], buf);
961 err = grub_env_set ("gfxpayload", buf);
962 grub_free (buf);
963 if (err)
964 goto fail;
965 }
966 }
967 else
968 #endif /* GRUB_MACHINE_PCBIOS */
969 if (grub_memcmp (argv[i], "mem=", 4) == 0)
970 {
971 char *val = argv[i] + 4;
972
973 linux_mem_size = grub_strtoul (val, &val, 0);
974
975 if (grub_errno)
976 {
977 grub_errno = GRUB_ERR_NONE;
978 linux_mem_size = 0;
979 }
980 else
981 {
982 int shift = 0;
983
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 }
1003 else if (grub_memcmp (argv[i], "quiet", sizeof ("quiet") - 1) == 0)
1004 {
1005 linux_params.loadflags |= GRUB_LINUX_FLAG_QUIET;
1006 }
1007
1008 /* Create kernel command line. */
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));
1013 grub_create_loader_cmdline (argc, argv,
1014 linux_cmdline
1015 + sizeof (LINUX_IMAGE) - 1,
1016 maximal_cmdline_size
1017 - (sizeof (LINUX_IMAGE) - 1));
1018
1019 len = prot_file_size;
1020 if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
1021 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
1022 argv[0]);
1023
1024 if (grub_errno == GRUB_ERR_NONE)
1025 {
1026 grub_loader_set (grub_linux_boot, grub_linux_unload,
1027 0 /* set noreturn=0 in order to avoid grub_console_fini() */);
1028 loaded = 1;
1029 }
1030
1031 fail:
1032
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 }
1041
1042 return grub_errno;
1043 }
1044
1045 static grub_err_t
1046 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
1047 int argc, char *argv[])
1048 {
1049 grub_size_t size = 0;
1050 grub_addr_t addr_min, addr_max;
1051 grub_addr_t addr;
1052 grub_err_t err;
1053 struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
1054
1055 if (argc == 0)
1056 {
1057 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
1058 goto fail;
1059 }
1060
1061 if (! loaded)
1062 {
1063 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
1064 goto fail;
1065 }
1066
1067 if (grub_initrd_init (argc, argv, &initrd_ctx))
1068 goto fail;
1069
1070 size = grub_get_initrd_size (&initrd_ctx);
1071
1072 initrd_pages = (page_align (size) >> 12);
1073
1074 /* Get the highest address available for the initrd. */
1075 if (grub_le_to_cpu16 (linux_params.version) >= 0x0203)
1076 {
1077 addr_max = grub_cpu_to_le32 (linux_params.initrd_addr_max);
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;
1087
1088 if (linux_mem_size != 0 && linux_mem_size < addr_max)
1089 addr_max = linux_mem_size;
1090
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
1097 addr_min = (grub_addr_t) prot_mode_target + prot_init_space;
1098
1099 /* Put the initrd as high as possible, 4KiB aligned. */
1100 addr = (addr_max - size) & ~0xFFF;
1101
1102 if (addr < addr_min)
1103 {
1104 grub_error (GRUB_ERR_OUT_OF_RANGE, "the initrd is too big");
1105 goto fail;
1106 }
1107
1108 {
1109 grub_relocator_chunk_t ch;
1110 err = grub_relocator_alloc_chunk_align (relocator, &ch,
1111 addr_min, addr, size, 0x1000,
1112 GRUB_RELOCATOR_PREFERENCE_HIGH,
1113 1);
1114 if (err)
1115 return err;
1116 initrd_mem = get_virtual_current_address (ch);
1117 initrd_mem_target = get_physical_target_address (ch);
1118 }
1119
1120 if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
1121 goto fail;
1122
1123 grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n",
1124 (unsigned) addr, (unsigned) size);
1125
1126 linux_params.ramdisk_image = initrd_mem_target;
1127 linux_params.ramdisk_size = size;
1128 linux_params.root_dev = 0x0100; /* XXX */
1129
1130 fail:
1131 grub_initrd_close (&initrd_ctx);
1132
1133 return grub_errno;
1134 }
1135
1136 static grub_command_t cmd_linux, cmd_initrd;
1137
1138 GRUB_MOD_INIT(linux)
1139 {
1140 cmd_linux = grub_register_command ("linux", grub_cmd_linux,
1141 0, N_("Load Linux."));
1142 cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
1143 0, N_("Load initrd."));
1144 my_mod = mod;
1145 }
1146
1147 GRUB_MOD_FINI(linux)
1148 {
1149 grub_unregister_command (cmd_linux);
1150 grub_unregister_command (cmd_initrd);
1151 }