ext4fs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #include <uuid.h>
  29. int ext4fs_symlinknest;
  30. struct ext_filesystem ext_fs;
  31. struct ext_filesystem *get_fs(void)
  32. {
  33. return &ext_fs;
  34. }
  35. void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
  36. {
  37. if ((node != &ext4fs_root->diropen) && (node != currroot))
  38. free(node);
  39. }
  40. /*
  41. * Taken from openmoko-kernel mailing list: By Andy green
  42. * Optimized read file API : collects and defers contiguous sector
  43. * reads into one potentially more efficient larger sequential read action
  44. */
  45. int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
  46. loff_t len, char *buf, loff_t *actread)
  47. {
  48. struct ext_filesystem *fs = get_fs();
  49. int i;
  50. lbaint_t blockcnt;
  51. int log2blksz = fs->dev_desc->log2blksz;
  52. int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz;
  53. int blocksize = (1 << (log2_fs_blocksize + log2blksz));
  54. unsigned int filesize = le32_to_cpu(node->inode.size);
  55. lbaint_t previous_block_number = -1;
  56. lbaint_t delayed_start = 0;
  57. lbaint_t delayed_extent = 0;
  58. lbaint_t delayed_skipfirst = 0;
  59. lbaint_t delayed_next = 0;
  60. char *delayed_buf = NULL;
  61. char *start_buf = buf;
  62. short status;
  63. struct ext_block_cache cache;
  64. ext_cache_init(&cache);
  65. /* Adjust len so it we can't read past the end of the file. */
  66. if (len + pos > filesize)
  67. len = (filesize - pos);
  68. if (blocksize <= 0 || len <= 0) {
  69. ext_cache_fini(&cache);
  70. return -1;
  71. }
  72. blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
  73. for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
  74. long int blknr;
  75. int blockoff = pos - (blocksize * i);
  76. int blockend = blocksize;
  77. int skipfirst = 0;
  78. blknr = read_allocated_block(&node->inode, i, &cache);
  79. if (blknr < 0) {
  80. ext_cache_fini(&cache);
  81. return -1;
  82. }
  83. blknr = blknr << log2_fs_blocksize;
  84. /* Last block. */
  85. if (i == blockcnt - 1) {
  86. blockend = (len + pos) - (blocksize * i);
  87. /* The last portion is exactly blocksize. */
  88. if (!blockend)
  89. blockend = blocksize;
  90. }
  91. /* First block. */
  92. if (i == lldiv(pos, blocksize)) {
  93. skipfirst = blockoff;
  94. blockend -= skipfirst;
  95. }
  96. if (blknr) {
  97. int status;
  98. if (previous_block_number != -1) {
  99. if (delayed_next == blknr) {
  100. delayed_extent += blockend;
  101. delayed_next += blockend >> log2blksz;
  102. } else { /* spill */
  103. status = ext4fs_devread(delayed_start,
  104. delayed_skipfirst,
  105. delayed_extent,
  106. delayed_buf);
  107. if (status == 0) {
  108. ext_cache_fini(&cache);
  109. return -1;
  110. }
  111. previous_block_number = blknr;
  112. delayed_start = blknr;
  113. delayed_extent = blockend;
  114. delayed_skipfirst = skipfirst;
  115. delayed_buf = buf;
  116. delayed_next = blknr +
  117. (blockend >> log2blksz);
  118. }
  119. } else {
  120. previous_block_number = blknr;
  121. delayed_start = blknr;
  122. delayed_extent = blockend;
  123. delayed_skipfirst = skipfirst;
  124. delayed_buf = buf;
  125. delayed_next = blknr +
  126. (blockend >> log2blksz);
  127. }
  128. } else {
  129. int n;
  130. int n_left;
  131. if (previous_block_number != -1) {
  132. /* spill */
  133. status = ext4fs_devread(delayed_start,
  134. delayed_skipfirst,
  135. delayed_extent,
  136. delayed_buf);
  137. if (status == 0) {
  138. ext_cache_fini(&cache);
  139. return -1;
  140. }
  141. previous_block_number = -1;
  142. }
  143. /* Zero no more than `len' bytes. */
  144. n = blocksize - skipfirst;
  145. n_left = len - ( buf - start_buf );
  146. if (n > n_left)
  147. n = n_left;
  148. memset(buf, 0, n);
  149. }
  150. buf += blocksize - skipfirst;
  151. }
  152. if (previous_block_number != -1) {
  153. /* spill */
  154. status = ext4fs_devread(delayed_start,
  155. delayed_skipfirst, delayed_extent,
  156. delayed_buf);
  157. if (status == 0) {
  158. ext_cache_fini(&cache);
  159. return -1;
  160. }
  161. previous_block_number = -1;
  162. }
  163. *actread = len;
  164. ext_cache_fini(&cache);
  165. return 0;
  166. }
  167. int ext4fs_ls(const char *dirname)
  168. {
  169. struct ext2fs_node *dirnode = NULL;
  170. int status;
  171. if (dirname == NULL)
  172. return 0;
  173. status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
  174. FILETYPE_DIRECTORY);
  175. if (status != 1) {
  176. printf("** Can not find directory. **\n");
  177. if (dirnode)
  178. ext4fs_free_node(dirnode, &ext4fs_root->diropen);
  179. return 1;
  180. }
  181. ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
  182. ext4fs_free_node(dirnode, &ext4fs_root->diropen);
  183. return 0;
  184. }
  185. int ext4fs_exists(const char *filename)
  186. {
  187. loff_t file_len;
  188. int ret;
  189. ret = ext4fs_open(filename, &file_len);
  190. return ret == 0;
  191. }
  192. int ext4fs_size(const char *filename, loff_t *size)
  193. {
  194. return ext4fs_open(filename, size);
  195. }
  196. int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
  197. {
  198. if (ext4fs_root == NULL || ext4fs_file == NULL)
  199. return -1;
  200. return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
  201. }
  202. int ext4fs_probe(struct blk_desc *fs_dev_desc,
  203. struct disk_partition *fs_partition)
  204. {
  205. ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
  206. if (!ext4fs_mount(fs_partition->size)) {
  207. ext4fs_close();
  208. return -1;
  209. }
  210. return 0;
  211. }
  212. int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
  213. loff_t *len_read)
  214. {
  215. loff_t file_len;
  216. int ret;
  217. ret = ext4fs_open(filename, &file_len);
  218. if (ret < 0) {
  219. printf("** File not found %s **\n", filename);
  220. return -1;
  221. }
  222. if (len == 0)
  223. len = file_len;
  224. return ext4fs_read(buf, offset, len, len_read);
  225. }
  226. int ext4fs_uuid(char *uuid_str)
  227. {
  228. if (ext4fs_root == NULL)
  229. return -1;
  230. #ifdef CONFIG_LIB_UUID
  231. uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
  232. uuid_str, UUID_STR_FORMAT_STD);
  233. return 0;
  234. #else
  235. return -ENOSYS;
  236. #endif
  237. }
  238. void ext_cache_init(struct ext_block_cache *cache)
  239. {
  240. memset(cache, 0, sizeof(*cache));
  241. }
  242. void ext_cache_fini(struct ext_block_cache *cache)
  243. {
  244. free(cache->buf);
  245. ext_cache_init(cache);
  246. }
  247. int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
  248. {
  249. /* This could be more lenient, but this is simple and enough for now */
  250. if (cache->buf && cache->block == block && cache->size == size)
  251. return 1;
  252. ext_cache_fini(cache);
  253. cache->buf = memalign(ARCH_DMA_MINALIGN, size);
  254. if (!cache->buf)
  255. return 0;
  256. if (!ext4fs_devread(block, 0, size, cache->buf)) {
  257. ext_cache_fini(cache);
  258. return 0;
  259. }
  260. cache->block = block;
  261. cache->size = size;
  262. return 1;
  263. }