]> git.ipfire.org Git - people/ms/u-boot.git/blame - fs/ext4/dev.c
Fix ext2/ext4 filesystem accesses beyond 2TiB
[people/ms/u-boot.git] / fs / ext4 / dev.c
CommitLineData
a1596438
US
1/*
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>
81180819 41#include <ext4fs.h>
a1596438 42#include <ext_common.h>
50ce4c07 43#include "ext4_common.h"
a1596438 44
04735e9c 45lbaint_t part_offset;
81180819 46
a1596438 47static block_dev_desc_t *ext4fs_block_dev_desc;
81180819 48static disk_partition_t *part_info;
a1596438 49
81180819 50void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
a1596438 51{
50ce4c07 52 assert(rbdd->blksz == (1 << rbdd->log2blksz));
a1596438 53 ext4fs_block_dev_desc = rbdd;
c28cbfa1 54 get_fs()->dev_desc = rbdd;
81180819
RH
55 part_info = info;
56 part_offset = info->start;
50ce4c07
EE
57 get_fs()->total_sect = (info->size * info->blksz) >>
58 get_fs()->dev_desc->log2blksz;
a1596438
US
59}
60
04735e9c 61int ext4fs_devread(lbaint_t sector, int byte_offset, int byte_len, char *buf)
a1596438 62{
a1596438 63 unsigned block_len;
50ce4c07
EE
64 int log2blksz = ext4fs_block_dev_desc->log2blksz;
65 ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (ext4fs_block_dev_desc ?
66 ext4fs_block_dev_desc->blksz :
67 0));
68 if (ext4fs_block_dev_desc == NULL) {
69 printf("** Invalid Block Device Descriptor (NULL)\n");
70 return 0;
71 }
a1596438
US
72
73 /* Check partition boundaries */
50ce4c07
EE
74 if ((sector < 0) ||
75 ((sector + ((byte_offset + byte_len - 1) >> log2blksz))
76 >= part_info->size)) {
04735e9c
FL
77 printf("%s read outside partition " LBAFU "\n", __func__,
78 sector);
a1596438
US
79 return 0;
80 }
81
82 /* Get the read to the beginning of a partition */
50ce4c07
EE
83 sector += byte_offset >> log2blksz;
84 byte_offset &= ext4fs_block_dev_desc->blksz - 1;
a1596438 85
04735e9c 86 debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len);
a1596438 87
a1596438
US
88 if (byte_offset != 0) {
89 /* read first part which isn't aligned with start of sector */
90 if (ext4fs_block_dev_desc->
91 block_read(ext4fs_block_dev_desc->dev,
81180819 92 part_info->start + sector, 1,
a1596438
US
93 (unsigned long *) sec_buf) != 1) {
94 printf(" ** ext2fs_devread() read error **\n");
95 return 0;
96 }
97 memcpy(buf, sec_buf + byte_offset,
50ce4c07
EE
98 min(ext4fs_block_dev_desc->blksz
99 - byte_offset, byte_len));
100 buf += min(ext4fs_block_dev_desc->blksz
101 - byte_offset, byte_len);
102 byte_len -= min(ext4fs_block_dev_desc->blksz
103 - byte_offset, byte_len);
a1596438
US
104 sector++;
105 }
106
107 if (byte_len == 0)
108 return 1;
109
110 /* read sector aligned part */
50ce4c07 111 block_len = byte_len & ~(ext4fs_block_dev_desc->blksz - 1);
a1596438
US
112
113 if (block_len == 0) {
50ce4c07 114 ALLOC_CACHE_ALIGN_BUFFER(u8, p, ext4fs_block_dev_desc->blksz);
a1596438 115
50ce4c07 116 block_len = ext4fs_block_dev_desc->blksz;
a1596438 117 ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
81180819 118 part_info->start + sector,
a1596438
US
119 1, (unsigned long *)p);
120 memcpy(buf, p, byte_len);
121 return 1;
122 }
123
124 if (ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
81180819 125 part_info->start + sector,
50ce4c07 126 block_len >> log2blksz,
a1596438 127 (unsigned long *) buf) !=
50ce4c07 128 block_len >> log2blksz) {
a1596438
US
129 printf(" ** %s read error - block\n", __func__);
130 return 0;
131 }
50ce4c07 132 block_len = byte_len & ~(ext4fs_block_dev_desc->blksz - 1);
a1596438
US
133 buf += block_len;
134 byte_len -= block_len;
50ce4c07 135 sector += block_len / ext4fs_block_dev_desc->blksz;
a1596438
US
136
137 if (byte_len != 0) {
138 /* read rest of data which are not in whole sector */
139 if (ext4fs_block_dev_desc->
140 block_read(ext4fs_block_dev_desc->dev,
81180819 141 part_info->start + sector, 1,
a1596438
US
142 (unsigned long *) sec_buf) != 1) {
143 printf("* %s read error - last part\n", __func__);
144 return 0;
145 }
146 memcpy(buf, sec_buf, byte_len);
147 }
148 return 1;
149}
50ce4c07
EE
150
151int ext4_read_superblock(char *buffer)
152{
153 struct ext_filesystem *fs = get_fs();
154 int sect = SUPERBLOCK_START >> fs->dev_desc->log2blksz;
155 int off = SUPERBLOCK_START % fs->dev_desc->blksz;
156
157 return ext4fs_devread(sect, off, SUPERBLOCK_SIZE,
158 buffer);
159}