ext2.c 29 KB

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