1
/* sfs.c - Amiga Smart FileSystem. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2005 Free Software Foundation, Inc.
6
* This program 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 2 of the License, or
9
* (at your option) any later version.
11
* This program 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 this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
#include <grub/file.h>
24
#include <grub/misc.h>
25
#include <grub/disk.h>
27
#include <grub/types.h>
28
#include <grub/fshelp.h>
30
/* The common header for a block. */
31
struct grub_sfs_bheader
33
grub_uint8_t magic[4];
35
grub_uint32_t ipointtomyself;
36
} __attribute__ ((packed));
38
/* The sfs rootblock. */
39
struct grub_sfs_rblock
41
struct grub_sfs_bheader header;
42
grub_uint32_t version;
43
grub_uint8_t unused1[36];
44
grub_uint32_t blocksize;
45
grub_uint8_t unused2[40];
46
grub_uint8_t unused3[8];
47
grub_uint32_t rootobject;
49
} __attribute__ ((packed));
51
/* A SFS object container. */
54
grub_uint8_t unused1[4];
56
grub_uint8_t unused2[4];
61
grub_uint32_t first_block;
63
} file __attribute__ ((packed));
66
grub_uint32_t hashtable;
67
grub_uint32_t dir_objc;
68
} dir __attribute__ ((packed));
70
grub_uint8_t unused3[4];
72
grub_uint8_t filename[1];
73
grub_uint8_t comment[1];
74
} __attribute__ ((packed));
76
#define GRUB_SFS_TYPE_DELETED 32
77
#define GRUB_SFS_TYPE_SYMLINK 64
78
#define GRUB_SFS_TYPE_DIR 128
80
/* A SFS object container. */
83
struct grub_sfs_bheader header;
87
/* The amount of objects depends on the blocksize. */
88
struct grub_sfs_obj objects[1];
89
} __attribute__ ((packed));
91
struct grub_sfs_btree_node
95
} __attribute__ ((packed));
97
struct grub_sfs_btree_extent
103
} __attribute__ ((packed));
105
struct grub_sfs_btree
107
struct grub_sfs_bheader header;
110
grub_uint8_t nodesize;
111
/* Normally this can be kind of node, but just extents are
113
struct grub_sfs_btree_node node[1];
114
} __attribute__ ((packed));
118
struct grub_fshelp_node
120
struct grub_sfs_data *data;
125
/* Information about a "mounted" sfs filesystem. */
128
struct grub_sfs_rblock rblock;
129
struct grub_fshelp_node diropen;
132
/* Blocksize in sectors. */
133
unsigned int blocksize;
135
/* Label of the filesystem. */
140
static grub_dl_t my_mod;
144
/* Lookup the extent starting with BLOCK in the filesystem described
145
by DATA. Return the extent size in SIZE and the following extent
148
grub_sfs_read_extent (struct grub_sfs_data *data, unsigned int block,
149
int *size, int *nextext)
152
struct grub_sfs_btree *tree;
157
treeblock = grub_malloc (data->blocksize);
161
next = grub_be_to_cpu32 (data->rblock.btree);
162
tree = (struct grub_sfs_btree *) treeblock;
164
/* Handle this level in the btree. */
169
grub_disk_read (data->disk, next, 0, data->blocksize, treeblock);
172
grub_free (treeblock);
176
for (i = 0; i < tree->nodes; i++)
179
#define EXTNODE(tree, index) \
180
((struct grub_sfs_btree_node *) (((char *) &(tree)->node[0]) \
181
+ (index) * (tree)->nodesize))
183
/* Follow the tree down to the leaf level. */
184
if ((grub_be_to_cpu32 (EXTNODE(tree, i)->key) >= block)
187
next = grub_be_to_cpu32 (EXTNODE (tree, i - 1)->data);
191
/* In case the last node is reached just use that one, it is
193
if (i + 1 == tree->nodes && !tree->leaf)
195
next = grub_be_to_cpu32 (EXTNODE (tree, i)->data);
199
/* If the leaf level is reached, just find the correct extent. */
200
if (grub_be_to_cpu32 (EXTNODE (tree, i)->key) == block && tree->leaf)
202
struct grub_sfs_btree_extent *extent;
203
extent = (struct grub_sfs_btree_extent *) EXTNODE (tree, i);
205
/* We found a correct leaf. */
206
*size = grub_be_to_cpu16 (extent->size);
207
*nextext = grub_be_to_cpu32 (extent->next);
209
grub_free (treeblock);
216
} while (!tree->leaf);
218
grub_free (treeblock);
220
return grub_error (GRUB_ERR_FILE_READ_ERROR, "SFS extent not found");
224
grub_sfs_read_block (grub_fshelp_node_t node, int fileblock)
226
int blk = node->block;
234
/* In case of the first block we don't have to lookup the
235
extent, the minimum size is always 1. */
239
err = grub_sfs_read_extent (node->data, blk, &size, &next);
243
if (fileblock < size)
244
return fileblock + blk;
251
grub_error (GRUB_ERR_FILE_READ_ERROR,
252
"reading a SFS block outside the extent");
258
/* Read LEN bytes from the file described by DATA starting with byte
259
POS. Return the amount of read bytes in READ. */
261
grub_sfs_read_file (grub_fshelp_node_t node,
262
void (*read_hook) (unsigned long sector,
263
unsigned offset, unsigned length),
264
int pos, unsigned int len, char *buf)
266
return grub_fshelp_read_file (node->data->disk, node, read_hook,
267
pos, len, buf, grub_sfs_read_block,
272
static struct grub_sfs_data *
273
grub_sfs_mount (grub_disk_t disk)
275
struct grub_sfs_data *data;
276
struct grub_sfs_objc *rootobjc;
277
char *rootobjc_data = 0;
280
data = grub_malloc (sizeof (*data));
284
/* Read the rootblock. */
285
grub_disk_read (disk, 0, 0, sizeof (struct grub_sfs_rblock),
286
(char *) &data->rblock);
290
/* Make sure this is a sfs filesystem. */
291
if (grub_strncmp (data->rblock.header.magic, "SFS", 4))
293
grub_error (GRUB_ERR_BAD_FS, "not a sfs filesystem");
297
data->blocksize = grub_be_to_cpu32 (data->rblock.blocksize);
298
rootobjc_data = grub_malloc (data->blocksize);
302
/* Read the root object container. */
303
grub_disk_read (disk, grub_be_to_cpu32 (data->rblock.rootobject), 0,
304
data->blocksize, rootobjc_data);
308
rootobjc = (struct grub_sfs_objc *) rootobjc_data;
310
blk = grub_be_to_cpu32 (rootobjc->objects[0].file_dir.dir.dir_objc);
311
data->diropen.size = 0;
312
data->diropen.block = blk;
313
data->diropen.data = data;
315
data->label = grub_strdup (rootobjc->objects[0].filename);
321
grub_free (rootobjc_data);
327
grub_sfs_read_symlink (grub_fshelp_node_t node)
329
struct grub_sfs_data *data = node->data;
333
block = grub_malloc (data->blocksize);
337
grub_disk_read (data->disk, node->block, 0, data->blocksize, block);
344
/* This is just a wild guess, but it always worked for me. How the
345
SLNK block looks like is not documented in the SFS docs. */
346
symlink = grub_strdup (&block[24]);
355
grub_sfs_iterate_dir (grub_fshelp_node_t dir,
357
(*hook) (const char *filename,
358
enum grub_fshelp_filetype filetype,
359
grub_fshelp_node_t node))
361
struct grub_fshelp_node *node = 0;
362
struct grub_sfs_data *data = dir->data;
364
struct grub_sfs_objc *objc;
365
unsigned int next = dir->block;
368
auto int NESTED_FUNC_ATTR grub_sfs_create_node (const char *name, int block,
371
int NESTED_FUNC_ATTR grub_sfs_create_node (const char *name, int block,
374
node = grub_malloc (sizeof (*node));
382
return hook (name, type, node);
385
objc_data = grub_malloc (data->blocksize);
389
/* The Object container can consist of multiple blocks, iterate over
393
grub_disk_read (data->disk, next, 0, data->blocksize, objc_data);
397
objc = (struct grub_sfs_objc *) objc_data;
399
pos = (char *) &objc->objects[0] - (char *) objc;
401
/* Iterate over all entries in this block. */
402
while (pos + sizeof (struct grub_sfs_obj) < data->blocksize)
404
struct grub_sfs_obj *obj;
405
obj = (struct grub_sfs_obj *) ((char *) objc + pos);
406
char *filename = obj->filename;
408
enum grub_fshelp_filetype type;
411
/* The filename and comment dynamically increase the size of
413
len = grub_strlen (filename);
414
len += grub_strlen (filename + len + 1);
416
pos += sizeof (*obj) + len;
417
/* Round up to a multiple of two bytes. */
418
pos = ((pos + 1) >> 1) << 1;
420
if (grub_strlen (filename) == 0)
423
/* First check if the file was not deleted. */
424
if (obj->type & GRUB_SFS_TYPE_DELETED)
426
else if (obj->type & GRUB_SFS_TYPE_SYMLINK)
427
type = GRUB_FSHELP_SYMLINK;
428
else if (obj->type & GRUB_SFS_TYPE_DIR)
429
type = GRUB_FSHELP_DIR;
431
type = GRUB_FSHELP_REG;
433
if (type == GRUB_FSHELP_DIR)
434
block = grub_be_to_cpu32 (obj->file_dir.dir.dir_objc);
436
block = grub_be_to_cpu32 (obj->file_dir.file.first_block);
438
if (grub_sfs_create_node (filename, block,
439
grub_be_to_cpu32 (obj->file_dir.file.size),
442
grub_free (objc_data);
447
next = grub_be_to_cpu32 (objc->next);
451
grub_free (objc_data);
456
/* Open a file named NAME and initialize FILE. */
458
grub_sfs_open (struct grub_file *file, const char *name)
460
struct grub_sfs_data *data;
461
struct grub_fshelp_node *fdiro = 0;
464
grub_dl_ref (my_mod);
467
data = grub_sfs_mount (file->device->disk);
471
grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_sfs_iterate_dir,
472
grub_sfs_read_symlink, GRUB_FSHELP_REG);
476
file->size = fdiro->size;
477
data->diropen = *fdiro;
486
if (data && fdiro != &data->diropen)
489
grub_free (data->label);
493
grub_dl_unref (my_mod);
501
grub_sfs_close (grub_file_t file)
503
grub_free (file->data);
506
grub_dl_unref (my_mod);
509
return GRUB_ERR_NONE;
513
/* Read LEN bytes data from FILE into BUF. */
515
grub_sfs_read (grub_file_t file, char *buf, grub_ssize_t len)
517
struct grub_sfs_data *data = (struct grub_sfs_data *) file->data;
519
int size = grub_sfs_read_file (&data->diropen, file->read_hook,
520
file->offset, len, buf);
527
grub_sfs_dir (grub_device_t device, const char *path,
528
int (*hook) (const char *filename, int dir))
530
struct grub_sfs_data *data = 0;
531
struct grub_fshelp_node *fdiro = 0;
533
auto int NESTED_FUNC_ATTR iterate (const char *filename,
534
enum grub_fshelp_filetype filetype,
535
grub_fshelp_node_t node);
537
int NESTED_FUNC_ATTR iterate (const char *filename,
538
enum grub_fshelp_filetype filetype,
539
grub_fshelp_node_t node)
543
if (filetype == GRUB_FSHELP_DIR)
544
return hook (filename, 1);
546
return hook (filename, 0);
552
grub_dl_ref (my_mod);
555
data = grub_sfs_mount (device->disk);
559
grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_sfs_iterate_dir,
560
grub_sfs_read_symlink, GRUB_FSHELP_DIR);
564
grub_sfs_iterate_dir (fdiro, iterate);
567
if (data && fdiro != &data->diropen)
570
grub_free (data->label);
574
grub_dl_unref (my_mod);
582
grub_sfs_label (grub_device_t device, char **label)
584
struct grub_sfs_data *data;
585
grub_disk_t disk = device->disk;
587
data = grub_sfs_mount (disk);
589
*label = data->label;
597
static struct grub_fs grub_sfs_fs =
601
.open = grub_sfs_open,
602
.read = grub_sfs_read,
603
.close = grub_sfs_close,
604
.label = grub_sfs_label,
610
grub_fs_register (&grub_sfs_fs);
618
grub_fs_unregister (&grub_sfs_fs);