Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 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. |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #ifndef __EXT_COMMON__ |
| 21 | #define __EXT_COMMON__ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 22 | |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 23 | #include <compiler.h> |
| 24 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 25 | struct cmd_tbl; |
| 26 | |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 27 | #define SECTOR_SIZE 0x200 |
Marek Szyprowski | 07a39ab | 2019-06-21 15:32:51 +0200 | [diff] [blame] | 28 | #define LOG2_SECTOR_SIZE 9 |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 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 |
Sean Anderson | d90fa0b | 2023-10-14 16:47:50 -0400 | [diff] [blame] | 38 | /* Maximum file name length */ |
| 39 | #define EXT2_NAME_LEN 255 |
| 40 | |
| 41 | /* |
| 42 | * Revision levels |
| 43 | */ |
| 44 | #define EXT2_GOOD_OLD_REV 0 /* The good old (original) format */ |
| 45 | #define EXT2_DYNAMIC_REV 1 /* V2 format w/ dynamic inode sizes */ |
| 46 | |
| 47 | #define EXT2_GOOD_OLD_INODE_SIZE 128 |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 48 | |
| 49 | /* Filetype used in directory entry. */ |
| 50 | #define FILETYPE_UNKNOWN 0 |
| 51 | #define FILETYPE_REG 1 |
| 52 | #define FILETYPE_DIRECTORY 2 |
| 53 | #define FILETYPE_SYMLINK 7 |
| 54 | |
| 55 | /* Filetype information as used in inodes. */ |
| 56 | #define FILETYPE_INO_MASK 0170000 |
| 57 | #define FILETYPE_INO_REG 0100000 |
| 58 | #define FILETYPE_INO_DIRECTORY 0040000 |
| 59 | #define FILETYPE_INO_SYMLINK 0120000 |
| 60 | #define EXT2_ROOT_INO 2 /* Root inode */ |
Sean Anderson | d90fa0b | 2023-10-14 16:47:50 -0400 | [diff] [blame] | 61 | #define EXT2_BOOT_LOADER_INO 5 /* Boot loader inode */ |
| 62 | |
| 63 | /* First non-reserved inode for old ext2 filesystems */ |
| 64 | #define EXT2_GOOD_OLD_FIRST_INO 11 |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 65 | |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 66 | /* The size of an ext2 block in bytes. */ |
| 67 | #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data)) |
| 68 | |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 69 | /* Log2 size of ext2 block in bytes. */ |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 70 | #define LOG2_BLOCK_SIZE(data) (le32_to_cpu \ |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 71 | (data->sblock.log2_block_size) \ |
| 72 | + EXT2_MIN_BLOCK_LOG_SIZE) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 73 | |
| 74 | #define EXT2_FT_DIR 2 |
| 75 | #define SUCCESS 1 |
| 76 | |
| 77 | /* Macro-instructions used to manage several block sizes */ |
| 78 | #define EXT2_MIN_BLOCK_LOG_SIZE 10 /* 1024 */ |
| 79 | #define EXT2_MAX_BLOCK_LOG_SIZE 16 /* 65536 */ |
| 80 | #define EXT2_MIN_BLOCK_SIZE (1 << EXT2_MIN_BLOCK_LOG_SIZE) |
| 81 | #define EXT2_MAX_BLOCK_SIZE (1 << EXT2_MAX_BLOCK_LOG_SIZE) |
| 82 | |
| 83 | /* The ext2 superblock. */ |
| 84 | struct ext2_sblock { |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 85 | __le32 total_inodes; |
| 86 | __le32 total_blocks; |
| 87 | __le32 reserved_blocks; |
| 88 | __le32 free_blocks; |
| 89 | __le32 free_inodes; |
| 90 | __le32 first_data_block; |
| 91 | __le32 log2_block_size; |
| 92 | __le32 log2_fragment_size; |
| 93 | __le32 blocks_per_group; |
| 94 | __le32 fragments_per_group; |
| 95 | __le32 inodes_per_group; |
| 96 | __le32 mtime; |
| 97 | __le32 utime; |
| 98 | __le16 mnt_count; |
| 99 | __le16 max_mnt_count; |
| 100 | __le16 magic; |
| 101 | __le16 fs_state; |
| 102 | __le16 error_handling; |
| 103 | __le16 minor_revision_level; |
| 104 | __le32 lastcheck; |
| 105 | __le32 checkinterval; |
| 106 | __le32 creator_os; |
| 107 | __le32 revision_level; |
| 108 | __le16 uid_reserved; |
| 109 | __le16 gid_reserved; |
| 110 | __le32 first_inode; |
| 111 | __le16 inode_size; |
| 112 | __le16 block_group_number; |
| 113 | __le32 feature_compatibility; |
| 114 | __le32 feature_incompat; |
| 115 | __le32 feature_ro_compat; |
| 116 | __le32 unique_id[4]; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 117 | char volume_name[16]; |
| 118 | char last_mounted_on[64]; |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 119 | __le32 compression_info; |
Stefan Brüns | f3c6e44 | 2016-09-17 02:10:06 +0200 | [diff] [blame] | 120 | uint8_t prealloc_blocks; |
| 121 | uint8_t prealloc_dir_blocks; |
| 122 | __le16 reserved_gdt_blocks; |
| 123 | uint8_t journal_uuid[16]; |
| 124 | __le32 journal_inode; |
| 125 | __le32 journal_dev; |
| 126 | __le32 last_orphan; |
| 127 | __le32 hash_seed[4]; |
| 128 | uint8_t default_hash_version; |
| 129 | uint8_t journal_backup_type; |
| 130 | __le16 descriptor_size; |
| 131 | __le32 default_mount_options; |
| 132 | __le32 first_meta_block_group; |
| 133 | __le32 mkfs_time; |
| 134 | __le32 journal_blocks[17]; |
| 135 | __le32 total_blocks_high; |
| 136 | __le32 reserved_blocks_high; |
| 137 | __le32 free_blocks_high; |
| 138 | __le16 min_extra_inode_size; |
| 139 | __le16 want_extra_inode_size; |
| 140 | __le32 flags; |
| 141 | __le16 raid_stride; |
| 142 | __le16 mmp_interval; |
| 143 | __le64 mmp_block; |
| 144 | __le32 raid_stripe_width; |
| 145 | uint8_t log2_groups_per_flex; |
| 146 | uint8_t checksum_type; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | struct ext2_block_group { |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 150 | __le32 block_id; /* Blocks bitmap block */ |
| 151 | __le32 inode_id; /* Inodes bitmap block */ |
| 152 | __le32 inode_table_id; /* Inodes table block */ |
| 153 | __le16 free_blocks; /* Free blocks count */ |
| 154 | __le16 free_inodes; /* Free inodes count */ |
| 155 | __le16 used_dir_cnt; /* Directories count */ |
| 156 | __le16 bg_flags; |
Stefan Brüns | f3c6e44 | 2016-09-17 02:10:06 +0200 | [diff] [blame] | 157 | __le32 bg_exclude_bitmap; |
| 158 | __le16 bg_block_id_csum; |
| 159 | __le16 bg_inode_id_csum; |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 160 | __le16 bg_itable_unused; /* Unused inodes count */ |
Stefan Brüns | f3c6e44 | 2016-09-17 02:10:06 +0200 | [diff] [blame] | 161 | __le16 bg_checksum; /* crc16(s_uuid+group_num+group_desc)*/ |
| 162 | /* following fields only exist if descriptor size is 64 */ |
| 163 | __le32 block_id_high; |
| 164 | __le32 inode_id_high; |
| 165 | __le32 inode_table_id_high; |
| 166 | __le16 free_blocks_high; |
| 167 | __le16 free_inodes_high; |
| 168 | __le16 used_dir_cnt_high; |
| 169 | __le16 bg_itable_unused_high; |
| 170 | __le32 bg_exclude_bitmap_high; |
| 171 | __le16 bg_block_id_csum_high; |
| 172 | __le16 bg_inode_id_csum_high; |
| 173 | __le32 bg_reserved; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 174 | }; |
| 175 | |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 176 | /** |
| 177 | * struct ext2_inode - ext2 inode |
| 178 | * |
| 179 | * For details see Linux file |
| 180 | * Documentation/filesystems/ext4/inodes.rst. |
| 181 | */ |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 182 | struct ext2_inode { |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 183 | /** @mode: file mode */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 184 | __le16 mode; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 185 | /** @uid: lower 16 bits of owner UID */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 186 | __le16 uid; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 187 | /** @size: lower 32 bits of file size */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 188 | __le32 size; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 189 | /** @atime: last access time */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 190 | __le32 atime; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 191 | /** @ctime: last change time */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 192 | __le32 ctime; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 193 | /** @mtime: last modification time */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 194 | __le32 mtime; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 195 | /** @dtime: deletion time */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 196 | __le32 dtime; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 197 | /** @gid: lower 16 bits of group ID */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 198 | __le16 gid; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 199 | /** @nlinks: number of hard links */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 200 | __le16 nlinks; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 201 | /** @blockcnt: lower 32 bit of block count */ |
| 202 | __le32 blockcnt; |
| 203 | /** @flags: inode flags */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 204 | __le32 flags; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 205 | /** @osd1: operating system specific data */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 206 | __le32 osd1; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 207 | /** @b: block map or extent tree */ |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 208 | union { |
| 209 | struct datablocks { |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 210 | __le32 dir_blocks[INDIRECT_BLOCKS]; |
| 211 | __le32 indir_block; |
| 212 | __le32 double_indir_block; |
| 213 | __le32 triple_indir_block; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 214 | } blocks; |
| 215 | char symlink[60]; |
Stefan Brüns | f3c6e44 | 2016-09-17 02:10:06 +0200 | [diff] [blame] | 216 | char inline_data[60]; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 217 | } b; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 218 | /** @version: file version (for NFS) */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 219 | __le32 version; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 220 | /** @acl: lower 32 bit of extended attribute block */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 221 | __le32 acl; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 222 | /** @size_high - dir_acl on ext2/3, upper 32 size bits on ext4 |
| 223 | * |
| 224 | * In ext2/3 this field was named i_dir_acl, though it was usually set |
| 225 | * to zero and never used. |
| 226 | */ |
| 227 | __le32 size_high; |
| 228 | /** @fragment_addr - (obsolete) fragment address */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 229 | __le32 fragment_addr; |
Heinrich Schuchardt | 1eae1d3 | 2024-10-25 06:56:01 +0200 | [diff] [blame] | 230 | /** @osd2: operating system specific data */ |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 231 | __le32 osd2[3]; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | /* The header of an ext2 directory entry. */ |
| 235 | struct ext2_dirent { |
Michael Walle | 2a81961 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 236 | __le32 inode; |
| 237 | __le16 direntlen; |
| 238 | __u8 namelen; |
| 239 | __u8 filetype; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | struct ext2fs_node { |
| 243 | struct ext2_data *data; |
| 244 | struct ext2_inode inode; |
| 245 | int ino; |
| 246 | int inode_read; |
| 247 | }; |
| 248 | |
| 249 | /* Information about a "mounted" ext2 filesystem. */ |
| 250 | struct ext2_data { |
| 251 | struct ext2_sblock sblock; |
| 252 | struct ext2_inode *inode; |
| 253 | struct ext2fs_node diropen; |
| 254 | }; |
| 255 | |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 256 | extern lbaint_t part_offset; |
Rob Herring | 752e1bd | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 257 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 258 | int do_ext2ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); |
| 259 | int do_ext2load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); |
| 260 | int do_ext4_load(struct cmd_tbl *cmdtp, int flag, int argc, |
| 261 | char *const argv[]); |
| 262 | int do_ext4_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); |
| 263 | int do_ext4_write(struct cmd_tbl *cmdtp, int flag, int argc, |
| 264 | char *const argv[]); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 265 | #endif |