2 * (C) Copyright 2011 - 2012 Samsung Electronics
3 * EXT4 filesystem implementation in Uboot by
4 * Uma Shankar <uma.shankar@samsung.com>
5 * Manjunatha C Achar <a.manjunatha@samsung.com>
7 * Data structures and headers for ext4 support have been taken from
8 * ext2 ls load support in Uboot
11 * esd gmbh <www.esd-electronics.com>
12 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
14 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
15 * GRUB -- GRand Unified Bootloader
16 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #ifndef __EXT_COMMON__
34 #define __EXT_COMMON__
36 #define SECTOR_SIZE 0x200
40 /* Magic value used to identify an ext2 filesystem. */
41 #define EXT2_MAGIC 0xEF53
45 /* Amount of indirect blocks in an inode. */
46 #define INDIRECT_BLOCKS 12
49 #define EXT4_NDIR_BLOCKS 12
50 #define EXT4_IND_BLOCK EXT4_NDIR_BLOCKS
51 #define EXT4_DIND_BLOCK (EXT4_IND_BLOCK + 1)
52 #define EXT4_TIND_BLOCK (EXT4_DIND_BLOCK + 1)
53 #define EXT4_N_BLOCKS (EXT4_TIND_BLOCK + 1)
55 #define EXT4_SECRM_FL 0x00000001
56 #define EXT4_UNRM_FL 0x00000002
57 #define EXT4_COMPR_FL 0x00000004
58 #define EXT4_SYNC_FL 0x00000008
59 #define EXT4_IMMUTABLE_FL 0x00000010
60 #define EXT4_APPEND_FL 0x00000020
61 #define EXT4_NODUMP_FL 0x00000040
62 #define EXT4_NOATIME_FL 0x00000080
64 #define EXT4_DIRTY_FL 0x00000100
65 #define EXT4_COMPRBLK_FL 0x00000200
66 #define EXT4_NOCOMPR_FL 0x00000400
67 #define EXT4_ECOMPR_FL 0x00000800
69 #define EXT4_INDEX_FL 0x00001000
70 #define EXT4_IMAGIC_FL 0x00002000
71 #define EXT4_JOURNAL_DATA_FL 0x00004000
72 #define EXT4_NOTAIL_FL 0x00008000
73 #define EXT4_DIRSYNC_FL 0x00010000
74 #define EXT4_TOPDIR_FL 0x00020000
75 #define EXT4_HUGE_FILE_FL 0x00040000
76 #define EXT4_EXTENTS_FL 0x00080000
77 #define EXT4_EA_INODE_FL 0x00200000
78 #define EXT4_EOFBLOCKS_FL 0x00400000
79 #define EXT4_RESERVED_FL 0x80000000
81 #define EXT4_FL_USER_VISIBLE 0x004BDFFF
82 #define EXT4_FL_USER_MODIFIABLE 0x004B80FF
84 #define EXT4_FL_INHERITED (EXT4_SECRM_FL | EXT4_UNRM_FL | EXT4_COMPR_FL | EXT4_SYNC_FL | EXT4_IMMUTABLE_FL | EXT4_APPEND_FL | EXT4_NODUMP_FL | EXT4_NOATIME_FL | EXT4_NOCOMPR_FL | EXT4_JOURNAL_DATA_FL | EXT4_NOTAIL_FL | EXT4_DIRSYNC_FL)
86 #define EXT4_REG_FLMASK (~(EXT4_DIRSYNC_FL | EXT4_TOPDIR_FL))
88 #define EXT4_OTHER_FLMASK (EXT4_NODUMP_FL | EXT4_NOATIME_FL)
90 /* Maximum lenght of a pathname. */
91 #define EXT2_PATH_MAX 4096
92 /* Maximum nesting of symlinks, used to prevent a loop. */
93 #define EXT2_MAX_SYMLINKCNT 8
95 /* Filetype used in directory entry. */
96 #define FILETYPE_UNKNOWN 0
97 #define FILETYPE_REG 1
98 #define FILETYPE_DIRECTORY 2
99 #define FILETYPE_SYMLINK 7
101 /* Filetype information as used in inodes. */
102 #define FILETYPE_INO_MASK 0170000
103 #define FILETYPE_INO_REG 0100000
104 #define FILETYPE_INO_DIRECTORY 0040000
105 #define FILETYPE_INO_SYMLINK 0120000
106 #define EXT2_ROOT_INO 2 /* Root inode */
108 /* Bits used as offset in sector */
109 #define DISK_SECTOR_BITS 9
110 /* The size of an ext2 block in bytes. */
111 #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data))
113 /* Log2 size of ext2 block in 512 blocks. */
114 #define LOG2_EXT2_BLOCK_SIZE(data) (__le32_to_cpu \
115 (data->sblock.log2_block_size) + 1)
117 /* Log2 size of ext2 block in bytes. */
118 #define LOG2_BLOCK_SIZE(data) (__le32_to_cpu \
119 (data->sblock.log2_block_size) + 10)
120 #define INODE_SIZE_FILESYSTEM(data) (__le32_to_cpu \
121 (data->sblock.inode_size))
123 #define EXT2_FT_DIR 2
126 /* Macro-instructions used to manage several block sizes */
127 #define EXT2_MIN_BLOCK_LOG_SIZE 10 /* 1024 */
128 #define EXT2_MAX_BLOCK_LOG_SIZE 16 /* 65536 */
129 #define EXT2_MIN_BLOCK_SIZE (1 << EXT2_MIN_BLOCK_LOG_SIZE)
130 #define EXT2_MAX_BLOCK_SIZE (1 << EXT2_MAX_BLOCK_LOG_SIZE)
132 /* The ext2 superblock. */
134 uint32_t total_inodes;
135 uint32_t total_blocks;
136 uint32_t reserved_blocks;
137 uint32_t free_blocks;
138 uint32_t free_inodes;
139 uint32_t first_data_block;
140 uint32_t log2_block_size;
141 uint32_t log2_fragment_size;
142 uint32_t blocks_per_group;
143 uint32_t fragments_per_group;
144 uint32_t inodes_per_group;
148 uint16_t max_mnt_count;
151 uint16_t error_handling;
152 uint16_t minor_revision_level;
154 uint32_t checkinterval;
156 uint32_t revision_level;
157 uint16_t uid_reserved;
158 uint16_t gid_reserved;
159 uint32_t first_inode;
161 uint16_t block_group_number;
162 uint32_t feature_compatibility;
163 uint32_t feature_incompat;
164 uint32_t feature_ro_compat;
165 uint32_t unique_id[4];
166 char volume_name[16];
167 char last_mounted_on[64];
168 uint32_t compression_info;
171 struct ext4_super_block {
172 __le32 s_inodes_count;
173 __le32 s_blocks_count_lo;
174 __le32 s_r_blocks_count_lo;
175 __le32 s_free_blocks_count_lo;
176 __le32 s_free_inodes_count;
177 __le32 s_first_data_block;
178 __le32 s_log_block_size;
179 __le32 s_obso_log_frag_size;
180 __le32 s_blocks_per_group;
181 __le32 s_obso_frags_per_group;
182 __le32 s_inodes_per_group;
186 __le16 s_max_mnt_count;
190 __le16 s_minor_rev_level;
192 __le32 s_checkinterval;
200 __le16 s_block_group_nr;
201 __le32 s_feature_compat;
202 __le32 s_feature_incompat;
203 __le32 s_feature_ro_compat;
205 char s_volume_name[16];
206 char s_last_mounted[64];
207 __le32 s_algorithm_usage_bitmap;
209 __u8 s_prealloc_blocks;
210 __u8 s_prealloc_dir_blocks;
211 __le16 s_reserved_gdt_blocks;
213 __u8 s_journal_uuid[16];
214 __le32 s_journal_inum;
215 __le32 s_journal_dev;
216 __le32 s_last_orphan;
217 __le32 s_hash_seed[4];
218 __u8 s_def_hash_version;
219 __u8 s_reserved_char_pad;
221 __le32 s_default_mount_opts;
222 __le32 s_first_meta_bg;
224 __le32 s_jnl_blocks[17];
226 __le32 s_blocks_count_hi;
227 __le32 s_r_blocks_count_hi;
228 __le32 s_free_blocks_count_hi;
229 __le16 s_min_extra_isize;
230 __le16 s_want_extra_isize;
232 __le16 s_raid_stride;
233 __le16 s_mmp_interval;
235 __le32 s_raid_stripe_width;
236 __u8 s_log_groups_per_flex;
237 __u8 s_reserved_char_pad2;
238 __le16 s_reserved_pad;
239 __le64 s_kbytes_written;
240 __u32 s_reserved[160];
243 struct ext2_block_group {
244 __u32 block_id; /* Blocks bitmap block */
245 __u32 inode_id; /* Inodes bitmap block */
246 __u32 inode_table_id; /* Inodes table block */
247 __u16 free_blocks; /* Free blocks count */
248 __u16 free_inodes; /* Free inodes count */
249 __u16 used_dir_cnt; /* Directories count */
251 __u32 bg_reserved[2];
252 __u16 bg_itable_unused; /* Unused inodes count */
253 __u16 bg_checksum; /* crc16(s_uuid+grouo_num+group_desc)*/
256 struct ext4_group_desc
258 __le32 bg_block_bitmap_lo;
259 __le32 bg_inode_bitmap_lo;
260 __le32 bg_inode_table_lo;
261 __le16 bg_free_blocks_count_lo;
262 __le16 bg_free_inodes_count_lo;
263 __le16 bg_used_dirs_count_lo;
265 __u32 bg_reserved[2];
266 __le16 bg_itable_unused_lo;
268 __le32 bg_block_bitmap_hi;
269 __le32 bg_inode_bitmap_hi;
270 __le32 bg_inode_table_hi;
271 __le16 bg_free_blocks_count_hi;
272 __le16 bg_free_inodes_count_hi;
273 __le16 bg_used_dirs_count_hi;
274 __le16 bg_itable_unused_hi;
275 __u32 bg_reserved2[3];
278 /* The ext2 inode. */
289 uint32_t blockcnt; /* Blocks of 512 bytes!! */
294 uint32_t dir_blocks[INDIRECT_BLOCKS];
295 uint32_t indir_block;
296 uint32_t double_indir_block;
297 uint32_t triple_indir_block;
304 uint32_t fragment_addr;
307 /* The ext4 inode. */
317 uint16_t i_links_count;
318 uint32_t i_blocks_lo;
322 uint32_t l_i_version;
325 __u32 h_i_translator;
331 uint32_t i_block[EXT4_N_BLOCKS];
332 uint32_t i_generation;
333 uint32_t i_file_acl_lo;
334 uint32_t i_size_high;
335 uint32_t i_obso_faddr;
338 uint16_t l_i_blocks_high;
339 uint16_t l_i_file_acl_high;
340 uint16_t l_i_uid_high;
341 uint16_t l_i_gid_high;
345 uint16_t h_i_reserved1;
352 uint16_t h_i_reserved1;
353 uint16_t m_i_file_acl_high;
354 __u32 m_i_reserved2[2];
357 uint16_t i_extra_isize;
359 uint32_t i_ctime_extra;
360 uint32_t i_mtime_extra;
361 uint32_t i_atime_extra;
363 uint32_t i_crtime_extra;
364 uint32_t i_version_hi;
367 /* The header of an ext2 directory entry. */
376 struct ext2_data *data;
377 struct ext2_inode inode;
382 /* Information about a "mounted" ext2 filesystem. */
384 struct ext2_sblock sblock;
385 struct ext2_inode *inode;
386 struct ext2fs_node diropen;