]> git.proxmox.com Git - grub2.git/blob - fs/ufs.c
2005-11-13 Marco Gerards <mgerards@xs4all.nl>
[grub2.git] / fs / ufs.c
1 /* ufs.c - Unix File System */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 *
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <grub/err.h>
22 #include <grub/file.h>
23 #include <grub/mm.h>
24 #include <grub/misc.h>
25 #include <grub/disk.h>
26 #include <grub/dl.h>
27 #include <grub/types.h>
28
29
30 #define GRUB_UFS_MAGIC 0x11954
31 #define GRUB_UFS2_MAGIC 0x19540119
32 #define GRUB_UFS_INODE 2
33 #define GRUB_UFS_FILETYPE_DIR 4
34 #define GRUB_UFS_FILETYPE_LNK 10
35 #define GRUB_UFS_MAX_SYMLNK_CNT 8
36
37 #define GRUB_UFS_DIRBLKS 12
38 #define GRUB_UFS_INDIRBLKS 3
39
40 #define GRUB_UFS_ATTR_DIR 040000
41
42 /* Calculate in which group the inode can be found. */
43 #define inode_group(inode,sblock) ()
44
45 #define UFS_BLKSZ(sblock) (grub_le_to_cpu32 (sblock->bsize))
46
47 #define INODE(data,field) (data->ufs_type == UFS1 ? \
48 data->inode. field : data->inode2. field)
49 #define INODE_ENDIAN(data,field,bits1,bits2) (data->ufs_type == UFS1 ? \
50 grub_le_to_cpu##bits1 (data->inode.field) : \
51 grub_le_to_cpu##bits2 (data->inode2.field))
52 #define INODE_SIZE(data) INODE_ENDIAN (data,size,32,64)
53 #define INODE_MODE(data) INODE_ENDIAN (data,mode,16,16)
54 #define INODE_BLKSZ(data) (data->ufs_type == UFS1 ? 32 : 64)
55 #define INODE_DIRBLOCKS(data,blk) INODE_ENDIAN \
56 (data,blocks.dir_blocks[blk],32,64)
57 #define INODE_INDIRBLOCKS(data,blk) INODE_ENDIAN \
58 (data,blocks.indir_blocks[blk],32,64)
59
60 /* The blocks on which the superblock can be found. */
61 static int sblocklist[] = { 128, 16, 0, 512, -1 };
62
63 struct grub_ufs_sblock
64 {
65 grub_uint8_t unused[16];
66 /* The offset of the inodes in the cylinder group. */
67 grub_uint32_t inoblk_offs;
68
69 grub_uint8_t unused2[4];
70
71 /* The start of the cylinder group. */
72 grub_uint32_t cylg_offset;
73
74 grub_uint8_t unused3[20];
75
76 /* The size of a block in bytes. */
77 grub_int32_t bsize;
78 grub_uint8_t unused4[48];
79
80 /* The size of filesystem blocks to disk blocks. */
81 grub_uint32_t log2_blksz;
82 grub_uint8_t unused5[80];
83
84 /* Inodes stored per cylinder group. */
85 grub_uint32_t ino_per_group;
86
87 /* The frags per cylinder group. */
88 grub_uint32_t frags_per_group;
89
90 grub_uint8_t unused7[1180];
91
92 /* Magic value to check if this is really a UFS filesystem. */
93 grub_uint32_t magic;
94 };
95
96 /* UFS inode. */
97 struct grub_ufs_inode
98 {
99 grub_uint16_t mode;
100 grub_uint16_t nlinks;
101 grub_uint16_t uid;
102 grub_uint16_t gid;
103 grub_int64_t size;
104 grub_uint64_t atime;
105 grub_uint64_t mtime;
106 grub_uint64_t ctime;
107 union
108 {
109 struct
110 {
111 grub_uint32_t dir_blocks[GRUB_UFS_DIRBLKS];
112 grub_uint32_t indir_blocks[GRUB_UFS_INDIRBLKS];
113 } blocks;
114 grub_uint8_t symlink[(GRUB_UFS_DIRBLKS + GRUB_UFS_INDIRBLKS) * 4];
115 };
116 grub_uint32_t flags;
117 grub_uint32_t nblocks;
118 grub_uint32_t gen;
119 grub_uint32_t unused;
120 grub_uint8_t pad[12];
121 };
122
123 /* UFS inode. */
124 struct grub_ufs2_inode
125 {
126 grub_uint16_t mode;
127 grub_uint16_t nlinks;
128 grub_uint32_t uid;
129 grub_uint32_t gid;
130 grub_uint32_t blocksize;
131 grub_int64_t size;
132 grub_int64_t nblocks;
133 grub_uint64_t atime;
134 grub_uint64_t mtime;
135 grub_uint64_t ctime;
136 grub_uint64_t create_time;
137 grub_uint32_t atime_sec;
138 grub_uint32_t mtime_sec;
139 grub_uint32_t ctime_sec;
140 grub_uint32_t create_time_sec;
141 grub_uint32_t gen;
142 grub_uint32_t kernel_flags;
143 grub_uint32_t flags;
144 grub_uint32_t extsz;
145 grub_uint64_t ext[2];
146 union
147 {
148 struct
149 {
150 grub_uint64_t dir_blocks[GRUB_UFS_DIRBLKS];
151 grub_uint64_t indir_blocks[GRUB_UFS_INDIRBLKS];
152 } blocks;
153 grub_uint8_t symlink[(GRUB_UFS_DIRBLKS + GRUB_UFS_INDIRBLKS) * 8];
154 };
155
156 grub_uint8_t unused[24];
157 };
158
159 /* Directory entry. */
160 struct grub_ufs_dirent
161 {
162 grub_uint32_t ino;
163 grub_uint16_t direntlen;
164 grub_uint8_t filetype;
165 grub_uint8_t namelen;
166 };
167
168 /* Information about a "mounted" ufs filesystem. */
169 struct grub_ufs_data
170 {
171 struct grub_ufs_sblock sblock;
172 grub_disk_t disk;
173 union
174 {
175 struct grub_ufs_inode inode;
176 struct grub_ufs2_inode inode2;
177 };
178 enum
179 {
180 UFS1,
181 UFS2,
182 UNKNOWN
183 } ufs_type;
184 int ino;
185 int linknest;
186 };
187
188 #ifndef GRUB_UTIL
189 static grub_dl_t my_mod;
190 #endif
191 \f
192 /* Forward declaration. */
193 static grub_err_t grub_ufs_find_file (struct grub_ufs_data *data,
194 const char *path);
195
196
197 static int
198 grub_ufs_get_file_block (struct grub_ufs_data *data, unsigned int blk)
199 {
200 struct grub_ufs_sblock *sblock = &data->sblock;
201 unsigned int indirsz;
202
203 /* Direct. */
204 if (blk < GRUB_UFS_DIRBLKS)
205 return INODE_DIRBLOCKS (data, blk);
206
207 blk -= GRUB_UFS_DIRBLKS;
208
209 indirsz = UFS_BLKSZ (sblock) / INODE_BLKSZ (data);
210 /* Single indirect block. */
211 if (blk < indirsz)
212 {
213 grub_uint32_t indir[UFS_BLKSZ (sblock)];
214 grub_disk_read (data->disk, INODE_INDIRBLOCKS (data, 0),
215 0, sizeof (indir), (char *) indir);
216 return indir[blk];
217 }
218 blk -= indirsz;
219
220 /* Double indirect block. */
221 if (blk < UFS_BLKSZ (sblock) / indirsz)
222 {
223 grub_uint32_t indir[UFS_BLKSZ (sblock)];
224
225 grub_disk_read (data->disk, INODE_INDIRBLOCKS (data, 1),
226 0, sizeof (indir), (char *) indir);
227 grub_disk_read (data->disk, indir[blk / indirsz],
228 0, sizeof (indir), (char *) indir);
229
230 return indir[blk % indirsz];
231 }
232
233
234 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
235 "ufs does not support tripple indirect blocks");
236 return 0;
237 }
238
239
240 /* Read LEN bytes from the file described by DATA starting with byte
241 POS. Return the amount of read bytes in READ. */
242 static grub_ssize_t
243 grub_ufs_read_file (struct grub_ufs_data *data,
244 void (*read_hook) (unsigned long sector,
245 unsigned offset, unsigned length),
246 int pos, unsigned int len, char *buf)
247 {
248 struct grub_ufs_sblock *sblock = &data->sblock;
249 int i;
250 int blockcnt;
251
252 /* Adjust len so it we can't read past the end of the file. */
253 if (len > INODE_SIZE (data))
254 len = INODE_SIZE (data);
255
256 blockcnt = (len + pos + UFS_BLKSZ (sblock) - 1) / UFS_BLKSZ (sblock);
257
258 for (i = pos / UFS_BLKSZ (sblock); i < blockcnt; i++)
259 {
260 int blknr;
261 int blockoff = pos % UFS_BLKSZ (sblock);
262 int blockend = UFS_BLKSZ (sblock);
263
264 int skipfirst = 0;
265
266 blknr = grub_ufs_get_file_block (data, i);
267 if (grub_errno)
268 return -1;
269
270 /* Last block. */
271 if (i == blockcnt - 1)
272 {
273 blockend = (len + pos) % UFS_BLKSZ (sblock);
274
275 if (!blockend)
276 blockend = UFS_BLKSZ (sblock);
277 }
278
279 /* First block. */
280 if (i == (pos / (int) UFS_BLKSZ (sblock)))
281 {
282 skipfirst = blockoff;
283 blockend -= skipfirst;
284 }
285
286 /* XXX: If the block number is 0 this block is not stored on
287 disk but is zero filled instead. */
288 if (blknr)
289 {
290 data->disk->read_hook = read_hook;
291 grub_disk_read (data->disk,
292 blknr << grub_le_to_cpu32 (data->sblock.log2_blksz),
293 skipfirst, blockend, buf);
294 data->disk->read_hook = 0;
295 if (grub_errno)
296 return -1;
297 }
298 else
299 grub_memset (buf, UFS_BLKSZ (sblock) - skipfirst, 0);
300
301 buf += UFS_BLKSZ (sblock) - skipfirst;
302 }
303
304 return len;
305 }
306
307
308 /* Read inode INO from the mounted filesystem described by DATA. This
309 inode is used by default now. */
310 static grub_err_t
311 grub_ufs_read_inode (struct grub_ufs_data *data, int ino)
312 {
313 struct grub_ufs_sblock *sblock = &data->sblock;
314
315 /* Determine the group the inode is in. */
316 int group = ino / grub_le_to_cpu32 (sblock->ino_per_group);
317
318 /* Determine the inode within the group. */
319 int grpino = ino % grub_le_to_cpu32 (sblock->ino_per_group);
320
321 /* The first block of the group. */
322 int grpblk = group * (grub_le_to_cpu32 (sblock->frags_per_group));
323
324 if (data->ufs_type == UFS1)
325 {
326 struct grub_ufs_inode *inode = &data->inode;
327
328 grub_disk_read (data->disk,
329 (((grub_le_to_cpu32 (sblock->inoblk_offs) + grpblk)
330 << grub_le_to_cpu32 (data->sblock.log2_blksz)))
331 + grpino / 4,
332 (grpino % 4) * sizeof (struct grub_ufs_inode),
333 sizeof (struct grub_ufs_inode),
334 (char *) inode);
335 }
336 else
337 {
338 struct grub_ufs2_inode *inode = &data->inode2;
339
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 / 2,
344 (grpino % 2) * sizeof (struct grub_ufs2_inode),
345 sizeof (struct grub_ufs2_inode),
346 (char *) inode);
347 }
348
349 data->ino = ino;
350 return grub_errno;
351 }
352
353
354 /* Lookup the symlink the current inode points to. INO is the inode
355 number of the directory the symlink is relative to. */
356 static grub_err_t
357 grub_ufs_lookup_symlink (struct grub_ufs_data *data, int ino)
358 {
359 char symlink[INODE_SIZE (data)];
360
361 if (++data->linknest > GRUB_UFS_MAX_SYMLNK_CNT)
362 return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks");
363
364 if (INODE_SIZE (data) < (GRUB_UFS_DIRBLKS + GRUB_UFS_INDIRBLKS
365 * INODE_BLKSZ (data)))
366 grub_strcpy (symlink, INODE (data, symlink));
367 else
368 {
369 grub_disk_read (data->disk,
370 (INODE_DIRBLOCKS (data, 0)
371 << grub_le_to_cpu32 (data->sblock.log2_blksz)),
372 0, INODE_SIZE (data), symlink);
373 symlink[INODE_SIZE (data)] = '\0';
374 }
375
376 /* The symlink is an absolute path, go back to the root inode. */
377 if (symlink[0] == '/')
378 ino = GRUB_UFS_INODE;
379
380 /* Now load in the old inode. */
381 if (grub_ufs_read_inode (data, ino))
382 return grub_errno;
383
384 grub_ufs_find_file (data, symlink);
385 if (grub_errno)
386 grub_error (grub_errno, "Can not follow symlink `%s'.", symlink);
387
388 return grub_errno;
389 }
390
391
392 /* Find the file with the pathname PATH on the filesystem described by
393 DATA. */
394 static grub_err_t
395 grub_ufs_find_file (struct grub_ufs_data *data, const char *path)
396 {
397 char fpath[grub_strlen (path)];
398 char *name = fpath;
399 char *next;
400 unsigned int pos = 0;
401 int dirino;
402
403 grub_strncpy (fpath, path, grub_strlen (path));
404
405 /* Skip the first slash. */
406 if (name[0] == '/')
407 {
408 name++;
409 if (!*name)
410 return 0;
411 }
412
413 /* Extract the actual part from the pathname. */
414 next = grub_strchr (name, '/');
415 if (next)
416 {
417 next[0] = '\0';
418 next++;
419 }
420
421 do
422 {
423 struct grub_ufs_dirent dirent;
424
425 if (grub_strlen (name) == 0)
426 return GRUB_ERR_NONE;
427
428 if (grub_ufs_read_file (data, 0, pos, sizeof (dirent),
429 (char *) &dirent) < 0)
430 return grub_errno;
431
432 {
433 char filename[dirent.namelen + 1];
434
435 if (grub_ufs_read_file (data, 0, pos + sizeof (dirent),
436 dirent.namelen, filename) < 0)
437 return grub_errno;
438
439 filename[dirent.namelen] = '\0';
440
441 if (!grub_strcmp (name, filename))
442 {
443 dirino = data->ino;
444 grub_ufs_read_inode (data, grub_le_to_cpu32 (dirent.ino));
445
446 if (dirent.filetype == GRUB_UFS_FILETYPE_LNK)
447 {
448 grub_ufs_lookup_symlink (data, dirino);
449 if (grub_errno)
450 return grub_errno;
451 }
452
453 if (!next)
454 return 0;
455
456 pos = 0;
457
458 name = next;
459 next = grub_strchr (name, '/');
460 if (next)
461 {
462 next[0] = '\0';
463 next++;
464 }
465
466 if (!(dirent.filetype & GRUB_UFS_FILETYPE_DIR))
467 return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
468
469 continue;
470 }
471 }
472
473 pos += grub_le_to_cpu16 (dirent.direntlen);
474 } while (pos < grub_le_to_cpu32 (INODE_SIZE (data)));
475
476 grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
477 return grub_errno;
478 }
479
480
481 /* Mount the filesystem on the disk DISK. */
482 static struct grub_ufs_data *
483 grub_ufs_mount (grub_disk_t disk)
484 {
485 struct grub_ufs_data *data;
486 int *sblklist = sblocklist;
487
488 data = grub_malloc (sizeof (struct grub_ufs_data));
489 if (!data)
490 return 0;
491
492 /* Find a UFS1 or UFS2 sblock. */
493 data->ufs_type = UNKNOWN;
494 while (*sblklist != -1)
495 {
496 grub_disk_read (disk, *sblklist, 0, sizeof (struct grub_ufs_sblock),
497 (char *) &data->sblock);
498 if (grub_errno)
499 goto fail;
500
501 if (grub_le_to_cpu32 (data->sblock.magic) == GRUB_UFS_MAGIC)
502 {
503 data->ufs_type = UFS1;
504 break;
505 }
506 else if (grub_le_to_cpu32 (data->sblock.magic) == GRUB_UFS2_MAGIC)
507 {
508 data->ufs_type = UFS2;
509 break;
510 }
511 sblklist++;
512 }
513 if (data->ufs_type == UNKNOWN)
514 {
515 grub_error (GRUB_ERR_BAD_FS, "not an ufs filesystem");
516 goto fail;
517 }
518
519 data->disk = disk;
520 data->linknest = 0;
521 return data;
522
523 fail:
524 grub_free (data);
525
526 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
527 grub_error (GRUB_ERR_BAD_FS, "not a ufs filesystem");
528
529 return 0;
530 }
531
532
533 static grub_err_t
534 grub_ufs_dir (grub_device_t device, const char *path,
535 int (*hook) (const char *filename, int dir))
536 {
537 struct grub_ufs_data *data;
538 struct grub_ufs_sblock *sblock;
539 unsigned int pos = 0;
540
541 data = grub_ufs_mount (device->disk);
542 if (!data)
543 return grub_errno;
544
545 grub_ufs_read_inode (data, GRUB_UFS_INODE);
546 if (grub_errno)
547 return grub_errno;
548
549 sblock = &data->sblock;
550
551 if (!path || path[0] != '/')
552 {
553 grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
554 return grub_errno;
555 }
556
557 grub_ufs_find_file (data, path);
558 if (grub_errno)
559 goto fail;
560
561 if (!(INODE_MODE (data) & GRUB_UFS_ATTR_DIR))
562 {
563 grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
564 goto fail;
565 }
566
567 while (pos < INODE_SIZE (data))
568 {
569 struct grub_ufs_dirent dirent;
570
571 if (grub_ufs_read_file (data, 0, pos, sizeof (dirent),
572 (char *) &dirent) < 0)
573 break;
574
575 {
576 char filename[dirent.namelen + 1];
577
578 if (grub_ufs_read_file (data, 0, pos + sizeof (dirent),
579 dirent.namelen, filename) < 0)
580 break;
581
582 filename[dirent.namelen] = '\0';
583 if (hook (filename, dirent.filetype == GRUB_UFS_FILETYPE_DIR))
584 break;
585 }
586
587 pos += grub_le_to_cpu16 (dirent.direntlen);
588 }
589
590 fail:
591 grub_free (data);
592
593 return grub_errno;
594 }
595
596
597 /* Open a file named NAME and initialize FILE. */
598 static grub_err_t
599 grub_ufs_open (struct grub_file *file, const char *name)
600 {
601 struct grub_ufs_data *data;
602 data = grub_ufs_mount (file->device->disk);
603 if (!data)
604 return grub_errno;
605
606 grub_ufs_read_inode (data, 2);
607 if (grub_errno)
608 {
609 grub_free (data);
610 return grub_errno;
611 }
612
613 if (!name || name[0] != '/')
614 {
615 grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
616 return grub_errno;
617 }
618
619 grub_ufs_find_file (data, name);
620 if (grub_errno)
621 {
622 grub_free (data);
623 return grub_errno;
624 }
625
626 file->data = data;
627 file->size = INODE_SIZE (data);
628
629 return GRUB_ERR_NONE;
630 }
631
632
633 static grub_ssize_t
634 grub_ufs_read (grub_file_t file, char *buf, grub_ssize_t len)
635 {
636 struct grub_ufs_data *data =
637 (struct grub_ufs_data *) file->data;
638
639 return grub_ufs_read_file (data, file->read_hook, file->offset, len, buf);
640 }
641
642
643 static grub_err_t
644 grub_ufs_close (grub_file_t file)
645 {
646 grub_free (file->data);
647
648 return GRUB_ERR_NONE;
649 }
650
651
652 static grub_err_t
653 grub_ufs_label (grub_device_t device __attribute ((unused)),
654 char **label __attribute ((unused)))
655 {
656 return GRUB_ERR_NONE;
657 }
658
659 \f
660 static struct grub_fs grub_ufs_fs =
661 {
662 .name = "ufs",
663 .dir = grub_ufs_dir,
664 .open = grub_ufs_open,
665 .read = grub_ufs_read,
666 .close = grub_ufs_close,
667 .label = grub_ufs_label,
668 .next = 0
669 };
670
671 GRUB_MOD_INIT(ufs)
672 {
673 grub_fs_register (&grub_ufs_fs);
674 #ifndef GRUB_UTIL
675 my_mod = mod;
676 #endif
677 }
678
679 GRUB_MOD_FINI(ufs)
680 {
681 grub_fs_unregister (&grub_ufs_fs);
682 }
683