Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / disk / loopback.c
blob2d8deaeafbd128cf6b791adc4a50fec4e53770a4
1 /* loopback.c - command to add loopback devices. */
2 /*
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/>.
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>
28 GRUB_MOD_LICENSE ("GPLv3+");
30 struct grub_loopback
32 char *devname;
33 grub_file_t file;
34 struct grub_loopback *next;
35 unsigned long id;
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},
46 {0, 0, 0, 0, 0, 0}
49 /* Delete the loopback device NAME. */
50 static grub_err_t
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;
58 dev;
59 prev = &dev->next, dev = dev->next)
60 if (grub_strcmp (dev->devname, name) == 0)
61 break;
63 if (! dev)
64 return grub_error (GRUB_ERR_BAD_DEVICE, "device not found");
66 /* Remove the device from the list. */
67 *prev = dev->next;
69 grub_free (dev->devname);
70 grub_file_close (dev->file);
71 grub_free (dev);
73 return 0;
76 /* The command to add and remove loopback devices. */
77 static grub_err_t
78 grub_cmd_loopback (grub_extcmd_context_t ctxt, int argc, char **args)
80 struct grub_arg_list *state = ctxt->state;
81 grub_file_t file;
82 struct grub_loopback *newdev;
83 grub_err_t ret;
85 if (argc < 1)
86 return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
88 /* Check if `-d' was used. */
89 if (state[0].set)
90 return delete_loopback (args[0]);
92 if (argc < 2)
93 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
95 file = grub_file_open (args[1]);
96 if (! file)
97 return grub_errno;
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)
102 break;
104 if (newdev)
106 grub_file_close (newdev->file);
107 newdev->file = file;
109 return 0;
112 /* Unable to replace it, make a new entry. */
113 newdev = grub_malloc (sizeof (struct grub_loopback));
114 if (! newdev)
115 goto fail;
117 newdev->devname = grub_strdup (args[0]);
118 if (! newdev->devname)
120 grub_free (newdev);
121 goto fail;
124 newdev->file = file;
125 newdev->id = last_id++;
127 /* Add the new entry to the list. */
128 newdev->next = loopback_list;
129 loopback_list = newdev;
131 return 0;
133 fail:
134 ret = grub_errno;
135 grub_file_close (file);
136 return ret;
140 static int
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)
146 return 0;
147 for (d = loopback_list; d; d = d->next)
149 if (hook (d->devname, hook_data))
150 return 1;
152 return 0;
155 static grub_err_t
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)
162 break;
164 if (! dev)
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);
171 else
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);
177 disk->id = dev->id;
179 disk->data = dev;
181 return 0;
184 static grub_err_t
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;
189 grub_off_t pos;
191 grub_file_seek (file, sector << GRUB_DISK_SECTOR_BITS);
193 grub_file_read (file, buf, size << GRUB_DISK_SECTOR_BITS);
194 if (grub_errno)
195 return grub_errno;
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);
207 return 0;
210 static grub_err_t
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 =
222 .name = "loopback",
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,
228 .next = 0
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);