Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 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 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 18 | * SPDX-License-Identifier: GPL-2.0+ |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #ifndef __EXT_COMMON__ |
| 22 | #define __EXT_COMMON__ |
| 23 | #include <command.h> |
| 24 | #define SECTOR_SIZE 0x200 |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 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 | |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 48 | /* The size of an ext2 block in bytes. */ |
| 49 | #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data)) |
| 50 | |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 51 | /* Log2 size of ext2 block in bytes. */ |
Michael Walle | 7f101be | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 52 | #define LOG2_BLOCK_SIZE(data) (le32_to_cpu \ |
Egbert Eich | 50ce4c0 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 53 | (data->sblock.log2_block_size) \ |
| 54 | + EXT2_MIN_BLOCK_LOG_SIZE) |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 55 | |
| 56 | #define EXT2_FT_DIR 2 |
| 57 | #define SUCCESS 1 |
| 58 | |
| 59 | /* Macro-instructions used to manage several block sizes */ |
| 60 | #define EXT2_MIN_BLOCK_LOG_SIZE 10 /* 1024 */ |
| 61 | #define EXT2_MAX_BLOCK_LOG_SIZE 16 /* 65536 */ |
| 62 | #define EXT2_MIN_BLOCK_SIZE (1 << EXT2_MIN_BLOCK_LOG_SIZE) |
| 63 | #define EXT2_MAX_BLOCK_SIZE (1 << EXT2_MAX_BLOCK_LOG_SIZE) |
| 64 | |
| 65 | /* The ext2 superblock. */ |
| 66 | struct ext2_sblock { |
Michael Walle | 2a0b7a9 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 67 | __le32 total_inodes; |
| 68 | __le32 total_blocks; |
| 69 | __le32 reserved_blocks; |
| 70 | __le32 free_blocks; |
| 71 | __le32 free_inodes; |
| 72 | __le32 first_data_block; |
| 73 | __le32 log2_block_size; |
| 74 | __le32 log2_fragment_size; |
| 75 | __le32 blocks_per_group; |
| 76 | __le32 fragments_per_group; |
| 77 | __le32 inodes_per_group; |
| 78 | __le32 mtime; |
| 79 | __le32 utime; |
| 80 | __le16 mnt_count; |
| 81 | __le16 max_mnt_count; |
| 82 | __le16 magic; |
| 83 | __le16 fs_state; |
| 84 | __le16 error_handling; |
| 85 | __le16 minor_revision_level; |
| 86 | __le32 lastcheck; |
| 87 | __le32 checkinterval; |
| 88 | __le32 creator_os; |
| 89 | __le32 revision_level; |
| 90 | __le16 uid_reserved; |
| 91 | __le16 gid_reserved; |
| 92 | __le32 first_inode; |
| 93 | __le16 inode_size; |
| 94 | __le16 block_group_number; |
| 95 | __le32 feature_compatibility; |
| 96 | __le32 feature_incompat; |
| 97 | __le32 feature_ro_compat; |
| 98 | __le32 unique_id[4]; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 99 | char volume_name[16]; |
| 100 | char last_mounted_on[64]; |
Michael Walle | 2a0b7a9 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 101 | __le32 compression_info; |
Stefan Brüns | 3ee2f97 | 2016-09-17 02:10:06 +0200 | [diff] [blame] | 102 | uint8_t prealloc_blocks; |
| 103 | uint8_t prealloc_dir_blocks; |
| 104 | __le16 reserved_gdt_blocks; |
| 105 | uint8_t journal_uuid[16]; |
| 106 | __le32 journal_inode; |
| 107 | __le32 journal_dev; |
| 108 | __le32 last_orphan; |
| 109 | __le32 hash_seed[4]; |
| 110 | uint8_t default_hash_version; |
| 111 | uint8_t journal_backup_type; |
| 112 | __le16 descriptor_size; |
| 113 | __le32 default_mount_options; |
| 114 | __le32 first_meta_block_group; |
| 115 | __le32 mkfs_time; |
| 116 | __le32 journal_blocks[17]; |
| 117 | __le32 total_blocks_high; |
| 118 | __le32 reserved_blocks_high; |
| 119 | __le32 free_blocks_high; |
| 120 | __le16 min_extra_inode_size; |
| 121 | __le16 want_extra_inode_size; |
| 122 | __le32 flags; |
| 123 | __le16 raid_stride; |
| 124 | __le16 mmp_interval; |
| 125 | __le64 mmp_block; |
| 126 | __le32 raid_stripe_width; |
| 127 | uint8_t log2_groups_per_flex; |
| 128 | uint8_t checksum_type; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | struct ext2_block_group { |
Michael Walle | 2a0b7a9 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 132 | __le32 block_id; /* Blocks bitmap block */ |
| 133 | __le32 inode_id; /* Inodes bitmap block */ |
| 134 | __le32 inode_table_id; /* Inodes table block */ |
| 135 | __le16 free_blocks; /* Free blocks count */ |
| 136 | __le16 free_inodes; /* Free inodes count */ |
| 137 | __le16 used_dir_cnt; /* Directories count */ |
| 138 | __le16 bg_flags; |
Stefan Brüns | 3ee2f97 | 2016-09-17 02:10:06 +0200 | [diff] [blame] | 139 | __le32 bg_exclude_bitmap; |
| 140 | __le16 bg_block_id_csum; |
| 141 | __le16 bg_inode_id_csum; |
Michael Walle | 2a0b7a9 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 142 | __le16 bg_itable_unused; /* Unused inodes count */ |
Stefan Brüns | 3ee2f97 | 2016-09-17 02:10:06 +0200 | [diff] [blame] | 143 | __le16 bg_checksum; /* crc16(s_uuid+group_num+group_desc)*/ |
| 144 | /* following fields only exist if descriptor size is 64 */ |
| 145 | __le32 block_id_high; |
| 146 | __le32 inode_id_high; |
| 147 | __le32 inode_table_id_high; |
| 148 | __le16 free_blocks_high; |
| 149 | __le16 free_inodes_high; |
| 150 | __le16 used_dir_cnt_high; |
| 151 | __le16 bg_itable_unused_high; |
| 152 | __le32 bg_exclude_bitmap_high; |
| 153 | __le16 bg_block_id_csum_high; |
| 154 | __le16 bg_inode_id_csum_high; |
| 155 | __le32 bg_reserved; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | /* The ext2 inode. */ |
| 159 | struct ext2_inode { |
Michael Walle | 2a0b7a9 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 160 | __le16 mode; |
| 161 | __le16 uid; |
| 162 | __le32 size; |
| 163 | __le32 atime; |
| 164 | __le32 ctime; |
| 165 | __le32 mtime; |
| 166 | __le32 dtime; |
| 167 | __le16 gid; |
| 168 | __le16 nlinks; |
Stefan Brüns | 3ee2f97 | 2016-09-17 02:10:06 +0200 | [diff] [blame] | 169 | __le32 blockcnt; /* Blocks of either 512 or block_size bytes */ |
Michael Walle | 2a0b7a9 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 170 | __le32 flags; |
| 171 | __le32 osd1; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 172 | union { |
| 173 | struct datablocks { |
Michael Walle | 2a0b7a9 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 174 | __le32 dir_blocks[INDIRECT_BLOCKS]; |
| 175 | __le32 indir_block; |
| 176 | __le32 double_indir_block; |
| 177 | __le32 triple_indir_block; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 178 | } blocks; |
| 179 | char symlink[60]; |
Stefan Brüns | 3ee2f97 | 2016-09-17 02:10:06 +0200 | [diff] [blame] | 180 | char inline_data[60]; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 181 | } b; |
Michael Walle | 2a0b7a9 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 182 | __le32 version; |
| 183 | __le32 acl; |
Stefan Brüns | 3ee2f97 | 2016-09-17 02:10:06 +0200 | [diff] [blame] | 184 | __le32 size_high; /* previously dir_acl, but never used */ |
Michael Walle | 2a0b7a9 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 185 | __le32 fragment_addr; |
| 186 | __le32 osd2[3]; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | /* The header of an ext2 directory entry. */ |
| 190 | struct ext2_dirent { |
Michael Walle | 2a0b7a9 | 2016-08-29 10:46:43 +0200 | [diff] [blame] | 191 | __le32 inode; |
| 192 | __le16 direntlen; |
| 193 | __u8 namelen; |
| 194 | __u8 filetype; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | struct ext2fs_node { |
| 198 | struct ext2_data *data; |
| 199 | struct ext2_inode inode; |
| 200 | int ino; |
| 201 | int inode_read; |
| 202 | }; |
| 203 | |
| 204 | /* Information about a "mounted" ext2 filesystem. */ |
| 205 | struct ext2_data { |
| 206 | struct ext2_sblock sblock; |
| 207 | struct ext2_inode *inode; |
| 208 | struct ext2fs_node diropen; |
| 209 | }; |
| 210 | |
Frederic Leroy | 04735e9 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 211 | extern lbaint_t part_offset; |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 212 | |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 213 | int do_ext2ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); |
| 214 | int do_ext2load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); |
| 215 | int do_ext4_load(cmd_tbl_t *cmdtp, int flag, int argc, |
| 216 | char *const argv[]); |
| 217 | int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]); |
Uma Shankar | ed34f34 | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 218 | int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc, |
| 219 | char *const argv[]); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 220 | #endif |