1 /* sfs.c - Amiga Smart FileSystem. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2006,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/>.
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. */
139 static grub_dl_t my_mod
;
143 /* Lookup the extent starting with BLOCK in the filesystem described
144 by DATA. Return the extent size in SIZE and the following extent
147 grub_sfs_read_extent (struct grub_sfs_data
*data
, unsigned int block
,
148 int *size
, int *nextext
)
151 struct grub_sfs_btree
*tree
;
156 treeblock
= grub_malloc (data
->blocksize
);
160 next
= grub_be_to_cpu32 (data
->rblock
.btree
);
161 tree
= (struct grub_sfs_btree
*) treeblock
;
163 /* Handle this level in the btree. */
168 grub_disk_read (data
->disk
, next
, 0, data
->blocksize
, treeblock
);
171 grub_free (treeblock
);
175 grub_uint16_t nodescount
= grub_be_to_cpu16(tree
->nodes
);
176 for (i
= 0; i
< nodescount
; 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 == nodescount
&& !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 NESTED_FUNC_ATTR (*read_hook
) (grub_disk_addr_t sector
,
263 unsigned offset
, unsigned length
),
264 int pos
, grub_size_t 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 ((char *) (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 ((char *) (rootobjc
->objects
[0].filename
));
320 if (grub_errno
== GRUB_ERR_OUT_OF_RANGE
)
321 grub_error (GRUB_ERR_BAD_FS
, "not an sfs filesystem");
324 grub_free (rootobjc_data
);
330 grub_sfs_read_symlink (grub_fshelp_node_t node
)
332 struct grub_sfs_data
*data
= node
->data
;
336 block
= grub_malloc (data
->blocksize
);
340 grub_disk_read (data
->disk
, node
->block
, 0, data
->blocksize
, block
);
347 /* This is just a wild guess, but it always worked for me. How the
348 SLNK block looks like is not documented in the SFS docs. */
349 symlink
= grub_strdup (&block
[24]);
358 grub_sfs_iterate_dir (grub_fshelp_node_t dir
,
360 (*hook
) (const char *filename
,
361 enum grub_fshelp_filetype filetype
,
362 grub_fshelp_node_t node
))
364 struct grub_fshelp_node
*node
= 0;
365 struct grub_sfs_data
*data
= dir
->data
;
367 struct grub_sfs_objc
*objc
;
368 unsigned int next
= dir
->block
;
371 auto int NESTED_FUNC_ATTR
grub_sfs_create_node (const char *name
, int block
,
374 int NESTED_FUNC_ATTR
grub_sfs_create_node (const char *name
, int block
,
377 node
= grub_malloc (sizeof (*node
));
385 return hook (name
, type
, node
);
388 objc_data
= grub_malloc (data
->blocksize
);
392 /* The Object container can consist of multiple blocks, iterate over
396 grub_disk_read (data
->disk
, next
, 0, data
->blocksize
, objc_data
);
400 objc
= (struct grub_sfs_objc
*) objc_data
;
402 pos
= (char *) &objc
->objects
[0] - (char *) objc
;
404 /* Iterate over all entries in this block. */
405 while (pos
+ sizeof (struct grub_sfs_obj
) < data
->blocksize
)
407 struct grub_sfs_obj
*obj
;
408 obj
= (struct grub_sfs_obj
*) ((char *) objc
+ pos
);
409 char *filename
= (char *) (obj
->filename
);
411 enum grub_fshelp_filetype type
;
414 /* The filename and comment dynamically increase the size of
416 len
= grub_strlen (filename
);
417 len
+= grub_strlen (filename
+ len
+ 1);
419 pos
+= sizeof (*obj
) + len
;
420 /* Round up to a multiple of two bytes. */
421 pos
= ((pos
+ 1) >> 1) << 1;
423 if (grub_strlen (filename
) == 0)
426 /* First check if the file was not deleted. */
427 if (obj
->type
& GRUB_SFS_TYPE_DELETED
)
429 else if (obj
->type
& GRUB_SFS_TYPE_SYMLINK
)
430 type
= GRUB_FSHELP_SYMLINK
;
431 else if (obj
->type
& GRUB_SFS_TYPE_DIR
)
432 type
= GRUB_FSHELP_DIR
;
434 type
= GRUB_FSHELP_REG
;
436 if (type
== GRUB_FSHELP_DIR
)
437 block
= grub_be_to_cpu32 (obj
->file_dir
.dir
.dir_objc
);
439 block
= grub_be_to_cpu32 (obj
->file_dir
.file
.first_block
);
441 if (grub_sfs_create_node (filename
, block
,
442 grub_be_to_cpu32 (obj
->file_dir
.file
.size
),
445 grub_free (objc_data
);
450 next
= grub_be_to_cpu32 (objc
->next
);
454 grub_free (objc_data
);
459 /* Open a file named NAME and initialize FILE. */
461 grub_sfs_open (struct grub_file
*file
, const char *name
)
463 struct grub_sfs_data
*data
;
464 struct grub_fshelp_node
*fdiro
= 0;
467 grub_dl_ref (my_mod
);
470 data
= grub_sfs_mount (file
->device
->disk
);
474 grub_fshelp_find_file (name
, &data
->diropen
, &fdiro
, grub_sfs_iterate_dir
,
475 grub_sfs_read_symlink
, GRUB_FSHELP_REG
);
479 file
->size
= fdiro
->size
;
480 data
->diropen
= *fdiro
;
489 if (data
&& fdiro
!= &data
->diropen
)
492 grub_free (data
->label
);
496 grub_dl_unref (my_mod
);
504 grub_sfs_close (grub_file_t file
)
506 grub_free (file
->data
);
509 grub_dl_unref (my_mod
);
512 return GRUB_ERR_NONE
;
516 /* Read LEN bytes data from FILE into BUF. */
518 grub_sfs_read (grub_file_t file
, char *buf
, grub_size_t len
)
520 struct grub_sfs_data
*data
= (struct grub_sfs_data
*) file
->data
;
522 int size
= grub_sfs_read_file (&data
->diropen
, file
->read_hook
,
523 file
->offset
, len
, buf
);
530 grub_sfs_dir (grub_device_t device
, const char *path
,
531 int (*hook
) (const char *filename
, int dir
))
533 struct grub_sfs_data
*data
= 0;
534 struct grub_fshelp_node
*fdiro
= 0;
536 auto int NESTED_FUNC_ATTR
iterate (const char *filename
,
537 enum grub_fshelp_filetype filetype
,
538 grub_fshelp_node_t node
);
540 int NESTED_FUNC_ATTR
iterate (const char *filename
,
541 enum grub_fshelp_filetype filetype
,
542 grub_fshelp_node_t node
)
546 if (filetype
== GRUB_FSHELP_DIR
)
547 return hook (filename
, 1);
549 return hook (filename
, 0);
555 grub_dl_ref (my_mod
);
558 data
= grub_sfs_mount (device
->disk
);
562 grub_fshelp_find_file (path
, &data
->diropen
, &fdiro
, grub_sfs_iterate_dir
,
563 grub_sfs_read_symlink
, GRUB_FSHELP_DIR
);
567 grub_sfs_iterate_dir (fdiro
, iterate
);
570 if (data
&& fdiro
!= &data
->diropen
)
573 grub_free (data
->label
);
577 grub_dl_unref (my_mod
);
585 grub_sfs_label (grub_device_t device
, char **label
)
587 struct grub_sfs_data
*data
;
588 grub_disk_t disk
= device
->disk
;
590 data
= grub_sfs_mount (disk
);
592 *label
= data
->label
;
600 static struct grub_fs grub_sfs_fs
=
604 .open
= grub_sfs_open
,
605 .read
= grub_sfs_read
,
606 .close
= grub_sfs_close
,
607 .label
= grub_sfs_label
,
613 grub_fs_register (&grub_sfs_fs
);
621 grub_fs_unregister (&grub_sfs_fs
);