1
/* sfs.c - Amiga Smart FileSystem. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2005,2006,2007,2008,2009 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/>.
21
#include <grub/file.h>
23
#include <grub/misc.h>
24
#include <grub/disk.h>
26
#include <grub/types.h>
27
#include <grub/fshelp.h>
29
/* The common header for a block. */
30
struct grub_sfs_bheader
32
grub_uint8_t magic[4];
34
grub_uint32_t ipointtomyself;
35
} __attribute__ ((packed));
37
/* The sfs rootblock. */
38
struct grub_sfs_rblock
40
struct grub_sfs_bheader header;
41
grub_uint32_t version;
42
grub_uint8_t unused1[36];
43
grub_uint32_t blocksize;
44
grub_uint8_t unused2[40];
45
grub_uint8_t unused3[8];
46
grub_uint32_t rootobject;
48
} __attribute__ ((packed));
50
/* A SFS object container. */
53
grub_uint8_t unused1[4];
55
grub_uint8_t unused2[4];
60
grub_uint32_t first_block;
62
} file __attribute__ ((packed));
65
grub_uint32_t hashtable;
66
grub_uint32_t dir_objc;
67
} dir __attribute__ ((packed));
69
grub_uint8_t unused3[4];
71
grub_uint8_t filename[1];
72
grub_uint8_t comment[1];
73
} __attribute__ ((packed));
75
#define GRUB_SFS_TYPE_DELETED 32
76
#define GRUB_SFS_TYPE_SYMLINK 64
77
#define GRUB_SFS_TYPE_DIR 128
79
/* A SFS object container. */
82
struct grub_sfs_bheader header;
86
/* The amount of objects depends on the blocksize. */
87
struct grub_sfs_obj objects[1];
88
} __attribute__ ((packed));
90
struct grub_sfs_btree_node
94
} __attribute__ ((packed));
96
struct grub_sfs_btree_extent
102
} __attribute__ ((packed));
104
struct grub_sfs_btree
106
struct grub_sfs_bheader header;
109
grub_uint8_t nodesize;
110
/* Normally this can be kind of node, but just extents are
112
struct grub_sfs_btree_node node[1];
113
} __attribute__ ((packed));
117
struct grub_fshelp_node
119
struct grub_sfs_data *data;
124
/* Information about a "mounted" sfs filesystem. */
127
struct grub_sfs_rblock rblock;
128
struct grub_fshelp_node diropen;
131
/* Blocksize in sectors. */
132
unsigned int blocksize;
134
/* Label of the filesystem. */
138
static grub_dl_t my_mod;
141
/* Lookup the extent starting with BLOCK in the filesystem described
142
by DATA. Return the extent size in SIZE and the following extent
145
grub_sfs_read_extent (struct grub_sfs_data *data, unsigned int block,
146
int *size, int *nextext)
149
struct grub_sfs_btree *tree;
153
treeblock = grub_malloc (data->blocksize);
157
next = grub_be_to_cpu32 (data->rblock.btree);
158
tree = (struct grub_sfs_btree *) treeblock;
160
/* Handle this level in the btree. */
163
grub_disk_read (data->disk, next, 0, data->blocksize, treeblock);
166
grub_free (treeblock);
170
for (i = grub_be_to_cpu16 (tree->nodes) - 1; i >= 0; i--)
173
#define EXTNODE(tree, index) \
174
((struct grub_sfs_btree_node *) (((char *) &(tree)->node[0]) \
175
+ (index) * (tree)->nodesize))
177
/* Follow the tree down to the leaf level. */
178
if ((grub_be_to_cpu32 (EXTNODE(tree, i)->key) <= block)
181
next = grub_be_to_cpu32 (EXTNODE (tree, i)->data);
185
/* If the leaf level is reached, just find the correct extent. */
186
if (grub_be_to_cpu32 (EXTNODE (tree, i)->key) == block && tree->leaf)
188
struct grub_sfs_btree_extent *extent;
189
extent = (struct grub_sfs_btree_extent *) EXTNODE (tree, i);
191
/* We found a correct leaf. */
192
*size = grub_be_to_cpu16 (extent->size);
193
*nextext = grub_be_to_cpu32 (extent->next);
195
grub_free (treeblock);
202
} while (!tree->leaf);
204
grub_free (treeblock);
206
return grub_error (GRUB_ERR_FILE_READ_ERROR, "SFS extent not found");
209
static grub_disk_addr_t
210
grub_sfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
212
int blk = node->block;
220
/* In case of the first block we don't have to lookup the
221
extent, the minimum size is always 1. */
225
err = grub_sfs_read_extent (node->data, blk, &size, &next);
229
if (fileblock < (unsigned int) size)
230
return fileblock + blk;
237
grub_error (GRUB_ERR_FILE_READ_ERROR,
238
"reading a SFS block outside the extent");
244
/* Read LEN bytes from the file described by DATA starting with byte
245
POS. Return the amount of read bytes in READ. */
247
grub_sfs_read_file (grub_fshelp_node_t node,
248
void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
249
unsigned offset, unsigned length),
250
int pos, grub_size_t len, char *buf)
252
return grub_fshelp_read_file (node->data->disk, node, read_hook,
253
pos, len, buf, grub_sfs_read_block,
258
static struct grub_sfs_data *
259
grub_sfs_mount (grub_disk_t disk)
261
struct grub_sfs_data *data;
262
struct grub_sfs_objc *rootobjc;
263
char *rootobjc_data = 0;
266
data = grub_malloc (sizeof (*data));
270
/* Read the rootblock. */
271
grub_disk_read (disk, 0, 0, sizeof (struct grub_sfs_rblock),
276
/* Make sure this is a sfs filesystem. */
277
if (grub_strncmp ((char *) (data->rblock.header.magic), "SFS", 4))
279
grub_error (GRUB_ERR_BAD_FS, "not a SFS filesystem");
283
data->blocksize = grub_be_to_cpu32 (data->rblock.blocksize);
284
rootobjc_data = grub_malloc (data->blocksize);
288
/* Read the root object container. */
289
grub_disk_read (disk, grub_be_to_cpu32 (data->rblock.rootobject), 0,
290
data->blocksize, rootobjc_data);
294
rootobjc = (struct grub_sfs_objc *) rootobjc_data;
296
blk = grub_be_to_cpu32 (rootobjc->objects[0].file_dir.dir.dir_objc);
297
data->diropen.size = 0;
298
data->diropen.block = blk;
299
data->diropen.data = data;
301
data->label = grub_strdup ((char *) (rootobjc->objects[0].filename));
306
if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
307
grub_error (GRUB_ERR_BAD_FS, "not an SFS filesystem");
310
grub_free (rootobjc_data);
316
grub_sfs_read_symlink (grub_fshelp_node_t node)
318
struct grub_sfs_data *data = node->data;
322
block = grub_malloc (data->blocksize);
326
grub_disk_read (data->disk, node->block, 0, data->blocksize, block);
333
/* This is just a wild guess, but it always worked for me. How the
334
SLNK block looks like is not documented in the SFS docs. */
335
symlink = grub_strdup (&block[24]);
344
grub_sfs_iterate_dir (grub_fshelp_node_t dir,
346
(*hook) (const char *filename,
347
enum grub_fshelp_filetype filetype,
348
grub_fshelp_node_t node))
350
struct grub_fshelp_node *node = 0;
351
struct grub_sfs_data *data = dir->data;
353
struct grub_sfs_objc *objc;
354
unsigned int next = dir->block;
357
auto int NESTED_FUNC_ATTR grub_sfs_create_node (const char *name, int block,
360
int NESTED_FUNC_ATTR grub_sfs_create_node (const char *name, int block,
363
node = grub_malloc (sizeof (*node));
371
return hook (name, type, node);
374
objc_data = grub_malloc (data->blocksize);
378
/* The Object container can consist of multiple blocks, iterate over
382
grub_disk_read (data->disk, next, 0, data->blocksize, objc_data);
386
objc = (struct grub_sfs_objc *) objc_data;
388
pos = (char *) &objc->objects[0] - (char *) objc;
390
/* Iterate over all entries in this block. */
391
while (pos + sizeof (struct grub_sfs_obj) < data->blocksize)
393
struct grub_sfs_obj *obj;
394
obj = (struct grub_sfs_obj *) ((char *) objc + pos);
395
char *filename = (char *) (obj->filename);
397
enum grub_fshelp_filetype type;
400
/* The filename and comment dynamically increase the size of
402
len = grub_strlen (filename);
403
len += grub_strlen (filename + len + 1);
405
pos += sizeof (*obj) + len;
406
/* Round up to a multiple of two bytes. */
407
pos = ((pos + 1) >> 1) << 1;
409
if (grub_strlen (filename) == 0)
412
/* First check if the file was not deleted. */
413
if (obj->type & GRUB_SFS_TYPE_DELETED)
415
else if (obj->type & GRUB_SFS_TYPE_SYMLINK)
416
type = GRUB_FSHELP_SYMLINK;
417
else if (obj->type & GRUB_SFS_TYPE_DIR)
418
type = GRUB_FSHELP_DIR;
420
type = GRUB_FSHELP_REG;
422
if (type == GRUB_FSHELP_DIR)
423
block = grub_be_to_cpu32 (obj->file_dir.dir.dir_objc);
425
block = grub_be_to_cpu32 (obj->file_dir.file.first_block);
427
if (grub_sfs_create_node (filename, block,
428
grub_be_to_cpu32 (obj->file_dir.file.size),
431
grub_free (objc_data);
436
next = grub_be_to_cpu32 (objc->next);
440
grub_free (objc_data);
445
/* Open a file named NAME and initialize FILE. */
447
grub_sfs_open (struct grub_file *file, const char *name)
449
struct grub_sfs_data *data;
450
struct grub_fshelp_node *fdiro = 0;
452
grub_dl_ref (my_mod);
454
data = grub_sfs_mount (file->device->disk);
458
grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_sfs_iterate_dir,
459
grub_sfs_read_symlink, GRUB_FSHELP_REG);
463
file->size = fdiro->size;
464
data->diropen = *fdiro;
473
if (data && fdiro != &data->diropen)
476
grub_free (data->label);
479
grub_dl_unref (my_mod);
486
grub_sfs_close (grub_file_t file)
488
grub_free (file->data);
490
grub_dl_unref (my_mod);
492
return GRUB_ERR_NONE;
496
/* Read LEN bytes data from FILE into BUF. */
498
grub_sfs_read (grub_file_t file, char *buf, grub_size_t len)
500
struct grub_sfs_data *data = (struct grub_sfs_data *) file->data;
502
int size = grub_sfs_read_file (&data->diropen, file->read_hook,
503
file->offset, len, buf);
510
grub_sfs_dir (grub_device_t device, const char *path,
511
int (*hook) (const char *filename,
512
const struct grub_dirhook_info *info))
514
struct grub_sfs_data *data = 0;
515
struct grub_fshelp_node *fdiro = 0;
517
auto int NESTED_FUNC_ATTR iterate (const char *filename,
518
enum grub_fshelp_filetype filetype,
519
grub_fshelp_node_t node);
521
int NESTED_FUNC_ATTR iterate (const char *filename,
522
enum grub_fshelp_filetype filetype,
523
grub_fshelp_node_t node)
525
struct grub_dirhook_info info;
526
grub_memset (&info, 0, sizeof (info));
527
info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
529
return hook (filename, &info);
532
grub_dl_ref (my_mod);
534
data = grub_sfs_mount (device->disk);
538
grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_sfs_iterate_dir,
539
grub_sfs_read_symlink, GRUB_FSHELP_DIR);
543
grub_sfs_iterate_dir (fdiro, iterate);
546
if (data && fdiro != &data->diropen)
549
grub_free (data->label);
552
grub_dl_unref (my_mod);
559
grub_sfs_label (grub_device_t device, char **label)
561
struct grub_sfs_data *data;
562
grub_disk_t disk = device->disk;
564
data = grub_sfs_mount (disk);
566
*label = data->label;
574
static struct grub_fs grub_sfs_fs =
578
.open = grub_sfs_open,
579
.read = grub_sfs_read,
580
.close = grub_sfs_close,
581
.label = grub_sfs_label,
587
grub_fs_register (&grub_sfs_fs);
593
grub_fs_unregister (&grub_sfs_fs);