1 /* minix.c - The minix filesystem, version 1 and 2. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2004,2005,2006,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_MINIX_MAGIC 0x4D5A
30 #elif defined(MODE_MINIX2)
31 #define GRUB_MINIX_MAGIC 0x2468
32 #define GRUB_MINIX_MAGIC_30 0x2478
34 #define GRUB_MINIX_MAGIC 0x137F
35 #define GRUB_MINIX_MAGIC_30 0x138F
38 #define GRUB_MINIX_INODE_DIR_BLOCKS 7
39 #define GRUB_MINIX_LOG2_BSIZE 1
40 #define GRUB_MINIX_ROOT_INODE 1
41 #define GRUB_MINIX_MAX_SYMLNK_CNT 8
42 #define GRUB_MINIX_SBLOCK 2
44 #define GRUB_MINIX_IFDIR 0040000U
45 #define GRUB_MINIX_IFLNK 0120000U
47 #if defined(MODE_MINIX2) || defined(MODE_MINIX3)
48 typedef grub_uint32_t grub_minix_uintn_t
;
49 #define grub_minix_le_to_cpu_n grub_le_to_cpu32
51 typedef grub_uint16_t grub_minix_uintn_t
;
52 #define grub_minix_le_to_cpu_n grub_le_to_cpu16
55 #define GRUB_MINIX_INODE_BLKSZ(data) sizeof (grub_minix_uintn_t)
57 typedef grub_uint32_t grub_minix_ino_t
;
58 #define grub_minix_le_to_cpu_ino grub_le_to_cpu32
60 typedef grub_uint16_t grub_minix_ino_t
;
61 #define grub_minix_le_to_cpu_ino grub_le_to_cpu16
64 #define GRUB_MINIX_INODE_SIZE(data) (grub_minix_le_to_cpu_n (data->inode.size))
65 #define GRUB_MINIX_INODE_MODE(data) (grub_le_to_cpu16 (data->inode.mode))
66 #define GRUB_MINIX_INODE_DIR_ZONES(data,blk) (grub_minix_le_to_cpu_n \
67 (data->inode.dir_zones[blk]))
68 #define GRUB_MINIX_INODE_INDIR_ZONE(data) (grub_minix_le_to_cpu_n \
69 (data->inode.indir_zone))
70 #define GRUB_MINIX_INODE_DINDIR_ZONE(data) (grub_minix_le_to_cpu_n \
71 (data->inode.double_indir_zone))
73 #define GRUB_MINIX_LOG2_ZONESZ (GRUB_MINIX_LOG2_BSIZE \
74 + grub_le_to_cpu16 (sblock->log2_zone_size))
75 #define GRUB_MINIX_ZONESZ (data->block_size \
76 << grub_le_to_cpu16 (sblock->log2_zone_size))
79 struct grub_minix_sblock
81 grub_uint32_t inode_cnt
;
82 grub_uint16_t zone_cnt
;
83 grub_uint16_t inode_bmap_size
;
84 grub_uint16_t zone_bmap_size
;
85 grub_uint16_t first_data_zone
;
86 grub_uint16_t log2_zone_size
;
88 grub_uint32_t max_file_size
;
93 grub_uint16_t block_size
;
94 grub_uint8_t disk_version
;
97 struct grub_minix_sblock
99 grub_uint16_t inode_cnt
;
100 grub_uint16_t zone_cnt
;
101 grub_uint16_t inode_bmap_size
;
102 grub_uint16_t zone_bmap_size
;
103 grub_uint16_t first_data_zone
;
104 grub_uint16_t log2_zone_size
;
105 grub_uint32_t max_file_size
;
110 #if defined(MODE_MINIX3)
112 struct grub_minix_inode
115 grub_uint16_t nlinks
;
123 grub_uint32_t dir_zones
[7];
124 grub_uint32_t indir_zone
;
125 grub_uint32_t double_indir_zone
;
126 grub_uint32_t unused
;
129 #elif defined(MODE_MINIX2)
130 struct grub_minix_inode
138 grub_uint16_t dir_zones
[7];
139 grub_uint16_t indir_zone
;
140 grub_uint16_t double_indir_zone
;
145 struct grub_minix_inode
148 grub_uint16_t nlinks
;
155 grub_uint32_t dir_zones
[7];
156 grub_uint32_t indir_zone
;
157 grub_uint32_t double_indir_zone
;
158 grub_uint32_t unused
;
164 /* Information about a "mounted" minix filesystem. */
165 struct grub_minix_data
167 struct grub_minix_sblock sblock
;
168 struct grub_minix_inode inode
;
173 grub_size_t block_size
;
176 static grub_dl_t my_mod
;
178 static grub_err_t
grub_minix_find_file (struct grub_minix_data
*data
,
182 grub_minix_get_file_block (struct grub_minix_data
*data
, unsigned int blk
)
184 struct grub_minix_sblock
*sblock
= &data
->sblock
;
187 auto int grub_get_indir (int, int);
189 /* Read the block pointer in ZONE, on the offset NUM. */
190 int grub_get_indir (int zone
, int num
)
192 grub_minix_uintn_t indirn
;
193 grub_disk_read (data
->disk
,
195 zone
* (data
->block_size
/ GRUB_DISK_SECTOR_SIZE
),
197 zone
<< GRUB_MINIX_LOG2_ZONESZ
,
199 sizeof (grub_minix_uintn_t
) * num
,
200 sizeof (grub_minix_uintn_t
), (char *) &indirn
);
201 return grub_minix_le_to_cpu_n (indirn
);
205 if (blk
< GRUB_MINIX_INODE_DIR_BLOCKS
)
206 return GRUB_MINIX_INODE_DIR_ZONES (data
, blk
);
208 /* Indirect block. */
209 blk
-= GRUB_MINIX_INODE_DIR_BLOCKS
;
210 if (blk
< GRUB_MINIX_ZONESZ
/ GRUB_MINIX_INODE_BLKSZ (data
))
212 indir
= grub_get_indir (GRUB_MINIX_INODE_INDIR_ZONE (data
), blk
);
216 /* Double indirect block. */
217 blk
-= GRUB_MINIX_ZONESZ
/ GRUB_MINIX_INODE_BLKSZ (data
);
218 if (blk
< (GRUB_MINIX_ZONESZ
/ GRUB_MINIX_INODE_BLKSZ (data
))
219 * (GRUB_MINIX_ZONESZ
/ GRUB_MINIX_INODE_BLKSZ (data
)))
221 indir
= grub_get_indir (GRUB_MINIX_INODE_DINDIR_ZONE (data
),
222 blk
/ GRUB_MINIX_ZONESZ
);
224 indir
= grub_get_indir (indir
, blk
% GRUB_MINIX_ZONESZ
);
229 /* This should never happen. */
230 grub_error (GRUB_ERR_OUT_OF_RANGE
, "file bigger than maximum size");
236 /* Read LEN bytes from the file described by DATA starting with byte
237 POS. Return the amount of read bytes in READ. */
239 grub_minix_read_file (struct grub_minix_data
*data
,
240 void NESTED_FUNC_ATTR (*read_hook
) (grub_disk_addr_t sector
,
241 unsigned offset
, unsigned length
),
242 grub_off_t pos
, grub_disk_addr_t len
, char *buf
)
244 struct grub_minix_sblock
*sblock
= &data
->sblock
;
246 grub_disk_addr_t blockcnt
;
247 grub_uint64_t posblock
;
248 grub_uint32_t blockoff
;
250 /* Adjust len so it we can't read past the end of the file. */
251 if (len
+ pos
> GRUB_MINIX_INODE_SIZE (data
))
252 len
= GRUB_MINIX_INODE_SIZE (data
) - pos
;
254 blockcnt
= grub_divmod64 ((len
+ pos
+ data
->block_size
- 1),
255 data
->block_size
, 0);
256 posblock
= grub_divmod64 (pos
, data
->block_size
, &blockoff
);
258 for (i
= posblock
; i
< blockcnt
; i
++)
260 grub_disk_addr_t blknr
;
261 grub_uint32_t blockend
= data
->block_size
;
262 grub_off_t skipfirst
= 0;
264 blknr
= grub_minix_get_file_block (data
, i
);
269 if (i
== blockcnt
- 1)
271 grub_divmod64 (len
+ pos
, data
->block_size
, &blockend
);
274 blockend
= data
->block_size
;
280 skipfirst
= blockoff
;
281 blockend
-= skipfirst
;
284 data
->disk
->read_hook
= read_hook
;
285 grub_disk_read (data
->disk
,
287 blknr
* (sblock
->block_size
/ GRUB_DISK_SECTOR_SIZE
),
289 blknr
<< GRUB_MINIX_LOG2_ZONESZ
,
291 skipfirst
, blockend
, buf
);
292 data
->disk
->read_hook
= 0;
296 buf
+= data
->block_size
- skipfirst
;
303 /* Read inode INO from the mounted filesystem described by DATA. This
304 inode is used by default now. */
306 grub_minix_read_inode (struct grub_minix_data
*data
, int ino
)
308 struct grub_minix_sblock
*sblock
= &data
->sblock
;
310 /* Block in which the inode is stored. */
311 grub_disk_addr_t block
;
314 /* The first inode in minix is inode 1. */
316 block
= (2 + grub_le_to_cpu16 (sblock
->inode_bmap_size
)
317 + grub_le_to_cpu16 (sblock
->zone_bmap_size
));
319 block
<<= GRUB_MINIX_LOG2_BSIZE
;
321 block
*= sblock
->block_size
/ GRUB_DISK_SECTOR_SIZE
;
323 block
+= ino
/ (GRUB_DISK_SECTOR_SIZE
/ sizeof (struct grub_minix_inode
));
324 int offs
= (ino
% (GRUB_DISK_SECTOR_SIZE
325 / sizeof (struct grub_minix_inode
))
326 * sizeof (struct grub_minix_inode
));
328 grub_disk_read (data
->disk
, block
, offs
,
329 sizeof (struct grub_minix_inode
), &data
->inode
);
331 return GRUB_ERR_NONE
;
335 /* Lookup the symlink the current inode points to. INO is the inode
336 number of the directory the symlink is relative to. */
338 grub_minix_lookup_symlink (struct grub_minix_data
*data
, int ino
)
340 char symlink
[GRUB_MINIX_INODE_SIZE (data
) + 1];
342 if (++data
->linknest
> GRUB_MINIX_MAX_SYMLNK_CNT
)
343 return grub_error (GRUB_ERR_SYMLINK_LOOP
, "too deep nesting of symlinks");
345 if (grub_minix_read_file (data
, 0, 0,
346 GRUB_MINIX_INODE_SIZE (data
), symlink
) < 0)
349 symlink
[GRUB_MINIX_INODE_SIZE (data
)] = '\0';
351 /* The symlink is an absolute path, go back to the root inode. */
352 if (symlink
[0] == '/')
353 ino
= GRUB_MINIX_ROOT_INODE
;
355 /* Now load in the old inode. */
356 if (grub_minix_read_inode (data
, ino
))
359 grub_minix_find_file (data
, symlink
);
361 grub_error (grub_errno
, "cannot follow symlink `%s'", symlink
);
367 /* Find the file with the pathname PATH on the filesystem described by
370 grub_minix_find_file (struct grub_minix_data
*data
, const char *path
)
372 char fpath
[grub_strlen (path
) + 1];
375 unsigned int pos
= 0;
378 grub_strcpy (fpath
, path
);
380 /* Skip the first slash. */
388 /* Extract the actual part from the pathname. */
389 next
= grub_strchr (name
, '/');
398 grub_minix_ino_t ino
;
399 char filename
[data
->filename_size
+ 1];
401 if (grub_strlen (name
) == 0)
402 return GRUB_ERR_NONE
;
404 if (grub_minix_read_file (data
, 0, pos
, sizeof (ino
),
407 if (grub_minix_read_file (data
, 0, pos
+ sizeof (ino
),
408 data
->filename_size
, (char *) filename
)< 0)
411 filename
[data
->filename_size
] = '\0';
413 /* Check if the current direntry matches the current part of the
415 if (!grub_strcmp (name
, filename
))
418 grub_minix_read_inode (data
, grub_minix_le_to_cpu_ino (ino
));
420 /* Follow the symlink. */
421 if ((GRUB_MINIX_INODE_MODE (data
)
422 & GRUB_MINIX_IFLNK
) == GRUB_MINIX_IFLNK
)
424 grub_minix_lookup_symlink (data
, dirino
);
435 next
= grub_strchr (name
, '/');
442 if ((GRUB_MINIX_INODE_MODE (data
)
443 & GRUB_MINIX_IFDIR
) != GRUB_MINIX_IFDIR
)
444 return grub_error (GRUB_ERR_BAD_FILE_TYPE
, "not a directory");
449 pos
+= sizeof (ino
) + data
->filename_size
;
450 } while (pos
< GRUB_MINIX_INODE_SIZE (data
));
452 grub_error (GRUB_ERR_FILE_NOT_FOUND
, "file not found");
457 /* Mount the filesystem on the disk DISK. */
458 static struct grub_minix_data
*
459 grub_minix_mount (grub_disk_t disk
)
461 struct grub_minix_data
*data
;
463 data
= grub_malloc (sizeof (struct grub_minix_data
));
467 /* Read the superblock. */
468 grub_disk_read (disk
, GRUB_MINIX_SBLOCK
, 0,
469 sizeof (struct grub_minix_sblock
),&data
->sblock
);
473 if (grub_le_to_cpu16 (data
->sblock
.magic
) == GRUB_MINIX_MAGIC
)
475 #if !defined(MODE_MINIX3)
476 data
->filename_size
= 14;
478 data
->filename_size
= 60;
481 #if !defined(MODE_MINIX3)
482 else if (grub_le_to_cpu16 (data
->sblock
.magic
) == GRUB_MINIX_MAGIC_30
)
483 data
->filename_size
= 30;
491 data
->block_size
= grub_le_to_cpu16 (data
->sblock
.block_size
);
493 data
->block_size
= 1024U;
500 #if defined(MODE_MINIX3)
501 grub_error (GRUB_ERR_BAD_FS
, "not a minix3 filesystem");
502 #elif defined(MODE_MINIX2)
503 grub_error (GRUB_ERR_BAD_FS
, "not a minix2 filesystem");
505 grub_error (GRUB_ERR_BAD_FS
, "not a minix filesystem");
511 grub_minix_dir (grub_device_t device
, const char *path
,
512 int (*hook
) (const char *filename
,
513 const struct grub_dirhook_info
*info
))
515 struct grub_minix_data
*data
= 0;
516 unsigned int pos
= 0;
518 data
= grub_minix_mount (device
->disk
);
522 grub_minix_read_inode (data
, GRUB_MINIX_ROOT_INODE
);
526 grub_minix_find_file (data
, path
);
530 if ((GRUB_MINIX_INODE_MODE (data
) & GRUB_MINIX_IFDIR
) != GRUB_MINIX_IFDIR
)
532 grub_error (GRUB_ERR_BAD_FILE_TYPE
, "not a directory");
536 while (pos
< GRUB_MINIX_INODE_SIZE (data
))
538 grub_minix_ino_t ino
;
539 char filename
[data
->filename_size
+ 1];
540 int dirino
= data
->ino
;
541 struct grub_dirhook_info info
;
542 grub_memset (&info
, 0, sizeof (info
));
545 if (grub_minix_read_file (data
, 0, pos
, sizeof (ino
),
549 if (grub_minix_read_file (data
, 0, pos
+ sizeof (ino
),
551 (char *) filename
) < 0)
553 filename
[data
->filename_size
] = '\0';
556 pos
+= sizeof (ino
) + data
->filename_size
;
560 grub_minix_read_inode (data
, grub_minix_le_to_cpu_ino (ino
));
561 info
.dir
= ((GRUB_MINIX_INODE_MODE (data
)
562 & GRUB_MINIX_IFDIR
) == GRUB_MINIX_IFDIR
);
563 if (hook (filename
, &info
) ? 1 : 0)
566 /* Load the old inode back in. */
567 grub_minix_read_inode (data
, dirino
);
569 pos
+= sizeof (ino
) + data
->filename_size
;
578 /* Open a file named NAME and initialize FILE. */
580 grub_minix_open (struct grub_file
*file
, const char *name
)
582 struct grub_minix_data
*data
;
583 data
= grub_minix_mount (file
->device
->disk
);
587 /* Open the inode op the root directory. */
588 grub_minix_read_inode (data
, GRUB_MINIX_ROOT_INODE
);
595 if (!name
|| name
[0] != '/')
597 grub_error (GRUB_ERR_BAD_FILENAME
, "bad filename");
601 /* Traverse the directory tree to the node that should be
603 grub_minix_find_file (data
, name
);
611 file
->size
= GRUB_MINIX_INODE_SIZE (data
);
613 return GRUB_ERR_NONE
;
618 grub_minix_read (grub_file_t file
, char *buf
, grub_size_t len
)
620 struct grub_minix_data
*data
=
621 (struct grub_minix_data
*) file
->data
;
623 return grub_minix_read_file (data
, file
->read_hook
, file
->offset
, len
, buf
);
628 grub_minix_close (grub_file_t file
)
630 grub_free (file
->data
);
632 return GRUB_ERR_NONE
;
637 static struct grub_fs grub_minix_fs
=
639 #if defined(MODE_MINIX3)
641 #elif defined(MODE_MINIX2)
646 .dir
= grub_minix_dir
,
647 .open
= grub_minix_open
,
648 .read
= grub_minix_read
,
649 .close
= grub_minix_close
,
653 #if defined(MODE_MINIX3)
654 GRUB_MOD_INIT(minix3
)
655 #elif defined(MODE_MINIX2)
656 GRUB_MOD_INIT(minix2
)
661 grub_fs_register (&grub_minix_fs
);
665 #if defined(MODE_MINIX3)
666 GRUB_MOD_FINI(minix3
)
667 #elif defined(MODE_MINIX2)
668 GRUB_MOD_FINI(minix2
)
673 grub_fs_unregister (&grub_minix_fs
);