]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/fs/ext2.c
keep up with 1.0.67 (#1464)
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / fs / ext2.c
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
25 /* The good old revision and the default inode size. */
26 #define EXT2_GOOD_OLD_REVISION 0
27 #define EXT2_GOOD_OLD_INODE_SIZE 128
28
29 /* Filetype used in directory entry. */
30 #define FILETYPE_UNKNOWN 0
31 #define FILETYPE_REG 1
32 #define FILETYPE_DIRECTORY 2
33 #define FILETYPE_SYMLINK 7
34
35 /* Filetype information as used in inodes. */
36 #define FILETYPE_INO_MASK 0170000
37 #define FILETYPE_INO_REG 0100000
38 #define FILETYPE_INO_DIRECTORY 0040000
39 #define FILETYPE_INO_SYMLINK 0120000
40
41 #include <grub/err.h>
42 #include <grub/file.h>
43 #include <grub/mm.h>
44 #include <grub/misc.h>
45 #include <grub/disk.h>
46 #include <grub/dl.h>
47 #include <grub/types.h>
48 #include <grub/fshelp.h>
49 #include <grub/ventoy.h>
50
51 GRUB_MOD_LICENSE ("GPLv3+");
52
53 /* Log2 size of ext2 block in 512 blocks. */
54 #define LOG2_EXT2_BLOCK_SIZE(data) \
55 (grub_le_to_cpu32 (data->sblock.log2_block_size) + 1)
56
57 /* Log2 size of ext2 block in bytes. */
58 #define LOG2_BLOCK_SIZE(data) \
59 (grub_le_to_cpu32 (data->sblock.log2_block_size) + 10)
60
61 /* The size of an ext2 block in bytes. */
62 #define EXT2_BLOCK_SIZE(data) (1U << LOG2_BLOCK_SIZE (data))
63
64 /* The revision level. */
65 #define EXT2_REVISION(data) grub_le_to_cpu32 (data->sblock.revision_level)
66
67 /* The inode size. */
68 #define EXT2_INODE_SIZE(data) \
69 (data->sblock.revision_level \
70 == grub_cpu_to_le32_compile_time (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_MMP 0x0100
105 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
106 #define EXT4_FEATURE_INCOMPAT_ENCRYPT 0x10000
107
108 /* The set of back-incompatible features this driver DOES support. Add (OR)
109 * flags here as the related features are implemented into the driver. */
110 #define EXT2_DRIVER_SUPPORTED_INCOMPAT ( EXT2_FEATURE_INCOMPAT_FILETYPE \
111 | EXT4_FEATURE_INCOMPAT_EXTENTS \
112 | EXT4_FEATURE_INCOMPAT_FLEX_BG \
113 | EXT2_FEATURE_INCOMPAT_META_BG \
114 | EXT4_FEATURE_INCOMPAT_64BIT \
115 | EXT4_FEATURE_INCOMPAT_ENCRYPT)
116 /* List of rationales for the ignored "incompatible" features:
117 * needs_recovery: Not really back-incompatible - was added as such to forbid
118 * ext2 drivers from mounting an ext3 volume with a dirty
119 * journal because they will ignore the journal, but the next
120 * ext3 driver to mount the volume will find the journal and
121 * replay it, potentially corrupting the metadata written by
122 * the ext2 drivers. Safe to ignore for this RO driver.
123 * mmp: Not really back-incompatible - was added as such to
124 * avoid multiple read-write mounts. Safe to ignore for this
125 * RO driver.
126 */
127 #define EXT2_DRIVER_IGNORED_INCOMPAT ( EXT3_FEATURE_INCOMPAT_RECOVER \
128 | EXT4_FEATURE_INCOMPAT_MMP)
129
130
131 #define EXT3_JOURNAL_MAGIC_NUMBER 0xc03b3998U
132
133 #define EXT3_JOURNAL_DESCRIPTOR_BLOCK 1
134 #define EXT3_JOURNAL_COMMIT_BLOCK 2
135 #define EXT3_JOURNAL_SUPERBLOCK_V1 3
136 #define EXT3_JOURNAL_SUPERBLOCK_V2 4
137 #define EXT3_JOURNAL_REVOKE_BLOCK 5
138
139 #define EXT3_JOURNAL_FLAG_ESCAPE 1
140 #define EXT3_JOURNAL_FLAG_SAME_UUID 2
141 #define EXT3_JOURNAL_FLAG_DELETED 4
142 #define EXT3_JOURNAL_FLAG_LAST_TAG 8
143
144 #define EXT4_ENCRYPT_FLAG 0x800
145 #define EXT4_EXTENTS_FLAG 0x80000
146
147 /* The ext2 superblock. */
148 struct grub_ext2_sblock
149 {
150 grub_uint32_t total_inodes;
151 grub_uint32_t total_blocks;
152 grub_uint32_t reserved_blocks;
153 grub_uint32_t free_blocks;
154 grub_uint32_t free_inodes;
155 grub_uint32_t first_data_block;
156 grub_uint32_t log2_block_size;
157 grub_uint32_t log2_fragment_size;
158 grub_uint32_t blocks_per_group;
159 grub_uint32_t fragments_per_group;
160 grub_uint32_t inodes_per_group;
161 grub_uint32_t mtime;
162 grub_uint32_t utime;
163 grub_uint16_t mnt_count;
164 grub_uint16_t max_mnt_count;
165 grub_uint16_t magic;
166 grub_uint16_t fs_state;
167 grub_uint16_t error_handling;
168 grub_uint16_t minor_revision_level;
169 grub_uint32_t lastcheck;
170 grub_uint32_t checkinterval;
171 grub_uint32_t creator_os;
172 grub_uint32_t revision_level;
173 grub_uint16_t uid_reserved;
174 grub_uint16_t gid_reserved;
175 grub_uint32_t first_inode;
176 grub_uint16_t inode_size;
177 grub_uint16_t block_group_number;
178 grub_uint32_t feature_compatibility;
179 grub_uint32_t feature_incompat;
180 grub_uint32_t feature_ro_compat;
181 grub_uint16_t uuid[8];
182 char volume_name[16];
183 char last_mounted_on[64];
184 grub_uint32_t compression_info;
185 grub_uint8_t prealloc_blocks;
186 grub_uint8_t prealloc_dir_blocks;
187 grub_uint16_t reserved_gdt_blocks;
188 grub_uint8_t journal_uuid[16];
189 grub_uint32_t journal_inum;
190 grub_uint32_t journal_dev;
191 grub_uint32_t last_orphan;
192 grub_uint32_t hash_seed[4];
193 grub_uint8_t def_hash_version;
194 grub_uint8_t jnl_backup_type;
195 grub_uint16_t group_desc_size;
196 grub_uint32_t default_mount_opts;
197 grub_uint32_t first_meta_bg;
198 grub_uint32_t mkfs_time;
199 grub_uint32_t jnl_blocks[17];
200 };
201
202 /* The ext2 blockgroup. */
203 struct grub_ext2_block_group
204 {
205 grub_uint32_t block_id;
206 grub_uint32_t inode_id;
207 grub_uint32_t inode_table_id;
208 grub_uint16_t free_blocks;
209 grub_uint16_t free_inodes;
210 grub_uint16_t used_dirs;
211 grub_uint16_t pad;
212 grub_uint32_t reserved[3];
213 grub_uint32_t block_id_hi;
214 grub_uint32_t inode_id_hi;
215 grub_uint32_t inode_table_id_hi;
216 grub_uint16_t free_blocks_hi;
217 grub_uint16_t free_inodes_hi;
218 grub_uint16_t used_dirs_hi;
219 grub_uint16_t pad2;
220 grub_uint32_t reserved2[3];
221 };
222
223 /* The ext2 inode. */
224 struct grub_ext2_inode
225 {
226 grub_uint16_t mode;
227 grub_uint16_t uid;
228 grub_uint32_t size;
229 grub_uint32_t atime;
230 grub_uint32_t ctime;
231 grub_uint32_t mtime;
232 grub_uint32_t dtime;
233 grub_uint16_t gid;
234 grub_uint16_t nlinks;
235 grub_uint32_t blockcnt; /* Blocks of 512 bytes!! */
236 grub_uint32_t flags;
237 grub_uint32_t osd1;
238 union
239 {
240 struct datablocks
241 {
242 grub_uint32_t dir_blocks[INDIRECT_BLOCKS];
243 grub_uint32_t indir_block;
244 grub_uint32_t double_indir_block;
245 grub_uint32_t triple_indir_block;
246 } blocks;
247 char symlink[60];
248 };
249 grub_uint32_t version;
250 grub_uint32_t acl;
251 grub_uint32_t size_high;
252 grub_uint32_t fragment_addr;
253 grub_uint32_t osd2[3];
254 };
255
256 /* The header of an ext2 directory entry. */
257 struct ext2_dirent
258 {
259 grub_uint32_t inode;
260 grub_uint16_t direntlen;
261 #define MAX_NAMELEN 255
262 grub_uint8_t namelen;
263 grub_uint8_t filetype;
264 };
265
266 struct grub_ext3_journal_header
267 {
268 grub_uint32_t magic;
269 grub_uint32_t block_type;
270 grub_uint32_t sequence;
271 };
272
273 struct grub_ext3_journal_revoke_header
274 {
275 struct grub_ext3_journal_header header;
276 grub_uint32_t count;
277 grub_uint32_t data[0];
278 };
279
280 struct grub_ext3_journal_block_tag
281 {
282 grub_uint32_t block;
283 grub_uint32_t flags;
284 };
285
286 struct grub_ext3_journal_sblock
287 {
288 struct grub_ext3_journal_header header;
289 grub_uint32_t block_size;
290 grub_uint32_t maxlen;
291 grub_uint32_t first;
292 grub_uint32_t sequence;
293 grub_uint32_t start;
294 };
295
296 #define EXT4_EXT_MAGIC 0xf30a
297
298 struct grub_ext4_extent_header
299 {
300 grub_uint16_t magic;
301 grub_uint16_t entries;
302 grub_uint16_t max;
303 grub_uint16_t depth;
304 grub_uint32_t generation;
305 };
306
307 struct grub_ext4_extent
308 {
309 grub_uint32_t block;
310 grub_uint16_t len;
311 grub_uint16_t start_hi;
312 grub_uint32_t start;
313 };
314
315 struct grub_ext4_extent_idx
316 {
317 grub_uint32_t block;
318 grub_uint32_t leaf;
319 grub_uint16_t leaf_hi;
320 grub_uint16_t unused;
321 };
322
323 struct grub_fshelp_node
324 {
325 struct grub_ext2_data *data;
326 struct grub_ext2_inode inode;
327 int ino;
328 int inode_read;
329 };
330
331 /* Information about a "mounted" ext2 filesystem. */
332 struct grub_ext2_data
333 {
334 struct grub_ext2_sblock sblock;
335 int log_group_desc_size;
336 grub_disk_t disk;
337 struct grub_ext2_inode *inode;
338 struct grub_fshelp_node diropen;
339 };
340
341 static grub_dl_t my_mod;
342
343 static int g_ventoy_block_count;
344
345 /* Check is a = b^x for some x. */
346 static inline int
347 is_power_of (grub_uint64_t a, grub_uint32_t b)
348 {
349 grub_uint64_t c;
350 /* Prevent overflow assuming b < 8. */
351 if (a >= (1LL << 60))
352 return 0;
353 for (c = 1; c <= a; c *= b);
354 return (c == a);
355 }
356
357
358 static inline int
359 group_has_super_block (struct grub_ext2_data *data, grub_uint64_t group)
360 {
361 if (!(data->sblock.feature_ro_compat
362 & grub_cpu_to_le32_compile_time(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)))
363 return 1;
364 /* Algorithm looked up in Linux source. */
365 if (group <= 1)
366 return 1;
367 /* Even number is never a power of odd number. */
368 if (!(group & 1))
369 return 0;
370 return (is_power_of(group, 7) || is_power_of(group, 5) ||
371 is_power_of(group, 3));
372 }
373
374 /* Read into BLKGRP the blockgroup descriptor of blockgroup GROUP of
375 the mounted filesystem DATA. */
376 inline static grub_err_t
377 grub_ext2_blockgroup (struct grub_ext2_data *data, grub_uint64_t group,
378 struct grub_ext2_block_group *blkgrp)
379 {
380 grub_uint64_t full_offset = (group << data->log_group_desc_size);
381 grub_uint64_t block, offset;
382 block = (full_offset >> LOG2_BLOCK_SIZE (data));
383 offset = (full_offset & ((1 << LOG2_BLOCK_SIZE (data)) - 1));
384 if ((data->sblock.feature_incompat
385 & grub_cpu_to_le32_compile_time (EXT2_FEATURE_INCOMPAT_META_BG))
386 && block >= grub_le_to_cpu32(data->sblock.first_meta_bg))
387 {
388 grub_uint64_t first_block_group;
389 /* Find the first block group for which a descriptor
390 is stored in given block. */
391 first_block_group = (block << (LOG2_BLOCK_SIZE (data)
392 - data->log_group_desc_size));
393
394 block = (first_block_group
395 * grub_le_to_cpu32(data->sblock.blocks_per_group));
396
397 if (group_has_super_block (data, first_block_group))
398 block++;
399 }
400 else
401 /* Superblock. */
402 block++;
403 return grub_disk_read (data->disk,
404 ((grub_le_to_cpu32 (data->sblock.first_data_block)
405 + block)
406 << LOG2_EXT2_BLOCK_SIZE (data)), offset,
407 sizeof (struct grub_ext2_block_group), blkgrp);
408 }
409
410 static struct grub_ext4_extent_header *
411 grub_ext4_find_leaf (struct grub_ext2_data *data,
412 struct grub_ext4_extent_header *ext_block,
413 grub_uint32_t fileblock)
414 {
415 struct grub_ext4_extent_idx *index;
416 void *buf = NULL;
417
418 while (1)
419 {
420 int i;
421 grub_disk_addr_t block;
422
423 index = (struct grub_ext4_extent_idx *) (ext_block + 1);
424
425 if (ext_block->magic != grub_cpu_to_le16_compile_time (EXT4_EXT_MAGIC))
426 goto fail;
427
428 if (ext_block->depth == 0)
429 return ext_block;
430
431 for (i = 0; i < grub_le_to_cpu16 (ext_block->entries); i++)
432 {
433 if (fileblock < grub_le_to_cpu32(index[i].block))
434 break;
435 }
436
437 if (--i < 0)
438 goto fail;
439
440 block = grub_le_to_cpu16 (index[i].leaf_hi);
441 block = (block << 32) | grub_le_to_cpu32 (index[i].leaf);
442 if (!buf)
443 buf = grub_malloc (EXT2_BLOCK_SIZE(data));
444 if (!buf)
445 goto fail;
446 if (grub_disk_read (data->disk,
447 block << LOG2_EXT2_BLOCK_SIZE (data),
448 0, EXT2_BLOCK_SIZE(data), buf))
449 goto fail;
450
451 ext_block = buf;
452 }
453 fail:
454 grub_free (buf);
455 return 0;
456 }
457
458 static grub_disk_addr_t
459 grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
460 {
461 struct grub_ext2_data *data = node->data;
462 struct grub_ext2_inode *inode = &node->inode;
463 unsigned int blksz = EXT2_BLOCK_SIZE (data);
464 grub_disk_addr_t blksz_quarter = blksz / 4;
465 int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
466 int log_perblock = log2_blksz + 9 - 2;
467 grub_uint32_t indir;
468 int shift;
469
470 if (inode->flags & grub_cpu_to_le32_compile_time (EXT4_EXTENTS_FLAG))
471 {
472 struct grub_ext4_extent_header *leaf;
473 struct grub_ext4_extent *ext;
474 int i;
475 grub_disk_addr_t ret;
476
477 leaf = grub_ext4_find_leaf (data, (struct grub_ext4_extent_header *) inode->blocks.dir_blocks, fileblock);
478 if (! leaf)
479 {
480 grub_error (GRUB_ERR_BAD_FS, "invalid extent");
481 return -1;
482 }
483
484 ext = (struct grub_ext4_extent *) (leaf + 1);
485 for (i = 0; i < grub_le_to_cpu16 (leaf->entries); i++)
486 {
487 if (fileblock < grub_le_to_cpu32 (ext[i].block))
488 break;
489 }
490
491 if (--i >= 0)
492 {
493 fileblock -= grub_le_to_cpu32 (ext[i].block);
494 if (fileblock >= grub_le_to_cpu16 (ext[i].len))
495 ret = 0;
496 else
497 {
498 grub_disk_addr_t start;
499
500 start = grub_le_to_cpu16 (ext[i].start_hi);
501 start = (start << 32) + grub_le_to_cpu32 (ext[i].start);
502
503 g_ventoy_block_count = (int)(grub_le_to_cpu16 (ext[i].len) - fileblock);
504 ret = fileblock + start;
505 }
506 }
507 else
508 {
509 grub_error (GRUB_ERR_BAD_FS, "something wrong with extent");
510 ret = -1;
511 }
512
513 if (leaf != (struct grub_ext4_extent_header *) inode->blocks.dir_blocks)
514 grub_free (leaf);
515
516 return ret;
517 }
518
519 /* Direct blocks. */
520 if (fileblock < INDIRECT_BLOCKS)
521 return grub_le_to_cpu32 (inode->blocks.dir_blocks[fileblock]);
522 fileblock -= INDIRECT_BLOCKS;
523 /* Indirect. */
524 if (fileblock < blksz_quarter)
525 {
526 indir = inode->blocks.indir_block;
527 shift = 0;
528 goto indirect;
529 }
530 fileblock -= blksz_quarter;
531 /* Double indirect. */
532 if (fileblock < blksz_quarter * blksz_quarter)
533 {
534 indir = inode->blocks.double_indir_block;
535 shift = 1;
536 goto indirect;
537 }
538 fileblock -= blksz_quarter * blksz_quarter;
539 /* Triple indirect. */
540 if (fileblock < blksz_quarter * blksz_quarter * (blksz_quarter + 1))
541 {
542 indir = inode->blocks.triple_indir_block;
543 shift = 2;
544 goto indirect;
545 }
546 grub_error (GRUB_ERR_BAD_FS,
547 "ext2fs doesn't support quadruple indirect blocks");
548 return -1;
549
550 indirect:
551 do {
552 /* If the indirect block is zero, all child blocks are absent
553 (i.e. filled with zeros.) */
554 if (indir == 0)
555 return 0;
556 if (grub_disk_read (data->disk,
557 ((grub_disk_addr_t) grub_le_to_cpu32 (indir))
558 << log2_blksz,
559 ((fileblock >> (log_perblock * shift))
560 & ((1 << log_perblock) - 1))
561 * sizeof (indir),
562 sizeof (indir), &indir))
563 return -1;
564 } while (shift--);
565
566 return grub_le_to_cpu32 (indir);
567 }
568
569 /* Read LEN bytes from the file described by DATA starting with byte
570 POS. Return the amount of read bytes in READ. */
571 static grub_ssize_t
572 grub_ext2_read_file (grub_fshelp_node_t node,
573 grub_disk_read_hook_t read_hook, void *read_hook_data,
574 grub_off_t pos, grub_size_t len, char *buf)
575 {
576 return grub_fshelp_read_file (node->data->disk, node,
577 read_hook, read_hook_data,
578 pos, len, buf, grub_ext2_read_block,
579 grub_cpu_to_le32 (node->inode.size)
580 | (((grub_off_t) grub_cpu_to_le32 (node->inode.size_high)) << 32),
581 LOG2_EXT2_BLOCK_SIZE (node->data), 0);
582
583 }
584
585
586 /* Read the inode INO for the file described by DATA into INODE. */
587 static grub_err_t
588 grub_ext2_read_inode (struct grub_ext2_data *data,
589 int ino, struct grub_ext2_inode *inode)
590 {
591 struct grub_ext2_block_group blkgrp;
592 struct grub_ext2_sblock *sblock = &data->sblock;
593 int inodes_per_block;
594 unsigned int blkno;
595 unsigned int blkoff;
596 grub_disk_addr_t base;
597
598 /* It is easier to calculate if the first inode is 0. */
599 ino--;
600
601 grub_ext2_blockgroup (data,
602 ino / grub_le_to_cpu32 (sblock->inodes_per_group),
603 &blkgrp);
604 if (grub_errno)
605 return grub_errno;
606
607 inodes_per_block = EXT2_BLOCK_SIZE (data) / EXT2_INODE_SIZE (data);
608 blkno = (ino % grub_le_to_cpu32 (sblock->inodes_per_group))
609 / inodes_per_block;
610 blkoff = (ino % grub_le_to_cpu32 (sblock->inodes_per_group))
611 % inodes_per_block;
612
613 base = grub_le_to_cpu32 (blkgrp.inode_table_id);
614 if (data->log_group_desc_size >= 6)
615 base |= (((grub_disk_addr_t) grub_le_to_cpu32 (blkgrp.inode_table_id_hi))
616 << 32);
617
618 /* Read the inode. */
619 if (grub_disk_read (data->disk,
620 ((base + blkno) << LOG2_EXT2_BLOCK_SIZE (data)),
621 EXT2_INODE_SIZE (data) * blkoff,
622 sizeof (struct grub_ext2_inode), inode))
623 return grub_errno;
624
625 return 0;
626 }
627
628 static struct grub_ext2_data *
629 grub_ext2_mount (grub_disk_t disk)
630 {
631 struct grub_ext2_data *data;
632
633 data = grub_malloc (sizeof (struct grub_ext2_data));
634 if (!data)
635 return 0;
636
637 /* Read the superblock. */
638 grub_disk_read (disk, 1 * 2, 0, sizeof (struct grub_ext2_sblock),
639 &data->sblock);
640 if (grub_errno)
641 goto fail;
642
643 /* Make sure this is an ext2 filesystem. */
644 if (data->sblock.magic != grub_cpu_to_le16_compile_time (EXT2_MAGIC)
645 || grub_le_to_cpu32 (data->sblock.log2_block_size) >= 16
646 || data->sblock.inodes_per_group == 0
647 /* 20 already means 1GiB blocks. We don't want to deal with blocks overflowing int32. */
648 || grub_le_to_cpu32 (data->sblock.log2_block_size) > 20
649 || EXT2_INODE_SIZE (data) == 0
650 || EXT2_BLOCK_SIZE (data) / EXT2_INODE_SIZE (data) == 0)
651 {
652 grub_error (GRUB_ERR_BAD_FS, "not an ext2 filesystem");
653 goto fail;
654 }
655
656 /* Check the FS doesn't have feature bits enabled that we don't support. */
657 if (data->sblock.revision_level != grub_cpu_to_le32_compile_time (EXT2_GOOD_OLD_REVISION)
658 && (data->sblock.feature_incompat
659 & grub_cpu_to_le32_compile_time (~(EXT2_DRIVER_SUPPORTED_INCOMPAT
660 | EXT2_DRIVER_IGNORED_INCOMPAT))))
661 {
662 grub_error (GRUB_ERR_BAD_FS, "filesystem has unsupported incompatible features");
663 goto fail;
664 }
665
666 if (data->sblock.revision_level != grub_cpu_to_le32_compile_time (EXT2_GOOD_OLD_REVISION)
667 && (data->sblock.feature_incompat
668 & grub_cpu_to_le32_compile_time (EXT4_FEATURE_INCOMPAT_64BIT))
669 && data->sblock.group_desc_size != 0
670 && ((data->sblock.group_desc_size & (data->sblock.group_desc_size - 1))
671 == 0)
672 && (data->sblock.group_desc_size & grub_cpu_to_le16_compile_time (0x1fe0)))
673 {
674 grub_uint16_t b = grub_le_to_cpu16 (data->sblock.group_desc_size);
675 for (data->log_group_desc_size = 0; b != (1 << data->log_group_desc_size);
676 data->log_group_desc_size++);
677 }
678 else
679 data->log_group_desc_size = 5;
680
681 data->disk = disk;
682
683 data->diropen.data = data;
684 data->diropen.ino = 2;
685 data->diropen.inode_read = 1;
686
687 data->inode = &data->diropen.inode;
688
689 grub_ext2_read_inode (data, 2, data->inode);
690 if (grub_errno)
691 goto fail;
692
693 return data;
694
695 fail:
696 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
697 grub_error (GRUB_ERR_BAD_FS, "not an ext2 filesystem");
698
699 grub_free (data);
700 return 0;
701 }
702
703 static char *
704 grub_ext2_read_symlink (grub_fshelp_node_t node)
705 {
706 char *symlink;
707 struct grub_fshelp_node *diro = node;
708
709 if (! diro->inode_read)
710 {
711 grub_ext2_read_inode (diro->data, diro->ino, &diro->inode);
712 if (grub_errno)
713 return 0;
714
715 if (diro->inode.flags & grub_cpu_to_le32_compile_time (EXT4_ENCRYPT_FLAG))
716 {
717 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "symlink is encrypted");
718 return 0;
719 }
720 }
721
722 symlink = grub_malloc (grub_le_to_cpu32 (diro->inode.size) + 1);
723 if (! symlink)
724 return 0;
725
726 /* If the filesize of the symlink is bigger than
727 60 the symlink is stored in a separate block,
728 otherwise it is stored in the inode. */
729 if (grub_le_to_cpu32 (diro->inode.size) <= sizeof (diro->inode.symlink))
730 grub_memcpy (symlink,
731 diro->inode.symlink,
732 grub_le_to_cpu32 (diro->inode.size));
733 else
734 {
735 grub_ext2_read_file (diro, 0, 0, 0,
736 grub_le_to_cpu32 (diro->inode.size),
737 symlink);
738 if (grub_errno)
739 {
740 grub_free (symlink);
741 return 0;
742 }
743 }
744
745 symlink[grub_le_to_cpu32 (diro->inode.size)] = '\0';
746 return symlink;
747 }
748
749 static int
750 grub_ext2_iterate_dir (grub_fshelp_node_t dir,
751 grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
752 {
753 unsigned int fpos = 0;
754 struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
755
756 if (! diro->inode_read)
757 {
758 grub_ext2_read_inode (diro->data, diro->ino, &diro->inode);
759 if (grub_errno)
760 return 0;
761 }
762
763 if (diro->inode.flags & grub_cpu_to_le32_compile_time (EXT4_ENCRYPT_FLAG))
764 {
765 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "directory is encrypted");
766 return 0;
767 }
768
769 /* Search the file. */
770 while (fpos < grub_le_to_cpu32 (diro->inode.size))
771 {
772 struct ext2_dirent dirent;
773
774 grub_ext2_read_file (diro, 0, 0, fpos, sizeof (struct ext2_dirent),
775 (char *) &dirent);
776 if (grub_errno)
777 return 0;
778
779 if (dirent.direntlen == 0)
780 return 0;
781
782 if (dirent.inode != 0 && dirent.namelen != 0)
783 {
784 char filename[MAX_NAMELEN + 1];
785 struct grub_fshelp_node *fdiro;
786 enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
787
788 grub_ext2_read_file (diro, 0, 0, fpos + sizeof (struct ext2_dirent),
789 dirent.namelen, filename);
790 if (grub_errno)
791 return 0;
792
793 fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
794 if (! fdiro)
795 return 0;
796
797 fdiro->data = diro->data;
798 fdiro->ino = grub_le_to_cpu32 (dirent.inode);
799
800 filename[dirent.namelen] = '\0';
801
802 if (dirent.filetype != FILETYPE_UNKNOWN)
803 {
804 fdiro->inode_read = 0;
805
806 if (dirent.filetype == FILETYPE_DIRECTORY)
807 type = GRUB_FSHELP_DIR;
808 else if (dirent.filetype == FILETYPE_SYMLINK)
809 type = GRUB_FSHELP_SYMLINK;
810 else if (dirent.filetype == FILETYPE_REG)
811 type = GRUB_FSHELP_REG;
812 }
813 else
814 {
815 /* The filetype can not be read from the dirent, read
816 the inode to get more information. */
817 grub_ext2_read_inode (diro->data,
818 grub_le_to_cpu32 (dirent.inode),
819 &fdiro->inode);
820 if (grub_errno)
821 {
822 grub_free (fdiro);
823 return 0;
824 }
825
826 fdiro->inode_read = 1;
827
828 if ((grub_le_to_cpu16 (fdiro->inode.mode)
829 & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
830 type = GRUB_FSHELP_DIR;
831 else if ((grub_le_to_cpu16 (fdiro->inode.mode)
832 & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
833 type = GRUB_FSHELP_SYMLINK;
834 else if ((grub_le_to_cpu16 (fdiro->inode.mode)
835 & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
836 type = GRUB_FSHELP_REG;
837 }
838
839 if (hook (filename, type, fdiro, hook_data))
840 return 1;
841 }
842
843 fpos += grub_le_to_cpu16 (dirent.direntlen);
844 }
845
846 return 0;
847 }
848
849 /* Open a file named NAME and initialize FILE. */
850 static grub_err_t
851 grub_ext2_open (struct grub_file *file, const char *name)
852 {
853 struct grub_ext2_data *data;
854 struct grub_fshelp_node *fdiro = 0;
855 grub_err_t err;
856
857 grub_dl_ref (my_mod);
858
859 data = grub_ext2_mount (file->device->disk);
860 if (! data)
861 {
862 err = grub_errno;
863 goto fail;
864 }
865
866 err = grub_fshelp_find_file (name, &data->diropen, &fdiro,
867 grub_ext2_iterate_dir,
868 grub_ext2_read_symlink, GRUB_FSHELP_REG);
869 if (err)
870 goto fail;
871
872 if (! fdiro->inode_read)
873 {
874 err = grub_ext2_read_inode (data, fdiro->ino, &fdiro->inode);
875 if (err)
876 goto fail;
877 }
878
879 if (fdiro->inode.flags & grub_cpu_to_le32_compile_time (EXT4_ENCRYPT_FLAG))
880 {
881 err = grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "file is encrypted");
882 goto fail;
883 }
884
885 grub_memcpy (data->inode, &fdiro->inode, sizeof (struct grub_ext2_inode));
886 grub_free (fdiro);
887
888 file->size = grub_le_to_cpu32 (data->inode->size);
889 file->size |= ((grub_off_t) grub_le_to_cpu32 (data->inode->size_high)) << 32;
890 file->data = data;
891 file->offset = 0;
892
893 return 0;
894
895 fail:
896 if (fdiro != &data->diropen)
897 grub_free (fdiro);
898 grub_free (data);
899
900 grub_dl_unref (my_mod);
901
902 return err;
903 }
904
905 static grub_err_t
906 grub_ext2_close (grub_file_t file)
907 {
908 grub_free (file->data);
909
910 grub_dl_unref (my_mod);
911
912 return GRUB_ERR_NONE;
913 }
914
915 /* Read LEN bytes data from FILE into BUF. */
916 static grub_ssize_t
917 grub_ext2_read (grub_file_t file, char *buf, grub_size_t len)
918 {
919 struct grub_ext2_data *data = (struct grub_ext2_data *) file->data;
920
921 return grub_ext2_read_file (&data->diropen,
922 file->read_hook, file->read_hook_data,
923 file->offset, len, buf);
924 }
925
926
927 /* Context for grub_ext2_dir. */
928 struct grub_ext2_dir_ctx
929 {
930 grub_fs_dir_hook_t hook;
931 void *hook_data;
932 struct grub_ext2_data *data;
933 };
934
935 /* Helper for grub_ext2_dir. */
936 static int
937 grub_ext2_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
938 grub_fshelp_node_t node, void *data)
939 {
940 struct grub_ext2_dir_ctx *ctx = data;
941 struct grub_dirhook_info info;
942
943 grub_memset (&info, 0, sizeof (info));
944 if (! node->inode_read)
945 {
946 grub_ext2_read_inode (ctx->data, node->ino, &node->inode);
947 if (!grub_errno)
948 node->inode_read = 1;
949 grub_errno = GRUB_ERR_NONE;
950 }
951 if (node->inode_read)
952 {
953 info.mtimeset = 1;
954 info.mtime = grub_le_to_cpu32 (node->inode.mtime);
955 }
956
957 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
958 if (!info.dir)
959 info.size = (((grub_off_t) grub_le_to_cpu32 (node->inode.size_high)) << 32) | grub_le_to_cpu32 (node->inode.size);
960 grub_free (node);
961 return ctx->hook (filename, &info, ctx->hook_data);
962 }
963
964 static grub_err_t
965 grub_ext2_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook,
966 void *hook_data)
967 {
968 struct grub_ext2_dir_ctx ctx = {
969 .hook = hook,
970 .hook_data = hook_data
971 };
972 struct grub_fshelp_node *fdiro = 0;
973
974 grub_dl_ref (my_mod);
975
976 ctx.data = grub_ext2_mount (device->disk);
977 if (! ctx.data)
978 goto fail;
979
980 grub_fshelp_find_file (path, &ctx.data->diropen, &fdiro,
981 grub_ext2_iterate_dir, grub_ext2_read_symlink,
982 GRUB_FSHELP_DIR);
983 if (grub_errno)
984 goto fail;
985
986 grub_ext2_iterate_dir (fdiro, grub_ext2_dir_iter, &ctx);
987
988 fail:
989 if (fdiro != &ctx.data->diropen)
990 grub_free (fdiro);
991 grub_free (ctx.data);
992
993 grub_dl_unref (my_mod);
994
995 return grub_errno;
996 }
997
998 static grub_err_t
999 grub_ext2_label (grub_device_t device, char **label)
1000 {
1001 struct grub_ext2_data *data;
1002 grub_disk_t disk = device->disk;
1003
1004 grub_dl_ref (my_mod);
1005
1006 data = grub_ext2_mount (disk);
1007 if (data)
1008 *label = grub_strndup (data->sblock.volume_name,
1009 sizeof (data->sblock.volume_name));
1010 else
1011 *label = NULL;
1012
1013 grub_dl_unref (my_mod);
1014
1015 grub_free (data);
1016
1017 return grub_errno;
1018 }
1019
1020 static grub_err_t
1021 grub_ext2_uuid (grub_device_t device, char **uuid)
1022 {
1023 struct grub_ext2_data *data;
1024 grub_disk_t disk = device->disk;
1025
1026 grub_dl_ref (my_mod);
1027
1028 data = grub_ext2_mount (disk);
1029 if (data)
1030 {
1031 *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
1032 grub_be_to_cpu16 (data->sblock.uuid[0]),
1033 grub_be_to_cpu16 (data->sblock.uuid[1]),
1034 grub_be_to_cpu16 (data->sblock.uuid[2]),
1035 grub_be_to_cpu16 (data->sblock.uuid[3]),
1036 grub_be_to_cpu16 (data->sblock.uuid[4]),
1037 grub_be_to_cpu16 (data->sblock.uuid[5]),
1038 grub_be_to_cpu16 (data->sblock.uuid[6]),
1039 grub_be_to_cpu16 (data->sblock.uuid[7]));
1040 }
1041 else
1042 *uuid = NULL;
1043
1044 grub_dl_unref (my_mod);
1045
1046 grub_free (data);
1047
1048 return grub_errno;
1049 }
1050
1051 /* Get mtime. */
1052 static grub_err_t
1053 grub_ext2_mtime (grub_device_t device, grub_int32_t *tm)
1054 {
1055 struct grub_ext2_data *data;
1056 grub_disk_t disk = device->disk;
1057
1058 grub_dl_ref (my_mod);
1059
1060 data = grub_ext2_mount (disk);
1061 if (!data)
1062 *tm = 0;
1063 else
1064 *tm = grub_le_to_cpu32 (data->sblock.utime);
1065
1066 grub_dl_unref (my_mod);
1067
1068 grub_free (data);
1069
1070 return grub_errno;
1071
1072 }
1073
1074 int grub_ext_get_file_chunk(grub_uint64_t part_start, grub_file_t file, ventoy_img_chunk_list *chunk_list)
1075 {
1076 int blocksize;
1077 int log2blocksize;
1078 grub_disk_t disk;
1079 grub_disk_addr_t i = 0;
1080 grub_disk_addr_t blockcnt;
1081 grub_disk_addr_t blknr;
1082 grub_fshelp_node_t node = NULL;
1083
1084 disk = file->device->disk;
1085 node = &(((struct grub_ext2_data *)file->data)->diropen);
1086
1087 log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
1088 blocksize = 1 << (log2blocksize + GRUB_DISK_SECTOR_BITS);
1089 blockcnt = (file->size + blocksize - 1) >> (log2blocksize + GRUB_DISK_SECTOR_BITS);
1090
1091 while (i < blockcnt)
1092 {
1093 g_ventoy_block_count = 1;
1094 blknr = grub_ext2_read_block(node, i);
1095 if (blknr == 0)
1096 {
1097 return 0;
1098 }
1099
1100 i += g_ventoy_block_count;
1101 blknr = blknr << log2blocksize;
1102 grub_disk_blocklist_read(chunk_list, blknr, g_ventoy_block_count * blocksize, disk->log_sector_size);
1103 }
1104
1105 for (i = 0; i < chunk_list->cur_chunk; i++)
1106 {
1107 chunk_list->chunk[i].disk_start_sector += part_start;
1108 chunk_list->chunk[i].disk_end_sector += part_start;
1109 }
1110
1111 return 0;
1112 }
1113
1114 static struct grub_fs grub_ext2_fs =
1115 {
1116 .name = "ext2",
1117 .fs_dir = grub_ext2_dir,
1118 .fs_open = grub_ext2_open,
1119 .fs_read = grub_ext2_read,
1120 .fs_close = grub_ext2_close,
1121 .fs_label = grub_ext2_label,
1122 .fs_uuid = grub_ext2_uuid,
1123 .fs_mtime = grub_ext2_mtime,
1124 #ifdef GRUB_UTIL
1125 .reserved_first_sector = 1,
1126 .blocklist_install = 1,
1127 #endif
1128 .next = 0
1129 };
1130
1131 GRUB_MOD_INIT(ext2)
1132 {
1133 grub_fs_register (&grub_ext2_fs);
1134 my_mod = mod;
1135 }
1136
1137 GRUB_MOD_FINI(ext2)
1138 {
1139 grub_fs_unregister (&grub_ext2_fs);
1140 }