blob: cb288d60f567a51246ac1346138324edbdcfbb15 [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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20
21#include <common.h>
wdenk518e2e12004-03-25 14:59:05 +000022#include <config.h>
23#include <reiserfs.h>
24
25#include "reiserfs_private.h"
26
27static block_dev_desc_t *reiserfs_block_dev_desc;
Rob Herring650f3662012-08-23 11:31:50 +000028static disk_partition_t *part_info;
wdenk518e2e12004-03-25 14:59:05 +000029
30
Rob Herring650f3662012-08-23 11:31:50 +000031void reiserfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
wdenk518e2e12004-03-25 14:59:05 +000032{
33 reiserfs_block_dev_desc = rbdd;
Rob Herring650f3662012-08-23 11:31:50 +000034 part_info = info;
wdenk518e2e12004-03-25 14:59:05 +000035}
36
37
38int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf)
39{
40 char sec_buf[SECTOR_SIZE];
41 unsigned block_len;
42/*
43 unsigned len = byte_len;
44 u8 *start = buf;
45*/
46 /*
47 * Check partition boundaries
48 */
49 if (sector < 0
50 || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS))
Rob Herring650f3662012-08-23 11:31:50 +000051 >= part_info->size)) {
wdenkb79a11c2004-03-25 15:14:43 +000052/* errnum = ERR_OUTSIDE_PART; */
wdenk518e2e12004-03-25 14:59:05 +000053 printf (" ** reiserfs_devread() read outside partition\n");
54 return 0;
55 }
56
57 /*
58 * Get the read to the beginning of a partition.
59 */
60 sector += byte_offset >> SECTOR_BITS;
61 byte_offset &= SECTOR_SIZE - 1;
62
63#if defined(DEBUG)
64 printf (" <%d, %d, %d> ", sector, byte_offset, byte_len);
65#endif
66
67
68 if (reiserfs_block_dev_desc == NULL)
69 return 0;
70
71
72 if (byte_offset != 0) {
73 /* read first part which isn't aligned with start of sector */
74 if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
Rob Herring650f3662012-08-23 11:31:50 +000075 part_info->start + sector, 1,
76 (unsigned long *)sec_buf) != 1) {
wdenk518e2e12004-03-25 14:59:05 +000077 printf (" ** reiserfs_devread() read error\n");
78 return 0;
79 }
80 memcpy(buf, sec_buf+byte_offset, min(SECTOR_SIZE-byte_offset, byte_len));
81 buf+=min(SECTOR_SIZE-byte_offset, byte_len);
82 byte_len-=min(SECTOR_SIZE-byte_offset, byte_len);
83 sector++;
84 }
85
86 /* read sector aligned part */
87 block_len = byte_len & ~(SECTOR_SIZE-1);
88 if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
Rob Herring650f3662012-08-23 11:31:50 +000089 part_info->start + sector, block_len/SECTOR_SIZE,
90 (unsigned long *)buf) != block_len/SECTOR_SIZE) {
wdenk518e2e12004-03-25 14:59:05 +000091 printf (" ** reiserfs_devread() read error - block\n");
92 return 0;
93 }
94 buf+=block_len;
95 byte_len-=block_len;
96 sector+= block_len/SECTOR_SIZE;
97
98 if ( byte_len != 0 ) {
99 /* read rest of data which are not in whole sector */
100 if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
Rob Herring650f3662012-08-23 11:31:50 +0000101 part_info->start + sector, 1,
102 (unsigned long *)sec_buf) != 1) {
wdenk518e2e12004-03-25 14:59:05 +0000103 printf (" ** reiserfs_devread() read error - last part\n");
104 return 0;
105 }
106 memcpy(buf, sec_buf, byte_len);
107 }
108
109 return 1;
110}