1 /* ext2.c - Second Extended filesystem */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2004,2005,2007,2008,2009 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 * (at your option) any later version.
10 *
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.
15 *
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/>.
18 */
19
20 /* Magic value used to identify an ext2 filesystem. */
21 #define EXT2_MAGIC 0xEF53
22 /* Amount of indirect blocks in an inode. */
23 #define INDIRECT_BLOCKS 12
24 /* Maximum length of a pathname. */
25 #define EXT2_PATH_MAX 4096
26 /* Maximum nesting of symlinks, used to prevent a loop. */
27 #define EXT2_MAX_SYMLINKCNT 8
28
29 /* The good old revision and the default inode size. */
30 #define EXT2_GOOD_OLD_REVISION 0
31 #define EXT2_GOOD_OLD_INODE_SIZE 128
32
33 /* Filetype used in directory entry. */
34 #define FILETYPE_UNKNOWN 0
35 #define FILETYPE_REG 1
36 #define FILETYPE_DIRECTORY 2
37 #define FILETYPE_SYMLINK 7
38
39 /* Filetype information as used in inodes. */
40 #define FILETYPE_INO_MASK 0170000
41 #define FILETYPE_INO_REG 0100000
42 #define FILETYPE_INO_DIRECTORY 0040000
43 #define FILETYPE_INO_SYMLINK 0120000
44
45 #include <stdlib.h>
46 #include <grub/err.h>
47 #include <grub/file.h>
48 #include <grub/mm.h>
49 #include <grub/misc.h>
50 #include <grub/disk.h>
51 #include <grub/types.h>
52 #include <grub/fshelp.h>
53
54 /* Log2 size of ext2 block in 512 blocks. */
55 #define LOG2_EXT2_BLOCK_SIZE(data) \
56 (grub_le_to_cpu32 (data->sblock.log2_block_size) + 1)
57
58 /* Log2 size of ext2 block in bytes. */
59 #define LOG2_BLOCK_SIZE(data) \
60 (grub_le_to_cpu32 (data->sblock.log2_block_size) + 10)
61
62 /* The size of an ext2 block in bytes. */
63 #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE (data))
64
65 /* The revision level. */
66 #define EXT2_REVISION(data) grub_le_to_cpu32 (data->sblock.revision_level)
67
68 /* The inode size. */
69 #define EXT2_INODE_SIZE(data) \
70 (EXT2_REVISION (data) == EXT2_GOOD_OLD_REVISION \
71 ? EXT2_GOOD_OLD_INODE_SIZE \
72 : grub_le_to_cpu16 (data->sblock.inode_size))
73
74 /* Superblock filesystem feature flags (RW compatible)
75 * A filesystem with any of these enabled can be read and written by a driver
76 * that does not understand them without causing metadata/data corruption. */
77 #define EXT2_FEATURE_COMPAT_DIR_PREALLOC 0x0001
78 #define EXT2_FEATURE_COMPAT_IMAGIC_INODES 0x0002
79 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004
80 #define EXT2_FEATURE_COMPAT_EXT_ATTR 0x0008
81 #define EXT2_FEATURE_COMPAT_RESIZE_INODE 0x0010
82 #define EXT2_FEATURE_COMPAT_DIR_INDEX 0x0020
83 /* Superblock filesystem feature flags (RO compatible)
84 * A filesystem with any of these enabled can be safely read by a driver that
85 * does not understand them, but should not be written to, usually because
86 * additional metadata is required. */
87 #define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
88 #define EXT2_FEATURE_RO_COMPAT_LARGE_FILE 0x0002
89 #define EXT2_FEATURE_RO_COMPAT_BTREE_DIR 0x0004
90 #define EXT4_FEATURE_RO_COMPAT_GDT_CSUM 0x0010
91 #define EXT4_FEATURE_RO_COMPAT_DIR_NLINK 0x0020
92 #define EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE 0x0040
93 /* Superblock filesystem feature flags (back-incompatible)
94 * A filesystem with any of these enabled should not be attempted to be read
95 * by a driver that does not understand them, since they usually indicate
96 * metadata format changes that might confuse the reader. */
97 #define EXT2_FEATURE_INCOMPAT_COMPRESSION 0x0001
98 #define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002
99 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
100 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Volume is journal device */
101 #define EXT2_FEATURE_INCOMPAT_META_BG 0x0010
102 #define EXT4_FEATURE_INCOMPAT_EXTENTS 0x0040 /* Extents used */
103 #define EXT4_FEATURE_INCOMPAT_64BIT 0x0080
104 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
105
106 /* The set of back-incompatible features this driver DOES support. Add (OR)
107 * flags here as the related features are implemented into the driver. */
108 #define EXT2_DRIVER_SUPPORTED_INCOMPAT ( EXT2_FEATURE_INCOMPAT_FILETYPE \
109 | EXT4_FEATURE_INCOMPAT_EXTENTS \
110 | EXT4_FEATURE_INCOMPAT_FLEX_BG )
111 /* List of rationales for the ignored "incompatible" features:
112 * needs_recovery: Not really back-incompatible - was added as such to forbid
113 * ext2 drivers from mounting an ext3 volume with a dirty
114 * journal because they will ignore the journal, but the next
115 * ext3 driver to mount the volume will find the journal and
116 * replay it, potentially corrupting the metadata written by
117 * the ext2 drivers. Safe to ignore for this RO driver. */
118 #define EXT2_DRIVER_IGNORED_INCOMPAT ( EXT3_FEATURE_INCOMPAT_RECOVER )
119
120
121 #define EXT3_JOURNAL_MAGIC_NUMBER 0xc03b3998U
122
123 #define EXT3_JOURNAL_DESCRIPTOR_BLOCK 1
124 #define EXT3_JOURNAL_COMMIT_BLOCK 2
125 #define EXT3_JOURNAL_SUPERBLOCK_V1 3
126 #define EXT3_JOURNAL_SUPERBLOCK_V2 4
127 #define EXT3_JOURNAL_REVOKE_BLOCK 5
128
129 #define EXT3_JOURNAL_FLAG_ESCAPE 1
130 #define EXT3_JOURNAL_FLAG_SAME_UUID 2
131 #define EXT3_JOURNAL_FLAG_DELETED 4
132 #define EXT3_JOURNAL_FLAG_LAST_TAG 8
133
134 #define EXT4_EXTENTS_FLAG 0x80000
135
136 /* The ext2 superblock. */
137 struct grub_ext2_sblock
138 {
139 grub_uint32_t total_inodes;
140 grub_uint32_t total_blocks;
141 grub_uint32_t reserved_blocks;
142 grub_uint32_t free_blocks;
143 grub_uint32_t free_inodes;
144 grub_uint32_t first_data_block;
145 grub_uint32_t log2_block_size;
146 grub_uint32_t log2_fragment_size;
147 grub_uint32_t blocks_per_group;
148 grub_uint32_t fragments_per_group;
149 grub_uint32_t inodes_per_group;
150 grub_uint32_t mtime;
151 grub_uint32_t utime;
152 grub_uint16_t mnt_count;
153 grub_uint16_t max_mnt_count;
154 grub_uint16_t magic;
155 grub_uint16_t fs_state;
156 grub_uint16_t error_handling;
157 grub_uint16_t minor_revision_level;
158 grub_uint32_t lastcheck;
159 grub_uint32_t checkinterval;
160 grub_uint32_t creator_os;
161 grub_uint32_t revision_level;
162 grub_uint16_t uid_reserved;
163 grub_uint16_t gid_reserved;
164 grub_uint32_t first_inode;
165 grub_uint16_t inode_size;
166 grub_uint16_t block_group_number;
167 grub_uint32_t feature_compatibility;
168 grub_uint32_t feature_incompat;
169 grub_uint32_t feature_ro_compat;
170 grub_uint16_t uuid[8];
171 char volume_name[16];
172 char last_mounted_on[64];
173 grub_uint32_t compression_info;
174 grub_uint8_t prealloc_blocks;
175 grub_uint8_t prealloc_dir_blocks;
176 grub_uint16_t reserved_gdt_blocks;
177 grub_uint8_t journal_uuid[16];
178 grub_uint32_t journal_inum;
179 grub_uint32_t journal_dev;
180 grub_uint32_t last_orphan;
181 grub_uint32_t hash_seed[4];
182 grub_uint8_t def_hash_version;
183 grub_uint8_t jnl_backup_type;
184 grub_uint16_t reserved_word_pad;
185 grub_uint32_t default_mount_opts;
186 grub_uint32_t first_meta_bg;
187 grub_uint32_t mkfs_time;
188 grub_uint32_t jnl_blocks[17];
189 };
190
191 /* The ext2 blockgroup. */
192 struct grub_ext2_block_group
193 {
194 grub_uint32_t block_id;
195 grub_uint32_t inode_id;
196 grub_uint32_t inode_table_id;
197 grub_uint16_t free_blocks;
198 grub_uint16_t free_inodes;
199 grub_uint16_t used_dirs;
200 grub_uint16_t pad;
201 grub_uint32_t reserved[3];
202 };
203
204 /* The ext2 inode. */
205 struct grub_ext2_inode
206 {
207 grub_uint16_t mode;
208 grub_uint16_t uid;
209 grub_uint32_t size;
210 grub_uint32_t atime;
211 grub_uint32_t ctime;
212 grub_uint32_t mtime;
213 grub_uint32_t dtime;
214 grub_uint16_t gid;
215 grub_uint16_t nlinks;
216 grub_uint32_t blockcnt; /* Blocks of 512 bytes!! */
217 grub_uint32_t flags;
218 grub_uint32_t osd1;
219 union
220 {
221 struct datablocks
222 {
223 grub_uint32_t dir_blocks[INDIRECT_BLOCKS];
224 grub_uint32_t indir_block;
225 grub_uint32_t double_indir_block;
226 grub_uint32_t triple_indir_block;
227 } blocks;
228 char symlink[60];
229 };
230 grub_uint32_t version;
231 grub_uint32_t acl;
232 grub_uint32_t dir_acl;
233 grub_uint32_t fragment_addr;
234 grub_uint32_t osd2[3];
235 };
236
237 /* The header of an ext2 directory entry. */
238 struct ext2_dirent
239 {
240 grub_uint32_t inode;
241 grub_uint16_t direntlen;
242 grub_uint8_t namelen;
243 grub_uint8_t filetype;
244 };
245
246 struct grub_ext3_journal_header
247 {
248 grub_uint32_t magic;
249 grub_uint32_t block_type;
250 grub_uint32_t sequence;
251 };
252
253 struct grub_ext3_journal_revoke_header
254 {
255 struct grub_ext3_journal_header header;
256 grub_uint32_t count;
257 grub_uint32_t data[0];
258 };
259
260 struct grub_ext3_journal_block_tag
261 {
262 grub_uint32_t block;
263 grub_uint32_t flags;
264 };
265
266 struct grub_ext3_journal_sblock
267 {
268 struct grub_ext3_journal_header header;
269 grub_uint32_t block_size;
270 grub_uint32_t maxlen;
271 grub_uint32_t first;
272 grub_uint32_t sequence;
273 grub_uint32_t start;
274 };
275
276 #define EXT4_EXT_MAGIC 0xf30a
277
278 struct grub_ext4_extent_header
279 {
280 grub_uint16_t magic;
281 grub_uint16_t entries;
282 grub_uint16_t max;
283 grub_uint16_t depth;
284 grub_uint32_t generation;
285 };
286
287 struct grub_ext4_extent
288 {
289 grub_uint32_t block;
290 grub_uint16_t len;
291 grub_uint16_t start_hi;
292 grub_uint32_t start;
293 };
294
295 struct grub_ext4_extent_idx
296 {
297 grub_uint32_t block;
298 grub_uint32_t leaf;
299 grub_uint16_t leaf_hi;
300 grub_uint16_t unused;
301 };
302
303 struct grub_fshelp_node
304 {
305 struct grub_ext2_data *data;
306 struct grub_ext2_inode inode;
307 int ino;
308 int inode_read;
309 };
310
311 /* Information about a "mounted" ext2 filesystem. */
312 struct grub_ext2_data
313 {
314 struct grub_ext2_sblock sblock;
315 grub_disk_t disk;
316 struct grub_ext2_inode *inode;
317 struct grub_fshelp_node diropen;
318 };
319
320 /* Read into BLKGRP the blockgroup descriptor of blockgroup GROUP of
321 the mounted filesystem DATA. */
322 inline static grub_err_t
grub_ext2_blockgroup(struct grub_ext2_data * data,int group,struct grub_ext2_block_group * blkgrp)323 grub_ext2_blockgroup (struct grub_ext2_data *data, int group,
324 struct grub_ext2_block_group *blkgrp)
325 {
326 return grub_disk_read (data->disk,
327 ((grub_le_to_cpu32 (data->sblock.first_data_block) + 1)
328 << LOG2_EXT2_BLOCK_SIZE (data)),
329 group * sizeof (struct grub_ext2_block_group),
330 sizeof (struct grub_ext2_block_group), blkgrp);
331 }
332
333 static struct grub_ext4_extent_header *
grub_ext4_find_leaf(struct grub_ext2_data * data,char * buf,struct grub_ext4_extent_header * ext_block,grub_uint32_t fileblock)334 grub_ext4_find_leaf (struct grub_ext2_data *data, char *buf,
335 struct grub_ext4_extent_header *ext_block,
336 grub_uint32_t fileblock)
337 {
338 struct grub_ext4_extent_idx *index;
339
340 while (1)
341 {
342 int i;
343 grub_disk_addr_t block;
344
345 index = (struct grub_ext4_extent_idx *) (ext_block + 1);
346
347 if (grub_le_to_cpu16(ext_block->magic) != EXT4_EXT_MAGIC)
348 return 0;
349
350 if (ext_block->depth == 0)
351 return ext_block;
352
353 for (i = 0; i < grub_le_to_cpu16 (ext_block->entries); i++)
354 {
355 if (fileblock < grub_le_to_cpu32(index[i].block))
356 break;
357 }
358
359 if (--i < 0)
360 return 0;
361
362 block = grub_le_to_cpu16 (index[i].leaf_hi);
363 block = (block << 32) + grub_le_to_cpu32 (index[i].leaf);
364 if (grub_disk_read (data->disk,
365 block << LOG2_EXT2_BLOCK_SIZE (data),
366 0, EXT2_BLOCK_SIZE(data), buf)) {
367 return 0;
368 }
369
370 ext_block = (struct grub_ext4_extent_header *) buf;
371 }
372 }
373
374 static grub_disk_addr_t
grub_ext2_read_block(grub_fshelp_node_t node,grub_disk_addr_t fileblock)375 grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
376 {
377 struct grub_ext2_data *data = node->data;
378 struct grub_ext2_inode *inode = &node->inode;
379 int blknr = -1;
380 unsigned int blksz = EXT2_BLOCK_SIZE (data);
381 int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
382
383 if (grub_le_to_cpu32(inode->flags) & EXT4_EXTENTS_FLAG)
384 {
385 char * buf = grub_malloc (EXT2_BLOCK_SIZE (data));
386 if (!buf) {
387 return -1;
388 }
389 struct grub_ext4_extent_header *leaf;
390 struct grub_ext4_extent *ext;
391 int i;
392
393 leaf = grub_ext4_find_leaf (data, buf,
394 (struct grub_ext4_extent_header *) inode->blocks.dir_blocks,
395 fileblock);
396 if (! leaf)
397 {
398 grub_error (GRUB_ERR_BAD_FS, "invalid extent");
399 grub_free (buf);
400 return -1;
401 }
402
403 ext = (struct grub_ext4_extent *) (leaf + 1);
404 for (i = 0; i < grub_le_to_cpu16 (leaf->entries); i++)
405 {
406 if (fileblock < grub_le_to_cpu32 (ext[i].block))
407 break;
408 }
409
410 if (--i >= 0)
411 {
412 fileblock -= grub_le_to_cpu32 (ext[i].block);
413 if (fileblock >= grub_le_to_cpu16 (ext[i].len)) {
414 grub_free (buf);
415 return 0;
416 } else
417 {
418 grub_disk_addr_t start;
419
420 start = grub_le_to_cpu16 (ext[i].start_hi);
421 start = (start << 32) + grub_le_to_cpu32 (ext[i].start);
422 grub_free (buf);
423
424 return fileblock + start;
425 }
426 }
427 else
428 {
429 grub_error (GRUB_ERR_BAD_FS, "something wrong with extent");
430 grub_free (buf);
431 return -1;
432 }
433 }
434 /* Direct blocks. */
435 if (fileblock < INDIRECT_BLOCKS) {
436 blknr = grub_le_to_cpu32 (inode->blocks.dir_blocks[fileblock]);
437 /* Indirect. */
438 } else if (fileblock < INDIRECT_BLOCKS + blksz / 4)
439 {
440 grub_uint32_t *indir;
441
442 indir = grub_malloc (blksz);
443 if (! indir) {
444 return grub_errno;
445 }
446
447 if (grub_disk_read (data->disk,
448 ((grub_disk_addr_t)
449 grub_le_to_cpu32 (inode->blocks.indir_block))
450 << log2_blksz,
451 0, blksz, indir)) {
452 return grub_errno;
453 }
454
455 blknr = grub_le_to_cpu32 (indir[fileblock - INDIRECT_BLOCKS]);
456 grub_free (indir);
457 }
458 /* Double indirect. */
459 else if (fileblock < (grub_disk_addr_t)(INDIRECT_BLOCKS + blksz / 4) \
460 * (grub_disk_addr_t)(blksz / 4 + 1))
461 {
462 unsigned int perblock = blksz / 4;
463 unsigned int rblock = fileblock - (INDIRECT_BLOCKS
464 + blksz / 4);
465 grub_uint32_t *indir;
466
467 indir = grub_malloc (blksz);
468 if (! indir) {
469 return grub_errno;
470 }
471
472 if (grub_disk_read (data->disk,
473 ((grub_disk_addr_t)
474 grub_le_to_cpu32 (inode->blocks.double_indir_block))
475 << log2_blksz,
476 0, blksz, indir)) {
477 return grub_errno;
478 }
479
480 if (grub_disk_read (data->disk,
481 ((grub_disk_addr_t)
482 grub_le_to_cpu32 (indir[rblock / perblock]))
483 << log2_blksz,
484 0, blksz, indir)) {
485 return grub_errno;
486 }
487
488 blknr = grub_le_to_cpu32 (indir[rblock % perblock]);
489 grub_free (indir);
490 }
491 /* triple indirect. */
492 else
493 {
494 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
495 "ext2fs doesn't support triple indirect blocks");
496 }
497
498 return blknr;
499 }
500
501 /* Read LEN bytes from the file described by DATA starting with byte
502 POS. Return the amount of read bytes in READ. */
503 static grub_ssize_t
grub_ext2_read_file(grub_fshelp_node_t node,void (* read_hook)(grub_disk_addr_t sector,unsigned offset,unsigned length,void * closure),void * closure,int flags,int pos,grub_size_t len,char * buf)504 grub_ext2_read_file (grub_fshelp_node_t node,
505 void (*read_hook) (grub_disk_addr_t sector,
506 unsigned offset, unsigned length, void *closure),
507 void *closure, int flags, int pos, grub_size_t len, char *buf)
508 {
509 return grub_fshelp_read_file (node->data->disk, node, read_hook, closure,
510 flags, pos, len, buf, grub_ext2_read_block,
511 node->inode.size, LOG2_EXT2_BLOCK_SIZE (node->data));
512 }
513
514
515 /* Read the inode INO for the file described by DATA into INODE. */
516 static grub_err_t
grub_ext2_read_inode(struct grub_ext2_data * data,int ino,struct grub_ext2_inode * inode)517 grub_ext2_read_inode (struct grub_ext2_data *data,
518 int ino, struct grub_ext2_inode *inode)
519 {
520 struct grub_ext2_block_group blkgrp;
521 struct grub_ext2_sblock *sblock = &data->sblock;
522 int inodes_per_block;
523 unsigned int blkno;
524 unsigned int blkoff;
525
526 /* It is easier to calculate if the first inode is 0. */
527 ino--;
528 int div = grub_le_to_cpu32 (sblock->inodes_per_group);
529 if (div < 1) {
530 return grub_errno = GRUB_ERR_BAD_FS;
531 }
532 grub_ext2_blockgroup (data, ino / div, &blkgrp);
533 if (grub_errno)
534 return grub_errno;
535
536 int inode_size = EXT2_INODE_SIZE (data);
537 if (inode_size < 1) {
538 return grub_errno = GRUB_ERR_BAD_FS;
539 }
540 inodes_per_block = EXT2_BLOCK_SIZE (data) / inode_size;
541 if (inodes_per_block < 1) {
542 return grub_errno = GRUB_ERR_BAD_FS;
543 }
544 blkno = (ino % grub_le_to_cpu32 (sblock->inodes_per_group))
545 / inodes_per_block;
546 blkoff = (ino % grub_le_to_cpu32 (sblock->inodes_per_group))
547 % inodes_per_block;
548
549 /* Read the inode. */
550 if (grub_disk_read (data->disk,
551 ((grub_le_to_cpu32 (blkgrp.inode_table_id) + blkno)
552 << LOG2_EXT2_BLOCK_SIZE (data)),
553 EXT2_INODE_SIZE (data) * blkoff,
554 sizeof (struct grub_ext2_inode), inode))
555 return grub_errno;
556
557 return 0;
558 }
559
560 static struct grub_ext2_data *
grub_ext2_mount(grub_disk_t disk)561 grub_ext2_mount (grub_disk_t disk)
562 {
563 struct grub_ext2_data *data;
564
565 data = grub_malloc (sizeof (struct grub_ext2_data));
566 if (!data)
567 return 0;
568
569 /* Read the superblock. */
570 grub_disk_read (disk, 1 * 2, 0, sizeof (struct grub_ext2_sblock),
571 &data->sblock);
572 if (grub_errno)
573 goto fail;
574
575 /* Make sure this is an ext2 filesystem. */
576 if (grub_le_to_cpu16 (data->sblock.magic) != EXT2_MAGIC)
577 {
578 grub_error (GRUB_ERR_BAD_FS, "not an ext2 filesystem");
579 goto fail;
580 }
581
582 /* Check the FS doesn't have feature bits enabled that we don't support. */
583 if (grub_le_to_cpu32 (data->sblock.feature_incompat)
584 & ~(EXT2_DRIVER_SUPPORTED_INCOMPAT | EXT2_DRIVER_IGNORED_INCOMPAT))
585 {
586 grub_error (GRUB_ERR_BAD_FS, "filesystem has unsupported incompatible features");
587 goto fail;
588 }
589
590
591 data->disk = disk;
592
593 data->diropen.data = data;
594 data->diropen.ino = 2;
595 data->diropen.inode_read = 1;
596
597 data->inode = &data->diropen.inode;
598
599 grub_ext2_read_inode (data, 2, data->inode);
600 if (grub_errno)
601 goto fail;
602
603 return data;
604
605 fail:
606 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
607 grub_error (GRUB_ERR_BAD_FS, "not an ext2 filesystem");
608
609 grub_free (data);
610 return 0;
611 }
612
613 static char *
grub_ext2_read_symlink(grub_fshelp_node_t node)614 grub_ext2_read_symlink (grub_fshelp_node_t node)
615 {
616 char *symlink;
617 struct grub_fshelp_node *diro = node;
618
619 if (! diro->inode_read)
620 {
621 grub_ext2_read_inode (diro->data, diro->ino, &diro->inode);
622 if (grub_errno)
623 return 0;
624 }
625
626 symlink = grub_malloc (grub_le_to_cpu32 (diro->inode.size) + 1);
627 if (! symlink)
628 return 0;
629
630 /* If the filesize of the symlink is bigger than
631 60 the symlink is stored in a separate block,
632 otherwise it is stored in the inode. */
633 if (grub_le_to_cpu32 (diro->inode.size) <= 60)
634 grub_strncpy (symlink,
635 diro->inode.symlink,
636 grub_le_to_cpu32 (diro->inode.size));
637 else
638 {
639 grub_ext2_read_file (diro, 0, 0, 0, 0,
640 grub_le_to_cpu32 (diro->inode.size),
641 symlink);
642 if (grub_errno)
643 {
644 grub_free (symlink);
645 return 0;
646 }
647 }
648
649 symlink[grub_le_to_cpu32 (diro->inode.size)] = '\0';
650 return symlink;
651 }
652
653 static int
grub_ext2_iterate_dir(grub_fshelp_node_t dir,int (* hook)(const char * filename,enum grub_fshelp_filetype filetype,grub_fshelp_node_t node,void * closure),void * closure)654 grub_ext2_iterate_dir (grub_fshelp_node_t dir,
655 int (*hook) (const char *filename,
656 enum grub_fshelp_filetype filetype,
657 grub_fshelp_node_t node,
658 void *closure),
659 void *closure)
660 {
661 unsigned int fpos = 0;
662 struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
663
664 if (! diro->inode_read)
665 {
666 grub_ext2_read_inode (diro->data, diro->ino, &diro->inode);
667 if (grub_errno)
668 return 0;
669 }
670
671 /* Search the file. */
672 if (hook)
673 while (fpos < grub_le_to_cpu32 (diro->inode.size))
674 {
675 struct ext2_dirent dirent;
676
677 grub_ext2_read_file (diro, NULL, NULL, 0, fpos, sizeof (dirent),
678 (char *) &dirent);
679 if (grub_errno)
680 return 0;
681
682 if (dirent.direntlen == 0)
683 return 0;
684
685 if (dirent.namelen != 0)
686 {
687 char * filename = grub_malloc (dirent.namelen + 1);
688 struct grub_fshelp_node *fdiro;
689 enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
690
691 if (!filename) {
692 break;
693 }
694 grub_ext2_read_file (diro, 0, 0, 0,
695 fpos + sizeof (struct ext2_dirent),
696 dirent.namelen, filename);
697 if (grub_errno) {
698 grub_free (filename);
699 return 0;
700 }
701
702 fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
703 if (! fdiro) {
704 grub_free (filename);
705 return 0;
706 }
707
708 fdiro->data = diro->data;
709 fdiro->ino = grub_le_to_cpu32 (dirent.inode);
710
711 filename[dirent.namelen] = '\0';
712
713 if (dirent.filetype != FILETYPE_UNKNOWN)
714 {
715 fdiro->inode_read = 0;
716
717 if (dirent.filetype == FILETYPE_DIRECTORY)
718 type = GRUB_FSHELP_DIR;
719 else if (dirent.filetype == FILETYPE_SYMLINK)
720 type = GRUB_FSHELP_SYMLINK;
721 else if (dirent.filetype == FILETYPE_REG)
722 type = GRUB_FSHELP_REG;
723 }
724 else
725 {
726 /* The filetype can not be read from the dirent, read
727 the inode to get more information. */
728 grub_ext2_read_inode (diro->data,
729 grub_le_to_cpu32 (dirent.inode),
730 &fdiro->inode);
731 if (grub_errno) {
732 grub_free (filename);
733 grub_free (fdiro);
734 return 0;
735 }
736
737 fdiro->inode_read = 1;
738
739 if ((grub_le_to_cpu16 (fdiro->inode.mode)
740 & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
741 type = GRUB_FSHELP_DIR;
742 else if ((grub_le_to_cpu16 (fdiro->inode.mode)
743 & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
744 type = GRUB_FSHELP_SYMLINK;
745 else if ((grub_le_to_cpu16 (fdiro->inode.mode)
746 & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
747 type = GRUB_FSHELP_REG;
748 }
749
750 if (hook (filename, type, fdiro, closure)) {
751 grub_free (filename);
752 return 1;
753 }
754 grub_free (filename);
755 }
756
757 fpos += grub_le_to_cpu16 (dirent.direntlen);
758 }
759
760 return 0;
761 }
762
763 /* Open a file named NAME and initialize FILE. */
764 static grub_err_t
grub_ext2_open(struct grub_file * file,const char * name)765 grub_ext2_open (struct grub_file *file, const char *name)
766 {
767 struct grub_ext2_data *data;
768 struct grub_fshelp_node *fdiro = 0;
769
770 data = grub_ext2_mount (file->device->disk);
771 if (! data)
772 goto fail;
773
774 grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_ext2_iterate_dir, 0,
775 grub_ext2_read_symlink, GRUB_FSHELP_REG);
776 if (grub_errno)
777 goto fail;
778
779 if (! fdiro->inode_read)
780 {
781 grub_ext2_read_inode (data, fdiro->ino, &fdiro->inode);
782 if (grub_errno)
783 goto fail;
784 }
785
786 grub_memcpy (data->inode, &fdiro->inode, sizeof (struct grub_ext2_inode));
787 grub_free (fdiro);
788
789 file->size = grub_le_to_cpu32 (data->inode->size);
790 file->data = data;
791 file->offset = 0;
792
793 return 0;
794
795 fail:
796 if (fdiro != &data->diropen)
797 grub_free (fdiro);
798 grub_free (data);
799
800 return grub_errno;
801 }
802
803 static grub_err_t
grub_ext2_close(grub_file_t file)804 grub_ext2_close (grub_file_t file)
805 {
806 grub_free (file->data);
807
808 return GRUB_ERR_NONE;
809 }
810
811 /* Read LEN bytes data from FILE into BUF. */
812 static grub_ssize_t
grub_ext2_read(grub_file_t file,char * buf,grub_size_t len)813 grub_ext2_read (grub_file_t file, char *buf, grub_size_t len)
814 {
815 struct grub_ext2_data *data = (struct grub_ext2_data *) file->data;
816 return grub_ext2_read_file (&data->diropen, file->read_hook, file->closure,
817 file->flags, file->offset, len, buf);
818 }
819
820 struct grub_ext2_dir_closure
821 {
822 int (*hook) (const char *filename,
823 const struct grub_dirhook_info *info,
824 void *closure);
825 void *closure;
826 struct grub_ext2_data *data;
827 };
828
829 static int
iterate(const char * filename,enum grub_fshelp_filetype filetype,grub_fshelp_node_t node,void * closure)830 iterate (const char *filename,
831 enum grub_fshelp_filetype filetype,
832 grub_fshelp_node_t node,
833 void *closure)
834 {
835 struct grub_ext2_dir_closure *c = closure;
836 struct grub_dirhook_info info;
837 grub_memset (&info, 0, sizeof (info));
838 if (! node->inode_read)
839 {
840 grub_ext2_read_inode (c->data, node->ino, &node->inode);
841 if (!grub_errno)
842 node->inode_read = 1;
843 grub_errno = GRUB_ERR_NONE;
844 }
845 if (node->inode_read)
846 {
847 info.mtimeset = 1;
848 info.mtime = grub_le_to_cpu32 (node->inode.mtime);
849 }
850
851 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
852 grub_free (node);
853 return (c->hook != NULL)? c->hook (filename, &info, c->closure): 0;
854 }
855
856 static grub_err_t
grub_ext2_dir(grub_device_t device,const char * path,int (* hook)(const char * filename,const struct grub_dirhook_info * info,void * closure),void * closure)857 grub_ext2_dir (grub_device_t device, const char *path,
858 int (*hook) (const char *filename,
859 const struct grub_dirhook_info *info,
860 void *closure),
861 void *closure)
862 {
863 struct grub_ext2_data *data = 0;
864 struct grub_fshelp_node *fdiro = 0;
865 struct grub_ext2_dir_closure c;
866
867 data = grub_ext2_mount (device->disk);
868 if (! data)
869 goto fail;
870
871 grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_ext2_iterate_dir,
872 0, grub_ext2_read_symlink, GRUB_FSHELP_DIR);
873 if (grub_errno)
874 goto fail;
875
876 c.hook = hook;
877 c.closure = closure;
878 c.data = data;
879 grub_ext2_iterate_dir (fdiro, iterate, &c);
880
881 fail:
882 if (fdiro != &data->diropen)
883 grub_free (fdiro);
884 grub_free (data);
885
886 return grub_errno;
887 }
888
889 static grub_err_t
grub_ext2_label(grub_device_t device,char ** label)890 grub_ext2_label (grub_device_t device, char **label)
891 {
892 struct grub_ext2_data *data;
893 grub_disk_t disk = device->disk;
894
895 data = grub_ext2_mount (disk);
896 if (data)
897 *label = grub_strndup (data->sblock.volume_name, 14);
898 else
899 *label = NULL;
900
901 grub_free (data);
902
903 return grub_errno;
904 }
905
906 static grub_err_t
grub_ext2_uuid(grub_device_t device,char ** uuid)907 grub_ext2_uuid (grub_device_t device, char **uuid)
908 {
909 struct grub_ext2_data *data;
910 grub_disk_t disk = device->disk;
911
912 data = grub_ext2_mount (disk);
913 if (data)
914 {
915 *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
916 grub_be_to_cpu16 (data->sblock.uuid[0]),
917 grub_be_to_cpu16 (data->sblock.uuid[1]),
918 grub_be_to_cpu16 (data->sblock.uuid[2]),
919 grub_be_to_cpu16 (data->sblock.uuid[3]),
920 grub_be_to_cpu16 (data->sblock.uuid[4]),
921 grub_be_to_cpu16 (data->sblock.uuid[5]),
922 grub_be_to_cpu16 (data->sblock.uuid[6]),
923 grub_be_to_cpu16 (data->sblock.uuid[7]));
924 }
925 else
926 *uuid = NULL;
927
928 grub_free (data);
929
930 return grub_errno;
931 }
932
933 /* Get mtime. */
934 static grub_err_t
grub_ext2_mtime(grub_device_t device,grub_int32_t * tm)935 grub_ext2_mtime (grub_device_t device, grub_int32_t *tm)
936 {
937 struct grub_ext2_data *data;
938 grub_disk_t disk = device->disk;
939
940 data = grub_ext2_mount (disk);
941 if (!data)
942 *tm = 0;
943 else
944 *tm = grub_le_to_cpu32 (data->sblock.utime);
945
946 grub_free (data);
947
948 return grub_errno;
949
950 }
951
952
953
954 struct grub_fs grub_ext2_fs =
955 {
956 .name = "ext2",
957 .dir = grub_ext2_dir,
958 .open = grub_ext2_open,
959 .read = grub_ext2_read,
960 .close = grub_ext2_close,
961 .label = grub_ext2_label,
962 .uuid = grub_ext2_uuid,
963 .mtime = grub_ext2_mtime,
964 #ifdef GRUB_UTIL
965 .reserved_first_sector = 1,
966 #endif
967 .next = 0
968 };
969