blob: 7b24d6aa715c275b107b2c54eb5318fcd45aa517 [file] [log] [blame]
wdenk518e2e12004-03-25 14:59:05 +00001/*
2 * (C) Copyright 2003 - 2004
3 * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk518e2e12004-03-25 14:59:05 +00006 */
7
8
9#include <common.h>
wdenk518e2e12004-03-25 14:59:05 +000010#include <config.h>
11#include <reiserfs.h>
12
13#include "reiserfs_private.h"
14
15static block_dev_desc_t *reiserfs_block_dev_desc;
Rob Herring650f3662012-08-23 11:31:50 +000016static disk_partition_t *part_info;
wdenk518e2e12004-03-25 14:59:05 +000017
18
Rob Herring650f3662012-08-23 11:31:50 +000019void reiserfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
wdenk518e2e12004-03-25 14:59:05 +000020{
21 reiserfs_block_dev_desc = rbdd;
Rob Herring650f3662012-08-23 11:31:50 +000022 part_info = info;
wdenk518e2e12004-03-25 14:59:05 +000023}
24
25
26int 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 Herring650f3662012-08-23 11:31:50 +000039 >= part_info->size)) {
wdenkb79a11c2004-03-25 15:14:43 +000040/* errnum = ERR_OUTSIDE_PART; */
wdenk518e2e12004-03-25 14:59:05 +000041 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
56 if (reiserfs_block_dev_desc == NULL)
57 return 0;
58
59
60 if (byte_offset != 0) {
61 /* read first part which isn't aligned with start of sector */
Stephen Warren7c4213f2015-12-07 11:38:48 -070062 if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc,
63 part_info->start +
64 sector,
65 1, (void *)sec_buf)
66 != 1) {
wdenk518e2e12004-03-25 14:59:05 +000067 printf (" ** reiserfs_devread() read error\n");
68 return 0;
69 }
70 memcpy(buf, sec_buf+byte_offset, min(SECTOR_SIZE-byte_offset, byte_len));
71 buf+=min(SECTOR_SIZE-byte_offset, byte_len);
72 byte_len-=min(SECTOR_SIZE-byte_offset, byte_len);
73 sector++;
74 }
75
76 /* read sector aligned part */
77 block_len = byte_len & ~(SECTOR_SIZE-1);
Stephen Warren7c4213f2015-12-07 11:38:48 -070078 if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc,
79 part_info->start + sector,
80 block_len / SECTOR_SIZE,
81 (void *)buf)
82 != block_len/SECTOR_SIZE) {
wdenk518e2e12004-03-25 14:59:05 +000083 printf (" ** reiserfs_devread() read error - block\n");
84 return 0;
85 }
86 buf+=block_len;
87 byte_len-=block_len;
88 sector+= block_len/SECTOR_SIZE;
89
90 if ( byte_len != 0 ) {
91 /* read rest of data which are not in whole sector */
Stephen Warren7c4213f2015-12-07 11:38:48 -070092 if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc,
93 part_info->start +
94 sector,
95 1, (void *)sec_buf)
96 != 1) {
wdenk518e2e12004-03-25 14:59:05 +000097 printf (" ** reiserfs_devread() read error - last part\n");
98 return 0;
99 }
100 memcpy(buf, sec_buf, byte_len);
101 }
102
103 return 1;
104}