ext4fs.c 5.6 KB

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