dev.c 3.7 KB

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