loopback.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /* Check that a device with requested name does not already exist. */
  80. for (newdev = loopback_list; newdev; newdev = newdev->next)
  81. if (grub_strcmp (newdev->devname, args[0]) == 0)
  82. return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name already exists");
  83. file = grub_file_open (args[1], GRUB_FILE_TYPE_LOOPBACK
  84. | GRUB_FILE_TYPE_NO_DECOMPRESS);
  85. if (! file)
  86. return grub_errno;
  87. /* Unable to replace it, make a new entry. */
  88. newdev = grub_malloc (sizeof (struct grub_loopback));
  89. if (! newdev)
  90. goto fail;
  91. newdev->devname = grub_strdup (args[0]);
  92. if (! newdev->devname)
  93. {
  94. grub_free (newdev);
  95. goto fail;
  96. }
  97. newdev->file = file;
  98. newdev->id = last_id++;
  99. /* Add the new entry to the list. */
  100. newdev->next = loopback_list;
  101. loopback_list = newdev;
  102. return 0;
  103. fail:
  104. ret = grub_errno;
  105. grub_file_close (file);
  106. return ret;
  107. }
  108. static int
  109. grub_loopback_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  110. grub_disk_pull_t pull)
  111. {
  112. struct grub_loopback *d;
  113. if (pull != GRUB_DISK_PULL_NONE)
  114. return 0;
  115. for (d = loopback_list; d; d = d->next)
  116. {
  117. if (hook (d->devname, hook_data))
  118. return 1;
  119. }
  120. return 0;
  121. }
  122. static grub_err_t
  123. grub_loopback_open (const char *name, grub_disk_t disk)
  124. {
  125. struct grub_loopback *dev;
  126. for (dev = loopback_list; dev; dev = dev->next)
  127. if (grub_strcmp (dev->devname, name) == 0)
  128. break;
  129. if (! dev)
  130. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
  131. /* Use the filesize for the disk size, round up to a complete sector. */
  132. if (dev->file->size != GRUB_FILE_SIZE_UNKNOWN)
  133. disk->total_sectors = ((dev->file->size + GRUB_DISK_SECTOR_SIZE - 1)
  134. / GRUB_DISK_SECTOR_SIZE);
  135. else
  136. disk->total_sectors = GRUB_DISK_SIZE_UNKNOWN;
  137. /* Avoid reading more than 512M. */
  138. disk->max_agglomerate = 1 << (29 - GRUB_DISK_SECTOR_BITS
  139. - GRUB_DISK_CACHE_BITS);
  140. disk->id = dev->id;
  141. disk->data = dev;
  142. return 0;
  143. }
  144. static grub_err_t
  145. grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector,
  146. grub_size_t size, char *buf)
  147. {
  148. grub_file_t file = ((struct grub_loopback *) disk->data)->file;
  149. grub_off_t pos;
  150. grub_file_seek (file, sector << GRUB_DISK_SECTOR_BITS);
  151. grub_file_read (file, buf, size << GRUB_DISK_SECTOR_BITS);
  152. if (grub_errno)
  153. return grub_errno;
  154. /* In case there is more data read than there is available, in case
  155. of files that are not a multiple of GRUB_DISK_SECTOR_SIZE, fill
  156. the rest with zeros. */
  157. pos = (sector + size) << GRUB_DISK_SECTOR_BITS;
  158. if (pos > file->size)
  159. {
  160. grub_size_t amount = pos - file->size;
  161. grub_memset (buf + (size << GRUB_DISK_SECTOR_BITS) - amount, 0, amount);
  162. }
  163. return 0;
  164. }
  165. static grub_err_t
  166. grub_loopback_write (grub_disk_t disk __attribute ((unused)),
  167. grub_disk_addr_t sector __attribute ((unused)),
  168. grub_size_t size __attribute ((unused)),
  169. const char *buf __attribute ((unused)))
  170. {
  171. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  172. "loopback write is not supported");
  173. }
  174. static struct grub_disk_dev grub_loopback_dev =
  175. {
  176. .name = "loopback",
  177. .id = GRUB_DISK_DEVICE_LOOPBACK_ID,
  178. .disk_iterate = grub_loopback_iterate,
  179. .disk_open = grub_loopback_open,
  180. .disk_read = grub_loopback_read,
  181. .disk_write = grub_loopback_write,
  182. .next = 0
  183. };
  184. static grub_extcmd_t cmd;
  185. GRUB_MOD_INIT(loopback)
  186. {
  187. cmd = grub_register_extcmd ("loopback", grub_cmd_loopback, 0,
  188. N_("[-d] DEVICENAME FILE."),
  189. /* TRANSLATORS: The file itself is not destroyed
  190. or transformed into drive. */
  191. N_("Make a virtual drive from 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. }