1 /* ufs.c - Unix File System */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2004,2005,2007,2008 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>
29 #define GRUB_UFS_MAGIC 0x11954
30 #define GRUB_UFS2_MAGIC 0x19540119
31 #define GRUB_UFS_INODE 2
32 #define GRUB_UFS_FILETYPE_DIR 4
33 #define GRUB_UFS_FILETYPE_LNK 10
34 #define GRUB_UFS_MAX_SYMLNK_CNT 8
36 #define GRUB_UFS_DIRBLKS 12
37 #define GRUB_UFS_INDIRBLKS 3
39 #define GRUB_UFS_ATTR_TYPE 0160000
40 #define GRUB_UFS_ATTR_FILE 0100000
41 #define GRUB_UFS_ATTR_DIR 0040000
42 #define GRUB_UFS_ATTR_LNK 0120000
44 #define GRUB_UFS_VOLNAME_LEN 32
46 /* Calculate in which group the inode can be found. */
47 #define inode_group(inode,sblock) ()
49 #define UFS_BLKSZ(sblock) (grub_le_to_cpu32 (sblock->bsize))
51 #define INODE(data,field) (data->ufs_type == UFS1 ? \
52 data->inode. field : data->inode2. field)
53 #define INODE_ENDIAN(data,field,bits1,bits2) (data->ufs_type == UFS1 ? \
54 grub_le_to_cpu##bits1 (data->inode.field) : \
55 grub_le_to_cpu##bits2 (data->inode2.field))
56 #define INODE_SIZE(data) INODE_ENDIAN (data,size,32,64)
57 #define INODE_NBLOCKS(data) INODE_ENDIAN (data,nblocks,32,64)
59 #define INODE_MODE(data) INODE_ENDIAN (data,mode,16,16)
60 #define INODE_BLKSZ(data) (data->ufs_type == UFS1 ? 4 : 8)
61 #define INODE_DIRBLOCKS(data,blk) INODE_ENDIAN \
62 (data,blocks.dir_blocks[blk],32,64)
63 #define INODE_INDIRBLOCKS(data,blk) INODE_ENDIAN \
64 (data,blocks.indir_blocks[blk],32,64)
66 /* The blocks on which the superblock can be found. */
67 static int sblocklist
[] = { 128, 16, 0, 512, -1 };
69 struct grub_ufs_sblock
71 grub_uint8_t unused
[16];
72 /* The offset of the inodes in the cylinder group. */
73 grub_uint32_t inoblk_offs
;
75 grub_uint8_t unused2
[4];
77 /* The start of the cylinder group. */
78 grub_uint32_t cylg_offset
;
79 grub_uint8_t unused3
[4];
82 grub_uint8_t unused4
[12];
84 /* The size of a block in bytes. */
86 grub_uint8_t unused5
[48];
88 /* The size of filesystem blocks to disk blocks. */
89 grub_uint32_t log2_blksz
;
90 grub_uint8_t unused6
[80];
92 /* Inodes stored per cylinder group. */
93 grub_uint32_t ino_per_group
;
95 /* The frags per cylinder group. */
96 grub_uint32_t frags_per_group
;
98 grub_uint8_t unused7
[488];
100 /* Volume name for UFS2. */
101 grub_uint8_t volume_name
[GRUB_UFS_VOLNAME_LEN
];
102 grub_uint8_t unused8
[232];
104 grub_uint64_t mtime2
;
105 grub_uint8_t unused9
[420];
107 /* Magic value to check if this is really a UFS filesystem. */
112 struct grub_ufs_inode
115 grub_uint16_t nlinks
;
126 grub_uint32_t dir_blocks
[GRUB_UFS_DIRBLKS
];
127 grub_uint32_t indir_blocks
[GRUB_UFS_INDIRBLKS
];
129 grub_uint8_t symlink
[(GRUB_UFS_DIRBLKS
+ GRUB_UFS_INDIRBLKS
) * 4];
132 grub_uint32_t nblocks
;
134 grub_uint32_t unused
;
135 grub_uint8_t pad
[12];
136 } __attribute__ ((packed
));
139 struct grub_ufs2_inode
142 grub_uint16_t nlinks
;
145 grub_uint32_t blocksize
;
147 grub_int64_t nblocks
;
151 grub_uint64_t create_time
;
152 grub_uint32_t atime_sec
;
153 grub_uint32_t mtime_sec
;
154 grub_uint32_t ctime_sec
;
155 grub_uint32_t create_time_sec
;
157 grub_uint32_t kernel_flags
;
160 grub_uint64_t ext
[2];
165 grub_uint64_t dir_blocks
[GRUB_UFS_DIRBLKS
];
166 grub_uint64_t indir_blocks
[GRUB_UFS_INDIRBLKS
];
168 grub_uint8_t symlink
[(GRUB_UFS_DIRBLKS
+ GRUB_UFS_INDIRBLKS
) * 8];
171 grub_uint8_t unused
[24];
172 } __attribute__ ((packed
));
174 /* Directory entry. */
175 struct grub_ufs_dirent
178 grub_uint16_t direntlen
;
181 grub_uint16_t namelen
;
184 grub_uint8_t filetype_bsd
;
185 grub_uint8_t namelen_bsd
;
188 } __attribute__ ((packed
));
190 /* Information about a "mounted" ufs filesystem. */
193 struct grub_ufs_sblock sblock
;
197 struct grub_ufs_inode inode
;
198 struct grub_ufs2_inode inode2
;
210 static grub_dl_t my_mod
;
212 /* Forward declaration. */
213 static grub_err_t
grub_ufs_find_file (struct grub_ufs_data
*data
,
218 grub_ufs_get_file_block (struct grub_ufs_data
*data
, unsigned int blk
)
220 struct grub_ufs_sblock
*sblock
= &data
->sblock
;
221 unsigned int indirsz
;
225 if (blk
< GRUB_UFS_DIRBLKS
)
226 return INODE_DIRBLOCKS (data
, blk
);
228 log2_blksz
= grub_le_to_cpu32 (data
->sblock
.log2_blksz
);
230 blk
-= GRUB_UFS_DIRBLKS
;
232 indirsz
= UFS_BLKSZ (sblock
) / INODE_BLKSZ (data
);
233 /* Single indirect block. */
236 grub_uint32_t indir
[UFS_BLKSZ (sblock
) >> 2];
237 grub_disk_read (data
->disk
, INODE_INDIRBLOCKS (data
, 0) << log2_blksz
,
238 0, sizeof (indir
), indir
);
239 return (data
->ufs_type
== UFS1
) ? indir
[blk
] : indir
[blk
<< 1];
243 /* Double indirect block. */
244 if (blk
< indirsz
* indirsz
)
246 grub_uint32_t indir
[UFS_BLKSZ (sblock
) >> 2];
248 grub_disk_read (data
->disk
, INODE_INDIRBLOCKS (data
, 1) << log2_blksz
,
249 0, sizeof (indir
), indir
);
250 grub_disk_read (data
->disk
,
251 ((data
->ufs_type
== UFS1
) ?
252 indir
[blk
/ indirsz
] : indir
[(blk
/ indirsz
) << 1])
254 0, sizeof (indir
), indir
);
256 return (data
->ufs_type
== UFS1
) ?
257 indir
[blk
% indirsz
] : indir
[(blk
% indirsz
) << 1];
261 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
262 "ufs does not support triple indirect blocks");
267 /* Read LEN bytes from the file described by DATA starting with byte
268 POS. Return the amount of read bytes in READ. */
270 grub_ufs_read_file (struct grub_ufs_data
*data
,
271 void NESTED_FUNC_ATTR (*read_hook
) (grub_disk_addr_t sector
,
272 unsigned offset
, unsigned length
),
273 int pos
, grub_size_t len
, char *buf
)
275 struct grub_ufs_sblock
*sblock
= &data
->sblock
;
279 /* Adjust len so it we can't read past the end of the file. */
280 if (len
> INODE_SIZE (data
))
281 len
= INODE_SIZE (data
);
283 blockcnt
= (len
+ pos
+ UFS_BLKSZ (sblock
) - 1) / UFS_BLKSZ (sblock
);
285 for (i
= pos
/ UFS_BLKSZ (sblock
); i
< blockcnt
; i
++)
288 int blockoff
= pos
% UFS_BLKSZ (sblock
);
289 int blockend
= UFS_BLKSZ (sblock
);
293 blknr
= grub_ufs_get_file_block (data
, i
);
298 if (i
== blockcnt
- 1)
300 blockend
= (len
+ pos
) % UFS_BLKSZ (sblock
);
303 blockend
= UFS_BLKSZ (sblock
);
307 if (i
== (pos
/ (int) UFS_BLKSZ (sblock
)))
309 skipfirst
= blockoff
;
310 blockend
-= skipfirst
;
313 /* XXX: If the block number is 0 this block is not stored on
314 disk but is zero filled instead. */
317 data
->disk
->read_hook
= read_hook
;
318 grub_disk_read (data
->disk
,
319 blknr
<< grub_le_to_cpu32 (data
->sblock
.log2_blksz
),
320 skipfirst
, blockend
, buf
);
321 data
->disk
->read_hook
= 0;
326 grub_memset (buf
, UFS_BLKSZ (sblock
) - skipfirst
, 0);
328 buf
+= UFS_BLKSZ (sblock
) - skipfirst
;
335 /* Read inode INO from the mounted filesystem described by DATA. This
336 inode is used by default now. */
338 grub_ufs_read_inode (struct grub_ufs_data
*data
, int ino
, char *inode
)
340 struct grub_ufs_sblock
*sblock
= &data
->sblock
;
342 /* Determine the group the inode is in. */
343 int group
= ino
/ grub_le_to_cpu32 (sblock
->ino_per_group
);
345 /* Determine the inode within the group. */
346 int grpino
= ino
% grub_le_to_cpu32 (sblock
->ino_per_group
);
348 /* The first block of the group. */
349 int grpblk
= group
* (grub_le_to_cpu32 (sblock
->frags_per_group
));
351 if (data
->ufs_type
== UFS1
)
355 inode
= (char *) &data
->inode
;
359 grub_disk_read (data
->disk
,
360 (((grub_le_to_cpu32 (sblock
->inoblk_offs
) + grpblk
)
361 << grub_le_to_cpu32 (data
->sblock
.log2_blksz
)))
363 (grpino
% 4) * sizeof (struct grub_ufs_inode
),
364 sizeof (struct grub_ufs_inode
),
371 inode
= (char *) &data
->inode2
;
375 grub_disk_read (data
->disk
,
376 (((grub_le_to_cpu32 (sblock
->inoblk_offs
) + grpblk
)
377 << grub_le_to_cpu32 (data
->sblock
.log2_blksz
)))
379 (grpino
% 2) * sizeof (struct grub_ufs2_inode
),
380 sizeof (struct grub_ufs2_inode
),
388 /* Lookup the symlink the current inode points to. INO is the inode
389 number of the directory the symlink is relative to. */
391 grub_ufs_lookup_symlink (struct grub_ufs_data
*data
, int ino
)
393 char symlink
[INODE_SIZE (data
)];
395 if (++data
->linknest
> GRUB_UFS_MAX_SYMLNK_CNT
)
396 return grub_error (GRUB_ERR_SYMLINK_LOOP
, "too deep nesting of symlinks");
398 if (INODE_NBLOCKS (data
) == 0)
399 grub_strcpy (symlink
, (char *) INODE (data
, symlink
));
402 grub_disk_read (data
->disk
,
403 (INODE_DIRBLOCKS (data
, 0)
404 << grub_le_to_cpu32 (data
->sblock
.log2_blksz
)),
405 0, INODE_SIZE (data
), symlink
);
406 symlink
[INODE_SIZE (data
)] = '\0';
409 /* The symlink is an absolute path, go back to the root inode. */
410 if (symlink
[0] == '/')
411 ino
= GRUB_UFS_INODE
;
413 /* Now load in the old inode. */
414 if (grub_ufs_read_inode (data
, ino
, 0))
417 grub_ufs_find_file (data
, symlink
);
419 grub_error (grub_errno
, "Can not follow symlink `%s'.", symlink
);
425 /* Find the file with the pathname PATH on the filesystem described by
428 grub_ufs_find_file (struct grub_ufs_data
*data
, const char *path
)
430 char fpath
[grub_strlen (path
) + 1];
433 unsigned int pos
= 0;
436 grub_strcpy (fpath
, path
);
438 /* Skip the first slash. */
446 /* Extract the actual part from the pathname. */
447 next
= grub_strchr (name
, '/');
456 struct grub_ufs_dirent dirent
;
459 if (grub_strlen (name
) == 0)
460 return GRUB_ERR_NONE
;
462 if (grub_ufs_read_file (data
, 0, pos
, sizeof (dirent
),
463 (char *) &dirent
) < 0)
466 namelen
= (data
->ufs_type
== UFS2
)
467 ? dirent
.namelen_bsd
: grub_le_to_cpu16 (dirent
.namelen
);
470 char filename
[namelen
+ 1];
472 if (grub_ufs_read_file (data
, 0, pos
+ sizeof (dirent
),
473 namelen
, filename
) < 0)
476 filename
[namelen
] = '\0';
478 if (!grub_strcmp (name
, filename
))
481 grub_ufs_read_inode (data
, grub_le_to_cpu32 (dirent
.ino
), 0);
483 if ((INODE_MODE(data
) & GRUB_UFS_ATTR_TYPE
)
484 == GRUB_UFS_ATTR_LNK
)
486 grub_ufs_lookup_symlink (data
, dirino
);
497 next
= grub_strchr (name
, '/');
504 if ((INODE_MODE(data
) & GRUB_UFS_ATTR_TYPE
) != GRUB_UFS_ATTR_DIR
)
505 return grub_error (GRUB_ERR_BAD_FILE_TYPE
, "not a directory");
511 pos
+= grub_le_to_cpu16 (dirent
.direntlen
);
512 } while (pos
< INODE_SIZE (data
));
514 grub_error (GRUB_ERR_FILE_NOT_FOUND
, "file not found");
519 /* Mount the filesystem on the disk DISK. */
520 static struct grub_ufs_data
*
521 grub_ufs_mount (grub_disk_t disk
)
523 struct grub_ufs_data
*data
;
524 int *sblklist
= sblocklist
;
526 data
= grub_malloc (sizeof (struct grub_ufs_data
));
530 /* Find a UFS1 or UFS2 sblock. */
531 data
->ufs_type
= UNKNOWN
;
532 while (*sblklist
!= -1)
534 grub_disk_read (disk
, *sblklist
, 0, sizeof (struct grub_ufs_sblock
),
539 if (grub_le_to_cpu32 (data
->sblock
.magic
) == GRUB_UFS_MAGIC
)
541 data
->ufs_type
= UFS1
;
544 else if (grub_le_to_cpu32 (data
->sblock
.magic
) == GRUB_UFS2_MAGIC
)
546 data
->ufs_type
= UFS2
;
551 if (data
->ufs_type
== UNKNOWN
)
553 grub_error (GRUB_ERR_BAD_FS
, "not an ufs filesystem");
564 if (grub_errno
== GRUB_ERR_OUT_OF_RANGE
)
565 grub_error (GRUB_ERR_BAD_FS
, "not a ufs filesystem");
572 grub_ufs_dir (grub_device_t device
, const char *path
,
573 int (*hook
) (const char *filename
,
574 const struct grub_dirhook_info
*info
))
576 struct grub_ufs_data
*data
;
577 struct grub_ufs_sblock
*sblock
;
578 unsigned int pos
= 0;
580 data
= grub_ufs_mount (device
->disk
);
584 grub_ufs_read_inode (data
, GRUB_UFS_INODE
, 0);
588 sblock
= &data
->sblock
;
590 if (!path
|| path
[0] != '/')
592 grub_error (GRUB_ERR_BAD_FILENAME
, "bad filename");
596 grub_ufs_find_file (data
, path
);
600 if ((INODE_MODE (data
) & GRUB_UFS_ATTR_TYPE
) != GRUB_UFS_ATTR_DIR
)
602 grub_error (GRUB_ERR_BAD_FILE_TYPE
, "not a directory");
606 while (pos
< INODE_SIZE (data
))
608 struct grub_ufs_dirent dirent
;
611 if (grub_ufs_read_file (data
, 0, pos
, sizeof (dirent
),
612 (char *) &dirent
) < 0)
615 namelen
= (data
->ufs_type
== UFS2
)
616 ? dirent
.namelen_bsd
: grub_le_to_cpu16 (dirent
.namelen
);
619 char filename
[namelen
+ 1];
620 struct grub_dirhook_info info
;
621 grub_memset (&info
, 0, sizeof (info
));
623 if (grub_ufs_read_file (data
, 0, pos
+ sizeof (dirent
),
624 namelen
, filename
) < 0)
627 filename
[namelen
] = '\0';
628 if (data
->ufs_type
== UFS1
)
630 struct grub_ufs_inode inode
;
631 grub_ufs_read_inode (data
, dirent
.ino
, (char *) &inode
);
632 info
.dir
= ((grub_le_to_cpu16 (inode
.mode
) & GRUB_UFS_ATTR_TYPE
)
633 == GRUB_UFS_ATTR_DIR
);
634 info
.mtime
= grub_le_to_cpu64 (inode
.mtime
);
639 struct grub_ufs2_inode inode
;
640 grub_ufs_read_inode (data
, dirent
.ino
, (char *) &inode
);
641 info
.dir
= ((grub_le_to_cpu16 (inode
.mode
) & GRUB_UFS_ATTR_TYPE
)
642 == GRUB_UFS_ATTR_DIR
);
643 info
.mtime
= grub_le_to_cpu64 (inode
.mtime
);
647 if (hook (filename
, &info
))
651 pos
+= grub_le_to_cpu16 (dirent
.direntlen
);
661 /* Open a file named NAME and initialize FILE. */
663 grub_ufs_open (struct grub_file
*file
, const char *name
)
665 struct grub_ufs_data
*data
;
666 data
= grub_ufs_mount (file
->device
->disk
);
670 grub_ufs_read_inode (data
, 2, 0);
677 if (!name
|| name
[0] != '/')
679 grub_error (GRUB_ERR_BAD_FILENAME
, "bad filename");
683 grub_ufs_find_file (data
, name
);
691 file
->size
= INODE_SIZE (data
);
693 return GRUB_ERR_NONE
;
698 grub_ufs_read (grub_file_t file
, char *buf
, grub_size_t len
)
700 struct grub_ufs_data
*data
=
701 (struct grub_ufs_data
*) file
->data
;
703 return grub_ufs_read_file (data
, file
->read_hook
, file
->offset
, len
, buf
);
708 grub_ufs_close (grub_file_t file
)
710 grub_free (file
->data
);
712 return GRUB_ERR_NONE
;
717 grub_ufs_label (grub_device_t device
, char **label
)
719 struct grub_ufs_data
*data
= 0;
721 grub_dl_ref (my_mod
);
725 data
= grub_ufs_mount (device
->disk
);
728 if (data
->ufs_type
== UFS2
)
729 *label
= grub_strdup ((char *) data
->sblock
.volume_name
);
732 grub_dl_unref (my_mod
);
741 grub_ufs_mtime (grub_device_t device
, grub_int32_t
*tm
)
743 struct grub_ufs_data
*data
= 0;
745 grub_dl_ref (my_mod
);
747 data
= grub_ufs_mount (device
->disk
);
750 else if (data
->ufs_type
== UFS1
)
751 *tm
= grub_le_to_cpu32 (data
->sblock
.mtime
);
753 *tm
= grub_le_to_cpu64 (data
->sblock
.mtime2
);
755 grub_dl_unref (my_mod
);
764 static struct grub_fs grub_ufs_fs
=
768 .open
= grub_ufs_open
,
769 .read
= grub_ufs_read
,
770 .close
= grub_ufs_close
,
771 .label
= grub_ufs_label
,
772 .mtime
= grub_ufs_mtime
,
778 grub_fs_register (&grub_ufs_fs
);
784 grub_fs_unregister (&grub_ufs_fs
);