2009-06-22 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / fs / ufs.c
blob7a2d21f251dbaffe88bbe500c348cea0ade0f687
1 /* ufs.c - Unix File System */
2 /*
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/>.
20 #include <grub/err.h>
21 #include <grub/file.h>
22 #include <grub/mm.h>
23 #include <grub/misc.h>
24 #include <grub/disk.h>
25 #include <grub/dl.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];
81 grub_uint32_t mtime;
82 grub_uint8_t unused4[12];
84 /* The size of a block in bytes. */
85 grub_int32_t bsize;
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. */
108 grub_uint32_t magic;
111 /* UFS inode. */
112 struct grub_ufs_inode
114 grub_uint16_t mode;
115 grub_uint16_t nlinks;
116 grub_uint16_t uid;
117 grub_uint16_t gid;
118 grub_int64_t size;
119 grub_uint64_t atime;
120 grub_uint64_t mtime;
121 grub_uint64_t ctime;
122 union
124 struct
126 grub_uint32_t dir_blocks[GRUB_UFS_DIRBLKS];
127 grub_uint32_t indir_blocks[GRUB_UFS_INDIRBLKS];
128 } blocks;
129 grub_uint8_t symlink[(GRUB_UFS_DIRBLKS + GRUB_UFS_INDIRBLKS) * 4];
131 grub_uint32_t flags;
132 grub_uint32_t nblocks;
133 grub_uint32_t gen;
134 grub_uint32_t unused;
135 grub_uint8_t pad[12];
136 } __attribute__ ((packed));
138 /* UFS inode. */
139 struct grub_ufs2_inode
141 grub_uint16_t mode;
142 grub_uint16_t nlinks;
143 grub_uint32_t uid;
144 grub_uint32_t gid;
145 grub_uint32_t blocksize;
146 grub_int64_t size;
147 grub_int64_t nblocks;
148 grub_uint64_t atime;
149 grub_uint64_t mtime;
150 grub_uint64_t ctime;
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;
156 grub_uint32_t gen;
157 grub_uint32_t kernel_flags;
158 grub_uint32_t flags;
159 grub_uint32_t extsz;
160 grub_uint64_t ext[2];
161 union
163 struct
165 grub_uint64_t dir_blocks[GRUB_UFS_DIRBLKS];
166 grub_uint64_t indir_blocks[GRUB_UFS_INDIRBLKS];
167 } blocks;
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
177 grub_uint32_t ino;
178 grub_uint16_t direntlen;
179 union
181 grub_uint16_t namelen;
182 struct
184 grub_uint8_t filetype_bsd;
185 grub_uint8_t namelen_bsd;
188 } __attribute__ ((packed));
190 /* Information about a "mounted" ufs filesystem. */
191 struct grub_ufs_data
193 struct grub_ufs_sblock sblock;
194 grub_disk_t disk;
195 union
197 struct grub_ufs_inode inode;
198 struct grub_ufs2_inode inode2;
200 enum
202 UFS1,
203 UFS2,
204 UNKNOWN
205 } ufs_type;
206 int ino;
207 int linknest;
210 static grub_dl_t my_mod;
212 /* Forward declaration. */
213 static grub_err_t grub_ufs_find_file (struct grub_ufs_data *data,
214 const char *path);
217 static int
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;
222 int log2_blksz;
224 /* Direct. */
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. */
234 if (blk < indirsz)
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];
241 blk -= indirsz;
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])
253 << log2_blksz,
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");
263 return 0;
267 /* Read LEN bytes from the file described by DATA starting with byte
268 POS. Return the amount of read bytes in READ. */
269 static grub_ssize_t
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;
276 int i;
277 int blockcnt;
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++)
287 int blknr;
288 int blockoff = pos % UFS_BLKSZ (sblock);
289 int blockend = UFS_BLKSZ (sblock);
291 int skipfirst = 0;
293 blknr = grub_ufs_get_file_block (data, i);
294 if (grub_errno)
295 return -1;
297 /* Last block. */
298 if (i == blockcnt - 1)
300 blockend = (len + pos) % UFS_BLKSZ (sblock);
302 if (!blockend)
303 blockend = UFS_BLKSZ (sblock);
306 /* First block. */
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. */
315 if (blknr)
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;
322 if (grub_errno)
323 return -1;
325 else
326 grub_memset (buf, UFS_BLKSZ (sblock) - skipfirst, 0);
328 buf += UFS_BLKSZ (sblock) - skipfirst;
331 return len;
335 /* Read inode INO from the mounted filesystem described by DATA. This
336 inode is used by default now. */
337 static grub_err_t
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)
353 if (!inode)
355 inode = (char *) &data->inode;
356 data->ino = ino;
359 grub_disk_read (data->disk,
360 (((grub_le_to_cpu32 (sblock->inoblk_offs) + grpblk)
361 << grub_le_to_cpu32 (data->sblock.log2_blksz)))
362 + grpino / 4,
363 (grpino % 4) * sizeof (struct grub_ufs_inode),
364 sizeof (struct grub_ufs_inode),
365 inode);
367 else
369 if (!inode)
371 inode = (char *) &data->inode2;
372 data->ino = ino;
375 grub_disk_read (data->disk,
376 (((grub_le_to_cpu32 (sblock->inoblk_offs) + grpblk)
377 << grub_le_to_cpu32 (data->sblock.log2_blksz)))
378 + grpino / 2,
379 (grpino % 2) * sizeof (struct grub_ufs2_inode),
380 sizeof (struct grub_ufs2_inode),
381 inode);
384 return grub_errno;
388 /* Lookup the symlink the current inode points to. INO is the inode
389 number of the directory the symlink is relative to. */
390 static grub_err_t
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));
400 else
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))
415 return grub_errno;
417 grub_ufs_find_file (data, symlink);
418 if (grub_errno)
419 grub_error (grub_errno, "Can not follow symlink `%s'.", symlink);
421 return grub_errno;
425 /* Find the file with the pathname PATH on the filesystem described by
426 DATA. */
427 static grub_err_t
428 grub_ufs_find_file (struct grub_ufs_data *data, const char *path)
430 char fpath[grub_strlen (path) + 1];
431 char *name = fpath;
432 char *next;
433 unsigned int pos = 0;
434 int dirino;
436 grub_strcpy (fpath, path);
438 /* Skip the first slash. */
439 if (name[0] == '/')
441 name++;
442 if (!*name)
443 return 0;
446 /* Extract the actual part from the pathname. */
447 next = grub_strchr (name, '/');
448 if (next)
450 next[0] = '\0';
451 next++;
456 struct grub_ufs_dirent dirent;
457 int namelen;
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)
464 return grub_errno;
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)
474 return grub_errno;
476 filename[namelen] = '\0';
478 if (!grub_strcmp (name, filename))
480 dirino = data->ino;
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);
487 if (grub_errno)
488 return grub_errno;
491 if (!next)
492 return 0;
494 pos = 0;
496 name = next;
497 next = grub_strchr (name, '/');
498 if (next)
500 next[0] = '\0';
501 next++;
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");
507 continue;
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");
515 return grub_errno;
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));
527 if (!data)
528 return 0;
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),
535 &data->sblock);
536 if (grub_errno)
537 goto fail;
539 if (grub_le_to_cpu32 (data->sblock.magic) == GRUB_UFS_MAGIC)
541 data->ufs_type = UFS1;
542 break;
544 else if (grub_le_to_cpu32 (data->sblock.magic) == GRUB_UFS2_MAGIC)
546 data->ufs_type = UFS2;
547 break;
549 sblklist++;
551 if (data->ufs_type == UNKNOWN)
553 grub_error (GRUB_ERR_BAD_FS, "not an ufs filesystem");
554 goto fail;
557 data->disk = disk;
558 data->linknest = 0;
559 return data;
561 fail:
562 grub_free (data);
564 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
565 grub_error (GRUB_ERR_BAD_FS, "not a ufs filesystem");
567 return 0;
571 static grub_err_t
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);
581 if (!data)
582 return grub_errno;
584 grub_ufs_read_inode (data, GRUB_UFS_INODE, 0);
585 if (grub_errno)
586 return grub_errno;
588 sblock = &data->sblock;
590 if (!path || path[0] != '/')
592 grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
593 return grub_errno;
596 grub_ufs_find_file (data, path);
597 if (grub_errno)
598 goto fail;
600 if ((INODE_MODE (data) & GRUB_UFS_ATTR_TYPE) != GRUB_UFS_ATTR_DIR)
602 grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
603 goto fail;
606 while (pos < INODE_SIZE (data))
608 struct grub_ufs_dirent dirent;
609 int namelen;
611 if (grub_ufs_read_file (data, 0, pos, sizeof (dirent),
612 (char *) &dirent) < 0)
613 break;
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)
625 break;
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);
635 info.mtimeset = 1;
637 else
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);
644 info.mtimeset = 1;
647 if (hook (filename, &info))
648 break;
651 pos += grub_le_to_cpu16 (dirent.direntlen);
654 fail:
655 grub_free (data);
657 return grub_errno;
661 /* Open a file named NAME and initialize FILE. */
662 static grub_err_t
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);
667 if (!data)
668 return grub_errno;
670 grub_ufs_read_inode (data, 2, 0);
671 if (grub_errno)
673 grub_free (data);
674 return grub_errno;
677 if (!name || name[0] != '/')
679 grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
680 return grub_errno;
683 grub_ufs_find_file (data, name);
684 if (grub_errno)
686 grub_free (data);
687 return grub_errno;
690 file->data = data;
691 file->size = INODE_SIZE (data);
693 return GRUB_ERR_NONE;
697 static grub_ssize_t
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);
707 static grub_err_t
708 grub_ufs_close (grub_file_t file)
710 grub_free (file->data);
712 return GRUB_ERR_NONE;
716 static grub_err_t
717 grub_ufs_label (grub_device_t device, char **label)
719 struct grub_ufs_data *data = 0;
721 grub_dl_ref (my_mod);
723 *label = 0;
725 data = grub_ufs_mount (device->disk);
726 if (data)
728 if (data->ufs_type == UFS2)
729 *label = grub_strdup ((char *) data->sblock.volume_name);
732 grub_dl_unref (my_mod);
734 grub_free (data);
736 return grub_errno;
739 /* Get mtime. */
740 static grub_err_t
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);
748 if (!data)
749 *tm = 0;
750 else if (data->ufs_type == UFS1)
751 *tm = grub_le_to_cpu32 (data->sblock.mtime);
752 else
753 *tm = grub_le_to_cpu64 (data->sblock.mtime2);
755 grub_dl_unref (my_mod);
757 grub_free (data);
759 return grub_errno;
764 static struct grub_fs grub_ufs_fs =
766 .name = "ufs",
767 .dir = grub_ufs_dir,
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,
773 .next = 0
776 GRUB_MOD_INIT(ufs)
778 grub_fs_register (&grub_ufs_fs);
779 my_mod = mod;
782 GRUB_MOD_FINI(ufs)
784 grub_fs_unregister (&grub_ufs_fs);