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
;
38 } __attribute__ ((packed
));
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
;
53 } __attribute__ ((packed
));
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
;
72 } file
__attribute__ ((packed
));
75 grub_uint32_t hashtable
;
76 grub_uint32_t dir_objc
;
77 } dir
__attribute__ ((packed
));
81 grub_uint8_t filename
[1];
82 grub_uint8_t comment
[1];
83 } __attribute__ ((packed
));
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];
98 } __attribute__ ((packed
));
100 struct grub_sfs_btree_node
104 } __attribute__ ((packed
));
106 struct grub_sfs_btree_extent
112 } __attribute__ ((packed
));
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];
123 } __attribute__ ((packed
));
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
;
177 treeblock
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
);
181 next
= grub_be_to_cpu32 (data
->rblock
.btree
);
182 tree
= (struct grub_sfs_btree
*) treeblock
;
184 /* Handle this level in the btree. */
187 grub_disk_read (data
->disk
,
188 ((grub_disk_addr_t
) next
) << data
->log_blocksize
,
189 0, GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
,
193 grub_free (treeblock
);
197 for (i
= grub_be_to_cpu16 (tree
->nodes
) - 1; i
>= 0; i
--)
200 #define EXTNODE(tree, index) \
201 ((struct grub_sfs_btree_node *) (((char *) &(tree)->node[0]) \
202 + (index) * (tree)->nodesize))
204 /* Follow the tree down to the leaf level. */
205 if ((grub_be_to_cpu32 (EXTNODE(tree
, i
)->key
) <= block
)
208 next
= grub_be_to_cpu32 (EXTNODE (tree
, i
)->data
);
212 /* If the leaf level is reached, just find the correct extent. */
213 if (grub_be_to_cpu32 (EXTNODE (tree
, i
)->key
) == block
&& tree
->leaf
)
215 struct grub_sfs_btree_extent
*extent
;
216 extent
= (struct grub_sfs_btree_extent
*) EXTNODE (tree
, i
);
218 /* We found a correct leaf. */
219 *size
= grub_be_to_cpu16 (extent
->size
);
220 *nextext
= grub_be_to_cpu32 (extent
->next
);
222 grub_free (treeblock
);
229 } while (!tree
->leaf
);
231 grub_free (treeblock
);
233 return grub_error (GRUB_ERR_FILE_READ_ERROR
, "SFS extent not found");
236 static grub_disk_addr_t
237 grub_sfs_read_block (grub_fshelp_node_t node
, grub_disk_addr_t fileblock
)
240 grub_uint32_t size
= 0;
241 grub_uint32_t next
= 0;
242 grub_disk_addr_t off
;
243 struct grub_sfs_data
*data
= node
->data
;
245 /* In case of the first block we don't have to lookup the
246 extent, the minimum size is always 1. */
252 grub_size_t cache_size
;
253 /* Assume half-max extents (32768 sectors). */
254 cache_size
= ((node
->size
>> (data
->log_blocksize
+ GRUB_DISK_SECTOR_BITS
261 node
->next_extent
= node
->block
;
262 node
->cache_size
= 0;
264 node
->cache
= grub_malloc (sizeof (node
->cache
[0]) * cache_size
);
268 node
->cache_allocated
= 0;
272 node
->cache_allocated
= cache_size
;
273 node
->cache
[0].off
= 0;
274 node
->cache
[0].block
= node
->block
;
278 if (fileblock
< node
->cache_off
)
282 for (lg
= 0; node
->cache_size
>> lg
; lg
++);
284 for (j
= lg
- 1; j
>= 0; j
--)
285 if ((i
| (1 << j
)) < node
->cache_size
286 && node
->cache
[(i
| (1 << j
))].off
<= fileblock
)
288 return node
->cache
[i
].block
+ fileblock
- node
->cache
[i
].off
;
291 off
= node
->cache_off
;
292 blk
= node
->next_extent
;
298 err
= grub_sfs_read_extent (node
->data
, blk
, &size
, &next
);
302 if (node
->cache
&& node
->cache_size
>= node
->cache_allocated
)
304 struct cache_entry
*e
= node
->cache
;
305 e
= grub_realloc (node
->cache
,node
->cache_allocated
* 2
310 grub_free (node
->cache
);
315 node
->cache_allocated
*= 2;
322 node
->cache_off
= off
+ size
;
323 node
->next_extent
= next
;
324 node
->cache
[node
->cache_size
].off
= off
;
325 node
->cache
[node
->cache_size
].block
= blk
;
329 if (fileblock
- off
< size
)
330 return fileblock
- off
+ blk
;
337 grub_error (GRUB_ERR_FILE_READ_ERROR
,
338 "reading a SFS block outside the extent");
344 /* Read LEN bytes from the file described by DATA starting with byte
345 POS. Return the amount of read bytes in READ. */
347 grub_sfs_read_file (grub_fshelp_node_t node
,
348 void NESTED_FUNC_ATTR (*read_hook
) (grub_disk_addr_t sector
,
349 unsigned offset
, unsigned length
),
350 grub_off_t pos
, grub_size_t len
, char *buf
)
352 return grub_fshelp_read_file (node
->data
->disk
, node
, read_hook
,
353 pos
, len
, buf
, grub_sfs_read_block
,
354 node
->size
, node
->data
->log_blocksize
, 0);
358 static struct grub_sfs_data
*
359 grub_sfs_mount (grub_disk_t disk
)
361 struct grub_sfs_data
*data
;
362 struct grub_sfs_objc
*rootobjc
;
363 char *rootobjc_data
= 0;
366 data
= grub_malloc (sizeof (*data
));
370 /* Read the rootblock. */
371 grub_disk_read (disk
, 0, 0, sizeof (struct grub_sfs_rblock
),
376 /* Make sure this is a sfs filesystem. */
377 if (grub_strncmp ((char *) (data
->rblock
.header
.magic
), "SFS", 4)
378 || data
->rblock
.blocksize
== 0
379 || (data
->rblock
.blocksize
& (data
->rblock
.blocksize
- 1)) != 0
380 || (data
->rblock
.blocksize
& grub_cpu_to_be32_compile_time (0xf00001ff)))
382 grub_error (GRUB_ERR_BAD_FS
, "not a SFS filesystem");
386 for (data
->log_blocksize
= 9;
387 (1U << data
->log_blocksize
) < grub_be_to_cpu32 (data
->rblock
.blocksize
);
388 data
->log_blocksize
++);
389 data
->log_blocksize
-= GRUB_DISK_SECTOR_BITS
;
390 if (data
->rblock
.flags
& FLAGS_CASE_SENSITIVE
)
391 data
->fshelp_flags
= 0;
393 data
->fshelp_flags
= GRUB_FSHELP_CASE_INSENSITIVE
;
394 rootobjc_data
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
);
398 /* Read the root object container. */
399 grub_disk_read (disk
, ((grub_disk_addr_t
) grub_be_to_cpu32 (data
->rblock
.rootobject
))
400 << data
->log_blocksize
, 0,
401 GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
, rootobjc_data
);
405 rootobjc
= (struct grub_sfs_objc
*) rootobjc_data
;
407 blk
= grub_be_to_cpu32 (rootobjc
->objects
[0].file_dir
.dir
.dir_objc
);
408 data
->diropen
.size
= 0;
409 data
->diropen
.block
= blk
;
410 data
->diropen
.data
= data
;
411 data
->diropen
.cache
= 0;
413 data
->label
= grub_strdup ((char *) (rootobjc
->objects
[0].filename
));
415 grub_free (rootobjc_data
);
419 if (grub_errno
== GRUB_ERR_OUT_OF_RANGE
)
420 grub_error (GRUB_ERR_BAD_FS
, "not an SFS filesystem");
423 grub_free (rootobjc_data
);
429 grub_sfs_read_symlink (grub_fshelp_node_t node
)
431 struct grub_sfs_data
*data
= node
->data
;
435 block
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
);
439 grub_disk_read (data
->disk
, ((grub_disk_addr_t
) node
->block
)
440 << data
->log_blocksize
,
441 0, GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
, block
);
448 /* This is just a wild guess, but it always worked for me. How the
449 SLNK block looks like is not documented in the SFS docs. */
450 symlink
= grub_malloc (((GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
)
451 - 24) * GRUB_MAX_UTF8_PER_LATIN1
+ 1);
457 *grub_latin1_to_utf8 ((grub_uint8_t
*) symlink
, (grub_uint8_t
*) &block
[24],
458 (GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
) - 24) = '\0';
464 grub_sfs_iterate_dir (grub_fshelp_node_t dir
,
466 (*hook
) (const char *filename
,
467 enum grub_fshelp_filetype filetype
,
468 grub_fshelp_node_t node
))
470 struct grub_fshelp_node
*node
= 0;
471 struct grub_sfs_data
*data
= dir
->data
;
473 struct grub_sfs_objc
*objc
;
474 unsigned int next
= dir
->block
;
477 auto int NESTED_FUNC_ATTR
grub_sfs_create_node (const char *name
,
479 grub_uint32_t size
, int type
,
480 grub_uint32_t mtime
);
482 int NESTED_FUNC_ATTR
grub_sfs_create_node (const char *name
,
484 grub_uint32_t size
, int type
,
487 grub_size_t len
= grub_strlen (name
);
488 grub_uint8_t
*name_u8
;
490 node
= grub_malloc (sizeof (*node
));
493 name_u8
= grub_malloc (len
* GRUB_MAX_UTF8_PER_LATIN1
+ 1);
506 node
->next_extent
= block
;
507 node
->cache_size
= 0;
508 node
->cache_allocated
= 0;
510 *grub_latin1_to_utf8 (name_u8
, (const grub_uint8_t
*) name
, len
) = '\0';
512 ret
= hook ((char *) name_u8
, type
| data
->fshelp_flags
, node
);
517 objc_data
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
);
521 /* The Object container can consist of multiple blocks, iterate over
525 grub_disk_read (data
->disk
, ((grub_disk_addr_t
) next
)
526 << data
->log_blocksize
, 0,
527 GRUB_DISK_SECTOR_SIZE
<< data
->log_blocksize
, objc_data
);
531 objc
= (struct grub_sfs_objc
*) objc_data
;
533 pos
= (char *) &objc
->objects
[0] - (char *) objc
;
535 /* Iterate over all entries in this block. */
536 while (pos
+ sizeof (struct grub_sfs_obj
)
537 < (1U << (GRUB_DISK_SECTOR_BITS
+ data
->log_blocksize
)))
539 struct grub_sfs_obj
*obj
;
540 obj
= (struct grub_sfs_obj
*) ((char *) objc
+ pos
);
541 const char *filename
= (const char *) obj
->filename
;
543 enum grub_fshelp_filetype type
;
546 /* The filename and comment dynamically increase the size of
548 len
= grub_strlen (filename
);
549 len
+= grub_strlen (filename
+ len
+ 1);
551 pos
+= sizeof (*obj
) + len
;
552 /* Round up to a multiple of two bytes. */
553 pos
= ((pos
+ 1) >> 1) << 1;
555 if (filename
[0] == 0)
558 /* First check if the file was not deleted. */
559 if (obj
->type
& GRUB_SFS_TYPE_DELETED
)
561 else if (obj
->type
& GRUB_SFS_TYPE_SYMLINK
)
562 type
= GRUB_FSHELP_SYMLINK
;
563 else if (obj
->type
& GRUB_SFS_TYPE_DIR
)
564 type
= GRUB_FSHELP_DIR
;
566 type
= GRUB_FSHELP_REG
;
568 if (type
== GRUB_FSHELP_DIR
)
569 block
= grub_be_to_cpu32 (obj
->file_dir
.dir
.dir_objc
);
571 block
= grub_be_to_cpu32 (obj
->file_dir
.file
.first_block
);
573 if (grub_sfs_create_node (filename
, block
,
574 grub_be_to_cpu32 (obj
->file_dir
.file
.size
),
575 type
, grub_be_to_cpu32 (obj
->mtime
)))
577 grub_free (objc_data
);
582 next
= grub_be_to_cpu32 (objc
->next
);
586 grub_free (objc_data
);
591 /* Open a file named NAME and initialize FILE. */
593 grub_sfs_open (struct grub_file
*file
, const char *name
)
595 struct grub_sfs_data
*data
;
596 struct grub_fshelp_node
*fdiro
= 0;
598 grub_dl_ref (my_mod
);
600 data
= grub_sfs_mount (file
->device
->disk
);
604 grub_fshelp_find_file (name
, &data
->diropen
, &fdiro
, grub_sfs_iterate_dir
,
605 grub_sfs_read_symlink
, GRUB_FSHELP_REG
);
609 file
->size
= fdiro
->size
;
610 data
->diropen
= *fdiro
;
619 if (data
&& fdiro
!= &data
->diropen
)
622 grub_free (data
->label
);
625 grub_dl_unref (my_mod
);
632 grub_sfs_close (grub_file_t file
)
634 struct grub_sfs_data
*data
= (struct grub_sfs_data
*) file
->data
;
636 grub_free (data
->diropen
.cache
);
637 grub_free (data
->label
);
640 grub_dl_unref (my_mod
);
642 return GRUB_ERR_NONE
;
646 /* Read LEN bytes data from FILE into BUF. */
648 grub_sfs_read (grub_file_t file
, char *buf
, grub_size_t len
)
650 struct grub_sfs_data
*data
= (struct grub_sfs_data
*) file
->data
;
652 return grub_sfs_read_file (&data
->diropen
, file
->read_hook
,
653 file
->offset
, len
, buf
);
658 grub_sfs_dir (grub_device_t device
, const char *path
,
659 int (*hook
) (const char *filename
,
660 const struct grub_dirhook_info
*info
))
662 struct grub_sfs_data
*data
= 0;
663 struct grub_fshelp_node
*fdiro
= 0;
665 auto int NESTED_FUNC_ATTR
iterate (const char *filename
,
666 enum grub_fshelp_filetype filetype
,
667 grub_fshelp_node_t node
);
669 int NESTED_FUNC_ATTR
iterate (const char *filename
,
670 enum grub_fshelp_filetype filetype
,
671 grub_fshelp_node_t node
)
673 struct grub_dirhook_info info
;
674 grub_memset (&info
, 0, sizeof (info
));
675 info
.dir
= ((filetype
& GRUB_FSHELP_TYPE_MASK
) == GRUB_FSHELP_DIR
);
676 info
.mtime
= node
->mtime
+ 8 * 365 * 86400 + 86400 * 2;
678 grub_free (node
->cache
);
680 return hook (filename
, &info
);
683 grub_dl_ref (my_mod
);
685 data
= grub_sfs_mount (device
->disk
);
689 grub_fshelp_find_file (path
, &data
->diropen
, &fdiro
, grub_sfs_iterate_dir
,
690 grub_sfs_read_symlink
, GRUB_FSHELP_DIR
);
694 grub_sfs_iterate_dir (fdiro
, iterate
);
697 if (data
&& fdiro
!= &data
->diropen
)
700 grub_free (data
->label
);
703 grub_dl_unref (my_mod
);
710 grub_sfs_label (grub_device_t device
, char **label
)
712 struct grub_sfs_data
*data
;
713 grub_disk_t disk
= device
->disk
;
715 data
= grub_sfs_mount (disk
);
718 grub_size_t len
= grub_strlen (data
->label
);
719 *label
= grub_malloc (len
* GRUB_MAX_UTF8_PER_LATIN1
+ 1);
721 *grub_latin1_to_utf8 ((grub_uint8_t
*) *label
,
722 (const grub_uint8_t
*) data
->label
,
724 grub_free (data
->label
);
732 static struct grub_fs grub_sfs_fs
=
736 .open
= grub_sfs_open
,
737 .read
= grub_sfs_read
,
738 .close
= grub_sfs_close
,
739 .label
= grub_sfs_label
,
741 .reserved_first_sector
= 0,
742 .blocklist_install
= 1,
749 grub_fs_register (&grub_sfs_fs
);
755 grub_fs_unregister (&grub_sfs_fs
);