blob: bc3324172a082abb34faff16f025cb0fd335df8a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Uma Shankar71014b62012-05-25 21:21:44 +05302/*
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.
Uma Shankar71014b62012-05-25 21:21:44 +053018 */
19
20#ifndef __EXT_COMMON__
21#define __EXT_COMMON__
Simon Glassed38aef2020-05-10 11:40:03 -060022
23struct cmd_tbl;
24
Uma Shankar71014b62012-05-25 21:21:44 +053025#define SECTOR_SIZE 0x200
Marek Szyprowski07a39ab2019-06-21 15:32:51 +020026#define LOG2_SECTOR_SIZE 9
Uma Shankar71014b62012-05-25 21:21:44 +053027
28/* Magic value used to identify an ext2 filesystem. */
29#define EXT2_MAGIC 0xEF53
30/* Amount of indirect blocks in an inode. */
31#define INDIRECT_BLOCKS 12
32/* Maximum lenght of a pathname. */
33#define EXT2_PATH_MAX 4096
34/* Maximum nesting of symlinks, used to prevent a loop. */
35#define EXT2_MAX_SYMLINKCNT 8
36
37/* Filetype used in directory entry. */
38#define FILETYPE_UNKNOWN 0
39#define FILETYPE_REG 1
40#define FILETYPE_DIRECTORY 2
41#define FILETYPE_SYMLINK 7
42
43/* Filetype information as used in inodes. */
44#define FILETYPE_INO_MASK 0170000
45#define FILETYPE_INO_REG 0100000
46#define FILETYPE_INO_DIRECTORY 0040000
47#define FILETYPE_INO_SYMLINK 0120000
48#define EXT2_ROOT_INO 2 /* Root inode */
49
Uma Shankar71014b62012-05-25 21:21:44 +053050/* The size of an ext2 block in bytes. */
51#define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data))
52
Uma Shankar71014b62012-05-25 21:21:44 +053053/* Log2 size of ext2 block in bytes. */
Michael Wallec07cdcb2016-08-29 10:46:44 +020054#define LOG2_BLOCK_SIZE(data) (le32_to_cpu \
Egbert Eich7b1b2552013-05-01 01:13:19 +000055 (data->sblock.log2_block_size) \
56 + EXT2_MIN_BLOCK_LOG_SIZE)
Uma Shankar71014b62012-05-25 21:21:44 +053057
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. */
68struct ext2_sblock {
Michael Walle2a819612016-08-29 10:46:43 +020069 __le32 total_inodes;
70 __le32 total_blocks;
71 __le32 reserved_blocks;
72 __le32 free_blocks;
73 __le32 free_inodes;
74 __le32 first_data_block;
75 __le32 log2_block_size;
76 __le32 log2_fragment_size;
77 __le32 blocks_per_group;
78 __le32 fragments_per_group;
79 __le32 inodes_per_group;
80 __le32 mtime;
81 __le32 utime;
82 __le16 mnt_count;
83 __le16 max_mnt_count;
84 __le16 magic;
85 __le16 fs_state;
86 __le16 error_handling;
87 __le16 minor_revision_level;
88 __le32 lastcheck;
89 __le32 checkinterval;
90 __le32 creator_os;
91 __le32 revision_level;
92 __le16 uid_reserved;
93 __le16 gid_reserved;
94 __le32 first_inode;
95 __le16 inode_size;
96 __le16 block_group_number;
97 __le32 feature_compatibility;
98 __le32 feature_incompat;
99 __le32 feature_ro_compat;
100 __le32 unique_id[4];
Uma Shankar71014b62012-05-25 21:21:44 +0530101 char volume_name[16];
102 char last_mounted_on[64];
Michael Walle2a819612016-08-29 10:46:43 +0200103 __le32 compression_info;
Stefan Brünsf3c6e442016-09-17 02:10:06 +0200104 uint8_t prealloc_blocks;
105 uint8_t prealloc_dir_blocks;
106 __le16 reserved_gdt_blocks;
107 uint8_t journal_uuid[16];
108 __le32 journal_inode;
109 __le32 journal_dev;
110 __le32 last_orphan;
111 __le32 hash_seed[4];
112 uint8_t default_hash_version;
113 uint8_t journal_backup_type;
114 __le16 descriptor_size;
115 __le32 default_mount_options;
116 __le32 first_meta_block_group;
117 __le32 mkfs_time;
118 __le32 journal_blocks[17];
119 __le32 total_blocks_high;
120 __le32 reserved_blocks_high;
121 __le32 free_blocks_high;
122 __le16 min_extra_inode_size;
123 __le16 want_extra_inode_size;
124 __le32 flags;
125 __le16 raid_stride;
126 __le16 mmp_interval;
127 __le64 mmp_block;
128 __le32 raid_stripe_width;
129 uint8_t log2_groups_per_flex;
130 uint8_t checksum_type;
Uma Shankar71014b62012-05-25 21:21:44 +0530131};
132
133struct ext2_block_group {
Michael Walle2a819612016-08-29 10:46:43 +0200134 __le32 block_id; /* Blocks bitmap block */
135 __le32 inode_id; /* Inodes bitmap block */
136 __le32 inode_table_id; /* Inodes table block */
137 __le16 free_blocks; /* Free blocks count */
138 __le16 free_inodes; /* Free inodes count */
139 __le16 used_dir_cnt; /* Directories count */
140 __le16 bg_flags;
Stefan Brünsf3c6e442016-09-17 02:10:06 +0200141 __le32 bg_exclude_bitmap;
142 __le16 bg_block_id_csum;
143 __le16 bg_inode_id_csum;
Michael Walle2a819612016-08-29 10:46:43 +0200144 __le16 bg_itable_unused; /* Unused inodes count */
Stefan Brünsf3c6e442016-09-17 02:10:06 +0200145 __le16 bg_checksum; /* crc16(s_uuid+group_num+group_desc)*/
146 /* following fields only exist if descriptor size is 64 */
147 __le32 block_id_high;
148 __le32 inode_id_high;
149 __le32 inode_table_id_high;
150 __le16 free_blocks_high;
151 __le16 free_inodes_high;
152 __le16 used_dir_cnt_high;
153 __le16 bg_itable_unused_high;
154 __le32 bg_exclude_bitmap_high;
155 __le16 bg_block_id_csum_high;
156 __le16 bg_inode_id_csum_high;
157 __le32 bg_reserved;
Uma Shankar71014b62012-05-25 21:21:44 +0530158};
159
160/* The ext2 inode. */
161struct ext2_inode {
Michael Walle2a819612016-08-29 10:46:43 +0200162 __le16 mode;
163 __le16 uid;
164 __le32 size;
165 __le32 atime;
166 __le32 ctime;
167 __le32 mtime;
168 __le32 dtime;
169 __le16 gid;
170 __le16 nlinks;
Stefan Brünsf3c6e442016-09-17 02:10:06 +0200171 __le32 blockcnt; /* Blocks of either 512 or block_size bytes */
Michael Walle2a819612016-08-29 10:46:43 +0200172 __le32 flags;
173 __le32 osd1;
Uma Shankar71014b62012-05-25 21:21:44 +0530174 union {
175 struct datablocks {
Michael Walle2a819612016-08-29 10:46:43 +0200176 __le32 dir_blocks[INDIRECT_BLOCKS];
177 __le32 indir_block;
178 __le32 double_indir_block;
179 __le32 triple_indir_block;
Uma Shankar71014b62012-05-25 21:21:44 +0530180 } blocks;
181 char symlink[60];
Stefan Brünsf3c6e442016-09-17 02:10:06 +0200182 char inline_data[60];
Uma Shankar71014b62012-05-25 21:21:44 +0530183 } b;
Michael Walle2a819612016-08-29 10:46:43 +0200184 __le32 version;
185 __le32 acl;
Stefan Brünsf3c6e442016-09-17 02:10:06 +0200186 __le32 size_high; /* previously dir_acl, but never used */
Michael Walle2a819612016-08-29 10:46:43 +0200187 __le32 fragment_addr;
188 __le32 osd2[3];
Uma Shankar71014b62012-05-25 21:21:44 +0530189};
190
191/* The header of an ext2 directory entry. */
192struct ext2_dirent {
Michael Walle2a819612016-08-29 10:46:43 +0200193 __le32 inode;
194 __le16 direntlen;
195 __u8 namelen;
196 __u8 filetype;
Uma Shankar71014b62012-05-25 21:21:44 +0530197};
198
199struct ext2fs_node {
200 struct ext2_data *data;
201 struct ext2_inode inode;
202 int ino;
203 int inode_read;
204};
205
206/* Information about a "mounted" ext2 filesystem. */
207struct ext2_data {
208 struct ext2_sblock sblock;
209 struct ext2_inode *inode;
210 struct ext2fs_node diropen;
211};
212
Frederic Leroye7ee0282013-06-26 18:11:25 +0200213extern lbaint_t part_offset;
Rob Herring752e1bd2012-08-23 11:31:46 +0000214
Simon Glassed38aef2020-05-10 11:40:03 -0600215int do_ext2ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
216int do_ext2load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
217int do_ext4_load(struct cmd_tbl *cmdtp, int flag, int argc,
218 char *const argv[]);
219int do_ext4_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
220int do_ext4_write(struct cmd_tbl *cmdtp, int flag, int argc,
221 char *const argv[]);
Uma Shankar71014b62012-05-25 21:21:44 +0530222#endif