loopback.c 6.21 KB
Newer Older
1 2 3
/* loopback.c - command to add loopback devices.  */
/*
 *  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 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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 21 22 23 24
 */

#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/file.h>
#include <grub/disk.h>
#include <grub/mm.h>
25
#include <grub/extcmd.h>
26
#include <grub/i18n.h>
27

28 29
GRUB_MOD_LICENSE ("GPLv3+");

30 31 32
struct grub_loopback
{
  char *devname;
33
  grub_file_t file;
34
  struct grub_loopback *next;
35
  unsigned long id;
36 37
};

38
static struct grub_loopback *loopback_list;
39
static unsigned long last_id = 0;
40 41 42

static const struct grub_arg_option options[] =
  {
43 44
    /* TRANSLATORS: The disk is simply removed from the list of available ones,
       not wiped, avoid to scare user.  */
45
    {"delete", 'd', 0, N_("Delete the specified loopback drive."), 0, 0},
46 47 48 49 50 51 52 53 54 55 56 57
    {0, 0, 0, 0, 0, 0}
  };

/* Delete the loopback device NAME.  */
static grub_err_t
delete_loopback (const char *name)
{
  struct grub_loopback *dev;
  struct grub_loopback **prev;

  /* Search for the device.  */
  for (dev = loopback_list, prev = &loopback_list;
58 59 60
       dev;
       prev = &dev->next, dev = dev->next)
    if (grub_strcmp (dev->devname, name) == 0)
61
      break;
62

63
  if (! dev)
64
    return grub_error (GRUB_ERR_BAD_DEVICE, "device not found");
65

66 67 68 69
  /* Remove the device from the list.  */
  *prev = dev->next;

  grub_free (dev->devname);
70
  grub_file_close (dev->file);
71
  grub_free (dev);
72

73 74 75 76 77
  return 0;
}

/* The command to add and remove loopback devices.  */
static grub_err_t
78
grub_cmd_loopback (grub_extcmd_context_t ctxt, int argc, char **args)
79
{
80
  struct grub_arg_list *state = ctxt->state;
81 82
  grub_file_t file;
  struct grub_loopback *newdev;
83
  grub_err_t ret;
84

85 86
  if (argc < 1)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
87

88 89 90
  /* Check if `-d' was used.  */
  if (state[0].set)
      return delete_loopback (args[0]);
91

92
  if (argc < 2)
93
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
94 95 96 97

  file = grub_file_open (args[1]);
  if (! file)
    return grub_errno;
98

99 100
  /* First try to replace the old device.  */
  for (newdev = loopback_list; newdev; newdev = newdev->next)
101
    if (grub_strcmp (newdev->devname, args[0]) == 0)
102
      break;
103

104 105
  if (newdev)
    {
106 107
      grub_file_close (newdev->file);
      newdev->file = file;
108

109 110
      return 0;
    }
111

112 113
  /* Unable to replace it, make a new entry.  */
  newdev = grub_malloc (sizeof (struct grub_loopback));
114
  if (! newdev)
115
    goto fail;
116

117
  newdev->devname = grub_strdup (args[0]);
118
  if (! newdev->devname)
119 120
    {
      grub_free (newdev);
121
      goto fail;
122
    }
123

124
  newdev->file = file;
125
  newdev->id = last_id++;
126

127 128 129
  /* Add the new entry to the list.  */
  newdev->next = loopback_list;
  loopback_list = newdev;
130

131
  return 0;
132 133 134 135 136

fail:
  ret = grub_errno;
  grub_file_close (file);
  return ret;
137 138 139 140
}


static int
141 142
grub_loopback_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
		       grub_disk_pull_t pull)
143 144
{
  struct grub_loopback *d;
145 146
  if (pull != GRUB_DISK_PULL_NONE)
    return 0;
147 148
  for (d = loopback_list; d; d = d->next)
    {
149
      if (hook (d->devname, hook_data))
150 151 152 153 154 155
	return 1;
    }
  return 0;
}

static grub_err_t
156
grub_loopback_open (const char *name, grub_disk_t disk)
157 158
{
  struct grub_loopback *dev;
159

160
  for (dev = loopback_list; dev; dev = dev->next)
161
    if (grub_strcmp (dev->devname, name) == 0)
162
      break;
163

164
  if (! dev)
165
    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
166 167

  /* Use the filesize for the disk size, round up to a complete sector.  */
168 169 170 171 172
  if (dev->file->size != GRUB_FILE_SIZE_UNKNOWN)
    disk->total_sectors = ((dev->file->size + GRUB_DISK_SECTOR_SIZE - 1)
			   / GRUB_DISK_SECTOR_SIZE);
  else
    disk->total_sectors = GRUB_DISK_SIZE_UNKNOWN;
173 174 175 176
  /* Avoid reading more than 512M.  */
  disk->max_agglomerate = 1 << (29 - GRUB_DISK_SECTOR_BITS
				- GRUB_DISK_CACHE_BITS);

177
  disk->id = dev->id;
178

179
  disk->data = dev;
180

181 182 183 184
  return 0;
}

static grub_err_t
185 186
grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector,
		    grub_size_t size, char *buf)
187
{
188
  grub_file_t file = ((struct grub_loopback *) disk->data)->file;
189
  grub_off_t pos;
190

191
  grub_file_seek (file, sector << GRUB_DISK_SECTOR_BITS);
192

193
  grub_file_read (file, buf, size << GRUB_DISK_SECTOR_BITS);
194 195
  if (grub_errno)
    return grub_errno;
196

197 198 199
  /* In case there is more data read than there is available, in case
     of files that are not a multiple of GRUB_DISK_SECTOR_SIZE, fill
     the rest with zeros.  */
200
  pos = (sector + size) << GRUB_DISK_SECTOR_BITS;
201 202
  if (pos > file->size)
    {
203
      grub_size_t amount = pos - file->size;
204
      grub_memset (buf + (size << GRUB_DISK_SECTOR_BITS) - amount, 0, amount);
205
    }
206

207 208 209 210 211
  return 0;
}

static grub_err_t
grub_loopback_write (grub_disk_t disk __attribute ((unused)),
212 213 214
		     grub_disk_addr_t sector __attribute ((unused)),
		     grub_size_t size __attribute ((unused)),
		     const char *buf __attribute ((unused)))
215
{
216 217
  return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
		     "loopback write is not supported");
218 219 220 221 222 223 224 225 226 227 228 229 230
}

static struct grub_disk_dev grub_loopback_dev =
  {
    .name = "loopback",
    .id = GRUB_DISK_DEVICE_LOOPBACK_ID,
    .iterate = grub_loopback_iterate,
    .open = grub_loopback_open,
    .read = grub_loopback_read,
    .write = grub_loopback_write,
    .next = 0
  };

231
static grub_extcmd_t cmd;
232

233
GRUB_MOD_INIT(loopback)
234
{
235
  cmd = grub_register_extcmd ("loopback", grub_cmd_loopback, 0,
236
			      N_("[-d] DEVICENAME FILE."),
237 238
			      /* TRANSLATORS: The file itself is not destroyed
				 or transformed into drive.  */
239
			      N_("Make a virtual drive from a file."), options);
240 241 242
  grub_disk_dev_register (&grub_loopback_dev);
}

243
GRUB_MOD_FINI(loopback)
244
{
245
  grub_unregister_extcmd (cmd);
246 247
  grub_disk_dev_unregister (&grub_loopback_dev);
}