wdenk | 4d01d9e | 2004-03-25 14:59:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 - 2004 |
| 3 | * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 4d01d9e | 2004-03-25 14:59:05 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | |
| 9 | #include <common.h> |
wdenk | 4d01d9e | 2004-03-25 14:59:05 +0000 | [diff] [blame] | 10 | #include <config.h> |
| 11 | #include <reiserfs.h> |
| 12 | |
| 13 | #include "reiserfs_private.h" |
| 14 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 15 | static struct blk_desc *reiserfs_blk_desc; |
Rob Herring | 8d6ba05 | 2012-08-23 11:31:50 +0000 | [diff] [blame] | 16 | static disk_partition_t *part_info; |
wdenk | 4d01d9e | 2004-03-25 14:59:05 +0000 | [diff] [blame] | 17 | |
| 18 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 19 | void reiserfs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info) |
wdenk | 4d01d9e | 2004-03-25 14:59:05 +0000 | [diff] [blame] | 20 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 21 | reiserfs_blk_desc = rbdd; |
Rob Herring | 8d6ba05 | 2012-08-23 11:31:50 +0000 | [diff] [blame] | 22 | part_info = info; |
wdenk | 4d01d9e | 2004-03-25 14:59:05 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | |
| 26 | int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf) |
| 27 | { |
| 28 | char sec_buf[SECTOR_SIZE]; |
| 29 | unsigned block_len; |
| 30 | /* |
| 31 | unsigned len = byte_len; |
| 32 | u8 *start = buf; |
| 33 | */ |
| 34 | /* |
| 35 | * Check partition boundaries |
| 36 | */ |
| 37 | if (sector < 0 |
| 38 | || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) |
Rob Herring | 8d6ba05 | 2012-08-23 11:31:50 +0000 | [diff] [blame] | 39 | >= part_info->size)) { |
wdenk | d360213 | 2004-03-25 15:14:43 +0000 | [diff] [blame] | 40 | /* errnum = ERR_OUTSIDE_PART; */ |
wdenk | 4d01d9e | 2004-03-25 14:59:05 +0000 | [diff] [blame] | 41 | printf (" ** reiserfs_devread() read outside partition\n"); |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Get the read to the beginning of a partition. |
| 47 | */ |
| 48 | sector += byte_offset >> SECTOR_BITS; |
| 49 | byte_offset &= SECTOR_SIZE - 1; |
| 50 | |
| 51 | #if defined(DEBUG) |
| 52 | printf (" <%d, %d, %d> ", sector, byte_offset, byte_len); |
| 53 | #endif |
| 54 | |
| 55 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 56 | if (reiserfs_blk_desc == NULL) |
wdenk | 4d01d9e | 2004-03-25 14:59:05 +0000 | [diff] [blame] | 57 | return 0; |
| 58 | |
| 59 | |
| 60 | if (byte_offset != 0) { |
| 61 | /* read first part which isn't aligned with start of sector */ |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 62 | if (reiserfs_blk_desc->block_read(reiserfs_blk_desc, |
| 63 | part_info->start + sector, |
| 64 | 1, (void *)sec_buf) != 1) { |
wdenk | 4d01d9e | 2004-03-25 14:59:05 +0000 | [diff] [blame] | 65 | printf (" ** reiserfs_devread() read error\n"); |
| 66 | return 0; |
| 67 | } |
| 68 | memcpy(buf, sec_buf+byte_offset, min(SECTOR_SIZE-byte_offset, byte_len)); |
| 69 | buf+=min(SECTOR_SIZE-byte_offset, byte_len); |
| 70 | byte_len-=min(SECTOR_SIZE-byte_offset, byte_len); |
| 71 | sector++; |
| 72 | } |
| 73 | |
| 74 | /* read sector aligned part */ |
| 75 | block_len = byte_len & ~(SECTOR_SIZE-1); |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 76 | if (reiserfs_blk_desc->block_read(reiserfs_blk_desc, |
| 77 | part_info->start + sector, |
| 78 | block_len / SECTOR_SIZE, (void *)buf) |
| 79 | != block_len/SECTOR_SIZE) { |
wdenk | 4d01d9e | 2004-03-25 14:59:05 +0000 | [diff] [blame] | 80 | printf (" ** reiserfs_devread() read error - block\n"); |
| 81 | return 0; |
| 82 | } |
| 83 | buf+=block_len; |
| 84 | byte_len-=block_len; |
| 85 | sector+= block_len/SECTOR_SIZE; |
| 86 | |
| 87 | if ( byte_len != 0 ) { |
| 88 | /* read rest of data which are not in whole sector */ |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 89 | if (reiserfs_blk_desc->block_read(reiserfs_blk_desc, |
| 90 | part_info->start + sector, |
| 91 | 1, (void *)sec_buf) != 1) { |
wdenk | 4d01d9e | 2004-03-25 14:59:05 +0000 | [diff] [blame] | 92 | printf (" ** reiserfs_devread() read error - last part\n"); |
| 93 | return 0; |
| 94 | } |
| 95 | memcpy(buf, sec_buf, byte_len); |
| 96 | } |
| 97 | |
| 98 | return 1; |
| 99 | } |