blob: 2f409e66cddf0f6f5748144a962c1fd335c9f154 [file] [log] [blame]
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +00001/*
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 Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +00009 */
10
11
12#include <common.h>
13#include <config.h>
14#include <zfs_common.h>
15
Simon Glasse3394752016-02-29 15:25:34 -070016static struct blk_desc *zfs_blk_desc;
Rob Herring153817e2012-08-23 11:31:49 +000017static disk_partition_t *part_info;
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000018
Simon Glasse3394752016-02-29 15:25:34 -070019void zfs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info)
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000020{
Simon Glasse3394752016-02-29 15:25:34 -070021 zfs_blk_desc = rbdd;
Rob Herring153817e2012-08-23 11:31:49 +000022 part_info = info;
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000023}
24
25/* err */
26int 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 Herring153817e2012-08-23 11:31:49 +000037 part_info->size)) {
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000038 /* 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
Simon Glasse3394752016-02-29 15:25:34 -070051 if (zfs_blk_desc == NULL) {
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000052 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 */
Simon Glasse3394752016-02-29 15:25:34 -070058 if (zfs_blk_desc->block_read(zfs_blk_desc,
59 part_info->start + sector, 1,
60 (void *)sec_buf) != 1) {
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000061 printf(" ** zfs_devread() read error **\n");
62 return 1;
63 }
64 memcpy(buf, sec_buf + byte_offset,
65 min(SECTOR_SIZE - byte_offset, byte_len));
66 buf += min(SECTOR_SIZE - byte_offset, byte_len);
67 byte_len -= min(SECTOR_SIZE - byte_offset, byte_len);
68 sector++;
69 }
70
71 if (byte_len == 0)
72 return 0;
73
74 /* read sector aligned part */
75 block_len = byte_len & ~(SECTOR_SIZE - 1);
76
77 if (block_len == 0) {
78 u8 p[SECTOR_SIZE];
79
80 block_len = SECTOR_SIZE;
Simon Glasse3394752016-02-29 15:25:34 -070081 zfs_blk_desc->block_read(zfs_blk_desc,
82 part_info->start + sector,
83 1, (void *)p);
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000084 memcpy(buf, p, byte_len);
85 return 0;
86 }
87
Simon Glasse3394752016-02-29 15:25:34 -070088 if (zfs_blk_desc->block_read(zfs_blk_desc, part_info->start + sector,
89 block_len / SECTOR_SIZE,
90 (void *)buf) != block_len / SECTOR_SIZE) {
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000091 printf(" ** zfs_devread() read error - block\n");
92 return 1;
93 }
94
95 block_len = byte_len & ~(SECTOR_SIZE - 1);
96 buf += block_len;
97 byte_len -= block_len;
98 sector += block_len / SECTOR_SIZE;
99
100 if (byte_len != 0) {
101 /* read rest of data which are not in whole sector */
Simon Glasse3394752016-02-29 15:25:34 -0700102 if (zfs_blk_desc->block_read(zfs_blk_desc,
103 part_info->start + sector,
104 1, (void *)sec_buf) != 1) {
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +0000105 printf(" ** zfs_devread() read error - last part\n");
106 return 1;
107 }
108 memcpy(buf, sec_buf, byte_len);
109 }
110 return 0;
111}