Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 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> |
Rob Herring | 752e1bd | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 41 | #include <ext4fs.h> |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 42 | #include <ext_common.h> |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 43 | #include "ext4_common.h" |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 44 | |
Rob Herring | 752e1bd | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 45 | unsigned long part_offset; |
| 46 | |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 47 | static block_dev_desc_t *ext4fs_block_dev_desc; |
Rob Herring | 752e1bd | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 48 | static disk_partition_t *part_info; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 49 | |
Rob Herring | 752e1bd | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 50 | void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 51 | { |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 52 | assert(rbdd->blksz == (1 << rbdd->log2blksz)); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 53 | ext4fs_block_dev_desc = rbdd; |
Stephen Warren | ceb785a | 2013-05-23 10:22:10 +0000 | [diff] [blame] | 54 | get_fs()->dev_desc = rbdd; |
Rob Herring | 752e1bd | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 55 | part_info = info; |
| 56 | part_offset = info->start; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 57 | get_fs()->total_sect = (info->size * info->blksz) >> |
| 58 | get_fs()->dev_desc->log2blksz; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf) |
| 62 | { |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 63 | unsigned block_len; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 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 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 72 | |
| 73 | /* Check partition boundaries */ |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 74 | if ((sector < 0) || |
| 75 | ((sector + ((byte_offset + byte_len - 1) >> log2blksz)) |
| 76 | >= part_info->size)) { |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 77 | printf("%s read outside partition %d\n", __func__, sector); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | /* Get the read to the beginning of a partition */ |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 82 | sector += byte_offset >> log2blksz; |
| 83 | byte_offset &= ext4fs_block_dev_desc->blksz - 1; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 84 | |
| 85 | debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len); |
| 86 | |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 87 | if (byte_offset != 0) { |
| 88 | /* read first part which isn't aligned with start of sector */ |
| 89 | if (ext4fs_block_dev_desc-> |
| 90 | block_read(ext4fs_block_dev_desc->dev, |
Rob Herring | 752e1bd | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 91 | part_info->start + sector, 1, |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 92 | (unsigned long *) sec_buf) != 1) { |
| 93 | printf(" ** ext2fs_devread() read error **\n"); |
| 94 | return 0; |
| 95 | } |
| 96 | memcpy(buf, sec_buf + byte_offset, |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 97 | min(ext4fs_block_dev_desc->blksz |
| 98 | - byte_offset, byte_len)); |
| 99 | buf += min(ext4fs_block_dev_desc->blksz |
| 100 | - byte_offset, byte_len); |
| 101 | byte_len -= min(ext4fs_block_dev_desc->blksz |
| 102 | - byte_offset, byte_len); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 103 | sector++; |
| 104 | } |
| 105 | |
| 106 | if (byte_len == 0) |
| 107 | return 1; |
| 108 | |
| 109 | /* read sector aligned part */ |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 110 | block_len = byte_len & ~(ext4fs_block_dev_desc->blksz - 1); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 111 | |
| 112 | if (block_len == 0) { |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 113 | ALLOC_CACHE_ALIGN_BUFFER(u8, p, ext4fs_block_dev_desc->blksz); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 114 | |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 115 | block_len = ext4fs_block_dev_desc->blksz; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 116 | ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev, |
Rob Herring | 752e1bd | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 117 | part_info->start + sector, |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 118 | 1, (unsigned long *)p); |
| 119 | memcpy(buf, p, byte_len); |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | if (ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev, |
Rob Herring | 752e1bd | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 124 | part_info->start + sector, |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 125 | block_len >> log2blksz, |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 126 | (unsigned long *) buf) != |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 127 | block_len >> log2blksz) { |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 128 | printf(" ** %s read error - block\n", __func__); |
| 129 | return 0; |
| 130 | } |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 131 | block_len = byte_len & ~(ext4fs_block_dev_desc->blksz - 1); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 132 | buf += block_len; |
| 133 | byte_len -= block_len; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 134 | sector += block_len / ext4fs_block_dev_desc->blksz; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 135 | |
| 136 | if (byte_len != 0) { |
| 137 | /* read rest of data which are not in whole sector */ |
| 138 | if (ext4fs_block_dev_desc-> |
| 139 | block_read(ext4fs_block_dev_desc->dev, |
Rob Herring | 752e1bd | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 140 | part_info->start + sector, 1, |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 141 | (unsigned long *) sec_buf) != 1) { |
| 142 | printf("* %s read error - last part\n", __func__); |
| 143 | return 0; |
| 144 | } |
| 145 | memcpy(buf, sec_buf, byte_len); |
| 146 | } |
| 147 | return 1; |
| 148 | } |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 149 | |
| 150 | int ext4_read_superblock(char *buffer) |
| 151 | { |
| 152 | struct ext_filesystem *fs = get_fs(); |
| 153 | int sect = SUPERBLOCK_START >> fs->dev_desc->log2blksz; |
| 154 | int off = SUPERBLOCK_START % fs->dev_desc->blksz; |
| 155 | |
| 156 | return ext4fs_devread(sect, off, SUPERBLOCK_SIZE, |
| 157 | buffer); |
| 158 | } |