]> git.ipfire.org Git - people/ms/u-boot.git/blame - fs/fs.c
Merge branch 'u-boot-imx/master' into 'u-boot-arm/master'
[people/ms/u-boot.git] / fs / fs.c
CommitLineData
045fa1e1
SW
1/*
2 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <config.h>
18#include <common.h>
19#include <part.h>
20#include <ext4fs.h>
21#include <fat.h>
22#include <fs.h>
92ccc96b 23#include <sandboxfs.h>
117e0507 24#include <asm/io.h>
045fa1e1 25
a1b231ce
SW
26DECLARE_GLOBAL_DATA_PTR;
27
045fa1e1
SW
28static block_dev_desc_t *fs_dev_desc;
29static disk_partition_t fs_partition;
30static int fs_type = FS_TYPE_ANY;
31
2ded0d47
SG
32static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
33 disk_partition_t *fs_partition)
436e2b73
SG
34{
35 printf("** Unrecognized filesystem type **\n");
36 return -1;
37}
38
045fa1e1
SW
39static inline int fs_ls_unsupported(const char *dirname)
40{
045fa1e1
SW
41 return -1;
42}
43
117e0507 44static inline int fs_read_unsupported(const char *filename, void *buf,
045fa1e1
SW
45 int offset, int len)
46{
045fa1e1
SW
47 return -1;
48}
49
436e2b73
SG
50static inline void fs_close_unsupported(void)
51{
52}
53
436e2b73 54struct fstype_info {
045fa1e1 55 int fstype;
2ded0d47
SG
56 int (*probe)(block_dev_desc_t *fs_dev_desc,
57 disk_partition_t *fs_partition);
436e2b73 58 int (*ls)(const char *dirname);
117e0507 59 int (*read)(const char *filename, void *buf, int offset, int len);
436e2b73
SG
60 void (*close)(void);
61};
62
63static struct fstype_info fstypes[] = {
64#ifdef CONFIG_FS_FAT
045fa1e1
SW
65 {
66 .fstype = FS_TYPE_FAT,
e6d52415
SG
67 .probe = fat_set_blk_dev,
68 .close = fat_close,
436e2b73 69 .ls = file_fat_ls,
e6d52415 70 .read = fat_read_file,
045fa1e1 71 },
436e2b73
SG
72#endif
73#ifdef CONFIG_FS_EXT4
045fa1e1
SW
74 {
75 .fstype = FS_TYPE_EXT,
e6d52415
SG
76 .probe = ext4fs_probe,
77 .close = ext4fs_close,
436e2b73 78 .ls = ext4fs_ls,
e6d52415 79 .read = ext4_read_file,
436e2b73 80 },
92ccc96b
SG
81#endif
82#ifdef CONFIG_SANDBOX
83 {
84 .fstype = FS_TYPE_SANDBOX,
85 .probe = sandbox_fs_set_blk_dev,
86 .close = sandbox_fs_close,
87 .ls = sandbox_fs_ls,
88 .read = fs_read_sandbox,
89 },
436e2b73
SG
90#endif
91 {
92 .fstype = FS_TYPE_ANY,
93 .probe = fs_probe_unsupported,
94 .close = fs_close_unsupported,
95 .ls = fs_ls_unsupported,
96 .read = fs_read_unsupported,
045fa1e1
SW
97 },
98};
99
c6f548d2
SG
100static struct fstype_info *fs_get_info(int fstype)
101{
102 struct fstype_info *info;
103 int i;
104
105 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
106 if (fstype == info->fstype)
107 return info;
108 }
109
110 /* Return the 'unsupported' sentinel */
111 return info;
112}
113
045fa1e1
SW
114int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
115{
436e2b73 116 struct fstype_info *info;
045fa1e1 117 int part, i;
a1b231ce
SW
118#ifdef CONFIG_NEEDS_MANUAL_RELOC
119 static int relocated;
120
121 if (!relocated) {
436e2b73
SG
122 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
123 i++, info++) {
124 info->probe += gd->reloc_off;
125 info->close += gd->reloc_off;
126 info->ls += gd->reloc_off;
127 info->read += gd->reloc_off;
128 }
a1b231ce
SW
129 relocated = 1;
130 }
131#endif
045fa1e1
SW
132
133 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
134 &fs_partition, 1);
135 if (part < 0)
136 return -1;
137
436e2b73
SG
138 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
139 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
140 fstype != info->fstype)
045fa1e1
SW
141 continue;
142
2ded0d47 143 if (!info->probe(fs_dev_desc, &fs_partition)) {
436e2b73 144 fs_type = info->fstype;
045fa1e1
SW
145 return 0;
146 }
147 }
148
045fa1e1
SW
149 return -1;
150}
151
152static void fs_close(void)
153{
c6f548d2 154 struct fstype_info *info = fs_get_info(fs_type);
045fa1e1 155
c6f548d2 156 info->close();
e6d52415 157
045fa1e1
SW
158 fs_type = FS_TYPE_ANY;
159}
160
161int fs_ls(const char *dirname)
162{
163 int ret;
164
c6f548d2
SG
165 struct fstype_info *info = fs_get_info(fs_type);
166
167 ret = info->ls(dirname);
045fa1e1 168
e6d52415 169 fs_type = FS_TYPE_ANY;
045fa1e1
SW
170 fs_close();
171
172 return ret;
173}
174
175int fs_read(const char *filename, ulong addr, int offset, int len)
176{
c6f548d2 177 struct fstype_info *info = fs_get_info(fs_type);
117e0507 178 void *buf;
045fa1e1
SW
179 int ret;
180
117e0507
SG
181 /*
182 * We don't actually know how many bytes are being read, since len==0
183 * means read the whole file.
184 */
185 buf = map_sysmem(addr, len);
186 ret = info->read(filename, buf, offset, len);
187 unmap_sysmem(buf);
045fa1e1 188
c6f548d2
SG
189 /* If we requested a specific number of bytes, check we got it */
190 if (ret >= 0 && len && ret != len) {
191 printf("** Unable to read file %s **\n", filename);
192 ret = -1;
193 }
045fa1e1
SW
194 fs_close();
195
196 return ret;
197}
198
f9b55e22 199int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
3f83c87e 200 int fstype, int cmdline_base)
045fa1e1
SW
201{
202 unsigned long addr;
203 const char *addr_str;
204 const char *filename;
205 unsigned long bytes;
206 unsigned long pos;
207 int len_read;
da1fd96c 208 unsigned long time;
045fa1e1 209
e9b0f99e
SW
210 if (argc < 2)
211 return CMD_RET_USAGE;
212 if (argc > 7)
045fa1e1
SW
213 return CMD_RET_USAGE;
214
e9b0f99e 215 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
045fa1e1
SW
216 return 1;
217
218 if (argc >= 4) {
3f83c87e 219 addr = simple_strtoul(argv[3], NULL, cmdline_base);
045fa1e1
SW
220 } else {
221 addr_str = getenv("loadaddr");
222 if (addr_str != NULL)
223 addr = simple_strtoul(addr_str, NULL, 16);
224 else
225 addr = CONFIG_SYS_LOAD_ADDR;
226 }
227 if (argc >= 5) {
228 filename = argv[4];
229 } else {
230 filename = getenv("bootfile");
231 if (!filename) {
232 puts("** No boot file defined **\n");
233 return 1;
234 }
235 }
236 if (argc >= 6)
3f83c87e 237 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
045fa1e1
SW
238 else
239 bytes = 0;
240 if (argc >= 7)
3f83c87e 241 pos = simple_strtoul(argv[6], NULL, cmdline_base);
045fa1e1
SW
242 else
243 pos = 0;
244
da1fd96c 245 time = get_timer(0);
045fa1e1 246 len_read = fs_read(filename, addr, pos, bytes);
da1fd96c 247 time = get_timer(time);
045fa1e1
SW
248 if (len_read <= 0)
249 return 1;
250
da1fd96c
AB
251 printf("%d bytes read in %lu ms", len_read, time);
252 if (time > 0) {
253 puts(" (");
254 print_size(len_read / time * 1000, "/s");
255 puts(")");
256 }
257 puts("\n");
045fa1e1 258
49c4f037 259 setenv_hex("filesize", len_read);
045fa1e1
SW
260
261 return 0;
262}
263
264int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
265 int fstype)
266{
267 if (argc < 2)
268 return CMD_RET_USAGE;
e9b0f99e
SW
269 if (argc > 4)
270 return CMD_RET_USAGE;
045fa1e1
SW
271
272 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
273 return 1;
274
e9b0f99e 275 if (fs_ls(argc >= 4 ? argv[3] : "/"))
045fa1e1
SW
276 return 1;
277
278 return 0;
279}