2008-07-01 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / fs / ufs.c
blobebb71987868e964b5c1a0d599b11c603ca0adb54
1 /* ufs.c - Unix File System */
2 /*
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/>.
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_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. */
78 grub_int32_t bsize;
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. */
99 grub_uint32_t magic;
102 /* UFS inode. */
103 struct grub_ufs_inode
105 grub_uint16_t mode;
106 grub_uint16_t nlinks;
107 grub_uint16_t uid;
108 grub_uint16_t gid;
109 grub_int64_t size;
110 grub_uint64_t atime;
111 grub_uint64_t mtime;
112 grub_uint64_t ctime;
113 union
115 struct
117 grub_uint32_t dir_blocks[GRUB_UFS_DIRBLKS];
118 grub_uint32_t indir_blocks[GRUB_UFS_INDIRBLKS];
119 } blocks;
120 grub_uint8_t symlink[(GRUB_UFS_DIRBLKS + GRUB_UFS_INDIRBLKS) * 4];
122 grub_uint32_t flags;
123 grub_uint32_t nblocks;
124 grub_uint32_t gen;
125 grub_uint32_t unused;
126 grub_uint8_t pad[12];
129 /* UFS inode. */
130 struct grub_ufs2_inode
132 grub_uint16_t mode;
133 grub_uint16_t nlinks;
134 grub_uint32_t uid;
135 grub_uint32_t gid;
136 grub_uint32_t blocksize;
137 grub_int64_t size;
138 grub_int64_t nblocks;
139 grub_uint64_t atime;
140 grub_uint64_t mtime;
141 grub_uint64_t ctime;
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;
147 grub_uint32_t gen;
148 grub_uint32_t kernel_flags;
149 grub_uint32_t flags;
150 grub_uint32_t extsz;
151 grub_uint64_t ext[2];
152 union
154 struct
156 grub_uint64_t dir_blocks[GRUB_UFS_DIRBLKS];
157 grub_uint64_t indir_blocks[GRUB_UFS_INDIRBLKS];
158 } blocks;
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
168 grub_uint32_t ino;
169 grub_uint16_t direntlen;
170 grub_uint8_t filetype;
171 grub_uint8_t namelen;
174 /* Information about a "mounted" ufs filesystem. */
175 struct grub_ufs_data
177 struct grub_ufs_sblock sblock;
178 grub_disk_t disk;
179 union
181 struct grub_ufs_inode inode;
182 struct grub_ufs2_inode inode2;
184 enum
186 UFS1,
187 UFS2,
188 UNKNOWN
189 } ufs_type;
190 int ino;
191 int linknest;
194 #ifndef GRUB_UTIL
195 static grub_dl_t my_mod;
196 #endif
198 /* Forward declaration. */
199 static grub_err_t grub_ufs_find_file (struct grub_ufs_data *data,
200 const char *path);
203 static int
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;
208 int log2_blksz;
210 /* Direct. */
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. */
220 if (blk < indirsz)
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];
227 blk -= indirsz;
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");
248 return 0;
252 /* Read LEN bytes from the file described by DATA starting with byte
253 POS. Return the amount of read bytes in READ. */
254 static grub_ssize_t
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;
261 int i;
262 int blockcnt;
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++)
272 int blknr;
273 int blockoff = pos % UFS_BLKSZ (sblock);
274 int blockend = UFS_BLKSZ (sblock);
276 int skipfirst = 0;
278 blknr = grub_ufs_get_file_block (data, i);
279 if (grub_errno)
280 return -1;
282 /* Last block. */
283 if (i == blockcnt - 1)
285 blockend = (len + pos) % UFS_BLKSZ (sblock);
287 if (!blockend)
288 blockend = UFS_BLKSZ (sblock);
291 /* First block. */
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. */
300 if (blknr)
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;
307 if (grub_errno)
308 return -1;
310 else
311 grub_memset (buf, UFS_BLKSZ (sblock) - skipfirst, 0);
313 buf += UFS_BLKSZ (sblock) - skipfirst;
316 return len;
320 /* Read inode INO from the mounted filesystem described by DATA. This
321 inode is used by default now. */
322 static grub_err_t
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)))
343 + grpino / 4,
344 (grpino % 4) * sizeof (struct grub_ufs_inode),
345 sizeof (struct grub_ufs_inode),
346 (char *) inode);
348 else
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)))
355 + grpino / 2,
356 (grpino % 2) * sizeof (struct grub_ufs2_inode),
357 sizeof (struct grub_ufs2_inode),
358 (char *) inode);
361 data->ino = ino;
362 return grub_errno;
366 /* Lookup the symlink the current inode points to. INO is the inode
367 number of the directory the symlink is relative to. */
368 static grub_err_t
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));
379 else
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))
394 return grub_errno;
396 grub_ufs_find_file (data, symlink);
397 if (grub_errno)
398 grub_error (grub_errno, "Can not follow symlink `%s'.", symlink);
400 return grub_errno;
404 /* Find the file with the pathname PATH on the filesystem described by
405 DATA. */
406 static grub_err_t
407 grub_ufs_find_file (struct grub_ufs_data *data, const char *path)
409 char fpath[grub_strlen (path) + 1];
410 char *name = fpath;
411 char *next;
412 unsigned int pos = 0;
413 int dirino;
415 grub_strcpy (fpath, path);
417 /* Skip the first slash. */
418 if (name[0] == '/')
420 name++;
421 if (!*name)
422 return 0;
425 /* Extract the actual part from the pathname. */
426 next = grub_strchr (name, '/');
427 if (next)
429 next[0] = '\0';
430 next++;
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)
442 return grub_errno;
445 char filename[dirent.namelen + 1];
447 if (grub_ufs_read_file (data, 0, pos + sizeof (dirent),
448 dirent.namelen, filename) < 0)
449 return grub_errno;
451 filename[dirent.namelen] = '\0';
453 if (!grub_strcmp (name, filename))
455 dirino = data->ino;
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);
461 if (grub_errno)
462 return grub_errno;
465 if (!next)
466 return 0;
468 pos = 0;
470 name = next;
471 next = grub_strchr (name, '/');
472 if (next)
474 next[0] = '\0';
475 next++;
478 if (!(dirent.filetype & GRUB_UFS_FILETYPE_DIR))
479 return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
481 continue;
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");
489 return grub_errno;
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));
501 if (!data)
502 return 0;
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);
510 if (grub_errno)
511 goto fail;
513 if (grub_le_to_cpu32 (data->sblock.magic) == GRUB_UFS_MAGIC)
515 data->ufs_type = UFS1;
516 break;
518 else if (grub_le_to_cpu32 (data->sblock.magic) == GRUB_UFS2_MAGIC)
520 data->ufs_type = UFS2;
521 break;
523 sblklist++;
525 if (data->ufs_type == UNKNOWN)
527 grub_error (GRUB_ERR_BAD_FS, "not an ufs filesystem");
528 goto fail;
531 data->disk = disk;
532 data->linknest = 0;
533 return data;
535 fail:
536 grub_free (data);
538 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
539 grub_error (GRUB_ERR_BAD_FS, "not a ufs filesystem");
541 return 0;
545 static grub_err_t
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);
554 if (!data)
555 return grub_errno;
557 grub_ufs_read_inode (data, GRUB_UFS_INODE);
558 if (grub_errno)
559 return grub_errno;
561 sblock = &data->sblock;
563 if (!path || path[0] != '/')
565 grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
566 return grub_errno;
569 grub_ufs_find_file (data, path);
570 if (grub_errno)
571 goto fail;
573 if (!(INODE_MODE (data) & GRUB_UFS_ATTR_DIR))
575 grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
576 goto fail;
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)
585 break;
588 char filename[dirent.namelen + 1];
590 if (grub_ufs_read_file (data, 0, pos + sizeof (dirent),
591 dirent.namelen, filename) < 0)
592 break;
594 filename[dirent.namelen] = '\0';
595 if (hook (filename, dirent.filetype == GRUB_UFS_FILETYPE_DIR))
596 break;
599 pos += grub_le_to_cpu16 (dirent.direntlen);
602 fail:
603 grub_free (data);
605 return grub_errno;
609 /* Open a file named NAME and initialize FILE. */
610 static grub_err_t
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);
615 if (!data)
616 return grub_errno;
618 grub_ufs_read_inode (data, 2);
619 if (grub_errno)
621 grub_free (data);
622 return grub_errno;
625 if (!name || name[0] != '/')
627 grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
628 return grub_errno;
631 grub_ufs_find_file (data, name);
632 if (grub_errno)
634 grub_free (data);
635 return grub_errno;
638 file->data = data;
639 file->size = INODE_SIZE (data);
641 return GRUB_ERR_NONE;
645 static grub_ssize_t
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);
655 static grub_err_t
656 grub_ufs_close (grub_file_t file)
658 grub_free (file->data);
660 return GRUB_ERR_NONE;
664 static grub_err_t
665 grub_ufs_label (grub_device_t device, char **label)
667 struct grub_ufs_data *data = 0;
669 #ifndef GRUB_UTIL
670 grub_dl_ref (my_mod);
671 #endif
673 *label = 0;
675 data = grub_ufs_mount (device->disk);
676 if (data)
678 if (data->ufs_type == UFS2)
679 *label = grub_strdup ((char *) data->sblock.volume_name);
682 #ifndef GRUB_UTIL
683 grub_dl_unref (my_mod);
684 #endif
686 grub_free (data);
688 return grub_errno;
692 static struct grub_fs grub_ufs_fs =
694 .name = "ufs",
695 .dir = grub_ufs_dir,
696 .open = grub_ufs_open,
697 .read = grub_ufs_read,
698 .close = grub_ufs_close,
699 .label = grub_ufs_label,
700 .next = 0
703 GRUB_MOD_INIT(ufs)
705 grub_fs_register (&grub_ufs_fs);
706 #ifndef GRUB_UTIL
707 my_mod = mod;
708 #endif
711 GRUB_MOD_FINI(ufs)
713 grub_fs_unregister (&grub_ufs_fs);