~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to loader/i386/pc/linux.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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/machine/loader.h>
22
 
#include <grub/file.h>
23
 
#include <grub/err.h>
24
 
#include <grub/device.h>
25
 
#include <grub/disk.h>
26
 
#include <grub/misc.h>
27
 
#include <grub/types.h>
28
 
#include <grub/machine/init.h>
29
 
#include <grub/machine/memory.h>
30
 
#include <grub/dl.h>
31
 
#include <grub/cpu/linux.h>
32
 
#include <grub/command.h>
33
 
#include <grub/i18n.h>
34
 
#include <grub/mm.h>
35
 
#include <grub/video.h>
36
 
 
37
 
#define GRUB_LINUX_CL_OFFSET            0x9000
38
 
#define GRUB_LINUX_CL_END_OFFSET        0x90FF
39
 
 
40
 
static grub_dl_t my_mod;
41
 
 
42
 
static grub_size_t linux_mem_size;
43
 
static int loaded;
44
 
 
45
 
static grub_err_t
46
 
grub_linux_unload (void)
47
 
{
48
 
  grub_dl_unref (my_mod);
49
 
  loaded = 0;
50
 
  return GRUB_ERR_NONE;
51
 
}
52
 
 
53
 
static grub_err_t
54
 
grub_linux16_boot (void)
55
 
{
56
 
  grub_video_set_mode ("text", 0, 0);
57
 
  grub_linux16_real_boot ();
58
 
 
59
 
  /* Not reached.  */
60
 
  return GRUB_ERR_NONE;
61
 
}
62
 
 
63
 
static grub_err_t
64
 
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
65
 
                int argc, char *argv[])
66
 
{
67
 
  grub_file_t file = 0;
68
 
  struct linux_kernel_header lh;
69
 
  grub_uint8_t setup_sects;
70
 
  grub_size_t real_size, prot_size;
71
 
  grub_ssize_t len;
72
 
  int i;
73
 
  char *dest;
74
 
 
75
 
  grub_dl_ref (my_mod);
76
 
 
77
 
  if (argc == 0)
78
 
    {
79
 
      grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
80
 
      goto fail;
81
 
    }
82
 
 
83
 
  file = grub_file_open (argv[0]);
84
 
  if (! file)
85
 
    goto fail;
86
 
 
87
 
  if ((grub_size_t) grub_file_size (file) > grub_os_area_size)
88
 
    {
89
 
      grub_error (GRUB_ERR_OUT_OF_RANGE, "too big kernel (0x%x > 0x%x)",
90
 
                  (grub_size_t) grub_file_size (file),
91
 
                  grub_os_area_size);
92
 
      goto fail;
93
 
    }
94
 
 
95
 
  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
96
 
    {
97
 
      grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header");
98
 
      goto fail;
99
 
    }
100
 
 
101
 
  if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
102
 
    {
103
 
      grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
104
 
      goto fail;
105
 
    }
106
 
 
107
 
  if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
108
 
    {
109
 
      grub_error (GRUB_ERR_BAD_OS, "too many setup sectors");
110
 
      goto fail;
111
 
    }
112
 
 
113
 
  grub_linux_is_bzimage = 0;
114
 
  setup_sects = lh.setup_sects;
115
 
  linux_mem_size = 0;
116
 
 
117
 
  if (lh.header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
118
 
      && grub_le_to_cpu16 (lh.version) >= 0x0200)
119
 
    {
120
 
      grub_linux_is_bzimage = (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL);
121
 
      lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
122
 
 
123
 
      /* Put the real mode part at as a high location as possible.  */
124
 
      grub_linux_real_addr
125
 
        = (char *) UINT_TO_PTR (grub_mmap_get_lower ()
126
 
                                - GRUB_LINUX_SETUP_MOVE_SIZE);
127
 
      /* But it must not exceed the traditional area.  */
128
 
      if (grub_linux_real_addr > (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR)
129
 
        grub_linux_real_addr = (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR;
130
 
 
131
 
      if (grub_le_to_cpu16 (lh.version) >= 0x0201)
132
 
        {
133
 
          lh.heap_end_ptr = grub_cpu_to_le16 (GRUB_LINUX_HEAP_END_OFFSET);
134
 
          lh.loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
135
 
        }
136
 
 
137
 
      if (grub_le_to_cpu16 (lh.version) >= 0x0202)
138
 
        lh.cmd_line_ptr = grub_linux_real_addr + GRUB_LINUX_CL_OFFSET;
139
 
      else
140
 
        {
141
 
          lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC);
142
 
          lh.cl_offset = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET);
143
 
          lh.setup_move_size = grub_cpu_to_le16 (GRUB_LINUX_SETUP_MOVE_SIZE);
144
 
        }
145
 
    }
146
 
  else
147
 
    {
148
 
      /* Your kernel is quite old...  */
149
 
      lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC);
150
 
      lh.cl_offset = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET);
151
 
 
152
 
      setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
153
 
 
154
 
      grub_linux_real_addr = (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR;
155
 
    }
156
 
 
157
 
  /* If SETUP_SECTS is not set, set it to the default (4).  */
