blob: 1728b34fc8d5c4d5a394c53dd171aa46e6a46349 [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 of fs/reiserfs/dev.c by
7 *
8 * (C) Copyright 2003 - 2004
9 * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
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
27#include <common.h>
Jon Loeligerdd60d122007-07-09 17:56:50 -050028#if defined(CONFIG_CMD_EXT2)
stroese2b918712004-12-16 17:26:24 +000029
30#include <config.h>
31#include <ext2fs.h>
32
33static block_dev_desc_t *ext2fs_block_dev_desc;
34static disk_partition_t part_info;
35
wdenkefe2a4d2004-12-16 21:44:03 +000036int ext2fs_set_blk_dev (block_dev_desc_t * rbdd, int part)
37{
38 ext2fs_block_dev_desc = rbdd;
stroese2b918712004-12-16 17:26:24 +000039
wdenkefe2a4d2004-12-16 21:44:03 +000040 if (part == 0) {
41 /* disk doesn't use partition table */
42 part_info.start = 0;
43 part_info.size = rbdd->lba;
44 part_info.blksz = rbdd->blksz;
45 } else {
46 if (get_partition_info
47 (ext2fs_block_dev_desc, part, &part_info)) {
48 return 0;
49 }
stroese2b918712004-12-16 17:26:24 +000050 }
wdenkefe2a4d2004-12-16 21:44:03 +000051 return (part_info.size);
52}
stroese2b918712004-12-16 17:26:24 +000053
54
wdenkefe2a4d2004-12-16 21:44:03 +000055int ext2fs_devread (int sector, int byte_offset, int byte_len, char *buf) {
56 char sec_buf[SECTOR_SIZE];
57 unsigned block_len;
58
stroese2b918712004-12-16 17:26:24 +000059/*
60 * Check partition boundaries
61 */
wdenkefe2a4d2004-12-16 21:44:03 +000062 if ((sector < 0)
63 || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
64 part_info.size)) {
65 /* errnum = ERR_OUTSIDE_PART; */
66 printf (" ** ext2fs_devread() read outside partition sector %d\n", sector);
67 return (0);
stroese2b918712004-12-16 17:26:24 +000068 }
69
70/*
71 * Get the read to the beginning of a partition.
72 */
wdenkefe2a4d2004-12-16 21:44:03 +000073 sector += byte_offset >> SECTOR_BITS;
74 byte_offset &= SECTOR_SIZE - 1;
stroese2b918712004-12-16 17:26:24 +000075
wdenkb05dcb52005-03-04 11:27:31 +000076 debug (" <%d, %d, %d>\n", sector, byte_offset, byte_len);
stroese2b918712004-12-16 17:26:24 +000077
wdenkefe2a4d2004-12-16 21:44:03 +000078 if (ext2fs_block_dev_desc == NULL) {
79 printf ("** Invalid Block Device Descriptor (NULL)\n");
80 return (0);
stroese2b918712004-12-16 17:26:24 +000081 }
82
wdenkefe2a4d2004-12-16 21:44:03 +000083 if (byte_offset != 0) {
84 /* read first part which isn't aligned with start of sector */
85 if (ext2fs_block_dev_desc->
86 block_read (ext2fs_block_dev_desc->dev,
87 part_info.start + sector, 1,
88 (unsigned long *) sec_buf) != 1) {
89 printf (" ** ext2fs_devread() read error **\n");
90 return (0);
91 }
92 memcpy (buf, sec_buf + byte_offset,
93 min (SECTOR_SIZE - byte_offset, byte_len));
94 buf += min (SECTOR_SIZE - byte_offset, byte_len);
95 byte_len -= min (SECTOR_SIZE - byte_offset, byte_len);
96 sector++;
stroese2b918712004-12-16 17:26:24 +000097 }
98
Dave Liuce1120d2008-02-29 17:45:31 +080099 if (byte_len == 0)
100 return 1;
101
wdenkefe2a4d2004-12-16 21:44:03 +0000102 /* read sector aligned part */
103 block_len = byte_len & ~(SECTOR_SIZE - 1);
Dave Liuce1120d2008-02-29 17:45:31 +0800104
105 if (block_len == 0) {
106 u8 p[SECTOR_SIZE];
107
108 block_len = SECTOR_SIZE;
109 ext2fs_block_dev_desc->block_read(ext2fs_block_dev_desc->dev,
110 part_info.start + sector,
111 1, (unsigned long *)p);
112 memcpy(buf, p, byte_len);
113 return 1;
114 }
115
wdenkefe2a4d2004-12-16 21:44:03 +0000116 if (ext2fs_block_dev_desc->block_read (ext2fs_block_dev_desc->dev,
117 part_info.start + sector,
118 block_len / SECTOR_SIZE,
119 (unsigned long *) buf) !=
120 block_len / SECTOR_SIZE) {
121 printf (" ** ext2fs_devread() read error - block\n");
122 return (0);
stroese2b918712004-12-16 17:26:24 +0000123 }
Dave Liuce1120d2008-02-29 17:45:31 +0800124 block_len = byte_len & ~(SECTOR_SIZE - 1);
wdenkefe2a4d2004-12-16 21:44:03 +0000125 buf += block_len;
126 byte_len -= block_len;
127 sector += block_len / SECTOR_SIZE;
stroese2b918712004-12-16 17:26:24 +0000128
wdenkefe2a4d2004-12-16 21:44:03 +0000129 if (byte_len != 0) {
130 /* read rest of data which are not in whole sector */
131 if (ext2fs_block_dev_desc->
132 block_read (ext2fs_block_dev_desc->dev,
133 part_info.start + sector, 1,
134 (unsigned long *) sec_buf) != 1) {
135 printf (" ** ext2fs_devread() read error - last part\n");
136 return (0);
137 }
138 memcpy (buf, sec_buf, byte_len);
stroese2b918712004-12-16 17:26:24 +0000139 }
wdenkefe2a4d2004-12-16 21:44:03 +0000140 return (1);
141}
Jon Loeligerf40a7f32007-07-10 11:07:56 -0500142#endif