1 //==========================================================================
5 // RedBoot support for second extended filesystem
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>
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.
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
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.
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.
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.
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####
45 // Contributors: msalter
50 // This code is part of RedBoot (tm).
52 //####DESCRIPTIONEND####
54 //==========================================================================
63 static void dump_sb(struct e2fs_super_block *s);
64 static void dump_inode(struct e2fs_inode *i);
67 static void *e2fs_open(partition_t *p, const char *path);
68 static int e2fs_read(void *fp, char *buf, cyg_uint32 nbytes);
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.
74 fs_funs_t redboot_e2fs_funs = {
79 // A single block buffer to be shared carefully.
80 static cyg_uint32 blockbuf[E2FS_MAX_BLOCK_SIZE/sizeof(cyg_uint32)];
82 #define __READ_BLOCK(n) \
83 PARTITION_READ(e2fs->part, E2FS_BLOCK_TO_SECTOR(e2fs, (n)), \
84 blockbuf, e2fs->blocksize/SECTOR_SIZE)
86 // Get a group descriptor. Returns non-zero for success.
89 e2fs_get_gdesc(e2fs_desc_t *e2fs, cyg_uint32 group_nr, e2fs_group_t *gdesc)
93 if (group_nr < e2fs->gdesc_first ||
94 group_nr >= (e2fs->gdesc_first + E2FS_GDESC_CACHE_SIZE)) {
97 sec_nr = E2FS_BLOCK_TO_SECTOR(e2fs, e2fs->gdesc_block);
98 sec_nr += (group_nr / E2FS_GDESC_PER_SECTOR);
101 diag_printf("%s: group[%d] cache miss, sec_nr[%d]\n",
102 __FUNCTION__, group_nr, sec_nr);
104 if (!PARTITION_READ(e2fs->part, sec_nr, (cyg_uint32 *)e2fs->gdesc_cache,
105 sizeof(e2fs->gdesc_cache)/SECTOR_SIZE))
108 e2fs->gdesc_first = (group_nr / E2FS_GDESC_CACHE_SIZE) * E2FS_GDESC_CACHE_SIZE;
110 *gdesc = e2fs->gdesc_cache[group_nr - e2fs->gdesc_first];
115 // Read the requested inode from disk. Return non-zero if successful
118 e2fs_get_inode(e2fs_desc_t *e2fs, int ino, e2fs_inode_t *ip)
120 cyg_uint32 offset, sec_nr, buf[SECTOR_SIZE/sizeof(cyg_uint32)];
123 // get descriptor for group which this inode belongs to
124 if (!e2fs_get_gdesc(e2fs, (ino - 1) / e2fs->inodes_per_group, &gdesc))
126 if (gdesc.inode_table == 0)
129 // byte offset within group inode table
130 offset = ((ino - 1) % e2fs->inodes_per_group) * sizeof(struct e2fs_inode);
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;
136 // and the offset within that sector.
137 offset %= SECTOR_SIZE;
139 #if DEBUG_E2FS > 0x08
140 diag_printf("%s: ino[%d], sec_nr[%d] offset[%d]\n", __FUNCTION__,
141 ino, sec_nr, offset);
144 if (!PARTITION_READ(e2fs->part, sec_nr, buf, 1))
147 *ip = *(e2fs_inode_t *)((char *)buf + offset);
150 diag_printf("%s: inode size[%d]\n", __FUNCTION__, SWAB_LE32(ip->size));
156 // Mount an e2fs filesystem on the given partition.
157 // Return 0 if successful.
160 e2fs_mount(partition_t *part, e2fs_desc_t *e2fs)
163 cyg_uint32 sb_buf[E2FS_MIN_BLOCK_SIZE/sizeof(cyg_uint32)];
164 struct e2fs_super_block *sb = (struct e2fs_super_block *)((void *)sb_buf);
168 if (!PARTITION_READ(part, sb_block*(E2FS_MIN_BLOCK_SIZE/SECTOR_SIZE),
169 sb_buf, E2FS_MIN_BLOCK_SIZE/SECTOR_SIZE))
172 if (SWAB_LE16(sb->magic) != E2FS_SUPER_MAGIC) {
173 diag_printf("ext2_mount: bad magic 0x%x\n", SWAB_LE16(sb->magic));
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);
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
191 if (!PARTITION_READ(part, E2FS_BLOCK_TO_SECTOR(e2fs,e2fs->gdesc_block),
192 (cyg_uint32 *)e2fs->gdesc_cache, 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);
211 // Convert a block index into inode data into a block_nr.
212 // If successful, store block number in pblknr and return non-zero.
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.
219 e2fs_inode_block(e2fs_desc_t *e2fs, e2fs_inode_t *inode,
220 cyg_uint32 bindex, cyg_uint32 *pblknr)
222 if (bindex < E2FS_NR_DIR_BLOCKS) {
223 *pblknr = SWAB_LE32(inode->block[bindex]);
226 bindex -= E2FS_NR_DIR_BLOCKS;
228 if (bindex < e2fs->nr_ind_blocks) {
230 if (!__READ_BLOCK(SWAB_LE32(inode->block[E2FS_IND_BLOCK])))
232 *pblknr = SWAB_LE32(blockbuf[bindex]);
235 bindex -= e2fs->nr_ind_blocks;
237 if (bindex < e2fs->nr_dind_blocks) {
238 // Double indirect block
239 if (!__READ_BLOCK(SWAB_LE32(inode->block[E2FS_DIND_BLOCK])))
241 if (!__READ_BLOCK(SWAB_LE32(blockbuf[bindex / e2fs->nr_ind_blocks])))
243 *pblknr = SWAB_LE32(blockbuf[bindex % e2fs->nr_ind_blocks]);
246 bindex -= e2fs->nr_dind_blocks;
248 // Triple indirect block
249 if (!__READ_BLOCK(SWAB_LE32(inode->block[E2FS_TIND_BLOCK])))
251 if (!__READ_BLOCK(SWAB_LE32(blockbuf[bindex / e2fs->nr_dind_blocks])))
253 bindex %= e2fs->nr_dind_blocks;
254 if (!__READ_BLOCK(SWAB_LE32(blockbuf[bindex / e2fs->nr_ind_blocks])))
256 *pblknr = SWAB_LE32(blockbuf[bindex % e2fs->nr_ind_blocks]);
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.
265 static e2fs_dir_entry_t *
266 search_dir_block(e2fs_desc_t *e2fs, cyg_uint32 *blkbuf,
267 const char *name, int namelen)
269 e2fs_dir_entry_t *dir;
270 cyg_uint16 reclen, len;
274 diag_dump_buf(blkbuf, e2fs->blocksize);
277 while (offset < e2fs->blocksize) {
278 dir = (e2fs_dir_entry_t *)((char *)blkbuf + offset);
279 reclen = SWAB_LE16(dir->reclen);
283 // terminate on anything which doesn't make sense
284 if (reclen < 8 || (len + 8) > reclen || offset > (e2fs->blocksize + 1))
287 if (dir->inode && len == namelen && !strncmp(dir->name, name, len))
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
298 static e2fs_dir_entry_t *
299 e2fs_dir_lookup(e2fs_desc_t *e2fs, cyg_uint32 dir_ino,
300 const char *name, int namelen)
303 e2fs_dir_entry_t *dir;
304 cyg_uint32 nblocks, last_block_size, i, block_nr, nbytes;
307 diag_printf("%s: looking for %s [%d] in ino[%d]\n",
308 __FUNCTION__, name, namelen, dir_ino);
311 if (!e2fs_get_inode(e2fs, dir_ino, &inode)) {
313 diag_printf("%s: e2fs_get_inode [%d] failed\n", __FUNCTION__, dir_ino);
318 nbytes = SWAB_LE32(inode.size);
319 nblocks = (nbytes + e2fs->blocksize - 1) / e2fs->blocksize;
321 last_block_size = nbytes % e2fs->blocksize;
322 if (last_block_size == 0)
323 last_block_size = e2fs->blocksize;
325 for (i = 0; i < nblocks; i++) {
326 if (!e2fs_inode_block(e2fs, &inode, i, &block_nr))
330 if (!__READ_BLOCK(block_nr))
333 memset(blockbuf, 0, e2fs->blocksize);
335 dir = search_dir_block(e2fs, blockbuf, name, namelen);
343 typedef struct ino_info {
345 cyg_uint32 parent_ino;
349 static int e2fs_inode_lookup(e2fs_desc_t *e2fs, cyg_uint32 dir_ino,
350 const char *pathname, ino_info_t *info);
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.
357 e2fs_follow_symlink(e2fs_desc_t *e2fs, cyg_uint32 dir_ino, cyg_uint32 sym_ino, ino_info_t *info)
359 #define MAX_SYMLINK_NAME 255
360 char symlink[MAX_SYMLINK_NAME+1];
365 if (!e2fs_get_inode(e2fs, sym_ino, &inode)) {
367 diag_printf("%s: e2fs_get_inode [%d] failed\n", __FUNCTION__, sym_ino);
372 pathlen = SWAB_LE32(inode.size);
373 if (pathlen > MAX_SYMLINK_NAME)
377 if (!e2fs_inode_block(e2fs, &inode, 0, &block_nr))
380 if (!PARTITION_READ(e2fs->part, E2FS_BLOCK_TO_SECTOR(e2fs, block_nr),
381 blockbuf, e2fs->blocksize/SECTOR_SIZE))
383 memcpy(symlink, blockbuf, pathlen);
387 // small enough path to fit in inode struct
388 memcpy(symlink, (char *)&inode.block[0], pathlen);
390 symlink[pathlen] = 0;
392 return e2fs_inode_lookup(e2fs, dir_ino, symlink, info);
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.
401 e2fs_inode_lookup(e2fs_desc_t *e2fs, cyg_uint32 dir_ino, const char *pathname, ino_info_t *info)
405 e2fs_dir_entry_t *dir = NULL;
407 if (!pathname || (pathlen = strlen(pathname)) == 0)
410 if (*pathname == '/') {
411 if (--pathlen == 0) {
412 info->ino = info->parent_ino = E2FS_ROOT_INO;
413 info->filetype = E2FS_FTYPE_DIR;
417 dir_ino = E2FS_ROOT_INO;
421 // find next delimiter in path.
422 for (p = pathname, len = 0; len < pathlen; len++, p++) {
423 // skip delimiter if found.
430 dir = e2fs_dir_lookup(e2fs, dir_ino, pathname, len);
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))
444 // must be a dir if we want to continue
445 if (info->filetype != E2FS_FTYPE_DIR)
452 dir_ino = SWAB_LE32(dir->inode);
455 case E2FS_FTYPE_REG_FILE:
457 return 0; // regular file embedded in middle of path
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:
469 info->ino = SWAB_LE32(dir->inode);
470 info->parent_ino = dir_ino;
471 info->filetype = dir->filetype;
476 e2fs_desc_t e2fs_desc;
483 e2fs_open(partition_t *p, const char *filepath)
485 static struct read_info rinfo;
489 if (e2fs_mount(p, &rinfo.e2fs_desc) != 0) {
490 diag_printf("mount failed.\n");
495 if (!e2fs_inode_lookup(&rinfo.e2fs_desc, E2FS_ROOT_INO, filepath, &ino_info)) {
496 diag_printf("%s: e2fs_inode_lookup failed\n", __FUNCTION__);
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);
506 rinfo.fsize = SWAB_LE32(rinfo.inode.size);
513 e2fs_read(void *fp, char *buf, cyg_uint32 nbytes)
515 struct read_info *info = fp;
517 cyg_uint32 nread = 0, rem, block_nr, bindex, to_read;
519 if ((info->fpos + nbytes) > info->fsize)
520 nbytes = info->fsize - info->fpos;
522 e2fs = &info->e2fs_desc;
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;
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))
546 if (!PARTITION_READ(e2fs->part, E2FS_BLOCK_TO_SECTOR(e2fs, block_nr),
547 blockbuf, e2fs->blocksize/SECTOR_SIZE))
550 memset(blockbuf, 0, e2fs->blocksize);
552 to_read = nbytes - nread;
553 if (to_read > e2fs->blocksize)
554 to_read = e2fs->blocksize;
556 memcpy(buf, blockbuf, to_read);
560 info->fpos += to_read;
568 static void dump_sb(struct e2fs_super_block *s)
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));
593 static void dump_inode(struct e2fs_inode *i)
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));
605 if (n > E2FS_N_BLOCKS)
608 for (j = 0; j < n; j++)
609 diag_printf(" block: %d\n", SWAB_LE32(i->block[j]));