dev.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 + ((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 (blk_dread(ext4fs_blk_desc, part_info->start + sector, 1,
  70. (void *)sec_buf) != 1) {
  71. printf(" ** ext2fs_devread() read error **\n");
  72. return 0;
  73. }
  74. readlen = min((int)ext4fs_blk_desc->blksz - byte_offset,
  75. byte_len);
  76. memcpy(buf, sec_buf + byte_offset, readlen);
  77. buf += readlen;
  78. byte_len -= readlen;
  79. sector++;
  80. }
  81. if (byte_len == 0)
  82. return 1;
  83. /* read sector aligned part */
  84. block_len = byte_len & ~(ext4fs_blk_desc->blksz - 1);
  85. if (block_len == 0) {
  86. ALLOC_CACHE_ALIGN_BUFFER(u8, p, ext4fs_blk_desc->blksz);
  87. block_len = ext4fs_blk_desc->blksz;
  88. blk_dread(ext4fs_blk_desc, part_info->start + sector, 1,
  89. (void *)p);
  90. memcpy(buf, p, byte_len);
  91. return 1;
  92. }
  93. if (blk_dread(ext4fs_blk_desc, part_info->start + sector,
  94. block_len >> log2blksz, (void *)buf) !=
  95. block_len >> log2blksz) {
  96. printf(" ** %s read error - block\n", __func__);
  97. return 0;
  98. }
  99. block_len = byte_len & ~(ext4fs_blk_desc->blksz - 1);
  100. buf += block_len;
  101. byte_len -= block_len;
  102. sector += block_len / ext4fs_blk_desc->blksz;
  103. if (byte_len != 0) {
  104. /* read rest of data which are not in whole sector */
  105. if (blk_dread(ext4fs_blk_desc, part_info->start + sector, 1,
  106. (void *)sec_buf) != 1) {
  107. printf("* %s read error - last part\n", __func__);
  108. return 0;
  109. }
  110. memcpy(buf, sec_buf, byte_len);
  111. }
  112. return 1;
  113. }
  114. int ext4_read_superblock(char *buffer)
  115. {
  116. struct ext_filesystem *fs = get_fs();
  117. int sect = SUPERBLOCK_START >> fs->dev_desc->log2blksz;
  118. int off = SUPERBLOCK_START % fs->dev_desc->blksz;
  119. return ext4fs_devread(sect, off, SUPERBLOCK_SIZE,
  120. buffer);
  121. }