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

« back to all changes in this revision

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