~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to kern/emu/misc.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
 
/*
2
 
 *  GRUB  --  GRand Unified Bootloader
3
 
 *  Copyright (C) 2002,2003,2005,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 <config.h>
20
 
 
21
 
#include <errno.h>
22
 
#include <error.h>
23
 
#include <stdio.h>
24
 
#include <stdlib.h>
25
 
#include <stdint.h>
26
 
#include <string.h>
27
 
#include <sys/time.h>
28
 
#include <sys/types.h>
29
 
#include <sys/stat.h>
30
 
#include <unistd.h>
31
 
#ifdef HAVE_LIMITS_H
32
 
#include <limits.h>
33
 
#endif
34
 
 
35
 
#ifdef HAVE_GETMNTANY
36
 
# include <sys/mnttab.h>
37
 
#endif
38
 
 
39
 
#include <grub/mm.h>
40
 
#include <grub/err.h>
41
 
#include <grub/env.h>
42
 
#include <grub/types.h>
43
 
#include <grub/misc.h>
44
 
#include <grub/i18n.h>
45
 
#include <grub/time.h>
46
 
#include <grub/emu/misc.h>
47
 
 
48
 
#ifdef HAVE_DEVICE_MAPPER
49
 
# include <libdevmapper.h>
50
 
#endif
51
 
 
52
 
#ifdef HAVE_LIBZFS
53
 
# include <grub/util/libzfs.h>
54
 
#endif
55
 
 
56
 
#ifdef HAVE_LIBNVPAIR
57
 
# include <grub/util/libnvpair.h>
58
 
#endif
59
 
 
60
 
#ifdef HAVE_GETFSSTAT
61
 
# include <sys/mount.h>
62
 
#endif
63
 
 
64
 
int verbosity;
65
 
 
66
 
void
67
 
grub_util_warn (const char *fmt, ...)
68
 
{
69
 
  va_list ap;
70
 
 
71
 
  fprintf (stderr, _("%s: warn:"), program_name);
72
 
  fprintf (stderr, " ");
73
 
  va_start (ap, fmt);
74
 
  vfprintf (stderr, fmt, ap);
75
 
  va_end (ap);
76
 
  fprintf (stderr, ".\n");
77
 
  fflush (stderr);
78
 
}
79
 
 
80
 
void
81
 
grub_util_info (const char *fmt, ...)
82
 
{
83
 
  if (verbosity > 0)
84
 
    {
85
 
      va_list ap;
86
 
 
87
 
      fprintf (stderr, _("%s: info:"), program_name);
88
 
      fprintf (stderr, " ");
89
 
      va_start (ap, fmt);
90
 
      vfprintf (stderr, fmt, ap);
91
 
      va_end (ap);
92
 
      fprintf (stderr, ".\n");
93
 
      fflush (stderr);
94
 
    }
95
 
}
96
 
 
97
 
void
98
 
grub_util_error (const char *fmt, ...)
99
 
{
100
 
  va_list ap;
101
 
 
102
 
  fprintf (stderr, _("%s: error:"), program_name);
103
 
  fprintf (stderr, " ");
104
 
  va_start (ap, fmt);
105
 
  vfprintf (stderr, fmt, ap);
106
 
  va_end (ap);
107
 
  fprintf (stderr, ".\n");
108
 
  exit (1);
109
 
}
110
 
 
111
 
void *
112
 
xmalloc (grub_size_t size)
113
 
{
114
 
  void *p;
115
 
 
116
 
  p = malloc (size);
117
 
  if (! p)
118
 
    grub_util_error ("out of memory");
119
 
 
120
 
  return p;
121
 
}
122
 
 
123
 
void *
124
 
xrealloc (void *ptr, grub_size_t size)
125
 
{
126
 
  ptr = realloc (ptr, size);
127
 
  if (! ptr)
128
 
    grub_util_error ("out of memory");
129
 
 
130
 
  return ptr;
131
 
}
132
 
 
133
 
char *
134
 
xstrdup (const char *str)
135
 
{
136
 
  size_t len;
137
 
  char *newstr;
138
 
 
139
 
  len = strlen (str);
140
 
  newstr = (char *) xmalloc (len + 1);
141
 
  memcpy (newstr, str, len + 1);
142
 
 
143
 
  return newstr;
144
 
}
145
 
 
146
 
#ifndef HAVE_VASPRINTF
147
 
 
148
 
int
149
 
vasprintf (char **buf, const char *fmt, va_list ap)
150
 
{
151
 
  /* Should be large enough.  */
152
 
  *buf = xmalloc (512);
153
 
 
154
 
  return vsprintf (*buf, fmt, ap);
155
 
}
156
 
 
157
 
#endif
158
 
 
159
 
