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> |
| 41 | #include <ext_common.h> |
| 42 | |
| 43 | static block_dev_desc_t *ext4fs_block_dev_desc; |
| 44 | static disk_partition_t part_info; |
| 45 | |
| 46 | int ext4fs_set_blk_dev(block_dev_desc_t *rbdd, int part) |
| 47 | { |
| 48 | ext4fs_block_dev_desc = rbdd; |
| 49 | |
| 50 | if (part == 0) { |
| 51 | /* disk doesn't use partition table */ |
| 52 | part_info.start = 0; |
| 53 | part_info.size = rbdd->lba; |
| 54 | part_info.blksz = rbdd->blksz; |
| 55 | } else { |
| 56 | if (get_partition_info(ext4fs_block_dev_desc, |
| 57 | part, &part_info)) |
| 58 | return 0; |
| 59 | } |
| 60 | return part_info.size; |
| 61 | } |
| 62 | |
| 63 | int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf) |
| 64 | { |
| 65 | char sec_buf[SECTOR_SIZE]; |
| 66 | unsigned block_len; |
| 67 | |
| 68 | /* Check partition boundaries */ |
| 69 | if ((sector < 0) |
| 70 | || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >= |
| 71 | part_info.size)) { |
| 72 | printf("%s read outside partition %d\n", __func__, sector); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* Get the read to the beginning of a partition */ |
| 77 | sector += byte_offset >> SECTOR_BITS; |
| 78 | byte_offset &= SECTOR_SIZE - 1; |
| 79 | |
| 80 | debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len); |
| 81 | |
| 82 | if (ext4fs_block_dev_desc == NULL) { |
| 83 | printf("** Invalid Block Device Descriptor (NULL)\n"); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 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, |
| 91 | part_info.start + sector, 1, |
| 92 | (unsigned long *) sec_buf) != 1) { |
| 93 | printf(" ** ext2fs_devread() read error **\n"); |
| 94 | return 0; |
| 95 | } |
| 96 | memcpy(buf, sec_buf + byte_offset, |
| 97 | min(SECTOR_SIZE - byte_offset, byte_len)); |
| 98 | buf += min(SECTOR_SIZE - byte_offset, byte_len); |
| 99 | byte_len -= min(SECTOR_SIZE - byte_offset, byte_len); |
| 100 | sector++; |
| 101 | } |
| 102 | |
| 103 | if (byte_len == 0) |
| 104 | return 1; |
| 105 | |
| 106 | /* read sector aligned part */ |
| 107 | block_len = byte_len & ~(SECTOR_SIZE - 1); |
| 108 | |
| 109 | if (block_len == 0) { |
| 110 | u8 p[SECTOR_SIZE]; |
| 111 | |
| 112 | block_len = SECTOR_SIZE; |
| 113 | ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev, |
| 114 | part_info.start + sector, |
| 115 | 1, (unsigned long *)p); |
| 116 | memcpy(buf, p, byte_len); |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | if (ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev, |
| 121 | part_info.start + sector, |
| 122 | block_len / SECTOR_SIZE, |
| 123 | (unsigned long *) buf) != |
| 124 | block_len / SECTOR_SIZE) { |
| 125 | printf(" ** %s read error - block\n", __func__); |
| 126 | return 0; |
| 127 | } |
| 128 | block_len = byte_len & ~(SECTOR_SIZE - 1); |
| 129 | buf += block_len; |
| 130 | byte_len -= block_len; |
| 131 | sector += block_len / SECTOR_SIZE; |
| 132 | |
| 133 | if (byte_len != 0) { |
| 134 | /* read rest of data which are not in whole sector */ |
| 135 | if (ext4fs_block_dev_desc-> |
| 136 | block_read(ext4fs_block_dev_desc->dev, |
| 137 | part_info.start + sector, 1, |
| 138 | (unsigned long *) sec_buf) != 1) { |
| 139 | printf("* %s read error - last part\n", __func__); |
| 140 | return 0; |
| 141 | } |
| 142 | memcpy(buf, sec_buf, byte_len); |
| 143 | } |
| 144 | return 1; |
| 145 | } |