minix.c 18.7 KB
Newer Older
1 2 3
/* minix.c - The minix filesystem, version 1 and 2.  */
/*
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2004,2005,2006,2007,2008  Free Software Foundation, Inc.
5
 *
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 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18 19 20 21 22 23 24 25 26
 */

#include <grub/err.h>
#include <grub/file.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/disk.h>
#include <grub/dl.h>
#include <grub/types.h>
27
#include <grub/i18n.h>
28

29 30
GRUB_MOD_LICENSE ("GPLv3+");

Feiran Zheng's avatar
Feiran Zheng committed
31 32 33
#ifdef MODE_MINIX3
#define GRUB_MINIX_MAGIC	0x4D5A
#elif defined(MODE_MINIX2)
34 35 36
#define GRUB_MINIX_MAGIC	0x2468
#define GRUB_MINIX_MAGIC_30	0x2478
#else
37 38
#define GRUB_MINIX_MAGIC	0x137F
#define GRUB_MINIX_MAGIC_30	0x138F
39
#endif
Feiran Zheng's avatar
Feiran Zheng committed
40 41

#define GRUB_MINIX_INODE_DIR_BLOCKS	7
42 43 44 45 46 47 48 49
#define GRUB_MINIX_LOG2_BSIZE	1
#define GRUB_MINIX_ROOT_INODE	1
#define GRUB_MINIX_MAX_SYMLNK_CNT	8
#define GRUB_MINIX_SBLOCK	2

#define GRUB_MINIX_IFDIR	0040000U
#define GRUB_MINIX_IFLNK	0120000U

50 51 52 53 54 55 56 57 58 59
#ifdef MODE_BIGENDIAN
#define grub_cpu_to_minix16_compile_time grub_cpu_to_be16_compile_time
#define grub_minix_to_cpu16 grub_be_to_cpu16
#define grub_minix_to_cpu32 grub_be_to_cpu32
#else
#define grub_cpu_to_minix16_compile_time grub_cpu_to_le16_compile_time
#define grub_minix_to_cpu16 grub_le_to_cpu16
#define grub_minix_to_cpu32 grub_le_to_cpu32
#endif

Feiran Zheng's avatar
Feiran Zheng committed
60
#if defined(MODE_MINIX2) || defined(MODE_MINIX3)
61
typedef grub_uint32_t grub_minix_uintn_t;
62
#define grub_minix_to_cpu_n grub_minix_to_cpu32
63 64
#else
typedef grub_uint16_t grub_minix_uintn_t;
65
#define grub_minix_to_cpu_n grub_minix_to_cpu16
66 67
#endif

Feiran Zheng's avatar
Feiran Zheng committed
68 69
#ifdef MODE_MINIX3
typedef grub_uint32_t grub_minix_ino_t;
70
#define grub_minix_to_cpu_ino grub_minix_to_cpu32
Feiran Zheng's avatar
Feiran Zheng committed
71 72
#else
typedef grub_uint16_t grub_minix_ino_t;
73
#define grub_minix_to_cpu_ino grub_minix_to_cpu16
Feiran Zheng's avatar
Feiran Zheng committed
74
#endif
75

76 77 78
#define GRUB_MINIX_INODE_SIZE(data) (grub_minix_to_cpu32 (data->inode.size))
#define GRUB_MINIX_INODE_MODE(data) (grub_minix_to_cpu16 (data->inode.mode))
#define GRUB_MINIX_INODE_DIR_ZONES(data,blk) (grub_minix_to_cpu_n   \
79
					      (data->inode.dir_zones[blk]))
80
#define GRUB_MINIX_INODE_INDIR_ZONE(data)  (grub_minix_to_cpu_n \
81
					    (data->inode.indir_zone))
82
#define GRUB_MINIX_INODE_DINDIR_ZONE(data) (grub_minix_to_cpu_n \
83 84
					    (data->inode.double_indir_zone))

85

