loopback.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* loopback.c - command to add loopback devices. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2006,2007 Free Software Foundation, Inc.
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/file.h>
  22. #include <grub/disk.h>
  23. #include <grub/mm.h>
  24. #include <grub/env.h>
  25. #include <grub/extcmd.h>
  26. #include <grub/i18n.h>
  27. struct grub_loopback
  28. {
  29. char *devname;
  30. char *filename;
  31. grub_file_t file;
  32. int has_partitions;
  33. struct grub_loopback *next;
  34. };
  35. static struct grub_loopback *loopback_list;
  36. static const struct grub_arg_option options[] =
  37. {
  38. {"delete", 'd', 0, N_("Delete the loopback device entry."), 0, 0},
  39. {"partitions", 'p', 0, N_("Simulate a hard drive with partitions."), 0, 0},
  40. {0, 0, 0, 0, 0, 0}
  41. };
  42. /* Delete the loopback device NAME. */
  43. static grub_err_t
  44. delete_loopback (const char *name)
  45. {
  46. struct grub_loopback *dev;
  47. struct grub_loopback **prev;
  48. /* Search for the device. */
  49. for (dev = loopback_list, prev = &loopback_list;
  50. dev;
  51. prev = &dev->next, dev = dev->next)
  52. if (grub_strcmp (dev->devname, name) == 0)
  53. break;
  54. if (! dev)
  55. return grub_error (GRUB_ERR_BAD_DEVICE, "device not found");
  56. /* Remove the device from the list. */
  57. *prev = dev->next;
  58. grub_file_close (dev->file);
  59. grub_free (dev->devname);
  60. grub_free (dev);
  61. return 0;
  62. }
  63. /* The command to add and remove loopback devices. */
  64. static grub_err_t
  65. grub_cmd_loopback (grub_extcmd_t cmd, int argc, char **args)
  66. {
  67. struct grub_arg_list *state = state = cmd->state;
  68. grub_file_t file;
  69. struct grub_loopback *newdev;
  70. if (argc < 1)
  71. return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
  72. /* Check if `-d' was used. */
  73. if (state[0].set)
  74. return delete_loopback (args[0]);
  75. if (argc < 2)
  76. return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
  77. file = grub_file_open (args[1]);
  78. if (! file)
  79. return grub_errno;
  80. grub_blocklist_convert (file);
  81. /* First try to replace the old device. */
  82. for (newdev = loopback_list; newdev; newdev = newdev->next)
  83. if (grub_strcmp (newdev->devname, args[0]) == 0)
  84. break;
  85. if (newdev)
  86. {
  87. grub_file_close (newdev->file);
  88. newdev->file = file;
  89. /* Set has_partitions when `--partitions' was used. */
  90. newdev->has_partitions = state[1].set;
  91. return 0;
  92. }
  93. /* Unable to replace it, make a new entry. */
  94. newdev = grub_malloc (sizeof (struct grub_loopback));
  95. if (! newdev)
  96. goto fail;
  97. newdev->devname = grub_strdup (args[0]);
  98. if (! newdev->devname)
  99. goto fail;
  100. newdev->file = file;
  101. /* Set has_partitions when `--partitions' was used. */
  102. newdev->has_partitions = state[1].set;
  103. /* Add the new entry to the list. */
  104. newdev->next = loopback_list;
  105. loopback_list = newdev;
  106. return 0;
  107. fail:
  108. grub_free (newdev);
  109. grub_file_close (file);
  110. return grub_errno;
  111. }
  112. static int
  113. grub_loopback_iterate (int (*hook) (const char *name, void *closure),
  114. void *closure)
  115. {
  116. struct grub_loopback *d;
  117. for (d = loopback_list; d; d = d->next)
  118. {
  119. if (hook (d->devname, closure))
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. static grub_err_t
  125. grub_loopback_open (const char *name, grub_disk_t disk)
  126. {
  127. grub_file_t file;
  128. struct grub_loopback *dev;
  129. for (dev = loopback_list; dev; dev = dev->next)
  130. if (grub_strcmp (dev->devname, name) == 0)
  131. break;
  132. if (! dev)
  133. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
  134. file = dev->file;
  135. /* Use the filesize for the disk size, round up to a complete sector. */
  136. disk->total_sectors = ((file->size + GRUB_DISK_SECTOR_SIZE - 1)
  137. / GRUB_DISK_SECTOR_SIZE);
  138. disk->id = (unsigned long) dev;
  139. disk->has_partitions = dev->has_partitions;
  140. disk->data = file;
  141. return 0;
  142. }
  143. static grub_err_t
  144. grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector,
  145. grub_size_t size, char *buf)
  146. {
  147. grub_file_t file = (grub_file_t) disk->data;
  148. grub_off_t pos;
  149. grub_file_seek (file, sector << GRUB_DISK_SECTOR_BITS);
  150. grub_file_read (file, buf, size << GRUB_DISK_SECTOR_BITS);
  151. if (grub_errno)
  152. return grub_errno;
  153. /* In case there is more data read than there is available, in case
  154. of files that are not a multiple of GRUB_DISK_SECTOR_SIZE, fill
  155. the rest with zeros. */
  156. pos = (sector + size) << GRUB_DISK_SECTOR_BITS;
  157. if (pos > file->size)
  158. {
  159. grub_size_t amount = pos - file->size;
  160. grub_memset (buf + (size << GRUB_DISK_SECTOR_BITS) - amount, 0, amount);
  161. }
  162. return 0;
  163. }
  164. static grub_err_t
  165. grub_loopback_write (grub_disk_t disk, grub_disk_addr_t sector,
  166. grub_size_t size, const char *buf)
  167. {
  168. grub_file_t file = (grub_file_t) disk->data;
  169. grub_file_seek (file, sector << GRUB_DISK_SECTOR_BITS);
  170. return (grub_blocklist_write (file, buf,
  171. size << GRUB_DISK_SECTOR_BITS) >= 0) ?
  172. grub_errno :
  173. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "write operation not supported");
  174. }
  175. static struct grub_disk_dev grub_loopback_dev =
  176. {
  177. .name = "loopback",
  178. .id = GRUB_DISK_DEVICE_LOOPBACK_ID,
  179. .iterate = grub_loopback_iterate,
  180. .open = grub_loopback_open,
  181. .read = grub_loopback_read,
  182. .write = grub_loopback_write,
  183. .next = 0
  184. };
  185. static grub_extcmd_t cmd;
  186. GRUB_MOD_INIT(loopback)
  187. {
  188. cmd = grub_register_extcmd ("loopback", grub_cmd_loopback,
  189. GRUB_COMMAND_FLAG_BOTH,
  190. N_("[-d|-p] DEVICENAME FILE."),
  191. N_("Make a device of a file."), options);
  192. grub_disk_dev_register (&grub_loopback_dev);
  193. }
  194. GRUB_MOD_FINI(loopback)
  195. {
  196. grub_unregister_extcmd (cmd);
  197. grub_disk_dev_unregister (&grub_loopback_dev);
  198. }