fs.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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->uuid (device, &label);
  60. if (label)
  61. grub_free (label);
  62. }
  63. else
  64. #endif
  65. (p->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->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. char *p = (char *) name;
  115. unsigned num = 0;
  116. unsigned i;
  117. grub_disk_t disk = file->device->disk;
  118. struct grub_fs_block *blocks;
  119. /* First, count the number of blocks. */
  120. do
  121. {
  122. num++;
  123. p = grub_strchr (p, ',');
  124. if (p)
  125. p++;
  126. }
  127. while (p);
  128. /* Allocate a block list. */
  129. blocks = grub_zalloc (sizeof (struct grub_fs_block) * (num + 1));
  130. if (! blocks)
  131. return 0;
  132. file->size = 0;
  133. p = (char *) name;
  134. for (i = 0; i < num; i++)
  135. {
  136. if (*p != '+')
  137. {
  138. blocks[i].offset = grub_strtoull (p, &p, 0);
  139. if (grub_errno != GRUB_ERR_NONE || *p != '+')
  140. {
  141. grub_error (GRUB_ERR_BAD_FILENAME,
  142. N_("invalid file name `%s'"), name);
  143. goto fail;
  144. }
  145. }
  146. p++;
  147. blocks[i].length = grub_strtoul (p, &p, 0);
  148. if (grub_errno != GRUB_ERR_NONE
  149. || blocks[i].length == 0
  150. || (*p && *p != ',' && ! grub_isspace (*p)))
  151. {
  152. grub_error (GRUB_ERR_BAD_FILENAME,
  153. N_("invalid file name `%s'"), name);
  154. goto fail;
  155. }
  156. if (disk->total_sectors < blocks[i].offset + blocks[i].length)
  157. {
  158. grub_error (GRUB_ERR_BAD_FILENAME, "beyond the total sectors");
  159. goto fail;
  160. }
  161. file->size += (blocks[i].length << GRUB_DISK_SECTOR_BITS);
  162. p++;
  163. }
  164. file->data = blocks;
  165. return GRUB_ERR_NONE;
  166. fail:
  167. grub_free (blocks);
  168. return grub_errno;
  169. }
  170. static grub_ssize_t
  171. grub_fs_blocklist_read (grub_file_t file, char *buf, grub_size_t len)
  172. {
  173. struct grub_fs_block *p;
  174. grub_disk_addr_t sector;
  175. grub_off_t offset;
  176. grub_ssize_t ret = 0;
  177. if (len > file->size - file->offset)
  178. len = file->size - file->offset;
  179. sector = (file->offset >> GRUB_DISK_SECTOR_BITS);
  180. offset = (file->offset & (GRUB_DISK_SECTOR_SIZE - 1));
  181. for (p = file->data; p->length && len > 0; p++)
  182. {
  183. if (sector < p->length)
  184. {
  185. grub_size_t size;
  186. size = len;
  187. if (((size + offset + GRUB_DISK_SECTOR_SIZE - 1)
  188. >> GRUB_DISK_SECTOR_BITS) > p->length - sector)
  189. size = ((p->length - sector) << GRUB_DISK_SECTOR_BITS) - offset;
  190. if (grub_disk_read (file->device->disk, p->offset + sector, offset,
  191. size, buf) != GRUB_ERR_NONE)
  192. return -1;
  193. ret += size;
  194. len -= size;
  195. sector -= ((size + offset) >> GRUB_DISK_SECTOR_BITS);
  196. offset = ((size + offset) & (GRUB_DISK_SECTOR_SIZE - 1));
  197. }
  198. else
  199. sector -= p->length;
  200. }
  201. return ret;
  202. }
  203. struct grub_fs grub_fs_blocklist =
  204. {
  205. .name = "blocklist",
  206. .dir = 0,
  207. .open = grub_fs_blocklist_open,
  208. .read = grub_fs_blocklist_read,
  209. .close = 0,
  210. .next = 0
  211. };