Feiran Zheng's avatar
Feiran Zheng committed
86
#ifdef MODE_MINIX3
87 88
struct grub_minix_sblock
{
Feiran Zheng's avatar
Feiran Zheng committed
89
  grub_uint32_t inode_cnt;
90 91 92 93 94
  grub_uint16_t zone_cnt;
  grub_uint16_t inode_bmap_size;
  grub_uint16_t zone_bmap_size;
  grub_uint16_t first_data_zone;
  grub_uint16_t log2_zone_size;
Feiran Zheng's avatar
Feiran Zheng committed
95
  grub_uint16_t pad;
96
  grub_uint32_t max_file_size;
Feiran Zheng's avatar
Feiran Zheng committed
97
  grub_uint32_t zones;
98
  grub_uint16_t magic;
Feiran Zheng's avatar
Feiran Zheng committed
99 100 101 102
  
  grub_uint16_t pad2;
  grub_uint16_t block_size;
  grub_uint8_t disk_version; 
103
};
Feiran Zheng's avatar
Feiran Zheng committed
104
#else
105 106 107 108 109 110 111 112 113 114 115
struct grub_minix_sblock
{
  grub_uint16_t inode_cnt;
  grub_uint16_t zone_cnt;
  grub_uint16_t inode_bmap_size;
  grub_uint16_t zone_bmap_size;
  grub_uint16_t first_data_zone;
  grub_uint16_t log2_zone_size;
  grub_uint32_t max_file_size;
  grub_uint16_t magic;
};
Feiran Zheng's avatar
Feiran Zheng committed
116
#endif
117

118
#if defined(MODE_MINIX3) || defined(MODE_MINIX2)
119
struct grub_minix_inode
120 121 122 123 124 125 126 127 128 129 130 131
{
  grub_uint16_t mode;
  grub_uint16_t nlinks;
  grub_uint16_t uid;
  grub_uint16_t gid;
  grub_uint32_t size;
  grub_uint32_t atime;
  grub_uint32_t mtime;
  grub_uint32_t ctime;
  grub_uint32_t dir_zones[7];
  grub_uint32_t indir_zone;
  grub_uint32_t double_indir_zone;
132
  grub_uint32_t triple_indir_zone;
133
};
134
#else
135 136 137 138
struct grub_minix_inode
{
  grub_uint16_t mode;
  grub_uint16_t uid;
139 140
  grub_uint32_t size;
  grub_uint32_t mtime;
141 142 143 144 145 146 147
  grub_uint8_t gid;
  grub_uint8_t nlinks;
  grub_uint16_t dir_zones[7];
  grub_uint16_t indir_zone;
  grub_uint16_t double_indir_zone;
};

148 149
#endif

150 151 152 153 154 155
#if defined(MODE_MINIX3)
#define MAX_MINIX_FILENAME_SIZE 60
#else
#define MAX_MINIX_FILENAME_SIZE 30
#endif

156 157 158 159 160
/* Information about a "mounted" minix filesystem.  */
struct grub_minix_data
{
  struct grub_minix_sblock sblock;
  struct grub_minix_inode inode;
161
  grub_uint32_t block_per_zone;
162
  grub_minix_ino_t ino;
163 164 165
  int linknest;
  grub_disk_t disk;
  int filename_size;
166
  grub_size_t block_size;
167 168 169 170 171 172 173
};

static grub_dl_t my_mod;

static grub_err_t grub_minix_find_file (struct grub_minix_data *data,
					const char *path);

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
#ifdef MODE_MINIX3
static inline grub_disk_addr_t
grub_minix_zone2sect (struct grub_minix_data *data, grub_minix_uintn_t zone)
{
  return ((grub_disk_addr_t) zone) * data->block_size;
}
#else
static inline grub_disk_addr_t
grub_minix_zone2sect (struct grub_minix_data *data, grub_minix_uintn_t zone)
{
  int log2_zonesz = (GRUB_MINIX_LOG2_BSIZE
		     + grub_minix_to_cpu16 (data->sblock.log2_zone_size));
  return (((grub_disk_addr_t) zone) << log2_zonesz);
}
#endif


191 192
  /* Read the block pointer in ZONE, on the offset NUM.  */
static grub_minix_uintn_t
193
grub_get_indir (struct grub_minix_data *data,
194 195 196 197 198
		 grub_minix_uintn_t zone,
		 grub_minix_uintn_t num)
{
  grub_minix_uintn_t indirn;
  grub_disk_read (data->disk,
199
		  grub_minix_zone2sect(data, zone),
200 201 202 203 204
		  sizeof (grub_minix_uintn_t) * num,
		  sizeof (grub_minix_uintn_t), (char *) &indirn);
  return grub_minix_to_cpu_n (indirn);
}

