blob: f1fce48a3920d3ed1220005911ac2092a441f248 [file] [log] [blame]
stroese15be8132004-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>
stroese15be8132004-12-16 17:26:24 +000027#include <ext2fs.h>
28#include <malloc.h>
29#include <asm/byteorder.h>
30
wdenk07d7e6b2004-12-16 21:44:03 +000031extern int ext2fs_devread (int sector, int byte_offset, int byte_len,
32 char *buf);
stroese15be8132004-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. */
wdenk07d7e6b2004-12-16 21:44:03 +000068struct ext2_sblock {
stroese15be8132004-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. */
wdenk07d7e6b2004-12-16 21:44:03 +0000107struct ext2_block_group {
stroese15be8132004-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, Bernhard0d9e4852009-06-10 14:00:37 +0200113 uint16_t used_dir_cnt;
stroese15be8132004-12-16 17:26:24 +0000114 uint32_t reserved[3];
115};
116
117/* The ext2 inode. */
wdenk07d7e6b2004-12-16 21:44:03 +0000118struct ext2_inode {
stroese15be8132004-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;
wdenk07d7e6b2004-12-16 21:44:03 +0000128 uint32_t blockcnt; /* Blocks of 512 bytes!! */
stroese15be8132004-12-16 17:26:24 +0000129 uint32_t flags;
130 uint32_t osd1;
wdenk07d7e6b2004-12-16 21:44:03 +0000131 union {
132 struct datablocks {
stroese15be8132004-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];
wdenk07d7e6b2004-12-16 21:44:03 +0000139 } b;
stroese15be8132004-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. */
wdenk07d7e6b2004-12-16 21:44:03 +0000148struct ext2_dirent {
stroese15be8132004-12-16 17:26:24 +0000149 uint32_t inode;
150 uint16_t direntlen;
wdenk07d7e6b2004-12-16 21:44:03 +0000151 uint8_t namelen;
152 uint8_t filetype;
stroese15be8132004-12-16 17:26:24 +0000153};
154
wdenk07d7e6b2004-12-16 21:44:03 +0000155struct ext2fs_node {
stroese15be8132004-12-16 17:26:24 +0000156 struct ext2_data *data;
157 struct ext2_inode inode;
wdenk07d7e6b2004-12-16 21:44:03 +0000158 int ino;
159 int inode_read;
stroese15be8132004-12-16 17:26:24 +0000160};
161
162/* Information about a "mounted" ext2 filesystem. */
wdenk07d7e6b2004-12-16 21:44:03 +0000163struct ext2_data {
stroese15be8132004-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
wdenk07d7e6b2004-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;
Michael Brandtbcd124b2009-11-22 14:13:27 +0100181static unsigned int inode_size;
stroese15be8132004-12-16 17:26:24 +0000182
183
184static int ext2fs_blockgroup
wdenk07d7e6b2004-12-16 21:44:03 +0000185 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) {
Weirich, Bernhard0d9e4852009-06-10 14:00:37 +0200186 unsigned int blkno;
187 unsigned int blkoff;
188 unsigned int desc_per_blk;
189
190 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
191
192 blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 +
193 group / desc_per_blk;
194 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
stroese15be8132004-12-16 17:26:24 +0000195#ifdef DEBUG
Weirich, Bernhard0d9e4852009-06-10 14:00:37 +0200196 printf ("ext2fs read %d group descriptor (blkno %d blkoff %d)\n",
197 group, blkno, blkoff);
stroese15be8132004-12-16 17:26:24 +0000198#endif
Weirich, Bernhard0d9e4852009-06-10 14:00:37 +0200199 return (ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE(data),
200 blkoff, sizeof(struct ext2_block_group), (char *)blkgrp));
201
stroese15be8132004-12-16 17:26:24 +0000202}
203
204
205static int ext2fs_read_inode
wdenk07d7e6b2004-12-16 21:44:03 +0000206 (struct ext2_data *data, int ino, struct ext2_inode *inode) {
207 struct ext2_block_group blkgrp;
208 struct ext2_sblock *sblock = &data->sblock;
209 int inodes_per_block;
210 int status;
stroese15be8132004-12-16 17:26:24 +0000211
wdenk07d7e6b2004-12-16 21:44:03 +0000212 unsigned int blkno;
213 unsigned int blkoff;
stroese15be8132004-12-16 17:26:24 +0000214
stroese15be8132004-12-16 17:26:24 +0000215#ifdef DEBUG
Michael Brandtbcd124b2009-11-22 14:13:27 +0100216 printf ("ext2fs read inode %d, inode_size %d\n", ino, inode_size);
stroese15be8132004-12-16 17:26:24 +0000217#endif
Weirich, Bernhard0d9e4852009-06-10 14:00:37 +0200218 /* It is easier to calculate if the first inode is 0. */
219 ino--;
220 status = ext2fs_blockgroup (data, ino / __le32_to_cpu
221 (sblock->inodes_per_group), &blkgrp);
wdenk07d7e6b2004-12-16 21:44:03 +0000222 if (status == 0) {
223 return (0);
stroese15be8132004-12-16 17:26:24 +0000224 }
Weirich, Bernhard0d9e4852009-06-10 14:00:37 +0200225
Michael Brandtbcd124b2009-11-22 14:13:27 +0100226 inodes_per_block = EXT2_BLOCK_SIZE(data) / inode_size;
Weirich, Bernhard0d9e4852009-06-10 14:00:37 +0200227
228 blkno = __le32_to_cpu (blkgrp.inode_table_id) +
229 (ino % __le32_to_cpu (sblock->inodes_per_group))
230 / inodes_per_block;
Michael Brandtbcd124b2009-11-22 14:13:27 +0100231 blkoff = (ino % inodes_per_block) * inode_size;
stroese15be8132004-12-16 17:26:24 +0000232#ifdef DEBUG
wdenk07d7e6b2004-12-16 21:44:03 +0000233 printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
stroese15be8132004-12-16 17:26:24 +0000234#endif
235 /* Read the inode. */
Weirich, Bernhard0d9e4852009-06-10 14:00:37 +0200236 status = ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE (data), blkoff,
wdenk07d7e6b2004-12-16 21:44:03 +0000237 sizeof (struct ext2_inode), (char *) inode);
238 if (status == 0) {
239 return (0);
stroese15be8132004-12-16 17:26:24 +0000240 }
Weirich, Bernhard0d9e4852009-06-10 14:00:37 +0200241
wdenk07d7e6b2004-12-16 21:44:03 +0000242 return (1);
stroese15be8132004-12-16 17:26:24 +0000243}
244
245
wdenk07d7e6b2004-12-16 21:44:03 +0000246void ext2fs_free_node (ext2fs_node_t node, ext2fs_node_t currroot) {
247 if ((node != &ext2fs_root->diropen) && (node != currroot)) {
stroese15be8132004-12-16 17:26:24 +0000248 free (node);
249 }
250}
251
252
wdenk07d7e6b2004-12-16 21:44:03 +0000253static int ext2fs_read_block (ext2fs_node_t node, int fileblock) {
254 struct ext2_data *data = node->data;
255 struct ext2_inode *inode = &node->inode;
256 int blknr;
257 int blksz = EXT2_BLOCK_SIZE (data);
258 int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
259 int status;
stroese15be8132004-12-16 17:26:24 +0000260
261 /* Direct blocks. */
wdenk07d7e6b2004-12-16 21:44:03 +0000262 if (fileblock < INDIRECT_BLOCKS) {
stroese15be8132004-12-16 17:26:24 +0000263 blknr = __le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]);
wdenk07d7e6b2004-12-16 21:44:03 +0000264 }
stroese15be8132004-12-16 17:26:24 +0000265 /* Indirect. */
wdenk07d7e6b2004-12-16 21:44:03 +0000266 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
267 if (indir1_block == NULL) {
Simon Glass157d2932011-12-21 10:01:27 +0000268 indir1_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN,
269 blksz);
wdenk07d7e6b2004-12-16 21:44:03 +0000270 if (indir1_block == NULL) {
271 printf ("** ext2fs read block (indir 1) malloc failed. **\n");
272 return (-1);
stroese15be8132004-12-16 17:26:24 +0000273 }
wdenk07d7e6b2004-12-16 21:44:03 +0000274 indir1_size = blksz;
stroese15be8132004-12-16 17:26:24 +0000275 indir1_blkno = -1;
276 }
wdenk07d7e6b2004-12-16 21:44:03 +0000277 if (blksz != indir1_size) {
278 free (indir1_block);
stroese15be8132004-12-16 17:26:24 +0000279 indir1_block = NULL;
wdenk07d7e6b2004-12-16 21:44:03 +0000280 indir1_size = 0;
stroese15be8132004-12-16 17:26:24 +0000281 indir1_blkno = -1;
Simon Glass157d2932011-12-21 10:01:27 +0000282 indir1_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN,
283 blksz);
wdenk07d7e6b2004-12-16 21:44:03 +0000284 if (indir1_block == NULL) {
285 printf ("** ext2fs read block (indir 1) malloc failed. **\n");
286 return (-1);
stroese15be8132004-12-16 17:26:24 +0000287 }
wdenk07d7e6b2004-12-16 21:44:03 +0000288 indir1_size = blksz;
stroese15be8132004-12-16 17:26:24 +0000289 }
wdenk07d7e6b2004-12-16 21:44:03 +0000290 if ((__le32_to_cpu (inode->b.blocks.indir_block) <<
291 log2_blksz) != indir1_blkno) {
292 status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz,
293 0, blksz,
294 (char *) indir1_block);
295 if (status == 0) {
296 printf ("** ext2fs read block (indir 1) failed. **\n");
297 return (0);
stroese15be8132004-12-16 17:26:24 +0000298 }
wdenk07d7e6b2004-12-16 21:44:03 +0000299 indir1_blkno =
300 __le32_to_cpu (inode->b.blocks.
301 indir_block) << log2_blksz;
stroese15be8132004-12-16 17:26:24 +0000302 }
wdenk07d7e6b2004-12-16 21:44:03 +0000303 blknr = __le32_to_cpu (indir1_block
304 [fileblock - INDIRECT_BLOCKS]);
stroese15be8132004-12-16 17:26:24 +0000305 }
306 /* Double indirect. */
wdenk07d7e6b2004-12-16 21:44:03 +0000307 else if (fileblock <
308 (INDIRECT_BLOCKS + (blksz / 4 * (blksz / 4 + 1)))) {
stroese15be8132004-12-16 17:26:24 +0000309 unsigned int perblock = blksz / 4;
310 unsigned int rblock = fileblock - (INDIRECT_BLOCKS
311 + blksz / 4);
312
wdenk07d7e6b2004-12-16 21:44:03 +0000313 if (indir1_block == NULL) {
Simon Glass157d2932011-12-21 10:01:27 +0000314 indir1_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN,
315 blksz);
wdenk07d7e6b2004-12-16 21:44:03 +0000316 if (indir1_block == NULL) {
317 printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
318 return (-1);
stroese15be8132004-12-16 17:26:24 +0000319 }
wdenk07d7e6b2004-12-16 21:44:03 +0000320 indir1_size = blksz;
stroese15be8132004-12-16 17:26:24 +0000321 indir1_blkno = -1;
322 }
wdenk07d7e6b2004-12-16 21:44:03 +0000323 if (blksz != indir1_size) {
324 free (indir1_block);
stroese15be8132004-12-16 17:26:24 +0000325 indir1_block = NULL;
wdenk07d7e6b2004-12-16 21:44:03 +0000326 indir1_size = 0;
stroese15be8132004-12-16 17:26:24 +0000327 indir1_blkno = -1;
Simon Glass157d2932011-12-21 10:01:27 +0000328 indir1_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN,
329 blksz);
wdenk07d7e6b2004-12-16 21:44:03 +0000330 if (indir1_block == NULL) {
331 printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
332 return (-1);
stroese15be8132004-12-16 17:26:24 +0000333 }
wdenk07d7e6b2004-12-16 21:44:03 +0000334 indir1_size = blksz;
stroese15be8132004-12-16 17:26:24 +0000335 }
wdenk07d7e6b2004-12-16 21:44:03 +0000336 if ((__le32_to_cpu (inode->b.blocks.double_indir_block) <<
337 log2_blksz) != indir1_blkno) {
338 status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz,
339 0, blksz,
340 (char *) indir1_block);
341 if (status == 0) {
342 printf ("** ext2fs read block (indir 2 1) failed. **\n");
343 return (-1);
stroese15be8132004-12-16 17:26:24 +0000344 }
wdenk07d7e6b2004-12-16 21:44:03 +0000345 indir1_blkno =
346 __le32_to_cpu (inode->b.blocks.double_indir_block) << log2_blksz;
stroese15be8132004-12-16 17:26:24 +0000347 }
348
wdenk07d7e6b2004-12-16 21:44:03 +0000349 if (indir2_block == NULL) {
Simon Glass157d2932011-12-21 10:01:27 +0000350 indir2_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN,
351 blksz);
wdenk07d7e6b2004-12-16 21:44:03 +0000352 if (indir2_block == NULL) {
353 printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
354 return (-1);
stroese15be8132004-12-16 17:26:24 +0000355 }
wdenk07d7e6b2004-12-16 21:44:03 +0000356 indir2_size = blksz;
stroese15be8132004-12-16 17:26:24 +0000357 indir2_blkno = -1;
358 }
wdenk07d7e6b2004-12-16 21:44:03 +0000359 if (blksz != indir2_size) {
360 free (indir2_block);
stroese15be8132004-12-16 17:26:24 +0000361 indir2_block = NULL;
wdenk07d7e6b2004-12-16 21:44:03 +0000362 indir2_size = 0;
stroese15be8132004-12-16 17:26:24 +0000363 indir2_blkno = -1;
Simon Glass157d2932011-12-21 10:01:27 +0000364 indir2_block = (uint32_t *) memalign(ARCH_DMA_MINALIGN,
365 blksz);
wdenk07d7e6b2004-12-16 21:44:03 +0000366 if (indir2_block == NULL) {
367 printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
368 return (-1);
stroese15be8132004-12-16 17:26:24 +0000369 }
wdenk07d7e6b2004-12-16 21:44:03 +0000370 indir2_size = blksz;
stroese15be8132004-12-16 17:26:24 +0000371 }
wdenk07d7e6b2004-12-16 21:44:03 +0000372 if ((__le32_to_cpu (indir1_block[rblock / perblock]) <<
Aaron Pace3ebc2022010-07-26 14:24:44 -0600373 log2_blksz) != indir2_blkno) {
wdenk07d7e6b2004-12-16 21:44:03 +0000374 status = ext2fs_devread (__le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz,
375 0, blksz,
376 (char *) indir2_block);
377 if (status == 0) {
378 printf ("** ext2fs read block (indir 2 2) failed. **\n");
379 return (-1);
stroese15be8132004-12-16 17:26:24 +0000380 }
wdenk07d7e6b2004-12-16 21:44:03 +0000381 indir2_blkno =
382 __le32_to_cpu (indir1_block[rblock / perblock]) << log2_blksz;
stroese15be8132004-12-16 17:26:24 +0000383 }
wdenk07d7e6b2004-12-16 21:44:03 +0000384 blknr = __le32_to_cpu (indir2_block[rblock % perblock]);
stroese15be8132004-12-16 17:26:24 +0000385 }
386 /* Tripple indirect. */
wdenk07d7e6b2004-12-16 21:44:03 +0000387 else {
388 printf ("** ext2fs doesn't support tripple indirect blocks. **\n");
389 return (-1);
390 }
stroese15be8132004-12-16 17:26:24 +0000391#ifdef DEBUG
wdenk07d7e6b2004-12-16 21:44:03 +0000392 printf ("ext2fs_read_block %08x\n", blknr);
stroese15be8132004-12-16 17:26:24 +0000393#endif
wdenk07d7e6b2004-12-16 21:44:03 +0000394 return (blknr);
stroese15be8132004-12-16 17:26:24 +0000395}
396
397
398int ext2fs_read_file
wdenk07d7e6b2004-12-16 21:44:03 +0000399 (ext2fs_node_t node, int pos, unsigned int len, char *buf) {
400 int i;
401 int blockcnt;
402 int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
403 int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
Stefan Roeseb7a97c72006-01-18 20:05:34 +0100404 unsigned int filesize = __le32_to_cpu(node->inode.size);
stroese15be8132004-12-16 17:26:24 +0000405
406 /* Adjust len so it we can't read past the end of the file. */
wdenk07d7e6b2004-12-16 21:44:03 +0000407 if (len > filesize) {
stroese15be8132004-12-16 17:26:24 +0000408 len = filesize;
409 }
wdenk07d7e6b2004-12-16 21:44:03 +0000410 blockcnt = ((len + pos) + blocksize - 1) / blocksize;
stroese15be8132004-12-16 17:26:24 +0000411
wdenk07d7e6b2004-12-16 21:44:03 +0000412 for (i = pos / blocksize; i < blockcnt; i++) {
stroese15be8132004-12-16 17:26:24 +0000413 int blknr;
414 int blockoff = pos % blocksize;
415 int blockend = blocksize;
416
417 int skipfirst = 0;
418
wdenk07d7e6b2004-12-16 21:44:03 +0000419 blknr = ext2fs_read_block (node, i);
420 if (blknr < 0) {
421 return (-1);
stroese15be8132004-12-16 17:26:24 +0000422 }
stroese15be8132004-12-16 17:26:24 +0000423
424 /* Last block. */
wdenk07d7e6b2004-12-16 21:44:03 +0000425 if (i == blockcnt - 1) {
stroese15be8132004-12-16 17:26:24 +0000426 blockend = (len + pos) % blocksize;
427
428 /* The last portion is exactly blocksize. */
wdenk07d7e6b2004-12-16 21:44:03 +0000429 if (!blockend) {
stroese15be8132004-12-16 17:26:24 +0000430 blockend = blocksize;
431 }
432 }
433
434 /* First block. */
wdenk07d7e6b2004-12-16 21:44:03 +0000435 if (i == pos / blocksize) {
stroese15be8132004-12-16 17:26:24 +0000436 skipfirst = blockoff;
437 blockend -= skipfirst;
438 }
439
u-boot@lakedaemon.net97562a32012-03-28 04:37:11 +0000440 /* grab middle blocks in one go */
441 if (i != pos / blocksize && i != blockcnt - 1 && blockcnt > 3) {
442 int oldblk = blknr;
443 int blocknxt;
444 while (i < blockcnt - 1) {
445 blocknxt = ext2fs_read_block(node, i + 1);
446 if (blocknxt == (oldblk + 1)) {
447 oldblk = blocknxt;
448 i++;
449 } else {
450 blocknxt = ext2fs_read_block(node, i);
451 break;
452 }
453 }
454
455 if (oldblk == blknr)
456 blockend = blocksize;
457 else
458 blockend = (1 + blocknxt - blknr) * blocksize;
459 }
460
461 blknr = blknr << log2blocksize;
462
stroese15be8132004-12-16 17:26:24 +0000463 /* If the block number is 0 this block is not stored on disk but
464 is zero filled instead. */
wdenk07d7e6b2004-12-16 21:44:03 +0000465 if (blknr) {
stroese15be8132004-12-16 17:26:24 +0000466 int status;
467
468 status = ext2fs_devread (blknr, skipfirst, blockend, buf);
wdenk07d7e6b2004-12-16 21:44:03 +0000469 if (status == 0) {
470 return (-1);
stroese15be8132004-12-16 17:26:24 +0000471 }
wdenk07d7e6b2004-12-16 21:44:03 +0000472 } else {
Wolfgang Denk51fbbb72008-01-09 10:16:33 +0100473 memset (buf, 0, blocksize - skipfirst);
stroese15be8132004-12-16 17:26:24 +0000474 }
u-boot@lakedaemon.net97562a32012-03-28 04:37:11 +0000475 buf += blockend - skipfirst;
wdenk07d7e6b2004-12-16 21:44:03 +0000476 }
477 return (len);
stroese15be8132004-12-16 17:26:24 +0000478}
479
480
wdenk07d7e6b2004-12-16 21:44:03 +0000481static int ext2fs_iterate_dir (ext2fs_node_t dir, char *name, ext2fs_node_t * fnode, int *ftype)
stroese15be8132004-12-16 17:26:24 +0000482{
483 unsigned int fpos = 0;
wdenk07d7e6b2004-12-16 21:44:03 +0000484 int status;
stroese15be8132004-12-16 17:26:24 +0000485 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
wdenk07d7e6b2004-12-16 21:44:03 +0000486
stroese15be8132004-12-16 17:26:24 +0000487#ifdef DEBUG
wdenk07d7e6b2004-12-16 21:44:03 +0000488 if (name != NULL)
489 printf ("Iterate dir %s\n", name);
stroese15be8132004-12-16 17:26:24 +0000490#endif /* of DEBUG */
wdenk07d7e6b2004-12-16 21:44:03 +0000491 if (!diro->inode_read) {
492 status = ext2fs_read_inode (diro->data, diro->ino,
493 &diro->inode);
494 if (status == 0) {
495 return (0);
stroese15be8132004-12-16 17:26:24 +0000496 }
497 }
498 /* Search the file. */
wdenk07d7e6b2004-12-16 21:44:03 +0000499 while (fpos < __le32_to_cpu (diro->inode.size)) {
stroese15be8132004-12-16 17:26:24 +0000500 struct ext2_dirent dirent;
501
wdenk07d7e6b2004-12-16 21:44:03 +0000502 status = ext2fs_read_file (diro, fpos,
503 sizeof (struct ext2_dirent),
504 (char *) &dirent);
505 if (status < 1) {
506 return (0);
stroese15be8132004-12-16 17:26:24 +0000507 }
wdenk07d7e6b2004-12-16 21:44:03 +0000508 if (dirent.namelen != 0) {
509 char filename[dirent.namelen + 1];
510 ext2fs_node_t fdiro;
511 int type = FILETYPE_UNKNOWN;
stroese15be8132004-12-16 17:26:24 +0000512
wdenk07d7e6b2004-12-16 21:44:03 +0000513 status = ext2fs_read_file (diro,
514 fpos + sizeof (struct ext2_dirent),
515 dirent.namelen, filename);
516 if (status < 1) {
517 return (0);
stroese15be8132004-12-16 17:26:24 +0000518 }
519 fdiro = malloc (sizeof (struct ext2fs_node));
wdenk07d7e6b2004-12-16 21:44:03 +0000520 if (!fdiro) {
521 return (0);
stroese15be8132004-12-16 17:26:24 +0000522 }
523
524 fdiro->data = diro->data;
wdenk07d7e6b2004-12-16 21:44:03 +0000525 fdiro->ino = __le32_to_cpu (dirent.inode);
stroese15be8132004-12-16 17:26:24 +0000526
527 filename[dirent.namelen] = '\0';
528
wdenk07d7e6b2004-12-16 21:44:03 +0000529 if (dirent.filetype != FILETYPE_UNKNOWN) {
stroese15be8132004-12-16 17:26:24 +0000530 fdiro->inode_read = 0;
531
wdenk07d7e6b2004-12-16 21:44:03 +0000532 if (dirent.filetype == FILETYPE_DIRECTORY) {
stroese15be8132004-12-16 17:26:24 +0000533 type = FILETYPE_DIRECTORY;
wdenk07d7e6b2004-12-16 21:44:03 +0000534 } else if (dirent.filetype ==
535 FILETYPE_SYMLINK) {
stroese15be8132004-12-16 17:26:24 +0000536 type = FILETYPE_SYMLINK;
wdenk07d7e6b2004-12-16 21:44:03 +0000537 } else if (dirent.filetype == FILETYPE_REG) {
538 type = FILETYPE_REG;
stroese15be8132004-12-16 17:26:24 +0000539 }
wdenk07d7e6b2004-12-16 21:44:03 +0000540 } else {
stroese15be8132004-12-16 17:26:24 +0000541 /* The filetype can not be read from the dirent, get it from inode */
542
wdenk07d7e6b2004-12-16 21:44:03 +0000543 status = ext2fs_read_inode (diro->data,
544 __le32_to_cpu(dirent.inode),
545 &fdiro->inode);
546 if (status == 0) {
547 free (fdiro);
548 return (0);
stroese15be8132004-12-16 17:26:24 +0000549 }
550 fdiro->inode_read = 1;
551
wdenk07d7e6b2004-12-16 21:44:03 +0000552 if ((__le16_to_cpu (fdiro->inode.mode) &
553 FILETYPE_INO_MASK) ==
554 FILETYPE_INO_DIRECTORY) {
stroese15be8132004-12-16 17:26:24 +0000555 type = FILETYPE_DIRECTORY;
wdenk07d7e6b2004-12-16 21:44:03 +0000556 } else if ((__le16_to_cpu (fdiro->inode.mode)
557 & FILETYPE_INO_MASK) ==
558 FILETYPE_INO_SYMLINK) {
stroese15be8132004-12-16 17:26:24 +0000559 type = FILETYPE_SYMLINK;
wdenk07d7e6b2004-12-16 21:44:03 +0000560 } else if ((__le16_to_cpu (fdiro->inode.mode)
561 & FILETYPE_INO_MASK) ==
562 FILETYPE_INO_REG) {
stroese15be8132004-12-16 17:26:24 +0000563 type = FILETYPE_REG;
564 }
565 }
566#ifdef DEBUG
wdenk07d7e6b2004-12-16 21:44:03 +0000567 printf ("iterate >%s<\n", filename);
stroese15be8132004-12-16 17:26:24 +0000568#endif /* of DEBUG */
wdenk07d7e6b2004-12-16 21:44:03 +0000569 if ((name != NULL) && (fnode != NULL)
570 && (ftype != NULL)) {
571 if (strcmp (filename, name) == 0) {
stroese15be8132004-12-16 17:26:24 +0000572 *ftype = type;
573 *fnode = fdiro;
wdenk07d7e6b2004-12-16 21:44:03 +0000574 return (1);
stroese15be8132004-12-16 17:26:24 +0000575 }
wdenk07d7e6b2004-12-16 21:44:03 +0000576 } else {
577 if (fdiro->inode_read == 0) {
578 status = ext2fs_read_inode (diro->data,
579 __le32_to_cpu (dirent.inode),
580 &fdiro->inode);
581 if (status == 0) {
582 free (fdiro);
583 return (0);
stroese15be8132004-12-16 17:26:24 +0000584 }
585 fdiro->inode_read = 1;
586 }
wdenk07d7e6b2004-12-16 21:44:03 +0000587 switch (type) {
stroese15be8132004-12-16 17:26:24 +0000588 case FILETYPE_DIRECTORY:
wdenk07d7e6b2004-12-16 21:44:03 +0000589 printf ("<DIR> ");
stroese15be8132004-12-16 17:26:24 +0000590 break;
591 case FILETYPE_SYMLINK:
wdenk07d7e6b2004-12-16 21:44:03 +0000592 printf ("<SYM> ");
stroese15be8132004-12-16 17:26:24 +0000593 break;
594 case FILETYPE_REG:
wdenk07d7e6b2004-12-16 21:44:03 +0000595 printf (" ");
stroese15be8132004-12-16 17:26:24 +0000596 break;
597 default:
wdenk11392db2004-12-19 09:58:11 +0000598 printf ("< ? > ");
stroese15be8132004-12-16 17:26:24 +0000599 break;
600 }
wdenk07d7e6b2004-12-16 21:44:03 +0000601 printf ("%10d %s\n",
602 __le32_to_cpu (fdiro->inode.size),
603 filename);
stroese15be8132004-12-16 17:26:24 +0000604 }
wdenk07d7e6b2004-12-16 21:44:03 +0000605 free (fdiro);
stroese15be8132004-12-16 17:26:24 +0000606 }
607 fpos += __le16_to_cpu (dirent.direntlen);
608 }
wdenk07d7e6b2004-12-16 21:44:03 +0000609 return (0);
stroese15be8132004-12-16 17:26:24 +0000610}
611
612
wdenk07d7e6b2004-12-16 21:44:03 +0000613static char *ext2fs_read_symlink (ext2fs_node_t node) {
614 char *symlink;
stroese15be8132004-12-16 17:26:24 +0000615 struct ext2fs_node *diro = node;
wdenk07d7e6b2004-12-16 21:44:03 +0000616 int status;
stroese15be8132004-12-16 17:26:24 +0000617
wdenk07d7e6b2004-12-16 21:44:03 +0000618 if (!diro->inode_read) {
619 status = ext2fs_read_inode (diro->data, diro->ino,
620 &diro->inode);
621 if (status == 0) {
622 return (0);
stroese15be8132004-12-16 17:26:24 +0000623 }
624 }
625 symlink = malloc (__le32_to_cpu (diro->inode.size) + 1);
wdenk07d7e6b2004-12-16 21:44:03 +0000626 if (!symlink) {
627 return (0);
stroese15be8132004-12-16 17:26:24 +0000628 }
629 /* If the filesize of the symlink is bigger than
630 60 the symlink is stored in a separate block,
631 otherwise it is stored in the inode. */
wdenk07d7e6b2004-12-16 21:44:03 +0000632 if (__le32_to_cpu (diro->inode.size) <= 60) {
633 strncpy (symlink, diro->inode.b.symlink,
634 __le32_to_cpu (diro->inode.size));
635 } else {
636 status = ext2fs_read_file (diro, 0,
637 __le32_to_cpu (diro->inode.size),
638 symlink);
639 if (status == 0) {
stroese15be8132004-12-16 17:26:24 +0000640 free (symlink);
wdenk07d7e6b2004-12-16 21:44:03 +0000641 return (0);
stroese15be8132004-12-16 17:26:24 +0000642 }
643 }
644 symlink[__le32_to_cpu (diro->inode.size)] = '\0';
wdenk07d7e6b2004-12-16 21:44:03 +0000645 return (symlink);
stroese15be8132004-12-16 17:26:24 +0000646}
647
648
649int ext2fs_find_file1
wdenk07d7e6b2004-12-16 21:44:03 +0000650 (const char *currpath,
651 ext2fs_node_t currroot, ext2fs_node_t * currfound, int *foundtype) {
652 char fpath[strlen (currpath) + 1];
653 char *name = fpath;
654 char *next;
655 int status;
656 int type = FILETYPE_DIRECTORY;
657 ext2fs_node_t currnode = currroot;
658 ext2fs_node_t oldnode = currroot;
stroese15be8132004-12-16 17:26:24 +0000659
660 strncpy (fpath, currpath, strlen (currpath) + 1);
661
662 /* Remove all leading slashes. */
wdenk07d7e6b2004-12-16 21:44:03 +0000663 while (*name == '/') {
stroese15be8132004-12-16 17:26:24 +0000664 name++;
wdenk07d7e6b2004-12-16 21:44:03 +0000665 }
666 if (!*name) {
stroese15be8132004-12-16 17:26:24 +0000667 *currfound = currnode;
wdenk07d7e6b2004-12-16 21:44:03 +0000668 return (1);
stroese15be8132004-12-16 17:26:24 +0000669 }
670
wdenk07d7e6b2004-12-16 21:44:03 +0000671 for (;;) {
stroese15be8132004-12-16 17:26:24 +0000672 int found;
673
674 /* Extract the actual part from the pathname. */
675 next = strchr (name, '/');
wdenk07d7e6b2004-12-16 21:44:03 +0000676 if (next) {
stroese15be8132004-12-16 17:26:24 +0000677 /* Remove all leading slashes. */
wdenk07d7e6b2004-12-16 21:44:03 +0000678 while (*next == '/') {
stroese15be8132004-12-16 17:26:24 +0000679 *(next++) = '\0';
680 }
681 }
682
683 /* At this point it is expected that the current node is a directory, check if this is true. */
wdenk07d7e6b2004-12-16 21:44:03 +0000684 if (type != FILETYPE_DIRECTORY) {
stroese15be8132004-12-16 17:26:24 +0000685 ext2fs_free_node (currnode, currroot);
wdenk07d7e6b2004-12-16 21:44:03 +0000686 return (0);
stroese15be8132004-12-16 17:26:24 +0000687 }
688
689 oldnode = currnode;
690
691 /* Iterate over the directory. */
692 found = ext2fs_iterate_dir (currnode, name, &currnode, &type);
wdenk07d7e6b2004-12-16 21:44:03 +0000693 if (found == 0) {
694 return (0);
stroese15be8132004-12-16 17:26:24 +0000695 }
wdenk07d7e6b2004-12-16 21:44:03 +0000696 if (found == -1) {
stroese15be8132004-12-16 17:26:24 +0000697 break;
698 }
699
700 /* Read in the symlink and follow it. */
wdenk07d7e6b2004-12-16 21:44:03 +0000701 if (type == FILETYPE_SYMLINK) {
stroese15be8132004-12-16 17:26:24 +0000702 char *symlink;
703
704 /* Test if the symlink does not loop. */
wdenk07d7e6b2004-12-16 21:44:03 +0000705 if (++symlinknest == 8) {
stroese15be8132004-12-16 17:26:24 +0000706 ext2fs_free_node (currnode, currroot);
wdenk07d7e6b2004-12-16 21:44:03 +0000707 ext2fs_free_node (oldnode, currroot);
708 return (0);
stroese15be8132004-12-16 17:26:24 +0000709 }
710
711 symlink = ext2fs_read_symlink (currnode);
712 ext2fs_free_node (currnode, currroot);
713
wdenk07d7e6b2004-12-16 21:44:03 +0000714 if (!symlink) {
stroese15be8132004-12-16 17:26:24 +0000715 ext2fs_free_node (oldnode, currroot);
wdenk07d7e6b2004-12-16 21:44:03 +0000716 return (0);
stroese15be8132004-12-16 17:26:24 +0000717 }
718#ifdef DEBUG
wdenk07d7e6b2004-12-16 21:44:03 +0000719 printf ("Got symlink >%s<\n", symlink);
stroese15be8132004-12-16 17:26:24 +0000720#endif /* of DEBUG */
721 /* The symlink is an absolute path, go back to the root inode. */
wdenk07d7e6b2004-12-16 21:44:03 +0000722 if (symlink[0] == '/') {
stroese15be8132004-12-16 17:26:24 +0000723 ext2fs_free_node (oldnode, currroot);
724 oldnode = &ext2fs_root->diropen;
725 }
726
727 /* Lookup the node the symlink points to. */
wdenk07d7e6b2004-12-16 21:44:03 +0000728 status = ext2fs_find_file1 (symlink, oldnode,
729 &currnode, &type);
stroese15be8132004-12-16 17:26:24 +0000730
731 free (symlink);
732
wdenk07d7e6b2004-12-16 21:44:03 +0000733 if (status == 0) {
stroese15be8132004-12-16 17:26:24 +0000734 ext2fs_free_node (oldnode, currroot);
wdenk07d7e6b2004-12-16 21:44:03 +0000735 return (0);
stroese15be8132004-12-16 17:26:24 +0000736 }
737 }
738
739 ext2fs_free_node (oldnode, currroot);
740
741 /* Found the node! */
wdenk07d7e6b2004-12-16 21:44:03 +0000742 if (!next || *next == '\0') {
stroese15be8132004-12-16 17:26:24 +0000743 *currfound = currnode;
744 *foundtype = type;
wdenk07d7e6b2004-12-16 21:44:03 +0000745 return (1);
stroese15be8132004-12-16 17:26:24 +0000746 }
747 name = next;
748 }
wdenk07d7e6b2004-12-16 21:44:03 +0000749 return (-1);
stroese15be8132004-12-16 17:26:24 +0000750}
751
752
753int ext2fs_find_file
wdenk07d7e6b2004-12-16 21:44:03 +0000754 (const char *path,
755 ext2fs_node_t rootnode, ext2fs_node_t * foundnode, int expecttype) {
stroese15be8132004-12-16 17:26:24 +0000756 int status;
757 int foundtype = FILETYPE_DIRECTORY;
758
759
760 symlinknest = 0;
wdenkb7345fc2005-02-04 15:02:06 +0000761 if (!path) {
wdenk07d7e6b2004-12-16 21:44:03 +0000762 return (0);
stroese15be8132004-12-16 17:26:24 +0000763 }
764
wdenk07d7e6b2004-12-16 21:44:03 +0000765 status = ext2fs_find_file1 (path, rootnode, foundnode, &foundtype);
766 if (status == 0) {
767 return (0);
stroese15be8132004-12-16 17:26:24 +0000768 }
769 /* Check if the node that was found was of the expected type. */
wdenk07d7e6b2004-12-16 21:44:03 +0000770 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) {
771 return (0);
772 } else if ((expecttype == FILETYPE_DIRECTORY)
773 && (foundtype != expecttype)) {
774 return (0);
stroese15be8132004-12-16 17:26:24 +0000775 }
wdenk07d7e6b2004-12-16 21:44:03 +0000776 return (1);
stroese15be8132004-12-16 17:26:24 +0000777}
778
779
Mike Frysinger8c8f5822010-10-20 07:18:09 -0400780int ext2fs_ls (const char *dirname) {
stroese15be8132004-12-16 17:26:24 +0000781 ext2fs_node_t dirnode;
wdenk07d7e6b2004-12-16 21:44:03 +0000782 int status;
stroese15be8132004-12-16 17:26:24 +0000783
wdenk07d7e6b2004-12-16 21:44:03 +0000784 if (ext2fs_root == NULL) {
785 return (0);
stroese15be8132004-12-16 17:26:24 +0000786 }
787
wdenk07d7e6b2004-12-16 21:44:03 +0000788 status = ext2fs_find_file (dirname, &ext2fs_root->diropen, &dirnode,
789 FILETYPE_DIRECTORY);
790 if (status != 1) {
791 printf ("** Can not find directory. **\n");
792 return (1);
793 }
794 ext2fs_iterate_dir (dirnode, NULL, NULL, NULL);
795 ext2fs_free_node (dirnode, &ext2fs_root->diropen);
796 return (0);
stroese15be8132004-12-16 17:26:24 +0000797}
798
799
Mike Frysinger8c8f5822010-10-20 07:18:09 -0400800int ext2fs_open (const char *filename) {
wdenk07d7e6b2004-12-16 21:44:03 +0000801 ext2fs_node_t fdiro = NULL;
802 int status;
803 int len;
stroese15be8132004-12-16 17:26:24 +0000804
wdenk07d7e6b2004-12-16 21:44:03 +0000805 if (ext2fs_root == NULL) {
wdenkb7345fc2005-02-04 15:02:06 +0000806 return (-1);
stroese15be8132004-12-16 17:26:24 +0000807 }
808 ext2fs_file = NULL;
wdenk07d7e6b2004-12-16 21:44:03 +0000809 status = ext2fs_find_file (filename, &ext2fs_root->diropen, &fdiro,
810 FILETYPE_REG);
811 if (status == 0) {
stroese15be8132004-12-16 17:26:24 +0000812 goto fail;
813 }
wdenk07d7e6b2004-12-16 21:44:03 +0000814 if (!fdiro->inode_read) {
815 status = ext2fs_read_inode (fdiro->data, fdiro->ino,
816 &fdiro->inode);
817 if (status == 0) {
stroese15be8132004-12-16 17:26:24 +0000818 goto fail;
819 }
820 }
821 len = __le32_to_cpu (fdiro->inode.size);
822 ext2fs_file = fdiro;
wdenk07d7e6b2004-12-16 21:44:03 +0000823 return (len);
stroese15be8132004-12-16 17:26:24 +0000824
wdenkb7345fc2005-02-04 15:02:06 +0000825fail:
wdenk07d7e6b2004-12-16 21:44:03 +0000826 ext2fs_free_node (fdiro, &ext2fs_root->diropen);
wdenkb7345fc2005-02-04 15:02:06 +0000827 return (-1);
stroese15be8132004-12-16 17:26:24 +0000828}
829
830
wdenk07d7e6b2004-12-16 21:44:03 +0000831int ext2fs_close (void
832 ) {
833 if ((ext2fs_file != NULL) && (ext2fs_root != NULL)) {
834 ext2fs_free_node (ext2fs_file, &ext2fs_root->diropen);
stroese15be8132004-12-16 17:26:24 +0000835 ext2fs_file = NULL;
836 }
wdenk07d7e6b2004-12-16 21:44:03 +0000837 if (ext2fs_root != NULL) {
838 free (ext2fs_root);
839 ext2fs_root = NULL;
stroese15be8132004-12-16 17:26:24 +0000840 }
wdenk07d7e6b2004-12-16 21:44:03 +0000841 if (indir1_block != NULL) {
842 free (indir1_block);
843 indir1_block = NULL;
844 indir1_size = 0;
845 indir1_blkno = -1;
stroese15be8132004-12-16 17:26:24 +0000846 }
wdenk07d7e6b2004-12-16 21:44:03 +0000847 if (indir2_block != NULL) {
848 free (indir2_block);
849 indir2_block = NULL;
850 indir2_size = 0;
851 indir2_blkno = -1;
stroese15be8132004-12-16 17:26:24 +0000852 }
wdenk07d7e6b2004-12-16 21:44:03 +0000853 return (0);
stroese15be8132004-12-16 17:26:24 +0000854}
855
856
wdenk07d7e6b2004-12-16 21:44:03 +0000857int ext2fs_read (char *buf, unsigned len) {
stroese15be8132004-12-16 17:26:24 +0000858 int status;
859
wdenk07d7e6b2004-12-16 21:44:03 +0000860 if (ext2fs_root == NULL) {
861 return (0);
stroese15be8132004-12-16 17:26:24 +0000862 }
863
wdenk07d7e6b2004-12-16 21:44:03 +0000864 if (ext2fs_file == NULL) {
865 return (0);
stroese15be8132004-12-16 17:26:24 +0000866 }
867
wdenk07d7e6b2004-12-16 21:44:03 +0000868 status = ext2fs_read_file (ext2fs_file, 0, len, buf);
869 return (status);
stroese15be8132004-12-16 17:26:24 +0000870}
871
872
wdenk07d7e6b2004-12-16 21:44:03 +0000873int ext2fs_mount (unsigned part_length) {
stroese15be8132004-12-16 17:26:24 +0000874 struct ext2_data *data;
wdenk07d7e6b2004-12-16 21:44:03 +0000875 int status;
stroese15be8132004-12-16 17:26:24 +0000876
877 data = malloc (sizeof (struct ext2_data));
wdenk07d7e6b2004-12-16 21:44:03 +0000878 if (!data) {
879 return (0);
stroese15be8132004-12-16 17:26:24 +0000880 }
881 /* Read the superblock. */
wdenk07d7e6b2004-12-16 21:44:03 +0000882 status = ext2fs_devread (1 * 2, 0, sizeof (struct ext2_sblock),
883 (char *) &data->sblock);
884 if (status == 0) {
stroese15be8132004-12-16 17:26:24 +0000885 goto fail;
886 }
887 /* Make sure this is an ext2 filesystem. */
wdenk07d7e6b2004-12-16 21:44:03 +0000888 if (__le16_to_cpu (data->sblock.magic) != EXT2_MAGIC) {
stroese15be8132004-12-16 17:26:24 +0000889 goto fail;
890 }
Michael Brandtbcd124b2009-11-22 14:13:27 +0100891 if (__le32_to_cpu(data->sblock.revision_level == 0)) {
892 inode_size = 128;
893 } else {
894 inode_size = __le16_to_cpu(data->sblock.inode_size);
895 }
896#ifdef DEBUG
897 printf("EXT2 rev %d, inode_size %d\n",
898 __le32_to_cpu(data->sblock.revision_level), inode_size);
899#endif
wdenk07d7e6b2004-12-16 21:44:03 +0000900 data->diropen.data = data;
901 data->diropen.ino = 2;
902 data->diropen.inode_read = 1;
903 data->inode = &data->diropen.inode;
stroese15be8132004-12-16 17:26:24 +0000904
905 status = ext2fs_read_inode (data, 2, data->inode);
wdenk07d7e6b2004-12-16 21:44:03 +0000906 if (status == 0) {
stroese15be8132004-12-16 17:26:24 +0000907 goto fail;
908 }
909
910 ext2fs_root = data;
911
wdenk07d7e6b2004-12-16 21:44:03 +0000912 return (1);
stroese15be8132004-12-16 17:26:24 +0000913
wdenk07d7e6b2004-12-16 21:44:03 +0000914fail:
915 printf ("Failed to mount ext2 filesystem...\n");
916 free (data);
stroese15be8132004-12-16 17:26:24 +0000917 ext2fs_root = NULL;
wdenk07d7e6b2004-12-16 21:44:03 +0000918 return (0);
stroese15be8132004-12-16 17:26:24 +0000919}