blob: e4ad6bc3308d77dab534bc1fcd969ad68d037fb8 [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>
Christian Gmeiner59e890e2014-11-12 14:35:04 +010018#include <errno.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000019#include <common.h>
20#include <part.h>
21#include <ext4fs.h>
22#include <fat.h>
23#include <fs.h>
Simon Glass92ccc962012-12-26 09:53:35 +000024#include <sandboxfs.h>
Simon Glass117e0502012-12-26 09:53:32 +000025#include <asm/io.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000026
Stephen Warrena1b231c2012-10-30 07:50:47 +000027DECLARE_GLOBAL_DATA_PTR;
28
Stephen Warren045fa1e2012-10-22 06:43:51 +000029static block_dev_desc_t *fs_dev_desc;
30static disk_partition_t fs_partition;
31static int fs_type = FS_TYPE_ANY;
32
Simon Glass2ded0d42012-12-26 09:53:31 +000033static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
34 disk_partition_t *fs_partition)
Simon Glass436e2b72012-12-26 09:53:29 +000035{
36 printf("** Unrecognized filesystem type **\n");
37 return -1;
38}
39
Stephen Warren045fa1e2012-10-22 06:43:51 +000040static inline int fs_ls_unsupported(const char *dirname)
41{
Stephen Warren045fa1e2012-10-22 06:43:51 +000042 return -1;
43}
44
Stephen Warren61529162014-02-03 13:21:00 -070045static inline int fs_exists_unsupported(const char *filename)
46{
47 return 0;
48}
49
Stephen Warrencf659812014-06-11 12:47:26 -060050static inline int fs_size_unsupported(const char *filename)
51{
52 return -1;
53}
54
Simon Glass117e0502012-12-26 09:53:32 +000055static inline int fs_read_unsupported(const char *filename, void *buf,
Stephen Warren045fa1e2012-10-22 06:43:51 +000056 int offset, int len)
57{
Stephen Warren045fa1e2012-10-22 06:43:51 +000058 return -1;
59}
60
Simon Glassa8f6ab52013-04-20 08:42:50 +000061static inline int fs_write_unsupported(const char *filename, void *buf,
62 int offset, int len)
63{
64 return -1;
65}
66
Simon Glass436e2b72012-12-26 09:53:29 +000067static inline void fs_close_unsupported(void)
68{
69}
70
Christian Gmeiner59e890e2014-11-12 14:35:04 +010071static inline int fs_uuid_unsupported(char *uuid_str)
72{
73 return -1;
74}
75
Simon Glass436e2b72012-12-26 09:53:29 +000076struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +000077 int fstype;
Stephen Warren377202b2014-02-03 13:21:01 -070078 /*
79 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
80 * should be false in most cases. For "virtual" filesystems which
81 * aren't based on a U-Boot block device (e.g. sandbox), this can be
82 * set to true. This should also be true for the dumm entry at the end
83 * of fstypes[], since that is essentially a "virtual" (non-existent)
84 * filesystem.
85 */
86 bool null_dev_desc_ok;
Simon Glass2ded0d42012-12-26 09:53:31 +000087 int (*probe)(block_dev_desc_t *fs_dev_desc,
88 disk_partition_t *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +000089 int (*ls)(const char *dirname);
Stephen Warren61529162014-02-03 13:21:00 -070090 int (*exists)(const char *filename);
Stephen Warrencf659812014-06-11 12:47:26 -060091 int (*size)(const char *filename);
Simon Glass117e0502012-12-26 09:53:32 +000092 int (*read)(const char *filename, void *buf, int offset, int len);
Simon Glassa8f6ab52013-04-20 08:42:50 +000093 int (*write)(const char *filename, void *buf, int offset, int len);
Simon Glass436e2b72012-12-26 09:53:29 +000094 void (*close)(void);
Christian Gmeiner59e890e2014-11-12 14:35:04 +010095 int (*uuid)(char *uuid_str);
Simon Glass436e2b72012-12-26 09:53:29 +000096};
97
98static struct fstype_info fstypes[] = {
99#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000100 {
101 .fstype = FS_TYPE_FAT,
Stephen Warren377202b2014-02-03 13:21:01 -0700102 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000103 .probe = fat_set_blk_dev,
104 .close = fat_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000105 .ls = file_fat_ls,
Stephen Warrenb7b5f312014-02-03 13:21:10 -0700106 .exists = fat_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600107 .size = fat_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000108 .read = fat_read_file,
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700109 .write = fs_write_unsupported,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100110 .uuid = fs_uuid_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000111 },
Simon Glass436e2b72012-12-26 09:53:29 +0000112#endif
113#ifdef CONFIG_FS_EXT4
Stephen Warren045fa1e2012-10-22 06:43:51 +0000114 {
115 .fstype = FS_TYPE_EXT,
Stephen Warren377202b2014-02-03 13:21:01 -0700116 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000117 .probe = ext4fs_probe,
118 .close = ext4fs_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000119 .ls = ext4fs_ls,
Stephen Warren55af5c92014-02-03 13:21:09 -0700120 .exists = ext4fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600121 .size = ext4fs_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000122 .read = ext4_read_file,
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700123 .write = fs_write_unsupported,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100124 .uuid = ext4fs_uuid,
Simon Glass436e2b72012-12-26 09:53:29 +0000125 },
126#endif
Simon Glass92ccc962012-12-26 09:53:35 +0000127#ifdef CONFIG_SANDBOX
128 {
129 .fstype = FS_TYPE_SANDBOX,
Stephen Warren377202b2014-02-03 13:21:01 -0700130 .null_dev_desc_ok = true,
Simon Glass92ccc962012-12-26 09:53:35 +0000131 .probe = sandbox_fs_set_blk_dev,
132 .close = sandbox_fs_close,
133 .ls = sandbox_fs_ls,
Stephen Warren0a30aa12014-02-03 13:21:07 -0700134 .exists = sandbox_fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600135 .size = sandbox_fs_size,
Simon Glass92ccc962012-12-26 09:53:35 +0000136 .read = fs_read_sandbox,
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000137 .write = fs_write_sandbox,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100138 .uuid = fs_uuid_unsupported,
Simon Glass92ccc962012-12-26 09:53:35 +0000139 },
140#endif
Simon Glass436e2b72012-12-26 09:53:29 +0000141 {
142 .fstype = FS_TYPE_ANY,
Stephen Warren377202b2014-02-03 13:21:01 -0700143 .null_dev_desc_ok = true,
Simon Glass436e2b72012-12-26 09:53:29 +0000144 .probe = fs_probe_unsupported,
145 .close = fs_close_unsupported,
146 .ls = fs_ls_unsupported,
Stephen Warren61529162014-02-03 13:21:00 -0700147 .exists = fs_exists_unsupported,
Stephen Warrencf659812014-06-11 12:47:26 -0600148 .size = fs_size_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000149 .read = fs_read_unsupported,
Simon Glassa8f6ab52013-04-20 08:42:50 +0000150 .write = fs_write_unsupported,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100151 .uuid = fs_uuid_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000152 },
153};
154
Simon Glassc6f548d2012-12-26 09:53:30 +0000155static struct fstype_info *fs_get_info(int fstype)
156{
157 struct fstype_info *info;
158 int i;
159
160 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
161 if (fstype == info->fstype)
162 return info;
163 }
164
165 /* Return the 'unsupported' sentinel */
166 return info;
167}
168
Stephen Warren045fa1e2012-10-22 06:43:51 +0000169int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
170{
Simon Glass436e2b72012-12-26 09:53:29 +0000171 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000172 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000173#ifdef CONFIG_NEEDS_MANUAL_RELOC
174 static int relocated;
175
176 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000177 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
178 i++, info++) {
179 info->probe += gd->reloc_off;
180 info->close += gd->reloc_off;
181 info->ls += gd->reloc_off;
182 info->read += gd->reloc_off;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000183 info->write += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000184 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000185 relocated = 1;
186 }
187#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000188
189 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
190 &fs_partition, 1);
191 if (part < 0)
192 return -1;
193
Simon Glass436e2b72012-12-26 09:53:29 +0000194 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
195 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
196 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000197 continue;
198
Stephen Warren377202b2014-02-03 13:21:01 -0700199 if (!fs_dev_desc && !info->null_dev_desc_ok)
200 continue;
201
Simon Glass2ded0d42012-12-26 09:53:31 +0000202 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000203 fs_type = info->fstype;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000204 return 0;
205 }
206 }
207
Stephen Warren045fa1e2012-10-22 06:43:51 +0000208 return -1;
209}
210
211static void fs_close(void)
212{
Simon Glassc6f548d2012-12-26 09:53:30 +0000213 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000214
Simon Glassc6f548d2012-12-26 09:53:30 +0000215 info->close();
Simon Glasse6d52412012-12-26 09:53:33 +0000216
Stephen Warren045fa1e2012-10-22 06:43:51 +0000217 fs_type = FS_TYPE_ANY;
218}
219
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100220int fs_uuid(char *uuid_str)
221{
222 struct fstype_info *info = fs_get_info(fs_type);
223
224 return info->uuid(uuid_str);
225}
226
Stephen Warren045fa1e2012-10-22 06:43:51 +0000227int fs_ls(const char *dirname)
228{
229 int ret;
230
Simon Glassc6f548d2012-12-26 09:53:30 +0000231 struct fstype_info *info = fs_get_info(fs_type);
232
233 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000234
Simon Glasse6d52412012-12-26 09:53:33 +0000235 fs_type = FS_TYPE_ANY;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000236 fs_close();
237
238 return ret;
239}
240
Stephen Warren61529162014-02-03 13:21:00 -0700241int fs_exists(const char *filename)
242{
243 int ret;
244
245 struct fstype_info *info = fs_get_info(fs_type);
246
247 ret = info->exists(filename);
248
249 fs_close();
250
251 return ret;
252}
253
Stephen Warrencf659812014-06-11 12:47:26 -0600254int fs_size(const char *filename)
255{
256 int ret;
257
258 struct fstype_info *info = fs_get_info(fs_type);
259
260 ret = info->size(filename);
261
262 fs_close();
263
264 return ret;
265}
266
Stephen Warren045fa1e2012-10-22 06:43:51 +0000267int fs_read(const char *filename, ulong addr, int offset, int len)
268{
Simon Glassc6f548d2012-12-26 09:53:30 +0000269 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000270 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000271 int ret;
272
Simon Glass117e0502012-12-26 09:53:32 +0000273 /*
274 * We don't actually know how many bytes are being read, since len==0
275 * means read the whole file.
276 */
277 buf = map_sysmem(addr, len);
278 ret = info->read(filename, buf, offset, len);
279 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000280
Simon Glassc6f548d2012-12-26 09:53:30 +0000281 /* If we requested a specific number of bytes, check we got it */
282 if (ret >= 0 && len && ret != len) {
283 printf("** Unable to read file %s **\n", filename);
284 ret = -1;
285 }
Stephen Warren045fa1e2012-10-22 06:43:51 +0000286 fs_close();
287
288 return ret;
289}
290
Simon Glassa8f6ab52013-04-20 08:42:50 +0000291int fs_write(const char *filename, ulong addr, int offset, int len)
292{
293 struct fstype_info *info = fs_get_info(fs_type);
294 void *buf;
295 int ret;
296
Simon Glassa8f6ab52013-04-20 08:42:50 +0000297 buf = map_sysmem(addr, len);
298 ret = info->write(filename, buf, offset, len);
299 unmap_sysmem(buf);
300
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700301 if (ret >= 0 && ret != len) {
Simon Glassa8f6ab52013-04-20 08:42:50 +0000302 printf("** Unable to write file %s **\n", filename);
303 ret = -1;
304 }
305 fs_close();
306
307 return ret;
308}
309
Stephen Warrencf659812014-06-11 12:47:26 -0600310int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
311 int fstype)
312{
313 int size;
314
315 if (argc != 4)
316 return CMD_RET_USAGE;
317
318 if (fs_set_blk_dev(argv[1], argv[2], fstype))
319 return 1;
320
321 size = fs_size(argv[3]);
322 if (size < 0)
323 return CMD_RET_FAILURE;
324
325 setenv_hex("filesize", size);
326
327 return 0;
328}
329
Stephen Warrenf9b55e22012-10-31 11:05:07 +0000330int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200331 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000332{
333 unsigned long addr;
334 const char *addr_str;
335 const char *filename;
336 unsigned long bytes;
337 unsigned long pos;
338 int len_read;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100339 unsigned long time;
Pavel Machek949bbd72014-07-09 22:42:57 +0200340 char *ep;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000341
Stephen Warrene9b0f992012-10-30 12:04:17 +0000342 if (argc < 2)
343 return CMD_RET_USAGE;
344 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000345 return CMD_RET_USAGE;
346
Stephen Warrene9b0f992012-10-30 12:04:17 +0000347 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000348 return 1;
349
350 if (argc >= 4) {
Pavel Machek949bbd72014-07-09 22:42:57 +0200351 addr = simple_strtoul(argv[3], &ep, 16);
352 if (ep == argv[3] || *ep != '\0')
353 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000354 } else {
355 addr_str = getenv("loadaddr");
356 if (addr_str != NULL)
357 addr = simple_strtoul(addr_str, NULL, 16);
358 else
359 addr = CONFIG_SYS_LOAD_ADDR;
360 }
361 if (argc >= 5) {
362 filename = argv[4];
363 } else {
364 filename = getenv("bootfile");
365 if (!filename) {
366 puts("** No boot file defined **\n");
367 return 1;
368 }
369 }
370 if (argc >= 6)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200371 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000372 else
373 bytes = 0;
374 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200375 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000376 else
377 pos = 0;
378
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100379 time = get_timer(0);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000380 len_read = fs_read(filename, addr, pos, bytes);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100381 time = get_timer(time);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000382 if (len_read <= 0)
383 return 1;
384
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100385 printf("%d bytes read in %lu ms", len_read, time);
386 if (time > 0) {
387 puts(" (");
388 print_size(len_read / time * 1000, "/s");
389 puts(")");
390 }
391 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000392
Simon Glass49c4f032013-02-24 17:33:23 +0000393 setenv_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000394
395 return 0;
396}
397
398int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
399 int fstype)
400{
401 if (argc < 2)
402 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000403 if (argc > 4)
404 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000405
406 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
407 return 1;
408
Stephen Warrene9b0f992012-10-30 12:04:17 +0000409 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000410 return 1;
411
412 return 0;
413}
Simon Glassa8f6ab52013-04-20 08:42:50 +0000414
Stephen Warren61529162014-02-03 13:21:00 -0700415int file_exists(const char *dev_type, const char *dev_part, const char *file,
416 int fstype)
417{
418 if (fs_set_blk_dev(dev_type, dev_part, fstype))
419 return 0;
420
421 return fs_exists(file);
422}
423
Simon Glassa8f6ab52013-04-20 08:42:50 +0000424int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200425 int fstype)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000426{
427 unsigned long addr;
428 const char *filename;
429 unsigned long bytes;
430 unsigned long pos;
431 int len;
432 unsigned long time;
433
434 if (argc < 6 || argc > 7)
435 return CMD_RET_USAGE;
436
437 if (fs_set_blk_dev(argv[1], argv[2], fstype))
438 return 1;
439
440 filename = argv[3];
Wolfgang Denkb770e882013-10-05 21:07:25 +0200441 addr = simple_strtoul(argv[4], NULL, 16);
442 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000443 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200444 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000445 else
446 pos = 0;
447
448 time = get_timer(0);
449 len = fs_write(filename, addr, pos, bytes);
450 time = get_timer(time);
451 if (len <= 0)
452 return 1;
453
454 printf("%d bytes written in %lu ms", len, time);
455 if (time > 0) {
456 puts(" (");
457 print_size(len / time * 1000, "/s");
458 puts(")");
459 }
460 puts("\n");
461
462 return 0;
463}
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100464
465int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
466 int fstype)
467{
468 int ret;
469 char uuid[37];
470 memset(uuid, 0, sizeof(uuid));
471
472 if (argc < 3 || argc > 4)
473 return CMD_RET_USAGE;
474
475 if (fs_set_blk_dev(argv[1], argv[2], fstype))
476 return 1;
477
478 ret = fs_uuid(uuid);
479 if (ret)
480 return CMD_RET_FAILURE;
481
482 if (argc == 4)
483 setenv(argv[3], uuid);
484 else
485 printf("%s\n", uuid);
486
487 return CMD_RET_SUCCESS;
488}