blob: eee7e23392e4cb9f76c4e4247c3d6c55981bdded [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 Glasseda14ea2013-04-20 08:42:50 +000050static inline int fs_write_unsupported(const char *filename, void *buf,
51 int offset, int len)
52{
53 return -1;
54}
55
Simon Glass1aede482012-12-26 09:53:29 +000056static inline void fs_close_unsupported(void)
57{
58}
59
Simon Glass1aede482012-12-26 09:53:29 +000060struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +000061 int fstype;
Simon Glass6a46e2f2012-12-26 09:53:31 +000062 int (*probe)(block_dev_desc_t *fs_dev_desc,
63 disk_partition_t *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +000064 int (*ls)(const char *dirname);
Simon Glasscbe5d5d2012-12-26 09:53:32 +000065 int (*read)(const char *filename, void *buf, int offset, int len);
Simon Glasseda14ea2013-04-20 08:42:50 +000066 int (*write)(const char *filename, void *buf, int offset, int len);
Simon Glass1aede482012-12-26 09:53:29 +000067 void (*close)(void);
68};
69
70static struct fstype_info fstypes[] = {
71#ifdef CONFIG_FS_FAT
Stephen Warreneefbc3f2012-10-22 06:43:51 +000072 {
73 .fstype = FS_TYPE_FAT,
Simon Glass19e38582012-12-26 09:53:33 +000074 .probe = fat_set_blk_dev,
75 .close = fat_close,
Simon Glass1aede482012-12-26 09:53:29 +000076 .ls = file_fat_ls,
Simon Glass19e38582012-12-26 09:53:33 +000077 .read = fat_read_file,
Stephen Warreneefbc3f2012-10-22 06:43:51 +000078 },
Simon Glass1aede482012-12-26 09:53:29 +000079#endif
80#ifdef CONFIG_FS_EXT4
Stephen Warreneefbc3f2012-10-22 06:43:51 +000081 {
82 .fstype = FS_TYPE_EXT,
Simon Glass19e38582012-12-26 09:53:33 +000083 .probe = ext4fs_probe,
84 .close = ext4fs_close,
Simon Glass1aede482012-12-26 09:53:29 +000085 .ls = ext4fs_ls,
Simon Glass19e38582012-12-26 09:53:33 +000086 .read = ext4_read_file,
Simon Glass1aede482012-12-26 09:53:29 +000087 },
88#endif
Simon Glass11842872012-12-26 09:53:35 +000089#ifdef CONFIG_SANDBOX
90 {
91 .fstype = FS_TYPE_SANDBOX,
92 .probe = sandbox_fs_set_blk_dev,
93 .close = sandbox_fs_close,
94 .ls = sandbox_fs_ls,
95 .read = fs_read_sandbox,
96 },
97#endif
Simon Glass1aede482012-12-26 09:53:29 +000098 {
99 .fstype = FS_TYPE_ANY,
100 .probe = fs_probe_unsupported,
101 .close = fs_close_unsupported,
102 .ls = fs_ls_unsupported,
103 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000104 .write = fs_write_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000105 },
106};
107
Simon Glasse6aad852012-12-26 09:53:30 +0000108static struct fstype_info *fs_get_info(int fstype)
109{
110 struct fstype_info *info;
111 int i;
112
113 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
114 if (fstype == info->fstype)
115 return info;
116 }
117
118 /* Return the 'unsupported' sentinel */
119 return info;
120}
121
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000122int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
123{
Simon Glass1aede482012-12-26 09:53:29 +0000124 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000125 int part, i;
Stephen Warren44161af2012-10-30 07:50:47 +0000126#ifdef CONFIG_NEEDS_MANUAL_RELOC
127 static int relocated;
128
129 if (!relocated) {
Simon Glass1aede482012-12-26 09:53:29 +0000130 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
131 i++, info++) {
132 info->probe += gd->reloc_off;
133 info->close += gd->reloc_off;
134 info->ls += gd->reloc_off;
135 info->read += gd->reloc_off;
Simon Glasseda14ea2013-04-20 08:42:50 +0000136 info->write += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000137 }
Stephen Warren44161af2012-10-30 07:50:47 +0000138 relocated = 1;
139 }
140#endif
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000141
142 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
143 &fs_partition, 1);
144 if (part < 0)
145 return -1;
146
Simon Glass1aede482012-12-26 09:53:29 +0000147 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
148 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
149 fstype != info->fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000150 continue;
151
Simon Glass6a46e2f2012-12-26 09:53:31 +0000152 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000153 fs_type = info->fstype;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000154 return 0;
155 }
156 }
157
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000158 return -1;
159}
160
161static void fs_close(void)
162{
Simon Glasse6aad852012-12-26 09:53:30 +0000163 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000164
Simon Glasse6aad852012-12-26 09:53:30 +0000165 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000166
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000167 fs_type = FS_TYPE_ANY;
168}
169
170int fs_ls(const char *dirname)
171{
172 int ret;
173
Simon Glasse6aad852012-12-26 09:53:30 +0000174 struct fstype_info *info = fs_get_info(fs_type);
175
176 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000177
Simon Glass19e38582012-12-26 09:53:33 +0000178 fs_type = FS_TYPE_ANY;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000179 fs_close();
180
181 return ret;
182}
183
184int fs_read(const char *filename, ulong addr, int offset, int len)
185{
Simon Glasse6aad852012-12-26 09:53:30 +0000186 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000187 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000188 int ret;
189
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000190 /*
191 * We don't actually know how many bytes are being read, since len==0
192 * means read the whole file.
193 */
194 buf = map_sysmem(addr, len);
195 ret = info->read(filename, buf, offset, len);
196 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000197
Simon Glasse6aad852012-12-26 09:53:30 +0000198 /* If we requested a specific number of bytes, check we got it */
199 if (ret >= 0 && len && ret != len) {
200 printf("** Unable to read file %s **\n", filename);
201 ret = -1;
202 }
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000203 fs_close();
204
205 return ret;
206}
207
Simon Glasseda14ea2013-04-20 08:42:50 +0000208int fs_write(const char *filename, ulong addr, int offset, int len)
209{
210 struct fstype_info *info = fs_get_info(fs_type);
211 void *buf;
212 int ret;
213
214 /*
215 * We don't actually know how many bytes are being read, since len==0
216 * means read the whole file.
217 */
218 buf = map_sysmem(addr, len);
219 ret = info->write(filename, buf, offset, len);
220 unmap_sysmem(buf);
221
222 /* If we requested a specific number of bytes, check we got it */
223 if (ret >= 0 && len && ret != len) {
224 printf("** Unable to write file %s **\n", filename);
225 ret = -1;
226 }
227 fs_close();
228
229 return ret;
230}
231
Stephen Warren128d3d92012-10-31 11:05:07 +0000232int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Stephen Warren40816a02012-10-30 12:04:19 +0000233 int fstype, int cmdline_base)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000234{
235 unsigned long addr;
236 const char *addr_str;
237 const char *filename;
238 unsigned long bytes;
239 unsigned long pos;
240 int len_read;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100241 unsigned long time;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000242
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000243 if (argc < 2)
244 return CMD_RET_USAGE;
245 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000246 return CMD_RET_USAGE;
247
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000248 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000249 return 1;
250
251 if (argc >= 4) {
Stephen Warren40816a02012-10-30 12:04:19 +0000252 addr = simple_strtoul(argv[3], NULL, cmdline_base);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000253 } else {
254 addr_str = getenv("loadaddr");
255 if (addr_str != NULL)
256 addr = simple_strtoul(addr_str, NULL, 16);
257 else
258 addr = CONFIG_SYS_LOAD_ADDR;
259 }
260 if (argc >= 5) {
261 filename = argv[4];
262 } else {
263 filename = getenv("bootfile");
264 if (!filename) {
265 puts("** No boot file defined **\n");
266 return 1;
267 }
268 }
269 if (argc >= 6)
Stephen Warren40816a02012-10-30 12:04:19 +0000270 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000271 else
272 bytes = 0;
273 if (argc >= 7)
Stephen Warren40816a02012-10-30 12:04:19 +0000274 pos = simple_strtoul(argv[6], NULL, cmdline_base);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000275 else
276 pos = 0;
277
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100278 time = get_timer(0);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000279 len_read = fs_read(filename, addr, pos, bytes);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100280 time = get_timer(time);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000281 if (len_read <= 0)
282 return 1;
283
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100284 printf("%d bytes read in %lu ms", len_read, time);
285 if (time > 0) {
286 puts(" (");
287 print_size(len_read / time * 1000, "/s");
288 puts(")");
289 }
290 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000291
Simon Glassfff5c072013-02-24 17:33:23 +0000292 setenv_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000293
294 return 0;
295}
296
297int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
298 int fstype)
299{
300 if (argc < 2)
301 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000302 if (argc > 4)
303 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000304
305 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
306 return 1;
307
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000308 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000309 return 1;
310
311 return 0;
312}
Simon Glasseda14ea2013-04-20 08:42:50 +0000313
314int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
315 int fstype, int cmdline_base)
316{
317 unsigned long addr;
318 const char *filename;
319 unsigned long bytes;
320 unsigned long pos;
321 int len;
322 unsigned long time;
323
324 if (argc < 6 || argc > 7)
325 return CMD_RET_USAGE;
326
327 if (fs_set_blk_dev(argv[1], argv[2], fstype))
328 return 1;
329
330 filename = argv[3];
331 addr = simple_strtoul(argv[4], NULL, cmdline_base);
332 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
333 if (argc >= 7)
334 pos = simple_strtoul(argv[6], NULL, cmdline_base);
335 else
336 pos = 0;
337
338 time = get_timer(0);
339 len = fs_write(filename, addr, pos, bytes);
340 time = get_timer(time);
341 if (len <= 0)
342 return 1;
343
344 printf("%d bytes written in %lu ms", len, time);
345 if (time > 0) {
346 puts(" (");
347 print_size(len / time * 1000, "/s");
348 puts(")");
349 }
350 puts("\n");
351
352 return 0;
353}