xref: /openbmc/u-boot/fs/reiserfs/dev.c (revision ecd4551f)
1 /*
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>
22 #include <config.h>
23 #include <reiserfs.h>
24 
25 #include "reiserfs_private.h"
26 
27 static block_dev_desc_t *reiserfs_block_dev_desc;
28 static disk_partition_t *part_info;
29 
30 
31 void reiserfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
32 {
33 	reiserfs_block_dev_desc = rbdd;
34 	part_info = info;
35 }
36 
37 
38 int 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))
51 	    >= part_info->size)) {
52 /*		errnum = ERR_OUTSIDE_PART; */
53 		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,
75 		    part_info->start + sector, 1,
76 		    (unsigned long *)sec_buf) != 1) {
77 			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,
89 	    part_info->start + sector, block_len/SECTOR_SIZE,
90 	    (unsigned long *)buf) != block_len/SECTOR_SIZE) {
91 		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,
101 		    part_info->start + sector, 1,
102 		    (unsigned long *)sec_buf) != 1) {
103 			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 }
111