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