| 1 |
27 |
unneback |
//==========================================================================
|
| 2 |
|
|
//
|
| 3 |
|
|
// e2fs.c
|
| 4 |
|
|
//
|
| 5 |
|
|
// RedBoot support for second extended filesystem
|
| 6 |
|
|
//
|
| 7 |
|
|
//==========================================================================
|
| 8 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
| 9 |
|
|
// -------------------------------------------
|
| 10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
| 11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
| 12 |
|
|
// Copyright (C) 2003 Gary Thomas <gary@mind.be>
|
| 13 |
|
|
//
|
| 14 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
| 15 |
|
|
// the terms of the GNU General Public License as published by the Free
|
| 16 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
| 17 |
|
|
//
|
| 18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
| 19 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 21 |
|
|
// for more details.
|
| 22 |
|
|
//
|
| 23 |
|
|
// You should have received a copy of the GNU General Public License along
|
| 24 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
| 25 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
| 26 |
|
|
//
|
| 27 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
| 28 |
|
|
// or inline functions from this file, or you compile this file and link it
|
| 29 |
|
|
// with other works to produce a work based on this file, this file does not
|
| 30 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
| 31 |
|
|
// License. However the source code for this file must still be made available
|
| 32 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
| 33 |
|
|
//
|
| 34 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
| 35 |
|
|
// this file might be covered by the GNU General Public License.
|
| 36 |
|
|
//
|
| 37 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
| 38 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
| 39 |
|
|
// -------------------------------------------
|
| 40 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
| 41 |
|
|
//==========================================================================
|
| 42 |
|
|
//#####DESCRIPTIONBEGIN####
|
| 43 |
|
|
//
|
| 44 |
|
|
// Author(s): msalter
|
| 45 |
|
|
// Contributors: msalter
|
| 46 |
|
|
// Date: 2001-07-14
|
| 47 |
|
|
// Purpose:
|
| 48 |
|
|
// Description:
|
| 49 |
|
|
//
|
| 50 |
|
|
// This code is part of RedBoot (tm).
|
| 51 |
|
|
//
|
| 52 |
|
|
//####DESCRIPTIONEND####
|
| 53 |
|
|
//
|
| 54 |
|
|
//==========================================================================
|
| 55 |
|
|
|
| 56 |
|
|
#include <redboot.h>
|
| 57 |
|
|
#include <fs/disk.h>
|
| 58 |
|
|
#include <fs/e2fs.h>
|
| 59 |
|
|
|
| 60 |
|
|
#define DEBUG_E2FS 0
|
| 61 |
|
|
|
| 62 |
|
|
#if DEBUG_E2FS > 4
|
| 63 |
|
|
static void dump_sb(struct e2fs_super_block *s);
|
| 64 |
|
|
static void dump_inode(struct e2fs_inode *i);
|
| 65 |
|
|
#endif
|
| 66 |
|
|
|
| 67 |
|
|
static void *e2fs_open(partition_t *p, const char *path);
|
| 68 |
|
|
static int e2fs_read(void *fp, char *buf, cyg_uint32 nbytes);
|
| 69 |
|
|
|
| 70 |
|
|
// This structure is the only thing exported by this module.
|
| 71 |
|
|
// These filesystem function pointers are attached to disk
|
| 72 |
|
|
// partitions in the generic disk handling code.
|
| 73 |
|
|
//
|
| 74 |
|
|
fs_funs_t redboot_e2fs_funs = {
|
| 75 |
|
|
e2fs_open,
|
| 76 |
|
|
e2fs_read
|
| 77 |
|
|
};
|
| 78 |
|
|
|
| 79 |
|
|
// A single block buffer to be shared carefully.
|
| 80 |
|
|
static cyg_uint32 blockbuf[E2FS_MAX_BLOCK_SIZE/sizeof(cyg_uint32)];
|
| 81 |
|
|
|
| 82 |
|
|
#define __READ_BLOCK(n) \
|
| 83 |
|
|
PARTITION_READ(e2fs->part, E2FS_BLOCK_TO_SECTOR(e2fs, (n)), \
|
| 84 |
|
|
blockbuf, e2fs->blocksize/SECTOR_SIZE)
|
| 85 |
|
|
|
| 86 |
|
|
// Get a group descriptor. Returns non-zero for success.
|
| 87 |
|
|
//
|
| 88 |
|
|
static int
|
| 89 |
|
|
e2fs_get_gdesc(e2fs_desc_t *e2fs, cyg_uint32 group_nr, e2fs_group_t *gdesc)
|
| 90 |
|
|
{
|
| 91 |
|
|
cyg_uint32 sec_nr;
|
| 92 |
|
|
|
| 93 |
|
|
if (group_nr < e2fs->gdesc_first ||
|
| 94 |
|
|
group_nr >= (e2fs->gdesc_first + E2FS_GDESC_CACHE_SIZE)) {
|
| 95 |
|
|
|
| 96 |
|
|
// cache miss
|
| 97 |
|
|
sec_nr = E2FS_BLOCK_TO_SECTOR(e2fs, e2fs->gdesc_block);
|
| 98 |
|
|
sec_nr += (group_nr / E2FS_GDESC_PER_SECTOR);
|
| 99 |
|
|
|
| 100 |
|
|
#if DEBUG_E2FS > 2
|
| 101 |
|
|
diag_printf("%s: group[%d] cache miss, sec_nr[%d]\n",
|
| 102 |
|
|
__FUNCTION__, group_nr, sec_nr);
|
| 103 |
|
|
#endif
|
| 104 |
|
|
if (!PARTITION_READ(e2fs->part, sec_nr, (cyg_uint32 *)e2fs->gdesc_cache,
|
| 105 |
|
|
sizeof(e2fs->gdesc_cache)/SECTOR_SIZE))
|
| 106 |
|
|
return 0;
|
| 107 |
|
|
|
| 108 |
|
|
e2fs->gdesc_first = (group_nr / E2FS_GDESC_CACHE_SIZE) * E2FS_GDESC_CACHE_SIZE;
|
| 109 |
|
|
}
|
| 110 |
|
|
*gdesc = e2fs->gdesc_cache[group_nr - e2fs->gdesc_first];
|
| 111 |
|
|
|
| 112 |
|
|
return 1;
|
| 113 |
|
|
}
|
| 114 |
|
|
|
| 115 |
|
|
// Read the requested inode from disk. Return non-zero if successful
|
| 116 |
|
|
//
|
| 117 |
|
|
static int
|
| 118 |
|
|
e2fs_get_inode(e2fs_desc_t *e2fs, int ino, e2fs_inode_t *ip)
|
| 119 |
|
|
{
|
| 120 |
|
|
cyg_uint32 offset, sec_nr, buf[SECTOR_SIZE/sizeof(cyg_uint32)];
|
| 121 |
|
|
e2fs_group_t gdesc;
|
| 122 |
|
|
|
| 123 |
|
|
// get descriptor for group which this inode belongs to
|
| 124 |
|
|
if (!e2fs_get_gdesc(e2fs, (ino - 1) / e2fs->inodes_per_group, &gdesc))
|
| 125 |
|
|
return 0;
|
| 126 |
|
|
if (gdesc.inode_table == 0)
|
| 127 |
|
|
return 0;
|
| 128 |
|
|
|
| 129 |
|
|
// byte offset within group inode table
|
| 130 |
|
|
offset = ((ino - 1) % e2fs->inodes_per_group) * sizeof(struct e2fs_inode);
|
| 131 |
|
|
|
| 132 |
|
|
// figure out which sector holds the inode
|
| 133 |
|
|
sec_nr = E2FS_BLOCK_TO_SECTOR(e2fs, SWAB_LE32(gdesc.inode_table));
|
| 134 |
|
|
sec_nr += offset / SECTOR_SIZE;
|
| 135 |
|
|
|
| 136 |
|
|
// and the offset within that sector.
|
| 137 |
|
|
offset %= SECTOR_SIZE;
|
| 138 |
|
|
|
| 139 |
|
|
#if DEBUG_E2FS > 0x08
|
| 140 |
|
|
diag_printf("%s: ino[%d], sec_nr[%d] offset[%d]\n", __FUNCTION__,
|
| 141 |
|
|
ino, sec_nr, offset);
|
| 142 |
|
|
#endif
|
| 143 |
|
|
|
| 144 |
|
|
if (!PARTITION_READ(e2fs->part, sec_nr, buf, 1))
|
| 145 |
|
|
return 0;
|
| 146 |
|
|
|
| 147 |
|
|
*ip = *(e2fs_inode_t *)((char *)buf + offset);
|
| 148 |
|
|
|
| 149 |
|
|
#if DEBUG_E2FS > 0
|
| 150 |
|
|
diag_printf("%s: inode size[%d]\n", __FUNCTION__, SWAB_LE32(ip->size));
|
| 151 |
|
|
#endif
|
| 152 |
|
|
|
| 153 |
|
|
return 1;
|
| 154 |
|
|
}
|
| 155 |
|
|
|
| 156 |
|
|
// Mount an e2fs filesystem on the given partition.
|
| 157 |
|
|
// Return 0 if successful.
|
| 158 |
|
|
//
|
| 159 |
|
|
static int
|
| 160 |
|
|
e2fs_mount(partition_t *part, e2fs_desc_t *e2fs)
|
| 161 |
|
|
{
|
| 162 |
|
|
int sb_block = 1;
|
| 163 |
|
|
cyg_uint32 sb_buf[E2FS_MIN_BLOCK_SIZE/sizeof(cyg_uint32)];
|
| 164 |
|
|
struct e2fs_super_block *sb = (struct e2fs_super_block *)sb_buf;
|
| 165 |
|
|
|
| 166 |
|
|
e2fs->part = part;
|
| 167 |
|
|
|
| 168 |
|
|
if (!PARTITION_READ(part, sb_block*(E2FS_MIN_BLOCK_SIZE/SECTOR_SIZE),
|
| 169 |
|
|
(cyg_uint32 *)sb, E2FS_MIN_BLOCK_SIZE/SECTOR_SIZE))
|
| 170 |
|
|
return -1;
|
| 171 |
|
|
|
| 172 |
|
|
if (SWAB_LE16(sb->magic) != E2FS_SUPER_MAGIC) {
|
| 173 |
|
|
diag_printf("ext2_mount: bad magic 0x%x\n", SWAB_LE16(sb->magic));
|
| 174 |
|
|
return -1;
|
| 175 |
|
|
}
|
| 176 |
|
|
|
| 177 |
|
|
// save some stuff for easy access
|
| 178 |
|
|
e2fs->blocksize = E2FS_BLOCK_SIZE(sb);
|
| 179 |
|
|
e2fs->nr_ind_blocks = (e2fs)->blocksize / sizeof(cyg_uint32);
|
| 180 |
|
|
e2fs->nr_dind_blocks = e2fs->nr_ind_blocks * ((e2fs)->blocksize / sizeof(cyg_uint32));
|
| 181 |
|
|
e2fs->nr_tind_blocks = e2fs->nr_dind_blocks * ((e2fs)->blocksize / sizeof(cyg_uint32));
|
| 182 |
|
|
e2fs->blocks_per_group = SWAB_LE32(sb->blocks_per_group);
|
| 183 |
|
|
e2fs->ngroups = (SWAB_LE32(sb->blocks_count) + e2fs->blocks_per_group - 1) /
|
| 184 |
|
|
e2fs->blocks_per_group;
|
| 185 |
|
|
e2fs->inodes_per_group = SWAB_LE32(sb->inodes_per_group);
|
| 186 |
|
|
|
| 187 |
|
|
// Find the group descriptors which follow superblock
|
| 188 |
|
|
e2fs->gdesc_block = ((sb_block * E2FS_MIN_BLOCK_SIZE) / e2fs->blocksize) + 1;
|
| 189 |
|
|
e2fs->gdesc_first = 0; // cache group 0 initially
|
| 190 |
|
|
|
| 191 |
|
|
if (!PARTITION_READ(part, E2FS_BLOCK_TO_SECTOR(e2fs,e2fs->gdesc_block),
|
| 192 |
|
|
(cyg_uint32 *)e2fs->gdesc_cache, 1))
|
| 193 |
|
|
return -1;
|
| 194 |
|
|
|
| 195 |
|
|
#if DEBUG_E2FS > 1
|
| 196 |
|
|
diag_printf("E2FS superblock:\n");
|
| 197 |
|
|
diag_printf(" [%d] inodes\n", SWAB_LE32(sb->inodes_count));
|
| 198 |
|
|
diag_printf(" [%d] blocks\n", SWAB_LE32(sb->blocks_count));
|
| 199 |
|
|
diag_printf(" [%d] blocksize\n", e2fs->blocksize);
|
| 200 |
|
|
diag_printf(" [%d] blocks per group\n", e2fs->blocks_per_group);
|
| 201 |
|
|
diag_printf(" [%d] ngroups\n", e2fs->ngroups);
|
| 202 |
|
|
#endif
|
| 203 |
|
|
|
| 204 |
|
|
#if DEBUG_E2FS > 4
|
| 205 |
|
|
dump_sb(sb);
|
| 206 |
|
|
#endif
|
| 207 |
|
|
|
| 208 |
|
|
return 0;
|
| 209 |
|
|
}
|
| 210 |
|
|
|
| 211 |
|
|
// Convert a block index into inode data into a block_nr.
|
| 212 |
|
|
// If successful, store block number in pblknr and return non-zero.
|
| 213 |
|
|
//
|
| 214 |
|
|
// NB: This needs some block/sector caching to be speedier. But
|
| 215 |
|
|
// that takes memory and speed is not too bad now for files
|
| 216 |
|
|
// small enough to avoid double and triple indirection.
|
| 217 |
|
|
//
|
| 218 |
|
|
static int
|
| 219 |
|
|
e2fs_inode_block(e2fs_desc_t *e2fs, e2fs_inode_t *inode,
|
| 220 |
|
|
cyg_uint32 bindex, cyg_uint32 *pblknr)
|
| 221 |
|
|
{
|
| 222 |
|
|
if (bindex < E2FS_NR_DIR_BLOCKS) {
|
| 223 |
|
|
*pblknr = SWAB_LE32(inode->block[bindex]);
|
| 224 |
|
|
return 1;
|
| 225 |
|
|
}
|
| 226 |
|
|
bindex -= E2FS_NR_DIR_BLOCKS;
|
| 227 |
|
|
|
| 228 |
|
|
if (bindex < e2fs->nr_ind_blocks) {
|
| 229 |
|
|
// Indirect block
|
| 230 |
|
|
if (!__READ_BLOCK(SWAB_LE32(inode->block[E2FS_IND_BLOCK])))
|
| 231 |
|
|
return 0;
|
| 232 |
|
|
*pblknr = SWAB_LE32(blockbuf[bindex]);
|
| 233 |
|
|
return 1;
|
| 234 |
|
|
}
|
| 235 |
|
|
bindex -= e2fs->nr_ind_blocks;
|
| 236 |
|
|
|
| 237 |
|
|
if (bindex < e2fs->nr_dind_blocks) {
|
| 238 |
|
|
// Double indirect block
|
| 239 |
|
|
if (!__READ_BLOCK(SWAB_LE32(inode->block[E2FS_DIND_BLOCK])))
|
| 240 |
|
|
return 0;
|
| 241 |
|
|
if (!__READ_BLOCK(SWAB_LE32(blockbuf[bindex / e2fs->nr_ind_blocks])))
|
| 242 |
|
|
return 0;
|
| 243 |
|
|
*pblknr = SWAB_LE32(blockbuf[bindex % e2fs->nr_ind_blocks]);
|
| 244 |
|
|
return 1;
|
| 245 |
|
|
}
|
| 246 |
|
|
bindex -= e2fs->nr_dind_blocks;
|
| 247 |
|
|
|
| 248 |
|
|
// Triple indirect block
|
| 249 |
|
|
if (!__READ_BLOCK(SWAB_LE32(inode->block[E2FS_TIND_BLOCK])))
|
| 250 |
|
|
return 0;
|
| 251 |
|
|
if (!__READ_BLOCK(SWAB_LE32(blockbuf[bindex / e2fs->nr_dind_blocks])))
|
| 252 |
|
|
return 0;
|
| 253 |
|
|
bindex %= e2fs->nr_dind_blocks;
|
| 254 |
|
|
if (!__READ_BLOCK(SWAB_LE32(blockbuf[bindex / e2fs->nr_ind_blocks])))
|
| 255 |
|
|
return 0;
|
| 256 |
|
|
*pblknr = SWAB_LE32(blockbuf[bindex % e2fs->nr_ind_blocks]);
|
| 257 |
|
|
return 1;
|
| 258 |
|
|
}
|
| 259 |
|
|
|
| 260 |
|
|
|
| 261 |
|
|
// search a single directory block in memory looking for an
|
| 262 |
|
|
// entry with the given name. Return pointer to entry if
|
| 263 |
|
|
// found, NULL if not.
|
| 264 |
|
|
//
|
| 265 |
|
|
static e2fs_dir_entry_t *
|
| 266 |
|
|
search_dir_block(e2fs_desc_t *e2fs, cyg_uint32 *blkbuf,
|
| 267 |
|
|
const char *name, int namelen)
|
| 268 |
|
|
{
|
| 269 |
|
|
e2fs_dir_entry_t *dir;
|
| 270 |
|
|
cyg_uint16 reclen, len;
|
| 271 |
|
|
cyg_uint32 offset;
|
| 272 |
|
|
|
| 273 |
|
|
#if DEBUG_E2FS > 0
|
| 274 |
|
|
diag_dump_buf(blkbuf, e2fs->blocksize);
|
| 275 |
|
|
#endif
|
| 276 |
|
|
offset = 0;
|
| 277 |
|
|
while (offset < e2fs->blocksize) {
|
| 278 |
|
|
dir = (e2fs_dir_entry_t *)((char *)blkbuf + offset);
|
| 279 |
|
|
reclen = SWAB_LE16(dir->reclen);
|
| 280 |
|
|
offset += reclen;
|
| 281 |
|
|
len = dir->namelen;
|
| 282 |
|
|
|
| 283 |
|
|
// terminate on anything which doesn't make sense
|
| 284 |
|
|
if (reclen < 8 || (len + 8) > reclen || offset > (e2fs->blocksize + 1))
|
| 285 |
|
|
return NULL;
|
| 286 |
|
|
|
| 287 |
|
|
if (dir->inode && len == namelen && !strncmp(dir->name, name, len))
|
| 288 |
|
|
return dir;
|
| 289 |
|
|
}
|
| 290 |
|
|
return NULL;
|
| 291 |
|
|
}
|
| 292 |
|
|
|
| 293 |
|
|
|
| 294 |
|
|
// Look in the given directory for an entry with the given name.
|
| 295 |
|
|
// If found, return a pointer to that entry. Return NULL if not
|
| 296 |
|
|
// found.
|
| 297 |
|
|
//
|
| 298 |
|
|
static e2fs_dir_entry_t *
|
| 299 |
|
|
e2fs_dir_lookup(e2fs_desc_t *e2fs, cyg_uint32 dir_ino,
|
| 300 |
|
|
const char *name, int namelen)
|
| 301 |
|
|
{
|
| 302 |
|
|
e2fs_inode_t inode;
|
| 303 |
|
|
e2fs_dir_entry_t *dir;
|
| 304 |
|
|
cyg_uint32 nblocks, last_block_size, i, block_nr, nbytes;
|
| 305 |
|
|
|
| 306 |
|
|
#if DEBUG_E2FS > 0
|
| 307 |
|
|
diag_printf("%s: looking for %s [%d] in ino[%d]\n",
|
| 308 |
|
|
__FUNCTION__, name, namelen, dir_ino);
|
| 309 |
|
|
#endif
|
| 310 |
|
|
|
| 311 |
|
|
if (!e2fs_get_inode(e2fs, dir_ino, &inode)) {
|
| 312 |
|
|
#if DEBUG_E2FS > 0
|
| 313 |
|
|
diag_printf("%s: e2fs_get_inode [%d] failed\n", __FUNCTION__, dir_ino);
|
| 314 |
|
|
#endif
|
| 315 |
|
|
return NULL;
|
| 316 |
|
|
}
|
| 317 |
|
|
|
| 318 |
|
|
nbytes = SWAB_LE32(inode.size);
|
| 319 |
|
|
nblocks = (nbytes + e2fs->blocksize - 1) / e2fs->blocksize;
|
| 320 |
|
|
|
| 321 |
|
|
last_block_size = nbytes % e2fs->blocksize;
|
| 322 |
|
|
if (last_block_size == 0)
|
| 323 |
|
|
last_block_size = e2fs->blocksize;
|
| 324 |
|
|
|
| 325 |
|
|
for (i = 0; i < nblocks; i++) {
|
| 326 |
|
|
if (!e2fs_inode_block(e2fs, &inode, i, &block_nr))
|
| 327 |
|
|
return NULL;
|
| 328 |
|
|
|
| 329 |
|
|
if (block_nr) {
|
| 330 |
|
|
if (!__READ_BLOCK(block_nr))
|
| 331 |
|
|
return NULL;
|
| 332 |
|
|
} else
|
| 333 |
|
|
memset(blockbuf, 0, e2fs->blocksize);
|
| 334 |
|
|
|
| 335 |
|
|
dir = search_dir_block(e2fs, blockbuf, name, namelen);
|
| 336 |
|
|
|
| 337 |
|
|
if (dir != NULL)
|
| 338 |
|
|
return dir;
|
| 339 |
|
|
}
|
| 340 |
|
|
return NULL;
|
| 341 |
|
|
}
|
| 342 |
|
|
|
| 343 |
|
|
typedef struct ino_info {
|
| 344 |
|
|
cyg_uint32 ino;
|
| 345 |
|
|
cyg_uint32 parent_ino;
|
| 346 |
|
|
cyg_uint8 filetype;
|
| 347 |
|
|
} ino_info_t;
|
| 348 |
|
|
|
| 349 |
|
|
static int e2fs_inode_lookup(e2fs_desc_t *e2fs, cyg_uint32 dir_ino,
|
| 350 |
|
|
const char *pathname, ino_info_t *info);
|
| 351 |
|
|
|
| 352 |
|
|
// Starting from the given directory, find the inode number, filetype, and
|
| 353 |
|
|
// parent inode for the file pointed to by the given symbolic link inode.
|
| 354 |
|
|
// If successful, fills out ino_info_t and return true.
|
| 355 |
|
|
//
|
| 356 |
|
|
static int
|
| 357 |
|
|
e2fs_follow_symlink(e2fs_desc_t *e2fs, cyg_uint32 dir_ino, cyg_uint32 sym_ino, ino_info_t *info)
|
| 358 |
|
|
{
|
| 359 |
|
|
#define MAX_SYMLINK_NAME 255
|
| 360 |
|
|
char symlink[MAX_SYMLINK_NAME+1];
|
| 361 |
|
|
int pathlen;
|
| 362 |
|
|
cyg_uint32 block_nr;
|
| 363 |
|
|
e2fs_inode_t inode;
|
| 364 |
|
|
|
| 365 |
|
|
if (!e2fs_get_inode(e2fs, sym_ino, &inode)) {
|
| 366 |
|
|
#if DEBUG_E2FS > 0
|
| 367 |
|
|
diag_printf("%s: e2fs_get_inode [%d] failed\n", __FUNCTION__, sym_ino);
|
| 368 |
|
|
#endif
|
| 369 |
|
|
return 0;
|
| 370 |
|
|
}
|
| 371 |
|
|
|
| 372 |
|
|
pathlen = SWAB_LE32(inode.size);
|
| 373 |
|
|
if (pathlen > MAX_SYMLINK_NAME)
|
| 374 |
|
|
return 0;
|
| 375 |
|
|
|
| 376 |
|
|
if (inode.blocks) {
|
| 377 |
|
|
if (!e2fs_inode_block(e2fs, &inode, 0, &block_nr))
|
| 378 |
|
|
return 0;
|
| 379 |
|
|
if (block_nr) {
|
| 380 |
|
|
if (!PARTITION_READ(e2fs->part, E2FS_BLOCK_TO_SECTOR(e2fs, block_nr),
|
| 381 |
|
|
blockbuf, e2fs->blocksize/SECTOR_SIZE))
|
| 382 |
|
|
return 0;
|
| 383 |
|
|
memcpy(symlink, blockbuf, pathlen);
|
| 384 |
|
|
} else
|
| 385 |
|
|
return 0;
|
| 386 |
|
|
} else {
|
| 387 |
|
|
// small enough path to fit in inode struct
|
| 388 |
|
|
memcpy(symlink, (char *)&inode.block[0], pathlen);
|
| 389 |
|
|
}
|
| 390 |
|
|
symlink[pathlen] = 0;
|
| 391 |
|
|
|
| 392 |
|
|
return e2fs_inode_lookup(e2fs, dir_ino, symlink, info);
|
| 393 |
|
|
}
|
| 394 |
|
|
|
| 395 |
|
|
|
| 396 |
|
|
// Starting from the given directory, find the inode number, filetype, and
|
| 397 |
|
|
// parent inode for the given file pathname.
|
| 398 |
|
|
// If successful, fills out ino_info_t and return true.
|
| 399 |
|
|
//
|
| 400 |
|
|
static int
|
| 401 |
|
|
e2fs_inode_lookup(e2fs_desc_t *e2fs, cyg_uint32 dir_ino, const char *pathname, ino_info_t *info)
|
| 402 |
|
|
{
|
| 403 |
|
|
int len, pathlen;
|
| 404 |
|
|
const char *p;
|
| 405 |
|
|
e2fs_dir_entry_t *dir = NULL;
|
| 406 |
|
|
|
| 407 |
|
|
if (!pathname || (pathlen = strlen(pathname)) == 0)
|
| 408 |
|
|
return 0;
|
| 409 |
|
|
|
| 410 |
|
|
if (*pathname == '/') {
|
| 411 |
|
|
if (--pathlen == 0) {
|
| 412 |
|
|
info->ino = info->parent_ino = E2FS_ROOT_INO;
|
| 413 |
|
|
info->filetype = E2FS_FTYPE_DIR;
|
| 414 |
|
|
return 1;
|
| 415 |
|
|
}
|
| 416 |
|
|
++pathname;
|
| 417 |
|
|
dir_ino = E2FS_ROOT_INO;
|
| 418 |
|
|
}
|
| 419 |
|
|
|
| 420 |
|
|
while (pathlen) {
|
| 421 |
|
|
// find next delimiter in path.
|
| 422 |
|
|
for (p = pathname, len = 0; len < pathlen; len++, p++) {
|
| 423 |
|
|
// skip delimiter if found.
|
| 424 |
|
|
if (*p == '/') {
|
| 425 |
|
|
++p;
|
| 426 |
|
|
--pathlen;
|
| 427 |
|
|
break;
|
| 428 |
|
|
}
|
| 429 |
|
|
}
|
| 430 |
|
|
dir = e2fs_dir_lookup(e2fs, dir_ino, pathname, len);
|
| 431 |
|
|
if (dir == NULL)
|
| 432 |
|
|
return 0;
|
| 433 |
|
|
|
| 434 |
|
|
pathlen -= len;
|
| 435 |
|
|
pathname = p;
|
| 436 |
|
|
|
| 437 |
|
|
switch (dir->filetype) {
|
| 438 |
|
|
case E2FS_FTYPE_SYMLINK:
|
| 439 |
|
|
// follow the symbolic link (this will cause recursion)
|
| 440 |
|
|
if (!e2fs_follow_symlink(e2fs, dir_ino, SWAB_LE32(dir->inode), info))
|
| 441 |
|
|
return 0;
|
| 442 |
|
|
if (pathlen == 0)
|
| 443 |
|
|
return 1;
|
| 444 |
|
|
// must be a dir if we want to continue
|
| 445 |
|
|
if (info->filetype != E2FS_FTYPE_DIR)
|
| 446 |
|
|
return 0;
|
| 447 |
|
|
dir_ino = info->ino;
|
| 448 |
|
|
break;
|
| 449 |
|
|
|
| 450 |
|
|
case E2FS_FTYPE_DIR:
|
| 451 |
|
|
if (pathlen)
|
| 452 |
|
|
dir_ino = SWAB_LE32(dir->inode);
|
| 453 |
|
|
break;
|
| 454 |
|
|
|
| 455 |
|
|
case E2FS_FTYPE_REG_FILE:
|
| 456 |
|
|
if (pathlen)
|
| 457 |
|
|
return 0; // regular file embedded in middle of path
|
| 458 |
|
|
break;
|
| 459 |
|
|
|
| 460 |
|
|
case E2FS_FTYPE_UNKNOWN:
|
| 461 |
|
|
case E2FS_FTYPE_CHRDEV:
|
| 462 |
|
|
case E2FS_FTYPE_BLKDEV:
|
| 463 |
|
|
case E2FS_FTYPE_FIFO:
|
| 464 |
|
|
case E2FS_FTYPE_SOCK:
|
| 465 |
|
|
default:
|
| 466 |
|
|
return 0;
|
| 467 |
|
|
}
|
| 468 |
|
|
}
|
| 469 |
|
|
info->ino = SWAB_LE32(dir->inode);
|
| 470 |
|
|
info->parent_ino = dir_ino;
|
| 471 |
|
|
info->filetype = dir->filetype;
|
| 472 |
|
|
return 1;
|
| 473 |
|
|
}
|
| 474 |
|
|
|
| 475 |
|
|
struct read_info {
|
| 476 |
|
|
e2fs_desc_t e2fs_desc;
|
| 477 |
|
|
e2fs_inode_t inode;
|
| 478 |
|
|
cyg_uint32 fsize;
|
| 479 |
|
|
cyg_uint32 fpos;
|
| 480 |
|
|
};
|
| 481 |
|
|
|
| 482 |
|
|
static void *
|
| 483 |
|
|
e2fs_open(partition_t *p, const char *filepath)
|
| 484 |
|
|
{
|
| 485 |
|
|
static struct read_info rinfo;
|
| 486 |
|
|
ino_info_t ino_info;
|
| 487 |
|
|
|
| 488 |
|
|
// mount partition
|
| 489 |
|
|
if (e2fs_mount(p, &rinfo.e2fs_desc) != 0) {
|
| 490 |
|
|
diag_printf("mount failed.\n");
|
| 491 |
|
|
return NULL;
|
| 492 |
|
|
}
|
| 493 |
|
|
|
| 494 |
|
|
// find file inode
|
| 495 |
|
|
if (!e2fs_inode_lookup(&rinfo.e2fs_desc, E2FS_ROOT_INO, filepath, &ino_info)) {
|
| 496 |
|
|
diag_printf("%s: e2fs_inode_lookup failed\n", __FUNCTION__);
|
| 497 |
|
|
return NULL;
|
| 498 |
|
|
}
|
| 499 |
|
|
|
| 500 |
|
|
// read inode
|
| 501 |
|
|
if (!e2fs_get_inode(&rinfo.e2fs_desc, ino_info.ino, &rinfo.inode)) {
|
| 502 |
|
|
diag_printf("%s: e2fs_get_inode failed for ino[%d]\n", __FUNCTION__, ino_info.ino);
|
| 503 |
|
|
return NULL;
|
| 504 |
|
|
}
|
| 505 |
|
|
|
| 506 |
|
|
rinfo.fsize = SWAB_LE32(rinfo.inode.size);
|
| 507 |
|
|
rinfo.fpos = 0;
|
| 508 |
|
|
|
| 509 |
|
|
return &rinfo;
|
| 510 |
|
|
}
|
| 511 |
|
|
|
| 512 |
|
|
static int
|
| 513 |
|
|
e2fs_read(void *fp, char *buf, cyg_uint32 nbytes)
|
| 514 |
|
|
{
|
| 515 |
|
|
struct read_info *info = fp;
|
| 516 |
|
|
e2fs_desc_t *e2fs;
|
| 517 |
|
|
cyg_uint32 nread = 0, rem, block_nr, bindex, to_read;
|
| 518 |
|
|
|
| 519 |
|
|
if ((info->fpos + nbytes) > info->fsize)
|
| 520 |
|
|
nbytes = info->fsize - info->fpos;
|
| 521 |
|
|
|
| 522 |
|
|
e2fs = &info->e2fs_desc;
|
| 523 |
|
|
|
| 524 |
|
|
// see if we need to copy leftover data from last read call
|
| 525 |
|
|
rem = e2fs->blocksize - (info->fpos % e2fs->blocksize);
|
| 526 |
|
|
if (rem != e2fs->blocksize) {
|
| 527 |
|
|
char *p = (char *)blockbuf + e2fs->blocksize - rem;
|
| 528 |
|
|
|
| 529 |
|
|
if (rem > nbytes)
|
| 530 |
|
|
rem = nbytes;
|
| 531 |
|
|
|
| 532 |
|
|
memcpy(buf, p, rem);
|
| 533 |
|
|
|
| 534 |
|
|
nread += rem;
|
| 535 |
|
|
buf += rem;
|
| 536 |
|
|
info->fpos += rem;
|
| 537 |
|
|
}
|
| 538 |
|
|
|
| 539 |
|
|
// now loop through blocks if we're not done
|
| 540 |
|
|
bindex = info->fpos / e2fs->blocksize;
|
| 541 |
|
|
while (nread < nbytes) {
|
| 542 |
|
|
if (!e2fs_inode_block(e2fs, &info->inode, bindex, &block_nr))
|
| 543 |
|
|
return -1;
|
| 544 |
|
|
|
| 545 |
|
|
if (block_nr) {
|
| 546 |
|
|
if (!PARTITION_READ(e2fs->part, E2FS_BLOCK_TO_SECTOR(e2fs, block_nr),
|
| 547 |
|
|
blockbuf, e2fs->blocksize/SECTOR_SIZE))
|
| 548 |
|
|
return 0;
|
| 549 |
|
|
} else
|
| 550 |
|
|
memset(blockbuf, 0, e2fs->blocksize);
|
| 551 |
|
|
|
| 552 |
|
|
to_read = nbytes - nread;
|
| 553 |
|
|
if (to_read > e2fs->blocksize)
|
| 554 |
|
|
to_read = e2fs->blocksize;
|
| 555 |
|
|
|
| 556 |
|
|
memcpy(buf, blockbuf, to_read);
|
| 557 |
|
|
|
| 558 |
|
|
nread += to_read;
|
| 559 |
|
|
buf += to_read;
|
| 560 |
|
|
info->fpos += to_read;
|
| 561 |
|
|
++bindex;
|
| 562 |
|
|
}
|
| 563 |
|
|
|
| 564 |
|
|
return nread;
|
| 565 |
|
|
}
|
| 566 |
|
|
|
| 567 |
|
|
#if DEBUG_E2FS > 4
|
| 568 |
|
|
static void dump_sb(struct e2fs_super_block *s)
|
| 569 |
|
|
{
|
| 570 |
|
|
diag_printf("inode_count: %d\n", SWAB_LE32(s->inodes_count));
|
| 571 |
|
|
diag_printf("blocks_count: %d\n", SWAB_LE32(s->blocks_count));
|
| 572 |
|
|
diag_printf("r_blocks_count: %d\n", SWAB_LE32(s->r_blocks_count));
|
| 573 |
|
|
diag_printf("free_blocks_count: %d\n", SWAB_LE32(s->free_blocks_count));
|
| 574 |
|
|
diag_printf("free_inodes_count: %d\n", SWAB_LE32(s->free_inodes_count));
|
| 575 |
|
|
diag_printf("first_data_block: %d\n", SWAB_LE32(s->first_data_block));
|
| 576 |
|
|
diag_printf("log_block_size: %d\n", SWAB_LE32(s->log_block_size));
|
| 577 |
|
|
diag_printf("log_frag_size: %d\n", SWAB_LE32(s->log_frag_size));
|
| 578 |
|
|
diag_printf("blocks_per_group: %d\n", SWAB_LE32(s->blocks_per_group));
|
| 579 |
|
|
diag_printf("frags_per_group: %d\n", SWAB_LE32(s->frags_per_group));
|
| 580 |
|
|
diag_printf("inodes_per_group: %d\n", SWAB_LE32(s->inodes_per_group));
|
| 581 |
|
|
diag_printf("mnt_count: %d\n", SWAB_LE16(s->mnt_count));
|
| 582 |
|
|
diag_printf("max_mnt_count: %d\n", SWAB_LE16(s->max_mnt_count));
|
| 583 |
|
|
diag_printf("magic: %d\n", SWAB_LE16(s->magic));
|
| 584 |
|
|
diag_printf("state: %d\n", SWAB_LE16(s->state));
|
| 585 |
|
|
diag_printf("errors: %d\n", SWAB_LE16(s->errors));
|
| 586 |
|
|
diag_printf("minor_rev_level: %d\n", SWAB_LE16(s->minor_rev_level));
|
| 587 |
|
|
diag_printf("lastcheck: %d\n", SWAB_LE32(s->lastcheck));
|
| 588 |
|
|
diag_printf("checkinterval: %d\n", SWAB_LE32(s->checkinterval));
|
| 589 |
|
|
diag_printf("creator_os: %d\n", SWAB_LE32(s->creator_os));
|
| 590 |
|
|
diag_printf("rev_level: %d\n", SWAB_LE32(s->rev_level));
|
| 591 |
|
|
}
|
| 592 |
|
|
|
| 593 |
|
|
static void dump_inode(struct e2fs_inode *i)
|
| 594 |
|
|
{
|
| 595 |
|
|
int j, n;
|
| 596 |
|
|
|
| 597 |
|
|
diag_printf("mode: %o\n", SWAB_LE16(i->mode));
|
| 598 |
|
|
diag_printf("uid: %o\n", SWAB_LE16(i->uid));
|
| 599 |
|
|
diag_printf("size: %d\n", SWAB_LE32(i->size));
|
| 600 |
|
|
diag_printf("gid: %o\n", SWAB_LE16(i->gid));
|
| 601 |
|
|
diag_printf("links: %d\n", SWAB_LE16(i->links_count));
|
| 602 |
|
|
diag_printf("blocks: %d\n", SWAB_LE32(i->blocks));
|
| 603 |
|
|
|
| 604 |
|
|
n = i->blocks;
|
| 605 |
|
|
if (n > E2FS_N_BLOCKS)
|
| 606 |
|
|
n = E2FS_N_BLOCKS;
|
| 607 |
|
|
|
| 608 |
|
|
for (j = 0; j < n; j++)
|
| 609 |
|
|
diag_printf(" block: %d\n", SWAB_LE32(i->block[j]));
|
| 610 |
|
|
}
|
| 611 |
|
|
#endif
|
| 612 |
|
|
|
| 613 |
|
|
|