blob: 8fe2403a46aea48e122972d9e4bdf65ba645148b [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
Stephen Warrend008fbb2014-02-03 13:21:00 -070044static inline int fs_exists_unsupported(const char *filename)
45{
46 return 0;
47}
48
Simon Glasscbe5d5d2012-12-26 09:53:32 +000049static inline int fs_read_unsupported(const char *filename, void *buf,
Stephen Warreneefbc3f2012-10-22 06:43:51 +000050 int offset, int len)
51{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000052 return -1;
53}
54
Simon Glasseda14ea2013-04-20 08:42:50 +000055static inline int fs_write_unsupported(const char *filename, void *buf,
56 int offset, int len)
57{
58 return -1;
59}
60
Simon Glass1aede482012-12-26 09:53:29 +000061static inline void fs_close_unsupported(void)
62{
63}
64
Simon Glass1aede482012-12-26 09:53:29 +000065struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +000066 int fstype;
Simon Glass6a46e2f2012-12-26 09:53:31 +000067 int (*probe)(block_dev_desc_t *fs_dev_desc,
68 disk_partition_t *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +000069 int (*ls)(const char *dirname);
Stephen Warrend008fbb2014-02-03 13:21:00 -070070 int (*exists)(const char *filename);
Simon Glasscbe5d5d2012-12-26 09:53:32 +000071 int (*read)(const char *filename, void *buf, int offset, int len);
Simon Glasseda14ea2013-04-20 08:42:50 +000072 int (*write)(const char *filename, void *buf, int offset, int len);
Simon Glass1aede482012-12-26 09:53:29 +000073 void (*close)(void);
74};
75
76static struct fstype_info fstypes[] = {
77#ifdef CONFIG_FS_FAT
Stephen Warreneefbc3f2012-10-22 06:43:51 +000078 {
79 .fstype = FS_TYPE_FAT,
Simon Glass19e38582012-12-26 09:53:33 +000080 .probe = fat_set_blk_dev,
81 .close = fat_close,
Simon Glass1aede482012-12-26 09:53:29 +000082 .ls = file_fat_ls,
Stephen Warrend008fbb2014-02-03 13:21:00 -070083 .exists = fs_exists_unsupported,
Simon Glass19e38582012-12-26 09:53:33 +000084 .read = fat_read_file,
Stephen Warren9df25802014-02-03 13:20:59 -070085 .write = fs_write_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +000086 },
Simon Glass1aede482012-12-26 09:53:29 +000087#endif
88#ifdef CONFIG_FS_EXT4
Stephen Warreneefbc3f2012-10-22 06:43:51 +000089 {
90 .fstype = FS_TYPE_EXT,
Simon Glass19e38582012-12-26 09:53:33 +000091 .probe = ext4fs_probe,
92 .close = ext4fs_close,
Simon Glass1aede482012-12-26 09:53:29 +000093 .ls = ext4fs_ls,
Stephen Warrend008fbb2014-02-03 13:21:00 -070094 .exists = fs_exists_unsupported,
Simon Glass19e38582012-12-26 09:53:33 +000095 .read = ext4_read_file,
Stephen Warren9df25802014-02-03 13:20:59 -070096 .write = fs_write_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +000097 },
98#endif
Simon Glass11842872012-12-26 09:53:35 +000099#ifdef CONFIG_SANDBOX
100 {
101 .fstype = FS_TYPE_SANDBOX,
102 .probe = sandbox_fs_set_blk_dev,
103 .close = sandbox_fs_close,
104 .ls = sandbox_fs_ls,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700105 .exists = fs_exists_unsupported,
Simon Glass11842872012-12-26 09:53:35 +0000106 .read = fs_read_sandbox,
Simon Glassea307e82013-04-20 08:42:51 +0000107 .write = fs_write_sandbox,
Simon Glass11842872012-12-26 09:53:35 +0000108 },
109#endif
Simon Glass1aede482012-12-26 09:53:29 +0000110 {
111 .fstype = FS_TYPE_ANY,
112 .probe = fs_probe_unsupported,
113 .close = fs_close_unsupported,
114 .ls = fs_ls_unsupported,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700115 .exists = fs_exists_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000116 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000117 .write = fs_write_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000118 },
119};
120
Simon Glasse6aad852012-12-26 09:53:30 +0000121static struct fstype_info *fs_get_info(int fstype)
122{
123 struct fstype_info *info;
124 int i;
125
126 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
127 if (fstype == info->fstype)
128 return info;
129 }
130
131 /* Return the 'unsupported' sentinel */
132 return info;
133}
134
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000135int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
136{
Simon Glass1aede482012-12-26 09:53:29 +0000137 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000138 int part, i;
Stephen Warren44161af2012-10-30 07:50:47 +0000139#ifdef CONFIG_NEEDS_MANUAL_RELOC
140 static int relocated;
141
142 if (!relocated) {
Simon Glass1aede482012-12-26 09:53:29 +0000143 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
144 i++, info++) {
145 info->probe += gd->reloc_off;
146 info->close += gd->reloc_off;
147 info->ls += gd->reloc_off;
148 info->read += gd->reloc_off;
Simon Glasseda14ea2013-04-20 08:42:50 +0000149 info->write += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000150 }
Stephen Warren44161af2012-10-30 07:50:47 +0000151 relocated = 1;
152 }
153#endif
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000154
155 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
156 &fs_partition, 1);
157 if (part < 0)
158 return -1;
159
Simon Glass1aede482012-12-26 09:53:29 +0000160 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
161 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
162 fstype != info->fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000163 continue;
164
Simon Glass6a46e2f2012-12-26 09:53:31 +0000165 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000166 fs_type = info->fstype;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000167 return 0;
168 }
169 }
170
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000171 return -1;
172}
173
174static void fs_close(void)
175{
Simon Glasse6aad852012-12-26 09:53:30 +0000176 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000177
Simon Glasse6aad852012-12-26 09:53:30 +0000178 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000179
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000180 fs_type = FS_TYPE_ANY;
181}
182
183int fs_ls(const char *dirname)
184{
185 int ret;
186
Simon Glasse6aad852012-12-26 09:53:30 +0000187 struct fstype_info *info = fs_get_info(fs_type);
188
189 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000190
Simon Glass19e38582012-12-26 09:53:33 +0000191 fs_type = FS_TYPE_ANY;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000192 fs_close();
193
194 return ret;
195}
196
Stephen Warrend008fbb2014-02-03 13:21:00 -0700197int fs_exists(const char *filename)
198{
199 int ret;
200
201 struct fstype_info *info = fs_get_info(fs_type);
202
203 ret = info->exists(filename);
204
205 fs_close();
206
207 return ret;
208}
209
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000210int fs_read(const char *filename, ulong addr, int offset, int len)
211{
Simon Glasse6aad852012-12-26 09:53:30 +0000212 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000213 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000214 int ret;
215
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000216 /*
217 * We don't actually know how many bytes are being read, since len==0
218 * means read the whole file.
219 */
220 buf = map_sysmem(addr, len);
221 ret = info->read(filename, buf, offset, len);
222 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000223
Simon Glasse6aad852012-12-26 09:53:30 +0000224 /* If we requested a specific number of bytes, check we got it */
225 if (ret >= 0 && len && ret != len) {
226 printf("** Unable to read file %s **\n", filename);
227 ret = -1;
228 }
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000229 fs_close();
230
231 return ret;
232}
233
Simon Glasseda14ea2013-04-20 08:42:50 +0000234int fs_write(const char *filename, ulong addr, int offset, int len)
235{
236 struct fstype_info *info = fs_get_info(fs_type);
237 void *buf;
238 int ret;
239
Simon Glasseda14ea2013-04-20 08:42:50 +0000240 buf = map_sysmem(addr, len);
241 ret = info->write(filename, buf, offset, len);
242 unmap_sysmem(buf);
243
Stephen Warren9df25802014-02-03 13:20:59 -0700244 if (ret >= 0 && ret != len) {
Simon Glasseda14ea2013-04-20 08:42:50 +0000245 printf("** Unable to write file %s **\n", filename);
246 ret = -1;
247 }
248 fs_close();
249
250 return ret;
251}
252
Stephen Warren128d3d92012-10-31 11:05:07 +0000253int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200254 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000255{
256 unsigned long addr;
257 const char *addr_str;
258 const char *filename;
259 unsigned long bytes;
260 unsigned long pos;
261 int len_read;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100262 unsigned long time;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000263
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000264 if (argc < 2)
265 return CMD_RET_USAGE;
266 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000267 return CMD_RET_USAGE;
268
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000269 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000270 return 1;
271
272 if (argc >= 4) {
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200273 addr = simple_strtoul(argv[3], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000274 } else {
275 addr_str = getenv("loadaddr");
276 if (addr_str != NULL)
277 addr = simple_strtoul(addr_str, NULL, 16);
278 else
279 addr = CONFIG_SYS_LOAD_ADDR;
280 }
281 if (argc >= 5) {
282 filename = argv[4];
283 } else {
284 filename = getenv("bootfile");
285 if (!filename) {
286 puts("** No boot file defined **\n");
287 return 1;
288 }
289 }
290 if (argc >= 6)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200291 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000292 else
293 bytes = 0;
294 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200295 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000296 else
297 pos = 0;
298
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100299 time = get_timer(0);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000300 len_read = fs_read(filename, addr, pos, bytes);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100301 time = get_timer(time);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000302 if (len_read <= 0)
303 return 1;
304
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100305 printf("%d bytes read in %lu ms", len_read, time);
306 if (time > 0) {
307 puts(" (");
308 print_size(len_read / time * 1000, "/s");
309 puts(")");
310 }
311 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000312
Simon Glassfff5c072013-02-24 17:33:23 +0000313 setenv_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000314
315 return 0;
316}
317
318int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
319 int fstype)
320{
321 if (argc < 2)
322 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000323 if (argc > 4)
324 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000325
326 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
327 return 1;
328
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000329 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000330 return 1;
331
332 return 0;
333}
Simon Glasseda14ea2013-04-20 08:42:50 +0000334
Stephen Warrend008fbb2014-02-03 13:21:00 -0700335int file_exists(const char *dev_type, const char *dev_part, const char *file,
336 int fstype)
337{
338 if (fs_set_blk_dev(dev_type, dev_part, fstype))
339 return 0;
340
341 return fs_exists(file);
342}
343
Simon Glasseda14ea2013-04-20 08:42:50 +0000344int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200345 int fstype)
Simon Glasseda14ea2013-04-20 08:42:50 +0000346{
347 unsigned long addr;
348 const char *filename;
349 unsigned long bytes;
350 unsigned long pos;
351 int len;
352 unsigned long time;
353
354 if (argc < 6 || argc > 7)
355 return CMD_RET_USAGE;
356
357 if (fs_set_blk_dev(argv[1], argv[2], fstype))
358 return 1;
359
360 filename = argv[3];
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200361 addr = simple_strtoul(argv[4], NULL, 16);
362 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000363 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200364 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000365 else
366 pos = 0;
367
368 time = get_timer(0);
369 len = fs_write(filename, addr, pos, bytes);
370 time = get_timer(time);
371 if (len <= 0)
372 return 1;
373
374 printf("%d bytes written in %lu ms", len, time);
375 if (time > 0) {
376 puts(" (");
377 print_size(len / time * 1000, "/s");
378 puts(")");
379 }
380 puts("\n");
381
382 return 0;
383}