blob: 4365b3b882a12cfc0c16ddf7341b7b378f8888a6 [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>
stroese2b918712004-12-16 17:26:24 +000028#include <config.h>
29#include <ext2fs.h>
30
31static block_dev_desc_t *ext2fs_block_dev_desc;
32static disk_partition_t part_info;
33
Anton Staaf9bac35f2011-06-13 11:40:39 +000034int ext2fs_set_blk_dev(block_dev_desc_t *rbdd, int part)
wdenkefe2a4d2004-12-16 21:44:03 +000035{
36 ext2fs_block_dev_desc = rbdd;
stroese2b918712004-12-16 17:26:24 +000037
wdenkefe2a4d2004-12-16 21:44:03 +000038 if (part == 0) {
39 /* disk doesn't use partition table */
40 part_info.start = 0;
41 part_info.size = rbdd->lba;
42 part_info.blksz = rbdd->blksz;
43 } else {
44 if (get_partition_info
45 (ext2fs_block_dev_desc, part, &part_info)) {
46 return 0;
47 }
stroese2b918712004-12-16 17:26:24 +000048 }
Anton Staaf9bac35f2011-06-13 11:40:39 +000049 return part_info.size;
wdenkefe2a4d2004-12-16 21:44:03 +000050}
stroese2b918712004-12-16 17:26:24 +000051
52
Anton Staaf9bac35f2011-06-13 11:40:39 +000053int ext2fs_devread(int sector, int byte_offset, int byte_len, char *buf)
54{
wdenkefe2a4d2004-12-16 21:44:03 +000055 char sec_buf[SECTOR_SIZE];
56 unsigned block_len;
57
Anton Staaf9bac35f2011-06-13 11:40:39 +000058 /*
59 * Check partition boundaries
60 */
61 if ((sector < 0) ||
62 ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
wdenkefe2a4d2004-12-16 21:44:03 +000063 part_info.size)) {
Anton Staaf9bac35f2011-06-13 11:40:39 +000064 /* errnum = ERR_OUTSIDE_PART; */
65 printf(" ** %s read outside partition sector %d\n",
66 __func__,
67 sector);
68 return 0;
stroese2b918712004-12-16 17:26:24 +000069 }
70
Anton Staaf9bac35f2011-06-13 11:40:39 +000071 /*
72 * Get the read to the beginning of a partition.
73 */
wdenkefe2a4d2004-12-16 21:44:03 +000074 sector += byte_offset >> SECTOR_BITS;
75 byte_offset &= SECTOR_SIZE - 1;
stroese2b918712004-12-16 17:26:24 +000076
Anton Staaf9bac35f2011-06-13 11:40:39 +000077 debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len);
stroese2b918712004-12-16 17:26:24 +000078
wdenkefe2a4d2004-12-16 21:44:03 +000079 if (ext2fs_block_dev_desc == NULL) {
Anton Staaf9bac35f2011-06-13 11:40:39 +000080 printf(" ** %s Invalid Block Device Descriptor (NULL)\n",
81 __func__);
82 return 0;
stroese2b918712004-12-16 17:26:24 +000083 }
84
wdenkefe2a4d2004-12-16 21:44:03 +000085 if (byte_offset != 0) {
86 /* read first part which isn't aligned with start of sector */
87 if (ext2fs_block_dev_desc->
Anton Staaf9bac35f2011-06-13 11:40:39 +000088 block_read(ext2fs_block_dev_desc->dev,
89 part_info.start + sector, 1,
90 (unsigned long *) sec_buf) != 1) {
91 printf(" ** %s read error **\n", __func__);
92 return 0;
wdenkefe2a4d2004-12-16 21:44:03 +000093 }
Anton Staaf9bac35f2011-06-13 11:40:39 +000094 memcpy(buf, sec_buf + byte_offset,
95 min(SECTOR_SIZE - byte_offset, byte_len));
96 buf += min(SECTOR_SIZE - byte_offset, byte_len);
97 byte_len -= min(SECTOR_SIZE - byte_offset, byte_len);
wdenkefe2a4d2004-12-16 21:44:03 +000098 sector++;
stroese2b918712004-12-16 17:26:24 +000099 }
100
Dave Liuce1120d2008-02-29 17:45:31 +0800101 if (byte_len == 0)
102 return 1;
103
wdenkefe2a4d2004-12-16 21:44:03 +0000104 /* read sector aligned part */
105 block_len = byte_len & ~(SECTOR_SIZE - 1);
Dave Liuce1120d2008-02-29 17:45:31 +0800106
107 if (block_len == 0) {
108 u8 p[SECTOR_SIZE];
109
110 block_len = SECTOR_SIZE;
111 ext2fs_block_dev_desc->block_read(ext2fs_block_dev_desc->dev,
112 part_info.start + sector,
113 1, (unsigned long *)p);
114 memcpy(buf, p, byte_len);
115 return 1;
116 }
117
Anton Staaf9bac35f2011-06-13 11:40:39 +0000118 if (ext2fs_block_dev_desc->block_read(ext2fs_block_dev_desc->dev,
119 part_info.start + sector,
120 block_len / SECTOR_SIZE,
121 (unsigned long *) buf) !=
wdenkefe2a4d2004-12-16 21:44:03 +0000122 block_len / SECTOR_SIZE) {
Anton Staaf9bac35f2011-06-13 11:40:39 +0000123 printf(" ** %s read error - block\n", __func__);
124 return 0;
stroese2b918712004-12-16 17:26:24 +0000125 }
Dave Liuce1120d2008-02-29 17:45:31 +0800126 block_len = byte_len & ~(SECTOR_SIZE - 1);
wdenkefe2a4d2004-12-16 21:44:03 +0000127 buf += block_len;
128 byte_len -= block_len;
129 sector += block_len / SECTOR_SIZE;
stroese2b918712004-12-16 17:26:24 +0000130
wdenkefe2a4d2004-12-16 21:44:03 +0000131 if (byte_len != 0) {
132 /* read rest of data which are not in whole sector */
133 if (ext2fs_block_dev_desc->
Anton Staaf9bac35f2011-06-13 11:40:39 +0000134 block_read(ext2fs_block_dev_desc->dev,
135 part_info.start + sector, 1,
136 (unsigned long *) sec_buf) != 1) {
137 printf(" ** %s read error - last part\n", __func__);
138 return 0;
wdenkefe2a4d2004-12-16 21:44:03 +0000139 }
Anton Staaf9bac35f2011-06-13 11:40:39 +0000140 memcpy(buf, sec_buf, byte_len);
stroese2b918712004-12-16 17:26:24 +0000141 }
Anton Staaf9bac35f2011-06-13 11:40:39 +0000142 return 1;
wdenkefe2a4d2004-12-16 21:44:03 +0000143}