#ifndef  HAVE_ASPRINTF
160
 
 
161
 
int
162
 
asprintf (char **buf, const char *fmt, ...)
163
 
{
164
 
  int status;
165
 
  va_list ap;
166
 
 
167
 
  va_start (ap, fmt);
168
 
  status = vasprintf (*buf, fmt, ap);
169
 
  va_end (ap);
170
 
 
171
 
  return status;
172
 
}
173
 
 
174
 
#endif
175
 
 
176
 
char *
177
 
xasprintf (const char *fmt, ...)
178
 
179
 
  va_list ap;
180
 
  char *result;
181
 
  
182
 
  va_start (ap, fmt);
183
 
  if (vasprintf (&result, fmt, ap) < 0)
184
 
    { 
185
 
      if (errno == ENOMEM)
186
 
        grub_util_error ("out of memory");
187
 
      return NULL;
188
 
    }
189
 
  
190
 
  return result;
191
 
}
192
 
 
193
 
void
194
 
grub_exit (void)
195
 
{
196
 
  exit (1);
197
 
}
198
 
 
199
 
grub_uint64_t
200
 
grub_get_time_ms (void)
201
 
{
202
 
  struct timeval tv;
203
 
 
204
 
  gettimeofday (&tv, 0);
205
 
 
206
 
  return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
207
 
}
208
 
 
209
 
grub_uint32_t
210
 
grub_get_rtc (void)
211
 
{
212
 
  struct timeval tv;
213
 
 
214
 
  gettimeofday (&tv, 0);
215
 
 
216
 
  return (tv.tv_sec * GRUB_TICKS_PER_SECOND
217
 
          + (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
218
 
             * GRUB_TICKS_PER_SECOND / 1000000));
219
 
}
220
 
 
221
 
char *
222
 
canonicalize_file_name (const char *path)
223
 
{
224
 
  char *ret;
225
 
#ifdef PATH_MAX
226
 
  ret = xmalloc (PATH_MAX);
227
 
  if (!realpath (path, ret))
228
 
    return NULL;
229
 
#else
230
 
  ret = realpath (path, NULL);
231
 
#endif
232
 
  return ret;
233
 
}
234
 
 
235
 
#ifdef __CYGWIN__
236
 
/* Convert POSIX path to Win32 path,
237
 
   remove drive letter, replace backslashes.  */
238
 
static char *
239
 
get_win32_path (const char *path)
240
 
{
241
 
  char winpath[PATH_MAX];
242
 
  if (cygwin_conv_path (CCP_POSIX_TO_WIN_A, path, winpath, sizeof(winpath)))
243
 
    grub_util_error ("cygwin_conv_path() failed");
244
 
 
245
 
  int len = strlen (winpath);
246
 
  int offs = (len > 2 && winpath[1] == ':' ? 2 : 0);
247
 
 
248
 
  int i;
249
 
  for (i = offs; i < len; i++)
250
 
    if (winpath[i] == '\\')
251
 
      winpath[i] = '/';
252
 
  return xstrdup (winpath + offs);
253
 
}
254
 
#endif
255
 
 
256
 
#ifdef HAVE_LIBZFS
257
 
static libzfs_handle_t *__libzfs_handle;
258
 
 
259
 
static void
260
 
fini_libzfs (void)
261
 
{
262
 
  libzfs_fini (__libzfs_handle);
263
 
}
264
 
 
265
 
libzfs_handle_t *
266
 
grub_get_libzfs_handle (void)
267
 
{
268
 
  if (! __libzfs_handle)
269
 
    {
270
 
      __libzfs_handle = libzfs_init ();
271
 
      atexit (fini_libzfs);
272
 
    }
273
 
 
274
 
  return __libzfs_handle;
275
 
}
276
 
#endif /* HAVE_LIBZFS */
277
 
 
278
 
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
279
 
/* Not ZFS-specific in itself, but for now it's only used by ZFS-related code.  */
280
 
char *
281
 
grub_find_mount_point_from_dir (const char *dir)
282
 