205
static grub_minix_uintn_t
206 207
grub_minix_get_file_block (struct grub_minix_data *data, unsigned int blk)
{
208
  grub_minix_uintn_t indir;
209 210

  /* Direct block.  */
Feiran Zheng's avatar
Feiran Zheng committed
211
  if (blk < GRUB_MINIX_INODE_DIR_BLOCKS)
212
    return GRUB_MINIX_INODE_DIR_ZONES (data, blk);
213

214
  /* Indirect block.  */
Feiran Zheng's avatar
Feiran Zheng committed
215
  blk -= GRUB_MINIX_INODE_DIR_BLOCKS;
216
  if (blk < data->block_per_zone)
217
    {
218
      indir = grub_get_indir (data, GRUB_MINIX_INODE_INDIR_ZONE (data), blk);
219 220
      return indir;
    }
221

222
  /* Double indirect block.  */
223 224
  blk -= data->block_per_zone;
  if (blk < (grub_uint64_t) data->block_per_zone * (grub_uint64_t) data->block_per_zone)
225
    {
226
      indir = grub_get_indir (data, GRUB_MINIX_INODE_DINDIR_ZONE (data),
227
			      blk / data->block_per_zone);
228

229
      indir = grub_get_indir (data, indir, blk % data->block_per_zone);
230

231 232
      return indir;
    }
233

234
#if defined (MODE_MINIX3) || defined (MODE_MINIX2)
235 236 237
  blk -= data->block_per_zone * data->block_per_zone;
  if (blk < ((grub_uint64_t) data->block_per_zone * (grub_uint64_t) data->block_per_zone
	     * (grub_uint64_t) data->block_per_zone))
238
    {
239
      indir = grub_get_indir (data, grub_minix_to_cpu_n (data->inode.triple_indir_zone),
240 241 242
			      (blk / data->block_per_zone) / data->block_per_zone);
      indir = grub_get_indir (data, indir, (blk / data->block_per_zone) % data->block_per_zone);
      indir = grub_get_indir (data, indir, blk % data->block_per_zone);
243 244 245 246 247

      return indir;
    }
#endif

248 249
  /* This should never happen.  */
  grub_error (GRUB_ERR_OUT_OF_RANGE, "file bigger than maximum size");
250

251 252 253 254 255 256 257 258
  return 0;
}


/* Read LEN bytes from the file described by DATA starting with byte
   POS.  Return the amount of read bytes in READ.  */
static grub_ssize_t
grub_minix_read_file (struct grub_minix_data *data,
259
		      grub_disk_read_hook_t read_hook, void *read_hook_data,
260
		      grub_off_t pos, grub_size_t len, char *buf)
