dev.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * Changelog:
  34. * 0.1 - Newly created file for ext4fs support. Taken from
  35. * fs/ext2/dev.c file in uboot.
  36. */
  37. #include <common.h>
  38. #include <config.h>
  39. #include <ext_common.h>
  40. static block_dev_desc_t *ext4fs_block_dev_desc;
  41. static disk_partition_t part_info;
  42. int ext4fs_set_blk_dev(block_dev_desc_t *rbdd, int part)
  43. {
  44. ext4fs_block_dev_desc = rbdd;
  45. if (part == 0) {
  46. /* disk doesn't use partition table */
  47. part_info.start = 0;
  48. part_info.size = rbdd->lba;
  49. part_info.blksz = rbdd->blksz;
  50. } else {
  51. if (get_partition_info(ext4fs_block_dev_desc,
  52. part, &part_info))
  53. return 0;
  54. }
  55. return part_info.size;
  56. }
  57. int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf)
  58. {
  59. char sec_buf[SECTOR_SIZE];
  60. unsigned block_len;
  61. /* Check partition boundaries */
  62. if ((sector < 0)
  63. || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
  64. part_info.size)) {
  65. printf("%s read outside partition %d\n", __func__, sector);
  66. return 0;
  67. }
  68. /* Get the read to the beginning of a partition */
  69. sector += byte_offset >> SECTOR_BITS;
  70. byte_offset &= SECTOR_SIZE - 1;
  71. debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len);
  72. if (ext4fs_block_dev_desc == NULL) {
  73. printf("** Invalid Block Device Descriptor (NULL)\n");
  74. return 0;
  75. }
  76. if (byte_offset != 0) {
  77. /* read first part which isn't aligned with start of sector */
  78. if (ext4fs_block_dev_desc->
  79. block_read(ext4fs_block_dev_desc->dev,
  80. part_info.start + sector, 1,
  81. (unsigned long *) sec_buf) != 1) {
  82. printf(" ** ext2fs_devread() read error **\n");
  83. return 0;
  84. }
  85. memcpy(buf, sec_buf + byte_offset,
  86. min(SECTOR_SIZE - byte_offset, byte_len));
  87. buf += min(SECTOR_SIZE - byte_offset, byte_len);
  88. byte_len -= min(SECTOR_SIZE - byte_offset, byte_len);
  89. sector++;
  90. }
  91. if (byte_len == 0)
  92. return 1;
  93. /* read sector aligned part */
  94. block_len = byte_len & ~(SECTOR_SIZE - 1);
  95. if (block_len == 0) {
  96. u8 p[SECTOR_SIZE];
  97. block_len = SECTOR_SIZE;
  98. ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
  99. part_info.start + sector,
  100. 1, (unsigned long *)p);
  101. memcpy(buf, p, byte_len);
  102. return 1;
  103. }
  104. if (ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
  105. part_info.start + sector,
  106. block_len / SECTOR_SIZE,
  107. (unsigned long *) buf) !=
  108. block_len / SECTOR_SIZE) {
  109. printf(" ** %s read error - block\n", __func__);
  110. return 0;
  111. }
  112. block_len = byte_len & ~(SECTOR_SIZE - 1);
  113. buf += block_len;
  114. byte_len -= block_len;
  115. sector += block_len / SECTOR_SIZE;
  116. if (byte_len != 0) {
  117. /* read rest of data which are not in whole sector */
  118. if (ext4fs_block_dev_desc->
  119. block_read(ext4fs_block_dev_desc->dev,
  120. part_info.start + sector, 1,
  121. (unsigned long *) sec_buf) != 1) {
  122. printf("* %s read error - last part\n", __func__);
  123. return 0;
  124. }
  125. memcpy(buf, sec_buf, byte_len);
  126. }
  127. return 1;
  128. }