ext4fs.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * ext4ls and ext4load : Based on ext2 ls and load support in Uboot.
  8. * Ext4 read optimization taken from Open-Moko
  9. * Qi bootloader
  10. *
  11. * (C) Copyright 2004
  12. * esd gmbh <www.esd-electronics.com>
  13. * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
  14. *
  15. * based on code from grub2 fs/ext2.c and fs/fshelp.c by
  16. * GRUB -- GRand Unified Bootloader
  17. * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  18. *
  19. * ext4write : Based on generic ext4 protocol.
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License as published by
  23. * the Free Software Foundation; either version 2 of the License, or
  24. * (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. */
  35. #include <common.h>
  36. #include <ext_common.h>
  37. #include <ext4fs.h>
  38. #include "ext4_common.h"
  39. int ext4fs_symlinknest;
  40. struct ext_filesystem ext_fs;
  41. struct ext_filesystem *get_fs(void)
  42. {
  43. return &ext_fs;
  44. }
  45. void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
  46. {
  47. if ((node != &ext4fs_root->diropen) && (node != currroot))
  48. free(node);
  49. }
  50. /*
  51. * Taken from openmoko-kernel mailing list: By Andy green
  52. * Optimized read file API : collects and defers contiguous sector
  53. * reads into one potentially more efficient larger sequential read action
  54. */
  55. int ext4fs_read_file(struct ext2fs_node *node, int pos,
  56. unsigned int len, char *buf)
  57. {
  58. int i;
  59. int blockcnt;
  60. int log2blocksize = LOG2_EXT2_BLOCK_SIZE(node->data);
  61. int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
  62. unsigned int filesize = __le32_to_cpu(node->inode.size);
  63. int previous_block_number = -1;
  64. int delayed_start = 0;
  65. int delayed_extent = 0;
  66. int delayed_skipfirst = 0;
  67. int delayed_next = 0;
  68. char *delayed_buf = NULL;
  69. short status;
  70. /* Adjust len so it we can't read past the end of the file. */
  71. if (len > filesize)
  72. len = filesize;
  73. blockcnt = ((len + pos) + blocksize - 1) / blocksize;
  74. for (i = pos / blocksize; i < blockcnt; i++) {
  75. int blknr;
  76. int blockoff = pos % blocksize;
  77. int blockend = blocksize;
  78. int skipfirst = 0;
  79. blknr = read_allocated_block(&(node->inode), i);
  80. if (blknr < 0)
  81. return -1;
  82. blknr = blknr << log2blocksize;
  83. /* Last block. */
  84. if (i == blockcnt - 1) {
  85. blockend = (len + pos) % blocksize;
  86. /* The last portion is exactly blocksize. */
  87. if (!blockend)
  88. blockend = blocksize;
  89. }
  90. /* First block. */
  91. if (i == pos / blocksize) {
  92. skipfirst = blockoff;
  93. blockend -= skipfirst;
  94. }
  95. if (blknr) {
  96. int status;
  97. if (previous_block_number != -1) {
  98. if (delayed_next == blknr) {
  99. delayed_extent += blockend;
  100. delayed_next += blockend >> SECTOR_BITS;
  101. } else { /* spill */
  102. status = ext4fs_devread(delayed_start,
  103. delayed_skipfirst,
  104. delayed_extent,
  105. delayed_buf);
  106. if (status == 0)
  107. return -1;
  108. previous_block_number = blknr;
  109. delayed_start = blknr;
  110. delayed_extent = blockend;
  111. delayed_skipfirst = skipfirst;
  112. delayed_buf = buf;
  113. delayed_next = blknr +
  114. (blockend >> SECTOR_BITS);
  115. }
  116. } else {
  117. previous_block_number = blknr;
  118. delayed_start = blknr;
  119. delayed_extent = blockend;
  120. delayed_skipfirst = skipfirst;
  121. delayed_buf = buf;
  122. delayed_next = blknr +
  123. (blockend >> SECTOR_BITS);
  124. }
  125. } else {
  126. if (previous_block_number != -1) {
  127. /* spill */
  128. status = ext4fs_devread(delayed_start,
  129. delayed_skipfirst,
  130. delayed_extent,
  131. delayed_buf);
  132. if (status == 0)
  133. return -1;
  134. previous_block_number = -1;
  135. }
  136. memset(buf, 0, blocksize - skipfirst);
  137. }
  138. buf += blocksize - skipfirst;
  139. }
  140. if (previous_block_number != -1) {
  141. /* spill */
  142. status = ext4fs_devread(delayed_start,
  143. delayed_skipfirst, delayed_extent,
  144. delayed_buf);
  145. if (status == 0)
  146. return -1;
  147. previous_block_number = -1;
  148. }
  149. return len;
  150. }
  151. int ext4fs_ls(const char *dirname)
  152. {
  153. struct ext2fs_node *dirnode;
  154. int status;
  155. if (dirname == NULL)
  156. return 0;
  157. status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
  158. FILETYPE_DIRECTORY);
  159. if (status != 1) {
  160. printf("** Can not find directory. **\n");
  161. return 1;
  162. }
  163. ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
  164. ext4fs_free_node(dirnode, &ext4fs_root->diropen);
  165. return 0;
  166. }
  167. int ext4fs_read(char *buf, unsigned len)
  168. {
  169. if (ext4fs_root == NULL || ext4fs_file == NULL)
  170. return 0;
  171. return ext4fs_read_file(ext4fs_file, 0, len, buf);
  172. }