261
{
262 263 264 265
  grub_uint32_t i;
  grub_uint32_t blockcnt;
  grub_uint32_t posblock;
  grub_uint32_t blockoff;
266

267 268 269 270 271 272 273
  if (pos > GRUB_MINIX_INODE_SIZE (data))
    {
      grub_error (GRUB_ERR_OUT_OF_RANGE,
		  N_("attempt to read past the end of file"));
      return -1;
    }

274
  /* Adjust len so it we can't read past the end of the file.  */
275 276
  if (len + pos > GRUB_MINIX_INODE_SIZE (data))
    len = GRUB_MINIX_INODE_SIZE (data) - pos;
277 278
  if (len == 0)
    return 0;
279

280
  /* Files are at most 2G/4G - 1 bytes on minixfs. Avoid 64-bit division.  */
281 282
  blockcnt = ((grub_uint32_t) ((len + pos - 1)
	       >> GRUB_DISK_SECTOR_BITS)) / data->block_size + 1;
283 284 285 286
  posblock = (((grub_uint32_t) pos)
	      / (data->block_size << GRUB_DISK_SECTOR_BITS));
  blockoff = (((grub_uint32_t) pos)
	      % (data->block_size << GRUB_DISK_SECTOR_BITS));
287

Feiran Zheng's avatar
Feiran Zheng committed
288
  for (i = posblock; i < blockcnt; i++)
289
    {
290
      grub_minix_uintn_t blknr;
291
      grub_uint64_t blockend = data->block_size << GRUB_DISK_SECTOR_BITS;
Feiran Zheng's avatar
Feiran Zheng committed
292
      grub_off_t skipfirst = 0;
293

294 295 296
      blknr = grub_minix_get_file_block (data, i);
      if (grub_errno)
	return -1;
297

298 299 300
      /* Last block.  */
      if (i == blockcnt - 1)
	{
301 302 303
	  /* len + pos < 4G (checked above), so it doesn't overflow.  */
	  blockend = (((grub_uint32_t) (len + pos))
		      % (data->block_size << GRUB_DISK_SECTOR_BITS));
304

305
	  if (!blockend)
306
	    blockend = data->block_size << GRUB_DISK_SECTOR_BITS;
307
	}
308

309
      /* First block.  */
Feiran Zheng's avatar
Feiran Zheng committed
310
      if (i == posblock)
311 312 313 314
	{
	  skipfirst = blockoff;
	  blockend -= skipfirst;
	}
315

316
      data->disk->read_hook = read_hook;
317
      data->disk->read_hook_data = read_hook_data;
Feiran Zheng's avatar
Feiran Zheng committed
318
      grub_disk_read (data->disk,
319
		      grub_minix_zone2sect(data, blknr),
320 321 322 323
		      skipfirst, blockend, buf);
      data->disk->read_hook = 0;
      if (grub_errno)
	return -1;
324

325
      buf += (data->block_size << GRUB_DISK_SECTOR_BITS) - skipfirst;
326
    }
327

328 329 330 331 332 333 334
  return len;
}


/* Read inode INO from the mounted filesystem described by DATA.  This
   inode is used by default now.  */
static grub_err_t
335
grub_minix_read_inode (struct grub_minix_data *data, grub_minix_ino_t ino)
336 337 338 339
{
  struct grub_minix_sblock *sblock = &data->sblock;

  /* Block in which the inode is stored.  */
Feiran Zheng's avatar
Feiran Zheng committed
340
  grub_disk_addr_t block;
341 342 343 344
  data->ino = ino;

  /* The first inode in minix is inode 1.  */
  ino--;
345 346
  block = grub_minix_zone2sect (data,
				2 + grub_minix_to_cpu16 (sblock->inode_bmap_size)
347
				+ grub_minix_to_cpu16 (sblock->zone_bmap_size));
348 349 350 351 352 353 354
  block += ino / (GRUB_DISK_SECTOR_SIZE / sizeof (struct grub_minix_inode));
  int offs = (ino % (GRUB_DISK_SECTOR_SIZE
		     / sizeof (struct grub_minix_inode))
	      * sizeof (struct grub_minix_inode));
  
  grub_disk_read (data->disk, block, offs,
		  sizeof (struct grub_minix_inode), &data->inode);
355

356 357 358 359 360 361 362
  return GRUB_ERR_NONE;
}


/* Lookup the symlink the current inode points to.  INO is the inode
   number of the directory the symlink is relative to.  */
static grub_err_t
363
grub_minix_lookup_symlink (struct grub_minix_data *data, grub_minix_ino_t ino)
364
{
365 366
  char *symlink;
  grub_size_t sz = GRUB_MINIX_INODE_SIZE (data);
367

368
  if (++data->linknest > GRUB_MINIX_MAX_SYMLNK_CNT)
369
    return grub_error (GRUB_ERR_SYMLINK_LOOP, N_("too deep nesting of symlinks"));
370

371 372 373 374
  symlink = grub_malloc (sz + 1);
  if (!symlink)
    return grub_errno;
  if (grub_minix_read_file (data, 0, 0, 0, sz, symlink) < 0)
375 376
    return grub_errno;

377
  symlink[sz] = '\0';
378

379 380 381
  /* The symlink is an absolute path, go back to the root inode.  */
  if (symlink[0] == '/')
    ino = GRUB_MINIX_ROOT_INODE;
382

383 384 385
  /* Now load in the old inode.  */
  if (grub_minix_read_inode (data, ino))
    return grub_errno;
386

387
  grub_minix_find_file (data, symlink);
388

389 390 391 392 393 394 395 396 397
  return grub_errno;
}


