Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * based on code of fs/reiserfs/dev.c by |
| 4 | * |
| 5 | * (C) Copyright 2003 - 2004 |
| 6 | * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com> |
| 7 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <config.h> |
| 14 | #include <zfs_common.h> |
| 15 | |
| 16 | static block_dev_desc_t *zfs_block_dev_desc; |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 17 | static disk_partition_t *part_info; |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 18 | |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 19 | void zfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info) |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 20 | { |
| 21 | zfs_block_dev_desc = rbdd; |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 22 | part_info = info; |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | /* err */ |
| 26 | int zfs_devread(int sector, int byte_offset, int byte_len, char *buf) |
| 27 | { |
| 28 | short sec_buffer[SECTOR_SIZE/sizeof(short)]; |
| 29 | char *sec_buf = (char *)sec_buffer; |
| 30 | unsigned block_len; |
| 31 | |
| 32 | /* |
| 33 | * Check partition boundaries |
| 34 | */ |
| 35 | if ((sector < 0) || |
| 36 | ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >= |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 37 | part_info->size)) { |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 38 | /* errnum = ERR_OUTSIDE_PART; */ |
| 39 | printf(" ** zfs_devread() read outside partition sector %d\n", sector); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Get the read to the beginning of a partition. |
| 45 | */ |
| 46 | sector += byte_offset >> SECTOR_BITS; |
| 47 | byte_offset &= SECTOR_SIZE - 1; |
| 48 | |
| 49 | debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len); |
| 50 | |
| 51 | if (zfs_block_dev_desc == NULL) { |
| 52 | printf("** Invalid Block Device Descriptor (NULL)\n"); |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | if (byte_offset != 0) { |
| 57 | /* read first part which isn't aligned with start of sector */ |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 58 | if (zfs_block_dev_desc->block_read(zfs_block_dev_desc, |
| 59 | part_info->start + sector, 1, |
| 60 | (void *)sec_buf) |
| 61 | != 1) { |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 62 | printf(" ** zfs_devread() read error **\n"); |
| 63 | return 1; |
| 64 | } |
| 65 | memcpy(buf, sec_buf + byte_offset, |
| 66 | min(SECTOR_SIZE - byte_offset, byte_len)); |
| 67 | buf += min(SECTOR_SIZE - byte_offset, byte_len); |
| 68 | byte_len -= min(SECTOR_SIZE - byte_offset, byte_len); |
| 69 | sector++; |
| 70 | } |
| 71 | |
| 72 | if (byte_len == 0) |
| 73 | return 0; |
| 74 | |
| 75 | /* read sector aligned part */ |
| 76 | block_len = byte_len & ~(SECTOR_SIZE - 1); |
| 77 | |
| 78 | if (block_len == 0) { |
| 79 | u8 p[SECTOR_SIZE]; |
| 80 | |
| 81 | block_len = SECTOR_SIZE; |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 82 | zfs_block_dev_desc->block_read(zfs_block_dev_desc, |
| 83 | part_info->start + sector, |
| 84 | 1, (void *)p); |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 85 | memcpy(buf, p, byte_len); |
| 86 | return 0; |
| 87 | } |
| 88 | |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 89 | if (zfs_block_dev_desc->block_read(zfs_block_dev_desc, |
| 90 | part_info->start + sector, |
| 91 | block_len / SECTOR_SIZE, |
| 92 | (void *)buf) |
| 93 | != block_len / SECTOR_SIZE) { |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 94 | printf(" ** zfs_devread() read error - block\n"); |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | block_len = byte_len & ~(SECTOR_SIZE - 1); |
| 99 | buf += block_len; |
| 100 | byte_len -= block_len; |
| 101 | sector += block_len / SECTOR_SIZE; |
| 102 | |
| 103 | if (byte_len != 0) { |
| 104 | /* read rest of data which are not in whole sector */ |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 105 | if (zfs_block_dev_desc->block_read(zfs_block_dev_desc, |
| 106 | part_info->start + sector, |
| 107 | 1, (void *)sec_buf) != 1) { |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 108 | printf(" ** zfs_devread() read error - last part\n"); |
| 109 | return 1; |
| 110 | } |
| 111 | memcpy(buf, sec_buf, byte_len); |
| 112 | } |
| 113 | return 0; |
| 114 | } |