make GRUB_LINUX_MAGIC_SIGNATURE architecture-specific
[grub.git] / grub-core / loader / i386 / pc / linux.c
1 /* linux.c - boot Linux zImage or bzImage */
2 /*
3  *  GRUB  --  GRand Unified Bootloader
4  *  Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010  Free Software Foundation, Inc.
5  *
6  *  GRUB is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  GRUB is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <grub/loader.h>
21 #include <grub/file.h>
22 #include <grub/err.h>
23 #include <grub/device.h>
24 #include <grub/disk.h>
25 #include <grub/misc.h>
26 #include <grub/types.h>
27 #include <grub/memory.h>
28 #include <grub/dl.h>
29 #include <grub/cpu/linux.h>
30 #include <grub/command.h>
31 #include <grub/i18n.h>
32 #include <grub/mm.h>
33 #include <grub/cpu/relocator.h>
34 #include <grub/video.h>
35 #include <grub/i386/floppy.h>
36 #include <grub/lib/cmdline.h>
37 #include <grub/linux.h>
38
39 GRUB_MOD_LICENSE ("GPLv3+");
40
41 #define GRUB_LINUX_CL_OFFSET            0x9000
42
43 static grub_dl_t my_mod;
44
45 static grub_size_t linux_mem_size;
46 static int loaded;
47 static struct grub_relocator *relocator = NULL;
48 static grub_addr_t grub_linux_real_target;
49 static char *grub_linux_real_chunk;
50 static grub_size_t grub_linux16_prot_size;
51 static grub_size_t maximal_cmdline_size;
52
53 static grub_err_t
54 grub_linux16_boot (void)
55 {
56   grub_uint16_t segment;
57   struct grub_relocator16_state state;
58
59   segment = grub_linux_real_target >> 4;
60   state.gs = state.fs = state.es = state.ds = state.ss = segment;
61   state.sp = GRUB_LINUX_SETUP_STACK;
62   state.cs = segment + 0x20;
63   state.ip = 0;
64   state.a20 = 1;
65
66   grub_video_set_mode ("text", 0, 0);
67
68   grub_stop_floppy ();
69   
70   return grub_relocator16_boot (relocator, state);
71 }
72
73 static grub_err_t
74 grub_linux_unload (void)
75 {
76   grub_dl_unref (my_mod);
77   loaded = 0;
78   grub_relocator_unload (relocator);
79   relocator = NULL;
80   return GRUB_ERR_NONE;
81 }
82
83 static int
84 target_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
85             void *data)
86 {
87   grub_uint64_t *result = data;
88   grub_uint64_t candidate;
89
90   if (type != GRUB_MEMORY_AVAILABLE)
91     return 0;
92   if (addr >= 0xa0000)
93     return 0;
94   if (addr + size >= 0xa0000)
95     size = 0xa0000 - addr;
96
97   /* Put the real mode part at as a high location as possible.  */
98   candidate = addr + size - (GRUB_LINUX_CL_OFFSET + maximal_cmdline_size);
99   /* But it must not exceed the traditional area.  */
100   if (candidate > GRUB_LINUX_OLD_REAL_MODE_ADDR)
101     candidate = GRUB_LINUX_OLD_REAL_MODE_ADDR;
102   if (candidate < addr)
103     return 0;
104
105   if (candidate > *result || *result == (grub_uint64_t) -1)
106     *result = candidate;
107   return 0;
108 }
109
110 static grub_addr_t
111 grub_find_real_target (void)
112 {
113   grub_uint64_t result = (grub_uint64_t) -1;
114
115   grub_mmap_iterate (target_hook, &result);
116   return result;
117 }
118
119 static grub_err_t
120 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
121                 int argc, char *argv[])
122 {
123   grub_file_t file = 0;
124   struct linux_kernel_header lh;
125   grub_uint8_t setup_sects;
126   grub_size_t real_size;
127   grub_ssize_t len;
128   int i;
129   char *grub_linux_prot_chunk;
130   int grub_linux_is_bzimage;
131   grub_addr_t grub_linux_prot_target;
132   grub_err_t err;
133
134   grub_dl_ref (my_mod);
135
136   if (argc == 0)
137     {
138       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
139       goto fail;
140     }
141
142   file = grub_file_open (argv[0]);
143   if (! file)
144     goto fail;
145
146   if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
147     {
148       if (!grub_errno)
149         grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
150                     argv[0]);
151       goto fail;
152     }
153
154   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
155     {
156       grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
157       goto fail;
158     }
159
160   if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
161     {
162       grub_error (GRUB_ERR_BAD_OS, "too many setup sectors");
163       goto fail;
164     }
165
166   grub_linux_is_bzimage = 0;
167   setup_sects = lh.setup_sects;
168   linux_mem_size = 0;
169
170   maximal_cmdline_size = 256;
171
172   if (lh.header == grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
173       && grub_le_to_cpu16 (lh.version) >= 0x0200)
174     {
175       grub_linux_is_bzimage = (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL);
176       lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
177
178       if (grub_le_to_cpu16 (lh.version) >= 0x0206)
179         maximal_cmdline_size = grub_le_to_cpu32 (lh.cmdline_size) + 1;
180
181       grub_linux_real_target = grub_find_real_target ();
182       if (grub_linux_real_target == (grub_addr_t)-1)
183         {
184           grub_error (GRUB_ERR_OUT_OF_RANGE,
185                       "no appropriate low memory found");
186           goto fail;
187         }
188
189       if (grub_le_to_cpu16 (lh.version) >= 0x0201)
190         {
191           lh.heap_end_ptr = grub_cpu_to_le16_compile_time (GRUB_LINUX_HEAP_END_OFFSET);
192           lh.loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
193         }
194
195       if (grub_le_to_cpu16 (lh.version) >= 0x0202)
196         lh.cmd_line_ptr = grub_linux_real_target + GRUB_LINUX_CL_OFFSET;
197       else
198         {
199           lh.cl_magic = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_MAGIC);
200           lh.cl_offset = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_OFFSET);
201           lh.setup_move_size = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_OFFSET
202                                                  + maximal_cmdline_size);
203         }
204     }
205   else
206     {
207       /* Your kernel is quite old...  */
208       lh.cl_magic = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_MAGIC);
209       lh.cl_offset = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_OFFSET);
210
211       setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
212
213       grub_linux_real_target = GRUB_LINUX_OLD_REAL_MODE_ADDR;
214     }
215
216   /* If SETUP_SECTS is not set, set it to the default (4).  */
217   if (! setup_sects)
218     setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
219
220   real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
221   grub_linux16_prot_size = grub_file_size (file)
222     - real_size - GRUB_DISK_SECTOR_SIZE;
223
224   if (! grub_linux_is_bzimage
225       && GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size
226       > grub_linux_real_target)
227     {
228       grub_error (GRUB_ERR_BAD_OS, "too big zImage (0x%x > 0x%x), use bzImage instead",
229                   (char *) GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size,
230                   (grub_size_t) grub_linux_real_target);
231       goto fail;
232     }
233
234   grub_dprintf ("linux", "[Linux-%s, setup=0x%x, size=0x%x]\n",
235                 grub_linux_is_bzimage ? "bzImage" : "zImage",
236                 (unsigned) real_size,
237                 (unsigned) grub_linux16_prot_size);
238
239   relocator = grub_relocator_new ();
240   if (!relocator)
241     goto fail;
242
243   for (i = 1; i < argc; i++)
244     if (grub_memcmp (argv[i], "vga=", 4) == 0)
245       {
246         /* Video mode selection support.  */
247         grub_uint16_t vid_mode;
248         char *val = argv[i] + 4;
249
250         if (grub_strcmp (val, "normal") == 0)
251           vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
252         else if (grub_strcmp (val, "ext") == 0)
253           vid_mode = GRUB_LINUX_VID_MODE_EXTENDED;
254         else if (grub_strcmp (val, "ask") == 0)
255           vid_mode = GRUB_LINUX_VID_MODE_ASK;
256         else
257           vid_mode = (grub_uint16_t) grub_strtoul (val, 0, 0);
258
259         if (grub_errno)
260           goto fail;
261
262         lh.vid_mode = grub_cpu_to_le16 (vid_mode);
263       }
264     else if (grub_memcmp (argv[i], "mem=", 4) == 0)
265       {
266         char *val = argv[i] + 4;
267
268         linux_mem_size = grub_strtoul (val, &val, 0);
269
270         if (grub_errno)
271           {
272             grub_errno = GRUB_ERR_NONE;
273             linux_mem_size = 0;
274           }
275         else
276           {
277             int shift = 0;
278
279             switch (grub_tolower (val[0]))
280               {
281               case 'g':
282                 shift += 10;
283                 /* Fallthrough.  */
284               case 'm':
285                 shift += 10;
286                 /* Fallthrough.  */
287               case 'k':
288                 shift += 10;
289                 /* Fallthrough.  */
290               default:
291                 break;
292               }
293
294             /* Check an overflow.  */
295             if (linux_mem_size > (~0UL >> shift))
296               linux_mem_size = 0;
297             else
298               linux_mem_size <<= shift;
299           }
300       }
301
302   {
303     grub_relocator_chunk_t ch;
304     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
305                                            grub_linux_real_target,
306                                            GRUB_LINUX_CL_OFFSET
307                                            + maximal_cmdline_size);
308     if (err)
309       return err;
310     grub_linux_real_chunk = get_virtual_current_address (ch);
311   }
312
313   /* Put the real mode code at the temporary address.  */
314   grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));
315
316   len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
317   if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len)
318     {
319       if (!grub_errno)
320         grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
321                     argv[0]);
322       goto fail;
323     }
324
325   if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
326       || grub_le_to_cpu16 (lh.version) < 0x0200)
327     /* Clear the heap space.  */
328     grub_memset (grub_linux_real_chunk
329                  + ((setup_sects + 1) << GRUB_DISK_SECTOR_BITS),
330                  0,
331                  ((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1)
332                   << GRUB_DISK_SECTOR_BITS));
333
334   /* Create kernel command line.  */
335   grub_memcpy ((char *)grub_linux_real_chunk + GRUB_LINUX_CL_OFFSET,
336                 LINUX_IMAGE, sizeof (LINUX_IMAGE));
337   grub_create_loader_cmdline (argc, argv,
338                               (char *)grub_linux_real_chunk
339                               + GRUB_LINUX_CL_OFFSET + sizeof (LINUX_IMAGE) - 1,
340                               maximal_cmdline_size
341                               - (sizeof (LINUX_IMAGE) - 1));
342
343   if (grub_linux_is_bzimage)
344     grub_linux_prot_target = GRUB_LINUX_BZIMAGE_ADDR;
345   else
346     grub_linux_prot_target = GRUB_LINUX_ZIMAGE_ADDR;
347   {
348     grub_relocator_chunk_t ch;
349     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
350                                            grub_linux_prot_target,
351                                            grub_linux16_prot_size);
352     if (err)
353       return err;
354     grub_linux_prot_chunk = get_virtual_current_address (ch);
355   }
356
357   len = grub_linux16_prot_size;
358   if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size)
359       != (grub_ssize_t) grub_linux16_prot_size && !grub_errno)
360     grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
361                 argv[0]);
362
363   if (grub_errno == GRUB_ERR_NONE)
364     {
365       grub_loader_set (grub_linux16_boot, grub_linux_unload, 0);
366       loaded = 1;
367     }
368
369  fail:
370
371   if (file)
372     grub_file_close (file);
373
374   if (grub_errno != GRUB_ERR_NONE)
375     {
376       grub_dl_unref (my_mod);
377       loaded = 0;
378       grub_relocator_unload (relocator);
379     }
380
381   return grub_errno;
382 }
383
384 static grub_err_t
385 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
386                  int argc, char *argv[])
387 {
388   grub_size_t size = 0;
389   grub_addr_t addr_max, addr_min;
390   struct linux_kernel_header *lh;
391   grub_uint8_t *initrd_chunk;
392   grub_addr_t initrd_addr;
393   grub_err_t err;
394   struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
395
396   if (argc == 0)
397     {
398       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
399       goto fail;
400     }
401
402   if (!loaded)
403     {
404       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
405       goto fail;
406     }
407
408   lh = (struct linux_kernel_header *) grub_linux_real_chunk;
409
410   if (!(lh->header == grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
411         && grub_le_to_cpu16 (lh->version) >= 0x0200))
412     {
413       grub_error (GRUB_ERR_BAD_OS, "the kernel is too old for initrd");
414       goto fail;
415     }
416
417   /* Get the highest address available for the initrd.  */
418   if (grub_le_to_cpu16 (lh->version) >= 0x0203)
419     {
420       addr_max = grub_cpu_to_le32 (lh->initrd_addr_max);
421
422       /* XXX in reality, Linux specifies a bogus value, so
423          it is necessary to make sure that ADDR_MAX does not exceed
424          0x3fffffff.  */
425       if (addr_max > GRUB_LINUX_INITRD_MAX_ADDRESS)
426         addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
427     }
428   else
429     addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
430
431   if (linux_mem_size != 0 && linux_mem_size < addr_max)
432     addr_max = linux_mem_size;
433
434   /* Linux 2.3.xx has a bug in the memory range check, so avoid
435      the last page.
436      Linux 2.2.xx has a bug in the memory range check, which is
437      worse than that of Linux 2.3.xx, so avoid the last 64kb.  */
438   addr_max -= 0x10000;
439
440   addr_min = GRUB_LINUX_BZIMAGE_ADDR + grub_linux16_prot_size;
441
442   if (grub_initrd_init (argc, argv, &initrd_ctx))
443     goto fail;
444
445   size = grub_get_initrd_size (&initrd_ctx);
446
447   {
448     grub_relocator_chunk_t ch;
449     err = grub_relocator_alloc_chunk_align (relocator, &ch,
450                                             addr_min, addr_max - size,
451                                             size, 0x1000,
452                                             GRUB_RELOCATOR_PREFERENCE_HIGH, 0);
453     if (err)
454       return err;
455     initrd_chunk = get_virtual_current_address (ch);
456     initrd_addr = get_physical_target_address (ch);
457   }
458
459   if (grub_initrd_load (&initrd_ctx, argv, initrd_chunk))
460     goto fail;
461
462   lh->ramdisk_image = initrd_addr;
463   lh->ramdisk_size = size;
464
465  fail:
466   grub_initrd_close (&initrd_ctx);
467
468   return grub_errno;
469 }
470
471 static grub_command_t cmd_linux, cmd_initrd;
472
473 GRUB_MOD_INIT(linux16)
474 {
475   cmd_linux =
476     grub_register_command ("linux16", grub_cmd_linux,
477                            0, N_("Load Linux."));
478   cmd_initrd =
479     grub_register_command ("initrd16", grub_cmd_initrd,
480                            0, N_("Load initrd."));
481   my_mod = mod;
482 }
483
484 GRUB_MOD_FINI(linux16)
485 {
486   grub_unregister_command (cmd_linux);
487   grub_unregister_command (cmd_initrd);
488 }