dev.c 3.6 KB

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