blob: dd680f39c9ca60472ea91f37a7a325f95da3b672 [file] [log] [blame]
Stephen Warren045fa1e2012-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 Glass92ccc962012-12-26 09:53:35 +000023#include <sandboxfs.h>
Simon Glass117e0502012-12-26 09:53:32 +000024#include <asm/io.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000025
Stephen Warrena1b231c2012-10-30 07:50:47 +000026DECLARE_GLOBAL_DATA_PTR;
27
Stephen Warren045fa1e2012-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 Glass2ded0d42012-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 Glass436e2b72012-12-26 09:53:29 +000034{
35 printf("** Unrecognized filesystem type **\n");
36 return -1;
37}
38
Stephen Warren045fa1e2012-10-22 06:43:51 +000039static inline int fs_ls_unsupported(const char *dirname)
40{
Stephen Warren045fa1e2012-10-22 06:43:51 +000041 return -1;
42}
43
Stephen Warren61529162014-02-03 13:21:00 -070044static inline int fs_exists_unsupported(const char *filename)
45{
46 return 0;
47}
48
Stephen Warrencf659812014-06-11 12:47:26 -060049static inline int fs_size_unsupported(const char *filename)
50{
51 return -1;
52}
53
Simon Glass117e0502012-12-26 09:53:32 +000054static inline int fs_read_unsupported(const char *filename, void *buf,
Stephen Warren045fa1e2012-10-22 06:43:51 +000055 int offset, int len)
56{
Stephen Warren045fa1e2012-10-22 06:43:51 +000057 return -1;
58}
59
Simon Glassa8f6ab52013-04-20 08:42:50 +000060static inline int fs_write_unsupported(const char *filename, void *buf,
61 int offset, int len)
62{
63 return -1;
64}
65
Simon Glass436e2b72012-12-26 09:53:29 +000066static inline void fs_close_unsupported(void)
67{
68}
69
Simon Glass436e2b72012-12-26 09:53:29 +000070struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +000071 int fstype;
Stephen Warren377202b2014-02-03 13:21:01 -070072 /*
73 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
74 * should be false in most cases. For "virtual" filesystems which
75 * aren't based on a U-Boot block device (e.g. sandbox), this can be
76 * set to true. This should also be true for the dumm entry at the end
77 * of fstypes[], since that is essentially a "virtual" (non-existent)
78 * filesystem.
79 */
80 bool null_dev_desc_ok;
Simon Glass2ded0d42012-12-26 09:53:31 +000081 int (*probe)(block_dev_desc_t *fs_dev_desc,
82 disk_partition_t *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +000083 int (*ls)(const char *dirname);
Stephen Warren61529162014-02-03 13:21:00 -070084 int (*exists)(const char *filename);
Stephen Warrencf659812014-06-11 12:47:26 -060085 int (*size)(const char *filename);
Simon Glass117e0502012-12-26 09:53:32 +000086 int (*read)(const char *filename, void *buf, int offset, int len);
Simon Glassa8f6ab52013-04-20 08:42:50 +000087 int (*write)(const char *filename, void *buf, int offset, int len);
Simon Glass436e2b72012-12-26 09:53:29 +000088 void (*close)(void);
89};
90
91static struct fstype_info fstypes[] = {
92#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +000093 {
94 .fstype = FS_TYPE_FAT,
Stephen Warren377202b2014-02-03 13:21:01 -070095 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +000096 .probe = fat_set_blk_dev,
97 .close = fat_close,
Simon Glass436e2b72012-12-26 09:53:29 +000098 .ls = file_fat_ls,
Stephen Warrenb7b5f312014-02-03 13:21:10 -070099 .exists = fat_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600100 .size = fat_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000101 .read = fat_read_file,
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700102 .write = fs_write_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000103 },
Simon Glass436e2b72012-12-26 09:53:29 +0000104#endif
105#ifdef CONFIG_FS_EXT4
Stephen Warren045fa1e2012-10-22 06:43:51 +0000106 {
107 .fstype = FS_TYPE_EXT,
Stephen Warren377202b2014-02-03 13:21:01 -0700108 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000109 .probe = ext4fs_probe,
110 .close = ext4fs_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000111 .ls = ext4fs_ls,
Stephen Warren55af5c92014-02-03 13:21:09 -0700112 .exists = ext4fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600113 .size = ext4fs_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000114 .read = ext4_read_file,
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700115 .write = fs_write_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000116 },
117#endif
Simon Glass92ccc962012-12-26 09:53:35 +0000118#ifdef CONFIG_SANDBOX
119 {
120 .fstype = FS_TYPE_SANDBOX,
Stephen Warren377202b2014-02-03 13:21:01 -0700121 .null_dev_desc_ok = true,
Simon Glass92ccc962012-12-26 09:53:35 +0000122 .probe = sandbox_fs_set_blk_dev,
123 .close = sandbox_fs_close,
124 .ls = sandbox_fs_ls,
Stephen Warren0a30aa12014-02-03 13:21:07 -0700125 .exists = sandbox_fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600126 .size = sandbox_fs_size,
Simon Glass92ccc962012-12-26 09:53:35 +0000127 .read = fs_read_sandbox,
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000128 .write = fs_write_sandbox,
Simon Glass92ccc962012-12-26 09:53:35 +0000129 },
130#endif
Simon Glass436e2b72012-12-26 09:53:29 +0000131 {
132 .fstype = FS_TYPE_ANY,
Stephen Warren377202b2014-02-03 13:21:01 -0700133 .null_dev_desc_ok = true,
Simon Glass436e2b72012-12-26 09:53:29 +0000134 .probe = fs_probe_unsupported,
135 .close = fs_close_unsupported,
136 .ls = fs_ls_unsupported,
Stephen Warren61529162014-02-03 13:21:00 -0700137 .exists = fs_exists_unsupported,
Stephen Warrencf659812014-06-11 12:47:26 -0600138 .size = fs_size_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000139 .read = fs_read_unsupported,
Simon Glassa8f6ab52013-04-20 08:42:50 +0000140 .write = fs_write_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000141 },
142};
143
Simon Glassc6f548d2012-12-26 09:53:30 +0000144static struct fstype_info *fs_get_info(int fstype)
145{
146 struct fstype_info *info;
147 int i;
148
149 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
150 if (fstype == info->fstype)
151 return info;
152 }
153
154 /* Return the 'unsupported' sentinel */
155 return info;
156}
157
Stephen Warren045fa1e2012-10-22 06:43:51 +0000158int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
159{
Simon Glass436e2b72012-12-26 09:53:29 +0000160 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000161 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000162#ifdef CONFIG_NEEDS_MANUAL_RELOC
163 static int relocated;
164
165 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000166 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
167 i++, info++) {
168 info->probe += gd->reloc_off;
169 info->close += gd->reloc_off;
170 info->ls += gd->reloc_off;
171 info->read += gd->reloc_off;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000172 info->write += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000173 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000174 relocated = 1;
175 }
176#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000177
178 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
179 &fs_partition, 1);
180 if (part < 0)
181 return -1;
182
Simon Glass436e2b72012-12-26 09:53:29 +0000183 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
184 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
185 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000186 continue;
187
Stephen Warren377202b2014-02-03 13:21:01 -0700188 if (!fs_dev_desc && !info->null_dev_desc_ok)
189 continue;
190
Simon Glass2ded0d42012-12-26 09:53:31 +0000191 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000192 fs_type = info->fstype;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000193 return 0;
194 }
195 }
196
Stephen Warren045fa1e2012-10-22 06:43:51 +0000197 return -1;
198}
199
200static void fs_close(void)
201{
Simon Glassc6f548d2012-12-26 09:53:30 +0000202 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000203
Simon Glassc6f548d2012-12-26 09:53:30 +0000204 info->close();
Simon Glasse6d52412012-12-26 09:53:33 +0000205
Stephen Warren045fa1e2012-10-22 06:43:51 +0000206 fs_type = FS_TYPE_ANY;
207}
208
209int fs_ls(const char *dirname)
210{
211 int ret;
212
Simon Glassc6f548d2012-12-26 09:53:30 +0000213 struct fstype_info *info = fs_get_info(fs_type);
214
215 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000216
Simon Glasse6d52412012-12-26 09:53:33 +0000217 fs_type = FS_TYPE_ANY;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000218 fs_close();
219
220 return ret;
221}
222
Stephen Warren61529162014-02-03 13:21:00 -0700223int fs_exists(const char *filename)
224{
225 int ret;
226
227 struct fstype_info *info = fs_get_info(fs_type);
228
229 ret = info->exists(filename);
230
231 fs_close();
232
233 return ret;
234}
235
Stephen Warrencf659812014-06-11 12:47:26 -0600236int fs_size(const char *filename)
237{
238 int ret;
239
240 struct fstype_info *info = fs_get_info(fs_type);
241
242 ret = info->size(filename);
243
244 fs_close();
245
246 return ret;
247}
248
Stephen Warren045fa1e2012-10-22 06:43:51 +0000249int fs_read(const char *filename, ulong addr, int offset, int len)
250{
Simon Glassc6f548d2012-12-26 09:53:30 +0000251 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000252 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000253 int ret;
254
Simon Glass117e0502012-12-26 09:53:32 +0000255 /*
256 * We don't actually know how many bytes are being read, since len==0
257 * means read the whole file.
258 */
259 buf = map_sysmem(addr, len);
260 ret = info->read(filename, buf, offset, len);
261 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000262
Simon Glassc6f548d2012-12-26 09:53:30 +0000263 /* If we requested a specific number of bytes, check we got it */
264 if (ret >= 0 && len && ret != len) {
265 printf("** Unable to read file %s **\n", filename);
266 ret = -1;
267 }
Stephen Warren045fa1e2012-10-22 06:43:51 +0000268 fs_close();
269
270 return ret;
271}
272
Simon Glassa8f6ab52013-04-20 08:42:50 +0000273int fs_write(const char *filename, ulong addr, int offset, int len)
274{
275 struct fstype_info *info = fs_get_info(fs_type);
276 void *buf;
277 int ret;
278
Simon Glassa8f6ab52013-04-20 08:42:50 +0000279 buf = map_sysmem(addr, len);
280 ret = info->write(filename, buf, offset, len);
281 unmap_sysmem(buf);
282
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700283 if (ret >= 0 && ret != len) {
Simon Glassa8f6ab52013-04-20 08:42:50 +0000284 printf("** Unable to write file %s **\n", filename);
285 ret = -1;
286 }
287 fs_close();
288
289 return ret;
290}
291
Stephen Warrencf659812014-06-11 12:47:26 -0600292int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
293 int fstype)
294{
295 int size;
296
297 if (argc != 4)
298 return CMD_RET_USAGE;
299
300 if (fs_set_blk_dev(argv[1], argv[2], fstype))
301 return 1;
302
303 size = fs_size(argv[3]);
304 if (size < 0)
305 return CMD_RET_FAILURE;
306
307 setenv_hex("filesize", size);
308
309 return 0;
310}
311
Stephen Warrenf9b55e22012-10-31 11:05:07 +0000312int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200313 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000314{
315 unsigned long addr;
316 const char *addr_str;
317 const char *filename;
318 unsigned long bytes;
319 unsigned long pos;
320 int len_read;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100321 unsigned long time;
Pavel Machek949bbd72014-07-09 22:42:57 +0200322 char *ep;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000323
Stephen Warrene9b0f992012-10-30 12:04:17 +0000324 if (argc < 2)
325 return CMD_RET_USAGE;
326 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000327 return CMD_RET_USAGE;
328
Stephen Warrene9b0f992012-10-30 12:04:17 +0000329 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000330 return 1;
331
332 if (argc >= 4) {
Pavel Machek949bbd72014-07-09 22:42:57 +0200333 addr = simple_strtoul(argv[3], &ep, 16);
334 if (ep == argv[3] || *ep != '\0')
335 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000336 } else {
337 addr_str = getenv("loadaddr");
338 if (addr_str != NULL)
339 addr = simple_strtoul(addr_str, NULL, 16);
340 else
341 addr = CONFIG_SYS_LOAD_ADDR;
342 }
343 if (argc >= 5) {
344 filename = argv[4];
345 } else {
346 filename = getenv("bootfile");
347 if (!filename) {
348 puts("** No boot file defined **\n");
349 return 1;
350 }
351 }
352 if (argc >= 6)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200353 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000354 else
355 bytes = 0;
356 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200357 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000358 else
359 pos = 0;
360
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100361 time = get_timer(0);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000362 len_read = fs_read(filename, addr, pos, bytes);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100363 time = get_timer(time);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000364 if (len_read <= 0)
365 return 1;
366
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100367 printf("%d bytes read in %lu ms", len_read, time);
368 if (time > 0) {
369 puts(" (");
370 print_size(len_read / time * 1000, "/s");
371 puts(")");
372 }
373 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000374
Simon Glass49c4f032013-02-24 17:33:23 +0000375 setenv_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000376
377 return 0;
378}
379
380int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
381 int fstype)
382{
383 if (argc < 2)
384 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000385 if (argc > 4)
386 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000387
388 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
389 return 1;
390
Stephen Warrene9b0f992012-10-30 12:04:17 +0000391 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000392 return 1;
393
394 return 0;
395}
Simon Glassa8f6ab52013-04-20 08:42:50 +0000396
Stephen Warren61529162014-02-03 13:21:00 -0700397int file_exists(const char *dev_type, const char *dev_part, const char *file,
398 int fstype)
399{
400 if (fs_set_blk_dev(dev_type, dev_part, fstype))
401 return 0;
402
403 return fs_exists(file);
404}
405
Simon Glassa8f6ab52013-04-20 08:42:50 +0000406int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200407 int fstype)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000408{
409 unsigned long addr;
410 const char *filename;
411 unsigned long bytes;
412 unsigned long pos;
413 int len;
414 unsigned long time;
415
416 if (argc < 6 || argc > 7)
417 return CMD_RET_USAGE;
418
419 if (fs_set_blk_dev(argv[1], argv[2], fstype))
420 return 1;
421
422 filename = argv[3];
Wolfgang Denkb770e882013-10-05 21:07:25 +0200423 addr = simple_strtoul(argv[4], NULL, 16);
424 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000425 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200426 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000427 else
428 pos = 0;
429
430 time = get_timer(0);
431 len = fs_write(filename, addr, pos, bytes);
432 time = get_timer(time);
433 if (len <= 0)
434 return 1;
435
436 printf("%d bytes written in %lu ms", len, time);
437 if (time > 0) {
438 puts(" (");
439 print_size(len / time * 1000, "/s");
440 puts(")");
441 }
442 puts("\n");
443
444 return 0;
445}