dev.c 3.9 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. * SPDX-License-Identifier: GPL-2.0+
  18. */
  19. /*
  20. * Changelog:
  21. * 0.1 - Newly created file for ext4fs support. Taken from
  22. * fs/ext2/dev.c file in uboot.
  23. */
  24. #include <common.h>
  25. #include <config.h>
  26. #include <ext4fs.h>
  27. #include <ext_common.h>
  28. #include "ext4_common.h"
  29. lbaint_t part_offset;
  30. static block_dev_desc_t *ext4fs_block_dev_desc;
  31. static disk_partition_t *part_info;
  32. void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
  33. {
  34. assert(rbdd->blksz == (1 << rbdd->log2blksz));
  35. ext4fs_block_dev_desc = rbdd;
  36. get_fs()->dev_desc = rbdd;
  37. part_info = info;
  38. part_offset = info->start;
  39. get_fs()->total_sect = ((uint64_t)info->size * info->blksz) >>
  40. get_fs()->dev_desc->log2blksz;
  41. }
  42. int ext4fs_devread(lbaint_t sector, int byte_offset, int byte_len, char *buf)
  43. {
  44. unsigned block_len;
  45. int log2blksz = ext4fs_block_dev_desc->log2blksz;
  46. ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, (ext4fs_block_dev_desc ?
  47. ext4fs_block_dev_desc->blksz :
  48. 0));
  49. if (ext4fs_block_dev_desc == NULL) {
  50. printf("** Invalid Block Device Descriptor (NULL)\n");
  51. return 0;
  52. }
  53. /* Check partition boundaries */
  54. if ((sector < 0) ||
  55. ((sector + ((byte_offset + byte_len - 1) >> log2blksz))
  56. >= part_info->size)) {
  57. printf("%s read outside partition " LBAFU "\n", __func__,
  58. sector);
  59. return 0;
  60. }
  61. /* Get the read to the beginning of a partition */
  62. sector += byte_offset >> log2blksz;
  63. byte_offset &= ext4fs_block_dev_desc->blksz - 1;
  64. debug(" <" LBAFU ", %d, %d>\n", sector, byte_offset, byte_len);
  65. if (byte_offset != 0) {
  66. int readlen;
  67. /* read first part which isn't aligned with start of sector */
  68. if (ext4fs_block_dev_desc->
  69. block_read(ext4fs_block_dev_desc->dev,
  70. part_info->start + sector, 1,
  71. (unsigned long *) sec_buf) != 1) {
  72. printf(" ** ext2fs_devread() read error **\n");
  73. return 0;
  74. }
  75. readlen = min((int)ext4fs_block_dev_desc->blksz - byte_offset,
  76. byte_len);
  77. memcpy(buf, sec_buf + byte_offset, readlen);
  78. buf += readlen;
  79. byte_len -= readlen;
  80. sector++;
  81. }
  82. if (byte_len == 0)
  83. return 1;
  84. /* read sector aligned part */
  85. block_len = byte_len & ~(ext4fs_block_dev_desc->blksz - 1);
  86. if (block_len == 0) {
  87. ALLOC_CACHE_ALIGN_BUFFER(u8, p, ext4fs_block_dev_desc->blksz);
  88. block_len = ext4fs_block_dev_desc->blksz;
  89. ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
  90. part_info->start + sector,
  91. 1, (unsigned long *)p);
  92. memcpy(buf, p, byte_len);
  93. return 1;
  94. }
  95. if (ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
  96. part_info->start + sector,
  97. block_len >> log2blksz,
  98. (unsigned long *) buf) !=
  99. block_len >> log2blksz) {
  100. printf(" ** %s read error - block\n", __func__);
  101. return 0;
  102. }
  103. block_len = byte_len & ~(ext4fs_block_dev_desc->blksz - 1);
  104. buf += block_len;
  105. byte_len -= block_len;
  106. sector += block_len / ext4fs_block_dev_desc->blksz;
  107. if (byte_len != 0) {
  108. /* read rest of data which are not in whole sector */
  109. if (ext4fs_block_dev_desc->
  110. block_read(ext4fs_block_dev_desc->dev,
  111. part_info->start + sector, 1,
  112. (unsigned long *) sec_buf) != 1) {
  113. printf("* %s read error - last part\n", __func__);
  114. return 0;
  115. }
  116. memcpy(buf, sec_buf, byte_len);
  117. }
  118. return 1;
  119. }
  120. int ext4_read_superblock(char *buffer)
  121. {
  122. struct ext_filesystem *fs = get_fs();
  123. int sect = SUPERBLOCK_START >> fs->dev_desc->log2blksz;
  124. int off = SUPERBLOCK_START % fs->dev_desc->blksz;
  125. return ext4fs_devread(sect, off, SUPERBLOCK_SIZE,
  126. buffer);
  127. }