3 * esd gmbh <www.esd-electronics.com>
4 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
6 * based on code of fs/reiserfs/dev.c by
8 * (C) Copyright 2003 - 2004
9 * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
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.
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.
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.
28 #if (CONFIG_COMMANDS & CFG_CMD_EXT2)
33 static block_dev_desc_t *ext2fs_block_dev_desc;
34 static disk_partition_t part_info;
37 int ext2fs_set_blk_dev (block_dev_desc_t * rbdd, int part)
39 ext2fs_block_dev_desc = rbdd;
42 /* disk doesn't use partition table */
44 part_info.size = rbdd->lba;
45 part_info.blksz = rbdd->blksz;
47 if (get_partition_info
48 (ext2fs_block_dev_desc, part, &part_info)) {
52 return (part_info.size);
56 int ext2fs_devread (int sector, int byte_offset, int byte_len, char *buf) {
57 char sec_buf[SECTOR_SIZE];
61 * Check partition boundaries
64 || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
66 /* errnum = ERR_OUTSIDE_PART; */
67 printf (" ** ext2fs_devread() read outside partition sector %d\n", sector);
72 * Get the read to the beginning of a partition.
74 sector += byte_offset >> SECTOR_BITS;
75 byte_offset &= SECTOR_SIZE - 1;
78 printf (" <%d, %d, %d>\n", sector, byte_offset, byte_len);
81 if (ext2fs_block_dev_desc == NULL) {
82 printf ("** Invalid Block Device Descriptor (NULL)\n");
86 if (byte_offset != 0) {
87 /* read first part which isn't aligned with start of sector */
88 if (ext2fs_block_dev_desc->
89 block_read (ext2fs_block_dev_desc->dev,
90 part_info.start + sector, 1,
91 (unsigned long *) sec_buf) != 1) {
92 printf (" ** ext2fs_devread() read error **\n");
95 memcpy (buf, sec_buf + byte_offset,
96 min (SECTOR_SIZE - byte_offset, byte_len));
97 buf += min (SECTOR_SIZE - byte_offset, byte_len);
98 byte_len -= min (SECTOR_SIZE - byte_offset, byte_len);
102 /* read sector aligned part */
103 block_len = byte_len & ~(SECTOR_SIZE - 1);
104 if (ext2fs_block_dev_desc->block_read (ext2fs_block_dev_desc->dev,
105 part_info.start + sector,
106 block_len / SECTOR_SIZE,
107 (unsigned long *) buf) !=
108 block_len / SECTOR_SIZE) {
109 printf (" ** ext2fs_devread() read error - block\n");
113 byte_len -= block_len;
114 sector += block_len / SECTOR_SIZE;
117 /* read rest of data which are not in whole sector */
118 if (ext2fs_block_dev_desc->
119 block_read (ext2fs_block_dev_desc->dev,
120 part_info.start + sector, 1,
121 (unsigned long *) sec_buf) != 1) {
122 printf (" ** ext2fs_devread() read error - last part\n");
125 memcpy (buf, sec_buf, byte_len);
129 #endif /* CFG_CMD_EXT2FS */