blob: 1596a92b9ada611fd6889741f5b5d421b290f0fc [file] [log] [blame]
Uma Shankar71014b62012-05-25 21:21:44 +05301/*
2 * (C) Copyright 2011 - 2012 Samsung Electronics
3 * EXT4 filesystem implementation in Uboot by
4 * Uma Shankar <uma.shankar@samsung.com>
5 * Manjunatha C Achar <a.manjunatha@samsung.com>
6 *
7 * made from existing ext2/dev.c file of Uboot
8 * (C) Copyright 2004
9 * esd gmbh <www.esd-electronics.com>
10 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
11 *
12 * based on code of fs/reiserfs/dev.c by
13 *
14 * (C) Copyright 2003 - 2004
15 * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 *
31 */
32
33/*
34 * Changelog:
35 * 0.1 - Newly created file for ext4fs support. Taken from
36 * fs/ext2/dev.c file in uboot.
37 */
38
39#include <common.h>
40#include <config.h>
Rob Herring752e1bd2012-08-23 11:31:46 +000041#include <ext4fs.h>
Uma Shankar71014b62012-05-25 21:21:44 +053042#include <ext_common.h>
43
Rob Herring752e1bd2012-08-23 11:31:46 +000044unsigned long part_offset;
45
Uma Shankar71014b62012-05-25 21:21:44 +053046static block_dev_desc_t *ext4fs_block_dev_desc;
Rob Herring752e1bd2012-08-23 11:31:46 +000047static disk_partition_t *part_info;
Uma Shankar71014b62012-05-25 21:21:44 +053048
Rob Herring752e1bd2012-08-23 11:31:46 +000049void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
Uma Shankar71014b62012-05-25 21:21:44 +053050{
51 ext4fs_block_dev_desc = rbdd;
Rob Herring752e1bd2012-08-23 11:31:46 +000052 part_info = info;
53 part_offset = info->start;
54 get_fs()->total_sect = (info->size * info->blksz) / SECTOR_SIZE;
Uma Shankar71014b62012-05-25 21:21:44 +053055}
56
57int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf)
58{
Stephen Warren07639f22012-09-18 08:05:28 +000059 ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, SECTOR_SIZE);
Uma Shankar71014b62012-05-25 21:21:44 +053060 unsigned block_len;
61
62 /* Check partition boundaries */
63 if ((sector < 0)
64 || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
Rob Herring752e1bd2012-08-23 11:31:46 +000065 part_info->size)) {
Uma Shankar71014b62012-05-25 21:21:44 +053066 printf("%s read outside partition %d\n", __func__, sector);
67 return 0;
68 }
69
70 /* Get the read to the beginning of a partition */
71 sector += byte_offset >> SECTOR_BITS;
72 byte_offset &= SECTOR_SIZE - 1;
73
74 debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len);
75
76 if (ext4fs_block_dev_desc == NULL) {
77 printf("** Invalid Block Device Descriptor (NULL)\n");
78 return 0;
79 }
80
81 if (byte_offset != 0) {
82 /* read first part which isn't aligned with start of sector */
83 if (ext4fs_block_dev_desc->
84 block_read(ext4fs_block_dev_desc->dev,
Rob Herring752e1bd2012-08-23 11:31:46 +000085 part_info->start + sector, 1,
Uma Shankar71014b62012-05-25 21:21:44 +053086 (unsigned long *) sec_buf) != 1) {
87 printf(" ** ext2fs_devread() read error **\n");
88 return 0;
89 }
90 memcpy(buf, sec_buf + byte_offset,
91 min(SECTOR_SIZE - byte_offset, byte_len));
92 buf += min(SECTOR_SIZE - byte_offset, byte_len);
93 byte_len -= min(SECTOR_SIZE - byte_offset, byte_len);
94 sector++;
95 }
96
97 if (byte_len == 0)
98 return 1;
99
100 /* read sector aligned part */
101 block_len = byte_len & ~(SECTOR_SIZE - 1);
102
103 if (block_len == 0) {
Stephen Warren07639f22012-09-18 08:05:28 +0000104 ALLOC_CACHE_ALIGN_BUFFER(u8, p, SECTOR_SIZE);
Uma Shankar71014b62012-05-25 21:21:44 +0530105
106 block_len = SECTOR_SIZE;
107 ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
Rob Herring752e1bd2012-08-23 11:31:46 +0000108 part_info->start + sector,
Uma Shankar71014b62012-05-25 21:21:44 +0530109 1, (unsigned long *)p);
110 memcpy(buf, p, byte_len);
111 return 1;
112 }
113
114 if (ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
Rob Herring752e1bd2012-08-23 11:31:46 +0000115 part_info->start + sector,
Uma Shankar71014b62012-05-25 21:21:44 +0530116 block_len / SECTOR_SIZE,
117 (unsigned long *) buf) !=
118 block_len / SECTOR_SIZE) {
119 printf(" ** %s read error - block\n", __func__);
120 return 0;
121 }
122 block_len = byte_len & ~(SECTOR_SIZE - 1);
123 buf += block_len;
124 byte_len -= block_len;
125 sector += block_len / SECTOR_SIZE;
126
127 if (byte_len != 0) {
128 /* read rest of data which are not in whole sector */
129 if (ext4fs_block_dev_desc->
130 block_read(ext4fs_block_dev_desc->dev,
Rob Herring752e1bd2012-08-23 11:31:46 +0000131 part_info->start + sector, 1,
Uma Shankar71014b62012-05-25 21:21:44 +0530132 (unsigned long *) sec_buf) != 1) {
133 printf("* %s read error - last part\n", __func__);
134 return 0;
135 }
136 memcpy(buf, sec_buf, byte_len);
137 }
138 return 1;
139}