1
/* fs.c - filesystem manager */
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>
23
#include <grub/file.h>
25
#include <grub/misc.h>
26
#include <grub/types.h>
28
#include <grub/term.h>
30
grub_fs_t grub_fs_list = 0;
32
grub_fs_autoload_hook_t grub_fs_autoload_hook = 0;
35
grub_fs_probe (grub_device_t device)
38
auto int dummy_func (const char *filename,
39
const struct grub_dirhook_info *info);
41
int dummy_func (const char *filename __attribute__ ((unused)),
42
const struct grub_dirhook_info *info __attribute__ ((unused)))
49
/* Make it sure not to have an infinite recursive calls. */
52
for (p = grub_fs_list; p; p = p->next)
54
grub_dprintf ("fs", "Detecting %s...\n", p->name);
55
(p->dir) (device, "/", dummy_func);
56
if (grub_errno == GRUB_ERR_NONE)
60
grub_dprintf ("fs", "%s detection failed.\n", p->name);
63
if (grub_errno != GRUB_ERR_BAD_FS)
66
grub_errno = GRUB_ERR_NONE;
69
/* Let's load modules automatically. */
70
if (grub_fs_autoload_hook && count == 0)
74
while (grub_fs_autoload_hook ())
78
(p->dir) (device, "/", dummy_func);
79
if (grub_errno == GRUB_ERR_NONE)
85
if (grub_errno != GRUB_ERR_BAD_FS)
91
grub_errno = GRUB_ERR_NONE;
97
else if (device->net->fs)
98
return device->net->fs;
100
grub_error (GRUB_ERR_UNKNOWN_FS, "unknown filesystem");
106
/* Block list support routines. */
110
grub_disk_addr_t offset;
111
unsigned long length;
115
grub_fs_blocklist_open (grub_file_t file, const char *name)
117
char *p = (char *) name;
120
grub_disk_t disk = file->device->disk;
121
struct grub_fs_block *blocks;
123
/* First, count the number of blocks. */
127
p = grub_strchr (p, ',');
133
/* Allocate a block list. */
134
blocks = grub_zalloc (sizeof (struct grub_fs_block) * (num + 1));
140
for (i = 0; i < num; i++)
144
blocks[i].offset = grub_strtoull (p, &p, 0);
145
if (grub_errno != GRUB_ERR_NONE || *p != '+')
147
grub_error (GRUB_ERR_BAD_FILENAME,
148
"invalid file name `%s'", name);
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)))
159
grub_error (GRUB_ERR_BAD_FILENAME,
160
"invalid file name `%s'", name);
164
if (disk->total_sectors < blocks[i].offset + blocks[i].length)
166
grub_error (GRUB_ERR_BAD_FILENAME, "beyond the total sectors");
170
file->size += (blocks[i].length << GRUB_DISK_SECTOR_BITS);
176
return GRUB_ERR_NONE;
184
grub_fs_blocklist_read (grub_file_t file, char *buf, grub_size_t len)
186
struct grub_fs_block *p;
187
grub_disk_addr_t sector;
189
grub_ssize_t ret = 0;
191
if (len > file->size - file->offset)
192
len = file->size - file->offset;
194
sector = (file->offset >> GRUB_DISK_SECTOR_BITS);
195
offset = (file->offset & (GRUB_DISK_SECTOR_SIZE - 1));
196
for (p = file->data; p->length && len > 0; p++)
198
if (sector < p->length)
203
if (((size + offset + GRUB_DISK_SECTOR_SIZE - 1)
204
>> GRUB_DISK_SECTOR_BITS) > p->length - sector)
205
size = ((p->length - sector) << GRUB_DISK_SECTOR_BITS) - offset;
207
if (grub_disk_read (file->device->disk, p->offset + sector, offset,
208
size, buf) != GRUB_ERR_NONE)
213
sector -= ((size + offset) >> GRUB_DISK_SECTOR_BITS);
214
offset = ((size + offset) & (GRUB_DISK_SECTOR_SIZE - 1));
223
struct grub_fs grub_fs_blocklist =
227
.open = grub_fs_blocklist_open,
228
.read = grub_fs_blocklist_read,