ext4fs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. * SPDX-License-Identifier: GPL-2.0+
  22. */
  23. #include <common.h>
  24. #include <ext_common.h>
  25. #include <ext4fs.h>
  26. #include "ext4_common.h"
  27. int ext4fs_symlinknest;
  28. struct ext_filesystem ext_fs;
  29. struct ext_filesystem *get_fs(void)
  30. {
  31. return &ext_fs;
  32. }
  33. void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
  34. {
  35. if ((node != &ext4fs_root->diropen) && (node != currroot))
  36. free(node);
  37. }
  38. /*
  39. * Taken from openmoko-kernel mailing list: By Andy green
  40. * Optimized read file API : collects and defers contiguous sector
  41. * reads into one potentially more efficient larger sequential read action
  42. */
  43. int ext4fs_read_file(struct ext2fs_node *node, int pos,
  44. unsigned int len, char *buf)
  45. {
  46. struct ext_filesystem *fs = get_fs();
  47. int i;
  48. lbaint_t blockcnt;
  49. int log2blksz = fs->dev_desc->log2blksz;
  50. int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz;
  51. int blocksize = (1 << (log2_fs_blocksize + log2blksz));
  52. unsigned int filesize = __le32_to_cpu(node->inode.size);
  53. lbaint_t previous_block_number = -1;
  54. lbaint_t delayed_start = 0;
  55. lbaint_t delayed_extent = 0;
  56. lbaint_t delayed_skipfirst = 0;
  57. lbaint_t delayed_next = 0;
  58. char *delayed_buf = NULL;
  59. short status;
  60. /* Adjust len so it we can't read past the end of the file. */
  61. if (len > filesize)
  62. len = filesize;
  63. blockcnt = ((len + pos) + blocksize - 1) / blocksize;
  64. for (i = pos / blocksize; i < blockcnt; i++) {
  65. lbaint_t blknr;
  66. int blockoff = pos % blocksize;
  67. int blockend = blocksize;
  68. int skipfirst = 0;
  69. blknr = read_allocated_block(&(node->inode), i);
  70. if (blknr < 0)
  71. return -1;
  72. blknr = blknr << log2_fs_blocksize;
  73. /* Last block. */
  74. if (i == blockcnt - 1) {
  75. blockend = (len + pos) % blocksize;
  76. /* The last portion is exactly blocksize. */
  77. if (!blockend)
  78. blockend = blocksize;
  79. }
  80. /* First block. */
  81. if (i == pos / blocksize) {
  82. skipfirst = blockoff;
  83. blockend -= skipfirst;
  84. }
  85. if (blknr) {
  86. int status;
  87. if (previous_block_number != -1) {
  88. if (delayed_next == blknr) {
  89. delayed_extent += blockend;
  90. delayed_next += blockend >> log2blksz;
  91. } else { /* spill */
  92. status = ext4fs_devread(delayed_start,
  93. delayed_skipfirst,
  94. delayed_extent,
  95. delayed_buf);
  96. if (status == 0)
  97. return -1;
  98. previous_block_number = blknr;
  99. delayed_start = blknr;
  100. delayed_extent = blockend;
  101. delayed_skipfirst = skipfirst;
  102. delayed_buf = buf;
  103. delayed_next = blknr +
  104. (blockend >> log2blksz);
  105. }
  106. } else {
  107. previous_block_number = blknr;
  108. delayed_start = blknr;
  109. delayed_extent = blockend;
  110. delayed_skipfirst = skipfirst;
  111. delayed_buf = buf;
  112. delayed_next = blknr +
  113. (blockend >> log2blksz);
  114. }
  115. } else {
  116. if (previous_block_number != -1) {
  117. /* spill */
  118. status = ext4fs_devread(delayed_start,
  119. delayed_skipfirst,
  120. delayed_extent,
  121. delayed_buf);
  122. if (status == 0)
  123. return -1;
  124. previous_block_number = -1;
  125. }
  126. memset(buf, 0, blocksize - skipfirst);
  127. }
  128. buf += blocksize - skipfirst;
  129. }
  130. if (previous_block_number != -1) {
  131. /* spill */
  132. status = ext4fs_devread(delayed_start,
  133. delayed_skipfirst, delayed_extent,
  134. delayed_buf);
  135. if (status == 0)
  136. return -1;
  137. previous_block_number = -1;
  138. }
  139. return len;
  140. }
  141. int ext4fs_ls(const char *dirname)
  142. {
  143. struct ext2fs_node *dirnode;
  144. int status;
  145. if (dirname == NULL)
  146. return 0;
  147. status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
  148. FILETYPE_DIRECTORY);
  149. if (status != 1) {
  150. printf("** Can not find directory. **\n");
  151. return 1;
  152. }
  153. ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
  154. ext4fs_free_node(dirnode, &ext4fs_root->diropen);
  155. return 0;
  156. }
  157. int ext4fs_exists(const char *filename)
  158. {
  159. int file_len;
  160. file_len = ext4fs_open(filename);
  161. return file_len >= 0;
  162. }
  163. int ext4fs_size(const char *filename)
  164. {
  165. return ext4fs_open(filename);
  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. }
  173. int ext4fs_probe(block_dev_desc_t *fs_dev_desc,
  174. disk_partition_t *fs_partition)
  175. {
  176. ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
  177. if (!ext4fs_mount(fs_partition->size)) {
  178. ext4fs_close();
  179. return -1;
  180. }
  181. return 0;
  182. }
  183. int ext4_read_file(const char *filename, void *buf, int offset, int len)
  184. {
  185. int file_len;
  186. int len_read;
  187. if (offset != 0) {
  188. printf("** Cannot support non-zero offset **\n");
  189. return -1;
  190. }
  191. file_len = ext4fs_open(filename);
  192. if (file_len < 0) {
  193. printf("** File not found %s **\n", filename);
  194. return -1;
  195. }
  196. if (len == 0)
  197. len = file_len;
  198. len_read = ext4fs_read(buf, len);
  199. return len_read;
  200. }