158
 
  if (! setup_sects)
159
 
    setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
160
 
 
161
 
  real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
162
 
  prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
163
 
 
164
 
  grub_linux_tmp_addr = (char *) GRUB_LINUX_BZIMAGE_ADDR + prot_size;
165
 
 
166
 
  if (! grub_linux_is_bzimage
167
 
      && ((char *) GRUB_LINUX_ZIMAGE_ADDR + prot_size > grub_linux_real_addr))
168
 
    {
169
 
      grub_error (GRUB_ERR_BAD_OS, "too big zImage (0x%x > 0x%x), use bzImage instead",
170
 
                  (char *) GRUB_LINUX_ZIMAGE_ADDR + prot_size,
171
 
                  (grub_size_t) grub_linux_real_addr);
172
 
      goto fail;
173
 
    }
174
 
 
175
 
  if (grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE
176
 
      > (char *) UINT_TO_PTR (grub_mmap_get_lower ()))
177
 
    {
178
 
      grub_error (GRUB_ERR_OUT_OF_RANGE,
179
 
                 "too small lower memory (0x%x > 0x%x)",
180
 
                  grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE,
181
 
                  (int) grub_mmap_get_lower ());
182
 
      goto fail;
183
 
    }
184
 
 
185
 
  grub_dprintf ("linux", "Linux-%s, setup=0x%x, size=0x%x\n",
186
 
                grub_linux_is_bzimage ? "bzImage" : "zImage",
187
 
                real_size, prot_size);
188
 
 
189
 
  for (i = 1; i < argc; i++)
190
 
    if (grub_memcmp (argv[i], "vga=", 4) == 0)
191
 
      {
192
 
        /* Video mode selection support.  */
193
 
        grub_uint16_t vid_mode;
194
 
        char *val = argv[i] + 4;
195
 
 
196
 
        if (grub_strcmp (val, "normal") == 0)
197
 
          vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
198
 
        else if (grub_strcmp (val, "ext") == 0)
199
 
          vid_mode = GRUB_LINUX_VID_MODE_EXTENDED;
200
 
        else if (grub_strcmp (val, "ask") == 0)
201
 
          vid_mode = GRUB_LINUX_VID_MODE_ASK;
202
 
        else
203
 
          vid_mode = (grub_uint16_t) grub_strtoul (val, 0, 0);
204
 
 
205
 
        if (grub_errno)
206
 
          goto fail;
207
 
 
208
 
        lh.vid_mode = grub_cpu_to_le16 (vid_mode);
209
 
      }
210
 
    else if (grub_memcmp (argv[i], "mem=", 4) == 0)
211
 
      {
212
 
        char *val = argv[i] + 4;
213
 
 
214
 
        linux_mem_size = grub_strtoul (val, &val, 0);
215
 
 
216
 
        if (grub_errno)
217
 
          {
218
 
            grub_errno = GRUB_ERR_NONE;
219
 
            linux_mem_size = 0;
220
 
          }
221
 
        else
222
 
          {
223
 
            int shift = 0;
224
 
 
225
 
            switch (grub_tolower (val[0]))
226
 
              {
227
 
              case 'g':
228
 
                shift += 10;
229
 
              case 'm':
230
 
                shift += 10;
231
 
              case 'k':
232
 
                shift += 10;
233
 
              default:
234
 
                break;
235
 
              }
236
 
 
237
 
            /* Check an overflow.  */
238
 
            if (linux_mem_size > (~0UL >> shift))
239
 
              linux_mem_size = 0;
240
 
            else
241
 
              linux_mem_size <<= shift;
242
 
          }
243
 
      }
244
 
 
245
 
  /* Put the real mode code at the temporary address.  */
246
 
  grub_memmove (grub_linux_tmp_addr, &lh, sizeof (lh));
247
 
 
248
 
  len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
249
 
  if (grub_file_read (file, grub_linux_tmp_addr + sizeof (lh), len) != len)
250
 
    {
251
 
      grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
252
 
      goto fail;
253
 
    }
254
 
 
255
 
  if (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
256
 
      || grub_le_to_cpu16 (lh.version) < 0x0200)
257
 
    /* Clear the heap space.  */
258
 
    grub_memset (grub_linux_tmp_addr
259
 
                 + ((setup_sects + 1) << GRUB_DISK_SECTOR_BITS),
260
 
                 0,
261
 
                 ((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1)
262
 
                  << GRUB_DISK_SECTOR_BITS));
263
 
 
264
 
  /* Specify the boot file.  */
265
 
  dest = grub_stpcpy (grub_linux_tmp_addr + GRUB_LINUX_CL_OFFSET,
266
 
                      "BOOT_IMAGE=");
267
 
  dest = grub_stpcpy (dest, argv[0]);
268
 
 
269
 
  /* Copy kernel parameters.  */
270
 
  for (i = 1;
271
 
       i < argc
272
 
         && dest + grub_strlen (argv[i]) + 1 < (grub_linux_tmp_addr
273
 
                                                + GRUB_LINUX_CL_END_OFFSET);
274
 
       i++)
