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>
28 GRUB_MOD_LICENSE ("GPLv3+");
34 struct grub_loopback
*next
;
38 static struct grub_loopback
*loopback_list
;
39 static unsigned long last_id
= 0;
41 static const struct grub_arg_option options
[] =
43 /* TRANSLATORS: The disk is simply removed from the list of available ones,
44 not wiped, avoid to scare user. */
45 {"delete", 'd', 0, N_("Delete the specified loopback drive."), 0, 0},
49 /* Delete the loopback device NAME. */
51 delete_loopback (const char *name
)
53 struct grub_loopback
*dev
;
54 struct grub_loopback
**prev
;
56 /* Search for the device. */
57 for (dev
= loopback_list
, prev
= &loopback_list
;
59 prev
= &dev
->next
, dev
= dev
->next
)
60 if (grub_strcmp (dev
->devname
, name
) == 0)
64 return grub_error (GRUB_ERR_BAD_DEVICE
, "device not found");
66 /* Remove the device from the list. */
69 grub_free (dev
->devname
);
70 grub_file_close (dev
->file
);
76 /* The command to add and remove loopback devices. */
78 grub_cmd_loopback (grub_extcmd_context_t ctxt
, int argc
, char **args
)
80 struct grub_arg_list
*state
= ctxt
->state
;
82 struct grub_loopback
*newdev
;
86 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "device name required");
88 /* Check if `-d' was used. */
90 return delete_loopback (args
[0]);
93 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
95 file
= grub_file_open (args
[1]);
99 /* First try to replace the old device. */
100 for (newdev
= loopback_list
; newdev
; newdev
= newdev
->next
)
101 if (grub_strcmp (newdev
->devname
, args
[0]) == 0)
106 grub_file_close (newdev
->file
);
112 /* Unable to replace it, make a new entry. */
113 newdev
= grub_malloc (sizeof (struct grub_loopback
));
117 newdev
->devname
= grub_strdup (args
[0]);
118 if (! newdev
->devname
)
125 newdev
->id
= last_id
++;
127 /* Add the new entry to the list. */
128 newdev
->next
= loopback_list
;
129 loopback_list
= newdev
;
135 grub_file_close (file
);
141 grub_loopback_iterate (grub_disk_dev_iterate_hook_t hook
, void *hook_data
,
142 grub_disk_pull_t pull
)
144 struct grub_loopback
*d
;
145 if (pull
!= GRUB_DISK_PULL_NONE
)
147 for (d
= loopback_list
; d
; d
= d
->next
)
149 if (hook (d
->devname
, hook_data
))
156 grub_loopback_open (const char *name
, grub_disk_t disk
)
158 struct grub_loopback
*dev
;
160 for (dev
= loopback_list
; dev
; dev
= dev
->next
)
161 if (grub_strcmp (dev
->devname
, name
) == 0)
165 return grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "can't open device");
167 /* Use the filesize for the disk size, round up to a complete sector. */
168 if (dev
->file
->size
!= GRUB_FILE_SIZE_UNKNOWN
)
169 disk
->total_sectors
= ((dev
->file
->size
+ GRUB_DISK_SECTOR_SIZE
- 1)
170 / GRUB_DISK_SECTOR_SIZE
);
172 disk
->total_sectors
= GRUB_DISK_SIZE_UNKNOWN
;
173 /* Avoid reading more than 512M. */
174 disk
->max_agglomerate
= 1 << (29 - GRUB_DISK_SECTOR_BITS
175 - GRUB_DISK_CACHE_BITS
);
185 grub_loopback_read (grub_disk_t disk
, grub_disk_addr_t sector
,
186 grub_size_t size
, char *buf
)
188 grub_file_t file
= ((struct grub_loopback
*) disk
->data
)->file
;
191 grub_file_seek (file
, sector
<< GRUB_DISK_SECTOR_BITS
);
193 grub_file_read (file
, buf
, size
<< GRUB_DISK_SECTOR_BITS
);
197 /* In case there is more data read than there is available, in case
198 of files that are not a multiple of GRUB_DISK_SECTOR_SIZE, fill
199 the rest with zeros. */
200 pos
= (sector
+ size
) << GRUB_DISK_SECTOR_BITS
;
201 if (pos
> file
->size
)
203 grub_size_t amount
= pos
- file
->size
;
204 grub_memset (buf
+ (size
<< GRUB_DISK_SECTOR_BITS
) - amount
, 0, amount
);
211 grub_loopback_write (grub_disk_t disk
__attribute ((unused
)),
212 grub_disk_addr_t sector
__attribute ((unused
)),
213 grub_size_t size
__attribute ((unused
)),
214 const char *buf
__attribute ((unused
)))
216 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
217 "loopback write is not supported");
220 static struct grub_disk_dev grub_loopback_dev
=
223 .id
= GRUB_DISK_DEVICE_LOOPBACK_ID
,
224 .iterate
= grub_loopback_iterate
,
225 .open
= grub_loopback_open
,
226 .read
= grub_loopback_read
,
227 .write
= grub_loopback_write
,
231 static grub_extcmd_t cmd
;
233 GRUB_MOD_INIT(loopback
)
235 cmd
= grub_register_extcmd ("loopback", grub_cmd_loopback
, 0,
236 N_("[-d] DEVICENAME FILE."),
237 /* TRANSLATORS: The file itself is not destroyed
238 or transformed into drive. */
239 N_("Make a virtual drive from a file."), options
);
240 grub_disk_dev_register (&grub_loopback_dev
);
243 GRUB_MOD_FINI(loopback
)
245 grub_unregister_extcmd (cmd
);
246 grub_disk_dev_unregister (&grub_loopback_dev
);