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
;
39 static struct grub_loopback
*loopback_list
;
40 static unsigned long last_id
= 0;
42 static const struct grub_arg_option options
[] =
44 /* TRANSLATORS: The disk is simply removed from the list of available ones,
45 not wiped, avoid to scare user. */
46 {"delete", 'd', 0, N_("Delete the specified loopback drive."), 0, 0},
47 {"skip", 's', 0, "skip sectors of the file.", "SECTORS", ARG_TYPE_INT
},
51 /* Delete the loopback device NAME. */
53 delete_loopback (const char *name
)
55 struct grub_loopback
*dev
;
56 struct grub_loopback
**prev
;
58 /* Search for the device. */
59 for (dev
= loopback_list
, prev
= &loopback_list
;
61 prev
= &dev
->next
, dev
= dev
->next
)
62 if (grub_strcmp (dev
->devname
, name
) == 0)
66 return grub_error (GRUB_ERR_BAD_DEVICE
, "device not found");
68 /* Remove the device from the list. */
71 grub_free (dev
->devname
);
72 grub_file_close (dev
->file
);
78 /* The command to add and remove loopback devices. */
80 grub_cmd_loopback (grub_extcmd_context_t ctxt
, int argc
, char **args
)
82 struct grub_arg_list
*state
= ctxt
->state
;
84 struct grub_loopback
*newdev
;
89 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "device name required");
91 /* Check if `-d' was used. */
93 return delete_loopback (args
[0]);
96 skip
= (grub_off_t
)grub_strtoull(state
[1].arg
, NULL
, 10);
99 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
101 file
= grub_file_open (args
[1], GRUB_FILE_TYPE_LOOPBACK
102 | GRUB_FILE_TYPE_NO_DECOMPRESS
);
106 /* First try to replace the old device. */
107 for (newdev
= loopback_list
; newdev
; newdev
= newdev
->next
)
108 if (grub_strcmp (newdev
->devname
, args
[0]) == 0)
113 grub_file_close (newdev
->file
);
120 /* Unable to replace it, make a new entry. */
121 newdev
= grub_malloc (sizeof (struct grub_loopback
));
125 newdev
->devname
= grub_strdup (args
[0]);
126 if (! newdev
->devname
)
134 newdev
->id
= last_id
++;
136 /* Add the new entry to the list. */
137 newdev
->next
= loopback_list
;
138 loopback_list
= newdev
;
144 grub_file_close (file
);
150 grub_loopback_iterate (grub_disk_dev_iterate_hook_t hook
, void *hook_data
,
151 grub_disk_pull_t pull
)
153 struct grub_loopback
*d
;
154 if (pull
!= GRUB_DISK_PULL_NONE
)
156 for (d
= loopback_list
; d
; d
= d
->next
)
158 if (hook (d
->devname
, hook_data
))
165 grub_loopback_open (const char *name
, grub_disk_t disk
)
167 struct grub_loopback
*dev
;
169 for (dev
= loopback_list
; dev
; dev
= dev
->next
)
170 if (grub_strcmp (dev
->devname
, name
) == 0)
174 return grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "can't open device");
176 /* Use the filesize for the disk size, round up to a complete sector. */
177 if (dev
->file
->size
!= GRUB_FILE_SIZE_UNKNOWN
)
178 disk
->total_sectors
= ((dev
->file
->size
+ GRUB_DISK_SECTOR_SIZE
- 1)
179 / GRUB_DISK_SECTOR_SIZE
);
181 disk
->total_sectors
= GRUB_DISK_SIZE_UNKNOWN
;
182 /* Avoid reading more than 512M. */
183 disk
->max_agglomerate
= 1 << (29 - GRUB_DISK_SECTOR_BITS
184 - GRUB_DISK_CACHE_BITS
);
194 grub_loopback_read (grub_disk_t disk
, grub_disk_addr_t sector
,
195 grub_size_t size
, char *buf
)
197 grub_file_t file
= ((struct grub_loopback
*) disk
->data
)->file
;
198 grub_off_t skip
= ((struct grub_loopback
*) disk
->data
)->skip
;
201 grub_file_seek (file
, (sector
+ skip
) << GRUB_DISK_SECTOR_BITS
);
203 grub_file_read (file
, buf
, size
<< GRUB_DISK_SECTOR_BITS
);
207 /* In case there is more data read than there is available, in case
208 of files that are not a multiple of GRUB_DISK_SECTOR_SIZE, fill
209 the rest with zeros. */
210 pos
= (sector
+ skip
+ size
) << GRUB_DISK_SECTOR_BITS
;
211 if (pos
> file
->size
)
213 grub_size_t amount
= pos
- file
->size
;
214 grub_memset (buf
+ (size
<< GRUB_DISK_SECTOR_BITS
) - amount
, 0, amount
);
221 grub_loopback_write (grub_disk_t disk
__attribute ((unused
)),
222 grub_disk_addr_t sector
__attribute ((unused
)),
223 grub_size_t size
__attribute ((unused
)),
224 const char *buf
__attribute ((unused
)))
226 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
227 "loopback write is not supported");
230 static struct grub_disk_dev grub_loopback_dev
=
233 .id
= GRUB_DISK_DEVICE_LOOPBACK_ID
,
234 .disk_iterate
= grub_loopback_iterate
,
235 .disk_open
= grub_loopback_open
,
236 .disk_read
= grub_loopback_read
,
237 .disk_write
= grub_loopback_write
,
241 static grub_extcmd_t cmd
;
243 GRUB_MOD_INIT(loopback
)
245 cmd
= grub_register_extcmd ("loopback", grub_cmd_loopback
, 0,
246 N_("[-d] DEVICENAME FILE."),
247 /* TRANSLATORS: The file itself is not destroyed
248 or transformed into drive. */
249 N_("Make a virtual drive from a file."), options
);
250 grub_disk_dev_register (&grub_loopback_dev
);
253 GRUB_MOD_FINI(loopback
)
255 grub_unregister_extcmd (cmd
);
256 grub_disk_dev_unregister (&grub_loopback_dev
);