1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2011 - 2012 Samsung Electronics
4  * EXT4 filesystem implementation in Uboot by
5  * Uma Shankar <uma.shankar@samsung.com>
6  * Manjunatha C Achar <a.manjunatha@samsung.com>
7  *
8  * Data structures and headers for ext4 support have been taken from
9  * ext2 ls load support in Uboot
10  *
11  * (C) Copyright 2004
12  * esd gmbh <www.esd-electronics.com>
13  * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
14  *
15  * based on code from grub2 fs/ext2.c and fs/fshelp.c by
16  * GRUB  --  GRand Unified Bootloader
17  * Copyright (C) 2003, 2004  Free Software Foundation, Inc.
18  */
19 
20 #ifndef __EXT_COMMON__
21 #define __EXT_COMMON__
22 
23 #include <compiler.h>
24 
25 struct cmd_tbl;
26 
27 #define SECTOR_SIZE		0x200
28 #define LOG2_SECTOR_SIZE	9
29 
30 /* Magic value used to identify an ext2 filesystem.  */
31 #define	EXT2_MAGIC			0xEF53
32 /* Amount of indirect blocks in an inode.  */
33 #define INDIRECT_BLOCKS			12
34 /* Maximum lenght of a pathname.  */
35 #define EXT2_PATH_MAX				4096
36 /* Maximum nesting of symlinks, used to prevent a loop.  */
37 #define	EXT2_MAX_SYMLINKCNT		8
38 
39 /* Filetype used in directory entry.  */
40 #define	FILETYPE_UNKNOWN		0
41 #define	FILETYPE_REG			1
42 #define	FILETYPE_DIRECTORY		2
43 #define	FILETYPE_SYMLINK		7
44 
45 /* Filetype information as used in inodes.  */
46 #define FILETYPE_INO_MASK		0170000
47 #define FILETYPE_INO_REG		0100000
48 #define FILETYPE_INO_DIRECTORY		0040000
49 #define FILETYPE_INO_SYMLINK		0120000
50 #define EXT2_ROOT_INO			2 /* Root inode */
51 
52 /* The size of an ext2 block in bytes.  */
53 #define EXT2_BLOCK_SIZE(data)	   (1 << LOG2_BLOCK_SIZE(data))
54 
55 /* Log2 size of ext2 block in bytes.  */
56 #define LOG2_BLOCK_SIZE(data)	   (le32_to_cpu		   \
57 				    (data->sblock.log2_block_size) \
58 				    + EXT2_MIN_BLOCK_LOG_SIZE)
59 
60 #define EXT2_FT_DIR	2
61 #define SUCCESS	1
62 
63 /* Macro-instructions used to manage several block sizes  */
64 #define EXT2_MIN_BLOCK_LOG_SIZE	10 /* 1024 */
65 #define EXT2_MAX_BLOCK_LOG_SIZE	16 /* 65536 */
66 #define EXT2_MIN_BLOCK_SIZE		(1 << EXT2_MIN_BLOCK_LOG_SIZE)
67 #define EXT2_MAX_BLOCK_SIZE		(1 << EXT2_MAX_BLOCK_LOG_SIZE)
68 
69 /* The ext2 superblock.  */
70 struct ext2_sblock {
71 	__le32 total_inodes;
72 	__le32 total_blocks;
73 	__le32 reserved_blocks;
74 	__le32 free_blocks;
75 	__le32 free_inodes;
76 	__le32 first_data_block;
77 	__le32 log2_block_size;
78 	__le32 log2_fragment_size;
79 	__le32 blocks_per_group;
80 	__le32 fragments_per_group;
81 	__le32 inodes_per_group;
82 	__le32 mtime;
83 	__le32 utime;
84 	__le16 mnt_count;
85 	__le16 max_mnt_count;
86 	__le16 magic;
87 	__le16 fs_state;
88 	__le16 error_handling;
89 	__le16 minor_revision_level;
90 	__le32 lastcheck;
91 	__le32 checkinterval;
92 	__le32 creator_os;
93 	__le32 revision_level;
94 	__le16 uid_reserved;
95 	__le16 gid_reserved;
96 	__le32 first_inode;
97 	__le16 inode_size;
98 	__le16 block_group_number;
99 	__le32 feature_compatibility;
100 	__le32 feature_incompat;
101 	__le32 feature_ro_compat;
102 	__le32 unique_id[4];
103 	char volume_name[16];
104 	char last_mounted_on[64];
105 	__le32 compression_info;
106 	uint8_t prealloc_blocks;
107 	uint8_t prealloc_dir_blocks;
108 	__le16 reserved_gdt_blocks;
109 	uint8_t journal_uuid[16];
110 	__le32 journal_inode;
111 	__le32 journal_dev;
112 	__le32 last_orphan;
113 	__le32 hash_seed[4];
114 	uint8_t default_hash_version;
115 	uint8_t journal_backup_type;
116 	__le16 descriptor_size;
117 	__le32 default_mount_options;
118 	__le32 first_meta_block_group;
119 	__le32 mkfs_time;
120 	__le32 journal_blocks[17];
121 	__le32 total_blocks_high;
122 	__le32 reserved_blocks_high;
123 	__le32 free_blocks_high;
124 	__le16 min_extra_inode_size;
125 	__le16 want_extra_inode_size;
126 	__le32 flags;
127 	__le16 raid_stride;
128 	__le16 mmp_interval;
129 	__le64 mmp_block;
130 	__le32 raid_stripe_width;
131 	uint8_t log2_groups_per_flex;
132 	uint8_t checksum_type;
133 };
134 
135 struct ext2_block_group {
136 	__le32 block_id;	/* Blocks bitmap block */
137 	__le32 inode_id;	/* Inodes bitmap block */
138 	__le32 inode_table_id;	/* Inodes table block */
139 	__le16 free_blocks;	/* Free blocks count */
140 	__le16 free_inodes;	/* Free inodes count */
141 	__le16 used_dir_cnt;	/* Directories count */
142 	__le16 bg_flags;
143 	__le32 bg_exclude_bitmap;
144 	__le16 bg_block_id_csum;
145 	__le16 bg_inode_id_csum;
146 	__le16 bg_itable_unused; /* Unused inodes count */
147 	__le16 bg_checksum;	/* crc16(s_uuid+group_num+group_desc)*/
148 	/* following fields only exist if descriptor size is 64 */
149 	__le32 block_id_high;
150 	__le32 inode_id_high;
151 	__le32 inode_table_id_high;
152 	__le16 free_blocks_high;
153 	__le16 free_inodes_high;
154 	__le16 used_dir_cnt_high;
155 	__le16 bg_itable_unused_high;
156 	__le32 bg_exclude_bitmap_high;
157 	__le16 bg_block_id_csum_high;
158 	__le16 bg_inode_id_csum_high;
159 	__le32 bg_reserved;
160 };
161 
162 /* The ext2 inode. */
163 struct ext2_inode {
164 	__le16 mode;
165 	__le16 uid;
166 	__le32 size;
167 	__le32 atime;
168 	__le32 ctime;
169 	__le32 mtime;
170 	__le32 dtime;
171 	__le16 gid;
172 	__le16 nlinks;
173 	__le32 blockcnt;	/* Blocks of either 512 or block_size bytes */
174 	__le32 flags;
175 	__le32 osd1;
176 	union {
177 		struct datablocks {
178 			__le32 dir_blocks[INDIRECT_BLOCKS];
179 			__le32 indir_block;
180 			__le32 double_indir_block;
181 			__le32 triple_indir_block;
182 		} blocks;
183 		char symlink[60];
184 		char inline_data[60];
185 	} b;
186 	__le32 version;
187 	__le32 acl;
188 	__le32 size_high;	/* previously dir_acl, but never used */
189 	__le32 fragment_addr;
190 	__le32 osd2[3];
191 };
192 
193 /* The header of an ext2 directory entry. */
194 struct ext2_dirent {
195 	__le32 inode;
196 	__le16 direntlen;
197 	__u8 namelen;
198 	__u8 filetype;
199 };
200 
201 struct ext2fs_node {
202 	struct ext2_data *data;
203 	struct ext2_inode inode;
204 	int ino;
205 	int inode_read;
206 };
207 
208 /* Information about a "mounted" ext2 filesystem. */
209 struct ext2_data {
210 	struct ext2_sblock sblock;
211 	struct ext2_inode *inode;
212 	struct ext2fs_node diropen;
213 };
214 
215 extern lbaint_t part_offset;
216 
217 int do_ext2ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
218 int do_ext2load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
219 int do_ext4_load(struct cmd_tbl *cmdtp, int flag, int argc,
220 		 char *const argv[]);
221 int do_ext4_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
222 int do_ext4_write(struct cmd_tbl *cmdtp, int flag, int argc,
223 		  char *const argv[]);
224 #endif
225