ext4fs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2011 - 2012 Samsung Electronics
  4. * EXT4 filesystem implementation in Uboot by
  5. * Uma Shankar <uma.shankar@samsung.com>
  6. * Manjunatha C Achar <a.manjunatha@samsung.com>
  7. *
  8. * ext4ls and ext4load : Based on ext2 ls and load support in Uboot.
  9. * Ext4 read optimization taken from Open-Moko
  10. * Qi bootloader
  11. *
  12. * (C) Copyright 2004
  13. * esd gmbh <www.esd-electronics.com>
  14. * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
  15. *
  16. * based on code from grub2 fs/ext2.c and fs/fshelp.c by
  17. * GRUB -- GRand Unified Bootloader
  18. * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  19. *
  20. * ext4write : Based on generic ext4 protocol.
  21. */
  22. #include <common.h>
  23. #include <ext_common.h>
  24. #include <ext4fs.h>
  25. #include "ext4_common.h"
  26. #include <div64.h>
  27. #include <malloc.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. char *start_buf = buf;
  61. short status;
  62. struct ext_block_cache cache;
  63. ext_cache_init(&cache);
  64. /* Adjust len so it we can't read past the end of the file. */
  65. if (len + pos > filesize)
  66. len = (filesize - pos);
  67. if (blocksize <= 0 || len <= 0) {
  68. ext_cache_fini(&cache);
  69. return -1;
  70. }
  71. blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
  72. for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
  73. long int blknr;
  74. int blockoff = pos - (blocksize * i);
  75. int blockend = blocksize;
  76. int skipfirst = 0;
  77. blknr = read_allocated_block(&node->inode, i, &cache);
  78. if (blknr < 0) {
  79. ext_cache_fini(&cache);
  80. return -1;
  81. }
  82. blknr = blknr << log2_fs_blocksize;
  83. /* Last block. */
  84. if (i == blockcnt - 1) {
  85. blockend = (len + pos) - (blocksize * i);
  86. /* The last portion is exactly blocksize. */
  87. if (!blockend)
  88. blockend = blocksize;
  89. }
  90. /* First block. */
  91. if (i == lldiv(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 >> log2blksz;
  101. } else { /* spill */
  102. status = ext4fs_devread(delayed_start,
  103. delayed_skipfirst,
  104. delayed_extent,
  105. delayed_buf);
  106. if (status == 0) {
  107. ext_cache_fini(&cache);
  108. return -1;
  109. }
  110. previous_block_number = blknr;
  111. delayed_start = blknr;
  112. delayed_extent = blockend;
  113. delayed_skipfirst = skipfirst;
  114. delayed_buf = buf;
  115. delayed_next = blknr +
  116. (blockend >> log2blksz);
  117. }
  118. } else {
  119. previous_block_number = blknr;
  120. delayed_start = blknr;
  121. delayed_extent = blockend;
  122. delayed_skipfirst = skipfirst;
  123. delayed_buf = buf;
  124. delayed_next = blknr +
  125. (blockend >> log2blksz);
  126. }
  127. } else {
  128. int n;
  129. int n_left;
  130. if (previous_block_number != -1) {
  131. /* spill */
  132. status = ext4fs_devread(delayed_start,
  133. delayed_skipfirst,
  134. delayed_extent,
  135. delayed_buf);
  136. if (status == 0) {
  137. ext_cache_fini(&cache);
  138. return -1;
  139. }
  140. previous_block_number = -1;
  141. }
  142. /* Zero no more than `len' bytes. */
  143. n = blocksize - skipfirst;
  144. n_left = len - ( buf - start_buf );
  145. if (n > n_left)
  146. n = n_left;
  147. memset(buf, 0, n);
  148. }
  149. buf += blocksize - skipfirst;
  150. }
  151. if (previous_block_number != -1) {
  152. /* spill */
  153. status = ext4fs_devread(delayed_start,
  154. delayed_skipfirst, delayed_extent,
  155. delayed_buf);
  156. if (status == 0) {
  157. ext_cache_fini(&cache);
  158. return -1;
  159. }
  160. previous_block_number = -1;
  161. }
  162. *actread = len;
  163. ext_cache_fini(&cache);
  164. return 0;
  165. }
  166. int ext4fs_ls(const char *dirname)
  167. {
  168. struct ext2fs_node *dirnode = NULL;
  169. int status;
  170. if (dirname == NULL)
  171. return 0;
  172. status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
  173. FILETYPE_DIRECTORY);
  174. if (status != 1) {
  175. printf("** Can not find directory. **\n");
  176. if (dirnode)
  177. ext4fs_free_node(dirnode, &ext4fs_root->diropen);
  178. return 1;
  179. }
  180. ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
  181. ext4fs_free_node(dirnode, &ext4fs_root->diropen);
  182. return 0;
  183. }
  184. int ext4fs_exists(const char *filename)
  185. {
  186. loff_t file_len;
  187. int ret;
  188. ret = ext4fs_open(filename, &file_len);
  189. return ret == 0;
  190. }
  191. int ext4fs_size(const char *filename, loff_t *size)
  192. {
  193. return ext4fs_open(filename, size);
  194. }
  195. int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
  196. {
  197. if (ext4fs_root == NULL || ext4fs_file == NULL)
  198. return -1;
  199. return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
  200. }
  201. int ext4fs_probe(struct blk_desc *fs_dev_desc,
  202. disk_partition_t *fs_partition)
  203. {
  204. ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
  205. if (!ext4fs_mount(fs_partition->size)) {
  206. ext4fs_close();
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
  212. loff_t *len_read)
  213. {
  214. loff_t file_len;
  215. int ret;
  216. ret = ext4fs_open(filename, &file_len);
  217. if (ret < 0) {
  218. printf("** File not found %s **\n", filename);
  219. return -1;
  220. }
  221. if (len == 0)
  222. len = file_len;
  223. return ext4fs_read(buf, offset, len, len_read);
  224. }
  225. int ext4fs_uuid(char *uuid_str)
  226. {
  227. if (ext4fs_root == NULL)
  228. return -1;
  229. #ifdef CONFIG_LIB_UUID
  230. uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
  231. uuid_str, UUID_STR_FORMAT_STD);
  232. return 0;
  233. #else
  234. return -ENOSYS;
  235. #endif
  236. }
  237. void ext_cache_init(struct ext_block_cache *cache)
  238. {
  239. memset(cache, 0, sizeof(*cache));
  240. }
  241. void ext_cache_fini(struct ext_block_cache *cache)
  242. {
  243. free(cache->buf);
  244. ext_cache_init(cache);
  245. }
  246. int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
  247. {
  248. /* This could be more lenient, but this is simple and enough for now */
  249. if (cache->buf && cache->block == block && cache->size == size)
  250. return 1;
  251. ext_cache_fini(cache);
  252. cache->buf = memalign(ARCH_DMA_MINALIGN, size);
  253. if (!cache->buf)
  254. return 0;
  255. if (!ext4fs_devread(block, 0, size, cache->buf)) {
  256. ext_cache_fini(cache);
  257. return 0;
  258. }
  259. cache->block = block;
  260. cache->size = size;
  261. return 1;
  262. }