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>
28 GRUB_MOD_LICENSE ("GPLv3+");
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
37 #define GRUB_MINIX_BSIZE 1024U
38 #define GRUB_MINIX_LOG2_BSIZE 1
39 #define GRUB_MINIX_ROOT_INODE 1
40 #define GRUB_MINIX_MAX_SYMLNK_CNT 8
41 #define GRUB_MINIX_SBLOCK 2
43 #define GRUB_MINIX_IFDIR 0040000U
44 #define GRUB_MINIX_IFLNK 0120000U
47 typedef grub_uint32_t grub_minix_uintn_t
;
48 #define grub_minix_le_to_cpu_n grub_le_to_cpu32
50 typedef grub_uint16_t grub_minix_uintn_t
;
51 #define grub_minix_le_to_cpu_n grub_le_to_cpu16
54 #define GRUB_MINIX_INODE_BLKSZ(data) sizeof (grub_minix_uintn_t)
56 #define GRUB_MINIX_INODE_SIZE(data) (grub_minix_le_to_cpu_n (data->inode.size))
57 #define GRUB_MINIX_INODE_MODE(data) (grub_le_to_cpu16 (data->inode.mode))
58 #define GRUB_MINIX_INODE_DIR_ZONES(data,blk) (grub_minix_le_to_cpu_n \
59 (data->inode.dir_zones[blk]))
60 #define GRUB_MINIX_INODE_INDIR_ZONE(data) (grub_minix_le_to_cpu_n \
61 (data->inode.indir_zone))
62 #define GRUB_MINIX_INODE_DINDIR_ZONE(data) (grub_minix_le_to_cpu_n \
63 (data->inode.double_indir_zone))
65 #define GRUB_MINIX_LOG2_ZONESZ (GRUB_MINIX_LOG2_BSIZE \
66 + grub_le_to_cpu16 (sblock->log2_zone_size))
67 #define GRUB_MINIX_ZONESZ (GRUB_MINIX_BSIZE \
68 << grub_le_to_cpu16 (sblock->log2_zone_size))
70 struct grub_minix_sblock
72 grub_uint16_t inode_cnt
;
73 grub_uint16_t zone_cnt
;
74 grub_uint16_t inode_bmap_size
;
75 grub_uint16_t zone_bmap_size
;
76 grub_uint16_t first_data_zone
;
77 grub_uint16_t log2_zone_size
;
78 grub_uint32_t max_file_size
;
83 struct grub_minix_inode
91 grub_uint16_t dir_zones
[7];
92 grub_uint16_t indir_zone
;
93 grub_uint16_t double_indir_zone
;
98 struct grub_minix_inode
101 grub_uint16_t nlinks
;
108 grub_uint32_t dir_zones
[7];
109 grub_uint32_t indir_zone
;
110 grub_uint32_t double_indir_zone
;
111 grub_uint32_t unused
;
117 /* Information about a "mounted" minix filesystem. */
118 struct grub_minix_data
120 struct grub_minix_sblock sblock
;
121 struct grub_minix_inode inode
;
128 static grub_dl_t my_mod
;
130 static grub_err_t
grub_minix_find_file (struct grub_minix_data
*data
,
134 grub_minix_get_file_block (struct grub_minix_data
*data
, unsigned int blk
)
136 struct grub_minix_sblock
*sblock
= &data
->sblock
;
139 auto int grub_get_indir (int, int);
141 /* Read the block pointer in ZONE, on the offset NUM. */
142 int grub_get_indir (int zone
, int num
)
144 grub_minix_uintn_t indirn
;
145 grub_disk_read (data
->disk
,
146 zone
<< GRUB_MINIX_LOG2_ZONESZ
,
147 sizeof (grub_minix_uintn_t
) * num
,
148 sizeof (grub_minix_uintn_t
), (char *) &indirn
);
149 return grub_minix_le_to_cpu_n (indirn
);
154 return GRUB_MINIX_INODE_DIR_ZONES (data
, blk
);
156 /* Indirect block. */
158 if (blk
< GRUB_MINIX_ZONESZ
/ GRUB_MINIX_INODE_BLKSZ (data
))
160 indir
= grub_get_indir (GRUB_MINIX_INODE_INDIR_ZONE (data
), blk
);
164 /* Double indirect block. */
165 blk
-= GRUB_MINIX_ZONESZ
/ GRUB_MINIX_INODE_BLKSZ (data
);
166 if (blk
< (GRUB_MINIX_ZONESZ
/ GRUB_MINIX_INODE_BLKSZ (data
))
167 * (GRUB_MINIX_ZONESZ
/ GRUB_MINIX_INODE_BLKSZ (data
)))
169 indir
= grub_get_indir (GRUB_MINIX_INODE_DINDIR_ZONE (data
),
170 blk
/ GRUB_MINIX_ZONESZ
);
172 indir
= grub_get_indir (indir
, blk
% GRUB_MINIX_ZONESZ
);
177 /* This should never happen. */
178 grub_error (GRUB_ERR_OUT_OF_RANGE
, "file bigger than maximum size");
184 /* Read LEN bytes from the file described by DATA starting with byte
185 POS. Return the amount of read bytes in READ. */
187 grub_minix_read_file (struct grub_minix_data
*data
,
188 void NESTED_FUNC_ATTR (*read_hook
) (grub_disk_addr_t sector
,
189 unsigned offset
, unsigned length
),
190 int pos
, grub_disk_addr_t len
, char *buf
)
192 struct grub_minix_sblock
*sblock
= &data
->sblock
;
196 /* Adjust len so it we can't read past the end of the file. */
197 if (len
+ pos
> GRUB_MINIX_INODE_SIZE (data
))
198 len
= GRUB_MINIX_INODE_SIZE (data
) - pos
;
200 blockcnt
= (len
+ pos
+ GRUB_MINIX_BSIZE
- 1) / GRUB_MINIX_BSIZE
;
202 for (i
= pos
/ GRUB_MINIX_BSIZE
; i
< blockcnt
; i
++)
205 int blockoff
= pos
% GRUB_MINIX_BSIZE
;
206 int blockend
= GRUB_MINIX_BSIZE
;
210 blknr
= grub_minix_get_file_block (data
, i
);
215 if (i
== blockcnt
- 1)
217 blockend
= (len
+ pos
) % GRUB_MINIX_BSIZE
;
220 blockend
= GRUB_MINIX_BSIZE
;
224 if (i
== (pos
/ (int) GRUB_MINIX_BSIZE
))
226 skipfirst
= blockoff
;
227 blockend
-= skipfirst
;
230 data
->disk
->read_hook
= read_hook
;
231 grub_disk_read (data
->disk
, blknr
<< GRUB_MINIX_LOG2_ZONESZ
,
232 skipfirst
, blockend
, buf
);
234 data
->disk
->read_hook
= 0;
238 buf
+= GRUB_MINIX_BSIZE
- skipfirst
;
245 /* Read inode INO from the mounted filesystem described by DATA. This
246 inode is used by default now. */
248 grub_minix_read_inode (struct grub_minix_data
*data
, int ino
)
250 struct grub_minix_sblock
*sblock
= &data
->sblock
;
252 /* Block in which the inode is stored. */
256 /* The first inode in minix is inode 1. */
259 block
= ((2 + grub_le_to_cpu16 (sblock
->inode_bmap_size
)
260 + grub_le_to_cpu16 (sblock
->zone_bmap_size
))
261 << GRUB_MINIX_LOG2_BSIZE
);
263 block
+= ino
/ (GRUB_DISK_SECTOR_SIZE
/ sizeof (struct grub_minix_inode
));
264 int offs
= (ino
% (GRUB_DISK_SECTOR_SIZE
265 / sizeof (struct grub_minix_inode
))
266 * sizeof (struct grub_minix_inode
));
268 grub_disk_read (data
->disk
, block
, offs
,
269 sizeof (struct grub_minix_inode
), &data
->inode
);
271 return GRUB_ERR_NONE
;
275 /* Lookup the symlink the current inode points to. INO is the inode
276 number of the directory the symlink is relative to. */
278 grub_minix_lookup_symlink (struct grub_minix_data
*data
, int ino
)
280 char symlink
[GRUB_MINIX_INODE_SIZE (data
) + 1];
282 if (++data
->linknest
> GRUB_MINIX_MAX_SYMLNK_CNT
)
283 return grub_error (GRUB_ERR_SYMLINK_LOOP
, "too deep nesting of symlinks");
285 if (grub_minix_read_file (data
, 0, 0,
286 GRUB_MINIX_INODE_SIZE (data
), symlink
) < 0)
289 symlink
[GRUB_MINIX_INODE_SIZE (data
)] = '\0';
291 /* The symlink is an absolute path, go back to the root inode. */
292 if (symlink
[0] == '/')
293 ino
= GRUB_MINIX_ROOT_INODE
;
295 /* Now load in the old inode. */
296 if (grub_minix_read_inode (data
, ino
))
299 grub_minix_find_file (data
, symlink
);
301 grub_error (grub_errno
, "cannot follow symlink `%s'", symlink
);
307 /* Find the file with the pathname PATH on the filesystem described by
310 grub_minix_find_file (struct grub_minix_data
*data
, const char *path
)
312 char fpath
[grub_strlen (path
) + 1];
315 unsigned int pos
= 0;
318 grub_strcpy (fpath
, path
);
320 /* Skip the first slash. */
328 /* Extract the actual part from the pathname. */
329 next
= grub_strchr (name
, '/');
339 char filename
[data
->filename_size
+ 1];
341 if (grub_strlen (name
) == 0)
342 return GRUB_ERR_NONE
;
344 if (grub_minix_read_file (data
, 0, pos
, sizeof (ino
),
347 if (grub_minix_read_file (data
, 0, pos
+ sizeof (ino
),
348 data
->filename_size
, (char *) filename
)< 0)
351 filename
[data
->filename_size
] = '\0';
353 /* Check if the current direntry matches the current part of the
355 if (!grub_strcmp (name
, filename
))
358 grub_minix_read_inode (data
, grub_le_to_cpu16 (ino
));
360 /* Follow the symlink. */
361 if ((GRUB_MINIX_INODE_MODE (data
)
362 & GRUB_MINIX_IFLNK
) == GRUB_MINIX_IFLNK
)
364 grub_minix_lookup_symlink (data
, dirino
);
375 next
= grub_strchr (name
, '/');
382 if ((GRUB_MINIX_INODE_MODE (data
)
383 & GRUB_MINIX_IFDIR
) != GRUB_MINIX_IFDIR
)
384 return grub_error (GRUB_ERR_BAD_FILE_TYPE
, "not a directory");
389 pos
+= sizeof (ino
) + data
->filename_size
;
390 } while (pos
< GRUB_MINIX_INODE_SIZE (data
));
392 grub_error (GRUB_ERR_FILE_NOT_FOUND
, "file not found");
397 /* Mount the filesystem on the disk DISK. */
398 static struct grub_minix_data
*
399 grub_minix_mount (grub_disk_t disk
)
401 struct grub_minix_data
*data
;
403 data
= grub_malloc (sizeof (struct grub_minix_data
));
407 /* Read the superblock. */
408 grub_disk_read (disk
, GRUB_MINIX_SBLOCK
, 0,
409 sizeof (struct grub_minix_sblock
),&data
->sblock
);
413 if (grub_le_to_cpu16 (data
->sblock
.magic
) == GRUB_MINIX_MAGIC
)
414 data
->filename_size
= 14;
415 else if (grub_le_to_cpu16 (data
->sblock
.magic
) == GRUB_MINIX_MAGIC_30
)
416 data
->filename_size
= 30;
428 grub_error (GRUB_ERR_BAD_FS
, "not a minix2 filesystem");
430 grub_error (GRUB_ERR_BAD_FS
, "not a minix filesystem");
436 grub_minix_dir (grub_device_t device
, const char *path
,
437 int (*hook
) (const char *filename
,
438 const struct grub_dirhook_info
*info
))
440 struct grub_minix_data
*data
= 0;
441 unsigned int pos
= 0;
443 data
= grub_minix_mount (device
->disk
);
447 grub_minix_read_inode (data
, GRUB_MINIX_ROOT_INODE
);
451 grub_minix_find_file (data
, path
);
455 if ((GRUB_MINIX_INODE_MODE (data
) & GRUB_MINIX_IFDIR
) != GRUB_MINIX_IFDIR
)
457 grub_error (GRUB_ERR_BAD_FILE_TYPE
, "not a directory");
461 while (pos
< GRUB_MINIX_INODE_SIZE (data
))
464 char filename
[data
->filename_size
+ 1];
465 int dirino
= data
->ino
;
466 struct grub_dirhook_info info
;
467 grub_memset (&info
, 0, sizeof (info
));
470 if (grub_minix_read_file (data
, 0, pos
, sizeof (ino
),
474 if (grub_minix_read_file (data
, 0, pos
+ sizeof (ino
),
476 (char *) filename
) < 0)
478 filename
[data
->filename_size
] = '\0';
480 /* The filetype is not stored in the dirent. Read the inode to
481 find out the filetype. This *REALLY* sucks. */
482 grub_minix_read_inode (data
, grub_le_to_cpu16 (ino
));
483 info
.dir
= ((GRUB_MINIX_INODE_MODE (data
)
484 & GRUB_MINIX_IFDIR
) == GRUB_MINIX_IFDIR
);
485 if (hook (filename
, &info
) ? 1 : 0)
488 /* Load the old inode back in. */
489 grub_minix_read_inode (data
, dirino
);
491 pos
+= sizeof (ino
) + data
->filename_size
;
500 /* Open a file named NAME and initialize FILE. */
502 grub_minix_open (struct grub_file
*file
, const char *name
)
504 struct grub_minix_data
*data
;
505 data
= grub_minix_mount (file
->device
->disk
);
509 /* Open the inode op the root directory. */
510 grub_minix_read_inode (data
, GRUB_MINIX_ROOT_INODE
);
517 if (!name
|| name
[0] != '/')
519 grub_error (GRUB_ERR_BAD_FILENAME
, "bad filename");
523 /* Traverse the directory tree to the node that should be
525 grub_minix_find_file (data
, name
);
533 file
->size
= GRUB_MINIX_INODE_SIZE (data
);
535 return GRUB_ERR_NONE
;
540 grub_minix_read (grub_file_t file
, char *buf
, grub_size_t len
)
542 struct grub_minix_data
*data
=
543 (struct grub_minix_data
*) file
->data
;
545 return grub_minix_read_file (data
, file
->read_hook
, file
->offset
, len
, buf
);
550 grub_minix_close (grub_file_t file
)
552 grub_free (file
->data
);
554 return GRUB_ERR_NONE
;
559 static struct grub_fs grub_minix_fs
=
566 .dir
= grub_minix_dir
,
567 .open
= grub_minix_open
,
568 .read
= grub_minix_read
,
569 .close
= grub_minix_close
,
574 GRUB_MOD_INIT(minix2
)
579 grub_fs_register (&grub_minix_fs
);
584 GRUB_MOD_FINI(minix2
)
589 grub_fs_unregister (&grub_minix_fs
);