/* Find the file with the pathname PATH on the filesystem described by
   DATA.  */
static grub_err_t
grub_minix_find_file (struct grub_minix_data *data, const char *path)
{
398 399
  const char *name;
  const char *next = path;
400
  unsigned int pos = 0;
401
  grub_minix_ino_t dirino;
402

403
  while (1)
404
    {
405 406 407 408 409
      name = next;
      /* Skip the first slash.  */
      while (*name == '/')
	name++;
      if (!*name)
410
	return GRUB_ERR_NONE;
411

412 413 414
      if ((GRUB_MINIX_INODE_MODE (data)
	   & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR)
	return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
415

416 417
      /* Extract the actual part from the pathname.  */
      for (next = name; *next && *next != '/'; next++);
418

419
      for (pos = 0; ; )
420
	{
421 422
	  grub_minix_ino_t ino;
	  char filename[MAX_MINIX_FILENAME_SIZE + 1];
423

424
	  if (pos >= GRUB_MINIX_INODE_SIZE (data))
425
	    {
426 427
	      grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), path);
	      return grub_errno;
428
	    }
429

430 431 432 433 434 435 436 437
	  if (grub_minix_read_file (data, 0, 0, pos, sizeof (ino),
				    (char *) &ino) < 0)
	    return grub_errno;
	  if (grub_minix_read_file (data, 0, 0, pos + sizeof (ino),
				    data->filename_size, (char *) filename)< 0)
	    return grub_errno;

	  pos += sizeof (ino) + data->filename_size;
438

439
	  filename[data->filename_size] = '\0';
440

441 442 443 444
	  /* Check if the current direntry matches the current part of the
	     pathname.  */
	  if (grub_strncmp (name, filename, next - name) == 0
	      && filename[next - name] == '\0')
445
	    {
446 447 448 449 450 451 452 453 454 455 456 457 458
	      dirino = data->ino;
	      grub_minix_read_inode (data, grub_minix_to_cpu_ino (ino));

	      /* Follow the symlink.  */
	      if ((GRUB_MINIX_INODE_MODE (data)
		   & GRUB_MINIX_IFLNK) == GRUB_MINIX_IFLNK)
		{
		  grub_minix_lookup_symlink (data, dirino);
		  if (grub_errno)
		    return grub_errno;
		}

	      break;
459 460
	    }
	}
461
    }
462 463 464 465 466 467 468 469
}


