stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * esd gmbh <www.esd-electronics.com> |
| 4 | * Reinhard Arlt <reinhard.arlt@esd-electronics.com> |
| 5 | * |
| 6 | * based on code from grub2 fs/ext2.c and fs/fshelp.c by |
| 7 | * |
| 8 | * GRUB -- GRand Unified Bootloader |
| 9 | * Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 27 | #include <ext2fs.h> |
| 28 | #include <malloc.h> |
| 29 | #include <asm/byteorder.h> |
| 30 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 31 | extern int ext2fs_devread (int sector, int byte_offset, int byte_len, |
| 32 | char *buf); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 33 | |
| 34 | /* Magic value used to identify an ext2 filesystem. */ |
| 35 | #define EXT2_MAGIC 0xEF53 |
| 36 | /* Amount of indirect blocks in an inode. */ |
| 37 | #define INDIRECT_BLOCKS 12 |
| 38 | /* Maximum lenght of a pathname. */ |
| 39 | #define EXT2_PATH_MAX 4096 |
| 40 | /* Maximum nesting of symlinks, used to prevent a loop. */ |
| 41 | #define EXT2_MAX_SYMLINKCNT 8 |
| 42 | |
| 43 | /* Filetype used in directory entry. */ |
| 44 | #define FILETYPE_UNKNOWN 0 |
| 45 | #define FILETYPE_REG 1 |
| 46 | #define FILETYPE_DIRECTORY 2 |
| 47 | #define FILETYPE_SYMLINK 7 |
| 48 | |
| 49 | /* Filetype information as used in inodes. */ |
| 50 | #define FILETYPE_INO_MASK 0170000 |
| 51 | #define FILETYPE_INO_REG 0100000 |
| 52 | #define FILETYPE_INO_DIRECTORY 0040000 |
| 53 | #define FILETYPE_INO_SYMLINK 0120000 |
| 54 | |
| 55 | /* Bits used as offset in sector */ |
| 56 | #define DISK_SECTOR_BITS 9 |
| 57 | |
| 58 | /* Log2 size of ext2 block in 512 blocks. */ |
| 59 | #define LOG2_EXT2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 1) |
| 60 | |
| 61 | /* Log2 size of ext2 block in bytes. */ |
| 62 | #define LOG2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 10) |
| 63 | |
| 64 | /* The size of an ext2 block in bytes. */ |
| 65 | #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data)) |
| 66 | |
| 67 | /* The ext2 superblock. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 68 | struct ext2_sblock { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 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 | /* The ext2 blockgroup. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 107 | struct ext2_block_group { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 108 | uint32_t block_id; |
| 109 | uint32_t inode_id; |
| 110 | uint32_t inode_table_id; |
| 111 | uint16_t free_blocks; |
| 112 | uint16_t free_inodes; |
Weirich, Bernhard | 56fdaad | 2009-06-10 14:00:37 +0200 | [diff] [blame] | 113 | uint16_t used_dir_cnt; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 114 | uint32_t reserved[3]; |
| 115 | }; |
| 116 | |
| 117 | /* The ext2 inode. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 118 | struct ext2_inode { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 119 | uint16_t mode; |
| 120 | uint16_t uid; |
| 121 | uint32_t size; |
| 122 | uint32_t atime; |
| 123 | uint32_t ctime; |
| 124 | uint32_t mtime; |
| 125 | uint32_t dtime; |
| 126 | uint16_t gid; |
| 127 | uint16_t nlinks; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 128 | uint32_t blockcnt; /* Blocks of 512 bytes!! */ |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 129 | uint32_t flags; |
| 130 | uint32_t osd1; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 131 | union { |
| 132 | struct datablocks { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 133 | uint32_t dir_blocks[INDIRECT_BLOCKS]; |
| 134 | uint32_t indir_block; |
| 135 | uint32_t double_indir_block; |
| 136 | uint32_t tripple_indir_block; |
| 137 | } blocks; |
| 138 | char symlink[60]; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 139 | } b; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 140 | uint32_t version; |
| 141 | uint32_t acl; |
| 142 | uint32_t dir_acl; |
| 143 | uint32_t fragment_addr; |
| 144 | uint32_t osd2[3]; |
| 145 | }; |
| 146 | |
| 147 | /* The header of an ext2 directory entry. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 148 | struct ext2_dirent { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 149 | uint32_t inode; |
| 150 | uint16_t direntlen; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 151 | uint8_t namelen; |
| 152 | uint8_t filetype; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 155 | struct ext2fs_node { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 156 | struct ext2_data *data; |
| 157 | struct ext2_inode inode; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 158 | int ino; |
| 159 | int inode_read; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | /* Information about a "mounted" ext2 filesystem. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 163 | struct ext2_data { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 164 | struct ext2_sblock sblock; |
| 165 | struct ext2_inode *inode; |
| 166 | struct ext2fs_node diropen; |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | typedef struct ext2fs_node *ext2fs_node_t; |
| 171 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 172 | struct ext2_data *ext2fs_root = NULL; |
| 173 | ext2fs_node_t ext2fs_file = NULL; |
| 174 | int symlinknest = 0; |
| 175 | uint32_t *indir1_block = NULL; |
| 176 | int indir1_size = 0; |
| 177 | int indir1_blkno = -1; |
| 178 | uint32_t *indir2_block = NULL; |
| 179 | int indir2_size = 0; |
| 180 | int indir2_blkno = -1; |
Michael Brandt | 270737a | 2009-11-22 14:13:27 +0100 | [diff] [blame] | 181 | static unsigned int inode_size; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 182 | |
| 183 | |
| 184 | static int ext2fs_blockgroup |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 185 | (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) { |
Weirich, Bernhard | 56fdaad | 2009-06-10 14:00:37 +0200 | [diff] [blame] | 186 | unsigned int blkno; |
| 187 | unsigned int blkoff; |
| 188 | unsigned int desc_per_blk; |
| 189 | |
| 190 | desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group); |
| 191 | |
| 192 | blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 + |
| 193 | group / desc_per_blk; |
| 194 | blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 195 | #ifdef DEBUG |
Weirich, Bernhard | 56fdaad | 2009-06-10 14:00:37 +0200 | [diff] [blame] | 196 | printf ("ext2fs read %d group descriptor (blkno %d blkoff %d)\n", |
| 197 | group, blkno, blkoff); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 198 | #endif |
Weirich, Bernhard | 56fdaad | 2009-06-10 14:00:37 +0200 | [diff] [blame] | 199 | return (ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE(data), |
| 200 | blkoff, sizeof(struct ext2_block_group), (char *)blkgrp)); |
| 201 | |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
| 205 | static int ext2fs_read_inode |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 206 | (struct ext2_data *data, int ino, struct ext2_inode *inode) { |
| 207 | struct ext2_block_group blkgrp; |
| 208 | struct ext2_sblock *sblock = &data->sblock; |
| 209 | int inodes_per_block; |
| 210 | int status; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 211 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 212 | unsigned int blkno; |
| 213 | unsigned int blkoff; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 214 | |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 215 | #ifdef DEBUG |
Michael Brandt | 270737a | 2009-11-22 14:13:27 +0100 | [diff] [blame] | 216 | printf ("ext2fs read inode %d, inode_size %d\n", ino, inode_size); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 217 | #endif |
Weirich, Bernhard | 56fdaad | 2009-06-10 14:00:37 +0200 | [diff] [blame] | 218 | /* It is easier to calculate if the first inode is 0. */ |
| 219 | ino--; |
| 220 | status = ext2fs_blockgroup (data, ino / __le32_to_cpu |
| 221 | (sblock->inodes_per_group), &blkgrp); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 222 | if (status == 0) { |
| 223 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 224 | } |
Weirich, Bernhard | 56fdaad | 2009-06-10 14:00:37 +0200 | [diff] [blame] | 225 | |
Michael Brandt | 270737a | 2009-11-22 14:13:27 +0100 | [diff] [blame] | 226 | inodes_per_block = EXT2_BLOCK_SIZE(data) / inode_size; |
Weirich, Bernhard | 56fdaad | 2009-06-10 14:00:37 +0200 | [diff] [blame] | 227 | |
| 228 | blkno = __le32_to_cpu (blkgrp.inode_table_id) + |
| 229 | (ino % __le32_to_cpu (sblock->inodes_per_group)) |
| 230 | / inodes_per_block; |
Michael Brandt | 270737a | 2009-11-22 14:13:27 +0100 | [diff] [blame] | 231 | blkoff = (ino % inodes_per_block) * inode_size; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 232 | #ifdef DEBUG |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 233 | printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 234 | #endif |
| 235 | /* Read the inode. */ |
Weirich, Bernhard | 56fdaad | 2009-06-10 14:00:37 +0200 | [diff] [blame] | 236 | status = ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE (data), blkoff, |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 237 | sizeof (struct ext2_inode), (char *) inode); |
| 238 | if (status == 0) { |
| 239 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 240 | } |
Weirich, Bernhard | 56fdaad | 2009-06-10 14:00:37 +0200 | [diff] [blame] | 241 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 242 | return (1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 246 | void ext2fs_free_node (ext2fs_node_t node, ext2fs_node_t currroot) { |
| 247 | if ((node != &ext2fs_root->diropen) && (node != currroot)) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 248 | free (node); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 253 | static int ext2fs_read_block (ext2fs_node_t node, int fileblock) { |
| 254 | struct ext2_data *data = node->data; |
| 255 | struct ext2_inode *inode = &node->inode; |
| 256 | int blknr; |
| 257 | int blksz = EXT2_BLOCK_SIZE (data); |
| 258 | int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data); |
| 259 | int status; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 260 | |
| 261 | /* Direct blocks. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 262 | if (fileblock < INDIRECT_BLOCKS) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 263 | blknr = __le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 264 | } |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 265 | /* Indirect. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 266 | else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) { |
| 267 | if (indir1_block == NULL) { |
Simon Glass | 26784f1 | 2011-12-21 10:01:27 +0000 | [diff] [blame] | 268 | indir1_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN, |
| 269 | blksz); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 270 | if (indir1_block == NULL) { |
| 271 | printf ("** ext2fs read block (indir 1) malloc failed. **\n"); |
| 272 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 273 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 274 | indir1_size = blksz; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 275 | indir1_blkno = -1; |
| 276 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 277 | if (blksz != indir1_size) { |
| 278 | free (indir1_block); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 279 | indir1_block = NULL; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 280 | indir1_size = 0; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 281 | indir1_blkno = -1; |
Simon Glass | 26784f1 | 2011-12-21 10:01:27 +0000 | [diff] [blame] | 282 | indir1_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN, |
| 283 | blksz); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 284 | if (indir1_block == NULL) { |
| 285 | printf ("** ext2fs read block (indir 1) malloc failed. **\n"); |
| 286 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 287 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 288 | indir1_size = blksz; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 289 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 290 | if ((__le32_to_cpu (inode->b.blocks.indir_block) << |
| 291 | log2_blksz) != indir1_blkno) { |
| 292 | status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz, |
| 293 | 0, blksz, |
| 294 | (char *) indir1_block); |
| 295 | if (status == 0) { |
| 296 | printf ("** ext2fs read block (indir 1) failed. **\n"); |
| 297 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 298 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 299 | indir1_blkno = |
| 300 | __le32_to_cpu (inode->b.blocks. |
| 301 | indir_block) << log2_blksz; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 302 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 303 | blknr = __le32_to_cpu (indir1_block |
| 304 | [fileblock - INDIRECT_BLOCKS]); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 305 | } |
| 306 | /* Double indirect. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 307 | else if (fileblock < |
| 308 | (INDIRECT_BLOCKS + (blksz / 4 * (blksz / 4 + 1)))) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 309 | unsigned int perblock = blksz / 4; |
| 310 | unsigned int rblock = fileblock - (INDIRECT_BLOCKS |
| 311 | + blksz / 4); |
| 312 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 313 | if (indir1_block == NULL) { |
Simon Glass | 26784f1 | 2011-12-21 10:01:27 +0000 | [diff] [blame] | 314 | indir1_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN, |
| 315 | blksz); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 316 | if (indir1_block == NULL) { |
| 317 | printf ("** ext2fs read block (indir 2 1) malloc failed. **\n"); |
| 318 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 319 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 320 | indir1_size = blksz; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 321 | indir1_blkno = -1; |
| 322 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 323 | if (blksz != indir1_size) { |
| 324 | free (indir1_block); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 325 | indir1_block = NULL; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 326 | indir1_size = 0; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 327 | indir1_blkno = -1; |
Simon Glass | 26784f1 | 2011-12-21 10:01:27 +0000 | [diff] [blame] | 328 | indir1_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN, |
| 329 | blksz); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 330 | if (indir1_block == NULL) { |
| 331 | printf ("** ext2fs read block (indir 2 1) malloc failed. **\n"); |
| 332 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 333 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 334 | indir1_size = blksz; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 335 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 336 | if ((__le32_to_cpu (inode->b.blocks.double_indir_block) << |
| 337 | log2_blksz) != indir1_blkno) { |
| 338 | status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz, |
| 339 | 0, blksz, |
| 340 | (char *) indir1_block); |
| 341 | if (status == 0) { |
| 342 | printf ("** ext2fs read block (indir 2 1) failed. **\n"); |
| 343 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 344 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 345 | indir1_blkno = |
| 346 | __le32_to_cpu (inode->b.blocks.double_indir_block) << log2_blksz; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 347 | } |
| 348 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 349 | if (indir2_block == NULL) { |
Simon Glass | 26784f1 | 2011-12-21 10:01:27 +0000 | [diff] [blame] | 350 | indir2_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN, |
| 351 | blksz); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 352 | if (indir2_block == NULL) { |
| 353 | printf ("** ext2fs read block (indir 2 2) malloc failed. **\n"); |
| 354 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 355 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 356 | indir2_size = blksz; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 357 | indir2_blkno = -1; |
| 358 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 359 | if (blksz != indir2_size) { |
| 360 | free (indir2_block); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 361 | indir2_block = NULL; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 362 | indir2_size = 0; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 363 | indir2_blkno = -1; |
Simon Glass | 26784f1 | 2011-12-21 10:01:27 +0000 | [diff] [blame] | 364 | indir2_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN, |
| 365 | blksz); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 366 | if (indir2_block == NULL) { |
| 367 | printf ("** ext2fs read block (indir 2 2) malloc failed. **\n"); |
| 368 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 369 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 370 | indir2_size = blksz; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 371 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 372 | if ((__le32_to_cpu (indir1_block[rblock / perblock]) << |
Aaron Pace | a2740dd | 2010-07-26 14:24:44 -0600 | [diff] [blame] | 373 | log2_blksz) != indir2_blkno) { |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 374 | status = ext2fs_devread (__le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz, |
| 375 | 0, blksz, |
| 376 | (char *) indir2_block); |
| 377 | if (status == 0) { |
| 378 | printf ("** ext2fs read block (indir 2 2) failed. **\n"); |
| 379 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 380 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 381 | indir2_blkno = |
| 382 | __le32_to_cpu (indir1_block[rblock / perblock]) << log2_blksz; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 383 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 384 | blknr = __le32_to_cpu (indir2_block[rblock % perblock]); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 385 | } |
| 386 | /* Tripple indirect. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 387 | else { |
| 388 | printf ("** ext2fs doesn't support tripple indirect blocks. **\n"); |
| 389 | return (-1); |
| 390 | } |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 391 | #ifdef DEBUG |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 392 | printf ("ext2fs_read_block %08x\n", blknr); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 393 | #endif |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 394 | return (blknr); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | |
| 398 | int ext2fs_read_file |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 399 | (ext2fs_node_t node, int pos, unsigned int len, char *buf) { |
| 400 | int i; |
| 401 | int blockcnt; |
| 402 | int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data); |
| 403 | int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS); |
Stefan Roese | a7b9fb9 | 2006-01-18 20:05:34 +0100 | [diff] [blame] | 404 | unsigned int filesize = __le32_to_cpu(node->inode.size); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 405 | |
| 406 | /* Adjust len so it we can't read past the end of the file. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 407 | if (len > filesize) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 408 | len = filesize; |
| 409 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 410 | blockcnt = ((len + pos) + blocksize - 1) / blocksize; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 411 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 412 | for (i = pos / blocksize; i < blockcnt; i++) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 413 | int blknr; |
| 414 | int blockoff = pos % blocksize; |
| 415 | int blockend = blocksize; |
| 416 | |
| 417 | int skipfirst = 0; |
| 418 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 419 | blknr = ext2fs_read_block (node, i); |
| 420 | if (blknr < 0) { |
| 421 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 422 | } |
| 423 | blknr = blknr << log2blocksize; |
| 424 | |
| 425 | /* Last block. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 426 | if (i == blockcnt - 1) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 427 | blockend = (len + pos) % blocksize; |
| 428 | |
| 429 | /* The last portion is exactly blocksize. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 430 | if (!blockend) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 431 | blockend = blocksize; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* First block. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 436 | if (i == pos / blocksize) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 437 | skipfirst = blockoff; |
| 438 | blockend -= skipfirst; |
| 439 | } |
| 440 | |
| 441 | /* If the block number is 0 this block is not stored on disk but |
| 442 | is zero filled instead. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 443 | if (blknr) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 444 | int status; |
| 445 | |
| 446 | status = ext2fs_devread (blknr, skipfirst, blockend, buf); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 447 | if (status == 0) { |
| 448 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 449 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 450 | } else { |
Wolfgang Denk | 0ddb896 | 2008-01-09 10:16:33 +0100 | [diff] [blame] | 451 | memset (buf, 0, blocksize - skipfirst); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 452 | } |
| 453 | buf += blocksize - skipfirst; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 454 | } |
| 455 | return (len); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 459 | static int ext2fs_iterate_dir (ext2fs_node_t dir, char *name, ext2fs_node_t * fnode, int *ftype) |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 460 | { |
| 461 | unsigned int fpos = 0; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 462 | int status; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 463 | struct ext2fs_node *diro = (struct ext2fs_node *) dir; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 464 | |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 465 | #ifdef DEBUG |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 466 | if (name != NULL) |
| 467 | printf ("Iterate dir %s\n", name); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 468 | #endif /* of DEBUG */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 469 | if (!diro->inode_read) { |
| 470 | status = ext2fs_read_inode (diro->data, diro->ino, |
| 471 | &diro->inode); |
| 472 | if (status == 0) { |
| 473 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | /* Search the file. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 477 | while (fpos < __le32_to_cpu (diro->inode.size)) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 478 | struct ext2_dirent dirent; |
| 479 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 480 | status = ext2fs_read_file (diro, fpos, |
| 481 | sizeof (struct ext2_dirent), |
| 482 | (char *) &dirent); |
| 483 | if (status < 1) { |
| 484 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 485 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 486 | if (dirent.namelen != 0) { |
| 487 | char filename[dirent.namelen + 1]; |
| 488 | ext2fs_node_t fdiro; |
| 489 | int type = FILETYPE_UNKNOWN; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 490 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 491 | status = ext2fs_read_file (diro, |
| 492 | fpos + sizeof (struct ext2_dirent), |
| 493 | dirent.namelen, filename); |
| 494 | if (status < 1) { |
| 495 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 496 | } |
| 497 | fdiro = malloc (sizeof (struct ext2fs_node)); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 498 | if (!fdiro) { |
| 499 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | fdiro->data = diro->data; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 503 | fdiro->ino = __le32_to_cpu (dirent.inode); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 504 | |
| 505 | filename[dirent.namelen] = '\0'; |
| 506 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 507 | if (dirent.filetype != FILETYPE_UNKNOWN) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 508 | fdiro->inode_read = 0; |
| 509 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 510 | if (dirent.filetype == FILETYPE_DIRECTORY) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 511 | type = FILETYPE_DIRECTORY; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 512 | } else if (dirent.filetype == |
| 513 | FILETYPE_SYMLINK) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 514 | type = FILETYPE_SYMLINK; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 515 | } else if (dirent.filetype == FILETYPE_REG) { |
| 516 | type = FILETYPE_REG; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 517 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 518 | } else { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 519 | /* The filetype can not be read from the dirent, get it from inode */ |
| 520 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 521 | status = ext2fs_read_inode (diro->data, |
| 522 | __le32_to_cpu(dirent.inode), |
| 523 | &fdiro->inode); |
| 524 | if (status == 0) { |
| 525 | free (fdiro); |
| 526 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 527 | } |
| 528 | fdiro->inode_read = 1; |
| 529 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 530 | if ((__le16_to_cpu (fdiro->inode.mode) & |
| 531 | FILETYPE_INO_MASK) == |
| 532 | FILETYPE_INO_DIRECTORY) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 533 | type = FILETYPE_DIRECTORY; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 534 | } else if ((__le16_to_cpu (fdiro->inode.mode) |
| 535 | & FILETYPE_INO_MASK) == |
| 536 | FILETYPE_INO_SYMLINK) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 537 | type = FILETYPE_SYMLINK; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 538 | } else if ((__le16_to_cpu (fdiro->inode.mode) |
| 539 | & FILETYPE_INO_MASK) == |
| 540 | FILETYPE_INO_REG) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 541 | type = FILETYPE_REG; |
| 542 | } |
| 543 | } |
| 544 | #ifdef DEBUG |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 545 | printf ("iterate >%s<\n", filename); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 546 | #endif /* of DEBUG */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 547 | if ((name != NULL) && (fnode != NULL) |
| 548 | && (ftype != NULL)) { |
| 549 | if (strcmp (filename, name) == 0) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 550 | *ftype = type; |
| 551 | *fnode = fdiro; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 552 | return (1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 553 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 554 | } else { |
| 555 | if (fdiro->inode_read == 0) { |
| 556 | status = ext2fs_read_inode (diro->data, |
| 557 | __le32_to_cpu (dirent.inode), |
| 558 | &fdiro->inode); |
| 559 | if (status == 0) { |
| 560 | free (fdiro); |
| 561 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 562 | } |
| 563 | fdiro->inode_read = 1; |
| 564 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 565 | switch (type) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 566 | case FILETYPE_DIRECTORY: |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 567 | printf ("<DIR> "); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 568 | break; |
| 569 | case FILETYPE_SYMLINK: |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 570 | printf ("<SYM> "); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 571 | break; |
| 572 | case FILETYPE_REG: |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 573 | printf (" "); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 574 | break; |
| 575 | default: |
wdenk | ec0aee7 | 2004-12-19 09:58:11 +0000 | [diff] [blame] | 576 | printf ("< ? > "); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 577 | break; |
| 578 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 579 | printf ("%10d %s\n", |
| 580 | __le32_to_cpu (fdiro->inode.size), |
| 581 | filename); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 582 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 583 | free (fdiro); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 584 | } |
| 585 | fpos += __le16_to_cpu (dirent.direntlen); |
| 586 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 587 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 591 | static char *ext2fs_read_symlink (ext2fs_node_t node) { |
| 592 | char *symlink; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 593 | struct ext2fs_node *diro = node; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 594 | int status; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 595 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 596 | if (!diro->inode_read) { |
| 597 | status = ext2fs_read_inode (diro->data, diro->ino, |
| 598 | &diro->inode); |
| 599 | if (status == 0) { |
| 600 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | symlink = malloc (__le32_to_cpu (diro->inode.size) + 1); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 604 | if (!symlink) { |
| 605 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 606 | } |
| 607 | /* If the filesize of the symlink is bigger than |
| 608 | 60 the symlink is stored in a separate block, |
| 609 | otherwise it is stored in the inode. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 610 | if (__le32_to_cpu (diro->inode.size) <= 60) { |
| 611 | strncpy (symlink, diro->inode.b.symlink, |
| 612 | __le32_to_cpu (diro->inode.size)); |
| 613 | } else { |
| 614 | status = ext2fs_read_file (diro, 0, |
| 615 | __le32_to_cpu (diro->inode.size), |
| 616 | symlink); |
| 617 | if (status == 0) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 618 | free (symlink); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 619 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | symlink[__le32_to_cpu (diro->inode.size)] = '\0'; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 623 | return (symlink); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | |
| 627 | int ext2fs_find_file1 |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 628 | (const char *currpath, |
| 629 | ext2fs_node_t currroot, ext2fs_node_t * currfound, int *foundtype) { |
| 630 | char fpath[strlen (currpath) + 1]; |
| 631 | char *name = fpath; |
| 632 | char *next; |
| 633 | int status; |
| 634 | int type = FILETYPE_DIRECTORY; |
| 635 | ext2fs_node_t currnode = currroot; |
| 636 | ext2fs_node_t oldnode = currroot; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 637 | |
| 638 | strncpy (fpath, currpath, strlen (currpath) + 1); |
| 639 | |
| 640 | /* Remove all leading slashes. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 641 | while (*name == '/') { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 642 | name++; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 643 | } |
| 644 | if (!*name) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 645 | *currfound = currnode; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 646 | return (1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 647 | } |
| 648 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 649 | for (;;) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 650 | int found; |
| 651 | |
| 652 | /* Extract the actual part from the pathname. */ |
| 653 | next = strchr (name, '/'); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 654 | if (next) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 655 | /* Remove all leading slashes. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 656 | while (*next == '/') { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 657 | *(next++) = '\0'; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /* At this point it is expected that the current node is a directory, check if this is true. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 662 | if (type != FILETYPE_DIRECTORY) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 663 | ext2fs_free_node (currnode, currroot); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 664 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | oldnode = currnode; |
| 668 | |
| 669 | /* Iterate over the directory. */ |
| 670 | found = ext2fs_iterate_dir (currnode, name, &currnode, &type); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 671 | if (found == 0) { |
| 672 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 673 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 674 | if (found == -1) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 675 | break; |
| 676 | } |
| 677 | |
| 678 | /* Read in the symlink and follow it. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 679 | if (type == FILETYPE_SYMLINK) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 680 | char *symlink; |
| 681 | |
| 682 | /* Test if the symlink does not loop. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 683 | if (++symlinknest == 8) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 684 | ext2fs_free_node (currnode, currroot); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 685 | ext2fs_free_node (oldnode, currroot); |
| 686 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | symlink = ext2fs_read_symlink (currnode); |
| 690 | ext2fs_free_node (currnode, currroot); |
| 691 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 692 | if (!symlink) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 693 | ext2fs_free_node (oldnode, currroot); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 694 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 695 | } |
| 696 | #ifdef DEBUG |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 697 | printf ("Got symlink >%s<\n", symlink); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 698 | #endif /* of DEBUG */ |
| 699 | /* The symlink is an absolute path, go back to the root inode. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 700 | if (symlink[0] == '/') { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 701 | ext2fs_free_node (oldnode, currroot); |
| 702 | oldnode = &ext2fs_root->diropen; |
| 703 | } |
| 704 | |
| 705 | /* Lookup the node the symlink points to. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 706 | status = ext2fs_find_file1 (symlink, oldnode, |
| 707 | &currnode, &type); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 708 | |
| 709 | free (symlink); |
| 710 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 711 | if (status == 0) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 712 | ext2fs_free_node (oldnode, currroot); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 713 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
| 717 | ext2fs_free_node (oldnode, currroot); |
| 718 | |
| 719 | /* Found the node! */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 720 | if (!next || *next == '\0') { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 721 | *currfound = currnode; |
| 722 | *foundtype = type; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 723 | return (1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 724 | } |
| 725 | name = next; |
| 726 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 727 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | |
| 731 | int ext2fs_find_file |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 732 | (const char *path, |
| 733 | ext2fs_node_t rootnode, ext2fs_node_t * foundnode, int expecttype) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 734 | int status; |
| 735 | int foundtype = FILETYPE_DIRECTORY; |
| 736 | |
| 737 | |
| 738 | symlinknest = 0; |
wdenk | 20a8041 | 2005-02-04 15:02:06 +0000 | [diff] [blame] | 739 | if (!path) { |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 740 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 741 | } |
| 742 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 743 | status = ext2fs_find_file1 (path, rootnode, foundnode, &foundtype); |
| 744 | if (status == 0) { |
| 745 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 746 | } |
| 747 | /* Check if the node that was found was of the expected type. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 748 | if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) { |
| 749 | return (0); |
| 750 | } else if ((expecttype == FILETYPE_DIRECTORY) |
| 751 | && (foundtype != expecttype)) { |
| 752 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 753 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 754 | return (1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | |
Mike Frysinger | c87f645 | 2010-10-20 07:18:09 -0400 | [diff] [blame] | 758 | int ext2fs_ls (const char *dirname) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 759 | ext2fs_node_t dirnode; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 760 | int status; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 761 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 762 | if (ext2fs_root == NULL) { |
| 763 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 764 | } |
| 765 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 766 | status = ext2fs_find_file (dirname, &ext2fs_root->diropen, &dirnode, |
| 767 | FILETYPE_DIRECTORY); |
| 768 | if (status != 1) { |
| 769 | printf ("** Can not find directory. **\n"); |
| 770 | return (1); |
| 771 | } |
| 772 | ext2fs_iterate_dir (dirnode, NULL, NULL, NULL); |
| 773 | ext2fs_free_node (dirnode, &ext2fs_root->diropen); |
| 774 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | |
Mike Frysinger | c87f645 | 2010-10-20 07:18:09 -0400 | [diff] [blame] | 778 | int ext2fs_open (const char *filename) { |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 779 | ext2fs_node_t fdiro = NULL; |
| 780 | int status; |
| 781 | int len; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 782 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 783 | if (ext2fs_root == NULL) { |
wdenk | 20a8041 | 2005-02-04 15:02:06 +0000 | [diff] [blame] | 784 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 785 | } |
| 786 | ext2fs_file = NULL; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 787 | status = ext2fs_find_file (filename, &ext2fs_root->diropen, &fdiro, |
| 788 | FILETYPE_REG); |
| 789 | if (status == 0) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 790 | goto fail; |
| 791 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 792 | if (!fdiro->inode_read) { |
| 793 | status = ext2fs_read_inode (fdiro->data, fdiro->ino, |
| 794 | &fdiro->inode); |
| 795 | if (status == 0) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 796 | goto fail; |
| 797 | } |
| 798 | } |
| 799 | len = __le32_to_cpu (fdiro->inode.size); |
| 800 | ext2fs_file = fdiro; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 801 | return (len); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 802 | |
wdenk | 20a8041 | 2005-02-04 15:02:06 +0000 | [diff] [blame] | 803 | fail: |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 804 | ext2fs_free_node (fdiro, &ext2fs_root->diropen); |
wdenk | 20a8041 | 2005-02-04 15:02:06 +0000 | [diff] [blame] | 805 | return (-1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 809 | int ext2fs_close (void |
| 810 | ) { |
| 811 | if ((ext2fs_file != NULL) && (ext2fs_root != NULL)) { |
| 812 | ext2fs_free_node (ext2fs_file, &ext2fs_root->diropen); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 813 | ext2fs_file = NULL; |
| 814 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 815 | if (ext2fs_root != NULL) { |
| 816 | free (ext2fs_root); |
| 817 | ext2fs_root = NULL; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 818 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 819 | if (indir1_block != NULL) { |
| 820 | free (indir1_block); |
| 821 | indir1_block = NULL; |
| 822 | indir1_size = 0; |
| 823 | indir1_blkno = -1; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 824 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 825 | if (indir2_block != NULL) { |
| 826 | free (indir2_block); |
| 827 | indir2_block = NULL; |
| 828 | indir2_size = 0; |
| 829 | indir2_blkno = -1; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 830 | } |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 831 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 835 | int ext2fs_read (char *buf, unsigned len) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 836 | int status; |
| 837 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 838 | if (ext2fs_root == NULL) { |
| 839 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 840 | } |
| 841 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 842 | if (ext2fs_file == NULL) { |
| 843 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 844 | } |
| 845 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 846 | status = ext2fs_read_file (ext2fs_file, 0, len, buf); |
| 847 | return (status); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 851 | int ext2fs_mount (unsigned part_length) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 852 | struct ext2_data *data; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 853 | int status; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 854 | |
| 855 | data = malloc (sizeof (struct ext2_data)); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 856 | if (!data) { |
| 857 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 858 | } |
| 859 | /* Read the superblock. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 860 | status = ext2fs_devread (1 * 2, 0, sizeof (struct ext2_sblock), |
| 861 | (char *) &data->sblock); |
| 862 | if (status == 0) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 863 | goto fail; |
| 864 | } |
| 865 | /* Make sure this is an ext2 filesystem. */ |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 866 | if (__le16_to_cpu (data->sblock.magic) != EXT2_MAGIC) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 867 | goto fail; |
| 868 | } |
Michael Brandt | 270737a | 2009-11-22 14:13:27 +0100 | [diff] [blame] | 869 | if (__le32_to_cpu(data->sblock.revision_level == 0)) { |
| 870 | inode_size = 128; |
| 871 | } else { |
| 872 | inode_size = __le16_to_cpu(data->sblock.inode_size); |
| 873 | } |
| 874 | #ifdef DEBUG |
| 875 | printf("EXT2 rev %d, inode_size %d\n", |
| 876 | __le32_to_cpu(data->sblock.revision_level), inode_size); |
| 877 | #endif |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 878 | data->diropen.data = data; |
| 879 | data->diropen.ino = 2; |
| 880 | data->diropen.inode_read = 1; |
| 881 | data->inode = &data->diropen.inode; |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 882 | |
| 883 | status = ext2fs_read_inode (data, 2, data->inode); |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 884 | if (status == 0) { |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 885 | goto fail; |
| 886 | } |
| 887 | |
| 888 | ext2fs_root = data; |
| 889 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 890 | return (1); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 891 | |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 892 | fail: |
| 893 | printf ("Failed to mount ext2 filesystem...\n"); |
| 894 | free (data); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 895 | ext2fs_root = NULL; |
wdenk | efe2a4d | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 896 | return (0); |
stroese | 2b91871 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 897 | } |