blob: d54f60b30b9397f6955b9c26df98eeb1f2cd6675 [file] [log] [blame]
stroese2b918712004-12-16 17:26:24 +00001/*
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>
stroese2b918712004-12-16 17:26:24 +000027#include <ext2fs.h>
28#include <malloc.h>
29#include <asm/byteorder.h>
30
wdenkefe2a4d2004-12-16 21:44:03 +000031extern int ext2fs_devread (int sector, int byte_offset, int byte_len,
32 char *buf);
stroese2b918712004-12-16 17:26:24 +000033
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. */
wdenkefe2a4d2004-12-16 21:44:03 +000068struct ext2_sblock {
stroese2b918712004-12-16 17:26:24 +000069 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. */
wdenkefe2a4d2004-12-16 21:44:03 +0000107struct ext2_block_group {
stroese2b918712004-12-16 17:26:24 +0000108 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, Bernhard56fdaad2009-06-10 14:00:37 +0200113 uint16_t used_dir_cnt;
stroese2b918712004-12-16 17:26:24 +0000114 uint32_t reserved[3];
115};
116
117/* The ext2 inode. */
wdenkefe2a4d2004-12-16 21:44:03 +0000118struct ext2_inode {
stroese2b918712004-12-16 17:26:24 +0000119 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;
wdenkefe2a4d2004-12-16 21:44:03 +0000128 uint32_t blockcnt; /* Blocks of 512 bytes!! */
stroese2b918712004-12-16 17:26:24 +0000129 uint32_t flags;
130 uint32_t osd1;
wdenkefe2a4d2004-12-16 21:44:03 +0000131 union {
132 struct datablocks {
stroese2b918712004-12-16 17:26:24 +0000133 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];
wdenkefe2a4d2004-12-16 21:44:03 +0000139 } b;
stroese2b918712004-12-16 17:26:24 +0000140 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. */
wdenkefe2a4d2004-12-16 21:44:03 +0000148struct ext2_dirent {
stroese2b918712004-12-16 17:26:24 +0000149 uint32_t inode;
150 uint16_t direntlen;
wdenkefe2a4d2004-12-16 21:44:03 +0000151 uint8_t namelen;
152 uint8_t filetype;
stroese2b918712004-12-16 17:26:24 +0000153};
154
wdenkefe2a4d2004-12-16 21:44:03 +0000155struct ext2fs_node {
stroese2b918712004-12-16 17:26:24 +0000156 struct ext2_data *data;
157 struct ext2_inode inode;
wdenkefe2a4d2004-12-16 21:44:03 +0000158 int ino;
159 int inode_read;
stroese2b918712004-12-16 17:26:24 +0000160};
161
162/* Information about a "mounted" ext2 filesystem. */
wdenkefe2a4d2004-12-16 21:44:03 +0000163struct ext2_data {
stroese2b918712004-12-16 17:26:24 +0000164 struct ext2_sblock sblock;
165 struct ext2_inode *inode;
166 struct ext2fs_node diropen;
167};
168
169
170typedef struct ext2fs_node *ext2fs_node_t;
171
wdenkefe2a4d2004-12-16 21:44:03 +0000172struct ext2_data *ext2fs_root = NULL;
173ext2fs_node_t ext2fs_file = NULL;
174int symlinknest = 0;
175uint32_t *indir1_block = NULL;
176int indir1_size = 0;
177int indir1_blkno = -1;
178uint32_t *indir2_block = NULL;
179int indir2_size = 0;
180int indir2_blkno = -1;
stroese2b918712004-12-16 17:26:24 +0000181
182
183static int ext2fs_blockgroup
wdenkefe2a4d2004-12-16 21:44:03 +0000184 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) {
Weirich, Bernhard56fdaad2009-06-10 14:00:37 +0200185 unsigned int blkno;
186 unsigned int blkoff;
187 unsigned int desc_per_blk;
188
189 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
190
191 blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 +
192 group / desc_per_blk;
193 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
stroese2b918712004-12-16 17:26:24 +0000194#ifdef DEBUG
Weirich, Bernhard56fdaad2009-06-10 14:00:37 +0200195 printf ("ext2fs read %d group descriptor (blkno %d blkoff %d)\n",
196 group, blkno, blkoff);
stroese2b918712004-12-16 17:26:24 +0000197#endif
Weirich, Bernhard56fdaad2009-06-10 14:00:37 +0200198 return (ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE(data),
199 blkoff, sizeof(struct ext2_block_group), (char *)blkgrp));
200
stroese2b918712004-12-16 17:26:24 +0000201}
202
203
204static int ext2fs_read_inode
wdenkefe2a4d2004-12-16 21:44:03 +0000205 (struct ext2_data *data, int ino, struct ext2_inode *inode) {
206 struct ext2_block_group blkgrp;
207 struct ext2_sblock *sblock = &data->sblock;
208 int inodes_per_block;
209 int status;
stroese2b918712004-12-16 17:26:24 +0000210
wdenkefe2a4d2004-12-16 21:44:03 +0000211 unsigned int blkno;
212 unsigned int blkoff;
stroese2b918712004-12-16 17:26:24 +0000213
stroese2b918712004-12-16 17:26:24 +0000214#ifdef DEBUG
wdenkefe2a4d2004-12-16 21:44:03 +0000215 printf ("ext2fs read inode %d\n", ino);
stroese2b918712004-12-16 17:26:24 +0000216#endif
Weirich, Bernhard56fdaad2009-06-10 14:00:37 +0200217 /* It is easier to calculate if the first inode is 0. */
218 ino--;
219 status = ext2fs_blockgroup (data, ino / __le32_to_cpu
220 (sblock->inodes_per_group), &blkgrp);
wdenkefe2a4d2004-12-16 21:44:03 +0000221 if (status == 0) {
222 return (0);
stroese2b918712004-12-16 17:26:24 +0000223 }
Weirich, Bernhard56fdaad2009-06-10 14:00:37 +0200224
225 inodes_per_block = EXT2_BLOCK_SIZE(data) / __le16_to_cpu(sblock->inode_size);
226
227#ifdef DEBUG
228 printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
229#endif
230
231 blkno = __le32_to_cpu (blkgrp.inode_table_id) +
232 (ino % __le32_to_cpu (sblock->inodes_per_group))
233 / inodes_per_block;
234 blkoff = (ino % inodes_per_block) * __le16_to_cpu (sblock->inode_size);
stroese2b918712004-12-16 17:26:24 +0000235#ifdef DEBUG
wdenkefe2a4d2004-12-16 21:44:03 +0000236 printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
stroese2b918712004-12-16 17:26:24 +0000237#endif
238 /* Read the inode. */
Weirich, Bernhard56fdaad2009-06-10 14:00:37 +0200239 status = ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE (data), blkoff,
wdenkefe2a4d2004-12-16 21:44:03 +0000240 sizeof (struct ext2_inode), (char *) inode);
241 if (status == 0) {
242 return (0);
stroese2b918712004-12-16 17:26:24 +0000243 }
Weirich, Bernhard56fdaad2009-06-10 14:00:37 +0200244
wdenkefe2a4d2004-12-16 21:44:03 +0000245 return (1);
stroese2b918712004-12-16 17:26:24 +0000246}
247
248
wdenkefe2a4d2004-12-16 21:44:03 +0000249void ext2fs_free_node (ext2fs_node_t node, ext2fs_node_t currroot) {
250 if ((node != &ext2fs_root->diropen) && (node != currroot)) {
stroese2b918712004-12-16 17:26:24 +0000251 free (node);
252 }
253}
254
255
wdenkefe2a4d2004-12-16 21:44:03 +0000256static int ext2fs_read_block (ext2fs_node_t node, int fileblock) {
257 struct ext2_data *data = node->data;
258 struct ext2_inode *inode = &node->inode;
259 int blknr;
260 int blksz = EXT2_BLOCK_SIZE (data);
261 int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
262 int status;
stroese2b918712004-12-16 17:26:24 +0000263
264 /* Direct blocks. */
wdenkefe2a4d2004-12-16 21:44:03 +0000265 if (fileblock < INDIRECT_BLOCKS) {
stroese2b918712004-12-16 17:26:24 +0000266 blknr = __le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]);
wdenkefe2a4d2004-12-16 21:44:03 +0000267 }
stroese2b918712004-12-16 17:26:24 +0000268 /* Indirect. */
wdenkefe2a4d2004-12-16 21:44:03 +0000269 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
270 if (indir1_block == NULL) {
271 indir1_block = (uint32_t *) malloc (blksz);
272 if (indir1_block == NULL) {
273 printf ("** ext2fs read block (indir 1) malloc failed. **\n");
274 return (-1);
stroese2b918712004-12-16 17:26:24 +0000275 }
wdenkefe2a4d2004-12-16 21:44:03 +0000276 indir1_size = blksz;
stroese2b918712004-12-16 17:26:24 +0000277 indir1_blkno = -1;
278 }
wdenkefe2a4d2004-12-16 21:44:03 +0000279 if (blksz != indir1_size) {
280 free (indir1_block);
stroese2b918712004-12-16 17:26:24 +0000281 indir1_block = NULL;
wdenkefe2a4d2004-12-16 21:44:03 +0000282 indir1_size = 0;
stroese2b918712004-12-16 17:26:24 +0000283 indir1_blkno = -1;
wdenkefe2a4d2004-12-16 21:44:03 +0000284 indir1_block = (uint32_t *) malloc (blksz);
285 if (indir1_block == NULL) {
286 printf ("** ext2fs read block (indir 1) malloc failed. **\n");
287 return (-1);
stroese2b918712004-12-16 17:26:24 +0000288 }
wdenkefe2a4d2004-12-16 21:44:03 +0000289 indir1_size = blksz;
stroese2b918712004-12-16 17:26:24 +0000290 }
wdenkefe2a4d2004-12-16 21:44:03 +0000291 if ((__le32_to_cpu (inode->b.blocks.indir_block) <<
292 log2_blksz) != indir1_blkno) {
293 status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz,
294 0, blksz,
295 (char *) indir1_block);
296 if (status == 0) {
297 printf ("** ext2fs read block (indir 1) failed. **\n");
298 return (0);
stroese2b918712004-12-16 17:26:24 +0000299 }
wdenkefe2a4d2004-12-16 21:44:03 +0000300 indir1_blkno =
301 __le32_to_cpu (inode->b.blocks.
302 indir_block) << log2_blksz;
stroese2b918712004-12-16 17:26:24 +0000303 }
wdenkefe2a4d2004-12-16 21:44:03 +0000304 blknr = __le32_to_cpu (indir1_block
305 [fileblock - INDIRECT_BLOCKS]);
stroese2b918712004-12-16 17:26:24 +0000306 }
307 /* Double indirect. */
wdenkefe2a4d2004-12-16 21:44:03 +0000308 else if (fileblock <
309 (INDIRECT_BLOCKS + (blksz / 4 * (blksz / 4 + 1)))) {
stroese2b918712004-12-16 17:26:24 +0000310 unsigned int perblock = blksz / 4;
311 unsigned int rblock = fileblock - (INDIRECT_BLOCKS
312 + blksz / 4);
313
wdenkefe2a4d2004-12-16 21:44:03 +0000314 if (indir1_block == NULL) {
315 indir1_block = (uint32_t *) malloc (blksz);
316 if (indir1_block == NULL) {
317 printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
318 return (-1);
stroese2b918712004-12-16 17:26:24 +0000319 }
wdenkefe2a4d2004-12-16 21:44:03 +0000320 indir1_size = blksz;
stroese2b918712004-12-16 17:26:24 +0000321 indir1_blkno = -1;
322 }
wdenkefe2a4d2004-12-16 21:44:03 +0000323 if (blksz != indir1_size) {
324 free (indir1_block);
stroese2b918712004-12-16 17:26:24 +0000325 indir1_block = NULL;
wdenkefe2a4d2004-12-16 21:44:03 +0000326 indir1_size = 0;
stroese2b918712004-12-16 17:26:24 +0000327 indir1_blkno = -1;
wdenkefe2a4d2004-12-16 21:44:03 +0000328 indir1_block = (uint32_t *) malloc (blksz);
329 if (indir1_block == NULL) {
330 printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
331 return (-1);
stroese2b918712004-12-16 17:26:24 +0000332 }
wdenkefe2a4d2004-12-16 21:44:03 +0000333 indir1_size = blksz;
stroese2b918712004-12-16 17:26:24 +0000334 }
wdenkefe2a4d2004-12-16 21:44:03 +0000335 if ((__le32_to_cpu (inode->b.blocks.double_indir_block) <<
336 log2_blksz) != indir1_blkno) {
337 status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz,
338 0, blksz,
339 (char *) indir1_block);
340 if (status == 0) {
341 printf ("** ext2fs read block (indir 2 1) failed. **\n");
342 return (-1);
stroese2b918712004-12-16 17:26:24 +0000343 }
wdenkefe2a4d2004-12-16 21:44:03 +0000344 indir1_blkno =
345 __le32_to_cpu (inode->b.blocks.double_indir_block) << log2_blksz;
stroese2b918712004-12-16 17:26:24 +0000346 }
347
wdenkefe2a4d2004-12-16 21:44:03 +0000348 if (indir2_block == NULL) {
349 indir2_block = (uint32_t *) malloc (blksz);
350 if (indir2_block == NULL) {
351 printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
352 return (-1);
stroese2b918712004-12-16 17:26:24 +0000353 }
wdenkefe2a4d2004-12-16 21:44:03 +0000354 indir2_size = blksz;
stroese2b918712004-12-16 17:26:24 +0000355 indir2_blkno = -1;
356 }
wdenkefe2a4d2004-12-16 21:44:03 +0000357 if (blksz != indir2_size) {
358 free (indir2_block);
stroese2b918712004-12-16 17:26:24 +0000359 indir2_block = NULL;
wdenkefe2a4d2004-12-16 21:44:03 +0000360 indir2_size = 0;
stroese2b918712004-12-16 17:26:24 +0000361 indir2_blkno = -1;
wdenkefe2a4d2004-12-16 21:44:03 +0000362 indir2_block = (uint32_t *) malloc (blksz);
363 if (indir2_block == NULL) {
364 printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
365 return (-1);
stroese2b918712004-12-16 17:26:24 +0000366 }
wdenkefe2a4d2004-12-16 21:44:03 +0000367 indir2_size = blksz;
stroese2b918712004-12-16 17:26:24 +0000368 }
wdenkefe2a4d2004-12-16 21:44:03 +0000369 if ((__le32_to_cpu (indir1_block[rblock / perblock]) <<
370 log2_blksz) != indir1_blkno) {
371 status = ext2fs_devread (__le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz,
372 0, blksz,
373 (char *) indir2_block);
374 if (status == 0) {
375 printf ("** ext2fs read block (indir 2 2) failed. **\n");
376 return (-1);
stroese2b918712004-12-16 17:26:24 +0000377 }
wdenkefe2a4d2004-12-16 21:44:03 +0000378 indir2_blkno =
379 __le32_to_cpu (indir1_block[rblock / perblock]) << log2_blksz;
stroese2b918712004-12-16 17:26:24 +0000380 }
wdenkefe2a4d2004-12-16 21:44:03 +0000381 blknr = __le32_to_cpu (indir2_block[rblock % perblock]);
stroese2b918712004-12-16 17:26:24 +0000382 }
383 /* Tripple indirect. */
wdenkefe2a4d2004-12-16 21:44:03 +0000384 else {
385 printf ("** ext2fs doesn't support tripple indirect blocks. **\n");
386 return (-1);
387 }
stroese2b918712004-12-16 17:26:24 +0000388#ifdef DEBUG
wdenkefe2a4d2004-12-16 21:44:03 +0000389 printf ("ext2fs_read_block %08x\n", blknr);
stroese2b918712004-12-16 17:26:24 +0000390#endif
wdenkefe2a4d2004-12-16 21:44:03 +0000391 return (blknr);
stroese2b918712004-12-16 17:26:24 +0000392}
393
394
395int ext2fs_read_file
wdenkefe2a4d2004-12-16 21:44:03 +0000396 (ext2fs_node_t node, int pos, unsigned int len, char *buf) {
397 int i;
398 int blockcnt;
399 int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
400 int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
Stefan Roesea7b9fb92006-01-18 20:05:34 +0100401 unsigned int filesize = __le32_to_cpu(node->inode.size);
stroese2b918712004-12-16 17:26:24 +0000402
403 /* Adjust len so it we can't read past the end of the file. */
wdenkefe2a4d2004-12-16 21:44:03 +0000404 if (len > filesize) {
stroese2b918712004-12-16 17:26:24 +0000405 len = filesize;
406 }
wdenkefe2a4d2004-12-16 21:44:03 +0000407 blockcnt = ((len + pos) + blocksize - 1) / blocksize;
stroese2b918712004-12-16 17:26:24 +0000408
wdenkefe2a4d2004-12-16 21:44:03 +0000409 for (i = pos / blocksize; i < blockcnt; i++) {
stroese2b918712004-12-16 17:26:24 +0000410 int blknr;
411 int blockoff = pos % blocksize;
412 int blockend = blocksize;
413
414 int skipfirst = 0;
415
wdenkefe2a4d2004-12-16 21:44:03 +0000416 blknr = ext2fs_read_block (node, i);
417 if (blknr < 0) {
418 return (-1);
stroese2b918712004-12-16 17:26:24 +0000419 }
420 blknr = blknr << log2blocksize;
421
422 /* Last block. */
wdenkefe2a4d2004-12-16 21:44:03 +0000423 if (i == blockcnt - 1) {
stroese2b918712004-12-16 17:26:24 +0000424 blockend = (len + pos) % blocksize;
425
426 /* The last portion is exactly blocksize. */
wdenkefe2a4d2004-12-16 21:44:03 +0000427 if (!blockend) {
stroese2b918712004-12-16 17:26:24 +0000428 blockend = blocksize;
429 }
430 }
431
432 /* First block. */
wdenkefe2a4d2004-12-16 21:44:03 +0000433 if (i == pos / blocksize) {
stroese2b918712004-12-16 17:26:24 +0000434 skipfirst = blockoff;
435 blockend -= skipfirst;
436 }
437
438 /* If the block number is 0 this block is not stored on disk but
439 is zero filled instead. */
wdenkefe2a4d2004-12-16 21:44:03 +0000440 if (blknr) {
stroese2b918712004-12-16 17:26:24 +0000441 int status;
442
443 status = ext2fs_devread (blknr, skipfirst, blockend, buf);
wdenkefe2a4d2004-12-16 21:44:03 +0000444 if (status == 0) {
445 return (-1);
stroese2b918712004-12-16 17:26:24 +0000446 }
wdenkefe2a4d2004-12-16 21:44:03 +0000447 } else {
Wolfgang Denk0ddb8962008-01-09 10:16:33 +0100448 memset (buf, 0, blocksize - skipfirst);
stroese2b918712004-12-16 17:26:24 +0000449 }
450 buf += blocksize - skipfirst;
wdenkefe2a4d2004-12-16 21:44:03 +0000451 }
452 return (len);
stroese2b918712004-12-16 17:26:24 +0000453}
454
455
wdenkefe2a4d2004-12-16 21:44:03 +0000456static int ext2fs_iterate_dir (ext2fs_node_t dir, char *name, ext2fs_node_t * fnode, int *ftype)
stroese2b918712004-12-16 17:26:24 +0000457{
458 unsigned int fpos = 0;
wdenkefe2a4d2004-12-16 21:44:03 +0000459 int status;
stroese2b918712004-12-16 17:26:24 +0000460 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
wdenkefe2a4d2004-12-16 21:44:03 +0000461
stroese2b918712004-12-16 17:26:24 +0000462#ifdef DEBUG
wdenkefe2a4d2004-12-16 21:44:03 +0000463 if (name != NULL)
464 printf ("Iterate dir %s\n", name);
stroese2b918712004-12-16 17:26:24 +0000465#endif /* of DEBUG */
wdenkefe2a4d2004-12-16 21:44:03 +0000466 if (!diro->inode_read) {
467 status = ext2fs_read_inode (diro->data, diro->ino,
468 &diro->inode);
469 if (status == 0) {
470 return (0);
stroese2b918712004-12-16 17:26:24 +0000471 }
472 }
473 /* Search the file. */
wdenkefe2a4d2004-12-16 21:44:03 +0000474 while (fpos < __le32_to_cpu (diro->inode.size)) {
stroese2b918712004-12-16 17:26:24 +0000475 struct ext2_dirent dirent;
476
wdenkefe2a4d2004-12-16 21:44:03 +0000477 status = ext2fs_read_file (diro, fpos,
478 sizeof (struct ext2_dirent),
479 (char *) &dirent);
480 if (status < 1) {
481 return (0);
stroese2b918712004-12-16 17:26:24 +0000482 }
wdenkefe2a4d2004-12-16 21:44:03 +0000483 if (dirent.namelen != 0) {
484 char filename[dirent.namelen + 1];
485 ext2fs_node_t fdiro;
486 int type = FILETYPE_UNKNOWN;
stroese2b918712004-12-16 17:26:24 +0000487
wdenkefe2a4d2004-12-16 21:44:03 +0000488 status = ext2fs_read_file (diro,
489 fpos + sizeof (struct ext2_dirent),
490 dirent.namelen, filename);
491 if (status < 1) {
492 return (0);
stroese2b918712004-12-16 17:26:24 +0000493 }
494 fdiro = malloc (sizeof (struct ext2fs_node));
wdenkefe2a4d2004-12-16 21:44:03 +0000495 if (!fdiro) {
496 return (0);
stroese2b918712004-12-16 17:26:24 +0000497 }
498
499 fdiro->data = diro->data;
wdenkefe2a4d2004-12-16 21:44:03 +0000500 fdiro->ino = __le32_to_cpu (dirent.inode);
stroese2b918712004-12-16 17:26:24 +0000501
502 filename[dirent.namelen] = '\0';
503
wdenkefe2a4d2004-12-16 21:44:03 +0000504 if (dirent.filetype != FILETYPE_UNKNOWN) {
stroese2b918712004-12-16 17:26:24 +0000505 fdiro->inode_read = 0;
506
wdenkefe2a4d2004-12-16 21:44:03 +0000507 if (dirent.filetype == FILETYPE_DIRECTORY) {
stroese2b918712004-12-16 17:26:24 +0000508 type = FILETYPE_DIRECTORY;
wdenkefe2a4d2004-12-16 21:44:03 +0000509 } else if (dirent.filetype ==
510 FILETYPE_SYMLINK) {
stroese2b918712004-12-16 17:26:24 +0000511 type = FILETYPE_SYMLINK;
wdenkefe2a4d2004-12-16 21:44:03 +0000512 } else if (dirent.filetype == FILETYPE_REG) {
513 type = FILETYPE_REG;
stroese2b918712004-12-16 17:26:24 +0000514 }
wdenkefe2a4d2004-12-16 21:44:03 +0000515 } else {
stroese2b918712004-12-16 17:26:24 +0000516 /* The filetype can not be read from the dirent, get it from inode */
517
wdenkefe2a4d2004-12-16 21:44:03 +0000518 status = ext2fs_read_inode (diro->data,
519 __le32_to_cpu(dirent.inode),
520 &fdiro->inode);
521 if (status == 0) {
522 free (fdiro);
523 return (0);
stroese2b918712004-12-16 17:26:24 +0000524 }
525 fdiro->inode_read = 1;
526
wdenkefe2a4d2004-12-16 21:44:03 +0000527 if ((__le16_to_cpu (fdiro->inode.mode) &
528 FILETYPE_INO_MASK) ==
529 FILETYPE_INO_DIRECTORY) {
stroese2b918712004-12-16 17:26:24 +0000530 type = FILETYPE_DIRECTORY;
wdenkefe2a4d2004-12-16 21:44:03 +0000531 } else if ((__le16_to_cpu (fdiro->inode.mode)
532 & FILETYPE_INO_MASK) ==
533 FILETYPE_INO_SYMLINK) {
stroese2b918712004-12-16 17:26:24 +0000534 type = FILETYPE_SYMLINK;
wdenkefe2a4d2004-12-16 21:44:03 +0000535 } else if ((__le16_to_cpu (fdiro->inode.mode)
536 & FILETYPE_INO_MASK) ==
537 FILETYPE_INO_REG) {
stroese2b918712004-12-16 17:26:24 +0000538 type = FILETYPE_REG;
539 }
540 }
541#ifdef DEBUG
wdenkefe2a4d2004-12-16 21:44:03 +0000542 printf ("iterate >%s<\n", filename);
stroese2b918712004-12-16 17:26:24 +0000543#endif /* of DEBUG */
wdenkefe2a4d2004-12-16 21:44:03 +0000544 if ((name != NULL) && (fnode != NULL)
545 && (ftype != NULL)) {
546 if (strcmp (filename, name) == 0) {
stroese2b918712004-12-16 17:26:24 +0000547 *ftype = type;
548 *fnode = fdiro;
wdenkefe2a4d2004-12-16 21:44:03 +0000549 return (1);
stroese2b918712004-12-16 17:26:24 +0000550 }
wdenkefe2a4d2004-12-16 21:44:03 +0000551 } else {
552 if (fdiro->inode_read == 0) {
553 status = ext2fs_read_inode (diro->data,
554 __le32_to_cpu (dirent.inode),
555 &fdiro->inode);
556 if (status == 0) {
557 free (fdiro);
558 return (0);
stroese2b918712004-12-16 17:26:24 +0000559 }
560 fdiro->inode_read = 1;
561 }
wdenkefe2a4d2004-12-16 21:44:03 +0000562 switch (type) {
stroese2b918712004-12-16 17:26:24 +0000563 case FILETYPE_DIRECTORY:
wdenkefe2a4d2004-12-16 21:44:03 +0000564 printf ("<DIR> ");
stroese2b918712004-12-16 17:26:24 +0000565 break;
566 case FILETYPE_SYMLINK:
wdenkefe2a4d2004-12-16 21:44:03 +0000567 printf ("<SYM> ");
stroese2b918712004-12-16 17:26:24 +0000568 break;
569 case FILETYPE_REG:
wdenkefe2a4d2004-12-16 21:44:03 +0000570 printf (" ");
stroese2b918712004-12-16 17:26:24 +0000571 break;
572 default:
wdenkec0aee72004-12-19 09:58:11 +0000573 printf ("< ? > ");
stroese2b918712004-12-16 17:26:24 +0000574 break;
575 }
wdenkefe2a4d2004-12-16 21:44:03 +0000576 printf ("%10d %s\n",
577 __le32_to_cpu (fdiro->inode.size),
578 filename);
stroese2b918712004-12-16 17:26:24 +0000579 }
wdenkefe2a4d2004-12-16 21:44:03 +0000580 free (fdiro);
stroese2b918712004-12-16 17:26:24 +0000581 }
582 fpos += __le16_to_cpu (dirent.direntlen);
583 }
wdenkefe2a4d2004-12-16 21:44:03 +0000584 return (0);
stroese2b918712004-12-16 17:26:24 +0000585}
586
587
wdenkefe2a4d2004-12-16 21:44:03 +0000588static char *ext2fs_read_symlink (ext2fs_node_t node) {
589 char *symlink;
stroese2b918712004-12-16 17:26:24 +0000590 struct ext2fs_node *diro = node;
wdenkefe2a4d2004-12-16 21:44:03 +0000591 int status;
stroese2b918712004-12-16 17:26:24 +0000592
wdenkefe2a4d2004-12-16 21:44:03 +0000593 if (!diro->inode_read) {
594 status = ext2fs_read_inode (diro->data, diro->ino,
595 &diro->inode);
596 if (status == 0) {
597 return (0);
stroese2b918712004-12-16 17:26:24 +0000598 }
599 }
600 symlink = malloc (__le32_to_cpu (diro->inode.size) + 1);
wdenkefe2a4d2004-12-16 21:44:03 +0000601 if (!symlink) {
602 return (0);
stroese2b918712004-12-16 17:26:24 +0000603 }
604 /* If the filesize of the symlink is bigger than
605 60 the symlink is stored in a separate block,
606 otherwise it is stored in the inode. */
wdenkefe2a4d2004-12-16 21:44:03 +0000607 if (__le32_to_cpu (diro->inode.size) <= 60) {
608 strncpy (symlink, diro->inode.b.symlink,
609 __le32_to_cpu (diro->inode.size));
610 } else {
611 status = ext2fs_read_file (diro, 0,
612 __le32_to_cpu (diro->inode.size),
613 symlink);
614 if (status == 0) {
stroese2b918712004-12-16 17:26:24 +0000615 free (symlink);
wdenkefe2a4d2004-12-16 21:44:03 +0000616 return (0);
stroese2b918712004-12-16 17:26:24 +0000617 }
618 }
619 symlink[__le32_to_cpu (diro->inode.size)] = '\0';
wdenkefe2a4d2004-12-16 21:44:03 +0000620 return (symlink);
stroese2b918712004-12-16 17:26:24 +0000621}
622
623
624int ext2fs_find_file1
wdenkefe2a4d2004-12-16 21:44:03 +0000625 (const char *currpath,
626 ext2fs_node_t currroot, ext2fs_node_t * currfound, int *foundtype) {
627 char fpath[strlen (currpath) + 1];
628 char *name = fpath;
629 char *next;
630 int status;
631 int type = FILETYPE_DIRECTORY;
632 ext2fs_node_t currnode = currroot;
633 ext2fs_node_t oldnode = currroot;
stroese2b918712004-12-16 17:26:24 +0000634
635 strncpy (fpath, currpath, strlen (currpath) + 1);
636
637 /* Remove all leading slashes. */
wdenkefe2a4d2004-12-16 21:44:03 +0000638 while (*name == '/') {
stroese2b918712004-12-16 17:26:24 +0000639 name++;
wdenkefe2a4d2004-12-16 21:44:03 +0000640 }
641 if (!*name) {
stroese2b918712004-12-16 17:26:24 +0000642 *currfound = currnode;
wdenkefe2a4d2004-12-16 21:44:03 +0000643 return (1);
stroese2b918712004-12-16 17:26:24 +0000644 }
645
wdenkefe2a4d2004-12-16 21:44:03 +0000646 for (;;) {
stroese2b918712004-12-16 17:26:24 +0000647 int found;
648
649 /* Extract the actual part from the pathname. */
650 next = strchr (name, '/');
wdenkefe2a4d2004-12-16 21:44:03 +0000651 if (next) {
stroese2b918712004-12-16 17:26:24 +0000652 /* Remove all leading slashes. */
wdenkefe2a4d2004-12-16 21:44:03 +0000653 while (*next == '/') {
stroese2b918712004-12-16 17:26:24 +0000654 *(next++) = '\0';
655 }
656 }
657
658 /* At this point it is expected that the current node is a directory, check if this is true. */
wdenkefe2a4d2004-12-16 21:44:03 +0000659 if (type != FILETYPE_DIRECTORY) {
stroese2b918712004-12-16 17:26:24 +0000660 ext2fs_free_node (currnode, currroot);
wdenkefe2a4d2004-12-16 21:44:03 +0000661 return (0);
stroese2b918712004-12-16 17:26:24 +0000662 }
663
664 oldnode = currnode;
665
666 /* Iterate over the directory. */
667 found = ext2fs_iterate_dir (currnode, name, &currnode, &type);
wdenkefe2a4d2004-12-16 21:44:03 +0000668 if (found == 0) {
669 return (0);
stroese2b918712004-12-16 17:26:24 +0000670 }
wdenkefe2a4d2004-12-16 21:44:03 +0000671 if (found == -1) {
stroese2b918712004-12-16 17:26:24 +0000672 break;
673 }
674
675 /* Read in the symlink and follow it. */
wdenkefe2a4d2004-12-16 21:44:03 +0000676 if (type == FILETYPE_SYMLINK) {
stroese2b918712004-12-16 17:26:24 +0000677 char *symlink;
678
679 /* Test if the symlink does not loop. */
wdenkefe2a4d2004-12-16 21:44:03 +0000680 if (++symlinknest == 8) {
stroese2b918712004-12-16 17:26:24 +0000681 ext2fs_free_node (currnode, currroot);
wdenkefe2a4d2004-12-16 21:44:03 +0000682 ext2fs_free_node (oldnode, currroot);
683 return (0);
stroese2b918712004-12-16 17:26:24 +0000684 }
685
686 symlink = ext2fs_read_symlink (currnode);
687 ext2fs_free_node (currnode, currroot);
688
wdenkefe2a4d2004-12-16 21:44:03 +0000689 if (!symlink) {
stroese2b918712004-12-16 17:26:24 +0000690 ext2fs_free_node (oldnode, currroot);
wdenkefe2a4d2004-12-16 21:44:03 +0000691 return (0);
stroese2b918712004-12-16 17:26:24 +0000692 }
693#ifdef DEBUG
wdenkefe2a4d2004-12-16 21:44:03 +0000694 printf ("Got symlink >%s<\n", symlink);
stroese2b918712004-12-16 17:26:24 +0000695#endif /* of DEBUG */
696 /* The symlink is an absolute path, go back to the root inode. */
wdenkefe2a4d2004-12-16 21:44:03 +0000697 if (symlink[0] == '/') {
stroese2b918712004-12-16 17:26:24 +0000698 ext2fs_free_node (oldnode, currroot);
699 oldnode = &ext2fs_root->diropen;
700 }
701
702 /* Lookup the node the symlink points to. */
wdenkefe2a4d2004-12-16 21:44:03 +0000703 status = ext2fs_find_file1 (symlink, oldnode,
704 &currnode, &type);
stroese2b918712004-12-16 17:26:24 +0000705
706 free (symlink);
707
wdenkefe2a4d2004-12-16 21:44:03 +0000708 if (status == 0) {
stroese2b918712004-12-16 17:26:24 +0000709 ext2fs_free_node (oldnode, currroot);
wdenkefe2a4d2004-12-16 21:44:03 +0000710 return (0);
stroese2b918712004-12-16 17:26:24 +0000711 }
712 }
713
714 ext2fs_free_node (oldnode, currroot);
715
716 /* Found the node! */
wdenkefe2a4d2004-12-16 21:44:03 +0000717 if (!next || *next == '\0') {
stroese2b918712004-12-16 17:26:24 +0000718 *currfound = currnode;
719 *foundtype = type;
wdenkefe2a4d2004-12-16 21:44:03 +0000720 return (1);
stroese2b918712004-12-16 17:26:24 +0000721 }
722 name = next;
723 }
wdenkefe2a4d2004-12-16 21:44:03 +0000724 return (-1);
stroese2b918712004-12-16 17:26:24 +0000725}
726
727
728int ext2fs_find_file
wdenkefe2a4d2004-12-16 21:44:03 +0000729 (const char *path,
730 ext2fs_node_t rootnode, ext2fs_node_t * foundnode, int expecttype) {
stroese2b918712004-12-16 17:26:24 +0000731 int status;
732 int foundtype = FILETYPE_DIRECTORY;
733
734
735 symlinknest = 0;
wdenk20a80412005-02-04 15:02:06 +0000736 if (!path) {
wdenkefe2a4d2004-12-16 21:44:03 +0000737 return (0);
stroese2b918712004-12-16 17:26:24 +0000738 }
739
wdenkefe2a4d2004-12-16 21:44:03 +0000740 status = ext2fs_find_file1 (path, rootnode, foundnode, &foundtype);
741 if (status == 0) {
742 return (0);
stroese2b918712004-12-16 17:26:24 +0000743 }
744 /* Check if the node that was found was of the expected type. */
wdenkefe2a4d2004-12-16 21:44:03 +0000745 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) {
746 return (0);
747 } else if ((expecttype == FILETYPE_DIRECTORY)
748 && (foundtype != expecttype)) {
749 return (0);
stroese2b918712004-12-16 17:26:24 +0000750 }
wdenkefe2a4d2004-12-16 21:44:03 +0000751 return (1);
stroese2b918712004-12-16 17:26:24 +0000752}
753
754
wdenkefe2a4d2004-12-16 21:44:03 +0000755int ext2fs_ls (char *dirname) {
stroese2b918712004-12-16 17:26:24 +0000756 ext2fs_node_t dirnode;
wdenkefe2a4d2004-12-16 21:44:03 +0000757 int status;
stroese2b918712004-12-16 17:26:24 +0000758
wdenkefe2a4d2004-12-16 21:44:03 +0000759 if (ext2fs_root == NULL) {
760 return (0);
stroese2b918712004-12-16 17:26:24 +0000761 }
762
wdenkefe2a4d2004-12-16 21:44:03 +0000763 status = ext2fs_find_file (dirname, &ext2fs_root->diropen, &dirnode,
764 FILETYPE_DIRECTORY);
765 if (status != 1) {
766 printf ("** Can not find directory. **\n");
767 return (1);
768 }
769 ext2fs_iterate_dir (dirnode, NULL, NULL, NULL);
770 ext2fs_free_node (dirnode, &ext2fs_root->diropen);
771 return (0);
stroese2b918712004-12-16 17:26:24 +0000772}
773
774
wdenkefe2a4d2004-12-16 21:44:03 +0000775int ext2fs_open (char *filename) {
776 ext2fs_node_t fdiro = NULL;
777 int status;
778 int len;
stroese2b918712004-12-16 17:26:24 +0000779
wdenkefe2a4d2004-12-16 21:44:03 +0000780 if (ext2fs_root == NULL) {
wdenk20a80412005-02-04 15:02:06 +0000781 return (-1);
stroese2b918712004-12-16 17:26:24 +0000782 }
783 ext2fs_file = NULL;
wdenkefe2a4d2004-12-16 21:44:03 +0000784 status = ext2fs_find_file (filename, &ext2fs_root->diropen, &fdiro,
785 FILETYPE_REG);
786 if (status == 0) {
stroese2b918712004-12-16 17:26:24 +0000787 goto fail;
788 }
wdenkefe2a4d2004-12-16 21:44:03 +0000789 if (!fdiro->inode_read) {
790 status = ext2fs_read_inode (fdiro->data, fdiro->ino,
791 &fdiro->inode);
792 if (status == 0) {
stroese2b918712004-12-16 17:26:24 +0000793 goto fail;
794 }
795 }
796 len = __le32_to_cpu (fdiro->inode.size);
797 ext2fs_file = fdiro;
wdenkefe2a4d2004-12-16 21:44:03 +0000798 return (len);
stroese2b918712004-12-16 17:26:24 +0000799
wdenk20a80412005-02-04 15:02:06 +0000800fail:
wdenkefe2a4d2004-12-16 21:44:03 +0000801 ext2fs_free_node (fdiro, &ext2fs_root->diropen);
wdenk20a80412005-02-04 15:02:06 +0000802 return (-1);
stroese2b918712004-12-16 17:26:24 +0000803}
804
805
wdenkefe2a4d2004-12-16 21:44:03 +0000806int ext2fs_close (void
807 ) {
808 if ((ext2fs_file != NULL) && (ext2fs_root != NULL)) {
809 ext2fs_free_node (ext2fs_file, &ext2fs_root->diropen);
stroese2b918712004-12-16 17:26:24 +0000810 ext2fs_file = NULL;
811 }
wdenkefe2a4d2004-12-16 21:44:03 +0000812 if (ext2fs_root != NULL) {
813 free (ext2fs_root);
814 ext2fs_root = NULL;
stroese2b918712004-12-16 17:26:24 +0000815 }
wdenkefe2a4d2004-12-16 21:44:03 +0000816 if (indir1_block != NULL) {
817 free (indir1_block);
818 indir1_block = NULL;
819 indir1_size = 0;
820 indir1_blkno = -1;
stroese2b918712004-12-16 17:26:24 +0000821 }
wdenkefe2a4d2004-12-16 21:44:03 +0000822 if (indir2_block != NULL) {
823 free (indir2_block);
824 indir2_block = NULL;
825 indir2_size = 0;
826 indir2_blkno = -1;
stroese2b918712004-12-16 17:26:24 +0000827 }
wdenkefe2a4d2004-12-16 21:44:03 +0000828 return (0);
stroese2b918712004-12-16 17:26:24 +0000829}
830
831
wdenkefe2a4d2004-12-16 21:44:03 +0000832int ext2fs_read (char *buf, unsigned len) {
stroese2b918712004-12-16 17:26:24 +0000833 int status;
834
wdenkefe2a4d2004-12-16 21:44:03 +0000835 if (ext2fs_root == NULL) {
836 return (0);
stroese2b918712004-12-16 17:26:24 +0000837 }
838
wdenkefe2a4d2004-12-16 21:44:03 +0000839 if (ext2fs_file == NULL) {
840 return (0);
stroese2b918712004-12-16 17:26:24 +0000841 }
842
wdenkefe2a4d2004-12-16 21:44:03 +0000843 status = ext2fs_read_file (ext2fs_file, 0, len, buf);
844 return (status);
stroese2b918712004-12-16 17:26:24 +0000845}
846
847
wdenkefe2a4d2004-12-16 21:44:03 +0000848int ext2fs_mount (unsigned part_length) {
stroese2b918712004-12-16 17:26:24 +0000849 struct ext2_data *data;
wdenkefe2a4d2004-12-16 21:44:03 +0000850 int status;
stroese2b918712004-12-16 17:26:24 +0000851
852 data = malloc (sizeof (struct ext2_data));
wdenkefe2a4d2004-12-16 21:44:03 +0000853 if (!data) {
854 return (0);
stroese2b918712004-12-16 17:26:24 +0000855 }
856 /* Read the superblock. */
wdenkefe2a4d2004-12-16 21:44:03 +0000857 status = ext2fs_devread (1 * 2, 0, sizeof (struct ext2_sblock),
858 (char *) &data->sblock);
859 if (status == 0) {
stroese2b918712004-12-16 17:26:24 +0000860 goto fail;
861 }
862 /* Make sure this is an ext2 filesystem. */
wdenkefe2a4d2004-12-16 21:44:03 +0000863 if (__le16_to_cpu (data->sblock.magic) != EXT2_MAGIC) {
stroese2b918712004-12-16 17:26:24 +0000864 goto fail;
865 }
wdenkefe2a4d2004-12-16 21:44:03 +0000866 data->diropen.data = data;
867 data->diropen.ino = 2;
868 data->diropen.inode_read = 1;
869 data->inode = &data->diropen.inode;
stroese2b918712004-12-16 17:26:24 +0000870
871 status = ext2fs_read_inode (data, 2, data->inode);
wdenkefe2a4d2004-12-16 21:44:03 +0000872 if (status == 0) {
stroese2b918712004-12-16 17:26:24 +0000873 goto fail;
874 }
875
876 ext2fs_root = data;
877
wdenkefe2a4d2004-12-16 21:44:03 +0000878 return (1);
stroese2b918712004-12-16 17:26:24 +0000879
wdenkefe2a4d2004-12-16 21:44:03 +0000880fail:
881 printf ("Failed to mount ext2 filesystem...\n");
882 free (data);
stroese2b918712004-12-16 17:26:24 +0000883 ext2fs_root = NULL;
wdenkefe2a4d2004-12-16 21:44:03 +0000884 return (0);
stroese2b918712004-12-16 17:26:24 +0000885}