{
283
 
  struct stat st;
284
 
  typeof (st.st_dev) fs;
285
 
  char *prev, *next, *slash, *statdir;
286
 
 
287
 
  if (stat (dir, &st) == -1)
288
 
    error (1, errno, "stat (%s)", dir);
289
 
 
290
 
  fs = st.st_dev;
291
 
 
292
 
  prev = xstrdup (dir);
293
 
 
294
 
  while (1)
295
 
    {
296
 
      /* Remove last slash.  */
297
 
      next = xstrdup (prev);
298
 
      slash = strrchr (next, '/');
299
 
      if (! slash)
300
 
        {
301
 
          free (next);
302
 
          free (prev);
303
 
          return NULL;
304
 
        }
305
 
      *slash = '\0';
306
 
 
307
 
      /* A next empty string counts as /.  */
308
 
      if (next[0] == '\0')
309
 
        statdir = "/";
310
 
      else
311
 
        statdir = next;
312
 
 
313
 
      if (stat (statdir, &st) == -1)
314
 
        error (1, errno, "stat (%s)", next);
315
 
 
316
 
      if (st.st_dev != fs)
317
 
        {
318
 
          /* Found mount point.  */
319
 
          free (next);
320
 
          return prev;
321
 
        }
322
 
 
323
 
      free (prev);
324
 
      prev = next;
325
 
 
326
 
      /* We've already seen an empty string, which means we
327
 
         reached /.  Nothing left to do.  */
328
 
      if (prev[0] == '\0')
329
 
        {
330
 
          free (prev);
331
 
          return xstrdup ("/");
332
 
        }
333
 
    }
334
 
}
335
 
#endif
336
 
 
337
 
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
338
 
 
339
 
/* ZFS has similar problems to those of btrfs (see above).  */
340
 
void
341
 
grub_find_zpool_from_mount_point (const char *mnt_point, char **poolname, char **poolfs)
342
 
{
343
 
  char *slash;
344
 
 
345
 
  *poolname = *poolfs = NULL;
346
 
 
347
 
#if defined(HAVE_GETFSSTAT) /* FreeBSD and GNU/kFreeBSD */
348
 
  {
349
 
    int mnt_count = getfsstat (NULL, 0, MNT_WAIT);
350
 
    if (mnt_count == -1)
351
 
      error (1, errno, "getfsstat");
352
 
 
353
 
    struct statfs *mnt = xmalloc (mnt_count * sizeof (*mnt));
354
 
 
355
 
    mnt_count = getfsstat (mnt, mnt_count * sizeof (*mnt), MNT_WAIT);
356
 
    if (mnt_count == -1)
357
 
      error (1, errno, "getfsstat");
358
 
 
359
 
    unsigned int i;
360
 
    for (i = 0; i < (unsigned) mnt_count; i++)
361
 
      if (!strcmp (mnt[i].f_fstypename, "zfs")
362
 
          && !strcmp (mnt[i].f_mntonname, mnt_point))
363
 
        {
364
 
          *poolname = xstrdup (mnt[i].f_mntfromname);
365
 
          break;
366
 
        }
367
 
 
368
 
    free (mnt);
369
 
  }
370
 
#elif defined(HAVE_GETMNTANY) /* OpenSolaris */
371
 
  {
372
 
    FILE *mnttab = fopen ("/etc/mnttab", "r");
373
 
    struct mnttab mp;
374
 
    struct mnttab mpref =
375
 
      {
376
 
        .mnt_special = NULL,
377
 
        .mnt_mountp = mnt_point,
378
 
        .mnt_fstype = "zfs",
379
 
        .mnt_mntopts = NULL,
380
 
        .mnt_time = NULL,
381
 
      };
382
 
 
383
 
    if (getmntany (mnttab, &mp, &mpref) == 0)
384
 
      *poolname = xstrdup (mp.mnt_special);
385
 
 
386
 
    fclose (mnttab);
387
 
  }
388
 
#endif
389
 
 
390
 
  if (! *poolname)
391
 
    return;
392
 
 
393
 
  slash = strchr (*poolname, '/');
394
 
  if (slash)
395
 
    {
396
 
      *slash = '\0';
397
 
      *poolfs = xstrdup (slash + 1);
398
 
    }
399
 
  else
400
 
    *poolfs = xstrdup ("");
401
 
}
402
 
#endif
403
 
 
404
 
/* This function never prints trailing slashes (so that its output
405
 
   can be appended a slash unconditionally).  */
406
 
char *
407
 
grub_make_system_path_relative_to_its_root (const char *path)
408
 
