stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 1 | /* |
| 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> |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 28 | #include <config.h> |
| 29 | #include <ext2fs.h> |
| 30 | |
| 31 | static block_dev_desc_t *ext2fs_block_dev_desc; |
| 32 | static disk_partition_t part_info; |
| 33 | |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 34 | int ext2fs_set_blk_dev(block_dev_desc_t *rbdd, int part) |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 35 | { |
| 36 | ext2fs_block_dev_desc = rbdd; |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 37 | |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 38 | 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 | } |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 48 | } |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 49 | return part_info.size; |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 50 | } |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 51 | |
| 52 | |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 53 | int ext2fs_devread(int sector, int byte_offset, int byte_len, char *buf) |
| 54 | { |
Anton staaf | 0f3f02a | 2011-10-03 13:55:00 +0000 | [diff] [blame] | 55 | ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, SECTOR_SIZE); |
Anton Staaf | 44aef48 | 2011-06-13 11:40:40 +0000 | [diff] [blame] | 56 | unsigned sectors; |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 57 | |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 58 | /* |
| 59 | * Check partition boundaries |
| 60 | */ |
| 61 | if ((sector < 0) || |
| 62 | ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >= |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 63 | part_info.size)) { |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 64 | /* errnum = ERR_OUTSIDE_PART; */ |
| 65 | printf(" ** %s read outside partition sector %d\n", |
| 66 | __func__, |
| 67 | sector); |
| 68 | return 0; |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 71 | /* |
| 72 | * Get the read to the beginning of a partition. |
| 73 | */ |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 74 | sector += byte_offset >> SECTOR_BITS; |
| 75 | byte_offset &= SECTOR_SIZE - 1; |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 76 | |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 77 | debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len); |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 78 | |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 79 | if (ext2fs_block_dev_desc == NULL) { |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 80 | printf(" ** %s Invalid Block Device Descriptor (NULL)\n", |
| 81 | __func__); |
| 82 | return 0; |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 83 | } |
| 84 | |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 85 | if (byte_offset != 0) { |
| 86 | /* read first part which isn't aligned with start of sector */ |
| 87 | if (ext2fs_block_dev_desc-> |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 88 | 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; |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 93 | } |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 94 | 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); |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 98 | sector++; |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 99 | } |
| 100 | |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 101 | /* read sector aligned part */ |
Anton Staaf | 44aef48 | 2011-06-13 11:40:40 +0000 | [diff] [blame] | 102 | sectors = byte_len / SECTOR_SIZE; |
Dave Liu | 27b4343 | 2008-02-29 17:45:31 +0800 | [diff] [blame] | 103 | |
Anton Staaf | 44aef48 | 2011-06-13 11:40:40 +0000 | [diff] [blame] | 104 | if (sectors > 0) { |
| 105 | if (ext2fs_block_dev_desc->block_read( |
| 106 | ext2fs_block_dev_desc->dev, |
| 107 | part_info.start + sector, |
| 108 | sectors, |
| 109 | (unsigned long *) buf) != sectors) { |
| 110 | printf(" ** %s read error - block\n", __func__); |
| 111 | return 0; |
| 112 | } |
Dave Liu | 27b4343 | 2008-02-29 17:45:31 +0800 | [diff] [blame] | 113 | |
Anton Staaf | 44aef48 | 2011-06-13 11:40:40 +0000 | [diff] [blame] | 114 | buf += sectors * SECTOR_SIZE; |
| 115 | byte_len -= sectors * SECTOR_SIZE; |
| 116 | sector += sectors; |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 117 | } |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 118 | |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 119 | if (byte_len != 0) { |
| 120 | /* read rest of data which are not in whole sector */ |
| 121 | if (ext2fs_block_dev_desc-> |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 122 | block_read(ext2fs_block_dev_desc->dev, |
| 123 | part_info.start + sector, 1, |
| 124 | (unsigned long *) sec_buf) != 1) { |
| 125 | printf(" ** %s read error - last part\n", __func__); |
| 126 | return 0; |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 127 | } |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 128 | memcpy(buf, sec_buf, byte_len); |
stroese | 15be813 | 2004-12-16 17:26:24 +0000 | [diff] [blame] | 129 | } |
Anton Staaf | 949a89b | 2011-06-13 11:40:39 +0000 | [diff] [blame] | 130 | return 1; |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 131 | } |