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>
28 #include <grub/charset.h>
30 GRUB_MOD_LICENSE ("GPLv3+");
32 /* The common header for a block. */
33 struct grub_sfs_bheader
35 grub_uint8_t magic
[4];
37 grub_uint32_t ipointtomyself
;
40 /* The sfs rootblock. */
41 struct grub_sfs_rblock
43 struct grub_sfs_bheader header
;
44 grub_uint32_t version
;
45 grub_uint32_t createtime
;
47 grub_uint8_t unused1
[31];
48 grub_uint32_t blocksize
;
49 grub_uint8_t unused2
[40];
50 grub_uint8_t unused3
[8];
51 grub_uint32_t rootobject
;
57 FLAGS_CASE_SENSITIVE
= 0x80
60 /* A SFS object container. */
63 grub_uint8_t unused1
[4];
65 grub_uint8_t unused2
[4];
70 grub_uint32_t first_block
;
75 grub_uint32_t hashtable
;
76 grub_uint32_t dir_objc
;
81 grub_uint8_t filename
[1];
82 grub_uint8_t comment
[1];
85 #define GRUB_SFS_TYPE_DELETED 32
86 #define GRUB_SFS_TYPE_SYMLINK 64
87 #define GRUB_SFS_TYPE_DIR 128
89 /* A SFS object container. */
92 struct grub_sfs_bheader header
;
96 /* The amount of objects depends on the blocksize. */
97 struct grub_sfs_obj objects
[1];
100 struct grub_sfs_btree_node
106 struct grub_sfs_btree_extent
114 struct grub_sfs_btree
116 struct grub_sfs_bheader header
;
119 grub_uint8_t nodesize
;
120 /* Normally this can be kind of node, but just extents are
122 struct grub_sfs_btree_node node
[1];
133 struct grub_fshelp_node
135 struct grub_sfs_data
*data
;
139 grub_uint32_t cache_off
;
140 grub_uint32_t next_extent
;
141 grub_size_t cache_allocated
;
142 grub_size_t cache_size
;
143 struct cache_entry
*cache
;
146 /* Information about a "mounted" sfs filesystem. */
149 struct grub_sfs_rblock rblock
;
150 struct grub_fshelp_node diropen
;
153 /* Log of blocksize in sectors. */
158 /* Label of the filesystem. */
162 static grub_dl_t my_mod
;
165 /* Lookup the extent starting with BLOCK in the filesystem described
166 by DATA. Return the extent size in SIZE and the following extent
169 grub_sfs_read_extent (struct grub_sfs_data
*data
, unsigned int block
,
170 grub_uint32_t
*size
, grub_uint32_t
*nextext
)
173 struct grub_sfs_btree
*tree
;
176 grub_size_t blocksize
= GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
;
178 treeblock
= grub_malloc (blocksize
);
182 next
= grub_be_to_cpu32 (data
->rblock
.btree
);
183 tree
= (struct grub_sfs_btree
*) treeblock
;
185 /* Handle this level in the btree. */
188 grub_uint16_t nnodes
;
189 grub_disk_read (data
->disk
,
190 ((grub_disk_addr_t
) next
) << data
->log_blocksize
,
191 0, blocksize
, treeblock
);
194 grub_free (treeblock
);
198 nnodes
= grub_be_to_cpu16 (tree
->nodes
);
199 if (nnodes
* (grub_uint32_t
) (tree
)->nodesize
> blocksize
)
202 for (i
= (int) nnodes
- 1; i
>= 0; i
--)
205 #define EXTNODE(tree, index) \
206 ((struct grub_sfs_btree_node *) (((char *) &(tree)->node[0]) \
207 + (index) * (tree)->nodesize))
209 /* Follow the tree down to the leaf level. */
210 if ((grub_be_to_cpu32 (EXTNODE(tree
, i
)->key
) <= block
)
213 next
= grub_be_to_cpu32 (EXTNODE (tree
, i
)->data
);
217 /* If the leaf level is reached, just find the correct extent. */
218 if (grub_be_to_cpu32 (EXTNODE (tree
, i
)->key
) == block
&& tree
->leaf
)
220 struct grub_sfs_btree_extent
*extent
;
221 extent
= (struct grub_sfs_btree_extent
*) EXTNODE (tree
, i
);
223 /* We found a correct leaf. */
224 *size
= grub_be_to_cpu16 (extent
->size
);
225 *nextext
= grub_be_to_cpu32 (extent
->next
);
227 grub_free (treeblock
);
234 } while (!tree
->leaf
);
236 grub_free (treeblock
);
238 return grub_error (GRUB_ERR_FILE_READ_ERROR
, "SFS extent not found");
241 static grub_disk_addr_t
242 grub_sfs_read_block (grub_fshelp_node_t node
, grub_disk_addr_t fileblock
)
245 grub_uint32_t size
= 0;
246 grub_uint32_t next
= 0;
247 grub_disk_addr_t off
;
248 struct grub_sfs_data
*data
= node
->data
;
250 /* In case of the first block we don't have to lookup the
251 extent, the minimum size is always 1. */
257 grub_size_t cache_size
;
258 /* Assume half-max extents (32768 sectors). */
259 cache_size
= ((node
->size
>> (data
->log_blocksize
+ GRUB_DISK_SECTOR_BITS
266 node
->next_extent
= node
->block
;
267 node
->cache_size
= 0;
269 node
->cache
= grub_malloc (sizeof (node
->cache
[0]) * cache_size
);
273 node
->cache_allocated
= 0;
277 node
->cache_allocated
= cache_size
;
278 node
->cache
[0].off
= 0;
279 node
->cache
[0].block
= node
->block
;
283 if (fileblock
< node
->cache_off
)
287 for (lg
= 0; node
->cache_size
>> lg
; lg
++);
289 for (j
= lg
- 1; j
>= 0; j
--)
290 if ((i
| (1 << j
)) < node
->cache_size
291 && node
->cache
[(i
| (1 << j
))].off
<= fileblock
)
293 return node
->cache
[i
].block
+ fileblock
- node
->cache
[i
].off
;
296 off
= node
->cache_off
;
297 blk
= node
->next_extent
;
303 err
= grub_sfs_read_extent (node
->data
, blk
, &size
, &next
);
307 if (node
->cache
&& node
->cache_size
>= node
->cache_allocated
)
309 struct cache_entry
*e
= node
->cache
;
310 e
= grub_realloc (node
->cache
,node
->cache_allocated
* 2
315 grub_free (node
->cache
);
320 node
->cache_allocated
*= 2;
327 node
->cache_off
= off
+ size
;
328 node
->next_extent
= next
;
329 node
->cache
[node
->cache_size
].off
= off
;
330 node
->cache
[node
->cache_size
].block
= blk
;
334 if (fileblock
- off
< size
)
335 return fileblock
- off
+ blk
;
342 grub_error (GRUB_ERR_FILE_READ_ERROR
,
343 "reading a SFS block outside the extent");
349 /* Read LEN bytes from the file described by DATA starting with byte
350 POS. Return the amount of read bytes in READ. */
352 grub_sfs_read_file (grub_fshelp_node_t node
,
353 grub_disk_read_hook_t read_hook
, void *read_hook_data
,
354 grub_off_t pos
, grub_size_t len
, char *buf
)
356 return grub_fshelp_read_file (node
->data
->disk
, node
,
357 read_hook
, read_hook_data
,
358 pos
, len
, buf
, grub_sfs_read_block
,
359 node
->size
, node
->data
->log_blocksize
, 0);
363 static struct grub_sfs_data
*
364 grub_sfs_mount (grub_disk_t disk
)
366 struct grub_sfs_data
*data
;
367 struct grub_sfs_objc
*rootobjc
;
368 char *rootobjc_data
= 0;
371 data
= grub_malloc (sizeof (*data
));
375 /* Read the rootblock. */
376 grub_disk_read (disk
, 0, 0, sizeof (struct grub_sfs_rblock
),
381 /* Make sure this is a sfs filesystem. */
382 if (grub_strncmp ((char *) (data
->rblock
.header
.magic
), "SFS", 4)
383 || data
->rblock
.blocksize
== 0
384 || (data
->rblock
.blocksize
& (data
->rblock
.blocksize
- 1)) != 0
385 || (data
->rblock
.blocksize
& grub_cpu_to_be32_compile_time (0xf00001ff)))
387 grub_error (GRUB_ERR_BAD_FS
, "not a SFS filesystem");
391 for (data
->log_blocksize
= 9;
392 (1U << data
->log_blocksize
) < grub_be_to_cpu32 (data
->rblock
.blocksize
);
393 data
->log_blocksize
++);
394 data
->log_blocksize
-= GRUB_DISK_SECTOR_BITS
;
395 if (data
->rblock
.flags
& FLAGS_CASE_SENSITIVE
)
396 data
->fshelp_flags
= 0;
398 data
->fshelp_flags
= GRUB_FSHELP_CASE_INSENSITIVE
;
399 rootobjc_data
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
);
403 /* Read the root object container. */
404 grub_disk_read (disk
, ((grub_disk_addr_t
) grub_be_to_cpu32 (data
->rblock
.rootobject
))
405 << data
->log_blocksize
, 0,
406 GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
, rootobjc_data
);
410 rootobjc
= (struct grub_sfs_objc
*) rootobjc_data
;
412 blk
= grub_be_to_cpu32 (rootobjc
->objects
[0].file_dir
.dir
.dir_objc
);
413 data
->diropen
.size
= 0;
414 data
->diropen
.block
= blk
;
415 data
->diropen
.data
= data
;
416 data
->diropen
.cache
= 0;
418 data
->label
= grub_strdup ((char *) (rootobjc
->objects
[0].filename
));
420 grub_free (rootobjc_data
);
424 if (grub_errno
== GRUB_ERR_OUT_OF_RANGE
)
425 grub_error (GRUB_ERR_BAD_FS
, "not an SFS filesystem");
428 grub_free (rootobjc_data
);
434 grub_sfs_read_symlink (grub_fshelp_node_t node
)
436 struct grub_sfs_data
*data
= node
->data
;
440 block
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
);
444 grub_disk_read (data
->disk
, ((grub_disk_addr_t
) node
->block
)
445 << data
->log_blocksize
,
446 0, GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
, block
);
453 /* This is just a wild guess, but it always worked for me. How the
454 SLNK block looks like is not documented in the SFS docs. */
455 symlink
= grub_malloc (((GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
)
456 - 24) * GRUB_MAX_UTF8_PER_LATIN1
+ 1);
462 *grub_latin1_to_utf8 ((grub_uint8_t
*) symlink
, (grub_uint8_t
*) &block
[24],
463 (GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
) - 24) = '\0';
468 /* Helper for grub_sfs_iterate_dir. */
470 grub_sfs_create_node (struct grub_fshelp_node
**node
,
471 struct grub_sfs_data
*data
,
473 grub_uint32_t block
, grub_uint32_t size
, int type
,
475 grub_fshelp_iterate_dir_hook_t hook
, void *hook_data
)
477 grub_size_t len
= grub_strlen (name
);
478 grub_uint8_t
*name_u8
;
480 *node
= grub_malloc (sizeof (**node
));
483 name_u8
= grub_malloc (len
* GRUB_MAX_UTF8_PER_LATIN1
+ 1);
490 (*node
)->data
= data
;
491 (*node
)->size
= size
;
492 (*node
)->block
= block
;
493 (*node
)->mtime
= mtime
;
495 (*node
)->cache_off
= 0;
496 (*node
)->next_extent
= block
;
497 (*node
)->cache_size
= 0;
498 (*node
)->cache_allocated
= 0;
500 *grub_latin1_to_utf8 (name_u8
, (const grub_uint8_t
*) name
, len
) = '\0';
502 ret
= hook ((char *) name_u8
, type
| data
->fshelp_flags
, *node
, hook_data
);
508 grub_sfs_iterate_dir (grub_fshelp_node_t dir
,
509 grub_fshelp_iterate_dir_hook_t hook
, void *hook_data
)
511 struct grub_fshelp_node
*node
= 0;
512 struct grub_sfs_data
*data
= dir
->data
;
514 struct grub_sfs_objc
*objc
;
515 unsigned int next
= dir
->block
;
518 objc_data
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
);
522 /* The Object container can consist of multiple blocks, iterate over
526 grub_disk_read (data
->disk
, ((grub_disk_addr_t
) next
)
527 << data
->log_blocksize
, 0,
528 GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
, objc_data
);
532 objc
= (struct grub_sfs_objc
*) objc_data
;
534 pos
= (char *) &objc
->objects
[0] - (char *) objc
;
536 /* Iterate over all entries in this block. */
537 while (pos
+ sizeof (struct grub_sfs_obj
)
538 < (1U << (GRUB_DISK_SECTOR_BITS
+ data
->log_blocksize
)))
540 struct grub_sfs_obj
*obj
;
541 obj
= (struct grub_sfs_obj
*) ((char *) objc
+ pos
);
542 const char *filename
= (const char *) obj
->filename
;
544 enum grub_fshelp_filetype type
;
547 /* The filename and comment dynamically increase the size of
549 len
= grub_strlen (filename
);
550 len
+= grub_strlen (filename
+ len
+ 1);
552 pos
+= sizeof (*obj
) + len
;
553 /* Round up to a multiple of two bytes. */
554 pos
= ((pos
+ 1) >> 1) << 1;
556 if (filename
[0] == 0)
559 /* First check if the file was not deleted. */
560 if (obj
->type
& GRUB_SFS_TYPE_DELETED
)
562 else if (obj
->type
& GRUB_SFS_TYPE_SYMLINK
)
563 type
= GRUB_FSHELP_SYMLINK
;
564 else if (obj
->type
& GRUB_SFS_TYPE_DIR
)
565 type
= GRUB_FSHELP_DIR
;
567 type
= GRUB_FSHELP_REG
;
569 if (type
== GRUB_FSHELP_DIR
)
570 block
= grub_be_to_cpu32 (obj
->file_dir
.dir
.dir_objc
);
572 block
= grub_be_to_cpu32 (obj
->file_dir
.file
.first_block
);
574 if (grub_sfs_create_node (&node
, data
, filename
, block
,
575 grub_be_to_cpu32 (obj
->file_dir
.file
.size
),
576 type
, grub_be_to_cpu32 (obj
->mtime
),
579 grub_free (objc_data
);
584 next
= grub_be_to_cpu32 (objc
->next
);
588 grub_free (objc_data
);
593 /* Open a file named NAME and initialize FILE. */
595 grub_sfs_open (struct grub_file
*file
, const char *name
)
597 struct grub_sfs_data
*data
;
598 struct grub_fshelp_node
*fdiro
= 0;
600 grub_dl_ref (my_mod
);
602 data
= grub_sfs_mount (file
->device
->disk
);
606 grub_fshelp_find_file (name
, &data
->diropen
, &fdiro
, grub_sfs_iterate_dir
,
607 grub_sfs_read_symlink
, GRUB_FSHELP_REG
);
611 file
->size
= fdiro
->size
;
612 data
->diropen
= *fdiro
;
621 if (data
&& fdiro
!= &data
->diropen
)
624 grub_free (data
->label
);
627 grub_dl_unref (my_mod
);
634 grub_sfs_close (grub_file_t file
)
636 struct grub_sfs_data
*data
= (struct grub_sfs_data
*) file
->data
;
638 grub_free (data
->diropen
.cache
);
639 grub_free (data
->label
);
642 grub_dl_unref (my_mod
);
644 return GRUB_ERR_NONE
;
648 /* Read LEN bytes data from FILE into BUF. */
650 grub_sfs_read (grub_file_t file
, char *buf
, grub_size_t len
)
652 struct grub_sfs_data
*data
= (struct grub_sfs_data
*) file
->data
;
654 return grub_sfs_read_file (&data
->diropen
,
655 file
->read_hook
, file
->read_hook_data
,
656 file
->offset
, len
, buf
);
660 /* Context for grub_sfs_dir. */
661 struct grub_sfs_dir_ctx
663 grub_fs_dir_hook_t hook
;
667 /* Helper for grub_sfs_dir. */
669 grub_sfs_dir_iter (const char *filename
, enum grub_fshelp_filetype filetype
,
670 grub_fshelp_node_t node
, void *data
)
672 struct grub_sfs_dir_ctx
*ctx
= data
;
673 struct grub_dirhook_info info
;
675 grub_memset (&info
, 0, sizeof (info
));
676 info
.dir
= ((filetype
& GRUB_FSHELP_TYPE_MASK
) == GRUB_FSHELP_DIR
);
677 info
.mtime
= node
->mtime
+ 8 * 365 * 86400 + 86400 * 2;
679 grub_free (node
->cache
);
681 return ctx
->hook (filename
, &info
, ctx
->hook_data
);
685 grub_sfs_dir (grub_device_t device
, const char *path
,
686 grub_fs_dir_hook_t hook
, void *hook_data
)
688 struct grub_sfs_dir_ctx ctx
= { hook
, hook_data
};
689 struct grub_sfs_data
*data
= 0;
690 struct grub_fshelp_node
*fdiro
= 0;
692 grub_dl_ref (my_mod
);
694 data
= grub_sfs_mount (device
->disk
);
698 grub_fshelp_find_file (path
, &data
->diropen
, &fdiro
, grub_sfs_iterate_dir
,
699 grub_sfs_read_symlink
, GRUB_FSHELP_DIR
);
703 grub_sfs_iterate_dir (fdiro
, grub_sfs_dir_iter
, &ctx
);
706 if (data
&& fdiro
!= &data
->diropen
)
709 grub_free (data
->label
);
712 grub_dl_unref (my_mod
);
719 grub_sfs_label (grub_device_t device
, char **label
)
721 struct grub_sfs_data
*data
;
722 grub_disk_t disk
= device
->disk
;
724 data
= grub_sfs_mount (disk
);
727 grub_size_t len
= grub_strlen (data
->label
);
728 *label
= grub_malloc (len
* GRUB_MAX_UTF8_PER_LATIN1
+ 1);
730 *grub_latin1_to_utf8 ((grub_uint8_t
*) *label
,
731 (const grub_uint8_t
*) data
->label
,
733 grub_free (data
->label
);
741 static struct grub_fs grub_sfs_fs
=
745 .open
= grub_sfs_open
,
746 .read
= grub_sfs_read
,
747 .close
= grub_sfs_close
,
748 .label
= grub_sfs_label
,
750 .reserved_first_sector
= 0,
751 .blocklist_install
= 1,
758 grub_fs_register (&grub_sfs_fs
);
764 grub_fs_unregister (&grub_sfs_fs
);