ext4fs.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. 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, loff_t pos,
  44. loff_t len, char *buf, loff_t *actread)
  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. if (blocksize <= 0)
  61. return -1;
  62. /* Adjust len so it we can't read past the end of the file. */
  63. if (len + pos > filesize)
  64. len = (filesize - pos);
  65. blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
  66. for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
  67. long int blknr;
  68. int blockoff = pos - (blocksize * i);
  69. int blockend = blocksize;
  70. int skipfirst = 0;
  71. blknr = read_allocated_block(&(node->inode), i);
  72. if (blknr < 0)
  73. return -1;
  74. blknr = blknr << log2_fs_blocksize;
  75. /* Last block. */
  76. if (i == blockcnt - 1) {
  77. blockend = (len + pos) - (blocksize * i);
  78. /* The last portion is exactly blocksize. */
  79. if (!blockend)
  80. blockend = blocksize;
  81. }
  82. /* First block. */
  83. if (i == lldiv(pos, blocksize)) {
  84. skipfirst = blockoff;
  85. blockend -= skipfirst;
  86. }
  87. if (blknr) {
  88. int status;
  89. if (previous_block_number != -1) {
  90. if (delayed_next == blknr) {
  91. delayed_extent += blockend;
  92. delayed_next += blockend >> log2blksz;
  93. } else { /* spill */
  94. status = ext4fs_devread(delayed_start,
  95. delayed_skipfirst,
  96. delayed_extent,
  97. delayed_buf);
  98. if (status == 0)
  99. return -1;
  100. previous_block_number = blknr;
  101. delayed_start = blknr;
  102. delayed_extent = blockend;
  103. delayed_skipfirst = skipfirst;
  104. delayed_buf = buf;
  105. delayed_next = blknr +
  106. (blockend >> log2blksz);
  107. }
  108. } else {
  109. previous_block_number = blknr;
  110. delayed_start = blknr;
  111. delayed_extent = blockend;
  112. delayed_skipfirst = skipfirst;
  113. delayed_buf = buf;
  114. delayed_next = blknr +
  115. (blockend >> log2blksz);
  116. }
  117. } else {
  118. int n;
  119. if (previous_block_number != -1) {
  120. /* spill */
  121. status = ext4fs_devread(delayed_start,
  122. delayed_skipfirst,
  123. delayed_extent,
  124. delayed_buf);
  125. if (status == 0)
  126. return -1;
  127. previous_block_number = -1;
  128. }
  129. /* Zero no more than `len' bytes. */
  130. n = blocksize - skipfirst;
  131. if (n > len)
  132. n = len;
  133. memset(buf, 0, n);
  134. }
  135. buf += blocksize - skipfirst;
  136. }
  137. if (previous_block_number != -1) {
  138. /* spill */
  139. status = ext4fs_devread(delayed_start,
  140. delayed_skipfirst, delayed_extent,
  141. delayed_buf);
  142. if (status == 0)
  143. return -1;
  144. previous_block_number = -1;
  145. }
  146. *actread = len;
  147. return 0;
  148. }
  149. int ext4fs_ls(const char *dirname)
  150. {
  151. struct ext2fs_node *dirnode = NULL;
  152. int status;
  153. if (dirname == NULL)
  154. return 0;
  155. status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
  156. FILETYPE_DIRECTORY);
  157. if (status != 1) {
  158. printf("** Can not find directory. **\n");
  159. if (dirnode)
  160. ext4fs_free_node(dirnode, &ext4fs_root->diropen);
  161. return 1;
  162. }
  163. ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
  164. ext4fs_free_node(dirnode, &ext4fs_root->diropen);
  165. return 0;
  166. }
  167. int ext4fs_exists(const char *filename)
  168. {
  169. loff_t file_len;
  170. int ret;
  171. ret = ext4fs_open(filename, &file_len);
  172. return ret == 0;
  173. }
  174. int ext4fs_size(const char *filename, loff_t *size)
  175. {
  176. return ext4fs_open(filename, size);
  177. }
  178. int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
  179. {
  180. if (ext4fs_root == NULL || ext4fs_file == NULL)
  181. return -1;
  182. return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
  183. }
  184. int ext4fs_probe(struct blk_desc *fs_dev_desc,
  185. disk_partition_t *fs_partition)
  186. {
  187. ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
  188. if (!ext4fs_mount(fs_partition->size)) {
  189. ext4fs_close();
  190. return -1;
  191. }
  192. return 0;
  193. }
  194. int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
  195. loff_t *len_read)
  196. {
  197. loff_t file_len;
  198. int ret;
  199. ret = ext4fs_open(filename, &file_len);
  200. if (ret < 0) {
  201. printf("** File not found %s **\n", filename);
  202. return -1;
  203. }
  204. if (len == 0)
  205. len = file_len;
  206. return ext4fs_read(buf, offset, len, len_read);
  207. }
  208. int ext4fs_uuid(char *uuid_str)
  209. {
  210. if (ext4fs_root == NULL)
  211. return -1;
  212. #ifdef CONFIG_LIB_UUID
  213. uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
  214. uuid_str, UUID_STR_FORMAT_STD);
  215. return 0;
  216. #else
  217. return -ENOSYS;
  218. #endif
  219. }