275
 
    {
276
 
      *dest++ = ' ';
277
 
      dest = grub_stpcpy (dest, argv[i]);
278
 
    }
279
 
 
280
 
  len = prot_size;
281
 
  if (grub_file_read (file, (void *) GRUB_LINUX_BZIMAGE_ADDR, len) != len)
282
 
    grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
283
 
 
284
 
  if (grub_errno == GRUB_ERR_NONE)
285
 
    {
286
 
      grub_linux_prot_size = prot_size;
287
 
      grub_loader_set (grub_linux16_boot, grub_linux_unload, 1);
288
 
      loaded = 1;
289
 
    }
290
 
 
291
 
 fail:
292
 
 
293
 
  if (file)
294
 
    grub_file_close (file);
295
 
 
296
 
  if (grub_errno != GRUB_ERR_NONE)
297
 
    {
298
 
      grub_dl_unref (my_mod);
299
 
      loaded = 0;
300
 
    }
301
 
 
302
 
  return grub_errno;
303
 
}
304
 
 
305
 
static grub_err_t
306
 
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
307
 
                 int argc, char *argv[])
308
 
{
309
 
  grub_file_t file = 0;
310
 
  grub_ssize_t size;
311
 
  grub_addr_t addr_max, addr_min, addr;
312
 
  struct linux_kernel_header *lh;
313
 
 
314
 
  if (argc == 0)
315
 
    {
316
 
      grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
317
 
      goto fail;
318
 
    }
319
 
 
320
 
  if (!loaded)
321
 
    {
322
 
      grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
323
 
      goto fail;
324
 
    }
325
 
 
326
 
  lh = (struct linux_kernel_header *) grub_linux_tmp_addr;
327
 
 
328
 
  if (!(lh->header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
329
 
        && grub_le_to_cpu16 (lh->version) >= 0x0200))
330
 
    {
331
 
      grub_error (GRUB_ERR_BAD_OS, "the kernel is too old for initrd");
332
 
      goto fail;
333
 
    }
334
 
 
335
 
  /* Get the highest address available for the initrd.  */
336
 
  if (grub_le_to_cpu16 (lh->version) >= 0x0203)
337
 
    {
338
 
      addr_max = grub_cpu_to_le32 (lh->initrd_addr_max);
339
 
 
340
 
      /* XXX in reality, Linux specifies a bogus value, so
341
 
         it is necessary to make sure that ADDR_MAX does not exceed
342
 
         0x3fffffff.  */
343
 
      if (addr_max > GRUB_LINUX_INITRD_MAX_ADDRESS)
344
 
        addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
345
 
    }
346
 
  else
347
 
    addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
348
 
 
349
 
  if (linux_mem_size != 0 && linux_mem_size < addr_max)
350
 
    addr_max = linux_mem_size;
351
 
 
352
 
  /* Linux 2.3.xx has a bug in the memory range check, so avoid
353
 
     the last page.
354
 
     Linux 2.2.xx has a bug in the memory range check, which is
355
 
     worse than that of Linux 2.3.xx, so avoid the last 64kb.  */
356
 
  addr_max -= 0x10000;
357
 
 
358
 
  if (addr_max > grub_os_area_addr + grub_os_area_size)
359
 
    addr_max = grub_os_area_addr + grub_os_area_size;
360
 
 
361
 
  addr_min = (grub_addr_t) grub_linux_tmp_addr + GRUB_LINUX_CL_END_OFFSET;
362
 
 
363
 
  file = grub_file_open (argv[0]);
364
 
  if (!file)
365
 
    goto fail;
366
 
 
367
 
  size = grub_file_size (file);
368
 
 
369
 
  /* Put the initrd as high as possible, 4KiB aligned.  */
370
 
  addr = (addr_max - size) & ~0xFFF;
371
 
 
372
 
  if (addr < addr_min)
373
 
    {
374
 
      grub_error (GRUB_ERR_OUT_OF_RANGE, "the initrd is too big");
375
 
      goto fail;
376
 
    }
377
 
 
378
 
  if (grub_file_read (file, (void *) addr, size) != size)
379
 
    {
380
 
      grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
381
 
      goto fail;
382
 
    }
383
 
 
384
 
  lh->ramdisk_image = addr;
385
 
  lh->ramdisk_size = size;
386
 
 
387
 
 fail:
388
 
  if (file)
389
 
    grub_file_close (file);
390
 
 
391
 
  return grub_errno;
392
 
}
393
 
 
394
 
static grub_command_t cmd_linux, cmd_initrd;
395
 
 
396
 
GRUB_MOD_INIT(linux16)
397
 
{
398
 
  cmd_linux =
399
 
    grub_register_command ("linux16", grub_cmd_linux,
400
 
                           0, N_("Load Linux."));
401
 
  cmd_initrd =
402
 
    grub_register_command ("initrd16", grub_cmd_initrd,
403
 
                           0, N_("Load initrd."));
404
 
  my_mod = mod;
405
 
}
406
 
 
407
 
GRUB_MOD_FINI(linux16)
408
 
{
409
 
  grub_unregister_command (cmd_linux);
410
 
  grub_unregister_command (cmd_initrd);
411
 
}