/* Mount the filesystem on the disk DISK.  */
static struct grub_minix_data *
grub_minix_mount (grub_disk_t disk)
{
  struct grub_minix_data *data;
470

471 472 473
  data = grub_malloc (sizeof (struct grub_minix_data));
  if (!data)
    return 0;
474

475 476
  /* Read the superblock.  */
  grub_disk_read (disk, GRUB_MINIX_SBLOCK, 0,
477
		  sizeof (struct grub_minix_sblock),&data->sblock);
478 479
  if (grub_errno)
    goto fail;
480

481
  if (data->sblock.magic == grub_cpu_to_minix16_compile_time (GRUB_MINIX_MAGIC))
Feiran Zheng's avatar
Feiran Zheng committed
482 483
    {
#if !defined(MODE_MINIX3)
484
      data->filename_size = 14;
Feiran Zheng's avatar
Feiran Zheng committed
485
#else
486
      data->filename_size = 60;
Feiran Zheng's avatar
Feiran Zheng committed
487 488 489
#endif
    }
#if !defined(MODE_MINIX3)
490 491
  else if (data->sblock.magic
	   == grub_cpu_to_minix16_compile_time (GRUB_MINIX_MAGIC_30))
492
    data->filename_size = 30;
Feiran Zheng's avatar
Feiran Zheng committed
493
#endif
494
  else
495 496
    goto fail;

497 498 499 500 501
  /* 20 means 1G zones. We could support up to 31 but already 1G isn't
     supported by anything else.  */
  if (grub_minix_to_cpu16 (data->sblock.log2_zone_size) >= 20)
    goto fail;

502 503
  data->disk = disk;
  data->linknest = 0;
Feiran Zheng's avatar
Feiran Zheng committed
504
#ifdef MODE_MINIX3
505 506
  /* These tests are endian-independent. No need to byteswap.  */
  if (data->sblock.block_size == 0xffff)
507
    data->block_size = 2;
508 509
  else
    {
510 511 512
      if ((data->sblock.block_size == grub_cpu_to_minix16_compile_time (0x200))
	  || (data->sblock.block_size == 0)
	  || (data->sblock.block_size & grub_cpu_to_minix16_compile_time (0x1ff)))
513
	goto fail;
514 515
      data->block_size = grub_minix_to_cpu16 (data->sblock.block_size)
	>> GRUB_DISK_SECTOR_BITS;
516
    }
Feiran Zheng's avatar
Feiran Zheng committed
517
#else
518
  data->block_size = 2;
Feiran Zheng's avatar
Feiran Zheng committed
519
#endif
520

521 522 523 524 525 526
  data->block_per_zone = (((grub_uint64_t) data->block_size <<	\
			   (GRUB_DISK_SECTOR_BITS + grub_minix_to_cpu16 (data->sblock.log2_zone_size)))
			  / sizeof (grub_minix_uintn_t));
  if (!data->block_per_zone)
    goto fail;

527
  return data;
528 529 530

 fail:
  grub_free (data);
Feiran Zheng's avatar
Feiran Zheng committed
531 532 533
#if defined(MODE_MINIX3)
  grub_error (GRUB_ERR_BAD_FS, "not a minix3 filesystem");
#elif defined(MODE_MINIX2)
534 535
  grub_error (GRUB_ERR_BAD_FS, "not a minix2 filesystem");
#else
536
  grub_error (GRUB_ERR_BAD_FS, "not a minix filesystem");
537
#endif
538
  return 0;
539 540 541
}

static grub_err_t
542
grub_minix_dir (grub_device_t device, const char *path,
543
		grub_fs_dir_hook_t hook, void *hook_data)
544 545 546
{
  struct grub_minix_data *data = 0;
  unsigned int pos = 0;
547

548 549 550
  data = grub_minix_mount (device->disk);
  if (!data)
    return grub_errno;
551

552 553 554
  grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE);
  if (grub_errno)
    goto fail;
555

556 557 558
  grub_minix_find_file (data, path);
  if (grub_errno)
    goto fail;
559

560 561
  if ((GRUB_MINIX_INODE_MODE (data) & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR)
    {
562
      grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
563 564
      goto fail;
    }
565

566 567
  while (pos < GRUB_MINIX_INODE_SIZE (data))
    {
Feiran Zheng's avatar
Feiran Zheng committed
568
      grub_minix_ino_t ino;
569
      char filename[MAX_MINIX_FILENAME_SIZE + 1];
570
      grub_minix_ino_t dirino = data->ino;
571 572 573
      struct grub_dirhook_info info;
      grub_memset (&info, 0, sizeof (info));

574

575
      if (grub_minix_read_file (data, 0, 0, pos, sizeof (ino),
576 577
				(char *) &ino) < 0)
	return grub_errno;
578

579
      if (grub_minix_read_file (data, 0, 0, pos + sizeof (ino),
580 581 582 583
				data->filename_size,
				(char *) filename) < 0)
	return grub_errno;
      filename[data->filename_size] = '\0';
Feiran Zheng's avatar
Feiran Zheng committed
584 585 586 587 588
      if (!ino)
	{
	  pos += sizeof (ino) + data->filename_size;
	  continue;
	}
589

590
      grub_minix_read_inode (data, grub_minix_to_cpu_ino (ino));
591
      info.dir = ((GRUB_MINIX_INODE_MODE (data)
592
		   & GRUB_MINIX_IFDIR) == GRUB_MINIX_IFDIR);
593
      info.mtimeset = 1;
594
      info.mtime = grub_minix_to_cpu32 (data->inode.mtime);
595

596
      if (hook (filename, &info, hook_data) ? 1 : 0)
597
	break;
598

599 600 601 602 603
      /* Load the old inode back in.  */
      grub_minix_read_inode (data, dirino);

      pos += sizeof (ino) + data->filename_size;
    }
604

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
 fail:
  grub_free (data);
  return grub_errno;
}


