| 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 <qi.h> |
| 28 | #include <ext2.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | extern unsigned long partition_offset_blocks; |
| 32 | extern unsigned long partition_length_blocks; |
| 33 | |
| 34 | |
| 35 | int ext2fs_devread(int sector, int filesystem_block_log2, int byte_offset, int byte_len, u8 *buf) |
| 36 | { |
| 37 | unsigned char sec_buf[SECTOR_SIZE]; |
| 38 | unsigned block_len; |
| 39 | |
| 40 | sector = sector << filesystem_block_log2; |
| 41 | |
| 42 | |
| 43 | /* |
| 44 | * Check partition boundaries |
| 45 | */ |
| 46 | if ((sector < 0) |
| 47 | || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >= |
| 48 | partition_length_blocks)) { |
| 49 | /* errnum = ERR_OUTSIDE_PART; */ |
| 50 | puts(" ** ext2fs_devread() read outside partition sector "); |
| 51 | printdec(sector); |
| 52 | puts("\n"); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | if (this_board->get_ui_keys) |
| 57 | if ((this_board->get_ui_keys)() & UI_ACTION_SKIPKERNEL) { |
| 58 | puts(" ** skipping \n"); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Get the read to the beginning of a partition. |
| 64 | */ |
| 65 | sector += byte_offset >> SECTOR_BITS; |
| 66 | byte_offset &= SECTOR_SIZE - 1; |
| 67 | |
| 68 | if (byte_offset) { |
| 69 | int minimum = SECTOR_SIZE - byte_offset; |
| 70 | |
| 71 | if (byte_len < minimum) |
| 72 | minimum = byte_len; |
| 73 | |
| 74 | /* read first part which isn't aligned with start of sector */ |
| 75 | if ((this_kernel->block_read)(sec_buf, |
| 76 | partition_offset_blocks + sector, 1) < 0) { |
| 77 | puts(" ** ext2fs_devread() read error **\n"); |
| 78 | return 0; |
| 79 | } |
| 80 | memcpy(buf, sec_buf + byte_offset, |
| 81 | minimum); |
| 82 | buf += minimum; |
| 83 | byte_len -= minimum; |
| 84 | sector++; |
| 85 | } |
| 86 | |
| 87 | if (!byte_len) |
| 88 | return 1; |
| 89 | |
| 90 | /* read sector aligned part */ |
| 91 | block_len = byte_len & ~(SECTOR_SIZE - 1); |
| 92 | |
| 93 | if (block_len == 0) { |
| 94 | u8 p[SECTOR_SIZE]; |
| 95 | |
| 96 | block_len = SECTOR_SIZE; |
| 97 | this_kernel->block_read(p,partition_offset_blocks + sector, 1); |
| 98 | memcpy(buf, p, byte_len); |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | if (this_kernel->block_read(buf, partition_offset_blocks + sector, |
| 103 | block_len / SECTOR_SIZE) < 0) { |
| 104 | puts(" ** ext2fs_devread() read error - block\n"); |
| 105 | printdec(partition_offset_blocks + sector); |
| 106 | puts(" "); |
| 107 | print32(block_len); |
| 108 | puts(" "); |
| 109 | print32(sector); |
| 110 | return 0; |
| 111 | } |
| 112 | block_len = byte_len & ~(SECTOR_SIZE - 1); |
| 113 | buf += block_len; |
| 114 | byte_len -= block_len; |
| 115 | sector += block_len / SECTOR_SIZE; |
| 116 | |
| 117 | if (byte_len) { |
| 118 | /* read rest of data which are not in whole sector */ |
| 119 | if (this_kernel->block_read(sec_buf, |
| 120 | partition_offset_blocks + sector, 1) != 1) { |
| 121 | puts(" ** ext2fs_devread() read error - last part\n"); |
| 122 | printdec(partition_offset_blocks + sector); |
| 123 | puts(" "); |
| 124 | print32(block_len); |
| 125 | puts(" "); |
| 126 | print32(sector); |
| 127 | return 0; |
| 128 | } |
| 129 | memcpy (buf, sec_buf, byte_len); |
| 130 | } |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | |