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> |
| 28 | #if (CONFIG_COMMANDS & CFG_CMD_EXT2) |
| 29 | |
| 30 | #include <config.h> |
| 31 | #include <ext2fs.h> |
| 32 | |
| 33 | static block_dev_desc_t *ext2fs_block_dev_desc; |
| 34 | static disk_partition_t part_info; |
| 35 | |
| 36 | #undef DEBUG |
| 37 | int ext2fs_set_blk_dev |
| 38 | ( |
| 39 | block_dev_desc_t *rbdd, |
| 40 | int part |
| 41 | ) |
| 42 | { |
| 43 | ext2fs_block_dev_desc = rbdd; |
| 44 | |
| 45 | if (part == 0) |
| 46 | { |
| 47 | /* disk doesn't use partition table */ |
| 48 | part_info.start = 0; |
| 49 | part_info.size = rbdd->lba; |
| 50 | part_info.blksz = rbdd->blksz; |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | if (get_partition_info (ext2fs_block_dev_desc, part, &part_info)) |
| 55 | { |
| 56 | return 0; |
| 57 | } |
| 58 | } |
| 59 | return (part_info.size); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | int ext2fs_devread |
| 64 | ( |
| 65 | int sector, |
| 66 | int byte_offset, |
| 67 | int byte_len, |
| 68 | char *buf |
| 69 | ) |
| 70 | { |
| 71 | char sec_buf[SECTOR_SIZE]; |
| 72 | unsigned block_len; |
| 73 | /* |
| 74 | * Check partition boundaries |
| 75 | */ |
| 76 | if ((sector < 0) || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >= part_info.size)) |
| 77 | { |
| 78 | /* errnum = ERR_OUTSIDE_PART; */ |
| 79 | printf (" ** ext2fs_devread() read outside partition sector %d\n", sector); |
| 80 | return(0); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Get the read to the beginning of a partition. |
| 85 | */ |
| 86 | sector += byte_offset >> SECTOR_BITS; |
| 87 | byte_offset &= SECTOR_SIZE - 1; |
| 88 | |
| 89 | #if defined(DEBUG) |
| 90 | printf (" <%d, %d, %d>\n", sector, byte_offset, byte_len); |
| 91 | #endif |
| 92 | |
| 93 | if (ext2fs_block_dev_desc == NULL) |
| 94 | { |
| 95 | printf("** Invalid Block Device Descriptor (NULL)\n"); |
| 96 | return(0); |
| 97 | } |
| 98 | |
| 99 | if (byte_offset != 0) |
| 100 | { |
| 101 | /* read first part which isn't aligned with start of sector */ |
| 102 | if (ext2fs_block_dev_desc->block_read(ext2fs_block_dev_desc->dev, part_info.start+sector, 1, (unsigned long *)sec_buf) != 1) |
| 103 | { |
| 104 | printf (" ** ext2fs_devread() read error **\n"); |
| 105 | return(0); |
| 106 | } |
| 107 | memcpy(buf, sec_buf+byte_offset, min(SECTOR_SIZE-byte_offset, byte_len)); |
| 108 | buf+=min(SECTOR_SIZE-byte_offset, byte_len); |
| 109 | byte_len-=min(SECTOR_SIZE-byte_offset, byte_len); |
| 110 | sector++; |
| 111 | } |
| 112 | |
| 113 | /* read sector aligned part */ |
| 114 | block_len = byte_len & ~(SECTOR_SIZE-1); |
| 115 | if (ext2fs_block_dev_desc->block_read(ext2fs_block_dev_desc->dev, |
| 116 | part_info.start+sector, |
| 117 | block_len/SECTOR_SIZE, |
| 118 | (unsigned long *)buf) != block_len/SECTOR_SIZE) |
| 119 | { |
| 120 | printf (" ** ext2fs_devread() read error - block\n"); |
| 121 | return(0); |
| 122 | } |
| 123 | buf+=block_len; |
| 124 | byte_len-=block_len; |
| 125 | sector+= block_len/SECTOR_SIZE; |
| 126 | |
| 127 | if (byte_len != 0) |
| 128 | { |
| 129 | /* read rest of data which are not in whole sector */ |
| 130 | if (ext2fs_block_dev_desc->block_read(ext2fs_block_dev_desc->dev, |
| 131 | part_info.start+sector, |
| 132 | 1, |
| 133 | (unsigned long *)sec_buf) != 1) |
| 134 | { |
| 135 | printf (" ** ext2fs_devread() read error - last part\n"); |
| 136 | return(0); |
| 137 | } |
| 138 | memcpy(buf, sec_buf, byte_len); |
| 139 | } |
| 140 | return(1); |
| 141 | } |
| 142 | #endif /* CFG_CMD_EXT2FS */ |