Uma Shankar | a159643 | 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 | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 41 | #include <ext4fs.h> |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 42 | #include <ext_common.h> |
| 43 | |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 44 | unsigned long part_offset; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 45 | |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 46 | static block_dev_desc_t *ext4fs_block_dev_desc; |
| 47 | static disk_partition_t *part_info; |
| 48 | |
| 49 | void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info) |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 50 | { |
| 51 | ext4fs_block_dev_desc = rbdd; |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 52 | part_info = info; |
| 53 | part_offset = info->start; |
| 54 | get_fs()->total_sect = (info->size * info->blksz) / SECTOR_SIZE; |
Ćukasz Majewski | a1f41f1 | 2012-12-05 08:06:38 +0000 | [diff] [blame] | 55 | get_fs()->dev_desc = rbdd; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf) |
| 59 | { |
Stephen Warren | 55b523b | 2012-09-18 08:05:28 +0000 | [diff] [blame] | 60 | ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, SECTOR_SIZE); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 61 | unsigned block_len; |
| 62 | |
| 63 | /* Check partition boundaries */ |
| 64 | if ((sector < 0) |
| 65 | || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >= |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 66 | part_info->size)) { |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 67 | printf("%s read outside partition %d\n", __func__, sector); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /* Get the read to the beginning of a partition */ |
| 72 | sector += byte_offset >> SECTOR_BITS; |
| 73 | byte_offset &= SECTOR_SIZE - 1; |
| 74 | |
| 75 | debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len); |
| 76 | |
| 77 | if (ext4fs_block_dev_desc == NULL) { |
| 78 | printf("** Invalid Block Device Descriptor (NULL)\n"); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | if (byte_offset != 0) { |
| 83 | /* read first part which isn't aligned with start of sector */ |
| 84 | if (ext4fs_block_dev_desc-> |
| 85 | block_read(ext4fs_block_dev_desc->dev, |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 86 | part_info->start + sector, 1, |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 87 | (unsigned long *) sec_buf) != 1) { |
| 88 | printf(" ** ext2fs_devread() read error **\n"); |
| 89 | return 0; |
| 90 | } |
| 91 | memcpy(buf, sec_buf + byte_offset, |
| 92 | min(SECTOR_SIZE - byte_offset, byte_len)); |
| 93 | buf += min(SECTOR_SIZE - byte_offset, byte_len); |
| 94 | byte_len -= min(SECTOR_SIZE - byte_offset, byte_len); |
| 95 | sector++; |
| 96 | } |
| 97 | |
| 98 | if (byte_len == 0) |
| 99 | return 1; |
| 100 | |
| 101 | /* read sector aligned part */ |
| 102 | block_len = byte_len & ~(SECTOR_SIZE - 1); |
| 103 | |
| 104 | if (block_len == 0) { |
Stephen Warren | 55b523b | 2012-09-18 08:05:28 +0000 | [diff] [blame] | 105 | ALLOC_CACHE_ALIGN_BUFFER(u8, p, SECTOR_SIZE); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 106 | |
| 107 | block_len = SECTOR_SIZE; |
| 108 | ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev, |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 109 | part_info->start + sector, |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 110 | 1, (unsigned long *)p); |
| 111 | memcpy(buf, p, byte_len); |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | if (ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev, |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 116 | part_info->start + sector, |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 117 | block_len / SECTOR_SIZE, |
| 118 | (unsigned long *) buf) != |
| 119 | block_len / SECTOR_SIZE) { |
| 120 | printf(" ** %s read error - block\n", __func__); |
| 121 | return 0; |
| 122 | } |
| 123 | block_len = byte_len & ~(SECTOR_SIZE - 1); |
| 124 | buf += block_len; |
| 125 | byte_len -= block_len; |
| 126 | sector += block_len / SECTOR_SIZE; |
| 127 | |
| 128 | if (byte_len != 0) { |
| 129 | /* read rest of data which are not in whole sector */ |
| 130 | if (ext4fs_block_dev_desc-> |
| 131 | block_read(ext4fs_block_dev_desc->dev, |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 132 | part_info->start + sector, 1, |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 133 | (unsigned long *) sec_buf) != 1) { |
| 134 | printf("* %s read error - last part\n", __func__); |
| 135 | return 0; |
| 136 | } |
| 137 | memcpy(buf, sec_buf, byte_len); |
| 138 | } |
| 139 | return 1; |
| 140 | } |