xref: /openbmc/u-boot/fs/reiserfs/dev.c (revision 7d9b5bae)
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 int reiserfs_set_blk_dev(block_dev_desc_t *rbdd, int part)
32 {
33 	reiserfs_block_dev_desc = rbdd;
34 
35 	if (part == 0) {
36 		/* disk doesn't use partition table */
37 		part_info.start = 0;
38 		part_info.size = rbdd->lba;
39 		part_info.blksz = rbdd->blksz;
40 	} else {
41 		if (get_partition_info (reiserfs_block_dev_desc, part, &part_info)) {
42 			return 0;
43 		}
44 	}
45 	return (part_info.size);
46 }
47 
48 
49 int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf)
50 {
51 	char sec_buf[SECTOR_SIZE];
52 	unsigned block_len;
53 /*
54 	unsigned len = byte_len;
55 	u8 *start = buf;
56 */
57 	/*
58 	*  Check partition boundaries
59 	*/
60 	if (sector < 0
61 	    || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS))
62 	    >= part_info.size)) {
63 /*		errnum = ERR_OUTSIDE_PART; */
64 		printf (" ** reiserfs_devread() read outside partition\n");
65 		return 0;
66 	}
67 
68 	/*
69 	 *  Get the read to the beginning of a partition.
70 	 */
71 	sector += byte_offset >> SECTOR_BITS;
72 	byte_offset &= SECTOR_SIZE - 1;
73 
74 #if defined(DEBUG)
75 	printf (" <%d, %d, %d> ", sector, byte_offset, byte_len);
76 #endif
77 
78 
79 	if (reiserfs_block_dev_desc == NULL)
80 		return 0;
81 
82 
83 	if (byte_offset != 0) {
84 		/* read first part which isn't aligned with start of sector */
85 		if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
86 		    part_info.start+sector, 1, (unsigned long *)sec_buf) != 1) {
87 			printf (" ** reiserfs_devread() read error\n");
88 			return 0;
89 		}
90 		memcpy(buf, sec_buf+byte_offset, min(SECTOR_SIZE-byte_offset, byte_len));
91 		buf+=min(SECTOR_SIZE-byte_offset, byte_len);
92 		byte_len-=min(SECTOR_SIZE-byte_offset, byte_len);
93 		sector++;
94 	}
95 
96 	/* read sector aligned part */
97 	block_len = byte_len & ~(SECTOR_SIZE-1);
98 	if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
99 	    part_info.start+sector, block_len/SECTOR_SIZE, (unsigned long *)buf) !=
100 	    block_len/SECTOR_SIZE) {
101 		printf (" ** reiserfs_devread() read error - block\n");
102 		return 0;
103 	}
104 	buf+=block_len;
105 	byte_len-=block_len;
106 	sector+= block_len/SECTOR_SIZE;
107 
108 	if ( byte_len != 0 ) {
109 		/* read rest of data which are not in whole sector */
110 		if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
111 		    part_info.start+sector, 1, (unsigned long *)sec_buf) != 1) {
112 			printf (" ** reiserfs_devread() read error - last part\n");
113 			return 0;
114 		}
115 		memcpy(buf, sec_buf, byte_len);
116 	}
117 
118 	return 1;
119 }
120