blob: 6f5063c3aff5e2806320bead6547a6c4154d4495 [file] [log] [blame]
Stephen Warreneefbc3f2012-10-22 06:43:51 +00001/*
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>
Simon Glass11842872012-12-26 09:53:35 +000023#include <sandboxfs.h>
Simon Glasscbe5d5d2012-12-26 09:53:32 +000024#include <asm/io.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000025
Stephen Warren44161af2012-10-30 07:50:47 +000026DECLARE_GLOBAL_DATA_PTR;
27
Stephen Warreneefbc3f2012-10-22 06:43:51 +000028static block_dev_desc_t *fs_dev_desc;
29static disk_partition_t fs_partition;
30static int fs_type = FS_TYPE_ANY;
31
Simon Glass6a46e2f2012-12-26 09:53:31 +000032static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
33 disk_partition_t *fs_partition)
Simon Glass1aede482012-12-26 09:53:29 +000034{
35 printf("** Unrecognized filesystem type **\n");
36 return -1;
37}
38
Stephen Warreneefbc3f2012-10-22 06:43:51 +000039static inline int fs_ls_unsupported(const char *dirname)
40{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000041 return -1;
42}
43
Simon Glasscbe5d5d2012-12-26 09:53:32 +000044static inline int fs_read_unsupported(const char *filename, void *buf,
Stephen Warreneefbc3f2012-10-22 06:43:51 +000045 int offset, int len)
46{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000047 return -1;
48}
49
Simon Glass1aede482012-12-26 09:53:29 +000050static inline void fs_close_unsupported(void)
51{
52}
53
Simon Glass1aede482012-12-26 09:53:29 +000054struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +000055 int fstype;
Simon Glass6a46e2f2012-12-26 09:53:31 +000056 int (*probe)(block_dev_desc_t *fs_dev_desc,
57 disk_partition_t *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +000058 int (*ls)(const char *dirname);
Simon Glasscbe5d5d2012-12-26 09:53:32 +000059 int (*read)(const char *filename, void *buf, int offset, int len);
Simon Glass1aede482012-12-26 09:53:29 +000060 void (*close)(void);
61};
62
63static struct fstype_info fstypes[] = {
64#ifdef CONFIG_FS_FAT
Stephen Warreneefbc3f2012-10-22 06:43:51 +000065 {
66 .fstype = FS_TYPE_FAT,
Simon Glass19e38582012-12-26 09:53:33 +000067 .probe = fat_set_blk_dev,
68 .close = fat_close,
Simon Glass1aede482012-12-26 09:53:29 +000069 .ls = file_fat_ls,
Simon Glass19e38582012-12-26 09:53:33 +000070 .read = fat_read_file,
Stephen Warreneefbc3f2012-10-22 06:43:51 +000071 },
Simon Glass1aede482012-12-26 09:53:29 +000072#endif
73#ifdef CONFIG_FS_EXT4
Stephen Warreneefbc3f2012-10-22 06:43:51 +000074 {
75 .fstype = FS_TYPE_EXT,
Simon Glass19e38582012-12-26 09:53:33 +000076 .probe = ext4fs_probe,
77 .close = ext4fs_close,
Simon Glass1aede482012-12-26 09:53:29 +000078 .ls = ext4fs_ls,
Simon Glass19e38582012-12-26 09:53:33 +000079 .read = ext4_read_file,
Simon Glass1aede482012-12-26 09:53:29 +000080 },
81#endif
Simon Glass11842872012-12-26 09:53:35 +000082#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 },
90#endif
Simon Glass1aede482012-12-26 09:53:29 +000091 {
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,
Stephen Warreneefbc3f2012-10-22 06:43:51 +000097 },
98};
99
Simon Glasse6aad852012-12-26 09:53:30 +0000100static 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
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000114int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
115{
Simon Glass1aede482012-12-26 09:53:29 +0000116 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000117 int part, i;
Stephen Warren44161af2012-10-30 07:50:47 +0000118#ifdef CONFIG_NEEDS_MANUAL_RELOC
119 static int relocated;
120
121 if (!relocated) {
Simon Glass1aede482012-12-26 09:53:29 +0000122 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 }
Stephen Warren44161af2012-10-30 07:50:47 +0000129 relocated = 1;
130 }
131#endif
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000132
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
Simon Glass1aede482012-12-26 09:53:29 +0000138 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)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000141 continue;
142
Simon Glass6a46e2f2012-12-26 09:53:31 +0000143 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000144 fs_type = info->fstype;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000145 return 0;
146 }
147 }
148
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000149 return -1;
150}
151
152static void fs_close(void)
153{
Simon Glasse6aad852012-12-26 09:53:30 +0000154 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000155
Simon Glasse6aad852012-12-26 09:53:30 +0000156 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000157
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000158 fs_type = FS_TYPE_ANY;
159}
160
161int fs_ls(const char *dirname)
162{
163 int ret;
164
Simon Glasse6aad852012-12-26 09:53:30 +0000165 struct fstype_info *info = fs_get_info(fs_type);
166
167 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000168
Simon Glass19e38582012-12-26 09:53:33 +0000169 fs_type = FS_TYPE_ANY;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000170 fs_close();
171
172 return ret;
173}
174
175int fs_read(const char *filename, ulong addr, int offset, int len)
176{
Simon Glasse6aad852012-12-26 09:53:30 +0000177 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000178 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000179 int ret;
180
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000181 /*
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);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000188
Simon Glasse6aad852012-12-26 09:53:30 +0000189 /* 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 }
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000194 fs_close();
195
196 return ret;
197}
198
Stephen Warren128d3d92012-10-31 11:05:07 +0000199int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Stephen Warren40816a02012-10-30 12:04:19 +0000200 int fstype, int cmdline_base)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000201{
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;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100208 unsigned long time;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000209
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000210 if (argc < 2)
211 return CMD_RET_USAGE;
212 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000213 return CMD_RET_USAGE;
214
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000215 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000216 return 1;
217
218 if (argc >= 4) {
Stephen Warren40816a02012-10-30 12:04:19 +0000219 addr = simple_strtoul(argv[3], NULL, cmdline_base);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000220 } 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)
Stephen Warren40816a02012-10-30 12:04:19 +0000237 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000238 else
239 bytes = 0;
240 if (argc >= 7)
Stephen Warren40816a02012-10-30 12:04:19 +0000241 pos = simple_strtoul(argv[6], NULL, cmdline_base);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000242 else
243 pos = 0;
244
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100245 time = get_timer(0);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000246 len_read = fs_read(filename, addr, pos, bytes);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100247 time = get_timer(time);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000248 if (len_read <= 0)
249 return 1;
250
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100251 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");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000258
Simon Glassfff5c072013-02-24 17:33:23 +0000259 setenv_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000260
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;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000269 if (argc > 4)
270 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000271
272 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
273 return 1;
274
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000275 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000276 return 1;
277
278 return 0;
279}