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

« back to all changes in this revision

Viewing changes to grub-core/disk/loopback.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
/* loopback.c - command to add loopback devices.  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2005,2006,2007  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/dl.h>
 
21
#include <grub/misc.h>
 
22
#include <grub/file.h>
 
23
#include <grub/disk.h>
 
24
#include <grub/mm.h>
 
25
#include <grub/extcmd.h>
 
26
#include <grub/i18n.h>
 
27
 
 
28
struct grub_loopback
 
29
{
 
30
  char *devname;
 
31
  grub_file_t file;
 
32
  struct grub_loopback *next;
 
33
};
 
34
 
 
35
static struct grub_loopback *loopback_list;
 
36
 
 
37
static const struct grub_arg_option options[] =
 
38
  {
 
39
    {"delete", 'd', 0, N_("Delete the loopback device entry."), 0, 0},
 
40
    {0, 0, 0, 0, 0, 0}
 
41
  };
 
42
 
 
43
/* Delete the loopback device NAME.  */
 
44
static grub_err_t
 
45
delete_loopback (const char *name)
 
46
{
 
47
  struct grub_loopback *dev;
 
48
  struct grub_loopback **prev;
 
49
 
 
50
  /* Search for the device.  */
 
51
  for (dev = loopback_list, prev = &loopback_list;
 
52
       dev;
 
53
       prev = &dev->next, dev = dev->next)
 
54
    if (grub_strcmp (dev->devname, name) == 0)
 
55
      break;
 
56
 
 
57
  if (! dev)
 
58
    return grub_error (GRUB_ERR_BAD_DEVICE, "device not found");
 
59
 
 
60
  /* Remove the device from the list.  */
 
61
  *prev = dev->next;
 
62
 
 
63
  grub_free (dev->devname);
 
64
  grub_file_close (dev->file);
 
65
  grub_free (dev);
 
66
 
 
67
  return 0;
 
68
}
 
69
 
 
70
/* The command to add and remove loopback devices.  */
 
71
static grub_err_t
 
72
grub_cmd_loopback (grub_extcmd_context_t ctxt, int argc, char **args)
 
73
{
 
74
  struct grub_arg_list *state = ctxt->state;
 
75
  grub_file_t file;
 
76
  struct grub_loopback *newdev;
 
77
  grub_err_t ret;
 
78
 
 
79
  if (argc < 1)
 
80
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
 
81
 
 
82
  /* Check if `-d' was used.  */
 
83
  if (state[0].set)
 
84
      return delete_loopback (args[0]);
 
85
 
 
86
  if (argc < 2)
 
87
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
 
88
 
 
89
  file = grub_file_open (args[1]);
 
90
  if (! file)
 
91
    return grub_errno;
 
92
 
 
93
  /* First try to replace the old device.  */
 
94
  for (newdev = loopback_list; newdev; newdev = newdev->next)
 
95
    if (grub_strcmp (newdev->devname, args[0]) == 0)
 
96
      break;
 
97
 
 
98
  if (newdev)
 
99
    {
 
100
      char *newname = grub_strdup (args[1]);
 
101
      if (! newname)
 
102
        goto fail;
 
103
 
 
104
      grub_file_close (newdev->file);
 
105
      newdev->file = file;
 
106
 
 
107
      return 0;
 
108
    }
 
109
 
 
110
  /* Unable to replace it, make a new entry.  */
 
111
  newdev = grub_malloc (sizeof (struct grub_loopback));
 
112
  if (! newdev)
 
113
    goto fail;
 
114
 
 
115
  newdev->devname = grub_strdup (args[0]);
 
116
  if (! newdev->devname)
 
117
    {
 
118
      grub_free (newdev);
 
119
      goto fail;
 
120
    }
 
121
 
 
122
  newdev->file = file;
 
123
 
 
124
  /* Add the new entry to the list.  */
 
125
  newdev->next = loopback_list;
 
126
  loopback_list = newdev;
 
127
 
 
128
  return 0;
 
129
 
 
130
fail:
 
131
  ret = grub_errno;
 
132
  grub_file_close (file);
 
133
  return ret;
 
134
}
 
