loopback.c 6.2 KB

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