xref: /rk3399_rockchip-uboot/fs/ext4/dev.c (revision a1f41f1a55230dcd2e195f193809901fc4a082cf)
1a1596438SUma Shankar /*
2a1596438SUma Shankar  * (C) Copyright 2011 - 2012 Samsung Electronics
3a1596438SUma Shankar  * EXT4 filesystem implementation in Uboot by
4a1596438SUma Shankar  * Uma Shankar <uma.shankar@samsung.com>
5a1596438SUma Shankar  * Manjunatha C Achar <a.manjunatha@samsung.com>
6a1596438SUma Shankar  *
7a1596438SUma Shankar  * made from existing ext2/dev.c file of Uboot
8a1596438SUma Shankar  * (C) Copyright 2004
9a1596438SUma Shankar  * esd gmbh <www.esd-electronics.com>
10a1596438SUma Shankar  * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
11a1596438SUma Shankar  *
12a1596438SUma Shankar  * based on code of fs/reiserfs/dev.c by
13a1596438SUma Shankar  *
14a1596438SUma Shankar  * (C) Copyright 2003 - 2004
15a1596438SUma Shankar  * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
16a1596438SUma Shankar  *
17a1596438SUma Shankar  * This program is free software; you can redistribute it and/or modify
18a1596438SUma Shankar  * it under the terms of the GNU General Public License as published by
19a1596438SUma Shankar  * the Free Software Foundation; either version 2 of the License, or
20a1596438SUma Shankar  * (at your option) any later version.
21a1596438SUma Shankar  *
22a1596438SUma Shankar  * This program is distributed in the hope that it will be useful,
23a1596438SUma Shankar  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24a1596438SUma Shankar  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25a1596438SUma Shankar  * GNU General Public License for more details.
26a1596438SUma Shankar  *
27a1596438SUma Shankar  * You should have received a copy of the GNU General Public License
28a1596438SUma Shankar  * along with this program; if not, write to the Free Software
29a1596438SUma Shankar  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30a1596438SUma Shankar  *
31a1596438SUma Shankar  */
32a1596438SUma Shankar 
33a1596438SUma Shankar /*
34a1596438SUma Shankar  * Changelog:
35a1596438SUma Shankar  *	0.1 - Newly created file for ext4fs support. Taken from
36a1596438SUma Shankar  *		fs/ext2/dev.c file in uboot.
37a1596438SUma Shankar  */
38a1596438SUma Shankar 
39a1596438SUma Shankar #include <common.h>
40a1596438SUma Shankar #include <config.h>
4181180819SRob Herring #include <ext4fs.h>
42a1596438SUma Shankar #include <ext_common.h>
43a1596438SUma Shankar 
4481180819SRob Herring unsigned long part_offset;
45a1596438SUma Shankar 
4681180819SRob Herring static block_dev_desc_t *ext4fs_block_dev_desc;
4781180819SRob Herring static disk_partition_t *part_info;
4881180819SRob Herring 
4981180819SRob Herring void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
50a1596438SUma Shankar {
51a1596438SUma Shankar 	ext4fs_block_dev_desc = rbdd;
5281180819SRob Herring 	part_info = info;
5381180819SRob Herring 	part_offset = info->start;
5481180819SRob Herring 	get_fs()->total_sect = (info->size * info->blksz) / SECTOR_SIZE;
55*a1f41f1aSŁukasz Majewski 	get_fs()->dev_desc = rbdd;
56a1596438SUma Shankar }
57a1596438SUma Shankar 
58a1596438SUma Shankar int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf)
59a1596438SUma Shankar {
6055b523b7SStephen Warren 	ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, SECTOR_SIZE);
61a1596438SUma Shankar 	unsigned block_len;
62a1596438SUma Shankar 
63a1596438SUma Shankar 	/* Check partition boundaries */
64a1596438SUma Shankar 	if ((sector < 0)
65a1596438SUma Shankar 	    || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
6681180819SRob Herring 		part_info->size)) {
67a1596438SUma Shankar 		printf("%s read outside partition %d\n", __func__, sector);
68a1596438SUma Shankar 		return 0;
69a1596438SUma Shankar 	}
70a1596438SUma Shankar 
71a1596438SUma Shankar 	/* Get the read to the beginning of a partition */
72a1596438SUma Shankar 	sector += byte_offset >> SECTOR_BITS;
73a1596438SUma Shankar 	byte_offset &= SECTOR_SIZE - 1;
74a1596438SUma Shankar 
75a1596438SUma Shankar 	debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len);
76a1596438SUma Shankar 
77a1596438SUma Shankar 	if (ext4fs_block_dev_desc == NULL) {
78a1596438SUma Shankar 		printf("** Invalid Block Device Descriptor (NULL)\n");
79a1596438SUma Shankar 		return 0;
80a1596438SUma Shankar 	}
81a1596438SUma Shankar 
82a1596438SUma Shankar 	if (byte_offset != 0) {
83a1596438SUma Shankar 		/* read first part which isn't aligned with start of sector */
84a1596438SUma Shankar 		if (ext4fs_block_dev_desc->
85a1596438SUma Shankar 		    block_read(ext4fs_block_dev_desc->dev,
8681180819SRob Herring 				part_info->start + sector, 1,
87a1596438SUma Shankar 				(unsigned long *) sec_buf) != 1) {
88a1596438SUma Shankar 			printf(" ** ext2fs_devread() read error **\n");
89a1596438SUma Shankar 			return 0;
90a1596438SUma Shankar 		}
91a1596438SUma Shankar 		memcpy(buf, sec_buf + byte_offset,
92a1596438SUma Shankar 			min(SECTOR_SIZE - byte_offset, byte_len));
93a1596438SUma Shankar 		buf += min(SECTOR_SIZE - byte_offset, byte_len);
94a1596438SUma Shankar 		byte_len -= min(SECTOR_SIZE - byte_offset, byte_len);
95a1596438SUma Shankar 		sector++;
96a1596438SUma Shankar 	}
97a1596438SUma Shankar 
98a1596438SUma Shankar 	if (byte_len == 0)
99a1596438SUma Shankar 		return 1;
100a1596438SUma Shankar 
101a1596438SUma Shankar 	/* read sector aligned part */
102a1596438SUma Shankar 	block_len = byte_len & ~(SECTOR_SIZE - 1);
103a1596438SUma Shankar 
104a1596438SUma Shankar 	if (block_len == 0) {
10555b523b7SStephen Warren 		ALLOC_CACHE_ALIGN_BUFFER(u8, p, SECTOR_SIZE);
106a1596438SUma Shankar 
107a1596438SUma Shankar 		block_len = SECTOR_SIZE;
108a1596438SUma Shankar 		ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
10981180819SRob Herring 						  part_info->start + sector,
110a1596438SUma Shankar 						  1, (unsigned long *)p);
111a1596438SUma Shankar 		memcpy(buf, p, byte_len);
112a1596438SUma Shankar 		return 1;
113a1596438SUma Shankar 	}
114a1596438SUma Shankar 
115a1596438SUma Shankar 	if (ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
11681180819SRob Herring 					       part_info->start + sector,
117a1596438SUma Shankar 					       block_len / SECTOR_SIZE,
118a1596438SUma Shankar 					       (unsigned long *) buf) !=
119a1596438SUma Shankar 					       block_len / SECTOR_SIZE) {
120a1596438SUma Shankar 		printf(" ** %s read error - block\n", __func__);
121a1596438SUma Shankar 		return 0;
122a1596438SUma Shankar 	}
123a1596438SUma Shankar 	block_len = byte_len & ~(SECTOR_SIZE - 1);
124a1596438SUma Shankar 	buf += block_len;
125a1596438SUma Shankar 	byte_len -= block_len;
126a1596438SUma Shankar 	sector += block_len / SECTOR_SIZE;
127a1596438SUma Shankar 
128a1596438SUma Shankar 	if (byte_len != 0) {
129a1596438SUma Shankar 		/* read rest of data which are not in whole sector */
130a1596438SUma Shankar 		if (ext4fs_block_dev_desc->
131a1596438SUma Shankar 		    block_read(ext4fs_block_dev_desc->dev,
13281180819SRob Herring 				part_info->start + sector, 1,
133a1596438SUma Shankar 				(unsigned long *) sec_buf) != 1) {
134a1596438SUma Shankar 			printf("* %s read error - last part\n", __func__);
135a1596438SUma Shankar 			return 0;
136a1596438SUma Shankar 		}
137a1596438SUma Shankar 		memcpy(buf, sec_buf, byte_len);
138a1596438SUma Shankar 	}
139a1596438SUma Shankar 	return 1;
140a1596438SUma Shankar }
141