Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / common / boot / grub2 / disk / loopback.c
blob709c422dfe074fcad0828129d5c3d8095b5d1883
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, "simulate a hard drive with partitions", 0, 0},
42 {0, 0, 0, 0, 0, 0}
45 /* Delete the loopback device NAME. */
46 static grub_err_t
47 delete_loopback (const char *name)
49 struct grub_loopback *dev;
50 struct grub_loopback **prev;
52 /* Search for the device. */
53 for (dev = loopback_list, prev = &loopback_list;
54 dev;
55 prev = &dev->next, dev = dev->next)
56 if (grub_strcmp (dev->devname, name) == 0)
57 break;
59 if (! dev)
60 return grub_error (GRUB_ERR_BAD_DEVICE, "Device not found");
62 /* Remove the device from the list. */
63 *prev = dev->next;
65 grub_free (dev->devname);
66 grub_free (dev->filename);
67 grub_free (dev);
69 return 0;
72 /* The command to add and remove loopback devices. */
73 static grub_err_t
74 grub_cmd_loopback (struct grub_arg_list *state,
75 int argc, char **args)
77 grub_file_t file;
78 struct grub_loopback *newdev;
80 if (argc < 1)
81 return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
83 /* Check if `-d' was used. */
84 if (state[0].set)
85 return delete_loopback (args[0]);
87 if (argc < 2)
88 return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
90 file = grub_file_open (args[1]);
91 if (! file)
92 return grub_errno;
94 /* Close the file, the only reason for opening it is validation. */
95 grub_file_close (file);
97 /* First try to replace the old device. */
98 for (newdev = loopback_list; newdev; newdev = newdev->next)
99 if (grub_strcmp (newdev->devname, args[0]) == 0)
100 break;
102 if (newdev)
104 char *newname = grub_strdup (args[1]);
105 if (! newname)
106 return grub_errno;
108 grub_free (newdev->filename);
109 newdev->filename = newname;
111 /* Set has_partitions when `--partitions' was used. */
112 newdev->has_partitions = state[1].set;
114 return 0;
117 /* Unable to replace it, make a new entry. */
118 newdev = grub_malloc (sizeof (struct grub_loopback));
119 if (! newdev)
120 return grub_errno;
122 newdev->devname = grub_strdup (args[0]);
123 if (! newdev->devname)
125 grub_free (newdev);
126 return grub_errno;
129 newdev->filename = grub_strdup (args[1]);
130 if (! newdev->filename)
132 grub_free (newdev->devname);
133 grub_free (newdev);
134 return grub_errno;
137 /* Set has_partitions when `--partitions' was used. */
138 newdev->has_partitions = state[1].set;
140 /* Add the new entry to the list. */
141 newdev->next = loopback_list;
142 loopback_list = newdev;
144 return 0;
148 static int
149 grub_loopback_iterate (int (*hook) (const char *name))
151 struct grub_loopback *d;
152 for (d = loopback_list; d; d = d->next)
154 if (hook (d->devname))
155 return 1;
157 return 0;
160 static grub_err_t
161 grub_loopback_open (const char *name, grub_disk_t disk)
163 grub_file_t file;
164 struct grub_loopback *dev;
166 for (dev = loopback_list; dev; dev = dev->next)
167 if (grub_strcmp (dev->devname, name) == 0)
168 break;
170 if (! dev)
171 return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Can't open device");
173 file = grub_file_open (dev->filename);
174 if (! file)
175 return grub_errno;
177 /* Use the filesize for the disk size, round up to a complete sector. */
178 disk->total_sectors = ((file->size + GRUB_DISK_SECTOR_SIZE - 1)
179 / GRUB_DISK_SECTOR_SIZE);
180 disk->id = (unsigned long) dev;
182 disk->has_partitions = dev->has_partitions;
183 disk->data = file;
185 return 0;
188 static void
189 grub_loopback_close (grub_disk_t disk)
191 grub_file_t file = (grub_file_t) disk->data;
193 grub_file_close (file);
196 static grub_err_t
197 grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector,
198 grub_size_t size, char *buf)
200 grub_file_t file = (grub_file_t) disk->data;
201 grub_off_t pos;
203 grub_file_seek (file, sector << GRUB_DISK_SECTOR_BITS);
205 grub_file_read (file, buf, size << GRUB_DISK_SECTOR_BITS);
206 if (grub_errno)
207 return grub_errno;
209 /* In case there is more data read than there is available, in case
210 of files that are not a multiple of GRUB_DISK_SECTOR_SIZE, fill
211 the rest with zeros. */
212 pos = (sector + size) << GRUB_DISK_SECTOR_BITS;
213 if (pos > file->size)
215 grub_size_t amount = pos - file->size;
216 grub_memset (buf + (size << GRUB_DISK_SECTOR_BITS) - amount, 0, amount);
219 return 0;
222 static grub_err_t
223 grub_loopback_write (grub_disk_t disk __attribute ((unused)),
224 grub_disk_addr_t sector __attribute ((unused)),
225 grub_size_t size __attribute ((unused)),
226 const char *buf __attribute ((unused)))
228 return GRUB_ERR_NOT_IMPLEMENTED_YET;
231 static struct grub_disk_dev grub_loopback_dev =
233 .name = "loopback",
234 .id = GRUB_DISK_DEVICE_LOOPBACK_ID,
235 .iterate = grub_loopback_iterate,
236 .open = grub_loopback_open,
237 .close = grub_loopback_close,
238 .read = grub_loopback_read,
239 .write = grub_loopback_write,
240 .next = 0
245 GRUB_MOD_INIT(loop)
247 (void) mod; /* To stop warning. */
248 grub_register_command ("loopback", grub_cmd_loopback, GRUB_COMMAND_FLAG_BOTH,
249 "loopback [-d|-p] DEVICENAME FILE",
250 "Make a device of a file.", options);
251 grub_disk_dev_register (&grub_loopback_dev);
254 GRUB_MOD_FINI(loop)
256 grub_unregister_command ("loopback");
257 grub_disk_dev_unregister (&grub_loopback_dev);