008-09-21 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder.git] / kern / fs.c
blob4e21de2f699e364ed4e0b3c401611b26e94bd814
1 /* fs.c - filesystem manager */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2005,2007 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/disk.h>
21 #include <grub/net.h>
22 #include <grub/fs.h>
23 #include <grub/file.h>
24 #include <grub/err.h>
25 #include <grub/misc.h>
26 #include <grub/types.h>
27 #include <grub/mm.h>
28 #include <grub/term.h>
30 static grub_fs_t grub_fs_list;
32 grub_fs_autoload_hook_t grub_fs_autoload_hook = 0;
34 void
35 grub_fs_register (grub_fs_t fs)
37 fs->next = grub_fs_list;
38 grub_fs_list = fs;
41 void
42 grub_fs_unregister (grub_fs_t fs)
44 grub_fs_t *p, q;
46 for (p = &grub_fs_list, q = *p; q; p = &(q->next), q = q->next)
47 if (q == fs)
49 *p = q->next;
50 break;
54 void
55 grub_fs_iterate (int (*hook) (const grub_fs_t fs))
57 grub_fs_t p;
59 for (p = grub_fs_list; p; p = p->next)
60 if (hook (p))
61 break;
64 grub_fs_t
65 grub_fs_probe (grub_device_t device)
67 grub_fs_t p;
68 auto int dummy_func (const char *filename, int dir);
70 int dummy_func (const char *filename __attribute__ ((unused)),
71 int dir __attribute__ ((unused)))
73 return 1;
76 if (device->disk)
78 /* Make it sure not to have an infinite recursive calls. */
79 static int count = 0;
81 for (p = grub_fs_list; p; p = p->next)
83 grub_dprintf ("fs", "Detecting %s...\n", p->name);
84 (p->dir) (device, "/", dummy_func);
85 if (grub_errno == GRUB_ERR_NONE)
86 return p;
88 grub_error_push ();
89 grub_dprintf ("fs", "%s detection failed.\n", p->name);
90 grub_error_pop ();
92 if (grub_errno != GRUB_ERR_BAD_FS)
93 return 0;
95 grub_errno = GRUB_ERR_NONE;
98 /* Let's load modules automatically. */
99 if (grub_fs_autoload_hook && count == 0)
101 count++;
103 while (grub_fs_autoload_hook ())
105 p = grub_fs_list;
107 (p->dir) (device, "/", dummy_func);
108 if (grub_errno == GRUB_ERR_NONE)
110 count--;
111 return p;
114 if (grub_errno != GRUB_ERR_BAD_FS)
116 count--;
117 return 0;
120 grub_errno = GRUB_ERR_NONE;
123 count--;
126 else if (device->net->fs)
127 return device->net->fs;
129 grub_error (GRUB_ERR_UNKNOWN_FS, "unknown filesystem");
130 return 0;
135 /* Block list support routines. */
137 struct grub_fs_block
139 grub_disk_addr_t offset;
140 unsigned long length;
143 static grub_err_t
144 grub_fs_blocklist_open (grub_file_t file, const char *name)
146 char *p = (char *) name;
147 unsigned num = 0;
148 unsigned i;
149 grub_disk_t disk = file->device->disk;
150 struct grub_fs_block *blocks;
152 /* First, count the number of blocks. */
155 num++;
156 p = grub_strchr (p, ',');
157 if (p)
158 p++;
160 while (p);
162 /* Allocate a block list. */
163 blocks = grub_malloc (sizeof (struct grub_fs_block) * (num + 1));
164 if (! blocks)
165 return 0;
167 file->size = 0;
168 p = (char *) name;
169 for (i = 0; i < num; i++)
171 if (*p != '+')
173 blocks[i].offset = grub_strtoull (p, &p, 0);
174 if (grub_errno != GRUB_ERR_NONE || *p != '+')
176 grub_error (GRUB_ERR_BAD_FILENAME,
177 "invalid file name `%s'", name);
178 goto fail;
181 else
182 blocks[i].offset = 0;
184 p++;
185 blocks[i].length = grub_strtoul (p, &p, 0);
186 if (grub_errno != GRUB_ERR_NONE
187 || blocks[i].length == 0
188 || (*p && *p != ',' && ! grub_isspace (*p)))
190 grub_error (GRUB_ERR_BAD_FILENAME,
191 "invalid file name `%s'", name);
192 goto fail;
195 if (disk->total_sectors < blocks[i].offset + blocks[i].length)
197 grub_error (GRUB_ERR_BAD_FILENAME, "beyond the total sectors");
198 goto fail;
201 file->size += (blocks[i].length << GRUB_DISK_SECTOR_BITS);
202 p++;
205 blocks[i].length = 0;
206 file->data = blocks;
208 return GRUB_ERR_NONE;
210 fail:
211 grub_free (blocks);
212 return grub_errno;
215 static grub_ssize_t
216 grub_fs_blocklist_read (grub_file_t file, char *buf, grub_size_t len)
218 struct grub_fs_block *p;
219 grub_disk_addr_t sector;
220 grub_off_t offset;
221 grub_ssize_t ret = 0;
223 if (len > file->size - file->offset)
224 len = file->size - file->offset;
226 sector = (file->offset >> GRUB_DISK_SECTOR_BITS);
227 offset = (file->offset & (GRUB_DISK_SECTOR_SIZE - 1));
228 for (p = file->data; p->length && len > 0; p++)
230 if (sector < p->length)
232 grub_size_t size;
234 size = len;
235 if (((size + offset + GRUB_DISK_SECTOR_SIZE - 1)
236 >> GRUB_DISK_SECTOR_BITS) > p->length - sector)
237 size = ((p->length - sector) << GRUB_DISK_SECTOR_BITS) - offset;
239 if (grub_disk_read (file->device->disk, p->offset + sector, offset,
240 size, buf) != GRUB_ERR_NONE)
241 return -1;
243 ret += size;
244 len -= size;
245 sector -= ((size + offset) >> GRUB_DISK_SECTOR_BITS);
246 offset = ((size + offset) & (GRUB_DISK_SECTOR_SIZE - 1));
248 else
249 sector -= p->length;
252 return ret;
255 struct grub_fs grub_fs_blocklist =
257 .name = "blocklist",
258 .dir = 0,
259 .open = grub_fs_blocklist_open,
260 .read = grub_fs_blocklist_read,
261 .close = 0,
262 .next = 0