added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / arch / common / grub2 / disk / loopback.c
blob31d8116889a4853047597edf204e64c2c7b39f24
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/normal.h>
21 #include <grub/dl.h>
22 #include <grub/arg.h>
23 #include <grub/misc.h>
24 #include <grub/file.h>
25 #include <grub/disk.h>
26 #include <grub/mm.h>
28 struct grub_loopback
30 char *devname;
31 char *filename;
32 int has_partitions;
33 struct grub_loopback *next;
36 static struct grub_loopback *loopback_list;
38 static const struct grub_arg_option options[] =
40 {"delete", 'd', 0, "delete the loopback device entry", 0, 0},
41 {"partitions", 'p', 0, "set that the drive has partitions to"
42 " simulate a harddrive", 0, 0},
43 {0, 0, 0, 0, 0, 0}
46 /* Delete the loopback device NAME. */
47 static grub_err_t
48 delete_loopback (const char *name)
50 struct grub_loopback *dev;
51 struct grub_loopback **prev;
53 /* Search for the device. */
54 for (dev = loopback_list, prev = &loopback_list;
55 dev;
56 prev = &dev->next, dev = dev->next)
57 if (grub_strcmp (dev->devname, name) == 0)
58 break;
60 if (! dev)
61 return grub_error (GRUB_ERR_BAD_DEVICE, "Device not found");
63 /* Remove the device from the list. */
64 *prev = dev->next;
66 grub_free (dev->devname);
67 grub_free (dev->filename);
68 grub_free (dev);
70 return 0;
73 /* The command to add and remove loopback devices. */
74 static grub_err_t
75 grub_cmd_loopback (struct grub_arg_list *state,
76 int argc, char **args)
78 grub_file_t file;
79 struct grub_loopback *newdev;
81 if (argc < 1)
82 return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
84 /* Check if `-d' was used. */
85 if (state[0].set)
86 return delete_loopback (args[0]);
88 if (argc < 2)
89 return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
91 file = grub_file_open (args[1]);
92 if (! file)
93 return grub_errno;
95 /* Close the file, the only reason for opening it is validation. */
96 grub_file_close (file);
98 /* First try to replace the old device. */
99 for (newdev = loopback_list; newdev; newdev = newdev->next)
100 if (grub_strcmp (newdev->devname, args[0]) == 0)
101 break;
103 if (newdev)
105 char *newname = grub_strdup (args[1]);
106 if (! newname)
107 return grub_errno;
109 grub_free (newdev->filename);
110 newdev->filename = newname;
112 /* Set has_partitions when `--partitions' was used. */
113 newdev->has_partitions = state[1].set;
115 return 0;
118 /* Unable to replace it, make a new entry. */
119 newdev = grub_malloc (sizeof (struct grub_loopback));
120 if (! newdev)
121 return grub_errno;
123 newdev->devname = grub_strdup (args[0]);
124 if (! newdev->devname)
126 grub_free (newdev);
127 return grub_errno;
130 newdev->filename = grub_strdup (args[1]);
131 if (! newdev->filename)
133 grub_free (newdev->devname);
134 grub_free (newdev);
135 return grub_errno;
138 /* Set has_partitions when `--partitions' was used. */
139 newdev->has_partitions = state[1].set;
141 /* Add the new entry to the list. */
142 newdev->next = loopback_list;
143 loopback_list = newdev;
145 return 0;
149 static int
150 grub_loopback_iterate (int (*hook) (const char *name))
152 struct grub_loopback *d;
153 for (d = loopback_list; d; d = d->next)
155 if (hook (d->devname))
156 return 1;
158 return 0;
161 static grub_err_t
162 grub_loopback_open (const char *name, grub_disk_t disk)
164 grub_file_t file;
165 struct grub_loopback *dev;
167 for (dev = loopback_list; dev; dev = dev->next)
168 if (grub_strcmp (dev->devname, name) == 0)
169 break;
171 if (! dev)
172 return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Can't open device");
174 file = grub_file_open (dev->filename);
175 if (! file)
176 return grub_errno;
178 /* Use the filesize for the disk size, round up to a complete sector. */
179 disk->total_sectors = ((file->size + GRUB_DISK_SECTOR_SIZE - 1)
180 / GRUB_DISK_SECTOR_SIZE);
181 disk->id = (int) dev;
183 disk->has_partitions = dev->has_partitions;
184 disk->data = file;
186 return 0;
189 static void
190 grub_loopback_close (grub_disk_t disk)
192 grub_file_t file = (grub_file_t) disk->data;
194 grub_file_close (file);
197 static grub_err_t
198 grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector,
199 grub_size_t size, char *buf)
201 grub_file_t file = (grub_file_t) disk->data;
202 grub_off_t pos;
204 grub_file_seek (file, sector << GRUB_DISK_SECTOR_BITS);
206 grub_file_read (file, buf, size << GRUB_DISK_SECTOR_BITS);
207 if (grub_errno)
208 return grub_errno;
210 /* In case there is more data read than there is available, in case
211 of files that are not a multiple of GRUB_DISK_SECTOR_SIZE, fill
212 the rest with zeros. */
213 pos = (sector + size) << GRUB_DISK_SECTOR_BITS;
214 if (pos > file->size)
216 grub_size_t amount = pos - file->size;
217 grub_memset (buf + (size << GRUB_DISK_SECTOR_BITS) - amount, 0, amount);
220 return 0;
223 static grub_err_t
224 grub_loopback_write (grub_disk_t disk __attribute ((unused)),
225 grub_disk_addr_t sector __attribute ((unused)),
226 grub_size_t size __attribute ((unused)),
227 const char *buf __attribute ((unused)))
229 return GRUB_ERR_NOT_IMPLEMENTED_YET;
232 static struct grub_disk_dev grub_loopback_dev =
234 .name = "loopback",
235 .id = GRUB_DISK_DEVICE_LOOPBACK_ID,
236 .iterate = grub_loopback_iterate,
237 .open = grub_loopback_open,
238 .close = grub_loopback_close,
239 .read = grub_loopback_read,
240 .write = grub_loopback_write,
241 .next = 0
246 GRUB_MOD_INIT(loop)
248 (void) mod; /* To stop warning. */
249 grub_register_command ("loopback", grub_cmd_loopback, GRUB_COMMAND_FLAG_BOTH,
250 "loopback [-d|-p] DEVICENAME FILE",
251 "Make a device of a file.", options);
252 grub_disk_dev_register (&grub_loopback_dev);
255 GRUB_MOD_FINI(loop)
257 grub_unregister_command ("loopback");
258 grub_disk_dev_unregister (&grub_loopback_dev);