1
/* loopback.c - command to add loopback devices. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2005,2006,2007 Free Software Foundation, Inc.
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.
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.
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/>.
21
#include <grub/misc.h>
22
#include <grub/file.h>
23
#include <grub/disk.h>
25
#include <grub/extcmd.h>
26
#include <grub/i18n.h>
33
struct grub_loopback *next;
36
static struct grub_loopback *loopback_list;
38
static const struct grub_arg_option options[] =
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},
45
/* Delete the loopback device NAME. */
47
delete_loopback (const char *name)
49
struct grub_loopback *dev;
50
struct grub_loopback **prev;
52
/* Search for the device. */
53
for (dev = loopback_list, prev = &loopback_list;
55
prev = &dev->next, dev = dev->next)
56
if (grub_strcmp (dev->devname, name) == 0)
60
return grub_error (GRUB_ERR_BAD_DEVICE, "device not found");
62
/* Remove the device from the list. */
65
grub_free (dev->devname);
66
grub_file_close (dev->file);
72
/* The command to add and remove loopback devices. */
74
grub_cmd_loopback (grub_extcmd_t cmd, int argc, char **args)
76
struct grub_arg_list *state = state = cmd->state;
78
struct grub_loopback *newdev;
82
return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
84
/* Check if `-d' was used. */
86
return delete_loopback (args[0]);
89
return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
91
file = grub_file_open (args[1]);
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)
102
char *newname = grub_strdup (args[1]);
106
grub_file_close (newdev->file);
109
/* Set has_partitions when `--partitions' was used. */
110
newdev->has_partitions = state[1].set;
115
/* Unable to replace it, make a new entry. */
116
newdev = grub_malloc (sizeof (struct grub_loopback));
120
newdev->devname = grub_strdup (args[0]);
121
if (! newdev->devname)
129
/* Set has_partitions when `--partitions' was used. */
130
newdev->has_partitions = state[1].set;
132
/* Add the new entry to the list. */
133
newdev->next = loopback_list;
134
loopback_list = newdev;
140
grub_file_close (file);
146
grub_loopback_iterate (int (*hook) (const char *name))
148
struct grub_loopback *d;
149
for (d = loopback_list; d; d = d->next)
151
if (hook (d->devname))
158
grub_loopback_open (const char *name, grub_disk_t disk)
160
struct grub_loopback *dev;
162
for (dev = loopback_list; dev; dev = dev->next)
163
if (grub_strcmp (dev->devname, name) == 0)
167
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
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;
174
disk->has_partitions = dev->has_partitions;
175
disk->data = dev->file;
181
grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector,
182
grub_size_t size, char *buf)
184
grub_file_t file = (grub_file_t) disk->data;
187
grub_file_seek (file, sector << GRUB_DISK_SECTOR_BITS);
189
grub_file_read (file, buf, size << GRUB_DISK_SECTOR_BITS);
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)
199
grub_size_t amount = pos - file->size;
200
grub_memset (buf + (size << GRUB_DISK_SECTOR_BITS) - amount, 0, amount);
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)))
212
return GRUB_ERR_NOT_IMPLEMENTED_YET;
215
static struct grub_disk_dev grub_loopback_dev =
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,
226
static grub_extcmd_t cmd;
228
GRUB_MOD_INIT(loopback)
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);
237
GRUB_MOD_FINI(loopback)
239
grub_unregister_extcmd (cmd);
240
grub_disk_dev_unregister (&grub_loopback_dev);