xref: /openbmc/u-boot/include/ext_common.h (revision fd697ecf)
1 /*
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>
6  *
7  * Data structures and headers for ext4 support have been taken from
8  * ext2 ls load support in Uboot
9  *
10  * (C) Copyright 2004
11  * esd gmbh <www.esd-electronics.com>
12  * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
13  *
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.
17  *
18  * SPDX-License-Identifier:	GPL-2.0+
19  */
20 
21 #ifndef __EXT_COMMON__
22 #define __EXT_COMMON__
23 #include <command.h>
24 #define SECTOR_SIZE		0x200
25 
26 /* Magic value used to identify an ext2 filesystem.  */
27 #define	EXT2_MAGIC			0xEF53
28 /* Amount of indirect blocks in an inode.  */
29 #define INDIRECT_BLOCKS			12
30 /* Maximum lenght of a pathname.  */
31 #define EXT2_PATH_MAX				4096
32 /* Maximum nesting of symlinks, used to prevent a loop.  */
33 #define	EXT2_MAX_SYMLINKCNT		8
34 
35 /* Filetype used in directory entry.  */
36 #define	FILETYPE_UNKNOWN		0
37 #define	FILETYPE_REG			1
38 #define	FILETYPE_DIRECTORY		2
39 #define	FILETYPE_SYMLINK		7
40 
41 /* Filetype information as used in inodes.  */
42 #define FILETYPE_INO_MASK		0170000
43 #define FILETYPE_INO_REG		0100000
44 #define FILETYPE_INO_DIRECTORY		0040000
45 #define FILETYPE_INO_SYMLINK		0120000
46 #define EXT2_ROOT_INO			2 /* Root inode */
47 
48 /* The size of an ext2 block in bytes.  */
49 #define EXT2_BLOCK_SIZE(data)	   (1 << LOG2_BLOCK_SIZE(data))
50 
51 /* Log2 size of ext2 block in bytes.  */
52 #define LOG2_BLOCK_SIZE(data)	   (__le32_to_cpu		   \
53 				    (data->sblock.log2_block_size) \
54 				    + EXT2_MIN_BLOCK_LOG_SIZE)
55 #define INODE_SIZE_FILESYSTEM(data)	(__le32_to_cpu \
56 			(data->sblock.inode_size))
57 
58 #define EXT2_FT_DIR	2
59 #define SUCCESS	1
60 
61 /* Macro-instructions used to manage several block sizes  */
62 #define EXT2_MIN_BLOCK_LOG_SIZE	10 /* 1024 */
63 #define EXT2_MAX_BLOCK_LOG_SIZE	16 /* 65536 */
64 #define EXT2_MIN_BLOCK_SIZE		(1 << EXT2_MIN_BLOCK_LOG_SIZE)
65 #define EXT2_MAX_BLOCK_SIZE		(1 << EXT2_MAX_BLOCK_LOG_SIZE)
66 
67 /* The ext2 superblock.  */
68 struct ext2_sblock {
69 	uint32_t total_inodes;
70 	uint32_t total_blocks;
71 	uint32_t reserved_blocks;
72 	uint32_t free_blocks;
73 	uint32_t free_inodes;
74 	uint32_t first_data_block;
75 	uint32_t log2_block_size;
76 	uint32_t log2_fragment_size;
77 	uint32_t blocks_per_group;
78 	uint32_t fragments_per_group;
79 	uint32_t inodes_per_group;
80 	uint32_t mtime;
81 	uint32_t utime;
82 	uint16_t mnt_count;
83 	uint16_t max_mnt_count;
84 	uint16_t magic;
85 	uint16_t fs_state;
86 	uint16_t error_handling;
87 	uint16_t minor_revision_level;
88 	uint32_t lastcheck;
89 	uint32_t checkinterval;
90 	uint32_t creator_os;
91 	uint32_t revision_level;
92 	uint16_t uid_reserved;
93 	uint16_t gid_reserved;
94 	uint32_t first_inode;
95 	uint16_t inode_size;
96 	uint16_t block_group_number;
97 	uint32_t feature_compatibility;
98 	uint32_t feature_incompat;
99 	uint32_t feature_ro_compat;
100 	uint32_t unique_id[4];
101 	char volume_name[16];
102 	char last_mounted_on[64];
103 	uint32_t compression_info;
104 };
105 
106 struct ext2_block_group {
107 	__u32 block_id;	/* Blocks bitmap block */
108 	__u32 inode_id;	/* Inodes bitmap block */
109 	__u32 inode_table_id;	/* Inodes table block */
110 	__u16 free_blocks;	/* Free blocks count */
111 	__u16 free_inodes;	/* Free inodes count */
112 	__u16 used_dir_cnt;	/* Directories count */
113 	__u16 bg_flags;
114 	__u32 bg_reserved[2];
115 	__u16 bg_itable_unused; /* Unused inodes count */
116 	__u16 bg_checksum;	/* crc16(s_uuid+grouo_num+group_desc)*/
117 };
118 
119 /* The ext2 inode. */
120 struct ext2_inode {
121 	uint16_t mode;
122 	uint16_t uid;
123 	uint32_t size;
124 	uint32_t atime;
125 	uint32_t ctime;
126 	uint32_t mtime;
127 	uint32_t dtime;
128 	uint16_t gid;
129 	uint16_t nlinks;
130 	uint32_t blockcnt;	/* Blocks of 512 bytes!! */
131 	uint32_t flags;
132 	uint32_t osd1;
133 	union {
134 		struct datablocks {
135 			uint32_t dir_blocks[INDIRECT_BLOCKS];
136 			uint32_t indir_block;
137 			uint32_t double_indir_block;
138 			uint32_t triple_indir_block;
139 		} blocks;
140 		char symlink[60];
141 	} b;
142 	uint32_t version;
143 	uint32_t acl;
144 	uint32_t dir_acl;
145 	uint32_t fragment_addr;
146 	uint32_t osd2[3];
147 };
148 
149 /* The header of an ext2 directory entry. */
150 struct ext2_dirent {
151 	uint32_t inode;
152 	uint16_t direntlen;
153 	uint8_t namelen;
154 	uint8_t filetype;
155 };
156 
157 struct ext2fs_node {
158 	struct ext2_data *data;
159 	struct ext2_inode inode;
160 	int ino;
161 	int inode_read;
162 };
163 
164 /* Information about a "mounted" ext2 filesystem. */
165 struct ext2_data {
166 	struct ext2_sblock sblock;
167 	struct ext2_inode *inode;
168 	struct ext2fs_node diropen;
169 };
170 
171 extern lbaint_t part_offset;
172 
173 int do_ext2ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
174 int do_ext2load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
175 int do_ext4_load(cmd_tbl_t *cmdtp, int flag, int argc,
176 					char *const argv[]);
177 int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]);
178 int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc,
179 				char *const argv[]);
180 #endif
181