1
/* ext2.c - Second Extended filesystem */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
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.
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.
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.
21
/* Magic value used to identify an ext2 filesystem. */
22
#define EXT2_MAGIC 0xEF53
23
/* Amount of indirect blocks in an inode. */
24
#define INDIRECT_BLOCKS 12
25
/* Maximum lenght of a pathname. */
26
#define EXT2_PATH_MAX 4096
27
/* Maximum nesting of symlinks, used to prevent a loop. */
28
#define EXT2_MAX_SYMLINKCNT 8
30
/* Filetype used in directory entry. */
31
#define FILETYPE_UNKNOWN 0
32
#define FILETYPE_REG 1
33
#define FILETYPE_DIRECTORY 2
34
#define FILETYPE_SYMLINK 7
36
/* Filetype information as used in inodes. */
37
#define FILETYPE_INO_MASK 0170000
38
#define FILETYPE_INO_REG 0100000
39
#define FILETYPE_INO_DIRECTORY 0040000
40
#define FILETYPE_INO_SYMLINK 0120000
43
#include <grub/file.h>
45
#include <grub/misc.h>
46
#include <grub/disk.h>
48
#include <grub/types.h>
49
#include <grub/fshelp.h>
51
/* Log2 size of ext2 block in 512 blocks. */
52
#define LOG2_EXT2_BLOCK_SIZE(data) \
53
(grub_le_to_cpu32 (data->sblock.log2_block_size) + 1)
55
/* Log2 size of ext2 block in bytes. */
56
#define LOG2_BLOCK_SIZE(data) \
57
(grub_le_to_cpu32 (data->sblock.log2_block_size) + 10)
59
/* The size of an ext2 block in bytes. */
60
#define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data))
62
/* The ext2 superblock. */
63
struct grub_ext2_sblock
65
grub_uint32_t total_inodes;
66
grub_uint32_t total_blocks;
67
grub_uint32_t reserved_blocks;
68
grub_uint32_t free_blocks;
69
grub_uint32_t free_inodes;
70
grub_uint32_t first_data_block;
71
grub_uint32_t log2_block_size;
72
grub_uint32_t log2_fragment_size;
73
grub_uint32_t blocks_per_group;
74
grub_uint32_t fragments_per_group;
75
grub_uint32_t inodes_per_group;
78
grub_uint16_t mnt_count;
79
grub_uint16_t max_mnt_count;
81
grub_uint16_t fs_state;
82
grub_uint16_t error_handling;
83
grub_uint16_t minor_revision_level;
84
grub_uint32_t lastcheck;
85
grub_uint32_t checkinterval;
86
grub_uint32_t creator_os;
87
grub_uint32_t revision_level;
88
grub_uint16_t uid_reserved;
89
grub_uint16_t gid_reserved;
90
grub_uint32_t first_inode;
91
grub_uint16_t inode_size;
92
grub_uint16_t block_group_number;
93
grub_uint32_t feature_compatibility;
94
grub_uint32_t feature_incompat;
95
grub_uint32_t feature_ro_compat;
96
grub_uint32_t unique_id[4];
98
char last_mounted_on[64];
99
grub_uint32_t compression_info;
102
/* The ext2 blockgroup. */
103
struct grub_ext2_block_group
105
grub_uint32_t block_id;
106
grub_uint32_t inode_id;
107
grub_uint32_t inode_table_id;
108
grub_uint16_t free_blocks;
109
grub_uint16_t free_inodes;
111
grub_uint32_t reserved[3];
114
/* The ext2 inode. */
115
struct grub_ext2_inode
125
grub_uint16_t nlinks;
126
grub_uint32_t blockcnt; /* Blocks of 512 bytes!! */
133
grub_uint32_t dir_blocks[INDIRECT_BLOCKS];
134
grub_uint32_t indir_block;
135
grub_uint32_t double_indir_block;
136
grub_uint32_t tripple_indir_block;
140
grub_uint32_t version;
142
grub_uint32_t dir_acl;
143
grub_uint32_t fragment_addr;
144
grub_uint32_t osd2[3];
147
/* The header of an ext2 directory entry. */
151
grub_uint16_t direntlen;
152
grub_uint8_t namelen;
153
grub_uint8_t filetype;
156
struct grub_fshelp_node
158
struct grub_ext2_data *data;
159
struct grub_ext2_inode inode;
164
/* Information about a "mounted" ext2 filesystem. */
165
struct grub_ext2_data
167
struct grub_ext2_sblock sblock;
169
struct grub_ext2_inode *inode;
170
struct grub_fshelp_node diropen;
174
static grub_dl_t my_mod;
177
/* Read into BLKGRP the blockgroup descriptor of blockgroup GROUP of
178
the mounted filesystem DATA. */
179
inline static grub_err_t
180
grub_ext2_blockgroup (struct grub_ext2_data *data, int group,
181
struct grub_ext2_block_group *blkgrp)
183
return grub_disk_read (data->disk,
184
((grub_le_to_cpu32 (data->sblock.first_data_block) + 1)
185
<< LOG2_EXT2_BLOCK_SIZE (data)),
186
group * sizeof (struct grub_ext2_block_group),
187
sizeof (struct grub_ext2_block_group), (char *) blkgrp);
192
grub_ext2_read_block (grub_fshelp_node_t node, int fileblock)
194
struct grub_ext2_data *data = node->data;
195
struct grub_ext2_inode *inode = &node->inode;
197
int blksz = EXT2_BLOCK_SIZE (data);
198
int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
201
if (fileblock < INDIRECT_BLOCKS)
202
blknr = grub_le_to_cpu32 (inode->blocks.dir_blocks[fileblock]);
204
else if (fileblock < INDIRECT_BLOCKS + blksz / 4)
206
grub_uint32_t indir[blksz / 4];
208
if (grub_disk_read (data->disk,
209
grub_le_to_cpu32 (inode->blocks.indir_block)
211
0, blksz, (char *) indir))
214
blknr = grub_le_to_cpu32 (indir[fileblock - INDIRECT_BLOCKS]);
216
/* Double indirect. */
217
else if (fileblock < INDIRECT_BLOCKS + blksz / 4 * (blksz / 4 + 1))
219
unsigned int perblock = blksz / 4;
220
unsigned int rblock = fileblock - (INDIRECT_BLOCKS
222
grub_uint32_t indir[blksz / 4];
224
if (grub_disk_read (data->disk,
225
grub_le_to_cpu32 (inode->blocks.double_indir_block)
227
0, blksz, (char *) indir))
230
if (grub_disk_read (data->disk,
231
grub_le_to_cpu32 (indir[rblock / perblock])
233
0, blksz, (char *) indir))
237
blknr = grub_le_to_cpu32 (indir[rblock % perblock]);
239
/* Tripple indirect. */
242
grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
243
"ext2fs doesn't support tripple indirect blocks");
251
/* Read LEN bytes from the file described by DATA starting with byte
252
POS. Return the amount of read bytes in READ. */
254
grub_ext2_read_file (grub_fshelp_node_t node,
255
void (*read_hook) (unsigned long sector,
256
unsigned offset, unsigned length),
257
int pos, unsigned int len, char *buf)
259
return grub_fshelp_read_file (node->data->disk, node, read_hook,
260
pos, len, buf, grub_ext2_read_block,
262
LOG2_EXT2_BLOCK_SIZE (node->data));
267
/* Read the inode INO for the file described by DATA into INODE. */
269
grub_ext2_read_inode (struct grub_ext2_data *data,
270
int ino, struct grub_ext2_inode *inode)
272
struct grub_ext2_block_group blkgrp;
273
struct grub_ext2_sblock *sblock = &data->sblock;
274
int inodes_per_block;
279
/* It is easier to calculate if the first inode is 0. */
282
grub_ext2_blockgroup (data, ino / grub_le_to_cpu32 (sblock->inodes_per_group),
287
inodes_per_block = EXT2_BLOCK_SIZE (data) / 128;
288
blkno = (ino % grub_le_to_cpu32 (sblock->inodes_per_group))
290
blkoff = (ino % grub_le_to_cpu32 (sblock->inodes_per_group))
293
/* Read the inode. */
294
if (grub_disk_read (data->disk,
295
((grub_le_to_cpu32 (blkgrp.inode_table_id) + blkno)
296
<< LOG2_EXT2_BLOCK_SIZE (data)),
297
sizeof (struct grub_ext2_inode) * blkoff,
298
sizeof (struct grub_ext2_inode), (char *) inode))
304
static struct grub_ext2_data *
305
grub_ext2_mount (grub_disk_t disk)
307
struct grub_ext2_data *data;
309
data = grub_malloc (sizeof (struct grub_ext2_data));
313
/* Read the superblock. */
314
grub_disk_read (disk, 1 * 2, 0, sizeof (struct grub_ext2_sblock),
315
(char *) &data->sblock);
319
/* Make sure this is an ext2 filesystem. */
320
if (grub_le_to_cpu16 (data->sblock.magic) != EXT2_MAGIC)
323
data->diropen.data = data;
324
data->diropen.ino = 2;
325
data->diropen.inode_read = 1;
327
data->inode = &data->diropen.inode;
330
grub_ext2_read_inode (data, 2, data->inode);
337
grub_error (GRUB_ERR_BAD_FS, "not an ext2 filesystem");
343
grub_ext2_read_symlink (grub_fshelp_node_t node)
346
struct grub_fshelp_node *diro = node;
348
if (!diro->inode_read)
350
grub_ext2_read_inode (diro->data, diro->ino, &diro->inode);
355
symlink = grub_malloc (grub_le_to_cpu32 (diro->inode.size) + 1);
359
/* If the filesize of the symlink is bigger than
360
60 the symlink is stored in a separate block,
361
otherwise it is stored in the inode. */
362
if (grub_le_to_cpu32 (diro->inode.size) <= 60)
363
grub_strncpy (symlink,
365
grub_le_to_cpu32 (diro->inode.size));
368
grub_ext2_read_file (diro, 0, 0,
369
grub_le_to_cpu32 (diro->inode.size),
378
symlink[grub_le_to_cpu32 (diro->inode.size)] = '\0';
383
grub_ext2_iterate_dir (grub_fshelp_node_t dir,
385
(*hook) (const char *filename,
386
enum grub_fshelp_filetype filetype,
387
grub_fshelp_node_t node))
389
unsigned int fpos = 0;
390
struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
392
if (!diro->inode_read)
394
grub_ext2_read_inode (diro->data, diro->ino, &diro->inode);
399
/* Search the file. */
400
while (fpos < grub_le_to_cpu32 (diro->inode.size))
402
struct ext2_dirent dirent;
404
grub_ext2_read_file (diro, 0, fpos, sizeof (struct ext2_dirent),
409
if (dirent.namelen != 0)
411
char filename[dirent.namelen + 1];
412
struct grub_fshelp_node *fdiro;
413
enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
415
grub_ext2_read_file (diro, 0, fpos + sizeof (struct ext2_dirent),
416
dirent.namelen, filename);
420
fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
424
fdiro->data = diro->data;
425
fdiro->ino = grub_le_to_cpu32 (dirent.inode);
427
filename[dirent.namelen] = '\0';
429
if (dirent.filetype != FILETYPE_UNKNOWN)
431
fdiro->inode_read = 0;
433
if (dirent.filetype == FILETYPE_DIRECTORY)
434
type = GRUB_FSHELP_DIR;
435
else if (dirent.filetype == FILETYPE_SYMLINK)
436
type = GRUB_FSHELP_SYMLINK;
437
else if (dirent.filetype == FILETYPE_REG)
438
type = GRUB_FSHELP_REG;
442
/* The filetype can not be read from the dirent, read
443
the inode to get more information. */
444
grub_ext2_read_inode (diro->data, grub_le_to_cpu32 (dirent.inode),
452
fdiro->inode_read = 1;
454
if ((grub_le_to_cpu16 (fdiro->inode.mode)
455
& FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
456
type = GRUB_FSHELP_DIR;
457
else if ((grub_le_to_cpu16 (fdiro->inode.mode)
458
& FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
459
type = GRUB_FSHELP_SYMLINK;
460
else if ((grub_le_to_cpu16 (fdiro->inode.mode)
461
& FILETYPE_INO_MASK) == FILETYPE_INO_REG)
462
type = GRUB_FSHELP_REG;
465
if (hook (filename, type, fdiro))
469
fpos += grub_le_to_cpu16 (dirent.direntlen);
475
/* Open a file named NAME and initialize FILE. */
477
grub_ext2_open (struct grub_file *file, const char *name)
479
struct grub_ext2_data *data;
480
struct grub_fshelp_node *fdiro = 0;
483
grub_dl_ref (my_mod);
486
data = grub_ext2_mount (file->device->disk);
490
grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_ext2_iterate_dir,
491
grub_ext2_read_symlink, GRUB_FSHELP_REG);
495
if (!fdiro->inode_read)
497
grub_ext2_read_inode (data, fdiro->ino, &fdiro->inode);
502
grub_memcpy (data->inode, &fdiro->inode, sizeof (struct grub_ext2_inode));
505
file->size = grub_le_to_cpu32 (data->inode->size);
512
if (fdiro != &data->diropen)
517
grub_dl_unref (my_mod);
524
grub_ext2_close (grub_file_t file)
526
grub_free (file->data);
529
grub_dl_unref (my_mod);
532
return GRUB_ERR_NONE;
535
/* Read LEN bytes data from FILE into BUF. */
537
grub_ext2_read (grub_file_t file, char *buf, grub_ssize_t len)
539
struct grub_ext2_data *data =
540
(struct grub_ext2_data *) file->data;
542
return grub_ext2_read_file (&data->diropen, file->read_hook,
543
file->offset, len, buf);
548
grub_ext2_dir (grub_device_t device, const char *path,
549
int (*hook) (const char *filename, int dir))
551
struct grub_ext2_data *data = 0;;
552
struct grub_fshelp_node *fdiro = 0;
554
auto int NESTED_FUNC_ATTR iterate (const char *filename,
555
enum grub_fshelp_filetype filetype,
556
grub_fshelp_node_t node);
558
int NESTED_FUNC_ATTR iterate (const char *filename,
559
enum grub_fshelp_filetype filetype,
560
grub_fshelp_node_t node)
564
if (filetype == GRUB_FSHELP_DIR)
565
return hook (filename, 1);
567
return hook (filename, 0);
573
grub_dl_ref (my_mod);
576
data = grub_ext2_mount (device->disk);
580
grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_ext2_iterate_dir,
581
grub_ext2_read_symlink, GRUB_FSHELP_DIR);
585
grub_ext2_iterate_dir (fdiro, iterate);
588
if (fdiro != &data->diropen)
593
grub_dl_unref (my_mod);
600
grub_ext2_label (grub_device_t device, char **label)
602
struct grub_ext2_data *data;
603
grub_disk_t disk = device->disk;
606
grub_dl_ref (my_mod);
609
data = grub_ext2_mount (disk);
611
*label = grub_strndup (data->sblock.volume_name, 14);
616
grub_dl_unref (my_mod);
625
static struct grub_fs grub_ext2_fs =
628
.dir = grub_ext2_dir,
629
.open = grub_ext2_open,
630
.read = grub_ext2_read,
631
.close = grub_ext2_close,
632
.label = grub_ext2_label,
638
grub_fs_register (&grub_ext2_fs);
646
grub_fs_unregister (&grub_ext2_fs);