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