fs.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* fs.c - filesystem manager */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2005,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/disk.h>
  20. #include <grub/net.h>
  21. #include <grub/fs.h>
  22. #include <grub/file.h>
  23. #include <grub/err.h>
  24. #include <grub/misc.h>
  25. #include <grub/types.h>
  26. #include <grub/mm.h>
  27. #include <grub/term.h>
  28. #include <grub/i18n.h>
  29. grub_fs_t grub_fs_list = 0;
  30. grub_fs_autoload_hook_t grub_fs_autoload_hook = 0;
  31. /* Helper for grub_fs_probe. */
  32. static int
  33. probe_dummy_iter (const char *filename __attribute__ ((unused)),
  34. const struct grub_dirhook_info *info __attribute__ ((unused)),
  35. void *data __attribute__ ((unused)))
  36. {
  37. return 1;
  38. }
  39. grub_fs_t
  40. grub_fs_probe (grub_device_t device)
  41. {
  42. grub_fs_t p;
  43. if (device->disk)
  44. {
  45. /* Make it sure not to have an infinite recursive calls. */
  46. static int count = 0;
  47. for (p = grub_fs_list; p; p = p->next)
  48. {
  49. grub_dprintf ("fs", "Detecting %s...\n", p->name);
  50. /* This is evil: newly-created just mounted BtrFS after copying all
  51. GRUB files has a very peculiar unrecoverable corruption which
  52. will be fixed at sync but we'd rather not do a global sync and
  53. syncing just files doesn't seem to help. Relax the check for
  54. this time. */
  55. #ifdef GRUB_UTIL
  56. if (grub_strcmp (p->name, "btrfs") == 0)
  57. {
  58. char *label = 0;
  59. p->fs_uuid (device, &label);
  60. if (label)
  61. grub_free (label);
  62. }
  63. else
  64. #endif
  65. (p->fs_dir) (device, "/", probe_dummy_iter, NULL);
  66. if (grub_errno == GRUB_ERR_NONE)
  67. return p;
  68. grub_error_push ();
  69. grub_dprintf ("fs", "%s detection failed.\n", p->name);
  70. grub_error_pop ();
  71. if (grub_errno != GRUB_ERR_BAD_FS
  72. && grub_errno != GRUB_ERR_OUT_OF_RANGE)
  73. return 0;
  74. grub_errno = GRUB_ERR_NONE;
  75. }
  76. /* Let's load modules automatically. */
  77. if (grub_fs_autoload_hook && count == 0)
  78. {
  79. count++;
  80. while (grub_fs_autoload_hook ())
  81. {
  82. p = grub_fs_list;
  83. (p->fs_dir) (device, "/", probe_dummy_iter, NULL);
  84. if (grub_errno == GRUB_ERR_NONE)
  85. {
  86. count--;
  87. return p;
  88. }
  89. if (grub_errno != GRUB_ERR_BAD_FS
  90. && grub_errno != GRUB_ERR_OUT_OF_RANGE)
  91. {
  92. count--;
  93. return 0;
  94. }
  95. grub_errno = GRUB_ERR_NONE;
  96. }
  97. count--;
  98. }
  99. }
  100. else if (device->net && device->net->fs)
  101. return device->net->fs;
  102. grub_error (GRUB_ERR_UNKNOWN_FS, N_("unknown filesystem"));
  103. return 0;
  104. }
  105. /* Block list support routines. */
  106. struct grub_fs_block
  107. {
  108. grub_disk_addr_t offset;
  109. unsigned long length;
  110. };
  111. static grub_err_t
  112. grub_fs_blocklist_open (grub_file_t file, const char *name)
  113. {
  114. const char *p = name;
  115. unsigned num = 0;
  116. unsigned i;
  117. grub_disk_t disk = file->device->disk;
  118. struct grub_fs_block *blocks;
  119. grub_size_t max_sectors;
  120. /* First, count the number of blocks. */
  121. do
  122. {
  123. num++;
  124. p = grub_strchr (p, ',');
  125. if (p)
  126. p++;
  127. }
  128. while (p);
  129. /* Allocate a block list. */
  130. blocks = grub_calloc (num + 1, sizeof (struct grub_fs_block));
  131. if (! blocks)
  132. return 0;
  133. file->size = 0;
  134. max_sectors = grub_disk_from_native_sector (disk, disk->total_sectors);
  135. p = (char *) name;
  136. for (i = 0; i < num; i++)
  137. {
  138. if (*p != '+')
  139. {
  140. blocks[i].offset = grub_strtoull (p, &p, 0);
  141. if (grub_errno != GRUB_ERR_NONE || *p != '+')
  142. {
  143. grub_error (GRUB_ERR_BAD_FILENAME,
  144. N_("invalid file name `%s'"), name);
  145. goto fail;
  146. }
  147. }
  148. p++;
  149. blocks[i].length = grub_strtoul (p, &p, 0);
  150. if (grub_errno != GRUB_ERR_NONE
  151. || blocks[i].length == 0
  152. || (*p && *p != ',' && ! grub_isspace (*p)))
  153. {
  154. grub_error (GRUB_ERR_BAD_FILENAME,
  155. N_("invalid file name `%s'"), name);
  156. goto fail;
  157. }
  158. if (max_sectors < blocks[i].offset + blocks[i].length)
  159. {
  160. grub_error (GRUB_ERR_BAD_FILENAME, "beyond the total sectors");
  161. goto fail;
  162. }
  163. file->size += (blocks[i].length << GRUB_DISK_SECTOR_BITS);
  164. p++;
  165. }
  166. file->data = blocks;
  167. return GRUB_ERR_NONE;
  168. fail:
  169. grub_free (blocks);
  170. return grub_errno;
  171. }
  172. static grub_ssize_t
  173. grub_fs_blocklist_read (grub_file_t file, char *buf, grub_size_t len)
  174. {
  175. struct grub_fs_block *p;
  176. grub_disk_addr_t sector;
  177. grub_off_t offset;
  178. grub_ssize_t ret = 0;
  179. if (len > file->size - file->offset)
  180. len = file->size - file->offset;
  181. sector = (file->offset >> GRUB_DISK_SECTOR_BITS);
  182. offset = (file->offset & (GRUB_DISK_SECTOR_SIZE - 1));
  183. for (p = file->data; p->length && len > 0; p++)
  184. {
  185. if (sector < p->length)
  186. {
  187. grub_size_t size;
  188. size = len;
  189. if (((size + offset + GRUB_DISK_SECTOR_SIZE - 1)
  190. >> GRUB_DISK_SECTOR_BITS) > p->length - sector)
  191. size = ((p->length - sector) << GRUB_DISK_SECTOR_BITS) - offset;
  192. if (grub_disk_read (file->device->disk, p->offset + sector, offset,
  193. size, buf) != GRUB_ERR_NONE)
  194. return -1;
  195. ret += size;
  196. len -= size;
  197. sector -= ((size + offset) >> GRUB_DISK_SECTOR_BITS);
  198. offset = ((size + offset) & (GRUB_DISK_SECTOR_SIZE - 1));
  199. }
  200. else
  201. sector -= p->length;
  202. }
  203. return ret;
  204. }
  205. struct grub_fs grub_fs_blocklist =
  206. {
  207. .name = "blocklist",
  208. .fs_dir = 0,
  209. .fs_open = grub_fs_blocklist_open,
  210. .fs_read = grub_fs_blocklist_read,
  211. .fs_close = 0,
  212. .next = 0
  213. };