dev.c 4.6 KB

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