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>
7 * ext4ls and ext4load : Based on ext2 ls and load support in Uboot.
8 * Ext4 read optimization taken from Open-Moko
12 * esd gmbh <www.esd-electronics.com>
13 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
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.
19 * ext4write : Based on generic ext4 protocol.
21 * SPDX-License-Identifier: GPL-2.0+
25 #include <ext_common.h>
27 #include "ext4_common.h"
30 int ext4fs_symlinknest
;
31 struct ext_filesystem ext_fs
;
33 struct ext_filesystem
*get_fs(void)
38 void ext4fs_free_node(struct ext2fs_node
*node
, struct ext2fs_node
*currroot
)
40 if ((node
!= &ext4fs_root
->diropen
) && (node
!= currroot
))
45 * Taken from openmoko-kernel mailing list: By Andy green
46 * Optimized read file API : collects and defers contiguous sector
47 * reads into one potentially more efficient larger sequential read action
49 int ext4fs_read_file(struct ext2fs_node
*node
, loff_t pos
,
50 loff_t len
, char *buf
, loff_t
*actread
)
52 struct ext_filesystem
*fs
= get_fs();
55 int log2blksz
= fs
->dev_desc
->log2blksz
;
56 int log2_fs_blocksize
= LOG2_BLOCK_SIZE(node
->data
) - log2blksz
;
57 int blocksize
= (1 << (log2_fs_blocksize
+ log2blksz
));
58 unsigned int filesize
= __le32_to_cpu(node
->inode
.size
);
59 lbaint_t previous_block_number
= -1;
60 lbaint_t delayed_start
= 0;
61 lbaint_t delayed_extent
= 0;
62 lbaint_t delayed_skipfirst
= 0;
63 lbaint_t delayed_next
= 0;
64 char *delayed_buf
= NULL
;
67 /* Adjust len so it we can't read past the end of the file. */
71 blockcnt
= lldiv(((len
+ pos
) + blocksize
- 1), blocksize
);
73 for (i
= lldiv(pos
, blocksize
); i
< blockcnt
; i
++) {
75 int blockoff
= pos
- (blocksize
* i
);
76 int blockend
= blocksize
;
78 blknr
= read_allocated_block(&(node
->inode
), i
);
82 blknr
= blknr
<< log2_fs_blocksize
;
85 if (i
== blockcnt
- 1) {
86 blockend
= (len
+ pos
) - (blocksize
* i
);
88 /* The last portion is exactly blocksize. */
94 if (i
== lldiv(pos
, blocksize
)) {
96 blockend
-= skipfirst
;
101 if (previous_block_number
!= -1) {
102 if (delayed_next
== blknr
) {
103 delayed_extent
+= blockend
;
104 delayed_next
+= blockend
>> log2blksz
;
106 status
= ext4fs_devread(delayed_start
,
112 previous_block_number
= blknr
;
113 delayed_start
= blknr
;
114 delayed_extent
= blockend
;
115 delayed_skipfirst
= skipfirst
;
117 delayed_next
= blknr
+
118 (blockend
>> log2blksz
);
121 previous_block_number
= blknr
;
122 delayed_start
= blknr
;
123 delayed_extent
= blockend
;
124 delayed_skipfirst
= skipfirst
;
126 delayed_next
= blknr
+
127 (blockend
>> log2blksz
);
130 if (previous_block_number
!= -1) {
132 status
= ext4fs_devread(delayed_start
,
138 previous_block_number
= -1;
140 memset(buf
, 0, blocksize
- skipfirst
);
142 buf
+= blocksize
- skipfirst
;
144 if (previous_block_number
!= -1) {
146 status
= ext4fs_devread(delayed_start
,
147 delayed_skipfirst
, delayed_extent
,
151 previous_block_number
= -1;
158 int ext4fs_ls(const char *dirname
)
160 struct ext2fs_node
*dirnode
;
166 status
= ext4fs_find_file(dirname
, &ext4fs_root
->diropen
, &dirnode
,
169 printf("** Can not find directory. **\n");
173 ext4fs_iterate_dir(dirnode
, NULL
, NULL
, NULL
);
174 ext4fs_free_node(dirnode
, &ext4fs_root
->diropen
);
179 int ext4fs_exists(const char *filename
)
184 ret
= ext4fs_open(filename
, &file_len
);
188 int ext4fs_size(const char *filename
, loff_t
*size
)
190 return ext4fs_open(filename
, size
);
193 int ext4fs_read(char *buf
, loff_t len
, loff_t
*actread
)
195 if (ext4fs_root
== NULL
|| ext4fs_file
== NULL
)
198 return ext4fs_read_file(ext4fs_file
, 0, len
, buf
, actread
);
201 int ext4fs_probe(block_dev_desc_t
*fs_dev_desc
,
202 disk_partition_t
*fs_partition
)
204 ext4fs_set_blk_dev(fs_dev_desc
, fs_partition
);
206 if (!ext4fs_mount(fs_partition
->size
)) {
214 int ext4_read_file(const char *filename
, void *buf
, loff_t offset
, loff_t len
,
221 printf("** Cannot support non-zero offset **\n");
225 ret
= ext4fs_open(filename
, &file_len
);
227 printf("** File not found %s **\n", filename
);
234 return ext4fs_read(buf
, len
, len_read
);
237 int ext4fs_uuid(char *uuid_str
)
239 if (ext4fs_root
== NULL
)
242 #ifdef CONFIG_LIB_UUID
243 uuid_bin_to_str((unsigned char *)ext4fs_root
->sblock
.unique_id
,
244 uuid_str
, UUID_STR_FORMAT_STD
);