135
 
 
136
 
 
137
static int
 
138
grub_loopback_iterate (int (*hook) (const char *name))
 
139
{
 
140
  struct grub_loopback *d;
 
141
  for (d = loopback_list; d; d = d->next)
 
142
    {
 
143
      if (hook (d->devname))
 
144
        return 1;
 
145
    }
 
146
  return 0;
 
147
}
 
148
 
 
149
static grub_err_t
 
150
grub_loopback_open (const char *name, grub_disk_t disk)
 
151
{
 
152
  struct grub_loopback *dev;
 
153
 
 
154
  for (dev = loopback_list; dev; dev = dev->next)
 
155
    if (grub_strcmp (dev->devname, name) == 0)
 
156
      break;
 
157
 
 
158
  if (! dev)
 
159
    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
 
160
 
 
161
  /* Use the filesize for the disk size, round up to a complete sector.  */
 
162
  if (dev->file->size != GRUB_FILE_SIZE_UNKNOWN)
 
163
    disk->total_sectors = ((dev->file->size + GRUB_DISK_SECTOR_SIZE - 1)
 
164
                           / GRUB_DISK_SECTOR_SIZE);
 
165
  else
 
166
    disk->total_sectors = GRUB_DISK_SIZE_UNKNOWN;
 
167
  disk->id = (unsigned long) dev;
 
168
 
 
169
  disk->data = dev->file;
 
170
 
 
171
  return 0;
 
172
}
 
173
 
 
174
static grub_err_t
 
175
grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector,
 
176
                    grub_size_t size, char *buf)
 
177
{
 
178
  grub_file_t file = (grub_file_t) disk->data;
 
179
  grub_off_t pos;
 
180
 
 
181
  grub_file_seek (file, sector << GRUB_DISK_SECTOR_BITS);
 
182
 
 
183
  grub_file_read (file, buf, size << GRUB_DISK_SECTOR_BITS);
 
184
  if (grub_errno)
 
185
    return grub_errno;
 
186
 
 
187
  /* In case there is more data read than there is available, in case
 
188
     of files that are not a multiple of GRUB_DISK_SECTOR_SIZE, fill
 
189
     the rest with zeros.  */
 
190
  pos = (sector + size) << GRUB_DISK_SECTOR_BITS;
 
191
  if (pos > file->size)
 
192
    {
 
193
      grub_size_t amount = pos - file->size;
 
194
      grub_memset (buf + (size << GRUB_DISK_SECTOR_BITS) - amount, 0, amount);
 
195
    }
 
196
 
 
197
  return 0;
 
198
}
 
199
 
 
200
static grub_err_t
 
201
grub_loopback_write (grub_disk_t disk __attribute ((unused)),
 
202
                     grub_disk_addr_t sector __attribute ((unused)),
 
203
                     grub_size_t size __attribute ((unused)),
 
204
                     const char *buf __attribute ((unused)))
 
205
{
 
206
  return GRUB_ERR_NOT_IMPLEMENTED_YET;
 
207
}
 
208
 
 
209
static struct grub_disk_dev grub_loopback_dev =
 
210
  {
 
211
    .name = "loopback",
 
212
    .id = GRUB_DISK_DEVICE_LOOPBACK_ID,
 
213
    .iterate = grub_loopback_iterate,
 
214
    .open = grub_loopback_open,
 
215
    .read = grub_loopback_read,
 
216
    .write = grub_loopback_write,
 
217
    .next = 0
 
218
  };
 
219
 
 
220
static grub_extcmd_t cmd;
 
221
 
 
222
GRUB_MOD_INIT(loopback)
 
223
{
 
224
  cmd = grub_register_extcmd ("loopback", grub_cmd_loopback, 0,
 
225
                              N_("[-d|-p] DEVICENAME FILE."),
 
226
                              N_("Make a device of a file."), options);
 
227
  grub_disk_dev_register (&grub_loopback_dev);
 
228
}
 
229
 
 
230
GRUB_MOD_FINI(loopback)
 
231
{
 
232
  grub_unregister_extcmd (cmd);
 
233
  grub_disk_dev_unregister (&grub_loopback_dev);
 
234
}