ext4fs.c 6.8 KB

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