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