{
409
 
  struct stat st;
410
 
  char *p, *buf, *buf2, *buf3;
411
 
  char *mnt_point, *poolname = NULL, *poolfs = NULL, *ret;
412
 
  uintptr_t offset = 0;
413
 
  dev_t num;
414
 
  size_t len;
415
 
 
416
 
  /* canonicalize.  */
417
 
  p = canonicalize_file_name (path);
418
 
  if (p == NULL)
419
 
    grub_util_error ("failed to get canonical path of %s", path);
420
 
 
421
 
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
422
 
  /* For ZFS sub-pool filesystems, could be extended to others (btrfs?).  */
423
 
  mnt_point = grub_find_mount_point_from_dir (p);
424
 
  if (mnt_point)
425
 
    {
426
 
      grub_find_zpool_from_mount_point (mnt_point, &poolname, &poolfs);
427
 
      free (mnt_point);
428
 
    }
429
 
#endif
430
 
 
431
 
  len = strlen (p) + 1;
432
 
  buf = xstrdup (p);
433
 
  free (p);
434
 
 
435
 
  if (stat (buf, &st) < 0)
436
 
    grub_util_error ("cannot stat %s: %s", buf, strerror (errno));
437
 
 
438
 
  buf2 = xstrdup (buf);
439
 
  num = st.st_dev;
440
 
 
441
 
  /* This loop sets offset to the number of chars of the root
442
 
     directory we're inspecting.  */
443
 
  while (1)
444
 
    {
445
 
      p = strrchr (buf, '/');
446
 
      if (p == NULL)
447
 
        /* This should never happen.  */
448
 
        grub_util_error ("FIXME: no / in buf. (make_system_path_relative_to_its_root)");
449
 
      if (p != buf)
450
 
        *p = 0;
451
 
      else
452
 
        *++p = 0;
453
 
 
454
 
      if (stat (buf, &st) < 0)
455
 
        grub_util_error ("cannot stat %s: %s", buf, strerror (errno));
456
 
 
457
 
      /* buf is another filesystem; we found it.  */
458
 
      if (st.st_dev != num)
459
 
        {
460
 
          /* offset == 0 means path given is the mount point.
461
 
             This works around special-casing of "/" in Un*x.  This function never
462
 
             prints trailing slashes (so that its output can be appended a slash
463
 
             unconditionally).  Each slash in is considered a preceding slash, and
464
 
             therefore the root directory is an empty string.  */
465
 
          if (offset == 0)
466
 
            {
467
 
              free (buf);
468
 
              free (buf2);
469
 
              return xstrdup ("");
470
 
            }
471
 
          else
472
 
            break;
473
 
        }
474
 
 
475
 
      offset = p - buf;
476
 
      /* offset == 1 means root directory.  */
477
 
      if (offset == 1)
478
 
        {
479
 
          /* Include leading slash.  */
480
 
          offset = 0;
481
 
          break;
482
 
        }
483
 
    }
484
 
  free (buf);
485
 
  buf3 = xstrdup (buf2 + offset);
486
 
  free (buf2);
487
 
 
488
 
#ifdef __CYGWIN__
489
 
  if (st.st_dev != (DEV_CYGDRIVE_MAJOR << 16))
490
 
    {
491
 
      /* Reached some mount point not below /cygdrive.
492
 
         GRUB does not know Cygwin's emulated mounts,
493
 
         convert to Win32 path.  */
494
 
      grub_util_info ("Cygwin path = %s\n", buf3);
495
 
      char * temp = get_win32_path (buf3);
496
 
      free (buf3);
497
 
      buf3 = temp;
498
 
    }
499
 
#endif
500
 
 
501
 
  /* Remove trailing slashes, return empty string if root directory.  */
502
 
  len = strlen (buf3);
503
 
  while (len > 0 && buf3[len - 1] == '/')
504
 
    {
505
 
      buf3[len - 1] = '\0';
506
 
      len--;
507
 
    }
508
 
 
509
 
  if (poolfs)
510
 
    {
511
 
      ret = xasprintf ("/%s@%s", poolfs, buf3);
512
 
      free (buf3);
513
 
    }
514
 
  else
515
 
    ret = buf3;
516
 
 
517
 
  return ret;
518
 
}
519
 
 
520
 
#ifdef HAVE_DEVICE_MAPPER
521
 
static void device_mapper_null_log (int level __attribute__ ((unused)),
522
 
                                    const char *file __attribute__ ((unused)),
523
 
                                    int line __attribute__ ((unused)),
524
 
                                    int dm_errno __attribute__ ((unused)),
525
 
                                    const char *f __attribute__ ((unused)),
526
 
                                    ...)
527
 
{
528
 
}
529
 
 
530
 
int
531
 
grub_device_mapper_supported (void)
532
 
{
533
 
  static int supported = -1;
534
 
 
535
 
  if (supported == -1)
536
 
    {
537
 
      struct dm_task *dmt;
538
 
 
539
 
      /* Suppress annoying log messages.  */
540
 
      dm_log_with_errno_init (&device_mapper_null_log);
541
 
 
542
 
      dmt = dm_task_create (DM_DEVICE_VERSION);
543
 
      supported = (dmt != NULL);
544
 
      if (dmt)
545
 
        dm_task_destroy (dmt);
546
 
 
547
 
      /* Restore the original logger.  */
548
 
      dm_log_with_errno_init (NULL);
549
 
    }
550
 
 
551
 
  return supported;
552
 
}
553
 
#endif /* HAVE_DEVICE_MAPPER */