fs.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /* The grub_error_push() does not touch grub_errmsg. */
  70. grub_dprintf ("fs", _("error: %s.\n"), grub_errmsg);
  71. grub_dprintf ("fs", "%s detection failed.\n", p->name);
  72. grub_error_pop ();
  73. if (grub_errno != GRUB_ERR_BAD_FS
  74. && grub_errno != GRUB_ERR_OUT_OF_RANGE)
  75. return 0;
  76. grub_errno = GRUB_ERR_NONE;
  77. }
  78. /* Let's load modules automatically. */
  79. if (grub_fs_autoload_hook && count == 0)
  80. {
  81. count++;
  82. while (grub_fs_autoload_hook ())
  83. {
  84. p = grub_fs_list;
  85. (p->fs_dir) (device, "/", probe_dummy_iter, NULL);
  86. if (grub_errno == GRUB_ERR_NONE)
  87. {
  88. count--;
  89. return p;
  90. }
  91. if (grub_errno != GRUB_ERR_BAD_FS
  92. && grub_errno != GRUB_ERR_OUT_OF_RANGE)
  93. {
  94. count--;
  95. return 0;
  96. }
  97. grub_errno = GRUB_ERR_NONE;
  98. }
  99. count--;
  100. }
  101. }
  102. else if (device->net && device->net->fs)
  103. return device->net->fs;
  104. grub_error (GRUB_ERR_UNKNOWN_FS, N_("unknown filesystem"));
  105. return 0;
  106. }
  107. /* Block list support routines. */
  108. struct grub_fs_block
  109. {
  110. grub_disk_addr_t offset;
  111. grub_disk_addr_t length;
  112. };
  113. static grub_err_t
  114. grub_fs_blocklist_open (grub_file_t file, const char *name)
  115. {
  116. const char *p = name;
  117. unsigned num = 0;
  118. unsigned i;
  119. grub_disk_t disk = file->device->disk;
  120. struct grub_fs_block *blocks;
  121. grub_size_t max_sectors;
  122. /* First, count the number of blocks. */
  123. do
  124. {
  125. num++;
  126. p = grub_strchr (p, ',');
  127. if (p)
  128. p++;
  129. }
  130. while (p);
  131. /* Allocate a block list. */
  132. blocks = grub_calloc (num + 1, sizeof (struct grub_fs_block));
  133. if (! blocks)
  134. return 0;
  135. file->size = 0;
  136. max_sectors = grub_disk_from_native_sector (disk, disk->total_sectors);
  137. p = (char *) name;
  138. for (i = 0; i < num; i++)
  139. {
  140. if (*p != '+')
  141. {
  142. blocks[i].offset = grub_strtoull (p, &p, 0);
  143. if (grub_errno != GRUB_ERR_NONE || *p != '+')
  144. {
  145. grub_error (GRUB_ERR_BAD_FILENAME,
  146. N_("invalid file name `%s'"), name);
  147. goto fail;
  148. }
  149. }
  150. p++;
  151. if (*p == '\0' || *p == ',')
  152. blocks[i].length = max_sectors - blocks[i].offset;
  153. else
  154. blocks[i].length = grub_strtoul (p, &p, 0);
  155. if (grub_errno != GRUB_ERR_NONE
  156. || blocks[i].length == 0
  157. || (*p && *p != ',' && ! grub_isspace (*p)))
  158. {
  159. grub_error (GRUB_ERR_BAD_FILENAME,
  160. N_("invalid file name `%s'"), name);
  161. goto fail;
  162. }
  163. if (max_sectors < blocks[i].offset + blocks[i].length)
  164. {
  165. grub_error (GRUB_ERR_BAD_FILENAME, "beyond the total sectors");
  166. goto fail;
  167. }
  168. file->size += (blocks[i].length << GRUB_DISK_SECTOR_BITS);
  169. p++;
  170. }
  171. file->data = blocks;
  172. return GRUB_ERR_NONE;
  173. fail:
  174. grub_free (blocks);
  175. return grub_errno;
  176. }
  177. static grub_ssize_t
  178. grub_fs_blocklist_read (grub_file_t file, char *buf, grub_size_t len)
  179. {
  180. struct grub_fs_block *p;
  181. grub_disk_addr_t sector;
  182. grub_off_t offset;
  183. grub_ssize_t ret = 0;
  184. grub_disk_t disk = file->device->disk;
  185. if (len > file->size - file->offset)
  186. len = file->size - file->offset;
  187. sector = (file->offset >> GRUB_DISK_SECTOR_BITS);
  188. offset = (file->offset & (GRUB_DISK_SECTOR_SIZE - 1));
  189. disk->read_hook = file->read_hook;
  190. disk->read_hook_data = file->read_hook_data;
  191. for (p = file->data; p->length && len > 0; p++)
  192. {
  193. if (sector < p->length)
  194. {
  195. grub_size_t size;
  196. size = len;
  197. if (((size + offset + GRUB_DISK_SECTOR_SIZE - 1)
  198. >> GRUB_DISK_SECTOR_BITS) > p->length - sector)
  199. size = ((p->length - sector) << GRUB_DISK_SECTOR_BITS) - offset;
  200. if (grub_disk_read (disk, p->offset + sector, offset,
  201. size, buf) != GRUB_ERR_NONE)
  202. {
  203. ret = -1;
  204. break;
  205. }
  206. ret += size;
  207. len -= size;
  208. sector -= ((size + offset) >> GRUB_DISK_SECTOR_BITS);
  209. offset = ((size + offset) & (GRUB_DISK_SECTOR_SIZE - 1));
  210. }
  211. else
  212. sector -= p->length;
  213. }
  214. disk->read_hook = NULL;
  215. disk->read_hook_data = NULL;
  216. return ret;
  217. }
  218. struct grub_fs grub_fs_blocklist =
  219. {
  220. .name = "blocklist",
  221. .fs_dir = 0,
  222. .fs_open = grub_fs_blocklist_open,
  223. .fs_read = grub_fs_blocklist_read,
  224. .fs_close = 0,
  225. .next = 0
  226. };