loopback.c 6.9 KB

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