/* Open a file named NAME and initialize FILE.  */
static grub_err_t
grub_minix_open (struct grub_file *file, const char *name)
{
  struct grub_minix_data *data;
  data = grub_minix_mount (file->device->disk);
  if (!data)
    return grub_errno;

  /* Open the inode op the root directory.  */
  grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE);
  if (grub_errno)
    {
      grub_free (data);
      return grub_errno;
    }
627

628 629
  if (!name || name[0] != '/')
    {
630
      grub_error (GRUB_ERR_BAD_FILENAME, N_("invalid file name `%s'"), name);
631 632
      return grub_errno;
    }
633

634 635 636 637 638 639 640 641
  /* Traverse the directory tree to the node that should be
     opened.  */
  grub_minix_find_file (data, name);
  if (grub_errno)
    {
      grub_free (data);
      return grub_errno;
    }
642

643 644
  file->data = data;
  file->size = GRUB_MINIX_INODE_SIZE (data);
645

646 647 648 649 650
  return GRUB_ERR_NONE;
}


static grub_ssize_t
651
grub_minix_read (grub_file_t file, char *buf, grub_size_t len)
652
{
653
  struct grub_minix_data *data =
654
    (struct grub_minix_data *) file->data;
655

656 657
  return grub_minix_read_file (data, file->read_hook, file->read_hook_data,
			       file->offset, len, buf);
658 659 660 661 662 663 664
}


static grub_err_t
grub_minix_close (grub_file_t file)
{
  grub_free (file->data);
665

666 667 668 669 670 671 672
  return GRUB_ERR_NONE;
}



static struct grub_fs grub_minix_fs =
  {
673 674 675 676 677 678 679 680 681
#ifdef MODE_BIGENDIAN
#if defined(MODE_MINIX3)
    .name = "minix3_be",
#elif defined(MODE_MINIX2)
    .name = "minix2_be",
#else
    .name = "minix_be",
#endif
#else
Feiran Zheng's avatar
Feiran Zheng committed
682 683 684
#if defined(MODE_MINIX3)
    .name = "minix3",
#elif defined(MODE_MINIX2)
685 686
    .name = "minix2",
#else
687
    .name = "minix",
688
#endif
689
#endif
690 691 692 693
    .dir = grub_minix_dir,
    .open = grub_minix_open,
    .read = grub_minix_read,
    .close = grub_minix_close,
694 695 696 697
#ifdef GRUB_UTIL
    .reserved_first_sector = 1,
    .blocklist_install = 1,
#endif
698 699 700
    .next = 0
  };

701 702 703 704 705 706 707 708 709
#ifdef MODE_BIGENDIAN
#if defined(MODE_MINIX3)
GRUB_MOD_INIT(minix3_be)
#elif defined(MODE_MINIX2)
GRUB_MOD_INIT(minix2_be)
#else
GRUB_MOD_INIT(minix_be)
#endif
#else
Feiran Zheng's avatar
Feiran Zheng committed
710 711 712
#if defined(MODE_MINIX3)
GRUB_MOD_INIT(minix3)
#elif defined(MODE_MINIX2)
713 714
GRUB_MOD_INIT(minix2)
#else
715
GRUB_MOD_INIT(minix)
716
#endif
717
#endif
718 719 720 721 722
{
  grub_fs_register (&grub_minix_fs);
  my_mod = mod;
}

723 724 725 726 727 728 729 730 731
#ifdef MODE_BIGENDIAN
#if defined(MODE_MINIX3)
GRUB_MOD_FINI(minix3_be)
#elif defined(MODE_MINIX2)
GRUB_MOD_FINI(minix2_be)
#else
GRUB_MOD_FINI(minix_be)
#endif
#else
Feiran Zheng's avatar
Feiran Zheng committed
732 733 734
#if defined(MODE_MINIX3)
GRUB_MOD_FINI(minix3)
#elif defined(MODE_MINIX2)
735 736
GRUB_MOD_FINI(minix2)
#else
737
GRUB_MOD_FINI(minix)
738
#endif
739
#endif
740 741 742
{
  grub_fs_unregister (&grub_minix_fs);
}