1 /* ufs.c - Unix File System */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2004,2005,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>
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_DIR 040000
41 #define GRUB_UFS_VOLNAME_LEN 32
43 /* Calculate in which group the inode can be found. */
44 #define inode_group(inode,sblock) ()
46 #define UFS_BLKSZ(sblock) (grub_le_to_cpu32 (sblock->bsize))
48 #define INODE(data,field) (data->ufs_type == UFS1 ? \
49 data->inode. field : data->inode2. field)
50 #define INODE_ENDIAN(data,field,bits1,bits2) (data->ufs_type == UFS1 ? \
51 grub_le_to_cpu##bits1 (data->inode.field) : \
52 grub_le_to_cpu##bits2 (data->inode2.field))
53 #define INODE_SIZE(data) INODE_ENDIAN (data,size,32,64)
54 #define INODE_MODE(data) INODE_ENDIAN (data,mode,16,16)
55 #define INODE_BLKSZ(data) (data->ufs_type == UFS1 ? 4 : 8)
56 #define INODE_DIRBLOCKS(data,blk) INODE_ENDIAN \
57 (data,blocks.dir_blocks[blk],32,64)
58 #define INODE_INDIRBLOCKS(data,blk) INODE_ENDIAN \
59 (data,blocks.indir_blocks[blk],32,64)
61 /* The blocks on which the superblock can be found. */
62 static int sblocklist
[] = { 128, 16, 0, 512, -1 };
64 struct grub_ufs_sblock
66 grub_uint8_t unused
[16];
67 /* The offset of the inodes in the cylinder group. */
68 grub_uint32_t inoblk_offs
;
70 grub_uint8_t unused2
[4];
72 /* The start of the cylinder group. */
73 grub_uint32_t cylg_offset
;
75 grub_uint8_t unused3
[20];
77 /* The size of a block in bytes. */
79 grub_uint8_t unused4
[48];
81 /* The size of filesystem blocks to disk blocks. */
82 grub_uint32_t log2_blksz
;
83 grub_uint8_t unused5
[80];
85 /* Inodes stored per cylinder group. */
86 grub_uint32_t ino_per_group
;
88 /* The frags per cylinder group. */
89 grub_uint32_t frags_per_group
;
91 grub_uint8_t unused7
[488];
93 /* Volume name for UFS2. */
94 grub_uint8_t volume_name
[GRUB_UFS_VOLNAME_LEN
];
96 grub_uint8_t unused8
[660];
98 /* Magic value to check if this is really a UFS filesystem. */
103 struct grub_ufs_inode
106 grub_uint16_t nlinks
;
117 grub_uint32_t dir_blocks
[GRUB_UFS_DIRBLKS
];
118 grub_uint32_t indir_blocks
[GRUB_UFS_INDIRBLKS
];
120 grub_uint8_t symlink
[(GRUB_UFS_DIRBLKS
+ GRUB_UFS_INDIRBLKS
) * 4];
123 grub_uint32_t nblocks
;
125 grub_uint32_t unused
;
126 grub_uint8_t pad
[12];
130 struct grub_ufs2_inode
133 grub_uint16_t nlinks
;
136 grub_uint32_t blocksize
;
138 grub_int64_t nblocks
;
142 grub_uint64_t create_time
;
143 grub_uint32_t atime_sec
;
144 grub_uint32_t mtime_sec
;
145 grub_uint32_t ctime_sec
;
146 grub_uint32_t create_time_sec
;
148 grub_uint32_t kernel_flags
;
151 grub_uint64_t ext
[2];
156 grub_uint64_t dir_blocks
[GRUB_UFS_DIRBLKS
];
157 grub_uint64_t indir_blocks
[GRUB_UFS_INDIRBLKS
];
159 grub_uint8_t symlink
[(GRUB_UFS_DIRBLKS
+ GRUB_UFS_INDIRBLKS
) * 8];
162 grub_uint8_t unused
[24];
165 /* Directory entry. */
166 struct grub_ufs_dirent
169 grub_uint16_t direntlen
;
170 grub_uint8_t filetype
;
171 grub_uint8_t namelen
;
174 /* Information about a "mounted" ufs filesystem. */
177 struct grub_ufs_sblock sblock
;
181 struct grub_ufs_inode inode
;
182 struct grub_ufs2_inode inode2
;
195 static grub_dl_t my_mod
;
198 /* Forward declaration. */
199 static grub_err_t
grub_ufs_find_file (struct grub_ufs_data
*data
,
204 grub_ufs_get_file_block (struct grub_ufs_data
*data
, unsigned int blk
)
206 struct grub_ufs_sblock
*sblock
= &data
->sblock
;
207 unsigned int indirsz
;
211 if (blk
< GRUB_UFS_DIRBLKS
)
212 return INODE_DIRBLOCKS (data
, blk
);
214 log2_blksz
= grub_le_to_cpu32 (data
->sblock
.log2_blksz
);
216 blk
-= GRUB_UFS_DIRBLKS
;
218 indirsz
= UFS_BLKSZ (sblock
) / INODE_BLKSZ (data
);
219 /* Single indirect block. */
222 grub_uint32_t indir
[UFS_BLKSZ (sblock
) >> 2];
223 grub_disk_read (data
->disk
, INODE_INDIRBLOCKS (data
, 0) << log2_blksz
,
224 0, sizeof (indir
), (char *) indir
);
225 return (data
->ufs_type
== UFS1
) ? indir
[blk
] : indir
[blk
<< 1];
229 /* Double indirect block. */
230 if (blk
< UFS_BLKSZ (sblock
) / indirsz
)
232 grub_uint32_t indir
[UFS_BLKSZ (sblock
) >> 2];
234 grub_disk_read (data
->disk
, INODE_INDIRBLOCKS (data
, 1) << log2_blksz
,
235 0, sizeof (indir
), (char *) indir
);
236 grub_disk_read (data
->disk
,
237 (data
->ufs_type
== UFS1
) ?
238 indir
[blk
/ indirsz
] : indir
[(blk
/ indirsz
) << 1],
239 0, sizeof (indir
), (char *) indir
);
241 return (data
->ufs_type
== UFS1
) ?
242 indir
[blk
% indirsz
] : indir
[(blk
% indirsz
) << 1];
246 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
247 "ufs does not support triple indirect blocks");
252 /* Read LEN bytes from the file described by DATA starting with byte
253 POS. Return the amount of read bytes in READ. */
255 grub_ufs_read_file (struct grub_ufs_data
*data
,
256 void NESTED_FUNC_ATTR (*read_hook
) (grub_disk_addr_t sector
,
257 unsigned offset
, unsigned length
),
258 int pos
, grub_size_t len
, char *buf
)
260 struct grub_ufs_sblock
*sblock
= &data
->sblock
;
264 /* Adjust len so it we can't read past the end of the file. */
265 if (len
> INODE_SIZE (data
))
266 len
= INODE_SIZE (data
);
268 blockcnt
= (len
+ pos
+ UFS_BLKSZ (sblock
) - 1) / UFS_BLKSZ (sblock
);
270 for (i
= pos
/ UFS_BLKSZ (sblock
); i
< blockcnt
; i
++)
273 int blockoff
= pos
% UFS_BLKSZ (sblock
);
274 int blockend
= UFS_BLKSZ (sblock
);
278 blknr
= grub_ufs_get_file_block (data
, i
);
283 if (i
== blockcnt
- 1)
285 blockend
= (len
+ pos
) % UFS_BLKSZ (sblock
);
288 blockend
= UFS_BLKSZ (sblock
);
292 if (i
== (pos
/ (int) UFS_BLKSZ (sblock
)))
294 skipfirst
= blockoff
;
295 blockend
-= skipfirst
;
298 /* XXX: If the block number is 0 this block is not stored on
299 disk but is zero filled instead. */
302 data
->disk
->read_hook
= read_hook
;
303 grub_disk_read (data
->disk
,
304 blknr
<< grub_le_to_cpu32 (data
->sblock
.log2_blksz
),
305 skipfirst
, blockend
, buf
);
306 data
->disk
->read_hook
= 0;
311 grub_memset (buf
, UFS_BLKSZ (sblock
) - skipfirst
, 0);
313 buf
+= UFS_BLKSZ (sblock
) - skipfirst
;
320 /* Read inode INO from the mounted filesystem described by DATA. This
321 inode is used by default now. */
323 grub_ufs_read_inode (struct grub_ufs_data
*data
, int ino
)
325 struct grub_ufs_sblock
*sblock
= &data
->sblock
;
327 /* Determine the group the inode is in. */
328 int group
= ino
/ grub_le_to_cpu32 (sblock
->ino_per_group
);
330 /* Determine the inode within the group. */
331 int grpino
= ino
% grub_le_to_cpu32 (sblock
->ino_per_group
);
333 /* The first block of the group. */
334 int grpblk
= group
* (grub_le_to_cpu32 (sblock
->frags_per_group
));
336 if (data
->ufs_type
== UFS1
)
338 struct grub_ufs_inode
*inode
= &data
->inode
;
340 grub_disk_read (data
->disk
,
341 (((grub_le_to_cpu32 (sblock
->inoblk_offs
) + grpblk
)
342 << grub_le_to_cpu32 (data
->sblock
.log2_blksz
)))
344 (grpino
% 4) * sizeof (struct grub_ufs_inode
),
345 sizeof (struct grub_ufs_inode
),
350 struct grub_ufs2_inode
*inode
= &data
->inode2
;
352 grub_disk_read (data
->disk
,
353 (((grub_le_to_cpu32 (sblock
->inoblk_offs
) + grpblk
)
354 << grub_le_to_cpu32 (data
->sblock
.log2_blksz
)))
356 (grpino
% 2) * sizeof (struct grub_ufs2_inode
),
357 sizeof (struct grub_ufs2_inode
),
366 /* Lookup the symlink the current inode points to. INO is the inode
367 number of the directory the symlink is relative to. */
369 grub_ufs_lookup_symlink (struct grub_ufs_data
*data
, int ino
)
371 char symlink
[INODE_SIZE (data
)];
373 if (++data
->linknest
> GRUB_UFS_MAX_SYMLNK_CNT
)
374 return grub_error (GRUB_ERR_SYMLINK_LOOP
, "too deep nesting of symlinks");
376 if (INODE_SIZE (data
) < (GRUB_UFS_DIRBLKS
+ GRUB_UFS_INDIRBLKS
377 * INODE_BLKSZ (data
)))
378 grub_strcpy (symlink
, (char *) INODE (data
, symlink
));
381 grub_disk_read (data
->disk
,
382 (INODE_DIRBLOCKS (data
, 0)
383 << grub_le_to_cpu32 (data
->sblock
.log2_blksz
)),
384 0, INODE_SIZE (data
), symlink
);
385 symlink
[INODE_SIZE (data
)] = '\0';
388 /* The symlink is an absolute path, go back to the root inode. */
389 if (symlink
[0] == '/')
390 ino
= GRUB_UFS_INODE
;
392 /* Now load in the old inode. */
393 if (grub_ufs_read_inode (data
, ino
))
396 grub_ufs_find_file (data
, symlink
);
398 grub_error (grub_errno
, "Can not follow symlink `%s'.", symlink
);
404 /* Find the file with the pathname PATH on the filesystem described by
407 grub_ufs_find_file (struct grub_ufs_data
*data
, const char *path
)
409 char fpath
[grub_strlen (path
) + 1];
412 unsigned int pos
= 0;
415 grub_strcpy (fpath
, path
);
417 /* Skip the first slash. */
425 /* Extract the actual part from the pathname. */
426 next
= grub_strchr (name
, '/');
435 struct grub_ufs_dirent dirent
;
437 if (grub_strlen (name
) == 0)
438 return GRUB_ERR_NONE
;
440 if (grub_ufs_read_file (data
, 0, pos
, sizeof (dirent
),
441 (char *) &dirent
) < 0)
445 char filename
[dirent
.namelen
+ 1];
447 if (grub_ufs_read_file (data
, 0, pos
+ sizeof (dirent
),
448 dirent
.namelen
, filename
) < 0)
451 filename
[dirent
.namelen
] = '\0';
453 if (!grub_strcmp (name
, filename
))
456 grub_ufs_read_inode (data
, grub_le_to_cpu32 (dirent
.ino
));
458 if (dirent
.filetype
== GRUB_UFS_FILETYPE_LNK
)
460 grub_ufs_lookup_symlink (data
, dirino
);
471 next
= grub_strchr (name
, '/');
478 if (!(dirent
.filetype
& GRUB_UFS_FILETYPE_DIR
))
479 return grub_error (GRUB_ERR_BAD_FILE_TYPE
, "not a directory");
485 pos
+= grub_le_to_cpu16 (dirent
.direntlen
);
486 } while (pos
< INODE_SIZE (data
));
488 grub_error (GRUB_ERR_FILE_NOT_FOUND
, "file not found");
493 /* Mount the filesystem on the disk DISK. */
494 static struct grub_ufs_data
*
495 grub_ufs_mount (grub_disk_t disk
)
497 struct grub_ufs_data
*data
;
498 int *sblklist
= sblocklist
;
500 data
= grub_malloc (sizeof (struct grub_ufs_data
));
504 /* Find a UFS1 or UFS2 sblock. */
505 data
->ufs_type
= UNKNOWN
;
506 while (*sblklist
!= -1)
508 grub_disk_read (disk
, *sblklist
, 0, sizeof (struct grub_ufs_sblock
),
509 (char *) &data
->sblock
);
513 if (grub_le_to_cpu32 (data
->sblock
.magic
) == GRUB_UFS_MAGIC
)
515 data
->ufs_type
= UFS1
;
518 else if (grub_le_to_cpu32 (data
->sblock
.magic
) == GRUB_UFS2_MAGIC
)
520 data
->ufs_type
= UFS2
;
525 if (data
->ufs_type
== UNKNOWN
)
527 grub_error (GRUB_ERR_BAD_FS
, "not an ufs filesystem");
538 if (grub_errno
== GRUB_ERR_OUT_OF_RANGE
)
539 grub_error (GRUB_ERR_BAD_FS
, "not a ufs filesystem");
546 grub_ufs_dir (grub_device_t device
, const char *path
,
547 int (*hook
) (const char *filename
, int dir
))
549 struct grub_ufs_data
*data
;
550 struct grub_ufs_sblock
*sblock
;
551 unsigned int pos
= 0;
553 data
= grub_ufs_mount (device
->disk
);
557 grub_ufs_read_inode (data
, GRUB_UFS_INODE
);
561 sblock
= &data
->sblock
;
563 if (!path
|| path
[0] != '/')
565 grub_error (GRUB_ERR_BAD_FILENAME
, "bad filename");
569 grub_ufs_find_file (data
, path
);
573 if (!(INODE_MODE (data
) & GRUB_UFS_ATTR_DIR
))
575 grub_error (GRUB_ERR_BAD_FILE_TYPE
, "not a directory");
579 while (pos
< INODE_SIZE (data
))
581 struct grub_ufs_dirent dirent
;
583 if (grub_ufs_read_file (data
, 0, pos
, sizeof (dirent
),
584 (char *) &dirent
) < 0)
588 char filename
[dirent
.namelen
+ 1];
590 if (grub_ufs_read_file (data
, 0, pos
+ sizeof (dirent
),
591 dirent
.namelen
, filename
) < 0)
594 filename
[dirent
.namelen
] = '\0';
595 if (hook (filename
, dirent
.filetype
== GRUB_UFS_FILETYPE_DIR
))
599 pos
+= grub_le_to_cpu16 (dirent
.direntlen
);
609 /* Open a file named NAME and initialize FILE. */
611 grub_ufs_open (struct grub_file
*file
, const char *name
)
613 struct grub_ufs_data
*data
;
614 data
= grub_ufs_mount (file
->device
->disk
);
618 grub_ufs_read_inode (data
, 2);
625 if (!name
|| name
[0] != '/')
627 grub_error (GRUB_ERR_BAD_FILENAME
, "bad filename");
631 grub_ufs_find_file (data
, name
);
639 file
->size
= INODE_SIZE (data
);
641 return GRUB_ERR_NONE
;
646 grub_ufs_read (grub_file_t file
, char *buf
, grub_size_t len
)
648 struct grub_ufs_data
*data
=
649 (struct grub_ufs_data
*) file
->data
;
651 return grub_ufs_read_file (data
, file
->read_hook
, file
->offset
, len
, buf
);
656 grub_ufs_close (grub_file_t file
)
658 grub_free (file
->data
);
660 return GRUB_ERR_NONE
;
665 grub_ufs_label (grub_device_t device
, char **label
)
667 struct grub_ufs_data
*data
= 0;
670 grub_dl_ref (my_mod
);
675 data
= grub_ufs_mount (device
->disk
);
678 if (data
->ufs_type
== UFS2
)
679 *label
= grub_strdup ((char *) data
->sblock
.volume_name
);
683 grub_dl_unref (my_mod
);
692 static struct grub_fs grub_ufs_fs
=
696 .open
= grub_ufs_open
,
697 .read
= grub_ufs_read
,
698 .close
= grub_ufs_close
,
699 .label
= grub_ufs_label
,
705 grub_fs_register (&grub_ufs_fs
);
713 grub_fs_unregister (&grub_ufs_fs
);