fs.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/partition.h>
  29. GRUB_EXPORT(grub_fs_autoload_hook);
  30. GRUB_EXPORT(grub_fs_list);
  31. GRUB_EXPORT(grub_fs_probe);
  32. GRUB_EXPORT(grub_blocklist_convert);
  33. GRUB_EXPORT(grub_blocklist_write);
  34. grub_fs_t grub_fs_list;
  35. grub_fs_autoload_hook_t grub_fs_autoload_hook = 0;
  36. static int
  37. dummy_func (const char *filename __attribute__ ((unused)),
  38. const struct grub_dirhook_info *info __attribute__ ((unused)),
  39. void *closure __attribute__ ((unused)))
  40. {
  41. return 1;
  42. }
  43. grub_fs_t
  44. grub_fs_probe (grub_device_t device)
  45. {
  46. grub_fs_t p;
  47. if (device->disk)
  48. {
  49. /* Make it sure not to have an infinite recursive calls. */
  50. static int count = 0;
  51. for (p = grub_fs_list; p; p = p->next)
  52. {
  53. grub_dprintf ("fs", "Detecting %s...\n", p->name);
  54. (p->dir) (device, "/", dummy_func, 0);
  55. if (grub_errno == GRUB_ERR_NONE)
  56. return p;
  57. grub_error_push ();
  58. grub_dprintf ("fs", "%s detection failed.\n", p->name);
  59. grub_error_pop ();
  60. if (grub_errno != GRUB_ERR_BAD_FS)
  61. return 0;
  62. grub_errno = GRUB_ERR_NONE;
  63. }
  64. /* Let's load modules automatically. */
  65. if (grub_fs_autoload_hook && count == 0)
  66. {
  67. count++;
  68. while (grub_fs_autoload_hook ())
  69. {
  70. p = grub_fs_list;
  71. (p->dir) (device, "/", dummy_func, 0);
  72. if (grub_errno == GRUB_ERR_NONE)
  73. {
  74. count--;
  75. return p;
  76. }
  77. if (grub_errno != GRUB_ERR_BAD_FS)
  78. {
  79. count--;
  80. return 0;
  81. }
  82. grub_errno = GRUB_ERR_NONE;
  83. }
  84. count--;
  85. }
  86. }
  87. else if (device->net->fs)
  88. return device->net->fs;
  89. grub_error (GRUB_ERR_UNKNOWN_FS, "unknown filesystem");
  90. return 0;
  91. }
  92. /* Block list support routines. */
  93. struct grub_fs_block
  94. {
  95. grub_disk_addr_t offset;
  96. unsigned long length;
  97. };
  98. static grub_err_t
  99. grub_fs_blocklist_open (grub_file_t file, const char *name)
  100. {
  101. char *p = (char *) name;
  102. unsigned num = 0;
  103. unsigned i;
  104. grub_disk_t disk = file->device->disk;
  105. struct grub_fs_block *blocks;
  106. /* First, count the number of blocks. */
  107. do
  108. {
  109. num++;
  110. p = grub_strchr (p, ',');
  111. if (p)
  112. p++;
  113. }
  114. while (p);
  115. /* Allocate a block list. */
  116. blocks = grub_zalloc (sizeof (struct grub_fs_block) * (num + 1));
  117. if (! blocks)
  118. return 0;
  119. file->size = 0;
  120. p = (char *) name;
  121. if (! *p)
  122. {
  123. blocks[0].offset = 0;
  124. blocks[0].length = (disk->total_sectors == GRUB_ULONG_MAX) ?
  125. GRUB_ULONG_MAX : (disk->total_sectors << 9);
  126. file->size = blocks[0].length;
  127. }
  128. else for (i = 0; i < num; i++)
  129. {
  130. if (*p != '+')
  131. {
  132. blocks[i].offset = grub_strtoull (p, &p, 0);
  133. if (grub_errno != GRUB_ERR_NONE || *p != '+')
  134. {
  135. grub_error (GRUB_ERR_BAD_FILENAME,
  136. "invalid file name `%s'", name);
  137. goto fail;
  138. }
  139. }
  140. p++;
  141. blocks[i].length = grub_strtoul (p, &p, 0);
  142. if (grub_errno != GRUB_ERR_NONE
  143. || blocks[i].length == 0
  144. || (*p && *p != ',' && ! grub_isspace (*p)))
  145. {
  146. grub_error (GRUB_ERR_BAD_FILENAME,
  147. "invalid file name `%s'", name);
  148. goto fail;
  149. }
  150. if (disk->total_sectors < blocks[i].offset + blocks[i].length)
  151. {
  152. grub_error (GRUB_ERR_BAD_FILENAME, "beyond the total sectors");
  153. goto fail;
  154. }
  155. blocks[i].offset <<= GRUB_DISK_SECTOR_BITS;
  156. blocks[i].length <<= GRUB_DISK_SECTOR_BITS;
  157. file->size += blocks[i].length;
  158. p++;
  159. }
  160. file->data = blocks;
  161. return GRUB_ERR_NONE;
  162. fail:
  163. grub_free (blocks);
  164. return grub_errno;
  165. }
  166. static grub_err_t
  167. grub_fs_blocklist_close (grub_file_t file)
  168. {
  169. grub_free (file->data);
  170. return grub_errno;
  171. }
  172. static grub_ssize_t
  173. grub_fs_blocklist_rw (int write, grub_file_t file, char *buf, grub_size_t len)
  174. {
  175. struct grub_fs_block *p;
  176. grub_off_t offset;
  177. grub_ssize_t ret = 0;
  178. if (len > file->size - file->offset)
  179. len = file->size - file->offset;
  180. offset = file->offset;
  181. for (p = file->data; p->length && len > 0; p++)
  182. {
  183. if (offset < p->length)
  184. {
  185. grub_size_t size;
  186. size = len;
  187. if (offset + size > p->length)
  188. size = p->length - offset;
  189. if ((write) ?
  190. grub_disk_write (file->device->disk, 0, p->offset + offset,
  191. size, buf) :
  192. grub_disk_read_ex (file->device->disk, 0, p->offset + offset,
  193. size, buf, file->flags) != GRUB_ERR_NONE)
  194. return -1;
  195. ret += size;
  196. len -= size;
  197. if (buf)
  198. buf += size;
  199. offset += size;
  200. }
  201. offset -= p->length;
  202. }
  203. return ret;
  204. }
  205. static grub_ssize_t
  206. grub_fs_blocklist_read (grub_file_t file, char *buf, grub_size_t len)
  207. {
  208. grub_ssize_t ret;
  209. file->device->disk->read_hook = file->read_hook;
  210. file->device->disk->closure = file->closure;
  211. ret = grub_fs_blocklist_rw (0, file, buf, len);
  212. file->device->disk->read_hook = 0;
  213. return ret;
  214. }
  215. grub_ssize_t
  216. grub_blocklist_write (grub_file_t file, const char *buf, grub_size_t len)
  217. {
  218. return (file->fs != &grub_fs_blocklist) ? -1 :
  219. grub_fs_blocklist_rw (1, file, (char *) buf, len);
  220. }
  221. #define BLOCKLIST_INC_STEP 8
  222. struct read_blocklist_closure
  223. {
  224. int num;
  225. struct grub_fs_block *blocks;
  226. grub_off_t total_size;
  227. grub_disk_addr_t part_start;
  228. };
  229. static void
  230. read_blocklist (grub_disk_addr_t sector, unsigned offset,
  231. unsigned length, void *closure)
  232. {
  233. struct read_blocklist_closure *c = closure;
  234. sector = ((sector - c->part_start) << GRUB_DISK_SECTOR_BITS) + offset;
  235. if ((c->num) &&
  236. (c->blocks[c->num - 1].offset + c->blocks[c->num - 1].length == sector))
  237. {
  238. c->blocks[c->num - 1].length += length;
  239. goto quit;
  240. }
  241. if ((c->num & (BLOCKLIST_INC_STEP - 1)) == 0)
  242. {
  243. c->blocks = grub_realloc (c->blocks, (c->num + BLOCKLIST_INC_STEP) *
  244. sizeof (struct grub_fs_block));
  245. if (! c->blocks)
  246. return;
  247. }
  248. c->blocks[c->num].offset = sector;
  249. c->blocks[c->num].length = length;
  250. c->num++;
  251. quit:
  252. c->total_size += length;
  253. }
  254. void
  255. grub_blocklist_convert (grub_file_t file)
  256. {
  257. struct read_blocklist_closure c;
  258. if ((file->fs == &grub_fs_blocklist) || (! file->device->disk) ||
  259. (! file->size))
  260. return;
  261. file->offset = 0;
  262. file->flags = 1;
  263. c.num = 0;
  264. c.blocks = 0;
  265. c.total_size = 0;
  266. c.part_start = grub_partition_get_start (file->device->disk->partition);
  267. file->read_hook = read_blocklist;
  268. file->closure = &c;
  269. grub_file_read (file, 0, file->size);
  270. file->read_hook = 0;
  271. if ((grub_errno) || (c.total_size != file->size))
  272. {
  273. grub_errno = 0;
  274. grub_free (c.blocks);
  275. }
  276. else
  277. {
  278. if (file->fs->close)
  279. (file->fs->close) (file);
  280. file->fs = &grub_fs_blocklist;
  281. file->data = c.blocks;
  282. }
  283. file->offset = 0;
  284. }
  285. struct grub_fs grub_fs_blocklist =
  286. {
  287. .name = "blocklist",
  288. .dir = 0,
  289. .open = grub_fs_blocklist_open,
  290. .read = grub_fs_blocklist_read,
  291. .close = grub_fs_blocklist_close,
  292. .next = 0
  293. };